1 /*
2  * HLLib
3  * Copyright (C) 2006-2012 Ryan Gregg
4 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later
9  * version.
10  */
11 
12 #ifndef ZIPFILE_H
13 #define ZIPFILE_H
14 
15 #include "stdafx.h"
16 #include "Package.h"
17 
18 namespace HLLib
19 {
20 	class HLLIB_API CZIPFile : public CPackage
21 	{
22 	private:
23 		#pragma pack(1)
24 
25 		struct ZIPEndOfCentralDirectoryRecord
26 		{
27 			hlUInt uiSignature; // 4 bytes (0x06054b50)
28 			hlUInt16 uiNumberOfThisDisk;  // 2 bytes
29 			hlUInt16 uiNumberOfTheDiskWithStartOfCentralDirectory; // 2 bytes
30 			hlUInt16 uiCentralDirectoryEntriesThisDisk;	// 2 bytes
31 			hlUInt16 uiCentralDirectoryEntriesTotal;	// 2 bytes
32 			hlUInt uiCentralDirectorySize; // 4 bytes
33 			hlUInt uiStartOfCentralDirOffset; // 4 bytes
34 			hlUInt16 uiCommentLength; // 2 bytes
35 			// zip file comment follows
36 		};
37 
38 		struct ZIPFileHeader
39 		{
40 			hlUInt uiSignature; //  4 bytes (0x02014b50)
41 			hlUInt16 uiVersionMadeBy; // version made by 2 bytes
42 			hlUInt16 uiVersionNeededToExtract; // version needed to extract 2 bytes
43 			hlUInt16 uiFlags; // general purpose bit flag 2 bytes
44 			hlUInt16 uiCompressionMethod; // compression method 2 bytes
45 			hlUInt16 uiLastModifiedTime; // last mod file time 2 bytes
46 			hlUInt16 uiLastModifiedDate; // last mod file date 2 bytes
47 			hlUInt uiCRC32; // crc-32 4 bytes
48 			hlUInt uiCompressedSize; // compressed size 4 bytes
49 			hlUInt uiUncompressedSize; // uncompressed size 4 bytes
50 			hlUInt16 uiFileNameLength; // file name length 2 bytes
51 			hlUInt16 uiExtraFieldLength; // extra field length 2 bytes
52 			hlUInt16 uiFileCommentLength; // file comment length 2 bytes
53 			hlUInt16 uiDiskNumberStart; // disk number start 2 bytes
54 			hlUInt16 uiInternalFileAttribs; // internal file attributes 2 bytes
55 			hlUInt uiExternalFileAttribs; // external file attributes 4 bytes
56 			hlUInt uiRelativeOffsetOfLocalHeader; // relative offset of local header 4 bytes
57 			// file name (variable size)
58 			// extra field (variable size)
59 			// file comment (variable size)
60 		};
61 
62 		struct ZIPLocalFileHeader
63 		{
64 			hlUInt uiSignature; //local file header signature 4 bytes (0x04034b50)
65 			hlUInt16 uiVersionNeededToExtract; // version needed to extract 2 bytes
66 			hlUInt16 uiFlags; // general purpose bit flag 2 bytes
67 			hlUInt16 uiCompressionMethod; // compression method 2 bytes
68 			hlUInt16 uiLastModifiedTime; // last mod file time 2 bytes
69 			hlUInt16 uiLastModifiedDate; // last mod file date 2 bytes
70 			hlUInt uiCRC32; // crc-32 4 bytes
71 			hlUInt uiCompressedSize; // compressed size 4 bytes
72 			hlUInt uiUncompressedSize; // uncompressed size 4 bytes
73 			hlUInt16 uiFileNameLength; // file name length 2 bytes
74 			hlUInt16 uiExtraFieldLength; // extra field length 2 bytes
75 			// file name (variable size)
76 			// extra field (variable size)
77 			// file data (variable size)
78 		};
79 
80 		#pragma pack()
81 
82 	private:
83 		static const char *lpAttributeNames[];
84 		static const char *lpItemAttributeNames[];
85 
86 		Mapping::CView *pFileHeaderView;
87 		Mapping::CView *pEndOfCentralDirectoryRecordView;
88 
89 		const ZIPEndOfCentralDirectoryRecord *pEndOfCentralDirectoryRecord;
90 
91 	public:
92 		CZIPFile();
93 		virtual ~CZIPFile();
94 
95 		virtual HLPackageType GetType() const;
96 		virtual const hlChar *GetExtension() const;
97 		virtual const hlChar *GetDescription() const;
98 
99 	protected:
100 		virtual hlBool MapDataStructures();
101 		virtual hlVoid UnmapDataStructures();
102 
103 		virtual CDirectoryFolder *CreateRoot();
104 
105 		virtual hlUInt GetAttributeCountInternal() const;
106 		virtual const hlChar *GetAttributeNameInternal(HLPackageAttribute eAttribute) const;
107 		virtual hlBool GetAttributeInternal(HLPackageAttribute eAttribute, HLAttribute &Attribute) const;
108 
109 		virtual hlUInt GetItemAttributeCountInternal() const;
110 		virtual const hlChar *GetItemAttributeNameInternal(HLPackageAttribute eAttribute) const;
111 		virtual hlBool GetItemAttributeInternal(const CDirectoryItem *pItem, HLPackageAttribute eAttribute, HLAttribute &Attribute) const;
112 
113 		virtual hlBool GetFileExtractableInternal(const CDirectoryFile *pFile, hlBool &bExtractable) const;
114 		virtual hlBool GetFileValidationInternal(const CDirectoryFile *pFile, HLValidation &eValidation) const;
115 		virtual hlBool GetFileSizeInternal(const CDirectoryFile *pFile, hlUInt &uiSize) const;
116 		virtual hlBool GetFileSizeOnDiskInternal(const CDirectoryFile *pFile, hlUInt &uiSize) const;
117 
118 		virtual hlBool CreateStreamInternal(const CDirectoryFile *pFile, Streams::IStream *&pStream) const;
119 		virtual hlVoid ReleaseStreamInternal(Streams::IStream &Stream) const;
120 	};
121 }
122 
123 #endif
124