1 /*
2 * Copyright (C) 2008 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 * Some utility functions for use with command-line utilities.
18 */
19 #include "DexFile.h"
20 #include "ZipArchive.h"
21 #include "CmdUtils.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <errno.h>
27
28
29 /*
30 * Extract "classes.dex" from archive file.
31 *
32 * If "quiet" is set, don't report common errors.
33 */
dexUnzipToFile(const char * zipFileName,const char * outFileName,bool quiet)34 UnzipToFileResult dexUnzipToFile(const char* zipFileName,
35 const char* outFileName, bool quiet)
36 {
37 UnzipToFileResult result = kUTFRSuccess;
38 static const char* kFileToExtract = "classes.dex";
39 ZipArchive archive;
40 ZipEntry entry;
41 bool unlinkOnFailure = false;
42 int fd = -1;
43
44 if (dexZipOpenArchive(zipFileName, &archive) != 0) {
45 if (!quiet) {
46 fprintf(stderr, "Unable to open '%s' as zip archive\n",
47 zipFileName);
48 }
49 result = kUTFRNotZip;
50 goto bail;
51 }
52
53 fd = open(outFileName, O_WRONLY | O_CREAT | O_EXCL, 0600);
54 if (fd < 0) {
55 fprintf(stderr, "Unable to create output file '%s': %s\n",
56 outFileName, strerror(errno));
57 result = kUTFROutputFileProblem;
58 goto bail;
59 }
60
61 unlinkOnFailure = true;
62
63 entry = dexZipFindEntry(&archive, kFileToExtract);
64 if (entry == NULL) {
65 if (!quiet) {
66 fprintf(stderr, "Unable to find '%s' in '%s'\n",
67 kFileToExtract, zipFileName);
68 }
69 result = kUTFRNoClassesDex;
70 goto bail;
71 }
72
73 if (!dexZipExtractEntryToFile(&archive, entry, fd)) {
74 fprintf(stderr, "Extract of '%s' from '%s' failed\n",
75 kFileToExtract, zipFileName);
76 result = kUTFRBadZip;
77 goto bail;
78 }
79
80 bail:
81 if (fd >= 0)
82 close(fd);
83 if (unlinkOnFailure && result != kUTFRSuccess)
84 unlink(outFileName);
85 dexZipCloseArchive(&archive);
86 return result;
87 }
88
89 /*
90 * Map the specified DEX file read-only (possibly after expanding it into a
91 * temp file from a Jar). Pass in a MemMapping struct to hold the info.
92 *
93 * The temp file is deleted after the map succeeds.
94 *
95 * This is intended for use by tools (e.g. dexdump) that need to get a
96 * read-only copy of a DEX file that could be in a number of different states.
97 *
98 * If "quiet" is set, don't report common errors.
99 *
100 * Returns 0 (kUTFRSuccess) on success.
101 */
dexOpenAndMap(const char * fileName,const char * tempFileName,MemMapping * pMap,bool quiet)102 UnzipToFileResult dexOpenAndMap(const char* fileName, const char* tempFileName,
103 MemMapping* pMap, bool quiet)
104 {
105 UnzipToFileResult result = kUTFRGenericFailure;
106 int len = strlen(fileName);
107 char tempNameBuf[32];
108 bool removeTemp = false;
109 int fd = -1;
110
111 if (len < 5) {
112 if (!quiet) {
113 fprintf(stderr,
114 "ERROR: filename must end in .dex, .zip, .jar, or .apk\n");
115 }
116 result = kUTFRBadArgs;
117 goto bail;
118 }
119
120 if (strcasecmp(fileName + len -3, "dex") != 0) {
121 if (tempFileName == NULL) {
122 /*
123 * Try .zip/.jar/.apk, all of which are Zip archives with
124 * "classes.dex" inside. We need to extract the compressed
125 * data to a temp file, the location of which varies.
126 */
127 if (access("/tmp", W_OK) == 0)
128 sprintf(tempNameBuf, "/tmp/dex-temp-%d", getpid());
129 else
130 sprintf(tempNameBuf, "/sdcard/dex-temp-%d", getpid());
131
132 tempFileName = tempNameBuf;
133 }
134
135 result = dexUnzipToFile(fileName, tempFileName, quiet);
136
137 if (result == kUTFRSuccess) {
138 //printf("+++ Good unzip to '%s'\n", tempFileName);
139 fileName = tempFileName;
140 removeTemp = true;
141 } else if (result == kUTFRNotZip) {
142 if (!quiet) {
143 fprintf(stderr, "Not Zip, retrying as DEX\n");
144 }
145 } else {
146 if (!quiet && result == kUTFRNoClassesDex) {
147 fprintf(stderr, "Zip has no classes.dex\n");
148 }
149 goto bail;
150 }
151 }
152
153 /*
154 * Pop open the (presumed) DEX file.
155 */
156 fd = open(fileName, O_RDONLY);
157 if (fd < 0) {
158 if (!quiet) {
159 fprintf(stderr, "ERROR: unable to open '%s': %s\n",
160 fileName, strerror(errno));
161 }
162 goto bail;
163 }
164
165 if (sysMapFileInShmem(fd, pMap) != 0) {
166 fprintf(stderr, "ERROR: Unable to map %s\n", fileName);
167 close(fd);
168 goto bail;
169 }
170
171 /*
172 * Success! Close the file and return with the start/length in pMap.
173 */
174 result = 0;
175
176 bail:
177 if (fd >= 0)
178 close(fd);
179 if (removeTemp) {
180 if (unlink(tempFileName) != 0) {
181 fprintf(stderr, "Warning: unable to remove temp '%s'\n",
182 tempFileName);
183 }
184 }
185 return result;
186 }
187