xs_hex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  2. #ifndef _XS_HEX_H
  3. #define _XS_HEX_H
  4. int xs_is_hex_digit(char str);
  5. void xs_hex_enc_1(char **dst, const char **src);
  6. int xs_hex_dec_1(char **dst, const char **src);
  7. char *_xs_hex_enc(char *dst, const char *src, int src_size);
  8. char *_xs_hex_dec(char *dst, const char *src, int src_size);
  9. #ifdef _XS_H
  10. xs_str *xs_hex_enc(const xs_val *data, int size);
  11. xs_val *xs_hex_dec(const xs_str *hex, int *size);
  12. int xs_is_hex(const char *str);
  13. #endif /* _XS_H */
  14. #ifdef XS_IMPLEMENTATION
  15. #include <string.h>
  16. /** hex **/
  17. static char rev_hex_digits[] = "fedcba9876543210FEDCBA";
  18. int xs_is_hex_digit(char str)
  19. /* checks if the char is an hex digit */
  20. {
  21. return strchr(rev_hex_digits, str) != NULL;
  22. }
  23. void xs_hex_enc_1(char **dst, const char **src)
  24. /* decodes one character into two hex digits */
  25. {
  26. const char *i = *src;
  27. char *o = *dst;
  28. *o++ = rev_hex_digits[0xf - (*i >> 4 & 0xf)];
  29. *o++ = rev_hex_digits[0xf - (*i & 0xf)];
  30. *src = i + 1;
  31. *dst = o;
  32. }
  33. int xs_hex_dec_1(char **dst, const char **src)
  34. /* decodes two hex digits (returns 0 on error) */
  35. {
  36. const char *i = *src;
  37. char *o = *dst;
  38. char *d1 = strchr(rev_hex_digits, *i++);
  39. char *d2 = strchr(rev_hex_digits, *i++);
  40. if (!d1 || !d2) {
  41. /* decoding error */
  42. return 0;
  43. }
  44. *o++ = (0xf - ((d1 - rev_hex_digits) & 0xf)) << 4 |
  45. (0xf - ((d2 - rev_hex_digits) & 0xf));
  46. *src = i;
  47. *dst = o;
  48. return 1;
  49. }
  50. char *_xs_hex_enc(char *dst, const char *src, int src_size)
  51. /* hex-encodes the src buffer into dst, which has enough size */
  52. {
  53. const char *e = src + src_size;
  54. while (src < e)
  55. xs_hex_enc_1(&dst, &src);
  56. return dst;
  57. }
  58. char *_xs_hex_dec(char *dst, const char *src, int src_size)
  59. /* hex-decodes the src string int dst, which has enough size.
  60. return NULL on decoding errors or the final position of dst */
  61. {
  62. if (src_size % 2)
  63. return NULL;
  64. const char *e = src + src_size;
  65. while (src < e) {
  66. if (!xs_hex_dec_1(&dst, &src))
  67. return NULL;
  68. }
  69. return dst;
  70. }
  71. #ifdef _XS_H
  72. xs_str *xs_hex_enc(const xs_val *data, int size)
  73. /* returns an hexdump of data */
  74. {
  75. xs_str *s = xs_realloc(NULL, _xs_blk_size(size * 2 + 1));
  76. char *q = _xs_hex_enc(s, data, size);
  77. *q = '\0';
  78. return s;
  79. }
  80. xs_val *xs_hex_dec(const xs_str *hex, int *size)
  81. /* decodes an hexdump into data */
  82. {
  83. int sz = strlen(hex);
  84. xs_val *s = NULL;
  85. *size = sz / 2;
  86. s = xs_realloc(NULL, _xs_blk_size(*size + 1));
  87. if (!_xs_hex_dec(s, hex, sz))
  88. return xs_free(s);
  89. s[*size] = '\0';
  90. return s;
  91. }
  92. int xs_is_hex(const char *str)
  93. /* returns 1 if str is an hex string */
  94. {
  95. if (strlen(str) % 2)
  96. return 0;
  97. while (*str) {
  98. if (!xs_is_hex_digit(*str++))
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. #endif /* _XS_H */
  104. #endif /* XS_IMPLEMENTATION */
  105. #endif /* _XS_HEX_H */