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
|
/*
+----------------------------------------------------------------------+
| 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) |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_zlib.h"
/* {{{ data structure */
/* Passed as opaque in malloc callbacks */
typedef struct _php_zlib_filter_data {
z_stream strm;
unsigned char *inbuf;
size_t inbuf_len;
unsigned char *outbuf;
size_t outbuf_len;
int persistent;
bool finished; /* for zlib.deflate: signals that no flush is pending */
} php_zlib_filter_data;
/* }}} */
/* {{{ Memory management wrappers */
static voidpf php_zlib_alloc(voidpf opaque, uInt items, uInt size)
{
return (voidpf)safe_pemalloc(items, size, 0, ((php_zlib_filter_data*)opaque)->persistent);
}
static void php_zlib_free(voidpf opaque, voidpf address)
{
pefree((void*)address, ((php_zlib_filter_data*)opaque)->persistent);
}
/* }}} */
/* {{{ zlib.inflate filter implementation */
static php_stream_filter_status_t php_zlib_inflate_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_zlib_filter_data *data;
php_stream_bucket *bucket;
size_t consumed = 0;
int status;
php_stream_filter_status_t exit_status = PSFS_FEED_ME;
if (!thisfilter || !Z_PTR(thisfilter->abstract)) {
/* Should never happen */
return PSFS_ERR_FATAL;
}
data = (php_zlib_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 < (unsigned int) bucket->buflen && !data->finished) {
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 = inflate(&(data->strm), flags & PSFS_FLAG_FLUSH_CLOSE ? Z_FINISH : Z_SYNC_FLUSH);
if (status == Z_STREAM_END) {
inflateEnd(&(data->strm));
data->finished = '\1';
exit_status = PSFS_PASS_ON;
} else if (status != Z_OK && status != Z_BUF_ERROR) {
/* Something bad happened */
php_error_docref(NULL, E_NOTICE, "zlib: %s", zError(status));
php_stream_bucket_delref(bucket);
/* reset these because despite the error the filter may be used again */
data->strm.next_in = data->inbuf;
data->strm.avail_in = 0;
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;
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((char *) 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;
}
}
consumed += bucket->buflen;
php_stream_bucket_delref(bucket);
}
if (!data->finished && flags & PSFS_FLAG_FLUSH_CLOSE) {
/* Spit it out! */
status = Z_OK;
while (status == Z_OK) {
status = inflate(&(data->strm), Z_FINISH);
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((char *) 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;
}
}
}
if (bytes_consumed) {
*bytes_consumed = consumed;
}
return exit_status;
}
static void php_zlib_inflate_dtor(php_stream_filter *thisfilter)
{
if (thisfilter && Z_PTR(thisfilter->abstract)) {
php_zlib_filter_data *data = Z_PTR(thisfilter->abstract);
if (!data->finished) {
inflateEnd(&(data->strm));
}
pefree(data->inbuf, data->persistent);
pefree(data->outbuf, data->persistent);
pefree(data, data->persistent);
}
}
static const php_stream_filter_ops php_zlib_inflate_ops = {
php_zlib_inflate_filter,
php_zlib_inflate_dtor,
"zlib.inflate"
};
/* }}} */
/* {{{ zlib.deflate filter implementation */
static php_stream_filter_status_t php_zlib_deflate_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_zlib_filter_data *data;
php_stream_bucket *bucket;
size_t consumed = 0;
int status;
php_stream_filter_status_t exit_status = PSFS_FEED_ME;
if (!thisfilter || !Z_PTR(thisfilter->abstract)) {
/* Should never happen */
return PSFS_ERR_FATAL;
}
data = (php_zlib_filter_data *)(Z_PTR(thisfilter->abstract));
while (buckets_in->head) {
size_t bin = 0, desired;
bucket = buckets_in->head;
bucket = php_stream_bucket_make_writeable(bucket);
while (bin < (unsigned int) 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 ? Z_FULL_FLUSH : (flags & PSFS_FLAG_FLUSH_INC ? Z_SYNC_FLUSH : Z_NO_FLUSH);
data->finished = flush_mode != Z_NO_FLUSH;
status = deflate(&(data->strm), flush_mode);
if (status != Z_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;
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((char *) 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;
}
}
consumed += bucket->buflen;
php_stream_bucket_delref(bucket);
}
if (flags & PSFS_FLAG_FLUSH_CLOSE || ((flags & PSFS_FLAG_FLUSH_INC) && !data->finished)) {
/* Spit it out! */
status = Z_OK;
while (status == Z_OK) {
status = deflate(&(data->strm), (flags & PSFS_FLAG_FLUSH_CLOSE ? Z_FINISH : Z_SYNC_FLUSH));
data->finished = 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((char *) 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;
}
}
}
if (bytes_consumed) {
*bytes_consumed = consumed;
}
return exit_status;
}
static void php_zlib_deflate_dtor(php_stream_filter *thisfilter)
{
if (thisfilter && Z_PTR(thisfilter->abstract)) {
php_zlib_filter_data *data = Z_PTR(thisfilter->abstract);
deflateEnd(&(data->strm));
pefree(data->inbuf, data->persistent);
pefree(data->outbuf, data->persistent);
pefree(data, data->persistent);
}
}
static const php_stream_filter_ops php_zlib_deflate_ops = {
php_zlib_deflate_filter,
php_zlib_deflate_dtor,
"zlib.deflate"
};
/* }}} */
/* {{{ zlib.* common factory */
static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *filterparams, uint8_t persistent)
{
const php_stream_filter_ops *fops = NULL;
php_zlib_filter_data *data;
int status;
/* Create this filter */
data = pecalloc(1, sizeof(php_zlib_filter_data), persistent);
if (!data) {
php_error_docref(NULL, E_WARNING, "Failed allocating %zd bytes", sizeof(php_zlib_filter_data));
return NULL;
}
/* Circular reference */
data->strm.opaque = (voidpf) data;
data->strm.zalloc = (alloc_func) php_zlib_alloc;
data->strm.zfree = (free_func) php_zlib_free;
data->strm.avail_out = data->outbuf_len = data->inbuf_len = 0x8000;
data->strm.next_in = data->inbuf = (Bytef *) pemalloc(data->inbuf_len, persistent);
if (!data->inbuf) {
php_error_docref(NULL, E_WARNING, "Failed allocating %zd bytes", data->inbuf_len);
pefree(data, persistent);
return NULL;
}
data->strm.avail_in = 0;
data->strm.next_out = data->outbuf = (Bytef *) pemalloc(data->outbuf_len, persistent);
if (!data->outbuf) {
php_error_docref(NULL, E_WARNING, "Failed allocating %zd bytes", data->outbuf_len);
pefree(data->inbuf, persistent);
pefree(data, persistent);
return NULL;
}
data->strm.data_type = Z_ASCII;
if (strcasecmp(filtername, "zlib.inflate") == 0) {
int windowBits = -MAX_WBITS;
if (filterparams) {
zval *tmpzval;
if ((Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) &&
(tmpzval = zend_hash_str_find(HASH_OF(filterparams), "window", sizeof("window") - 1))) {
/* log-2 base of history window (9 - 15) */
zend_long tmp = zval_get_long(tmpzval);
if (tmp < -MAX_WBITS || tmp > MAX_WBITS + 32) {
php_error_docref(NULL, E_WARNING, "Invalid parameter given for window size (" ZEND_LONG_FMT ")", tmp);
} else {
windowBits = tmp;
}
}
}
/* RFC 1951 Inflate */
data->finished = '\0';
status = inflateInit2(&(data->strm), windowBits);
fops = &php_zlib_inflate_ops;
} else if (strcasecmp(filtername, "zlib.deflate") == 0) {
/* RFC 1951 Deflate */
int level = Z_DEFAULT_COMPRESSION;
int windowBits = -MAX_WBITS;
int memLevel = MAX_MEM_LEVEL;
if (filterparams) {
zval *tmpzval;
zend_long tmp;
/* filterparams can either be a scalar value to indicate compression level (shortcut method)
Or can be a hash containing one or more of 'window', 'memory', and/or 'level' members. */
switch (Z_TYPE_P(filterparams)) {
case IS_ARRAY:
case IS_OBJECT:
if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "memory", sizeof("memory") -1))) {
/* Memory Level (1 - 9) */
tmp = zval_get_long(tmpzval);
if (tmp < 1 || tmp > MAX_MEM_LEVEL) {
php_error_docref(NULL, E_WARNING, "Invalid parameter given for memory level (" ZEND_LONG_FMT ")", tmp);
} else {
memLevel = tmp;
}
}
if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "window", sizeof("window") - 1))) {
/* log-2 base of history window (9 - 15) */
tmp = zval_get_long(tmpzval);
if (tmp < -MAX_WBITS || tmp > MAX_WBITS + 16) {
php_error_docref(NULL, E_WARNING, "Invalid parameter given for window size (" ZEND_LONG_FMT ")", tmp);
} else {
windowBits = tmp;
}
}
if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "level", sizeof("level") - 1))) {
tmp = zval_get_long(tmpzval);
/* Pseudo pass through to catch level validating code */
goto factory_setlevel;
}
break;
case IS_STRING:
case IS_DOUBLE:
case IS_LONG:
tmp = zval_get_long(filterparams);
factory_setlevel:
/* Set compression level within reason (-1 == default, 0 == none, 1-9 == least to most compression */
if (tmp < -1 || tmp > 9) {
php_error_docref(NULL, E_WARNING, "Invalid compression level specified. (" ZEND_LONG_FMT ")", tmp);
} else {
level = tmp;
}
break;
default:
php_error_docref(NULL, E_WARNING, "Invalid filter parameter, ignored");
}
}
status = deflateInit2(&(data->strm), level, Z_DEFLATED, windowBits, memLevel, 0);
data->finished = 1;
fops = &php_zlib_deflate_ops;
} else {
status = Z_DATA_ERROR;
}
if (status != Z_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);
}
const php_stream_filter_factory php_zlib_filter_factory = {
php_zlib_filter_create
};
/* }}} */
|