summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2018-11-02 14:09:23 +0100
committerSébastien Helleu <flashcode@flashtux.org>2018-11-02 14:09:23 +0100
commit8848b0e22aaba6f3d7116c7137ede3b43f393a85 (patch)
tree65ffb8531d608f899bffcd47bc8721ec059cdc26 /src
parent74a17d821f066c41f1450e9fae805c1711482265 (diff)
downloadweechat-8848b0e22aaba6f3d7116c7137ede3b43f393a85.zip
api: return integer in function string_encode_base64
Diffstat (limited to 'src')
-rw-r--r--src/core/wee-network.c3
-rw-r--r--src/core/wee-string.c40
-rw-r--r--src/core/wee-string.h2
-rw-r--r--src/gui/curses/gui-curses-window.c12
-rw-r--r--src/plugins/irc/irc-sasl.c55
-rw-r--r--src/plugins/relay/relay-websocket.c6
-rw-r--r--src/plugins/weechat-plugin.h4
7 files changed, 85 insertions, 37 deletions
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,