summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog.asciidoc1
-rw-r--r--doc/en/weechat_plugin_api.en.asciidoc41
-rw-r--r--doc/fr/weechat_plugin_api.fr.asciidoc42
-rw-r--r--doc/it/weechat_plugin_api.it.asciidoc44
-rw-r--r--doc/ja/weechat_plugin_api.ja.asciidoc44
-rw-r--r--src/core/wee-string.c105
-rw-r--r--src/core/wee-string.h3
-rw-r--r--src/plugins/plugin.c1
-rw-r--r--src/plugins/weechat-plugin.h10
-rw-r--r--tests/unit/core/test-string.cpp70
10 files changed, 360 insertions, 1 deletions
diff --git a/ChangeLog.asciidoc b/ChangeLog.asciidoc
index 1d15d2fba..ce0a25ecf 100644
--- a/ChangeLog.asciidoc
+++ b/ChangeLog.asciidoc
@@ -19,6 +19,7 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes]
=== New features
+* api: add function string_hex_dump()
* api: add argument "length" in function utf8_is_valid()
=== Bugs fixed
diff --git a/doc/en/weechat_plugin_api.en.asciidoc b/doc/en/weechat_plugin_api.en.asciidoc
index 2398e45e7..823f80252 100644
--- a/doc/en/weechat_plugin_api.en.asciidoc
+++ b/doc/en/weechat_plugin_api.en.asciidoc
@@ -1818,6 +1818,47 @@ length = weechat_string_decode_base64 (string, result);
[NOTE]
This function is not available in scripting API.
+==== string_hex_dump
+
+_WeeChat ≥ 1.4._
+
+Display a dump of data as hexadecimal and ascii bytes.
+
+Prototype:
+
+[source,C]
+----
+char *string_hex_dump (const char *data, int data_size, int bytes_per_line,
+ const char *prefix, const char *suffix);
+----
+
+Arguments:
+
+* 'data': the data to dump
+* 'data_size': number of bytes to dump in 'data'
+* 'bytes_per_line': number of bytes to display in each line
+* 'prefix': the prefix to display at the beginning of each line
+ (optional, can be NULL)
+* 'suffix': the suffix to display at the end of each line
+ (optional, can be NULL)
+
+Return value:
+
+* string with dump of data (must be freed by calling "free" after use)
+
+C example:
+
+[source,C]
+----
+char *string = "abc def-ghi";
+char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL);
+/* dump == " >> 61 62 63 20 64 65 66 2D a b c d e f - \n"
+ " >> 67 68 69 g h i " */
+----
+
+[NOTE]
+This function is not available in scripting API.
+
==== string_is_command_char
_WeeChat ≥ 0.3.2._
diff --git a/doc/fr/weechat_plugin_api.fr.asciidoc b/doc/fr/weechat_plugin_api.fr.asciidoc
index a5895d68a..c769281e6 100644
--- a/doc/fr/weechat_plugin_api.fr.asciidoc
+++ b/doc/fr/weechat_plugin_api.fr.asciidoc
@@ -1851,6 +1851,48 @@ length = weechat_string_decode_base64 (string, result);
[NOTE]
Cette fonction n'est pas disponible dans l'API script.
+==== string_hex_dump
+
+_WeeChat ≥ 1.4._
+
+Afficher les données sous forme d'octets en hexadécimal et ascii.
+
+Prototype :
+
+[source,C]
+----
+char *string_hex_dump (const char *data, int data_size, int bytes_per_line,
+ const char *prefix, const char *suffix);
+----
+
+Paramètres :
+
+* 'data' : les données à afficher
+* 'data_size' : nombre d'octets à afficher dans 'data'
+* 'bytes_per_line' : nombre d'octets à afficher sur chaque ligne
+* 'prefix' : le préfixe à afficher au début de chaque ligne
+ (optionnel, peut être NULL)
+* 'suffix' : le suffixe à afficher à la fin de chaque ligne
+ (optionnel, peut être NULL)
+
+Valeur de retour :
+
+* chaîne avec les données (doit être supprimée par un appel à "free" après
+ utilisation)
+
+Exemple en C :
+
+[source,C]
+----
+char *string = "abc def-ghi";
+char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL);
+/* dump == " >> 61 62 63 20 64 65 66 2D a b c d e f - \n"
+ " >> 67 68 69 g h i " */
+----
+
+[NOTE]
+Cette fonction n'est pas disponible dans l'API script.
+
==== string_is_command_char
_WeeChat ≥ 0.3.2._
diff --git a/doc/it/weechat_plugin_api.it.asciidoc b/doc/it/weechat_plugin_api.it.asciidoc
index 07df84698..f0d3fe226 100644
--- a/doc/it/weechat_plugin_api.it.asciidoc
+++ b/doc/it/weechat_plugin_api.it.asciidoc
@@ -1882,6 +1882,50 @@ length = weechat_string_decode_base64 (string, result);
[NOTE]
Questa funzione non è disponibile nelle API per lo scripting.
+==== string_hex_dump
+
+_WeeChat ≥ 1.4._
+
+// TRANSLATION MISSING
+Display a dump of data as hexadecimal and ascii bytes.
+
+Prototipo:
+
+[source,C]
+----
+char *string_hex_dump (const char *data, int data_size, int bytes_per_line,
+ const char *prefix, const char *suffix);
+----
+
+Argomenti:
+
+// TRANSLATION MISSING
+* 'data': the data to dump
+* 'data_size': number of bytes to dump in 'data'
+* 'bytes_per_line': number of bytes to display in each line
+* 'prefix': the prefix to display at the beginning of each line
+ (optional, can be NULL)
+* 'suffix': the suffix to display at the end of each line
+ (optional, can be NULL)
+
+Valore restituito:
+
+// TRANSLATION MISSING
+* string with dump of data (must be freed by calling "free" after use)
+
+Esempio in C:
+
+[source,C]
+----
+char *string = "abc def-ghi";
+char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL);
+/* dump == " >> 61 62 63 20 64 65 66 2D a b c d e f - \n"
+ " >> 67 68 69 g h i " */
+----
+
+[NOTE]
+Questa funzione non è disponibile nelle API per lo scripting.
+
==== string_is_command_char
_WeeChat ≥ 0.3.2._
diff --git a/doc/ja/weechat_plugin_api.ja.asciidoc b/doc/ja/weechat_plugin_api.ja.asciidoc
index f7f17fb9c..e888da021 100644
--- a/doc/ja/weechat_plugin_api.ja.asciidoc
+++ b/doc/ja/weechat_plugin_api.ja.asciidoc
@@ -1819,6 +1819,50 @@ length = weechat_string_decode_base64 (string, result);
[NOTE]
スクリプト API ではこの関数を利用できません。
+==== string_hex_dump
+
+_WeeChat ≥ 1.4._
+
+// TRANSLATION MISSING
+Display a dump of data as hexadecimal and ascii bytes.
+
+プロトタイプ:
+
+[source,C]
+----
+char *string_hex_dump (const char *data, int data_size, int bytes_per_line,
+ const char *prefix, const char *suffix);
+----
+
+引数:
+
+// TRANSLATION MISSING
+* 'data': the data to dump
+* 'data_size': number of bytes to dump in 'data'
+* 'bytes_per_line': number of bytes to display in each line
+* 'prefix': the prefix to display at the beginning of each line
+ (optional, can be NULL)
+* 'suffix': the suffix to display at the end of each line
+ (optional, can be NULL)
+
+戻り値:
+
+// TRANSLATION MISSING
+* string with dump of data (must be freed by calling "free" after use)
+
+C 言語での使用例:
+
+[source,C]
+----
+char *string = "abc def-ghi";
+char *dump = weechat_string_hex_dump (string, strlen (string), 8, " >> ", NULL);
+/* dump == " >> 61 62 63 20 64 65 66 2D a b c d e f - \n"
+ " >> 67 68 69 g h i " */
+----
+
+[NOTE]
+スクリプト API ではこの関数を利用できません。
+
==== string_is_command_char
_WeeChat バージョン 0.3.2 以上で利用可。_
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) \
diff --git a/tests/unit/core/test-string.cpp b/tests/unit/core/test-string.cpp
index 4171aa5cc..dc33029cd 100644
--- a/tests/unit/core/test-string.cpp
+++ b/tests/unit/core/test-string.cpp
@@ -1125,6 +1125,76 @@ TEST(String, BaseN)
/*
* Tests functions:
+ * string_hex_dump
+ */
+
+TEST(String, Hex_dump)
+{
+ const char *noel_utf8 = "no\xc3\xabl"; /* noël */
+ const char *noel_iso = "no\xebl";
+ char *str;
+
+ POINTERS_EQUAL(NULL, string_hex_dump (NULL, 0, 0, NULL, NULL));
+ POINTERS_EQUAL(NULL, string_hex_dump ("abc", 0, 0, NULL, NULL));
+ POINTERS_EQUAL(NULL, string_hex_dump ("abc", 3, 0, NULL, NULL));
+ POINTERS_EQUAL(NULL, string_hex_dump ("abc", 0, 5, NULL, NULL));
+
+ str = string_hex_dump ("abc", 3, 3, NULL, NULL);
+ STRCMP_EQUAL("61 62 63 a b c ", str);
+
+ str = string_hex_dump ("abc", 3, 3, "", "");
+ STRCMP_EQUAL("61 62 63 a b c ", str);
+
+ str = string_hex_dump ("abc", 3, 3, "(( ", NULL);
+ STRCMP_EQUAL("(( 61 62 63 a b c ", str);
+
+ str = string_hex_dump ("abc", 3, 3, NULL, " ))");
+ STRCMP_EQUAL("61 62 63 a b c ))", str);
+
+ str = string_hex_dump ("abc", 3, 3, "(( ", " ))");
+ STRCMP_EQUAL("(( 61 62 63 a b c ))", str);
+
+ str = string_hex_dump ("abc", 3, 5, NULL, NULL);
+ STRCMP_EQUAL("61 62 63 a b c ", str);
+
+ str = string_hex_dump ("abc", 3, 10, NULL, NULL);
+ STRCMP_EQUAL("61 62 63 a b c ", str);
+
+ str = string_hex_dump ("abc", 3, 2, NULL, NULL);
+ STRCMP_EQUAL("61 62 a b \n"
+ "63 c ",
+ str);
+
+ str = string_hex_dump (noel_utf8, strlen (noel_utf8), 5, NULL, NULL);
+ STRCMP_EQUAL("6E 6F C3 AB 6C n o . . l ", str);
+
+ str = string_hex_dump (noel_utf8, strlen (noel_utf8), 2, NULL, NULL);
+ STRCMP_EQUAL("6E 6F n o \n"
+ "C3 AB . . \n"
+ "6C l ",
+ str);
+ str = string_hex_dump (noel_utf8, strlen (noel_utf8), 2, "( ", NULL);
+ STRCMP_EQUAL("( 6E 6F n o \n"
+ "( C3 AB . . \n"
+ "( 6C l ",
+ str);
+ str = string_hex_dump (noel_utf8, strlen (noel_utf8), 2, "( ", " )");
+ STRCMP_EQUAL("( 6E 6F n o )\n"
+ "( C3 AB . . )\n"
+ "( 6C l )",
+ str);
+
+ str = string_hex_dump (noel_iso, strlen (noel_iso), 5, NULL, NULL);
+ STRCMP_EQUAL("6E 6F EB 6C n o . l ", str);
+
+ str = string_hex_dump (noel_iso, strlen (noel_iso), 2, NULL, NULL);
+ STRCMP_EQUAL("6E 6F n o \n"
+ "EB 6C . l ",
+ str);
+}
+
+/*
+ * Tests functions:
* string_is_command_char
* string_input_for_buffer
*/