From ce1b23311c2ff76a97aba7b35d9f56e72dc912eb Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Tue, 16 Feb 2010 16:57:22 +0100 Subject: [PATCH] Add function "string_decode_base64" in plugin API --- ChangeLog | 5 ++- doc/en/weechat_plugin_api.en.txt | 35 ++++++++++++++- doc/fr/weechat_plugin_api.fr.txt | 34 +++++++++++++- src/core/wee-string.c | 77 ++++++++++++++++++++++++++++++++ src/core/wee-string.h | 1 + src/plugins/plugin.c | 1 + src/plugins/weechat-plugin.h | 5 ++- 7 files changed, 153 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 08cb42daa..202676db1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,7 @@ WeeChat ChangeLog ================= FlashCode -v0.3.2-dev, 2010-02-15 +v0.3.2-dev, 2010-02-16 Version 0.3.2 (under dev!) @@ -20,7 +20,8 @@ Version 0.3.2 (under dev!) input (bug #28754) * api: add "version_number" for function info_get to get WeeChat version as number -* api: add function "string_encode_base64", fix bug with base64 encoding +* api: add functions "string_encode_base64" and "string_decode_base64",, fix + bug with base64 encoding * api: add missing infos in functions buffer_get_integer / buffer_get_string and in buffer infolist * api: add description of arguments for functions hook_info and hook_infolist diff --git a/doc/en/weechat_plugin_api.en.txt b/doc/en/weechat_plugin_api.en.txt index 6c5d3b518..54c1e07fb 100644 --- a/doc/en/weechat_plugin_api.en.txt +++ b/doc/en/weechat_plugin_api.en.txt @@ -1124,7 +1124,8 @@ Arguments: * 'from': string to encode * 'length': length of string to encode (for example `strlen(from)`) -* 'to': pointer to string to store result (must be long enough) +* 'to': pointer to string to store result (must be long enough, result is + longer than initial string) Example: @@ -1135,6 +1136,38 @@ weechat_string_encode_base64 (string, strlen (string), result); /* result == "YWJjZGVmZ2g=" */ ---------------------------------------- +weechat_string_decode_base64 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Decode a base64 string. + +Prototype: + +[source,C] +---------------------------------------- +int weechat_string_decode_base64 (const char *from, char *to); +---------------------------------------- + +Arguments: + +* 'from': string to decode +* 'to': pointer to string to store result (must be long enough, result is + shorter than initial string) + +Return value: + +* length of string stored in *to (does not count final '\0') + +Example: + +[source,C] +---------------------------------------- +char *string = "YWJjZGVmZ2g=", result[128]; +int length; +length = weechat_string_decode_base64 (string, result); +/* length == 8, result == "abcdefgh" */ +---------------------------------------- + [[utf-8]] UTF-8 ~~~~~ diff --git a/doc/fr/weechat_plugin_api.fr.txt b/doc/fr/weechat_plugin_api.fr.txt index 755f8bf45..40c04e3f2 100644 --- a/doc/fr/weechat_plugin_api.fr.txt +++ b/doc/fr/weechat_plugin_api.fr.txt @@ -1139,7 +1139,7 @@ Paramètres : * 'from': chaîne à encoder * 'length': longueur de chaîne à encoder (par exemple `strlen(from)`) * 'to': pointeur vers la chaîne pour stocker le résultat (doit être suffisamment - long) + long, le résultat est plus long que la chaîne initiale) Exemple : @@ -1150,6 +1150,38 @@ weechat_string_encode_base64 (string, strlen (string), result); /* result == "YWJjZGVmZ2g=" */ ---------------------------------------- +weechat_string_decode_base64 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Décode une chaîne base64. + +Prototype : + +[source,C] +---------------------------------------- +int weechat_string_decode_base64 (const char *from, char *to); +---------------------------------------- + +Paramètres : + +* 'from': chaîne à décoder +* 'to': pointeur vers la chaîne pour stocker le résultat (doit être suffisamment + long, le résultat est plus court que la chaîne initiale) + +Valeur de retour : + +* longueur de la chaîne stockée dans *to (ne compte pas le '\0' final) + +Example: + +[source,C] +---------------------------------------- +char *string = "YWJjZGVmZ2g=", result[128]; +int length; +length = weechat_string_decode_base64 (string, result); +/* length == 8, result == "abcdefgh" */ +---------------------------------------- + [[utf-8]] UTF-8 ~~~~~ diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 52162a55e..94c626f9d 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -1333,3 +1333,80 @@ string_encode_base64 (const char *from, int length, char *to) else ptr_to[0] = '\0'; } + +/* + * string_convbase64_6x4_to_8x3 : convert 4 bytes of 6 bits to 3 bytes of 8 bits + */ + +void +string_convbase64_6x4_to_8x3 (const unsigned char *from, unsigned char *to) +{ + to[0] = from[0] << 2 | from[1] >> 4; + to[1] = from[1] << 4 | from[2] >> 2; + to[2] = ((from[2] << 6) & 0xc0) | from[3]; +} + +/* + * string_decode_base64: decode a base64 string + * return length of string in *to + * (does not count final \0) + */ + +int +string_decode_base64 (const char *from, char *to) +{ + const char *ptr_from; + int length, to_length, i; + char *ptr_to; + unsigned char c, in[4], out[3]; + unsigned char base64_table[]="|$$$}rstuvwxyz{$$$$$$$>?" + "@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq"; + + ptr_from = from; + ptr_to = to; + + ptr_to[0] = '\0'; + to_length = 0; + + while (ptr_from && ptr_from[0]) + { + length = 0; + for (i = 0; i < 4; i++) + { + c = 0; + while (ptr_from[0] && (c == 0)) + { + c = (unsigned char) ptr_from[0]; + ptr_from++; + c = ((c < 43) || (c > 122)) ? 0 : base64_table[c - 43]; + if (c) + c = (c == '$') ? 0 : c - 61; + } + if (ptr_from[0]) + { + length++; + if (c) + in[i] = c - 1; + } + else + { + in[i] = '\0'; + break; + } + } + if (length) + { + string_convbase64_6x4_to_8x3 (in, out); + for (i = 0; i < length - 1; i++) + { + ptr_to[0] = out[i]; + ptr_to++; + to_length++; + } + } + } + + ptr_to[0] = '\0'; + + return to_length; +} diff --git a/src/core/wee-string.h b/src/core/wee-string.h index ea77bb0f5..2666b79c2 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -57,5 +57,6 @@ extern char *string_iconv_from_internal (const char *charset, extern void string_iconv_fprintf (FILE *file, const char *data, ...); extern char *string_format_size (unsigned long size); extern void string_encode_base64 (const char *from, int length, char *to); +extern int string_decode_base64 (const char *from, char *to); #endif /* wee-string.h */ diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 4b4258471..5cebfe8ea 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -385,6 +385,7 @@ plugin_load (const char *filename) new_plugin->string_format_size = &string_format_size; 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->utf8_has_8bits = &utf8_has_8bits; new_plugin->utf8_is_valid = &utf8_is_valid; diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 467e9afaa..f364ecc26 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -34,7 +34,7 @@ struct t_weelist; struct timeval; /* API version (used to check that plugin has same API and can be loaded) */ -#define WEECHAT_PLUGIN_API_VERSION "20100215-01" +#define WEECHAT_PLUGIN_API_VERSION "20100216-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -171,6 +171,7 @@ struct t_weechat_plugin char *(*string_format_size) (unsigned long size); 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); /* UTF-8 strings */ int (*utf8_has_8bits) (const char *string); @@ -720,6 +721,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin); weechat_plugin->string_remove_color(__string, __replacement) #define weechat_string_encode_base64(__from, __length, __to) \ weechat_plugin->string_encode_base64(__from, __length, __to) +#define weechat_string_decode_base64(__from, __to) \ + weechat_plugin->string_decode_base64(__from, __to) /* UTF-8 strings */ #define weechat_utf8_has_8bits(__string) \