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