ZipEntry.h revision 48a621c2779c74cba3555c96e3fbc7b1989ac90b
1/*
2 * Copyright (C) 2006 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// Zip archive entries.
19//
20// The ZipEntry class is tightly meshed with the ZipFile class.
21//
22#ifndef __LIBS_ZIPENTRY_H
23#define __LIBS_ZIPENTRY_H
24
25#include <stdlib.h>
26#include <stdio.h>
27
28typedef int status_t;
29
30namespace android {
31
32class ZipFile;
33
34/*
35 * ZipEntry objects represent a single entry in a Zip archive.
36 *
37 * File information is stored in two places: next to the file data (the Local
38 * File Header, and possibly a Data Descriptor), and at the end of the file
39 * (the Central Directory Entry).  The two must be kept in sync.
40 */
41class ZipEntry {
42public:
43    friend class ZipFile;
44
45    ZipEntry(void) {}
46    ~ZipEntry(void) {}
47
48    /*
49     * Some basic functions for raw data manipulation.  "LE" means
50     * Little Endian.
51     */
52    static inline unsigned short getShortLE(const unsigned char* buf) {
53        return buf[0] | (buf[1] << 8);
54    }
55    static inline unsigned long getLongLE(const unsigned char* buf) {
56        return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
57    }
58    static inline void putShortLE(unsigned char* buf, short val) {
59        buf[0] = (unsigned char) val;
60        buf[1] = (unsigned char) (val >> 8);
61    }
62
63protected:
64    /*
65     * Initialize the structure from the file, which is pointing at
66     * our Central Directory entry. And rewrite it.
67     */
68    status_t initAndRewriteFromCDE(FILE* fp);
69
70private:
71    /* these are private and not defined */
72    ZipEntry(const ZipEntry& src);
73    ZipEntry& operator=(const ZipEntry& src);
74
75    /*
76     * Every entry in the Zip archive starts off with one of these.
77     */
78    class LocalFileHeader {
79    public:
80        LocalFileHeader(void) {}
81
82        status_t rewrite(FILE* fp);
83
84        enum {
85            kSignature      = 0x04034b50,
86            kLFHLen         = 30,       // LocalFileHdr len, excl. var fields
87        };
88    };
89
90    /*
91     * Every entry in the Zip archive has one of these in the "central
92     * directory" at the end of the file.
93     */
94    class CentralDirEntry {
95    public:
96        CentralDirEntry(void) :
97            mLocalHeaderRelOffset(0)
98        {}
99
100        status_t rewrite(FILE* fp);
101
102        unsigned long   mLocalHeaderRelOffset;
103
104        enum {
105            kSignature      = 0x02014b50,
106            kCDELen         = 46,       // CentralDirEnt len, excl. var fields
107        };
108    };
109
110    LocalFileHeader     mLFH;
111    CentralDirEntry     mCDE;
112};
113
114}; // namespace android
115
116#endif // __LIBS_ZIPENTRY_H
117