mirror of
https://github.com/weechat/weechat.git
synced 2026-07-03 16:23:14 +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 */
|
||||
|
||||
Reference in New Issue
Block a user