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