hex.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* hex.c - conversion for hexadecimal and base32 strings.
  2. *
  3. * Copyright: 2008-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
  15. */
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include "hex.h"
  19. /**
  20. * Convert a byte to a hexadecimal number. The result, consisting of two
  21. * hexadecimal digits is stored into a buffer.
  22. *
  23. * @param dest the buffer to receive two symbols of hex representation
  24. * @param byte the byte to decode
  25. * @param upper_case flag to print string in uppercase
  26. * @return pointer to the chararcter just after the written number (dest + 2)
  27. */
  28. char* rhash_print_hex_byte(char *dest, const unsigned char byte, int upper_case)
  29. {
  30. const char add = (upper_case ? 'A' - 10 : 'a' - 10);
  31. unsigned char c = (byte >> 4) & 15;
  32. *dest++ = (c > 9 ? c + add : c + '0');
  33. c = byte & 15;
  34. *dest++ = (c > 9 ? c + add : c + '0');
  35. return dest;
  36. }
  37. /**
  38. * Store hexadecimal representation of a binary string to given buffer.
  39. *
  40. * @param dest the buffer to receive hexadecimal representation
  41. * @param src binary string
  42. * @param len string length
  43. * @param upper_case flag to print string in uppercase
  44. */
  45. void rhash_byte_to_hex(char *dest, const unsigned char *src, unsigned len, int upper_case)
  46. {
  47. while (len-- > 0) {
  48. dest = rhash_print_hex_byte(dest, *src++, upper_case);
  49. }
  50. *dest = '\0';
  51. }
  52. /**
  53. * Encode a binary string to base32.
  54. *
  55. * @param dest the buffer to store result
  56. * @param src binary string
  57. * @param len string length
  58. * @param upper_case flag to print string in uppercase
  59. */
  60. void rhash_byte_to_base32(char* dest, const unsigned char* src, unsigned len, int upper_case)
  61. {
  62. const char a = (upper_case ? 'A' : 'a');
  63. unsigned shift = 0;
  64. unsigned char word;
  65. const unsigned char* e = src + len;
  66. while (src < e) {
  67. if (shift > 3) {
  68. word = (*src & (0xFF >> shift));
  69. shift = (shift + 5) % 8;
  70. word <<= shift;
  71. if (src + 1 < e)
  72. word |= *(src + 1) >> (8 - shift);
  73. ++src;
  74. } else {
  75. shift = (shift + 5) % 8;
  76. word = ( *src >> ( (8 - shift) & 7 ) ) & 0x1F;
  77. if (shift == 0) src++;
  78. }
  79. *dest++ = ( word < 26 ? word + a : word + '2' - 26 );
  80. }
  81. *dest = '\0';
  82. }
  83. /**
  84. * Encode a binary string to base64.
  85. * Encoded output length is always a multiple of 4 bytes.
  86. *
  87. * @param dest the buffer to store result
  88. * @param src binary string
  89. * @param len string length
  90. */
  91. void rhash_byte_to_base64(char* dest, const unsigned char* src, unsigned len)
  92. {
  93. static const char* tail = "0123456789+/";
  94. unsigned shift = 0;
  95. unsigned char word;
  96. const unsigned char* e = src + len;
  97. while (src < e) {
  98. if (shift > 2) {
  99. word = (*src & (0xFF >> shift));
  100. shift = (shift + 6) % 8;
  101. word <<= shift;
  102. if (src + 1 < e)
  103. word |= *(src + 1) >> (8 - shift);
  104. ++src;
  105. } else {
  106. shift = (shift + 6) % 8;
  107. word = ( *src >> ( (8 - shift) & 7 ) ) & 0x3F;
  108. if (shift == 0) src++;
  109. }
  110. *dest++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]);
  111. }
  112. if (shift > 0) {
  113. *dest++ = '=';
  114. if (shift == 4) *dest++ = '=';
  115. }
  116. *dest = '\0';
  117. }
  118. /* unsafe characters are "<>{}[]%#/|\^~`@:;?=&+ */
  119. #define IS_GOOD_URL_CHAR(c) (isalnum((unsigned char)c) || strchr("$-_.!'(),", c))
  120. /**
  121. * URL-encode a string.
  122. *
  123. * @param dst buffer to receive result or NULL to calculate
  124. * the lengths of encoded string
  125. * @param filename the file name
  126. * @return the length of the result string
  127. */
  128. int rhash_urlencode(char *dst, const char *name)
  129. {
  130. const char *start;
  131. if (!dst) {
  132. int len;
  133. for (len = 0; *name; name++) len += (IS_GOOD_URL_CHAR(*name) ? 1 : 3);
  134. return len;
  135. }
  136. /* encode URL as specified by RFC 1738 */
  137. for (start = dst; *name; name++) {
  138. if ( IS_GOOD_URL_CHAR(*name) ) {
  139. *dst++ = *name;
  140. } else {
  141. *dst++ = '%';
  142. dst = rhash_print_hex_byte(dst, *name, 'A');
  143. }
  144. }
  145. *dst = 0;
  146. return (int)(dst - start);
  147. }
  148. /**
  149. * Print 64-bit number with trailing '\0' to a string buffer.
  150. * if dst is NULL, then just return the length of the number.
  151. *
  152. * @param dst output buffer
  153. * @param number the number to print
  154. * @return length of the printed number (without trailing '\0')
  155. */
  156. int rhash_sprintI64(char *dst, uint64_t number)
  157. {
  158. /* The biggest number has 20 digits: 2^64 = 18 446 744 073 709 551 616 */
  159. char buf[24], *p;
  160. size_t length;
  161. if (dst == NULL) {
  162. /* just calculate the length of the number */
  163. if (number == 0) return 1;
  164. for (length = 0; number != 0; number /= 10) length++;
  165. return (int)length;
  166. }
  167. p = buf + 23;
  168. *p = '\0'; /* last symbol should be '\0' */
  169. if (number == 0) {
  170. *(--p) = '0';
  171. } else {
  172. for (; p >= buf && number != 0; number /= 10) {
  173. *(--p) = '0' + (char)(number % 10);
  174. }
  175. }
  176. length = buf + 23 - p;
  177. memcpy(dst, p, length + 1);
  178. return (int)length;
  179. }