_hex.c

filter: Statementstmt 92.0% 69 / 75 Branchbran 79.0% 49 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.1% 118 / 137 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« _sha3.c ↑ Summary _gost94.c »
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 /**
21 * Convert a byte to a hexadecimal number. The result, consisting of two
22 * hexadecimal digits is stored into a buffer.
23 *
24 * @param dest the buffer to receive two symbols of hex representation
25 * @param byte the byte to decode
26 * @param upper_case flag to print string in uppercase
27 * @return pointer to the chararcter just after the written number (dest + 2)
28 */
29 1084 char* rhash_print_hex_byte(char *dest, const unsigned char byte, int upper_case)
30 {
31 1084 ▶ const char add = (upper_case ? 'A' - 10 : 'a' - 10);
Branch: 1 branch
BranchTrueFalseTotal
const char add = (upper_case ? 'A' - 10 : 'a' - 10); 0 1084 1084
32 1084 unsigned char c = (byte >> 4) & 15;
33 1084 ▶ *dest++ = (c > 9 ? c + add : c + '0');
Branch: 1 branch
BranchTrueFalseTotal
*dest++ = (c > 9 ? c + add : c + '0'); 411 673 1084
34 1084 c = byte & 15;
35 1084 ▶ *dest++ = (c > 9 ? c + add : c + '0');
Branch: 1 branch
BranchTrueFalseTotal
*dest++ = (c > 9 ? c + add : c + '0'); 402 682 1084
36 1084 return dest;
37 }
38  
39 /**
40 * Store hexadecimal representation of a binary string to given buffer.
41 *
42 * @param dest the buffer to receive hexadecimal representation
43 * @param src binary string
44 * @param len string length
45 * @param upper_case flag to print string in uppercase
46 */
47 41 void rhash_byte_to_hex(char *dest, const unsigned char *src, unsigned len, int upper_case)
48 {
49 1125 ▶ while (len-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (len-- > 0) { 1084 41 1125
50 1084 dest = rhash_print_hex_byte(dest, *src++, upper_case);
51 }
52 41 *dest = '\0';
53 41 }
54  
55 /**
56 * Encode a binary string to base32.
57 *
58 * @param dest the buffer to store result
59 * @param src binary string
60 * @param len string length
61 * @param upper_case flag to print string in uppercase
62 */
63 8 void rhash_byte_to_base32(char* dest, const unsigned char* src, unsigned len, int upper_case)
64 {
65 8 ▶ const char a = (upper_case ? 'A' : 'a');
Branch: 1 branch
BranchTrueFalseTotal
const char a = (upper_case ? 'A' : 'a'); 0 8 8
66 8 unsigned shift = 0;
67 unsigned char word;
68 8 const unsigned char* e = src + len;
69 260 ▶ while (src < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (src < e) { 252 8 260
70 252 ▶ if (shift > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (shift > 3) { 128 124 252
71 128 word = (*src & (0xFF >> shift));
72 128 shift = (shift + 5) % 8;
73 128 word <<= shift;
74 128 ▶ if (src + 1 < e)
Branch: 1 branch
BranchTrueFalseTotal
if (src + 1 < e) 123 5 128
75 123 word |= *(src + 1) >> (8 - shift);
76 128 ++src;
77 } else {
78 124 shift = (shift + 5) % 8;
79 124 word = ( *src >> ( (8 - shift) & 7 ) ) & 0x1F;
80 124 ▶ if (shift == 0) src++;
Branch: 1 branch
BranchTrueFalseTotal
if (shift == 0) src++; 28 96 124
81 }
82 252 ▶ *dest++ = ( word < 26 ? word + a : word + '2' - 26 );
Branch: 1 branch
BranchTrueFalseTotal
*dest++ = ( word < 26 ? word + a : word + '2' - 26 ); 181 71 252
83 }
84 8 *dest = '\0';
85 8 }
86  
87 /**
88 * Encode a binary string to base64.
89 * Encoded output length is always a multiple of 4 bytes.
90 *
91 * @param dest the buffer to store result
92 * @param src binary string
93 * @param len string length
94 */
95 1 void rhash_byte_to_base64(char* dest, const unsigned char* src, unsigned len)
96 {
97 static const char* tail = "0123456789+/";
98 1 unsigned shift = 0;
99 unsigned char word;
100 1 const unsigned char* e = src + len;
101 12 ▶ while (src < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (src < e) { 11 1 12
102 11 ▶ if (shift > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (shift > 2) { 6 5 11
103 6 word = (*src & (0xFF >> shift));
104 6 shift = (shift + 6) % 8;
105 6 word <<= shift;
106 6 ▶ if (src + 1 < e)
Branch: 1 branch
BranchTrueFalseTotal
if (src + 1 < e) 5 1 6
107 5 word |= *(src + 1) >> (8 - shift);
108 6 ++src;
109 } else {
110 5 shift = (shift + 6) % 8;
111 5 word = ( *src >> ( (8 - shift) & 7 ) ) & 0x3F;
112 5 ▶ if (shift == 0) src++;
Branch: 1 branch
BranchTrueFalseTotal
if (shift == 0) src++; 2 3 5
113 }
114 11 ▶ *dest++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]);
Branch: 2 branches
BranchTrueFalseTotal
*dest++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]); 10 1 11
*dest++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]); 4 6 10
115 }
116 1 ▶ if (shift > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (shift > 0) { 1 0 1
117 1 *dest++ = '=';
118 1 ▶ if (shift == 4) *dest++ = '=';
Branch: 1 branch
BranchTrueFalseTotal
if (shift == 4) *dest++ = '='; 0 1 1
119 }
120 1 *dest = '\0';
121 1 }
122  
123 /* unsafe characters are "<>{}[]%#/|\^~`@:;?=&+ */
124 #define IS_GOOD_URL_CHAR(c) (isalnum((unsigned char)c) || strchr("$-_.!'(),", c))
125  
126 /**
127 * URL-encode a string.
128 *
129 * @param dst buffer to receive result or NULL to calculate
130 * the lengths of encoded string
131 * @param filename the file name
132 * @return the length of the result string
133 */
134 2 int rhash_urlencode(char *dst, const char *name)
135 {
136 const char *start;
137 2 ▶ if (!dst) {
Branch: 1 branch
BranchTrueFalseTotal
if (!dst) { 1 1 2
138 int len;
139 9 ▶ for (len = 0; *name; name++) len += (IS_GOOD_URL_CHAR(*name) ? 1 : 3);
Branch: 3 branches
BranchTrueFalseTotal
for (len = 0; *name; name++) len += (IS_GOOD_URL_CHAR(*name) ? 1 : 3); 1 7 8
for (len = 0; *name; name++) len += (IS_GOOD_URL_CHAR(*name) ? 1 : 3); 1 0 1
for (len = 0; *name; name++) len += (IS_GOOD_URL_CHAR(*name) ? 1 : 3); 8 1 9
140 1 return len;
141 }
142 /* encode URL as specified by RFC 1738 */
143 9 ▶ for (start = dst; *name; name++) {
Branch: 1 branch
BranchTrueFalseTotal
for (start = dst; *name; name++) { 8 1 9
144 8 ▶ if ( IS_GOOD_URL_CHAR(*name) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( IS_GOOD_URL_CHAR(*name) ) { 1 7 8
if ( IS_GOOD_URL_CHAR(*name) ) { 1 0 1
145 8 *dst++ = *name;
146 } else {
147 0 *dst++ = '%';
148 0 dst = rhash_print_hex_byte(dst, *name, 'A');
149 }
150 }
151 1 *dst = 0;
152 1 return (int)(dst - start);
153 }
154  
155 /**
156 * Print 64-bit number with trailing '\0' to a string buffer.
157 * if dst is NULL, then just return the length of the number.
158 *
159 * @param dst output buffer
160 * @param number the number to print
161 * @return length of the printed number (without trailing '\0')
162 */
163 9 int rhash_sprintI64(char *dst, uint64_t number)
164 {
165 /* The biggest number has 20 digits: 2^64 = 18 446 744 073 709 551 616 */
166 char buf[24], *p;
167 size_t length;
168  
169 9 ▶ if (dst == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (dst == NULL) { 0 9 9
170 /* just calculate the length of the number */
171 0 ▶ if (number == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (number == 0) return 1; 0 0 0
172 0 ▶ for (length = 0; number != 0; number /= 10) length++;
Branch: 1 branch
BranchTrueFalseTotal
for (length = 0; number != 0; number /= 10) length++; 0 0 0
173 0 return (int)length;
174 }
175  
176 9 p = buf + 23;
177 9 *p = '\0'; /* last symbol should be '\0' */
178 9 ▶ if (number == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (number == 0) { 0 9 9
179 0 *(--p) = '0';
180 } else {
181 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
182 37 *(--p) = '0' + (char)(number % 10);
183 }
184 }
185 9 length = buf + 23 - p;
186 9 memcpy(dst, p, length + 1);
187 9 return (int)length;
188 }
« _sha3.c ↑ Summary _gost94.c »