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 | |
Fabien Sanglard | 8163cfa | 2022-10-10 22:19:47 +0000 | [diff] [blame^] | 25 | // An entry is considered a directory if it has a stored size of zero |
| 26 | // and it ends with '/' or '\' character. |
| 27 | static bool isDirectory(ZipEntry* entry) { |
| 28 | if (entry->getUncompressedLen() != 0) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | const char* name = entry->getFileName(); |
| 33 | size_t nameLength = strlen(name); |
| 34 | char lastChar = name[nameLength-1]; |
| 35 | return lastChar == '/' || lastChar == '\\'; |
| 36 | } |
| 37 | |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 38 | static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment, |
| 39 | ZipEntry* pEntry) { |
| 40 | |
Narayan Kamath | e0b8d19 | 2015-02-26 17:57:55 +0000 | [diff] [blame] | 41 | static const int kPageAlignment = 4096; |
| 42 | |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 43 | if (!pageAlignSharedLibs) { |
| 44 | return defaultAlignment; |
| 45 | } |
| 46 | |
| 47 | const char* ext = strrchr(pEntry->getFileName(), '.'); |
| 48 | if (ext && strcmp(ext, ".so") == 0) { |
| 49 | return kPageAlignment; |
| 50 | } |
| 51 | |
| 52 | return defaultAlignment; |
| 53 | } |
| 54 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 55 | /* |
| 56 | * Copy all entries from "pZin" to "pZout", aligning as needed. |
| 57 | */ |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 58 | static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli, |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 59 | bool pageAlignSharedLibs) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 60 | { |
| 61 | int numEntries = pZin->getNumEntries(); |
| 62 | ZipEntry* pEntry; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 63 | status_t status; |
| 64 | |
| 65 | for (int i = 0; i < numEntries; i++) { |
| 66 | ZipEntry* pNewEntry; |
| 67 | int padding = 0; |
| 68 | |
| 69 | pEntry = pZin->getEntryByIndex(i); |
| 70 | if (pEntry == NULL) { |
| 71 | fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i); |
| 72 | return 1; |
| 73 | } |
| 74 | |
Fabien Sanglard | 8163cfa | 2022-10-10 22:19:47 +0000 | [diff] [blame^] | 75 | if (pEntry->isCompressed() || isDirectory(pEntry)) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 76 | /* copy the entry without padding */ |
| 77 | //printf("--- %s: orig at %ld len=%ld (compressed)\n", |
| 78 | // pEntry->getFileName(), (long) pEntry->getFileOffset(), |
| 79 | // (long) pEntry->getUncompressedLen()); |
| 80 | |
Raph Levien | 093d04c | 2014-07-07 16:00:29 -0700 | [diff] [blame] | 81 | if (zopfli) { |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 82 | status = pZout->addRecompress(pZin, pEntry, &pNewEntry); |
Raph Levien | 093d04c | 2014-07-07 16:00:29 -0700 | [diff] [blame] | 83 | } else { |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 84 | status = pZout->add(pZin, pEntry, padding, &pNewEntry); |
Raph Levien | 093d04c | 2014-07-07 16:00:29 -0700 | [diff] [blame] | 85 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 86 | } else { |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 87 | const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry); |
| 88 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 89 | //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n", |
| 90 | // pEntry->getFileName(), (long) pEntry->getFileOffset(), |
| 91 | // bias, (long) pEntry->getUncompressedLen(), padding); |
Fabien Sanglard | a720635 | 2020-10-20 15:47:10 -0700 | [diff] [blame] | 92 | status = pZout->add(pZin, pEntry, alignTo, &pNewEntry); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 95 | if (status != OK) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 96 | return 1; |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 97 | //printf(" added '%s' at %ld (pad=%d)\n", |
| 98 | // pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(), |
| 99 | // padding); |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Process a file. We open the input and output files, failing if the |
| 107 | * output file exists and "force" wasn't specified. |
| 108 | */ |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 109 | int process(const char* inFileName, const char* outFileName, |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 110 | int alignment, bool force, bool zopfli, bool pageAlignSharedLibs) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 111 | { |
| 112 | ZipFile zin, zout; |
| 113 | |
| 114 | //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n", |
| 115 | // alignment, inFileName, outFileName, force); |
| 116 | |
| 117 | /* this mode isn't supported -- do a trivial check */ |
| 118 | if (strcmp(inFileName, outFileName) == 0) { |
| 119 | fprintf(stderr, "Input and output can't be same file\n"); |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | /* don't overwrite existing unless given permission */ |
| 124 | if (!force && access(outFileName, F_OK) == 0) { |
| 125 | fprintf(stderr, "Output file '%s' exists\n", outFileName); |
| 126 | return 1; |
| 127 | } |
| 128 | |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 129 | if (zin.open(inFileName, ZipFile::kOpenReadOnly) != OK) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName); |
| 131 | return 1; |
| 132 | } |
| 133 | if (zout.open(outFileName, |
| 134 | ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate) |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 135 | != OK) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 136 | { |
Jingwen Owen Ou | 3032139 | 2012-08-17 16:21:11 -0700 | [diff] [blame] | 137 | 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] | 138 | return 1; |
| 139 | } |
| 140 | |
Dan Willemsen | b589ae4 | 2015-10-29 21:26:18 +0000 | [diff] [blame] | 141 | int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | */ |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 152 | int verify(const char* fileName, int alignment, bool verbose, |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 153 | bool pageAlignSharedLibs) |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 154 | { |
| 155 | ZipFile zipFile; |
| 156 | bool foundBad = false; |
| 157 | |
| 158 | if (verbose) |
| 159 | printf("Verifying alignment of %s (%d)...\n", fileName, alignment); |
| 160 | |
Elliott Hughes | ad7d562 | 2018-10-08 11:19:28 -0700 | [diff] [blame] | 161 | if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != OK) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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) { |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 173 | printf("%8jd %s (OK - compressed)\n", |
| 174 | (intmax_t) pEntry->getFileOffset(), pEntry->getFileName()); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 175 | } |
Fabien Sanglard | 8163cfa | 2022-10-10 22:19:47 +0000 | [diff] [blame^] | 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 { |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 183 | off_t offset = pEntry->getFileOffset(); |
Dmitriy Ivanov | 13e5965 | 2014-07-23 15:27:21 -0700 | [diff] [blame] | 184 | const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry); |
| 185 | if ((offset % alignTo) != 0) { |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 186 | if (verbose) { |
Chih-Hung Hsieh | 9b2bf2d | 2018-08-11 11:33:43 -0700 | [diff] [blame] | 187 | printf("%8jd %s (BAD - %jd)\n", |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 188 | (intmax_t) offset, pEntry->getFileName(), |
Chih-Hung Hsieh | 9b2bf2d | 2018-08-11 11:33:43 -0700 | [diff] [blame] | 189 | (intmax_t) (offset % alignTo)); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 190 | } |
| 191 | foundBad = true; |
| 192 | } else { |
| 193 | if (verbose) { |
Chih-Hung Hsieh | 0c0d928 | 2018-08-10 15:14:26 -0700 | [diff] [blame] | 194 | printf("%8jd %s (OK)\n", |
| 195 | (intmax_t) offset, pEntry->getFileName()); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (verbose) |
| 202 | printf("Verification %s\n", foundBad ? "FAILED" : "succesful"); |
| 203 | |
| 204 | return foundBad ? 1 : 0; |
| 205 | } |
| 206 | |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 207 | } // namespace android |