proxygen
proxygen::ZlibStreamDecompressor Class Reference

#include <ZlibStreamDecompressor.h>

Public Member Functions

 ZlibStreamDecompressor (ZlibCompressionType type)
 
 ZlibStreamDecompressor ()
 
 ~ZlibStreamDecompressor ()
 
void init (ZlibCompressionType type)
 
std::unique_ptr< folly::IOBufdecompress (const folly::IOBuf *in)
 
int getStatus ()
 
bool hasError ()
 
bool finished ()
 

Private Attributes

ZlibCompressionType type_ {ZlibCompressionType::NONE}
 
z_stream zlibStream_
 
int status_ {-1}
 

Detailed Description

Definition at line 31 of file ZlibStreamDecompressor.h.

Constructor & Destructor Documentation

proxygen::ZlibStreamDecompressor::ZlibStreamDecompressor ( ZlibCompressionType  type)
explicit
proxygen::ZlibStreamDecompressor::ZlibStreamDecompressor ( )
inline

Definition at line 35 of file ZlibStreamDecompressor.h.

References folly::init().

35 { }
proxygen::ZlibStreamDecompressor::~ZlibStreamDecompressor ( )

Member Function Documentation

std::unique_ptr< IOBuf > proxygen::ZlibStreamDecompressor::decompress ( const folly::IOBuf in)

Definition at line 57 of file ZlibStreamDecompressor.cpp.

References proxygen::ERROR, folly::IOBuf::next(), status_, uint8_t, and zlibStream_.

Referenced by ZlibServerFilterTest::exercise_compression().

57  {
58  auto out = IOBuf::create(FLAGS_zlib_buffer_growth);
59  auto appender = folly::io::Appender(out.get(),
60  FLAGS_zlib_buffer_growth);
61 
62  const IOBuf* crtBuf = in;
63  size_t offset = 0;
64  while (true) {
65  // Advance to the next IOBuf if necessary
66  DCHECK_GE(crtBuf->length(), offset);
67  if (crtBuf->length() == offset) {
68  crtBuf = crtBuf->next();
69  offset = 0;
70  if (crtBuf == in) {
71  // We hit the end of the IOBuf chain, and are done.
72  break;
73  }
74  }
75 
76  if (status_ == Z_STREAM_END) {
77  // we convert this into a stream error
78  status_ = Z_STREAM_ERROR;
79  // we should probably bump up a counter here
80  LOG(ERROR) << "error uncompressing buffer: reached end of zlib data "
81  "before the end of the buffer";
82  return nullptr;
83  }
84 
85  // Ensure there is space in the output IOBuf
86  appender.ensure(FLAGS_zlib_buffer_minsize);
87  DCHECK_GT(appender.length(), 0);
88 
89  const size_t origAvailIn = crtBuf->length() - offset;
90  zlibStream_.next_in = const_cast<uint8_t*>(crtBuf->data() + offset);
91  zlibStream_.avail_in = origAvailIn;
92  zlibStream_.next_out = appender.writableData();
93  zlibStream_.avail_out = appender.length();
94  status_ = inflate(&zlibStream_, Z_PARTIAL_FLUSH);
95  if (status_ != Z_OK && status_ != Z_STREAM_END) {
96  LOG(INFO) << "error uncompressing buffer: r=" << status_;
97  return nullptr;
98  }
99 
100  // Adjust the input offset ahead
101  auto inConsumed = origAvailIn - zlibStream_.avail_in;
102  offset += inConsumed;
103  // Move output buffer ahead
104  auto outMove = appender.length() - zlibStream_.avail_out;
105  appender.append(outMove);
106  }
107 
108  return out;
109 }
IOBuf * next()
Definition: IOBuf.h:600
bool proxygen::ZlibStreamDecompressor::finished ( )
inline

Definition at line 47 of file ZlibStreamDecompressor.h.

47 { return status_ == Z_STREAM_END; }
int proxygen::ZlibStreamDecompressor::getStatus ( )
inline
bool proxygen::ZlibStreamDecompressor::hasError ( )
inline

Definition at line 45 of file ZlibStreamDecompressor.h.

Referenced by ZlibServerFilterTest::exercise_compression().

45 { return status_ != Z_OK && status_ != Z_STREAM_END; }
void proxygen::ZlibStreamDecompressor::init ( ZlibCompressionType  type)

Definition at line 29 of file ZlibStreamDecompressor.cpp.

References proxygen::NONE, status_, type, type_, and zlibStream_.

Referenced by ZlibStreamDecompressor().

29  {
30  DCHECK(type_ == ZlibCompressionType::NONE) << "Must be uninitialized";
31  type_ = type;
32  status_ = Z_OK;
33  zlibStream_.zalloc = Z_NULL;
34  zlibStream_.zfree = Z_NULL;
35  zlibStream_.opaque = Z_NULL;
36  zlibStream_.total_in = 0;
37  zlibStream_.next_in = Z_NULL;
38  zlibStream_.avail_in = 0;
39  zlibStream_.avail_out = 0;
40  zlibStream_.next_out = Z_NULL;
41 
43  status_ = inflateInit2(&zlibStream_, static_cast<int>(type_));
44 }
PskType type

Member Data Documentation

int proxygen::ZlibStreamDecompressor::status_ {-1}
private

Definition at line 52 of file ZlibStreamDecompressor.h.

Referenced by decompress(), init(), and ~ZlibStreamDecompressor().

ZlibCompressionType proxygen::ZlibStreamDecompressor::type_ {ZlibCompressionType::NONE}
private

Definition at line 50 of file ZlibStreamDecompressor.h.

Referenced by init(), and ~ZlibStreamDecompressor().

z_stream proxygen::ZlibStreamDecompressor::zlibStream_
private

Definition at line 51 of file ZlibStreamDecompressor.h.

Referenced by decompress(), init(), and ~ZlibStreamDecompressor().


The documentation for this class was generated from the following files: