Imported Upstream version 9.20
[platform/upstream/7zip.git] / CPP / 7zip / Archive / Nsis / NsisIn.h
1 // NsisIn.h\r
2 \r
3 #ifndef __ARCHIVE_NSIS_IN_H\r
4 #define __ARCHIVE_NSIS_IN_H\r
5 \r
6 #include "Common/Buffer.h"\r
7 #include "Common/MyCom.h"\r
8 #include "Common/StringConvert.h"\r
9 \r
10 #include "NsisDecode.h"\r
11 \r
12 // #define NSIS_SCRIPT\r
13 \r
14 namespace NArchive {\r
15 namespace NNsis {\r
16 \r
17 const int kSignatureSize = 16;\r
18 #define NSIS_SIGNATURE { 0xEF, 0xBE, 0xAD, 0xDE, 0x4E, 0x75, 0x6C, 0x6C, 0x73, 0x6F, 0x66, 0x74, 0x49, 0x6E, 0x73, 0x74}\r
19 \r
20 extern Byte kSignature[kSignatureSize];\r
21 \r
22 const UInt32 kFlagsMask = 0xF;\r
23 namespace NFlags\r
24 {\r
25   const UInt32 kUninstall = 1;\r
26   const UInt32 kSilent = 2;\r
27   const UInt32 kNoCrc = 4;\r
28   const UInt32 kForceCrc = 8;\r
29 }\r
30 \r
31 struct CFirstHeader\r
32 {\r
33   UInt32 Flags;\r
34   UInt32 HeaderLength;\r
35  \r
36   UInt32 ArchiveSize;\r
37 \r
38   bool ThereIsCrc() const\r
39   {\r
40     if ((Flags & NFlags::kForceCrc ) != 0)\r
41       return true;\r
42     return ((Flags & NFlags::kNoCrc) == 0);\r
43   }\r
44 \r
45   UInt32 GetDataSize() const { return ArchiveSize - (ThereIsCrc() ? 4 : 0); }\r
46 };\r
47 \r
48 \r
49 struct CBlockHeader\r
50 {\r
51   UInt32 Offset;\r
52   UInt32 Num;\r
53 };\r
54 \r
55 struct CItem\r
56 {\r
57   AString PrefixA;\r
58   UString PrefixU;\r
59   AString NameA;\r
60   UString NameU;\r
61   FILETIME MTime;\r
62   bool IsUnicode;\r
63   bool UseFilter;\r
64   bool IsCompressed;\r
65   bool SizeIsDefined;\r
66   bool CompressedSizeIsDefined;\r
67   bool EstimatedSizeIsDefined;\r
68   UInt32 Pos;\r
69   UInt32 Size;\r
70   UInt32 CompressedSize;\r
71   UInt32 EstimatedSize;\r
72   UInt32 DictionarySize;\r
73   \r
74   CItem(): IsUnicode(false), UseFilter(false), IsCompressed(true), SizeIsDefined(false),\r
75       CompressedSizeIsDefined(false), EstimatedSizeIsDefined(false), Size(0), DictionarySize(1) {}\r
76 \r
77   bool IsINSTDIR() const\r
78   {\r
79     return (PrefixA.Length() >= 3 || PrefixU.Length() >= 3);\r
80   }\r
81 \r
82   UString GetReducedName(bool unicode) const\r
83   {\r
84     UString s;\r
85     if (unicode)\r
86       s = PrefixU;\r
87     else\r
88       s = MultiByteToUnicodeString(PrefixA);\r
89     if (s.Length() > 0)\r
90       if (s[s.Length() - 1] != L'\\')\r
91         s += L'\\';\r
92     if (unicode)\r
93       s += NameU;\r
94     else\r
95       s += MultiByteToUnicodeString(NameA);\r
96     const int len = 9;\r
97     if (s.Left(len).CompareNoCase(L"$INSTDIR\\") == 0)\r
98       s = s.Mid(len);\r
99     return s;\r
100   }\r
101 };\r
102 \r
103 class CInArchive\r
104 {\r
105   UInt64 _archiveSize;\r
106   CMyComPtr<IInStream> _stream;\r
107 \r
108   Byte ReadByte();\r
109   UInt32 ReadUInt32();\r
110   HRESULT Open2(\r
111       DECL_EXTERNAL_CODECS_LOC_VARS2\r
112       );\r
113   void ReadBlockHeader(CBlockHeader &bh);\r
114   AString ReadStringA(UInt32 pos) const;\r
115   UString ReadStringU(UInt32 pos) const;\r
116   AString ReadString2A(UInt32 pos) const;\r
117   UString ReadString2U(UInt32 pos) const;\r
118   AString ReadString2(UInt32 pos) const;\r
119   AString ReadString2Qw(UInt32 pos) const;\r
120   HRESULT ReadEntries(const CBlockHeader &bh);\r
121   HRESULT Parse();\r
122 \r
123   CByteBuffer _data;\r
124   UInt64 _size;\r
125 \r
126   size_t _posInData;\r
127 \r
128   UInt32 _stringsPos;\r
129 \r
130 \r
131   bool _headerIsCompressed;\r
132   UInt32 _nonSolidStartOffset;\r
133 public:\r
134   HRESULT Open(\r
135       DECL_EXTERNAL_CODECS_LOC_VARS\r
136       IInStream *inStream, const UInt64 *maxCheckStartPosition);\r
137   void Clear();\r
138 \r
139   UInt64 StreamOffset;\r
140   CDecoder Decoder;\r
141   CObjectVector<CItem> Items;\r
142   CFirstHeader FirstHeader;\r
143   NMethodType::EEnum Method;\r
144   UInt32 DictionarySize;\r
145   bool IsSolid;\r
146   bool UseFilter;\r
147   bool FilterFlag;\r
148   bool IsUnicode;\r
149 \r
150   #ifdef NSIS_SCRIPT\r
151   AString Script;\r
152   #endif\r
153   UInt32 GetOffset() const { return IsSolid ? 4 : 0; }\r
154   UInt64 GetDataPos(int index)\r
155   {\r
156     const CItem &item = Items[index];\r
157     return GetOffset() + FirstHeader.HeaderLength + item.Pos;\r
158   }\r
159 \r
160   UInt64 GetPosOfSolidItem(int index) const\r
161   {\r
162     const CItem &item = Items[index];\r
163     return 4 + FirstHeader.HeaderLength + item.Pos;\r
164   }\r
165   \r
166   UInt64 GetPosOfNonSolidItem(int index) const\r
167   {\r
168     const CItem &item = Items[index];\r
169     return StreamOffset + _nonSolidStartOffset + 4  + item.Pos;\r
170   }\r
171 \r
172   void Release()\r
173   {\r
174     Decoder.Release();\r
175   }\r
176 \r
177 };\r
178 \r
179 }}\r
180   \r
181 #endif\r