xs_encdec.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_base64_enc(const xs_val *data, int sz);
  8. xs_val *xs_base64_dec(const xs_str *data, int *size);
  9. int xs_is_base64(const char *str);
  10. #ifdef XS_IMPLEMENTATION
  11. /** hex **/
  12. xs_str *xs_hex_enc(const xs_val *data, int size)
  13. /* returns an hexdump of data */
  14. {
  15. xs_str *s;
  16. char *p;
  17. int n;
  18. p = s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
  19. for (n = 0; n < size; n++) {
  20. snprintf(p, 3, "%02x", (unsigned char)data[n]);
  21. p += 2;
  22. }
  23. *p = '\0';
  24. return s;
  25. }
  26. xs_val *xs_hex_dec(const xs_str *hex, int *size)
  27. /* decodes an hexdump into data */
  28. {
  29. int sz = strlen(hex);
  30. xs_val *s = NULL;
  31. char *p;
  32. int n;
  33. if (sz % 2)
  34. return NULL;
  35. p = s = xs_realloc(NULL, _xs_blk_size(sz / 2 + 1));
  36. for (n = 0; n < sz; n += 2) {
  37. int i;
  38. if (sscanf(&hex[n], "%02x", &i) == 0) {
  39. /* decoding error */
  40. return xs_free(s);
  41. }
  42. else
  43. *p = i;
  44. p++;
  45. }
  46. *p = '\0';
  47. *size = sz / 2;
  48. return s;
  49. }
  50. int xs_is_hex(const char *str)
  51. /* returns 1 if str is an hex string */
  52. {
  53. while (*str) {
  54. if (strchr("0123456789abcdefABCDEF", *str++) == NULL)
  55. return 0;
  56. }
  57. return 1;
  58. }
  59. /** base64 */
  60. static char *xs_b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  61. "abcdefghijklmnopqrstuvwxyz"
  62. "0123456789+/=";
  63. xs_str *xs_base64_enc_tbl(const xs_val *data, int sz, const char *b64_tbl)
  64. /* encodes data to base64 using a table */
  65. {
  66. xs_str *s;
  67. unsigned char *p;
  68. char *i;
  69. int bsz, n;
  70. bsz = ((sz + 3 - 1) / 3) * 4;
  71. i = s = xs_realloc(NULL, _xs_blk_size(bsz + 1));
  72. p = (unsigned char *)data;
  73. for (n = 0; n < sz; n += 3) {
  74. int l = sz - n;
  75. if (l == 1) {
  76. *i++ = b64_tbl[(p[n] >> 2) & 0x3f];
  77. *i++ = b64_tbl[(p[n] << 4) & 0x3f];
  78. *i++ = '=';
  79. *i++ = '=';
  80. }
  81. else
  82. if (l == 2) {
  83. *i++ = b64_tbl[(p[n] >> 2) & 0x3f];
  84. *i++ = b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f];
  85. *i++ = b64_tbl[(p[n + 1] << 2) & 0x3f];
  86. *i++ = '=';
  87. }
  88. else {
  89. *i++ = b64_tbl[(p[n] >> 2) & 0x3f];
  90. *i++ = b64_tbl[(p[n] << 4 | p[n + 1] >> 4) & 0x3f];
  91. *i++ = b64_tbl[(p[n + 1] << 2 | p[n + 2] >> 6) & 0x3f];
  92. *i++ = b64_tbl[(p[n + 2]) & 0x3f];
  93. }
  94. }
  95. *i = '\0';
  96. return s;
  97. }
  98. xs_str *xs_base64_enc(const xs_val *data, int sz)
  99. /* encodes data to base64 */
  100. {
  101. return xs_base64_enc_tbl(data, sz, xs_b64_tbl);
  102. }
  103. xs_val *xs_base64_dec_tbl(const xs_str *data, int *size, const char *b64_tbl)
  104. /* decodes data from base64 using a table */
  105. {
  106. xs_val *s = NULL;
  107. int sz = 0;
  108. char *p;
  109. p = (char *)data;
  110. /* size of data must be a multiple of 4 */
  111. if (strlen(p) % 4)
  112. return NULL;
  113. for (p = (char *)data; *p; p += 4) {
  114. int cs[4];
  115. int n;
  116. unsigned char tmp[3];
  117. for (n = 0; n < 4; n++) {
  118. char *ss = strchr(b64_tbl, p[n]);
  119. if (ss == NULL) {
  120. /* not a base64 char */
  121. return xs_free(s);
  122. }
  123. cs[n] = ss - b64_tbl;
  124. }
  125. n = 0;
  126. /* first byte */
  127. tmp[n++] = cs[0] << 2 | ((cs[1] >> 4) & 0x0f);
  128. /* second byte */
  129. if (cs[2] != 64)
  130. tmp[n++] = cs[1] << 4 | ((cs[2] >> 2) & 0x3f);
  131. /* third byte */
  132. if (cs[3] != 64)
  133. tmp[n++] = cs[2] << 6 | (cs[3] & 0x3f);
  134. /* must be done manually because data can be pure binary */
  135. s = xs_realloc(s, _xs_blk_size(sz + n));
  136. memcpy(s + sz, tmp, n);
  137. sz += n;
  138. }
  139. /* asciiz it to use it as a string */
  140. s = xs_realloc(s, _xs_blk_size(sz + 1));
  141. s[sz] = '\0';
  142. *size = sz;
  143. return s;
  144. }
  145. xs_val *xs_base64_dec(const xs_str *data, int *size)
  146. /* decodes data from base64 */
  147. {
  148. return xs_base64_dec_tbl(data, size, xs_b64_tbl);
  149. }
  150. int xs_is_base64_tbl(const char *str, const char *b64_tbl)
  151. /* returns 1 if str is a base64 string, with table */
  152. {
  153. while (*str) {
  154. if (strchr(b64_tbl, *str++) == NULL)
  155. return 0;
  156. }
  157. return 1;
  158. }
  159. int xs_is_base64(const char *str)
  160. /* returns 1 if str is a base64 string */
  161. {
  162. return xs_is_base64_tbl(str, xs_b64_tbl);
  163. }
  164. #endif /* XS_IMPLEMENTATION */
  165. #endif /* _XS_ENCDEC_H */