commit 80c5bac826c480fdabfaeae991a9a7fb298fb865
parent acf3cdcf80da7c3443e202a02d4b626c13e9e8dd
Author: default <nobody@localhost>
Date: Fri, 17 Nov 2023 08:51:53 +0100
Backport from xs.
Diffstat:
M | data.c | | | 1 | + |
M | mastoapi.c | | | 1 | + |
M | snac.c | | | 1 | + |
M | xs.h | | | 73 | ------------------------------------------------------------------------- |
A | xs_hex.h | | | 85 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | xs_version.h | | | 2 | +- |
6 files changed, 89 insertions(+), 74 deletions(-)
diff --git a/data.c b/data.c
@@ -2,6 +2,7 @@
/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
#include "xs.h"
+#include "xs_hex.h"
#include "xs_io.h"
#include "xs_json.h"
#include "xs_openssl.h"
diff --git a/mastoapi.c b/mastoapi.c
@@ -4,6 +4,7 @@
#ifndef NO_MASTODON_API
#include "xs.h"
+#include "xs_hex.h"
#include "xs_openssl.h"
#include "xs_json.h"
#include "xs_io.h"
diff --git a/snac.c b/snac.c
@@ -4,6 +4,7 @@
#define XS_IMPLEMENTATION
#include "xs.h"
+#include "xs_hex.h"
#include "xs_io.h"
#include "xs_unicode.h"
#include "xs_json.h"
diff --git a/xs.h b/xs.h
@@ -119,10 +119,6 @@ void xs_data_get(void *data, const xs_data *value);
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);
-
unsigned int xs_hash_func(const char *data, int size);
#ifdef XS_ASSERT
@@ -1178,75 +1174,6 @@ void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size
}
-/** hex **/
-
-static char xs_hex_digits[] = "0123456789abcdef";
-
-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++) {
- *p++ = xs_hex_digits[*data >> 4 & 0xf];
- *p++ = xs_hex_digits[*data & 0xf];
- data++;
- }
-
- *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;
-}
-
-
unsigned int xs_hash_func(const char *data, int size)
/* a general purpose hashing function */
{
diff --git a/xs_hex.h b/xs_hex.h
@@ -0,0 +1,85 @@
+/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
+
+#ifndef _XS_HEX_H
+
+#define _XS_HEX_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);
+
+#ifdef XS_IMPLEMENTATION
+
+/** hex **/
+
+static char rev_hex_digits[] = "fedcba9876543210FEDCBA";
+
+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++) {
+ *p++ = rev_hex_digits[0xf - (*data >> 4 & 0xf)];
+ *p++ = rev_hex_digits[0xf - (*data & 0xf)];
+ data++;
+ }
+
+ *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) {
+ char *d1 = strchr(rev_hex_digits, *hex++);
+ char *d2 = strchr(rev_hex_digits, *hex++);
+
+ if (!d1 || !d2) {
+ /* decoding error */
+ return xs_free(s);
+ }
+
+ *p++ = (0xf - ((d1 - rev_hex_digits) & 0xf)) << 4 |
+ (0xf - ((d2 - rev_hex_digits) & 0xf));
+ }
+
+ *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(rev_hex_digits, *str++) == NULL)
+ return 0;
+ }
+
+ return 1;
+}
+
+
+#endif /* XS_IMPLEMENTATION */
+
+#endif /* _XS_HEX_H */
diff --git a/xs_version.h b/xs_version.h
@@ -1 +1 @@
-/* 0932615dfe85e5d8544c4b2052eb66f3a430eb8c */
+/* 416f5ffa99ecd4a3ec25d273b986d3d99dc92d22 */