]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CZipReader.h
02de98fb262abca1567d34eccb7dde2e0f6efb07
[irrlicht.git] / source / Irrlicht / CZipReader.h
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #ifndef __C_ZIP_READER_H_INCLUDED__\r
6 #define __C_ZIP_READER_H_INCLUDED__\r
7 \r
8 #include "IrrCompileConfig.h"\r
9 \r
10 #include "IReadFile.h"\r
11 #include "irrArray.h"\r
12 #include "irrString.h"\r
13 #include "IFileSystem.h"\r
14 #include "CFileList.h"\r
15 \r
16 namespace irr\r
17 {\r
18 namespace io\r
19 {\r
20         // set if the file is encrypted\r
21         const s16 ZIP_FILE_ENCRYPTED =          0x0001;\r
22         // the fields crc-32, compressed size and uncompressed size are set to\r
23         // zero in the local header\r
24         const s16 ZIP_INFO_IN_DATA_DESCRIPTOR = 0x0008;\r
25 \r
26 // byte-align structures\r
27 #include "irrpack.h"\r
28 \r
29         struct SZIPFileDataDescriptor\r
30         {\r
31                 u32 CRC32;\r
32                 u32 CompressedSize;\r
33                 u32 UncompressedSize;\r
34         } PACK_STRUCT;\r
35 \r
36         struct SZIPFileHeader\r
37         {\r
38                 u32 Sig;                                // 'PK0304' little endian (0x04034b50)\r
39                 s16 VersionToExtract;\r
40                 s16 GeneralBitFlag;\r
41                 s16 CompressionMethod;\r
42                 s16 LastModFileTime;\r
43                 s16 LastModFileDate;\r
44                 SZIPFileDataDescriptor DataDescriptor;\r
45                 s16 FilenameLength;\r
46                 s16 ExtraFieldLength;\r
47                 // filename (variable size)\r
48                 // extra field (variable size )\r
49         } PACK_STRUCT;\r
50 \r
51         struct SZIPFileCentralDirFileHeader\r
52         {\r
53                 u32 Sig;        // 'PK0102' (0x02014b50)\r
54                 u16 VersionMadeBy;\r
55                 u16 VersionToExtract;\r
56                 u16 GeneralBitFlag;\r
57                 u16 CompressionMethod;\r
58                 u16 LastModFileTime;\r
59                 u16 LastModFileDate;\r
60                 u32 CRC32;\r
61                 u32 CompressedSize;\r
62                 u32 UncompressedSize;\r
63                 u16 FilenameLength;\r
64                 u16 ExtraFieldLength;\r
65                 u16 FileCommentLength;\r
66                 u16 DiskNumberStart;\r
67                 u16 InternalFileAttributes;\r
68                 u32 ExternalFileAttributes;\r
69                 u32 RelativeOffsetOfLocalHeader;\r
70 \r
71                 // filename (variable size)\r
72                 // extra field (variable size)\r
73                 // file comment (variable size)\r
74 \r
75         } PACK_STRUCT;\r
76 \r
77         struct SZIPFileCentralDirEnd\r
78         {\r
79                 u32 Sig;                        // 'PK0506' end_of central dir signature                        // (0x06054b50)\r
80                 u16 NumberDisk;         // number of this disk\r
81                 u16 NumberStart;        // number of the disk with the start of the central directory\r
82                 u16 TotalDisk;          // total number of entries in the central dir on this disk\r
83                 u16 TotalEntries;       // total number of entries in the central dir\r
84                 u32 Size;                       // size of the central directory\r
85                 u32 Offset;                     // offset of start of centraldirectory with respect to the starting disk number\r
86                 u16 CommentLength;      // zipfile comment length\r
87                 // zipfile comment (variable size)\r
88         } PACK_STRUCT;\r
89 \r
90         struct SZipFileExtraHeader\r
91         {\r
92                 s16 ID;\r
93                 s16 Size;\r
94         } PACK_STRUCT;\r
95 \r
96         struct SZipFileAESExtraData\r
97         {\r
98                 s16 Version;\r
99                 u8 Vendor[2];\r
100                 u8 EncryptionStrength;\r
101                 s16 CompressionMode;\r
102         } PACK_STRUCT;\r
103 \r
104         enum E_GZIP_FLAGS\r
105         {\r
106                 EGZF_TEXT_DAT      = 1,\r
107                 EGZF_CRC16         = 2,\r
108                 EGZF_EXTRA_FIELDS  = 4,\r
109                 EGZF_FILE_NAME     = 8,\r
110                 EGZF_COMMENT       = 16\r
111         };\r
112 \r
113         struct SGZIPMemberHeader\r
114         {\r
115                 u16 sig; // 0x8b1f\r
116                 u8  compressionMethod; // 8 = deflate\r
117                 u8  flags;\r
118                 u32 time;\r
119                 u8  extraFlags; // slow compress = 2, fast compress = 4\r
120                 u8  operatingSystem;\r
121         } PACK_STRUCT;\r
122 \r
123 // Default alignment\r
124 #include "irrunpack.h"\r
125 \r
126         //! Contains extended info about zip files in the archive\r
127         struct SZipFileEntry\r
128         {\r
129                 //! Position of data in the archive file\r
130                 s32 Offset;\r
131 \r
132                 //! The header for this file containing compression info etc\r
133                 SZIPFileHeader header;\r
134         };\r
135 \r
136         //! Archiveloader capable of loading ZIP Archives\r
137         class CArchiveLoaderZIP : public IArchiveLoader\r
138         {\r
139         public:\r
140 \r
141                 //! Constructor\r
142                 CArchiveLoaderZIP(io::IFileSystem* fs);\r
143 \r
144                 //! returns true if the file maybe is able to be loaded by this class\r
145                 //! based on the file extension (e.g. ".zip")\r
146                 bool isALoadableFileFormat(const io::path& filename) const override;\r
147 \r
148                 //! Check if the file might be loaded by this class\r
149                 /** Check might look into the file.\r
150                 \param file File handle to check.\r
151                 \return True if file seems to be loadable. */\r
152                 bool isALoadableFileFormat(io::IReadFile* file) const override;\r
153 \r
154                 //! Check to see if the loader can create archives of this type.\r
155                 /** Check based on the archive type.\r
156                 \param fileType The archive type to check.\r
157                 \return True if the archile loader supports this type, false if not */\r
158                 bool isALoadableFileFormat(E_FILE_ARCHIVE_TYPE fileType) const override;\r
159 \r
160                 //! Creates an archive from the filename\r
161                 /** \param file File handle to check.\r
162                 \return Pointer to newly created archive, or 0 upon error. */\r
163                 IFileArchive* createArchive(const io::path& filename, bool ignoreCase, bool ignorePaths) const override;\r
164 \r
165                 //! creates/loads an archive from the file.\r
166                 //! \return Pointer to the created archive. Returns 0 if loading failed.\r
167                 io::IFileArchive* createArchive(io::IReadFile* file, bool ignoreCase, bool ignorePaths) const override;\r
168 \r
169         private:\r
170                 io::IFileSystem* FileSystem;\r
171         };\r
172 \r
173 /*!\r
174         Zip file Reader written April 2002 by N.Gebhardt.\r
175 */\r
176         class CZipReader : public virtual IFileArchive, virtual CFileList\r
177         {\r
178         public:\r
179 \r
180                 //! constructor\r
181                 CZipReader(IFileSystem* fs, IReadFile* file, bool ignoreCase, bool ignorePaths, bool isGZip=false);\r
182 \r
183                 //! destructor\r
184                 virtual ~CZipReader();\r
185 \r
186                 //! opens a file by file name\r
187                 IReadFile* createAndOpenFile(const io::path& filename) override;\r
188 \r
189                 //! opens a file by index\r
190                 IReadFile* createAndOpenFile(u32 index) override;\r
191 \r
192                 //! returns the list of files\r
193                 const IFileList* getFileList() const override;\r
194 \r
195                 //! get the archive type\r
196                 E_FILE_ARCHIVE_TYPE getType() const override;\r
197 \r
198                 //! return the id of the file Archive\r
199                 const io::path& getArchiveName() const override {return Path;}\r
200 \r
201         protected:\r
202 \r
203                 //! reads the next file header from a ZIP file, returns false if there are no more headers.\r
204                 /* if ignoreGPBits is set, the item will be read despite missing\r
205                 file information. This is used when reading items from the central\r
206                 directory. */\r
207                 bool scanZipHeader(bool ignoreGPBits=false);\r
208 \r
209                 //! the same but for gzip files\r
210                 bool scanGZipHeader();\r
211 \r
212                 bool scanCentralDirectoryHeader();\r
213 \r
214                 io::IFileSystem* FileSystem;\r
215                 IReadFile* File;\r
216 \r
217                 // holds extended info about files\r
218                 core::array<SZipFileEntry> FileInfo;\r
219 \r
220                 bool IsGZip;\r
221         };\r
222 \r
223 \r
224 } // end namespace io\r
225 } // end namespace irr\r
226 \r
227 #endif // __C_ZIP_READER_H_INCLUDED__\r