forked from facebook/redex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocatortool.cpp
86 lines (77 loc) · 2.08 KB
/
locatortool.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
/*
* 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.
*/
#include <iostream>
#include <vector>
#include <cstdlib>
#include <locator.h>
using namespace std;
using namespace facebook;
void print_usage() {
cout << "Usage:" << endl;
cout << " locatortool d" << endl;
cout << " locatortool e [-h|--hex] <class_num> <dex_num> <store_num>" << endl;
cout << endl;
cout << endl;
cout << " Commands:" << endl;
cout << " d Decode a (raw, not hex) locator string from stdin." << endl;
cout << " e Encode a value" << endl;
cout << " -h | --hex Print a hexdump of the locator instead of the raw string" << endl;
cout << endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
print_usage();
return 0;
}
vector<string> args;
for (int i = 0; i < argc; i++) {
args.emplace_back(argv[i]);
}
try {
switch (*argv[1]) {
case 'd': {
string locator_str;
cin >> locator_str;
Locator locator = Locator::decodeBackward(locator_str.c_str() + locator_str.size());
cout << "class: " << locator.clsnr << endl;
cout << "dex : " << locator.dexnr << endl;
cout << "store: " << locator.strnr << endl;
break;
}
case 'e': {
int p = 2;
bool use_hex = args[p] == "-h" || args[p] == "--hex";
if (use_hex) {
p++;
}
char buf[Locator::encoded_max];
int clsnr = stoi(args[p++]);
int dexnr = stoi(args[p++]);
int strnr = stoi(args[p++]);
if (!Locator::make(strnr, dexnr, clsnr).encode(buf)) {
break;
}
if (use_hex) {
for (size_t i = 0; i < strlen(buf) + 1; i++) {
cout << hex << (int)buf[i] << " ";
}
cout << endl;
} else {
cout << buf << endl;
}
break;
}
default: {
throw 0;
break;
}
}
} catch (...) {
print_usage();
}
return 0;
}