summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xs.h72
-rw-r--r--xs_encdec.h69
-rw-r--r--xs_version.h2
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);
118 118
119void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size); 119void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size);
120 120
121xs_str *xs_hex_enc(const xs_val *data, int size);
122xs_val *xs_hex_dec(const xs_str *hex, int *size);
123int xs_is_hex(const char *str);
124
121 125
122#ifdef XS_ASSERT 126#ifdef XS_ASSERT
123#include <assert.h> 127#include <assert.h>
@@ -1053,7 +1057,7 @@ const char *xs_number_str(const xs_number *v)
1053} 1057}
1054 1058
1055 1059
1056/* raw data blocks */ 1060/** raw data blocks **/
1057 1061
1058xs_data *xs_data_new(const void *data, int size) 1062xs_data *xs_data_new(const void *data, int size)
1059/* returns a new raw data value */ 1063/* 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
1107} 1111}
1108 1112
1109 1113
1114/** hex **/
1115
1116xs_str *xs_hex_enc(const xs_val *data, int size)
1117/* returns an hexdump of data */
1118{
1119 xs_str *s;
1120 char *p;
1121 int n;
1122
1123 p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
1124
1125 for (n = 0; n < size; n++) {
1126 snprintf(p, 3, "%02x", (unsigned char)data[n]);
1127 p += 2;
1128 }
1129
1130 *p = '\0';
1131
1132 return s;
1133}
1134
1135
1136xs_val *xs_hex_dec(const xs_str *hex, int *size)
1137/* decodes an hexdump into data */
1138{
1139 int sz = strlen(hex);
1140 xs_val *s = NULL;
1141 char *p;
1142 int n;
1143
1144 if (sz % 2)
1145 return NULL;
1146
1147 p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
1148
1149 for (n = 0; n < sz; n += 2) {
1150 int i;
1151 if (sscanf(&hex[n], "%02x", &i) == 0) {
1152 /* decoding error */
1153 return xs_free(s);
1154 }
1155 else
1156 *p = i;
1157
1158 p++;
1159 }
1160
1161 *p = '\0';
1162 *size = sz / 2;
1163
1164 return s;
1165}
1166
1167
1168int xs_is_hex(const char *str)
1169/* returns 1 if str is an hex string */
1170{
1171 while (*str) {
1172 if (strchr("0123456789abcdefABCDEF", *str++) == NULL)
1173 return 0;
1174 }
1175
1176 return 1;
1177}
1178
1179
1110#endif /* XS_IMPLEMENTATION */ 1180#endif /* XS_IMPLEMENTATION */
1111 1181
1112#endif /* _XS_H */ 1182#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 @@
4 4
5#define _XS_ENCDEC_H 5#define _XS_ENCDEC_H
6 6
7 xs_str *xs_hex_enc(const xs_val *data, int size);
8 xs_val *xs_hex_dec(const xs_str *hex, int *size);
9 int xs_is_hex(const char *str);
10 xs_str *xs_base64_enc(const xs_val *data, int sz); 7 xs_str *xs_base64_enc(const xs_val *data, int sz);
11 xs_val *xs_base64_dec(const xs_str *data, int *size); 8 xs_val *xs_base64_dec(const xs_str *data, int *size);
12 int xs_is_base64(const char *str); 9 int xs_is_base64(const char *str);
@@ -14,72 +11,6 @@
14 11
15#ifdef XS_IMPLEMENTATION 12#ifdef XS_IMPLEMENTATION
16 13
17/** hex **/
18
19xs_str *xs_hex_enc(const xs_val *data, int size)
20/* returns an hexdump of data */
21{
22 xs_str *s;
23 char *p;
24 int n;
25
26 p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
27
28 for (n = 0; n < size; n++) {
29 snprintf(p, 3, "%02x", (unsigned char)data[n]);
30 p += 2;
31 }
32
33 *p = '\0';
34
35 return s;
36}
37
38
39xs_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 int i;
54 if (sscanf(&hex[n], "%02x", &i) == 0) {
55 /* decoding error */
56 return xs_free(s);
57 }
58 else
59 *p = i;
60
61 p++;
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("0123456789abcdefABCDEF", *str++) == NULL)
76 return 0;
77 }
78
79 return 1;
80}
81
82
83/** base64 */ 14/** base64 */
84 15
85static char *xs_b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 16static 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 */