2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-09-29 19:55:32 +02:00
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* Zip alignment tool
|
|
|
|
*/
|
2009-06-05 23:55:48 +02:00
|
|
|
#include "ZipFile.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace android;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Show program usage.
|
|
|
|
*/
|
|
|
|
void usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Zip alignment utility\n");
|
2009-09-29 19:55:32 +02:00
|
|
|
fprintf(stderr, "Copyright (C) 2009 The Android Open Source Project\n\n");
|
2009-03-04 04:28:42 +01:00
|
|
|
fprintf(stderr,
|
2014-07-24 00:27:21 +02:00
|
|
|
"Usage: zipalign [-f] [-p] [-v] [-z] <align> infile.zip outfile.zip\n"
|
2015-02-26 18:57:55 +01:00
|
|
|
" zipalign -c [-v] <align> infile.zip\n\n" );
|
2009-09-29 19:55:32 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
" <align>: alignment in bytes, e.g. '4' provides 32-bit alignment\n");
|
|
|
|
fprintf(stderr, " -c: check alignment only (does not modify file)\n");
|
|
|
|
fprintf(stderr, " -f: overwrite existing outfile.zip\n");
|
2015-02-26 18:57:55 +01:00
|
|
|
fprintf(stderr, " -p: page align stored shared object files\n");
|
2009-09-29 19:55:32 +02:00
|
|
|
fprintf(stderr, " -v: verbose output\n");
|
2014-07-08 01:00:29 +02:00
|
|
|
fprintf(stderr, " -z: recompress using Zopfli\n");
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2014-07-24 00:27:21 +02:00
|
|
|
static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment,
|
|
|
|
ZipEntry* pEntry) {
|
|
|
|
|
2015-02-26 18:57:55 +01:00
|
|
|
static const int kPageAlignment = 4096;
|
|
|
|
|
2014-07-24 00:27:21 +02:00
|
|
|
if (!pageAlignSharedLibs) {
|
|
|
|
return defaultAlignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* ext = strrchr(pEntry->getFileName(), '.');
|
|
|
|
if (ext && strcmp(ext, ".so") == 0) {
|
|
|
|
return kPageAlignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultAlignment;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* Copy all entries from "pZin" to "pZout", aligning as needed.
|
|
|
|
*/
|
2014-07-24 00:27:21 +02:00
|
|
|
static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli,
|
2015-10-29 22:26:18 +01:00
|
|
|
bool pageAlignSharedLibs)
|
2009-03-04 04:28:42 +01:00
|
|
|
{
|
|
|
|
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());
|
|
|
|
|
2014-07-08 01:00:29 +02:00
|
|
|
if (zopfli) {
|
2015-10-29 22:26:18 +01:00
|
|
|
status = pZout->addRecompress(pZin, pEntry, &pNewEntry);
|
2014-07-08 01:00:29 +02:00
|
|
|
bias += pNewEntry->getCompressedLen() - pEntry->getCompressedLen();
|
|
|
|
} else {
|
2015-10-29 22:26:18 +01:00
|
|
|
status = pZout->add(pZin, pEntry, padding, &pNewEntry);
|
2014-07-08 01:00:29 +02:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
} else {
|
2014-07-24 00:27:21 +02:00
|
|
|
const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
2014-07-24 00:27:21 +02:00
|
|
|
padding = (alignTo - (newOffset % alignTo)) % alignTo;
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
//printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
|
|
|
|
// pEntry->getFileName(), (long) pEntry->getFileOffset(),
|
|
|
|
// bias, (long) pEntry->getUncompressedLen(), padding);
|
2015-10-29 22:26:18 +01:00
|
|
|
status = pZout->add(pZin, pEntry, padding, &pNewEntry);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2015-10-29 22:26:18 +01:00
|
|
|
int alignment, bool force, bool zopfli, bool pageAlignSharedLibs)
|
2009-03-04 04:28:42 +01:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2012-08-18 01:21:11 +02:00
|
|
|
fprintf(stderr, "Unable to open '%s' as zip archive\n", outFileName);
|
2009-03-04 04:28:42 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-29 22:26:18 +01:00
|
|
|
int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs);
|
2009-03-04 04:28:42 +01:00
|
|
|
if (result != 0) {
|
|
|
|
printf("zipalign: failed rewriting '%s' to '%s'\n",
|
|
|
|
inFileName, outFileName);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify the alignment of a zip archive.
|
|
|
|
*/
|
2014-07-24 00:27:21 +02:00
|
|
|
static int verify(const char* fileName, int alignment, bool verbose,
|
|
|
|
bool pageAlignSharedLibs)
|
2009-03-04 04:28:42 +01:00
|
|
|
{
|
|
|
|
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) {
|
2009-04-16 19:16:38 +02:00
|
|
|
printf("%8ld %s (OK - compressed)\n",
|
2009-03-04 04:28:42 +01:00
|
|
|
(long) pEntry->getFileOffset(), pEntry->getFileName());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
long offset = pEntry->getFileOffset();
|
2014-07-24 00:27:21 +02:00
|
|
|
const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
|
|
|
|
if ((offset % alignTo) != 0) {
|
2009-03-04 04:28:42 +01:00
|
|
|
if (verbose) {
|
2009-04-16 19:16:38 +02:00
|
|
|
printf("%8ld %s (BAD - %ld)\n",
|
2009-03-04 04:28:42 +01:00
|
|
|
(long) offset, pEntry->getFileName(),
|
2014-07-24 00:27:21 +02:00
|
|
|
offset % alignTo);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
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;
|
2009-04-16 19:16:38 +02:00
|
|
|
bool check = false;
|
2009-03-04 04:28:42 +01:00
|
|
|
bool force = false;
|
|
|
|
bool verbose = false;
|
2014-07-08 01:00:29 +02:00
|
|
|
bool zopfli = false;
|
2014-07-24 00:27:21 +02:00
|
|
|
bool pageAlignSharedLibs = false;
|
2009-03-04 04:28:42 +01:00
|
|
|
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) {
|
2009-04-16 19:16:38 +02:00
|
|
|
case 'c':
|
|
|
|
check = true;
|
|
|
|
break;
|
2009-03-04 04:28:42 +01:00
|
|
|
case 'f':
|
|
|
|
force = true;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
2014-07-08 01:00:29 +02:00
|
|
|
case 'z':
|
|
|
|
zopfli = true;
|
|
|
|
break;
|
2014-07-24 00:27:21 +02:00
|
|
|
case 'p':
|
|
|
|
pageAlignSharedLibs = true;
|
|
|
|
break;
|
2009-03-04 04:28:42 +01:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "ERROR: unknown flag -%c\n", *cp);
|
|
|
|
wantUsage = true;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
2009-04-16 19:16:38 +02:00
|
|
|
if (!((check && argc == 2) || (!check && argc == 3))) {
|
2009-03-04 04:28:42 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-04-16 19:16:38 +02:00
|
|
|
if (check) {
|
|
|
|
/* check existing archive for correct alignment */
|
2014-07-24 00:27:21 +02:00
|
|
|
result = verify(argv[1], alignment, verbose, pageAlignSharedLibs);
|
2009-04-16 19:16:38 +02:00
|
|
|
} else {
|
|
|
|
/* create the new archive */
|
2015-10-29 22:26:18 +01:00
|
|
|
result = process(argv[1], argv[2], alignment, force, zopfli, pageAlignSharedLibs);
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2009-04-16 19:16:38 +02:00
|
|
|
/* trust, but verify */
|
2014-07-24 00:27:21 +02:00
|
|
|
if (result == 0) {
|
|
|
|
result = verify(argv[2], alignment, verbose, pageAlignSharedLibs);
|
|
|
|
}
|
2009-04-16 19:16:38 +02:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
bail:
|
|
|
|
if (wantUsage) {
|
|
|
|
usage();
|
|
|
|
result = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|