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