summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/support/hex.c
blob: e48f0479225a48753e54d97ca11e3c56f656add3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*-
 * Copyright (c) 2014-2019 MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * __fill_hex --
 *	In-memory conversion of raw bytes to a hexadecimal representation.
 */
static inline void
__fill_hex(const uint8_t *src, size_t src_max,
    uint8_t *dest, size_t dest_max, size_t *lenp)
{
	uint8_t *dest_orig;

	dest_orig = dest;
	if (dest_max > 0)		/* save a byte for nul-termination */
		--dest_max;
	for (; src_max > 0 && dest_max > 1;
	    src_max -= 1, dest_max -= 2, ++src) {
		*dest++ = __wt_hex((*src & 0xf0) >> 4);
		*dest++ = __wt_hex(*src & 0x0f);
	}
	*dest++ = '\0';
	if (lenp != NULL)
		*lenp = WT_PTRDIFF(dest, dest_orig);
}

/*
 * __wt_fill_hex --
 *	In-memory conversion of raw bytes to a hexadecimal representation.
 */
void
__wt_fill_hex(const uint8_t *src, size_t src_max,
    uint8_t *dest, size_t dest_max, size_t *lenp)
{
	__fill_hex(src, src_max, dest, dest_max, lenp);
}

/*
 * __wt_raw_to_hex --
 *	Convert a chunk of data to a nul-terminated printable hex string.
 */
int
__wt_raw_to_hex(
    WT_SESSION_IMPL *session, const uint8_t *from, size_t size, WT_ITEM *to)
{
	size_t len;

	/*
	 * Every byte takes up 2 spaces, plus a trailing nul byte.
	 */
	len = size * 2 + 1;
	WT_RET(__wt_buf_init(session, to, len));

	__fill_hex(from, size, to->mem, len, &to->size);
	return (0);
}

/*
 * __wt_raw_to_esc_hex --
 *	Convert a chunk of data to a nul-terminated printable string using
 * escaped hex, as necessary.
 */
int
__wt_raw_to_esc_hex(
    WT_SESSION_IMPL *session, const uint8_t *from, size_t size, WT_ITEM *to)
{
	size_t i;
	const uint8_t *p;
	u_char *t;

	/*
	 * In the worst case, every character takes up 3 spaces, plus a
	 * trailing nul byte.
	 */
	WT_RET(__wt_buf_init(session, to, size * 3 + 1));

	for (p = from, t = to->mem, i = size; i > 0; --i, ++p)
		if (__wt_isprint((u_char)*p)) {
			if (*p == '\\')
				*t++ = '\\';
			*t++ = *p;
		} else {
			*t++ = '\\';
			*t++ = __wt_hex((*p & 0xf0) >> 4);
			*t++ = __wt_hex(*p & 0x0f);
		}
	*t++ = '\0';
	to->size = WT_PTRDIFF(t, to->mem);
	return (0);
}

/*
 * __wt_hex2byte --
 *	Convert a pair of hex characters into a byte.
 */
int
__wt_hex2byte(const u_char *from, u_char *to)
{
	uint8_t byte;

	switch (from[0]) {
	case '0': byte = 0; break;
	case '1': byte = 1 << 4; break;
	case '2': byte = 2 << 4; break;
	case '3': byte = 3 << 4; break;
	case '4': byte = 4 << 4; break;
	case '5': byte = 5 << 4; break;
	case '6': byte = 6 << 4; break;
	case '7': byte = 7 << 4; break;
	case '8': byte = 8 << 4; break;
	case '9': byte = 9 << 4; break;
	case 'A': byte = 10 << 4; break;
	case 'B': byte = 11 << 4; break;
	case 'C': byte = 12 << 4; break;
	case 'D': byte = 13 << 4; break;
	case 'E': byte = 14 << 4; break;
	case 'F': byte = 15 << 4; break;
	case 'a': byte = 10 << 4; break;
	case 'b': byte = 11 << 4; break;
	case 'c': byte = 12 << 4; break;
	case 'd': byte = 13 << 4; break;
	case 'e': byte = 14 << 4; break;
	case 'f': byte = 15 << 4; break;
	default:
		return (1);
	}

	switch (from[1]) {
	case '0': break;
	case '1': byte |= 1; break;
	case '2': byte |= 2; break;
	case '3': byte |= 3; break;
	case '4': byte |= 4; break;
	case '5': byte |= 5; break;
	case '6': byte |= 6; break;
	case '7': byte |= 7; break;
	case '8': byte |= 8; break;
	case '9': byte |= 9; break;
	case 'A': byte |= 10; break;
	case 'B': byte |= 11; break;
	case 'C': byte |= 12; break;
	case 'D': byte |= 13; break;
	case 'E': byte |= 14; break;
	case 'F': byte |= 15; break;
	case 'a': byte |= 10; break;
	case 'b': byte |= 11; break;
	case 'c': byte |= 12; break;
	case 'd': byte |= 13; break;
	case 'e': byte |= 14; break;
	case 'f': byte |= 15; break;
	default:
		return (1);
	}
	*to = byte;
	return (0);
}

/*
 * __hex_fmterr --
 *	Hex format error message.
 */
static int
__hex_fmterr(WT_SESSION_IMPL *session)
{
	WT_RET_MSG(session, EINVAL, "Invalid format in hexadecimal string");
}

/*
 * __wt_hex_to_raw --
 *	Convert a nul-terminated printable hex string to a chunk of data.
 */
int
__wt_hex_to_raw(WT_SESSION_IMPL *session, const char *from, WT_ITEM *to)
{
	return (__wt_nhex_to_raw(session, from, strlen(from), to));
}

/*
 * __wt_nhex_to_raw --
 *	Convert a printable hex string to a chunk of data.
 */
int
__wt_nhex_to_raw(
    WT_SESSION_IMPL *session, const char *from, size_t size, WT_ITEM *to)
{
	const u_char *p;
	u_char *t;

	if (size % 2 != 0)
		return (__hex_fmterr(session));

	WT_RET(__wt_buf_init(session, to, size / 2));

	for (p = (u_char *)from, t = to->mem; size > 0; p += 2, size -= 2, ++t)
		if (__wt_hex2byte(p, t))
			return (__hex_fmterr(session));

	to->size = WT_PTRDIFF(t, to->mem);
	return (0);
}

/*
 * __wt_esc_hex_to_raw --
 *	Convert a printable string, encoded in escaped hex, to a chunk of data.
 */
int
__wt_esc_hex_to_raw(WT_SESSION_IMPL *session, const char *from, WT_ITEM *to)
{
	const u_char *p;
	u_char *t;

	WT_RET(__wt_buf_init(session, to, strlen(from)));

	for (p = (u_char *)from, t = to->mem; *p != '\0'; ++p, ++t) {
		if ((*t = *p) != '\\')
			continue;
		++p;
		if (p[0] != '\\') {
			if (p[0] == '\0' || p[1] == '\0' || __wt_hex2byte(p, t))
				return (__hex_fmterr(session));
			++p;
		}
	}
	to->size = WT_PTRDIFF(t, to->mem);
	return (0);
}