| 1 |
|
| /* Buffer size for inflate/deflate. */ |
| 2 |
|
| |
| 3 |
|
| #define CHUNK 0x4000 |
| 4 |
|
| |
| 5 |
|
| /* These are magic numbers for zlib, please refer to |
| 6 |
|
| "/usr/include/zlib.h" for the details. zlib does not provide any |
| 7 |
|
| macros for these. */ |
| 8 |
|
| |
| 9 |
|
| /* Just use the maximum. */ |
| 10 |
|
| #define windowBits 15 |
| 11 |
|
| #define DEFLATE_ENABLE_ZLIB_GZIP 16 |
| 12 |
|
| #define INFLATE_ENABLE_ZLIB_GZIP 32 |
| 13 |
|
| /* Default as given in zlib.h. */ |
| 14 |
|
| #define DEFAULT_MEMLEVEL 8 |
| 15 |
|
| |
| 16 |
|
| #define CALL_ZLIB(x) \ |
| 17 |
|
| zlib_status = x; \ |
| 18 |
|
| if (zlib_status < 0) { \ |
| 19 |
|
| deflateEnd (& gf->strm); \ |
| 20 |
|
| croak ("zlib call %s returned error status %d", \ |
| 21 |
|
| #x, zlib_status); \ |
| 22 |
|
| } \ |
| 23 |
|
| |
| 24 |
|
| typedef struct |
| 25 |
|
| { |
| 26 |
|
| /* Input. */ |
| 27 |
|
| SV * in; |
| 28 |
|
| const char * in_char; |
| 29 |
|
| STRLEN in_length; |
| 30 |
|
| /* Compression structure. */ |
| 31 |
|
| z_stream strm; |
| 32 |
|
| /* Compression level. */ |
| 33 |
|
| int level; |
| 34 |
|
| /* This holds the stuff. */ |
| 35 |
|
| unsigned char out_buffer[CHUNK]; |
| 36 |
|
| /* windowBits, adjusted for monkey business. */ |
| 37 |
|
| int wb; |
| 38 |
|
| /* Optional file name for gzip format. This can only take values |
| 39 |
|
| for user-visible objects. */ |
| 40 |
|
| SV * file_name; |
| 41 |
|
| /* User-defined modification time. */ |
| 42 |
|
| SV * mod_time; |
| 43 |
|
| /* Maximum length of the output of unzip, or 0 for no limit. */ |
| 44 |
|
| UV max_size; |
| 45 |
|
| /* Gzip, not deflate or inflate. */ |
| 46 |
|
| unsigned int is_gzip : 1; |
| 47 |
|
| /* "Raw" inflate or deflate without adler32 check. */ |
| 48 |
|
| unsigned int is_raw : 1; |
| 49 |
|
| /* Copy Perl flags like UTF8 flag? */ |
| 50 |
|
| unsigned int copy_perl_flags : 1; |
| 51 |
|
| /* User can see this object? */ |
| 52 |
|
| unsigned int user_object : 1; |
| 53 |
|
| } |
| 54 |
|
| gzip_faster_t; |
| 55 |
|
| |
| 56 |
|
| /* This is our marker for the extra buffer in the Gzip header. See |
| 57 |
|
| "http://www.gzip.org/zlib/rfc-gzip.html". */ |
| 58 |
|
| |
| 59 |
|
| #define GZIP_PERL_ID "GF\1\0" |
| 60 |
|
| #define GZIP_PERL_ID_LENGTH 4 |
| 61 |
|
| |
| 62 |
|
| /* Perl flags which go in our extra buffer. */ |
| 63 |
|
| |
| 64 |
|
| #define GZIP_PERL_LENGTH 1 |
| 65 |
|
| #define EXTRA_LENGTH GZIP_PERL_ID_LENGTH + GZIP_PERL_LENGTH |
| 66 |
|
| #define GZIP_PERL_UTF8 (1<<0) |
| 67 |
|
| |
| 68 |
|
| /* Add more Perl flags here like |
| 69 |
|
| |
| 70 |
|
| #define SOMETHING (1<<1) |
| 71 |
|
| |
| 72 |
|
| etc. */ |
| 73 |
|
| |
| 74 |
|
| /* Set up the gzip buffer in "gf->strm" using the scalar in |
| 75 |
|
| "gf->in". */ |
| 76 |
|
| |
| 77 |
|
| static void |
| 78 |
63 |
| gf_set_up (gzip_faster_t * gf) |
| 79 |
|
| { |
| 80 |
|
| /* Extract the information from "gf->in". Note that SvPV is a |
| 81 |
|
| macro, and so the following line of code sets "gf->in_length" |
| 82 |
|
| rather than retrieving it. */ |
| 83 |
63 |
| gf->in_char = SvPV (gf->in, gf->in_length); |
| 84 |
63 |
| gf->strm.next_in = (unsigned char *) gf->in_char; |
| 85 |
63 |
| gf->strm.avail_in = gf->in_length; |
| 86 |
63 |
| gf->strm.zalloc = Z_NULL; |
| 87 |
63 |
| gf->strm.zfree = Z_NULL; |
| 88 |
63 |
| gf->strm.opaque = Z_NULL; |
| 89 |
63 |
▶ | if (! gf->user_object) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! gf->user_object) { |
32 |
31 |
63 |
|
| 90 |
32 |
| gf->level = Z_DEFAULT_COMPRESSION; |
| 91 |
|
| } |
| 92 |
63 |
| gf->wb = windowBits; |
| 93 |
63 |
| } |
| 94 |
|
| |
| 95 |
|
| /* Check that we are always dealing with a user-defined object, not a |
| 96 |
|
| module-defined object, before altering the values. */ |
| 97 |
|
| |
| 98 |
|
| #define UO \ |
| 99 |
|
| if (! gf->user_object) { \ |
| 100 |
|
| croak ("%s:%d: THIS IS NOT A USER OBJECT", \ |
| 101 |
|
| __FILE__, __LINE__); \ |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
|
| /* Delete the file name in the object. */ |
| 105 |
|
| |
| 106 |
|
| static void |
| 107 |
23 |
| gf_delete_file_name (gzip_faster_t * gf) |
| 108 |
|
| { |
| 109 |
23 |
▶ | UO; |
Branch: 1 branch
| Branch | True | False | Total |
| UO; |
0 |
23 |
23 |
|
| 110 |
23 |
▶ | if (gf->file_name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->file_name) { |
10 |
13 |
23 |
|
| 111 |
|
| /* Decrement the reference count by one to tell Perl we are no |
| 112 |
|
| longer keeping a pointer to this SV. */ |
| 113 |
10 |
| SvREFCNT_dec (gf->file_name); |
| 114 |
10 |
| gf->file_name = 0; |
| 115 |
|
| } |
| 116 |
23 |
| } |
| 117 |
|
| |
| 118 |
|
| /* Delete the file name in the object. */ |
| 119 |
|
| |
| 120 |
|
| static void |
| 121 |
19 |
| gf_delete_mod_time (gzip_faster_t * gf) |
| 122 |
|
| { |
| 123 |
19 |
▶ | UO; |
Branch: 1 branch
| Branch | True | False | Total |
| UO; |
0 |
19 |
19 |
|
| 124 |
19 |
▶ | if (gf->mod_time) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->mod_time) { |
2 |
17 |
19 |
|
| 125 |
|
| /* Decrement the reference count by one to tell Perl we are no |
| 126 |
|
| longer keeping a pointer to this SV. */ |
| 127 |
2 |
| SvREFCNT_dec (gf->mod_time); |
| 128 |
2 |
| gf->mod_time = 0; |
| 129 |
|
| } |
| 130 |
19 |
| } |
| 131 |
|
| |
| 132 |
|
| static void |
| 133 |
4 |
| gf_set_file_name (gzip_faster_t * gf, SV * file_name) |
| 134 |
|
| { |
| 135 |
4 |
▶ | UO; |
Branch: 1 branch
| Branch | True | False | Total |
| UO; |
0 |
4 |
4 |
|
| 136 |
4 |
▶ | if (gf->file_name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->file_name) { |
0 |
4 |
4 |
|
| 137 |
0 |
| gf_delete_file_name (gf); |
| 138 |
|
| } |
| 139 |
|
| /* Add one to the reference count of the SV to tell Perl we have a |
| 140 |
|
| pointer to it in our structure, in case it loses all its other |
| 141 |
|
| references and Perl decides to delete it. */ |
| 142 |
4 |
| SvREFCNT_inc (file_name); |
| 143 |
4 |
| gf->file_name = file_name; |
| 144 |
4 |
| } |
| 145 |
|
| |
| 146 |
|
| static SV * |
| 147 |
7 |
| gf_get_file_name (gzip_faster_t * gf) |
| 148 |
|
| { |
| 149 |
7 |
▶ | UO; |
Branch: 1 branch
| Branch | True | False | Total |
| UO; |
0 |
7 |
7 |
|
| 150 |
7 |
▶ | if (gf->file_name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->file_name) { |
5 |
2 |
7 |
|
| 151 |
5 |
| return gf->file_name; |
| 152 |
|
| } |
| 153 |
2 |
| return & PL_sv_undef; |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
|
| static void |
| 157 |
1 |
| gf_set_mod_time (gzip_faster_t * gf, SV * mod_time) |
| 158 |
|
| { |
| 159 |
1 |
▶ | UO; |
Branch: 1 branch
| Branch | True | False | Total |
| UO; |
0 |
1 |
1 |
|
| 160 |
1 |
▶ | if (gf->mod_time) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->mod_time) { |
0 |
1 |
1 |
|
| 161 |
0 |
| gf_delete_mod_time (gf); |
| 162 |
|
| } |
| 163 |
|
| /* Add one to the reference count of the SV to tell Perl we have a |
| 164 |
|
| pointer to it in our structure, in case it loses all its other |
| 165 |
|
| references and Perl decides to delete it. */ |
| 166 |
1 |
| SvREFCNT_inc (mod_time); |
| 167 |
1 |
| gf->mod_time = mod_time; |
| 168 |
1 |
| } |
| 169 |
|
| |
| 170 |
|
| static SV * |
| 171 |
5 |
| gf_get_mod_time (gzip_faster_t * gf) |
| 172 |
|
| { |
| 173 |
5 |
▶ | UO; |
Branch: 1 branch
| Branch | True | False | Total |
| UO; |
0 |
5 |
5 |
|
| 174 |
5 |
▶ | if (gf->mod_time) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->mod_time) { |
1 |
4 |
5 |
|
| 175 |
1 |
| return gf->mod_time; |
| 176 |
|
| } |
| 177 |
4 |
| return & PL_sv_undef; |
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
|
| /* Check for an unlikely condition after the end of the |
| 181 |
|
| inflate/deflate loop. */ |
| 182 |
|
| |
| 183 |
|
| static void |
| 184 |
23 |
| check_avail_in (gzip_faster_t * gf) |
| 185 |
|
| { |
| 186 |
23 |
▶ | if (gf->strm.avail_in != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->strm.avail_in != 0) { |
0 |
23 |
23 |
|
| 187 |
0 |
| croak ("Zlib did not finish processing the string: %d bytes left", |
| 188 |
|
| gf->strm.avail_in); |
| 189 |
|
| } |
| 190 |
23 |
| } |
| 191 |
|
| |
| 192 |
|
| /* Check for an unlikely condition after the end of the |
| 193 |
|
| inflate/deflate loop. */ |
| 194 |
|
| |
| 195 |
|
| static void |
| 196 |
23 |
| check_zlib_status (int zlib_status) |
| 197 |
|
| { |
| 198 |
23 |
▶ | if (zlib_status != Z_STREAM_END) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (zlib_status != Z_STREAM_END) { |
0 |
23 |
23 |
|
| 199 |
0 |
| croak ("Zlib did not come to the end of the string: zlib_status = %d", |
| 200 |
|
| zlib_status); |
| 201 |
|
| } |
| 202 |
23 |
| } |
| 203 |
|
| |
| 204 |
|
| /* Compress "gf->in" (an SV *) and return the compressed value. */ |
| 205 |
|
| |
| 206 |
|
| static SV * |
| 207 |
26 |
| gzip_faster (gzip_faster_t * gf) |
| 208 |
|
| { |
| 209 |
|
| /* The output. */ |
| 210 |
|
| SV * zipped; |
| 211 |
|
| /* The message from zlib. */ |
| 212 |
|
| int zlib_status; |
| 213 |
|
| |
| 214 |
26 |
▶ | if (! SvOK (gf->in)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! SvOK (gf->in)) { |
0 |
26 |
26 |
|
| 215 |
0 |
| warn ("Empty input"); |
| 216 |
0 |
| return & PL_sv_undef; |
| 217 |
|
| } |
| 218 |
26 |
| gf_set_up (gf); |
| 219 |
26 |
▶ | if (gf->in_length == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->in_length == 0) { |
3 |
23 |
26 |
|
| 220 |
3 |
| warn ("Attempt to compress empty string"); |
| 221 |
3 |
| return & PL_sv_undef; |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
|
| /* Set the windowBits parameter "gf->wb" depending on what kind of |
| 225 |
|
| compression the user wants. */ |
| 226 |
23 |
▶ | if (gf->is_gzip) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->is_gzip) { |
19 |
4 |
23 |
|
| 227 |
19 |
▶ | if (gf->is_raw) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->is_raw) { |
0 |
19 |
19 |
|
| 228 |
0 |
| croak ("Raw deflate and gzip are incompatible"); |
| 229 |
|
| } |
| 230 |
|
| /* From zlib.h: "windowBits can also be greater than 15 for |
| 231 |
|
| optional gzip decoding. Add 32 to windowBits to enable |
| 232 |
|
| zlib and gzip decoding with automatic header detection, or |
| 233 |
|
| add 16 to decode only the gzip format (the zlib format will |
| 234 |
|
| return a Z_DATA_ERROR)." */ |
| 235 |
|
| |
| 236 |
19 |
| gf->wb += DEFLATE_ENABLE_ZLIB_GZIP; |
| 237 |
|
| } |
| 238 |
|
| else { |
| 239 |
4 |
▶ | if (gf->is_raw) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->is_raw) { |
3 |
1 |
4 |
|
| 240 |
|
| |
| 241 |
|
| /* From zlib.h: " windowBits can also be -8..-15 for raw |
| 242 |
|
| inflate. In this case, -windowBits determines the |
| 243 |
|
| window size. inflate() will then process raw deflate |
| 244 |
|
| data, not looking for a zlib or gzip header, not |
| 245 |
|
| generating a check value, and not looking for any check |
| 246 |
|
| values for comparison at the end of the stream. " */ |
| 247 |
|
| |
| 248 |
3 |
| gf->wb = -gf->wb; |
| 249 |
|
| } |
| 250 |
|
| } |
| 251 |
|
| /* The second-to-final parameter is "memLevel". Currently there is |
| 252 |
|
| no way for the user to set this. */ |
| 253 |
23 |
▶ | CALL_ZLIB (deflateInit2 (& gf->strm, gf->level, Z_DEFLATED, |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_ZLIB (deflateInit2 (& gf->strm, gf->level, Z_DEFLATED, |
0 |
23 |
23 |
|
| 254 |
|
| gf->wb, DEFAULT_MEMLEVEL, |
| 255 |
|
| Z_DEFAULT_STRATEGY)); |
| 256 |
|
| |
| 257 |
23 |
▶ | if (gf->user_object) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->user_object) { |
11 |
12 |
23 |
|
| 258 |
11 |
▶ | if (gf->is_gzip) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->is_gzip) { |
10 |
1 |
11 |
|
| 259 |
10 |
| gz_header header = {0}; |
| 260 |
|
| unsigned char extra[EXTRA_LENGTH]; |
| 261 |
|
| /* Have at least one of the fields in the header been set? */ |
| 262 |
|
| int set_header; |
| 263 |
10 |
| set_header = 0; |
| 264 |
10 |
▶ | if (gf->copy_perl_flags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->copy_perl_flags) { |
2 |
8 |
10 |
|
| 265 |
|
| /* Copy the Perl flags of "in" into the compressed |
| 266 |
|
| output. */ |
| 267 |
2 |
| memcpy (extra, GZIP_PERL_ID, GZIP_PERL_ID_LENGTH); |
| 268 |
2 |
| extra[GZIP_PERL_ID_LENGTH] = 0; |
| 269 |
2 |
▶ | if (SvUTF8 (gf->in)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8 (gf->in)) { |
1 |
1 |
2 |
|
| 270 |
1 |
| extra[GZIP_PERL_ID_LENGTH] |= GZIP_PERL_UTF8; |
| 271 |
|
| } |
| 272 |
2 |
| header.extra = extra; |
| 273 |
2 |
| header.extra_len = EXTRA_LENGTH; |
| 274 |
2 |
| set_header++; |
| 275 |
|
| } |
| 276 |
10 |
▶ | if (gf->file_name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->file_name) { |
4 |
6 |
10 |
|
| 277 |
|
| char * fn; |
| 278 |
|
| /* zlib doesn't use the length of the name, it uses |
| 279 |
|
| nul-termination of the string to indicate the |
| 280 |
|
| length of the file name. */ |
| 281 |
4 |
| fn = SvPV_nolen (gf->file_name); |
| 282 |
4 |
| header.name = (Bytef *) fn; |
| 283 |
4 |
| set_header++; |
| 284 |
|
| } |
| 285 |
10 |
▶ | if (gf->mod_time) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->mod_time) { |
1 |
9 |
10 |
|
| 286 |
1 |
| header.time = (uLong) SvUV (gf->mod_time); |
| 287 |
1 |
| set_header++; |
| 288 |
|
| } |
| 289 |
10 |
▶ | if (set_header) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (set_header) { |
6 |
4 |
10 |
|
| 290 |
6 |
▶ | CALL_ZLIB (deflateSetHeader (& gf->strm, & header)); |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_ZLIB (deflateSetHeader (& gf->strm, & header)); |
0 |
6 |
6 |
|
| 291 |
|
| } |
| 292 |
|
| } |
| 293 |
|
| else { |
| 294 |
1 |
▶ | if (gf->copy_perl_flags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->copy_perl_flags) { |
0 |
1 |
1 |
|
| 295 |
0 |
| warn ("wrong format: perl flags not copied: use gzip_format(1)"); |
| 296 |
|
| } |
| 297 |
1 |
▶ | if (gf->file_name || gf->mod_time) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (gf->file_name || gf->mod_time) { |
1 |
0 |
1 |
| if (gf->file_name || gf->mod_time) { |
0 |
1 |
1 |
|
| 298 |
0 |
| warn ("wrong format: file name/modification time ignored: use gzip_format(1)"); |
| 299 |
|
| } |
| 300 |
|
| } |
| 301 |
|
| } |
| 302 |
|
| /* Mark the return value as uninitialised. */ |
| 303 |
23 |
| zipped = 0; |
| 304 |
|
| |
| 305 |
|
| do { |
| 306 |
|
| unsigned int have; |
| 307 |
23 |
| gf->strm.avail_out = CHUNK; |
| 308 |
23 |
| gf->strm.next_out = gf->out_buffer; |
| 309 |
23 |
| zlib_status = deflate (& gf->strm, Z_FINISH); |
| 310 |
23 |
| switch (zlib_status) { |
| 311 |
23 |
| case Z_OK: |
| 312 |
|
| case Z_STREAM_END: |
| 313 |
|
| case Z_BUF_ERROR: |
| 314 |
|
| /* Keep on chugging. */ |
| 315 |
23 |
| break; |
| 316 |
|
| |
| 317 |
0 |
| case Z_STREAM_ERROR: |
| 318 |
0 |
| deflateEnd (& gf->strm); |
| 319 |
|
| /* This is supposed to never happen, but just in case it |
| 320 |
|
| does. */ |
| 321 |
0 |
| croak ("Z_STREAM_ERROR from zlib during deflate"); |
| 322 |
|
| break; |
| 323 |
|
| |
| 324 |
0 |
| default: |
| 325 |
0 |
| deflateEnd (& gf->strm); |
| 326 |
0 |
| croak ("Unknown status %d from deflate", zlib_status); |
| 327 |
|
| break; |
| 328 |
|
| } |
| 329 |
|
| /* The number of bytes we "have". */ |
| 330 |
23 |
| have = CHUNK - gf->strm.avail_out; |
| 331 |
23 |
▶ | if (! zipped) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! zipped) { |
23 |
0 |
23 |
|
| 332 |
23 |
| zipped = newSVpv ((const char *) gf->out_buffer, have); |
| 333 |
|
| } |
| 334 |
|
| else { |
| 335 |
0 |
| sv_catpvn (zipped, (const char *) gf->out_buffer, have); |
| 336 |
|
| } |
| 337 |
|
| } |
| 338 |
23 |
▶ | while (gf->strm.avail_out == 0); |
Branch: 1 branch
| Branch | True | False | Total |
| while (gf->strm.avail_out == 0); |
0 |
23 |
23 |
|
| 339 |
|
| /* Check completed ok */ |
| 340 |
23 |
| check_avail_in (gf); |
| 341 |
23 |
| check_zlib_status (zlib_status); |
| 342 |
|
| /* zlib clean up */ |
| 343 |
23 |
| deflateEnd (& gf->strm); |
| 344 |
|
| /* Delete any user-defined file names so that they don't |
| 345 |
|
| accidentally get recycled into the next thing to be zipped. */ |
| 346 |
23 |
▶ | if (gf->user_object) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->user_object) { |
11 |
12 |
23 |
|
| 347 |
11 |
▶ | if (gf->file_name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->file_name) { |
4 |
7 |
11 |
|
| 348 |
4 |
| gf_delete_file_name (gf); |
| 349 |
|
| } |
| 350 |
|
| } |
| 351 |
|
| /* Safety measure. We don't expect this to happen, but on the |
| 352 |
|
| other hand we have set "zipped" to be zero before the |
| 353 |
|
| do...while loop, so just check it didn't accidentally remain |
| 354 |
|
| unset here. */ |
| 355 |
23 |
▶ | if (! zipped) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! zipped) { |
0 |
23 |
23 |
|
| 356 |
0 |
| croak ("%s:%d: failed to set zipped", __FILE__, __LINE__); |
| 357 |
|
| } |
| 358 |
23 |
| return zipped; |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
|
| /* The maximum length of file name which we allow for. */ |
| 362 |
|
| |
| 363 |
|
| #define GF_FILE_NAME_MAX 0x400 |
| 364 |
|
| |
| 365 |
|
| /* Free the zlib state and any partial output of an unzip which is |
| 366 |
|
| about to fail. */ |
| 367 |
|
| |
| 368 |
|
| static void |
| 369 |
3 |
| gunzip_fail (gzip_faster_t * gf, SV * plain) |
| 370 |
|
| { |
| 371 |
3 |
| inflateEnd (& gf->strm); |
| 372 |
3 |
▶ | if (plain) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (plain) { |
1 |
2 |
3 |
|
| 373 |
1 |
| SvREFCNT_dec (plain); |
| 374 |
|
| } |
| 375 |
3 |
| } |
| 376 |
|
| |
| 377 |
|
| /* Decompress "gf->in" (an SV *) and return the decompressed value. */ |
| 378 |
|
| |
| 379 |
|
| static SV * |
| 380 |
38 |
| gunzip_faster (gzip_faster_t * gf) |
| 381 |
|
| { |
| 382 |
|
| /* The return value. */ |
| 383 |
|
| SV * plain; |
| 384 |
|
| /* The message from zlib. */ |
| 385 |
|
| int zlib_status; |
| 386 |
|
| /* The gzip library header. */ |
| 387 |
|
| gz_header header; |
| 388 |
|
| /* The name of the file, if it exists. */ |
| 389 |
|
| unsigned char name[GF_FILE_NAME_MAX]; |
| 390 |
|
| /* Extra bytes in the header, if they exist. */ |
| 391 |
|
| unsigned char extra[EXTRA_LENGTH]; |
| 392 |
|
| |
| 393 |
38 |
▶ | if (! SvOK (gf->in)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! SvOK (gf->in)) { |
1 |
37 |
38 |
|
| 394 |
1 |
| warn ("Empty input"); |
| 395 |
1 |
| return & PL_sv_undef; |
| 396 |
|
| } |
| 397 |
37 |
| gf_set_up (gf); |
| 398 |
37 |
▶ | if (gf->in_length == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->in_length == 0) { |
3 |
34 |
37 |
|
| 399 |
3 |
| warn ("Attempt to uncompress empty string"); |
| 400 |
3 |
| return & PL_sv_undef; |
| 401 |
|
| } |
| 402 |
|
| |
| 403 |
34 |
▶ | if (gf->is_gzip) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->is_gzip) { |
29 |
5 |
34 |
|
| 404 |
29 |
▶ | if (gf->is_raw) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->is_raw) { |
0 |
29 |
29 |
|
| 405 |
0 |
| croak ("Raw deflate and gzip are incompatible"); |
| 406 |
|
| } |
| 407 |
29 |
| gf->wb += INFLATE_ENABLE_ZLIB_GZIP; |
| 408 |
|
| } |
| 409 |
|
| else { |
| 410 |
5 |
▶ | if (gf->is_raw) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->is_raw) { |
3 |
2 |
5 |
|
| 411 |
3 |
| gf->wb = -gf->wb; |
| 412 |
|
| } |
| 413 |
|
| } |
| 414 |
34 |
▶ | CALL_ZLIB (inflateInit2 (& gf->strm, gf->wb)); |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_ZLIB (inflateInit2 (& gf->strm, gf->wb)); |
0 |
34 |
34 |
|
| 415 |
34 |
▶ | if (gf->user_object) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->user_object) { |
20 |
14 |
34 |
|
| 416 |
20 |
▶ | if (gf->is_gzip) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->is_gzip) { |
19 |
1 |
20 |
|
| 417 |
19 |
▶ | if (gf->copy_perl_flags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->copy_perl_flags) { |
2 |
17 |
19 |
|
| 418 |
2 |
| header.extra = extra; |
| 419 |
2 |
| header.extra_max = EXTRA_LENGTH; |
| 420 |
|
| } |
| 421 |
|
| /* If there are stale file names or modification times in |
| 422 |
|
| our object, delete them. */ |
| 423 |
19 |
▶ | if (gf->file_name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->file_name) { |
0 |
19 |
19 |
|
| 424 |
0 |
| gf_delete_file_name (gf); |
| 425 |
|
| } |
| 426 |
19 |
▶ | if (gf->mod_time) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->mod_time) { |
0 |
19 |
19 |
|
| 427 |
0 |
| gf_delete_mod_time (gf); |
| 428 |
|
| } |
| 429 |
|
| /* Point the header values to our buffer, "name", and give |
| 430 |
|
| the length of the buffer. */ |
| 431 |
19 |
| header.name = name; |
| 432 |
19 |
| header.name_max = GF_FILE_NAME_MAX; |
| 433 |
19 |
| inflateGetHeader (& gf->strm, & header); |
| 434 |
|
| } |
| 435 |
|
| } |
| 436 |
|
| |
| 437 |
|
| /* Mark the return value as uninitialised. */ |
| 438 |
34 |
| plain = 0; |
| 439 |
|
| |
| 440 |
|
| do { |
| 441 |
|
| unsigned int have; |
| 442 |
66 |
| gf->strm.avail_out = CHUNK; |
| 443 |
66 |
| gf->strm.next_out = gf->out_buffer; |
| 444 |
66 |
| zlib_status = inflate (& gf->strm, Z_FINISH); |
| 445 |
66 |
| switch (zlib_status) { |
| 446 |
65 |
| case Z_OK: |
| 447 |
|
| case Z_STREAM_END: |
| 448 |
|
| case Z_BUF_ERROR: |
| 449 |
65 |
| break; |
| 450 |
|
| |
| 451 |
1 |
| case Z_DATA_ERROR: |
| 452 |
1 |
| gunzip_fail (gf, plain); |
| 453 |
1 |
| croak ("Data input to inflate is not in libz format"); |
| 454 |
|
| break; |
| 455 |
|
| |
| 456 |
0 |
| case Z_MEM_ERROR: |
| 457 |
0 |
| gunzip_fail (gf, plain); |
| 458 |
0 |
| croak ("Out of memory during inflate"); |
| 459 |
|
| |
| 460 |
0 |
| case Z_STREAM_ERROR: |
| 461 |
0 |
| gunzip_fail (gf, plain); |
| 462 |
0 |
| croak ("Internal error in zlib"); |
| 463 |
|
| |
| 464 |
0 |
| default: |
| 465 |
0 |
| gunzip_fail (gf, plain); |
| 466 |
0 |
| croak ("Unknown status %d from inflate", zlib_status); |
| 467 |
|
| break; |
| 468 |
|
| } |
| 469 |
65 |
| have = CHUNK - gf->strm.avail_out; |
| 470 |
65 |
▶ | if (gf->user_object && gf->max_size && |
Branch: 2 branches
| Branch | True | False | Total |
| if (gf->user_object && gf->max_size && |
45 |
20 |
65 |
| if (gf->user_object && gf->max_size && |
16 |
29 |
45 |
|
| 471 |
16 |
▶ | (plain ? SvCUR (plain) : 0) + have > gf->max_size) { |
Branch: 2 branches
| Branch | True | False | Total |
| (plain ? SvCUR (plain) : 0) + have > gf->max_size) { |
12 |
4 |
16 |
| (plain ? SvCUR (plain) : 0) + have > gf->max_size) { |
2 |
14 |
16 |
|
| 472 |
2 |
| gunzip_fail (gf, plain); |
| 473 |
2 |
| croak ("Uncompressed data exceeds max_size of %" UVuf " bytes", |
| 474 |
|
| gf->max_size); |
| 475 |
|
| } |
| 476 |
63 |
▶ | if (! plain) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! plain) { |
32 |
31 |
63 |
|
| 477 |
|
| /* If the return value is uninitialised, set up a new |
| 478 |
|
| one. */ |
| 479 |
32 |
| plain = newSVpvn ((const char *) gf->out_buffer, have); |
| 480 |
|
| } |
| 481 |
|
| else { |
| 482 |
|
| /* If the return value was initialised, append the |
| 483 |
|
| contents of "gf->out_buffer" to it using |
| 484 |
|
| "sv_catpvn". */ |
| 485 |
31 |
| sv_catpvn (plain, (const char *) gf->out_buffer, have); |
| 486 |
|
| } |
| 487 |
|
| } |
| 488 |
63 |
▶ | while (gf->strm.avail_out == 0); |
Branch: 1 branch
| Branch | True | False | Total |
| while (gf->strm.avail_out == 0); |
32 |
31 |
63 |
|
| 489 |
|
| /* Check everything is OK. */ |
| 490 |
31 |
▶ | if (gf->strm.avail_in != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->strm.avail_in != 0) { |
0 |
31 |
31 |
|
| 491 |
0 |
| unsigned int left = gf->strm.avail_in; |
| 492 |
0 |
| gunzip_fail (gf, plain); |
| 493 |
0 |
| croak ("Zlib did not finish processing the string: %d bytes left", |
| 494 |
|
| left); |
| 495 |
|
| } |
| 496 |
31 |
▶ | if (zlib_status != Z_STREAM_END) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (zlib_status != Z_STREAM_END) { |
0 |
31 |
31 |
|
| 497 |
0 |
| gunzip_fail (gf, plain); |
| 498 |
0 |
| croak ("Zlib did not come to the end of the string: zlib_status = %d", |
| 499 |
|
| zlib_status); |
| 500 |
|
| } |
| 501 |
|
| /* Clean up. */ |
| 502 |
31 |
| inflateEnd (& gf->strm); |
| 503 |
|
| /* "header.done" is filled by zlib. */ |
| 504 |
31 |
▶ | if (gf->user_object && gf->is_gzip && header.done == 1) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (gf->user_object && gf->is_gzip && header.done == 1) { |
18 |
13 |
31 |
| if (gf->user_object && gf->is_gzip && header.done == 1) { |
17 |
1 |
18 |
| if (gf->user_object && gf->is_gzip && header.done == 1) { |
17 |
0 |
17 |
|
| 505 |
|
| |
| 506 |
17 |
▶ | if (gf->copy_perl_flags) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gf->copy_perl_flags) { |
2 |
15 |
17 |
|
| 507 |
|
| /* Check whether the "extra" segment of the header looks |
| 508 |
|
| like one of ours. */ |
| 509 |
2 |
▶ | if (strncmp ((const char *) header.extra, GZIP_PERL_ID, |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp ((const char *) header.extra, GZIP_PERL_ID, |
2 |
0 |
2 |
|
| 510 |
|
| GZIP_PERL_ID_LENGTH) == 0) { |
| 511 |
|
| /* Copy the Perl flags from the extra segment. */ |
| 512 |
|
| unsigned is_utf8; |
| 513 |
2 |
| is_utf8 = header.extra[GZIP_PERL_ID_LENGTH] & GZIP_PERL_UTF8; |
| 514 |
2 |
▶ | if (is_utf8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_utf8) { |
1 |
1 |
2 |
|
| 515 |
1 |
| SvUTF8_on (plain); |
| 516 |
|
| } |
| 517 |
|
| } |
| 518 |
|
| } |
| 519 |
|
| |
| 520 |
|
| /* If the header includes a file name, copy it into |
| 521 |
|
| $gf->file_name. */ |
| 522 |
17 |
▶ | if (header.name && header.name_max > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (header.name && header.name_max > 0) { |
6 |
11 |
17 |
| if (header.name && header.name_max > 0) { |
6 |
0 |
6 |
|
| 523 |
6 |
| gf->file_name = newSVpv ((const char *) header.name, 0); |
| 524 |
|
| } |
| 525 |
|
| |
| 526 |
|
| /* If the header includes a modification time, copy it into |
| 527 |
|
| $gf->mod_time. */ |
| 528 |
17 |
▶ | if (header.time) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (header.time) { |
1 |
16 |
17 |
|
| 529 |
|
| /* I'm not 100% sure of the type conversion |
| 530 |
|
| here. header.time is uLong in the zlib documentation, |
| 531 |
|
| and UV is unsigned int, or something? */ |
| 532 |
1 |
| gf->mod_time = newSVuv (header.time); |
| 533 |
|
| } |
| 534 |
|
| |
| 535 |
|
| /* Old file names and modification times have been deleted |
| 536 |
|
| before the call to inflate, so we don't need to delete them |
| 537 |
|
| again here. */ |
| 538 |
|
| |
| 539 |
|
| } |
| 540 |
|
| /* Safety measure. See similar comment above. */ |
| 541 |
31 |
▶ | if (! plain) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! plain) { |
0 |
31 |
31 |
|
| 542 |
0 |
| croak ("%s:%d: failed to set plain", __FILE__, __LINE__); |
| 543 |
|
| } |
| 544 |
31 |
| return plain; |
| 545 |
|
| } |
| 546 |
|
| |
| 547 |
|
| /* Set the values of "gf" to the module defaults. */ |
| 548 |
|
| |
| 549 |
|
| static void |
| 550 |
19 |
| new_user_object (gzip_faster_t * gf) |
| 551 |
|
| { |
| 552 |
19 |
| gf->file_name = 0; |
| 553 |
19 |
| gf->mod_time = 0; |
| 554 |
19 |
| gf->max_size = 0; |
| 555 |
19 |
| gf->is_gzip = 1; |
| 556 |
19 |
| gf->is_raw = 0; |
| 557 |
19 |
| gf->user_object = 1; |
| 558 |
19 |
| gf->level = Z_DEFAULT_COMPRESSION; |
| 559 |
19 |
| } |
| 560 |
|
| |
| 561 |
|
| /* Set the compression level of "gf" to "level", with checks. */ |
| 562 |
|
| |
| 563 |
|
| static void |
| 564 |
3 |
| set_compression_level (gzip_faster_t * gf, int level) |
| 565 |
|
| { |
| 566 |
3 |
▶ | if (level < Z_NO_COMPRESSION) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (level < Z_NO_COMPRESSION) { |
1 |
2 |
3 |
|
| 567 |
1 |
| warn ("Cannot set compression level to less than %d", |
| 568 |
|
| Z_NO_COMPRESSION); |
| 569 |
1 |
| gf->level = Z_NO_COMPRESSION; |
| 570 |
|
| } |
| 571 |
2 |
▶ | else if (level > Z_BEST_COMPRESSION) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (level > Z_BEST_COMPRESSION) { |
1 |
1 |
2 |
|
| 572 |
1 |
| warn ("Cannot set compression level to more than %d", |
| 573 |
|
| Z_BEST_COMPRESSION); |
| 574 |
1 |
| gf->level = Z_BEST_COMPRESSION; |
| 575 |
|
| } |
| 576 |
|
| else { |
| 577 |
1 |
| gf->level = level; |
| 578 |
|
| } |
| 579 |
3 |
| } |