-
Notifications
You must be signed in to change notification settings - Fork 667
/
Copy pathlocator.cpp
89 lines (74 loc) · 2.11 KB
/
locator.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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <assert.h>
#include <exception>
#include <stdexcept>
// clang-format off (import order matters)
#include "locator.h"
// clang-format on
namespace facebook {
Locator Locator::make(uint32_t str, uint32_t dex, uint32_t cls) {
if (str >= (1 << strnr_bits)) {
throw std::runtime_error("too many dex stores");
}
if (dex >= (1 << dexnr_bits)) {
throw std::runtime_error("too many dex files");
}
if (cls >= (1 << clsnr_bits)) {
throw std::runtime_error("too many classes in one dex");
}
return Locator(str, dex, cls);
}
uint32_t Locator::encode(char buf[encoded_max]) noexcept {
uint64_t value = ((uint64_t)strnr) << clsnr_bits;
value = (value | clsnr) << dexnr_bits;
value = (value | dexnr);
uint8_t* pos = (uint8_t*)&buf[0];
while (value != 0) {
uint8_t enc = (value % base) + bias;
assert((enc & 0x80) == 0);
assert(enc >= bias);
*pos++ = enc;
value /= base;
}
*pos = '\0';
uint32_t len = (pos - (uint8_t*)buf);
assert(len <= encoded_max);
return len;
}
static char getDigit(uint32_t num) {
assert(num >= 0 && num < Locator::global_class_index_digits_base);
if (num < 10) {
return num + '0';
} else if (num >= 10 && num < 36) {
return num - 10 + 'A';
} else {
return num - 10 - 26 + 'a';
}
}
void Locator::encodeGlobalClassIndex(
uint32_t globalClassIndex,
size_t digits,
char buf[encoded_global_class_index_max]) noexcept {
assert(digits > 0 && digits <= global_class_index_digits_max);
char* pos = buf;
*pos++ = 'L';
*pos++ = 'X';
*pos++ = '/';
uint32_t num = globalClassIndex;
char* digit_pos = pos + digits;
do {
*--digit_pos = getDigit(num % global_class_index_digits_base);
num /= global_class_index_digits_base;
} while (digit_pos != pos);
assert(num == 0);
pos += digits;
*pos++ = ';';
*pos++ = '\0';
assert(static_cast<uint32_t>(pos - buf) <= encoded_global_class_index_max);
}
} // namespace facebook