blob: f7f62c51a25b7826bb80e2f591f699d0f9fbba55 [file] [log] [blame]
Mathias Agopian1f5762e2013-05-06 20:20:34 -07001/*
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
Narayan Kamatha07e1222017-10-30 12:57:24 +000023#include "android-base/file.h"
Mathias Agopian1f5762e2013-05-06 20:20:34 -070024#include <androidfw/ZipUtils.h>
Mathias Agopian1f5762e2013-05-06 20:20:34 -070025#include <utils/Log.h>
26#include <utils/Compat.h>
Narayan Kamatha07e1222017-10-30 12:57:24 +000027#include <ziparchive/zip_archive.h>
Mathias Agopian1f5762e2013-05-06 20:20:34 -070028
29#include <stdlib.h>
30#include <string.h>
31#include <assert.h>
32
33#include <zlib.h>
34
35using namespace android;
36
Narayan Kamatha07e1222017-10-30 12:57:24 +000037// TODO: This can go away once the only remaining usage in aapt goes away.
Yurii Zubrytskyi27038bc2022-11-11 15:33:59 -080038class FileReader final : public zip_archive::Reader {
Narayan Kamatha07e1222017-10-30 12:57:24 +000039 public:
Chih-Hung Hsiehc2ace0c2018-12-20 13:46:53 -080040 explicit FileReader(FILE* fp) : Reader(), mFp(fp), mCurrentOffset(0) {
Mathias Agopian1f5762e2013-05-06 20:20:34 -070041 }
42
Ryan Mitchell80094e32020-11-16 23:08:18 +000043 bool ReadAtOffset(uint8_t* buf, size_t len, off64_t offset) const override {
Narayan Kamatha07e1222017-10-30 12:57:24 +000044 // 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;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070050 }
51
Narayan Kamatha07e1222017-10-30 12:57:24 +000052 mCurrentOffset = offset;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070053 }
54
Narayan Kamatha07e1222017-10-30 12:57:24 +000055 size_t read = fread(buf, 1, len, mFp);
56 if (read != len) {
57 return false;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070058 }
59
Narayan Kamatha07e1222017-10-30 12:57:24 +000060 mCurrentOffset += read;
61 return true;
Mathias Agopian1f5762e2013-05-06 20:20:34 -070062 }
63
Narayan Kamatha07e1222017-10-30 12:57:24 +000064 private:
65 FILE* mFp;
Tianjie01ae3f42020-04-01 17:04:09 -070066 mutable off64_t mCurrentOffset;
Narayan Kamathafd31e02013-12-03 13:16:03 +000067};
68
Yurii Zubrytskyi27038bc2022-11-11 15:33:59 -080069class FdReader final : public zip_archive::Reader {
Narayan Kamatha07e1222017-10-30 12:57:24 +000070 public:
71 explicit FdReader(int fd) : mFd(fd) {
72 }
Narayan Kamathafd31e02013-12-03 13:16:03 +000073
Ryan Mitchell80094e32020-11-16 23:08:18 +000074 bool ReadAtOffset(uint8_t* buf, size_t len, off64_t offset) const override {
Tianjie01ae3f42020-04-01 17:04:09 -070075 return android::base::ReadFullyAtOffset(mFd, buf, len, offset);
Narayan Kamatha07e1222017-10-30 12:57:24 +000076 }
Narayan Kamathafd31e02013-12-03 13:16:03 +000077
Narayan Kamatha07e1222017-10-30 12:57:24 +000078 private:
79 const int mFd;
Narayan Kamathafd31e02013-12-03 13:16:03 +000080};
81
Yurii Zubrytskyi27038bc2022-11-11 15:33:59 -080082class BufferReader final : public zip_archive::Reader {
Narayan Kamatha07e1222017-10-30 12:57:24 +000083 public:
Ryan Mitchell80094e32020-11-16 23:08:18 +000084 BufferReader(incfs::map_ptr<void> input, size_t inputSize) : Reader(),
85 mInput(input.convert<uint8_t>()),
Narayan Kamatha07e1222017-10-30 12:57:24 +000086 mInputSize(inputSize) {
Narayan Kamathafd31e02013-12-03 13:16:03 +000087 }
88
Ryan Mitchell80094e32020-11-16 23:08:18 +000089 bool ReadAtOffset(uint8_t* buf, size_t len, off64_t offset) const override {
Yurii Zubrytskyi82a34372024-12-27 16:43:35 -080090 auto in = AccessAtOffset(buf, len, offset);
91 if (!in) {
Ryan Mitchell80094e32020-11-16 23:08:18 +000092 return false;
93 }
Yurii Zubrytskyi82a34372024-12-27 16:43:35 -080094 memcpy(buf, in, len);
Narayan Kamatha07e1222017-10-30 12:57:24 +000095 return true;
Narayan Kamathafd31e02013-12-03 13:16:03 +000096 }
97
Yurii Zubrytskyi82a34372024-12-27 16:43:35 -080098 const uint8_t* AccessAtOffset(uint8_t*, size_t len, off64_t offset) const override {
99 if (offset > mInputSize - len) {
100 return nullptr;
101 }
102 const incfs::map_ptr<uint8_t> pos = mInput.offset(offset);
103 if (!pos.verify(len)) {
104 return nullptr;
105 }
106 return pos.unsafe_ptr();
107 }
108
109 bool IsZeroCopy() const override {
110 return true;
111 }
112
Narayan Kamatha07e1222017-10-30 12:57:24 +0000113 private:
Ryan Mitchell80094e32020-11-16 23:08:18 +0000114 const incfs::map_ptr<uint8_t> mInput;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000115 const size_t mInputSize;
Narayan Kamatha07e1222017-10-30 12:57:24 +0000116};
117
Yurii Zubrytskyi27038bc2022-11-11 15:33:59 -0800118class BufferWriter final : public zip_archive::Writer {
Narayan Kamatha07e1222017-10-30 12:57:24 +0000119 public:
Yurii Zubrytskyi82a34372024-12-27 16:43:35 -0800120 BufferWriter(void* output, size_t outputSize) :
Narayan Kamatha07e1222017-10-30 12:57:24 +0000121 mOutput(reinterpret_cast<uint8_t*>(output)), mOutputSize(outputSize), mBytesWritten(0) {
122 }
123
124 bool Append(uint8_t* buf, size_t bufSize) override {
125 if (mBytesWritten + bufSize > mOutputSize) {
126 return false;
127 }
128
129 memcpy(mOutput + mBytesWritten, buf, bufSize);
130 mBytesWritten += bufSize;
131 return true;
132 }
133
Yurii Zubrytskyi82a34372024-12-27 16:43:35 -0800134 Buffer GetBuffer(size_t length) override {
135 const auto remaining_size = mOutputSize - mBytesWritten;
136 return remaining_size >= length
137 ? Buffer(mOutput + mBytesWritten, remaining_size) : Buffer();
138 }
139
Narayan Kamatha07e1222017-10-30 12:57:24 +0000140 private:
141 uint8_t* const mOutput;
142 const size_t mOutputSize;
143 size_t mBytesWritten;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000144};
145
146/*static*/ bool ZipUtils::inflateToBuffer(FILE* fp, void* buf,
147 long uncompressedLen, long compressedLen)
148{
149 FileReader reader(fp);
Narayan Kamatha07e1222017-10-30 12:57:24 +0000150 BufferWriter writer(buf, uncompressedLen);
151 return (zip_archive::Inflate(reader, compressedLen, uncompressedLen, &writer, nullptr) == 0);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000152}
153
154/*static*/ bool ZipUtils::inflateToBuffer(int fd, void* buf,
155 long uncompressedLen, long compressedLen)
156{
157 FdReader reader(fd);
Narayan Kamatha07e1222017-10-30 12:57:24 +0000158 BufferWriter writer(buf, uncompressedLen);
159 return (zip_archive::Inflate(reader, compressedLen, uncompressedLen, &writer, nullptr) == 0);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000160}
161
Ryan Mitchell80094e32020-11-16 23:08:18 +0000162/*static*/ bool ZipUtils::inflateToBuffer(incfs::map_ptr<void> in, void* buf,
Narayan Kamathafd31e02013-12-03 13:16:03 +0000163 long uncompressedLen, long compressedLen)
164{
165 BufferReader reader(in, compressedLen);
Narayan Kamatha07e1222017-10-30 12:57:24 +0000166 BufferWriter writer(buf, uncompressedLen);
167 return (zip_archive::Inflate(reader, compressedLen, uncompressedLen, &writer, nullptr) == 0);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000168}
169
Narayan Kamatha07e1222017-10-30 12:57:24 +0000170static inline unsigned long get4LE(const unsigned char* buf) {
171 return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
172}
Narayan Kamathafd31e02013-12-03 13:16:03 +0000173
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700174/*
175 * Look at the contents of a gzip archive. We want to know where the
176 * data starts, and how long it will be after it is uncompressed.
177 *
178 * We expect to find the CRC and length as the last 8 bytes on the file.
179 * This is a pretty reasonable thing to expect for locally-compressed
180 * files, but there's a small chance that some extra padding got thrown
181 * on (the man page talks about compressed data written to tape). We
182 * don't currently deal with that here. If "gzip -l" whines, we're going
183 * to fail too.
184 *
185 * On exit, "fp" is pointing at the start of the compressed data.
186 */
187/*static*/ bool ZipUtils::examineGzip(FILE* fp, int* pCompressionMethod,
188 long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32)
189{
190 enum { // flags
191 FTEXT = 0x01,
192 FHCRC = 0x02,
193 FEXTRA = 0x04,
194 FNAME = 0x08,
195 FCOMMENT = 0x10,
196 };
197 int ic;
198 int method, flags;
199 int i;
200
201 ic = getc(fp);
202 if (ic != 0x1f || getc(fp) != 0x8b)
203 return false; // not gzip
204 method = getc(fp);
205 flags = getc(fp);
206
207 /* quick sanity checks */
208 if (method == EOF || flags == EOF)
209 return false;
Narayan Kamatha07e1222017-10-30 12:57:24 +0000210 if (method != kCompressDeflated)
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700211 return false;
212
213 /* skip over 4 bytes of mod time, 1 byte XFL, 1 byte OS */
214 for (i = 0; i < 6; i++)
215 (void) getc(fp);
216 /* consume "extra" field, if present */
217 if ((flags & FEXTRA) != 0) {
218 int len;
219
220 len = getc(fp);
221 len |= getc(fp) << 8;
222 while (len-- && getc(fp) != EOF)
223 ;
224 }
225 /* consume filename, if present */
226 if ((flags & FNAME) != 0) {
227 do {
228 ic = getc(fp);
229 } while (ic != 0 && ic != EOF);
230 }
231 /* consume comment, if present */
232 if ((flags & FCOMMENT) != 0) {
233 do {
234 ic = getc(fp);
235 } while (ic != 0 && ic != EOF);
236 }
237 /* consume 16-bit header CRC, if present */
238 if ((flags & FHCRC) != 0) {
239 (void) getc(fp);
240 (void) getc(fp);
241 }
242
243 if (feof(fp) || ferror(fp))
244 return false;
245
246 /* seek to the end; CRC and length are in the last 8 bytes */
247 long curPosn = ftell(fp);
248 unsigned char buf[8];
249 fseek(fp, -8, SEEK_END);
250 *pCompressedLen = ftell(fp) - curPosn;
251
252 if (fread(buf, 1, 8, fp) != 8)
253 return false;
254 /* seek back to start of compressed data */
255 fseek(fp, curPosn, SEEK_SET);
256
257 *pCompressionMethod = method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000258 *pCRC32 = get4LE(&buf[0]);
259 *pUncompressedLen = get4LE(&buf[4]);
Mathias Agopian1f5762e2013-05-06 20:20:34 -0700260
261 return true;
262}