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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#include "pack.h"
////////////////////////////////////////////////////////////////////////////////
//
// Pack
//
int Pack(ui8* buf, ui32 value) {
ui8* p = buf;
for (;;) {
ui8 v = (ui8)value & 0x7f;
value >>= 7;
if (!value) {
*p++ = v;
break;
}
*p++ = (v | 0x80);
}
return (int)(p - buf);
}
ui32 Unpack(const ui8*& data) {
ui32 value = 0;
const ui8* p = data;
for (int off = 0;; ++p, off += 7) {
value |= (*p & 0x7f) << off;
if ((*p & 0x80) == 0) {
++p;
break;
}
}
data = p;
return value;
}
////////////////////////////////////////////////////////////////////////////////
//
// PackU32
//
int PackU32(ui32 value, void* buf) {
ui8* p = (ui8*)buf; // 654 3210 <-- bit of 'value' (7 bits total)
if (value < (1u << 7)) { // [0--- ----]
p[0] = (ui8)value; // ^ ^
return 1; // MSB LSB
}
value -= (1u << 7); // DC BA98 7654 3210 <-- bit of 'value' (14 == 0xD bits total)
if (value < (1u << 14)) { // [10-- ----] [---- ----]
p[1] = (ui8)value; value >>= 8;
p[0] = 0x80 | (ui8)value;
return 2;
}
value -= (1u << 14); // And so on...
if (value < (1u << 21)) { // [110- ----] [---- ----] [---- ----]
p[2] = (ui8)value; value >>= 8;
p[1] = (ui8)value; value >>= 8;
p[0] = 0xc0 | (ui8)value;
return 3;
}
value -= (1u << 21);
if (value < (1u << 28)) { // [1110 ----] [---- ----] [---- ----] [---- ----]
p[3] = (ui8)value; value >>= 8;
p[2] = (ui8)value; value >>= 8;
p[1] = (ui8)value; value >>= 8;
p[0] = 0xe0 | (ui8)value;
return 4;
} // ...but for largest numbers, whole first byte is reserved.
value -= (1u << 28); // [1111 0000] [---- ----] [---- ----] [---- ----] [---- ----]
p[4] = (ui8)value; value >>= 8;
p[3] = (ui8)value; value >>= 8;
p[2] = (ui8)value; value >>= 8;
p[1] = (ui8)value;
p[0] = 0xf0;
return 5;
}
int UnpackU32(ui32* value, const void* buf) {
const ui8* p = (const ui8*)buf;
ui8 b = p[0];
if (!(b & 0x80)) {
*value = b;
return 1;
}
if (!(b & 0x40)) {
*value = 0x80 + (((b & 0x3f) << 8) | p[1]);
return 2;
}
if (!(b & 0x20)) {
*value = 0x4080 + (((b & 0x1f) << 16) | (p[1] << 8) | p[2]);
return 3;
}
if (!(b & 0x10)) {
*value = 0x204080 + (((b & 0x0f) << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
return 4;
}
*value = 0x10204080 + ((p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4]);
return 5;
}
int SkipU32(const void* buf) {
static const i8 skip[16] = { 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5 };
int i = ((const ui8*)buf)[0] >> 4;
return skip[i];
}
////////////////////////////////////////////////////////////////////////////////
//
// PackU64
//
int PackU64(ui64 value, void* buf) { // Packing scheme is analogous to PackU32()
ui8* p = (ui8*)buf;
if (value < (1u << 7)) {
p[0] = (ui8)value;
return 1;
}
value -= (1u << 7);
if (value < (1u << 14)) {
p[1] = (ui8)value; value >>= 8;
p[0] = 0x80 | (ui8)value;
return 2;
}
value -= (1u << 14);
if (value < (1u << 21)) {
p[2] = (ui8)value; value >>= 8;
p[1] = (ui8)value; value >>= 8;
p[0] = 0xc0 | (ui8)value;
return 3;
}
value -= (1u << 21);
if (value < (1u << 28)) {
p[3] = (ui8)value; value >>= 8;
p[2] = (ui8)value; value >>= 8;
p[1] = (ui8)value; value >>= 8;
p[0] = 0xe0 | (ui8)value;
return 4;
}
value -= (1u << 28);
if (value < (ui64(1) << 35)) {
p[4] = (ui8)value; value >>= 8;
p[3] = (ui8)value; value >>= 8;
p[2] = (ui8)value; value >>= 8;
p[1] = (ui8)value; value >>= 8;
p[0] = 0xf0 | (ui8)value;
return 5;
}
value -= ui64(1) << 35;
if (value < (ui64(1) << 42)) {
p[5] = (ui8)value; value >>= 8;
p[4] = (ui8)value; value >>= 8;
p[3] = (ui8)value; value >>= 8;
p[2] = (ui8)value; value >>= 8;
p[1] = (ui8)value; value >>= 8;
p[0] = 0xf8 | (ui8)value;
return 6;
}
value -= ui64(1) << 42;
if (value < (ui64(1) << 49)) { // [1111 110-] [---- ----] ... (+ 5 bytes)
p[6] = (ui8)value; value >>= 8;
p[5] = (ui8)value; value >>= 8;
p[4] = (ui8)value; value >>= 8;
p[3] = (ui8)value; value >>= 8;
p[2] = (ui8)value; value >>= 8;
p[1] = (ui8)value; value >>= 8;
p[0] = 0xfc | (ui8)value;
return 7;
}
value -= ui64(1) << 49;
if (value < (ui64(1) << 56)) { // [1111 1110] [---- ----] ... (+ 6 bytes)
p[7] = (ui8)value; value >>= 8;
p[6] = (ui8)value; value >>= 8;
p[5] = (ui8)value; value >>= 8;
p[4] = (ui8)value; value >>= 8;
p[3] = (ui8)value; value >>= 8;
p[2] = (ui8)value; value >>= 8;
p[1] = (ui8)value; value >>= 8;
p[0] = 0xfe | (ui8)value;
return 8;
}
value -= ui64(1) << 56; // [1111 1111] [---- ----] ... (+ 7 bytes)
p[8] = (ui8)value; value >>= 8; // ^
p[7] = (ui8)value; value >>= 8; // not a zero; contains actual bit
p[6] = (ui8)value; value >>= 8;
p[5] = (ui8)value; value >>= 8;
p[4] = (ui8)value; value >>= 8;
p[3] = (ui8)value; value >>= 8;
p[2] = (ui8)value; value >>= 8;
p[1] = (ui8)value; value >>= 8;
p[0] = 0xff;
return 9;
}
int UnpackU64(ui64* value, const void* buf) {
const ui8* p = (const ui8*)buf;
ui8 b = p[0];
if (!(b & 0x80)) {
*value = b;
return 1;
}
if (!(b & 0x40)) {
*value = 0x80 + (((b & 0x3f) << 8) | p[1]);
return 2;
}
if (!(b & 0x20)) {
*value = 0x4080 + (((b & 0x1f) << 16) | (p[1] << 8) | p[2]);
return 3;
}
if (!(b & 0x10)) {
*value = 0x204080 + (((b & 0x0f) << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
return 4;
}
if (!(b & 0x08)) {
*value = 0x10204080 + (
(ui64(b & 0x07) << 32) |
(ui32(p[1]) << 24) |
( p[2] << 16) |
( p[3] << 8) |
p[4]
);
return 5;
}
if (!(b & 0x04)) {
*value = 0x0810204080ull + (
(ui64(b & 0x03) << 40) |
(ui64(p[1]) << 32) |
(ui32(p[2]) << 24) |
( p[3] << 16) |
( p[4] << 8) |
p[5]
);
return 6;
}
if (!(b & 0x02)) {
*value = 0x040810204080ull + (
(ui64(b & 0x01) << 48) |
(ui64(p[1]) << 40) |
(ui64(p[2]) << 32) |
(ui32(p[3]) << 24) |
( p[4] << 16) |
( p[5] << 8) |
p[6]
);
return 7;
}
if (!(b & 0x01)) {
*value = 0x02040810204080ull + (
(ui64(p[1]) << 48) |
(ui64(p[2]) << 40) |
(ui64(p[3]) << 32) |
(ui32(p[4]) << 24) |
( p[5] << 16) |
( p[6] << 8) |
p[7]
);
return 8;
}
*value = 0x0102040810204080ull + (
(ui64(p[1]) << 56) |
(ui64(p[2]) << 48) |
(ui64(p[3]) << 40) |
(ui64(p[4]) << 32) |
(ui32(p[5]) << 24) |
( p[6] << 16) |
( p[7] << 8) |
p[8]
);
return 9;
}
#define REPEAT_16(x) x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x
#define REPEAT_32(x) REPEAT_16(x), REPEAT_16(x)
#define REPEAT_64(x) REPEAT_32(x), REPEAT_32(x)
#define REPEAT_128(x) REPEAT_64(x), REPEAT_64(x)
int SkipU64(const void* buf) {
static const i8 skip[256] = {
REPEAT_128(1), REPEAT_64(2), REPEAT_32(3), REPEAT_16(4),
5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 8, 9
};
return skip[*(const ui8*)buf];
}
#undef REPEAT_16
#undef REPEAT_32
#undef REPEAT_64
#undef REPEAT_128
|