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 | d4f71a9 | 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; |
| 50 | int bias = 0; |
| 51 | status_t status; |
| 52 | |
| 53 | for (int i = 0; i < numEntries; i++) { |
| 54 | ZipEntry* pNewEntry; |
| 55 | int padding = 0; |
| 56 | |
| 57 | pEntry = pZin->getEntryByIndex(i); |
| 58 | if (pEntry == NULL) { |
| 59 | fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | if (pEntry->isCompressed()) { |
| 64 | /* copy the entry without padding */ |
| 65 | //printf("--- %s: orig at %ld len=%ld (compressed)\n", |
| 66 | // pEntry->getFileName(), (long) pEntry->getFileOffset(), |
| 67 | // (long) pEntry->getUncompressedLen()); |
| 68 | |
Raph Levien | 093d04c | 2014-07-07 16:00:29 -0700 | [diff] [blame] | 69 | if (zopfli) { |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 70 | status = pZout->addRecompress(pZin, pEntry, &pNewEntry); |
Raph Levien | 093d04c | 2014-07-07 16:00:29 -0700 | [diff] [blame] | 71 | bias += pNewEntry->getCompressedLen() - pEntry->getCompressedLen(); |
| 72 | } else { |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 73 | status = pZout->add(pZin, pEntry, padding, &pNewEntry); |
Raph Levien | 093d04c | 2014-07-07 16:00:29 -0700 | [diff] [blame] | 74 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 75 | } else { |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 76 | const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry); |
| 77 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 78 | /* |
| 79 | * Copy the entry, adjusting as required. We assume that the |
| 80 | * file position in the new file will be equal to the file |
| 81 | * position in the original. |
| 82 | */ |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 83 | off_t newOffset = pEntry->getFileOffset() + bias; |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 84 | padding = (alignTo - (newOffset % alignTo)) % alignTo; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 85 | |
| 86 | //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n", |
| 87 | // pEntry->getFileName(), (long) pEntry->getFileOffset(), |
| 88 | // bias, (long) pEntry->getUncompressedLen(), padding); |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 89 | status = pZout->add(pZin, pEntry, padding, &pNewEntry); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 92 | if (status != OK) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 93 | return 1; |
| 94 | bias += padding; |
| 95 | //printf(" added '%s' at %ld (pad=%d)\n", |
| 96 | // pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(), |
| 97 | // padding); |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Process a file. We open the input and output files, failing if the |
| 105 | * output file exists and "force" wasn't specified. |
| 106 | */ |
Fabien Sanglard | d4f71a9 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 107 | int process(const char* inFileName, const char* outFileName, |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 108 | int alignment, bool force, bool zopfli, bool pageAlignSharedLibs) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 109 | { |
| 110 | ZipFile zin, zout; |
| 111 | |
| 112 | //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n", |
| 113 | // alignment, inFileName, outFileName, force); |
| 114 | |
| 115 | /* this mode isn't supported -- do a trivial check */ |
| 116 | if (strcmp(inFileName, outFileName) == 0) { |
| 117 | fprintf(stderr, "Input and output can't be same file\n"); |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | /* don't overwrite existing unless given permission */ |
| 122 | if (!force && access(outFileName, F_OK) == 0) { |
| 123 | fprintf(stderr, "Output file '%s' exists\n", outFileName); |
| 124 | return 1; |
| 125 | } |
| 126 | |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 127 | if (zin.open(inFileName, ZipFile::kOpenReadOnly) != OK) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 128 | fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName); |
| 129 | return 1; |
| 130 | } |
| 131 | if (zout.open(outFileName, |
| 132 | ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate) |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 133 | != OK) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 134 | { |
Jingwen Owen Ou | 3032139 | 2012-08-17 16:21:11 -0700 | [diff] [blame] | 135 | 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] | 136 | return 1; |
| 137 | } |
| 138 | |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 139 | int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 140 | if (result != 0) { |
| 141 | printf("zipalign: failed rewriting '%s' to '%s'\n", |
| 142 | inFileName, outFileName); |
| 143 | } |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Verify the alignment of a zip archive. |
| 149 | */ |
Fabien Sanglard | d4f71a9 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 150 | int verify(const char* fileName, int alignment, bool verbose, |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 151 | bool pageAlignSharedLibs) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 152 | { |
| 153 | ZipFile zipFile; |
| 154 | bool foundBad = false; |
| 155 | |
| 156 | if (verbose) |
| 157 | printf("Verifying alignment of %s (%d)...\n", fileName, alignment); |
| 158 | |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 159 | if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != OK) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 160 | fprintf(stderr, "Unable to open '%s' for verification\n", fileName); |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | int numEntries = zipFile.getNumEntries(); |
| 165 | ZipEntry* pEntry; |
| 166 | |
| 167 | for (int i = 0; i < numEntries; i++) { |
| 168 | pEntry = zipFile.getEntryByIndex(i); |
| 169 | if (pEntry->isCompressed()) { |
| 170 | if (verbose) { |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 171 | printf("%8jd %s (OK - compressed)\n", |
| 172 | (intmax_t) pEntry->getFileOffset(), pEntry->getFileName()); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 173 | } |
| 174 | } else { |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 175 | off_t offset = pEntry->getFileOffset(); |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 176 | const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry); |
| 177 | if ((offset % alignTo) != 0) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 178 | if (verbose) { |
Chih-Hung Hsieh | 9b2bf2d | 2018-08-11 11:33:43 -0700 | [diff] [blame] | 179 | printf("%8jd %s (BAD - %jd)\n", |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 180 | (intmax_t) offset, pEntry->getFileName(), |
Chih-Hung Hsieh | 9b2bf2d | 2018-08-11 11:33:43 -0700 | [diff] [blame] | 181 | (intmax_t) (offset % alignTo)); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 182 | } |
| 183 | foundBad = true; |
| 184 | } else { |
| 185 | if (verbose) { |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 186 | printf("%8jd %s (OK)\n", |
| 187 | (intmax_t) offset, pEntry->getFileName()); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (verbose) |
| 194 | printf("Verification %s\n", foundBad ? "FAILED" : "succesful"); |
| 195 | |
| 196 | return foundBad ? 1 : 0; |
| 197 | } |
| 198 | |
Fabien Sanglard | d4f71a9 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 199 | } // namespace android |