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
234600dd053dbdbd4b95f3b11057a1cc55b99f9c77Narayan Kamath#include <stdint.h>
241ead474f61fb3fe7d77ccdd8e65037fb8c93ad6dShammi Khattar#include <string.h>
2516c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <stdio.h>
26560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath#include <time.h>
2716c4d154dca43c662571129af31b27433b919a32Adam Lesinski
2816c4d154dca43c662571129af31b27433b919a32Adam Lesinskinamespace android {
2916c4d154dca43c662571129af31b27433b919a32Adam Lesinski
3016c4d154dca43c662571129af31b27433b919a32Adam Lesinski/*
3116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Container class for utility functions, primarily for namespace reasons.
3216c4d154dca43c662571129af31b27433b919a32Adam Lesinski */
3316c4d154dca43c662571129af31b27433b919a32Adam Lesinskiclass ZipUtils {
3416c4d154dca43c662571129af31b27433b919a32Adam Lesinskipublic:
3516c4d154dca43c662571129af31b27433b919a32Adam Lesinski    /*
3616c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * General utility function for uncompressing "deflate" data from a file
3716c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * to a buffer.
3816c4d154dca43c662571129af31b27433b919a32Adam Lesinski     */
39560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath    static bool inflateToBuffer(FILE* fp, void* buf, long uncompressedLen,
40560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        long compressedLen);
4116c4d154dca43c662571129af31b27433b919a32Adam Lesinski    static bool inflateToBuffer(int fd, void* buf, long uncompressedLen,
4216c4d154dca43c662571129af31b27433b919a32Adam Lesinski        long compressedLen);
43560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath    static bool inflateToBuffer(void *in, void* buf, long uncompressedLen,
4416c4d154dca43c662571129af31b27433b919a32Adam Lesinski        long compressedLen);
4516c4d154dca43c662571129af31b27433b919a32Adam Lesinski
4616c4d154dca43c662571129af31b27433b919a32Adam Lesinski    /*
4716c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * Someday we might want to make this generic and handle bzip2 ".bz2"
4816c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * files too.
4916c4d154dca43c662571129af31b27433b919a32Adam Lesinski     *
5016c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * We could declare gzip to be a sub-class of zip that has exactly
5116c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * one always-compressed entry, but we currently want to treat Zip
5216c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * and gzip as distinct, so there's no value.
5316c4d154dca43c662571129af31b27433b919a32Adam Lesinski     *
5416c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * The zlib library has some gzip utilities, but it has no interface
5516c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * for extracting the uncompressed length of the file (you do *not*
5616c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * want to gzseek to the end).
5716c4d154dca43c662571129af31b27433b919a32Adam Lesinski     *
5816c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * Pass in a seeked file pointer for the gzip file.  If this is a gzip
5916c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * file, we set our return values appropriately and return "true" with
6016c4d154dca43c662571129af31b27433b919a32Adam Lesinski     * the file seeked to the start of the compressed data.
6116c4d154dca43c662571129af31b27433b919a32Adam Lesinski     */
6216c4d154dca43c662571129af31b27433b919a32Adam Lesinski    static bool examineGzip(FILE* fp, int* pCompressionMethod,
6316c4d154dca43c662571129af31b27433b919a32Adam Lesinski        long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32);
6416c4d154dca43c662571129af31b27433b919a32Adam Lesinski
65560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath    /*
66560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath     * Utility function to convert ZIP's time format to a timespec struct.
671ead474f61fb3fe7d77ccdd8e65037fb8c93ad6dShammi Khattar     *
681ead474f61fb3fe7d77ccdd8e65037fb8c93ad6dShammi Khattar     * NOTE: this method will clear all existing state from |timespec|.
69560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath     */
704600dd053dbdbd4b95f3b11057a1cc55b99f9c77Narayan Kamath    static inline void zipTimeToTimespec(uint32_t when, struct tm* timespec) {
714600dd053dbdbd4b95f3b11057a1cc55b99f9c77Narayan Kamath        const uint32_t date = when >> 16;
721ead474f61fb3fe7d77ccdd8e65037fb8c93ad6dShammi Khattar
731ead474f61fb3fe7d77ccdd8e65037fb8c93ad6dShammi Khattar        memset(timespec, 0, sizeof(struct tm));
74560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_year = ((date >> 9) & 0x7F) + 80; // Zip is years since 1980
751ead474f61fb3fe7d77ccdd8e65037fb8c93ad6dShammi Khattar        timespec->tm_mon = ((date >> 5) & 0x0F) - 1;
76560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_mday = date & 0x1F;
77560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath
78560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_hour = (when >> 11) & 0x1F;
79560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_min = (when >> 5) & 0x3F;
80560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath        timespec->tm_sec = (when & 0x1F) << 1;
811ead474f61fb3fe7d77ccdd8e65037fb8c93ad6dShammi Khattar        timespec->tm_isdst = -1;
82560566d2915c03bed338fc532ac7f7aa3620cfdfNarayan Kamath    }
8316c4d154dca43c662571129af31b27433b919a32Adam Lesinskiprivate:
8416c4d154dca43c662571129af31b27433b919a32Adam Lesinski    ZipUtils() {}
8516c4d154dca43c662571129af31b27433b919a32Adam Lesinski    ~ZipUtils() {}
8616c4d154dca43c662571129af31b27433b919a32Adam Lesinski};
8716c4d154dca43c662571129af31b27433b919a32Adam Lesinski
8816c4d154dca43c662571129af31b27433b919a32Adam Lesinski}; // namespace android
8916c4d154dca43c662571129af31b27433b919a32Adam Lesinski
9016c4d154dca43c662571129af31b27433b919a32Adam Lesinski#endif /*__LIBS_ZIPUTILS_H*/
91