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 #include "ZipFile.h"
18 
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 
24 namespace android {
25 
26 // An entry is considered a directory if it has a stored size of zero
27 // and it ends with '/' or '\' character.
isDirectory(ZipEntry * entry)28 static bool isDirectory(ZipEntry* entry) {
29    if (entry->getUncompressedLen() != 0) {
30        return false;
31    }
32 
33    const char* name = entry->getFileName();
34    size_t nameLength = strlen(name);
35    char lastChar = name[nameLength-1];
36    return lastChar == '/' || lastChar == '\\';
37 }
38 
getAlignment(bool pageAlignSharedLibs,int defaultAlignment,ZipEntry * pEntry,int pageSize)39 static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment,
40     ZipEntry* pEntry, int pageSize) {
41     if (!pageAlignSharedLibs) {
42         return defaultAlignment;
43     }
44 
45     const char* ext = strrchr(pEntry->getFileName(), '.');
46     if (ext && strcmp(ext, ".so") == 0) {
47         return pageSize;
48     }
49 
50     return defaultAlignment;
51 }
52 
53 /*
54  * Copy all entries from "pZin" to "pZout", aligning as needed.
55  */
copyAndAlign(ZipFile * pZin,ZipFile * pZout,int alignment,bool zopfli,bool pageAlignSharedLibs,int pageSize)56 static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli,
57     bool pageAlignSharedLibs, int pageSize)
58 {
59     int numEntries = pZin->getNumEntries();
60     ZipEntry* pEntry;
61     status_t status;
62 
63     for (int i = 0; i < numEntries; i++) {
64         ZipEntry* pNewEntry;
65         int padding = 0;
66 
67         pEntry = pZin->getEntryByIndex(i);
68         if (pEntry == NULL) {
69             fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i);
70             return 1;
71         }
72 
73         if (pEntry->isCompressed() || isDirectory(pEntry)) {
74             /* copy the entry without padding */
75             //printf("--- %s: orig at %ld len=%ld (compressed)\n",
76             //    pEntry->getFileName(), (long) pEntry->getFileOffset(),
77             //    (long) pEntry->getUncompressedLen());
78 
79             if (zopfli) {
80                 status = pZout->addRecompress(pZin, pEntry, &pNewEntry);
81             } else {
82                 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
83             }
84         } else {
85             const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry,
86                                              pageSize);
87 
88             //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
89             //    pEntry->getFileName(), (long) pEntry->getFileOffset(),
90             //    bias, (long) pEntry->getUncompressedLen(), padding);
91             status = pZout->add(pZin, pEntry, alignTo, &pNewEntry);
92         }
93 
94         if (status != OK)
95             return 1;
96         //printf(" added '%s' at %ld (pad=%d)\n",
97         //    pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(),
98         //    padding);
99     }
100 
101     return 0;
102 }
103 
104 /*
105  * Process a file.  We open the input and output files, failing if the
106  * output file exists and "force" wasn't specified.
107  */
process(const char * inFileName,const char * outFileName,int alignment,bool force,bool zopfli,bool pageAlignSharedLibs,int pageSize)108 int process(const char* inFileName, const char* outFileName,
109     int alignment, bool force, bool zopfli, bool pageAlignSharedLibs, int pageSize)
110 {
111     ZipFile zin, zout;
112 
113     //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n",
114     //    alignment, inFileName, outFileName, force);
115 
116     /* this mode isn't supported -- do a trivial check */
117     if (strcmp(inFileName, outFileName) == 0) {
118         fprintf(stderr, "Input and output can't be same file\n");
119         return 1;
120     }
121 
122     /* don't overwrite existing unless given permission */
123     if (!force && access(outFileName, F_OK) == 0) {
124         fprintf(stderr, "Output file '%s' exists\n", outFileName);
125         return 1;
126     }
127 
128     if (zin.open(inFileName, ZipFile::kOpenReadOnly) != OK) {
129         fprintf(stderr, "Unable to open '%s' as zip archive: %s\n", inFileName, strerror(errno));
130         return 1;
131     }
132     if (zout.open(outFileName,
133             ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate)
134         != OK)
135     {
136         fprintf(stderr, "Unable to open '%s' as zip archive\n", outFileName);
137         return 1;
138     }
139 
140     int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs,
141                               pageSize);
142     if (result != 0) {
143         printf("zipalign: failed rewriting '%s' to '%s'\n",
144             inFileName, outFileName);
145     }
146     return result;
147 }
148 
149 /*
150  * Verify the alignment of a zip archive.
151  */
verify(const char * fileName,int alignment,bool verbose,bool pageAlignSharedLibs,int pageSize)152 int verify(const char* fileName, int alignment, bool verbose,
153     bool pageAlignSharedLibs, int pageSize)
154 {
155     ZipFile zipFile;
156     bool foundBad = false;
157 
158     if (verbose)
159         printf("Verifying alignment of %s (%d)...\n", fileName, alignment);
160 
161     if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != OK) {
162         fprintf(stderr, "Unable to open '%s' for verification\n", fileName);
163         return 1;
164     }
165 
166     int numEntries = zipFile.getNumEntries();
167     ZipEntry* pEntry;
168 
169     for (int i = 0; i < numEntries; i++) {
170         pEntry = zipFile.getEntryByIndex(i);
171         if (pEntry->isCompressed()) {
172             if (verbose) {
173                 printf("%8jd %s (OK - compressed)\n",
174                     (intmax_t) pEntry->getFileOffset(), pEntry->getFileName());
175             }
176         } else if(isDirectory(pEntry)) {
177             // Directory entries do not need to be aligned.
178             if (verbose)
179                 printf("%8jd %s (OK - directory)\n",
180                        (intmax_t) pEntry->getFileOffset(), pEntry->getFileName());
181             continue;
182        } else {
183             off_t offset = pEntry->getFileOffset();
184             const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry,
185                                              pageSize);
186             if ((offset % alignTo) != 0) {
187                 if (verbose) {
188                     printf("%8jd %s (BAD - %jd)\n",
189                         (intmax_t) offset, pEntry->getFileName(),
190                         (intmax_t) (offset % alignTo));
191                 }
192                 foundBad = true;
193             } else {
194                 if (verbose) {
195                     printf("%8jd %s (OK)\n",
196                         (intmax_t) offset, pEntry->getFileName());
197                 }
198             }
199         }
200     }
201 
202     if (verbose)
203         printf("Verification %s\n", foundBad ? "FAILED" : "successful");
204 
205     return foundBad ? 1 : 0;
206 }
207 
208 } // namespace android
209