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 // Access to entries in a Zip archive.
19 //
20
21 #include "ZipEntry.h"
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <inttypes.h>
27
28 using namespace android;
29
30 #define LOG(...) fprintf(stderr, __VA_ARGS__)
31
32 /* Jan 01 2008 */
33 #define STATIC_DATE (28 << 9 | 1 << 5 | 1)
34 #define STATIC_TIME 0
35
36 /*
37 * Initialize a new ZipEntry structure from a FILE* positioned at a
38 * CentralDirectoryEntry. Rewrites the headers to remove the dynamic
39 * timestamps.
40 *
41 * On exit, the file pointer will be at the start of the next CDE or
42 * at the EOCD.
43 */
initAndRewriteFromCDE(FILE * fp)44 status_t ZipEntry::initAndRewriteFromCDE(FILE* fp)
45 {
46 /* read the CDE */
47 status_t result = mCDE.rewrite(fp);
48 if (result != 0) {
49 LOG("mCDE.rewrite failed\n");
50 return result;
51 }
52
53 /* using the info in the CDE, go load up the LFH */
54 off_t posn = ftello(fp);
55 if (fseeko(fp, mCDE.mLocalHeaderRelOffset, SEEK_SET) != 0) {
56 LOG("local header seek failed (%" PRIu32 ")\n",
57 mCDE.mLocalHeaderRelOffset);
58 return -1;
59 }
60
61 result = mLFH.rewrite(fp);
62 if (result != 0) {
63 LOG("mLFH.rewrite failed\n");
64 return result;
65 }
66
67 if (fseeko(fp, posn, SEEK_SET) != 0)
68 return -1;
69
70 return 0;
71 }
72
73 /*
74 * ===========================================================================
75 * ZipEntry::LocalFileHeader
76 * ===========================================================================
77 */
78
79 /*
80 * Rewrite a local file header.
81 *
82 * On entry, "fp" points to the signature at the start of the header.
83 */
rewrite(FILE * fp)84 status_t ZipEntry::LocalFileHeader::rewrite(FILE* fp)
85 {
86 uint8_t buf[kLFHLen];
87
88 if (fread(buf, 1, kLFHLen, fp) != kLFHLen)
89 return -1;
90
91 if (ZipEntry::getLongLE(&buf[0x00]) != kSignature) {
92 LOG("whoops: didn't find expected signature\n");
93 return -1;
94 }
95
96 ZipEntry::putShortLE(&buf[0x0a], STATIC_TIME);
97 ZipEntry::putShortLE(&buf[0x0c], STATIC_DATE);
98
99 if (fseek(fp, -kLFHLen, SEEK_CUR) != 0)
100 return -1;
101
102 if (fwrite(buf, 1, kLFHLen, fp) != kLFHLen)
103 return -1;
104
105 return 0;
106 }
107
108 /*
109 * ===========================================================================
110 * ZipEntry::CentralDirEntry
111 * ===========================================================================
112 */
113
114 /*
115 * Read and rewrite the central dir entry that appears next in the file.
116 *
117 * On entry, "fp" should be positioned on the signature bytes for the
118 * entry. On exit, "fp" will point at the signature word for the next
119 * entry or for the EOCD.
120 */
rewrite(FILE * fp)121 status_t ZipEntry::CentralDirEntry::rewrite(FILE* fp)
122 {
123 uint8_t buf[kCDELen];
124 uint16_t fileNameLength, extraFieldLength, fileCommentLength;
125
126 if (fread(buf, 1, kCDELen, fp) != kCDELen)
127 return -1;
128
129 if (ZipEntry::getLongLE(&buf[0x00]) != kSignature) {
130 LOG("Whoops: didn't find expected signature\n");
131 return -1;
132 }
133
134 ZipEntry::putShortLE(&buf[0x0c], STATIC_TIME);
135 ZipEntry::putShortLE(&buf[0x0e], STATIC_DATE);
136
137 fileNameLength = ZipEntry::getShortLE(&buf[0x1c]);
138 extraFieldLength = ZipEntry::getShortLE(&buf[0x1e]);
139 fileCommentLength = ZipEntry::getShortLE(&buf[0x20]);
140 mLocalHeaderRelOffset = ZipEntry::getLongLE(&buf[0x2a]);
141
142 if (fseek(fp, -kCDELen, SEEK_CUR) != 0)
143 return -1;
144
145 if (fwrite(buf, 1, kCDELen, fp) != kCDELen)
146 return -1;
147
148 if (fseek(fp, fileNameLength + extraFieldLength + fileCommentLength, SEEK_CUR) != 0)
149 return -1;
150
151 return 0;
152 }
153