api: add function string_hex_dump()

v2.8-utf8proc
Sébastien Helleu 2015-08-22 09:30:08 +02:00
parent 8b47243516
commit 951d1f91a4
10 changed files with 360 additions and 1 deletions

View File

@ -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

View File

@ -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._

View File

@ -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._

View File

@ -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._

View File

@ -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 以上で利用可。_

View File

@ -2668,6 +2668,111 @@ string_decode_base64 (const char *from, char *to)
return to_length;
}
/*
* 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.
*

View File

@ -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,

View File

@ -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;

View File

@ -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) \

View File

@ -1123,6 +1123,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