commit 28720d4
default
·
2023-05-09 12:23:30 +0000 UTC
parent d562c3c
Backport from xs.
2 files changed,
+1,
-164
+0,
-163
1@@ -7,10 +7,6 @@
2 xs_str *xs_hex_enc(const xs_val *data, int size);
3 xs_val *xs_hex_dec(const xs_str *hex, int *size);
4 int xs_is_hex(const char *str);
5- xs_str *xs_base32_enc(const xs_val *data, int sz);
6- xs_str *xs_base32hex_enc(const xs_val *data, int sz);
7- xs_val *xs_base32_dec(const xs_str *data, int *size);
8- xs_val *xs_base32hex_dec(const xs_str *data, int *size);
9 xs_str *xs_base64_enc(const xs_val *data, int sz);
10 xs_val *xs_base64_dec(const xs_str *data, int *size);
11 int xs_is_base64(const char *str);
12@@ -84,165 +80,6 @@ int xs_is_hex(const char *str)
13 }
14
15
16-/** base32 */
17-
18-static char *xs_b32_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
19- "234567=";
20-
21-static char *xs_b32hex_tbl = "0123456789"
22- "ABCDEFGHIJKLMNOPQRSTUV=";
23-
24-/*
25- 00000|00011|11111|12222|22223|33333|33444|44444
26-*/
27-
28-xs_str *xs_base32_enc_tbl(const xs_val *data, int sz, const char *b32_tbl)
29-/* encodes data to base32 using a table */
30-{
31- xs_str *s = xs_str_new(NULL);
32- unsigned char *p;
33- int n;
34-
35- p = (unsigned char *)data;
36-
37- for (n = 0; n < sz; n += 5) {
38- int l = sz - n;
39- char enc[9] = "========";
40-
41- enc[0] = b32_tbl[(p[n] >> 3) & 0x1f];
42-
43- if (l > 1) {
44- enc[1] = b32_tbl[(p[n] << 2 | p[n + 1] >> 6) & 0x1f];
45- enc[2] = b32_tbl[(p[n + 1] >> 1) & 0x1f];
46-
47- if (l > 2) {
48- enc[3] = b32_tbl[(p[n + 1] << 4 | p[n + 2] >> 4) & 0x1f];
49-
50- if (l > 3) {
51- enc[4] = b32_tbl[(p[n + 2] << 1 | p[n + 3] >> 7) & 0x1f];
52- enc[5] = b32_tbl[(p[n + 3] >> 2) & 0x1f];
53-
54- if (l > 4) {
55- enc[6] = b32_tbl[(p[n + 3] << 3 | p[n + 4] >> 5) & 0x1f];
56- enc[7] = b32_tbl[(p[n + 4]) & 0x1f];
57- }
58- else
59- enc[6] = b32_tbl[(p[n + 3] << 3) & 0x1f];
60- }
61- else
62- enc[4] = b32_tbl[(p[n + 2] << 1) & 0x1f];
63- }
64- else
65- enc[3] = b32_tbl[(p[n + 1] << 4) & 0x1f];
66- }
67- else
68- enc[1] = b32_tbl[(p[n] << 2) & 0x1f];
69-
70- s = xs_str_cat(s, enc);
71- }
72-
73- return s;
74-}
75-
76-
77-xs_str *xs_base32_enc(const xs_val *data, int sz)
78-/* encodes data to base32 */
79-{
80- return xs_base32_enc_tbl(data, sz, xs_b32_tbl);
81-}
82-
83-
84-xs_str *xs_base32hex_enc(const xs_val *data, int sz)
85-/* encodes data to base32 with HEX alphabet (RFC4648) */
86-{
87- return xs_base32_enc_tbl(data, sz, xs_b32hex_tbl);
88-}
89-
90-
91-xs_val *xs_base32_dec_tbl(const xs_str *data, int *size, const char *b32_tbl)
92-/* decodes data from base32 using a table */
93-{
94- xs_val *s = NULL;
95- int sz = 0;
96- char *p;
97-
98- p = (char *)data;
99-
100- /* size of data must be a multiple of 8 */
101- if (strlen(p) % 8)
102- return NULL;
103-
104- for (p = (char *)data; *p; p += 8) {
105- int cs[8];
106- int n;
107- unsigned char tmp[5];
108-
109- for (n = 0; n < 8; n++) {
110- char *ss = strchr(b32_tbl, p[n]);
111-
112- if (ss == NULL) {
113- /* not a base32 char */
114- return xs_free(s);
115- }
116-
117- cs[n] = ss - b32_tbl;
118- }
119-
120- n = 0;
121-
122- /* #0 byte */
123- tmp[n++] = cs[0] << 3 | cs[1] >> 2;
124-
125- if (cs[2] != 32) {
126- /* #1 byte */
127- tmp[n++] = (cs[1] & 0x3) << 6 | cs[2] << 1 | (cs[3] & 0x10) >> 4;
128-
129- if (cs[4] != 32) {
130- /* #2 byte */
131- tmp[n++] = (cs[3] & 0xf) << 4 | cs[4] >> 1;
132-
133- if (cs[5] != 32) {
134- /* #3 byte */
135- tmp[n++] = (cs[4] & 0x1) << 7 | cs[5] << 2 | cs[6] >> 3;
136-
137- if (cs[7] != 32) {
138- /* #4 byte */
139- tmp[n++] = (cs[6] & 0x7) << 5 | cs[7];
140- }
141- }
142- }
143- }
144-
145- /* must be done manually because data can be pure binary */
146- s = xs_realloc(s, _xs_blk_size(sz + n));
147- memcpy(s + sz, tmp, n);
148- sz += n;
149- }
150-
151- /* asciiz it to use it as a string */
152- s = xs_realloc(s, _xs_blk_size(sz + 1));
153- s[sz] = '\0';
154-
155- *size = sz;
156-
157- return s;
158-}
159-
160-
161-xs_val *xs_base32_dec(const xs_str *data, int *size)
162-/* decodes data from base32 */
163-{
164- return xs_base32_dec_tbl(data, size, xs_b32_tbl);
165-}
166-
167-
168-xs_val *xs_base32hex_dec(const xs_str *data, int *size)
169-/* decodes data from base32 with HEX alphabet (RFC4648) */
170-{
171- return xs_base32_dec_tbl(data, size, xs_b32hex_tbl);
172-}
173-
174-
175 /** base64 */
176
177 static char *xs_b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+1,
-1
1@@ -1 +1 @@
2-/* d8ec27efc55ba67403e88bfbe7d2ce9905364d6d */
3+/* 01bea7d4a0e631c0406b358dda9cc9409362c003 */