summaryrefslogtreecommitdiff
path: root/src/core/wee-string.c
diff options
context:
space:
mode:
authorSebastien Helleu <flashcode@flashtux.org>2013-08-02 19:19:25 +0200
committerSebastien Helleu <flashcode@flashtux.org>2013-08-02 19:19:25 +0200
commit663de422849a591c2b7fdf47fbabdf6e2ecb98da (patch)
tree9aeb522a13e4232c78ccef92561ab855acd6767e /src/core/wee-string.c
parentfd5fc2e5a35926387eb332f612752a6b0726e10e (diff)
parentade379ac114ba76a142d9f430defc9579ead029a (diff)
downloadweechat-663de422849a591c2b7fdf47fbabdf6e2ecb98da.zip
Merge branch 'secured-data'
Diffstat (limited to 'src/core/wee-string.c')
-rw-r--r--src/core/wee-string.c96
1 files changed, 86 insertions, 10 deletions
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index 94402b52c..34ad3b1bb 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -1797,6 +1797,78 @@ string_format_size (unsigned long long size)
}
/*
+ * Encodes a string in base16 (hexadecimal).
+ *
+ * Argument "length" is number of bytes in "from" to convert (commonly
+ * strlen(from)).
+ */
+
+void
+string_encode_base16 (const char *from, int length, char *to)
+{
+ int i;
+ const char *hexa = "0123456789ABCDEF";
+ char *ptr_to;
+
+ 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;
+ }
+ ptr_to[0] = '\0';
+}
+
+/*
+ * Decodes a base16 string (hexadecimal).
+ *
+ * Returns length of string in "*to" (it does not count final \0).
+ */
+
+int
+string_decode_base16 (const char *from, char *to)
+{
+ int length, to_length, i, pos;
+ unsigned char *ptr_to, value;
+
+ length = strlen (from) / 2;
+
+ ptr_to = (unsigned char *)to;
+ ptr_to[0] = '\0';
+ to_length = 0;
+
+ for (i = 0; i < length; i++)
+ {
+ pos = i * 2;
+ value = 0;
+ /* 4 bits on the left */
+ if ((from[pos] >= '0') && (from[pos] <= '9'))
+ value |= (from[pos] - '0') << 4;
+ else if ((from[pos] >= 'a') && (from[pos] <= 'f'))
+ value |= (from[pos] - 'a' + 10) << 4;
+ else if ((from[pos] >= 'A') && (from[pos] <= 'F'))
+ value |= (from[pos] - 'A' + 10) << 4;
+ /* 4 bits on the right */
+ pos++;
+ if ((from[pos] >= '0') && (from[pos] <= '9'))
+ value |= from[pos] - '0';
+ else if ((from[pos] >= 'a') && (from[pos] <= 'f'))
+ value |= from[pos] - 'a' + 10;
+ else if ((from[pos] >= 'A') && (from[pos] <= 'F'))
+ value |= from[pos] - 'A' + 10;
+
+ ptr_to[0] = value;
+ ptr_to++;
+ to_length++;
+ }
+ ptr_to[0] = '\0';
+
+ return to_length;
+}
+
+/*
* Converts 3 bytes of 8 bits in 4 bytes of 6 bits.
*/
@@ -2031,19 +2103,25 @@ string_input_for_buffer (const char *string)
char *
string_replace_with_callback (const char *string,
+ const char *prefix,
+ const char *suffix,
char *(*callback)(void *data, const char *text),
void *callback_data,
int *errors)
{
- int length, length_value, index_string, index_result;
+ int length_prefix, length_suffix, length, length_value, index_string;
+ int index_result;
char *result, *result2, *key, *value;
const char *pos_end_name;
*errors = 0;
- if (!string)
+ if (!string || !prefix || !prefix[0] || !suffix || !suffix[0])
return NULL;
+ length_prefix = strlen (prefix);
+ length_suffix = strlen (suffix);
+
length = strlen (string) + 1;
result = malloc (length);
if (result)
@@ -2053,19 +2131,18 @@ string_replace_with_callback (const char *string,
while (string[index_string])
{
if ((string[index_string] == '\\')
- && (string[index_string + 1] == '$'))
+ && (string[index_string + 1] == prefix[0]))
{
index_string++;
result[index_result++] = string[index_string++];
}
- else if ((string[index_string] == '$')
- && (string[index_string + 1] == '{'))
+ else if (strncmp (string + index_string, prefix, length_prefix) == 0)
{
- pos_end_name = strchr (string + index_string + 2, '}');
+ pos_end_name = strstr (string + index_string + length_prefix, suffix);
if (pos_end_name)
{
- key = string_strndup (string + index_string + 2,
- pos_end_name - (string + index_string + 2));
+ key = string_strndup (string + index_string + length_prefix,
+ pos_end_name - (string + index_string + length_prefix));
if (key)
{
value = (*callback) (callback_data, key);
@@ -2086,7 +2163,7 @@ string_replace_with_callback (const char *string,
strcpy (result + index_result, value);
index_result += length_value;
index_string += pos_end_name - string -
- index_string + 1;
+ index_string + length_suffix;
free (value);
}
else
@@ -2094,7 +2171,6 @@ string_replace_with_callback (const char *string,
result[index_result++] = string[index_string++];
(*errors)++;
}
-
free (key);
}
else