CPPMyth
Library to interoperate with MythTV server
compressor.h
1 /*
2  * Copyright (C) 2016 Jean-Luc Barriere
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 3, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; see the file COPYING. If not, write to
16  * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21 
22 #ifndef COMPRESSOR_H
23 #define COMPRESSOR_H
24 
25 #include <cppmyth_config.h>
26 
27 #include <cstddef> // for size_t
28 #include <cstring> // for memcpy
29 
30 namespace NSROOT
31 {
32 
33  class Compressor
34  {
35  public:
36  typedef int(*STREAM_READER)(void *handle, void *buf, int sz);
37 
38  Compressor(STREAM_READER reader, void *handle, int level = -1);
39  Compressor(const char *input, size_t len, int level = -1);
40  virtual ~Compressor();
41 
46  bool HasOutputData() { return !m_stop; }
47 
52  bool IsCompleted();
53 
58  bool HasBufferError();
59 
64  bool HasStreamError();
65 
72  size_t ReadOutput(char *buf, size_t len);
73 
80  size_t FetchOutput(const char **data);
81 
82  private:
83  int m_status;
84  int m_flush;
85  bool m_stop;
86  size_t m_chunk_size;
87 
88  const enum
89  {
90  MEM_BUFFER,
91  FCB_READER
92  } m_type_in;
93 
94  size_t m_input_len;
95  const char *m_input;
96 
97  STREAM_READER m_rstream;
98  void *m_rstream_hdl;
99  char *m_rstream_buf;
100 
101  char *m_output;
102  size_t m_output_pos;
103  size_t m_output_len;
104 
105  void *_opaque;
106 
107  static int _init(void *zp, void *out, size_t len, int level);
108  size_t NextChunk();
109  };
110 
111 
113  {
114  public:
115  typedef int(*STREAM_READER)(void *handle, void *buf, int sz);
116 
117  Decompressor(STREAM_READER reader, void *handle);
118  Decompressor(const char *input, size_t len);
119  virtual ~Decompressor();
120 
125  bool HasOutputData() { return !m_stop; }
126 
131  bool IsCompleted();
132 
137  bool HasBufferError();
138 
143  bool HasStreamError();
144 
151  size_t ReadOutput(char *buf, size_t len);
152 
159  size_t FetchOutput(const char **data);
160 
161  private:
162  int m_status;
163  bool m_stop;
164  size_t m_chunk_size;
165 
166  const enum
167  {
168  MEM_BUFFER,
169  FCB_READER
170  } m_type_in;
171 
172  size_t m_input_len;
173  const char *m_input;
174 
175  STREAM_READER m_rstream;
176  void *m_rstream_hdl;
177  char *m_rstream_buf;
178 
179  char *m_output;
180  size_t m_output_pos;
181  size_t m_output_len;
182 
183  void *_opaque;
184 
185  static int _init(void *zp, void *out, size_t len);
186  size_t NextChunk();
187  };
188 
189 }
190 
191 #endif /* COMPRESSOR_H */
192 
bool HasBufferError()
Data cannot be read from input.
Definition: compressor.cpp:107
bool HasOutputData()
More data can be read from output stream.
Definition: compressor.h:125
bool HasStreamError()
Data error occurred from stream.
Definition: compressor.cpp:112
size_t FetchOutput(const char **data)
Fetch next chunk of data from output stream. No copy of data is performed and result can be used as i...
Definition: compressor.cpp:172
size_t ReadOutput(char *buf, size_t len)
Copy data from output stream to the given pointer until size limit.
Definition: compressor.cpp:125
bool HasOutputData()
More data can be read from output stream.
Definition: compressor.h:46
bool IsCompleted()
Output stream is completed.
Definition: compressor.cpp:102