Rizin
unix-like reverse engineering framework and cli tools
zfstream.h
Go to the documentation of this file.
1 /*
2  * A C++ I/O streams interface to the zlib gz* functions
3  *
4  * by Ludwig Schwardt <schwardt@sun.ac.za>
5  * original version by Kevin Ruland <kevin@rodin.wustl.edu>
6  *
7  * This version is standard-compliant and compatible with gcc 3.x.
8  */
9 
10 #ifndef ZFSTREAM_H
11 #define ZFSTREAM_H
12 
13 #include <istream> // not iostream, since we don't need cin/cout
14 #include <ostream>
15 #include "zlib.h"
16 
17 /*****************************************************************************/
18 
27 class gzfilebuf : public std::streambuf
28 {
29 public:
30  // Default constructor.
32 
33  // Destructor.
34  virtual
36 
48  int
49  setcompression(int comp_level,
50  int comp_strategy = Z_DEFAULT_STRATEGY);
51 
56  bool
57  is_open() const { return (file != NULL); }
58 
65  gzfilebuf*
66  open(const char* name,
67  std::ios_base::openmode mode);
68 
75  gzfilebuf*
76  attach(int fd,
77  std::ios_base::openmode mode);
78 
83  gzfilebuf*
84  close();
85 
86 protected:
91  bool
92  open_mode(std::ios_base::openmode mode,
93  char* c_mode) const;
94 
102  virtual std::streamsize
103  showmanyc();
104 
112  virtual int_type
114 
124  virtual int_type
125  overflow(int_type c = traits_type::eof());
126 
135  virtual std::streambuf*
136  setbuf(char_type* p,
137  std::streamsize n);
138 
145  virtual int
146  sync();
147 
148 //
149 // Some future enhancements
150 //
151 // virtual int_type uflow();
152 // virtual int_type pbackfail(int_type c = traits_type::eof());
153 // virtual pos_type
154 // seekoff(off_type off,
155 // std::ios_base::seekdir way,
156 // std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
157 // virtual pos_type
158 // seekpos(pos_type sp,
159 // std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
160 
161 private:
170  void
171  enable_buffer();
172 
180  void
181  disable_buffer();
182 
186  gzFile file;
187 
191  std::ios_base::openmode io_mode;
192 
199  bool own_fd;
200 
207  char_type* buffer;
208 
215  std::streamsize buffer_size;
216 
224 };
225 
226 /*****************************************************************************/
227 
234 class gzifstream : public std::istream
235 {
236 public:
237  // Default constructor
239 
245  explicit
246  gzifstream(const char* name,
247  std::ios_base::openmode mode = std::ios_base::in);
248 
254  explicit
255  gzifstream(int fd,
256  std::ios_base::openmode mode = std::ios_base::in);
257 
261  gzfilebuf*
262  rdbuf() const
263  { return const_cast<gzfilebuf*>(&sb); }
264 
269  bool
270  is_open() { return sb.is_open(); }
271 
284  void
285  open(const char* name,
286  std::ios_base::openmode mode = std::ios_base::in);
287 
296  void
297  attach(int fd,
298  std::ios_base::openmode mode = std::ios_base::in);
299 
305  void
306  close();
307 
308 private:
313 };
314 
315 /*****************************************************************************/
316 
323 class gzofstream : public std::ostream
324 {
325 public:
326  // Default constructor
328 
334  explicit
335  gzofstream(const char* name,
336  std::ios_base::openmode mode = std::ios_base::out);
337 
343  explicit
344  gzofstream(int fd,
345  std::ios_base::openmode mode = std::ios_base::out);
346 
350  gzfilebuf*
351  rdbuf() const
352  { return const_cast<gzfilebuf*>(&sb); }
353 
358  bool
359  is_open() { return sb.is_open(); }
360 
373  void
374  open(const char* name,
375  std::ios_base::openmode mode = std::ios_base::out);
376 
385  void
386  attach(int fd,
387  std::ios_base::openmode mode = std::ios_base::out);
388 
394  void
395  close();
396 
397 private:
402 };
403 
404 /*****************************************************************************/
405 
412 template<typename T1, typename T2>
413  class gzomanip2
414  {
415  public:
416  // Allows insertor to peek at internals
417  template <typename Ta, typename Tb>
418  friend gzofstream&
420  const gzomanip2<Ta,Tb>&);
421 
422  // Constructor
423  gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2),
424  T1 v1,
425  T2 v2);
426  private:
427  // Underlying manipulator function
429  (*func)(gzofstream&, T1, T2);
430 
431  // Arguments for manipulator function
432  T1 val1;
433  T2 val2;
434  };
435 
436 /*****************************************************************************/
437 
438 // Manipulator function thunks through to stream buffer
439 inline gzofstream&
441 {
442  (gzs.rdbuf())->setcompression(l, s);
443  return gzs;
444 }
445 
446 // Manipulator constructor stores arguments
447 template<typename T1, typename T2>
448  inline
450  T1 v1,
451  T2 v2)
452  : func(f), val1(v1), val2(v2)
453  { }
454 
455 // Insertor applies underlying manipulator function to stream
456 template<typename T1, typename T2>
457  inline gzofstream&
459  { return (*m.func)(s, m.val1, m.val2); }
460 
461 // Insert this onto stream to simplify setting of compression level
462 inline gzomanip2<int,int>
464 { return gzomanip2<int,int>(&setcompression, l, s); }
465 
466 #endif // ZFSTREAM_H
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
Gzipped file stream buffer class.
Definition: zfstream.h:8
std::streamsize buffer_size
Stream buffer size.
Definition: zfstream.h:215
gzFile file
Definition: zfstream.h:35
virtual int_type underflow()
Fill get area from gzipped file.
bool is_open() const
Check if file is open.
Definition: zfstream.h:57
gzfilebuf * close()
Close gzipped file.
std::ios_base::openmode io_mode
Definition: zfstream.h:191
bool own_fd
True if this object owns file descriptor.
Definition: zfstream.h:199
gzfilebuf * attach(int file_descriptor, int io_mode)
Definition: zfstream.cpp:60
virtual int overflow(int=EOF)
Definition: zfstream.cpp:173
virtual std::streambuf * setbuf(char_type *p, std::streamsize n)
Installs external stream buffer.
Definition: zfstream.cc:266
void disable_buffer()
Destroy internal buffer.
Definition: zfstream.cc:347
virtual int sync()
Flush stream buffer to file.
char_type * buffer
Stream buffer.
Definition: zfstream.h:207
bool open_mode(std::ios_base::openmode mode, char *c_mode) const
Convert ios open mode int to mode string used by zlib.
Definition: zfstream.cc:131
bool own_buffer
True if this object owns stream buffer.
Definition: zfstream.h:223
virtual ~gzfilebuf()
int setcompression(int comp_level, int comp_strategy=Z_DEFAULT_STRATEGY)
Set compression level and strategy on the fly.
Definition: zfstream.cc:43
void enable_buffer()
Allocate internal buffer.
Definition: zfstream.cc:308
gzfilebuf * open(const char *name, int io_mode)
Definition: zfstream.cpp:18
int is_open() const
Definition: zfstream.h:22
virtual std::streamsize showmanyc()
Number of characters available in stream buffer.
Definition: zfstream.cc:169
Gzipped file input stream class.
Definition: zfstream.h:68
void close()
Close gzipped file.
Definition: zfstream.cc:420
gzfilebuf * rdbuf() const
Definition: zfstream.h:262
void attach(int fd, std::ios_base::openmode mode=std::ios_base::in)
Attach to already open gzipped file.
Definition: zfstream.cc:409
gzfilebuf sb
Definition: zfstream.h:312
void open(const char *name, std::ios_base::openmode mode=std::ios_base::in)
Open gzipped file.
Definition: zfstream.cc:398
bool is_open()
Check if file is open.
Definition: zfstream.h:270
Gzipped file output stream class.
Definition: zfstream.h:80
gzfilebuf * rdbuf() const
Definition: zfstream.h:351
gzfilebuf sb
Definition: zfstream.h:401
void close()
Close gzipped file.
Definition: zfstream.cc:475
void attach(int fd, std::ios_base::openmode mode=std::ios_base::out)
Attach to already open gzipped file.
Definition: zfstream.cc:464
bool is_open()
Check if file is open.
Definition: zfstream.h:359
void open(const char *name, std::ios_base::openmode mode=std::ios_base::out)
Open gzipped file.
Definition: zfstream.cc:453
Gzipped file output stream manipulator class.
Definition: zfstream.h:414
friend gzofstream & operator<<(gzofstream &, const gzomanip2< Ta, Tb > &)
gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2), T1 v1, T2 v2)
Definition: zfstream.h:449
#define NULL
Definition: cris-opc.c:27
const char int mode
Definition: ioapi.h:137
gzofstream & setcompression(gzofstream &gzs, int l, int s=Z_DEFAULT_STRATEGY)
Definition: zfstream.h:440
gzofstream & operator<<(gzofstream &s, const gzomanip< T > &m)
Definition: zfstream.h:101
@ v1
Definition: lanai.h:85
void * p
Definition: libc.cpp:67
int n
Definition: mipsasm.c:19
static RzSocket * s
Definition: rtr.c:28
#define f(i)
Definition: sha256.c:46
#define c(i)
Definition: sha256.c:43
Definition: gzappend.c:170
Definition: z80asm.h:102
static const z80_opcode fd[]
Definition: z80_tab.h:997
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:200