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