xref: /haiku/headers/private/support/ZlibCompressionAlgorithm.h (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
1 /*
2  * Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _ZLIB_COMPRESSION_ALGORITHM_H_
6 #define _ZLIB_COMPRESSION_ALGORITHM_H_
7 
8 
9 #include <CompressionAlgorithm.h>
10 
11 
12 // compression level
13 enum {
14 	B_ZLIB_COMPRESSION_NONE		= 0,
15 	B_ZLIB_COMPRESSION_FASTEST	= 1,
16 	B_ZLIB_COMPRESSION_BEST		= 9,
17 	B_ZLIB_COMPRESSION_DEFAULT	= -1,
18 };
19 
20 
21 class BZlibCompressionParameters : public BCompressionParameters {
22 public:
23 								BZlibCompressionParameters(
24 									int compressionLevel
25 										= B_ZLIB_COMPRESSION_DEFAULT);
26 	virtual						~BZlibCompressionParameters();
27 
28 			int32				CompressionLevel() const;
29 			void				SetCompressionLevel(int32 level);
30 
31 			size_t				BufferSize() const;
32 			void				SetBufferSize(size_t size);
33 
34 			// TODO: Support setting the gzip header.
35 			bool				IsGzipFormat() const;
36 			void				SetGzipFormat(bool gzipFormat);
37 
38 private:
39 			int32				fCompressionLevel;
40 			size_t				fBufferSize;
41 			bool				fGzipFormat;
42 };
43 
44 
45 class BZlibDecompressionParameters : public BDecompressionParameters {
46 public:
47 								BZlibDecompressionParameters();
48 	virtual						~BZlibDecompressionParameters();
49 
50 			size_t				BufferSize() const;
51 			void				SetBufferSize(size_t size);
52 
53 private:
54 			size_t				fBufferSize;
55 };
56 
57 
58 class BZlibCompressionAlgorithm : public BCompressionAlgorithm {
59 public:
60 								BZlibCompressionAlgorithm();
61 	virtual						~BZlibCompressionAlgorithm();
62 
63 	virtual	status_t			CreateCompressingInputStream(BDataIO* input,
64 									const BCompressionParameters* parameters,
65 									BDataIO*& _stream);
66 	virtual	status_t			CreateCompressingOutputStream(BDataIO* output,
67 									const BCompressionParameters* parameters,
68 									BDataIO*& _stream);
69 	virtual	status_t			CreateDecompressingInputStream(BDataIO* input,
70 									const BDecompressionParameters* parameters,
71 									BDataIO*& _stream);
72 	virtual	status_t			CreateDecompressingOutputStream(BDataIO* output,
73 									const BDecompressionParameters* parameters,
74 									BDataIO*& _stream);
75 
76 	virtual	status_t			CompressBuffer(const void* input,
77 									size_t inputSize, void* output,
78 									size_t outputSize, size_t& _compressedSize,
79 									const BCompressionParameters* parameters
80 										= NULL);
81 	virtual	status_t			DecompressBuffer(const void* input,
82 									size_t inputSize, void* output,
83 									size_t outputSize,
84 									size_t& _uncompressedSize,
85 									const BDecompressionParameters* parameters
86 										= NULL);
87 
88 private:
89 			struct CompressionStrategy;
90 			struct DecompressionStrategy;
91 
92 			template<typename BaseClass, typename Strategy> struct Stream;
93 			template<typename BaseClass, typename Strategy>
94 				friend struct Stream;
95 
96 private:
97 	static	status_t			_TranslateZlibError(int error);
98 };
99 
100 
101 #endif	// _ZLIB_COMPRESSION_ALGORITHM_H_
102