g4tools  5.4.0
zlib
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_zlib
5 #define tools_zlib
6 
7 // what is needed for root file compression with zlib.
8 
9 //NOTE : zlib contains deflate/inflate and also the gz functions to write/read a .gz file.
10 // The gz functions use also deflate/inflate.
11 
12 #ifndef TOOLS_USE_OUREX_ZLIB
13 #include <zlib.h>
14 #else
15 #include <ourex_zlib.h> //enforce ourex zlib
16 #endif
17 
18 #include <ostream>
19 
20 namespace tools {
21 
22 inline bool compress_buffer(std::ostream& a_out,
23  unsigned int a_level,
24  unsigned int a_srcsize,const char* a_src,
25  unsigned int a_tgtsize,char* a_tgt,
26  unsigned int& a_irep) {
27 
28  z_stream stream; // decompression stream
29 
30  stream.next_in = (Bytef*)(a_src);
31  stream.avail_in = (uInt)(a_srcsize);
32  stream.next_out = (Bytef*)a_tgt;
33  stream.avail_out = (uInt)(a_tgtsize);
34  stream.zalloc = (alloc_func)0;
35  stream.zfree = (free_func)0;
36  stream.opaque = (voidpf)0;
37  stream.total_in = 0; /*to quiet Coverity.*/
38  stream.total_out = 0; /*to quiet Coverity.*/
39 
40  int err = deflateInit(&stream,a_level);
41  if(err!=Z_OK) {
42  a_out << "tools::compress_buffer :"
43  << " error in zlib/deflateInit." << std::endl;
44  a_irep = 0;
45  return false;
46  }
47 
48  err = deflate(&stream, Z_FINISH);
49  if(err!=Z_STREAM_END) {
50  deflateEnd(&stream);
51  a_out << "tools::compress_buffer :"
52  << " error in zlib/deflate." << std::endl;
53  a_irep = 0;
54  return false;
55  }
56 
57  deflateEnd(&stream);
58 
59  //a_out << "tools::compress_buffer : ok "
60  // << stream.total_out << std::endl;
61 
62  a_irep = stream.total_out;
63 
64  return true;
65 }
66 
67 inline bool decompress_buffer(std::ostream& a_out,
68  unsigned int a_srcsize,const char* a_src,
69  unsigned int a_tgtsize,char* a_tgt,
70  unsigned int& a_irep) {
71 
72  z_stream stream; // decompression stream
73 
74  stream.next_in = (Bytef*)(a_src);
75  stream.avail_in = (uInt)(a_srcsize);
76  stream.next_out = (Bytef*)a_tgt;
77  stream.avail_out = (uInt)(a_tgtsize);
78  stream.zalloc = (alloc_func)0;
79  stream.zfree = (free_func)0;
80  stream.opaque = (voidpf)0;
81  stream.total_in = 0; /*to quiet Coverity.*/
82  stream.total_out = 0; /*to quiet Coverity.*/
83 
84  int err = inflateInit(&stream);
85  if (err != Z_OK) {
86  a_out << "tools::decompress_buffer :"
87  << " error " << err << " in zlib/inflateInit." << std::endl;
88  return false;
89  }
90 
91  err = inflate(&stream, Z_FINISH);
92  if (err != Z_STREAM_END) {
93  inflateEnd(&stream);
94  a_out << "tools::decompress_buffer :"
95  << " error " << err << " in zlib/inflate." << std::endl;
96  return false;
97  }
98 
99  inflateEnd(&stream);
100 
101  //a_out << "tools::decompress_buffer : zlib : ok "
102  // << stream.total_out << std::endl;
103 
104  a_irep = stream.total_out;
105 
106  return true;
107 }
108 
109 }
110 
111 #if ZLIB_VERNUM <= 0x1140
112 #include <cstdio>
113 #endif
114 
115 namespace tools {
116 
117 #if ZLIB_VERNUM <= 0x1140
118 inline int gunzip_get_byte(char*& a_buffer) {
119  int c = *a_buffer;a_buffer++;
120  return c;
121 }
122 
123 inline int gunzip_check_header(char*& a_buffer) {
124 #define TOOLS_ZLIB_HEAD_CRC 0x02 /* bit 1 set: header CRC present */
125 #define TOOLS_ZLIB_EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
126 #define TOOLS_ZLIB_ORIG_NAME 0x08 /* bit 3 set: original file name present */
127 #define TOOLS_ZLIB_COMMENT 0x10 /* bit 4 set: file comment present */
128 #define TOOLS_ZLIB_RESERVED 0xE0 /* bits 5..7: reserved */
129 
130  uInt len;
131  int c;
132 
133  /* Check the gzip magic header */
134  for (len = 0; len < 2; len++) {
135  c = gunzip_get_byte(a_buffer);
136 /*
137  if (c != gz_magic[len]) {
138  if (len != 0) s->stream.avail_in++, s->stream.next_in--;
139  if (c != EOF) {
140  s->stream.avail_in++, s->stream.next_in--;
141  s->transparent = 1;
142  }
143  s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
144  return;
145  }
146 */
147  }
148  int method = gunzip_get_byte(a_buffer);
149  int flags = gunzip_get_byte(a_buffer);
150  if (method != Z_DEFLATED || (flags & TOOLS_ZLIB_RESERVED) != 0) {
151  return Z_DATA_ERROR;
152  }
153 
154  /* Discard time, xflags and OS code: */
155  for (len = 0; len < 6; len++) (void)gunzip_get_byte(a_buffer);
156 
157  if ((flags & TOOLS_ZLIB_EXTRA_FIELD) != 0) { /* skip the extra field */
158  len = (uInt)gunzip_get_byte(a_buffer);
159  len += ((uInt)gunzip_get_byte(a_buffer))<<8;
160  /* len is garbage if EOF but the loop below will quit anyway */
161  while (len-- != 0 && gunzip_get_byte(a_buffer) != EOF) ;
162  }
163  if ((flags & TOOLS_ZLIB_ORIG_NAME) != 0) { /* skip the original file name */
164  while ((c = gunzip_get_byte(a_buffer)) != 0 && c != EOF) ;
165  }
166  if ((flags & TOOLS_ZLIB_COMMENT) != 0) { /* skip the .gz file comment */
167  while ((c = gunzip_get_byte(a_buffer)) != 0 && c != EOF) ;
168  }
169  if ((flags & TOOLS_ZLIB_HEAD_CRC) != 0) { /* skip the header crc */
170  for (len = 0; len < 2; len++) (void)gunzip_get_byte(a_buffer);
171  }
172  //s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
173  return Z_OK;
174 
175 #undef TOOLS_ZLIB_HEAD_CRC
176 #undef TOOLS_ZLIB_EXTRA_FIELD
177 #undef TOOLS_ZLIB_ORIG_NAME
178 #undef TOOLS_ZLIB_COMMENT
179 #undef TOOLS_ZLIB_RESERVED
180 }
181 #endif //ZLIB_VERNUM <= 0x1140
182 
183 inline bool gunzip_buffer(std::ostream& a_out,
184  unsigned int a_srcsize,const char* a_src,
185  unsigned int a_tgtsize,char* a_tgt,
186  unsigned int& a_irep) {
187 
188  z_stream stream; // decompression stream
189 
190 #if ZLIB_VERNUM <= 0x1140
191  char* pos = (char*)a_src;
192  if(gunzip_check_header(pos)!=Z_OK) return false;
193  stream.next_in = (Bytef*)pos;
194  stream.avail_in = (uInt)(a_srcsize-(pos-a_src));
195 #else
196  stream.next_in = (Bytef*)a_src;
197  stream.avail_in = (uInt)a_srcsize;
198 #endif //ZLIB_VERNUM
199 
200  stream.next_out = (Bytef*)a_tgt;
201  stream.avail_out = (uInt)a_tgtsize;
202  stream.zalloc = (alloc_func)0;
203  stream.zfree = (free_func)0;
204  stream.opaque = (voidpf)0;
205 
206 #if ZLIB_VERNUM <= 0x1140
207  int err = inflateInit2(&stream,-MAX_WBITS);
208 #else
209  int err = inflateInit2(&stream,MAX_WBITS+16);
210 #endif
211  if (err != Z_OK) {
212  a_out << "tools::gunzip_buffer :"
213  << " error " << err << " in zlib/inflateInit2." << std::endl;
214  return false;
215  }
216 
217  err = inflate(&stream, Z_FINISH);
218  if (err != Z_STREAM_END) {
219  inflateEnd(&stream);
220  a_out << "tools::gunzip_buffer :"
221  << " error " << err << " in zlib/inflate." << std::endl;
222  return false;
223  }
224 
225  inflateEnd(&stream);
226 
227  a_irep = stream.total_out;
228 
229  return true;
230 }
231 
232 }
233 
234 #endif
tools::decompress_buffer
bool decompress_buffer(std::ostream &a_out, unsigned int a_srcsize, const char *a_src, unsigned int a_tgtsize, char *a_tgt, unsigned int &a_irep)
Definition: zlib:67
tools::gunzip_get_byte
int gunzip_get_byte(char *&a_buffer)
Definition: zlib:118
tools::gunzip_check_header
int gunzip_check_header(char *&a_buffer)
Definition: zlib:123
tools::gunzip_buffer
bool gunzip_buffer(std::ostream &a_out, unsigned int a_srcsize, const char *a_src, unsigned int a_tgtsize, char *a_tgt, unsigned int &a_irep)
Definition: zlib:183
TOOLS_ZLIB_RESERVED
#define TOOLS_ZLIB_RESERVED
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
TOOLS_ZLIB_COMMENT
#define TOOLS_ZLIB_COMMENT
TOOLS_ZLIB_EXTRA_FIELD
#define TOOLS_ZLIB_EXTRA_FIELD
tools::compress_buffer
bool compress_buffer(std::ostream &a_out, unsigned int a_level, unsigned int a_srcsize, const char *a_src, unsigned int a_tgtsize, char *a_tgt, unsigned int &a_irep)
Definition: zlib:22
TOOLS_ZLIB_HEAD_CRC
#define TOOLS_ZLIB_HEAD_CRC
TOOLS_ZLIB_ORIG_NAME
#define TOOLS_ZLIB_ORIG_NAME