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 #include "slicer/common.h"
19
20 #include <zlib.h>
21 #include <sstream>
22
23 namespace dex {
24
25 // Compute the DEX file checksum for a memory-mapped DEX file
ComputeChecksum(const Header * header)26 u4 ComputeChecksum(const Header* header) {
27 const u1* start = reinterpret_cast<const u1*>(header);
28
29 uLong adler = adler32(0L, Z_NULL, 0);
30 const int non_sum = sizeof(header->magic) + sizeof(header->checksum);
31
32 return static_cast<u4>(
33 adler32(adler, start + non_sum, header->file_size - non_sum));
34 }
35
36 // Returns the human-readable name for a primitive type
PrimitiveTypeName(char type_char)37 static const char* PrimitiveTypeName(char type_char) {
38 switch (type_char) {
39 case 'B': return "byte";
40 case 'C': return "char";
41 case 'D': return "double";
42 case 'F': return "float";
43 case 'I': return "int";
44 case 'J': return "long";
45 case 'S': return "short";
46 case 'V': return "void";
47 case 'Z': return "boolean";
48 }
49 SLICER_CHECK(!"unexpected type");
50 return nullptr;
51 }
52
53 // Converts a type descriptor to human-readable "dotted" form. For
54 // example, "Ljava/lang/String;" becomes "java.lang.String", and
55 // "[I" becomes "int[]".
DescriptorToDecl(const char * descriptor)56 std::string DescriptorToDecl(const char* descriptor) {
57 std::stringstream ss;
58
59 int array_dimensions = 0;
60 while (*descriptor == '[') {
61 ++array_dimensions;
62 ++descriptor;
63 }
64
65 if (*descriptor == 'L') {
66 for (++descriptor; *descriptor != ';'; ++descriptor) {
67 SLICER_CHECK(*descriptor != '\0');
68 ss << (*descriptor == '/' ? '.' : *descriptor);
69 }
70 } else {
71 ss << PrimitiveTypeName(*descriptor);
72 }
73
74 SLICER_CHECK(descriptor[1] == '\0');
75
76 // add the array brackets
77 for (int i = 0; i < array_dimensions; ++i) {
78 ss << "[]";
79 }
80
81 return ss.str();
82 }
83
84 // Converts a type descriptor to a single "shorty" char
85 // (ex. "LFoo;" and "[[I" become 'L', "I" stays 'I')
DescriptorToShorty(const char * descriptor)86 char DescriptorToShorty(const char* descriptor) {
87 // skip array dimensions
88 int array_dimensions = 0;
89 while (*descriptor == '[') {
90 ++array_dimensions;
91 ++descriptor;
92 }
93
94 char short_descriptor = *descriptor;
95 if (short_descriptor == 'L') {
96 // skip the full class name
97 for(; *descriptor && *descriptor != ';'; ++descriptor);
98 SLICER_CHECK(*descriptor == ';');
99 }
100
101 SLICER_CHECK(descriptor[1] == '\0');
102 SLICER_CHECK(short_descriptor == 'L' || PrimitiveTypeName(short_descriptor) != nullptr);
103
104 return array_dimensions > 0 ? 'L' : short_descriptor;
105 }
106
107 } // namespace dex
108