| 1 |
|
| typedef enum { |
| 2 |
|
| libdeflate_none = 0, |
| 3 |
|
| libdeflate_deflate = 1, |
| 4 |
|
| libdeflate_gzip = 2, |
| 5 |
|
| libdeflate_zlib = 3, |
| 6 |
|
| } |
| 7 |
|
| gzip_libdeflate_type_t; |
| 8 |
|
| |
| 9 |
|
| typedef struct { |
| 10 |
|
| /* Type (gzip, zlib, deflate) */ |
| 11 |
|
| gzip_libdeflate_type_t t; |
| 12 |
|
| /* Compressor's level. */ |
| 13 |
|
| int level; |
| 14 |
|
| struct libdeflate_compressor * c; |
| 15 |
|
| struct libdeflate_decompressor * d; |
| 16 |
|
| /* Debugging flag */ |
| 17 |
|
| unsigned int verbose : 1; |
| 18 |
|
| unsigned int init_ok : 1; |
| 19 |
|
| } |
| 20 |
|
| gzip_libdeflate_t; |
| 21 |
|
| |
| 22 |
|
| struct type2name { |
| 23 |
|
| const char * name; |
| 24 |
|
| int namelen; |
| 25 |
|
| int value; |
| 26 |
|
| } |
| 27 |
|
| gl_type_name[] = { |
| 28 |
|
| {"deflate", strlen ("deflate"), libdeflate_deflate}, |
| 29 |
|
| {"gzip", strlen ("gzip"), libdeflate_gzip}, |
| 30 |
|
| {"zlib", strlen ("zlib"), libdeflate_zlib}, |
| 31 |
|
| }; |
| 32 |
|
| |
| 33 |
|
| #define N_TYPES (sizeof (gl_type_name)/sizeof (struct type2name)) |
| 34 |
|
| |
| 35 |
|
| #define DEBUG |
| 36 |
|
| #ifdef DEBUG |
| 37 |
|
| #define MSG(format, args...) \ |
| 38 |
|
| if (gl->verbose) { \ |
| 39 |
|
| fprintf (stderr, "%s:%d: ", __FILE__, __LINE__); \ |
| 40 |
|
| fprintf (stderr, format, ## args); \ |
| 41 |
|
| fprintf (stderr, "\n"); \ |
| 42 |
|
| } |
| 43 |
|
| #else |
| 44 |
|
| #define MSG(format, args...) |
| 45 |
|
| #endif /* def DEBUG */ |
| 46 |
|
| |
| 47 |
|
| static SV * |
| 48 |
3 |
| gl_get_type (gzip_libdeflate_t * gl) |
| 49 |
|
| { |
| 50 |
|
| int i; |
| 51 |
6 |
▶ | for (i = 0; i < N_TYPES; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < N_TYPES; i++) { |
6 |
0 |
6 |
|
| 52 |
|
| struct type2name * b; |
| 53 |
6 |
| b = & gl_type_name[i]; |
| 54 |
6 |
▶ | if (gl->t == b->value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gl->t == b->value) { |
3 |
3 |
6 |
|
| 55 |
3 |
| return newSVpv (b->name, b->namelen); |
| 56 |
|
| } |
| 57 |
|
| } |
| 58 |
0 |
| return &PL_sv_undef; |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
|
| static void |
| 62 |
36 |
| gl_set_type (gzip_libdeflate_t * gl, int type) |
| 63 |
|
| { |
| 64 |
36 |
▶ | if (type < 1 || type > 3) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (type < 1 || type > 3) { |
36 |
0 |
36 |
| if (type < 1 || type > 3) { |
0 |
36 |
36 |
|
| 65 |
0 |
| warn ("Type out of bounds %d", type); |
| 66 |
0 |
| return; |
| 67 |
|
| } |
| 68 |
36 |
▶ | MSG ("Setting type to %d", type); |
Branch: 1 branch
| Branch | True | False | Total |
| MSG ("Setting type to %d", type); |
0 |
36 |
36 |
|
| 69 |
36 |
| gl->t = type; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
|
| static SV * |
| 73 |
0 |
| gl_get_level (gzip_libdeflate_t * gl) |
| 74 |
|
| { |
| 75 |
0 |
| return newSViv (gl->level); |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
|
| static void |
| 79 |
36 |
| gl_set_level (gzip_libdeflate_t * gl, int level) |
| 80 |
|
| { |
| 81 |
36 |
▶ | if (level < 0 || level > 12) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (level < 0 || level > 12) { |
36 |
0 |
36 |
| if (level < 0 || level > 12) { |
0 |
36 |
36 |
|
| 82 |
0 |
| warn ("Level out of bounds %d", level); |
| 83 |
0 |
| return; |
| 84 |
|
| } |
| 85 |
36 |
▶ | MSG ("Setting level to %d", level); |
Branch: 1 branch
| Branch | True | False | Total |
| MSG ("Setting level to %d", level); |
0 |
36 |
36 |
|
| 86 |
36 |
| gl->level = level; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| static void |
| 90 |
149 |
| gl_check (gzip_libdeflate_t * gl) |
| 91 |
|
| { |
| 92 |
149 |
▶ | if (! gl->init_ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! gl->init_ok) { |
0 |
149 |
149 |
|
| 93 |
0 |
| croak ("%s:%d: BUG: Uninitialised gl", __FILE__, __LINE__); |
| 94 |
|
| } |
| 95 |
149 |
| } |
| 96 |
|
| |
| 97 |
|
| static void |
| 98 |
72 |
| gl_set (gzip_libdeflate_t * gl, SV * key_sv, SV * value_sv) |
| 99 |
|
| { |
| 100 |
|
| const char * key; |
| 101 |
|
| STRLEN keyl; |
| 102 |
|
| const char * value; |
| 103 |
|
| STRLEN valuel; |
| 104 |
|
| |
| 105 |
72 |
| gl_check (gl); |
| 106 |
72 |
| key = SvPV (key_sv, keyl); |
| 107 |
72 |
▶ | MSG ("Handling key %s", key); |
Branch: 1 branch
| Branch | True | False | Total |
| MSG ("Handling key %s", key); |
0 |
72 |
72 |
|
| 108 |
72 |
▶ | if (strcmp (key, "type") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp (key, "type") == 0) { |
36 |
36 |
72 |
|
| 109 |
|
| int i; |
| 110 |
36 |
▶ | if (SvIOK (value_sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvIOK (value_sv)) { |
0 |
36 |
36 |
|
| 111 |
0 |
| gl_set_type (gl, SvIV (value_sv)); |
| 112 |
0 |
| return; |
| 113 |
|
| } |
| 114 |
36 |
| value = SvPV (value_sv, valuel); |
| 115 |
72 |
▶ | for (i = 0; i < 3; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 3; i++) { |
72 |
0 |
72 |
|
| 116 |
|
| struct type2name * b; |
| 117 |
72 |
| b = & gl_type_name[i]; |
| 118 |
72 |
▶ | if (valuel == b->namelen && strcmp (value, b->name) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (valuel == b->namelen && strcmp (value, b->name) == 0) { |
48 |
24 |
72 |
| if (valuel == b->namelen && strcmp (value, b->name) == 0) { |
36 |
12 |
48 |
|
| 119 |
36 |
| gl_set_type (gl, b->value); |
| 120 |
36 |
| return; |
| 121 |
|
| } |
| 122 |
|
| } |
| 123 |
0 |
| warn ("Failed to handle 'type' argument - use name or integer"); |
| 124 |
0 |
| return; |
| 125 |
|
| } |
| 126 |
36 |
▶ | if (strcmp (key, "level") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp (key, "level") == 0) { |
36 |
0 |
36 |
|
| 127 |
36 |
▶ | if (SvIOK (value_sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvIOK (value_sv)) { |
36 |
0 |
36 |
|
| 128 |
36 |
| gl_set_level (gl, SvIV (value_sv)); |
| 129 |
36 |
| return; |
| 130 |
|
| } |
| 131 |
0 |
| warn ("Failed to handle 'level' argument - require integer"); |
| 132 |
0 |
| return; |
| 133 |
|
| } |
| 134 |
0 |
▶ | if (strcmp (key, "verbose") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp (key, "verbose") == 0) { |
0 |
0 |
0 |
|
| 135 |
0 |
| gl->verbose = !! SvTRUE (value_sv); |
| 136 |
0 |
| return; |
| 137 |
|
| } |
| 138 |
0 |
| warn ("Failed to handle '%s' argument", key); |
| 139 |
0 |
| return; |
| 140 |
|
| } |
| 141 |
|
| |
| 142 |
|
| static void |
| 143 |
38 |
| gl_init (gzip_libdeflate_t * gl) |
| 144 |
|
| { |
| 145 |
38 |
| gl->t = libdeflate_gzip; |
| 146 |
38 |
| gl->level = 6; |
| 147 |
38 |
| gl->init_ok = 1; |
| 148 |
38 |
| } |
| 149 |
|
| |
| 150 |
|
| static SV * |
| 151 |
77 |
| set_up_out (SV * out, size_t r) |
| 152 |
|
| { |
| 153 |
77 |
▶ | if (r == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == 0) { |
0 |
77 |
77 |
|
| 154 |
0 |
| warn ("compression failed, not enough room"); |
| 155 |
0 |
| return &PL_sv_undef; |
| 156 |
|
| } |
| 157 |
77 |
| SvPOK_on (out); |
| 158 |
77 |
| SvCUR_set(out, (STRLEN) r); |
| 159 |
77 |
| return out; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| static SV * |
| 163 |
37 |
| gzip_libdeflate_compress (gzip_libdeflate_t * gl, SV * in_sv) |
| 164 |
|
| { |
| 165 |
|
| const char * in; |
| 166 |
|
| STRLEN in_len; |
| 167 |
|
| size_t out_nbytes; |
| 168 |
|
| size_t r; |
| 169 |
|
| SV * out; |
| 170 |
|
| char * out_p; |
| 171 |
|
| |
| 172 |
37 |
| gl_check (gl); |
| 173 |
37 |
▶ | if (! gl->c) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! gl->c) { |
37 |
0 |
37 |
|
| 174 |
37 |
| gl->c = libdeflate_alloc_compressor (gl->level); |
| 175 |
37 |
▶ | if (! gl->c) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! gl->c) { |
0 |
37 |
37 |
|
| 176 |
0 |
| warn ("Could not allocate a compressor"); |
| 177 |
0 |
| return &PL_sv_undef; |
| 178 |
|
| } |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
37 |
| in = SvPV (in_sv, in_len); |
| 182 |
37 |
▶ | MSG ("Input buffer of length %d\n", in_len); |
Branch: 1 branch
| Branch | True | False | Total |
| MSG ("Input buffer of length %d\n", in_len); |
0 |
37 |
37 |
|
| 183 |
37 |
| switch (gl->t) { |
| 184 |
12 |
| case libdeflate_deflate: |
| 185 |
12 |
| out_nbytes = libdeflate_deflate_compress_bound (gl->c, in_len); |
| 186 |
12 |
| break; |
| 187 |
13 |
| case libdeflate_gzip: |
| 188 |
13 |
| out_nbytes = libdeflate_gzip_compress_bound (gl->c, in_len); |
| 189 |
13 |
| break; |
| 190 |
12 |
| case libdeflate_zlib: |
| 191 |
12 |
| out_nbytes = libdeflate_zlib_compress_bound (gl->c, in_len); |
| 192 |
12 |
| break; |
| 193 |
0 |
| case libdeflate_none: |
| 194 |
|
| default: |
| 195 |
0 |
| warn ("Type of compression is not specified"); |
| 196 |
0 |
| return &PL_sv_undef; |
| 197 |
|
| } |
| 198 |
37 |
| out = newSV (out_nbytes); |
| 199 |
37 |
| out_p = SvPVX (out); |
| 200 |
37 |
▶ | MSG ("Output buffer of length %d\n", out_nbytes); |
Branch: 1 branch
| Branch | True | False | Total |
| MSG ("Output buffer of length %d\n", out_nbytes); |
0 |
37 |
37 |
|
| 201 |
37 |
| switch (gl->t) { |
| 202 |
12 |
| case libdeflate_deflate: |
| 203 |
12 |
| r = libdeflate_deflate_compress (gl->c, in, (size_t) in_len, |
| 204 |
|
| out_p, out_nbytes); |
| 205 |
12 |
| break; |
| 206 |
13 |
| case libdeflate_gzip: |
| 207 |
13 |
▶ | MSG ("Compressing with gzip %p", gl->c); |
Branch: 1 branch
| Branch | True | False | Total |
| MSG ("Compressing with gzip %p", gl->c); |
0 |
13 |
13 |
|
| 208 |
13 |
| r = libdeflate_gzip_compress (gl->c, in, (size_t) in_len, |
| 209 |
|
| out_p, out_nbytes); |
| 210 |
13 |
| break; |
| 211 |
12 |
| case libdeflate_zlib: |
| 212 |
12 |
| r = libdeflate_zlib_compress (gl->c, in, (size_t) in_len, |
| 213 |
|
| out_p, out_nbytes); |
| 214 |
12 |
| break; |
| 215 |
0 |
| case libdeflate_none: |
| 216 |
|
| default: |
| 217 |
0 |
| warn ("Type of compression is not specified"); |
| 218 |
0 |
| return &PL_sv_undef; |
| 219 |
|
| } |
| 220 |
37 |
▶ | MSG ("Finished compression, final length %d", r); |
Branch: 1 branch
| Branch | True | False | Total |
| MSG ("Finished compression, final length %d", r); |
0 |
37 |
37 |
|
| 221 |
37 |
| return set_up_out (out, r); |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
|
| /* https://github.com/ebiggers/libdeflate/blob/master/programs/gzip.c#L177 */ |
| 225 |
|
| |
| 226 |
|
| static u32 |
| 227 |
4 |
| load_u32_gzip(const u8 *p) |
| 228 |
|
| { |
| 229 |
|
| return |
| 230 |
4 |
| ((u32)p[0] << 0) | |
| 231 |
4 |
| ((u32)p[1] << 8) | |
| 232 |
8 |
| ((u32)p[2] << 16) | |
| 233 |
4 |
| ((u32)p[3] << 24); |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
|
| static SV * |
| 237 |
40 |
| gzip_libdeflate_decompress (gzip_libdeflate_t * gl, SV * in_sv, size_t size) |
| 238 |
|
| { |
| 239 |
|
| const char * in; |
| 240 |
|
| STRLEN in_len; |
| 241 |
|
| size_t r; |
| 242 |
|
| SV * out; |
| 243 |
|
| char * out_p; |
| 244 |
|
| |
| 245 |
40 |
| gl_check (gl); |
| 246 |
40 |
▶ | if (! gl->d) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! gl->d) { |
38 |
2 |
40 |
|
| 247 |
38 |
| gl->d = libdeflate_alloc_decompressor (); |
| 248 |
38 |
▶ | if (! gl->d) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! gl->d) { |
0 |
38 |
38 |
|
| 249 |
0 |
| warn ("Could not allocate a decompressor"); |
| 250 |
0 |
| return &PL_sv_undef; |
| 251 |
|
| } |
| 252 |
|
| } |
| 253 |
|
| |
| 254 |
40 |
| in = SvPV (in_sv, in_len); |
| 255 |
|
| |
| 256 |
40 |
| switch (gl->t) { |
| 257 |
24 |
| case libdeflate_deflate: |
| 258 |
|
| case libdeflate_zlib: |
| 259 |
24 |
▶ | if (size == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size == 0) { |
0 |
24 |
24 |
|
| 260 |
0 |
| warn ("A non-zero size is required to decompress deflate/zlib inputs"); |
| 261 |
0 |
| return &PL_sv_undef; |
| 262 |
|
| } |
| 263 |
24 |
| r = size; |
| 264 |
24 |
| break; |
| 265 |
16 |
| case libdeflate_gzip: |
| 266 |
16 |
▶ | if (size == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size == 0) { |
4 |
12 |
16 |
|
| 267 |
4 |
| r = load_u32_gzip((u8*)(&in[in_len - 4])); |
| 268 |
|
| } |
| 269 |
|
| else { |
| 270 |
12 |
| r = size; |
| 271 |
|
| } |
| 272 |
16 |
| break; |
| 273 |
0 |
| case libdeflate_none: |
| 274 |
|
| default: |
| 275 |
0 |
| warn ("Type of compression is not specified"); |
| 276 |
0 |
| return &PL_sv_undef; |
| 277 |
|
| } |
| 278 |
40 |
▶ | if (r == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == 0) { |
0 |
40 |
40 |
|
| 279 |
0 |
| r = 1; |
| 280 |
|
| } |
| 281 |
40 |
| out = newSV (r); |
| 282 |
40 |
| out_p = SvPVX (out); |
| 283 |
|
| do { |
| 284 |
|
| size_t n; |
| 285 |
|
| size_t o; |
| 286 |
|
| enum libdeflate_result result; |
| 287 |
|
| #define ARGXYZ gl->d, in, in_len, out_p, r, & n, & o |
| 288 |
40 |
| switch (gl->t) { |
| 289 |
12 |
| case libdeflate_deflate: |
| 290 |
12 |
| result = libdeflate_deflate_decompress_ex (ARGXYZ); |
| 291 |
12 |
| break; |
| 292 |
16 |
| case libdeflate_gzip: |
| 293 |
16 |
| result = libdeflate_gzip_decompress_ex (ARGXYZ); |
| 294 |
16 |
| break; |
| 295 |
12 |
| case libdeflate_zlib: |
| 296 |
12 |
| result = libdeflate_zlib_decompress_ex (ARGXYZ); |
| 297 |
12 |
| break; |
| 298 |
0 |
| default: |
| 299 |
0 |
| warn ("Type of compression is not specified"); |
| 300 |
0 |
| return &PL_sv_undef; |
| 301 |
|
| } |
| 302 |
40 |
▶ | if (result != LIBDEFLATE_SUCCESS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (result != LIBDEFLATE_SUCCESS) { |
0 |
40 |
40 |
|
| 303 |
0 |
| warn ("Decompress failed with error %d", result); |
| 304 |
0 |
| return &PL_sv_undef; |
| 305 |
|
| } |
| 306 |
|
| #undef ARGXYZ |
| 307 |
|
| } |
| 308 |
|
| while (0); |
| 309 |
40 |
| return set_up_out (out, r); |
| 310 |
|
| } |
| 311 |
|
| |
| 312 |
|
| static void |
| 313 |
38 |
| gzip_libdeflate_free (gzip_libdeflate_t * gl) |
| 314 |
|
| { |
| 315 |
38 |
▶ | MSG ("Freeing"); |
Branch: 1 branch
| Branch | True | False | Total |
| MSG ("Freeing"); |
0 |
38 |
38 |
|
| 316 |
38 |
▶ | if (gl->c) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gl->c) { |
37 |
1 |
38 |
|
| 317 |
37 |
| libdeflate_free_compressor (gl->c); |
| 318 |
37 |
| gl->c = 0; |
| 319 |
|
| } |
| 320 |
38 |
▶ | if (gl->d) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gl->d) { |
38 |
0 |
38 |
|
| 321 |
38 |
| libdeflate_free_decompressor (gl->d); |
| 322 |
38 |
| gl->d = 0; |
| 323 |
|
| } |
| 324 |
38 |
| Safefree (gl); |
| 325 |
38 |
| } |
| 326 |
|
| |
| 327 |
|
| #define GLSET \ |
| 328 |
|
| if (items > 1) { \ |
| 329 |
|
| if ((items - 1) % 2 != 0) { \ |
| 330 |
|
| warn ("odd number of arguments ignored"); \ |
| 331 |
|
| } \ |
| 332 |
|
| else { \ |
| 333 |
|
| int i; \ |
| 334 |
|
| for (i = 1; i < items; i += 2) { \ |
| 335 |
|
| gl_set (gl, ST(i), ST(i+1)); \ |
| 336 |
|
| } \ |
| 337 |
|
| } \ |
| 338 |
|
| } |