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10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /externals/g4tools/include/toolx/zlib (Version 11.3.0) and /externals/g4tools/include/toolx/zlib (Version 11.1.2)


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