_hex.c

filter: Statementstmt 79.5% 74 / 93 Branchbran 68.0% 49 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.5% 123 / 165 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« _sha512.c ↑ Summary _md4.c »
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 <assert.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 43 void rhash_byte_to_hex(char* dst, const unsigned char* src, size_t length, int upper_case)
30 {
31 43 ▶ const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10);
Branch: 1 branch
BranchTrueFalseTotal
const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10); 0 43 43
32 1223 ▶ for (; length > 0; src++, length--) {
Branch: 1 branch
BranchTrueFalseTotal
for (; length > 0; src++, length--) { 1180 43 1223
33 1180 const unsigned char hi = (*src >> 4) & 15;
34 1180 const unsigned char lo = *src & 15;
35 1180 ▶ *dst++ = (hi > 9 ? hi + hex_add : hi + '0');
Branch: 1 branch
BranchTrueFalseTotal
*dst++ = (hi > 9 ? hi + hex_add : hi + '0'); 446 734 1180
36 1180 ▶ *dst++ = (lo > 9 ? lo + hex_add : lo + '0');
Branch: 1 branch
BranchTrueFalseTotal
*dst++ = (lo > 9 ? lo + hex_add : lo + '0'); 442 738 1180
37 }
38 43 *dst = '\0';
39 43 }
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
while (src < e) { 252 8 260
56 252 ▶ if (shift > 3) {
Branch: 1 branch
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
if (shift == 0) src++; 28 96 124
67 }
68 252 ▶ *dst++ = ( word < 26 ? word + a : word + '2' - 26 );
Branch: 1 branch
BranchTrueFalseTotal
*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 1 void rhash_byte_to_base64(char* dst, const unsigned char* src, size_t length)
82 {
83 static const char* tail = "0123456789+/";
84 1 unsigned shift = 0;
85 unsigned char word;
86 1 const unsigned char* e = src + length;
87 12 ▶ while (src < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (src < e) { 11 1 12
88 11 ▶ if (shift > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (shift > 2) { 6 5 11
89 6 word = (*src & (0xFF >> shift));
90 6 shift = (shift + 6) % 8;
91 6 word <<= shift;
92 6 ▶ if (src + 1 < e)
Branch: 1 branch
BranchTrueFalseTotal
if (src + 1 < e) 5 1 6
93 5 word |= *(src + 1) >> (8 - shift);
94 6 ++src;
95 } else {
96 5 shift = (shift + 6) % 8;
97 5 word = ( *src >> ( (8 - shift) & 7 ) ) & 0x3F;
98 5 ▶ if (shift == 0) src++;
Branch: 1 branch
BranchTrueFalseTotal
if (shift == 0) src++; 2 3 5
99 }
100 11 ▶ *dst++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]);
Branch: 2 branches
BranchTrueFalseTotal
*dst++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]); 10 1 11
*dst++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]); 4 6 10
101 }
102 1 ▶ if (shift > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (shift > 0) { 1 0 1
103 1 *dst++ = '=';
104 1 ▶ if (shift == 4) *dst++ = '=';
Branch: 1 branch
BranchTrueFalseTotal
if (shift == 4) *dst++ = '='; 0 1 1
105 }
106 1 *dst = '\0';
107 1 }
108  
109 1 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 assert((BASE64_LENGTH(B64_CHUNK_SIZE) + 4) <= sizeof(buffer));
114 assert((B64_CHUNK_SIZE % 6) == 0);
115 1 ▶ if (url_encode) {
Branch: 1 branch
BranchTrueFalseTotal
if (url_encode) { 0 1 1
116 0 size_t result_length = 0;
117 0 ▶ for (; length > 0; src += B64_CHUNK_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
for (; length > 0; src += B64_CHUNK_SIZE) { 0 0 0
118 0 size_t chunk_size = (length < B64_CHUNK_SIZE ? length : B64_CHUNK_SIZE);
119 size_t encoded_length;
120 0 rhash_byte_to_base64(buffer, src, chunk_size);
121 0 encoded_length = rhash_urlencode(dst, buffer, BASE64_LENGTH(chunk_size), upper_case);
122 0 result_length += encoded_length;
123 0 dst += encoded_length;
124 0 length -= chunk_size;
125 }
126 0 return result_length;
127 }
128 1 rhash_byte_to_base64(dst, src, length);
129 1 return BASE64_LENGTH(length);
130 }
131  
132 /* RFC 3986: safe url characters are ascii alpha-numeric and "-._~", other characters should be percent-encoded */
133 static unsigned url_safe_char_mask[4] = { 0, 0x03ff6000, 0x87fffffe, 0x47fffffe };
134 #define IS_URL_GOOD_CHAR(c) ((unsigned)(c) < 128 && (url_safe_char_mask[c >> 5] & (1 << (c & 31))))
135  
136 /**
137 * URL-encode specified binary string.
138 *
139 * @param dst (nullable) buffer to output encoded string to,
140 * NULL to just calculate the lengths of encoded string
141 * @param src binary string to encode
142 * @param size size of the binary string
143 * @param upper_case flag to output hex-codes in uppercase
144 * @return the length of the result string
145 */
146 2 size_t rhash_urlencode(char* dst, const char* src, size_t size, int upper_case)
147 {
148 const char* start;
149 size_t i;
150 2 ▶ if (!dst) {
Branch: 1 branch
BranchTrueFalseTotal
if (!dst) { 1 1 2
151 1 size_t length = size;
152 9 ▶ for (i = 0; i < size; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < size; i++) 8 1 9
153 8 ▶ if (!IS_URL_GOOD_CHAR(src[i]))
Branch: 2 branches
BranchTrueFalseTotal
if (!IS_URL_GOOD_CHAR(src[i])) 8 0 8
if (!IS_URL_GOOD_CHAR(src[i])) 0 8 8
154 0 length += 2;
155 1 return length;
156 } else {
157 1 ▶ const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10);
Branch: 1 branch
BranchTrueFalseTotal
const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10); 0 1 1
158 1 start = dst;
159 /* percent-encode all but unreserved URL characters */
160 9 ▶ for (i = 0; i < size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < size; i++) { 8 1 9
161 8 ▶ if (IS_URL_GOOD_CHAR(src[i])) {
Branch: 2 branches
BranchTrueFalseTotal
if (IS_URL_GOOD_CHAR(src[i])) { 8 0 8
if (IS_URL_GOOD_CHAR(src[i])) { 8 0 8
162 8 *dst++ = src[i];
163 } else {
164 0 unsigned char hi = ((unsigned char)(src[i]) >> 4) & 0x0f;
165 0 unsigned char lo = (unsigned char)(src[i]) & 0x0f;
166 0 *dst++ = '%';
167 0 ▶ *dst++ = (hi > 9 ? hi + hex_add : hi + '0');
Branch: 1 branch
BranchTrueFalseTotal
*dst++ = (hi > 9 ? hi + hex_add : hi + '0'); 0 0 0
168 0 ▶ *dst++ = (lo > 9 ? lo + hex_add : lo + '0');
Branch: 1 branch
BranchTrueFalseTotal
*dst++ = (lo > 9 ? lo + hex_add : lo + '0'); 0 0 0
169 }
170 }
171 1 *dst = 0;
172 }
173 1 return dst - start;
174 }
175  
176 /**
177 * Print 64-bit number with trailing '\0' to a string buffer.
178 * if dst is NULL, then just return the length of the number.
179 *
180 * @param dst output buffer
181 * @param number the number to print
182 * @return length of the printed number (without trailing '\0')
183 */
184 9 int rhash_sprintI64(char* dst, uint64_t number)
185 {
186 /* The biggest number has 20 digits: 2^64 = 18 446 744 073 709 551 616 */
187 char buf[24];
188 char* p;
189 size_t length;
190  
191 9 ▶ if (dst == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (dst == NULL) { 0 9 9
192 /* just calculate the length of the number */
193 0 ▶ if (number == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (number == 0) return 1; 0 0 0
194 0 ▶ for (length = 0; number != 0; number /= 10) length++;
Branch: 1 branch
BranchTrueFalseTotal
for (length = 0; number != 0; number /= 10) length++; 0 0 0
195 0 return (int)length;
196 }
197  
198 9 p = buf + 23;
199 9 *p = '\0'; /* last symbol should be '\0' */
200 9 ▶ if (number == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (number == 0) { 0 9 9
201 0 *(--p) = '0';
202 } else {
203 46 ▶ for (; p >= buf && number != 0; number /= 10) {
Branch: 2 branches
BranchTrueFalseTotal
for (; p >= buf && number != 0; number /= 10) { 46 0 46
for (; p >= buf && number != 0; number /= 10) { 37 9 46
204 37 *(--p) = '0' + (char)(number % 10);
205 }
206 }
207 9 length = buf + 23 - p;
208 9 memcpy(dst, p, length + 1);
209 9 return (int)length;
210 }
« _sha512.c ↑ Summary _md4.c »