commit 80c5bac
default
·
2023-11-17 07:51:53 +0000 UTC
parent acf3cdc
Backport from xs.
6 files changed,
+89,
-74
M
data.c
+1,
-0
1@@ -2,6 +2,7 @@
2 /* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
3
4 #include "xs.h"
5+#include "xs_hex.h"
6 #include "xs_io.h"
7 #include "xs_json.h"
8 #include "xs_openssl.h"
+1,
-0
1@@ -4,6 +4,7 @@
2 #ifndef NO_MASTODON_API
3
4 #include "xs.h"
5+#include "xs_hex.h"
6 #include "xs_openssl.h"
7 #include "xs_json.h"
8 #include "xs_io.h"
M
snac.c
+1,
-0
1@@ -4,6 +4,7 @@
2 #define XS_IMPLEMENTATION
3
4 #include "xs.h"
5+#include "xs_hex.h"
6 #include "xs_io.h"
7 #include "xs_unicode.h"
8 #include "xs_json.h"
M
xs.h
+0,
-73
1@@ -119,10 +119,6 @@ void xs_data_get(void *data, const xs_data *value);
2
3 void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size);
4
5-xs_str *xs_hex_enc(const xs_val *data, int size);
6-xs_val *xs_hex_dec(const xs_str *hex, int *size);
7-int xs_is_hex(const char *str);
8-
9 unsigned int xs_hash_func(const char *data, int size);
10
11 #ifdef XS_ASSERT
12@@ -1178,75 +1174,6 @@ void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size
13 }
14
15
16-/** hex **/
17-
18-static char xs_hex_digits[] = "0123456789abcdef";
19-
20-xs_str *xs_hex_enc(const xs_val *data, int size)
21-/* returns an hexdump of data */
22-{
23- xs_str *s;
24- char *p;
25- int n;
26-
27- p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
28-
29- for (n = 0; n < size; n++) {
30- *p++ = xs_hex_digits[*data >> 4 & 0xf];
31- *p++ = xs_hex_digits[*data & 0xf];
32- data++;
33- }
34-
35- *p = '\0';
36-
37- return s;
38-}
39-
40-
41-xs_val *xs_hex_dec(const xs_str *hex, int *size)
42-/* decodes an hexdump into data */
43-{
44- int sz = strlen(hex);
45- xs_val *s = NULL;
46- char *p;
47- int n;
48-
49- if (sz % 2)
50- return NULL;
51-
52- p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
53-
54- for (n = 0; n < sz; n += 2) {
55- int i;
56- if (sscanf(&hex[n], "%02x", &i) == 0) {
57- /* decoding error */
58- return xs_free(s);
59- }
60- else
61- *p = i;
62-
63- p++;
64- }
65-
66- *p = '\0';
67- *size = sz / 2;
68-
69- return s;
70-}
71-
72-
73-int xs_is_hex(const char *str)
74-/* returns 1 if str is an hex string */
75-{
76- while (*str) {
77- if (strchr("0123456789abcdefABCDEF", *str++) == NULL)
78- return 0;
79- }
80-
81- return 1;
82-}
83-
84-
85 unsigned int xs_hash_func(const char *data, int size)
86 /* a general purpose hashing function */
87 {
A
xs_hex.h
+85,
-0
1@@ -0,0 +1,85 @@
2+/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
3+
4+#ifndef _XS_HEX_H
5+
6+#define _XS_HEX_H
7+
8+xs_str *xs_hex_enc(const xs_val *data, int size);
9+xs_val *xs_hex_dec(const xs_str *hex, int *size);
10+int xs_is_hex(const char *str);
11+
12+#ifdef XS_IMPLEMENTATION
13+
14+/** hex **/
15+
16+static char rev_hex_digits[] = "fedcba9876543210FEDCBA";
17+
18+xs_str *xs_hex_enc(const xs_val *data, int size)
19+/* returns an hexdump of data */
20+{
21+ xs_str *s;
22+ char *p;
23+ int n;
24+
25+ p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
26+
27+ for (n = 0; n < size; n++) {
28+ *p++ = rev_hex_digits[0xf - (*data >> 4 & 0xf)];
29+ *p++ = rev_hex_digits[0xf - (*data & 0xf)];
30+ data++;
31+ }
32+
33+ *p = '\0';
34+
35+ return s;
36+}
37+
38+
39+xs_val *xs_hex_dec(const xs_str *hex, int *size)
40+/* decodes an hexdump into data */
41+{
42+ int sz = strlen(hex);
43+ xs_val *s = NULL;
44+ char *p;
45+ int n;
46+
47+ if (sz % 2)
48+ return NULL;
49+
50+ p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
51+
52+ for (n = 0; n < sz; n += 2) {
53+ char *d1 = strchr(rev_hex_digits, *hex++);
54+ char *d2 = strchr(rev_hex_digits, *hex++);
55+
56+ if (!d1 || !d2) {
57+ /* decoding error */
58+ return xs_free(s);
59+ }
60+
61+ *p++ = (0xf - ((d1 - rev_hex_digits) & 0xf)) << 4 |
62+ (0xf - ((d2 - rev_hex_digits) & 0xf));
63+ }
64+
65+ *p = '\0';
66+ *size = sz / 2;
67+
68+ return s;
69+}
70+
71+
72+int xs_is_hex(const char *str)
73+/* returns 1 if str is an hex string */
74+{
75+ while (*str) {
76+ if (strchr(rev_hex_digits, *str++) == NULL)
77+ return 0;
78+ }
79+
80+ return 1;
81+}
82+
83+
84+#endif /* XS_IMPLEMENTATION */
85+
86+#endif /* _XS_HEX_H */
+1,
-1
1@@ -1 +1 @@
2-/* 0932615dfe85e5d8544c4b2052eb66f3a430eb8c */
3+/* 416f5ffa99ecd4a3ec25d273b986d3d99dc92d22 */