// This file contains the declaration of classes // LzipInputStream and LzipOutputStream used to compress // and decompress Google's Protocol Buffer Streams using // the Lempel-Ziv-Markow-Algorithm. // // Derived from http://protobuf.googlecode.com/svn/tags/2.2.0/src/google/protobuf/io/gzip_stream.h // Copyright 2009 by Jacob Rief // Evaluation copy - don't use in production code #ifndef GOOGLE_PROTOBUF_IO_LZIP_STREAM_H__ #define GOOGLE_PROTOBUF_IO_LZIP_STREAM_H__ #include #include #include #include namespace google { namespace protobuf { namespace io { // A ZeroCopyInputStream that reads compressed data through lzib class LIBPROTOBUF_EXPORT LzipInputStream : public ZeroCopyInputStream { public: explicit LzipInputStream(ZeroCopyInputStream* sub_stream); virtual ~LzipInputStream(); // Releases the decoder. bool Close(); // --- implements ZeroCopyInputStream --- bool Next(const void** data, int* size); void BackUp(int count); bool Skip(int count); int64 ByteCount() const; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LzipInputStream); void Decompress(); // compressed input stream ZeroCopyInputStream* sub_stream_; bool finished_; // plain text output stream const int output_buffer_length_; void* const output_buffer_; uint8_t* output_position_; uint8_t* next_out_; int avail_out_; // Lzip decoder void* decoder_; LZ_errno errno_; }; class LIBPROTOBUF_EXPORT LzipOutputStream : public ZeroCopyOutputStream { public: // Create a LzipOutputStream with default options. explicit LzipOutputStream(ZeroCopyOutputStream* sub_stream, size_t compression_level = 5, int64_t member_size = std::numeric_limits::max()); virtual ~LzipOutputStream(); // Flushes data written so far to zipped data in the underlying stream. // It is the caller's responsibility to flush the underlying stream if // necessary. // Compression may be less efficient stopping and starting around flushes. // Returns true if no error. bool Flush(); // Writes out all data and closes the lzip stream. // It is the caller's responsibility to close the underlying stream if // necessary. // Returns true if no error. bool Close(); // --- implements ZeroCopyOutputStream --- bool Next(void** data, int* size); void BackUp(int count); int64 ByteCount() const; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LzipOutputStream); void Compress(bool flush = false); // plain text input stream const int input_buffer_length_; void* const input_buffer_; uint8_t* input_position_; uint8_t* const input_buffer_end_; // compressed output stream ZeroCopyOutputStream* sub_stream_; bool finished_; // Lzip encoder struct Options { int dictionary_size; // 4KiB..512MiB int match_len_limit; // 5..273 }; static const Options options[9]; void* encoder_; int member_size_; LZ_errno errno_; }; } // namespace io } // namespace protobuf } // namespace google #endif // GOOGLE_PROTOBUF_IO_LZIP_STREAM_H__