nlib
ZlibInputStream.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_ZLIBINPUTSTREAM_H_
4 #define INCLUDE_NN_NLIB_ZLIBINPUTSTREAM_H_
5 #ifndef NLIB_NO_ZLIB
6 
7 #include <stdlib.h>
8 #include <zlib.h>
9 #include "nn/nlib/InputStream.h"
11 
12 NLIB_NAMESPACE_BEGIN
13 
15  typedef void* (*ReallocFunc)(void* p, size_t n);
16  void* buf_in;
17  void* buf_out;
18  size_t buffer_size;
19  ReallocFunc realloc_func;
20 
21  public:
23  buf_out(NULL),
24  buffer_size(64 * 1024),
25  realloc_func(::nlib_realloc) {}
26  NLIB_CEXPR ZlibInputStreamSettings(void* buf_in_, void* buf_out_,
27  size_t buffer_size_) NLIB_NOEXCEPT
28  : buf_in(buf_in_),
29  buf_out(buf_out_),
30  buffer_size(buffer_size_),
31  realloc_func(::nlib_realloc) {}
32  NLIB_CEXPR ZlibInputStreamSettings(void* buf_in_, void* buf_out_, size_t buffer_size_,
33  ReallocFunc realloc_func_) NLIB_NOEXCEPT
34  : buf_in(buf_in_),
35  buf_out(buf_out_),
36  buffer_size(buffer_size_),
37  realloc_func(realloc_func_) {}
38 };
39 
40 namespace detail {
41 
42 class NLIB_VIS_PUBLIC ZlibInputTransform NLIB_FINAL : public InputTransform {
43  public:
44  typedef ZlibInputStreamSettings::ReallocFunc ReallocFunc;
45  ZlibInputTransform() NLIB_NOEXCEPT;
46  virtual ~ZlibInputTransform() NLIB_NOEXCEPT NLIB_OVERRIDE;
47  errno_t Init(const ZlibInputStreamSettings& settings) NLIB_NOEXCEPT;
48  virtual errno_t Transform(InputStream* is, void* p, size_t nBytes,
49  size_t* written) NLIB_NOEXCEPT NLIB_OVERRIDE;
50  virtual void* GetWorkBuffer(size_t* n) NLIB_NOEXCEPT NLIB_OVERRIDE;
51  virtual errno_t OnSetStream(InputStream* is) NLIB_NOEXCEPT NLIB_OVERRIDE;
52  virtual errno_t OnClose() NLIB_NOEXCEPT NLIB_OVERRIDE;
53 
54  private:
55  z_stream m_Strm;
56  ReallocFunc m_Realloc;
57  unsigned char* m_BufIn; // to z_stream::next_in
58  unsigned char* m_BufOut; // to z_stream::next_out
59  size_t m_BufSize; // for both m_BufIn and m_BufOut
60  size_t m_BufOutCur;
61  size_t m_BufOutDataSize;
62  bool m_IsInternalBuffer;
63  bool m_IsZStreamEnd;
64  unsigned char m_BaseBuf[256]; // buffer for InputConverterStream
65 
66  NLIB_DISALLOW_COPY_AND_ASSIGN(ZlibInputTransform);
67 };
68 
69 } // namespace detail
70 
72  : public InputConverterStreamTempl<detail::ZlibInputTransform> {
73  public:
75  virtual ~ZlibInputStream() NLIB_NOEXCEPT NLIB_OVERRIDE;
76  errno_t Init() NLIB_NOEXCEPT {
78  return this->Init(settings);
79  }
80  errno_t Init(const ZlibInputStreamSettings& settings) NLIB_NOEXCEPT {
81  return m_Transform.Init(settings);
82  }
83 
84  private:
86 };
87 
88 NLIB_NAMESPACE_END
89 
90 #endif
91 #endif // INCLUDE_NN_NLIB_ZLIBINPUTSTREAM_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
Defines the base class for input streams.
The structure that stores the various ZlibInputStream settings. At construction time, the default settings are stored.
The class for holding the InputTransform derived class.
ReallocFunc realloc_func
The realloc function or its equivalent that ZlibInputStream uses internally.
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
size_t buffer_size
The size of ZlibInputStreamSettings::buf_in and ZlibInputStreamSettings::buf_out. ...
constexpr ZlibInputStreamSettings(void *buf_in_, void *buf_out_, size_t buffer_size_) noexcept
Sets the individual data members.
constexpr ZlibInputStreamSettings() noexcept
Instantiates the object with default parameters (default constructor). Sets the default values...
void * buf_in
The buffer set for z_stream::next_in.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
errno_t Init() noexcept
Initializes using the default settings.
constexpr ZlibInputStreamSettings(void *buf_in_, void *buf_out_, size_t buffer_size_, ReallocFunc realloc_func_) noexcept
Sets the individual data members.
The stream class for reading compressed data using zlib.
NLIB_CHECK_RESULT void * nlib_realloc(void *ptr, size_t size)
A weak function that calls the C standard function realloc. nlib calls realloc via this function...
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
void * buf_out
The buffer set for z_stream::next_out.
errno_t Init(const ZlibInputStreamSettings &settings) noexcept
Configures the buffer and the other settings to use for streaming according to the values specified i...
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24