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