hex.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* hex.c - conversion for hexadecimal and base32 strings.
  2. *
  3. * Copyright (c) 2008, Aleksey Kravchenko <[email protected]>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "hex.h"
  17. #include "util.h"
  18. #include <ctype.h>
  19. #include <string.h>
  20. /**
  21. * Store hexadecimal representation of a binary string to given buffer.
  22. *
  23. * @param dst the buffer to receive hexadecimal representation
  24. * @param src binary string
  25. * @param length string length
  26. * @param upper_case flag to print string in uppercase
  27. */
  28. void rhash_byte_to_hex(char* dst, const unsigned char* src, size_t length, int upper_case)
  29. {
  30. const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10);
  31. for (; length > 0; src++, length--) {
  32. const unsigned char hi = (*src >> 4) & 15;
  33. const unsigned char lo = *src & 15;
  34. *dst++ = (hi > 9 ? hi + hex_add : hi + '0');
  35. *dst++ = (lo > 9 ? lo + hex_add : lo + '0');
  36. }
  37. *dst = '\0';
  38. }
  39. /**
  40. * Encode a binary string to base32.
  41. *
  42. * @param dst the buffer to store result
  43. * @param src binary string
  44. * @param length string length
  45. * @param upper_case flag to print string in uppercase
  46. */
  47. void rhash_byte_to_base32(char* dst, const unsigned char* src, size_t length, int upper_case)
  48. {
  49. const char a = (upper_case ? 'A' : 'a');
  50. unsigned shift = 0;
  51. unsigned char word;
  52. const unsigned char* e = src + length;
  53. while (src < e) {
  54. if (shift > 3) {
  55. word = (*src & (0xFF >> shift));
  56. shift = (shift + 5) % 8;
  57. word <<= shift;
  58. if (src + 1 < e)
  59. word |= *(src + 1) >> (8 - shift);
  60. ++src;
  61. } else {
  62. shift = (shift + 5) % 8;
  63. word = ( *src >> ( (8 - shift) & 7 ) ) & 0x1F;
  64. if (shift == 0) src++;
  65. }
  66. *dst++ = ( word < 26 ? word + a : word + '2' - 26 );
  67. }
  68. *dst = '\0';
  69. }
  70. /**
  71. * Encode a binary string to base64.
  72. * Encoded output length is always a multiple of 4 bytes.
  73. *
  74. * @param dst the buffer to store result
  75. * @param src binary string
  76. * @param length string length
  77. */
  78. void rhash_byte_to_base64(char* dst, const unsigned char* src, size_t length)
  79. {
  80. static const char* tail = "0123456789+/";
  81. unsigned shift = 0;
  82. unsigned char word;
  83. const unsigned char* e = src + length;
  84. while (src < e) {
  85. if (shift > 2) {
  86. word = (*src & (0xFF >> shift));
  87. shift = (shift + 6) % 8;
  88. word <<= shift;
  89. if (src + 1 < e)
  90. word |= *(src + 1) >> (8 - shift);
  91. ++src;
  92. } else {
  93. shift = (shift + 6) % 8;
  94. word = ( *src >> ( (8 - shift) & 7 ) ) & 0x3F;
  95. if (shift == 0) src++;
  96. }
  97. *dst++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]);
  98. }
  99. if (shift > 0) {
  100. *dst++ = '=';
  101. if (shift == 4) *dst++ = '=';
  102. }
  103. *dst = '\0';
  104. }
  105. size_t rhash_base64_url_encoded_helper(char* dst, const unsigned char* src, size_t length, int url_encode, int upper_case)
  106. {
  107. #define B64_CHUNK_SIZE 120
  108. char buffer[164];
  109. #ifdef __clang_analyzer__
  110. memset(buffer, 0, sizeof(buffer));
  111. #endif
  112. RHASH_ASSERT((BASE64_LENGTH(B64_CHUNK_SIZE) + 4) <= sizeof(buffer));
  113. RHASH_ASSERT((B64_CHUNK_SIZE % 6) == 0);
  114. if (url_encode) {
  115. size_t result_length = 0;
  116. for (; length > 0; src += B64_CHUNK_SIZE) {
  117. size_t chunk_size = (length < B64_CHUNK_SIZE ? length : B64_CHUNK_SIZE);
  118. size_t encoded_length;
  119. rhash_byte_to_base64(buffer, src, chunk_size);
  120. encoded_length = rhash_urlencode(dst, buffer, BASE64_LENGTH(chunk_size), upper_case);
  121. result_length += encoded_length;
  122. dst += encoded_length;
  123. length -= chunk_size;
  124. }
  125. return result_length;
  126. }
  127. rhash_byte_to_base64(dst, src, length);
  128. return BASE64_LENGTH(length);
  129. }
  130. /* RFC 3986: safe url characters are ascii alpha-numeric and "-._~", other characters should be percent-encoded */
  131. static unsigned url_safe_char_mask[4] = { 0, 0x03ff6000, 0x87fffffe, 0x47fffffe };
  132. #define IS_URL_GOOD_CHAR(c) ((unsigned)(c) < 128 && (url_safe_char_mask[c >> 5] & (1 << (c & 31))))
  133. /**
  134. * URL-encode specified binary string.
  135. *
  136. * @param dst (nullable) buffer to output encoded string to,
  137. * NULL to just calculate the lengths of encoded string
  138. * @param src binary string to encode
  139. * @param size size of the binary string
  140. * @param upper_case flag to output hex-codes in uppercase
  141. * @return the length of the result string
  142. */
  143. size_t rhash_urlencode(char* dst, const char* src, size_t size, int upper_case)
  144. {
  145. const char* start;
  146. size_t i;
  147. if (!dst) {
  148. size_t length = size;
  149. for (i = 0; i < size; i++)
  150. if (!IS_URL_GOOD_CHAR(src[i]))
  151. length += 2;
  152. return length;
  153. } else {
  154. const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10);
  155. start = dst;
  156. /* percent-encode all but unreserved URL characters */
  157. for (i = 0; i < size; i++) {
  158. if (IS_URL_GOOD_CHAR(src[i])) {
  159. *dst++ = src[i];
  160. } else {
  161. unsigned char hi = ((unsigned char)(src[i]) >> 4) & 0x0f;
  162. unsigned char lo = (unsigned char)(src[i]) & 0x0f;
  163. *dst++ = '%';
  164. *dst++ = (hi > 9 ? hi + hex_add : hi + '0');
  165. *dst++ = (lo > 9 ? lo + hex_add : lo + '0');
  166. }
  167. }
  168. *dst = 0;
  169. }
  170. return dst - start;
  171. }
  172. /**
  173. * Print 64-bit number with trailing '\0' to a string buffer.
  174. * if dst is NULL, then just return the length of the number.
  175. *
  176. * @param dst output buffer
  177. * @param number the number to print
  178. * @return length of the printed number (without trailing '\0')
  179. */
  180. int rhash_sprintI64(char* dst, uint64_t number)
  181. {
  182. /* The biggest number has 20 digits: 2^64 = 18 446 744 073 709 551 616 */
  183. char buf[24];
  184. char* p;
  185. size_t length;
  186. if (dst == NULL) {
  187. /* just calculate the length of the number */
  188. if (number == 0) return 1;
  189. for (length = 0; number != 0; number /= 10) length++;
  190. return (int)length;
  191. }
  192. p = buf + 23;
  193. *p = '\0'; /* last symbol should be '\0' */
  194. if (number == 0) {
  195. *(--p) = '0';
  196. } else {
  197. for (; p >= buf && number != 0; number /= 10) {
  198. *(--p) = '0' + (char)(number % 10);
  199. }
  200. }
  201. length = buf + 23 - p;
  202. memcpy(dst, p, length + 1);
  203. return (int)length;
  204. }