File: ZipCompressor.h

package info (click to toggle)
tuxcmd-modules 0.6.70%2Bds-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 1,872 kB
  • ctags: 2,807
  • sloc: cpp: 11,743; ansic: 8,064; makefile: 245
file content (485 lines) | stat: -rw-r--r-- 11,328 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyrighted 2000 - 2007 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// For the licensing details refer to the License.txt file.
//
// Web Site: http://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////

/**
* \file ZipCompressor.h
* Includes the CZipCompressor class.
*
*/

#if !defined(ZIPARCHIVE_ZIPCOMPRESSOR_DOT_H)
#define ZIPARCHIVE_ZIPCOMPRESSOR_DOT_H

#if _MSC_VER > 1000
	#pragma once
	#pragma warning( push )
	#pragma warning (disable : 4100) // unreferenced formal parameter
#endif

#include "ZipExport.h"
#include "ZipAutoBuffer.h"
#include "ZipFileHeader.h"
#include "ZipStorage.h"
#include "ZipCryptograph.h"
#include "ZipException.h"

/**
	A base class for compressors used in compression and decompression of data.
*/
class ZIP_API CZipCompressor
{
protected:
	CZipStorage* m_pStorage;			///< The current storage object.
	CZipAutoBuffer m_pBuffer;			///< A buffer that receives compressed data or provides data for decompression.
	CZipCryptograph* m_pCryptograph;	///< Current cryptograph.
	CZipFileHeader* m_pFile;			///< The file header being compressed or decompressed.

	
	/**
		Initializes a new instance of the CZipCompressor class.

		\param pStorage
			The current storage object.
	 */
	CZipCompressor(CZipStorage* pStorage)
		:m_pStorage(pStorage)
	{
		m_pCryptograph = NULL;
		m_uUncomprLeft = 0;
		m_uComprLeft  = 0;
		m_uCrc32 = 0;
	}
	
public:		
	/**
		The type of a compressor.
	*/
	enum CompressorType
	{
		typeDeflate = 1,	///< Deflate compression (default in zip archives).
		typeBzip2			///< Bzip2 compression.
	};

	/**
		The compression level.
	*/
	enum CompressionLevel
	{		
		levelDefault = -1,	///< The default compression level (equals 6 for deflate).
		levelStore = 0,		///< No compression used. Data is stored.
		levelFastest = 1,	///< The fastest compression. The compression ratio is the lowest (apart from #levelStore).
		levelBest = 9		///< The highest compression ratio. It's usually the slowest.
	};

	/**
		The compression method.
	*/
	enum CompressionMethod
	{
		methodStore = 0, ///< A file is stored, not compressed.
		methodDeflate = 8, ///< The deflate compression method.
		/**
			A file is compressed using the bzip2 algorithm. 

			\see
				<a href="kb">0610231446|bzip2</a>				
		*/
		methodBzip2 = 12,
		/**
			This value means that WinZip AES encryption is used.
			The original compression method is stored in a WinZip extra field.
			It is only an informational value - you cannot set it as a compression method. The ZipArchive 
			Library handles this value internally.

			\see
				<a href="kb">0610201627|aes</a>
		*/
		methodWinZipAes = 99
	};

	/**
		The base class for compressors options.

		\see
			<a href="kb">0610231446|options</a>
		\see
			CZipArchive::SetCompressionOptions
	*/
	struct ZIP_API COptions
	{

		/**	  
			  Helper constants.
		*/
		enum Constants
		{
			/**
				The default size of the buffer used in compression and decompression operations.
			*/
			cDefaultBufferSize = 2 * 65536
		};

		COptions()
		{
			m_iBufferSize = cDefaultBufferSize;
		}

		/**
			Gets the type of the compressor to which the current options apply.

			\return
				The type of the compressor. It can be one of the #CompressorType values.
		*/
		virtual int GetType() const = 0;

		/**
			Clones the current options object.

			\return 
				The cloned object of the same type as the current object.
		*/
		virtual COptions* Clone() const = 0;

		/**
			The size of the buffer used in compression and decompression operations. 
			By default it is set to to #cDefaultBufferSize. For the optimal performance of the 
			deflate algorithm it should be set at least to 128kB.

			\see
				CZipArchive::SetAdvanced
		*/
		int m_iBufferSize;
		virtual ~COptions()
		{
		}
	};


	/**
		A dictionary used for keeping options for different types of compressors.

		\see
			CZipArchive::SetCompressionOptions
	*/
	class ZIP_API COptionsMap : public CZipMap<int, COptions*>
	{
		public:
			void Set(const COptions* pOptions);
			void Remove(int iType);
			COptions* Get(int iType) const; 
			~COptionsMap();
	};

	/**
		Returns the value indicating whether the given compression method is supported by the ZipArchive Library.
		
		\param uCompressionMethod
			The compression method. Can be one of the #CompressionMethod values.

		\return 
			\c true, if the compression method is supported, \c false otherwise.
	*/
	static bool IsCompressionSupported(WORD uCompressionMethod)
	{		
		return uCompressionMethod == methodStore || uCompressionMethod == methodDeflate
			;
	}

	ZIP_SIZE_TYPE m_uUncomprLeft;	///< The number of bytes left to decompress.
	ZIP_SIZE_TYPE m_uComprLeft;		///< The number of bytes left to compress.
	DWORD m_uCrc32;	///< The CRC32 file checksum.	

	/**
		Returns the value indicating, if the current #CZipCompressor object supports the given compression method.
		
		\param uMethod
			The compression method. Can be one of the #CompressionMethod values.

		\return 
			\c true, if the compression method is supported; \c false otherwise.
			
	*/
	virtual bool CanProcess(WORD uMethod) = 0;


