2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 // Misc zip/gzip utility functions.
21 #define LOG_TAG "ziputil"
23 #include <utils/Log.h>
24 #include <utils/ZipUtils.h>
25 #include <utils/ZipFileRO.h>
33 using namespace android;
36 * Utility function that expands zip/gzip "deflate" compressed data
39 * "fd" is an open file positioned at the start of the "deflate" data
40 * "buf" must hold at least "uncompressedLen" bytes.
42 /*static*/ bool ZipUtils::inflateToBuffer(int fd, void* buf,
43 long uncompressedLen, long compressedLen)
46 const unsigned long kReadBufSize = 32768;
47 unsigned char* readBuf = NULL;
50 unsigned long compRemaining;
52 assert(uncompressedLen >= 0);
53 assert(compressedLen >= 0);
55 readBuf = new unsigned char[kReadBufSize];
58 compRemaining = compressedLen;
61 * Initialize the zlib stream.
63 memset(&zstream, 0, sizeof(zstream));
64 zstream.zalloc = Z_NULL;
65 zstream.zfree = Z_NULL;
66 zstream.opaque = Z_NULL;
67 zstream.next_in = NULL;
69 zstream.next_out = (Bytef*) buf;
70 zstream.avail_out = uncompressedLen;
71 zstream.data_type = Z_UNKNOWN;
74 * Use the undocumented "negative window bits" feature to tell zlib
75 * that there's no zlib header waiting for it.
77 zerr = inflateInit2(&zstream, -MAX_WBITS);
79 if (zerr == Z_VERSION_ERROR) {
80 ALOGE("Installed zlib is not compatible with linked version (%s)\n",
83 ALOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
89 * Loop while we have data.
92 unsigned long getSize;
94 /* read as much as we can */
95 if (zstream.avail_in == 0) {
96 getSize = (compRemaining > kReadBufSize) ?
97 kReadBufSize : compRemaining;
98 ALOGV("+++ reading %ld bytes (%ld left)\n",
99 getSize, compRemaining);
101 int cc = read(fd, readBuf, getSize);
102 if (cc != (int) getSize) {
103 ALOGD("inflate read failed (%d vs %ld)\n",
108 compRemaining -= getSize;
110 zstream.next_in = readBuf;
111 zstream.avail_in = getSize;
114 /* uncompress the data */
115 zerr = inflate(&zstream, Z_NO_FLUSH);
116 if (zerr != Z_OK && zerr != Z_STREAM_END) {
117 ALOGD("zlib inflate call failed (zerr=%d)\n", zerr);
121 /* output buffer holds all, so no need to write the output */
122 } while (zerr == Z_OK);
124 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
126 if ((long) zstream.total_out != uncompressedLen) {
127 ALOGW("Size mismatch on inflated file (%ld vs %ld)\n",
128 zstream.total_out, uncompressedLen);
136 inflateEnd(&zstream); /* free up any allocated structures */
144 * Utility function that expands zip/gzip "deflate" compressed data
147 * (This is a clone of the previous function, but it takes a FILE* instead
148 * of an fd. We could pass fileno(fd) to the above, but we can run into
149 * trouble when "fp" has a different notion of what fd's file position is.)
151 * "fp" is an open file positioned at the start of the "deflate" data
152 * "buf" must hold at least "uncompressedLen" bytes.
154 /*static*/ bool ZipUtils::inflateToBuffer(FILE* fp, void* buf,
155 long uncompressedLen, long compressedLen)
158 const unsigned long kReadBufSize = 32768;
159 unsigned char* readBuf = NULL;
162 unsigned long compRemaining;
164 assert(uncompressedLen >= 0);
165 assert(compressedLen >= 0);
167 readBuf = new unsigned char[kReadBufSize];
170 compRemaining = compressedLen;
173 * Initialize the zlib stream.
175 memset(&zstream, 0, sizeof(zstream));
176 zstream.zalloc = Z_NULL;
177 zstream.zfree = Z_NULL;
178 zstream.opaque = Z_NULL;
179 zstream.next_in = NULL;
180 zstream.avail_in = 0;
181 zstream.next_out = (Bytef*) buf;
182 zstream.avail_out = uncompressedLen;
183 zstream.data_type = Z_UNKNOWN;
186 * Use the undocumented "negative window bits" feature to tell zlib
187 * that there's no zlib header waiting for it.
189 zerr = inflateInit2(&zstream, -MAX_WBITS);
191 if (zerr == Z_VERSION_ERROR) {
192 ALOGE("Installed zlib is not compatible with linked version (%s)\n",
195 ALOGE("Call to inflateInit2 failed (zerr=%d)\n", zerr);
201 * Loop while we have data.
204 unsigned long getSize;
206 /* read as much as we can */
207 if (zstream.avail_in == 0) {
208 getSize = (compRemaining > kReadBufSize) ?
209 kReadBufSize : compRemaining;
210 ALOGV("+++ reading %ld bytes (%ld left)\n",
211 getSize, compRemaining);
213 int cc = fread(readBuf, 1, getSize, fp);
214 if (cc != (int) getSize) {
215 ALOGD("inflate read failed (%d vs %ld)\n",
220 compRemaining -= getSize;
222 zstream.next_in = readBuf;
223 zstream.avail_in = getSize;
226 /* uncompress the data */
227 zerr = inflate(&zstream, Z_NO_FLUSH);
228 if (zerr != Z_OK && zerr != Z_STREAM_END) {
229 ALOGD("zlib inflate call failed (zerr=%d)\n", zerr);
233 /* output buffer holds all, so no need to write the output */
234 } while (zerr == Z_OK);
236 assert(zerr == Z_STREAM_END); /* other errors should've been caught */
238 if ((long) zstream.total_out != uncompressedLen) {
239 ALOGW("Size mismatch on inflated file (%ld vs %ld)\n",
240 zstream.total_out, uncompressedLen);
248 inflateEnd(&zstream); /* free up any allocated structures */
256 * Look at the contents of a gzip archive. We want to know where the
257 * data starts, and how long it will be after it is uncompressed.
259 * We expect to find the CRC and length as the last 8 bytes on the file.
260 * This is a pretty reasonable thing to expect for locally-compressed
261 * files, but there's a small chance that some extra padding got thrown
262 * on (the man page talks about compressed data written to tape). We
263 * don't currently deal with that here. If "gzip -l" whines, we're going
266 * On exit, "fp" is pointing at the start of the compressed data.
268 /*static*/ bool ZipUtils::examineGzip(FILE* fp, int* pCompressionMethod,
269 long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32)
283 if (ic != 0x1f || getc(fp) != 0x8b)
284 return false; // not gzip
288 /* quick sanity checks */
289 if (method == EOF || flags == EOF)
291 if (method != ZipFileRO::kCompressDeflated)
294 /* skip over 4 bytes of mod time, 1 byte XFL, 1 byte OS */
295 for (i = 0; i < 6; i++)
297 /* consume "extra" field, if present */
298 if ((flags & FEXTRA) != 0) {
302 len |= getc(fp) << 8;
303 while (len-- && getc(fp) != EOF)
306 /* consume filename, if present */
307 if ((flags & FNAME) != 0) {
310 } while (ic != 0 && ic != EOF);
312 /* consume comment, if present */
313 if ((flags & FCOMMENT) != 0) {
316 } while (ic != 0 && ic != EOF);
318 /* consume 16-bit header CRC, if present */
319 if ((flags & FHCRC) != 0) {
324 if (feof(fp) || ferror(fp))
327 /* seek to the end; CRC and length are in the last 8 bytes */
328 long curPosn = ftell(fp);
329 unsigned char buf[8];
330 fseek(fp, -8, SEEK_END);
331 *pCompressedLen = ftell(fp) - curPosn;
333 if (fread(buf, 1, 8, fp) != 8)
335 /* seek back to start of compressed data */
336 fseek(fp, curPosn, SEEK_SET);
338 *pCompressionMethod = method;
339 *pCRC32 = ZipFileRO::get4LE(&buf[0]);
340 *pUncompressedLen = ZipFileRO::get4LE(&buf[4]);