diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-string.c | 105 | ||||
-rw-r--r-- | src/core/wee-string.h | 3 | ||||
-rw-r--r-- | src/plugins/plugin.c | 1 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 10 |
4 files changed, 118 insertions, 1 deletions
diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 2f1adb7d9..389d12c62 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -2669,6 +2669,111 @@ string_decode_base64 (const char *from, char *to) } /* + * Dumps a data buffer as hexadecimal + ascii. + * + * Note: result must be freed after use. + */ + +char * +string_hex_dump (const char *data, int data_size, int bytes_per_line, + const char *prefix, const char *suffix) +{ + char *buf, *str_hexa, *str_ascii, str_format_line[64], *str_line; + int length_hexa, length_ascii, length_prefix, length_suffix, length_line; + int hexa_pos, ascii_pos, i; + + if (!data || (data_size < 1) || (bytes_per_line < 1)) + return NULL; + + str_hexa = NULL; + str_ascii = NULL; + str_line = NULL; + buf = NULL; + + length_hexa = bytes_per_line * 3; + str_hexa = malloc (length_hexa + 1); + if (!str_hexa) + goto end; + + length_ascii = bytes_per_line * 2; + str_ascii = malloc (length_ascii + 1); + if (!str_ascii) + goto end; + + length_prefix = (prefix) ? strlen (prefix) : 0; + length_suffix = (suffix) ? strlen (suffix) : 0; + + length_line = length_prefix + (bytes_per_line * 3) + 2 + length_ascii + + length_suffix; + str_line = malloc (length_line + 1); + if (!str_line) + goto end; + + buf = malloc ((((data_size / bytes_per_line) + 1) * (length_line + 1)) + 1); + if (!buf) + goto end; + buf[0] = '\0'; + + snprintf (str_format_line, sizeof (str_format_line), + "%%s%%-%ds %%-%ds%%s", + length_hexa, + length_ascii); + + hexa_pos = 0; + ascii_pos = 0; + for (i = 0; i < data_size; i++) + { + snprintf (str_hexa + hexa_pos, 4, + "%02X ", (unsigned char)(data[i])); + hexa_pos += 3; + snprintf (str_ascii + ascii_pos, 3, "%c ", + ((((unsigned char)data[i]) < 32) + || (((unsigned char)data[i]) > 127)) ? + '.' : (unsigned char)(data[i])); + ascii_pos += 2; + if (ascii_pos == bytes_per_line * 2) + { + if (buf[0]) + strcat (buf, "\n"); + str_ascii[ascii_pos - 1] = '\0'; + snprintf (str_line, length_line + 1, + str_format_line, + (prefix) ? prefix : "", + str_hexa, + str_ascii, + (suffix) ? suffix : ""); + strcat (buf, str_line); + hexa_pos = 0; + ascii_pos = 0; + } + } + if (ascii_pos > 0) + { + if (buf[0]) + strcat (buf, "\n"); + str_ascii[ascii_pos - 1] = '\0'; + str_ascii[ascii_pos] = '\0'; + snprintf (str_line, length_line + 1, + str_format_line, + (prefix) ? prefix : "", + str_hexa, + str_ascii, + (suffix) ? suffix : ""); + strcat (buf, str_line); + } + +end: + if (str_hexa) + free (str_hexa); + if (str_ascii) + free (str_ascii); + if (str_line) + free (str_line); + + return buf; +} + +/* * Checks if a string is a command. * * Returns: diff --git a/src/core/wee-string.h b/src/core/wee-string.h index 45df857b5..55899dead 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -91,6 +91,9 @@ extern void string_encode_base16 (const char *from, int length, char *to); extern int string_decode_base16 (const char *from, char *to); extern void 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, + const char *prefix, const char *suffix); extern int string_is_command_char (const char *string); extern const char *string_input_for_buffer (const char *string); extern char *string_replace_with_callback (const char *string, diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 7bd76dc5b..960310d1f 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -634,6 +634,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv) new_plugin->string_remove_color = &gui_color_decode; new_plugin->string_encode_base64 = &string_encode_base64; new_plugin->string_decode_base64 = &string_decode_base64; + new_plugin->string_hex_dump = &string_hex_dump; new_plugin->string_is_command_char = &string_is_command_char; new_plugin->string_input_for_buffer = &string_input_for_buffer; new_plugin->string_eval_expression = &eval_expression; diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index ca3e9f86b..801ab78e9 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -57,7 +57,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 "20150818-01" +#define WEECHAT_PLUGIN_API_VERSION "20150822-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -308,6 +308,9 @@ struct t_weechat_plugin char *(*string_remove_color) (const char *string, const char *replacement); void (*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, + const char *suffix); int (*string_is_command_char) (const char *string); const char *(*string_input_for_buffer) (const char *string); char *(*string_eval_expression )(const char *expr, @@ -1098,6 +1101,11 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin); (weechat_plugin->string_encode_base64)(__from, __length, __to) #define weechat_string_decode_base64(__from, __to) \ (weechat_plugin->string_decode_base64)(__from, __to) +#define weechat_string_hex_dump(__data, __data_size, __bytes_per_line, \ + __prefix, __suffix) \ + (weechat_plugin->string_hex_dump)(__data, __data_size, \ + __bytes_per_line, __prefix, \ + __suffix) #define weechat_string_is_command_char(__string) \ (weechat_plugin->string_is_command_char)(__string) #define weechat_string_input_for_buffer(__string) \ |