	/**
		The method called when a new file is opened for compression.
		
		\param iLevel
			The compression level.
		
		\param pFile
			The file being compressed.
		
		\param pCryptograph
			The current CZipCryptograph. Can be \c NULL, if no encryption is used.

		\see
			Compress
		\see 
			FinishCompression
	 */
	virtual void InitCompression(int iLevel, CZipFileHeader* pFile, CZipCryptograph* pCryptograph)	
	{
		InitBuffer();
		m_uComprLeft = 0;
		m_pFile = pFile;
		m_pCryptograph = pCryptograph;
	}

	/**
		The method called when a new file is opened for extraction.
		
		\param pFile
			The file being extracted.
		
		\param pCryptograph
			The current CZipCryptograph. Can be \c NULL, if no decryption is used.

		\see
			Decompress
		\see 
			FinishDecompression
	 */
	virtual void InitDecompression(CZipFileHeader* pFile, CZipCryptograph* pCryptograph)
	{
		InitBuffer();
		m_pFile = pFile;
		m_pCryptograph = pCryptograph;

		m_uComprLeft = m_pFile->GetDataSize(false, true);
		m_uUncomprLeft = m_pFile->m_uUncomprSize;
		m_uCrc32 = 0;
	}

	/**
		Compresses the given data.
	
		\param pBuffer
			The buffer that holds the data to compress.
	
		\param uSize
			The size of \a pBuffer.	

		\see
			InitCompression
		\see 
			FinishCompression
	 */
	virtual void Compress(const void *pBuffer, DWORD uSize) = 0;

	/**
		Decompresses the given data.
	
		\param pBuffer
			The buffer that receives the decompressed data.
	
		\param uSize
			The size of \a pBuffer.	

		\return 
			The number of bytes decompressed and written to \a pBuffer.

		\note
			This method should be called repeatedly until it returns 0.

		\see
			InitDecompression
		\see 
			FinishDecompression
	 */
	virtual DWORD Decompress(void *pBuffer, DWORD uSize) = 0;

	/**
		The method called at the end of the compression process.
	
		\param bAfterException
			Set to \c true, if an exception occurred before or to \c false otherwise.

		\see
			InitCompression
		\see 
			Compress
	 */
	virtual void FinishCompression(bool bAfterException){}

	/**
		The method called at the end of the decompression process.
	
		\param bAfterException
			Set to \c true, if an exception occurred before or to \c false otherwise.

		\see
			InitDecompression
		\see 
			Decompress
	 */
	virtual void FinishDecompression(bool bAfterException){}

	/**
		Returns the current options of the compressor.

		\return
			The current options for the compressor.

		\see
			<a href="kb">0610231446|options</a>
		\see
			CZipArchive::SetCompressionOptions
		\see 
			UpdateOptions
	*/
	virtual const COptions* GetOptions() const
	{
		return NULL;
	}

	/**
		Updates the current options with the options stored in \a optionsMap,
		if the appropriate options are present in the map.

		\param optionsMap
			The map to get the new options from.

		\see
			<a href="kb">0610231446|options</a>
		\see
			GetOptions		
	*/
	void UpdateOptions(const COptionsMap& optionsMap);


	virtual ~CZipCompressor()
	{
	}

	/**
		A factory method that creates an appropriate compressor for the given compression method.

		\param uMethod
			The compression method to create a compressor for. Can be one of #CompressionMethod values.

		\param pStorage
			The current storage object.
	*/
	static CZipCompressor* CreateCompressor(WORD uMethod, CZipStorage* pStorage);

	
protected:
	/**
		Updates the current options with the new options.

		\param pOptions
			The new options to apply.
	*/
	virtual void UpdateOptions(const COptions* pOptions)
	{		
	}
	/**
		Updates CRC value while compression. 

		\param pBuffer
			A buffer with data for which the CRC value should be updated.

		\param uSize
			The size of the buffer.
	*/
	void UpdateFileCrc(const void *pBuffer, DWORD uSize);

	/**
		Updates CRC value while decompression. 

		\param pBuffer
			A buffer with data for which the CRC value should be updated.

		\param uSize
			The size of the buffer.
	*/
	void UpdateCrc(const void *pBuffer, DWORD uSize);

	/**
		Flushes data in the buffer into the storage, encrypting the data if needed.

		\note
			Throws exceptions.
	*/
	void FlushWriteBuffer()
	{
		if (m_pCryptograph)
			m_pCryptograph->Encode(m_pBuffer, (DWORD)m_uComprLeft);
		m_pStorage->Write(m_pBuffer, (DWORD)m_uComprLeft, false);
		m_uComprLeft = 0;
	}

	/**
		Initializes the internal buffer.

		\see
			ReleaseBuffer
	*/
	void InitBuffer();

	/**
		Releases the internal buffer.

		\see
			InitBuffer
	*/
	void ReleaseBuffer()
	{
		m_pBuffer.Release();
	}

	/**
		Converts internal error code of the compressor to the ZipArchive Library error code.

		\param iErr
			An internal error code.

		\return
			A ZipArchive Library error code.
	*/
	virtual int ConvertInternalError(int iErr) const
	{
		return iErr;
	}

	/**
		Throws an exception with a given error code.

		\param iErr
			An error code.

		\param bInternal
			\c true, if \a iErr is an internal error code and needs a conversion to the ZipArchive Library error code; \c false otherwise.

		\note
			Throws exceptions.

		\see
			ConvertInternalError
	*/
	void ThrowError(int iErr, bool bInternal = false)
	{
		if (bInternal)
			iErr = ConvertInternalError(iErr);
		CZipException::Throw(iErr, m_pStorage->IsClosed(true) ? _T("") : (LPCTSTR)m_pStorage->m_pFile->GetFilePath());
	}
};

#if _MSC_VER > 1000
	#pragma warning( pop )
#endif

#endif