From 7f1b87a6b69055e5f0a3dcc890f9fecd45466a90 Mon Sep 17 00:00:00 2001 From: default Date: Wed, 17 May 2023 09:42:31 +0200 Subject: [PATCH] Backport from xs. --- xs.h | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++- xs_encdec.h | 69 ------------------------------------------------- xs_version.h | 2 +- 3 files changed, 72 insertions(+), 71 deletions(-) diff --git a/xs.h b/xs.h index c4c961b..c9825b9 100644 --- a/xs.h +++ b/xs.h @@ -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 @@ -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 */ diff --git a/xs_encdec.h b/xs_encdec.h index f4ffe22..14cb36e 100644 --- a/xs_encdec.h +++ b/xs_encdec.h @@ -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" diff --git a/xs_version.h b/xs_version.h index fce4f34..e3b333f 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* 494e346f92431041350f72431417eee03a23eafd */ +/* e0835629880a2846ad69c02a63a9209d5dd34945 */