xs_encdec.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  2. #ifndef _XS_ENCDEC_H
  3. #define _XS_ENCDEC_H
  4. xs_str *xs_hex_enc(const xs_val *data, int size);
  5. xs_val *xs_hex_dec(const xs_str *hex, int *size);
  6. int xs_is_hex(const char *str);
  7. xs_str *xs_base32_enc(const xs_val *data, int sz);
  8. xs_str *xs_base32hex_enc(const xs_val *data, int sz);
  9. xs_val *xs_base32_dec(const xs_str *data, int *size);
  10. xs_val *xs_base32hex_dec(const xs_str *data, int *size);
  11. xs_str *xs_base64_enc(const xs_val *data, int sz);
  12. xs_val *xs_base64_dec(const xs_str *data, int *size);
  13. int xs_is_base64(const char *str);
  14. xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint);
  15. #ifdef XS_IMPLEMENTATION
  16. /** hex **/
  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. p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
  24. for (n = 0; n < size; n++) {
  25. snprintf(p, 3, "%02x", (unsigned char)data[n]);
  26. p += 2;
  27. }
  28. *p = '\0';
  29. return s;
  30. }
  31. xs_val *xs_hex_dec(const xs_str *hex, int *size)
  32. /* decodes an hexdump into data */
  33. {
  34. int sz = strlen(hex);
  35. xs_val *s = NULL;
  36. char *p;
  37. int n;
  38. if (sz % 2)
  39. return NULL;
  40. p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
  41. for (n = 0; n < sz; n += 2) {
  42. int i;
  43. if (sscanf(&hex[n], "%02x", &i) == 0) {
  44. /* decoding error */
  45. return xs_free(s);
  46. }
  47. else
  48. *p = i;
  49. p++;
  50. }
  51. *p = '\0';
  52. *size = sz / 2;
  53. return s;
  54. }
  55. int xs_is_hex(const char *str)
  56. /* returns 1 if str is an hex string */
  57. {
  58. while (*str) {
  59. if (strchr("0123456789abcdefABCDEF", *str++) == NULL)
  60. return 0;
  61. }
  62. return 1;
  63. }
  64. /** base32 */
  65. static char *xs_b32_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  66. "234567=";
  67. static char *xs_b32hex_tbl = "0123456789"
  68. "ABCDEFGHIJKLMNOPQRSTUV=";
  69. /*
  70. 00000|00011|11111|12222|22223|33333|33444|44444
  71. */
  72. xs_str *xs_base32_enc_tbl(const xs_val *data, int sz, const char *b32_tbl)
  73. /* encodes data to base32 using a table */
  74. {
  75. xs_str *s = xs_str_new(NULL);
  76. unsigned char *p;
  77. int n;
  78. p = (unsigned char *)data;
  79. for (n = 0; n < sz; n += 5) {
  80. int l = sz - n;
  81. char enc[9] = "========";
  82. enc[0] = b32_tbl[(p[n] >> 3) & 0x1f];
  83. if (l > 1) {
  84. enc[1] = b32_tbl[(p[n] << 2 | p[n + 1] >> 6) & 0x1f];
  85. enc[2] = b32_tbl[(p[n + 1] >> 1) & 0x1f];
  86. if (l > 2) {
  87. enc[3] = b32_tbl[(p[n + 1] << 4 | p[n + 2] >> 4) & 0x1f];
  88. if (l > 3) {
  89. enc[4] = b32_tbl[(p[n + 2] << 1 | p[n + 3] >> 7) & 0x1f];
  90. enc[5] = b32_tbl[(p[n + 3] >> 2) & 0x1f];
  91. if (l > 4) {
  92. enc[6] = b32_tbl[(p[n + 3] << 3 | p[n + 4] >> 5) & 0x1f];
  93. enc[7] = b32_tbl[(p[n + 4]) & 0x1f];
  94. }
  95. else
  96. enc[6] = b32_tbl[(p[n + 3] << 3) & 0x1f];
  97. }
  98. else
  99. enc[4] = b32_tbl[(p[n + 2] << 1) & 0x1f];
  100. }
  101. else
  102. enc[3] = b32_tbl[(p[n + 1] << 4) & 0x1f];
  103. }
  104. else
  105. enc[1] = b32_tbl[(p[n] << 2) & 0x1f];
  106. s = xs_str_cat(s, enc);
  107. }
  108. return s;
  109. }
  110. xs_str *xs_base32_enc(const xs_val *data, int sz)
  111. /* encodes data to base32 */
  112. {
  113. return xs_base32_enc_tbl(data, sz, xs_b32_tbl);
  114. }
  115. xs_str *xs_base32hex_enc(const xs_val *data, int sz)
  116. /* encodes data to base32 with HEX alphabet (RFC4648) */
  117. {
  118. return xs_base32_enc_tbl(data, sz, xs_b32hex_tbl);
  119. }
  120. xs_val *xs_base32_dec_tbl(const xs_str *data, int *size, const char *b32_tbl)
  121. /* decodes data from base32 using a table */
  122. {
  123. xs_val *s = NULL;
  124. int sz = 0;
  125. char *p;
  126. p = (char *)data;
  127. /* size of data must be a multiple of 8 */
  128. if (strlen(p) % 8)
  129. return NULL;
  130. for (p = (char *)data; *p; p += 8) {
  131. int cs[8];
  132. int n;
  133. unsigned char tmp[5];
  134. for (n = 0; n < 8; n++) {
  135. char *ss = strchr(b32_tbl, p[n]);
  136. if (ss == NULL) {
  137. /* not a base32 char */
  138. return xs_free(s);
  139. }
  140. cs[n] = ss - b32_tbl;
  141. }
  142. n = 0;
  143. /* #0 byte */
  144. tmp[n++] = cs[0] << 3 | cs[1] >> 2;
  145. if (cs[2] != 32) {
  146. /* #1 byte */
  147. tmp[n++] = (cs[1] & 0x3) << 6 | cs[2] << 1 | (cs[3] & 0x10) >> 4;
  148. if (cs[4] != 32) {
  149. /* #2 byte */
  150. tmp[n++] = (cs[3] & 0xf) << 4 | cs[4] >> 1;
  151. if (cs[5] != 32) {
  152. /* #3 byte */
  153. tmp[n++] = (cs[4] & 0x1) << 7 | cs[5] << 2 | cs[6] >> 3;
  154. if (cs[7] != 32) {
  155. /* #4 byte */
  156. tmp[n++] = (cs[6] & 0x7) << 5 | cs[7];
  157. }
  158. }
  159. }
  160. }
  161. /* must be done manually because data can be pure binary */
  162. s = xs_realloc(s, _xs_blk_size(sz + n));
  163. memcpy(s + sz, tmp, n);
  164. sz += n;
  165. }
  166. /* asciiz it to use it as a string */
  167. s = xs_realloc(s, _xs_blk_size(sz + 1));
  168. s[sz] = '\0';
  169. *size = sz;
  170. return s;
  171. }
  172. xs_val *xs_base32_dec(const xs_str *data, int *size)
  173. /* decodes data from base32 */
  174. {
  175. return xs_base32_dec_tbl(data, size, xs_b32_tbl);
  176. }
  177. xs_val *xs_base32hex_dec(const xs_str *data, int *size)
  178. /* decodes data from base32 with HEX alphabet (RFC4648) */
  179. {
  180. return xs_base32_dec_tbl(data, size, xs_b32hex_tbl);
  181. }
  182. /** base64 */
  183. static char *xs_b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  184. "abcdefghijklmnopqrstuvwxyz"
  185. "0123456789+/=";
  186. xs_str *xs_base64_enc_tbl(const xs_val *data, int sz, const char *b64_tbl)
  187. /* encodes data to base64 using a table */
  188. {
  189. xs_str *s;
  190. unsigned char *p;
  191. char *i;
  192. int bsz, n;
  193. bsz = ((sz + 3 - 1) / 3) * 4;
  194. i = s = xs_realloc(NULL, _xs_blk_size(bsz + 1));
  195. p = (unsigned char *)data;
  196. for (n = 0; n < sz; n += 3) {
  197. int l = sz - n;
  198. if (l == 1) {
  199. *i++ = b64_tbl[(p[n] >> 2) & 0x3f];
  200. *i++ = b64_tbl[(p[n] << 4) & 0x3f];
  201. *i++ = '=';
  202. *i++ = '=';
  203. }
  204. else
  205. if (l == 2) {
  206. *i++ = b64_tbl[(p[n] >> 2) & 0x3f];
  207. *i++ = b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f];
  208. *i++ = b64_tbl[(p[n + 1] << 2) & 0x3f];
  209. *i++ = '=';
  210. }
  211. else {
  212. *i++ = b64_tbl[(p[n] >> 2) & 0x3f];
  213. *i++ = b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f];
  214. *i++ = b64_tbl[(p[n + 1] << 2 | p[n + 2] >> 6) & 0x3f];
  215. *i++ = b64_tbl[(p[n + 2]) & 0x3f];
  216. }
  217. }
  218. *i = '\0';
  219. return s;
  220. }
  221. xs_str *xs_base64_enc(const xs_val *data, int sz)
  222. /* encodes data to base64 */
  223. {
  224. return xs_base64_enc_tbl(data, sz, xs_b64_tbl);
  225. }
  226. xs_val *xs_base64_dec_tbl(const xs_str *data, int *size, const char *b64_tbl)
  227. /* decodes data from base64 using a table */
  228. {
  229. xs_val *s = NULL;
  230. int sz = 0;
  231. char *p;
  232. p = (char *)data;
  233. /* size of data must be a multiple of 4 */
  234. if (strlen(p) % 4)
  235. return NULL;
  236. for (p = (char *)data; *p; p += 4) {
  237. int cs[4];
  238. int n;
  239. unsigned char tmp[3];
  240. for (n = 0; n < 4; n++) {
  241. char *ss = strchr(b64_tbl, p[n]);
  242. if (ss == NULL) {
  243. /* not a base64 char */
  244. return xs_free(s);
  245. }
  246. cs[n] = ss - b64_tbl;
  247. }
  248. n = 0;
  249. /* first byte */
  250. tmp[n++] = cs[0] << 2 | ((cs[1] >> 4) & 0x0f);
  251. /* second byte */
  252. if (cs[2] != 64)
  253. tmp[n++] = cs[1] << 4 | ((cs[2] >> 2) & 0x3f);
  254. /* third byte */
  255. if (cs[3] != 64)
  256. tmp[n++] = cs[2] << 6 | (cs[3] & 0x3f);
  257. /* must be done manually because data can be pure binary */
  258. s = xs_realloc(s, _xs_blk_size(sz + n));
  259. memcpy(s + sz, tmp, n);
  260. sz += n;
  261. }
  262. /* asciiz it to use it as a string */
  263. s = xs_realloc(s, _xs_blk_size(sz + 1));
  264. s[sz] = '\0';
  265. *size = sz;
  266. return s;
  267. }
  268. xs_val *xs_base64_dec(const xs_str *data, int *size)
  269. /* decodes data from base64 */
  270. {
  271. return xs_base64_dec_tbl(data, size, xs_b64_tbl);
  272. }
  273. int xs_is_base64_tbl(const char *str, const char *b64_tbl)
  274. /* returns 1 if str is a base64 string, with table */
  275. {
  276. while (*str) {
  277. if (strchr(b64_tbl, *str++) == NULL)
  278. return 0;
  279. }
  280. return 1;
  281. }
  282. int xs_is_base64(const char *str)
  283. /* returns 1 if str is a base64 string */
  284. {
  285. return xs_is_base64_tbl(str, xs_b64_tbl);
  286. }
  287. /** utf-8 **/
  288. xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint)
  289. /* encodes an Unicode codepoint to utf8 */
  290. {
  291. unsigned char tmp[4];
  292. int n = 0;
  293. if (cpoint < 0x80)
  294. tmp[n++] = cpoint & 0xff;
  295. else
  296. if (cpoint < 0x800) {
  297. tmp[n++] = 0xc0 | (cpoint >> 6);
  298. tmp[n++] = 0x80 | (cpoint & 0x3f);
  299. }
  300. else
  301. if (cpoint < 0x10000) {
  302. tmp[n++] = 0xe0 | (cpoint >> 12);
  303. tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f);
  304. tmp[n++] = 0x80 | (cpoint & 0x3f);
  305. }
  306. else
  307. if (cpoint < 0x200000) {
  308. tmp[n++] = 0xf0 | (cpoint >> 18);
  309. tmp[n++] = 0x80 | ((cpoint >> 12) & 0x3f);
  310. tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f);
  311. tmp[n++] = 0x80 | (cpoint & 0x3f);
  312. }
  313. return xs_append_m(str, (char *)tmp, n);
  314. }
  315. #endif /* XS_IMPLEMENTATION */
  316. #endif /* _XS_ENCDEC_H */