commit 7f1b87a

default  ·  2023-05-17 07:42:31 +0000 UTC
parent 81cec4a
Backport from xs.
3 files changed,  +72, -71
M xs.h
M xs.h
+71, -1
 1@@ -118,6 +118,10 @@ void xs_data_get(const xs_data *value, void *data);
 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 
10 #ifdef XS_ASSERT
11 #include <assert.h>
12@@ -1053,7 +1057,7 @@ const char *xs_number_str(const xs_number *v)
13 }
14 
15 
16-/* raw data blocks */
17+/** raw data blocks **/
18 
19 xs_data *xs_data_new(const void *data, int size)
20 /* returns a new raw data value */
21@@ -1107,6 +1111,72 @@ void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size
22 }
23 
24 
25+/** hex **/
26+
27+xs_str *xs_hex_enc(const xs_val *data, int size)
28+/* returns an hexdump of data */
29+{
30+    xs_str *s;
31+    char *p;
32+    int n;
33+
34+    p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
35+
36+    for (n = 0; n < size; n++) {
37+        snprintf(p, 3, "%02x", (unsigned char)data[n]);
38+        p += 2;
39+    }
40+
41+    *p = '\0';
42+
43+    return s;
44+}
45+
46+
47+xs_val *xs_hex_dec(const xs_str *hex, int *size)
48+/* decodes an hexdump into data */
49+{
50+    int sz = strlen(hex);
51+    xs_val *s = NULL;
52+    char *p;
53+    int n;
54+
55+    if (sz % 2)
56+        return NULL;
57+
58+    p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
59+
60+    for (n = 0; n < sz; n += 2) {
61+        int i;
62+        if (sscanf(&hex[n], "%02x", &i) == 0) {
63+            /* decoding error */
64+            return xs_free(s);
65+        }
66+        else
67+            *p = i;
68+
69+        p++;
70+    }
71+
72+    *p = '\0';
73+    *size = sz / 2;
74+
75+    return s;
76+}
77+
78+
79+int xs_is_hex(const char *str)
80+/* returns 1 if str is an hex string */
81+{
82+    while (*str) {
83+        if (strchr("0123456789abcdefABCDEF", *str++) == NULL)
84+            return 0;
85+    }
86+
87+    return 1;
88+}
89+
90+
91 #endif /* XS_IMPLEMENTATION */
92 
93 #endif /* _XS_H */
+0, -69
 1@@ -4,9 +4,6 @@
 2 
 3 #define _XS_ENCDEC_H
 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  xs_str *xs_base64_enc(const xs_val *data, int sz);
 9  xs_val *xs_base64_dec(const xs_str *data, int *size);
10  int xs_is_base64(const char *str);
11@@ -14,72 +11,6 @@
12 
13 #ifdef XS_IMPLEMENTATION
14 
15-/** hex **/
16-
17-xs_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-        snprintf(p, 3, "%02x", (unsigned char)data[n]);
28-        p += 2;
29-    }
30-
31-    *p = '\0';
32-
33-    return s;
34-}
35-
36-
37-xs_val *xs_hex_dec(const xs_str *hex, int *size)
38-/* decodes an hexdump into data */
39-{
40-    int sz = strlen(hex);
41-    xs_val *s = NULL;
42-    char *p;
43-    int n;
44-
45-    if (sz % 2)
46-        return NULL;
47-
48-    p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
49-
50-    for (n = 0; n < sz; n += 2) {
51-        int i;
52-        if (sscanf(&hex[n], "%02x", &i) == 0) {
53-            /* decoding error */
54-            return xs_free(s);
55-        }
56-        else
57-            *p = i;
58-
59-        p++;
60-    }
61-
62-    *p = '\0';
63-    *size = sz / 2;
64-
65-    return s;
66-}
67-
68-
69-int xs_is_hex(const char *str)
70-/* returns 1 if str is an hex string */
71-{
72-    while (*str) {
73-        if (strchr("0123456789abcdefABCDEF", *str++) == NULL)
74-            return 0;
75-    }
76-
77-    return 1;
78-}
79-
80-
81 /** base64 */
82 
83 static char *xs_b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+1, -1
1@@ -1 +1 @@
2-/* 494e346f92431041350f72431417eee03a23eafd */
3+/* e0835629880a2846ad69c02a63a9209d5dd34945 */