1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4  */
5 
6 #include "StreamFunctions.h"
7 #include "nsZipHeader.h"
8 #include "nsMemory.h"
9 #include "prtime.h"
10 
11 #define ZIP_FILE_HEADER_SIGNATURE 0x04034b50
12 #define ZIP_FILE_HEADER_SIZE 30
13 #define ZIP_CDS_HEADER_SIGNATURE 0x02014b50
14 #define ZIP_CDS_HEADER_SIZE 46
15 
16 #define FLAGS_IS_UTF8 0x800
17 
18 #define ZIP_EXTENDED_TIMESTAMP_FIELD 0x5455
19 #define ZIP_EXTENDED_TIMESTAMP_MODTIME 0x01
20 
21 using namespace mozilla;
22 
23 /**
24  * nsZipHeader represents an entry from a zip file.
25  */
NS_IMPL_ISUPPORTS(nsZipHeader,nsIZipEntry)26 NS_IMPL_ISUPPORTS(nsZipHeader, nsIZipEntry)
27 
28 NS_IMETHODIMP nsZipHeader::GetCompression(uint16_t *aCompression)
29 {
30     NS_ASSERTION(mInited, "Not initalised");
31 
32     *aCompression = mMethod;
33     return NS_OK;
34 }
35 
GetSize(uint32_t * aSize)36 NS_IMETHODIMP nsZipHeader::GetSize(uint32_t *aSize)
37 {
38     NS_ASSERTION(mInited, "Not initalised");
39 
40     *aSize = mCSize;
41     return NS_OK;
42 }
43 
GetRealSize(uint32_t * aRealSize)44 NS_IMETHODIMP nsZipHeader::GetRealSize(uint32_t *aRealSize)
45 {
46     NS_ASSERTION(mInited, "Not initalised");
47 
48     *aRealSize = mUSize;
49     return NS_OK;
50 }
51 
GetCRC32(uint32_t * aCRC32)52 NS_IMETHODIMP nsZipHeader::GetCRC32(uint32_t *aCRC32)
53 {
54     NS_ASSERTION(mInited, "Not initalised");
55 
56     *aCRC32 = mCRC;
57     return NS_OK;
58 }
59 
GetIsDirectory(bool * aIsDirectory)60 NS_IMETHODIMP nsZipHeader::GetIsDirectory(bool *aIsDirectory)
61 {
62     NS_ASSERTION(mInited, "Not initalised");
63 
64     if (mName.Last() == '/')
65         *aIsDirectory = true;
66     else
67         *aIsDirectory = false;
68     return NS_OK;
69 }
70 
GetLastModifiedTime(PRTime * aLastModifiedTime)71 NS_IMETHODIMP nsZipHeader::GetLastModifiedTime(PRTime *aLastModifiedTime)
72 {
73     NS_ASSERTION(mInited, "Not initalised");
74 
75     // Try to read timestamp from extra field
76     uint16_t blocksize;
77     const uint8_t *tsField = GetExtraField(ZIP_EXTENDED_TIMESTAMP_FIELD, false, &blocksize);
78     if (tsField && blocksize >= 5) {
79         uint32_t pos = 4;
80         uint8_t flags;
81         flags = READ8(tsField, &pos);
82         if (flags & ZIP_EXTENDED_TIMESTAMP_MODTIME) {
83             *aLastModifiedTime = (PRTime)(READ32(tsField, &pos))
84                                  * PR_USEC_PER_SEC;
85             return NS_OK;
86         }
87     }
88 
89     // Use DOS date/time fields
90     // Note that on DST shift we can't handle correctly the hour that is valid
91     // in both DST zones
92     PRExplodedTime time;
93 
94     time.tm_usec = 0;
95 
96     time.tm_hour = (mTime >> 11) & 0x1F;
97     time.tm_min = (mTime >> 5) & 0x3F;
98     time.tm_sec = (mTime & 0x1F) * 2;
99 
100     time.tm_year = (mDate >> 9) + 1980;
101     time.tm_month = ((mDate >> 5) & 0x0F) - 1;
102     time.tm_mday = mDate & 0x1F;
103 
104     time.tm_params.tp_gmt_offset = 0;
105     time.tm_params.tp_dst_offset = 0;
106 
107     PR_NormalizeTime(&time, PR_GMTParameters);
108     time.tm_params.tp_gmt_offset = PR_LocalTimeParameters(&time).tp_gmt_offset;
109     PR_NormalizeTime(&time, PR_GMTParameters);
110     time.tm_params.tp_dst_offset = PR_LocalTimeParameters(&time).tp_dst_offset;
111 
112     *aLastModifiedTime = PR_ImplodeTime(&time);
113 
114     return NS_OK;
115 }
116 
GetIsSynthetic(bool * aIsSynthetic)117 NS_IMETHODIMP nsZipHeader::GetIsSynthetic(bool *aIsSynthetic)
118 {
119     NS_ASSERTION(mInited, "Not initalised");
120 
121     *aIsSynthetic = false;
122     return NS_OK;
123 }
124 
GetPermissions(uint32_t * aPermissions)125 NS_IMETHODIMP nsZipHeader::GetPermissions(uint32_t *aPermissions)
126 {
127     NS_ASSERTION(mInited, "Not initalised");
128 
129     // Always give user read access at least, this matches nsIZipReader's behaviour
130     *aPermissions = ((mEAttr >> 16) & 0xfff) | 0x100;
131     return NS_OK;
132 }
133 
Init(const nsACString & aPath,PRTime aDate,uint32_t aAttr,uint32_t aOffset)134 void nsZipHeader::Init(const nsACString & aPath, PRTime aDate, uint32_t aAttr,
135                        uint32_t aOffset)
136 {
137     NS_ASSERTION(!mInited, "Already initalised");
138 
139     PRExplodedTime time;
140     PR_ExplodeTime(aDate, PR_LocalTimeParameters, &time);
141 
142     mTime = time.tm_sec / 2 + (time.tm_min << 5) + (time.tm_hour << 11);
143     mDate = time.tm_mday + ((time.tm_month + 1) << 5) +
144             ((time.tm_year - 1980) << 9);
145 
146     // Store modification timestamp as extra field
147     // First fill CDS extra field
148     mFieldLength = 9;
149     mExtraField = MakeUnique<uint8_t[]>(mFieldLength);
150     if (!mExtraField) {
151         mFieldLength = 0;
152     } else {
153         uint32_t pos = 0;
154         WRITE16(mExtraField.get(), &pos, ZIP_EXTENDED_TIMESTAMP_FIELD);
155         WRITE16(mExtraField.get(), &pos, 5);
156         WRITE8(mExtraField.get(), &pos, ZIP_EXTENDED_TIMESTAMP_MODTIME);
157         WRITE32(mExtraField.get(), &pos, aDate / PR_USEC_PER_SEC);
158 
159         // Fill local extra field
160         mLocalExtraField = MakeUnique<uint8_t[]>(mFieldLength);
161         if (mLocalExtraField) {
162             mLocalFieldLength = mFieldLength;
163             memcpy(mLocalExtraField.get(), mExtraField.get(), mLocalFieldLength);
164         }
165     }
166 
167     mEAttr = aAttr;
168     mOffset = aOffset;
169     mName = aPath;
170     mComment = NS_LITERAL_CSTRING("");
171     // Claim a UTF-8 path in case it needs it.
172     mFlags |= FLAGS_IS_UTF8;
173     mInited = true;
174 }
175 
GetFileHeaderLength()176 uint32_t nsZipHeader::GetFileHeaderLength()
177 {
178     return ZIP_FILE_HEADER_SIZE + mName.Length() + mLocalFieldLength;
179 }
180 
WriteFileHeader(nsIOutputStream * aStream)181 nsresult nsZipHeader::WriteFileHeader(nsIOutputStream *aStream)
182 {
183     NS_ASSERTION(mInited, "Not initalised");
184 
185     uint8_t buf[ZIP_FILE_HEADER_SIZE];
186     uint32_t pos = 0;
187     WRITE32(buf, &pos, ZIP_FILE_HEADER_SIGNATURE);
188     WRITE16(buf, &pos, mVersionNeeded);
189     WRITE16(buf, &pos, mFlags);
190     WRITE16(buf, &pos, mMethod);
191     WRITE16(buf, &pos, mTime);
192     WRITE16(buf, &pos, mDate);
193     WRITE32(buf, &pos, mCRC);
194     WRITE32(buf, &pos, mCSize);
195     WRITE32(buf, &pos, mUSize);
196     WRITE16(buf, &pos, mName.Length());
197     WRITE16(buf, &pos, mLocalFieldLength);
198 
199     nsresult rv = ZW_WriteData(aStream, (const char *)buf, pos);
200     NS_ENSURE_SUCCESS(rv, rv);
201 
202     rv = ZW_WriteData(aStream, mName.get(), mName.Length());
203     NS_ENSURE_SUCCESS(rv, rv);
204 
205     if (mLocalFieldLength)
206     {
207       rv = ZW_WriteData(aStream, (const char *)mLocalExtraField.get(), mLocalFieldLength);
208       NS_ENSURE_SUCCESS(rv, rv);
209     }
210 
211     return NS_OK;
212 }
213 
GetCDSHeaderLength()214 uint32_t nsZipHeader::GetCDSHeaderLength()
215 {
216     return ZIP_CDS_HEADER_SIZE + mName.Length() + mComment.Length() +
217            mFieldLength;
218 }
219 
WriteCDSHeader(nsIOutputStream * aStream)220 nsresult nsZipHeader::WriteCDSHeader(nsIOutputStream *aStream)
221 {
222     NS_ASSERTION(mInited, "Not initalised");
223 
224     uint8_t buf[ZIP_CDS_HEADER_SIZE];
225     uint32_t pos = 0;
226     WRITE32(buf, &pos, ZIP_CDS_HEADER_SIGNATURE);
227     WRITE16(buf, &pos, mVersionMade);
228     WRITE16(buf, &pos, mVersionNeeded);
229     WRITE16(buf, &pos, mFlags);
230     WRITE16(buf, &pos, mMethod);
231     WRITE16(buf, &pos, mTime);
232     WRITE16(buf, &pos, mDate);
233     WRITE32(buf, &pos, mCRC);
234     WRITE32(buf, &pos, mCSize);
235     WRITE32(buf, &pos, mUSize);
236     WRITE16(buf, &pos, mName.Length());
237     WRITE16(buf, &pos, mFieldLength);
238     WRITE16(buf, &pos, mComment.Length());
239     WRITE16(buf, &pos, mDisk);
240     WRITE16(buf, &pos, mIAttr);
241     WRITE32(buf, &pos, mEAttr);
242     WRITE32(buf, &pos, mOffset);
243 
244     nsresult rv = ZW_WriteData(aStream, (const char *)buf, pos);
245     NS_ENSURE_SUCCESS(rv, rv);
246 
247     rv = ZW_WriteData(aStream, mName.get(), mName.Length());
248     NS_ENSURE_SUCCESS(rv, rv);
249     if (mExtraField) {
250         rv = ZW_WriteData(aStream, (const char *)mExtraField.get(), mFieldLength);
251         NS_ENSURE_SUCCESS(rv, rv);
252     }
253     return ZW_WriteData(aStream, mComment.get(), mComment.Length());
254 }
255 
ReadCDSHeader(nsIInputStream * stream)256 nsresult nsZipHeader::ReadCDSHeader(nsIInputStream *stream)
257 {
258     NS_ASSERTION(!mInited, "Already initalised");
259 
260     uint8_t buf[ZIP_CDS_HEADER_SIZE];
261 
262     nsresult rv = ZW_ReadData(stream, (char *)buf, ZIP_CDS_HEADER_SIZE);
263     NS_ENSURE_SUCCESS(rv, rv);
264 
265     uint32_t pos = 0;
266     uint32_t signature = READ32(buf, &pos);
267     if (signature != ZIP_CDS_HEADER_SIGNATURE)
268         return NS_ERROR_FILE_CORRUPTED;
269 
270     mVersionMade = READ16(buf, &pos);
271     mVersionNeeded = READ16(buf, &pos);
272     mFlags = READ16(buf, &pos);
273     mMethod = READ16(buf, &pos);
274     mTime = READ16(buf, &pos);
275     mDate = READ16(buf, &pos);
276     mCRC = READ32(buf, &pos);
277     mCSize = READ32(buf, &pos);
278     mUSize = READ32(buf, &pos);
279     uint16_t namelength = READ16(buf, &pos);
280     mFieldLength = READ16(buf, &pos);
281     uint16_t commentlength = READ16(buf, &pos);
282     mDisk = READ16(buf, &pos);
283     mIAttr = READ16(buf, &pos);
284     mEAttr = READ32(buf, &pos);
285     mOffset = READ32(buf, &pos);
286 
287     if (namelength > 0) {
288         auto field = MakeUnique<char[]>(namelength);
289         NS_ENSURE_TRUE(field, NS_ERROR_OUT_OF_MEMORY);
290         rv = ZW_ReadData(stream, field.get(), namelength);
291         NS_ENSURE_SUCCESS(rv, rv);
292         mName.Assign(field.get(), namelength);
293     }
294     else
295         mName = NS_LITERAL_CSTRING("");
296 
297     if (mFieldLength > 0) {
298         mExtraField = MakeUnique<uint8_t[]>(mFieldLength);
299         NS_ENSURE_TRUE(mExtraField, NS_ERROR_OUT_OF_MEMORY);
300         rv = ZW_ReadData(stream, (char *)mExtraField.get(), mFieldLength);
301         NS_ENSURE_SUCCESS(rv, rv);
302     }
303 
304     if (commentlength > 0) {
305         auto field = MakeUnique<char[]>(commentlength);
306         NS_ENSURE_TRUE(field, NS_ERROR_OUT_OF_MEMORY);
307         rv = ZW_ReadData(stream, field.get(), commentlength);
308         NS_ENSURE_SUCCESS(rv, rv);
309         mComment.Assign(field.get(), commentlength);
310     }
311     else
312         mComment = NS_LITERAL_CSTRING("");
313 
314     mInited = true;
315     return NS_OK;
316 }
317 
GetExtraField(uint16_t aTag,bool aLocal,uint16_t * aBlockSize)318 const uint8_t * nsZipHeader::GetExtraField(uint16_t aTag, bool aLocal, uint16_t *aBlockSize)
319 {
320     const uint8_t *buf = aLocal ? mLocalExtraField.get() : mExtraField.get();
321     uint32_t buflen = aLocal ? mLocalFieldLength : mFieldLength;
322     uint32_t pos = 0;
323     uint16_t tag, blocksize;
324 
325     while (buf && (pos + 4) <= buflen) {
326       tag = READ16(buf, &pos);
327       blocksize = READ16(buf, &pos);
328 
329       if (aTag == tag && (pos + blocksize) <= buflen) {
330         *aBlockSize = blocksize;
331         return buf + pos - 4;
332       }
333 
334       pos += blocksize;
335     }
336 
337     return nullptr;
338 }
339 
340 /*
341  * Pad extra field to align data starting position to specified size.
342  */
PadExtraField(uint32_t aOffset,uint16_t aAlignSize)343 nsresult nsZipHeader::PadExtraField(uint32_t aOffset, uint16_t aAlignSize)
344 {
345     uint32_t pad_size;
346     uint32_t pa_offset;
347     uint32_t pa_end;
348 
349     // Check for range and power of 2.
350     if (aAlignSize < 2 || aAlignSize > 32768 ||
351         (aAlignSize & (aAlignSize - 1)) != 0) {
352       return NS_ERROR_INVALID_ARG;
353     }
354 
355     // Point to current starting data position.
356     aOffset += ZIP_FILE_HEADER_SIZE + mName.Length() + mLocalFieldLength;
357 
358     // Calculate aligned offset.
359     pa_offset = aOffset & ~(aAlignSize - 1);
360     pa_end = pa_offset + aAlignSize;
361     pad_size = pa_end - aOffset;
362     if (pad_size == 0) {
363       return NS_OK;
364     }
365 
366     // Leave enough room(at least 4 bytes) for valid values in extra field.
367     while (pad_size < 4) {
368       pad_size += aAlignSize;
369     }
370     // Extra field length is 2 bytes.
371     if (mLocalFieldLength + pad_size > 65535) {
372       return NS_ERROR_FAILURE;
373     }
374 
375     UniquePtr<uint8_t[]> field = Move(mLocalExtraField);
376     uint32_t pos = mLocalFieldLength;
377 
378     mLocalExtraField = MakeUnique<uint8_t[]>(mLocalFieldLength + pad_size);
379     memcpy(mLocalExtraField.get(), field.get(), mLocalFieldLength);
380     // Use 0xFFFF as tag ID to avoid conflict with other IDs.
381     // For more information, please read "Extensible data fields" section in:
382     // http://www.pkware.com/documents/casestudies/APPNOTE.TXT
383     WRITE16(mLocalExtraField.get(), &pos, 0xFFFF);
384     WRITE16(mLocalExtraField.get(), &pos, pad_size - 4);
385     memset(mLocalExtraField.get() + pos, 0, pad_size - 4);
386     mLocalFieldLength += pad_size;
387 
388     return NS_OK;
389 }
390