Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include <algorithm> |
Kenny Root | f0cccdb | 2016-09-14 17:14:42 -0700 | [diff] [blame] | 22 | #include <memory> |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <ziparchive/zip_archive.h> |
| 27 | #include <ziparchive/zip_archive_stream_entry.h> |
| 28 | #include <ziparchive/zip_writer.h> |
| 29 | |
| 30 | static void usage() { |
| 31 | fprintf(stderr, "usage: bionic_tests_zipalign ALIGNMENT INPUT_ZIP_FILE OUTPUT_ZIP_FILE\n"); |
| 32 | fprintf(stderr, " ALIGNMENT:\n"); |
| 33 | fprintf(stderr, " The new alignment of all entries in the new zip file.\n"); |
| 34 | fprintf(stderr, " INPUT_ZIP_FILE:\n"); |
| 35 | fprintf(stderr, " The input zip file that will be read but left unmodified.\n"); |
| 36 | fprintf(stderr, " OUTPUT_ZIP_FILE:\n"); |
| 37 | fprintf(stderr, " The output zip file that will be created from the input file.\n"); |
| 38 | } |
| 39 | |
Elliott Hughes | 127a706 | 2019-05-22 18:50:53 -0700 | [diff] [blame] | 40 | using ZipData = std::pair<std::unique_ptr<ZipEntry>, std::string>; |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 41 | |
| 42 | static bool GetEntries(ZipArchiveHandle handle, std::vector<ZipData>* entries) { |
| 43 | void* cookie; |
Elliott Hughes | be6c641 | 2019-05-10 16:59:13 -0700 | [diff] [blame] | 44 | int32_t return_value = StartIteration(handle, &cookie); |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 45 | if (return_value != 0) { |
| 46 | fprintf(stderr, "Unable to iterate over entries: %s\n", ErrorCodeString(return_value)); |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | ZipEntry entry; |
Elliott Hughes | 127a706 | 2019-05-22 18:50:53 -0700 | [diff] [blame] | 51 | std::string name; |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 52 | while ((return_value = Next(cookie, &entry, &name)) == 0) { |
Elliott Hughes | 127a706 | 2019-05-22 18:50:53 -0700 | [diff] [blame] | 53 | entries->emplace_back(std::make_pair(std::make_unique<ZipEntry>(entry), name)); |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 54 | } |
| 55 | if (return_value != -1) { |
| 56 | fprintf(stderr, "Error while iterating over zip entries: %s\n", ErrorCodeString(return_value)); |
| 57 | } else { |
| 58 | // Sort by offset. |
| 59 | std::sort(entries->begin(), entries->end(), |
Kenny Root | f0cccdb | 2016-09-14 17:14:42 -0700 | [diff] [blame] | 60 | [](ZipData& a, ZipData& b) { return a.first->offset < b.first->offset; }); |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | EndIteration(cookie); |
| 64 | return return_value == -1; |
| 65 | } |
| 66 | |
| 67 | static bool CreateAlignedZip(ZipArchiveHandle& handle, FILE* zip_dst, uint32_t alignment) { |
| 68 | std::vector<ZipData> entries; |
| 69 | // We will not free the memory created in entries since the program |
| 70 | // terminates right after this function is called. |
| 71 | if (!GetEntries(handle, &entries)) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | ZipWriter writer(zip_dst); |
| 76 | |
| 77 | int32_t error; |
| 78 | for (auto& entry : entries) { |
Kenny Root | f0cccdb | 2016-09-14 17:14:42 -0700 | [diff] [blame] | 79 | ZipEntry* zip_entry = entry.first.get(); |
Elliott Hughes | 127a706 | 2019-05-22 18:50:53 -0700 | [diff] [blame] | 80 | std::string& zip_name = entry.second; |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 81 | |
| 82 | size_t flags = 0; |
| 83 | if ((zip_entry->method & kCompressDeflated) != 0) { |
| 84 | flags |= ZipWriter::kCompress; |
| 85 | } |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 86 | error = writer.StartAlignedEntry(zip_name.c_str(), flags, alignment); |
| 87 | if (error != 0) { |
| 88 | fprintf(stderr, "StartAlignedEntry failed: %s\n", ZipWriter::ErrorCodeString(error)); |
| 89 | return false; |
| 90 | } |
| 91 | std::unique_ptr<ZipArchiveStreamEntry> stream( |
| 92 | ZipArchiveStreamEntry::Create(handle, *zip_entry)); |
| 93 | const std::vector<uint8_t>* data; |
| 94 | while ((data = stream->Read()) != nullptr) { |
| 95 | error = writer.WriteBytes(data->data(), data->size()); |
| 96 | if (error != 0) { |
| 97 | fprintf(stderr, "WriteBytes failed: %s\n", ZipWriter::ErrorCodeString(error)); |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | if (!stream->Verify()) { |
| 102 | fprintf(stderr, "Failed to verify zip stream writer entry.\n"); |
| 103 | return false; |
| 104 | } |
| 105 | error = writer.FinishEntry(); |
| 106 | if (error != 0) { |
| 107 | fprintf(stderr, "FinishEntry failed: %s\n", ZipWriter::ErrorCodeString(error)); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | error = writer.Finish(); |
| 112 | if (error != 0) { |
| 113 | fprintf(stderr, "Finish failed: %s\n", ZipWriter::ErrorCodeString(error)); |
| 114 | return false; |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | int main(int argc, char* argv[]) { |
| 120 | if (argc != 4) { |
| 121 | usage(); |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | char* end; |
| 126 | unsigned long int alignment = strtoul(argv[1], &end, 10); |
| 127 | if ((alignment == ULONG_MAX && errno == ERANGE) || *end != '\0') { |
| 128 | fprintf(stderr, "ALIGNMENT value is not a valid number: %s\n", argv[1]); |
| 129 | usage(); |
| 130 | return 1; |
| 131 | } |
| 132 | if (((alignment - 1) & alignment) != 0) { |
| 133 | fprintf(stderr, "ALIGNMENT value is not a power of 2: %s\n", argv[1]); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | ZipArchiveHandle handle; |
| 138 | |
| 139 | int32_t return_value = OpenArchive(argv[2], &handle); |
| 140 | if (return_value != 0) { |
Yabin Cui | 722072d | 2016-03-21 17:10:12 -0700 | [diff] [blame] | 141 | CloseArchive(handle); |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 142 | fprintf(stderr, "Unable to open '%s': %s\n", argv[2], ErrorCodeString(return_value)); |
Yi Kong | 2d3122c | 2017-04-30 15:08:05 -0700 | [diff] [blame] | 143 | return 1; |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | FILE* zip_dst = fopen(argv[3], "we"); |
| 147 | if (zip_dst == nullptr) { |
| 148 | fprintf(stderr, "Unable to create '%s': %s\n", argv[3], strerror(errno)); |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | bool success = CreateAlignedZip(handle, zip_dst, static_cast<uint32_t>(alignment)); |
| 153 | |
| 154 | CloseArchive(handle); |
| 155 | fclose(zip_dst); |
| 156 | |
| 157 | return success ? 0 : 1; |
| 158 | } |