forked from facebook/redex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDexEncoding.h
240 lines (223 loc) · 5.81 KB
/
DexEncoding.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <sstream>
#include <stdexcept>
#include <stdint.h>
#include <string>
/*
* LEB128 is a DEX data type. It was borrowed by DEX from the DWARF3
* specification. Dex uses a subset of it, which it uses for encoding of
* both signed and unsigned 32bit values. The reason DEX uses it is to
* take up less space in a binary file for numbers which tend to be small.
*
* We are only using uleb128 encoded data from ClassDef's. ClassDef's do
* not contain signed encoded data (sleb128's), so we only include an
* implementation of uleb128.
*
* For more detailed information please consult the Dalvik Executable
* Format documentation.
*
* Link here:
* https://source.android.com/devices/tech/dalvik/dex-format.html
*/
/* read_uleb128:
* Returns the uint32_t encoded at the pointed to memory. Also
* advances the pointer to the next uleb128.
*/
inline uint32_t read_uleb128(const uint8_t** _ptr) {
const uint8_t* ptr = *_ptr;
int result = *(ptr++);
if (result > 0x7f) {
int cur = *(ptr++);
result = (result & 0x7f) | ((cur & 0x7f) << 7);
if (cur > 0x7f) {
cur = *(ptr++);
result |= (cur & 0x7f) << 14;
if (cur > 0x7f) {
cur = *(ptr++);
result |= (cur & 0x7f) << 21;
if (cur > 0x7f) {
cur = *(ptr++);
result |= cur << 28;
}
}
}
}
*_ptr = ptr;
return result;
}
inline uint32_t read_uleb128p1(const uint8_t** _ptr) {
int v = read_uleb128(_ptr);
return (v - 1);
}
/*
* Number of bytes it takes to encode a particular integer in a uleb128.
*/
inline uint8_t uleb128_encoding_size(uint32_t v) {
v >>= 7;
if (v == 0) return 1;
v >>= 7;
if (v == 0) return 2;
v >>= 7;
if (v == 0) return 3;
v >>= 7;
if (v == 0) return 4;
return 5;
}
inline int32_t read_sleb128(const uint8_t** _ptr) {
const uint8_t* ptr = *_ptr;
int32_t result = *(ptr++);
if (result <= 0x7f) {
result = (result << 25) >> 25;
} else {
int cur = *(ptr++);
result = (result & 0x7f) | ((cur & 0x7f) << 7);
if (cur <= 0x7f) {
result = (result << 18) >> 18;
} else {
cur = *(ptr++);
result |= (cur & 0x7f) << 14;
if (cur <= 0x7f) {
result = (result << 11) >> 11;
} else {
cur = *(ptr++);
result |= (cur & 0x7f) << 21;
if (cur <= 0x7f) {
result = (result << 4) >> 4;
} else {
cur = *(ptr++);
result |= cur << 28;
}
}
}
}
*_ptr = ptr;
return result;
}
/* write_uleb128
* Encode the uint32_t val at the output referred to by ptr. Returns the
* pointer to the next location for encoding.
*/
inline uint8_t* write_uleb128(uint8_t* ptr, uint32_t val) {
while (1) {
uint8_t v = val & 0x7f;
if (v != val) {
*ptr++ = v | 0x80;
val >>= 7;
} else {
*ptr++ = v;
return ptr;
}
}
}
inline uint8_t* write_uleb128p1(uint8_t* ptr, uint32_t val) {
return write_uleb128(ptr, val + 1);
}
inline uint8_t* write_sleb128(uint8_t* ptr, int32_t val) {
while (1) {
uint8_t v = val & 0x7f;
if (v == val) {
/* Positive sleb termination */
if (v & 0x40) {
/* Can't let it sign extend... */
*ptr++ = v | 0x80;
*ptr++ = 0;
return ptr;
}
*ptr++ = v;
return ptr;
}
if (val < 0 && val > -64) {
/* Negative sleb termination */
*ptr++ = v;
return ptr;
}
*ptr++ = v | 0x80;
val >>= 7;
}
}
inline uint32_t mutf8_next_code_point(const char*& s) {
uint8_t v = *s++;
/* Simple common case first, a utf8 char... */
if (!(v & 0x80)) return v;
uint8_t v2 = *s++;
if ((v2 & 0xc0) != 0x80) {
/* Invalid string. */
throw std::invalid_argument("Invalid 2nd byte on mutf8 string");
}
/* Two byte code point */
if ((v & 0xe0) == 0xc0) {
return (v & 0x1f) << 6 | (v2 & 0x3f);
}
/* Three byte code point */
if ((v & 0xf0) == 0xe0) {
uint8_t v3 = *s++;
if ((v2 & 0xc0) != 0x80) {
/* Invalid string. */
throw std::invalid_argument("Invalid 3rd byte on mutf8 string");
}
return (v & 0x1f) << 12 | (v2 & 0x3f) << 6 | (v3 & 0x3f);
}
/* Invalid string. */
throw std::invalid_argument("Invalid size encoding mutf8 string");
}
inline uint32_t length_of_utf8_string(const char* s) {
if (s == nullptr) {
return 0;
}
uint32_t len = 0;
while (*s != '\0') {
++len;
mutf8_next_code_point(s);
}
return len;
}
inline uint32_t size_of_utf8_char(const int32_t ival) {
if (ival >= 0x00 && ival <= 0x7F) {
return 1;
} else if (ival <= 0x7FF) {
return 2;
} else {
return 3;
}
}
// Pretty much the reverse of mutf8_next_code_point().
inline std::string encode_utf8_char_to_mutf8_string(const int32_t ival) {
uint32_t size = size_of_utf8_char(ival);
char buf[4];
int idx = 0;
if (size == 1) {
if (ival > 0x7F) {
throw std::invalid_argument("Invalid utf8_char for encoding to mutf8 string");
}
if (ival == 0x00) { // \u0000 in 2 bytes
buf[idx++] = 0xC0;
buf[idx++] = 0x80;
} else {
buf[idx++] = ival;
}
} else if (size == 2) {
uint8_t byte1 = 0xC0 | ((ival >> 6) & 0x1F);
uint8_t byte2 = 0x80 | (ival & 0x3F);
buf[idx++] = byte1;
buf[idx++] = byte2;
} else if (size == 3) {
uint8_t byte1 = 0xE0 | ((ival >> 12) & 0x0F);
uint8_t byte2 = 0x80 | ((ival >> 6) & 0x3F);
uint8_t byte3 = 0x80 | (ival & 0x3F);
buf[idx++] = byte1;
buf[idx++] = byte2;
buf[idx++] = byte3;
} else {
std::ostringstream exception_message;
exception_message << "Unexpected char size: " << size;
throw std::invalid_argument(exception_message.str());
}
buf[idx] = 0x00;
return std::string(buf);
}