01:
02:
03: #ifndef _AlgoZlib_HH
04: #define _AlgoZlib_HH 1
05:
06:
07:
08: #include "zlib.h"
09: #include <stdexcept>
10: #include <sstream>
11:
12:
13:
14: namespace Zlib {
15: struct Error;
16: struct Inflate;
17: struct Deflate;
18: typedef Bytef Bytef;
19: }
20:
21:
22:
23: struct Zlib::Error: std::runtime_error {
24: Error(): std::runtime_error("Zlib Error") {}
25: Error(std::string const & w): std::runtime_error(w) {}
26: };
27:
28: struct Zlib::Inflate {
29: z_stream z_inflate;
30: Inflate(std::string streamname)
31: {
32: z_inflate.zalloc = (alloc_func)0;
33: z_inflate.zfree = (free_func)0;
34: z_inflate.opaque = (voidpf)0;
35:
36: int s = inflateInit2(
37: &z_inflate,
38: MAX_WBITS);
39: if (s != Z_OK) {
40: std::ostringstream err;
41: err << streamname << " Initialization failed: " << s;
42: throw Error(err.str());
43: }
44: }
45: ~Inflate() { inflateEnd(&z_inflate); }
46: int zlib_inflate(int flush = Z_SYNC_FLUSH)
47: {
48: return inflate(&z_inflate, flush);
49: }
50: };
51:
52: struct Zlib::Deflate {
53: z_stream z_deflate;
54: Deflate(std::string streamname)
55: {
56: z_deflate.zalloc = (alloc_func)0;
57: z_deflate.zfree = (free_func)0;
58: z_deflate.opaque = (voidpf)0;
59:
60: int s = deflateInit2(
61: &z_deflate,
62: Z_DEFAULT_COMPRESSION,
63: Z_DEFLATED,
64: MAX_WBITS,
65: MAX_MEM_LEVEL,
66: Z_DEFAULT_STRATEGY);
67: if (s != Z_OK) {
68: std::ostringstream err;
69: err << streamname << " Initialization failed: " << s;
70: throw Error(err.str());
71: }
72: }
73: ~Deflate() { deflateEnd(&z_deflate); }
74: int zlib_deflate(int flush = Z_SYNC_FLUSH)
75: {
76: return deflate(&z_deflate, flush);
77: }
78: };
79:
80:
81:
82:
83:
84: #endif