| 1 |
|
| /* hex.c - conversion for hexadecimal and base32 strings. |
| 2 |
|
| * |
| 3 |
|
| * Copyright (c) 2008, Aleksey Kravchenko <rhash.admin@gmail.com> |
| 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 |
|
| /** |
| 22 |
|
| * Store hexadecimal representation of a binary string to given buffer. |
| 23 |
|
| * |
| 24 |
|
| * @param dst the buffer to receive hexadecimal representation |
| 25 |
|
| * @param src binary string |
| 26 |
|
| * @param length string length |
| 27 |
|
| * @param upper_case flag to print string in uppercase |
| 28 |
|
| */ |
| 29 |
45 |
| void rhash_byte_to_hex(char* dst, const unsigned char* src, size_t length, int upper_case) |
| 30 |
|
| { |
| 31 |
45 |
▶ | const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10); |
Branch: 1 branch
| Branch | True | False | Total |
| const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10); |
0 |
45 |
45 |
|
| 32 |
1245 |
▶ | for (; length > 0; src++, length--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; length > 0; src++, length--) { |
1200 |
45 |
1245 |
|
| 33 |
1200 |
| const unsigned char hi = (*src >> 4) & 15; |
| 34 |
1200 |
| const unsigned char lo = *src & 15; |
| 35 |
1200 |
▶ | *dst++ = (hi > 9 ? hi + hex_add : hi + '0'); |
Branch: 1 branch
| Branch | True | False | Total |
| *dst++ = (hi > 9 ? hi + hex_add : hi + '0'); |
453 |
747 |
1200 |
|
| 36 |
1200 |
▶ | *dst++ = (lo > 9 ? lo + hex_add : lo + '0'); |
Branch: 1 branch
| Branch | True | False | Total |
| *dst++ = (lo > 9 ? lo + hex_add : lo + '0'); |
448 |
752 |
1200 |
|
| 37 |
|
| } |
| 38 |
45 |
| *dst = '\0'; |
| 39 |
45 |
| } |
| 40 |
|
| |
| 41 |
|
| /** |
| 42 |
|
| * Encode a binary string to base32. |
| 43 |
|
| * |
| 44 |
|
| * @param dst the buffer to store result |
| 45 |
|
| * @param src binary string |
| 46 |
|
| * @param length string length |
| 47 |
|
| * @param upper_case flag to print string in uppercase |
| 48 |
|
| */ |
| 49 |
8 |
| void rhash_byte_to_base32(char* dst, const unsigned char* src, size_t length, int upper_case) |
| 50 |
|
| { |
| 51 |
8 |
▶ | const char a = (upper_case ? 'A' : 'a'); |
Branch: 1 branch
| Branch | True | False | Total |
| const char a = (upper_case ? 'A' : 'a'); |
0 |
8 |
8 |
|
| 52 |
8 |
| unsigned shift = 0; |
| 53 |
|
| unsigned char word; |
| 54 |
8 |
| const unsigned char* e = src + length; |
| 55 |
260 |
▶ | while (src < e) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (src < e) { |
252 |
8 |
260 |
|
| 56 |
252 |
▶ | if (shift > 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (shift > 3) { |
128 |
124 |
252 |
|
| 57 |
128 |
| word = (*src & (0xFF >> shift)); |
| 58 |
128 |
| shift = (shift + 5) % 8; |
| 59 |
128 |
| word <<= shift; |
| 60 |
128 |
▶ | if (src + 1 < e) |
Branch: 1 branch
| Branch | True | False | Total |
| if (src + 1 < e) |
123 |
5 |
128 |
|
| 61 |
123 |
| word |= *(src + 1) >> (8 - shift); |
| 62 |
128 |
| ++src; |
| 63 |
|
| } else { |
| 64 |
124 |
| shift = (shift + 5) % 8; |
| 65 |
124 |
| word = ( *src >> ( (8 - shift) & 7 ) ) & 0x1F; |
| 66 |
124 |
▶ | if (shift == 0) src++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (shift == 0) src++; |
28 |
96 |
124 |
|
| 67 |
|
| } |
| 68 |
252 |
▶ | *dst++ = ( word < 26 ? word + a : word + '2' - 26 ); |
Branch: 1 branch
| Branch | True | False | Total |
| *dst++ = ( word < 26 ? word + a : word + '2' - 26 ); |
181 |
71 |
252 |
|
| 69 |
|
| } |
| 70 |
8 |
| *dst = '\0'; |
| 71 |
8 |
| } |
| 72 |
|
| |
| 73 |
|
| /** |
| 74 |
|
| * Encode a binary string to base64. |
| 75 |
|
| * Encoded output length is always a multiple of 4 bytes. |
| 76 |
|
| * |
| 77 |
|
| * @param dst the buffer to store result |
| 78 |
|
| * @param src binary string |
| 79 |
|
| * @param length string length |
| 80 |
|
| */ |
| 81 |
2 |
| void rhash_byte_to_base64(char* dst, const unsigned char* src, size_t length) |
| 82 |
|
| { |
| 83 |
|
| static const char* tail = "0123456789+/"; |
| 84 |
2 |
| unsigned shift = 0; |
| 85 |
|
| unsigned char word; |
| 86 |
2 |
| const unsigned char* e = src + length; |
| 87 |
35 |
▶ | while (src < e) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (src < e) { |
33 |
2 |
35 |
|
| 88 |
33 |
▶ | if (shift > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (shift > 2) { |
17 |
16 |
33 |
|
| 89 |
17 |
| word = (*src & (0xFF >> shift)); |
| 90 |
17 |
| shift = (shift + 6) % 8; |
| 91 |
17 |
| word <<= shift; |
| 92 |
17 |
▶ | if (src + 1 < e) |
Branch: 1 branch
| Branch | True | False | Total |
| if (src + 1 < e) |
15 |
2 |
17 |
|
| 93 |
15 |
| word |= *(src + 1) >> (8 - shift); |
| 94 |
17 |
| ++src; |
| 95 |
|
| } else { |
| 96 |
16 |
| shift = (shift + 6) % 8; |
| 97 |
16 |
| word = ( *src >> ( (8 - shift) & 7 ) ) & 0x3F; |
| 98 |
16 |
▶ | if (shift == 0) src++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (shift == 0) src++; |
7 |
9 |
16 |
|
| 99 |
|
| } |
| 100 |
33 |
▶ | *dst++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]); |
Branch: 2 branches
| Branch | True | False | Total |
| *dst++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]); |
26 |
7 |
33 |
| *dst++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]); |
10 |
16 |
26 |
|
| 101 |
|
| } |
| 102 |
2 |
▶ | if (shift > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (shift > 0) { |
2 |
0 |
2 |
|
| 103 |
2 |
| *dst++ = '='; |
| 104 |
2 |
▶ | if (shift == 4) *dst++ = '='; |
Branch: 1 branch
| Branch | True | False | Total |
| if (shift == 4) *dst++ = '='; |
1 |
1 |
2 |
|
| 105 |
|
| } |
| 106 |
2 |
| *dst = '\0'; |
| 107 |
2 |
| } |
| 108 |
|
| |
| 109 |
2 |
| size_t rhash_base64_url_encoded_helper(char* dst, const unsigned char* src, size_t length, int url_encode, int upper_case) |
| 110 |
|
| { |
| 111 |
|
| #define B64_CHUNK_SIZE 120 |
| 112 |
|
| char buffer[164]; |
| 113 |
|
| RHASH_ASSERT((BASE64_LENGTH(B64_CHUNK_SIZE) + 4) <= sizeof(buffer)); |
| 114 |
|
| RHASH_ASSERT((B64_CHUNK_SIZE % 6) == 0); |
| 115 |
|
| #ifdef __clang_analyzer__ |
| 116 |
|
| memset(buffer, 0, sizeof(buffer)); |
| 117 |
|
| #endif |
| 118 |
2 |
▶ | if (url_encode) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (url_encode) { |
0 |
2 |
2 |
|
| 119 |
0 |
| size_t result_length = 0; |
| 120 |
0 |
▶ | for (; length > 0; src += B64_CHUNK_SIZE) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; length > 0; src += B64_CHUNK_SIZE) { |
0 |
0 |
0 |
|
| 121 |
0 |
| size_t chunk_size = (length < B64_CHUNK_SIZE ? length : B64_CHUNK_SIZE); |
| 122 |
|
| size_t encoded_length; |
| 123 |
0 |
| rhash_byte_to_base64(buffer, src, chunk_size); |
| 124 |
0 |
| encoded_length = rhash_urlencode(dst, buffer, BASE64_LENGTH(chunk_size), upper_case); |
| 125 |
0 |
| result_length += encoded_length; |
| 126 |
0 |
| dst += encoded_length; |
| 127 |
0 |
| length -= chunk_size; |
| 128 |
|
| } |
| 129 |
0 |
| return result_length; |
| 130 |
|
| } |
| 131 |
2 |
| rhash_byte_to_base64(dst, src, length); |
| 132 |
2 |
| return BASE64_LENGTH(length); |
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
|
| /* RFC 3986: safe url characters are ascii alpha-numeric and "-._~", other characters should be percent-encoded */ |
| 136 |
|
| static unsigned url_safe_char_mask[4] = { 0, 0x03ff6000, 0x87fffffe, 0x47fffffe }; |
| 137 |
|
| #define IS_URL_GOOD_CHAR(c) ((unsigned)(c) < 128 && (url_safe_char_mask[c >> 5] & (1 << (c & 31)))) |
| 138 |
|
| |
| 139 |
|
| /** |
| 140 |
|
| * URL-encode specified binary string. |
| 141 |
|
| * |
| 142 |
|
| * @param dst (nullable) buffer to output encoded string to, |
| 143 |
|
| * NULL to just calculate the lengths of encoded string |
| 144 |
|
| * @param src binary string to encode |
| 145 |
|
| * @param size size of the binary string |
| 146 |
|
| * @param upper_case flag to output hex-codes in uppercase |
| 147 |
|
| * @return the length of the result string |
| 148 |
|
| */ |
| 149 |
4 |
| size_t rhash_urlencode(char* dst, const char* src, size_t size, int upper_case) |
| 150 |
|
| { |
| 151 |
|
| const char* start; |
| 152 |
|
| size_t i; |
| 153 |
4 |
▶ | if (!dst) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dst) { |
2 |
2 |
4 |
|
| 154 |
2 |
| size_t length = size; |
| 155 |
11 |
▶ | for (i = 0; i < size; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; i++) |
9 |
2 |
11 |
|
| 156 |
9 |
▶ | if (!IS_URL_GOOD_CHAR(src[i])) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!IS_URL_GOOD_CHAR(src[i])) |
9 |
0 |
9 |
| if (!IS_URL_GOOD_CHAR(src[i])) |
0 |
9 |
9 |
|
| 157 |
0 |
| length += 2; |
| 158 |
2 |
| return length; |
| 159 |
|
| } else { |
| 160 |
2 |
▶ | const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10); |
Branch: 1 branch
| Branch | True | False | Total |
| const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10); |
0 |
2 |
2 |
|
| 161 |
2 |
| start = dst; |
| 162 |
|
| /* percent-encode all but unreserved URL characters */ |
| 163 |
11 |
▶ | for (i = 0; i < size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; i++) { |
9 |
2 |
11 |
|
| 164 |
9 |
▶ | if (IS_URL_GOOD_CHAR(src[i])) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (IS_URL_GOOD_CHAR(src[i])) { |
9 |
0 |
9 |
| if (IS_URL_GOOD_CHAR(src[i])) { |
9 |
0 |
9 |
|
| 165 |
9 |
| *dst++ = src[i]; |
| 166 |
|
| } else { |
| 167 |
0 |
| unsigned char hi = ((unsigned char)(src[i]) >> 4) & 0x0f; |
| 168 |
0 |
| unsigned char lo = (unsigned char)(src[i]) & 0x0f; |
| 169 |
0 |
| *dst++ = '%'; |
| 170 |
0 |
▶ | *dst++ = (hi > 9 ? hi + hex_add : hi + '0'); |
Branch: 1 branch
| Branch | True | False | Total |
| *dst++ = (hi > 9 ? hi + hex_add : hi + '0'); |
0 |
0 |
0 |
|
| 171 |
0 |
▶ | *dst++ = (lo > 9 ? lo + hex_add : lo + '0'); |
Branch: 1 branch
| Branch | True | False | Total |
| *dst++ = (lo > 9 ? lo + hex_add : lo + '0'); |
0 |
0 |
0 |
|
| 172 |
|
| } |
| 173 |
|
| } |
| 174 |
2 |
| *dst = 0; |
| 175 |
|
| } |
| 176 |
2 |
| return dst - start; |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
|
| /** |
| 180 |
|
| * Print 64-bit number with trailing '\0' to a string buffer. |
| 181 |
|
| * if dst is NULL, then just return the length of the number. |
| 182 |
|
| * |
| 183 |
|
| * @param dst output buffer |
| 184 |
|
| * @param number the number to print |
| 185 |
|
| * @return length of the printed number (without trailing '\0') |
| 186 |
|
| */ |
| 187 |
10 |
| int rhash_sprintI64(char* dst, uint64_t number) |
| 188 |
|
| { |
| 189 |
|
| /* The biggest number has 20 digits: 2^64 = 18 446 744 073 709 551 616 */ |
| 190 |
|
| char buf[24]; |
| 191 |
|
| char* p; |
| 192 |
|
| size_t length; |
| 193 |
|
| |
| 194 |
10 |
▶ | if (dst == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dst == NULL) { |
0 |
10 |
10 |
|
| 195 |
|
| /* just calculate the length of the number */ |
| 196 |
0 |
▶ | if (number == 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (number == 0) return 1; |
0 |
0 |
0 |
|
| 197 |
0 |
▶ | for (length = 0; number != 0; number /= 10) length++; |
Branch: 1 branch
| Branch | True | False | Total |
| for (length = 0; number != 0; number /= 10) length++; |
0 |
0 |
0 |
|
| 198 |
0 |
| return (int)length; |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
10 |
| p = buf + 23; |
| 202 |
10 |
| *p = '\0'; /* last symbol should be '\0' */ |
| 203 |
10 |
▶ | if (number == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (number == 0) { |
0 |
10 |
10 |
|
| 204 |
0 |
| *(--p) = '0'; |
| 205 |
|
| } else { |
| 206 |
48 |
▶ | for (; p >= buf && number != 0; number /= 10) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (; p >= buf && number != 0; number /= 10) { |
48 |
0 |
48 |
| for (; p >= buf && number != 0; number /= 10) { |
38 |
10 |
48 |
|
| 207 |
38 |
| *(--p) = '0' + (char)(number % 10); |
| 208 |
|
| } |
| 209 |
|
| } |
| 210 |
10 |
| length = buf + 23 - p; |
| 211 |
10 |
| memcpy(dst, p, length + 1); |
| 212 |
10 |
| return (int)length; |
| 213 |
|
| } |