diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
commit | b6c1cf6de79035f58b512f4400db458c8401379a (patch) | |
tree | 68979db37c85b499bc384e4ac337ed1424baab51 /tools/zipalign | |
download | build-b6c1cf6de79035f58b512f4400db458c8401379a.zip build-b6c1cf6de79035f58b512f4400db458c8401379a.tar.gz build-b6c1cf6de79035f58b512f4400db458c8401379a.tar.bz2 |
Initial Contribution
Diffstat (limited to 'tools/zipalign')
-rw-r--r-- | tools/zipalign/Android.mk | 35 | ||||
-rw-r--r-- | tools/zipalign/README.txt | 31 | ||||
-rw-r--r-- | tools/zipalign/ZipAlign.cpp | 253 |
3 files changed, 319 insertions, 0 deletions
diff --git a/tools/zipalign/Android.mk b/tools/zipalign/Android.mk new file mode 100644 index 0000000..e23b699 --- /dev/null +++ b/tools/zipalign/Android.mk @@ -0,0 +1,35 @@ +# +# Copyright 2008 The Android Open Source Project +# +# Zip alignment tool +# + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + ZipAlign.cpp + +LOCAL_C_INCLUDES += external/zlib + +LOCAL_STATIC_LIBRARIES := \ + libutils \ + libcutils + +LOCAL_LDLIBS := -lz + +ifeq ($(HOST_OS),linux) +LOCAL_LDLIBS += -lrt +endif + +# dunno if we need this, but some of the other tools include it +ifeq ($(HOST_OS),windows) +ifeq ($(strip $(USE_CYGWIN),),) +LOCAL_LDLIBS += -lws2_32 +endif +endif + +LOCAL_MODULE := zipalign + +include $(BUILD_HOST_EXECUTABLE) + diff --git a/tools/zipalign/README.txt b/tools/zipalign/README.txt new file mode 100644 index 0000000..a2e1a5e --- /dev/null +++ b/tools/zipalign/README.txt @@ -0,0 +1,31 @@ +zipalign -- zip archive alignment tool + +usage: zipalign [-f] [-v] <align> infile.zip outfile.zip + + -f : overwrite existing outfile.zip + -v : verbose output + <align> is in bytes, e.g. "4" provides 32-bit alignment + infile.zip is an existing Zip archive + outfile.zip will be created + + +The purpose of zipalign is to ensure that all uncompressed data starts +with a particular alignment relative to the start of the file. This +allows those portions to be accessed directly with mmap() even if they +contain binary data with alignment restrictions. + +Some data needs to be word-aligned for easy access, others might benefit +from being page-aligned. The adjustment is made by altering the size of +the "extra" field in the zip Local File Header sections. Existing data +in the "extra" fields may be altered by this process. + +Compressed data isn't very useful until it's uncompressed, so there's no +need to adjust its alignment. + +Alterations to the archive, such as renaming or deleting entries, will +potentially disrupt the alignment of the modified entry and all later +entries. Files added to an "aligned" archive will not be aligned. + +By default, zipalign will not overwrite an existing output file. With the +"-f" flag, an existing file will be overwritten. + diff --git a/tools/zipalign/ZipAlign.cpp b/tools/zipalign/ZipAlign.cpp new file mode 100644 index 0000000..9e3cb66 --- /dev/null +++ b/tools/zipalign/ZipAlign.cpp @@ -0,0 +1,253 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Zip alignment tool + */ +#include "utils/ZipFile.h" + +#include <stdlib.h> +#include <stdio.h> + +using namespace android; + +/* + * Show program usage. + */ +void usage(void) +{ + fprintf(stderr, "Zip alignment utility\n"); + fprintf(stderr, + "Usage: zipalign [-f] [-v] <align> infile.zip outfile.zip\n"); +} + +/* + * Copy all entries from "pZin" to "pZout", aligning as needed. + */ +static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment) +{ + int numEntries = pZin->getNumEntries(); + ZipEntry* pEntry; + int bias = 0; + status_t status; + + for (int i = 0; i < numEntries; i++) { + ZipEntry* pNewEntry; + int padding = 0; + + pEntry = pZin->getEntryByIndex(i); + if (pEntry == NULL) { + fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i); + return 1; + } + + if (pEntry->isCompressed()) { + /* copy the entry without padding */ + //printf("--- %s: orig at %ld len=%ld (compressed)\n", + // pEntry->getFileName(), (long) pEntry->getFileOffset(), + // (long) pEntry->getUncompressedLen()); + + } else { + /* + * Copy the entry, adjusting as required. We assume that the + * file position in the new file will be equal to the file + * position in the original. + */ + long newOffset = pEntry->getFileOffset() + bias; + padding = (alignment - (newOffset % alignment)) % alignment; + + //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n", + // pEntry->getFileName(), (long) pEntry->getFileOffset(), + // bias, (long) pEntry->getUncompressedLen(), padding); + } + + status = pZout->add(pZin, pEntry, padding, &pNewEntry); + if (status != NO_ERROR) + return 1; + bias += padding; + //printf(" added '%s' at %ld (pad=%d)\n", + // pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(), + // padding); + } + + return 0; +} + +/* + * Process a file. We open the input and output files, failing if the + * output file exists and "force" wasn't specified. + */ +static int process(const char* inFileName, const char* outFileName, + int alignment, bool force) +{ + ZipFile zin, zout; + + //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n", + // alignment, inFileName, outFileName, force); + + /* this mode isn't supported -- do a trivial check */ + if (strcmp(inFileName, outFileName) == 0) { + fprintf(stderr, "Input and output can't be same file\n"); + return 1; + } + + /* don't overwrite existing unless given permission */ + if (!force && access(outFileName, F_OK) == 0) { + fprintf(stderr, "Output file '%s' exists\n", outFileName); + return 1; + } + + if (zin.open(inFileName, ZipFile::kOpenReadOnly) != NO_ERROR) { + fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName); + return 1; + } + if (zout.open(outFileName, + ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate) + != NO_ERROR) + { + fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName); + return 1; + } + + int result = copyAndAlign(&zin, &zout, alignment); + if (result != 0) { + printf("zipalign: failed rewriting '%s' to '%s'\n", + inFileName, outFileName); + } + return result; +} + +/* + * Verify the alignment of a zip archive. + */ +static int verify(const char* fileName, int alignment, bool verbose) +{ + ZipFile zipFile; + bool foundBad = false; + + if (verbose) + printf("Verifying alignment of %s (%d)...\n", fileName, alignment); + + if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != NO_ERROR) { + fprintf(stderr, "Unable to open '%s' for verification\n", fileName); + return 1; + } + + int numEntries = zipFile.getNumEntries(); + ZipEntry* pEntry; + + for (int i = 0; i < numEntries; i++) { + pEntry = zipFile.getEntryByIndex(i); + if (pEntry->isCompressed()) { + if (verbose) { + printf("%8ld %s (OK - compressed)\n", + (long) pEntry->getFileOffset(), pEntry->getFileName()); + } + } else { + long offset = pEntry->getFileOffset(); + if ((offset % alignment) != 0) { + if (verbose) { + printf("%8ld %s (BAD - %ld)\n", + (long) offset, pEntry->getFileName(), + offset % alignment); + } + foundBad = true; + } else { + if (verbose) { + printf("%8ld %s (OK)\n", + (long) offset, pEntry->getFileName()); + } + } + } + } + + if (verbose) + printf("Verification %s\n", foundBad ? "FAILED" : "succesful"); + + return foundBad ? 1 : 0; +} + +/* + * Parse args. + */ +int main(int argc, char* const argv[]) +{ + bool wantUsage = false; + bool force = false; + bool verbose = false; + int result = 1; + int alignment; + char* endp; + + if (argc < 4) { + wantUsage = true; + goto bail; + } + + argc--; + argv++; + + while (argc && argv[0][0] == '-') { + const char* cp = argv[0] +1; + + while (*cp != '\0') { + switch (*cp) { + case 'f': + force = true; + break; + case 'v': + verbose = true; + break; + default: + fprintf(stderr, "ERROR: unknown flag -%c\n", *cp); + wantUsage = true; + goto bail; + } + + cp++; + } + + argc--; + argv++; + } + + if (argc != 3) { + wantUsage = true; + goto bail; + } + + alignment = strtol(argv[0], &endp, 10); + if (*endp != '\0' || alignment <= 0) { + fprintf(stderr, "Invalid value for alignment: %s\n", argv[0]); + wantUsage = true; + goto bail; + } + + /* create the new archive */ + result = process(argv[1], argv[2], alignment, force); + + /* trust, but verify */ + if (result == 0) + result = verify(argv[2], alignment, verbose); + +bail: + if (wantUsage) { + usage(); + result = 2; + } + + return result; +} + |