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