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