api: add functions string_base_{encode,decode}, remove functions string_{encode,decode}_base64

This commit is contained in:
Sébastien Helleu 2018-11-04 00:30:57 +01:00
parent a8b6fa08b7
commit ed3f281ba9
19 changed files with 280 additions and 149 deletions

View File

@ -20,7 +20,7 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes]
New features:: 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" * api: add support of Time-based One-Time Password (TOTP), add infos "totp_generate" and "totp_validate"
Bug fixes:: Bug fixes::

View File

@ -17,6 +17,25 @@ https://weechat.org/files/changelog/ChangeLog-devel.html[ChangeLog]
(file _ChangeLog.adoc_ in sources). (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]] [[v2.3]]
== Version 2.3 (2018-10-21) == Version 2.3 (2018-10-21)

View File

@ -1778,21 +1778,22 @@ str = weechat.string_remove_color(string, replacement)
str = weechat.string_remove_color(my_string, "?") 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: Prototype:
[source,C] [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: Arguments:
* _base_: 16, 32, or 64
* _from_: string to encode * _from_: string to encode
* _length_: length of string to encode (for example `strlen(from)`) * _length_: length of string to encode (for example `strlen(from)`)
* _to_: pointer to string to store result (must be long enough, result is * _to_: pointer to string to store result (must be long enough, result is
@ -1800,7 +1801,7 @@ Arguments:
Return value: 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: C example:
@ -1808,43 +1809,52 @@ C example:
---- ----
char *string = "abcdefgh", result[128]; char *string = "abcdefgh", result[128];
int length; 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=" */ /* length == 12, result == "YWJjZGVmZ2g=" */
---- ----
[NOTE] [NOTE]
This function is not available in scripting API. 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: Prototype:
[source,C] [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: Arguments:
* _base_: 16, 32, or 64
* _from_: string to decode * _from_: string to decode
* _to_: pointer to string to store result (must be long enough, result is * _to_: pointer to string to store result (must be long enough, result is
shorter than initial string) shorter than initial string)
Return value: 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: C example:
[source,C] [source,C]
---- ----
char *string = "YWJjZGVmZ2g=", result[128]; char result[128];
int length; 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" */ /* length == 8, result == "abcdefgh" */
---- ----

View File

@ -1812,21 +1812,22 @@ str = weechat.string_remove_color(string, replacement)
str = weechat.string_remove_color(ma_chaine, "?") 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 : Prototype :
[source,C] [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 : Paramètres :
* _base_ : 16, 32 ou 64
* _from_ : chaîne à encoder * _from_ : chaîne à encoder
* _length_ : longueur de chaîne à encoder (par exemple `strlen(from)`) * _length_ : longueur de chaîne à encoder (par exemple `strlen(from)`)
* _to_ : pointeur vers la chaîne pour stocker le résultat (doit être * _to_ : pointeur vers la chaîne pour stocker le résultat (doit être
@ -1834,7 +1835,8 @@ Paramètres :
Valeur de retour : 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 : Exemple en C :
@ -1842,43 +1844,53 @@ Exemple en C :
---- ----
char *string = "abcdefgh", result[128]; char *string = "abcdefgh", result[128];
int length; 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=" */ /* length == 12, result == "YWJjZGVmZ2g=" */
---- ----
[NOTE] [NOTE]
Cette fonction n'est pas disponible dans l'API script. 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 : Prototype :
[source,C] [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 : Paramètres :
* _base_ : 16, 32 ou 64
* _from_ : chaîne à décoder * _from_ : chaîne à décoder
* _to_ : pointeur vers la chaîne pour stocker le résultat (doit être * _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) suffisamment long, le résultat est plus court que la chaîne initiale)
Valeur de retour : 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 : Exemple en C :
[source,C] [source,C]
---- ----
char *string = "YWJjZGVmZ2g=", result[128]; char result[128];
int length; 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" */ /* length == 8, result == "abcdefgh" */
---- ----

View File

@ -1851,22 +1851,24 @@ str = weechat.string_remove_color(string, replacement)
str = weechat.string_remove_color(my_string, "?") str = weechat.string_remove_color(my_string, "?")
---- ----
==== string_encode_base64 ==== string_base_encode
_WeeChat ≥ 2.4._
// TRANSLATION MISSING // TRANSLATION MISSING
_WeeChat ≥ 0.3.2, updated in 2.4._ Encode a string in base 16, 32, or 64.
Codifica una stringa in base64.
Prototipo: Prototipo:
[source,C] [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: Argomenti:
// TRANSLATION MISSING
* _base_: 16, 32, or 64
* _from_: stringa da codificare * _from_: stringa da codificare
* _length_: lunghezza della stringa da codificare (ad esempio `strlen(from)`) * _length_: lunghezza della stringa da codificare (ad esempio `strlen(from)`)
* _to_: puntatore alla stringa per memorizzare il risultato (deve essere * _to_: puntatore alla stringa per memorizzare il risultato (deve essere
@ -1874,7 +1876,9 @@ Argomenti:
Valore restituito: 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: Esempio in C:
@ -1882,43 +1886,56 @@ Esempio in C:
---- ----
char *string = "abcdefgh", result[128]; char *string = "abcdefgh", result[128];
int length; 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=" */ /* length == 12, result == "YWJjZGVmZ2g=" */
---- ----
[NOTE] [NOTE]
Questa funzione non è disponibile nelle API per lo scripting. 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: Prototipo:
[source,C] [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: Argomenti:
// TRANSLATION MISSING
* _base_: 16, 32, or 64
* _from_: stringa da decodificare * _from_: stringa da decodificare
* _to_: puntatore alla stringa per memorizzare il risultato (deve essere * _to_: puntatore alla stringa per memorizzare il risultato (deve essere
sufficientemente lunga, il risultato è più lungo della stringa iniziale) sufficientemente lunga, il risultato è più lungo della stringa iniziale)
Valore restituito: 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: Esempio in C:
[source,C] [source,C]
---- ----
char *string = "YWJjZGVmZ2g=", result[128]; char result[128];
int length; 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" */ /* length == 8, result == "abcdefgh" */
---- ----

View File

@ -1784,22 +1784,24 @@ str = weechat.string_remove_color(string, replacement)
str = weechat.string_remove_color(my_string, "?") str = weechat.string_remove_color(my_string, "?")
---- ----
==== string_encode_base64 ==== string_base_encode
_WeeChat バージョン 2.4 以上で利用可。_
// TRANSLATION MISSING // TRANSLATION MISSING
_WeeChat ≥ 0.3.2, updated in 2.4._ Encode a string in base 16, 32, or 64.
文字列を base64 でエンコード。
プロトタイプ: プロトタイプ:
[source,C] [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_: エンコード元文字列 * _from_: エンコード元文字列
* _length_: エンコードする文字列の長さ (例えば `strlen(from)`) * _length_: エンコードする文字列の長さ (例えば `strlen(from)`)
* _to_: エンコード結果を保存する文字列へのポインタ * _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 言語での使用例: C 言語での使用例:
@ -1815,43 +1818,55 @@ C 言語での使用例:
---- ----
char *string = "abcdefgh", result[128]; char *string = "abcdefgh", result[128];
int length; 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=" */ /* length == 12, result == "YWJjZGVmZ2g=" */
---- ----
[NOTE] [NOTE]
スクリプト API ではこの関数を利用できません。 スクリプト 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] [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_: デコード元文字列 * _from_: デコード元文字列
* _to_: デコード結果を保存する文字列へのポインタ * _to_: デコード結果を保存する文字列へのポインタ
(十分な領域を確保してください、結果はデコード元文字列よりも短くなります) (十分な領域を確保してください、結果はデコード元文字列よりも短くなります)
戻り値: 戻り値:
* _*to_ に保存された文字列の長さ (最後の _\0_ は数えません) // TRANSLATION MISSING
* _*to_ に保存された文字列の長さ (最後の `\0` は数えません), -1 if error
C 言語での使用例: C 言語での使用例:
[source,C] [source,C]
---- ----
char *string = "YWJjZGVmZ2g=", result[128]; char result[128];
int length; 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" */ /* length == 8, result == "abcdefgh" */
---- ----

View File

@ -267,7 +267,7 @@ network_pass_httpproxy (struct t_proxy *proxy, int sock, const char *address,
snprintf (authbuf, sizeof (authbuf), "%s:%s", username, password); snprintf (authbuf, sizeof (authbuf), "%s:%s", username, password);
free (username); free (username);
free (password); free (password);
if (string_encode_base64 (authbuf, strlen (authbuf), authbuf_base64) < 0) if (string_base64_encode (authbuf, strlen (authbuf), authbuf_base64) < 0)
return 0; return 0;
length = snprintf (buffer, sizeof (buffer), length = snprintf (buffer, sizeof (buffer),
"CONNECT %s:%d HTTP/1.0\r\nProxy-Authorization: " "CONNECT %s:%d HTTP/1.0\r\nProxy-Authorization: "

View File

@ -274,7 +274,7 @@ secure_config_data_read_cb (const void *pointer, void *data,
if (!buffer) if (!buffer)
return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
length_buffer = string_decode_base16 (value, buffer); length_buffer = string_base16_decode (value, buffer);
while (1) while (1)
{ {
decrypted = NULL; decrypted = NULL;
@ -370,7 +370,7 @@ secure_config_data_write_map_cb (void *data,
buffer_base16 = malloc ((length_buffer * 2) + 1); buffer_base16 = malloc ((length_buffer * 2) + 1);
if (buffer_base16) if (buffer_base16)
{ {
if (string_encode_base16 (buffer, length_buffer, if (string_base16_encode (buffer, length_buffer,
buffer_base16) >= 0) buffer_base16) >= 0)
{ {
config_file_write_line (config_file, key, config_file_write_line (config_file, key,

View File

@ -466,7 +466,7 @@ secure_decrypt_data_not_decrypted (const char *passphrase)
buffer = malloc (strlen (value) + 1); buffer = malloc (strlen (value) + 1);
if (buffer) if (buffer)
{ {
length_buffer = string_decode_base16 (value, buffer); length_buffer = string_base16_decode (value, buffer);
decrypted = NULL; decrypted = NULL;
length_decrypted = 0; length_decrypted = 0;
rc = secure_decrypt_data ( rc = secure_decrypt_data (
@ -595,7 +595,7 @@ secure_totp_generate (const char *secret_base32, time_t totp_time, int digits)
if (!secret) if (!secret)
goto error; goto error;
length_secret = string_decode_base32 (secret_base32, secret); length_secret = string_base32_decode (secret_base32, secret);
if (length_secret < 0) if (length_secret < 0)
goto error; goto error;
@ -654,7 +654,7 @@ secure_totp_validate (const char *secret_base32, time_t totp_time, int window,
if (!secret) if (!secret)
goto error; goto error;
length_secret = string_decode_base32 (secret_base32, secret); length_secret = string_base32_decode (secret_base32, secret);
if (length_secret < 0) if (length_secret < 0)
goto error; goto error;

View File

@ -2707,7 +2707,7 @@ string_format_size (unsigned long long size)
*/ */
int int
string_encode_base16 (const char *from, int length, char *to) string_base16_encode (const char *from, int length, char *to)
{ {
int i, count; int i, count;
const char *hexa = "0123456789ABCDEF"; const char *hexa = "0123456789ABCDEF";
@ -2734,7 +2734,7 @@ string_encode_base16 (const char *from, int length, char *to)
*/ */
int int
string_decode_base16 (const char *from, char *to) string_base16_decode (const char *from, char *to)
{ {
int length, i, pos, count; int length, i, pos, count;
unsigned char value; unsigned char value;
@ -2803,7 +2803,7 @@ string_decode_base16 (const char *from, char *to)
*/ */
int 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"; unsigned char base32_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
int count, value, next, bits_left, pad, index; int count, value, next, bits_left, pad, index;
@ -2880,7 +2880,7 @@ string_encode_base32 (const char *from, int length, char *to)
*/ */
int int
string_decode_base32 (const char *from, char *to) string_base32_decode (const char *from, char *to)
{ {
const char *ptr_from; const char *ptr_from;
int value, bits_left, count; int value, bits_left, count;
@ -2956,7 +2956,7 @@ string_convbase64_8x3_to_6x4 (const char *from, char *to)
*/ */
int 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; const char *ptr_from;
char rest[3]; char rest[3];
@ -3027,7 +3027,7 @@ string_convbase64_6x4_to_8x3 (const unsigned char *from, unsigned char *to)
*/ */
int int
string_decode_base64 (const char *from, char *to) string_base64_decode (const char *from, char *to)
{ {
const char *ptr_from; const char *ptr_from;
int length, to_length, i; int length, to_length, i;

View File

@ -105,12 +105,12 @@ extern char *string_iconv_from_internal (const char *charset,
const char *string); const char *string);
extern int string_fprintf (FILE *file, const char *data, ...); extern int string_fprintf (FILE *file, const char *data, ...);
extern char *string_format_size (unsigned long long size); extern char *string_format_size (unsigned long long size);
extern int string_encode_base16 (const char *from, int length, char *to); extern int string_base16_encode (const char *from, int length, char *to);
extern int string_decode_base16 (const char *from, char *to); extern int string_base16_decode (const char *from, char *to);
extern int string_encode_base32 (const char *from, int length, char *to); extern int string_base32_encode (const char *from, int length, char *to);
extern int string_decode_base32 (const char *from, char *to); extern int string_base32_decode (const char *from, char *to);
extern int string_encode_base64 (const char *from, int length, char *to); extern int string_base64_encode (const char *from, int length, char *to);
extern int string_decode_base64 (const char *from, char *to); extern int string_base64_decode (const char *from, char *to);
extern char *string_hex_dump (const char *data, int data_size, extern char *string_hex_dump (const char *data, int data_size,
int bytes_per_line, int bytes_per_line,
const char *prefix, const char *suffix); const char *prefix, const char *suffix);

View File

@ -2509,7 +2509,7 @@ gui_window_send_clipboard (const char *storage_unit, const char *text)
text_base64 = malloc ((length * 4) + 1); text_base64 = malloc ((length * 4) + 1);
if (text_base64) 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", fprintf (stderr, "\033]52;%s;%s\a",
(storage_unit) ? storage_unit : "", (storage_unit) ? storage_unit : "",

View File

@ -73,8 +73,8 @@ irc_sasl_mechanism_plain (const char *sasl_username, const char *sasl_password)
answer_base64 = malloc (length * 4); answer_base64 = malloc (length * 4);
if (answer_base64) if (answer_base64)
{ {
if (weechat_string_encode_base64 (string, length - 1, if (weechat_string_base_encode (64, string, length - 1,
answer_base64) < 0) answer_base64) < 0)
{ {
free (answer_base64); free (answer_base64);
answer_base64 = NULL; answer_base64 = NULL;
@ -180,7 +180,7 @@ irc_sasl_mechanism_ecdsa_nist256p_challenge (struct t_irc_server *server,
data = malloc (strlen (data_base64) + 1); data = malloc (strlen (data_base64) + 1);
if (!data) if (!data)
return NULL; 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 */ /* read file with private key */
str_privkey = irc_sasl_get_key_content (server, sasl_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); pubkey_base64 = malloc ((x.size + 1 + 1) * 4);
if (pubkey_base64) if (pubkey_base64)
{ {
if (weechat_string_encode_base64 (pubkey, x.size + 1, if (weechat_string_base_encode (64, pubkey, x.size + 1,
pubkey_base64) >= 0) pubkey_base64) >= 0)
{ {
weechat_printf ( weechat_printf (
server->buffer, server->buffer,
@ -299,7 +299,8 @@ irc_sasl_mechanism_ecdsa_nist256p_challenge (struct t_irc_server *server,
answer_base64 = malloc ((length + 1) * 4); answer_base64 = malloc ((length + 1) * 4);
if (answer_base64) 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); free (answer_base64);
answer_base64 = NULL; answer_base64 = NULL;
@ -364,7 +365,7 @@ irc_sasl_dh (const char *data_base64,
data = malloc (strlen (data_base64) + 1); data = malloc (strlen (data_base64) + 1);
if (!data) if (!data)
goto dhend; 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; ptr_data = (unsigned char *)data;
/* extract prime number */ /* extract prime number */
@ -520,8 +521,8 @@ irc_sasl_mechanism_dh_blowfish (const char *data_base64,
answer_base64 = malloc ((length_answer + 1) * 4); answer_base64 = malloc ((length_answer + 1) * 4);
if (answer_base64) if (answer_base64)
{ {
if (weechat_string_encode_base64 (answer, length_answer, if (weechat_string_base_encode (64, answer, length_answer,
answer_base64) < 0) answer_base64) < 0)
{ {
free (answer_base64); free (answer_base64);
answer_base64 = NULL; answer_base64 = NULL;
@ -649,8 +650,8 @@ irc_sasl_mechanism_dh_aes (const char *data_base64,
answer_base64 = malloc ((length_answer + 1) * 4); answer_base64 = malloc ((length_answer + 1) * 4);
if (answer_base64) if (answer_base64)
{ {
if (weechat_string_encode_base64 (answer, length_answer, if (weechat_string_base_encode (64, answer, length_answer,
answer_base64) < 0) answer_base64) < 0)
{ {
free (answer_base64); free (answer_base64);
answer_base64 = NULL; answer_base64 = NULL;

View File

@ -94,6 +94,45 @@ plugin_api_ngettext (const char *single, const char *plural, int count)
return NG_(single, plural, count); return NG_(single, plural, 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. * Frees an option.
*/ */

View File

@ -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_gettext (const char *string);
extern const char *plugin_api_ngettext (const char *single, const char *plural, extern const char *plugin_api_ngettext (const char *single, const char *plural,
int count); 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 */ /* config */
extern void plugin_api_config_file_option_free (struct t_config_option *option); extern void plugin_api_config_file_option_free (struct t_config_option *option);

View File

@ -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_free_split_command = &string_free_split_command;
new_plugin->string_format_size = &string_format_size; new_plugin->string_format_size = &string_format_size;
new_plugin->string_remove_color = &gui_color_decode; new_plugin->string_remove_color = &gui_color_decode;
new_plugin->string_encode_base64 = &string_encode_base64; new_plugin->string_base_encode = &plugin_api_string_base_encode;
new_plugin->string_decode_base64 = &string_decode_base64; new_plugin->string_base_decode = &plugin_api_string_base_decode;
new_plugin->string_hex_dump = &string_hex_dump; new_plugin->string_hex_dump = &string_hex_dump;
new_plugin->string_is_command_char = &string_is_command_char; new_plugin->string_is_command_char = &string_is_command_char;
new_plugin->string_input_for_buffer = &string_input_for_buffer; new_plugin->string_input_for_buffer = &string_input_for_buffer;

View File

@ -214,8 +214,8 @@ relay_websocket_build_handshake (struct t_relay_client *client)
length = gcry_md_get_algo_dlen (GCRY_MD_SHA1); length = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
gcry_md_write (hd, key, strlen (key)); gcry_md_write (hd, key, strlen (key));
result = gcry_md_read (hd, GCRY_MD_SHA1); result = gcry_md_read (hd, GCRY_MD_SHA1);
if (weechat_string_encode_base64 ((char *)result, length, if (weechat_string_base_encode (64, (char *)result, length,
sec_websocket_accept) < 0) sec_websocket_accept) < 0)
{ {
sec_websocket_accept[0] = '\0'; sec_websocket_accept[0] = '\0';
} }

View File

@ -67,7 +67,7 @@ struct timeval;
* please change the date with current one; for a second change at same * please change the date with current one; for a second change at same
* date, increment the 01, otherwise please keep 01. * 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 */ /* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \ #define WEECHAT_PLUGIN_NAME(__name) \
@ -323,8 +323,9 @@ struct t_weechat_plugin
void (*string_free_split_command) (char **split_command); void (*string_free_split_command) (char **split_command);
char *(*string_format_size) (unsigned long long size); char *(*string_format_size) (unsigned long long size);
char *(*string_remove_color) (const char *string, const char *replacement); char *(*string_remove_color) (const char *string, const char *replacement);
int (*string_encode_base64) (const char *from, int length, char *to); int (*string_base_encode) (int base, const char *from, int length,
int (*string_decode_base64) (const char *from, char *to); char *to);
int (*string_base_decode) (int base, const char *from, char *to);
char *(*string_hex_dump) (const char *data, int data_size, char *(*string_hex_dump) (const char *data, int data_size,
int bytes_per_line, const char *prefix, int bytes_per_line, const char *prefix,
const char *suffix); const char *suffix);
@ -1224,10 +1225,11 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
(weechat_plugin->string_format_size)(__size) (weechat_plugin->string_format_size)(__size)
#define weechat_string_remove_color(__string, __replacement) \ #define weechat_string_remove_color(__string, __replacement) \
(weechat_plugin->string_remove_color)(__string, __replacement) (weechat_plugin->string_remove_color)(__string, __replacement)
#define weechat_string_encode_base64(__from, __length, __to) \ #define weechat_string_base_encode(__base, __from, __length, __to) \
(weechat_plugin->string_encode_base64)(__from, __length, __to) (weechat_plugin->string_base_encode)(__base, __from, __length, \
#define weechat_string_decode_base64(__from, __to) \ __to)
(weechat_plugin->string_decode_base64)(__from, __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, \ #define weechat_string_hex_dump(__data, __data_size, __bytes_per_line, \
__prefix, __suffix) \ __prefix, __suffix) \
(weechat_plugin->string_hex_dump)(__data, __data_size, \ (weechat_plugin->string_hex_dump)(__data, __data_size, \

View File

@ -1333,45 +1333,57 @@ TEST(CoreString, FormatSize)
/* /*
* Tests functions: * Tests functions:
* string_encode_base16 * string_base16_encode
* string_decode_base16 * string_base16_decode
*/ */
TEST(CoreString, Base16) TEST(CoreString, Base16)
{ {
int i, length;
char str[1024]; 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)); /* string_base16_encode */
LONGS_EQUAL(-1, string_encode_base16 (NULL, 0, str)); LONGS_EQUAL(-1, string_base16_encode (NULL, 0, NULL));
LONGS_EQUAL(-1, string_encode_base16 ("", 0, NULL)); LONGS_EQUAL(-1, string_base16_encode (NULL, 0, str));
LONGS_EQUAL(-1, string_base16_encode ("", 0, NULL));
str[0] = 0xAA; str[0] = 0xAA;
LONGS_EQUAL(0, string_encode_base16 ("", -1, str)); LONGS_EQUAL(0, string_base16_encode ("", -1, str));
BYTES_EQUAL(0x0, str[0]); BYTES_EQUAL(0x0, str[0]);
str[0] = 0xAA; str[0] = 0xAA;
LONGS_EQUAL(0, string_encode_base16 ("", 0, str)); LONGS_EQUAL(0, string_base16_encode ("", 0, str));
BYTES_EQUAL(0x0, str[0]); BYTES_EQUAL(0x0, str[0]);
LONGS_EQUAL(6, string_encode_base16 ("abc", 3, str)); for (i = 0; str_base16[i][0]; i++)
STRCMP_EQUAL("616263", str); {
LONGS_EQUAL(32, string_encode_base16 ("this is a *test*", 16, str)); length = strlen (str_base16[i][1]);
STRCMP_EQUAL("746869732069732061202A746573742A", str); LONGS_EQUAL(length, string_base16_encode (str_base16[i][0],
strlen (str_base16[i][0]),
str));
STRCMP_EQUAL(str_base16[i][1], str);
}
/* string_decode_base16 */ /* string_base16_decode */
LONGS_EQUAL(0, string_decode_base16 (NULL, NULL)); LONGS_EQUAL(0, string_base16_decode (NULL, NULL));
LONGS_EQUAL(0, string_decode_base16 (NULL, str)); LONGS_EQUAL(0, string_base16_decode (NULL, str));
LONGS_EQUAL(0, string_decode_base16 ("", NULL)); LONGS_EQUAL(0, string_base16_decode ("", NULL));
LONGS_EQUAL(0, string_decode_base16 ("", str)); LONGS_EQUAL(0, string_base16_decode ("", str));
LONGS_EQUAL(3, string_decode_base16 ("616263", str)); for (i = 0; str_base16[i][0]; i++)
STRCMP_EQUAL("abc", str); {
LONGS_EQUAL(16, string_decode_base16 ("746869732069732061202A746573742A", length = strlen (str_base16[i][0]);
str)); LONGS_EQUAL(length, string_base16_decode (str_base16[i][1], str));
STRCMP_EQUAL("this is a *test*", str); STRCMP_EQUAL(str_base16[i][0], str);
}
} }
/* /*
* Tests functions: * Tests functions:
* string_encode_base32 * string_base32_encode
* string_decode_base32 * string_base32_decode
*/ */
TEST(CoreString, Base32) TEST(CoreString, Base32)
@ -1384,7 +1396,7 @@ TEST(CoreString, Base32)
{ "B", "II======" }, { "B", "II======" },
{ "C", "IM======" }, { "C", "IM======" },
{ "D", "IQ======" }, { "D", "IQ======" },
{ "abc", "MFRGG===" }, { "abcdefgh", "MFRGGZDFMZTWQ===" },
{ "This is a test.", "KRUGS4ZANFZSAYJAORSXG5BO" }, { "This is a test.", "KRUGS4ZANFZSAYJAORSXG5BO" },
{ "This is a test..", "KRUGS4ZANFZSAYJAORSXG5BOFY======" }, { "This is a test..", "KRUGS4ZANFZSAYJAORSXG5BOFY======" },
{ "This is a test...", "KRUGS4ZANFZSAYJAORSXG5BOFYXA====" }, { "This is a test...", "KRUGS4ZANFZSAYJAORSXG5BOFYXA====" },
@ -1394,42 +1406,42 @@ TEST(CoreString, Base32)
"FY======" }, "FY======" },
{ NULL, NULL } }; { NULL, NULL } };
/* string_encode_base32 */ /* string_base32_encode */
LONGS_EQUAL(-1, string_encode_base32 (NULL, 0, NULL)); LONGS_EQUAL(-1, string_base32_encode (NULL, 0, NULL));
LONGS_EQUAL(-1, string_encode_base32 (NULL, 0, str)); LONGS_EQUAL(-1, string_base32_encode (NULL, 0, str));
LONGS_EQUAL(-1, string_encode_base32 ("", 0, NULL)); LONGS_EQUAL(-1, string_base32_encode ("", 0, NULL));
str[0] = 0xAA; str[0] = 0xAA;
LONGS_EQUAL(0, string_encode_base32 ("", -1, str)); LONGS_EQUAL(0, string_base32_encode ("", -1, str));
BYTES_EQUAL(0x0, str[0]); BYTES_EQUAL(0x0, str[0]);
str[0] = 0xAA; str[0] = 0xAA;
LONGS_EQUAL(0, string_encode_base32 ("", 0, str)); LONGS_EQUAL(0, string_base32_encode ("", 0, str));
BYTES_EQUAL(0x0, str[0]); BYTES_EQUAL(0x0, str[0]);
for (i = 0; str_base32[i][0]; i++) for (i = 0; str_base32[i][0]; i++)
{ {
length = strlen (str_base32[i][1]); 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]), strlen (str_base32[i][0]),
str)); str));
STRCMP_EQUAL(str_base32[i][1], str); STRCMP_EQUAL(str_base32[i][1], str);
} }
/* string_decode_base32 */ /* string_base32_decode */
LONGS_EQUAL(-1, string_decode_base32 (NULL, NULL)); LONGS_EQUAL(-1, string_base32_decode (NULL, NULL));
LONGS_EQUAL(-1, string_decode_base32 (NULL, str)); LONGS_EQUAL(-1, string_base32_decode (NULL, str));
LONGS_EQUAL(-1, string_decode_base32 ("", NULL)); LONGS_EQUAL(-1, string_base32_decode ("", NULL));
LONGS_EQUAL(0, string_decode_base32 ("", str)); LONGS_EQUAL(0, string_base32_decode ("", str));
for (i = 0; str_base32[i][0]; i++) for (i = 0; str_base32[i][0]; i++)
{ {
length = strlen (str_base32[i][0]); 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); STRCMP_EQUAL(str_base32[i][0], str);
} }
} }
/* /*
* Tests functions: * Tests functions:
* string_encode_base64 * string_base64_encode
* string_decode_base64 * string_base64_decode
*/ */
TEST(CoreString, Base64) TEST(CoreString, Base64)
@ -1459,34 +1471,34 @@ TEST(CoreString, Base64)
"QW5vdGhlciBleGFtcGxlIGZvciBiYXNlNjQuLi4=" }, "QW5vdGhlciBleGFtcGxlIGZvciBiYXNlNjQuLi4=" },
{ NULL, NULL } }; { NULL, NULL } };
/* string_encode_base64 */ /* string_base64_encode */
LONGS_EQUAL(-1, string_encode_base64 (NULL, 0, NULL)); LONGS_EQUAL(-1, string_base64_encode (NULL, 0, NULL));
LONGS_EQUAL(-1, string_encode_base64 (NULL, 0, str)); LONGS_EQUAL(-1, string_base64_encode (NULL, 0, str));
LONGS_EQUAL(-1, string_encode_base64 ("", 0, NULL)); LONGS_EQUAL(-1, string_base64_encode ("", 0, NULL));
str[0] = 0xAA; str[0] = 0xAA;
LONGS_EQUAL(0, string_encode_base64 ("", -1, str)); LONGS_EQUAL(0, string_base64_encode ("", -1, str));
BYTES_EQUAL(0x0, str[0]); BYTES_EQUAL(0x0, str[0]);
str[0] = 0xAA; str[0] = 0xAA;
LONGS_EQUAL(0, string_encode_base64 ("", 0, str)); LONGS_EQUAL(0, string_base64_encode ("", 0, str));
BYTES_EQUAL(0x0, str[0]); BYTES_EQUAL(0x0, str[0]);
for (i = 0; str_base64[i][0]; i++) for (i = 0; str_base64[i][0]; i++)
{ {
length = strlen (str_base64[i][1]); 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]), strlen (str_base64[i][0]),
str)); str));
STRCMP_EQUAL(str_base64[i][1], str); STRCMP_EQUAL(str_base64[i][1], str);
} }
/* string_decode_base64 */ /* string_base64_decode */
LONGS_EQUAL(0, string_decode_base64 (NULL, NULL)); LONGS_EQUAL(0, string_base64_decode (NULL, NULL));
LONGS_EQUAL(0, string_decode_base64 (NULL, str)); LONGS_EQUAL(0, string_base64_decode (NULL, str));
LONGS_EQUAL(0, string_decode_base64 ("", NULL)); LONGS_EQUAL(0, string_base64_decode ("", NULL));
LONGS_EQUAL(0, string_decode_base64 ("", str)); LONGS_EQUAL(0, string_base64_decode ("", str));
for (i = 0; str_base64[i][0]; i++) for (i = 0; str_base64[i][0]; i++)
{ {
length = strlen (str_base64[i][0]); 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); STRCMP_EQUAL(str_base64[i][0], str);
} }
} }