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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/*
* Copyright 2017 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "src/leb128.h"
#include <type_traits>
#include "src/stream.h"
#define MAX_U32_LEB128_BYTES 5
#define MAX_U64_LEB128_BYTES 10
namespace wabt {
Offset U32Leb128Length(uint32_t value) {
uint32_t size = 0;
do {
value >>= 7;
size++;
} while (value != 0);
return size;
}
#define LEB128_LOOP_UNTIL(end_cond) \
do { \
uint8_t byte = value & 0x7f; \
value >>= 7; \
if (end_cond) { \
data[length++] = byte; \
break; \
} else { \
data[length++] = byte | 0x80; \
} \
} while (1)
Offset WriteFixedU32Leb128At(Stream* stream,
Offset offset,
uint32_t value,
const char* desc) {
uint8_t data[MAX_U32_LEB128_BYTES];
Offset length =
WriteFixedU32Leb128Raw(data, data + MAX_U32_LEB128_BYTES, value);
stream->WriteDataAt(offset, data, length, desc);
return length;
}
void WriteU32Leb128(Stream* stream, uint32_t value, const char* desc) {
uint8_t data[MAX_U32_LEB128_BYTES];
Offset length = 0;
LEB128_LOOP_UNTIL(value == 0);
stream->WriteData(data, length, desc);
}
void WriteFixedU32Leb128(Stream* stream, uint32_t value, const char* desc) {
uint8_t data[MAX_U32_LEB128_BYTES];
Offset length =
WriteFixedU32Leb128Raw(data, data + MAX_U32_LEB128_BYTES, value);
stream->WriteData(data, length, desc);
}
// returns the length of the leb128.
Offset WriteU32Leb128At(Stream* stream,
Offset offset,
uint32_t value,
const char* desc) {
uint8_t data[MAX_U32_LEB128_BYTES];
Offset length = 0;
LEB128_LOOP_UNTIL(value == 0);
stream->WriteDataAt(offset, data, length, desc);
return length;
}
Offset WriteU32Leb128Raw(uint8_t* dest, uint8_t* dest_end, uint32_t value) {
uint8_t data[MAX_U32_LEB128_BYTES];
Offset length = 0;
LEB128_LOOP_UNTIL(value == 0);
if (static_cast<Offset>(dest_end - dest) < length) {
return 0;
}
memcpy(dest, data, length);
return length;
}
Offset WriteFixedU32Leb128Raw(uint8_t* data, uint8_t* end, uint32_t value) {
if (end - data < MAX_U32_LEB128_BYTES) {
return 0;
}
data[0] = (value & 0x7f) | 0x80;
data[1] = ((value >> 7) & 0x7f) | 0x80;
data[2] = ((value >> 14) & 0x7f) | 0x80;
data[3] = ((value >> 21) & 0x7f) | 0x80;
data[4] = ((value >> 28) & 0x0f);
return MAX_U32_LEB128_BYTES;
}
static void WriteS32Leb128(Stream* stream, int32_t value, const char* desc) {
uint8_t data[MAX_U32_LEB128_BYTES];
Offset length = 0;
if (value < 0) {
LEB128_LOOP_UNTIL(value == -1 && (byte & 0x40));
} else {
LEB128_LOOP_UNTIL(value == 0 && !(byte & 0x40));
}
stream->WriteData(data, length, desc);
}
static void WriteS64Leb128(Stream* stream, int64_t value, const char* desc) {
uint8_t data[MAX_U64_LEB128_BYTES];
Offset length = 0;
if (value < 0) {
LEB128_LOOP_UNTIL(value == -1 && (byte & 0x40));
} else {
LEB128_LOOP_UNTIL(value == 0 && !(byte & 0x40));
}
stream->WriteData(data, length, desc);
}
void WriteS32Leb128(Stream* stream, uint32_t value, const char* desc) {
WriteS32Leb128(stream, Bitcast<int32_t>(value), desc);
}
void WriteU64Leb128(Stream* stream, uint64_t value, const char* desc) {
uint8_t data[MAX_U64_LEB128_BYTES];
Offset length = 0;
LEB128_LOOP_UNTIL(value == 0);
stream->WriteData(data, length, desc);
}
void WriteS64Leb128(Stream* stream, uint64_t value, const char* desc) {
WriteS64Leb128(stream, Bitcast<int64_t>(value), desc);
}
void WriteFixedS32Leb128(Stream* stream, uint32_t value, const char* desc) {
uint8_t data[MAX_U32_LEB128_BYTES];
data[0] = (value & 0x7f) | 0x80;
data[1] = ((value >> 7) & 0x7f) | 0x80;
data[2] = ((value >> 14) & 0x7f) | 0x80;
data[3] = ((value >> 21) & 0x7f) | 0x80;
// The last byte needs to be sign-extended.
data[4] = ((value >> 28) & 0x0f);
if (static_cast<int32_t>(value) < 0) {
data[4] |= 0x70;
}
stream->WriteData(data, MAX_U32_LEB128_BYTES, desc);
}
#undef LEB128_LOOP_UNTIL
#define BYTE_AT(type, i, shift) ((static_cast<type>(p[i]) & 0x7f) << (shift))
#define LEB128_1(type) (BYTE_AT(type, 0, 0))
#define LEB128_2(type) (BYTE_AT(type, 1, 7) | LEB128_1(type))
#define LEB128_3(type) (BYTE_AT(type, 2, 14) | LEB128_2(type))
#define LEB128_4(type) (BYTE_AT(type, 3, 21) | LEB128_3(type))
#define LEB128_5(type) (BYTE_AT(type, 4, 28) | LEB128_4(type))
#define LEB128_6(type) (BYTE_AT(type, 5, 35) | LEB128_5(type))
#define LEB128_7(type) (BYTE_AT(type, 6, 42) | LEB128_6(type))
#define LEB128_8(type) (BYTE_AT(type, 7, 49) | LEB128_7(type))
#define LEB128_9(type) (BYTE_AT(type, 8, 56) | LEB128_8(type))
#define LEB128_10(type) (BYTE_AT(type, 9, 63) | LEB128_9(type))
#define SHIFT_AMOUNT(type, sign_bit) (sizeof(type) * 8 - 1 - (sign_bit))
#define SIGN_EXTEND(type, value, sign_bit) \
(static_cast<type>((value) << SHIFT_AMOUNT(type, sign_bit)) >> \
SHIFT_AMOUNT(type, sign_bit))
size_t ReadU32Leb128(const uint8_t* p,
const uint8_t* end,
uint32_t* out_value) {
if (p < end && (p[0] & 0x80) == 0) {
*out_value = LEB128_1(uint32_t);
return 1;
} else if (p + 1 < end && (p[1] & 0x80) == 0) {
*out_value = LEB128_2(uint32_t);
return 2;
} else if (p + 2 < end && (p[2] & 0x80) == 0) {
*out_value = LEB128_3(uint32_t);
return 3;
} else if (p + 3 < end && (p[3] & 0x80) == 0) {
*out_value = LEB128_4(uint32_t);
return 4;
} else if (p + 4 < end && (p[4] & 0x80) == 0) {
// The top bits set represent values > 32 bits.
if (p[4] & 0xf0) {
return 0;
}
*out_value = LEB128_5(uint32_t);
return 5;
} else {
// past the end.
*out_value = 0;
return 0;
}
}
size_t ReadU64Leb128(const uint8_t* p,
const uint8_t* end,
uint64_t* out_value) {
if (p < end && (p[0] & 0x80) == 0) {
*out_value = LEB128_1(uint64_t);
return 1;
} else if (p + 1 < end && (p[1] & 0x80) == 0) {
*out_value = LEB128_2(uint64_t);
return 2;
} else if (p + 2 < end && (p[2] & 0x80) == 0) {
*out_value = LEB128_3(uint64_t);
return 3;
} else if (p + 3 < end && (p[3] & 0x80) == 0) {
*out_value = LEB128_4(uint64_t);
return 4;
} else if (p + 4 < end && (p[4] & 0x80) == 0) {
*out_value = LEB128_5(uint64_t);
return 5;
} else if (p + 5 < end && (p[5] & 0x80) == 0) {
*out_value = LEB128_6(uint64_t);
return 6;
} else if (p + 6 < end && (p[6] & 0x80) == 0) {
*out_value = LEB128_7(uint64_t);
return 7;
} else if (p + 7 < end && (p[7] & 0x80) == 0) {
*out_value = LEB128_8(uint64_t);
return 8;
} else if (p + 8 < end && (p[8] & 0x80) == 0) {
*out_value = LEB128_9(uint64_t);
return 9;
} else if (p + 9 < end && (p[9] & 0x80) == 0) {
// The top bits set represent values > 32 bits.
if (p[9] & 0xf0) {
return 0;
}
*out_value = LEB128_10(uint64_t);
return 10;
} else {
// past the end.
*out_value = 0;
return 0;
}
}
size_t ReadS32Leb128(const uint8_t* p,
const uint8_t* end,
uint32_t* out_value) {
if (p < end && (p[0] & 0x80) == 0) {
uint32_t result = LEB128_1(uint32_t);
*out_value = SIGN_EXTEND(int32_t, result, 6);
return 1;
} else if (p + 1 < end && (p[1] & 0x80) == 0) {
uint32_t result = LEB128_2(uint32_t);
*out_value = SIGN_EXTEND(int32_t, result, 13);
return 2;
} else if (p + 2 < end && (p[2] & 0x80) == 0) {
uint32_t result = LEB128_3(uint32_t);
*out_value = SIGN_EXTEND(int32_t, result, 20);
return 3;
} else if (p + 3 < end && (p[3] & 0x80) == 0) {
uint32_t result = LEB128_4(uint32_t);
*out_value = SIGN_EXTEND(int32_t, result, 27);
return 4;
} else if (p + 4 < end && (p[4] & 0x80) == 0) {
// The top bits should be a sign-extension of the sign bit.
bool sign_bit_set = (p[4] & 0x8);
int top_bits = p[4] & 0xf0;
if ((sign_bit_set && top_bits != 0x70) ||
(!sign_bit_set && top_bits != 0)) {
return 0;
}
uint32_t result = LEB128_5(uint32_t);
*out_value = result;
return 5;
} else {
// Past the end.
return 0;
}
}
size_t ReadS64Leb128(const uint8_t* p,
const uint8_t* end,
uint64_t* out_value) {
if (p < end && (p[0] & 0x80) == 0) {
uint64_t result = LEB128_1(uint64_t);
*out_value = SIGN_EXTEND(int64_t, result, 6);
return 1;
} else if (p + 1 < end && (p[1] & 0x80) == 0) {
uint64_t result = LEB128_2(uint64_t);
*out_value = SIGN_EXTEND(int64_t, result, 13);
return 2;
} else if (p + 2 < end && (p[2] & 0x80) == 0) {
uint64_t result = LEB128_3(uint64_t);
*out_value = SIGN_EXTEND(int64_t, result, 20);
return 3;
} else if (p + 3 < end && (p[3] & 0x80) == 0) {
uint64_t result = LEB128_4(uint64_t);
*out_value = SIGN_EXTEND(int64_t, result, 27);
return 4;
} else if (p + 4 < end && (p[4] & 0x80) == 0) {
uint64_t result = LEB128_5(uint64_t);
*out_value = SIGN_EXTEND(int64_t, result, 34);
return 5;
} else if (p + 5 < end && (p[5] & 0x80) == 0) {
uint64_t result = LEB128_6(uint64_t);
*out_value = SIGN_EXTEND(int64_t, result, 41);
return 6;
} else if (p + 6 < end && (p[6] & 0x80) == 0) {
uint64_t result = LEB128_7(uint64_t);
*out_value = SIGN_EXTEND(int64_t, result, 48);
return 7;
} else if (p + 7 < end && (p[7] & 0x80) == 0) {
uint64_t result = LEB128_8(uint64_t);
*out_value = SIGN_EXTEND(int64_t, result, 55);
return 8;
} else if (p + 8 < end && (p[8] & 0x80) == 0) {
uint64_t result = LEB128_9(uint64_t);
*out_value = SIGN_EXTEND(int64_t, result, 62);
return 9;
} else if (p + 9 < end && (p[9] & 0x80) == 0) {
// The top bits should be a sign-extension of the sign bit.
bool sign_bit_set = (p[9] & 0x1);
int top_bits = p[9] & 0xfe;
if ((sign_bit_set && top_bits != 0x7e) ||
(!sign_bit_set && top_bits != 0)) {
return 0;
}
uint64_t result = LEB128_10(uint64_t);
*out_value = result;
return 10;
} else {
// Past the end.
return 0;
}
}
#undef BYTE_AT
#undef LEB128_1
#undef LEB128_2
#undef LEB128_3
#undef LEB128_4
#undef LEB128_5
#undef LEB128_6
#undef LEB128_7
#undef LEB128_8
#undef LEB128_9
#undef LEB128_10
#undef SHIFT_AMOUNT
#undef SIGN_EXTEND
} // namespace wabt
|