Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:02

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Author: brianolson@google.com (Brian Olson)
0009 //
0010 // This file contains the definition for classes GzipInputStream and
0011 // GzipOutputStream.
0012 //
0013 // GzipInputStream decompresses data from an underlying
0014 // ZeroCopyInputStream and provides the decompressed data as a
0015 // ZeroCopyInputStream.
0016 //
0017 // GzipOutputStream is an ZeroCopyOutputStream that compresses data to
0018 // an underlying ZeroCopyOutputStream.
0019 
0020 #ifndef GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__
0021 #define GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__
0022 
0023 #include "google/protobuf/stubs/common.h"
0024 #include "google/protobuf/io/zero_copy_stream.h"
0025 #include "google/protobuf/port.h"
0026 #include <zlib.h>
0027 
0028 // Must be included last.
0029 #include "google/protobuf/port_def.inc"
0030 
0031 namespace google {
0032 namespace protobuf {
0033 namespace io {
0034 
0035 // A ZeroCopyInputStream that reads compressed data through zlib
0036 class PROTOBUF_EXPORT GzipInputStream final : public ZeroCopyInputStream {
0037  public:
0038   // Format key for constructor
0039   enum Format {
0040     // zlib will autodetect gzip header or deflate stream
0041     AUTO = 0,
0042 
0043     // GZIP streams have some extra header data for file attributes.
0044     GZIP = 1,
0045 
0046     // Simpler zlib stream format.
0047     ZLIB = 2,
0048   };
0049 
0050   // buffer_size and format may be -1 for default of 64kB and GZIP format
0051   explicit GzipInputStream(ZeroCopyInputStream* sub_stream,
0052                            Format format = AUTO, int buffer_size = -1);
0053   GzipInputStream(const GzipInputStream&) = delete;
0054   GzipInputStream& operator=(const GzipInputStream&) = delete;
0055   ~GzipInputStream() override;
0056 
0057   // Return last error message or NULL if no error.
0058   inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
0059   inline int ZlibErrorCode() const { return zerror_; }
0060 
0061   // implements ZeroCopyInputStream ----------------------------------
0062   bool Next(const void** data, int* size) override;
0063   void BackUp(int count) override;
0064   bool Skip(int count) override;
0065   int64_t ByteCount() const override;
0066 
0067  private:
0068   Format format_;
0069 
0070   ZeroCopyInputStream* sub_stream_;
0071 
0072   z_stream zcontext_;
0073   int zerror_;
0074 
0075   void* output_buffer_;
0076   void* output_position_;
0077   size_t output_buffer_length_;
0078   int64_t byte_count_;
0079 
0080   int Inflate(int flush);
0081   void DoNextOutput(const void** data, int* size);
0082 };
0083 
0084 class PROTOBUF_EXPORT GzipOutputStream final : public ZeroCopyOutputStream {
0085  public:
0086   // Format key for constructor
0087   enum Format {
0088     // GZIP streams have some extra header data for file attributes.
0089     GZIP = 1,
0090 
0091     // Simpler zlib stream format.
0092     ZLIB = 2,
0093   };
0094 
0095   struct PROTOBUF_EXPORT Options {
0096     // Defaults to GZIP.
0097     Format format;
0098 
0099     // What size buffer to use internally.  Defaults to 64kB.
0100     int buffer_size;
0101 
0102     // A number between 0 and 9, where 0 is no compression and 9 is best
0103     // compression.  Defaults to Z_DEFAULT_COMPRESSION (see zlib.h).
0104     int compression_level;
0105 
0106     // Defaults to Z_DEFAULT_STRATEGY.  Can also be set to Z_FILTERED,
0107     // Z_HUFFMAN_ONLY, or Z_RLE.  See the documentation for deflateInit2 in
0108     // zlib.h for definitions of these constants.
0109     int compression_strategy;
0110 
0111     Options();  // Initializes with default values.
0112   };
0113 
0114   // Create a GzipOutputStream with default options.
0115   explicit GzipOutputStream(ZeroCopyOutputStream* sub_stream);
0116 
0117   // Create a GzipOutputStream with the given options.
0118   GzipOutputStream(ZeroCopyOutputStream* sub_stream, const Options& options);
0119   GzipOutputStream(const GzipOutputStream&) = delete;
0120   GzipOutputStream& operator=(const GzipOutputStream&) = delete;
0121 
0122   ~GzipOutputStream() override;
0123 
0124   // Return last error message or NULL if no error.
0125   inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
0126   inline int ZlibErrorCode() const { return zerror_; }
0127 
0128   // Flushes data written so far to zipped data in the underlying stream.
0129   // It is the caller's responsibility to flush the underlying stream if
0130   // necessary.
0131   // Compression may be less efficient stopping and starting around flushes.
0132   // Returns true if no error.
0133   //
0134   // Please ensure that block size is > 6. Here is an excerpt from the zlib
0135   // doc that explains why:
0136   //
0137   // In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that avail_out
0138   // is greater than six to avoid repeated flush markers due to
0139   // avail_out == 0 on return.
0140   bool Flush();
0141 
0142   // Writes out all data and closes the gzip stream.
0143   // It is the caller's responsibility to close the underlying stream if
0144   // necessary.
0145   // Returns true if no error.
0146   bool Close();
0147 
0148   // implements ZeroCopyOutputStream ---------------------------------
0149   bool Next(void** data, int* size) override;
0150   void BackUp(int count) override;
0151   int64_t ByteCount() const override;
0152 
0153  private:
0154   ZeroCopyOutputStream* sub_stream_;
0155   // Result from calling Next() on sub_stream_
0156   void* sub_data_;
0157   int sub_data_size_;
0158 
0159   z_stream zcontext_;
0160   int zerror_;
0161   void* input_buffer_;
0162   size_t input_buffer_length_;
0163 
0164   // Shared constructor code.
0165   void Init(ZeroCopyOutputStream* sub_stream, const Options& options);
0166 
0167   // Do some compression.
0168   // Takes zlib flush mode.
0169   // Returns zlib error code.
0170   int Deflate(int flush);
0171 };
0172 
0173 }  // namespace io
0174 }  // namespace protobuf
0175 }  // namespace google
0176 
0177 #include "google/protobuf/port_undef.inc"
0178 
0179 #endif  // GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__