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