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