File Coverage

_hex.c
Criterion Covered Total %
statement 74 93 79.5
branch 49 72 68.0
condition n/a
subroutine n/a
pod n/a
total 123 165 74.5


line stmt bran cond sub pod time code
1             /* hex.c - conversion for hexadecimal and base32 strings.
2             *
3             * Copyright (c) 2008, Aleksey Kravchenko
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
18             #include
19             #include
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 50         const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10);
32 1223 100         for (; length > 0; src++, length--) {
33 1180           const unsigned char hi = (*src >> 4) & 15;
34 1180           const unsigned char lo = *src & 15;
35 1180 100         *dst++ = (hi > 9 ? hi + hex_add : hi + '0');
36 1180 100         *dst++ = (lo > 9 ? lo + hex_add : lo + '0');
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 50         const char a = (upper_case ? 'A' : 'a');
52 8           unsigned shift = 0;
53             unsigned char word;
54 8           const unsigned char* e = src + length;
55 260 100         while (src < e) {
56 252 100         if (shift > 3) {
57 128           word = (*src & (0xFF >> shift));
58 128           shift = (shift + 5) % 8;
59 128           word <<= shift;
60 128 100         if (src + 1 < e)
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 100         if (shift == 0) src++;
67             }
68 252 100         *dst++ = ( word < 26 ? word + a : word + '2' - 26 );
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 100         while (src < e) {
88 11 100         if (shift > 2) {
89 6           word = (*src & (0xFF >> shift));
90 6           shift = (shift + 6) % 8;
91 6           word <<= shift;
92 6 100         if (src + 1 < e)
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 100         if (shift == 0) src++;
99             }
100 11 100         *dst++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]);
    100          
101             }
102 1 50         if (shift > 0) {
103 1           *dst++ = '=';
104 1 50         if (shift == 4) *dst++ = '=';
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 50         if (url_encode) {
116 0           size_t result_length = 0;
117 0 0         for (; length > 0; src += B64_CHUNK_SIZE) {
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 100         if (!dst) {
151 1           size_t length = size;
152 9 100         for (i = 0; i < size; i++)
153 8 50         if (!IS_URL_GOOD_CHAR(src[i]))
    50          
154 0           length += 2;
155 1           return length;
156             } else {
157 1 50         const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10);
158 1           start = dst;
159             /* percent-encode all but unreserved URL characters */
160 9 100         for (i = 0; i < size; i++) {
161 8 50         if (IS_URL_GOOD_CHAR(src[i])) {
    50          
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 0         *dst++ = (hi > 9 ? hi + hex_add : hi + '0');
168 0 0         *dst++ = (lo > 9 ? lo + hex_add : lo + '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 50         if (dst == NULL) {
192             /* just calculate the length of the number */
193 0 0         if (number == 0) return 1;
194 0 0         for (length = 0; number != 0; number /= 10) length++;
195 0           return (int)length;
196             }
197              
198 9           p = buf + 23;
199 9           *p = '\0'; /* last symbol should be '\0' */
200 9 50         if (number == 0) {
201 0           *(--p) = '0';
202             } else {
203 46 50         for (; p >= buf && number != 0; number /= 10) {
    100          
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             }