Add function "string_decode_base64" in plugin API

v2.8-utf8proc
Sebastien Helleu 2010-02-16 16:57:22 +01:00
parent 341551f2f2
commit ce1b23311c
7 changed files with 153 additions and 5 deletions

View File

@ -1,7 +1,7 @@
WeeChat ChangeLog
=================
FlashCode <flashcode@flashtux.org>
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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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