├── README.md ├── CREDITS ├── lzf.stub.php ├── tests ├── 002.phpt ├── 003.phpt ├── 006.phpt ├── 004.phpt ├── 005.phpt ├── 001.phpt └── bug70727.phpt ├── REFLECTION ├── .gitignore ├── config.w32 ├── lzf_legacy_arginfo.h ├── lzf_arginfo.h ├── lib ├── README ├── LICENSE ├── lzf.h ├── lzfP.h ├── lzf_d.c └── lzf_c.c ├── examples └── lzf.php ├── php_lzf.h ├── config.m4 ├── LICENSE ├── lzf.dsp ├── lzf.c ├── filter.c └── package.xml /README.md: -------------------------------------------------------------------------------- 1 | Mirror of http://git.php.net/?p=pecl/file_formats/lzf.git 2 | 3 | Issues: https://bugs.php.net/search.php?cmd=display&status=Open&package_name[]=lzf 4 | -------------------------------------------------------------------------------- /CREDITS: -------------------------------------------------------------------------------- 1 | lzf extension 2 | Marcin Gibula, Remi Collet 3 | 4 | LZF compression library has been written by 5 | Marc Alexander Lehmann and is 6 | available for download from http://liblzf.plan9.de/ 7 | -------------------------------------------------------------------------------- /lzf.stub.php: -------------------------------------------------------------------------------- 1 | 5 | 6 | --POST-- 7 | --GET-- 8 | --INI-- 9 | --FILE-- 10 | 15 | --EXPECT-- 16 | BFRoaXMgIAIUdGVzdCBvZiBMWkYgZXh0ZW5zaW9u 17 | -------------------------------------------------------------------------------- /tests/003.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | lzf_decompress() 3 | --SKIPIF-- 4 | 5 | 6 | --POST-- 7 | --GET-- 8 | --INI-- 9 | --FILE-- 10 | 15 | --EXPECT-- 16 | This is test of LZF extension 17 | -------------------------------------------------------------------------------- /tests/006.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | lzf.compress on binary may result in larger string than input 3 | --SKIPIF-- 4 | 5 | 6 | --FILE-- 7 | 14 | Done 15 | --EXPECT-- 16 | Done 17 | -------------------------------------------------------------------------------- /REFLECTION: -------------------------------------------------------------------------------- 1 | Extension [ extension #15 lzf version 1.6.5 ] { 2 | 3 | - Functions { 4 | Function [ function lzf_compress ] { 5 | 6 | - Parameters [1] { 7 | Parameter #0 [ $string ] 8 | } 9 | } 10 | Function [ function lzf_decompress ] { 11 | 12 | - Parameters [1] { 13 | Parameter #0 [ $string ] 14 | } 15 | } 16 | Function [ function lzf_optimized_for ] { 17 | 18 | - Parameters [0] { 19 | } 20 | } 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /tests/004.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | filter lzf.compress 3 | --SKIPIF-- 4 | 5 | 6 | --POST-- 7 | --GET-- 8 | --INI-- 9 | --FILE-- 10 | 18 | --EXPECT-- 19 | WlYAAB1UaGlzIGlzIHRlc3Qgb2YgTFpGIGV4dGVuc2lvbg== 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .deps 2 | *.lo 3 | *.la 4 | .libs 5 | Makefile 6 | Makefile.fragments 7 | Makefile.global 8 | Makefile.objects 9 | acinclude.m4 10 | aclocal.m4 11 | autom4te.cache/ 12 | build/ 13 | config.guess 14 | config.h 15 | config.h.in 16 | config.log 17 | config.nice 18 | config.status 19 | config.sub 20 | configure 21 | configure.in 22 | configure.ac 23 | install-sh 24 | libtool 25 | ltmain.sh 26 | ltmain.sh.backup 27 | missing 28 | mkinstalldirs 29 | run-tests.php 30 | tmp-php.ini 31 | modules 32 | tests/*.diff 33 | tests/*.exp 34 | tests/*.log 35 | tests/*.out 36 | tests/*.php 37 | tests/*.sh 38 | LZF-*.tgz 39 | -------------------------------------------------------------------------------- /tests/005.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | filter lzf.decompress 3 | --SKIPIF-- 4 | 5 | 6 | --XFAIL-- 7 | Decompress filter is not implemented 8 | --POST-- 9 | --GET-- 10 | --INI-- 11 | --FILE-- 12 | 20 | --EXPECT-- 21 | This is test of LZF extension 22 | -------------------------------------------------------------------------------- /tests/001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Check for lzf presence 3 | --SKIPIF-- 4 | 5 | --POST-- 6 | --GET-- 7 | --INI-- 8 | --FILE-- 9 | 23 | --EXPECT-- 24 | lzf extension is available 25 | -------------------------------------------------------------------------------- /config.w32: -------------------------------------------------------------------------------- 1 | // $Id$ 2 | // vim:ft=javascript 3 | 4 | ARG_ENABLE("lzf", "LZF support", "no"); 5 | 6 | ARG_ENABLE("lzf-better-compression", "trade off speed for better compression ratio", "no"); 7 | 8 | if (PHP_LZF != "no") { 9 | 10 | if( CHECK_HEADER_ADD_INCLUDE("lzf.h", "CFLAGS_LZF", PHP_LZF + ";" + configure_module_dirname + "\\lib")) { 11 | EXTENSION('lzf', 'lzf.c filter.c'); 12 | ADD_SOURCES(configure_module_dirname + "/lib", "lzf_c.c lzf_d.c", "lzf"); 13 | 14 | AC_DEFINE('PHP_LZF_ULTRA_FAST', PHP_LZF_BETTER_COMPRESSION != "yes" ? 1 : 0); 15 | AC_DEFINE('HAVE_LZF', 1); 16 | } else { 17 | WARNING("lzf not enabled; libraries and headers not found"); 18 | } 19 | 20 | } 21 | 22 | -------------------------------------------------------------------------------- /lzf_legacy_arginfo.h: -------------------------------------------------------------------------------- 1 | /* This is a generated file, edit the .stub.php file instead. 2 | * Stub hash: cafc352823f0a71f7df7acaca7ac7dbedfd6bcad */ 3 | 4 | ZEND_BEGIN_ARG_INFO_EX(arginfo_lzf_compress, 0, 0, 1) 5 | ZEND_ARG_INFO(0, string) 6 | ZEND_END_ARG_INFO() 7 | 8 | #define arginfo_lzf_decompress arginfo_lzf_compress 9 | 10 | ZEND_BEGIN_ARG_INFO_EX(arginfo_lzf_optimized_for, 0, 0, 0) 11 | ZEND_END_ARG_INFO() 12 | 13 | 14 | ZEND_FUNCTION(lzf_compress); 15 | ZEND_FUNCTION(lzf_decompress); 16 | ZEND_FUNCTION(lzf_optimized_for); 17 | 18 | 19 | static const zend_function_entry ext_functions[] = { 20 | ZEND_FE(lzf_compress, arginfo_lzf_compress) 21 | ZEND_FE(lzf_decompress, arginfo_lzf_decompress) 22 | ZEND_FE(lzf_optimized_for, arginfo_lzf_optimized_for) 23 | ZEND_FE_END 24 | }; 25 | -------------------------------------------------------------------------------- /lzf_arginfo.h: -------------------------------------------------------------------------------- 1 | /* This is a generated file, edit the .stub.php file instead. 2 | * Stub hash: cafc352823f0a71f7df7acaca7ac7dbedfd6bcad */ 3 | 4 | ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_lzf_compress, 0, 1, MAY_BE_STRING|MAY_BE_FALSE) 5 | ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0) 6 | ZEND_END_ARG_INFO() 7 | 8 | #define arginfo_lzf_decompress arginfo_lzf_compress 9 | 10 | ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_lzf_optimized_for, 0, 0, MAY_BE_LONG|MAY_BE_FALSE) 11 | ZEND_END_ARG_INFO() 12 | 13 | 14 | ZEND_FUNCTION(lzf_compress); 15 | ZEND_FUNCTION(lzf_decompress); 16 | ZEND_FUNCTION(lzf_optimized_for); 17 | 18 | 19 | static const zend_function_entry ext_functions[] = { 20 | ZEND_FE(lzf_compress, arginfo_lzf_compress) 21 | ZEND_FE(lzf_decompress, arginfo_lzf_decompress) 22 | ZEND_FE(lzf_optimized_for, arginfo_lzf_optimized_for) 23 | ZEND_FE_END 24 | }; 25 | -------------------------------------------------------------------------------- /tests/bug70727.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | bug70727 zend_mm_heap corrupted when uncompressed size multiple of 1024 3 | --SKIPIF-- 4 | 5 | 6 | --FILE-- 7 | 20 | Done 21 | --EXPECT-- 22 | 716 23 | 2048 24 | Done 25 | 26 | -------------------------------------------------------------------------------- /lib/README: -------------------------------------------------------------------------------- 1 | DESCRIPTION 2 | LZF is an extremely fast (not that much slower than a pure memcpy) 3 | compression algorithm. It is ideal for applications where you want to 4 | save *some* space but not at the cost of speed. It is ideal for 5 | repetitive data as well. The module is self-contained and very small. 6 | 7 | It's written in ISO-C with no external dependencies other than what 8 | C provides and can easily be #include'd into your code, no makefile 9 | changes or library builds requires. 10 | 11 | A C♯ implementation without external dependencies is available, too. 12 | 13 | I do not know for certain whether any patents in any countries apply 14 | to this algorithm, but at the moment it is believed that it is free 15 | from any patents. More importantly, it is also free to use in every 16 | software package (see LICENSE). 17 | 18 | See the lzf.h file for details on how the functions in this 19 | mini-library are to be used. 20 | 21 | NOTE: This package contains a very bare-bones command-line utility 22 | which is neither optimized for speed nor for compression. This library 23 | is really intended to be used inside larger programs. 24 | 25 | AUTHOR 26 | This library was written by Marc Lehmann (See also 27 | http://software.schmorp.de/pkg/liblzf). 28 | 29 | 30 | -------------------------------------------------------------------------------- /examples/lzf.php: -------------------------------------------------------------------------------- 1 | 3 ? $_SERVER['argv'][3] : $in.'.lzf'); 12 | 13 | $data = file_get_contents($in); 14 | if (!$data) { 15 | die("Can't read $in\n"); 16 | } 17 | printf("Read %10d from %s\n", $old=strlen($data), $in); 18 | 19 | $time = microtime(true); 20 | $data = lzf_compress($data); 21 | $time = microtime(true) - $time; 22 | 23 | if ($len=file_put_contents($out, $data)) { 24 | printf("Write %10ld to %s\n", $len, $out); 25 | printf("Saved %d%% in %.4f\"\n", ($old-$len)*100/$old, $time); 26 | } 27 | 28 | } else { 29 | $in = $_SERVER['argv'][2]; 30 | if ($_SERVER['argc']>3) { 31 | $out = $_SERVER['argv'][3]; 32 | } else if (substr($in, -4)=='.lzf') { 33 | $out = substr($in, 0, -4); 34 | } else { 35 | die("Missing output file name\n"); 36 | } 37 | $data = file_get_contents($in); 38 | if (!$data) { 39 | die("Can't read $in\n"); 40 | } 41 | printf("Read %10d from %s\n", $old=strlen($data), $in); 42 | 43 | $time = microtime(true); 44 | $data = lzf_decompress($data); 45 | $time = microtime(true) - $time; 46 | 47 | if ($len=file_put_contents($out, $data)) { 48 | printf("Write %10ld to %s\n", $len, $out); 49 | printf("Done in %.4f\"\n", $time); 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /lib/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2000-2009 Marc Alexander Lehmann 2 | 3 | Redistribution and use in source and binary forms, with or without modifica- 4 | tion, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, 7 | this list of conditions and the following disclaimer. 8 | 9 | 2. Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 14 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 15 | CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 16 | EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 17 | CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 19 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 20 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 21 | ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 22 | OF THE POSSIBILITY OF SUCH DAMAGE. 23 | 24 | Alternatively, the following files carry an additional notice that 25 | explicitly allows relicensing under the GPLv2: lzf.c lzf.h lzfP.h lzf_c.c 26 | lzf_d.c 27 | 28 | -------------------------------------------------------------------------------- /php_lzf.h: -------------------------------------------------------------------------------- 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 | | http://www.php.net/license/3_01.txt. | 9 | | If you did not receive a copy of the PHP license and are unable to | 10 | | obtain it through the world-wide-web, please send a note to | 11 | | license@php.net so we can mail you a copy immediately. | 12 | +----------------------------------------------------------------------+ 13 | | Author: Marcin Gibula | 14 | +----------------------------------------------------------------------+ 15 | */ 16 | 17 | #ifndef PHP_LZF_H 18 | #define PHP_LZF_H 19 | 20 | extern zend_module_entry lzf_module_entry; 21 | #define phpext_lzf_ptr &lzf_module_entry 22 | 23 | extern php_stream_filter_factory php_lzf_compress_filter_factory; 24 | extern php_stream_filter_factory php_lzf_decompress_filter_factory; 25 | 26 | #define PHP_LZF_VERSION "1.7.1-dev" 27 | 28 | #define LZF_MARGIN 128 29 | 30 | PHP_MINFO_FUNCTION(lzf); 31 | PHP_MINIT_FUNCTION(lzf); 32 | PHP_MSHUTDOWN_FUNCTION(lzf); 33 | 34 | #endif /* PHP_LZF_H */ 35 | 36 | 37 | /* 38 | * Local variables: 39 | * tab-width: 4 40 | * c-basic-offset: 4 41 | * indent-tabs-mode: t 42 | * End: 43 | */ 44 | -------------------------------------------------------------------------------- /config.m4: -------------------------------------------------------------------------------- 1 | dnl $Id$ 2 | dnl config.m4 for extension lzf 3 | 4 | PHP_ARG_ENABLE(lzf, whether to enable lzf support, 5 | [ --enable-lzf Enable lzf support]) 6 | 7 | PHP_ARG_ENABLE(lzf-better-compression, whether to sacrifice speed in favour of compression ratio, 8 | [ --enable-lzf-better-compression Sacrifice speed in favour of compression ratio], no, no) 9 | 10 | PHP_ARG_WITH(liblzf, Use system liblzf, 11 | [ --with-liblzf[=DIR] Use system liblzf], no, no) 12 | 13 | if test "$PHP_LZF" != "no"; then 14 | if test "$PHP_LZF_BETTER_COMPRESSION" != "yes"; then 15 | AC_DEFINE(PHP_LZF_ULTRA_FAST,1,[ ]) 16 | else 17 | AC_DEFINE(PHP_LZF_ULTRA_FAST,0,[ ]) 18 | fi 19 | 20 | if test "$PHP_LIBLZF" != "no"; then 21 | AC_MSG_CHECKING(for liblzf) 22 | for i in $PHP_LIBLZF /usr/local /usr; do 23 | if test -r $i/include/lzf.h; then 24 | LIBLZF_CFLAGS="-I$i/include" 25 | LIBLZF_LIBDIR="$i/$PHP_LIBDIR" 26 | AC_MSG_RESULT(found in $i) 27 | break 28 | fi 29 | done 30 | if test -z "$LIBLZF_LIBDIR"; then 31 | AC_MSG_RESULT(not found) 32 | AC_MSG_ERROR(Please reinstall the liblzf distribution) 33 | fi 34 | PHP_CHECK_LIBRARY(lzf, lzf_compress, 35 | [ 36 | PHP_ADD_LIBRARY_WITH_PATH(lzf, $LIBLZF_LIBDIR, LZF_SHARED_LIBADD) 37 | AC_DEFINE(HAVE_LIBLZF,1,[ ]) 38 | ], [ 39 | AC_MSG_ERROR(could not find usable liblzf) 40 | ], [ 41 | -L$LIBLZF_LIBDIR 42 | ]) 43 | PHP_NEW_EXTENSION(lzf, lzf.c filter.c, $ext_shared) 44 | PHP_SUBST(LZF_SHARED_LIBADD) 45 | 46 | else 47 | PHP_NEW_EXTENSION(lzf, lzf.c lib/lzf_c.c lib/lzf_d.c filter.c, $ext_shared) 48 | PHP_ADD_BUILD_DIR($ext_builddir/lib, 1) 49 | PHP_ADD_INCLUDE([$ext_srcdir/lib]) 50 | fi 51 | fi 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------- 2 | The PHP License, version 3.01 3 | Copyright (c) 1999 - 2014 The PHP Group. All rights reserved. 4 | -------------------------------------------------------------------- 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, is permitted provided that the following conditions 8 | are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 13 | 2. Redistributions in binary form must reproduce the above copyright 14 | notice, this list of conditions and the following disclaimer in 15 | the documentation and/or other materials provided with the 16 | distribution. 17 | 18 | 3. The name "PHP" must not be used to endorse or promote products 19 | derived from this software without prior written permission. For 20 | written permission, please contact group@php.net. 21 | 22 | 4. Products derived from this software may not be called "PHP", nor 23 | may "PHP" appear in their name, without prior written permission 24 | from group@php.net. You may indicate that your software works in 25 | conjunction with PHP by saying "Foo for PHP" instead of calling 26 | it "PHP Foo" or "phpfoo" 27 | 28 | 5. The PHP Group may publish revised and/or new versions of the 29 | license from time to time. Each version will be given a 30 | distinguishing version number. 31 | Once covered code has been published under a particular version 32 | of the license, you may always continue to use it under the terms 33 | of that version. You may also choose to use such covered code 34 | under the terms of any subsequent version of the license 35 | published by the PHP Group. No one other than the PHP Group has 36 | the right to modify the terms applicable to covered code created 37 | under this License. 38 | 39 | 6. Redistributions of any form whatsoever must retain the following 40 | acknowledgment: 41 | "This product includes PHP software, freely available from 42 | ". 43 | 44 | THIS SOFTWARE IS PROVIDED BY THE PHP DEVELOPMENT TEAM ``AS IS'' AND 45 | ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 46 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 47 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PHP 48 | DEVELOPMENT TEAM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 49 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 50 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 51 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 53 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 54 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 55 | OF THE POSSIBILITY OF SUCH DAMAGE. 56 | 57 | -------------------------------------------------------------------- 58 | 59 | This software consists of voluntary contributions made by many 60 | individuals on behalf of the PHP Group. 61 | 62 | The PHP Group can be contacted via Email at group@php.net. 63 | 64 | For more information on the PHP Group and the PHP project, 65 | please see . 66 | 67 | PHP includes the Zend Engine, freely available at 68 | . 69 | -------------------------------------------------------------------------------- /lib/lzf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2008 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #ifndef LZF_H 38 | #define LZF_H 39 | 40 | /*********************************************************************** 41 | ** 42 | ** lzf -- an extremely fast/free compression/decompression-method 43 | ** http://liblzf.plan9.de/ 44 | ** 45 | ** This algorithm is believed to be patent-free. 46 | ** 47 | ***********************************************************************/ 48 | 49 | #define LZF_VERSION 0x0105 /* 1.5, API version */ 50 | 51 | /* 52 | * Compress in_len bytes stored at the memory block starting at 53 | * in_data and write the result to out_data, up to a maximum length 54 | * of out_len bytes. 55 | * 56 | * If the output buffer is not large enough or any error occurs return 0, 57 | * otherwise return the number of bytes used, which might be considerably 58 | * more than in_len (but less than 104% of the original size), so it 59 | * makes sense to always use out_len == in_len - 1), to ensure _some_ 60 | * compression, and store the data uncompressed otherwise (with a flag, of 61 | * course. 62 | * 63 | * lzf_compress might use different algorithms on different systems and 64 | * even different runs, thus might result in different compressed strings 65 | * depending on the phase of the moon or similar factors. However, all 66 | * these strings are architecture-independent and will result in the 67 | * original data when decompressed using lzf_decompress. 68 | * 69 | * The buffers must not be overlapping. 70 | * 71 | * If the option LZF_STATE_ARG is enabled, an extra argument must be 72 | * supplied which is not reflected in this header file. Refer to lzfP.h 73 | * and lzf_c.c. 74 | * 75 | */ 76 | unsigned int 77 | lzf_compress (const void *const in_data, unsigned int in_len, 78 | void *out_data, unsigned int out_len); 79 | 80 | /* 81 | * Decompress data compressed with some version of the lzf_compress 82 | * function and stored at location in_data and length in_len. The result 83 | * will be stored at out_data up to a maximum of out_len characters. 84 | * 85 | * If the output buffer is not large enough to hold the decompressed 86 | * data, a 0 is returned and errno is set to E2BIG. Otherwise the number 87 | * of decompressed bytes (i.e. the original length of the data) is 88 | * returned. 89 | * 90 | * If an error in the compressed data is detected, a zero is returned and 91 | * errno is set to EINVAL. 92 | * 93 | * This function is very fast, about as fast as a copying loop. 94 | */ 95 | unsigned int 96 | lzf_decompress (const void *const in_data, unsigned int in_len, 97 | void *out_data, unsigned int out_len); 98 | 99 | #endif 100 | 101 | -------------------------------------------------------------------------------- /lzf.dsp: -------------------------------------------------------------------------------- 1 | # Microsoft Developer Studio Project File - Name="lzf" - Package Owner=<4> 2 | # Microsoft Developer Studio Generated Build File, Format Version 6.00 3 | # ** DO NOT EDIT ** 4 | 5 | # TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102 6 | 7 | CFG=lzf - Win32 Release_TS 8 | !MESSAGE This is not a valid makefile. To build this project using NMAKE, 9 | !MESSAGE use the Export Makefile command and run 10 | !MESSAGE 11 | !MESSAGE NMAKE /f "lzf.mak". 12 | !MESSAGE 13 | !MESSAGE You can specify a configuration when running NMAKE 14 | !MESSAGE by defining the macro CFG on the command line. For example: 15 | !MESSAGE 16 | !MESSAGE NMAKE /f "lzf.mak" CFG="lzf - Win32 Release_TS" 17 | !MESSAGE 18 | !MESSAGE Possible choices for configuration are: 19 | !MESSAGE 20 | !MESSAGE "lzf - Win32 Release_TS" (based on "Win32 (x86) Dynamic-Link Library") 21 | !MESSAGE "lzf - Win32 Debug_TS" (based on "Win32 (x86) Dynamic-Link Library") 22 | !MESSAGE 23 | 24 | # Begin Project 25 | # PROP AllowPerConfigDependencies 0 26 | # PROP Scc_ProjName "" 27 | # PROP Scc_LocalPath "" 28 | CPP=cl.exe 29 | MTL=midl.exe 30 | RSC=rc.exe 31 | 32 | !IF "$(CFG)" == "lzf - Win32 Release_TS" 33 | 34 | # PROP BASE Use_MFC 0 35 | # PROP BASE Use_Debug_Libraries 0 36 | # PROP BASE Output_Dir "Release_TS" 37 | # PROP BASE Intermediate_Dir "Release_TS" 38 | # PROP BASE Ignore_Export_Lib 0 39 | # PROP BASE Target_Dir "" 40 | # PROP Use_MFC 0 41 | # PROP Use_Debug_Libraries 0 42 | # PROP Output_Dir "Release_TS" 43 | # PROP Intermediate_Dir "Release_TS" 44 | # PROP Ignore_Export_Lib 0 45 | # PROP Target_Dir "" 46 | # ADD BASE CPP /nologo /MT /W3 /GX /O2 /I "..\.." /I "..\..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\..\TSRM" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "COMPILE_DL_LZF" /D ZTS=1 /YX /FD /c 47 | # ADD CPP /nologo /MD /W3 /GX /O2 /I "..\.." /I "..\..\main" /I "..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\TSRM" /D ZEND_DEBUG=0 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LZF_EXPORTS" /D "COMPILE_DL_LZF" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_LZF=1 /D "LIBZEND_EXPORTS" /FR /YX /FD /c 48 | # ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 49 | # ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 50 | # ADD BASE RSC /l 0x406 /d "NDEBUG" 51 | # ADD RSC /l 0x406 /d "NDEBUG" 52 | BSC32=bscmake.exe 53 | # ADD BASE BSC32 /nologo 54 | # ADD BSC32 /nologo 55 | LINK32=link.exe 56 | # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php4ts.lib /nologo /dll /machine:I386 57 | # ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php4ts.lib /nologo /dll /machine:I386 /out:"..\..\Release_TS/php_lzf.dll" /libpath:"..\..\Release_TS" /libpath:"..\..\Release_TS_Inline" 58 | 59 | !ELSEIF "$(CFG)" == "lzf - Win32 Debug_TS" 60 | 61 | # PROP BASE Use_MFC 0 62 | # PROP BASE Use_Debug_Libraries 0 63 | # PROP BASE Output_Dir "Debug_TS" 64 | # PROP BASE Intermediate_Dir "Debug_TS" 65 | # PROP BASE Ignore_Export_Lib 0 66 | # PROP BASE Target_Dir "" 67 | # PROP Use_MFC 0 68 | # PROP Use_Debug_Libraries 0 69 | # PROP Output_Dir "Debug_TS" 70 | # PROP Intermediate_Dir "Debug_TS" 71 | # PROP Ignore_Export_Lib 0 72 | # PROP Target_Dir "" 73 | # ADD BASE CPP /nologo /MT /W3 /GX /O2 /I "..\.." /I "..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\TSRM" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "COMPILE_DL_LZF" /D ZTS=1 /YX /FD /c 74 | # ADD CPP /nologo /MDd /W3 /GX /O2 /I "..\.." /I "..\..\main" /I "..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\TSRM" /D ZEND_DEBUG=1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LZF_EXPORTS" /D "COMPILE_DL_LZF" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_LZF=1 /D "LIBZEND_EXPORTS" /YX /FD /c 75 | # ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 76 | # ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 77 | # ADD BASE RSC /l 0x406 /d "NDEBUG" 78 | # ADD RSC /l 0x406 /d "NDEBUG" 79 | BSC32=bscmake.exe 80 | # ADD BASE BSC32 /nologo 81 | # ADD BSC32 /nologo 82 | LINK32=link.exe 83 | # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php4ts.lib /nologo /dll /machine:I386 84 | # ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php4ts_debug.lib /nologo /dll /machine:I386 /out:"..\..\Debug_TS/php_lzf.dll" /libpath:"..\..\Debug_TS" 85 | 86 | !ENDIF 87 | 88 | # Begin Target 89 | 90 | # Name "lzf - Win32 Release_TS" 91 | # Name "lzf - Win32 Debug_TS" 92 | # Begin Group "Source Files" 93 | 94 | # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" 95 | # Begin Source File 96 | 97 | SOURCE=.\lzf.c 98 | # End Source File 99 | # Begin Source File 100 | 101 | SOURCE=.\lzf_c.c 102 | # End Source File 103 | # Begin Source File 104 | 105 | SOURCE=.\lzf_d.c 106 | # End Source File 107 | # End Group 108 | # Begin Group "Header Files" 109 | 110 | # PROP Default_Filter "h;hpp;hxx;hm;inl" 111 | # Begin Source File 112 | 113 | SOURCE=.\lzfP.h 114 | # End Source File 115 | # Begin Source File 116 | 117 | SOURCE=.\php_lzf.h 118 | # End Source File 119 | # Begin Source File 120 | 121 | SOURCE=.\php_lzf.h 122 | # End Source File 123 | # End Group 124 | # Begin Group "Resource Files" 125 | 126 | # PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" 127 | # End Group 128 | # End Target 129 | # End Project 130 | -------------------------------------------------------------------------------- /lzf.c: -------------------------------------------------------------------------------- 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 | | http://www.php.net/license/3_01.txt. | 9 | | If you did not receive a copy of the PHP license and are unable to | 10 | | obtain it through the world-wide-web, please send a note to | 11 | | license@php.net so we can mail you a copy immediately. | 12 | +----------------------------------------------------------------------+ 13 | | Author: Marcin Gibula | 14 | +----------------------------------------------------------------------+ 15 | */ 16 | 17 | #ifdef HAVE_CONFIG_H 18 | #include "config.h" 19 | #endif 20 | 21 | #include "php.h" 22 | #include "php_ini.h" 23 | #include "ext/standard/info.h" 24 | #include "php_lzf.h" 25 | 26 | #include "lzf.h" 27 | 28 | #ifndef PHP_LZF_ULTRA_FAST 29 | #define PHP_LZF_ULTRA_FAST 1 30 | #endif 31 | 32 | #if PHP_VERSION_ID < 80000 33 | #define RETURN_THROWS() return 34 | #include "lzf_legacy_arginfo.h" 35 | #else 36 | #include "lzf_arginfo.h" 37 | #endif 38 | 39 | /* {{{ lzf_module_entry 40 | */ 41 | zend_module_entry lzf_module_entry = { 42 | STANDARD_MODULE_HEADER, 43 | "lzf", 44 | ext_functions, 45 | PHP_MINIT(lzf), 46 | PHP_MSHUTDOWN(lzf), 47 | NULL, 48 | NULL, 49 | PHP_MINFO(lzf), 50 | PHP_LZF_VERSION, 51 | STANDARD_MODULE_PROPERTIES 52 | }; 53 | /* }}} */ 54 | 55 | #ifdef COMPILE_DL_LZF 56 | ZEND_GET_MODULE(lzf) 57 | #endif 58 | 59 | /* {{{ PHP_MINIT_FUNCTION 60 | */ 61 | PHP_MINIT_FUNCTION(lzf) 62 | { 63 | php_stream_filter_register_factory("lzf.compress", &php_lzf_compress_filter_factory); 64 | php_stream_filter_register_factory("lzf.decompress", &php_lzf_decompress_filter_factory); 65 | 66 | return SUCCESS; 67 | } 68 | /* }}} */ 69 | 70 | /* {{{ PHP_MSHUTDOWN_FUNCTION 71 | */ 72 | PHP_MSHUTDOWN_FUNCTION(lzf) 73 | { 74 | php_stream_filter_unregister_factory("lzf.compress"); 75 | php_stream_filter_unregister_factory("lzf.decompress"); 76 | 77 | return SUCCESS; 78 | } 79 | /* }}} */ 80 | 81 | /* {{{ PHP_MINFO_FUNCTION 82 | */ 83 | PHP_MINFO_FUNCTION(lzf) 84 | { 85 | php_info_print_table_start(); 86 | php_info_print_table_header(2, "lzf support", "enabled"); 87 | php_info_print_table_row(2, "extension version", PHP_LZF_VERSION); 88 | #ifdef HAVE_LIBLZF 89 | php_info_print_table_row(2, "liblzf version", "system"); 90 | #else 91 | php_info_print_table_row(2, "liblzf version", "bundled"); 92 | #if PHP_LZF_ULTRA_FAST 93 | php_info_print_table_row(2, "optimized for", "speed"); 94 | #else 95 | php_info_print_table_row(2, "optimized for", "compression quality"); 96 | #endif 97 | #endif 98 | php_info_print_table_end(); 99 | } 100 | /* }}} */ 101 | 102 | 103 | /* {{{ proto string lzf_compress(string arg) 104 | Return a string compressed with LZF */ 105 | PHP_FUNCTION(lzf_compress) 106 | { 107 | char *retval, *arg = NULL; 108 | size_t arg_len, out_len, result; 109 | 110 | 111 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) { 112 | RETURN_THROWS(); 113 | } 114 | 115 | if (arg_len > UINT_MAX) { /* LZF library do not support support large strings */ 116 | RETURN_FALSE; 117 | } 118 | 119 | out_len = arg_len + MIN(UINT_MAX - arg_len, MAX(LZF_MARGIN, arg_len / 25)); 120 | retval = emalloc(out_len); 121 | if (!retval) { 122 | RETURN_FALSE; 123 | } 124 | 125 | result = lzf_compress(arg, arg_len, retval, out_len); 126 | if (result == 0) { 127 | efree(retval); 128 | RETURN_FALSE; 129 | } 130 | 131 | retval[result] = 0; 132 | 133 | RETVAL_STRINGL(retval, result); 134 | efree(retval); 135 | } 136 | /* }}} */ 137 | 138 | /* {{{ proto string lzf_decompress(string arg) 139 | Return a string decompressed with LZF */ 140 | PHP_FUNCTION(lzf_decompress) 141 | { 142 | char *arg = NULL; 143 | size_t arg_len, result; 144 | char *buffer; 145 | size_t buffer_size = 0; 146 | 147 | if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) { 148 | RETURN_THROWS(); 149 | } 150 | 151 | do { 152 | if (buffer_size) { 153 | buffer = safe_erealloc(buffer, buffer_size, 2, 1); 154 | buffer_size *= 2; 155 | } else { 156 | buffer_size = (arg_len > 512 ? arg_len : 512); 157 | buffer = safe_emalloc(buffer_size, 2, 1); 158 | buffer_size *= 2; 159 | } 160 | 161 | result = lzf_decompress(arg, arg_len, buffer, buffer_size); 162 | } while (result == 0 && errno == E2BIG); 163 | 164 | if (result == 0) { 165 | if (errno == EINVAL) { 166 | php_error(E_WARNING, "%s LZF decompression failed, compressed data corrupted", get_active_function_name()); 167 | } 168 | 169 | efree(buffer); 170 | RETURN_FALSE; 171 | } 172 | 173 | buffer[result] = 0; 174 | 175 | RETVAL_STRINGL(buffer, result); 176 | efree(buffer); 177 | } 178 | /* }}} */ 179 | 180 | /* {{{ proto int lzf_optimized_for() 181 | Return 1 if lzf was optimized for speed, 0 for compression */ 182 | PHP_FUNCTION(lzf_optimized_for) 183 | { 184 | if (zend_parse_parameters_none() == FAILURE) { 185 | RETURN_THROWS(); 186 | } 187 | 188 | #ifdef HAVE_LIBLZF 189 | RETURN_FALSE; 190 | #else 191 | RETURN_LONG(PHP_LZF_ULTRA_FAST); 192 | #endif 193 | } 194 | 195 | /* }}} */ 196 | 197 | /* 198 | * Local variables: 199 | * tab-width: 4 200 | * c-basic-offset: 4 201 | * End: 202 | * vim600: noet sw=4 ts=4 fdm=marker 203 | * vim<600: noet sw=4 ts=4 204 | */ 205 | -------------------------------------------------------------------------------- /lib/lzfP.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2007 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #ifndef LZFP_h 38 | #define LZFP_h 39 | 40 | #define STANDALONE 1 /* at the moment, this is ok. */ 41 | 42 | #ifndef STANDALONE 43 | # include "lzf.h" 44 | #endif 45 | 46 | /* 47 | * Size of hashtable is (1 << HLOG) * sizeof (char *) 48 | * decompression is independent of the hash table size 49 | * the difference between 15 and 14 is very small 50 | * for small blocks (and 14 is usually a bit faster). 51 | * For a low-memory/faster configuration, use HLOG == 13; 52 | * For best compression, use 15 or 16 (or more, up to 22). 53 | */ 54 | #ifndef HLOG 55 | # define HLOG 16 56 | #endif 57 | 58 | /* 59 | * Sacrifice very little compression quality in favour of compression speed. 60 | * This gives almost the same compression as the default code, and is 61 | * (very roughly) 15% faster. This is the preferred mode of operation. 62 | */ 63 | #ifndef VERY_FAST 64 | # define VERY_FAST 1 65 | #endif 66 | 67 | /* 68 | * Sacrifice some more compression quality in favour of compression speed. 69 | * (roughly 1-2% worse compression for large blocks and 70 | * 9-10% for small, redundant, blocks and >>20% better speed in both cases) 71 | * In short: when in need for speed, enable this for binary data, 72 | * possibly disable this for text data. 73 | */ 74 | 75 | #ifdef HAVE_CONFIG_H 76 | #include "config.h" 77 | #else 78 | #define PHP_LZF_ULTRA_FAST 1 79 | #endif 80 | 81 | /* 82 | * Unconditionally aligning does not cost very much, so do it if unsure 83 | */ 84 | #ifndef STRICT_ALIGN 85 | # define STRICT_ALIGN !(defined(__i386) || defined (__amd64)) 86 | #endif 87 | 88 | /* 89 | * You may choose to pre-set the hash table (might be faster on some 90 | * modern cpus and large (>>64k) blocks, and also makes compression 91 | * deterministic/repeatable when the configuration otherwise is the same). 92 | */ 93 | #ifndef INIT_HTAB 94 | # define INIT_HTAB 0 95 | #endif 96 | 97 | /* 98 | * Avoid assigning values to errno variable? for some embedding purposes 99 | * (linux kernel for example), this is necessary. NOTE: this breaks 100 | * the documentation in lzf.h. Avoiding errno has no speed impact. 101 | */ 102 | #ifndef AVOID_ERRNO 103 | # define AVOID_ERRNO 0 104 | #endif 105 | 106 | /* 107 | * Whether to pass the LZF_STATE variable as argument, or allocate it 108 | * on the stack. For small-stack environments, define this to 1. 109 | * NOTE: this breaks the prototype in lzf.h. 110 | */ 111 | #ifndef LZF_STATE_ARG 112 | # define LZF_STATE_ARG 0 113 | #endif 114 | 115 | /* 116 | * Whether to add extra checks for input validity in lzf_decompress 117 | * and return EINVAL if the input stream has been corrupted. This 118 | * only shields against overflowing the input buffer and will not 119 | * detect most corrupted streams. 120 | * This check is not normally noticeable on modern hardware 121 | * (<1% slowdown), but might slow down older cpus considerably. 122 | */ 123 | #ifndef CHECK_INPUT 124 | # define CHECK_INPUT 1 125 | #endif 126 | 127 | /* 128 | * Whether to store pointers or offsets inside the hash table. On 129 | * 64 bit architetcures, pointers take up twice as much space, 130 | * and might also be slower. Default is to autodetect. 131 | */ 132 | /*#define LZF_USER_OFFSETS autodetect */ 133 | 134 | /*****************************************************************************/ 135 | /* nothing should be changed below */ 136 | 137 | #ifdef __cplusplus 138 | # include 139 | # include 140 | using namespace std; 141 | #else 142 | # include 143 | # include 144 | #endif 145 | 146 | #ifndef LZF_USE_OFFSETS 147 | # if defined (WIN32) 148 | # define LZF_USE_OFFSETS defined(_M_X64) 149 | # else 150 | # if __cplusplus > 199711L 151 | # include 152 | # else 153 | # include 154 | # endif 155 | # define LZF_USE_OFFSETS (UINTPTR_MAX > 0xffffffffU) 156 | # endif 157 | #endif 158 | 159 | typedef unsigned char u8; 160 | 161 | #if LZF_USE_OFFSETS 162 | # define LZF_HSLOT_BIAS ((const u8 *)in_data) 163 | typedef unsigned int LZF_HSLOT; 164 | #else 165 | # define LZF_HSLOT_BIAS 0 166 | typedef const u8 *LZF_HSLOT; 167 | #endif 168 | 169 | typedef LZF_HSLOT LZF_STATE[1 << (HLOG)]; 170 | 171 | #if !STRICT_ALIGN 172 | /* for unaligned accesses we need a 16 bit datatype. */ 173 | # if USHRT_MAX == 65535 174 | typedef unsigned short u16; 175 | # elif UINT_MAX == 65535 176 | typedef unsigned int u16; 177 | # else 178 | # undef STRICT_ALIGN 179 | # define STRICT_ALIGN 1 180 | # endif 181 | #endif 182 | 183 | #if PHP_LZF_ULTRA_FAST 184 | # if defined(VERY_FAST) 185 | # undef VERY_FAST 186 | # endif 187 | #endif 188 | 189 | #endif 190 | 191 | -------------------------------------------------------------------------------- /lib/lzf_d.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2010 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #include "lzfP.h" 38 | 39 | #if AVOID_ERRNO 40 | # define SET_ERRNO(n) 41 | #else 42 | # include 43 | # define SET_ERRNO(n) errno = (n) 44 | #endif 45 | 46 | #if USE_REP_MOVSB /* small win on amd, big loss on intel */ 47 | #if (__i386 || __amd64) && __GNUC__ >= 3 48 | # define lzf_movsb(dst, src, len) \ 49 | asm ("rep movsb" \ 50 | : "=D" (dst), "=S" (src), "=c" (len) \ 51 | : "0" (dst), "1" (src), "2" (len)); 52 | #endif 53 | #endif 54 | 55 | unsigned int 56 | lzf_decompress (const void *const in_data, unsigned int in_len, 57 | void *out_data, unsigned int out_len) 58 | { 59 | u8 const *ip = (const u8 *)in_data; 60 | u8 *op = (u8 *)out_data; 61 | u8 const *const in_end = ip + in_len; 62 | u8 *const out_end = op + out_len; 63 | 64 | do 65 | { 66 | unsigned int ctrl = *ip++; 67 | 68 | if (ctrl < (1 << 5)) /* literal run */ 69 | { 70 | ctrl++; 71 | 72 | if (op + ctrl > out_end) 73 | { 74 | SET_ERRNO (E2BIG); 75 | return 0; 76 | } 77 | 78 | #if CHECK_INPUT 79 | if (ip + ctrl > in_end) 80 | { 81 | SET_ERRNO (EINVAL); 82 | return 0; 83 | } 84 | #endif 85 | 86 | #ifdef lzf_movsb 87 | lzf_movsb (op, ip, ctrl); 88 | #else 89 | switch (ctrl) 90 | { 91 | case 32: *op++ = *ip++; case 31: *op++ = *ip++; case 30: *op++ = *ip++; case 29: *op++ = *ip++; 92 | case 28: *op++ = *ip++; case 27: *op++ = *ip++; case 26: *op++ = *ip++; case 25: *op++ = *ip++; 93 | case 24: *op++ = *ip++; case 23: *op++ = *ip++; case 22: *op++ = *ip++; case 21: *op++ = *ip++; 94 | case 20: *op++ = *ip++; case 19: *op++ = *ip++; case 18: *op++ = *ip++; case 17: *op++ = *ip++; 95 | case 16: *op++ = *ip++; case 15: *op++ = *ip++; case 14: *op++ = *ip++; case 13: *op++ = *ip++; 96 | case 12: *op++ = *ip++; case 11: *op++ = *ip++; case 10: *op++ = *ip++; case 9: *op++ = *ip++; 97 | case 8: *op++ = *ip++; case 7: *op++ = *ip++; case 6: *op++ = *ip++; case 5: *op++ = *ip++; 98 | case 4: *op++ = *ip++; case 3: *op++ = *ip++; case 2: *op++ = *ip++; case 1: *op++ = *ip++; 99 | } 100 | #endif 101 | } 102 | else /* back reference */ 103 | { 104 | unsigned int len = ctrl >> 5; 105 | 106 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; 107 | 108 | #if CHECK_INPUT 109 | if (ip >= in_end) 110 | { 111 | SET_ERRNO (EINVAL); 112 | return 0; 113 | } 114 | #endif 115 | if (len == 7) 116 | { 117 | len += *ip++; 118 | #if CHECK_INPUT 119 | if (ip >= in_end) 120 | { 121 | SET_ERRNO (EINVAL); 122 | return 0; 123 | } 124 | #endif 125 | } 126 | 127 | ref -= *ip++; 128 | 129 | if (op + len + 2 > out_end) 130 | { 131 | SET_ERRNO (E2BIG); 132 | return 0; 133 | } 134 | 135 | if (ref < (u8 *)out_data) 136 | { 137 | SET_ERRNO (EINVAL); 138 | return 0; 139 | } 140 | 141 | #ifdef lzf_movsb 142 | len += 2; 143 | lzf_movsb (op, ref, len); 144 | #else 145 | switch (len) 146 | { 147 | default: 148 | len += 2; 149 | 150 | if (op >= ref + len) 151 | { 152 | /* disjunct areas */ 153 | memcpy (op, ref, len); 154 | op += len; 155 | } 156 | else 157 | { 158 | /* overlapping, use octte by octte copying */ 159 | do 160 | *op++ = *ref++; 161 | while (--len); 162 | } 163 | 164 | break; 165 | 166 | case 9: *op++ = *ref++; 167 | case 8: *op++ = *ref++; 168 | case 7: *op++ = *ref++; 169 | case 6: *op++ = *ref++; 170 | case 5: *op++ = *ref++; 171 | case 4: *op++ = *ref++; 172 | case 3: *op++ = *ref++; 173 | case 2: *op++ = *ref++; 174 | case 1: *op++ = *ref++; 175 | case 0: *op++ = *ref++; /* two octets more */ 176 | *op++ = *ref++; 177 | } 178 | #endif 179 | } 180 | } 181 | while (ip < in_end); 182 | 183 | return op - (u8 *)out_data; 184 | } 185 | 186 | -------------------------------------------------------------------------------- /filter.c: -------------------------------------------------------------------------------- 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 | | http://www.php.net/license/3_01.txt. | 9 | | If you did not receive a copy of the PHP license and are unable to | 10 | | obtain it through the world-wide-web, please send a note to | 11 | | license@php.net so we can mail you a copy immediately. | 12 | +----------------------------------------------------------------------+ 13 | | Author: Marcin Gibula | 14 | +----------------------------------------------------------------------+ 15 | */ 16 | 17 | #include "php.h" 18 | #include "lzf.h" 19 | #include "php_lzf.h" 20 | 21 | #define LZF_BLOCKSIZE (1024 * 64 - 1) 22 | 23 | typedef struct { 24 | char signature[3]; 25 | char usize[2]; 26 | } lzf_header_uncompressed; 27 | 28 | typedef struct { 29 | char signature[3]; 30 | char csize[2]; 31 | char usize[2]; 32 | } lzf_header_compressed; 33 | 34 | typedef struct _php_lzf_compress_filter { 35 | int persistent; 36 | char *buffer; 37 | size_t buffer_pos; 38 | } php_lzf_filter_state; 39 | 40 | static void fill_header_compressed(lzf_header_compressed *header, size_t compressed, size_t uncompressed) 41 | { 42 | /* Copied from liblzf/lzf.c */ 43 | 44 | header->signature[0] = 'Z'; 45 | header->signature[1] = 'V'; 46 | header->signature[2] = 1; 47 | header->csize[0] = compressed >> 8; 48 | header->csize[1] = compressed & 0xff; 49 | header->usize[0] = uncompressed >> 8; 50 | header->usize[1] = uncompressed & 0xff; 51 | } 52 | 53 | static void fill_header_uncompressed(lzf_header_uncompressed *header, size_t uncompressed) 54 | { 55 | /* Copied from liblzf/lzf.c */ 56 | 57 | header->signature[0] = 'Z'; 58 | header->signature[1] = 'V'; 59 | header->signature[2] = 0; 60 | header->usize[0] = uncompressed >> 8; 61 | header->usize[1] = uncompressed & 0xff; 62 | } 63 | 64 | static int php_lzf_filter_state_ctor(php_lzf_filter_state *inst, int persistent) 65 | { 66 | inst->persistent = persistent; 67 | inst->buffer = pemalloc(LZF_BLOCKSIZE, persistent); 68 | inst->buffer_pos = 0; 69 | 70 | return SUCCESS; 71 | } 72 | 73 | static void php_lzf_filter_state_dtor(php_lzf_filter_state *inst) 74 | { 75 | pefree(inst->buffer, inst->persistent); 76 | } 77 | 78 | static int lzf_compress_filter_append_bucket( 79 | php_stream *stream, 80 | php_stream_filter_status_t *exit_status, 81 | php_lzf_filter_state *inst, 82 | php_stream_bucket_brigade *buckets_out, 83 | int persistent) 84 | { 85 | int status; 86 | size_t buffer_size; 87 | php_stream_bucket *new_bucket; 88 | char *output_buffer; 89 | 90 | /* Allocate buffer with a size of data and (larger) header */ 91 | output_buffer = pemalloc(inst->buffer_pos + sizeof(lzf_header_compressed), persistent); 92 | 93 | if (!output_buffer) 94 | goto fail; 95 | 96 | /* Try to compress data. */ 97 | status = lzf_compress(inst->buffer, inst->buffer_pos, output_buffer + sizeof(lzf_header_compressed), inst->buffer_pos); 98 | 99 | /* 100 | * If we were able to compress data, write compressed block. Otherwise 101 | * use uncompressed block. 102 | */ 103 | if (status > 0) { 104 | output_buffer = perealloc(output_buffer, status + sizeof(lzf_header_compressed), persistent); 105 | fill_header_compressed((lzf_header_compressed *) output_buffer, status, inst->buffer_pos); 106 | buffer_size = status + sizeof(lzf_header_compressed); 107 | } else { 108 | /* Pessimistic case - we still need to memcpy() data */ 109 | output_buffer = perealloc(output_buffer, inst->buffer_pos + sizeof(lzf_header_uncompressed), persistent); 110 | fill_header_uncompressed((lzf_header_uncompressed *) output_buffer, inst->buffer_pos); 111 | memcpy(output_buffer + sizeof(lzf_header_uncompressed), inst->buffer, inst->buffer_pos); 112 | buffer_size = inst->buffer_pos + sizeof(lzf_header_uncompressed); 113 | } 114 | 115 | /* Create new bucket and append it */ 116 | new_bucket = php_stream_bucket_new(stream, output_buffer, buffer_size, 1, 0); 117 | if (!new_bucket) 118 | goto fail_free_buffer; 119 | 120 | php_stream_bucket_append(buckets_out, new_bucket); 121 | 122 | /* Clear our buffer */ 123 | inst->buffer_pos = 0; 124 | 125 | /* Change exit status */ 126 | *exit_status = PSFS_PASS_ON; 127 | 128 | return SUCCESS; 129 | 130 | fail_free_buffer: 131 | pefree(output_buffer, persistent); 132 | fail: 133 | return FAILURE; 134 | } 135 | 136 | static int lzf_compress_append_data( 137 | php_stream *stream, 138 | php_stream_filter_status_t *exit_status, 139 | php_stream_bucket_brigade *buckets_out, 140 | php_lzf_filter_state *inst, 141 | const char *input_buffer, 142 | size_t input_buffer_len, 143 | size_t *consumed, 144 | int persistent) 145 | { 146 | size_t free_buffer; 147 | size_t bytes_to_copy; 148 | 149 | /* As long as there are data in the input buffer... */ 150 | while (input_buffer_len) { 151 | free_buffer = LZF_BLOCKSIZE - inst->buffer_pos; /* Free space in buffer */ 152 | bytes_to_copy = MIN(free_buffer, input_buffer_len); /* Bytes to copy into buffer */ 153 | 154 | /* ... copy as many bytes into buffer as possible */ 155 | memcpy(inst->buffer + inst->buffer_pos, input_buffer, bytes_to_copy); 156 | inst->buffer_pos += bytes_to_copy; 157 | input_buffer += bytes_to_copy; 158 | input_buffer_len -= bytes_to_copy; 159 | (*consumed) += bytes_to_copy; 160 | 161 | /* If the buffer is full, we need to flush it */ 162 | if (inst->buffer_pos == LZF_BLOCKSIZE) { 163 | if (lzf_compress_filter_append_bucket(stream, exit_status, inst, buckets_out, persistent) != SUCCESS) 164 | return FAILURE; 165 | } 166 | } 167 | 168 | return SUCCESS; 169 | } 170 | 171 | static php_stream_filter_status_t lzf_compress_filter( 172 | php_stream *stream, 173 | php_stream_filter *thisfilter, 174 | php_stream_bucket_brigade *buckets_in, 175 | php_stream_bucket_brigade *buckets_out, 176 | size_t *bytes_consumed, 177 | int flags) 178 | { 179 | size_t consumed = 0; 180 | php_lzf_filter_state *inst = (php_lzf_filter_state *) Z_PTR(thisfilter->abstract); 181 | php_stream_filter_status_t exit_status = PSFS_FEED_ME; 182 | php_stream_bucket *bucket = NULL; 183 | 184 | while (buckets_in->head) { 185 | bucket = buckets_in->head; 186 | 187 | php_stream_bucket_unlink(bucket); 188 | 189 | if (lzf_compress_append_data(stream, &exit_status, buckets_out, inst, bucket->buf, bucket->buflen, &consumed, 190 | php_stream_is_persistent(stream)) != SUCCESS) 191 | goto fail_free_bucket; 192 | 193 | php_stream_bucket_delref(bucket); 194 | } 195 | 196 | if (bytes_consumed) 197 | *bytes_consumed = consumed; 198 | 199 | if (flags & PSFS_FLAG_FLUSH_CLOSE) { 200 | if (lzf_compress_filter_append_bucket(stream, &exit_status, inst, buckets_out, php_stream_is_persistent(stream)) != SUCCESS) 201 | goto fail; 202 | } 203 | 204 | return exit_status; 205 | 206 | fail_free_bucket: 207 | if (bucket != NULL) 208 | php_stream_bucket_delref(bucket); 209 | fail: 210 | return PSFS_ERR_FATAL; 211 | } 212 | 213 | static php_stream_filter_status_t lzf_decompress_filter( 214 | php_stream *stream, 215 | php_stream_filter *thisfilter, 216 | php_stream_bucket_brigade *buckets_in, 217 | php_stream_bucket_brigade *buckets_out, 218 | size_t *bytes_consumed, 219 | int flags) 220 | { 221 | return PSFS_PASS_ON; 222 | } 223 | 224 | static void lzf_filter_state_dtor(php_stream_filter *thisfilter) 225 | { 226 | #if PHP_MAJOR_VERSION < 7 227 | assert(thisfilter->abstract != NULL); 228 | #else 229 | assert(Z_PTR(thisfilter->abstract) != NULL); 230 | #endif 231 | 232 | php_lzf_filter_state_dtor((php_lzf_filter_state *) Z_PTR(thisfilter->abstract)); 233 | pefree(Z_PTR(thisfilter->abstract), ((php_lzf_filter_state *) Z_PTR(thisfilter->abstract))->persistent); 234 | } 235 | 236 | static php_stream_filter_ops lzf_compress_ops = { 237 | lzf_compress_filter, 238 | lzf_filter_state_dtor, 239 | "lzf.compress" 240 | }; 241 | 242 | static php_stream_filter_ops lzf_decompress_ops = { 243 | lzf_decompress_filter, 244 | lzf_filter_state_dtor, 245 | "lzf.decompress" 246 | }; 247 | 248 | static php_stream_filter *lzf_compress_filter_create(const char *filtername, zval *filterparams, uint8_t persistent) 249 | { 250 | php_lzf_filter_state *inst; 251 | 252 | inst = pemalloc(sizeof(php_lzf_filter_state), persistent); 253 | if (inst == NULL) 254 | return NULL; 255 | 256 | if (php_lzf_filter_state_ctor(inst, persistent) != SUCCESS) { 257 | pefree(inst, persistent); 258 | return NULL; 259 | } 260 | 261 | return php_stream_filter_alloc(&lzf_compress_ops, inst, persistent); 262 | } 263 | 264 | static php_stream_filter *lzf_decompress_filter_create(const char *filtername, zval *filterparams, uint8_t persistent) 265 | { 266 | php_lzf_filter_state *inst; 267 | 268 | inst = pemalloc(sizeof(php_lzf_filter_state), persistent); 269 | if (inst == NULL) 270 | return NULL; 271 | 272 | if (php_lzf_filter_state_ctor(inst, persistent) != SUCCESS) { 273 | pefree(inst, persistent); 274 | return NULL; 275 | } 276 | 277 | return php_stream_filter_alloc(&lzf_decompress_ops, inst, persistent); 278 | } 279 | 280 | php_stream_filter_factory php_lzf_compress_filter_factory = { 281 | lzf_compress_filter_create 282 | }; 283 | 284 | php_stream_filter_factory php_lzf_decompress_filter_factory = { 285 | lzf_decompress_filter_create 286 | }; 287 | -------------------------------------------------------------------------------- /lib/lzf_c.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2010 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #include "lzfP.h" 38 | 39 | #define HSIZE (1 << (HLOG)) 40 | 41 | /* 42 | * don't play with this unless you benchmark! 43 | * the data format is not dependent on the hash function. 44 | * the hash function might seem strange, just believe me, 45 | * it works ;) 46 | */ 47 | #ifndef FRST 48 | # define FRST(p) (((p[0]) << 8) | p[1]) 49 | # define NEXT(v,p) (((v) << 8) | p[2]) 50 | # if PHP_LZF_ULTRA_FAST 51 | # define IDX(h) ((( h >> (3*8 - HLOG)) - h ) & (HSIZE - 1)) 52 | # elif VERY_FAST 53 | # define IDX(h) ((( h >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) 54 | # else 55 | # define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) 56 | # endif 57 | #endif 58 | /* 59 | * IDX works because it is very similar to a multiplicative hash, e.g. 60 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) 61 | * the latter is also quite fast on newer CPUs, and compresses similarly. 62 | * 63 | * the next one is also quite good, albeit slow ;) 64 | * (int)(cos(h & 0xffffff) * 1e6) 65 | */ 66 | 67 | #if 0 68 | /* original lzv-like hash function, much worse and thus slower */ 69 | # define FRST(p) (p[0] << 5) ^ p[1] 70 | # define NEXT(v,p) ((v) << 5) ^ p[2] 71 | # define IDX(h) ((h) & (HSIZE - 1)) 72 | #endif 73 | 74 | #define MAX_LIT (1 << 5) 75 | #define MAX_OFF (1 << 13) 76 | #define MAX_REF ((1 << 8) + (1 << 3)) 77 | 78 | #if __GNUC__ >= 3 79 | # define expect(expr,value) __builtin_expect ((expr),(value)) 80 | # define inline inline 81 | #else 82 | # define expect(expr,value) (expr) 83 | # define inline static 84 | #endif 85 | 86 | #define expect_false(expr) expect ((expr) != 0, 0) 87 | #define expect_true(expr) expect ((expr) != 0, 1) 88 | 89 | /* 90 | * compressed format 91 | * 92 | * 000LLLLL ; literal, L+1=1..33 octets 93 | * LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset 94 | * 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset 95 | * 96 | */ 97 | 98 | unsigned int 99 | lzf_compress (const void *const in_data, unsigned int in_len, 100 | void *out_data, unsigned int out_len 101 | #if LZF_STATE_ARG 102 | , LZF_STATE htab 103 | #endif 104 | ) 105 | { 106 | #if !LZF_STATE_ARG 107 | LZF_STATE htab; 108 | #endif 109 | const u8 *ip = (const u8 *)in_data; 110 | u8 *op = (u8 *)out_data; 111 | const u8 *in_end = ip + in_len; 112 | u8 *out_end = op + out_len; 113 | const u8 *ref; 114 | 115 | /* off requires a type wide enough to hold a general pointer difference. 116 | * ISO C doesn't have that (size_t might not be enough and ptrdiff_t only 117 | * works for differences within a single object). We also assume that no 118 | * no bit pattern traps. Since the only platform that is both non-POSIX 119 | * and fails to support both assumptions is windows 64 bit, we make a 120 | * special workaround for it. 121 | */ 122 | #if defined (WIN32) && defined (_M_X64) 123 | unsigned _int64 off; /* workaround for missing POSIX compliance */ 124 | #else 125 | unsigned long off; 126 | #endif 127 | unsigned int hval; 128 | int lit; 129 | 130 | if (!in_len || !out_len) 131 | return 0; 132 | 133 | #if INIT_HTAB 134 | memset (htab, 0, sizeof (htab)); 135 | #endif 136 | 137 | lit = 0; op++; /* start run */ 138 | 139 | hval = FRST (ip); 140 | while (ip < in_end - 2) 141 | { 142 | LZF_HSLOT *hslot; 143 | 144 | hval = NEXT (hval, ip); 145 | hslot = htab + IDX (hval); 146 | ref = *hslot + LZF_HSLOT_BIAS; *hslot = ip - LZF_HSLOT_BIAS; 147 | 148 | if (1 149 | #if INIT_HTAB 150 | && ref < ip /* the next test will actually take care of this, but this is faster */ 151 | #endif 152 | && (off = ip - ref - 1) < MAX_OFF 153 | && ref > (u8 *)in_data 154 | && ref[2] == ip[2] 155 | #if STRICT_ALIGN 156 | && ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0]) 157 | #else 158 | && *(u16 *)ref == *(u16 *)ip 159 | #endif 160 | ) 161 | { 162 | /* match found at *ref++ */ 163 | unsigned int len = 2; 164 | unsigned int maxlen = in_end - ip - len; 165 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; 166 | 167 | if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */ 168 | if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */ 169 | return 0; 170 | 171 | op [- lit - 1] = lit - 1; /* stop run */ 172 | op -= !lit; /* undo run if length is zero */ 173 | 174 | for (;;) 175 | { 176 | if (expect_true (maxlen > 16)) 177 | { 178 | len++; if (ref [len] != ip [len]) break; 179 | len++; if (ref [len] != ip [len]) break; 180 | len++; if (ref [len] != ip [len]) break; 181 | len++; if (ref [len] != ip [len]) break; 182 | 183 | len++; if (ref [len] != ip [len]) break; 184 | len++; if (ref [len] != ip [len]) break; 185 | len++; if (ref [len] != ip [len]) break; 186 | len++; if (ref [len] != ip [len]) break; 187 | 188 | len++; if (ref [len] != ip [len]) break; 189 | len++; if (ref [len] != ip [len]) break; 190 | len++; if (ref [len] != ip [len]) break; 191 | len++; if (ref [len] != ip [len]) break; 192 | 193 | len++; if (ref [len] != ip [len]) break; 194 | len++; if (ref [len] != ip [len]) break; 195 | len++; if (ref [len] != ip [len]) break; 196 | len++; if (ref [len] != ip [len]) break; 197 | } 198 | 199 | do 200 | len++; 201 | while (len < maxlen && ref[len] == ip[len]); 202 | 203 | break; 204 | } 205 | 206 | len -= 2; /* len is now #octets - 1 */ 207 | ip++; 208 | 209 | if (len < 7) 210 | { 211 | *op++ = (off >> 8) + (len << 5); 212 | } 213 | else 214 | { 215 | *op++ = (off >> 8) + ( 7 << 5); 216 | *op++ = len - 7; 217 | } 218 | 219 | *op++ = off; 220 | 221 | lit = 0; op++; /* start run */ 222 | 223 | ip += len + 1; 224 | 225 | if (expect_false (ip >= in_end - 2)) 226 | break; 227 | 228 | #if PHP_LZF_ULTRA_FAST || VERY_FAST 229 | --ip; 230 | # if VERY_FAST && !PHP_LZF_ULTRA_FAST 231 | --ip; 232 | # endif 233 | hval = FRST (ip); 234 | 235 | hval = NEXT (hval, ip); 236 | htab[IDX (hval)] = ip - LZF_HSLOT_BIAS; 237 | ip++; 238 | 239 | # if VERY_FAST && !PHP_LZF_ULTRA_FAST 240 | hval = NEXT (hval, ip); 241 | htab[IDX (hval)] = ip - LZF_HSLOT_BIAS; 242 | ip++; 243 | # endif 244 | #else 245 | ip -= len + 1; 246 | 247 | do 248 | { 249 | hval = NEXT (hval, ip); 250 | htab[IDX (hval)] = ip - LZF_HSLOT_BIAS; 251 | ip++; 252 | } 253 | while (len--); 254 | #endif 255 | } 256 | else 257 | { 258 | /* one more literal byte we must copy */ 259 | if (expect_false (op >= out_end)) 260 | return 0; 261 | 262 | lit++; *op++ = *ip++; 263 | 264 | if (expect_false (lit == MAX_LIT)) 265 | { 266 | op [- lit - 1] = lit - 1; /* stop run */ 267 | lit = 0; op++; /* start run */ 268 | } 269 | } 270 | } 271 | 272 | if (op + 3 > out_end) /* at most 3 bytes can be missing here */ 273 | return 0; 274 | 275 | while (ip < in_end) 276 | { 277 | lit++; *op++ = *ip++; 278 | 279 | if (expect_false (lit == MAX_LIT)) 280 | { 281 | op [- lit - 1] = lit - 1; /* stop run */ 282 | lit = 0; op++; /* start run */ 283 | } 284 | } 285 | 286 | op [- lit - 1] = lit - 1; /* end run */ 287 | op -= !lit; /* undo run if length is zero */ 288 | 289 | return op - (u8 *)out_data; 290 | } 291 | 292 | -------------------------------------------------------------------------------- /package.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | LZF 7 | pecl.php.net 8 | LZF compression. 9 | This package handles LZF de/compression. 10 | 11 | 12 | Remi Collet 13 | remi 14 | remi@php.net 15 | yes 16 | 17 | 18 | Marcin Gibula 19 | mg 20 | mg@iceni.pl 21 | yes 22 | 23 | 2022-01-04 24 | 25 | 1.7.1dev 26 | 1.7.0 27 | 28 | 29 | stable 30 | stable 31 | 32 | PHP License 33 | 34 | - 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 7.2.0 75 | 76 | 77 | 1.10 78 | 79 | 80 | 81 | LZF 82 | 83 | 84 | 85 | 86 | 87 | 2022-01-04 88 | 89 | 1.7.0 90 | 1.7.0 91 | 92 | 93 | stable 94 | stable 95 | 96 | PHP License 97 | 98 | - drop support for PHP 7.1 and older 99 | - add type hinting 100 | 101 | 102 | 103 | 2020-04-16 104 | 105 | 1.6.8 106 | 1.5.0 107 | 108 | 109 | stable 110 | stable 111 | 112 | PHP License 113 | 114 | - fix filter prototype for 7.2+ 115 | 116 | 117 | 118 | 2018-12-20 119 | 120 | 1.6.7 121 | 1.5.0 122 | 123 | 124 | stable 125 | stable 126 | 127 | PHP License 128 | 129 | - increase compression buffer up to 104% to avoid failure when output is larger than input 130 | - don't try to compress very large input (>4GB) 131 | 132 | 133 | 134 | 2017-05-29 135 | 136 | 1.6.6 137 | 1.5.0 138 | 139 | 140 | stable 141 | stable 142 | 143 | PHP License 144 | 145 | - update bundled liblzf to 3.6 146 | 147 | 148 | 149 | 2016-04-03 150 | 151 | 1.6.5 152 | 1.5.0 153 | 154 | 155 | stable 156 | stable 157 | 158 | PHP License 159 | 160 | - optimize lzf_decompress on large input (~20%) 161 | - use safe allocation to avoid integer overflow 162 | 163 | 164 | 165 | 2016-04-02 166 | 167 | 1.6.4 168 | 1.5.0 169 | 170 | 171 | stable 172 | stable 173 | 174 | PHP License 175 | 176 | - Fixed bug #70727 zend_mm_heap corrupted when uncompressed size multiple of 1024 177 | 178 | 179 | 180 | 2015-04-20 181 | 182 | 1.6.3 183 | 1.5.0 184 | 185 | 186 | stable 187 | stable 188 | 189 | PHP License 190 | 191 | - PHP 7 compatibility 192 | - move liblzf sources to /lib directory 193 | - add --with-liblzf option to use system liblzf 194 | - add LICENSE file in documentation 195 | - adapt Windows build (Anatol) 196 | - add arginfo for reflection 197 | 198 | 199 | 200 | 2012-07-08 201 | 202 | 1.6.2 203 | 1.5.0 204 | 205 | 206 | stable 207 | stable 208 | 209 | PHP License 210 | Fixed package.xml 211 | 212 | 213 | 214 | 215 | 1.6.1 216 | 1.5.0 217 | 218 | 219 | stable 220 | stable 221 | 222 | 2012-07-06 223 | PHP License 224 | Fixed build on PHP 5.3+ 225 | 226 | 227 | 228 | 229 | 1.5.2 230 | 1.5.0 231 | 232 | 233 | stable 234 | stable 235 | 236 | 2009-02-22 237 | PHP License 238 | Fixed segfault in phpinfo() 239 | 240 | 241 | 242 | 243 | 1.5.1 244 | 1.5.0 245 | 246 | 247 | stable 248 | stable 249 | 250 | 2009-02-22 251 | PHP License 252 | Updated bundled library to the newest version (3.4). 253 | 254 | 255 | 256 | 1.5.0 257 | 1.5.0 258 | 259 | 260 | stable 261 | stable 262 | 263 | 2008-07-01 264 | PHP License 265 | Updated bundled library to the newest version (3.2). 266 | 267 | 268 | 269 | 1.4 270 | 1.4 271 | 272 | 273 | stable 274 | stable 275 | 276 | 2006-07-06 277 | PHP License 278 | Bugfix release. 279 | 280 | 281 | 282 | 1.3 283 | 1.3 284 | 285 | 286 | stable 287 | stable 288 | 289 | 2004-08-14 290 | PHP License 291 | Upgraded bundled liblzf to the latest version. 292 | 293 | 294 | 295 | 1.2 296 | 1.2 297 | 298 | 299 | stable 300 | stable 301 | 302 | 2004-01-07 303 | PHP License 304 | Added test files and improved PEAR installer support. 305 | 306 | 307 | 308 | 1.1 309 | 1.1 310 | 311 | 312 | stable 313 | stable 314 | 315 | 2003-10-25 316 | PHP License 317 | Broken win32 build fixed. 318 | 319 | 320 | 321 | 1.0 322 | 1.0 323 | 324 | 325 | stable 326 | stable 327 | 328 | 2003-10-24 329 | PHP License 330 | This release adds win32 support. 331 | 332 | 333 | 334 | 0.1 335 | 0.1 336 | 337 | 338 | stable 339 | stable 340 | 341 | 2003-10-14 342 | PHP License 343 | This is the first release of LZF extension. 344 | 345 | 346 | 347 | --------------------------------------------------------------------------------