Back to index

php5  5.3.10
Defines | Functions | Variables
zlib.c File Reference
#include "php.h"
#include "SAPI.h"
#include "php_ini.h"
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/param.h>
#include "ext/standard/head.h"
#include "safe_mode.h"
#include "ext/standard/php_standard.h"
#include "ext/standard/info.h"
#include "php_zlib.h"
#include "fopen_wrappers.h"

Go to the source code of this file.

Defines

#define PHP_ZLIB_MODIFIER   1000
#define OS_CODE   0x03 /* FIXME */
#define GZIP_HEADER_LENGTH   10
#define GZIP_FOOTER_LENGTH   8

Functions

static int php_zlib_output_compression_start (TSRMLS_D)
static PHP_MINIT_FUNCTION (zlib)
static PHP_MSHUTDOWN_FUNCTION (zlib)
static PHP_RINIT_FUNCTION (zlib)
static PHP_MINFO_FUNCTION (zlib)
static PHP_FUNCTION (gzopen)
static PHP_FUNCTION (readgzfile)
static PHP_FUNCTION (gzfile)
static PHP_FUNCTION (gzcompress)
static PHP_FUNCTION (gzuncompress)
static PHP_FUNCTION (gzdeflate)
static PHP_FUNCTION (gzinflate)
static PHP_FUNCTION (gzencode)
static PHP_FUNCTION (ob_gzhandler)
static PHP_FUNCTION (zlib_get_coding_type)
static voidpf php_zlib_alloc (voidpf opaque, uInt items, uInt size)
static void php_zlib_free (voidpf opaque, voidpf address)
static PHP_INI_MH (OnUpdate_zlib_output_compression)
static PHP_INI_MH (OnUpdate_zlib_output_compression_level)
static PHP_INI_MH (OnUpdate_zlib_output_handler)
static int php_do_deflate (uint str_length, Bytef **p_buffer, uint *p_buffer_len, zend_bool do_start, zend_bool do_end TSRMLS_DC)
static int php_deflate_string (const char *str, uint str_length, char **newstr, uint *new_length, zend_bool do_start, zend_bool do_end TSRMLS_DC)
int php_ob_gzhandler_check (TSRMLS_D)
static void php_gzip_output_handler (char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
static int php_enable_output_compression (int buffer_size TSRMLS_DC)

Variables

static const int gz_magic [2] = {0x1f, 0x8b}
static const zend_function_entry php_zlib_functions []
zend_module_entry php_zlib_module_entry

Define Documentation

#define GZIP_FOOTER_LENGTH   8

Definition at line 86 of file zlib.c.

#define GZIP_HEADER_LENGTH   10

Definition at line 85 of file zlib.c.

#define OS_CODE   0x03 /* FIXME */

Definition at line 84 of file zlib.c.

#define PHP_ZLIB_MODIFIER   1000

Definition at line 81 of file zlib.c.


Function Documentation

static int php_deflate_string ( const char *  str,
uint  str_length,
char **  newstr,
uint new_length,
zend_bool  do_start,
zend_bool do_end  TSRMLS_DC 
) [static]

Definition at line 784 of file zlib.c.

{
       int err;

       if (do_start) {
              ZLIBG(stream).zalloc = php_zlib_alloc;
              ZLIBG(stream).zfree = php_zlib_free;
              ZLIBG(stream).opaque = Z_NULL;

              switch (ZLIBG(compression_coding)) {
                     case CODING_GZIP:
                            /* windowBits is passed < 0 to suppress zlib header & trailer */
                            if (deflateInit2(&ZLIBG(stream), ZLIBG(output_compression_level), Z_DEFLATED,       -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) {
                                   /* TODO: print out error */
                                   return FAILURE;
                            }

                            ZLIBG(crc) = crc32(0L, Z_NULL, 0);
                            break;

                     case CODING_DEFLATE:
                            if (deflateInit(&ZLIBG(stream), ZLIBG(output_compression_level)) != Z_OK) {
                                   /* TODO: print out error */
                                   return FAILURE;
                            }
                            break;
              }
       }

       ZLIBG(stream).next_in = (Bytef *) str;
       ZLIBG(stream).avail_in = (uInt) str_length;

       if (ZLIBG(compression_coding) == CODING_GZIP) {
              ZLIBG(crc) = crc32(ZLIBG(crc), (const Bytef *) str, str_length);
       }

       err = php_do_deflate(str_length, (Bytef **) newstr, new_length, do_start, do_end TSRMLS_CC);
       /* TODO: error handling (err may be Z_STREAM_ERROR, Z_BUF_ERROR, ?) */

       if (do_start && ZLIBG(compression_coding) == CODING_GZIP) {
              /* Write a very simple .gz header: */
              (*newstr)[0] = gz_magic[0];
              (*newstr)[1] = gz_magic[1];
              (*newstr)[2] = Z_DEFLATED;
              (*newstr)[3] = (*newstr)[4] = (*newstr)[5] = (*newstr)[6] = (*newstr)[7] = (*newstr)[8] = 0;
              (*newstr)[9] = OS_CODE;
              *new_length += 10;
       }
       if (do_end) {
              if (ZLIBG(compression_coding) == CODING_GZIP) {
                     char *trailer = (*newstr) + (*new_length);

                     /* write crc & stream.total_in in LSB order */
                     trailer[0] = (char) ZLIBG(crc) & 0xFF;
                     trailer[1] = (char) (ZLIBG(crc) >> 8) & 0xFF;
                     trailer[2] = (char) (ZLIBG(crc) >> 16) & 0xFF;
                     trailer[3] = (char) (ZLIBG(crc) >> 24) & 0xFF;
                     trailer[4] = (char) ZLIBG(stream).total_in & 0xFF;
                     trailer[5] = (char) (ZLIBG(stream).total_in >> 8) & 0xFF;
                     trailer[6] = (char) (ZLIBG(stream).total_in >> 16) & 0xFF;
                     trailer[7] = (char) (ZLIBG(stream).total_in >> 24) & 0xFF;
                     trailer[8] = '\0';
                     *new_length += 8;
              }
              deflateEnd(&ZLIBG(stream));
       }

       return SUCCESS;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int php_do_deflate ( uint  str_length,
Bytef **  p_buffer,
uint p_buffer_len,
zend_bool  do_start,
zend_bool do_end  TSRMLS_DC 
) [static]

Definition at line 738 of file zlib.c.

{
       Bytef *buffer;
       uInt prev_outlen, outlen;
       int err;
       int start_offset = ((do_start && ZLIBG(compression_coding) == CODING_GZIP) ? 10 : 0);
       int end_offset = (do_end ? 8 : 0);

       outlen = (uint) (str_length + (str_length / PHP_ZLIB_MODIFIER) + 12 + 1); /* leave some room for a trailing \0 */
       if ((outlen + start_offset + end_offset) > *p_buffer_len) {
              buffer = (Bytef *) emalloc(outlen + start_offset + end_offset);
       } else {
              buffer = *p_buffer;
       }

       ZLIBG(stream).next_out = buffer + start_offset;
       ZLIBG(stream).avail_out = outlen;

       err = deflate(&ZLIBG(stream), Z_SYNC_FLUSH);
       while (err == Z_OK && !ZLIBG(stream).avail_out) {
              prev_outlen = outlen;
              outlen *= 3;
              if ((outlen + start_offset + end_offset) > *p_buffer_len) {
                     buffer = erealloc(buffer, outlen + start_offset + end_offset);
              }

              ZLIBG(stream).next_out = buffer + start_offset + prev_outlen;
              ZLIBG(stream).avail_out = prev_outlen * 2;

              err = deflate(&ZLIBG(stream), Z_SYNC_FLUSH);
       }

       if (do_end) {
              err = deflate(&ZLIBG(stream), Z_FINISH);
              buffer[outlen + start_offset - ZLIBG(stream).avail_out] = '\0';
       }

       *p_buffer = buffer;
       *p_buffer_len = outlen - ZLIBG(stream).avail_out;

       return err;
}

Here is the caller graph for this function:

static int php_enable_output_compression ( int buffer_size  TSRMLS_DC) [static]

Definition at line 1109 of file zlib.c.

{
       zval **a_encoding;

       zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);

       if (!PG(http_globals)[TRACK_VARS_SERVER]
              || zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING"), (void **) &a_encoding) == FAILURE
       ) {
              return FAILURE;
       }

       convert_to_string_ex(a_encoding);

       if (php_memnstr(Z_STRVAL_PP(a_encoding), "gzip", 4, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) {
              ZLIBG(compression_coding) = CODING_GZIP;
       } else if (php_memnstr(Z_STRVAL_PP(a_encoding), "deflate", 7, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) {
              ZLIBG(compression_coding) = CODING_DEFLATE;
       } else {
              return FAILURE;
       }

       php_ob_set_internal_handler(php_gzip_output_handler, (uint)buffer_size, "zlib output compression", 0 TSRMLS_CC);

       if (ZLIBG(output_handler) && strlen(ZLIBG(output_handler))) {
              php_start_ob_buffer_named(ZLIBG(output_handler), 0, 1 TSRMLS_CC);
       }
       return SUCCESS;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PHP_FUNCTION ( gzopen  ) [static]

Definition at line 436 of file zlib.c.

{
       char *filename, *mode;
       int filename_len, mode_len;
       long flags = 0;
       php_stream *stream;
       int use_include_path = 0;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &filename, &filename_len, &mode, &mode_len, &flags) == FAILURE) {
              return;
       }

       use_include_path = flags ? USE_PATH : 0;

       stream = php_stream_gzopen(NULL, filename, mode, use_include_path | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, NULL STREAMS_CC TSRMLS_CC);

       if (!stream) {
              RETURN_FALSE;
       }
       php_stream_to_zval(stream, return_value);
}

Here is the call graph for this function:

static PHP_FUNCTION ( readgzfile  ) [static]

Definition at line 464 of file zlib.c.

{
       char *filename;
       int filename_len;
       long flags = 0;
       php_stream *stream;
       int size;
       int use_include_path = 0;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &filename, &filename_len, &flags) == FAILURE) {
              return;
       }

       use_include_path = flags ? USE_PATH : 0;

       stream = php_stream_gzopen(NULL, filename, "rb", use_include_path | ENFORCE_SAFE_MODE, NULL, NULL STREAMS_CC TSRMLS_CC);
       if (!stream) {
              RETURN_FALSE;
       }
       size = php_stream_passthru(stream);
       php_stream_close(stream);
       RETURN_LONG(size);
}

Here is the call graph for this function:

static PHP_FUNCTION ( gzfile  ) [static]

Definition at line 391 of file zlib.c.

{
       char *filename;
       int filename_len;
       long flags = 0;
       char *slashed, buf[8192];
       register int i = 0;
       int use_include_path = 0;
       php_stream *stream;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &filename, &filename_len, &flags) == FAILURE) {
              return;
       }

       use_include_path = flags ? USE_PATH : 0;

       /* using a stream here is a bit more efficient (resource wise) than php_gzopen_wrapper */
       stream = php_stream_gzopen(NULL, filename, "rb", use_include_path | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, NULL STREAMS_CC TSRMLS_CC);
       if (stream == NULL) {
              /* Error reporting is already done by stream code */
              RETURN_FALSE;
       }

       /* Initialize return array */
       array_init(return_value);

       /* Now loop through the file and do the magic quotes thing if needed */
       memset(buf,0,sizeof(buf));

       while (php_stream_gets(stream, buf, sizeof(buf) - 1) != NULL) {
              if (PG(magic_quotes_runtime)) {
                     int len;

                     slashed = php_addslashes(buf, 0, &len, 0 TSRMLS_CC); /* 0 = don't free source string */
                     add_index_stringl(return_value, i++, slashed, len, 0);
              } else {
                     add_index_string(return_value, i++, buf, 1);
              }
       }
       php_stream_close(stream);
}

