diff options
-rw-r--r-- | ChangeLog.adoc | 2 | ||||
-rw-r--r-- | ReleaseNotes.adoc | 19 | ||||
-rw-r--r-- | doc/en/weechat_plugin_api.en.adoc | 36 | ||||
-rw-r--r-- | doc/fr/weechat_plugin_api.fr.adoc | 38 | ||||
-rw-r--r-- | doc/it/weechat_plugin_api.it.adoc | 45 | ||||
-rw-r--r-- | doc/ja/weechat_plugin_api.ja.adoc | 43 | ||||
-rw-r--r-- | src/core/wee-network.c | 2 | ||||
-rw-r--r-- | src/core/wee-secure-config.c | 4 | ||||
-rw-r--r-- | src/core/wee-secure.c | 6 | ||||
-rw-r--r-- | src/core/wee-string.c | 12 | ||||
-rw-r--r-- | src/core/wee-string.h | 12 | ||||
-rw-r--r-- | src/gui/curses/gui-curses-window.c | 2 | ||||
-rw-r--r-- | src/plugins/irc/irc-sasl.c | 23 | ||||
-rw-r--r-- | src/plugins/plugin-api.c | 39 | ||||
-rw-r--r-- | src/plugins/plugin-api.h | 4 | ||||
-rw-r--r-- | src/plugins/plugin.c | 4 | ||||
-rw-r--r-- | src/plugins/relay/relay-websocket.c | 4 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 16 | ||||
-rw-r--r-- | tests/unit/core/test-string.cpp | 120 |
19 files changed, 281 insertions, 150 deletions
diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 2cf2262ad..3d3fea9fb 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -20,7 +20,7 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes] New features:: - * api: return integer in function string_encode_base64 + * api: add functions string_base_encode and string_base_decode, remove functions string_encode_base64 and string_decode_base64 * api: add support of Time-based One-Time Password (TOTP), add infos "totp_generate" and "totp_validate" Bug fixes:: diff --git a/ReleaseNotes.adoc b/ReleaseNotes.adoc index cf71d993d..d3de45af7 100644 --- a/ReleaseNotes.adoc +++ b/ReleaseNotes.adoc @@ -17,6 +17,25 @@ https://weechat.org/files/changelog/ChangeLog-devel.html[ChangeLog] (file _ChangeLog.adoc_ in sources). +[[v2.4]] +== Version 2.4 (under dev) + +[[v2.4_api_base64_functions]] +=== Base64 API functions + +The functions to encode/decode base64 strings have been renamed and now support +base 16, 32, and 64. + +New functions in C API, supporting base 16, 32, and 64: + +* string_base_encode +* string_base_decode + +Functions removed from C API: + +* string_encode_base64 +* string_decode_base64 + [[v2.3]] == Version 2.3 (2018-10-21) diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc index 9ecbc9b81..98d8b47fd 100644 --- a/doc/en/weechat_plugin_api.en.adoc +++ b/doc/en/weechat_plugin_api.en.adoc @@ -1778,21 +1778,22 @@ str = weechat.string_remove_color(string, replacement) str = weechat.string_remove_color(my_string, "?") ---- -==== string_encode_base64 +==== string_base_encode -_WeeChat ≥ 0.3.2, updated in 2.4._ +_WeeChat ≥ 2.4._ -Encode a string in base64. +Encode a string in base 16, 32, or 64. Prototype: [source,C] ---- -int weechat_string_encode_base64 (const char *from, int length, char *to); +int weechat_string_base_encode (int base, const char *from, int length, char *to); ---- Arguments: +* _base_: 16, 32, or 64 * _from_: string to encode * _length_: length of string to encode (for example `strlen(from)`) * _to_: pointer to string to store result (must be long enough, result is @@ -1800,7 +1801,7 @@ Arguments: Return value: -* length of string stored in _*to_ (does not count final _\0_) +* length of string stored in _*to_ (does not count final `\0`), -1 if error C example: @@ -1808,43 +1809,52 @@ C example: ---- char *string = "abcdefgh", result[128]; int length; -length = weechat_string_encode_base64 (string, strlen (string), result); +length = weechat_string_base_encode (16, string, strlen (string), result); +/* length == 16, result == "6162636465666768" */ +length = weechat_string_base_encode (32, string, strlen (string), result); +/* length == 16, result == "MFRGGZDFMZTWQ===" */ +length = weechat_string_base_encode (64, string, strlen (string), result); /* length == 12, result == "YWJjZGVmZ2g=" */ ---- [NOTE] This function is not available in scripting API. -==== string_decode_base64 +==== string_base_decode -_WeeChat ≥ 0.3.2._ +_WeeChat ≥ 2.4._ -Decode a base64 string. +Decode a string encoded in base 16, 32, or 64. Prototype: [source,C] ---- -int weechat_string_decode_base64 (const char *from, char *to); +int weechat_string_base_decode (int base, const char *from, char *to); ---- Arguments: +* _base_: 16, 32, or 64 * _from_: string to decode * _to_: pointer to string to store result (must be long enough, result is shorter than initial string) Return value: -* length of string stored in _*to_ (does not count final _\0_) +* length of string stored in _*to_ (does not count final `\0`), -1 if error C example: [source,C] ---- -char *string = "YWJjZGVmZ2g=", result[128]; +char result[128]; int length; -length = weechat_string_decode_base64 (string, result); +length = weechat_string_base_decode (16, "6162636465666768", result); +/* length == 8, result == "abcdefgh" */ +length = weechat_string_base_decode (32, "MFRGGZDFMZTWQ===", result); +/* length == 8, result == "abcdefgh" */ +length = weechat_string_base_decode (64, "YWJjZGVmZ2g=", result); /* length == 8, result == "abcdefgh" */ ---- diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc index 5f3181dac..942ba06bd 100644 --- a/doc/fr/weechat_plugin_api.fr.adoc +++ b/doc/fr/weechat_plugin_api.fr.adoc @@ -1812,21 +1812,22 @@ str = weechat.string_remove_color(string, replacement) str = weechat.string_remove_color(ma_chaine, "?") ---- -==== string_encode_base64 +==== string_base_encode -_WeeChat ≥ 0.3.2, mis à jour dans la 2.4._ +_WeeChat ≥ 2.4._ -Encoder une chaîne en base64. +Encoder une chaîne en base 16, 32 ou 64. Prototype : [source,C] ---- -int weechat_string_encode_base64 (const char *from, int length, char *to); +int weechat_string_base_encode (int base, const char *from, int length, char *to); ---- Paramètres : +* _base_ : 16, 32 ou 64 * _from_ : chaîne à encoder * _length_ : longueur de chaîne à encoder (par exemple `strlen(from)`) * _to_ : pointeur vers la chaîne pour stocker le résultat (doit être @@ -1834,7 +1835,8 @@ Paramètres : Valeur de retour : -* longueur de la chaîne stockée dans _*to_ (ne compte pas le _\0_ final) +* longueur de la chaîne stockée dans _*to_ (ne compte pas le `\0` final), + -1 en cas d'erreur Exemple en C : @@ -1842,43 +1844,53 @@ Exemple en C : ---- char *string = "abcdefgh", result[128]; int length; -length = weechat_string_encode_base64 (string, strlen (string), result); +length = weechat_string_base_encode (16, string, strlen (string), result); +/* length == 16, result == "6162636465666768" */ +length = weechat_string_base_encode (32, string, strlen (string), result); +/* length == 16, result == "MFRGGZDFMZTWQ===" */ +length = weechat_string_base_encode (64, string, strlen (string), result); /* length == 12, result == "YWJjZGVmZ2g=" */ ---- [NOTE] Cette fonction n'est pas disponible dans l'API script. -==== string_decode_base64 +==== string_base_decode -_WeeChat ≥ 0.3.2._ +_WeeChat ≥ 2.4._ -Décoder une chaîne base64. +Décoder une chaîne encodée en base 16, 32 ou 64. Prototype : [source,C] ---- -int weechat_string_decode_base64 (const char *from, char *to); +int weechat_string_base_decode (int base, const char *from, char *to); ---- Paramètres : +* _base_ : 16, 32 ou 64 * _from_ : chaîne à décoder * _to_ : pointeur vers la chaîne pour stocker le résultat (doit être suffisamment long, le résultat est plus court que la chaîne initiale) Valeur de retour : -* longueur de la chaîne stockée dans _*to_ (ne compte pas le _\0_ final) +* longueur de la chaîne stockée dans _*to_ (ne compte pas le `\0` final), + -1 en cas d'erreur Exemple en C : [source,C] ---- -char *string = "YWJjZGVmZ2g=", result[128]; +char result[128]; int length; -length = weechat_string_decode_base64 (string, result); +length = weechat_string_base_decode (16, "6162636465666768", result); +/* length == 8, result == "abcdefgh" */ +length = weechat_string_base_decode (32, "MFRGGZDFMZTWQ===", result); +/* length == 8, result == "abcdefgh" */ +length = weechat_string_base_decode (64, "YWJjZGVmZ2g=", result); /* length == 8, result == "abcdefgh" */ ---- diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc index a0dd0988b..127fae44d 100644 --- a/doc/it/weechat_plugin_api.it.adoc +++ b/doc/it/weechat_plugin_api.it.adoc @@ -1851,22 +1851,24 @@ str = weechat.string_remove_color(string, replacement) str = weechat.string_remove_color(my_string, "?") ---- -==== string_encode_base64 +==== string_base_encode -// TRANSLATION MISSING -_WeeChat ≥ 0.3.2, updated in 2.4._ +_WeeChat ≥ 2.4._ -Codifica una stringa in base64. +// TRANSLATION MISSING +Encode a string in base 16, 32, or 64. Prototipo: [source,C] ---- -int weechat_string_encode_base64 (const char *from, int length, char *to); +int weechat_string_base_encode (int base, const char *from, int length, char *to); ---- Argomenti: +// TRANSLATION MISSING +* _base_: 16, 32, or 64 * _from_: stringa da codificare * _length_: lunghezza della stringa da codificare (ad esempio `strlen(from)`) * _to_: puntatore alla stringa per memorizzare il risultato (deve essere @@ -1874,7 +1876,9 @@ Argomenti: Valore restituito: -* lunghezza della stringa memorizzata in _*to_ (lo _\0_ finale non conta) +// TRANSLATION MISSING +* lunghezza della stringa memorizzata in _*to_ (lo `\0` finale non conta), + -1 if error Esempio in C: @@ -1882,43 +1886,56 @@ Esempio in C: ---- char *string = "abcdefgh", result[128]; int length; -length = weechat_string_encode_base64 (string, strlen (string), result); +length = weechat_string_base_encode (16, string, strlen (string), result); +/* length == 16, result == "6162636465666768" */ +length = weechat_string_base_encode (32, string, strlen (string), result); +/* length == 16, result == "MFRGGZDFMZTWQ===" */ +length = weechat_string_base_encode (64, string, strlen (string), result); /* length == 12, result == "YWJjZGVmZ2g=" */ ---- [NOTE] Questa funzione non è disponibile nelle API per lo scripting. -==== string_decode_base64 +==== string_base_decode -_WeeChat ≥ 0.3.2._ +_WeeChat ≥ 2.4._ -Decodifica una stringa in base64. +// TRANSLATION MISSING +Decode a string encoded in base 16, 32, or 64. Prototipo: [source,C] ---- -int weechat_string_decode_base64 (const char *from, char *to); +int weechat_string_base_decode (int base, const char *from, char *to); ---- Argomenti: +// TRANSLATION MISSING +* _base_: 16, 32, or 64 * _from_: stringa da decodificare * _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) +// TRANSLATION MISSING +* lunghezza della stringa memorizzata in _*to_ (lo `\0` finale non conta), + -1 if error Esempio in C: [source,C] ---- -char *string = "YWJjZGVmZ2g=", result[128]; +char result[128]; int length; -length = weechat_string_decode_base64 (string, result); +length = weechat_string_base_decode (16, "6162636465666768", result); +/* length == 8, result == "abcdefgh" */ +length = weechat_string_base_decode (32, "MFRGGZDFMZTWQ===", result); +/* length == 8, result == "abcdefgh" */ +length = weechat_string_base_decode (64, "YWJjZGVmZ2g=", result); /* length == 8, result == "abcdefgh" */ ---- diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc index 5ac11dfee..026131dd8 100644 --- a/doc/ja/weechat_plugin_api.ja.adoc +++ b/doc/ja/weechat_plugin_api.ja.adoc @@ -1784,22 +1784,24 @@ str = weechat.string_remove_color(string, replacement) str = weechat.string_remove_color(my_string, "?") ---- -==== string_encode_base64 +==== string_base_encode -// TRANSLATION MISSING -_WeeChat ≥ 0.3.2, updated in 2.4._ +_WeeChat バージョン 2.4 以上で利用可。_ -文字列を base64 でエンコード。 +// TRANSLATION MISSING +Encode a string in base 16, 32, or 64. プロトタイプ: [source,C] ---- -int weechat_string_encode_base64 (const char *from, int length, char *to); +int weechat_string_base_encode (int base, const char *from, int length, char *to); ---- 引数: +// TRANSLATION MISSING +* _base_: 16, 32, or 64 * _from_: エンコード元文字列 * _length_: エンコードする文字列の長さ (例えば `strlen(from)`) * _to_: エンコード結果を保存する文字列へのポインタ @@ -1807,7 +1809,8 @@ int weechat_string_encode_base64 (const char *from, int length, char *to); 戻り値: -* _*to_ に保存された文字列の長さ (最後の _\0_ は数えません) +// TRANSLATION MISSING +* _*to_ に保存された文字列の長さ (最後の `\0` は数えません), -1 if error C 言語での使用例: @@ -1815,43 +1818,55 @@ C 言語での使用例: ---- char *string = "abcdefgh", result[128]; int length; -length = weechat_string_encode_base64 (string, strlen (string), result); +length = weechat_string_base_encode (16, string, strlen (string), result); +/* length == 16, result == "6162636465666768" */ +length = weechat_string_base_encode (32, string, strlen (string), result); +/* length == 16, result == "MFRGGZDFMZTWQ===" */ +length = weechat_string_base_encode (64, string, strlen (string), result); /* length == 12, result == "YWJjZGVmZ2g=" */ ---- [NOTE] スクリプト API ではこの関数を利用できません。 -==== string_decode_base64 +==== string_base_decode -_WeeChat バージョン 0.3.2 以上で利用可。_ +_WeeChat バージョン 2.4 以上で利用可。_ -base64 文字列をデコード。 +// TRANSLATION MISSING +Decode a string encoded in base 16, 32, or 64. プロトタイプ: [source,C] ---- -int weechat_string_decode_base64 (const char *from, char *to); +int weechat_string_base_decode (int base, const char *from, char *to); ---- 引数: +// TRANSLATION MISSING +* _base_: 16, 32, or 64 * _from_: デコード元文字列 * _to_: デコード結果を保存する文字列へのポインタ (十分な領域を確保してください、結果はデコード元文字列よりも短くなります) 戻り値: -* _*to_ に保存された文字列の長さ (最後の _\0_ は数えません) +// TRANSLATION MISSING +* _*to_ に保存された文字列の長さ (最後の `\0` は数えません), -1 if error C 言語での使用例: [source,C] ---- -char *string = "YWJjZGVmZ2g=", result[128]; +char result[128]; int length; -length = weechat_string_decode_base64 (string, result); +length = weechat_string_base_decode (16, "6162636465666768", result); +/* length == 8, result == "abcdefgh" */ +length = weechat_string_base_decode (32, "MFRGGZDFMZTWQ===", result); +/* length == 8, result == "abcdefgh" */ +length = weechat_string_base_decode (64, "YWJjZGVmZ2g=", result); /* length == 8, result == "abcdefgh" */ ---- diff --git a/src/core/wee-network.c b/src/core/wee-network.c index dcb8e174d..8fd45d690 100644 --- a/src/core/wee-network.c +++ b/src/core/wee-network.c @@ -267,7 +267,7 @@ network_pass_httpproxy (struct t_proxy *proxy, int sock, const char *address, snprintf (authbuf, sizeof (authbuf), "%s:%s", username, password); free (username); free (password); - if (string_encode_base64 (authbuf, strlen (authbuf), authbuf_base64) < 0) + if (string_base64_encode (authbuf, strlen (authbuf), authbuf_base64) < 0) return 0; length = snprintf (buffer, sizeof (buffer), "CONNECT %s:%d HTTP/1.0\r\nProxy-Authorization: " diff --git a/src/core/wee-secure-config.c b/src/core/wee-secure-config.c index 5e1332019..4ee1f1112 100644 --- a/src/core/wee-secure-config.c +++ b/src/core/wee-secure-config.c @@ -274,7 +274,7 @@ secure_config_data_read_cb (const void *pointer, void *data, if (!buffer) return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; - length_buffer = string_decode_base16 (value, buffer); + length_buffer = string_base16_decode (value, buffer); while (1) { decrypted = NULL; @@ -370,7 +370,7 @@ secure_config_data_write_map_cb (void *data, buffer_base16 = malloc ((length_buffer * 2) + 1); if (buffer_base16) { - if (string_encode_base16 (buffer, length_buffer, + if (string_base16_encode (buffer, length_buffer, buffer_base16) >= 0) { config_file_write_line (config_file, key, diff --git a/src/core/wee-secure.c b/src/core/wee-secure.c index b9abe62c9..84680bec8 100644 --- a/src/core/wee-secure.c +++ b/src/core/wee-secure.c @@ -466,7 +466,7 @@ secure_decrypt_data_not_decrypted (const char *passphrase) buffer = malloc (strlen (value) + 1); if (buffer) { - length_buffer = string_decode_base16 (value, buffer); + length_buffer = string_base16_decode (value, buffer); decrypted = NULL; length_decrypted = 0; rc = secure_decrypt_data ( @@ -595,7 +595,7 @@ secure_totp_generate (const char *secret_base32, time_t totp_time, int digits) if (!secret) goto error; - length_secret = string_decode_base32 (secret_base32, secret); + length_secret = string_base32_decode (secret_base32, secret); if (length_secret < 0) goto error; @@ -654,7 +654,7 @@ secure_totp_validate (const char *secret_base32, time_t totp_time, int window, if (!secret) goto error; - length_secret = string_decode_base32 (secret_base32, secret); + length_secret = string_base32_decode (secret_base32, secret); if (length_secret < 0) goto error; diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 32afd060e..016f93f04 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -2707,7 +2707,7 @@ string_format_size (unsigned long long size) */ int -string_encode_base16 (const char *from, int length, char *to) +string_base16_encode (const char *from, int length, char *to) { int i, count; const char *hexa = "0123456789ABCDEF"; @@ -2734,7 +2734,7 @@ string_encode_base16 (const char *from, int length, char *to) */ int -string_decode_base16 (const char *from, char *to) +string_base16_decode (const char *from, char *to) { int length, i, pos, count; unsigned char value; @@ -2803,7 +2803,7 @@ string_decode_base16 (const char *from, char *to) */ int -string_encode_base32 (const char *from, int length, char *to) +string_base32_encode (const char *from, int length, char *to) { unsigned char base32_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; int count, value, next, bits_left, pad, index; @@ -2880,7 +2880,7 @@ string_encode_base32 (const char *from, int length, char *to) */ int -string_decode_base32 (const char *from, char *to) +string_base32_decode (const char *from, char *to) { const char *ptr_from; int value, bits_left, count; @@ -2956,7 +2956,7 @@ string_convbase64_8x3_to_6x4 (const char *from, char *to) */ int -string_encode_base64 (const char *from, int length, char *to) +string_base64_encode (const char *from, int length, char *to) { const char *ptr_from; char rest[3]; @@ -3027,7 +3027,7 @@ string_convbase64_6x4_to_8x3 (const unsigned char *from, unsigned char *to) */ int -string_decode_base64 (const char *from, char *to) +string_base64_decode (const char *from, char *to) { const char *ptr_from; int length, to_length, i; diff --git a/src/core/wee-string.h b/src/core/wee-string.h index 4709f73d7..6d059f8fa 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -105,12 +105,12 @@ extern char *string_iconv_from_internal (const char *charset, const char *string); extern int string_fprintf (FILE *file, const char *data, ...); extern char *string_format_size (unsigned long long size); -extern int 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 int string_encode_base64 (const char *from, int length, char *to); -extern int string_decode_base64 (const char *from, char *to); +extern int string_base16_encode (const char *from, int length, char *to); +extern int string_base16_decode (const char *from, char *to); +extern int string_base32_encode (const char *from, int length, char *to); +extern int string_base32_decode (const char *from, char *to); +extern int string_base64_encode (const char *from, int length, char *to); +extern int string_base64_decode (const char *from, char *to); extern char *string_hex_dump (const char *data, int data_size, int bytes_per_line, const char *prefix, const char *suffix); diff --git a/src/gui/curses/gui-curses-window.c b/src/gui/curses/gui-curses-window.c index e9ff4aae3..a177e2f95 100644 --- a/src/gui/curses/gui-curses-window.c +++ b/src/gui/curses/gui-curses-window.c @@ -2509,7 +2509,7 @@ gui_window_send_clipboard (const char *storage_unit, const char *text) text_base64 = malloc ((length * 4) + 1); if (text_base64) { - if (string_encode_base64 (text, length, text_base64) >= 0) + if (string_base64_encode (text, length, text_base64) >= 0) { fprintf (stderr, "\033]52;%s;%s\a", (storage_unit) ? storage_unit : "", diff --git a/src/plugins/irc/irc-sasl.c b/src/plugins/irc/irc-sasl.c index 63d9bf6e5..718e59e90 100644 --- a/src/plugins/irc/irc-sasl.c +++ b/src/plugins/irc/irc-sasl.c @@ -73,8 +73,8 @@ irc_sasl_mechanism_plain (const char *sasl_username, const char *sasl_password) answer_base64 = malloc (length * 4); if (answer_base64) { - if (weechat_string_encode_base64 (string, length - 1, - answer_base64) < 0) + if (weechat_string_base_encode (64, string, length - 1, + answer_base64) < 0) { free (answer_base64); answer_base64 = NULL; @@ -180,7 +180,7 @@ irc_sasl_mechanism_ecdsa_nist256p_challenge (struct t_irc_server *server, data = malloc (strlen (data_base64) + 1); if (!data) return NULL; - length_data = weechat_string_decode_base64 (data_base64, data); + length_data = weechat_string_base_decode (64, data_base64, data); /* read file with private key */ str_privkey = irc_sasl_get_key_content (server, sasl_key); @@ -226,8 +226,8 @@ irc_sasl_mechanism_ecdsa_nist256p_challenge (struct t_irc_server *server, pubkey_base64 = malloc ((x.size + 1 + 1) * 4); if (pubkey_base64) { - if (weechat_string_encode_base64 (pubkey, x.size + 1, - pubkey_base64) >= 0) + if (weechat_string_base_encode (64, pubkey, x.size + 1, + pubkey_base64) >= 0) { weechat_printf ( server->buffer, @@ -299,7 +299,8 @@ irc_sasl_mechanism_ecdsa_nist256p_challenge (struct t_irc_server *server, answer_base64 = malloc ((length + 1) * 4); if (answer_base64) { - if (weechat_string_encode_base64 (string, length, answer_base64) < 0) + if (weechat_string_base_encode (64, string, length, + answer_base64) < 0) { free (answer_base64); answer_base64 = NULL; @@ -364,7 +365,7 @@ irc_sasl_dh (const char *data_base64, data = malloc (strlen (data_base64) + 1); if (!data) goto dhend; - length_data = weechat_string_decode_base64 (data_base64, data); + length_data = weechat_string_base_decode (64, data_base64, data); ptr_data = (unsigned char *)data; /* extract prime number */ @@ -520,8 +521,8 @@ irc_sasl_mechanism_dh_blowfish (const char *data_base64, answer_base64 = malloc ((length_answer + 1) * 4); if (answer_base64) { - if (weechat_string_encode_base64 (answer, length_answer, - answer_base64) < 0) + if (weechat_string_base_encode (64, answer, length_answer, + answer_base64) < 0) { free (answer_base64); answer_base64 = NULL; @@ -649,8 +650,8 @@ irc_sasl_mechanism_dh_aes (const char *data_base64, answer_base64 = malloc ((length_answer + 1) * 4); if (answer_base64) { - if (weechat_string_encode_base64 (answer, length_answer, - answer_base64) < 0) + if (weechat_string_base_encode (64, answer, length_answer, + answer_base64) < 0) { free (answer_base64); answer_base64 = NULL; diff --git a/src/plugins/plugin-api.c b/src/plugins/plugin-api.c index 2ac0a8eeb..646fb9f9a 100644 --- a/src/plugins/plugin-api.c +++ b/src/plugins/plugin-api.c @@ -95,6 +95,45 @@ plugin_api_ngettext (const char *single, const char *plural, int count) } /* + * Encodes a string in base 16, 32, or 64. + */ + +int +plugin_api_string_base_encode (int base, const char *from, int length, + char *to) +{ + switch (base) + { + case 16: + return string_base16_encode (from, length, to); + case 32: + return string_base32_encode (from, length, to); + case 64: + return string_base64_encode (from, length, to); + } + return -1; +} + +/* + * Decodes a string encoded in base 16, 32, or 64. + */ + +int +plugin_api_string_base_decode (int base, const char *from, char *to) +{ + switch (base) + { + case 16: + return string_base16_decode (from, to); + case 32: + return string_base32_decode (from, to); + case 64: + return string_base64_decode (from, to); + } + return -1; +} + +/* * Frees an option. */ diff --git a/src/plugins/plugin-api.h b/src/plugins/plugin-api.h index c754fe243..29764096e 100644 --- a/src/plugins/plugin-api.h +++ b/src/plugins/plugin-api.h @@ -28,6 +28,10 @@ extern void plugin_api_charset_set (struct t_weechat_plugin *plugin, extern const char *plugin_api_gettext (const char *string); extern const char *plugin_api_ngettext (const char *single, const char *plural, int count); +extern int plugin_api_string_base_encode (int base, const char *from, + int length, char *to); +extern int plugin_api_string_base_decode (int base, const char *from, + char *to); /* config */ extern void plugin_api_config_file_option_free (struct t_config_option *option); diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 84e2e3a2c..d57b4d331 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -636,8 +636,8 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv) new_plugin->string_free_split_command = &string_free_split_command; new_plugin->string_format_size = &string_format_size; new_plugin->string_remove_color = &gui_color_decode; - new_plugin->string_encode_base64 = &string_encode_base64; - new_plugin->string_decode_base64 = &string_decode_base64; + new_plugin->string_base_encode = &plugin_api_string_base_encode; + new_plugin->string_base_decode = &plugin_api_string_base_decode; new_plugin->string_hex_dump = &string_hex_dump; new_plugin->string_is_command_char = &string_is_command_char; new_plugin->string_input_for_buffer = &string_input_for_buffer; diff --git a/src/plugins/relay/relay-websocket.c b/src/plugins/relay/relay-websocket.c index 60c4f4526..c0398061e 100644 --- a/src/plugins/relay/relay-websocket.c +++ b/src/plugins/relay/relay-websocket.c @@ -214,8 +214,8 @@ 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); - if (weechat_string_encode_base64 ((char *)result, length, - sec_websocket_accept) < 0) + if (weechat_string_base_encode (64, (char *)result, length, + sec_websocket_accept) < 0) { sec_websocket_accept[0] = '\0'; } diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 94aa54598..a05082b9a 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 "20181102-01" +#define WEECHAT_PLUGIN_API_VERSION "20181104-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -323,8 +323,9 @@ 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); - int (*string_encode_base64) (const char *from, int length, char *to); - int (*string_decode_base64) (const char *from, char *to); + int (*string_base_encode) (int base, const char *from, int length, + char *to); + int (*string_base_decode) (int base, const char *from, char *to); char *(*string_hex_dump) (const char *data, int data_size, int bytes_per_line, const char *prefix, const char *suffix); @@ -1224,10 +1225,11 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin); (weechat_plugin->string_format_size)(__size) #define weechat_string_remove_color(__string, __replacement) \ (weechat_plugin->string_remove_color)(__string, __replacement) -#define weechat_string_encode_base64(__from, __length, __to) \ - (weechat_plugin->string_encode_base64)(__from, __length, __to) -#define weechat_string_decode_base64(__from, __to) \ - (weechat_plugin->string_decode_base64)(__from, __to) +#define weechat_string_base_encode(__base, __from, __length, __to) \ + (weechat_plugin->string_base_encode)(__base, __from, __length, \ + __to) +#define weechat_string_base_decode(__base, __from, __to) \ + (weechat_plugin->string_base_decode)(__base, __from, __to) #define weechat_string_hex_dump(__data, __data_size, __bytes_per_line, \ __prefix, __suffix) \ (weechat_plugin->string_hex_dump)(__data, __data_size, \ diff --git a/tests/unit/core/test-string.cpp b/tests/unit/core/test-string.cpp index fb57410fb..294fb4a97 100644 --- a/tests/unit/core/test-string.cpp +++ b/tests/unit/core/test-string.cpp @@ -1333,45 +1333,57 @@ TEST(CoreString, FormatSize) /* * Tests functions: - * string_encode_base16 - * string_decode_base16 + * string_base16_encode + * string_base16_decode */ TEST(CoreString, Base16) { + int i, length; char str[1024]; + const char *str_base16[][2] = + { { "", "" }, + { "abcdefgh", "6162636465666768" }, + { "this is a *test*", "746869732069732061202A746573742A" }, + { NULL, NULL } }; - /* string_encode_base16 */ - LONGS_EQUAL(-1, string_encode_base16 (NULL, 0, NULL)); - LONGS_EQUAL(-1, string_encode_base16 (NULL, 0, str)); - LONGS_EQUAL(-1, string_encode_base16 ("", 0, NULL)); + + /* string_base16_encode */ + LONGS_EQUAL(-1, string_base16_encode (NULL, 0, NULL)); + LONGS_EQUAL(-1, string_base16_encode (NULL, 0, str)); + LONGS_EQUAL(-1, string_base16_encode ("", 0, NULL)); str[0] = 0xAA; - LONGS_EQUAL(0, string_encode_base16 ("", -1, str)); + LONGS_EQUAL(0, string_base16_encode ("", -1, str)); BYTES_EQUAL(0x0, str[0]); str[0] = 0xAA; - LONGS_EQUAL(0, string_encode_base16 ("", 0, str)); + LONGS_EQUAL(0, string_base16_encode ("", 0, str)); BYTES_EQUAL(0x0, str[0]); - LONGS_EQUAL(6, string_encode_base16 ("abc", 3, str)); - STRCMP_EQUAL("616263", str); - LONGS_EQUAL(32, string_encode_base16 ("this is a *test*", 16, str)); - STRCMP_EQUAL("746869732069732061202A746573742A", 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); - LONGS_EQUAL(16, string_decode_base16 ("746869732069732061202A746573742A", - str)); - STRCMP_EQUAL("this is a *test*", str); + for (i = 0; str_base16[i][0]; i++) + { + length = strlen (str_base16[i][1]); + LONGS_EQUAL(length, string_base16_encode (str_base16[i][0], + strlen (str_base16[i][0]), + str)); + STRCMP_EQUAL(str_base16[i][1], str); + } + + /* string_base16_decode */ + LONGS_EQUAL(0, string_base16_decode (NULL, NULL)); + LONGS_EQUAL(0, string_base16_decode (NULL, str)); + LONGS_EQUAL(0, string_base16_decode ("", NULL)); + LONGS_EQUAL(0, string_base16_decode ("", str)); + for (i = 0; str_base16[i][0]; i++) + { + length = strlen (str_base16[i][0]); + LONGS_EQUAL(length, string_base16_decode (str_base16[i][1], str)); + STRCMP_EQUAL(str_base16[i][0], str); + } } /* * Tests functions: - * string_encode_base32 - * string_decode_base32 + * string_base32_encode + * string_base32_decode */ TEST(CoreString, Base32) @@ -1384,7 +1396,7 @@ TEST(CoreString, Base32) { "B", "II======" }, { "C", "IM======" }, { "D", "IQ======" }, - { "abc", "MFRGG===" }, + { "abcdefgh", "MFRGGZDFMZTWQ===" }, { "This is a test.", "KRUGS4ZANFZSAYJAORSXG5BO" }, { "This is a test..", "KRUGS4ZANFZSAYJAORSXG5BOFY======" }, { "This is a test...", "KRUGS4ZANFZSAYJAORSXG5BOFYXA====" }, @@ -1394,42 +1406,42 @@ TEST(CoreString, Base32) "FY======" }, { NULL, NULL } }; - /* string_encode_base32 */ - LONGS_EQUAL(-1, string_encode_base32 (NULL, 0, NULL)); - LONGS_EQUAL(-1, string_encode_base32 (NULL, 0, str)); - LONGS_EQUAL(-1, string_encode_base32 ("", 0, NULL)); + /* string_base32_encode */ + LONGS_EQUAL(-1, string_base32_encode (NULL, 0, NULL)); + LONGS_EQUAL(-1, string_base32_encode (NULL, 0, str)); + LONGS_EQUAL(-1, string_base32_encode ("", 0, NULL)); str[0] = 0xAA; - LONGS_EQUAL(0, string_encode_base32 ("", -1, str)); + LONGS_EQUAL(0, string_base32_encode ("", -1, str)); BYTES_EQUAL(0x0, str[0]); str[0] = 0xAA; - LONGS_EQUAL(0, string_encode_base32 ("", 0, str)); + LONGS_EQUAL(0, string_base32_encode ("", 0, str)); BYTES_EQUAL(0x0, str[0]); for (i = 0; str_base32[i][0]; i++) { length = strlen (str_base32[i][1]); - LONGS_EQUAL(length, string_encode_base32 (str_base32[i][0], + LONGS_EQUAL(length, string_base32_encode (str_base32[i][0], strlen (str_base32[i][0]), str)); STRCMP_EQUAL(str_base32[i][1], str); } - /* string_decode_base32 */ - LONGS_EQUAL(-1, string_decode_base32 (NULL, NULL)); - LONGS_EQUAL(-1, string_decode_base32 (NULL, str)); - LONGS_EQUAL(-1, string_decode_base32 ("", NULL)); - LONGS_EQUAL(0, string_decode_base32 ("", str)); + /* string_base32_decode */ + LONGS_EQUAL(-1, string_base32_decode (NULL, NULL)); + LONGS_EQUAL(-1, string_base32_decode (NULL, str)); + LONGS_EQUAL(-1, string_base32_decode ("", NULL)); + LONGS_EQUAL(0, string_base32_decode ("", str)); for (i = 0; str_base32[i][0]; i++) { length = strlen (str_base32[i][0]); - LONGS_EQUAL(length, string_decode_base32 (str_base32[i][1], str)); + LONGS_EQUAL(length, string_base32_decode (str_base32[i][1], str)); STRCMP_EQUAL(str_base32[i][0], str); } } /* * Tests functions: - * string_encode_base64 - * string_decode_base64 + * string_base64_encode + * string_base64_decode */ TEST(CoreString, Base64) @@ -1459,34 +1471,34 @@ TEST(CoreString, Base64) "QW5vdGhlciBleGFtcGxlIGZvciBiYXNlNjQuLi4=" }, { NULL, NULL } }; - /* string_encode_base64 */ - 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)); + /* string_base64_encode */ + LONGS_EQUAL(-1, string_base64_encode (NULL, 0, NULL)); + LONGS_EQUAL(-1, string_base64_encode (NULL, 0, str)); + LONGS_EQUAL(-1, string_base64_encode ("", 0, NULL)); str[0] = 0xAA; - LONGS_EQUAL(0, string_encode_base64 ("", -1, str)); + LONGS_EQUAL(0, string_base64_encode ("", -1, str)); BYTES_EQUAL(0x0, str[0]); str[0] = 0xAA; - LONGS_EQUAL(0, string_encode_base64 ("", 0, str)); + LONGS_EQUAL(0, string_base64_encode ("", 0, str)); BYTES_EQUAL(0x0, str[0]); for (i = 0; str_base64[i][0]; i++) { length = strlen (str_base64[i][1]); - LONGS_EQUAL(length, string_encode_base64 (str_base64[i][0], + LONGS_EQUAL(length, string_base64_encode (str_base64[i][0], strlen (str_base64[i][0]), str)); STRCMP_EQUAL(str_base64[i][1], str); } - /* string_decode_base64 */ - LONGS_EQUAL(0, string_decode_base64 (NULL, NULL)); - LONGS_EQUAL(0, string_decode_base64 (NULL, str)); - LONGS_EQUAL(0, string_decode_base64 ("", NULL)); - LONGS_EQUAL(0, string_decode_base64 ("", str)); + /* string_base64_decode */ + LONGS_EQUAL(0, string_base64_decode (NULL, NULL)); + LONGS_EQUAL(0, string_base64_decode (NULL, str)); + LONGS_EQUAL(0, string_base64_decode ("", NULL)); + LONGS_EQUAL(0, string_base64_decode ("", str)); for (i = 0; str_base64[i][0]; i++) { length = strlen (str_base64[i][0]); - LONGS_EQUAL(length, string_decode_base64 (str_base64[i][1], str)); + LONGS_EQUAL(length, string_base64_decode (str_base64[i][1], str)); STRCMP_EQUAL(str_base64[i][0], str); } } |