summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2023-11-17 08:51:53 +0100
committerGravatar default2023-11-17 08:51:53 +0100
commit80c5bac826c480fdabfaeae991a9a7fb298fb865 (patch)
tree3dcc249478b68120aeb9cbdd0d9ba442463314dd
parentBackport from xs. (diff)
downloadsnac2-80c5bac826c480fdabfaeae991a9a7fb298fb865.tar.gz
snac2-80c5bac826c480fdabfaeae991a9a7fb298fb865.tar.xz
snac2-80c5bac826c480fdabfaeae991a9a7fb298fb865.zip
Backport from xs.
-rw-r--r--data.c1
-rw-r--r--mastoapi.c1
-rw-r--r--snac.c1
-rw-r--r--xs.h73
-rw-r--r--xs_hex.h85
-rw-r--r--xs_version.h2
6 files changed, 89 insertions, 74 deletions
diff --git a/data.c b/data.c
index d272617..a37fbdb 100644
--- a/data.c
+++ b/data.c
@@ -2,6 +2,7 @@
2/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */ 2/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
3 3
4#include "xs.h" 4#include "xs.h"
5#include "xs_hex.h"
5#include "xs_io.h" 6#include "xs_io.h"
6#include "xs_json.h" 7#include "xs_json.h"
7#include "xs_openssl.h" 8#include "xs_openssl.h"
diff --git a/mastoapi.c b/mastoapi.c
index 0c94dd9..9b0f32b 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -4,6 +4,7 @@
4#ifndef NO_MASTODON_API 4#ifndef NO_MASTODON_API
5 5
6#include "xs.h" 6#include "xs.h"
7#include "xs_hex.h"
7#include "xs_openssl.h" 8#include "xs_openssl.h"
8#include "xs_json.h" 9#include "xs_json.h"
9#include "xs_io.h" 10#include "xs_io.h"
diff --git a/snac.c b/snac.c
index ac371c5..6394608 100644
--- a/snac.c
+++ b/snac.c
@@ -4,6 +4,7 @@
4#define XS_IMPLEMENTATION 4#define XS_IMPLEMENTATION
5 5
6#include "xs.h" 6#include "xs.h"
7#include "xs_hex.h"
7#include "xs_io.h" 8#include "xs_io.h"
8#include "xs_unicode.h" 9#include "xs_unicode.h"
9#include "xs_json.h" 10#include "xs_json.h"
diff --git a/xs.h b/xs.h
index c0857bc..63715ac 100644
--- a/xs.h
+++ b/xs.h
@@ -119,10 +119,6 @@ void xs_data_get(void *data, const xs_data *value);
119 119
120void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); 120void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size);
121 121
122xs_str *xs_hex_enc(const xs_val *data, int size);
123xs_val *xs_hex_dec(const xs_str *hex, int *size);
124int xs_is_hex(const char *str);
125
126unsigned int xs_hash_func(const char *data, int size); 122unsigned int xs_hash_func(const char *data, int size);
127 123
128#ifdef XS_ASSERT 124#ifdef XS_ASSERT
@@ -1178,75 +1174,6 @@ void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size
1178} 1174}
1179 1175
1180 1176
1181/** hex **/
1182
1183static char xs_hex_digits[] = "0123456789abcdef";
1184
1185xs_str *xs_hex_enc(const xs_val *data, int size)
1186/* returns an hexdump of data */
1187{
1188 xs_str *s;
1189 char *p;
1190 int n;
1191
1192 p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
1193
1194 for (n = 0; n < size; n++) {
1195 *p++ = xs_hex_digits[*data >> 4 & 0xf];
1196 *p++ = xs_hex_digits[*data & 0xf];
1197 data++;
1198 }
1199
1200 *p = '\0';
1201
1202 return s;
1203}
1204
1205
1206xs_val *xs_hex_dec(const xs_str *hex, int *size)
1207/* decodes an hexdump into data */
1208{
1209 int sz = strlen(hex);
1210 xs_val *s = NULL;
1211 char *p;
1212 int n;
1213
1214 if (sz % 2)
1215 return NULL;
1216
1217 p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
1218
1219 for (n = 0; n < sz; n += 2) {
1220 int i;
1221 if (sscanf(&hex[n], "%02x", &i) == 0) {
1222 /* decoding error */
1223 return xs_free(s);
1224 }
1225 else
1226 *p = i;
1227
1228 p++;
1229 }
1230
1231 *p = '\0';
1232 *size = sz / 2;
1233
1234 return s;
1235}
1236
1237
1238int xs_is_hex(const char *str)
1239/* returns 1 if str is an hex string */
1240{
1241 while (*str) {
1242 if (strchr("0123456789abcdefABCDEF", *str++) == NULL)
1243 return 0;
1244 }
1245
1246 return 1;
1247}
1248
1249
1250unsigned int xs_hash_func(const char *data, int size) 1177unsigned int xs_hash_func(const char *data, int size)
1251/* a general purpose hashing function */ 1178/* a general purpose hashing function */
1252{ 1179{
diff --git a/xs_hex.h b/xs_hex.h
new file mode 100644
index 0000000..2d87a65
--- /dev/null
+++ b/xs_hex.h
@@ -0,0 +1,85 @@
1/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
2
3#ifndef _XS_HEX_H
4
5#define _XS_HEX_H
6
7xs_str *xs_hex_enc(const xs_val *data, int size);
8xs_val *xs_hex_dec(const xs_str *hex, int *size);
9int xs_is_hex(const char *str);
10
11#ifdef XS_IMPLEMENTATION
12
13/** hex **/
14
15static char rev_hex_digits[] = "fedcba9876543210FEDCBA";
16
17xs_str *xs_hex_enc(const xs_val *data, int size)
18/* returns an hexdump of data */
19{
20 xs_str *s;
21 char *p;
22 int n;
23
24 p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
25
26 for (n = 0; n < size; n++) {
27 *p++ = rev_hex_digits[0xf - (*data >> 4 & 0xf)];
28 *p++ = rev_hex_digits[0xf - (*data & 0xf)];
29 data++;
30 }
31
32 *p = '\0';
33
34 return s;
35}
36
37
38xs_val *xs_hex_dec(const xs_str *hex, int *size)
39/* decodes an hexdump into data */
40{
41 int sz = strlen(hex);
42 xs_val *s = NULL;
43 char *p;
44 int n;
45
46 if (sz % 2)
47 return NULL;
48
49 p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
50
51 for (n = 0; n < sz; n += 2) {
52 char *d1 = strchr(rev_hex_digits, *hex++);
53 char *d2 = strchr(rev_hex_digits, *hex++);
54
55 if (!d1 || !d2) {
56 /* decoding error */
57 return xs_free(s);
58 }
59
60 *p++ = (0xf - ((d1 - rev_hex_digits) & 0xf)) << 4 |
61 (0xf - ((d2 - rev_hex_digits) & 0xf));
62 }
63
64 *p = '\0';
65 *size = sz / 2;
66
67 return s;
68}
69
70
71int xs_is_hex(const char *str)
72/* returns 1 if str is an hex string */
73{
74 while (*str) {
75 if (strchr(rev_hex_digits, *str++) == NULL)
76 return 0;
77 }
78
79 return 1;
80}
81
82
83#endif /* XS_IMPLEMENTATION */
84
85#endif /* _XS_HEX_H */
diff --git a/xs_version.h b/xs_version.h
index 42dc7d2..e5a5e49 100644
--- a/xs_version.h
+++ b/xs_version.h
@@ -1 +1 @@
/* 0932615dfe85e5d8544c4b2052eb66f3a430eb8c */ /* 416f5ffa99ecd4a3ec25d273b986d3d99dc92d22 */