Here is the call graph for this function:

static PHP_FUNCTION ( gzcompress  ) [static]

Definition at line 491 of file zlib.c.

{
       int data_len, status;
       long level = Z_DEFAULT_COMPRESSION;
       unsigned long l2;
       char *data, *s2;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &level) == FAILURE) {
              return;
       }

       if ((level < -1) || (level > 9)) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level (%ld) must be within -1..9", level);
              RETURN_FALSE;
       }

       l2 = data_len + (data_len / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */
       s2 = (char *) emalloc(l2);
       if (!s2) {
              RETURN_FALSE;
       }

       if (level >= 0) {
              status = compress2(s2, &l2, data, data_len, level);
       } else {
              status = compress(s2, &l2, data, data_len);
       }

       if (status == Z_OK) {
              s2 = erealloc(s2, l2 + 1);
              s2[l2] = '\0';
              RETURN_STRINGL(s2, l2, 0);
       } else {
              efree(s2);
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

static PHP_FUNCTION ( gzuncompress  ) [static]

Definition at line 533 of file zlib.c.

{
       int data_len, status;
       unsigned int factor=1, maxfactor=16;
       long limit = 0;
       unsigned long plength=0, length;
       char *data, *s1=NULL, *s2=NULL;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &limit) == FAILURE) {
              return;
       }

       if (limit < 0) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "length (%ld) must be greater or equal zero", limit);
              RETURN_FALSE;
       }
       plength = limit;

       /*
        zlib::uncompress() wants to know the output data length
        if none was given as a parameter
        we try from input length * 2 up to input length * 2^15
        doubling it whenever it wasn't big enough
        that should be eneugh for all real life cases
       */
       do {
              length = plength ? plength : (unsigned long)data_len * (1 << factor++);
              s2 = (char *) erealloc(s1, length);
              status = uncompress(s2, &length, data, data_len);
              s1 = s2;
       } while ((status == Z_BUF_ERROR) && (!plength) && (factor < maxfactor));

       if (status == Z_OK) {
              s2 = erealloc(s2, length + 1); /* space for \0 */
              s2[ length ] = '\0';
              RETURN_STRINGL(s2, length, 0);
       } else {
              efree(s2);
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

static PHP_FUNCTION ( gzdeflate  ) [static]

Definition at line 579 of file zlib.c.

{
       int data_len,status;
       long level = Z_DEFAULT_COMPRESSION;
       z_stream stream;
       char *data, *s2;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &level) == FAILURE) {
              return;
       }

       if ((level < -1) || (level > 9)) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level (%ld) must be within -1..9", level);
              RETURN_FALSE;
       }

       stream.data_type = Z_ASCII;
       stream.zalloc = php_zlib_alloc;
       stream.zfree  = php_zlib_free;
       stream.opaque = (voidpf) Z_NULL;

       stream.next_in = (Bytef *) data;
       stream.avail_in = data_len;

       stream.avail_out = stream.avail_in + (stream.avail_in / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */

       s2 = (char *) emalloc(stream.avail_out);
       if (!s2) {
              RETURN_FALSE;
       }

       stream.next_out = s2;

       /* init with -MAX_WBITS disables the zlib internal headers */
       status = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, 0);
       if (status == Z_OK) {
              status = deflate(&stream, Z_FINISH);
              if (status != Z_STREAM_END) {
                     deflateEnd(&stream);
                     if (status == Z_OK) {
                            status = Z_BUF_ERROR;
                     }
              } else {
                     status = deflateEnd(&stream);
              }
       }

       if (status == Z_OK) {
              s2 = erealloc(s2,stream.total_out + 1); /* resize to buffer to the "right" size */
              s2[ stream.total_out ] = '\0';
              RETURN_STRINGL(s2, stream.total_out, 0);
       } else {
              efree(s2);
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

static PHP_FUNCTION ( gzinflate  ) [static]

Definition at line 640 of file zlib.c.

{
       int data_len, status;
       unsigned int factor=1, maxfactor=16;
       long limit = 0;
       unsigned long plength=0, length;
       char *data, *s1=NULL, *s2=NULL;
       z_stream stream;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &limit) == FAILURE) {
              return;
       }

       if (!data_len) {
              RETURN_FALSE;
       }

       if (limit < 0) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "length (%ld) must be greater or equal zero", limit);
              RETURN_FALSE;
       }
       plength = limit;

       stream.zalloc = php_zlib_alloc;
       stream.zfree = php_zlib_free;
       stream.opaque = Z_NULL;
       stream.avail_in = data_len + 1; /* there is room for \0 */
       stream.next_in = (Bytef *) data;
       stream.total_out = 0;

       /* init with -MAX_WBITS disables the zlib internal headers */
       status = inflateInit2(&stream, -MAX_WBITS);
       if (status != Z_OK) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
              RETURN_FALSE;
       }

       /*
         stream.avail_out wants to know the output data length
         if none was given as a parameter
         we try from input length * 2 up to input length * 2^15
         doubling it whenever it wasn't big enough
         that should be enaugh for all real life cases
       */
       do {
              length = plength ? plength : (unsigned long)data_len * (1 << factor++);
              s2 = (char *) erealloc(s1, length);

              if (!s2) {
                     if (s1) {
                            efree(s1);
                     }
                     inflateEnd(&stream);
                     RETURN_FALSE;
              }
              s1 = s2;

              stream.next_out = (Bytef *) &s2[stream.total_out];
              stream.avail_out = length - stream.total_out;
              status = inflate(&stream, Z_NO_FLUSH);

       } while ((Z_BUF_ERROR == status || (Z_OK == status && stream.avail_in)) && !plength && factor < maxfactor);

       inflateEnd(&stream);

       if ((plength && Z_OK == status) || factor >= maxfactor) {
              status = Z_MEM_ERROR;
       }

       if (Z_STREAM_END == status || Z_OK == status) {
              s2 = erealloc(s2, stream.total_out + 1); /* room for \0 */
              s2[ stream.total_out ] = '\0';
              RETURN_STRINGL(s2, stream.total_out, 0);
       } else {
              efree(s2);
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

static PHP_FUNCTION ( gzencode  ) [static]

Definition at line 857 of file zlib.c.

{
       char *data, *s2;
       int data_len;
       long level = Z_DEFAULT_COMPRESSION, coding = CODING_GZIP;
       int status;
       z_stream stream;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &data, &data_len, &level, &coding) == FAILURE) {
              return;
       }

       if ((level < -1) || (level > 9)) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level(%ld) must be within -1..9", level);
              RETURN_FALSE;
       }

       if ((coding != CODING_GZIP) && (coding != CODING_DEFLATE)) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "encoding mode must be FORCE_GZIP or FORCE_DEFLATE");
              RETURN_FALSE;
       }

       stream.zalloc = php_zlib_alloc;
       stream.zfree = php_zlib_free;
       stream.opaque = Z_NULL;

       stream.next_in = (Bytef *) data;
       stream.avail_in = data_len;

       stream.avail_out = stream.avail_in + (stream.avail_in / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */
       s2 = (char *) emalloc(stream.avail_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0));

       /* add gzip file header */
       s2[0] = gz_magic[0];
       s2[1] = gz_magic[1];
       s2[2] = Z_DEFLATED;
       s2[3] = s2[4] = s2[5] = s2[6] = s2[7] = s2[8] = 0; /* time set to 0 */
       s2[9] = OS_CODE;

       stream.next_out = &(s2[GZIP_HEADER_LENGTH]);

       switch (coding) {
              case CODING_GZIP:
                     /* windowBits is passed < 0 to suppress zlib header & trailer */
                     if ((status = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) != Z_OK) {
                            php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
                            RETURN_FALSE;
                     }

                     break;
              case CODING_DEFLATE:
                     if ((status = deflateInit(&stream, level)) != Z_OK) {
                            php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
                            RETURN_FALSE;
                     }
                     break;
       }

       status = deflate(&stream, Z_FINISH);
       if (status != Z_STREAM_END) {
              deflateEnd(&stream);
              if (status == Z_OK) {
                     status = Z_BUF_ERROR;
              }
       } else {
              status = deflateEnd(&stream);
       }

       if (status == Z_OK) {
              /* resize to buffer to the "right" size */
              s2 = erealloc(s2, stream.total_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0) + 1);

              if (coding == CODING_GZIP) {
                     char *trailer = s2 + (stream.total_out + GZIP_HEADER_LENGTH);
                     uLong crc = crc32(0L, Z_NULL, 0);

                     crc = crc32(crc, (const Bytef *) data, data_len);

                     /* write crc & stream.total_in in LSB order */
                     trailer[0] = (char) crc & 0xFF;
                     trailer[1] = (char) (crc >> 8) & 0xFF;
                     trailer[2] = (char) (crc >> 16) & 0xFF;
                     trailer[3] = (char) (crc >> 24) & 0xFF;
                     trailer[4] = (char) stream.total_in & 0xFF;
                     trailer[5] = (char) (stream.total_in >> 8) & 0xFF;
                     trailer[6] = (char) (stream.total_in >> 16) & 0xFF;
                     trailer[7] = (char) (stream.total_in >> 24) & 0xFF;
                     trailer[8] = '\0';
              } else {
                     s2[stream.total_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0)] = '\0';
              }
              RETURN_STRINGL(s2, stream.total_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0), 0);
       } else {
              efree(s2);
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

static PHP_FUNCTION ( ob_gzhandler  ) [static]

Definition at line 987 of file zlib.c.

{
       char *string;
       int string_len;
       long mode;
       zval **a_encoding;
       zend_bool return_original = 0;
       zend_bool do_start, do_end;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &string, &string_len, &mode) == FAILURE) {
              return;
       }

       if (ZLIBG(ob_gzhandler_status) == -1) {
              RETURN_FALSE;
       }

       zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);

       if (!PG(http_globals)[TRACK_VARS_SERVER]
              || zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING"), (void **) &a_encoding) == FAILURE
       ) {
              ZLIBG(ob_gzhandler_status) = -1;
              RETURN_FALSE;
       }

       convert_to_string_ex(a_encoding);
       if (php_memnstr(Z_STRVAL_PP(a_encoding), "gzip", 4, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) {
              ZLIBG(compression_coding) = CODING_GZIP;
       } else if (php_memnstr(Z_STRVAL_PP(a_encoding), "deflate", 7, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) {
              ZLIBG(compression_coding) = CODING_DEFLATE;
       } else {
              ZLIBG(ob_gzhandler_status) = -1;
              RETURN_FALSE;
       }

       do_start = ((mode & PHP_OUTPUT_HANDLER_START) ? 1 : 0);
       do_end = ((mode & PHP_OUTPUT_HANDLER_END) ? 1 : 0);
       Z_STRVAL_P(return_value) = NULL;
       Z_STRLEN_P(return_value) = 0;

       if (php_deflate_string(string, string_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), do_start, do_end TSRMLS_CC) == SUCCESS) {
              Z_TYPE_P(return_value) = IS_STRING;
              if (do_start) {
                     switch (ZLIBG(compression_coding)) {
                            case CODING_GZIP:
                                   if (sapi_add_header("Content-Encoding: gzip", sizeof("Content-Encoding: gzip") - 1, 1) == FAILURE) {
                                          return_original = 1;
                                   }
                                   if (sapi_add_header_ex("Vary: Accept-Encoding", sizeof("Vary: Accept-Encoding") - 1, 1, 0 TSRMLS_CC)==FAILURE) {
                                          return_original = 1;
                                   }
                                   break;
                            case CODING_DEFLATE:
                                   if (sapi_add_header("Content-Encoding: deflate", sizeof("Content-Encoding: deflate") - 1, 1) == FAILURE) {
                                          return_original = 1;
                                   }
                                   if (sapi_add_header_ex("Vary: Accept-Encoding", sizeof("Vary: Accept-Encoding") - 1, 1, 0 TSRMLS_CC)==FAILURE) {
                                          return_original = 1;
                                   }
                                   break;
                            default:
                                   return_original = 1;
                                   break;
                     }
              }

              if (return_original) {
                     zval_dtor(return_value);
              }

       } else {
              return_original = 1;
       }

       if (return_original) {
              /* return the original string */
              RETURN_STRINGL(string, string_len, 1);
       }
}

