• 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     uint8_t buf[kLFHLen];
90 
91     if (fread(buf, 1, kLFHLen, fp) != kLFHLen)
92         return -1;
93 
94     if (ZipEntry::getLongLE(&buf[0x00]) != kSignature) {
95         LOG("whoops: didn't find expected signature\n");
96         return -1;
97     }
98 
99     ZipEntry::putShortLE(&buf[0x0a], STATIC_TIME);
100     ZipEntry::putShortLE(&buf[0x0c], STATIC_DATE);
101 
102     if (fseek(fp, -kLFHLen, SEEK_CUR) != 0)
103         return -1;
104 
105     if (fwrite(buf, 1, kLFHLen, fp) != kLFHLen)
106         return -1;
107 
108     return 0;
109 }
110 
111 /*
112  * ===========================================================================
113  *      ZipEntry::CentralDirEntry
114  * ===========================================================================
115  */
116 
117 /*
118  * Read and rewrite the central dir entry that appears next in the file.
119  *
120  * On entry, "fp" should be positioned on the signature bytes for the
121  * entry.  On exit, "fp" will point at the signature word for the next
122  * entry or for the EOCD.
123  */
rewrite(FILE * fp)124 status_t ZipEntry::CentralDirEntry::rewrite(FILE* fp)
125 {
126     uint8_t buf[kCDELen];
127     uint16_t fileNameLength, extraFieldLength, fileCommentLength;
128 
129     if (fread(buf, 1, kCDELen, fp) != kCDELen)
130         return -1;
131 
132     if (ZipEntry::getLongLE(&buf[0x00]) != kSignature) {
133         LOG("Whoops: didn't find expected signature\n");
134         return -1;
135     }
136 
137     ZipEntry::putShortLE(&buf[0x0c], STATIC_TIME);
138     ZipEntry::putShortLE(&buf[0x0e], STATIC_DATE);
139 
140     fileNameLength = ZipEntry::getShortLE(&buf[0x1c]);
141     extraFieldLength = ZipEntry::getShortLE(&buf[0x1e]);
142     fileCommentLength = ZipEntry::getShortLE(&buf[0x20]);
143     mLocalHeaderRelOffset = ZipEntry::getLongLE(&buf[0x2a]);
144 
145     if (fseek(fp, -kCDELen, SEEK_CUR) != 0)
146         return -1;
147 
148     if (fwrite(buf, 1, kCDELen, fp) != kCDELen)
149         return -1;
150 
151     if (fseek(fp, fileNameLength + extraFieldLength + fileCommentLength, SEEK_CUR) != 0)
152         return -1;
153 
154     return 0;
155 }
156