Backport from xs.

This commit is contained in:
default 2023-05-17 09:42:31 +02:00
parent 81cec4ab27
commit 7f1b87a6b6
3 changed files with 72 additions and 71 deletions

72
xs.h
View File

@ -118,6 +118,10 @@ void xs_data_get(const xs_data *value, void *data);
void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size);
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);
#ifdef XS_ASSERT
#include <assert.h>
@ -1053,7 +1057,7 @@ const char *xs_number_str(const xs_number *v)
}
/* raw data blocks */
/** raw data blocks **/
xs_data *xs_data_new(const void *data, int size)
/* returns a new raw data value */
@ -1107,6 +1111,72 @@ void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size
}
/** hex **/
xs_str *xs_hex_enc(const xs_val *data, int size)
/* returns an hexdump of data */
{
xs_str *s;
char *p;
int n;
p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
for (n = 0; n < size; n++) {
snprintf(p, 3, "%02x", (unsigned char)data[n]);
p += 2;
}
*p = '\0';
return s;
}
xs_val *xs_hex_dec(const xs_str *hex, int *size)
/* decodes an hexdump into data */
{
int sz = strlen(hex);
xs_val *s = NULL;
char *p;
int n;
if (sz % 2)
return NULL;
p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
for (n = 0; n < sz; n += 2) {
int i;
if (sscanf(&hex[n], "%02x", &i) == 0) {
/* decoding error */
return xs_free(s);
}
else
*p = i;
p++;
}
*p = '\0';
*size = sz / 2;
return s;
}
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;
}
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_H */

View File

@ -4,9 +4,6 @@
#define _XS_ENCDEC_H
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_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);
@ -14,72 +11,6 @@
#ifdef XS_IMPLEMENTATION
/** hex **/
xs_str *xs_hex_enc(const xs_val *data, int size)
/* returns an hexdump of data */
{
xs_str *s;
char *p;
int n;
p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
for (n = 0; n < size; n++) {
snprintf(p, 3, "%02x", (unsigned char)data[n]);
p += 2;
}
*p = '\0';
return s;
}
xs_val *xs_hex_dec(const xs_str *hex, int *size)
/* decodes an hexdump into data */
{
int sz = strlen(hex);
xs_val *s = NULL;
char *p;
int n;
if (sz % 2)
return NULL;
p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
for (n = 0; n < sz; n += 2) {
int i;
if (sscanf(&hex[n], "%02x", &i) == 0) {
/* decoding error */
return xs_free(s);
}
else
*p = i;
p++;
}
*p = '\0';
*size = sz / 2;
return s;
}
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;
}
/** base64 */
static char *xs_b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

View File

@ -1 +1 @@
/* 494e346f92431041350f72431417eee03a23eafd */
/* e0835629880a2846ad69c02a63a9209d5dd34945 */