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 #ifndef _nsZipHeader_h_
7 #define _nsZipHeader_h_
8 
9 #include "nsString.h"
10 #include "nsIOutputStream.h"
11 #include "nsIInputStream.h"
12 #include "nsIZipReader.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/UniquePtr.h"
15 
16 // High word is S_IFREG, low word is DOS file attribute
17 #define ZIP_ATTRS_FILE 0x80000000
18 // High word is S_IFDIR, low word is DOS dir attribute
19 #define ZIP_ATTRS_DIRECTORY 0x40000010
20 #define PERMISSIONS_FILE 0644
21 #define PERMISSIONS_DIR 0755
22 
23 // Combine file type attributes with unix style permissions
24 #define ZIP_ATTRS(p, a) ((p & 0xfff) << 16) | a
25 
26 class nsZipHeader final : public nsIZipEntry {
~nsZipHeader()27   ~nsZipHeader() {
28     mExtraField = nullptr;
29     mLocalExtraField = nullptr;
30   }
31 
32  public:
33   NS_DECL_ISUPPORTS
34   NS_DECL_NSIZIPENTRY
35 
nsZipHeader()36   nsZipHeader()
37       : mCRC(0),
38         mCSize(0),
39         mUSize(0),
40         mEAttr(0),
41         mOffset(0),
42         mFieldLength(0),
43         mLocalFieldLength(0),
44         mVersionMade(0x0300 +
45                      23),    // Generated on Unix by v2.3 (matches infozip)
46         mVersionNeeded(20),  // Requires v2.0 to extract
47         mFlags(0),
48         mMethod(0),
49         mTime(0),
50         mDate(0),
51         mDisk(0),
52         mIAttr(0),
53         mInited(false),
54         mWriteOnClose(false),
55         mExtraField(nullptr),
56         mLocalExtraField(nullptr) {}
57 
58   uint32_t mCRC;
59   uint32_t mCSize;
60   uint32_t mUSize;
61   uint32_t mEAttr;
62   uint32_t mOffset;
63   uint32_t mFieldLength;
64   uint32_t mLocalFieldLength;
65   uint16_t mVersionMade;
66   uint16_t mVersionNeeded;
67   uint16_t mFlags;
68   uint16_t mMethod;
69   uint16_t mTime;
70   uint16_t mDate;
71   uint16_t mDisk;
72   uint16_t mIAttr;
73   bool mInited;
74   bool mWriteOnClose;
75   nsCString mName;
76   nsCString mComment;
77   mozilla::UniquePtr<uint8_t[]> mExtraField;
78   mozilla::UniquePtr<uint8_t[]> mLocalExtraField;
79 
80   nsresult Init(const nsACString& aPath, PRTime aDate, uint32_t aAttr,
81                 uint32_t aOffset);
82   uint32_t GetFileHeaderLength();
83   nsresult WriteFileHeader(nsIOutputStream* aStream);
84   uint32_t GetCDSHeaderLength();
85   nsresult WriteCDSHeader(nsIOutputStream* aStream);
86   nsresult ReadCDSHeader(nsIInputStream* aStream);
87   const uint8_t* GetExtraField(uint16_t aTag, bool aLocal,
88                                uint16_t* aBlockSize);
89   nsresult PadExtraField(uint32_t aOffset, uint16_t aAlignSize);
90 };
91 
92 #endif
93