bz2.c 18 KB

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