Here is the call graph for this function:

static PHP_FUNCTION ( zlib_get_coding_type  ) [static]

Definition at line 723 of file zlib.c.

{
       switch (ZLIBG(compression_coding)) {
              case CODING_GZIP:
                     RETURN_STRINGL("gzip", sizeof("gzip") - 1, 1);

              case CODING_DEFLATE:
                     RETURN_STRINGL("deflate", sizeof("deflate") - 1, 1);
       }

       RETURN_FALSE;
}
static void php_gzip_output_handler ( char *  output,
uint  output_len,
char **  handled_output,
uint handled_output_len,
int mode  TSRMLS_DC 
) [static]

Definition at line 1071 of file zlib.c.

{
       zend_bool do_start, do_end;

       if (!ZLIBG(output_compression) || SG(sapi_headers).http_response_code == 204 || SG(sapi_headers).http_response_code == 304) {
              *handled_output = NULL;
       } else {
              do_start = (mode & PHP_OUTPUT_HANDLER_START ? 1 : 0);
              do_end = (mode & PHP_OUTPUT_HANDLER_END ? 1 : 0);

              if (do_start) {
                     if (!SG(headers_sent) && !SG(request_info).no_headers) {
                            switch (ZLIBG(compression_coding)) {
                                   case CODING_GZIP:
                                          sapi_add_header_ex(ZEND_STRL("Content-Encoding: gzip"), 1, 1 TSRMLS_CC);
                                          break;
                                   case CODING_DEFLATE:
                                          sapi_add_header_ex(ZEND_STRL("Content-Encoding: deflate"), 1, 1 TSRMLS_CC);
                                          break;
                            }
                            sapi_add_header_ex(ZEND_STRL("Vary: Accept-Encoding"), 1, 0 TSRMLS_CC);
                     } else {
                            /* Disable compression if headers can not be set (Fix for bug #49816) */
                            ZLIBG(output_compression) = 0;
                            *handled_output = NULL;
                            return;
                     }
              }

              if (php_deflate_string(output, output_len, handled_output, handled_output_len, do_start, do_end TSRMLS_CC) != SUCCESS) {
                     zend_error(E_ERROR, "Compression failed");
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PHP_INI_MH ( OnUpdate_zlib_output_compression  ) [static]

Definition at line 261 of file zlib.c.

{
       int status, int_value;
       char *ini_value;

       if (new_value == NULL) {
              return FAILURE;
       }

       if (!strncasecmp(new_value, "off", sizeof("off"))) {
              new_value = "0";
              new_value_length = sizeof("0");
       } else if (!strncasecmp(new_value, "on", sizeof("on"))) {
              new_value = "1";
              new_value_length = sizeof("1");
       }

       int_value = zend_atoi(new_value, new_value_length);
       ini_value = zend_ini_string("output_handler", sizeof("output_handler"), 0);

       if (ini_value && *ini_value && int_value) {
              php_error_docref("ref.outcontrol" TSRMLS_CC, E_CORE_ERROR, "Cannot use both zlib.output_compression and output_handler together!!");
              return FAILURE;
       }

       if (stage == PHP_INI_STAGE_RUNTIME && SG(headers_sent) && !SG(request_info).no_headers) {
              php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot change zlib.output_compression - headers already sent");
              return FAILURE;
       }

       status = OnUpdateLong(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);

       if (stage == PHP_INI_STAGE_RUNTIME && int_value) {
              status = php_zlib_output_compression_start(TSRMLS_C);
       }

       return status;
}

Here is the call graph for this function:

static PHP_INI_MH ( OnUpdate_zlib_output_compression_level  ) [static]

Definition at line 302 of file zlib.c.

{
       OnUpdateLong(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);

       return SUCCESS;
}
static PHP_INI_MH ( OnUpdate_zlib_output_handler  ) [static]

Definition at line 311 of file zlib.c.

{
       if (stage == PHP_INI_STAGE_RUNTIME && SG(headers_sent) && !SG(request_info).no_headers) {
              php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot change zlib.output_handler - headers already sent");
              return FAILURE;
       }

       OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);

       return SUCCESS;
}
static PHP_MINFO_FUNCTION ( zlib  ) [static]

Definition at line 375 of file zlib.c.

{
       php_info_print_table_start();
       php_info_print_table_row(2, "ZLib Support", "enabled");
       php_info_print_table_row(2, "Stream Wrapper support", "compress.zlib://");
       php_info_print_table_row(2, "Stream Filter support", "zlib.inflate, zlib.deflate");
       php_info_print_table_row(2, "Compiled Version", ZLIB_VERSION);
       php_info_print_table_row(2, "Linked Version", (char *) zlibVersion());
       php_info_print_table_end();

       DISPLAY_INI_ENTRIES();
}

Here is the call graph for this function:

static PHP_MINIT_FUNCTION ( zlib  ) [static]
static PHP_MSHUTDOWN_FUNCTION ( zlib  ) [static]

Definition at line 362 of file zlib.c.

Here is the call graph for this function:

Definition at line 959 of file zlib.c.

{
       /* check for wrong usages */
       if (OG(ob_nesting_level > 0)) {
              if (php_ob_handler_used("ob_gzhandler" TSRMLS_CC)) {
                     php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler 'ob_gzhandler' cannot be used twice");
                     return FAILURE;
              }
              if (php_ob_handler_used("mb_output_handler" TSRMLS_CC)) {
                     php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler 'ob_gzhandler' cannot be used after 'mb_output_handler'");
                     return FAILURE;
              }
              if (php_ob_handler_used("URL-Rewriter" TSRMLS_CC)) {
                     php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler 'ob_gzhandler' cannot be used after 'URL-Rewriter'");
                     return FAILURE;
              }
              if (php_ob_init_conflict("ob_gzhandler", "zlib output compression" TSRMLS_CC)) {
                     return FAILURE;
              }
       }

       return SUCCESS;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PHP_RINIT_FUNCTION ( zlib  ) [static]

Definition at line 349 of file zlib.c.

Here is the call graph for this function:

static voidpf php_zlib_alloc ( voidpf  opaque,
uInt  items,
uInt  size 
) [static]

Definition at line 249 of file zlib.c.

{
       return (voidpf)safe_emalloc(items, size, 0);
}

Here is the caller graph for this function:

static void php_zlib_free ( voidpf  opaque,
voidpf  address 
) [static]

Definition at line 254 of file zlib.c.

{
       efree((void*)address);
}

Here is the caller graph for this function:

Definition at line 1141 of file zlib.c.

{
       switch (ZLIBG(output_compression)) {
              case 0:
                     break;
              case 1:
                     ZLIBG(output_compression) = 4096;
                     /* break omitted intentionally */
              default:
                     /* ZLIBG(compression_coding) should be 0 when zlib compression hasn't been started yet.. */
                     if (ZLIBG(compression_coding) == 0) {
                            return php_enable_output_compression(ZLIBG(output_compression) TSRMLS_CC);
                     }
       }
       return SUCCESS;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const int gz_magic[2] = {0x1f, 0x8b} [static]

Definition at line 89 of file zlib.c.

Initial value:

Definition at line 194 of file zlib.c.

zend_module_entry php_zlib_module_entry
Initial value:

Definition at line 225 of file zlib.c.