xref: /PHP-7.3/ext/bz2/bz2.c (revision e20baee1)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Sterling Hughes <sterling@php.net>                           |
16   +----------------------------------------------------------------------+
17 */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_bz2.h"
25 
26 #if HAVE_BZ2
27 
28 /* PHP Includes */
29 #include "ext/standard/file.h"
30 #include "ext/standard/info.h"
31 #include "ext/standard/php_string.h"
32 #include "main/php_network.h"
33 
34 /* for fileno() */
35 #include <stdio.h>
36 
37 /* Internal error constants */
38 #define PHP_BZ_ERRNO   0
39 #define PHP_BZ_ERRSTR  1
40 #define PHP_BZ_ERRBOTH 2
41 
42 static PHP_MINIT_FUNCTION(bz2);
43 static PHP_MSHUTDOWN_FUNCTION(bz2);
44 static PHP_MINFO_FUNCTION(bz2);
45 static PHP_FUNCTION(bzopen);
46 static PHP_FUNCTION(bzread);
47 static PHP_FUNCTION(bzerrno);
48 static PHP_FUNCTION(bzerrstr);
49 static PHP_FUNCTION(bzerror);
50 static PHP_FUNCTION(bzcompress);
51 static PHP_FUNCTION(bzdecompress);
52 
53 /* {{{ arginfo */
54 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzread, 0, 0, 1)
55 	ZEND_ARG_INFO(0, bz)
56 	ZEND_ARG_INFO(0, length)
57 ZEND_END_ARG_INFO()
58 
59 ZEND_BEGIN_ARG_INFO(arginfo_bzopen, 0)
60 	ZEND_ARG_INFO(0, file)
61 	ZEND_ARG_INFO(0, mode)
62 ZEND_END_ARG_INFO()
63 
64 ZEND_BEGIN_ARG_INFO(arginfo_bzerrno, 0)
65 	ZEND_ARG_INFO(0, bz)
66 ZEND_END_ARG_INFO()
67 
68 ZEND_BEGIN_ARG_INFO(arginfo_bzerrstr, 0)
69 	ZEND_ARG_INFO(0, bz)
70 ZEND_END_ARG_INFO()
71 
72 ZEND_BEGIN_ARG_INFO(arginfo_bzerror, 0)
73 	ZEND_ARG_INFO(0, bz)
74 ZEND_END_ARG_INFO()
75 
76 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzcompress, 0, 0, 1)
77 	ZEND_ARG_INFO(0, source)
78 	ZEND_ARG_INFO(0, blocksize)
79 	ZEND_ARG_INFO(0, workfactor)
80 ZEND_END_ARG_INFO()
81 
82 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzdecompress, 0, 0, 1)
83 	ZEND_ARG_INFO(0, source)
84 	ZEND_ARG_INFO(0, small)
85 ZEND_END_ARG_INFO()
86 
87 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzwrite, 0, 0, 2)
88 	ZEND_ARG_INFO(0, fp)
89 	ZEND_ARG_INFO(0, str)
90 	ZEND_ARG_INFO(0, length)
91 ZEND_END_ARG_INFO()
92 
93 ZEND_BEGIN_ARG_INFO(arginfo_bzflush, 0)
94 	ZEND_ARG_INFO(0, fp)
95 ZEND_END_ARG_INFO()
96 /* }}} */
97 
98 static const zend_function_entry bz2_functions[] = {
99 	PHP_FE(bzopen,       arginfo_bzopen)
100 	PHP_FE(bzread,       arginfo_bzread)
101 	PHP_FALIAS(bzwrite,   fwrite,		arginfo_bzwrite)
102 	PHP_FALIAS(bzflush,   fflush,		arginfo_bzflush)
103 	PHP_FALIAS(bzclose,   fclose,		arginfo_bzflush)
104 	PHP_FE(bzerrno,      arginfo_bzerrno)
105 	PHP_FE(bzerrstr,     arginfo_bzerrstr)
106 	PHP_FE(bzerror,      arginfo_bzerror)
107 	PHP_FE(bzcompress,   arginfo_bzcompress)
108 	PHP_FE(bzdecompress, arginfo_bzdecompress)
109 	PHP_FE_END
110 };
111 
112 zend_module_entry bz2_module_entry = {
113 	STANDARD_MODULE_HEADER,
114 	"bz2",
115 	bz2_functions,
116 	PHP_MINIT(bz2),
117 	PHP_MSHUTDOWN(bz2),
118 	NULL,
119 	NULL,
120 	PHP_MINFO(bz2),
121 	PHP_BZ2_VERSION,
122 	STANDARD_MODULE_PROPERTIES
123 };
124 
125 #ifdef COMPILE_DL_BZ2
126 ZEND_GET_MODULE(bz2)
127 #endif
128 
129 struct php_bz2_stream_data_t {
130 	BZFILE *bz_file;
131 	php_stream *stream;
132 };
133 
134 /* {{{ BZip2 stream implementation */
135 
php_bz2iop_read(php_stream * stream,char * buf,size_t count)136 static size_t php_bz2iop_read(php_stream *stream, char *buf, size_t count)
137 {
138 	struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
139 	size_t ret = 0;
140 
141 	do {
142 		int just_read;
143 		size_t remain = count - ret;
144 		int to_read = (int)(remain <= INT_MAX ? remain : INT_MAX);
145 
146 		just_read = BZ2_bzread(self->bz_file, buf, to_read);
147 
148 		if (just_read < 1) {
149 			/* it is not safe to keep reading after an error, see #72613 */
150 			stream->eof = 1;
151 			if (just_read < 0) {
152 				return -1;
153 			}
154 			break;
155 		}
156 
157 		ret += just_read;
158 	} while (ret < count);
159 
160 	return ret;
161 }
162 
php_bz2iop_write(php_stream * stream,const char * buf,size_t count)163 static size_t php_bz2iop_write(php_stream *stream, const char *buf, size_t count)
164 {
165 	size_t wrote = 0;
166 	struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
167 
168 
169 	do {
170 		int just_wrote;
171 		size_t remain = count - wrote;
172 		int to_write = (int)(remain <= INT_MAX ? remain : INT_MAX);
173 
174 		just_wrote = BZ2_bzwrite(self->bz_file, (char*)buf, to_write);
175 
176 		if (just_wrote < 1) {
177 			break;
178 		}
179 
180 		wrote += just_wrote;
181 
182 	} while (wrote < count);
183 
184 	return wrote;
185 }
186 
php_bz2iop_close(php_stream * stream,int close_handle)187 static int php_bz2iop_close(php_stream *stream, int close_handle)
188 {
189 	struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
190 	int ret = EOF;
191 
192 	if (close_handle) {
193 		BZ2_bzclose(self->bz_file);
194 	}
195 
196 	if (self->stream) {
197 		php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE | (close_handle == 0 ? PHP_STREAM_FREE_PRESERVE_HANDLE : 0));
198 	}
199 
200 	efree(self);
201 
202 	return ret;
203 }
204 
php_bz2iop_flush(php_stream * stream)205 static int php_bz2iop_flush(php_stream *stream)
206 {
207 	struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
208 	return BZ2_bzflush(self->bz_file);
209 }
210 /* }}} */
211 
212 const php_stream_ops php_stream_bz2io_ops = {
213 	php_bz2iop_write, php_bz2iop_read,
214 	php_bz2iop_close, php_bz2iop_flush,
215 	"BZip2",
216 	NULL, /* seek */
217 	NULL, /* cast */
218 	NULL, /* stat */
219 	NULL  /* set_option */
220 };
221 
222 /* {{{ Bzip2 stream openers */
_php_stream_bz2open_from_BZFILE(BZFILE * bz,const char * mode,php_stream * innerstream STREAMS_DC)223 PHP_BZ2_API php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz,
224 														const char *mode, php_stream *innerstream STREAMS_DC)
225 {
226 	struct php_bz2_stream_data_t *self;
227 
228 	self = emalloc(sizeof(*self));
229 
230 	self->stream = innerstream;
231 	if (innerstream) {
232 		GC_ADDREF(innerstream->res);
233 	}
234 	self->bz_file = bz;
235 
236 	return php_stream_alloc_rel(&php_stream_bz2io_ops, self, 0, mode);
237 }
238 
_php_stream_bz2open(php_stream_wrapper * wrapper,const char * path,const char * mode,int options,zend_string ** opened_path,php_stream_context * context STREAMS_DC)239 PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
240 											const char *path,
241 											const char *mode,
242 											int options,
243 											zend_string **opened_path,
244 											php_stream_context *context STREAMS_DC)
245 {
246 	php_stream *retstream = NULL, *stream = NULL;
247 	char *path_copy = NULL;
248 	BZFILE *bz_file = NULL;
249 
250 	if (strncasecmp("compress.bzip2://", path, 17) == 0) {
251 		path += 17;
252 	}
253 	if (mode[0] == '\0' || (mode[0] != 'w' && mode[0] != 'r' && mode[1] != '\0')) {
254 		return NULL;
255 	}
256 
257 #ifdef VIRTUAL_DIR
258 	virtual_filepath_ex(path, &path_copy, NULL);
259 #else
260 	path_copy = (char *)path;
261 #endif
262 
263 	if (php_check_open_basedir(path_copy)) {
264 #ifdef VIRTUAL_DIR
265 		efree(path_copy);
266 #endif
267 		return NULL;
268 	}
269 
270 	/* try and open it directly first */
271 	bz_file = BZ2_bzopen(path_copy, mode);
272 
273 	if (opened_path && bz_file) {
274 		*opened_path = zend_string_init(path_copy, strlen(path_copy), 0);
275 	}
276 
277 #ifdef VIRTUAL_DIR
278 	efree(path_copy);
279 #endif
280 
281 	if (bz_file == NULL) {
282 		/* that didn't work, so try and get something from the network/wrapper */
283 		stream = php_stream_open_wrapper(path, mode, options | STREAM_WILL_CAST, opened_path);
284 
285 		if (stream) {
286 			php_socket_t fd;
287 			if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
288 				bz_file = BZ2_bzdopen((int)fd, mode);
289 			}
290 		}
291 
292 		/* remove the file created by php_stream_open_wrapper(), it is not needed since BZ2 functions
293 		 * failed.
294 		 */
295 		if (opened_path && !bz_file && mode[0] == 'w') {
296 			VCWD_UNLINK(ZSTR_VAL(*opened_path));
297 		}
298 	}
299 
300 	if (bz_file) {
301 		retstream = _php_stream_bz2open_from_BZFILE(bz_file, mode, stream STREAMS_REL_CC);
302 		if (retstream) {
303 			return retstream;
304 		}
305 
306 		BZ2_bzclose(bz_file);
307 	}
308 
309 	if (stream) {
310 		php_stream_close(stream);
311 	}
312 
313 	return NULL;
314 }
315 
316 /* }}} */
317 
318 static const php_stream_wrapper_ops bzip2_stream_wops = {
319 	_php_stream_bz2open,
320 	NULL, /* close */
321 	NULL, /* fstat */
322 	NULL, /* stat */
323 	NULL, /* opendir */
324 	"BZip2",
325 	NULL, /* unlink */
326 	NULL, /* rename */
327 	NULL, /* mkdir */
328 	NULL, /* rmdir */
329 	NULL
330 };
331 
332 static const php_stream_wrapper php_stream_bzip2_wrapper = {
333 	&bzip2_stream_wops,
334 	NULL,
335 	0 /* is_url */
336 };
337 
338 static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int);
339 
PHP_MINIT_FUNCTION(bz2)340 static PHP_MINIT_FUNCTION(bz2)
341 {
342 	php_register_url_stream_wrapper("compress.bzip2", &php_stream_bzip2_wrapper);
343 	php_stream_filter_register_factory("bzip2.*", &php_bz2_filter_factory);
344 	return SUCCESS;
345 }
346 
PHP_MSHUTDOWN_FUNCTION(bz2)347 static PHP_MSHUTDOWN_FUNCTION(bz2)
348 {
349 	php_unregister_url_stream_wrapper("compress.bzip2");
350 	php_stream_filter_unregister_factory("bzip2.*");
351 
352 	return SUCCESS;
353 }
354 
PHP_MINFO_FUNCTION(bz2)355 static PHP_MINFO_FUNCTION(bz2)
356 {
357 	php_info_print_table_start();
358 	php_info_print_table_row(2, "BZip2 Support", "Enabled");
359 	php_info_print_table_row(2, "Stream Wrapper support", "compress.bzip2://");
360 	php_info_print_table_row(2, "Stream Filter support", "bzip2.decompress, bzip2.compress");
361 	php_info_print_table_row(2, "BZip2 Version", (char *) BZ2_bzlibVersion());
362 	php_info_print_table_end();
363 }
364 
365 /* {{{ proto string bzread(resource bz[, int length])
366    Reads up to length bytes from a BZip2 stream, or 1024 bytes if length is not specified */
PHP_FUNCTION(bzread)367 static PHP_FUNCTION(bzread)
368 {
369 	zval *bz;
370 	zend_long len = 1024;
371 	php_stream *stream;
372 	zend_string *data;
373 
374 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &bz, &len)) {
375 		RETURN_FALSE;
376 	}
377 
378 	php_stream_from_zval(stream, bz);
379 
380 	if ((len + 1) < 1) {
381 		php_error_docref(NULL, E_WARNING, "length may not be negative");
382 		RETURN_FALSE;
383 	}
384 	data = zend_string_alloc(len, 0);
385 	ZSTR_LEN(data) = php_stream_read(stream, ZSTR_VAL(data), ZSTR_LEN(data));
386 	ZSTR_VAL(data)[ZSTR_LEN(data)] = '\0';
387 
388 	RETURN_NEW_STR(data);
389 }
390 /* }}} */
391 
392 /* {{{ proto resource bzopen(string|int file|fp, string mode)
393    Opens a new BZip2 stream */
PHP_FUNCTION(bzopen)394 static PHP_FUNCTION(bzopen)
395 {
396 	zval     *file;   /* The file to open */
397 	char     *mode;   /* The mode to open the stream with */
398 	size_t      mode_len;
399 
400 	BZFILE   *bz;     /* The compressed file stream */
401 	php_stream *stream = NULL;
402 
403 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs", &file, &mode, &mode_len) == FAILURE) {
404 		return;
405 	}
406 
407 	if (mode_len != 1 || (mode[0] != 'r' && mode[0] != 'w')) {
408 		php_error_docref(NULL, E_WARNING, "'%s' is not a valid mode for bzopen(). Only 'w' and 'r' are supported.", mode);
409 		RETURN_FALSE;
410 	}
411 
412 	/* If it's not a resource its a string containing the filename to open */
413 	if (Z_TYPE_P(file) == IS_STRING) {
414 		if (Z_STRLEN_P(file) == 0) {
415 			php_error_docref(NULL, E_WARNING, "filename cannot be empty");
416 			RETURN_FALSE;
417 		}
418 
419 		if (CHECK_ZVAL_NULL_PATH(file)) {
420 			RETURN_FALSE;
421 		}
422 
423 		stream = php_stream_bz2open(NULL, Z_STRVAL_P(file), mode, REPORT_ERRORS, NULL);
424 	} else if (Z_TYPE_P(file) == IS_RESOURCE) {
425 		/* If it is a resource, than its a stream resource */
426 		php_socket_t fd;
427 		size_t stream_mode_len;
428 
429 		php_stream_from_zval(stream, file);
430 		stream_mode_len = strlen(stream->mode);
431 
432 		if (stream_mode_len != 1 && !(stream_mode_len == 2 && memchr(stream->mode, 'b', 2))) {
433 			php_error_docref(NULL, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
434 			RETURN_FALSE;
435 		} else if (stream_mode_len == 1 && stream->mode[0] != 'r' && stream->mode[0] != 'w' && stream->mode[0] != 'a' && stream->mode[0] != 'x') {
436 			php_error_docref(NULL, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
437 			RETURN_FALSE;
438 		}
439 
440 		switch(mode[0]) {
441 			case 'r':
442 				/* only "r" and "rb" are supported */
443 				if (stream->mode[0] != mode[0] && !(stream_mode_len == 2 && stream->mode[1] != mode[0])) {
444 					php_error_docref(NULL, E_WARNING, "cannot read from a stream opened in write only mode");
445 					RETURN_FALSE;
446 				}
447 				break;
448 			case 'w':
449 				/* support only "w"(b), "a"(b), "x"(b) */
450 				if (stream->mode[0] != mode[0] && !(stream_mode_len == 2 && stream->mode[1] != mode[0])
451 					&& stream->mode[0] != 'a' && !(stream_mode_len == 2 && stream->mode[1] != 'a')
452 					&& stream->mode[0] != 'x' && !(stream_mode_len == 2 && stream->mode[1] != 'x')) {
453 					php_error_docref(NULL, E_WARNING, "cannot write to a stream opened in read only mode");
454 					RETURN_FALSE;
455 				}
456 				break;
457 			default:
458 				/* not reachable */
459 				break;
460 		}
461 
462 		if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void *) &fd, REPORT_ERRORS)) {
463 			RETURN_FALSE;
464 		}
465 
466 		bz = BZ2_bzdopen((int)fd, mode);
467 
468 		stream = php_stream_bz2open_from_BZFILE(bz, mode, stream);
469 	} else {
470 		php_error_docref(NULL, E_WARNING, "first parameter has to be string or file-resource");
471 		RETURN_FALSE;
472 	}
473 
474 	if (stream) {
475 		php_stream_to_zval(stream, return_value);
476 	} else {
477 		RETURN_FALSE;
478 	}
479 }
480 /* }}} */
481 
482 /* {{{ proto int bzerrno(resource bz)
483    Returns the error number */
PHP_FUNCTION(bzerrno)484 static PHP_FUNCTION(bzerrno)
485 {
486 	php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRNO);
487 }
488 /* }}} */
489 
490 /* {{{ proto string bzerrstr(resource bz)
491    Returns the error string */
PHP_FUNCTION(bzerrstr)492 static PHP_FUNCTION(bzerrstr)
493 {
494 	php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRSTR);
495 }
496 /* }}} */
497 
498 /* {{{ proto array bzerror(resource bz)
499    Returns the error number and error string in an associative array */
PHP_FUNCTION(bzerror)500 static PHP_FUNCTION(bzerror)
501 {
502 	php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRBOTH);
503 }
504 /* }}} */
505 
506 /* {{{ proto string bzcompress(string source [, int blocksize100k [, int workfactor]])
507    Compresses a string into BZip2 encoded data */
PHP_FUNCTION(bzcompress)508 static PHP_FUNCTION(bzcompress)
509 {
510 	char             *source;          /* Source data to compress */
511 	zend_long              zblock_size = 0; /* Optional block size to use */
512 	zend_long              zwork_factor = 0;/* Optional work factor to use */
513 	zend_string      *dest = NULL;     /* Destination to place the compressed data into */
514 	int               error,           /* Error Container */
515 					  block_size  = 4, /* Block size for compression algorithm */
516 					  work_factor = 0, /* Work factor for compression algorithm */
517 					  argc = ZEND_NUM_ARGS(); /* Argument count */
518 	size_t               source_len;      /* Length of the source data */
519 	unsigned int      dest_len;        /* Length of the destination buffer */
520 
521 	if (zend_parse_parameters(argc, "s|ll", &source, &source_len, &zblock_size, &zwork_factor) == FAILURE) {
522 		return;
523 	}
524 
525 	/* Assign them to easy to use variables, dest_len is initially the length of the data
526 	   + .01 x length of data + 600 which is the largest size the results of the compression
527 	   could possibly be, at least that's what the libbz2 docs say (thanks to jeremy@nirvani.net
528 	   for pointing this out).  */
529 	dest_len = (unsigned int) (source_len + (0.01 * source_len) + 600);
530 
531 	/* Allocate the destination buffer */
532 	dest = zend_string_alloc(dest_len, 0);
533 
534 	/* Handle the optional arguments */
535 	if (argc > 1) {
536 		block_size = zblock_size;
537 	}
538 
539 	if (argc > 2) {
540 		work_factor = zwork_factor;
541 	}
542 
543 	error = BZ2_bzBuffToBuffCompress(ZSTR_VAL(dest), &dest_len, source, source_len, block_size, 0, work_factor);
544 	if (error != BZ_OK) {
545 		zend_string_efree(dest);
546 		RETURN_LONG(error);
547 	} else {
548 		/* Copy the buffer, we have perhaps allocate a lot more than we need,
549 		   so we erealloc() the buffer to the proper size */
550 		ZSTR_LEN(dest) = dest_len;
551 		ZSTR_VAL(dest)[ZSTR_LEN(dest)] = '\0';
552 		RETURN_NEW_STR(dest);
553 	}
554 }
555 /* }}} */
556 
557 /* {{{ proto string bzdecompress(string source [, int small])
558    Decompresses BZip2 compressed data */
PHP_FUNCTION(bzdecompress)559 static PHP_FUNCTION(bzdecompress)
560 {
561 	char *source;
562 	zend_string *dest;
563 	size_t source_len;
564 	int error;
565 	zend_long small = 0;
566 #if defined(PHP_WIN32)
567 	unsigned __int64 size = 0;
568 #else
569 	unsigned long long size = 0;
570 #endif
571 	bz_stream bzs;
572 
573 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &source, &source_len, &small)) {
574 		RETURN_FALSE;
575 	}
576 
577 	bzs.bzalloc = NULL;
578 	bzs.bzfree = NULL;
579 
580 	if (BZ2_bzDecompressInit(&bzs, 0, (int)small) != BZ_OK) {
581 		RETURN_FALSE;
582 	}
583 
584 	bzs.next_in = source;
585 	bzs.avail_in = source_len;
586 
587 	/* in most cases bz2 offers at least 2:1 compression, so we use that as our base */
588 	dest = zend_string_safe_alloc(source_len, 2, 1, 0);
589 	bzs.avail_out = source_len * 2;
590 	bzs.next_out = ZSTR_VAL(dest);
591 
592 	while ((error = BZ2_bzDecompress(&bzs)) == BZ_OK && bzs.avail_in > 0) {
593 		/* compression is better then 2:1, need to allocate more memory */
594 		bzs.avail_out = source_len;
595 		size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
596 #if !ZEND_ENABLE_ZVAL_LONG64
597 		if (size > SIZE_MAX) {
598 			/* no reason to continue if we're going to drop it anyway */
599 			break;
600 		}
601 #endif
602 		dest = zend_string_safe_realloc(dest, 1, bzs.avail_out+1, (size_t) size, 0);
603 		bzs.next_out = ZSTR_VAL(dest) + size;
604 	}
605 
606 	if (error == BZ_STREAM_END || error == BZ_OK) {
607 		size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
608 #if !ZEND_ENABLE_ZVAL_LONG64
609 		if (UNEXPECTED(size > SIZE_MAX)) {
610 			php_error_docref(NULL, E_WARNING, "Decompressed size too big, max is %zd", SIZE_MAX);
611 			zend_string_efree(dest);
612 			RETVAL_LONG(BZ_MEM_ERROR);
613 		} else
614 #endif
615 		{
616 			dest = zend_string_safe_realloc(dest, 1, (size_t)size, 1, 0);
617 			ZSTR_LEN(dest) = (size_t)size;
618 			ZSTR_VAL(dest)[(size_t)size] = '\0';
619 			RETVAL_STR(dest);
620 		}
621 	} else { /* real error */
622 		zend_string_efree(dest);
623 		RETVAL_LONG(error);
624 	}
625 
626 	BZ2_bzDecompressEnd(&bzs);
627 }
628 /* }}} */
629 
630 /* {{{ php_bz2_error()
631    The central error handling interface, does the work for bzerrno, bzerrstr and bzerror */
php_bz2_error(INTERNAL_FUNCTION_PARAMETERS,int opt)632 static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int opt)
633 {
634 	zval         *bzp;     /* BZip2 Resource Pointer */
635 	php_stream   *stream;
636 	const char   *errstr;  /* Error string */
637 	int           errnum;  /* Error number */
638 	struct php_bz2_stream_data_t *self;
639 
640 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &bzp) == FAILURE) {
641 		return;
642 	}
643 
644 	php_stream_from_zval(stream, bzp);
645 
646 	if (!php_stream_is(stream, PHP_STREAM_IS_BZIP2)) {
647 		RETURN_FALSE;
648 	}
649 
650 	self = (struct php_bz2_stream_data_t *) stream->abstract;
651 
652 	/* Fetch the error information */
653 	errstr = BZ2_bzerror(self->bz_file, &errnum);
654 
655 	/* Determine what to return */
656 	switch (opt) {
657 		case PHP_BZ_ERRNO:
658 			RETURN_LONG(errnum);
659 			break;
660 		case PHP_BZ_ERRSTR:
661 			RETURN_STRING((char*)errstr);
662 			break;
663 		case PHP_BZ_ERRBOTH:
664 			array_init(return_value);
665 
666 			add_assoc_long  (return_value, "errno",  errnum);
667 			add_assoc_string(return_value, "errstr", (char*)errstr);
668 			break;
669 	}
670 }
671 /* }}} */
672 
673 #endif
674 
675 /*
676  * Local variables:
677  * tab-width: 4
678  * c-basic-offset: 4
679  * End:
680  * vim600: fdm=marker
681  * vim: noet sw=4 ts=4
682  */
683