mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-22 13:25:04 +00:00
Backport from xs.
This commit is contained in:
parent
a52840414d
commit
bb9992a1a9
1 changed files with 24 additions and 14 deletions
38
xs_encdec.h
38
xs_encdec.h
|
@ -35,10 +35,13 @@ d_char *xs_hex_dec(const char *hex)
|
||||||
/* decodes an hexdump into data */
|
/* decodes an hexdump into data */
|
||||||
{
|
{
|
||||||
int sz = strlen(hex);
|
int sz = strlen(hex);
|
||||||
d_char *s;
|
d_char *s = NULL;
|
||||||
char *p;
|
char *p;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
|
if (sz % 2)
|
||||||
|
return s;
|
||||||
|
|
||||||
p = s = calloc(sz / 2, 1);
|
p = s = calloc(sz / 2, 1);
|
||||||
|
|
||||||
for (n = 0; n < sz; n += 2) {
|
for (n = 0; n < sz; n += 2) {
|
||||||
|
@ -63,34 +66,37 @@ d_char *xs_base64_enc(const char *data, int sz)
|
||||||
{
|
{
|
||||||
d_char *s;
|
d_char *s;
|
||||||
unsigned char *p;
|
unsigned char *p;
|
||||||
int n;
|
char *i;
|
||||||
|
int bsz, n;
|
||||||
static char *b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
static char *b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
"abcdefghijklmnopqrstuvwxyz"
|
"abcdefghijklmnopqrstuvwxyz"
|
||||||
"0123456789+/";
|
"0123456789+/";
|
||||||
|
|
||||||
s = xs_str_new(NULL);
|
bsz = ((sz + 3 - 1) / 3) * 4;
|
||||||
|
i = s = calloc(bsz + 1, 1);
|
||||||
p = (unsigned char *)data;
|
p = (unsigned char *)data;
|
||||||
|
|
||||||
for (n = 0; n < sz; n += 3) {
|
for (n = 0; n < sz; n += 3) {
|
||||||
int l = sz - n;
|
int l = sz - n;
|
||||||
|
|
||||||
if (l == 1) {
|
if (l == 1) {
|
||||||
s = xs_append_m(s, &b64_tbl[(p[n] >> 2) & 0x3f], 1);
|
*i++ = b64_tbl[(p[n] >> 2) & 0x3f];
|
||||||
s = xs_append_m(s, &b64_tbl[(p[n] << 4) & 0x3f], 1);
|
*i++ = b64_tbl[(p[n] << 4) & 0x3f];
|
||||||
s = xs_append_m(s, "==", 2);
|
*i++ = '=';
|
||||||
|
*i++ = '=';
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if (l == 2) {
|
if (l == 2) {
|
||||||
s = xs_append_m(s, &b64_tbl[(p[n] >> 2) & 0x3f], 1);
|
*i++ = b64_tbl[(p[n] >> 2) & 0x3f];
|
||||||
s = xs_append_m(s, &b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f], 1);
|
*i++ = b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f];
|
||||||
s = xs_append_m(s, &b64_tbl[(p[n + 1] << 2) & 0x3f], 1);
|
*i++ = b64_tbl[(p[n + 1] << 2) & 0x3f];
|
||||||
s = xs_append_m(s, "=", 1);
|
*i++ = '=';
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
s = xs_append_m(s, &b64_tbl[(p[n] >> 2) & 0x3f], 1);
|
*i++ = b64_tbl[(p[n] >> 2) & 0x3f];
|
||||||
s = xs_append_m(s, &b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f], 1);
|
*i++ = b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f];
|
||||||
s = xs_append_m(s, &b64_tbl[(p[n + 1] << 2 | p[n + 2] >> 6) & 0x3f], 1);
|
*i++ = b64_tbl[(p[n + 1] << 2 | p[n + 2] >> 6) & 0x3f];
|
||||||
s = xs_append_m(s, &b64_tbl[(p[n + 2]) & 0x3f], 1);
|
*i++ = b64_tbl[(p[n + 2]) & 0x3f];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,6 +116,10 @@ d_char *xs_base64_dec(const char *data, int *size)
|
||||||
|
|
||||||
p = (char *)data;
|
p = (char *)data;
|
||||||
|
|
||||||
|
/* size of data must be a multiple of 4 */
|
||||||
|
if (strlen(p) % 4)
|
||||||
|
return s;
|
||||||
|
|
||||||
for (p = (char *)data; *p; p += 4) {
|
for (p = (char *)data; *p; p += 4) {
|
||||||
int cs[4];
|
int cs[4];
|
||||||
int n;
|
int n;
|
||||||
|
|
Loading…
Reference in a new issue