cvs: php4 /ext/zlib/ php_zlib.h zlib.c

[email protected] ("Hartmut Holzgraefe")
Newsgroups php.version4
Message-ID <cvshholzgra959272550@cvsserver>
hholzgra		Thu May 25 18:35:50 2000 EDT

  Modified files:
    /php4/ext/zlib	php_zlib.h zlib.c 
  Log:
  added functions gzcompress() and gzuncompress() for direct string 
  compression as requested by BugId #1151
  
  
Index: php4/ext/zlib/php_zlib.h
diff -u php4/ext/zlib/php_zlib.h:1.4 php4/ext/zlib/php_zlib.h:1.5
--- php4/ext/zlib/php_zlib.h:1.4	Thu May 25 18:14:05 2000
+++ php4/ext/zlib/php_zlib.h	Thu May 25 18:35:49 2000
@@ -23,7 +23,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_zlib.h,v 1.4 2000/05/25 16:14:05 fmk Exp $ */
+/* $Id: php_zlib.h,v 1.5 2000/05/25 16:35:49 hholzgra Exp $ */
 
 #ifndef _PHP_ZLIB_H
 #define _PHP_ZLIB_H
@@ -59,6 +59,8 @@
 PHP_FUNCTION(gzpassthru);
 PHP_FUNCTION(readgzfile);
 PHP_FUNCTION(gzfile);
+PHP_FUNCTION(gzcompress);
+PHP_FUNCTION(gzuncompress);
 
 #ifdef ZTS
 #define ZLIBLS_D php_zlib_globals *zlib_globals
Index: php4/ext/zlib/zlib.c
diff -u php4/ext/zlib/zlib.c:1.43 php4/ext/zlib/zlib.c:1.44
--- php4/ext/zlib/zlib.c:1.43	Tue May 23 11:33:51 2000
+++ php4/ext/zlib/zlib.c	Thu May 25 18:35:49 2000
@@ -16,7 +16,7 @@
    |          Stefan Röhrich <[email protected]>                                |
    +----------------------------------------------------------------------+
  */
-/* $Id: zlib.c,v 1.43 2000/05/23 09:33:51 sas Exp $ */
+/* $Id: zlib.c,v 1.44 2000/05/25 16:35:49 hholzgra Exp $ */
 #define IS_EXT_MODULE
 
 #include "php.h"
@@ -95,6 +95,8 @@
 	PHP_FE(gzwrite,						NULL)
 	PHP_FALIAS(gzputs,		gzwrite,	NULL)
 	PHP_FE(gzfile,						NULL)
+	PHP_FE(gzcompress,                  NULL)
+	PHP_FE(gzuncompress,                NULL)
 	{NULL, NULL, NULL}
 };
 
@@ -709,6 +711,7 @@
 /* }}} */
 
 /* {{{ proto string gzread(int zp, int length)
+
    Binary-safe file read */
 PHP_FUNCTION(gzread)
 {
@@ -736,7 +739,115 @@
 	}
 	return_value->type = IS_STRING;
 }
+
 /* }}} */
+	
+
+/* {{{ proto string gzcompress(string data [,int level]) 
+   gzip-compress a string */
+PHP_FUNCTION(gzcompress)
+{
+	zval **data, **zlimit = NULL;
+	int limit,status;
+	unsigned long l2;
+	char *s2;
+
+	switch (ARG_COUNT(ht)) {
+	case 1:
+		if (zend_get_parameters_ex(1, &data) == FAILURE)
+			WRONG_PARAM_COUNT;
+		limit=-1;
+		break;
+	case 2:
+		if (zend_get_parameters_ex(2, &data, &zlimit) == FAILURE)
+			WRONG_PARAM_COUNT;
+		convert_to_long_ex(zlimit);
+		limit = (*zlimit)->value.lval;
+		if((limit<0)||(limit>9)) {
+			php_error(E_WARNING,"gzcompress: compression level must be whithin 0..9");
+			RETURN_FALSE;
+		}
+		break;
+	default:
+		WRONG_PARAM_COUNT;                                         
+	}
+	convert_to_string_ex(data);
+	
+	l2 = (*data)->value.str.len + ((*data)->value.str.len/1000) + 15;
+	s2 = (char *) emalloc(l2);
+	if(! s2) RETURN_FALSE;
+	
+	if(limit>=0) {
+		status = compress2(s2,&l2,(*data)->value.str.val, (*data)->value.str.len,limit);
+	} else {
+		status = compress(s2,&l2,(*data)->value.str.val, (*data)->value.str.len);
+	}
+	
+	if(status==Z_OK) {
+		RETURN_STRINGL(s2, l2, 0);
+	} else {
+		efree(s2);
+		php_error(E_WARNING,"gzcompress: %s",zError(status));
+		RETURN_FALSE;
+	}
+}
+/* }}} */
+
+/* {{{ proto string gzuncompress(string data ,int length) 
+   unzip a gzip-compressed string */
+PHP_FUNCTION(gzuncompress)
+{
+	zval **data, **zlimit = NULL;
+	int status,factor=1,maxfactor=8;
+	unsigned long plength=0,length;
+	char *s1=NULL,*s2=NULL;
+
+	switch (ARG_COUNT(ht)) {
+	case 1:
+		if (zend_get_parameters_ex(1, &data) == FAILURE)
+			WRONG_PARAM_COUNT;
+		length=0;
+		break;
+	case 2:
+		if (zend_get_parameters_ex(2, &data, &zlimit) == FAILURE)
+			WRONG_PARAM_COUNT;
+		convert_to_long_ex(zlimit);
+		if((*zlimit)->value.lval<=0) {
+			php_error(E_WARNING,"gzuncompress: length must be greater zero");
+			RETURN_FALSE;
+		}
+		plength = (*zlimit)->value.lval;
+		break;
+	default:
+		WRONG_PARAM_COUNT;                                         
+	}
+	convert_to_string_ex(data);
+	
+	// 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^8
+	// doubling it whenever it wasn't big enough
+	// that should be eneugh for all real life cases	
+	do {
+		length=plength?plength:(*data)->value.str.len*(1<<factor++);
+		s2 = (char *) erealloc(s1,length);
+		if(! s2) { if(s1) efree(s1); RETURN_FALSE; }
+		status = uncompress(s2, &length ,(*data)->value.str.val, (*data)->value.str.len);
+		s1=s2;
+	} while((status==Z_BUF_ERROR)&&(!plength)&&(factor<maxfactor));
+
+	if(status==Z_OK) {
+		s2 = erealloc(s2, length);
+		RETURN_STRINGL(s2, length, 0);
+	} else {
+		efree(s2);
+		php_error(E_WARNING,"gzuncompress: %s",zError(status));
+		RETURN_FALSE;
+	}
+}
+/* }}} */
+
+
 
 #endif /* HAVE_ZLIB */
 /*
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.