-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhex.cpp
209 lines (167 loc) · 4.78 KB
/
hex.cpp
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
/*
Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "hex.h"
#include <stdint.h>
// ************************
// *** Helper Functions ***
// ************************
inline int hexDecodeNibble(char src)
{
// 0-9 0x30-0x39
// A-F 0x41-0x46 or a-f 0x61-0x66
int ch = (unsigned char) src;
int ret = -1;
// if (ch > 0x2f && ch < 0x3a) ret += ch - 0x30 + 1; // -47
ret += (((0x2f - ch) & (ch - 0x3a)) >> 8) & (ch - 47);
ch |= 0x20;
// if (ch > 0x60 && ch < 0x67) ret += ch - 0x61 + 10 + 1; // -86
ret += (((0x60 - ch) & (ch - 0x67)) >> 8) & (ch - 86);
return ret;
}
inline int hexDecodeNibbleLower(char src)
{
// 0-9 0x30-0x39
// a-f 0x61-0x66
int ch = (unsigned char) src;
int ret = -1;
// if (ch > 0x2f && ch < 0x3a) ret += ch - 0x30 + 1; // -47
ret += (((0x2f - ch) & (ch - 0x3a)) >> 8) & (ch - 47);
// if (ch > 0x60 && ch < 0x67) ret += ch - 0x61 + 10 + 1; // -86
ret += (((0x60 - ch) & (ch - 0x67)) >> 8) & (ch - 86);
return ret;
}
inline int hexDecodeNibbleUpper(char src)
{
// 0-9 0x30-0x39
// A-F 0x41-0x46
int ch = (unsigned char) src;
int ret = -1;
// if (ch > 0x2f && ch < 0x3a) ret += ch - 0x30 + 1; // -47
ret += (((0x2f - ch) & (ch - 0x3a)) >> 8) & (ch - 47);
// if (ch > 0x40 && ch < 0x47) ret += ch - 0x41 + 10 + 1; // -54
ret += (((0x40 - ch) & (ch - 0x47)) >> 8) & (ch - 54);
return ret;
}
inline int hexDecodeByte(const char src[2])
{
return (hexDecodeNibble(src[0]) << 4) | hexDecodeNibble(src[1]);
}
inline int hexDecodeByteLower(const char src[2])
{
return (hexDecodeNibbleLower(src[0]) << 4) | hexDecodeNibbleLower(src[1]);
}
inline int hexDecodeByteUpper(const char src[2])
{
return (hexDecodeNibbleUpper(src[0]) << 4) | hexDecodeNibbleUpper(src[1]);
}
inline char hexEncodeNibbleLower(unsigned int src)
{
// 0-9 0x30-0x39
// a-f 0x61-0x66
src += 0x30;
// if (in > 0x39) in += 0x61 - 0x3a;
src += ((0x39 - src) >> 8) & (0x61 - 0x3a);
return (char) src;
}
inline char hexEncodeNibbleUpper(unsigned int src)
{
// 0-9 0x30-0x39
// A-F 0x41-0x46
src += 0x30;
// if (in > 0x39) in += 0x41 - 0x3a;
src += ((0x39 - src) >> 8) & (0x41 - 0x3a);
return (char) src;
}
inline void hexEncodeByteLower(char dest[2], uint8_t src)
{
dest[0] = hexEncodeNibbleLower(src >> 4);
dest[1] = hexEncodeNibbleLower(src & 0x0f);
}
inline void hexEncodeByteUpper(char dest[2], uint8_t src)
{
dest[0] = hexEncodeNibbleUpper(src >> 4);
dest[1] = hexEncodeNibbleUpper(src & 0x0f);
}
// **********************
// *** Main Functions ***
// **********************
int hexDecode(void *dest, const char *src, size_t srcLen)
{
int err = 0;
if (srcLen % 2 != 0)
{
return 1;
}
srcLen /= 2;
for (size_t i = 0; i < srcLen; i++)
{
int tmp = hexDecodeByte(src + 2 * i);
err |= tmp >> 8;
((uint8_t*) dest)[i] = (uint8_t) tmp;
}
return err != 0;
}
int hexDecodeLower(void *dest, const char *src, size_t srcLen)
{
int err = 0;
if (srcLen % 2 != 0)
{
return 1;
}
srcLen /= 2;
for (size_t i = 0; i < srcLen; i++)
{
int tmp = hexDecodeByteLower(src + 2 * i);
err |= tmp >> 8;
((uint8_t*) dest)[i] = (uint8_t) tmp;
}
return err != 0;
}
int hexDecodeUpper(void *dest, const char *src, size_t srcLen)
{
int err = 0;
if (srcLen % 2 != 0)
{
return 1;
}
srcLen /= 2;
for (size_t i = 0; i < srcLen; i++)
{
int tmp = hexDecodeByteUpper(src + 2 * i);
err |= tmp >> 8;
((uint8_t*) dest)[i] = (uint8_t) tmp;
}
return err != 0;
}
void hexEncode(char *dest, const void *src, size_t srcLen)
{
for (size_t i = 0; i < srcLen; i++)
{
hexEncodeByteLower(dest + 2 * i, ((const uint8_t*) src)[i]);
}
dest[2 * srcLen] = 0;
}
void hexEncodeUpper(char *dest, const void *src, size_t srcLen)
{
for (size_t i = 0; i < srcLen; i++)
{
hexEncodeByteUpper(dest + 2 * i, ((const uint8_t*) src)[i]);
}
dest[2 * srcLen] = 0;
}