blob: 08f67ff9d0fb935051d09fe51f251a7c72034ba7 [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001/*
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 */
Andy McFadden95ed76b2009-09-29 10:55:32 -070016
Mathias Agopian3344b2e2009-06-05 14:55:48 -070017#include "ZipFile.h"
The Android Open Source Project88b60792009-03-03 19:28:42 -080018
The Android Open Source Project88b60792009-03-03 19:28:42 -080019#include <stdio.h>
Mark Salyzyn404fd5b2016-10-17 10:20:33 -070020#include <stdlib.h>
21#include <unistd.h>
The Android Open Source Project88b60792009-03-03 19:28:42 -080022
Fabien Sanglard0f29f542020-10-22 17:58:12 -070023namespace android {
The Android Open Source Project88b60792009-03-03 19:28:42 -080024
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070025static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment,
26 ZipEntry* pEntry) {
27
Narayan Kamathe0b8d192015-02-26 17:57:55 +000028 static const int kPageAlignment = 4096;
29
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070030 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
The Android Open Source Project88b60792009-03-03 19:28:42 -080042/*
43 * Copy all entries from "pZin" to "pZout", aligning as needed.
44 */
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070045static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli,
Dan Willemsenb589ae42015-10-29 21:26:18 +000046 bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -080047{
48 int numEntries = pZin->getNumEntries();
49 ZipEntry* pEntry;
The Android Open Source Project88b60792009-03-03 19:28:42 -080050 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
Raph Levien093d04c2014-07-07 16:00:29 -070068 if (zopfli) {
Dan Willemsenb589ae42015-10-29 21:26:18 +000069 status = pZout->addRecompress(pZin, pEntry, &pNewEntry);
Raph Levien093d04c2014-07-07 16:00:29 -070070 } else {
Dan Willemsenb589ae42015-10-29 21:26:18 +000071 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
Raph Levien093d04c2014-07-07 16:00:29 -070072 }
The Android Open Source Project88b60792009-03-03 19:28:42 -080073 } else {
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070074 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
75
The Android Open Source Project88b60792009-03-03 19:28:42 -080076 //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
77 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
78 // bias, (long) pEntry->getUncompressedLen(), padding);
Fabien Sanglarda7206352020-10-20 15:47:10 -070079 status = pZout->add(pZin, pEntry, alignTo, &pNewEntry);
The Android Open Source Project88b60792009-03-03 19:28:42 -080080 }
81
Elliott Hughesad7d5622018-10-08 11:19:28 -070082 if (status != OK)
The Android Open Source Project88b60792009-03-03 19:28:42 -080083 return 1;
The Android Open Source Project88b60792009-03-03 19:28:42 -080084 //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 */
Fabien Sanglard0f29f542020-10-22 17:58:12 -070096int process(const char* inFileName, const char* outFileName,
Dan Willemsenb589ae42015-10-29 21:26:18 +000097 int alignment, bool force, bool zopfli, bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -080098{
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
Elliott Hughesad7d5622018-10-08 11:19:28 -0700116 if (zin.open(inFileName, ZipFile::kOpenReadOnly) != OK) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800117 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)
Elliott Hughesad7d5622018-10-08 11:19:28 -0700122 != OK)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800123 {
Jingwen Owen Ou30321392012-08-17 16:21:11 -0700124 fprintf(stderr, "Unable to open '%s' as zip archive\n", outFileName);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800125 return 1;
126 }
127
Dan Willemsenb589ae42015-10-29 21:26:18 +0000128 int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800129 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 */
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700139int verify(const char* fileName, int alignment, bool verbose,
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700140 bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800141{
142 ZipFile zipFile;
143 bool foundBad = false;
144
145 if (verbose)
146 printf("Verifying alignment of %s (%d)...\n", fileName, alignment);
147
Elliott Hughesad7d5622018-10-08 11:19:28 -0700148 if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != OK) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800149 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) {
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700160 printf("%8jd %s (OK - compressed)\n",
161 (intmax_t) pEntry->getFileOffset(), pEntry->getFileName());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800162 }
163 } else {
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700164 off_t offset = pEntry->getFileOffset();
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700165 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
166 if ((offset % alignTo) != 0) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800167 if (verbose) {
Chih-Hung Hsieh9b2bf2d2018-08-11 11:33:43 -0700168 printf("%8jd %s (BAD - %jd)\n",
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700169 (intmax_t) offset, pEntry->getFileName(),
Chih-Hung Hsieh9b2bf2d2018-08-11 11:33:43 -0700170 (intmax_t) (offset % alignTo));
The Android Open Source Project88b60792009-03-03 19:28:42 -0800171 }
172 foundBad = true;
173 } else {
174 if (verbose) {
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700175 printf("%8jd %s (OK)\n",
176 (intmax_t) offset, pEntry->getFileName());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800177 }
178 }
179 }
180 }
181
182 if (verbose)
183 printf("Verification %s\n", foundBad ? "FAILED" : "succesful");
184
185 return foundBad ? 1 : 0;
186}
187
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700188} // namespace android