api: return integer in function string_encode_base64

v2.8-utf8proc
Sébastien Helleu 2018-11-02 14:09:23 +01:00
parent 74a17d821f
commit 8848b0e22a
13 changed files with 137 additions and 85 deletions

View File

@ -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::

View File

@ -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]

View File

@ -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]

View File

@ -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]

View File

@ -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]

View File

@ -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",

View File

@ -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;
}
/*

View File

@ -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,

View File

@ -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);
}
}

View File

@ -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)

View File

@ -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);

View File

@ -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,

View File

@ -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);
}
}