diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2018-11-02 14:09:23 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2018-11-02 14:09:23 +0100 |
commit | 8848b0e22aaba6f3d7116c7137ede3b43f393a85 (patch) | |
tree | 65ffb8531d608f899bffcd47bc8721ec059cdc26 | |
parent | 74a17d821f066c41f1450e9fae805c1711482265 (diff) | |
download | weechat-8848b0e22aaba6f3d7116c7137ede3b43f393a85.zip |
api: return integer in function string_encode_base64
-rw-r--r-- | ChangeLog.adoc | 1 | ||||
-rw-r--r-- | doc/en/weechat_plugin_api.en.adoc | 13 | ||||
-rw-r--r-- | doc/fr/weechat_plugin_api.fr.adoc | 13 | ||||
-rw-r--r-- | doc/it/weechat_plugin_api.it.adoc | 14 | ||||
-rw-r--r-- | doc/ja/weechat_plugin_api.ja.adoc | 14 | ||||
-rw-r--r-- | src/core/wee-network.c | 3 | ||||
-rw-r--r-- | src/core/wee-string.c | 40 | ||||
-rw-r--r-- | src/core/wee-string.h | 2 | ||||
-rw-r--r-- | src/gui/curses/gui-curses-window.c | 12 | ||||
-rw-r--r-- | src/plugins/irc/irc-sasl.c | 55 | ||||
-rw-r--r-- | src/plugins/relay/relay-websocket.c | 6 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 4 | ||||
-rw-r--r-- | tests/unit/core/test-string.cpp | 45 |
13 files changed, 137 insertions, 85 deletions
diff --git a/ChangeLog.adoc b/ChangeLog.adoc index c14bd9cb0..2cf2262ad 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -20,6 +20,7 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes] New features:: + * api: return integer in function string_encode_base64 * api: add support of Time-based One-Time Password (TOTP), add infos "totp_generate" and "totp_validate" Bug fixes:: diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc index 056e97c22..9ecbc9b81 100644 --- a/doc/en/weechat_plugin_api.en.adoc +++ b/doc/en/weechat_plugin_api.en.adoc @@ -1780,7 +1780,7 @@ str = weechat.string_remove_color(my_string, "?") ==== string_encode_base64 -_WeeChat ≥ 0.3.2._ +_WeeChat ≥ 0.3.2, updated in 2.4._ Encode a string in base64. @@ -1788,7 +1788,7 @@ Prototype: [source,C] ---- -void weechat_string_encode_base64 (const char *from, int length, char *to); +int weechat_string_encode_base64 (const char *from, int length, char *to); ---- Arguments: @@ -1798,13 +1798,18 @@ Arguments: * _to_: pointer to string to store result (must be long enough, result is longer than initial string) +Return value: + +* length of string stored in _*to_ (does not count final _\0_) + C example: [source,C] ---- char *string = "abcdefgh", result[128]; -weechat_string_encode_base64 (string, strlen (string), result); -/* result == "YWJjZGVmZ2g=" */ +int length; +length = weechat_string_encode_base64 (string, strlen (string), result); +/* length == 12, result == "YWJjZGVmZ2g=" */ ---- [NOTE] diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc index c2a6815a2..5f3181dac 100644 --- a/doc/fr/weechat_plugin_api.fr.adoc +++ b/doc/fr/weechat_plugin_api.fr.adoc @@ -1814,7 +1814,7 @@ str = weechat.string_remove_color(ma_chaine, "?") ==== string_encode_base64 -_WeeChat ≥ 0.3.2._ +_WeeChat ≥ 0.3.2, mis à jour dans la 2.4._ Encoder une chaîne en base64. @@ -1822,7 +1822,7 @@ Prototype : [source,C] ---- -void weechat_string_encode_base64 (const char *from, int length, char *to); +int weechat_string_encode_base64 (const char *from, int length, char *to); ---- Paramètres : @@ -1832,13 +1832,18 @@ Paramètres : * _to_ : pointeur vers la chaîne pour stocker le résultat (doit être suffisamment long, le résultat est plus long que la chaîne initiale) +Valeur de retour : + +* longueur de la chaîne stockée dans _*to_ (ne compte pas le _\0_ final) + Exemple en C : [source,C] ---- char *string = "abcdefgh", result[128]; -weechat_string_encode_base64 (string, strlen (string), result); -/* result == "YWJjZGVmZ2g=" */ +int length; +length = weechat_string_encode_base64 (string, strlen (string), result); +/* length == 12, result == "YWJjZGVmZ2g=" */ ---- [NOTE] diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc index d8cd71367..a0dd0988b 100644 --- a/doc/it/weechat_plugin_api.it.adoc +++ b/doc/it/weechat_plugin_api.it.adoc @@ -1853,7 +1853,8 @@ str = weechat.string_remove_color(my_string, "?") ==== string_encode_base64 -_WeeChat ≥ 0.3.2._ +// TRANSLATION MISSING +_WeeChat ≥ 0.3.2, updated in 2.4._ Codifica una stringa in base64. @@ -1861,7 +1862,7 @@ Prototipo: [source,C] ---- -void weechat_string_encode_base64 (const char *from, int length, char *to); +int weechat_string_encode_base64 (const char *from, int length, char *to); ---- Argomenti: @@ -1871,13 +1872,18 @@ Argomenti: * _to_: puntatore alla stringa per memorizzare il risultato (deve essere sufficientemente lunga, il risultato è più lungo della stringa iniziale) +Valore restituito: + +* lunghezza della stringa memorizzata in _*to_ (lo _\0_ finale non conta) + Esempio in C: [source,C] ---- char *string = "abcdefgh", result[128]; -weechat_string_encode_base64 (string, strlen (string), result); -/* result == "YWJjZGVmZ2g=" */ +int length; +length = weechat_string_encode_base64 (string, strlen (string), result); +/* length == 12, result == "YWJjZGVmZ2g=" */ ---- [NOTE] diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc index c2067916e..5ac11dfee 100644 --- a/doc/ja/weechat_plugin_api.ja.adoc +++ b/doc/ja/weechat_plugin_api.ja.adoc @@ -1786,7 +1786,8 @@ str = weechat.string_remove_color(my_string, "?") ==== string_encode_base64 -_WeeChat バージョン 0.3.2 以上で利用可。_ +// TRANSLATION MISSING +_WeeChat ≥ 0.3.2, updated in 2.4._ 文字列を base64 でエンコード。 @@ -1794,7 +1795,7 @@ _WeeChat バージョン 0.3.2 以上で利用可。_ [source,C] ---- -void weechat_string_encode_base64 (const char *from, int length, char *to); +int weechat_string_encode_base64 (const char *from, int length, char *to); ---- 引数: @@ -1804,13 +1805,18 @@ void weechat_string_encode_base64 (const char *from, int length, char *to); * _to_: エンコード結果を保存する文字列へのポインタ (十分な領域を確保してください、結果はエンコード元文字列よりも長くなります) +戻り値: + +* _*to_ に保存された文字列の長さ (最後の _\0_ は数えません) + C 言語での使用例: [source,C] ---- char *string = "abcdefgh", result[128]; -weechat_string_encode_base64 (string, strlen (string), result); -/* result == "YWJjZGVmZ2g=" */ +int length; +length = weechat_string_encode_base64 (string, strlen (string), result); +/* length == 12, result == "YWJjZGVmZ2g=" */ ---- [NOTE] diff --git a/src/core/wee-network.c b/src/core/wee-network.c index e04b0dce5..dcb8e174d 100644 --- a/src/core/wee-network.c +++ b/src/core/wee-network.c @@ -267,7 +267,8 @@ network_pass_httpproxy (struct t_proxy *proxy, int sock, const char *address, snprintf (authbuf, sizeof (authbuf), "%s:%s", username, password); free (username); free (password); - string_encode_base64 (authbuf, strlen (authbuf), authbuf_base64); + if (string_encode_base64 (authbuf, strlen (authbuf), authbuf_base64) < 0) + return 0; length = snprintf (buffer, sizeof (buffer), "CONNECT %s:%d HTTP/1.0\r\nProxy-Authorization: " "Basic %s\r\n\r\n", diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 8ff1de3de..c7a83ad5e 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -2953,49 +2953,61 @@ string_convbase64_8x3_to_6x4 (const char *from, char *to) * * Argument "length" is number of bytes in "from" to convert (commonly * strlen(from)). + * + * Returns length of string in "*to" (it does not count final \0). */ -void +int string_encode_base64 (const char *from, int length, char *to) { const char *ptr_from; - char *ptr_to; + char rest[3]; + int count; if (!from || !to) - return; + return -1; ptr_from = from; - ptr_to = to; + count = 0; while (length >= 3) { - string_convbase64_8x3_to_6x4 (ptr_from, ptr_to); - ptr_from += 3 * sizeof (*ptr_from); - ptr_to += 4 * sizeof (*ptr_to); + string_convbase64_8x3_to_6x4 (ptr_from, to + count); + ptr_from += 3; + count += 4; length -= 3; } if (length > 0) { - char rest[3] = { 0, 0, 0 }; + rest[0] = 0; + rest[1] = 0; + rest[2] = 0; switch (length) { case 1 : rest[0] = ptr_from[0]; - string_convbase64_8x3_to_6x4 (rest, ptr_to); - ptr_to[2] = ptr_to[3] = '='; + string_convbase64_8x3_to_6x4 (rest, to + count); + count += 2; + to[count] = '='; + count++; + to[count] = '='; break; case 2 : rest[0] = ptr_from[0]; rest[1] = ptr_from[1]; - string_convbase64_8x3_to_6x4 (rest, ptr_to); - ptr_to[3] = '='; + string_convbase64_8x3_to_6x4 (rest, to + count); + count += 3; + to[count] = '='; break; } - ptr_to[4] = 0; + count++; + to[count] = '\0'; } else - ptr_to[0] = '\0'; + to[count] = '\0'; + + return count; } /* diff --git a/src/core/wee-string.h b/src/core/wee-string.h index 7e33352c8..2f9be0e7e 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -109,7 +109,7 @@ extern void string_encode_base16 (const char *from, int length, char *to); extern int string_decode_base16 (const char *from, char *to); extern int string_encode_base32 (const char *from, int length, char *to); extern int string_decode_base32 (const char *from, char *to); -extern void string_encode_base64 (const char *from, int length, char *to); +extern int string_encode_base64 (const char *from, int length, char *to); extern int string_decode_base64 (const char *from, char *to); extern char *string_hex_dump (const char *data, int data_size, int bytes_per_line, diff --git a/src/gui/curses/gui-curses-window.c b/src/gui/curses/gui-curses-window.c index 3b410dfd7..e9ff4aae3 100644 --- a/src/gui/curses/gui-curses-window.c +++ b/src/gui/curses/gui-curses-window.c @@ -2509,11 +2509,13 @@ gui_window_send_clipboard (const char *storage_unit, const char *text) text_base64 = malloc ((length * 4) + 1); if (text_base64) { - string_encode_base64 (text, length, text_base64); - fprintf (stderr, "\033]52;%s;%s\a", - (storage_unit) ? storage_unit : "", - text_base64); - fflush (stderr); + if (string_encode_base64 (text, length, text_base64) >= 0) + { + fprintf (stderr, "\033]52;%s;%s\a", + (storage_unit) ? storage_unit : "", + text_base64); + fflush (stderr); + } free (text_base64); } } diff --git a/src/plugins/irc/irc-sasl.c b/src/plugins/irc/irc-sasl.c index 689e7ecb8..63d9bf6e5 100644 --- a/src/plugins/irc/irc-sasl.c +++ b/src/plugins/irc/irc-sasl.c @@ -72,7 +72,14 @@ irc_sasl_mechanism_plain (const char *sasl_username, const char *sasl_password) answer_base64 = malloc (length * 4); if (answer_base64) - weechat_string_encode_base64 (string, length - 1, answer_base64); + { + if (weechat_string_encode_base64 (string, length - 1, + answer_base64) < 0) + { + free (answer_base64); + answer_base64 = NULL; + } + } free (string); } @@ -219,15 +226,17 @@ irc_sasl_mechanism_ecdsa_nist256p_challenge (struct t_irc_server *server, pubkey_base64 = malloc ((x.size + 1 + 1) * 4); if (pubkey_base64) { - weechat_string_encode_base64 (pubkey, x.size + 1, - pubkey_base64); - weechat_printf ( - server->buffer, - _("%s%s: signing the challenge with ECC public key: " - "%s"), - weechat_prefix ("network"), - IRC_PLUGIN_NAME, - pubkey_base64); + if (weechat_string_encode_base64 (pubkey, x.size + 1, + pubkey_base64) >= 0) + { + weechat_printf ( + server->buffer, + _("%s%s: signing the challenge with ECC public " + "key: %s"), + weechat_prefix ("network"), + IRC_PLUGIN_NAME, + pubkey_base64); + } free (pubkey_base64); } free (pubkey); @@ -289,7 +298,13 @@ irc_sasl_mechanism_ecdsa_nist256p_challenge (struct t_irc_server *server, { answer_base64 = malloc ((length + 1) * 4); if (answer_base64) - weechat_string_encode_base64 (string, length, answer_base64); + { + if (weechat_string_encode_base64 (string, length, answer_base64) < 0) + { + free (answer_base64); + answer_base64 = NULL; + } + } free (string); string = NULL; } @@ -504,7 +519,14 @@ irc_sasl_mechanism_dh_blowfish (const char *data_base64, /* encode answer to base64 */ answer_base64 = malloc ((length_answer + 1) * 4); if (answer_base64) - weechat_string_encode_base64 (answer, length_answer, answer_base64); + { + if (weechat_string_encode_base64 (answer, length_answer, + answer_base64) < 0) + { + free (answer_base64); + answer_base64 = NULL; + } + } bfend: if (secret_bin) @@ -626,7 +648,14 @@ irc_sasl_mechanism_dh_aes (const char *data_base64, /* encode answer to base64 */ answer_base64 = malloc ((length_answer + 1) * 4); if (answer_base64) - weechat_string_encode_base64 (answer, length_answer, answer_base64); + { + if (weechat_string_encode_base64 (answer, length_answer, + answer_base64) < 0) + { + free (answer_base64); + answer_base64 = NULL; + } + } aesend: if (secret_bin) diff --git a/src/plugins/relay/relay-websocket.c b/src/plugins/relay/relay-websocket.c index bbac751dc..60c4f4526 100644 --- a/src/plugins/relay/relay-websocket.c +++ b/src/plugins/relay/relay-websocket.c @@ -214,7 +214,11 @@ relay_websocket_build_handshake (struct t_relay_client *client) length = gcry_md_get_algo_dlen (GCRY_MD_SHA1); gcry_md_write (hd, key, strlen (key)); result = gcry_md_read (hd, GCRY_MD_SHA1); - weechat_string_encode_base64 ((char *)result, length, sec_websocket_accept); + if (weechat_string_encode_base64 ((char *)result, length, + sec_websocket_accept) < 0) + { + sec_websocket_accept[0] = '\0'; + } gcry_md_close (hd); free (key); diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index e9416d2ce..94aa54598 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -67,7 +67,7 @@ struct timeval; * please change the date with current one; for a second change at same * date, increment the 01, otherwise please keep 01. */ -#define WEECHAT_PLUGIN_API_VERSION "20180812-01" +#define WEECHAT_PLUGIN_API_VERSION "20181102-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -323,7 +323,7 @@ struct t_weechat_plugin void (*string_free_split_command) (char **split_command); char *(*string_format_size) (unsigned long long size); char *(*string_remove_color) (const char *string, const char *replacement); - void (*string_encode_base64) (const char *from, int length, char *to); + int (*string_encode_base64) (const char *from, int length, char *to); int (*string_decode_base64) (const char *from, char *to); char *(*string_hex_dump) (const char *data, int data_size, int bytes_per_line, const char *prefix, diff --git a/tests/unit/core/test-string.cpp b/tests/unit/core/test-string.cpp index 068ea1ed4..c9e6ac431 100644 --- a/tests/unit/core/test-string.cpp +++ b/tests/unit/core/test-string.cpp @@ -1427,8 +1427,9 @@ TEST(CoreString, Base32) * string_decode_base64 */ -TEST(CoreString, BaseN) +TEST(CoreString, Base64) { + int i, length; char str[1024]; const char *str_base64[][2] = { { "", "" }, @@ -1452,43 +1453,23 @@ TEST(CoreString, BaseN) { "Another example for base64...", "QW5vdGhlciBleGFtcGxlIGZvciBiYXNlNjQuLi4=" }, { NULL, NULL } }; - int i, length; - - /* string_encode_base16 */ - string_encode_base16 (NULL, 0, NULL); - string_encode_base16 (NULL, 0, str); - string_encode_base16 ("", 0, NULL); - str[0] = 0xAA; - string_encode_base16 ("", -1, str); - BYTES_EQUAL(0x0, str[0]); - str[0] = 0xAA; - string_encode_base16 ("", 0, str); - BYTES_EQUAL(0x0, str[0]); - string_encode_base16 ("abc", 3, str); - STRCMP_EQUAL("616263", str); - - /* string_decode_base16 */ - LONGS_EQUAL(0, string_decode_base16 (NULL, NULL)); - LONGS_EQUAL(0, string_decode_base16 (NULL, str)); - LONGS_EQUAL(0, string_decode_base16 ("", NULL)); - LONGS_EQUAL(0, string_decode_base16 ("", str)); - LONGS_EQUAL(3, string_decode_base16 ("616263", str)); - STRCMP_EQUAL("abc", str); /* string_encode_base64 */ - string_encode_base64 (NULL, 0, NULL); - string_encode_base64 (NULL, 0, str); - string_encode_base64 ("", 0, NULL); + LONGS_EQUAL(-1, string_encode_base64 (NULL, 0, NULL)); + LONGS_EQUAL(-1, string_encode_base64 (NULL, 0, str)); + LONGS_EQUAL(-1, string_encode_base64 ("", 0, NULL)); str[0] = 0xAA; - string_encode_base64 ("", -1, str); + LONGS_EQUAL(0, string_encode_base64 ("", -1, str)); BYTES_EQUAL(0x0, str[0]); str[0] = 0xAA; - string_encode_base64 ("", 0, str); + LONGS_EQUAL(0, string_encode_base64 ("", 0, str)); BYTES_EQUAL(0x0, str[0]); for (i = 0; str_base64[i][0]; i++) { - string_encode_base64 (str_base64[i][0], strlen (str_base64[i][0]), - str); + length = strlen (str_base64[i][1]); + LONGS_EQUAL(length, string_encode_base64 (str_base64[i][0], + strlen (str_base64[i][0]), + str)); STRCMP_EQUAL(str_base64[i][1], str); } @@ -1499,9 +1480,9 @@ TEST(CoreString, BaseN) LONGS_EQUAL(0, string_decode_base64 ("", str)); for (i = 0; str_base64[i][0]; i++) { - length = string_decode_base64 (str_base64[i][1], str); + length = strlen (str_base64[i][0]); + LONGS_EQUAL(length, string_decode_base64 (str_base64[i][1], str)); STRCMP_EQUAL(str_base64[i][0], str); - LONGS_EQUAL(strlen (str_base64[i][0]), length); } } |