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