This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dex_format.cc
executable file
·106 lines (88 loc) · 3.03 KB
/
dex_format.cc
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
/*
* Copyright (C) 2017 The Android Open Source Project
*
* 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 "slicer/dex_format.h"
#include "slicer/common.h"
#include <zlib.h>
namespace dex {
// Compute the DEX file checksum for a memory-mapped DEX file
u4 ComputeChecksum(const Header* header) {
const u1* start = reinterpret_cast<const u1*>(header);
uLong adler = adler32(0L, Z_NULL, 0);
const int non_sum = sizeof(header->magic) + sizeof(header->checksum);
return static_cast<u4>(
adler32(adler, start + non_sum, header->file_size - non_sum));
}
// Returns the human-readable name for a primitive type
static const char* PrimitiveTypeName(char type_char) {
switch (type_char) {
case 'B': return "byte";
case 'C': return "char";
case 'D': return "double";
case 'F': return "float";
case 'I': return "int";
case 'J': return "long";
case 'S': return "short";
case 'V': return "void";
case 'Z': return "boolean";
}
SLICER_CHECK(!"unexpected type");
return nullptr;
}
// Converts a type descriptor to human-readable "dotted" form. For
// example, "Ljava/lang/String;" becomes "java.lang.String", and
// "[I" becomes "int[]".
std::string DescriptorToDecl(const char* descriptor) {
std::string ss;
int array_dimensions = 0;
while (*descriptor == '[') {
++array_dimensions;
++descriptor;
}
if (*descriptor == 'L') {
for (++descriptor; *descriptor != ';'; ++descriptor) {
SLICER_CHECK(*descriptor != '\0');
ss += (*descriptor == '/' ? '.' : *descriptor);
}
} else {
ss += PrimitiveTypeName(*descriptor);
}
SLICER_CHECK(descriptor[1] == '\0');
// add the array brackets
for (int i = 0; i < array_dimensions; ++i) {
ss += "[]";
}
return ss;
}
// Converts a type descriptor to a single "shorty" char
// (ex. "LFoo;" and "[[I" become 'L', "I" stays 'I')
char DescriptorToShorty(const char* descriptor) {
// skip array dimensions
int array_dimensions = 0;
while (*descriptor == '[') {
++array_dimensions;
++descriptor;
}
char short_descriptor = *descriptor;
if (short_descriptor == 'L') {
// skip the full class name
for(; *descriptor && *descriptor != ';'; ++descriptor);
SLICER_CHECK(*descriptor == ';');
}
SLICER_CHECK(descriptor[1] == '\0');
SLICER_CHECK(short_descriptor == 'L' || PrimitiveTypeName(short_descriptor) != nullptr);
return array_dimensions > 0 ? 'L' : short_descriptor;
}
} // namespace dex