diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:32:55 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:32:55 -0800 |
commit | dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0 (patch) | |
tree | 2ba8d1a0846d69b18f623515e8d9b5d9fe38b590 /libzipfile | |
parent | e54eebbf1a908d65ee8cf80bab62821c05666d70 (diff) | |
download | system_core-dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0.tar.gz system_core-dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0.tar.bz2 system_core-dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0.zip |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'libzipfile')
-rw-r--r-- | libzipfile/Android.mk | 48 | ||||
-rw-r--r-- | libzipfile/MODULE_LICENSE_APACHE2 | 0 | ||||
-rw-r--r-- | libzipfile/NOTICE | 190 | ||||
-rw-r--r-- | libzipfile/centraldir.c | 256 | ||||
-rw-r--r-- | libzipfile/private.h | 45 | ||||
-rw-r--r-- | libzipfile/test_zipfile.c | 92 | ||||
-rw-r--r-- | libzipfile/zipfile.c | 160 |
7 files changed, 791 insertions, 0 deletions
diff --git a/libzipfile/Android.mk b/libzipfile/Android.mk new file mode 100644 index 000000000..d2d758cc2 --- /dev/null +++ b/libzipfile/Android.mk @@ -0,0 +1,48 @@ +LOCAL_PATH:= $(call my-dir) + +# build host static library +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + centraldir.c \ + zipfile.c + +LOCAL_STATIC_LIBRARIES := \ + libunz + +LOCAL_MODULE:= libzipfile + +LOCAL_C_INCLUDES += external/zlib + +include $(BUILD_HOST_STATIC_LIBRARY) + +# build device static library +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + centraldir.c \ + zipfile.c + +LOCAL_STATIC_LIBRARIES := \ + libunz + +LOCAL_MODULE:= libzipfile + +LOCAL_C_INCLUDES += external/zlib + +include $(BUILD_STATIC_LIBRARY) + + +# build test_zipfile +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + test_zipfile.c + +LOCAL_STATIC_LIBRARIES := libzipfile libunz + +LOCAL_MODULE := test_zipfile + +LOCAL_C_INCLUDES += external/zlib + +include $(BUILD_HOST_EXECUTABLE) diff --git a/libzipfile/MODULE_LICENSE_APACHE2 b/libzipfile/MODULE_LICENSE_APACHE2 new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/libzipfile/MODULE_LICENSE_APACHE2 diff --git a/libzipfile/NOTICE b/libzipfile/NOTICE new file mode 100644 index 000000000..c5b1efa7a --- /dev/null +++ b/libzipfile/NOTICE @@ -0,0 +1,190 @@ + + Copyright (c) 2005-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. + + 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. + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + diff --git a/libzipfile/centraldir.c b/libzipfile/centraldir.c new file mode 100644 index 000000000..4387cebaa --- /dev/null +++ b/libzipfile/centraldir.c @@ -0,0 +1,256 @@ +#include "private.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+enum {
+ // finding the directory
+ CD_SIGNATURE = 0x06054b50,
+ EOCD_LEN = 22, // EndOfCentralDir len, excl. comment
+ MAX_COMMENT_LEN = 65535,
+ MAX_EOCD_SEARCH = MAX_COMMENT_LEN + EOCD_LEN,
+
+ // central directory entries
+ ENTRY_SIGNATURE = 0x02014b50,
+ ENTRY_LEN = 46, // CentralDirEnt len, excl. var fields
+
+ // local file header
+ LFH_SIZE = 30,
+};
+
+unsigned int
+read_le_int(const unsigned char* buf)
+{
+ return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
+}
+
+unsigned int
+read_le_short(const unsigned char* buf)
+{
+ return buf[0] | (buf[1] << 8);
+}
+
+static int
+read_central_dir_values(Zipfile* file, const unsigned char* buf, int len)
+{
+ if (len < EOCD_LEN) {
+ // looks like ZIP file got truncated
+ fprintf(stderr, " Zip EOCD: expected >= %d bytes, found %d\n",
+ EOCD_LEN, len);
+ return -1;
+ }
+
+ file->disknum = read_le_short(&buf[0x04]);
+ file->diskWithCentralDir = read_le_short(&buf[0x06]);
+ file->entryCount = read_le_short(&buf[0x08]);
+ file->totalEntryCount = read_le_short(&buf[0x0a]);
+ file->centralDirSize = read_le_int(&buf[0x0c]);
+ file->centralDirOffest = read_le_int(&buf[0x10]);
+ file->commentLen = read_le_short(&buf[0x14]);
+
+ if (file->commentLen > 0) {
+ if (EOCD_LEN + file->commentLen > len) {
+ fprintf(stderr, "EOCD(%d) + comment(%d) exceeds len (%d)\n",
+ EOCD_LEN, file->commentLen, len);
+ return -1;
+ }
+ file->comment = buf + EOCD_LEN;
+ }
+
+ return 0;
+}
+
+static int
+read_central_directory_entry(Zipfile* file, Zipentry* entry,
+ const unsigned char** buf, ssize_t* len)
+{
+ const unsigned char* p;
+
+ unsigned short versionMadeBy;
+ unsigned short versionToExtract;
+ unsigned short gpBitFlag;
+ unsigned short compressionMethod;
+ unsigned short lastModFileTime;
+ unsigned short lastModFileDate;
+ unsigned long crc32;
+ unsigned long compressedSize;
+ unsigned long uncompressedSize;
+ unsigned short extraFieldLength;
+ unsigned short fileCommentLength;
+ unsigned short diskNumberStart;
+ unsigned short internalAttrs;
+ unsigned long externalAttrs;
+ unsigned long localHeaderRelOffset;
+ const unsigned char* extraField;
+ const unsigned char* fileComment;
+ unsigned int dataOffset;
+ unsigned short lfhExtraFieldSize;
+
+
+ p = *buf;
+
+ if (*len < ENTRY_LEN) {
+ fprintf(stderr, "cde entry not large enough\n");
+ return -1;
+ }
+
+ if (read_le_int(&p[0x00]) != ENTRY_SIGNATURE) {
+ fprintf(stderr, "Whoops: didn't find expected signature\n");
+ return -1;
+ }
+
+ versionMadeBy = read_le_short(&p[0x04]);
+ versionToExtract = read_le_short(&p[0x06]);
+ gpBitFlag = read_le_short(&p[0x08]);
+ entry->compressionMethod = read_le_short(&p[0x0a]);
+ lastModFileTime = read_le_short(&p[0x0c]);
+ lastModFileDate = read_le_short(&p[0x0e]);
+ crc32 = read_le_int(&p[0x10]);
+ compressedSize = read_le_int(&p[0x14]);
+ entry->uncompressedSize = read_le_int(&p[0x18]);
+ entry->fileNameLength = read_le_short(&p[0x1c]);
+ extraFieldLength = read_le_short(&p[0x1e]);
+ fileCommentLength = read_le_short(&p[0x20]);
+ diskNumberStart = read_le_short(&p[0x22]);
+ internalAttrs = read_le_short(&p[0x24]);
+ externalAttrs = read_le_int(&p[0x26]);
+ localHeaderRelOffset = read_le_int(&p[0x2a]);
+
+ p += ENTRY_LEN;
+
+ // filename
+ if (entry->fileNameLength != 0) {
+ entry->fileName = p;
+ } else {
+ entry->fileName = NULL;
+ }
+ p += entry->fileNameLength;
+
+ // extra field
+ if (extraFieldLength != 0) {
+ extraField = p;
+ } else {
+ extraField = NULL;
+ }
+ p += extraFieldLength;
+
+ // comment, if any
+ if (fileCommentLength != 0) {
+ fileComment = p;
+ } else {
+ fileComment = NULL;
+ }
+ p += fileCommentLength;
+
+ *buf = p;
+
+ // the size of the extraField in the central dir is how much data there is,
+ // but the one in the local file header also contains some padding.
+ p = file->buf + localHeaderRelOffset;
+ extraFieldLength = read_le_short(&p[0x1c]);
+
+ dataOffset = localHeaderRelOffset + LFH_SIZE
+ + entry->fileNameLength + extraFieldLength;
+ entry->data = file->buf + dataOffset;
+#if 0
+ printf("file->buf=%p entry->data=%p dataOffset=%x localHeaderRelOffset=%d "
+ "entry->fileNameLength=%d extraFieldLength=%d\n",
+ file->buf, entry->data, dataOffset, localHeaderRelOffset,
+ entry->fileNameLength, extraFieldLength);
+#endif
+ return 0;
+}
+
+/*
+ * Find the central directory and read the contents.
+ *
+ * The fun thing about ZIP archives is that they may or may not be
+ * readable from start to end. In some cases, notably for archives
+ * that were written to stdout, the only length information is in the
+ * central directory at the end of the file.
+ *
+ * Of course, the central directory can be followed by a variable-length
+ * comment field, so we have to scan through it backwards. The comment
+ * is at most 64K, plus we have 18 bytes for the end-of-central-dir stuff
+ * itself, plus apparently sometimes people throw random junk on the end
+ * just for the fun of it.
+ *
+ * This is all a little wobbly. If the wrong value ends up in the EOCD
+ * area, we're hosed. This appears to be the way that everbody handles
+ * it though, so we're in pretty good company if this fails.
+ */
+int
+read_central_dir(Zipfile *file)
+{
+ int err;
+
+ const unsigned char* buf = file->buf;
+ ssize_t bufsize = file->bufsize;
+ const unsigned char* eocd;
+ const unsigned char* p;
+ const unsigned char* start;
+ ssize_t len;
+ int i;
+
+ // too small to be a ZIP archive?
+ if (bufsize < EOCD_LEN) {
+ fprintf(stderr, "Length is %d -- too small\n", bufsize);
+ goto bail;
+ }
+
+ // find the end-of-central-dir magic
+ if (bufsize > MAX_EOCD_SEARCH) {
+ start = buf + bufsize - MAX_EOCD_SEARCH;
+ } else {
+ start = buf;
+ }
+ p = buf + bufsize - 4;
+ while (p >= start) {
+ if (*p == 0x50 && read_le_int(p) == CD_SIGNATURE) {
+ eocd = p;
+ break;
+ }
+ p--;
+ }
+ if (p < start) {
+ fprintf(stderr, "EOCD not found, not Zip\n");
+ goto bail;
+ }
+
+ // extract eocd values
+ err = read_central_dir_values(file, eocd, (buf+bufsize)-eocd);
+ if (err != 0) {
+ goto bail;
+ }
+
+ if (file->disknum != 0
+ || file->diskWithCentralDir != 0
+ || file->entryCount != file->totalEntryCount) {
+ fprintf(stderr, "Archive spanning not supported\n");
+ goto bail;
+ }
+
+ // Loop through and read the central dir entries.
+ p = buf + file->centralDirOffest;
+ len = (buf+bufsize)-p;
+ for (i=0; i < file->totalEntryCount; i++) {
+ Zipentry* entry = malloc(sizeof(Zipentry));
+ memset(entry, sizeof(Zipentry), 0);
+
+ err = read_central_directory_entry(file, entry, &p, &len);
+ if (err != 0) {
+ fprintf(stderr, "read_central_directory_entry failed\n");
+ free(entry);
+ goto bail;
+ }
+
+ // add it to our list
+ entry->next = file->entries;
+ file->entries = entry;
+ }
+
+ return 0;
+bail:
+ return -1;
+}
+
diff --git a/libzipfile/private.h b/libzipfile/private.h new file mode 100644 index 000000000..06f788dd6 --- /dev/null +++ b/libzipfile/private.h @@ -0,0 +1,45 @@ +#ifndef PRIVATE_H
+#define PRIVATE_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+typedef struct Zipentry {
+ unsigned long fileNameLength;
+ const unsigned char* fileName;
+ unsigned short compressionMethod;
+ unsigned int uncompressedSize;
+ unsigned int compressedSize;
+ const unsigned char* data;
+
+ struct Zipentry* next;
+} Zipentry;
+
+typedef struct Zipfile
+{
+ const unsigned char *buf;
+ ssize_t bufsize;
+
+ // Central directory
+ unsigned short disknum; //mDiskNumber;
+ unsigned short diskWithCentralDir; //mDiskWithCentralDir;
+ unsigned short entryCount; //mNumEntries;
+ unsigned short totalEntryCount; //mTotalNumEntries;
+ unsigned int centralDirSize; //mCentralDirSize;
+ unsigned int centralDirOffest; // offset from first disk //mCentralDirOffset;
+ unsigned short commentLen; //mCommentLen;
+ const unsigned char* comment; //mComment;
+
+ Zipentry* entries;
+} Zipfile;
+
+int read_central_dir(Zipfile* file);
+
+unsigned int read_le_int(const unsigned char* buf);
+unsigned int read_le_short(const unsigned char* buf);
+
+#endif // PRIVATE_H
+
diff --git a/libzipfile/test_zipfile.c b/libzipfile/test_zipfile.c new file mode 100644 index 000000000..40840ec20 --- /dev/null +++ b/libzipfile/test_zipfile.c @@ -0,0 +1,92 @@ +#include <zipfile/zipfile.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void dump_zipfile(FILE* to, zipfile_t file);
+
+int
+main(int argc, char** argv)
+{
+ FILE* f;
+ size_t size, unsize;
+ void* buf;
+ void* scratch;
+ zipfile_t zip;
+ zipentry_t entry;
+ int err;
+ enum { HUH, LIST, UNZIP } what = HUH;
+
+ if (strcmp(argv[2], "-l") == 0 && argc == 3) {
+ what = LIST;
+ }
+ else if (strcmp(argv[2], "-u") == 0 && argc == 5) {
+ what = UNZIP;
+ }
+ else {
+ fprintf(stderr, "usage: test_zipfile ZIPFILE -l\n"
+ " lists the files in the zipfile\n"
+ " test_zipfile ZIPFILE -u FILENAME SAVETO\n"
+ " saves FILENAME from the zip file into SAVETO\n");
+ return 1;
+ }
+
+ f = fopen(argv[1], "r");
+ if (f == NULL) {
+ fprintf(stderr, "couldn't open %s\n", argv[1]);
+ return 1;
+ }
+
+ fseek(f, 0, SEEK_END);
+ size = ftell(f);
+ rewind(f);
+
+ buf = malloc(size);
+ fread(buf, 1, size, f);
+
+ zip = init_zipfile(buf, size);
+ if (zip == NULL) {
+ fprintf(stderr, "inti_zipfile failed\n");
+ return 1;
+ }
+
+ fclose(f);
+
+
+ switch (what)
+ {
+ case LIST:
+ dump_zipfile(stdout, zip);
+ break;
+ case UNZIP:
+ entry = lookup_zipentry(zip, argv[3]);
+ if (entry == NULL) {
+ fprintf(stderr, "zip file '%s' does not contain file '%s'\n",
+ argv[1], argv[1]);
+ return 1;
+ }
+ f = fopen(argv[4], "w");
+ if (f == NULL) {
+ fprintf(stderr, "can't open file for writing '%s'\n", argv[4]);
+ return 1;
+ }
+ unsize = get_zipentry_size(entry);
+ size = unsize * 1.001;
+ scratch = malloc(size);
+ printf("scratch=%p\n", scratch);
+ err = decompress_zipentry(entry, scratch, size);
+ if (err != 0) {
+ fprintf(stderr, "error decompressing file\n");
+ return 1;
+ }
+ fwrite(scratch, unsize, 1, f);
+ free(scratch);
+ fclose(f);
+ break;
+ }
+
+ free(buf);
+
+ return 0;
+}
+
diff --git a/libzipfile/zipfile.c b/libzipfile/zipfile.c new file mode 100644 index 000000000..b52d02df7 --- /dev/null +++ b/libzipfile/zipfile.c @@ -0,0 +1,160 @@ +#include <zipfile/zipfile.h> + +#include "private.h" +#include <stdlib.h> +#include <string.h> +#include <zlib.h> +#define DEF_MEM_LEVEL 8 // normally in zutil.h? + +zipfile_t +init_zipfile(const void* data, size_t size) +{ + int err; + + Zipfile *file = malloc(sizeof(Zipfile)); + if (file == NULL) return NULL; + memset(file, 0, sizeof(Zipfile)); + file->buf = data; + file->bufsize = size; + + err = read_central_dir(file); + if (err != 0) goto fail; + + return file; +fail: + free(file); + return NULL; +} + +void +release_zipfile(zipfile_t f) +{ + Zipfile* file = (Zipfile*)f; + Zipentry* entry = file->entries; + while (entry) { + Zipentry* next = entry->next; + free(entry); + entry = next; + } + free(file); +} + +zipentry_t +lookup_zipentry(zipfile_t f, const char* entryName) +{ + Zipfile* file = (Zipfile*)f; + Zipentry* entry = file->entries; + while (entry) { + if (0 == memcmp(entryName, entry->fileName, entry->fileNameLength)) { + return entry; + } + entry = entry->next; + } + return NULL; +} + +size_t +get_zipentry_size(zipentry_t entry) +{ + return ((Zipentry*)entry)->uncompressedSize; +} + +char* +get_zipentry_name(zipentry_t entry) +{ + Zipentry* e = (Zipentry*)entry; + int l = e->fileNameLength; + char* s = malloc(l+1); + memcpy(s, e->fileName, l); + s[l] = '\0'; + return s; +} + +enum { + STORED = 0, + DEFLATED = 8 +}; + +static int +uninflate(unsigned char* out, int unlen, const unsigned char* in, int clen) +{ + z_stream zstream; + unsigned long crc; + int err = 0; + int zerr; + + memset(&zstream, 0, sizeof(zstream)); + zstream.zalloc = Z_NULL; + zstream.zfree = Z_NULL; + zstream.opaque = Z_NULL; + zstream.next_in = (void*)in; + zstream.avail_in = unlen; + zstream.next_out = (Bytef*) out; + zstream.avail_out = unlen; + zstream.data_type = Z_UNKNOWN; + + // Use the undocumented "negative window bits" feature to tell zlib + // that there's no zlib header waiting for it. + zerr = inflateInit2(&zstream, -MAX_WBITS); + if (zerr != Z_OK) { + return -1; + } + + // uncompress the data + zerr = inflate(&zstream, Z_FINISH); + if (zerr != Z_STREAM_END) { + fprintf(stderr, "zerr=%d Z_STREAM_END=%d total_out=%lu\n", zerr, Z_STREAM_END, + zstream.total_out); + err = -1; + } + + inflateEnd(&zstream); + return err; +} + +int +decompress_zipentry(zipentry_t e, void* buf, int bufsize) +{ + Zipentry* entry = (Zipentry*)e; + switch (entry->compressionMethod) + { + case STORED: + memcpy(buf, entry->data, entry->uncompressedSize); + return 0; + case DEFLATED: + return uninflate(buf, bufsize, entry->data, entry->compressedSize); + default: + return -1; + } +} + +void +dump_zipfile(FILE* to, zipfile_t file) +{ + Zipfile* zip = (Zipfile*)file; + Zipentry* entry = zip->entries; + int i; + + fprintf(to, "entryCount=%d\n", zip->entryCount); + for (i=0; i<zip->entryCount; i++) { + fprintf(to, " file \""); + fwrite(entry->fileName, entry->fileNameLength, 1, to); + fprintf(to, "\"\n"); + entry = entry->next; + } +} + +zipentry_t +iterate_zipfile(zipfile_t file, void** cookie) +{ + Zipentry* entry = (Zipentry*)*cookie; + if (entry == NULL) { + Zipfile* zip = (Zipfile*)file; + *cookie = zip->entries; + return *cookie; + } else { + entry = entry->next; + *cookie = entry; + return entry; + } +} |