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