-
Notifications
You must be signed in to change notification settings - Fork 8k
Expand file tree
/
Copy pathbz2_filter.c
More file actions
495 lines (412 loc) · 14.1 KB
/
bz2_filter.c
File metadata and controls
495 lines (412 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Sara Golemon (pollita@php.net) |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#include "php_bz2.h"
/* {{{ data structure */
enum strm_status {
PHP_BZ2_UNINITIALIZED,
PHP_BZ2_RUNNING,
PHP_BZ2_FINISHED
};
typedef struct _php_bz2_filter_data {
bz_stream strm;
char *inbuf;
char *outbuf;
size_t inbuf_len;
size_t outbuf_len;
enum strm_status status; /* Decompress option */
unsigned int small_footprint : 1; /* Decompress option */
unsigned int expect_concatenated : 1; /* Decompress option */
unsigned int is_flushed : 1; /* only for compression */
int persistent;
/* Configuration for reset - immutable */
int blockSize100k; /* compress only */
int workFactor; /* compress only */
} php_bz2_filter_data;
/* }}} */
/* {{{ Memory management wrappers */
static void *php_bz2_alloc(void *opaque, int items, int size)
{
return safe_pemalloc(items, size, 0, ((php_bz2_filter_data*)opaque)->persistent);
}
static void php_bz2_free(void *opaque, void *address)
{
pefree(address, ((php_bz2_filter_data*)opaque)->persistent);
}
/* }}} */
/* {{{ bzip2.decompress filter implementation */
static php_stream_filter_status_t php_bz2_decompress_filter(
php_stream *stream,
php_stream_filter *thisfilter,
php_stream_bucket_brigade *buckets_in,
php_stream_bucket_brigade *buckets_out,
size_t *bytes_consumed,
int flags
)
{
php_bz2_filter_data *data;
php_stream_bucket *bucket;
size_t consumed = 0;
int status;
php_stream_filter_status_t exit_status = PSFS_FEED_ME;
bz_stream *streamp;
if (!Z_PTR(thisfilter->abstract)) {
/* Should never happen */
return PSFS_ERR_FATAL;
}
data = (php_bz2_filter_data *)Z_PTR(thisfilter->abstract);
streamp = &(data->strm);
while (buckets_in->head) {
size_t bin = 0, desired;
bucket = php_stream_bucket_make_writeable(buckets_in->head);
while (bin < bucket->buflen) {
if (data->status == PHP_BZ2_UNINITIALIZED) {
status = BZ2_bzDecompressInit(streamp, 0, data->small_footprint);
if (BZ_OK != status) {
php_stream_bucket_delref(bucket);
return PSFS_ERR_FATAL;
}
data->status = PHP_BZ2_RUNNING;
}
if (data->status != PHP_BZ2_RUNNING) {
consumed += bucket->buflen;
break;
}
desired = bucket->buflen - bin;
if (desired > data->inbuf_len) {
desired = data->inbuf_len;
}
memcpy(data->strm.next_in, bucket->buf + bin, desired);
data->strm.avail_in = desired;
status = BZ2_bzDecompress(&(data->strm));
if (status == BZ_STREAM_END) {
BZ2_bzDecompressEnd(&(data->strm));
if (data->expect_concatenated) {
data->status = PHP_BZ2_UNINITIALIZED;
} else {
data->status = PHP_BZ2_FINISHED;
}
} else if (status != BZ_OK) {
/* Something bad happened */
php_error_docref(NULL, E_NOTICE, "bzip2 decompression failed");
php_stream_bucket_delref(bucket);
return PSFS_ERR_FATAL;
}
desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
data->strm.next_in = data->inbuf;
data->strm.avail_in = 0;
consumed += desired;
bin += desired;
if (data->strm.avail_out < data->outbuf_len) {
php_stream_bucket *out_bucket;
size_t bucketlen = data->outbuf_len - data->strm.avail_out;
out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0);
php_stream_bucket_append(buckets_out, out_bucket);
data->strm.avail_out = data->outbuf_len;
data->strm.next_out = data->outbuf;
exit_status = PSFS_PASS_ON;
} else if (status == BZ_STREAM_END && data->strm.avail_out >= data->outbuf_len) {
/* no more data to decompress, and nothing was spat out */
php_stream_bucket_delref(bucket);
return PSFS_PASS_ON;
}
}
php_stream_bucket_delref(bucket);
}
if ((data->status == PHP_BZ2_RUNNING) && (flags & PSFS_FLAG_FLUSH_CLOSE)) {
/* Spit it out! */
status = BZ_OK;
while (status == BZ_OK) {
status = BZ2_bzDecompress(&(data->strm));
if (data->strm.avail_out < data->outbuf_len) {
size_t bucketlen = data->outbuf_len - data->strm.avail_out;
bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0);
php_stream_bucket_append(buckets_out, bucket);
data->strm.avail_out = data->outbuf_len;
data->strm.next_out = data->outbuf;
exit_status = PSFS_PASS_ON;
} else if (status == BZ_OK) {
break;
}
}
}
if (bytes_consumed) {
*bytes_consumed = consumed;
}
return exit_status;
}
static zend_result php_bz2_decompress_seek(
php_stream *stream,
php_stream_filter *thisfilter,
zend_off_t offset,
int whence
)
{
if (!Z_PTR(thisfilter->abstract)) {
return FAILURE;
}
php_bz2_filter_data *data = Z_PTR(thisfilter->abstract);
/* End current decompression if running */
if (data->status == PHP_BZ2_RUNNING) {
BZ2_bzDecompressEnd(&(data->strm));
}
/* Reset stream state */
data->strm.next_in = data->inbuf;
data->strm.avail_in = 0;
data->strm.next_out = data->outbuf;
data->strm.avail_out = data->outbuf_len;
data->status = PHP_BZ2_UNINITIALIZED;
/* Note: We don't reinitialize here - it will be done on first use in the filter function */
return SUCCESS;
}
static void php_bz2_decompress_dtor(php_stream_filter *thisfilter)
{
if (thisfilter && Z_PTR(thisfilter->abstract)) {
php_bz2_filter_data *data = Z_PTR(thisfilter->abstract);
if (data->status == PHP_BZ2_RUNNING) {
BZ2_bzDecompressEnd(&(data->strm));
}
pefree(data->inbuf, data->persistent);
pefree(data->outbuf, data->persistent);
pefree(data, data->persistent);
}
}
static const php_stream_filter_ops php_bz2_decompress_ops = {
php_bz2_decompress_filter,
php_bz2_decompress_seek,
php_bz2_decompress_dtor,
"bzip2.decompress"
};
/* }}} */
/* {{{ bzip2.compress filter implementation */
static php_stream_filter_status_t php_bz2_compress_filter(
php_stream *stream,
php_stream_filter *thisfilter,
php_stream_bucket_brigade *buckets_in,
php_stream_bucket_brigade *buckets_out,
size_t *bytes_consumed,
int flags
)
{
php_bz2_filter_data *data;
php_stream_bucket *bucket;
size_t consumed = 0;
int status;
php_stream_filter_status_t exit_status = PSFS_FEED_ME;
if (!Z_PTR(thisfilter->abstract)) {
/* Should never happen */
return PSFS_ERR_FATAL;
}
data = (php_bz2_filter_data *)Z_PTR(thisfilter->abstract);
while (buckets_in->head) {
size_t bin = 0, desired;
bucket = php_stream_bucket_make_writeable(buckets_in->head);
while (bin < bucket->buflen) {
int flush_mode;
desired = bucket->buflen - bin;
if (desired > data->inbuf_len) {
desired = data->inbuf_len;
}
memcpy(data->strm.next_in, bucket->buf + bin, desired);
data->strm.avail_in = desired;
flush_mode = flags & PSFS_FLAG_FLUSH_CLOSE ? BZ_FINISH : (flags & PSFS_FLAG_FLUSH_INC ? BZ_FLUSH : BZ_RUN);
data->is_flushed = flush_mode != BZ_RUN;
status = BZ2_bzCompress(&(data->strm), flush_mode);
if (status != BZ_RUN_OK && status != BZ_FLUSH_OK && status != BZ_FINISH_OK) {
/* Something bad happened */
php_stream_bucket_delref(bucket);
return PSFS_ERR_FATAL;
}
desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
data->strm.next_in = data->inbuf;
data->strm.avail_in = 0;
consumed += desired;
bin += desired;
if (data->strm.avail_out < data->outbuf_len) {
php_stream_bucket *out_bucket;
size_t bucketlen = data->outbuf_len - data->strm.avail_out;
out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0);
php_stream_bucket_append(buckets_out, out_bucket);
data->strm.avail_out = data->outbuf_len;
data->strm.next_out = data->outbuf;
exit_status = PSFS_PASS_ON;
}
}
php_stream_bucket_delref(bucket);
}
if (flags & PSFS_FLAG_FLUSH_CLOSE || ((flags & PSFS_FLAG_FLUSH_INC) && !data->is_flushed)) {
/* Spit it out! */
do {
status = BZ2_bzCompress(&(data->strm), (flags & PSFS_FLAG_FLUSH_CLOSE ? BZ_FINISH : BZ_FLUSH));
data->is_flushed = 1;
if (data->strm.avail_out < data->outbuf_len) {
size_t bucketlen = data->outbuf_len - data->strm.avail_out;
bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0);
php_stream_bucket_append(buckets_out, bucket);
data->strm.avail_out = data->outbuf_len;
data->strm.next_out = data->outbuf;
exit_status = PSFS_PASS_ON;
}
} while (status == (flags & PSFS_FLAG_FLUSH_CLOSE ? BZ_FINISH_OK : BZ_FLUSH_OK));
}
if (bytes_consumed) {
*bytes_consumed = consumed;
}
return exit_status;
}
static zend_result php_bz2_compress_seek(
php_stream *stream,
php_stream_filter *thisfilter,
zend_off_t offset,
int whence
)
{
int status;
if (!Z_PTR(thisfilter->abstract)) {
return FAILURE;
}
php_bz2_filter_data *data = Z_PTR(thisfilter->abstract);
/* End current compression */
BZ2_bzCompressEnd(&(data->strm));
/* Reset stream state */
data->strm.next_in = data->inbuf;
data->strm.avail_in = 0;
data->strm.next_out = data->outbuf;
data->strm.avail_out = data->outbuf_len;
data->is_flushed = 1;
/* Reinitialize compression with saved configuration */
status = BZ2_bzCompressInit(&(data->strm), data->blockSize100k, 0, data->workFactor);
if (status != BZ_OK) {
php_error_docref(NULL, E_WARNING, "bzip2.compress: failed to reset compression state");
return FAILURE;
}
return SUCCESS;
}
static void php_bz2_compress_dtor(php_stream_filter *thisfilter)
{
if (Z_PTR(thisfilter->abstract)) {
php_bz2_filter_data *data = Z_PTR(thisfilter->abstract);
BZ2_bzCompressEnd(&(data->strm));
pefree(data->inbuf, data->persistent);
pefree(data->outbuf, data->persistent);
pefree(data, data->persistent);
}
}
static const php_stream_filter_ops php_bz2_compress_ops = {
php_bz2_compress_filter,
php_bz2_compress_seek,
php_bz2_compress_dtor,
"bzip2.compress"
};
/* }}} */
/* {{{ bzip2.* common factory */
static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *filterparams, bool persistent)
{
const php_stream_filter_ops *fops = NULL;
php_bz2_filter_data *data;
int status = BZ_OK;
/* Create this filter */
data = pecalloc(1, sizeof(php_bz2_filter_data), persistent);
/* Circular reference */
data->strm.opaque = (void *) data;
data->strm.bzalloc = php_bz2_alloc;
data->strm.bzfree = php_bz2_free;
data->persistent = persistent;
data->strm.avail_out = data->outbuf_len = data->inbuf_len = 2048;
data->strm.next_in = data->inbuf = (char *) pemalloc(data->inbuf_len, persistent);
data->strm.avail_in = 0;
data->strm.next_out = data->outbuf = (char *) pemalloc(data->outbuf_len, persistent);
if (strcasecmp(filtername, "bzip2.decompress") == 0) {
data->small_footprint = 0;
data->expect_concatenated = 0;
if (filterparams) {
zval *tmpzval = NULL;
if (Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) {
HashTable *ht = HASH_OF(filterparams);
if ((tmpzval = zend_hash_str_find_ind(ht, "concatenated", sizeof("concatenated")-1))) {
data->expect_concatenated = zend_is_true(tmpzval);
tmpzval = NULL;
}
tmpzval = zend_hash_str_find_ind(ht, "small", sizeof("small")-1);
} else {
tmpzval = filterparams;
}
if (tmpzval) {
data->small_footprint = zend_is_true(tmpzval);
}
}
data->status = PHP_BZ2_UNINITIALIZED;
fops = &php_bz2_decompress_ops;
} else if (strcasecmp(filtername, "bzip2.compress") == 0) {
int blockSize100k = PHP_BZ2_FILTER_DEFAULT_BLOCKSIZE;
int workFactor = PHP_BZ2_FILTER_DEFAULT_WORKFACTOR;
if (filterparams) {
zval *tmpzval;
if (Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) {
HashTable *ht = HASH_OF(filterparams);
if ((tmpzval = zend_hash_str_find_ind(ht, "blocks", sizeof("blocks")-1))) {
/* How much memory to allocate (1 - 9) x 100kb */
zend_long blocks = zval_get_long(tmpzval);
if (blocks < 1 || blocks > 9) {
php_error_docref(NULL, E_WARNING, "Invalid parameter given for number of blocks to allocate (" ZEND_LONG_FMT ")", blocks);
pefree(data->strm.next_in, persistent);
pefree(data->strm.next_out, persistent);
pefree(data, persistent);
return NULL;
} else {
blockSize100k = (int) blocks;
}
}
if ((tmpzval = zend_hash_str_find_ind(ht, "work", sizeof("work")-1))) {
/* Work Factor (0 - 250) */
zend_long work = zval_get_long(tmpzval);
if (work < 0 || work > 250) {
php_error_docref(NULL, E_WARNING, "Invalid parameter given for work factor (" ZEND_LONG_FMT ")", work);
pefree(data->strm.next_in, persistent);
pefree(data->strm.next_out, persistent);
pefree(data, persistent);
return NULL;
} else {
workFactor = (int) work;
}
}
}
}
/* Save configuration for reset */
data->blockSize100k = blockSize100k;
data->workFactor = workFactor;
status = BZ2_bzCompressInit(&(data->strm), blockSize100k, 0, workFactor);
data->is_flushed = 1;
fops = &php_bz2_compress_ops;
} else {
status = BZ_DATA_ERROR;
}
if (status != BZ_OK) {
/* Unspecified (probably strm) error, let stream-filter error do its own whining */
pefree(data->strm.next_in, persistent);
pefree(data->strm.next_out, persistent);
pefree(data, persistent);
return NULL;
}
return php_stream_filter_alloc(fops, data, persistent, PSFS_SEEKABLE_START);
}
const php_stream_filter_factory php_bz2_filter_factory = {
php_bz2_filter_create
};
/* }}} */