snac2/xs_encdec.h

421 lines
9.4 KiB
C
Raw Normal View History

2023-01-17 08:50:16 +00:00
/* copyright (c) 2022 - 2023 grunfink / MIT license */
2022-09-19 18:41:11 +00:00
#ifndef _XS_ENCDEC_H
#define _XS_ENCDEC_H
2023-01-28 16:49:02 +00:00
xs_str *xs_hex_enc(const xs_val *data, int size);
xs_val *xs_hex_dec(const xs_str *hex, int *size);
2023-04-10 07:14:44 +00:00
int xs_is_hex(const char *str);
2023-05-02 04:41:45 +00:00
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);
2023-01-28 16:49:02 +00:00
xs_str *xs_base64_enc(const xs_val *data, int sz);
xs_val *xs_base64_dec(const xs_str *data, int *size);
2023-05-02 04:41:45 +00:00
int xs_is_base64(const char *str);
2023-01-28 16:49:02 +00:00
xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint);
2022-09-19 18:41:11 +00:00
#ifdef XS_IMPLEMENTATION
2023-05-02 04:41:45 +00:00
/** hex **/
2023-01-28 16:49:02 +00:00
xs_str *xs_hex_enc(const xs_val *data, int size)
2022-09-19 18:41:11 +00:00
/* returns an hexdump of data */
{
2023-01-28 16:49:02 +00:00
xs_str *s;
2022-09-19 18:41:11 +00:00
char *p;
int n;
2022-10-18 09:21:37 +00:00
p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
2022-09-19 18:41:11 +00:00
for (n = 0; n < size; n++) {
2023-04-19 20:53:07 +00:00
snprintf(p, 3, "%02x", (unsigned char)data[n]);
2022-09-19 18:41:11 +00:00
p += 2;
}
2022-10-18 09:21:37 +00:00
*p = '\0';
2022-09-19 18:41:11 +00:00
return s;
}
2023-01-28 16:49:02 +00:00
xs_val *xs_hex_dec(const xs_str *hex, int *size)
2022-09-19 18:41:11 +00:00
/* decodes an hexdump into data */
{
int sz = strlen(hex);
2023-01-28 16:49:02 +00:00
xs_val *s = NULL;
2022-09-19 18:41:11 +00:00
char *p;
int n;
2022-09-30 08:03:35 +00:00
if (sz % 2)
2022-10-18 09:21:37 +00:00
return NULL;
2022-09-30 08:03:35 +00:00
2022-10-18 09:21:37 +00:00
p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
2022-09-19 18:41:11 +00:00
for (n = 0; n < sz; n += 2) {
int i;
if (sscanf(&hex[n], "%02x", &i) == 0) {
/* decoding error */
2022-10-25 07:32:41 +00:00
return xs_free(s);
2022-09-19 18:41:11 +00:00
}
else
*p = i;
p++;
}
2022-10-18 09:21:37 +00:00
*p = '\0';
*size = sz / 2;
2022-09-19 18:41:11 +00:00
return s;
}
2023-04-10 07:14:44 +00:00
int xs_is_hex(const char *str)
/* returns 1 if str is an hex string */
{
while (*str) {
if (strchr("0123456789abcdefABCDEF", *str++) == NULL)
return 0;
}
return 1;
}
2023-05-02 04:41:45 +00:00
/** 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"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/=";
xs_str *xs_base64_enc_tbl(const xs_val *data, int sz, const char *b64_tbl)
/* encodes data to base64 using a table */
2022-09-19 18:41:11 +00:00
{
2023-01-28 16:49:02 +00:00
xs_str *s;
2022-09-19 18:41:11 +00:00
unsigned char *p;
2022-09-30 08:03:35 +00:00
char *i;
int bsz, n;
2022-09-19 18:41:11 +00:00
2022-09-30 08:03:35 +00:00
bsz = ((sz + 3 - 1) / 3) * 4;
2022-10-18 09:21:37 +00:00
i = s = xs_realloc(NULL, _xs_blk_size(bsz + 1));
2022-09-19 18:41:11 +00:00
p = (unsigned char *)data;
for (n = 0; n < sz; n += 3) {
int l = sz - n;
if (l == 1) {
2022-09-30 08:03:35 +00:00
*i++ = b64_tbl[(p[n] >> 2) & 0x3f];
*i++ = b64_tbl[(p[n] << 4) & 0x3f];
*i++ = '=';
*i++ = '=';
2022-09-19 18:41:11 +00:00
}
else
if (l == 2) {
2022-09-30 08:03:35 +00:00
*i++ = b64_tbl[(p[n] >> 2) & 0x3f];
*i++ = b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f];
*i++ = b64_tbl[(p[n + 1] << 2) & 0x3f];
*i++ = '=';
2022-09-19 18:41:11 +00:00
}
else {
2022-09-30 08:03:35 +00:00
*i++ = b64_tbl[(p[n] >> 2) & 0x3f];
*i++ = b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f];
*i++ = b64_tbl[(p[n + 1] << 2 | p[n + 2] >> 6) & 0x3f];
*i++ = b64_tbl[(p[n + 2]) & 0x3f];
2022-09-19 18:41:11 +00:00
}
}
2022-10-18 09:21:37 +00:00
*i = '\0';
2022-09-19 18:41:11 +00:00
return s;
}
2023-05-02 04:41:45 +00:00
xs_str *xs_base64_enc(const xs_val *data, int sz)
/* encodes data to base64 */
{
return xs_base64_enc_tbl(data, sz, xs_b64_tbl);
}
xs_val *xs_base64_dec_tbl(const xs_str *data, int *size, const char *b64_tbl)
/* decodes data from base64 using a table */
2022-09-19 18:41:11 +00:00
{
2023-01-28 16:49:02 +00:00
xs_val *s = NULL;
2022-09-19 18:41:11 +00:00
int sz = 0;
char *p;
p = (char *)data;
2022-09-30 08:03:35 +00:00
/* size of data must be a multiple of 4 */
if (strlen(p) % 4)
2022-10-18 09:21:37 +00:00
return NULL;
2022-09-30 08:03:35 +00:00
2022-09-19 18:41:11 +00:00
for (p = (char *)data; *p; p += 4) {
int cs[4];
int n;
unsigned char tmp[3];
for (n = 0; n < 4; n++) {
char *ss = strchr(b64_tbl, p[n]);
if (ss == NULL) {
/* not a base64 char */
2022-10-25 07:32:41 +00:00
return xs_free(s);
2022-09-19 18:41:11 +00:00
}
cs[n] = ss - b64_tbl;
}
n = 0;
/* first byte */
tmp[n++] = cs[0] << 2 | ((cs[1] >> 4) & 0x0f);
/* second byte */
if (cs[2] != 64)
tmp[n++] = cs[1] << 4 | ((cs[2] >> 2) & 0x3f);
/* third byte */
if (cs[3] != 64)
tmp[n++] = cs[2] << 6 | (cs[3] & 0x3f);
/* must be done manually because data can be pure binary */
2022-10-18 09:21:37 +00:00
s = xs_realloc(s, _xs_blk_size(sz + n));
2022-09-19 18:41:11 +00:00
memcpy(s + sz, tmp, n);
sz += n;
}
2022-10-18 09:21:37 +00:00
/* asciiz it to use it as a string */
s = xs_realloc(s, _xs_blk_size(sz + 1));
s[sz] = '\0';
2022-09-19 18:41:11 +00:00
*size = sz;
return s;
}
2023-05-02 04:41:45 +00:00
xs_val *xs_base64_dec(const xs_str *data, int *size)
/* decodes data from base64 */
{
return xs_base64_dec_tbl(data, size, xs_b64_tbl);
}
int xs_is_base64_tbl(const char *str, const char *b64_tbl)
/* returns 1 if str is a base64 string, with table */
{
while (*str) {
if (strchr(b64_tbl, *str++) == NULL)
return 0;
}
return 1;
}
int xs_is_base64(const char *str)
/* returns 1 if str is a base64 string */
{
return xs_is_base64_tbl(str, xs_b64_tbl);
}
/** utf-8 **/
2023-01-28 16:49:02 +00:00
xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint)
2022-09-19 18:41:11 +00:00
/* encodes an Unicode codepoint to utf8 */
{
unsigned char tmp[4];
int n = 0;
if (cpoint < 0x80)
tmp[n++] = cpoint & 0xff;
else
if (cpoint < 0x800) {
tmp[n++] = 0xc0 | (cpoint >> 6);
tmp[n++] = 0x80 | (cpoint & 0x3f);
}
else
if (cpoint < 0x10000) {
tmp[n++] = 0xe0 | (cpoint >> 12);
tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f);
tmp[n++] = 0x80 | (cpoint & 0x3f);
}
else
if (cpoint < 0x200000) {
tmp[n++] = 0xf0 | (cpoint >> 18);
tmp[n++] = 0x80 | ((cpoint >> 12) & 0x3f);
tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f);
tmp[n++] = 0x80 | (cpoint & 0x3f);
}
2022-09-19 18:41:11 +00:00
return xs_append_m(str, (char *)tmp, n);
}
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_ENCDEC_H */