summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2018-11-02 14:20:16 +0100
committerSébastien Helleu <flashcode@flashtux.org>2018-11-02 14:20:16 +0100
commit6d72868e15b5b4ffd7f1cb11bd79ddfdf78fec9a (patch)
treef674529284fa181f38e05200f53448c297a2101c /src
parent8848b0e22aaba6f3d7116c7137ede3b43f393a85 (diff)
downloadweechat-6d72868e15b5b4ffd7f1cb11bd79ddfdf78fec9a.zip
api: return integer in function string_encode_base16
Diffstat (limited to 'src')
-rw-r--r--src/core/wee-secure-config.c9
-rw-r--r--src/core/wee-string.c40
-rw-r--r--src/core/wee-string.h2
3 files changed, 26 insertions, 25 deletions
diff --git a/src/core/wee-secure-config.c b/src/core/wee-secure-config.c
index 6d0d93fb8..5e1332019 100644
--- a/src/core/wee-secure-config.c
+++ b/src/core/wee-secure-config.c
@@ -370,9 +370,12 @@ secure_config_data_write_map_cb (void *data,
buffer_base16 = malloc ((length_buffer * 2) + 1);
if (buffer_base16)
{
- string_encode_base16 (buffer, length_buffer, buffer_base16);
- config_file_write_line (config_file, key,
- "\"%s\"", buffer_base16);
+ if (string_encode_base16 (buffer, length_buffer,
+ buffer_base16) >= 0)
+ {
+ config_file_write_line (config_file, key,
+ "\"%s\"", buffer_base16);
+ }
free (buffer_base16);
}
free (buffer);
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index c7a83ad5e..32afd060e 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -2702,27 +2702,29 @@ string_format_size (unsigned long long size)
*
* 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_base16 (const char *from, int length, char *to)
{
- int i;
+ int i, count;
const char *hexa = "0123456789ABCDEF";
- char *ptr_to;
if (!from || !to)
- return;
+ return -1;
+
+ count = 0;
- ptr_to = to;
- ptr_to[0] = '\0';
for (i = 0; i < length; i++)
{
- ptr_to[0] = hexa[((unsigned char)from[i]) / 16];
- ptr_to[1] = hexa[((unsigned char)from[i]) % 16];
- ptr_to += 2;
+ to[count++] = hexa[((unsigned char)from[i]) / 16];
+ to[count++] = hexa[((unsigned char)from[i]) % 16];
}
- ptr_to[0] = '\0';
+ to[count] = '\0';
+
+ return count;
}
/*
@@ -2734,17 +2736,15 @@ string_encode_base16 (const char *from, int length, char *to)
int
string_decode_base16 (const char *from, char *to)
{
- int length, to_length, i, pos;
- unsigned char *ptr_to, value;
+ int length, i, pos, count;
+ unsigned char value;
if (!from || !to)
return 0;
- length = strlen (from) / 2;
+ count = 0;
- ptr_to = (unsigned char *)to;
- ptr_to[0] = '\0';
- to_length = 0;
+ length = strlen (from) / 2;
for (i = 0; i < length; i++)
{
@@ -2766,13 +2766,11 @@ string_decode_base16 (const char *from, char *to)
else if ((from[pos] >= 'A') && (from[pos] <= 'F'))
value |= from[pos] - 'A' + 10;
- ptr_to[0] = value;
- ptr_to++;
- to_length++;
+ to[count++] = value;
}
- ptr_to[0] = '\0';
+ to[count] = '\0';
- return to_length;
+ return count;
}
/*
diff --git a/src/core/wee-string.h b/src/core/wee-string.h
index 2f9be0e7e..4709f73d7 100644
--- a/src/core/wee-string.h
+++ b/src/core/wee-string.h
@@ -105,7 +105,7 @@ 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 void string_encode_base16 (const char *from, int length, char *to);
+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);