mirror of
https://github.com/weechat/weechat.git
synced 2026-07-01 07:16:37 +02:00
Add function "string_decode_base64" in plugin API
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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) \
|
||||
|
||||
Reference in New Issue
Block a user