The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | */ |
Andy McFadden | 95ed76b | 2009-09-29 10:55:32 -0700 | [diff] [blame] | 16 | |
Mathias Agopian | 3344b2e | 2009-06-05 14:55:48 -0700 | [diff] [blame] | 17 | #include "ZipFile.h" |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
Mark Salyzyn | 404fd5b | 2016-10-17 10:20:33 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <unistd.h> |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 22 | |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 23 | namespace android { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 24 | |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 25 | static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment, |
| 26 | ZipEntry* pEntry) { |
| 27 | |
Narayan Kamath | e0b8d19 | 2015-02-26 17:57:55 +0000 | [diff] [blame] | 28 | static const int kPageAlignment = 4096; |
| 29 | |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 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 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 42 | /* |
| 43 | * Copy all entries from "pZin" to "pZout", aligning as needed. |
| 44 | */ |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 45 | static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli, |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 46 | bool pageAlignSharedLibs) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 47 | { |
| 48 | int numEntries = pZin->getNumEntries(); |
| 49 | ZipEntry* pEntry; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | |
Raph Levien | 093d04c | 2014-07-07 16:00:29 -0700 | [diff] [blame] | 68 | if (zopfli) { |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 69 | status = pZout->addRecompress(pZin, pEntry, &pNewEntry); |
Raph Levien | 093d04c | 2014-07-07 16:00:29 -0700 | [diff] [blame] | 70 | } else { |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 71 | status = pZout->add(pZin, pEntry, padding, &pNewEntry); |
Raph Levien | 093d04c | 2014-07-07 16:00:29 -0700 | [diff] [blame] | 72 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 73 | } else { |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 74 | const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry); |
| 75 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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); |
Fabien Sanglard | a720635 | 2020-10-20 15:47:10 -0700 | [diff] [blame^] | 79 | status = pZout->add(pZin, pEntry, alignTo, &pNewEntry); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 82 | if (status != OK) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 83 | return 1; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | */ |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 96 | int process(const char* inFileName, const char* outFileName, |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 97 | int alignment, bool force, bool zopfli, bool pageAlignSharedLibs) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 116 | if (zin.open(inFileName, ZipFile::kOpenReadOnly) != OK) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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) |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 122 | != OK) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 123 | { |
Jingwen Owen Ou | 3032139 | 2012-08-17 16:21:11 -0700 | [diff] [blame] | 124 | fprintf(stderr, "Unable to open '%s' as zip archive\n", outFileName); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 125 | return 1; |
| 126 | } |
| 127 | |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 128 | int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | */ |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 139 | int verify(const char* fileName, int alignment, bool verbose, |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 140 | bool pageAlignSharedLibs) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 141 | { |
| 142 | ZipFile zipFile; |
| 143 | bool foundBad = false; |
| 144 | |
| 145 | if (verbose) |
| 146 | printf("Verifying alignment of %s (%d)...\n", fileName, alignment); |
| 147 | |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 148 | if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != OK) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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) { |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 160 | printf("%8jd %s (OK - compressed)\n", |
| 161 | (intmax_t) pEntry->getFileOffset(), pEntry->getFileName()); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 162 | } |
| 163 | } else { |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 164 | off_t offset = pEntry->getFileOffset(); |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 165 | const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry); |
| 166 | if ((offset % alignTo) != 0) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 167 | if (verbose) { |
Chih-Hung Hsieh | 9b2bf2d | 2018-08-11 11:33:43 -0700 | [diff] [blame] | 168 | printf("%8jd %s (BAD - %jd)\n", |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 169 | (intmax_t) offset, pEntry->getFileName(), |
Chih-Hung Hsieh | 9b2bf2d | 2018-08-11 11:33:43 -0700 | [diff] [blame] | 170 | (intmax_t) (offset % alignTo)); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 171 | } |
| 172 | foundBad = true; |
| 173 | } else { |
| 174 | if (verbose) { |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 175 | printf("%8jd %s (OK)\n", |
| 176 | (intmax_t) offset, pEntry->getFileName()); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (verbose) |
| 183 | printf("Verification %s\n", foundBad ? "FAILED" : "succesful"); |
| 184 | |
| 185 | return foundBad ? 1 : 0; |
| 186 | } |
| 187 | |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 188 | } // namespace android |