1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "slicer/dex_format.h"
18
19 #include "slicer/common.h"
20
21 #include <sstream>
22 #include <zlib.h>
23
24 namespace dex {
25
26 // Compute the DEX file checksum for a memory-mapped DEX file
ComputeChecksum(const Header * header)27 u4 ComputeChecksum(const Header* header) {
28 const u1* start = reinterpret_cast<const u1*>(header);
29
30 uLong adler = adler32(0L, Z_NULL, 0);
31 const int non_sum = sizeof(header->magic) + sizeof(header->checksum);
32
33 return static_cast<u4>(
34 adler32(adler, start + non_sum, header->file_size - non_sum));
35 }
36
37 // Returns the human-readable name for a primitive type
PrimitiveTypeName(char type_char)38 static const char* PrimitiveTypeName(char type_char) {
39 switch (type_char) {
40 case 'B': return "byte";
41 case 'C': return "char";
42 case 'D': return "double";
43 case 'F': return "float";
44 case 'I': return "int";
45 case 'J': return "long";
46 case 'S': return "short";
47 case 'V': return "void";
48 case 'Z': return "boolean";
49 }
50 SLICER_CHECK(!"unexpected type");
51 return nullptr;
52 }
53
54 // Converts a type descriptor to human-readable "dotted" form. For
55 // example, "Ljava/lang/String;" becomes "java.lang.String", and
56 // "[I" becomes "int[]".
DescriptorToDecl(const char * descriptor)57 std::string DescriptorToDecl(const char* descriptor) {
58 std::stringstream ss;
59
60 int array_dimensions = 0;
61 while (*descriptor == '[') {
62 ++array_dimensions;
63 ++descriptor;
64 }
65
66 if (*descriptor == 'L') {
67 for (++descriptor; *descriptor != ';'; ++descriptor) {
68 SLICER_CHECK_NE(*descriptor, '\0');
69 ss << (*descriptor == '/' ? '.' : *descriptor);
70 }
71 } else {
72 ss << PrimitiveTypeName(*descriptor);
73 }
74
75 SLICER_CHECK_EQ(descriptor[1], '\0');
76
77 // add the array brackets
78 for (int i = 0; i < array_dimensions; ++i) {
79 ss << "[]";
80 }
81
82 return ss.str();
83 }
84
85 // Converts a type descriptor to a single "shorty" char
86 // (ex. "LFoo;" and "[[I" become 'L', "I" stays 'I')
DescriptorToShorty(const char * descriptor)87 char DescriptorToShorty(const char* descriptor) {
88 // skip array dimensions
89 int array_dimensions = 0;
90 while (*descriptor == '[') {
91 ++array_dimensions;
92 ++descriptor;
93 }
94
95 char short_descriptor = *descriptor;
96 if (short_descriptor == 'L') {
97 // skip the full class name
98 for(; *descriptor && *descriptor != ';'; ++descriptor);
99 SLICER_CHECK_EQ(*descriptor, ';');
100 }
101
102 SLICER_CHECK_EQ(descriptor[1], '\0');
103 SLICER_CHECK(short_descriptor == 'L' || PrimitiveTypeName(short_descriptor) != nullptr);
104
105 return array_dimensions > 0 ? 'L' : short_descriptor;
106 }
107
108 } // namespace dex
109