Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [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 | // Miscellaneous zip/gzip utility functions. |
| 19 | // |
| 20 | #ifndef __LIBS_ZIPUTILS_H |
| 21 | #define __LIBS_ZIPUTILS_H |
| 22 | |
Narayan Kamath | 4600dd0 | 2015-06-16 12:02:57 +0100 | [diff] [blame] | 23 | #include <stdint.h> |
Shammi Khattar | 7134ce3 | 2016-04-06 15:33:03 -0700 | [diff] [blame] | 24 | #include <string.h> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 26 | #include <time.h> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | /* |
| 31 | * Container class for utility functions, primarily for namespace reasons. |
| 32 | */ |
| 33 | class ZipUtils { |
| 34 | public: |
| 35 | /* |
| 36 | * General utility function for uncompressing "deflate" data from a file |
| 37 | * to a buffer. |
| 38 | */ |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 39 | static bool inflateToBuffer(FILE* fp, void* buf, long uncompressedLen, |
| 40 | long compressedLen); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 41 | static bool inflateToBuffer(int fd, void* buf, long uncompressedLen, |
| 42 | long compressedLen); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 43 | static bool inflateToBuffer(void *in, void* buf, long uncompressedLen, |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 44 | long compressedLen); |
| 45 | |
| 46 | /* |
| 47 | * Someday we might want to make this generic and handle bzip2 ".bz2" |
| 48 | * files too. |
| 49 | * |
| 50 | * We could declare gzip to be a sub-class of zip that has exactly |
| 51 | * one always-compressed entry, but we currently want to treat Zip |
| 52 | * and gzip as distinct, so there's no value. |
| 53 | * |
| 54 | * The zlib library has some gzip utilities, but it has no interface |
| 55 | * for extracting the uncompressed length of the file (you do *not* |
| 56 | * want to gzseek to the end). |
| 57 | * |
| 58 | * Pass in a seeked file pointer for the gzip file. If this is a gzip |
| 59 | * file, we set our return values appropriately and return "true" with |
| 60 | * the file seeked to the start of the compressed data. |
| 61 | */ |
| 62 | static bool examineGzip(FILE* fp, int* pCompressionMethod, |
| 63 | long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32); |
| 64 | |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 65 | /* |
| 66 | * Utility function to convert ZIP's time format to a timespec struct. |
Shammi Khattar | 7134ce3 | 2016-04-06 15:33:03 -0700 | [diff] [blame] | 67 | * |
| 68 | * NOTE: this method will clear all existing state from |timespec|. |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 69 | */ |
Narayan Kamath | 4600dd0 | 2015-06-16 12:02:57 +0100 | [diff] [blame] | 70 | static inline void zipTimeToTimespec(uint32_t when, struct tm* timespec) { |
| 71 | const uint32_t date = when >> 16; |
Shammi Khattar | 7134ce3 | 2016-04-06 15:33:03 -0700 | [diff] [blame] | 72 | |
| 73 | memset(timespec, 0, sizeof(struct tm)); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 74 | timespec->tm_year = ((date >> 9) & 0x7F) + 80; // Zip is years since 1980 |
Shammi Khattar | 7134ce3 | 2016-04-06 15:33:03 -0700 | [diff] [blame] | 75 | timespec->tm_mon = ((date >> 5) & 0x0F) - 1; |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 76 | timespec->tm_mday = date & 0x1F; |
| 77 | |
| 78 | timespec->tm_hour = (when >> 11) & 0x1F; |
| 79 | timespec->tm_min = (when >> 5) & 0x3F; |
| 80 | timespec->tm_sec = (when & 0x1F) << 1; |
Shammi Khattar | 7134ce3 | 2016-04-06 15:33:03 -0700 | [diff] [blame] | 81 | timespec->tm_isdst = -1; |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 82 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 83 | private: |
| 84 | ZipUtils() {} |
| 85 | ~ZipUtils() {} |
| 86 | }; |
| 87 | |
| 88 | }; // namespace android |
| 89 | |
| 90 | #endif /*__LIBS_ZIPUTILS_H*/ |