zlib.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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. | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
  14. | Stefan Röhrich <sr@linux.de> |
  15. | Zeev Suraski <zeev@php.net> |
  16. | Jade Nicoletti <nicoletti@nns.ch> |
  17. | Michael Wallner <mike@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "SAPI.h"
  25. #include "php_ini.h"
  26. #include "ext/standard/info.h"
  27. #include "ext/standard/php_string.h"
  28. #include "php_zlib.h"
  29. #include "zlib_arginfo.h"
  30. /*
  31. * zlib include files can define the following preprocessor defines which rename
  32. * the corresponding PHP functions to gzopen64, gzseek64 and gztell64 and thereby
  33. * breaking some software, most notably PEAR's Archive_Tar, which halts execution
  34. * without error message on gzip compressed archives.
  35. *
  36. * This only seems to happen on 32bit systems with large file support.
  37. */
  38. #undef gzopen
  39. #undef gzseek
  40. #undef gztell
  41. ZEND_DECLARE_MODULE_GLOBALS(zlib)
  42. /* InflateContext class */
  43. zend_class_entry *inflate_context_ce;
  44. static zend_object_handlers inflate_context_object_handlers;
  45. static inline php_zlib_context *inflate_context_from_obj(zend_object *obj) {
  46. return (php_zlib_context *)((char *)(obj) - XtOffsetOf(php_zlib_context, std));
  47. }
  48. #define Z_INFLATE_CONTEXT_P(zv) inflate_context_from_obj(Z_OBJ_P(zv))
  49. static zend_object *inflate_context_create_object(zend_class_entry *class_type) {
  50. php_zlib_context *intern = zend_object_alloc(sizeof(php_zlib_context), class_type);
  51. zend_object_std_init(&intern->std, class_type);
  52. object_properties_init(&intern->std, class_type);
  53. intern->std.handlers = &inflate_context_object_handlers;
  54. return &intern->std;
  55. }
  56. static zend_function *inflate_context_get_constructor(zend_object *object) {
  57. zend_throw_error(NULL, "Cannot directly construct InflateContext, use inflate_init() instead");
  58. return NULL;
  59. }
  60. static void inflate_context_free_obj(zend_object *object)
  61. {
  62. php_zlib_context *intern = inflate_context_from_obj(object);
  63. if (intern->inflateDict) {
  64. efree(intern->inflateDict);
  65. }
  66. inflateEnd(&intern->Z);
  67. zend_object_std_dtor(&intern->std);
  68. }
  69. /* }}} */
  70. /* DeflateContext class */
  71. zend_class_entry *deflate_context_ce;
  72. static zend_object_handlers deflate_context_object_handlers;
  73. static inline php_zlib_context *deflate_context_from_obj(zend_object *obj) {
  74. return (php_zlib_context *)((char *)(obj) - XtOffsetOf(php_zlib_context, std));
  75. }
  76. #define Z_DEFLATE_CONTEXT_P(zv) deflate_context_from_obj(Z_OBJ_P(zv))
  77. static zend_object *deflate_context_create_object(zend_class_entry *class_type) {
  78. php_zlib_context *intern = zend_object_alloc(sizeof(php_zlib_context), class_type);
  79. zend_object_std_init(&intern->std, class_type);
  80. object_properties_init(&intern->std, class_type);
  81. intern->std.handlers = &deflate_context_object_handlers;
  82. return &intern->std;
  83. }
  84. static zend_function *deflate_context_get_constructor(zend_object *object) {
  85. zend_throw_error(NULL, "Cannot directly construct DeflateContext, use deflate_init() instead");
  86. return NULL;
  87. }
  88. static void deflate_context_free_obj(zend_object *object)
  89. {
  90. php_zlib_context *intern = deflate_context_from_obj(object);
  91. deflateEnd(&intern->Z);
  92. zend_object_std_dtor(&intern->std);
  93. }
  94. /* }}} */
  95. /* {{{ Memory management wrappers */
  96. static voidpf php_zlib_alloc(voidpf opaque, uInt items, uInt size)
  97. {
  98. return (voidpf)safe_emalloc(items, size, 0);
  99. }
  100. static void php_zlib_free(voidpf opaque, voidpf address)
  101. {
  102. efree((void*)address);
  103. }
  104. /* }}} */
  105. /* {{{ php_zlib_output_conflict_check() */
  106. static int php_zlib_output_conflict_check(const char *handler_name, size_t handler_name_len)
  107. {
  108. if (php_output_get_level() > 0) {
  109. if (php_output_handler_conflict(handler_name, handler_name_len, ZEND_STRL(PHP_ZLIB_OUTPUT_HANDLER_NAME))
  110. || php_output_handler_conflict(handler_name, handler_name_len, ZEND_STRL("ob_gzhandler"))
  111. || php_output_handler_conflict(handler_name, handler_name_len, ZEND_STRL("mb_output_handler"))
  112. || php_output_handler_conflict(handler_name, handler_name_len, ZEND_STRL("URL-Rewriter"))) {
  113. return FAILURE;
  114. }
  115. }
  116. return SUCCESS;
  117. }
  118. /* }}} */
  119. /* {{{ php_zlib_output_encoding() */
  120. static int php_zlib_output_encoding(void)
  121. {
  122. zval *enc;
  123. if (!ZLIBG(compression_coding)) {
  124. if ((Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY || zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER))) &&
  125. (enc = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING") - 1))) {
  126. convert_to_string(enc);
  127. if (strstr(Z_STRVAL_P(enc), "gzip")) {
  128. ZLIBG(compression_coding) = PHP_ZLIB_ENCODING_GZIP;
  129. } else if (strstr(Z_STRVAL_P(enc), "deflate")) {
  130. ZLIBG(compression_coding) = PHP_ZLIB_ENCODING_DEFLATE;
  131. }
  132. }
  133. }
  134. return ZLIBG(compression_coding);
  135. }
  136. /* }}} */
  137. /* {{{ php_zlib_output_handler_ex() */
  138. static int php_zlib_output_handler_ex(php_zlib_context *ctx, php_output_context *output_context)
  139. {
  140. int flags = Z_SYNC_FLUSH;
  141. if (output_context->op & PHP_OUTPUT_HANDLER_START) {
  142. /* start up */
  143. if (Z_OK != deflateInit2(&ctx->Z, ZLIBG(output_compression_level), Z_DEFLATED, ZLIBG(compression_coding), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) {
  144. return FAILURE;
  145. }
  146. }
  147. if (output_context->op & PHP_OUTPUT_HANDLER_CLEAN) {
  148. /* free buffers */
  149. deflateEnd(&ctx->Z);
  150. if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) {
  151. /* discard */
  152. return SUCCESS;
  153. } else {
  154. /* restart */
  155. if (Z_OK != deflateInit2(&ctx->Z, ZLIBG(output_compression_level), Z_DEFLATED, ZLIBG(compression_coding), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) {
  156. return FAILURE;
  157. }
  158. ctx->buffer.used = 0;
  159. }
  160. } else {
  161. if (output_context->in.used) {
  162. /* append input */
  163. if (ctx->buffer.free < output_context->in.used) {
  164. if (!(ctx->buffer.aptr = erealloc_recoverable(ctx->buffer.data, ctx->buffer.used + ctx->buffer.free + output_context->in.used))) {
  165. deflateEnd(&ctx->Z);
  166. return FAILURE;
  167. }
  168. ctx->buffer.data = ctx->buffer.aptr;
  169. ctx->buffer.free += output_context->in.used;
  170. }
  171. memcpy(ctx->buffer.data + ctx->buffer.used, output_context->in.data, output_context->in.used);
  172. ctx->buffer.free -= output_context->in.used;
  173. ctx->buffer.used += output_context->in.used;
  174. }
  175. output_context->out.size = PHP_ZLIB_BUFFER_SIZE_GUESS(output_context->in.used);
  176. output_context->out.data = emalloc(output_context->out.size);
  177. output_context->out.free = 1;
  178. output_context->out.used = 0;
  179. ctx->Z.avail_in = ctx->buffer.used;
  180. ctx->Z.next_in = (Bytef *) ctx->buffer.data;
  181. ctx->Z.avail_out = output_context->out.size;
  182. ctx->Z.next_out = (Bytef *) output_context->out.data;
  183. if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) {
  184. flags = Z_FINISH;
  185. } else if (output_context->op & PHP_OUTPUT_HANDLER_FLUSH) {
  186. flags = Z_FULL_FLUSH;
  187. }
  188. switch (deflate(&ctx->Z, flags)) {
  189. case Z_OK:
  190. if (flags == Z_FINISH) {
  191. deflateEnd(&ctx->Z);
  192. return FAILURE;
  193. }
  194. ZEND_FALLTHROUGH;
  195. case Z_STREAM_END:
  196. if (ctx->Z.avail_in) {
  197. memmove(ctx->buffer.data, ctx->buffer.data + ctx->buffer.used - ctx->Z.avail_in, ctx->Z.avail_in);
  198. }
  199. ctx->buffer.free += ctx->buffer.used - ctx->Z.avail_in;
  200. ctx->buffer.used = ctx->Z.avail_in;
  201. output_context->out.used = output_context->out.size - ctx->Z.avail_out;
  202. break;
  203. default:
  204. deflateEnd(&ctx->Z);
  205. return FAILURE;
  206. }
  207. if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) {
  208. deflateEnd(&ctx->Z);
  209. }
  210. }
  211. return SUCCESS;
  212. }
  213. /* }}} */
  214. /* {{{ php_zlib_output_handler() */
  215. static int php_zlib_output_handler(void **handler_context, php_output_context *output_context)
  216. {
  217. php_zlib_context *ctx = *(php_zlib_context **) handler_context;
  218. if (!php_zlib_output_encoding()) {
  219. /* "Vary: Accept-Encoding" header sent along uncompressed content breaks caching in MSIE,
  220. so let's just send it with successfully compressed content or unless the complete
  221. buffer gets discarded, see http://bugs.php.net/40325;
  222. Test as follows:
  223. +Vary: $ HTTP_ACCEPT_ENCODING=gzip ./sapi/cgi/php <<<'<?php ob_start("ob_gzhandler"); echo "foo\n";'
  224. +Vary: $ HTTP_ACCEPT_ENCODING= ./sapi/cgi/php <<<'<?php ob_start("ob_gzhandler"); echo "foo\n";'
  225. -Vary: $ HTTP_ACCEPT_ENCODING=gzip ./sapi/cgi/php <<<'<?php ob_start("ob_gzhandler"); echo "foo\n"; ob_end_clean();'
  226. -Vary: $ HTTP_ACCEPT_ENCODING= ./sapi/cgi/php <<<'<?php ob_start("ob_gzhandler"); echo "foo\n"; ob_end_clean();'
  227. */
  228. if ((output_context->op & PHP_OUTPUT_HANDLER_START)
  229. && (output_context->op != (PHP_OUTPUT_HANDLER_START|PHP_OUTPUT_HANDLER_CLEAN|PHP_OUTPUT_HANDLER_FINAL))
  230. ) {
  231. sapi_add_header_ex(ZEND_STRL("Vary: Accept-Encoding"), 1, 0);
  232. }
  233. return FAILURE;
  234. }
  235. if (SUCCESS != php_zlib_output_handler_ex(ctx, output_context)) {
  236. return FAILURE;
  237. }
  238. if (!(output_context->op & PHP_OUTPUT_HANDLER_CLEAN) || ((output_context->op & PHP_OUTPUT_HANDLER_START) && !(output_context->op & PHP_OUTPUT_HANDLER_FINAL))) {
  239. int flags;
  240. if (SUCCESS == php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS, &flags)) {
  241. /* only run this once */
  242. if (!(flags & PHP_OUTPUT_HANDLER_STARTED)) {
  243. if (SG(headers_sent) || !ZLIBG(output_compression)) {
  244. deflateEnd(&ctx->Z);
  245. return FAILURE;
  246. }
  247. switch (ZLIBG(compression_coding)) {
  248. case PHP_ZLIB_ENCODING_GZIP:
  249. sapi_add_header_ex(ZEND_STRL("Content-Encoding: gzip"), 1, 1);
  250. break;
  251. case PHP_ZLIB_ENCODING_DEFLATE:
  252. sapi_add_header_ex(ZEND_STRL("Content-Encoding: deflate"), 1, 1);
  253. break;
  254. default:
  255. deflateEnd(&ctx->Z);
  256. return FAILURE;
  257. }
  258. sapi_add_header_ex(ZEND_STRL("Vary: Accept-Encoding"), 1, 0);
  259. php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE, NULL);
  260. }
  261. }
  262. }
  263. return SUCCESS;
  264. }
  265. /* }}} */
  266. /* {{{ php_zlib_output_handler_context_init() */
  267. static php_zlib_context *php_zlib_output_handler_context_init(void)
  268. {
  269. php_zlib_context *ctx = (php_zlib_context *) ecalloc(1, sizeof(php_zlib_context));
  270. ctx->Z.zalloc = php_zlib_alloc;
  271. ctx->Z.zfree = php_zlib_free;
  272. return ctx;
  273. }
  274. /* }}} */
  275. /* {{{ php_zlib_output_handler_context_dtor() */
  276. static void php_zlib_output_handler_context_dtor(void *opaq)
  277. {
  278. php_zlib_context *ctx = (php_zlib_context *) opaq;
  279. if (ctx) {
  280. if (ctx->buffer.data) {
  281. efree(ctx->buffer.data);
  282. }
  283. efree(ctx);
  284. }
  285. }
  286. /* }}} */
  287. /* {{{ php_zlib_output_handler_init() */
  288. static php_output_handler *php_zlib_output_handler_init(const char *handler_name, size_t handler_name_len, size_t chunk_size, int flags)
  289. {
  290. php_output_handler *h = NULL;
  291. if (!ZLIBG(output_compression)) {
  292. ZLIBG(output_compression) = chunk_size ? chunk_size : PHP_OUTPUT_HANDLER_DEFAULT_SIZE;
  293. }
  294. ZLIBG(handler_registered) = 1;
  295. if ((h = php_output_handler_create_internal(handler_name, handler_name_len, php_zlib_output_handler, chunk_size, flags))) {
  296. php_output_handler_set_context(h, php_zlib_output_handler_context_init(), php_zlib_output_handler_context_dtor);
  297. }
  298. return h;
  299. }
  300. /* }}} */
  301. /* {{{ php_zlib_output_compression_start() */
  302. static void php_zlib_output_compression_start(void)
  303. {
  304. zval zoh;
  305. php_output_handler *h;
  306. switch (ZLIBG(output_compression)) {
  307. case 0:
  308. break;
  309. case 1:
  310. ZLIBG(output_compression) = PHP_OUTPUT_HANDLER_DEFAULT_SIZE;
  311. ZEND_FALLTHROUGH;
  312. default:
  313. if ( php_zlib_output_encoding() &&
  314. (h = php_zlib_output_handler_init(ZEND_STRL(PHP_ZLIB_OUTPUT_HANDLER_NAME), ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS)) &&
  315. (SUCCESS == php_output_handler_start(h))) {
  316. if (ZLIBG(output_handler) && *ZLIBG(output_handler)) {
  317. ZVAL_STRING(&zoh, ZLIBG(output_handler));
  318. php_output_start_user(&zoh, ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS);
  319. zval_ptr_dtor(&zoh);
  320. }
  321. }
  322. break;
  323. }
  324. }
  325. /* }}} */
  326. /* {{{ php_zlib_encode() */
  327. static zend_string *php_zlib_encode(const char *in_buf, size_t in_len, int encoding, int level)
  328. {
  329. int status;
  330. z_stream Z;
  331. zend_string *out;
  332. memset(&Z, 0, sizeof(z_stream));
  333. Z.zalloc = php_zlib_alloc;
  334. Z.zfree = php_zlib_free;
  335. if (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, encoding, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) {
  336. out = zend_string_alloc(PHP_ZLIB_BUFFER_SIZE_GUESS(in_len), 0);
  337. Z.next_in = (Bytef *) in_buf;
  338. Z.next_out = (Bytef *) ZSTR_VAL(out);
  339. Z.avail_in = in_len;
  340. Z.avail_out = ZSTR_LEN(out);
  341. status = deflate(&Z, Z_FINISH);
  342. deflateEnd(&Z);
  343. if (Z_STREAM_END == status) {
  344. /* size buffer down to actual length */
  345. out = zend_string_truncate(out, Z.total_out, 0);
  346. ZSTR_VAL(out)[ZSTR_LEN(out)] = '\0';
  347. return out;
  348. } else {
  349. zend_string_efree(out);
  350. }
  351. }
  352. php_error_docref(NULL, E_WARNING, "%s", zError(status));
  353. return NULL;
  354. }
  355. /* }}} */
  356. /* {{{ php_zlib_inflate_rounds() */
  357. static inline int php_zlib_inflate_rounds(z_stream *Z, size_t max, char **buf, size_t *len)
  358. {
  359. int status, round = 0;
  360. php_zlib_buffer buffer = {NULL, NULL, 0, 0, 0};
  361. *buf = NULL;
  362. *len = 0;
  363. buffer.size = (max && (max < Z->avail_in)) ? max : Z->avail_in;
  364. do {
  365. if ((max && (max <= buffer.used)) || !(buffer.aptr = erealloc_recoverable(buffer.data, buffer.size))) {
  366. status = Z_MEM_ERROR;
  367. } else {
  368. buffer.data = buffer.aptr;
  369. Z->avail_out = buffer.free = buffer.size - buffer.used;
  370. Z->next_out = (Bytef *) buffer.data + buffer.used;
  371. #if 0
  372. fprintf(stderr, "\n%3d: %3d PRIOR: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round, status, buffer.size, buffer.free, buffer.used, Z->avail_in, Z->avail_out);
  373. #endif
  374. status = inflate(Z, Z_NO_FLUSH);
  375. buffer.used += buffer.free - Z->avail_out;
  376. buffer.free = Z->avail_out;
  377. #if 0
  378. fprintf(stderr, "%3d: %3d AFTER: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round, status, buffer.size, buffer.free, buffer.used, Z->avail_in, Z->avail_out);
  379. #endif
  380. buffer.size += (buffer.size >> 3) + 1;
  381. }
  382. } while ((Z_BUF_ERROR == status || (Z_OK == status && Z->avail_in)) && ++round < 100);
  383. if (status == Z_STREAM_END) {
  384. buffer.data = erealloc(buffer.data, buffer.used + 1);
  385. buffer.data[buffer.used] = '\0';
  386. *buf = buffer.data;
  387. *len = buffer.used;
  388. } else {
  389. if (buffer.data) {
  390. efree(buffer.data);
  391. }
  392. /* HACK: See zlib/examples/zpipe.c inf() function for explanation. */
  393. /* This works as long as this function is not used for streaming. Required to catch very short invalid data. */
  394. status = (status == Z_OK) ? Z_DATA_ERROR : status;
  395. }
  396. return status;
  397. }
  398. /* }}} */
  399. /* {{{ php_zlib_decode() */
  400. static int php_zlib_decode(const char *in_buf, size_t in_len, char **out_buf, size_t *out_len, int encoding, size_t max_len)
  401. {
  402. int status = Z_DATA_ERROR;
  403. z_stream Z;
  404. memset(&Z, 0, sizeof(z_stream));
  405. Z.zalloc = php_zlib_alloc;
  406. Z.zfree = php_zlib_free;
  407. if (in_len) {
  408. retry_raw_inflate:
  409. status = inflateInit2(&Z, encoding);
  410. if (Z_OK == status) {
  411. Z.next_in = (Bytef *) in_buf;
  412. Z.avail_in = in_len + 1; /* NOTE: data must be zero terminated */
  413. switch (status = php_zlib_inflate_rounds(&Z, max_len, out_buf, out_len)) {
  414. case Z_STREAM_END:
  415. inflateEnd(&Z);
  416. return SUCCESS;
  417. case Z_DATA_ERROR:
  418. /* raw deflated data? */
  419. if (PHP_ZLIB_ENCODING_ANY == encoding) {
  420. inflateEnd(&Z);
  421. encoding = PHP_ZLIB_ENCODING_RAW;
  422. goto retry_raw_inflate;
  423. }
  424. }
  425. inflateEnd(&Z);
  426. }
  427. }
  428. *out_buf = NULL;
  429. *out_len = 0;
  430. php_error_docref(NULL, E_WARNING, "%s", zError(status));
  431. return FAILURE;
  432. }
  433. /* }}} */
  434. /* {{{ php_zlib_cleanup_ob_gzhandler_mess() */
  435. static void php_zlib_cleanup_ob_gzhandler_mess(void)
  436. {
  437. if (ZLIBG(ob_gzhandler)) {
  438. deflateEnd(&(ZLIBG(ob_gzhandler)->Z));
  439. php_zlib_output_handler_context_dtor(ZLIBG(ob_gzhandler));
  440. ZLIBG(ob_gzhandler) = NULL;
  441. }
  442. }
  443. /* }}} */
  444. /* {{{ Legacy hack */
  445. PHP_FUNCTION(ob_gzhandler)
  446. {
  447. char *in_str;
  448. size_t in_len;
  449. zend_long flags = 0;
  450. php_output_context ctx = {0};
  451. int encoding, rv;
  452. /*
  453. * NOTE that the real ob_gzhandler is an alias to "zlib output compression".
  454. * This is a really bad hack, because
  455. * - we have to initialize a php_zlib_context on demand
  456. * - we have to clean it up in RSHUTDOWN
  457. * - OG(running) is not set or set to any other output handler
  458. * - we have to mess around with php_output_context */
  459. if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &in_str, &in_len, &flags)) {
  460. RETURN_THROWS();
  461. }
  462. if (!(encoding = php_zlib_output_encoding())) {
  463. RETURN_FALSE;
  464. }
  465. if (flags & PHP_OUTPUT_HANDLER_START) {
  466. switch (encoding) {
  467. case PHP_ZLIB_ENCODING_GZIP:
  468. sapi_add_header_ex(ZEND_STRL("Content-Encoding: gzip"), 1, 1);
  469. break;
  470. case PHP_ZLIB_ENCODING_DEFLATE:
  471. sapi_add_header_ex(ZEND_STRL("Content-Encoding: deflate"), 1, 1);
  472. break;
  473. }
  474. sapi_add_header_ex(ZEND_STRL("Vary: Accept-Encoding"), 1, 0);
  475. }
  476. if (!ZLIBG(ob_gzhandler)) {
  477. ZLIBG(ob_gzhandler) = php_zlib_output_handler_context_init();
  478. }
  479. ctx.op = flags;
  480. ctx.in.data = in_str;
  481. ctx.in.used = in_len;
  482. rv = php_zlib_output_handler_ex(ZLIBG(ob_gzhandler), &ctx);
  483. if (SUCCESS != rv) {
  484. if (ctx.out.data && ctx.out.free) {
  485. efree(ctx.out.data);
  486. }
  487. php_zlib_cleanup_ob_gzhandler_mess();
  488. RETURN_FALSE;
  489. }
  490. if (ctx.out.data) {
  491. RETVAL_STRINGL(ctx.out.data, ctx.out.used);
  492. if (ctx.out.free) {
  493. efree(ctx.out.data);
  494. }
  495. } else {
  496. RETVAL_EMPTY_STRING();
  497. }
  498. }
  499. /* }}} */
  500. /* {{{ Returns the coding type used for output compression */
  501. PHP_FUNCTION(zlib_get_coding_type)
  502. {
  503. if (zend_parse_parameters_none() == FAILURE) {
  504. RETURN_THROWS();
  505. }
  506. switch (ZLIBG(compression_coding)) {
  507. case PHP_ZLIB_ENCODING_GZIP:
  508. RETURN_STRINGL("gzip", sizeof("gzip") - 1);
  509. case PHP_ZLIB_ENCODING_DEFLATE:
  510. RETURN_STRINGL("deflate", sizeof("deflate") - 1);
  511. default:
  512. RETURN_FALSE;
  513. }
  514. }
  515. /* }}} */
  516. /* {{{ Read and uncompress entire .gz-file into an array */
  517. PHP_FUNCTION(gzfile)
  518. {
  519. char *filename;
  520. size_t filename_len;
  521. int flags = REPORT_ERRORS;
  522. char buf[8192] = {0};
  523. int i = 0;
  524. zend_long use_include_path = 0;
  525. php_stream *stream;
  526. if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "p|l", &filename, &filename_len, &use_include_path)) {
  527. RETURN_THROWS();
  528. }
  529. if (use_include_path) {
  530. flags |= USE_PATH;
  531. }
  532. /* using a stream here is a bit more efficient (resource wise) than php_gzopen_wrapper */
  533. stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, NULL STREAMS_CC);
  534. if (!stream) {
  535. /* Error reporting is already done by stream code */
  536. RETURN_FALSE;
  537. }
  538. /* Initialize return array */
  539. array_init(return_value);
  540. /* Now loop through the file and do the magic quotes thing if needed */
  541. memset(buf, 0, sizeof(buf));
  542. while (php_stream_gets(stream, buf, sizeof(buf) - 1) != NULL) {
  543. add_index_string(return_value, i++, buf);
  544. }
  545. php_stream_close(stream);
  546. }
  547. /* }}} */
  548. /* {{{ Open a .gz-file and return a .gz-file pointer */
  549. PHP_FUNCTION(gzopen)
  550. {
  551. char *filename;
  552. char *mode;
  553. size_t filename_len, mode_len;
  554. int flags = REPORT_ERRORS;
  555. php_stream *stream;
  556. zend_long use_include_path = 0;
  557. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ps|l", &filename, &filename_len, &mode, &mode_len, &use_include_path) == FAILURE) {
  558. RETURN_THROWS();
  559. }
  560. if (use_include_path) {
  561. flags |= USE_PATH;
  562. }
  563. stream = php_stream_gzopen(NULL, filename, mode, flags, NULL, NULL STREAMS_CC);
  564. if (!stream) {
  565. RETURN_FALSE;
  566. }
  567. php_stream_to_zval(stream, return_value);
  568. }
  569. /* }}} */
  570. /* {{{ Output a .gz-file */
  571. PHP_FUNCTION(readgzfile)
  572. {
  573. char *filename;
  574. size_t filename_len;
  575. int flags = REPORT_ERRORS;
  576. php_stream *stream;
  577. size_t size;
  578. zend_long use_include_path = 0;
  579. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|l", &filename, &filename_len, &use_include_path) == FAILURE) {
  580. RETURN_THROWS();
  581. }
  582. if (use_include_path) {
  583. flags |= USE_PATH;
  584. }
  585. stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, NULL STREAMS_CC);
  586. if (!stream) {
  587. RETURN_FALSE;
  588. }
  589. size = php_stream_passthru(stream);
  590. php_stream_close(stream);
  591. RETURN_LONG(size);
  592. }
  593. /* }}} */
  594. #define PHP_ZLIB_ENCODE_FUNC(name, default_encoding) \
  595. PHP_FUNCTION(name) \
  596. { \
  597. zend_string *in, *out; \
  598. zend_long level = -1; \
  599. zend_long encoding = default_encoding; \
  600. if (default_encoding) { \
  601. if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S|ll", &in, &level, &encoding)) { \
  602. RETURN_THROWS(); \
  603. } \
  604. } else { \
  605. if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Sl|l", &in, &encoding, &level)) { \
  606. RETURN_THROWS(); \
  607. } \
  608. } \
  609. if (level < -1 || level > 9) { \
  610. zend_argument_value_error(default_encoding ? 2 : 3, "must be between -1 and 9"); \
  611. RETURN_THROWS(); \
  612. } \
  613. switch (encoding) { \
  614. case PHP_ZLIB_ENCODING_RAW: \
  615. case PHP_ZLIB_ENCODING_GZIP: \
  616. case PHP_ZLIB_ENCODING_DEFLATE: \
  617. break; \
  618. default: \
  619. zend_argument_value_error(default_encoding ? 3 : 2, "must be one of ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP, or ZLIB_ENCODING_DEFLATE"); \
  620. RETURN_THROWS(); \
  621. } \
  622. if ((out = php_zlib_encode(ZSTR_VAL(in), ZSTR_LEN(in), encoding, level)) == NULL) { \
  623. RETURN_FALSE; \
  624. } \
  625. RETURN_STR(out); \
  626. }
  627. #define PHP_ZLIB_DECODE_FUNC(name, encoding) \
  628. PHP_FUNCTION(name) \
  629. { \
  630. char *in_buf, *out_buf; \
  631. size_t in_len; \
  632. size_t out_len; \
  633. zend_long max_len = 0; \
  634. if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &in_buf, &in_len, &max_len)) { \
  635. RETURN_THROWS(); \
  636. } \
  637. if (max_len < 0) { \
  638. zend_argument_value_error(2, "must be greater than or equal to 0"); \
  639. RETURN_THROWS(); \
  640. } \
  641. if (SUCCESS != php_zlib_decode(in_buf, in_len, &out_buf, &out_len, encoding, max_len)) { \
  642. RETURN_FALSE; \
  643. } \
  644. RETVAL_STRINGL(out_buf, out_len); \
  645. efree(out_buf); \
  646. }
  647. /* {{{ Compress data with the specified encoding */
  648. PHP_ZLIB_ENCODE_FUNC(zlib_encode, 0);
  649. /* }}} */
  650. /* {{{ Uncompress any raw/gzip/zlib encoded data */
  651. PHP_ZLIB_DECODE_FUNC(zlib_decode, PHP_ZLIB_ENCODING_ANY);
  652. /* }}} */
  653. /* NOTE: The naming of these userland functions was quite unlucky */
  654. /* {{{ Encode data with the raw deflate encoding */
  655. PHP_ZLIB_ENCODE_FUNC(gzdeflate, PHP_ZLIB_ENCODING_RAW);
  656. /* }}} */
  657. /* {{{ Encode data with the gzip encoding */
  658. PHP_ZLIB_ENCODE_FUNC(gzencode, PHP_ZLIB_ENCODING_GZIP);
  659. /* }}} */
  660. /* {{{ Encode data with the zlib encoding */
  661. PHP_ZLIB_ENCODE_FUNC(gzcompress, PHP_ZLIB_ENCODING_DEFLATE);
  662. /* }}} */
  663. /* {{{ Decode raw deflate encoded data */
  664. PHP_ZLIB_DECODE_FUNC(gzinflate, PHP_ZLIB_ENCODING_RAW);
  665. /* }}} */
  666. /* {{{ Decode gzip encoded data */
  667. PHP_ZLIB_DECODE_FUNC(gzdecode, PHP_ZLIB_ENCODING_GZIP);
  668. /* }}} */
  669. /* {{{ Decode zlib encoded data */
  670. PHP_ZLIB_DECODE_FUNC(gzuncompress, PHP_ZLIB_ENCODING_DEFLATE);
  671. /* }}} */
  672. static bool zlib_create_dictionary_string(HashTable *options, char **dict, size_t *dictlen) {
  673. zval *option_buffer;
  674. if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("dictionary"))) != NULL) {
  675. ZVAL_DEREF(option_buffer);
  676. switch (Z_TYPE_P(option_buffer)) {
  677. case IS_STRING: {
  678. zend_string *str = Z_STR_P(option_buffer);
  679. *dict = emalloc(ZSTR_LEN(str));
  680. memcpy(*dict, ZSTR_VAL(str), ZSTR_LEN(str));
  681. *dictlen = ZSTR_LEN(str);
  682. } break;
  683. case IS_ARRAY: {
  684. HashTable *dictionary = Z_ARR_P(option_buffer);
  685. if (zend_hash_num_elements(dictionary) > 0) {
  686. char *dictptr;
  687. zval *cur;
  688. zend_string **strings = emalloc(sizeof(zend_string *) * zend_hash_num_elements(dictionary));
  689. zend_string **end, **ptr = strings - 1;
  690. ZEND_HASH_FOREACH_VAL(dictionary, cur) {
  691. size_t i;
  692. *++ptr = zval_get_string(cur);
  693. if (!*ptr || ZSTR_LEN(*ptr) == 0 || EG(exception)) {
  694. if (*ptr) {
  695. efree(*ptr);
  696. }
  697. while (--ptr >= strings) {
  698. efree(ptr);
  699. }
  700. efree(strings);
  701. if (!EG(exception)) {
  702. zend_argument_value_error(2, "must not contain empty strings");
  703. }
  704. return 0;
  705. }
  706. for (i = 0; i < ZSTR_LEN(*ptr); i++) {
  707. if (ZSTR_VAL(*ptr)[i] == 0) {
  708. do {
  709. efree(ptr);
  710. } while (--ptr >= strings);
  711. efree(strings);
  712. zend_argument_value_error(2, "must not contain strings with null bytes");
  713. return 0;
  714. }
  715. }
  716. *dictlen += ZSTR_LEN(*ptr) + 1;
  717. } ZEND_HASH_FOREACH_END();
  718. dictptr = *dict = emalloc(*dictlen);
  719. ptr = strings;
  720. end = strings + zend_hash_num_elements(dictionary);
  721. do {
  722. memcpy(dictptr, ZSTR_VAL(*ptr), ZSTR_LEN(*ptr));
  723. dictptr += ZSTR_LEN(*ptr);
  724. *dictptr++ = 0;
  725. zend_string_release_ex(*ptr, 0);
  726. } while (++ptr != end);
  727. efree(strings);
  728. }
  729. } break;
  730. default:
  731. zend_argument_type_error(2, "must be of type zero-terminated string or array, %s given", zend_zval_type_name(option_buffer));
  732. return 0;
  733. }
  734. }
  735. return 1;
  736. }
  737. /* {{{ Initialize an incremental inflate context with the specified encoding */
  738. PHP_FUNCTION(inflate_init)
  739. {
  740. php_zlib_context *ctx;
  741. zend_long encoding, window = 15;
  742. char *dict = NULL;
  743. size_t dictlen = 0;
  744. HashTable *options = NULL;
  745. zval *option_buffer;
  746. if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l|H", &encoding, &options)) {
  747. RETURN_THROWS();
  748. }
  749. if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("window"))) != NULL) {
  750. window = zval_get_long(option_buffer);
  751. }
  752. if (window < 8 || window > 15) {
  753. zend_value_error("zlib window size (logarithm) (" ZEND_LONG_FMT ") must be within 8..15", window);
  754. RETURN_THROWS();
  755. }
  756. if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
  757. RETURN_THROWS();
  758. }
  759. switch (encoding) {
  760. case PHP_ZLIB_ENCODING_RAW:
  761. case PHP_ZLIB_ENCODING_GZIP:
  762. case PHP_ZLIB_ENCODING_DEFLATE:
  763. break;
  764. default:
  765. zend_value_error("Encoding mode must be ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE");
  766. RETURN_THROWS();
  767. }
  768. object_init_ex(return_value, inflate_context_ce);
  769. ctx = Z_INFLATE_CONTEXT_P(return_value);
  770. ctx->Z.zalloc = php_zlib_alloc;
  771. ctx->Z.zfree = php_zlib_free;
  772. ctx->inflateDict = dict;
  773. ctx->inflateDictlen = dictlen;
  774. ctx->status = Z_OK;
  775. if (encoding < 0) {
  776. encoding += 15 - window;
  777. } else {
  778. encoding -= 15 - window;
  779. }
  780. if (inflateInit2(&ctx->Z, encoding) != Z_OK) {
  781. zval_ptr_dtor(return_value);
  782. php_error_docref(NULL, E_WARNING, "Failed allocating zlib.inflate context");
  783. RETURN_FALSE;
  784. }
  785. if (encoding == PHP_ZLIB_ENCODING_RAW && dictlen > 0) {
  786. switch (inflateSetDictionary(&ctx->Z, (Bytef *) ctx->inflateDict, ctx->inflateDictlen)) {
  787. case Z_OK:
  788. efree(ctx->inflateDict);
  789. ctx->inflateDict = NULL;
  790. break;
  791. case Z_DATA_ERROR:
  792. php_error_docref(NULL, E_WARNING, "Dictionary does not match expected dictionary (incorrect adler32 hash)");
  793. efree(ctx->inflateDict);
  794. ctx->inflateDict = NULL;
  795. break;
  796. EMPTY_SWITCH_DEFAULT_CASE()
  797. }
  798. }
  799. }
  800. /* }}} */
  801. /* {{{ Incrementally inflate encoded data in the specified context */
  802. PHP_FUNCTION(inflate_add)
  803. {
  804. zend_string *out;
  805. char *in_buf;
  806. size_t in_len, buffer_used = 0, CHUNK_SIZE = 8192;
  807. zval *res;
  808. php_zlib_context *ctx;
  809. zend_long flush_type = Z_SYNC_FLUSH;
  810. int status;
  811. if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l", &res, inflate_context_ce, &in_buf, &in_len, &flush_type)) {
  812. RETURN_THROWS();
  813. }
  814. ctx = Z_INFLATE_CONTEXT_P(res);
  815. switch (flush_type) {
  816. case Z_NO_FLUSH:
  817. case Z_PARTIAL_FLUSH:
  818. case Z_SYNC_FLUSH:
  819. case Z_FULL_FLUSH:
  820. case Z_BLOCK:
  821. case Z_FINISH:
  822. break;
  823. default:
  824. zend_argument_value_error(3, "must be one of ZLIB_NO_FLUSH, ZLIB_PARTIAL_FLUSH, ZLIB_SYNC_FLUSH, ZLIB_FULL_FLUSH, ZLIB_BLOCK, or ZLIB_FINISH");
  825. RETURN_THROWS();
  826. }
  827. /* Lazy-resetting the zlib stream so ctx->total_in remains available until the next inflate_add() call. */
  828. if (ctx->status == Z_STREAM_END)
  829. {
  830. ctx->status = Z_OK;
  831. inflateReset(&ctx->Z);
  832. }
  833. if (in_len <= 0 && flush_type != Z_FINISH) {
  834. RETURN_EMPTY_STRING();
  835. }
  836. out = zend_string_alloc((in_len > CHUNK_SIZE) ? in_len : CHUNK_SIZE, 0);
  837. ctx->Z.next_in = (Bytef *) in_buf;
  838. ctx->Z.next_out = (Bytef *) ZSTR_VAL(out);
  839. ctx->Z.avail_in = in_len;
  840. ctx->Z.avail_out = ZSTR_LEN(out);
  841. do {
  842. status = inflate(&ctx->Z, flush_type);
  843. buffer_used = ZSTR_LEN(out) - ctx->Z.avail_out;
  844. ctx->status = status; /* Save status for exposing to userspace */
  845. switch (status) {
  846. case Z_OK:
  847. if (ctx->Z.avail_out == 0) {
  848. /* more output buffer space needed; realloc and try again */
  849. out = zend_string_realloc(out, ZSTR_LEN(out) + CHUNK_SIZE, 0);
  850. ctx->Z.avail_out = CHUNK_SIZE;
  851. ctx->Z.next_out = (Bytef *) ZSTR_VAL(out) + buffer_used;
  852. break;
  853. } else {
  854. goto complete;
  855. }
  856. case Z_STREAM_END:
  857. goto complete;
  858. case Z_BUF_ERROR:
  859. if (flush_type == Z_FINISH && ctx->Z.avail_out == 0) {
  860. /* more output buffer space needed; realloc and try again */
  861. out = zend_string_realloc(out, ZSTR_LEN(out) + CHUNK_SIZE, 0);
  862. ctx->Z.avail_out = CHUNK_SIZE;
  863. ctx->Z.next_out = (Bytef *) ZSTR_VAL(out) + buffer_used;
  864. break;
  865. } else {
  866. /* No more input data; we're finished */
  867. goto complete;
  868. }
  869. case Z_NEED_DICT:
  870. if (ctx->inflateDict) {
  871. switch (inflateSetDictionary(&ctx->Z, (Bytef *) ctx->inflateDict, ctx->inflateDictlen)) {
  872. case Z_OK:
  873. efree(ctx->inflateDict);
  874. ctx->inflateDict = NULL;
  875. break;
  876. case Z_DATA_ERROR:
  877. efree(ctx->inflateDict);
  878. ctx->inflateDict = NULL;
  879. zend_string_release_ex(out, 0);
  880. php_error_docref(NULL, E_WARNING, "Dictionary does not match expected dictionary (incorrect adler32 hash)");
  881. RETURN_FALSE;
  882. EMPTY_SWITCH_DEFAULT_CASE()
  883. }
  884. break;
  885. } else {
  886. php_error_docref(NULL, E_WARNING, "Inflating this data requires a preset dictionary, please specify it in the options array of inflate_init()");
  887. RETURN_FALSE;
  888. }
  889. default:
  890. zend_string_release_ex(out, 0);
  891. php_error_docref(NULL, E_WARNING, "%s", zError(status));
  892. RETURN_FALSE;
  893. }
  894. } while (1);
  895. complete:
  896. out = zend_string_realloc(out, buffer_used, 0);
  897. ZSTR_VAL(out)[buffer_used] = 0;
  898. RETURN_STR(out);
  899. }
  900. /* }}} */
  901. /* {{{ Get decompression status, usually returns either ZLIB_OK or ZLIB_STREAM_END. */
  902. PHP_FUNCTION(inflate_get_status)
  903. {
  904. zval *res;
  905. php_zlib_context *ctx;
  906. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &res, inflate_context_ce) != SUCCESS) {
  907. RETURN_THROWS();
  908. }
  909. ctx = Z_INFLATE_CONTEXT_P(res);
  910. RETURN_LONG(ctx->status);
  911. }
  912. /* }}} */
  913. /* {{{ Get number of bytes read so far. */
  914. PHP_FUNCTION(inflate_get_read_len)
  915. {
  916. zval *res;
  917. php_zlib_context *ctx;
  918. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &res, inflate_context_ce) != SUCCESS) {
  919. RETURN_THROWS();
  920. }
  921. ctx = Z_INFLATE_CONTEXT_P(res);
  922. RETURN_LONG(ctx->Z.total_in);
  923. }
  924. /* }}} */
  925. /* {{{ Initialize an incremental deflate context using the specified encoding */
  926. PHP_FUNCTION(deflate_init)
  927. {
  928. php_zlib_context *ctx;
  929. zend_long encoding, level = -1, memory = 8, window = 15, strategy = Z_DEFAULT_STRATEGY;
  930. char *dict = NULL;
  931. size_t dictlen = 0;
  932. HashTable *options = NULL;
  933. zval *option_buffer;
  934. if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l|H", &encoding, &options)) {
  935. RETURN_THROWS();
  936. }
  937. if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("level"))) != NULL) {
  938. level = zval_get_long(option_buffer);
  939. }
  940. if (level < -1 || level > 9) {
  941. zend_value_error("deflate_init(): \"level\" option must be between -1 and 9");
  942. RETURN_THROWS();
  943. }
  944. if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("memory"))) != NULL) {
  945. memory = zval_get_long(option_buffer);
  946. }
  947. if (memory < 1 || memory > 9) {
  948. zend_value_error("deflate_init(): \"memory\" option must be between 1 and 9");
  949. RETURN_THROWS();
  950. }
  951. if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("window"))) != NULL) {
  952. window = zval_get_long(option_buffer);
  953. }
  954. if (window < 8 || window > 15) {
  955. zend_value_error("deflate_init(): \"window\" option must be between 8 and 15");
  956. RETURN_THROWS();
  957. }
  958. if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("strategy"))) != NULL) {
  959. strategy = zval_get_long(option_buffer);
  960. }
  961. switch (strategy) {
  962. case Z_FILTERED:
  963. case Z_HUFFMAN_ONLY:
  964. case Z_RLE:
  965. case Z_FIXED:
  966. case Z_DEFAULT_STRATEGY:
  967. break;
  968. default:
  969. zend_value_error("deflate_init(): \"strategy\" option must be one of ZLIB_FILTERED, ZLIB_HUFFMAN_ONLY, ZLIB_RLE, ZLIB_FIXED, or ZLIB_DEFAULT_STRATEGY");
  970. RETURN_THROWS();
  971. }
  972. if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
  973. RETURN_THROWS();
  974. }
  975. switch (encoding) {
  976. case PHP_ZLIB_ENCODING_RAW:
  977. case PHP_ZLIB_ENCODING_GZIP:
  978. case PHP_ZLIB_ENCODING_DEFLATE:
  979. break;
  980. default:
  981. zend_argument_value_error(1, "must be one of ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP, or ZLIB_ENCODING_DEFLATE");
  982. RETURN_THROWS();
  983. }
  984. object_init_ex(return_value, deflate_context_ce);
  985. ctx = Z_DEFLATE_CONTEXT_P(return_value);
  986. ctx->Z.zalloc = php_zlib_alloc;
  987. ctx->Z.zfree = php_zlib_free;
  988. if (encoding < 0) {
  989. encoding += 15 - window;
  990. } else {
  991. encoding -= 15 - window;
  992. }
  993. if (deflateInit2(&ctx->Z, level, Z_DEFLATED, encoding, memory, strategy) != Z_OK) {
  994. zval_ptr_dtor(return_value);
  995. php_error_docref(NULL, E_WARNING, "Failed allocating zlib.deflate context");
  996. RETURN_FALSE;
  997. }
  998. if (dict) {
  999. int success = deflateSetDictionary(&ctx->Z, (Bytef *) dict, dictlen);
  1000. ZEND_ASSERT(success == Z_OK);
  1001. efree(dict);
  1002. }
  1003. }
  1004. /* }}} */
  1005. /* {{{ Incrementally deflate data in the specified context */
  1006. PHP_FUNCTION(deflate_add)
  1007. {
  1008. zend_string *out;
  1009. char *in_buf;
  1010. size_t in_len, out_size, buffer_used;
  1011. zval *res;
  1012. php_zlib_context *ctx;
  1013. zend_long flush_type = Z_SYNC_FLUSH;
  1014. int status;
  1015. if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l", &res, deflate_context_ce, &in_buf, &in_len, &flush_type)) {
  1016. RETURN_THROWS();
  1017. }
  1018. ctx = Z_DEFLATE_CONTEXT_P(res);
  1019. switch (flush_type) {
  1020. case Z_BLOCK:
  1021. #if ZLIB_VERNUM < 0x1240L
  1022. zend_throw_error(NULL, "zlib >= 1.2.4 required for BLOCK deflate; current version: %s", ZLIB_VERSION);
  1023. RETURN_THROWS();
  1024. #endif
  1025. case Z_NO_FLUSH:
  1026. case Z_PARTIAL_FLUSH:
  1027. case Z_SYNC_FLUSH:
  1028. case Z_FULL_FLUSH:
  1029. case Z_FINISH:
  1030. break;
  1031. default:
  1032. zend_argument_value_error(3, "must be one of ZLIB_NO_FLUSH, ZLIB_PARTIAL_FLUSH, ZLIB_SYNC_FLUSH, ZLIB_FULL_FLUSH, ZLIB_BLOCK, or ZLIB_FINISH");
  1033. RETURN_THROWS();
  1034. }
  1035. if (in_len <= 0 && flush_type != Z_FINISH) {
  1036. RETURN_EMPTY_STRING();
  1037. }
  1038. out_size = PHP_ZLIB_BUFFER_SIZE_GUESS(in_len);
  1039. out_size = (out_size < 64) ? 64 : out_size;
  1040. out = zend_string_alloc(out_size, 0);
  1041. ctx->Z.next_in = (Bytef *) in_buf;
  1042. ctx->Z.next_out = (Bytef *) ZSTR_VAL(out);
  1043. ctx->Z.avail_in = in_len;
  1044. ctx->Z.avail_out = ZSTR_LEN(out);
  1045. buffer_used = 0;
  1046. do {
  1047. if (ctx->Z.avail_out == 0) {
  1048. /* more output buffer space needed; realloc and try again */
  1049. /* adding 64 more bytes solved every issue I have seen */
  1050. out = zend_string_realloc(out, ZSTR_LEN(out) + 64, 0);
  1051. ctx->Z.avail_out = 64;
  1052. ctx->Z.next_out = (Bytef *) ZSTR_VAL(out) + buffer_used;
  1053. }
  1054. status = deflate(&ctx->Z, flush_type);
  1055. buffer_used = ZSTR_LEN(out) - ctx->Z.avail_out;
  1056. } while (status == Z_OK && ctx->Z.avail_out == 0);
  1057. switch (status) {
  1058. case Z_OK:
  1059. ZSTR_LEN(out) = (char *) ctx->Z.next_out - ZSTR_VAL(out);
  1060. ZSTR_VAL(out)[ZSTR_LEN(out)] = 0;
  1061. RETURN_STR(out);
  1062. break;
  1063. case Z_STREAM_END:
  1064. ZSTR_LEN(out) = (char *) ctx->Z.next_out - ZSTR_VAL(out);
  1065. ZSTR_VAL(out)[ZSTR_LEN(out)] = 0;
  1066. deflateReset(&ctx->Z);
  1067. RETURN_STR(out);
  1068. break;
  1069. default:
  1070. zend_string_release_ex(out, 0);
  1071. php_error_docref(NULL, E_WARNING, "zlib error (%s)", zError(status));
  1072. RETURN_FALSE;
  1073. }
  1074. }
  1075. /* }}} */
  1076. #ifdef COMPILE_DL_ZLIB
  1077. #ifdef ZTS
  1078. ZEND_TSRMLS_CACHE_DEFINE()
  1079. #endif
  1080. ZEND_GET_MODULE(php_zlib)
  1081. #endif
  1082. /* {{{ OnUpdate_zlib_output_compression */
  1083. static PHP_INI_MH(OnUpdate_zlib_output_compression)
  1084. {
  1085. int int_value;
  1086. char *ini_value;
  1087. if (new_value == NULL) {
  1088. return FAILURE;
  1089. }
  1090. if (zend_string_equals_literal_ci(new_value, "off")) {
  1091. int_value = 0;
  1092. } else if (zend_string_equals_literal_ci(new_value, "on")) {
  1093. int_value = 1;
  1094. } else {
  1095. int_value = zend_atoi(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
  1096. }
  1097. ini_value = zend_ini_string("output_handler", sizeof("output_handler"), 0);
  1098. if (ini_value && *ini_value && int_value) {
  1099. php_error_docref("ref.outcontrol", E_CORE_ERROR, "Cannot use both zlib.output_compression and output_handler together!!");
  1100. return FAILURE;
  1101. }
  1102. if (stage == PHP_INI_STAGE_RUNTIME) {
  1103. int status = php_output_get_status();
  1104. if (status & PHP_OUTPUT_SENT) {
  1105. php_error_docref("ref.outcontrol", E_WARNING, "Cannot change zlib.output_compression - headers already sent");
  1106. return FAILURE;
  1107. }
  1108. }
  1109. zend_long *p = (zend_long *) ZEND_INI_GET_ADDR();
  1110. *p = int_value;
  1111. ZLIBG(output_compression) = ZLIBG(output_compression_default);
  1112. if (stage == PHP_INI_STAGE_RUNTIME && int_value) {
  1113. if (!php_output_handler_started(ZEND_STRL(PHP_ZLIB_OUTPUT_HANDLER_NAME))) {
  1114. php_zlib_output_compression_start();
  1115. }
  1116. }
  1117. return SUCCESS;
  1118. }
  1119. /* }}} */
  1120. /* {{{ OnUpdate_zlib_output_handler */
  1121. static PHP_INI_MH(OnUpdate_zlib_output_handler)
  1122. {
  1123. if (stage == PHP_INI_STAGE_RUNTIME && (php_output_get_status() & PHP_OUTPUT_SENT)) {
  1124. php_error_docref("ref.outcontrol", E_WARNING, "Cannot change zlib.output_handler - headers already sent");
  1125. return FAILURE;
  1126. }
  1127. return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
  1128. }
  1129. /* }}} */
  1130. /* {{{ INI */
  1131. PHP_INI_BEGIN()
  1132. STD_PHP_INI_BOOLEAN("zlib.output_compression", "0", PHP_INI_ALL, OnUpdate_zlib_output_compression, output_compression_default, zend_zlib_globals, zlib_globals)
  1133. STD_PHP_INI_ENTRY("zlib.output_compression_level", "-1", PHP_INI_ALL, OnUpdateLong, output_compression_level, zend_zlib_globals, zlib_globals)
  1134. STD_PHP_INI_ENTRY("zlib.output_handler", "", PHP_INI_ALL, OnUpdate_zlib_output_handler, output_handler, zend_zlib_globals, zlib_globals)
  1135. PHP_INI_END()
  1136. /* }}} */
  1137. /* {{{ PHP_MINIT_FUNCTION */
  1138. static PHP_MINIT_FUNCTION(zlib)
  1139. {
  1140. php_register_url_stream_wrapper("compress.zlib", &php_stream_gzip_wrapper);
  1141. php_stream_filter_register_factory("zlib.*", &php_zlib_filter_factory);
  1142. php_output_handler_alias_register(ZEND_STRL("ob_gzhandler"), php_zlib_output_handler_init);
  1143. php_output_handler_conflict_register(ZEND_STRL("ob_gzhandler"), php_zlib_output_conflict_check);
  1144. php_output_handler_conflict_register(ZEND_STRL(PHP_ZLIB_OUTPUT_HANDLER_NAME), php_zlib_output_conflict_check);
  1145. inflate_context_ce = register_class_InflateContext();
  1146. inflate_context_ce->create_object = inflate_context_create_object;
  1147. memcpy(&inflate_context_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  1148. inflate_context_object_handlers.offset = XtOffsetOf(php_zlib_context, std);
  1149. inflate_context_object_handlers.free_obj = inflate_context_free_obj;
  1150. inflate_context_object_handlers.get_constructor = inflate_context_get_constructor;
  1151. inflate_context_object_handlers.clone_obj = NULL;
  1152. inflate_context_object_handlers.compare = zend_objects_not_comparable;
  1153. deflate_context_ce = register_class_DeflateContext();
  1154. deflate_context_ce->create_object = deflate_context_create_object;
  1155. memcpy(&deflate_context_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  1156. deflate_context_object_handlers.offset = XtOffsetOf(php_zlib_context, std);
  1157. deflate_context_object_handlers.free_obj = deflate_context_free_obj;
  1158. deflate_context_object_handlers.get_constructor = deflate_context_get_constructor;
  1159. deflate_context_object_handlers.clone_obj = NULL;
  1160. deflate_context_object_handlers.compare = zend_objects_not_comparable;
  1161. REGISTER_LONG_CONSTANT("FORCE_GZIP", PHP_ZLIB_ENCODING_GZIP, CONST_CS|CONST_PERSISTENT);
  1162. REGISTER_LONG_CONSTANT("FORCE_DEFLATE", PHP_ZLIB_ENCODING_DEFLATE, CONST_CS|CONST_PERSISTENT);
  1163. REGISTER_LONG_CONSTANT("ZLIB_ENCODING_RAW", PHP_ZLIB_ENCODING_RAW, CONST_CS|CONST_PERSISTENT);
  1164. REGISTER_LONG_CONSTANT("ZLIB_ENCODING_GZIP", PHP_ZLIB_ENCODING_GZIP, CONST_CS|CONST_PERSISTENT);
  1165. REGISTER_LONG_CONSTANT("ZLIB_ENCODING_DEFLATE", PHP_ZLIB_ENCODING_DEFLATE, CONST_CS|CONST_PERSISTENT);
  1166. REGISTER_LONG_CONSTANT("ZLIB_NO_FLUSH", Z_NO_FLUSH, CONST_CS|CONST_PERSISTENT);
  1167. REGISTER_LONG_CONSTANT("ZLIB_PARTIAL_FLUSH", Z_PARTIAL_FLUSH, CONST_CS|CONST_PERSISTENT);
  1168. REGISTER_LONG_CONSTANT("ZLIB_SYNC_FLUSH", Z_SYNC_FLUSH, CONST_CS|CONST_PERSISTENT);
  1169. REGISTER_LONG_CONSTANT("ZLIB_FULL_FLUSH", Z_FULL_FLUSH, CONST_CS|CONST_PERSISTENT);
  1170. REGISTER_LONG_CONSTANT("ZLIB_BLOCK", Z_BLOCK, CONST_CS|CONST_PERSISTENT);
  1171. REGISTER_LONG_CONSTANT("ZLIB_FINISH", Z_FINISH, CONST_CS|CONST_PERSISTENT);
  1172. REGISTER_LONG_CONSTANT("ZLIB_FILTERED", Z_FILTERED, CONST_CS|CONST_PERSISTENT);
  1173. REGISTER_LONG_CONSTANT("ZLIB_HUFFMAN_ONLY", Z_HUFFMAN_ONLY, CONST_CS|CONST_PERSISTENT);
  1174. REGISTER_LONG_CONSTANT("ZLIB_RLE", Z_RLE, CONST_CS|CONST_PERSISTENT);
  1175. REGISTER_LONG_CONSTANT("ZLIB_FIXED", Z_FIXED, CONST_CS|CONST_PERSISTENT);
  1176. REGISTER_LONG_CONSTANT("ZLIB_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY, CONST_CS|CONST_PERSISTENT);
  1177. REGISTER_STRING_CONSTANT("ZLIB_VERSION", ZLIB_VERSION, CONST_CS|CONST_PERSISTENT);
  1178. REGISTER_LONG_CONSTANT("ZLIB_VERNUM", ZLIB_VERNUM, CONST_CS|CONST_PERSISTENT);
  1179. REGISTER_LONG_CONSTANT("ZLIB_OK", Z_OK, CONST_CS|CONST_PERSISTENT);
  1180. REGISTER_LONG_CONSTANT("ZLIB_STREAM_END", Z_STREAM_END, CONST_CS|CONST_PERSISTENT);
  1181. REGISTER_LONG_CONSTANT("ZLIB_NEED_DICT", Z_NEED_DICT, CONST_CS|CONST_PERSISTENT);
  1182. REGISTER_LONG_CONSTANT("ZLIB_ERRNO", Z_ERRNO, CONST_CS|CONST_PERSISTENT);
  1183. REGISTER_LONG_CONSTANT("ZLIB_STREAM_ERROR", Z_STREAM_ERROR, CONST_CS|CONST_PERSISTENT);
  1184. REGISTER_LONG_CONSTANT("ZLIB_DATA_ERROR", Z_DATA_ERROR, CONST_CS|CONST_PERSISTENT);
  1185. REGISTER_LONG_CONSTANT("ZLIB_MEM_ERROR", Z_MEM_ERROR, CONST_CS|CONST_PERSISTENT);
  1186. REGISTER_LONG_CONSTANT("ZLIB_BUF_ERROR", Z_BUF_ERROR, CONST_CS|CONST_PERSISTENT);
  1187. REGISTER_LONG_CONSTANT("ZLIB_VERSION_ERROR", Z_VERSION_ERROR, CONST_CS|CONST_PERSISTENT);
  1188. REGISTER_INI_ENTRIES();
  1189. return SUCCESS;
  1190. }
  1191. /* }}} */
  1192. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  1193. static PHP_MSHUTDOWN_FUNCTION(zlib)
  1194. {
  1195. php_unregister_url_stream_wrapper("zlib");
  1196. php_stream_filter_unregister_factory("zlib.*");
  1197. UNREGISTER_INI_ENTRIES();
  1198. return SUCCESS;
  1199. }
  1200. /* }}} */
  1201. /* {{{ PHP_RINIT_FUNCTION */
  1202. static PHP_RINIT_FUNCTION(zlib)
  1203. {
  1204. ZLIBG(compression_coding) = 0;
  1205. if (!ZLIBG(handler_registered)) {
  1206. ZLIBG(output_compression) = ZLIBG(output_compression_default);
  1207. php_zlib_output_compression_start();
  1208. }
  1209. return SUCCESS;
  1210. }
  1211. /* }}} */
  1212. /* {{{ PHP_RSHUTDOWN_FUNCTION */
  1213. static PHP_RSHUTDOWN_FUNCTION(zlib)
  1214. {
  1215. php_zlib_cleanup_ob_gzhandler_mess();
  1216. ZLIBG(handler_registered) = 0;
  1217. return SUCCESS;
  1218. }
  1219. /* }}} */
  1220. /* {{{ PHP_MINFO_FUNCTION */
  1221. static PHP_MINFO_FUNCTION(zlib)
  1222. {
  1223. php_info_print_table_start();
  1224. php_info_print_table_header(2, "ZLib Support", "enabled");
  1225. php_info_print_table_row(2, "Stream Wrapper", "compress.zlib://");
  1226. php_info_print_table_row(2, "Stream Filter", "zlib.inflate, zlib.deflate");
  1227. php_info_print_table_row(2, "Compiled Version", ZLIB_VERSION);
  1228. php_info_print_table_row(2, "Linked Version", (char *) zlibVersion());
  1229. php_info_print_table_end();
  1230. DISPLAY_INI_ENTRIES();
  1231. }
  1232. /* }}} */
  1233. /* {{{ ZEND_MODULE_GLOBALS_CTOR */
  1234. static PHP_GINIT_FUNCTION(zlib)
  1235. {
  1236. #if defined(COMPILE_DL_ZLIB) && defined(ZTS)
  1237. ZEND_TSRMLS_CACHE_UPDATE();
  1238. #endif
  1239. zlib_globals->ob_gzhandler = NULL;
  1240. zlib_globals->handler_registered = 0;
  1241. }
  1242. /* }}} */
  1243. /* {{{ php_zlib_module_entry */
  1244. zend_module_entry php_zlib_module_entry = {
  1245. STANDARD_MODULE_HEADER,
  1246. "zlib",
  1247. ext_functions,
  1248. PHP_MINIT(zlib),
  1249. PHP_MSHUTDOWN(zlib),
  1250. PHP_RINIT(zlib),
  1251. PHP_RSHUTDOWN(zlib),
  1252. PHP_MINFO(zlib),
  1253. PHP_ZLIB_VERSION,
  1254. PHP_MODULE_GLOBALS(zlib),
  1255. PHP_GINIT(zlib),
  1256. NULL,
  1257. NULL,
  1258. STANDARD_MODULE_PROPERTIES_EX
  1259. };
  1260. /* }}} */