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