mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-12-26 01:03:37 +00:00
Backport from xs.
This commit is contained in:
parent
d562c3cfed
commit
28720d4a9d
2 changed files with 1 additions and 164 deletions
163
xs_encdec.h
163
xs_encdec.h
|
@ -7,10 +7,6 @@
|
|||
xs_str *xs_hex_enc(const xs_val *data, int size);
|
||||
xs_val *xs_hex_dec(const xs_str *hex, int *size);
|
||||
int xs_is_hex(const char *str);
|
||||
xs_str *xs_base32_enc(const xs_val *data, int sz);
|
||||
xs_str *xs_base32hex_enc(const xs_val *data, int sz);
|
||||
xs_val *xs_base32_dec(const xs_str *data, int *size);
|
||||
xs_val *xs_base32hex_dec(const xs_str *data, int *size);
|
||||
xs_str *xs_base64_enc(const xs_val *data, int sz);
|
||||
xs_val *xs_base64_dec(const xs_str *data, int *size);
|
||||
int xs_is_base64(const char *str);
|
||||
|
@ -84,165 +80,6 @@ int xs_is_hex(const char *str)
|
|||
}
|
||||
|
||||
|
||||
/** base32 */
|
||||
|
||||
static char *xs_b32_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"234567=";
|
||||
|
||||
static char *xs_b32hex_tbl = "0123456789"
|
||||
"ABCDEFGHIJKLMNOPQRSTUV=";
|
||||
|
||||
/*
|
||||
00000|00011|11111|12222|22223|33333|33444|44444
|
||||
*/
|
||||
|
||||
xs_str *xs_base32_enc_tbl(const xs_val *data, int sz, const char *b32_tbl)
|
||||
/* encodes data to base32 using a table */
|
||||
{
|
||||
xs_str *s = xs_str_new(NULL);
|
||||
unsigned char *p;
|
||||
int n;
|
||||
|
||||
p = (unsigned char *)data;
|
||||
|
||||
for (n = 0; n < sz; n += 5) {
|
||||
int l = sz - n;
|
||||
char enc[9] = "========";
|
||||
|
||||
enc[0] = b32_tbl[(p[n] >> 3) & 0x1f];
|
||||
|
||||
if (l > 1) {
|
||||
enc[1] = b32_tbl[(p[n] << 2 | p[n + 1] >> 6) & 0x1f];
|
||||
enc[2] = b32_tbl[(p[n + 1] >> 1) & 0x1f];
|
||||
|
||||
if (l > 2) {
|
||||
enc[3] = b32_tbl[(p[n + 1] << 4 | p[n + 2] >> 4) & 0x1f];
|
||||
|
||||
if (l > 3) {
|
||||
enc[4] = b32_tbl[(p[n + 2] << 1 | p[n + 3] >> 7) & 0x1f];
|
||||
enc[5] = b32_tbl[(p[n + 3] >> 2) & 0x1f];
|
||||
|
||||
if (l > 4) {
|
||||
enc[6] = b32_tbl[(p[n + 3] << 3 | p[n + 4] >> 5) & 0x1f];
|
||||
enc[7] = b32_tbl[(p[n + 4]) & 0x1f];
|
||||
}
|
||||
else
|
||||
enc[6] = b32_tbl[(p[n + 3] << 3) & 0x1f];
|
||||
}
|
||||
else
|
||||
enc[4] = b32_tbl[(p[n + 2] << 1) & 0x1f];
|
||||
}
|
||||
else
|
||||
enc[3] = b32_tbl[(p[n + 1] << 4) & 0x1f];
|
||||
}
|
||||
else
|
||||
enc[1] = b32_tbl[(p[n] << 2) & 0x1f];
|
||||
|
||||
s = xs_str_cat(s, enc);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
xs_str *xs_base32_enc(const xs_val *data, int sz)
|
||||
/* encodes data to base32 */
|
||||
{
|
||||
return xs_base32_enc_tbl(data, sz, xs_b32_tbl);
|
||||
}
|
||||
|
||||
|
||||
xs_str *xs_base32hex_enc(const xs_val *data, int sz)
|
||||
/* encodes data to base32 with HEX alphabet (RFC4648) */
|
||||
{
|
||||
return xs_base32_enc_tbl(data, sz, xs_b32hex_tbl);
|
||||
}
|
||||
|
||||
|
||||
xs_val *xs_base32_dec_tbl(const xs_str *data, int *size, const char *b32_tbl)
|
||||
/* decodes data from base32 using a table */
|
||||
{
|
||||
xs_val *s = NULL;
|
||||
int sz = 0;
|
||||
char *p;
|
||||
|
||||
p = (char *)data;
|
||||
|
||||
/* size of data must be a multiple of 8 */
|
||||
if (strlen(p) % 8)
|
||||
return NULL;
|
||||
|
||||
for (p = (char *)data; *p; p += 8) {
|
||||
int cs[8];
|
||||
int n;
|
||||
unsigned char tmp[5];
|
||||
|
||||
for (n = 0; n < 8; n++) {
|
||||
char *ss = strchr(b32_tbl, p[n]);
|
||||
|
||||
if (ss == NULL) {
|
||||
/* not a base32 char */
|
||||
return xs_free(s);
|
||||
}
|
||||
|
||||
cs[n] = ss - b32_tbl;
|
||||
}
|
||||
|
||||
n = 0;
|
||||
|
||||
/* #0 byte */
|
||||
tmp[n++] = cs[0] << 3 | cs[1] >> 2;
|
||||
|
||||
if (cs[2] != 32) {
|
||||
/* #1 byte */
|
||||
tmp[n++] = (cs[1] & 0x3) << 6 | cs[2] << 1 | (cs[3] & 0x10) >> 4;
|
||||
|
||||
if (cs[4] != 32) {
|
||||
/* #2 byte */
|
||||
tmp[n++] = (cs[3] & 0xf) << 4 | cs[4] >> 1;
|
||||
|
||||
if (cs[5] != 32) {
|
||||
/* #3 byte */
|
||||
tmp[n++] = (cs[4] & 0x1) << 7 | cs[5] << 2 | cs[6] >> 3;
|
||||
|
||||
if (cs[7] != 32) {
|
||||
/* #4 byte */
|
||||
tmp[n++] = (cs[6] & 0x7) << 5 | cs[7];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* must be done manually because data can be pure binary */
|
||||
s = xs_realloc(s, _xs_blk_size(sz + n));
|
||||
memcpy(s + sz, tmp, n);
|
||||
sz += n;
|
||||
}
|
||||
|
||||
/* asciiz it to use it as a string */
|
||||
s = xs_realloc(s, _xs_blk_size(sz + 1));
|
||||
s[sz] = '\0';
|
||||
|
||||
*size = sz;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
xs_val *xs_base32_dec(const xs_str *data, int *size)
|
||||
/* decodes data from base32 */
|
||||
{
|
||||
return xs_base32_dec_tbl(data, size, xs_b32_tbl);
|
||||
}
|
||||
|
||||
|
||||
xs_val *xs_base32hex_dec(const xs_str *data, int *size)
|
||||
/* decodes data from base32 with HEX alphabet (RFC4648) */
|
||||
{
|
||||
return xs_base32_dec_tbl(data, size, xs_b32hex_tbl);
|
||||
}
|
||||
|
||||
|
||||
/** base64 */
|
||||
|
||||
static char *xs_b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
|
|
@ -1 +1 @@
|
|||
/* d8ec27efc55ba67403e88bfbe7d2ce9905364d6d */
|
||||
/* 01bea7d4a0e631c0406b358dda9cc9409362c003 */
|
||||
|
|
Loading…
Reference in a new issue