• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     status_t result;
47     long posn;
48 
49     /* read the CDE */
50     result = mCDE.rewrite(fp);
51     if (result != 0) {
52         LOG("mCDE.rewrite failed\n");
53         return result;
54     }
55 
56     /* using the info in the CDE, go load up the LFH */
57     posn = ftell(fp);
58     if (fseek(fp, mCDE.mLocalHeaderRelOffset, SEEK_SET) != 0) {
59         LOG("local header seek failed (%" PRIu32 ")\n",
60             mCDE.mLocalHeaderRelOffset);
61         return -1;
62     }
63 
64     result = mLFH.rewrite(fp);
65     if (result != 0) {
66         LOG("mLFH.rewrite failed\n");
67         return result;
68     }
69 
70     if (fseek(fp, posn, SEEK_SET) != 0)
71         return -1;
72 
73     return 0;
74 }
75 
76 /*
77  * ===========================================================================
78  *      ZipEntry::LocalFileHeader
79  * ===========================================================================
80  */
81 
82 /*
83  * Rewrite a local file header.
84  *
85  * On entry, "fp" points to the signature at the start of the header.
86  */
rewrite(FILE * fp)87 status_t ZipEntry::LocalFileHeader::rewrite(FILE* fp)
88 {
89     status_t result = 0;
90     uint8_t buf[kLFHLen];
91 
92     if (fread(buf, 1, kLFHLen, fp) != kLFHLen)
93         return -1;
94 
95     if (ZipEntry::getLongLE(&buf[0x00]) != kSignature) {
96         LOG("whoops: didn't find expected signature\n");
97         return -1;
98     }
99 
100     ZipEntry::putShortLE(&buf[0x0a], STATIC_TIME);
101     ZipEntry::putShortLE(&buf[0x0c], STATIC_DATE);
102 
103     if (fseek(fp, -kLFHLen, SEEK_CUR) != 0)
104         return -1;
105 
106     if (fwrite(buf, 1, kLFHLen, fp) != kLFHLen)
107         return -1;
108 
109     return 0;
110 }
111 
112 /*
113  * ===========================================================================
114  *      ZipEntry::CentralDirEntry
115  * ===========================================================================
116  */
117 
118 /*
119  * Read and rewrite the central dir entry that appears next in the file.
120  *
121  * On entry, "fp" should be positioned on the signature bytes for the
122  * entry.  On exit, "fp" will point at the signature word for the next
123  * entry or for the EOCD.
124  */
rewrite(FILE * fp)125 status_t ZipEntry::CentralDirEntry::rewrite(FILE* fp)
126 {
127     status_t result = 0;
128     uint8_t buf[kCDELen];
129     uint16_t fileNameLength, extraFieldLength, fileCommentLength;
130 
131     if (fread(buf, 1, kCDELen, fp) != kCDELen)
132         return -1;
133 
134     if (ZipEntry::getLongLE(&buf[0x00]) != kSignature) {
135         LOG("Whoops: didn't find expected signature\n");
136         return -1;
137     }
138 
139     ZipEntry::putShortLE(&buf[0x0c], STATIC_TIME);
140     ZipEntry::putShortLE(&buf[0x0e], STATIC_DATE);
141 
142     fileNameLength = ZipEntry::getShortLE(&buf[0x1c]);
143     extraFieldLength = ZipEntry::getShortLE(&buf[0x1e]);
144     fileCommentLength = ZipEntry::getShortLE(&buf[0x20]);
145     mLocalHeaderRelOffset = ZipEntry::getLongLE(&buf[0x2a]);
146 
147     if (fseek(fp, -kCDELen, SEEK_CUR) != 0)
148         return -1;
149 
150     if (fwrite(buf, 1, kCDELen, fp) != kCDELen)
151         return -1;
152 
153     if (fseek(fp, fileNameLength + extraFieldLength + fileCommentLength, SEEK_CUR) != 0)
154         return -1;
155 
156     return 0;
157 }
158