116c4d154dca43c662571129af31b27433b919a32Adam Lesinski/*
216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Copyright (C) 2007 The Android Open Source Project
316c4d154dca43c662571129af31b27433b919a32Adam Lesinski *
416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * you may not use this file except in compliance with the License.
616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * You may obtain a copy of the License at
716c4d154dca43c662571129af31b27433b919a32Adam Lesinski *
816c4d154dca43c662571129af31b27433b919a32Adam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
916c4d154dca43c662571129af31b27433b919a32Adam Lesinski *
1016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Unless required by applicable law or agreed to in writing, software
1116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
1216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * See the License for the specific language governing permissions and
1416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * limitations under the License.
1516c4d154dca43c662571129af31b27433b919a32Adam Lesinski */
1616c4d154dca43c662571129af31b27433b919a32Adam Lesinski
1716c4d154dca43c662571129af31b27433b919a32Adam Lesinski//
1816c4d154dca43c662571129af31b27433b919a32Adam Lesinski// Miscellaneous zip/gzip utility functions.
1916c4d154dca43c662571129af31b27433b919a32Adam Lesinski//
2016c4d154dca43c662571129af31b27433b919a32Adam Lesinski#ifndef __LIBS_ZIPUTILS_H
2116c4d154dca43c662571129af31b27433b919a32Adam Lesinski#define __LIBS_ZIPUTILS_H
2216c4d154dca43c662571129af31b27433b919a32Adam Lesinski
2316c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <stdio.h>
24560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath#include <time.h>
2516c4d154dca43c662571129af31b27433b919a32Adam Lesinski
2616c4d154dca43c662571129af31b27433b919a32Adam Lesinskinamespace android {
2716c4d154dca43c662571129af31b27433b919a32Adam Lesinski
2816c4d154dca43c662571129af31b27433b919a32Adam Lesinski/*
2916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Container class for utility functions, primarily for namespace reasons.
3016c4d154dca43c662571129af31b27433b919a32Adam Lesinski */
3116c4d154dca43c662571129af31b27433b919a32Adam Lesinskiclass ZipUtils {
3216c4d154dca43c662571129af31b27433b919a32Adam Lesinskipublic:
3316c4d154dca43c662571129af31b27433b919a32Adam Lesinski    /*
3416c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * General utility function for uncompressing "deflate" data from a file
3516c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * to a buffer.
3616c4d154dca43c662571129af31b27433b919a32Adam Lesinski     */
37560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath    static bool inflateToBuffer(FILE* fp, void* buf, long uncompressedLen,
38560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        long compressedLen);
3916c4d154dca43c662571129af31b27433b919a32Adam Lesinski    static bool inflateToBuffer(int fd, void* buf, long uncompressedLen,
4016c4d154dca43c662571129af31b27433b919a32Adam Lesinski        long compressedLen);
41560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath    static bool inflateToBuffer(void *in, void* buf, long uncompressedLen,
4216c4d154dca43c662571129af31b27433b919a32Adam Lesinski        long compressedLen);
4316c4d154dca43c662571129af31b27433b919a32Adam Lesinski
4416c4d154dca43c662571129af31b27433b919a32Adam Lesinski    /*
4516c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * Someday we might want to make this generic and handle bzip2 ".bz2"
4616c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * files too.
4716c4d154dca43c662571129af31b27433b919a32Adam Lesinski     *
4816c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * We could declare gzip to be a sub-class of zip that has exactly
4916c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * one always-compressed entry, but we currently want to treat Zip
5016c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * and gzip as distinct, so there's no value.
5116c4d154dca43c662571129af31b27433b919a32Adam Lesinski     *
5216c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * The zlib library has some gzip utilities, but it has no interface
5316c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * for extracting the uncompressed length of the file (you do *not*
5416c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * want to gzseek to the end).
5516c4d154dca43c662571129af31b27433b919a32Adam Lesinski     *
5616c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * Pass in a seeked file pointer for the gzip file.  If this is a gzip
5716c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * file, we set our return values appropriately and return "true" with
5816c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * the file seeked to the start of the compressed data.
5916c4d154dca43c662571129af31b27433b919a32Adam Lesinski     */
6016c4d154dca43c662571129af31b27433b919a32Adam Lesinski    static bool examineGzip(FILE* fp, int* pCompressionMethod,
6116c4d154dca43c662571129af31b27433b919a32Adam Lesinski        long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32);
6216c4d154dca43c662571129af31b27433b919a32Adam Lesinski
63560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath    /*
64560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath     * Utility function to convert ZIP's time format to a timespec struct.
65560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath     */
66560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath    static inline void zipTimeToTimespec(long when, struct tm* timespec) {
67560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        const long date = when >> 16;
68560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_year = ((date >> 9) & 0x7F) + 80; // Zip is years since 1980
69560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_mon = (date >> 5) & 0x0F;
70560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_mday = date & 0x1F;
71560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath
72560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_hour = (when >> 11) & 0x1F;
73560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_min = (when >> 5) & 0x3F;
74560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_sec = (when & 0x1F) << 1;
75560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath    }
7616c4d154dca43c662571129af31b27433b919a32Adam Lesinskiprivate:
7716c4d154dca43c662571129af31b27433b919a32Adam Lesinski    ZipUtils() {}
7816c4d154dca43c662571129af31b27433b919a32Adam Lesinski    ~ZipUtils() {}
7916c4d154dca43c662571129af31b27433b919a32Adam Lesinski};
8016c4d154dca43c662571129af31b27433b919a32Adam Lesinski
8116c4d154dca43c662571129af31b27433b919a32Adam Lesinski}; // namespace android
8216c4d154dca43c662571129af31b27433b919a32Adam Lesinski
8316c4d154dca43c662571129af31b27433b919a32Adam Lesinski#endif /*__LIBS_ZIPUTILS_H*/
84