Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmlibrhash/librhash/hex.c
3150 views
1
/* hex.c - conversion for hexadecimal and base32 strings.
2
*
3
* Copyright (c) 2008, Aleksey Kravchenko <[email protected]>
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
void rhash_byte_to_hex(char* dst, const unsigned char* src, size_t length, int upper_case)
30
{
31
const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10);
32
for (; length > 0; src++, length--) {
33
const unsigned char hi = (*src >> 4) & 15;
34
const unsigned char lo = *src & 15;
35
*dst++ = (hi > 9 ? hi + hex_add : hi + '0');
36
*dst++ = (lo > 9 ? lo + hex_add : lo + '0');
37
}
38
*dst = '\0';
39
}
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
void rhash_byte_to_base32(char* dst, const unsigned char* src, size_t length, int upper_case)
50
{
51
const char a = (upper_case ? 'A' : 'a');
52
unsigned shift = 0;
53
unsigned char word;
54
const unsigned char* e = src + length;
55
while (src < e) {
56
if (shift > 3) {
57
word = (*src & (0xFF >> shift));
58
shift = (shift + 5) % 8;
59
word <<= shift;
60
if (src + 1 < e)
61
word |= *(src + 1) >> (8 - shift);
62
++src;
63
} else {
64
shift = (shift + 5) % 8;
65
word = ( *src >> ( (8 - shift) & 7 ) ) & 0x1F;
66
if (shift == 0) src++;
67
}
68
*dst++ = ( word < 26 ? word + a : word + '2' - 26 );
69
}
70
*dst = '\0';
71
}
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
void rhash_byte_to_base64(char* dst, const unsigned char* src, size_t length)
82
{
83
static const char* tail = "0123456789+/";
84
unsigned shift = 0;
85
unsigned char word;
86
const unsigned char* e = src + length;
87
while (src < e) {
88
if (shift > 2) {
89
word = (*src & (0xFF >> shift));
90
shift = (shift + 6) % 8;
91
word <<= shift;
92
if (src + 1 < e)
93
word |= *(src + 1) >> (8 - shift);
94
++src;
95
} else {
96
shift = (shift + 6) % 8;
97
word = ( *src >> ( (8 - shift) & 7 ) ) & 0x3F;
98
if (shift == 0) src++;
99
}
100
*dst++ = ( word < 52 ? (word < 26 ? word + 'A' : word - 26 + 'a') : tail[word - 52]);
101
}
102
if (shift > 0) {
103
*dst++ = '=';
104
if (shift == 4) *dst++ = '=';
105
}
106
*dst = '\0';
107
}
108
109
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
#ifdef __clang_analyzer__
114
memset(buffer, 0, sizeof(buffer));
115
#endif
116
RHASH_ASSERT((BASE64_LENGTH(B64_CHUNK_SIZE) + 4) <= sizeof(buffer));
117
RHASH_ASSERT((B64_CHUNK_SIZE % 6) == 0);
118
if (url_encode) {
119
size_t result_length = 0;
120
for (; length > 0; src += B64_CHUNK_SIZE) {
121
size_t chunk_size = (length < B64_CHUNK_SIZE ? length : B64_CHUNK_SIZE);
122
size_t encoded_length;
123
rhash_byte_to_base64(buffer, src, chunk_size);
124
encoded_length = rhash_urlencode(dst, buffer, BASE64_LENGTH(chunk_size), upper_case);
125
result_length += encoded_length;
126
dst += encoded_length;
127
length -= chunk_size;
128
}
129
return result_length;
130
}
131
rhash_byte_to_base64(dst, src, length);
132
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
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
if (!dst) {
154
size_t length = size;
155
for (i = 0; i < size; i++)
156
if (!IS_URL_GOOD_CHAR(src[i]))
157
length += 2;
158
return length;
159
} else {
160
const char hex_add = (upper_case ? 'A' - 10 : 'a' - 10);
161
start = dst;
162
/* percent-encode all but unreserved URL characters */
163
for (i = 0; i < size; i++) {
164
if (IS_URL_GOOD_CHAR(src[i])) {
165
*dst++ = src[i];
166
} else {
167
unsigned char hi = ((unsigned char)(src[i]) >> 4) & 0x0f;
168
unsigned char lo = (unsigned char)(src[i]) & 0x0f;
169
*dst++ = '%';
170
*dst++ = (hi > 9 ? hi + hex_add : hi + '0');
171
*dst++ = (lo > 9 ? lo + hex_add : lo + '0');
172
}
173
}
174
*dst = 0;
175
}
176
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
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
if (dst == NULL) {
195
/* just calculate the length of the number */
196
if (number == 0) return 1;
197
for (length = 0; number != 0; number /= 10) length++;
198
return (int)length;
199
}
200
201
p = buf + 23;
202
*p = '\0'; /* last symbol should be '\0' */
203
if (number == 0) {
204
*(--p) = '0';
205
} else {
206
for (; p >= buf && number != 0; number /= 10) {
207
*(--p) = '0' + (char)(number % 10);
208
}
209
}
210
length = buf + 23 - p;
211
memcpy(dst, p, length + 1);
212
return (int)length;
213
}
214
215