-
Notifications
You must be signed in to change notification settings - Fork 85
/
json_hex16.h
253 lines (238 loc) · 11.4 KB
/
json_hex16.h
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#pragma once
static const unsigned char hex_u_tbl_16[16] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'
};
static const unsigned char hex_l_tbl_16[16] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'
};
static const unsigned short hex_upper_256_little_endian[256] = {
0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, // 00~07
0x3830, 0x3930, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530, 0x4630, // 08~0F
0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, // 10~17
0x3831, 0x3931, 0x4131, 0x4231, 0x4331, 0x4431, 0x4531, 0x4631, // 18~1F
0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, // 20~27
0x3832, 0x3932, 0x4132, 0x4232, 0x4332, 0x4432, 0x4532, 0x4632, // 28~2F
0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, // 30~37
0x3833, 0x3933, 0x4133, 0x4233, 0x4333, 0x4433, 0x4533, 0x4633, // 38~3F
0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, // 40~47
0x3834, 0x3934, 0x4134, 0x4234, 0x4334, 0x4434, 0x4534, 0x4634, // 48~4F
0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, // 50~57
0x3835, 0x3935, 0x4135, 0x4235, 0x4335, 0x4435, 0x4535, 0x4635, // 58~5F
0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, // 60~67
0x3836, 0x3936, 0x4136, 0x4236, 0x4336, 0x4436, 0x4536, 0x4636, // 68~6F
0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, // 70~77
0x3837, 0x3937, 0x4137, 0x4237, 0x4337, 0x4437, 0x4537, 0x4637, // 78~7F
0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, // 80~87
0x3838, 0x3938, 0x4138, 0x4238, 0x4338, 0x4438, 0x4538, 0x4638, // 88~8F
0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, // 90~97
0x3839, 0x3939, 0x4139, 0x4239, 0x4339, 0x4439, 0x4539, 0x4639, // 98~9F
0x3041, 0x3141, 0x3241, 0x3341, 0x3441, 0x3541, 0x3641, 0x3741, // A0~A7
0x3841, 0x3941, 0x4141, 0x4241, 0x4341, 0x4441, 0x4541, 0x4641, // A8~AF
0x3042, 0x3142, 0x3242, 0x3342, 0x3442, 0x3542, 0x3642, 0x3742, // B0~B7
0x3842, 0x3942, 0x4142, 0x4242, 0x4342, 0x4442, 0x4542, 0x4642, // B8~BF
0x3043, 0x3143, 0x3243, 0x3343, 0x3443, 0x3543, 0x3643, 0x3743, // C0~C7
0x3843, 0x3943, 0x4143, 0x4243, 0x4343, 0x4443, 0x4543, 0x4643, // C8~CF
0x3044, 0x3144, 0x3244, 0x3344, 0x3444, 0x3544, 0x3644, 0x3744, // D0~D7
0x3844, 0x3944, 0x4144, 0x4244, 0x4344, 0x4444, 0x4544, 0x4644, // D8~DF
0x3045, 0x3145, 0x3245, 0x3345, 0x3445, 0x3545, 0x3645, 0x3745, // E0~E7
0x3845, 0x3945, 0x4145, 0x4245, 0x4345, 0x4445, 0x4545, 0x4645, // E8~EF
0x3046, 0x3146, 0x3246, 0x3346, 0x3446, 0x3546, 0x3646, 0x3746, // F0~F7
0x3846, 0x3946, 0x4146, 0x4246, 0x4346, 0x4446, 0x4546, 0x4646 // F8~FF
};
static const unsigned short hex_upper_256_big_endian[256] = {
0x3030, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, 0x3036, 0x3037, // 00~07
0x3038, 0x3039, 0x3041, 0x3042, 0x3043, 0x3044, 0x3045, 0x3046, // 08~0F
0x3130, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, // 10~17
0x3138, 0x3139, 0x3141, 0x3142, 0x3143, 0x3144, 0x3145, 0x3146, // 18~1F
0x3230, 0x3231, 0x3232, 0x3233, 0x3234, 0x3235, 0x3236, 0x3237, // 20~27
0x3238, 0x3239, 0x3241, 0x3242, 0x3243, 0x3244, 0x3245, 0x3246, // 28~2F
0x3330, 0x3331, 0x3332, 0x3333, 0x3334, 0x3335, 0x3336, 0x3337, // 30~37
0x3338, 0x3339, 0x3341, 0x3342, 0x3343, 0x3344, 0x3345, 0x3346, // 38~3F
0x3430, 0x3431, 0x3432, 0x3433, 0x3434, 0x3435, 0x3436, 0x3437, // 40~47
0x3438, 0x3439, 0x3441, 0x3442, 0x3443, 0x3444, 0x3445, 0x3446, // 48~4F
0x3530, 0x3531, 0x3532, 0x3533, 0x3534, 0x3535, 0x3536, 0x3537, // 50~57
0x3538, 0x3539, 0x3541, 0x3542, 0x3543, 0x3544, 0x3545, 0x3546, // 58~5F
0x3630, 0x3631, 0x3632, 0x3633, 0x3634, 0x3635, 0x3636, 0x3637, // 60~67
0x3638, 0x3639, 0x3641, 0x3642, 0x3643, 0x3644, 0x3645, 0x3646, // 68~6F
0x3730, 0x3731, 0x3732, 0x3733, 0x3734, 0x3735, 0x3736, 0x3737, // 70~77
0x3738, 0x3739, 0x3741, 0x3742, 0x3743, 0x3744, 0x3745, 0x3746, // 78~7F
0x3830, 0x3831, 0x3832, 0x3833, 0x3834, 0x3835, 0x3836, 0x3837, // 80~87
0x3838, 0x3839, 0x3841, 0x3842, 0x3843, 0x3844, 0x3845, 0x3846, // 88~8F
0x3930, 0x3931, 0x3932, 0x3933, 0x3934, 0x3935, 0x3936, 0x3937, // 90~97
0x3938, 0x3939, 0x3941, 0x3942, 0x3943, 0x3944, 0x3945, 0x3946, // 98~9F
0x4130, 0x4131, 0x4132, 0x4133, 0x4134, 0x4135, 0x4136, 0x4137, // A0~A7
0x4138, 0x4139, 0x4141, 0x4142, 0x4143, 0x4144, 0x4145, 0x4146, // A8~AF
0x4230, 0x4231, 0x4232, 0x4233, 0x4234, 0x4235, 0x4236, 0x4237, // B0~B7
0x4238, 0x4239, 0x4241, 0x4242, 0x4243, 0x4244, 0x4245, 0x4246, // B8~BF
0x4330, 0x4331, 0x4332, 0x4333, 0x4334, 0x4335, 0x4336, 0x4337, // C0~C7
0x4338, 0x4339, 0x4341, 0x4342, 0x4343, 0x4344, 0x4345, 0x4346, // C8~CF
0x4430, 0x4431, 0x4432, 0x4433, 0x4434, 0x4435, 0x4436, 0x4437, // D0~D7
0x4438, 0x4439, 0x4441, 0x4442, 0x4443, 0x4444, 0x4445, 0x4446, // D8~DF
0x4530, 0x4531, 0x4532, 0x4533, 0x4534, 0x4535, 0x4536, 0x4537, // E0~E7
0x4538, 0x4539, 0x4541, 0x4542, 0x4543, 0x4544, 0x4545, 0x4546, // E8~EF
0x4630, 0x4631, 0x4632, 0x4633, 0x4634, 0x4635, 0x4636, 0x4637, // F0~F7
0x4638, 0x4639, 0x4641, 0x4642, 0x4643, 0x4644, 0x4645, 0x4646 // F8~FF
};
#define F16 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
static const char hex_lookup_256[256] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
F16, // 00~0F
F16, // 10~1F
F16, // 20~2F
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 30~3F
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 40~4F
F16, // 50~5F
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 60~6F
F16, // 70~7F
F16, F16, F16, F16, F16, F16, F16, F16 // 80~FF
};
#undef F16
typedef union {
char c[4];
unsigned long value;
} hex16_endian_t;
static inline
bool hex16_is_little_endian()
{
static hex16_endian_t byte_order = {{ 'l', '?', '?', 'b' }};
return (byte_order.c[0] == 'l');
}
static inline
std::size_t hex16_get_encode_capacity(std::size_t data_length, bool add_quote = false) {
std::size_t capacity;
// One char convent to two Hex strings.
capacity = data_length * 2 + 1;
// Add double quote and nullor format like: "xxxxxx" '\0'
if (add_quote)
capacity += 2;
return capacity;
}
static inline
std::size_t hex16_get_decode_capacity(std::size_t data_length) {
std::size_t capacity;
// One char convent to two Hex strings.
capacity = (data_length + 1) / 2;
return capacity;
}
// encode to upper HEX strings, like "AABBCCF0E5D9".
static std::streamsize hex16_encode(const char * src, std::size_t src_len, char * buffer,
std::size_t buf_size, bool fill_null = true) {
assert(src != nullptr);
assert(buffer != nullptr);
const char * src_end = src + src_len;
char * dest = buffer;
if (((std::size_t)buffer & 0x02U) == 0) {
unsigned short * dest16 = (unsigned short *)buffer;
if (hex16_is_little_endian()) {
while (src < src_end) {
unsigned char c = (unsigned char)(*src);
*dest16++ = hex_upper_256_little_endian[c];
src++;
}
dest = (char * )dest16;
}
else {
while (src < src_end) {
unsigned char c = (unsigned char)(*src);
*dest16++ = hex_upper_256_big_endian[c];
src++;
}
dest = (char * )dest16;
}
}
else {
while (src < src_end) {
unsigned char c = (unsigned char)(*src);
*dest++ = hex_u_tbl_16[ c >> 4U ];
*dest++ = hex_u_tbl_16[ c & 0x0FU ];
src++;
}
}
if (fill_null)
*dest = '\0';
assert(dest >= buffer);
return (dest - buffer);
}
static std::streamsize hex16_encode(const char * src, std::size_t src_len, std::string & dest, bool fill_null = true) {
std::size_t alloc_size = hex16_get_encode_capacity(src_len);
dest.resize(alloc_size);
char * buffer = &dest[0];
std::streamsize encode_size = hex16_encode(src, src_len, buffer, dest.capacity(), fill_null);
if (encode_size >= 0)
dest.resize(encode_size);
else
dest.clear();
return encode_size;
}
static std::string hex16_encode(const char * src, std::size_t src_len, bool fill_null = true) {
std::string dest;
std::size_t alloc_size = hex16_get_encode_capacity(src_len);
dest.resize(alloc_size);
char * buffer = &dest[0];
std::streamsize encode_size = hex16_encode(src, src_len, buffer, dest.capacity(), fill_null);
if (encode_size >= 0)
dest.resize(encode_size);
else
dest.clear();
return dest;
}
static std::streamsize hex16_decode(const char * src, std::size_t src_len, char * buffer, std::size_t buf_size) {
assert(src != nullptr);
assert(buffer != nullptr);
// src_len must be multiply of 2.
if ((src_len & 1UL) != 0) {
// Error: src_len is not multiply of 2.
return -2;
}
src_len -= (src_len & 1U);
const char * src_end = src + src_len;
char * dest = buffer;
while (src < src_end) {
unsigned char hex, hex1, hex2;
unsigned char c1, c2;
// High Hex charactor
c1 = (unsigned char)(*src);
hex1 = hex_lookup_256[c1];
if (hex1 == (unsigned char)(char)(-1)) {
// Error: include non-hex chars.
return -1;
}
// Low Hex charactor
c2 = (unsigned char)(*(src + 1));
hex2 = hex_lookup_256[c2];
if (hex2 == (unsigned char)(char)(-1)) {
// Error: include non-hex chars.
return -1;
}
hex = (hex1 << 4) | hex2;
*dest++ = hex;
src += 2;
}
assert(dest >= buffer);
return (dest - buffer);
}
static std::streamsize hex16_decode(const std::string & src, std::string & dest) {
std::size_t alloc_size = hex16_get_decode_capacity(src.length());
dest.resize(alloc_size);
char * buffer = &dest[0];
std::streamsize decode_size = hex16_decode(src.c_str(), src.length(), buffer, dest.capacity());
if (decode_size >= 0)
dest.resize(decode_size);
else
dest.clear();
return decode_size;
}
static std::string hex16_decode(const std::string & src) {
std::string dest;
std::size_t alloc_size = hex16_get_decode_capacity(src.length());
dest.resize(alloc_size);
char * buffer = &dest[0];
std::streamsize decode_size = hex16_decode(src.c_str(), src.length(), buffer, dest.capacity());
if (decode_size >= 0)
dest.resize(decode_size);
else
dest.clear();
return dest;
}