1 /*
2 * Copyright (C) 2007 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 //
18 // Misc zip/gzip utility functions.
19 //
20
21 #define LOG_TAG "ziputil"
22
23 #include "android-base/file.h"
24 #include <androidfw/ZipUtils.h>
25 #include <utils/Log.h>
26 #include <utils/Compat.h>
27 #include <ziparchive/zip_archive.h>
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32
33 #include <zlib.h>
34
35 using namespace android;
36
37 // TODO: This can go away once the only remaining usage in aapt goes away.
38 class FileReader : public zip_archive::Reader {
39 public:
FileReader(FILE * fp)40 explicit FileReader(FILE* fp) : Reader(), mFp(fp), mCurrentOffset(0) {
41 }
42
ReadAtOffset(uint8_t * buf,size_t len,off64_t offset) const43 bool ReadAtOffset(uint8_t* buf, size_t len, off64_t offset) const override {
44 // Data is usually requested sequentially, so this helps avoid pointless
45 // fseeks every time we perform a read. There's an impedence mismatch
46 // here because the original API was designed around pread and pwrite.
47 if (offset != mCurrentOffset) {
48 if (fseek(mFp, offset, SEEK_SET) != 0) {
49 return false;
50 }
51
52 mCurrentOffset = offset;
53 }
54
55 size_t read = fread(buf, 1, len, mFp);
56 if (read != len) {
57 return false;
58 }
59
60 mCurrentOffset += read;
61 return true;
62 }
63
64 private:
65 FILE* mFp;
66 mutable off64_t mCurrentOffset;
67 };
68
69 class FdReader : public zip_archive::Reader {
70 public:
FdReader(int fd)71 explicit FdReader(int fd) : mFd(fd) {
72 }
73
ReadAtOffset(uint8_t * buf,size_t len,off64_t offset) const74 bool ReadAtOffset(uint8_t* buf, size_t len, off64_t offset) const override {
75 return android::base::ReadFullyAtOffset(mFd, buf, len, offset);
76 }
77
78 private:
79 const int mFd;
80 };
81
82 class BufferReader : public zip_archive::Reader {
83 public:
BufferReader(incfs::map_ptr<void> input,size_t inputSize)84 BufferReader(incfs::map_ptr<void> input, size_t inputSize) : Reader(),
85 mInput(input.convert<uint8_t>()),
86 mInputSize(inputSize) {
87 }
88
ReadAtOffset(uint8_t * buf,size_t len,off64_t offset) const89 bool ReadAtOffset(uint8_t* buf, size_t len, off64_t offset) const override {
90 if (mInputSize < len || offset > mInputSize - len) {
91 return false;
92 }
93
94 const incfs::map_ptr<uint8_t> pos = mInput.offset(offset);
95 if (!pos.verify(len)) {
96 return false;
97 }
98
99 memcpy(buf, pos.unsafe_ptr(), len);
100 return true;
101 }
102
103 private:
104 const incfs::map_ptr<uint8_t> mInput;
105 const size_t mInputSize;
106 };
107
108 class BufferWriter : public zip_archive::Writer {
109 public:
BufferWriter(void * output,size_t outputSize)110 BufferWriter(void* output, size_t outputSize) : Writer(),
111 mOutput(reinterpret_cast<uint8_t*>(output)), mOutputSize(outputSize), mBytesWritten(0) {
112 }
113
Append(uint8_t * buf,size_t bufSize)114 bool Append(uint8_t* buf, size_t bufSize) override {
115 if (mBytesWritten + bufSize > mOutputSize) {
116 return false;
117 }
118
119 memcpy(mOutput + mBytesWritten, buf, bufSize);
120 mBytesWritten += bufSize;
121 return true;
122 }
123
124 private:
125 uint8_t* const mOutput;
126 const size_t mOutputSize;
127 size_t mBytesWritten;
128 };
129
inflateToBuffer(FILE * fp,void * buf,long uncompressedLen,long compressedLen)130 /*static*/ bool ZipUtils::inflateToBuffer(FILE* fp, void* buf,
131 long uncompressedLen, long compressedLen)
132 {
133 FileReader reader(fp);
134 BufferWriter writer(buf, uncompressedLen);
135 return (zip_archive::Inflate(reader, compressedLen, uncompressedLen, &writer, nullptr) == 0);
136 }
137
inflateToBuffer(int fd,void * buf,long uncompressedLen,long compressedLen)138 /*static*/ bool ZipUtils::inflateToBuffer(int fd, void* buf,
139 long uncompressedLen, long compressedLen)
140 {
141 FdReader reader(fd);
142 BufferWriter writer(buf, uncompressedLen);
143 return (zip_archive::Inflate(reader, compressedLen, uncompressedLen, &writer, nullptr) == 0);
144 }
145
inflateToBuffer(incfs::map_ptr<void> in,void * buf,long uncompressedLen,long compressedLen)146 /*static*/ bool ZipUtils::inflateToBuffer(incfs::map_ptr<void> in, void* buf,
147 long uncompressedLen, long compressedLen)
148 {
149 BufferReader reader(in, compressedLen);
150 BufferWriter writer(buf, uncompressedLen);
151 return (zip_archive::Inflate(reader, compressedLen, uncompressedLen, &writer, nullptr) == 0);
152 }
153
get4LE(const unsigned char * buf)154 static inline unsigned long get4LE(const unsigned char* buf) {
155 return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
156 }
157
158 /*
159 * Look at the contents of a gzip archive. We want to know where the
160 * data starts, and how long it will be after it is uncompressed.
161 *
162 * We expect to find the CRC and length as the last 8 bytes on the file.
163 * This is a pretty reasonable thing to expect for locally-compressed
164 * files, but there's a small chance that some extra padding got thrown
165 * on (the man page talks about compressed data written to tape). We
166 * don't currently deal with that here. If "gzip -l" whines, we're going
167 * to fail too.
168 *
169 * On exit, "fp" is pointing at the start of the compressed data.
170 */
examineGzip(FILE * fp,int * pCompressionMethod,long * pUncompressedLen,long * pCompressedLen,unsigned long * pCRC32)171 /*static*/ bool ZipUtils::examineGzip(FILE* fp, int* pCompressionMethod,
172 long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32)
173 {
174 enum { // flags
175 FTEXT = 0x01,
176 FHCRC = 0x02,
177 FEXTRA = 0x04,
178 FNAME = 0x08,
179 FCOMMENT = 0x10,
180 };
181 int ic;
182 int method, flags;
183 int i;
184
185 ic = getc(fp);
186 if (ic != 0x1f || getc(fp) != 0x8b)
187 return false; // not gzip
188 method = getc(fp);
189 flags = getc(fp);
190
191 /* quick sanity checks */
192 if (method == EOF || flags == EOF)
193 return false;
194 if (method != kCompressDeflated)
195 return false;
196
197 /* skip over 4 bytes of mod time, 1 byte XFL, 1 byte OS */
198 for (i = 0; i < 6; i++)
199 (void) getc(fp);
200 /* consume "extra" field, if present */
201 if ((flags & FEXTRA) != 0) {
202 int len;
203
204 len = getc(fp);
205 len |= getc(fp) << 8;
206 while (len-- && getc(fp) != EOF)
207 ;
208 }
209 /* consume filename, if present */
210 if ((flags & FNAME) != 0) {
211 do {
212 ic = getc(fp);
213 } while (ic != 0 && ic != EOF);
214 }
215 /* consume comment, if present */
216 if ((flags & FCOMMENT) != 0) {
217 do {
218 ic = getc(fp);
219 } while (ic != 0 && ic != EOF);
220 }
221 /* consume 16-bit header CRC, if present */
222 if ((flags & FHCRC) != 0) {
223 (void) getc(fp);
224 (void) getc(fp);
225 }
226
227 if (feof(fp) || ferror(fp))
228 return false;
229
230 /* seek to the end; CRC and length are in the last 8 bytes */
231 long curPosn = ftell(fp);
232 unsigned char buf[8];
233 fseek(fp, -8, SEEK_END);
234 *pCompressedLen = ftell(fp) - curPosn;
235
236 if (fread(buf, 1, 8, fp) != 8)
237 return false;
238 /* seek back to start of compressed data */
239 fseek(fp, curPosn, SEEK_SET);
240
241 *pCompressionMethod = method;
242 *pCRC32 = get4LE(&buf[0]);
243 *pUncompressedLen = get4LE(&buf[4]);
244
245 return true;
246 }
247