xref: /haiku/headers/private/support/CompressionAlgorithm.h (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 /*
2  * Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _COMPRESSION_ALGORITHM_H_
6 #define _COMPRESSION_ALGORITHM_H_
7 
8 
9 #include <DataIO.h>
10 
11 
12 class BCompressionParameters {
13 public:
14 								BCompressionParameters();
15 	virtual						~BCompressionParameters();
16 };
17 
18 
19 class BDecompressionParameters {
20 public:
21 								BDecompressionParameters();
22 	virtual						~BDecompressionParameters();
23 };
24 
25 
26 class BCompressionAlgorithm {
27 public:
28 								BCompressionAlgorithm();
29 	virtual						~BCompressionAlgorithm();
30 
31 	virtual	status_t			CreateCompressingInputStream(BDataIO* input,
32 									const BCompressionParameters* parameters,
33 									BDataIO*& _stream);
34 	virtual	status_t			CreateCompressingOutputStream(BDataIO* output,
35 									const BCompressionParameters* parameters,
36 									BDataIO*& _stream);
37 	virtual	status_t			CreateDecompressingInputStream(BDataIO* input,
38 									const BDecompressionParameters* parameters,
39 									BDataIO*& _stream);
40 	virtual	status_t			CreateDecompressingOutputStream(BDataIO* output,
41 									const BDecompressionParameters* parameters,
42 									BDataIO*& _stream);
43 
44 	virtual	status_t			CompressBuffer(const void* input,
45 									size_t inputSize, void* output,
46 									size_t outputSize, size_t& _compressedSize,
47 									const BCompressionParameters* parameters
48 										= NULL);
49 	virtual	status_t			DecompressBuffer(const void* input,
50 									size_t inputSize, void* output,
51 									size_t outputSize,
52 									size_t& _uncompressedSize,
53 									const BDecompressionParameters* parameters
54 										= NULL);
55 
56 protected:
57 			class BAbstractStream;
58 			class BAbstractInputStream;
59 			class BAbstractOutputStream;
60 };
61 
62 
63 class BCompressionAlgorithm::BAbstractStream : public BDataIO {
64 public:
65 								BAbstractStream();
66 	virtual						~BAbstractStream();
67 
68 			status_t			Init(size_t bufferSize);
69 
70 protected:
71 	virtual	status_t			ProcessData(const void* input, size_t inputSize,
72 									void* output, size_t outputSize,
73 									size_t& bytesConsumed,
74 									size_t& bytesProduced) = 0;
75 									// must consume or produce at least 1 byte
76 									// or return an error
77 	virtual	status_t			FlushPendingData(void* output,
78 									size_t outputSize,
79 									size_t& bytesProduced) = 0;
80 
81 protected:
82 			uint8*				fBuffer;
83 			size_t				fBufferCapacity;
84 			size_t				fBufferOffset;
85 			size_t				fBufferSize;
86 };
87 
88 
89 class BCompressionAlgorithm::BAbstractInputStream : public BAbstractStream {
90 public:
91 								BAbstractInputStream(BDataIO* input);
92 	virtual						~BAbstractInputStream();
93 
94 	virtual	ssize_t				Read(void* buffer, size_t size);
95 
96 private:
97 			BDataIO*			fInput;
98 			bool				fEndOfInput;
99 			bool				fNoMorePendingData;
100 };
101 
102 
103 class BCompressionAlgorithm::BAbstractOutputStream : public BAbstractStream {
104 public:
105 								BAbstractOutputStream(BDataIO* output);
106 	virtual						~BAbstractOutputStream();
107 
108 	virtual	ssize_t				Write(const void* buffer, size_t size);
109 
110 	virtual	status_t			Flush();
111 
112 private:
113 			ssize_t				_Write(const void* buffer, size_t size,
114 									bool flush);
115 
116 private:
117 			BDataIO*			fOutput;
118 };
119 
120 
121 #endif	// _COMPRESSION_ALGORITHM_H_
122