Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Created August 19, 2019 13:41
Show Gist options
  • Save krakjoe/549ec38161ecd1ad9b8f16f6cf18657e to your computer and use it in GitHub Desktop.
Save krakjoe/549ec38161ecd1ad9b8f16f6cf18657e to your computer and use it in GitHub Desktop.
php-xz php7
diff --git a/config.m4 b/config.m4
index 90bbe66..e3ffe7c 100644
--- a/config.m4
+++ b/config.m4
@@ -1,47 +1,10 @@
dnl $Id$
dnl config.m4 for extension xz
-dnl Comments in this file start with the string 'dnl'.
-dnl Remove where necessary. This file will not work
-dnl without editing.
-
-dnl If your extension references something external, use with:
-
PHP_ARG_WITH(xz, for xz support,
[ --with-xz Include xz support])
-dnl Otherwise use enable:
-
-dnl PHP_ARG_ENABLE(xz, whether to enable xz support,
-dnl Make sure that the comment is aligned:
-dnl [ --enable-xz Enable xz support])
-
if test "$PHP_XZ" != "no"; then
- dnl Write more examples of tests here...
-
- dnl # --with-xz -> check with-path
- dnl SEARCH_PATH="/usr/local /usr" # you might want to change this
- dnl SEARCH_FOR="/include/xz.h" # you most likely want to change this
- dnl if test -r $PHP_XZ/$SEARCH_FOR; then # path given as parameter
- dnl XZ_DIR=$PHP_XZ
- dnl else # search default path list
- dnl AC_MSG_CHECKING([for xz files in default path])
- dnl for i in $SEARCH_PATH ; do
- dnl if test -r $i/$SEARCH_FOR; then
- dnl XZ_DIR=$i
- dnl AC_MSG_RESULT(found in $i)
- dnl fi
- dnl done
- dnl fi
- dnl
- dnl if test -z "$XZ_DIR"; then
- dnl AC_MSG_RESULT([not found])
- dnl AC_MSG_ERROR([Please reinstall the xz distribution])
- dnl fi
-
- dnl # --with-xz -> add include path
- dnl PHP_ADD_INCLUDE($XZ_DIR/include)
-
dnl # --with-xz -> check for lib and symbol presence
LIBNAME=lzma # you may want to change this
LIBSYMBOL=lzma_stream_encoder # you most likely want to change this
@@ -57,5 +20,5 @@ if test "$PHP_XZ" != "no"; then
])
PHP_SUBST(XZ_SHARED_LIBADD)
- PHP_NEW_EXTENSION(xz, xz.c xz_fopen_wrapper.c, $ext_shared)
+ PHP_NEW_EXTENSION(xz, xz.c xz_fopen_wrapper.c, $ext_shared, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
fi
diff --git a/php_xz.h b/php_xz.h
index 126b67b..b8bd6d9 100644
--- a/php_xz.h
+++ b/php_xz.h
@@ -1,8 +1,8 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 5 |
+ | PHP Version 7 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2013 The PHP Group |
+ | Copyright (c) 2019 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
@@ -14,6 +14,7 @@
+----------------------------------------------------------------------+
| Author: Payden Sutherland <payden@paydensutherland.com> |
| Mostly looked at ext/zlib for guidance |
+ | Updated for PHP 7: krakjoe |
+----------------------------------------------------------------------+
*/
@@ -48,32 +49,7 @@ PHP_RSHUTDOWN_FUNCTION(xz);
PHP_MINFO_FUNCTION(xz);
PHP_FUNCTION(xzopen);
-php_stream *php_stream_xzopen(php_stream_wrapper *wrapper, char *path, char *mode_pass, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
-/*
- Declare any global variables you may need between the BEGIN
- and END macros here:
-
-ZEND_BEGIN_MODULE_GLOBALS(xz)
- long global_value;
- char *global_string;
-ZEND_END_MODULE_GLOBALS(xz)
-*/
-
-/* In every utility function you add that needs to use variables
- in php_xz_globals, call TSRMLS_FETCH(); after declaring other
- variables used by that function, or better yet, pass in TSRMLS_CC
- after the last function argument and declare your utility function
- with TSRMLS_DC after the last declared argument. Always refer to
- the globals in your function as XZ_G(variable). You are
- encouraged to rename these macros something shorter, see
- examples in any other php module directory.
-*/
-
-#ifdef ZTS
-#define XZ_G(v) TSRMG(xz_globals_id, zend_xz_globals *, v)
-#else
-#define XZ_G(v) (xz_globals.v)
-#endif
+php_stream *php_stream_xzopen(php_stream_wrapper *wrapper, const char *path, const char *mode_pass, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC);
#endif /* PHP_XZ_H */
diff --git a/xz.c b/xz.c
index 773ac00..727e44c 100644
--- a/xz.c
+++ b/xz.c
@@ -1,8 +1,8 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 5 |
+ | PHP Version 7 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2013 The PHP Group |
+ | Copyright (c) 2019 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
@@ -14,6 +14,7 @@
+----------------------------------------------------------------------+
| Author: Payden Sutherland <payden@paydensutherland.com> |
| Mostly looked at ext/zlib for guidance |
+ | Updated for PHP 7: krakjoe |
+----------------------------------------------------------------------+
*/
@@ -30,11 +31,6 @@
#include "php_xz.h"
#include <lzma.h>
-/* If you declare any globals in php_xz.h uncomment this:
-ZEND_DECLARE_MODULE_GLOBALS(xz)
-*/
-
-/* True global resources - no need for thread safety here */
static int le_xz;
@@ -85,46 +81,19 @@ zend_module_entry xz_module_entry = {
xz_functions,
PHP_MINIT(xz),
PHP_MSHUTDOWN(xz),
- PHP_RINIT(xz), /* Replace with NULL if there's nothing to do at request start */
- PHP_RSHUTDOWN(xz), /* Replace with NULL if there's nothing to do at request end */
+ PHP_RINIT(xz),
+ PHP_RSHUTDOWN(xz),
PHP_MINFO(xz),
-#if ZEND_MODULE_API_NO >= 20010901
- "0.1", /* Replace with version number for your extension */
-#endif
+ PHP_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
-#ifdef COMPILE_DL_XZ
-ZEND_GET_MODULE(xz)
-#endif
-
-/* {{{ PHP_INI
- */
-/* Remove comments and fill if you need to have entries in php.ini
-PHP_INI_BEGIN()
- STD_PHP_INI_ENTRY("xz.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_xz_globals, xz_globals)
- STD_PHP_INI_ENTRY("xz.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_xz_globals, xz_globals)
-PHP_INI_END()
-*/
-/* }}} */
-
-/* {{{ php_xz_init_globals
- */
-/* Uncomment this function if you have INI entries
-static void php_xz_init_globals(zend_xz_globals *xz_globals)
-{
- xz_globals->global_value = 0;
- xz_globals->global_string = NULL;
-}
-*/
-/* }}} */
-
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(xz)
{
- php_register_url_stream_wrapper("compress.lzma", &php_stream_xz_wrapper TSRMLS_CC);
+ php_register_url_stream_wrapper("compress.lzma", &php_stream_xz_wrapper);
return SUCCESS;
}
/* }}} */
@@ -133,16 +102,19 @@ PHP_MINIT_FUNCTION(xz)
*/
PHP_MSHUTDOWN_FUNCTION(xz)
{
- php_unregister_url_stream_wrapper("compress.lzma" TSRMLS_CC);
+ php_unregister_url_stream_wrapper("compress.lzma");
return SUCCESS;
}
/* }}} */
-/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(xz)
{
+#ifdef ZTS
+ ZEND_TSRMLS_CACHE_UPDATE();
+#endif
+
return SUCCESS;
}
/* }}} */
@@ -176,17 +148,18 @@ PHP_FUNCTION(xzopen)
{
char *filename;
char *mode, *mode_to_pass;
- int filename_len, mode_len;
+ zend_long filename_len, mode_len;
php_stream *stream;
- unsigned long compression_level = 6;
+ zend_ulong compression_level = 6;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &filename, &filename_len, &mode, &mode_len, &compression_level) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|l", &filename, &filename_len, &mode, &mode_len, &compression_level) == FAILURE) {
return;
}
+
mode_to_pass = (char *) emalloc(mode_len + 20); //20 is just some arbitrary padding
snprintf(mode_to_pass, mode_len + 20, "%s:%lu", mode, compression_level);
- stream = php_stream_xzopen(NULL, filename, mode_to_pass, 0, NULL, NULL STREAMS_CC TSRMLS_CC);
+ stream = php_stream_xzopen(NULL, filename, mode_to_pass, 0, NULL, NULL STREAMS_CC);
if (!stream) {
RETURN_FALSE;
@@ -196,6 +169,13 @@ PHP_FUNCTION(xzopen)
}
+#ifdef COMPILE_DL_XZ
+#ifdef ZTS
+ ZEND_TSRMLS_CACHE_DEFINE();
+#endif
+ZEND_GET_MODULE(xz)
+#endif
+
/*
* Local variables:
* tab-width: 4
diff --git a/xz_fopen_wrapper.c b/xz_fopen_wrapper.c
index 60f5416..d6d880c 100644
--- a/xz_fopen_wrapper.c
+++ b/xz_fopen_wrapper.c
@@ -1,8 +1,8 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 5 |
+ | PHP Version 7 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2013 The PHP Group |
+ | Copyright (c) 2019 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
@@ -14,11 +14,14 @@
+----------------------------------------------------------------------+
| Author: Payden Sutherland <payden@paydensutherland.com> |
| Mostly looked at ext/zlib for guidance |
+ | Updated for PHP 7: krakjoe |
+----------------------------------------------------------------------+
*/
+
#include "php.h"
#include "php_xz.h"
+#include "php_streams.h"
#include "fopen_wrappers.h"
#include <lzma.h>
@@ -35,7 +38,7 @@ struct php_xz_stream_data_t {
unsigned long level;
};
-static int php_xz_decompress(struct php_xz_stream_data_t *self TSRMLS_DC)
+static int php_xz_decompress(struct php_xz_stream_data_t *self)
{
lzma_stream *strm = &self->strm;
lzma_action action = LZMA_RUN;
@@ -54,7 +57,7 @@ static int php_xz_decompress(struct php_xz_stream_data_t *self TSRMLS_DC)
}
}
-static int php_xz_compress(struct php_xz_stream_data_t *self TSRMLS_DC)
+static int php_xz_compress(struct php_xz_stream_data_t *self)
{
//this function will attempt to consume all bytes in lzma_stream->next_in
// and write to underlying file
@@ -151,14 +154,16 @@ static int php_xz_init_encoder(struct php_xz_stream_data_t *self)
msg = "Unknown error, possibly a bug";
break;
}
- php_error_docref(NULL TSRMLS_CC, E_WARNING, msg);
+ php_error_docref(NULL, E_WARNING, msg);
*/
return 0;
}
-
-
-static size_t php_xziop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
+#if PHP_VERSION_ID >= 70400
+static ssize_t php_xziop_read(php_stream *stream, char *buf, size_t count)
+#else
+static size_t php_xziop_read(php_stream *stream, char *buf, size_t count)
+#endif
{
struct php_xz_stream_data_t *self = (struct php_xz_stream_data_t *) stream->abstract;
lzma_stream *strm = &self->strm;
@@ -189,13 +194,17 @@ static size_t php_xziop_read(php_stream *stream, char *buf, size_t count TSRMLS_
return have_read;
}
- php_xz_decompress(self TSRMLS_CC);
+ php_xz_decompress(self);
}
return have_read;
}
-static size_t php_xziop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
+#if PHP_VERSION_ID >= 70400
+static ssize_t php_xziop_write(php_stream *stream, const char *buf, size_t count)
+#else
+static size_t php_xziop_write(php_stream *stream, const char *buf, size_t count)
+#endif
{
struct php_xz_stream_data_t *self = (struct php_xz_stream_data_t *) stream->abstract;
int wrote = 0;
@@ -208,7 +217,7 @@ static size_t php_xziop_write(php_stream *stream, const char *buf, size_t count
memcpy((void *)self->in_buf + strm->avail_in, buf + wrote, self->in_buf_sz - strm->avail_in);
wrote += self->in_buf_sz - strm->avail_in;
strm->avail_in = self->in_buf_sz;
- bytes_consumed = php_xz_compress(self TSRMLS_CC);
+ bytes_consumed = php_xz_compress(self);
if (bytes_consumed < 0) {
break;
}
@@ -222,7 +231,7 @@ static size_t php_xziop_write(php_stream *stream, const char *buf, size_t count
return count;
}
-static int php_xziop_close(php_stream *stream, int close_handle TSRMLS_DC)
+static int php_xziop_close(php_stream *stream, int close_handle)
{
struct php_xz_stream_data_t *self = (struct php_xz_stream_data_t *) stream->abstract;
int ret = EOF;
@@ -261,28 +270,30 @@ static int php_xziop_close(php_stream *stream, int close_handle TSRMLS_DC)
return ret;
}
-static int php_xziop_flush(php_stream *stream TSRMLS_DC)
+static int php_xziop_flush(php_stream *stream)
{
struct php_xz_stream_data_t *self = (struct php_xz_stream_data_t *) stream->abstract;
if (strcmp(self->mode, "w") == 0 || strcmp(self->mode, "wb") == 0) {
- php_xz_compress(self TSRMLS_CC);
+ php_xz_compress(self);
}
php_stream_flush(self->stream);
return 0;
}
php_stream_ops php_stream_xzio_ops = {
- php_xziop_write, php_xziop_read,
- php_xziop_close, php_xziop_flush,
- "XZ",
- NULL, /* seek */
- NULL, /* cast */
- NULL, /* stat */
- NULL /* set_option */
+ .label = "XZ",
+ .write = php_xziop_write,
+ .read = php_xziop_read,
+ .close = php_xziop_close,
+ .flush = php_xziop_flush,
+ .seek = NULL,
+ .cast = NULL,
+ .stat = NULL,
+ .set_option = NULL
};
-php_stream *php_stream_xzopen(php_stream_wrapper *wrapper, char *path, char *mode_pass, int options,
- char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
+php_stream *php_stream_xzopen(php_stream_wrapper *wrapper, const char *path, const char *mode_pass, int options,
+ zend_string **opened_path, php_stream_context *context STREAMS_DC)
{
char mode[64];
unsigned long level = 6;
@@ -291,7 +302,7 @@ php_stream *php_stream_xzopen(php_stream_wrapper *wrapper, char *path, char *mod
strncpy(mode, mode_pass, 63);
//we allocated this in PHP_FUNCTION manually, free it.
- efree(mode_pass);
+ efree((char*)mode_pass);
//split compression level out of mode
char *colonp = strchr(mode, ':');
@@ -302,12 +313,12 @@ php_stream *php_stream_xzopen(php_stream_wrapper *wrapper, char *path, char *mod
if (strchr(mode, '+') || strcmp(mode, "rw") == 0) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "cannot open xz stream for reading and writing at the same time.");
+ php_error_docref(NULL, E_ERROR, "cannot open xz stream for reading and writing at the same time.");
return NULL;
}
if (level > 9 || level < 0) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Invalid compression level");
+ php_error_docref(NULL, E_ERROR, "Invalid compression level");
return NULL;
}
@@ -333,20 +344,20 @@ php_stream *php_stream_xzopen(php_stream_wrapper *wrapper, char *path, char *mod
if (strcmp(mode, "w") == 0 || strcmp(mode, "wb") == 0) {
if (!php_xz_init_encoder(self)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize xz encoder.");
+ php_error_docref(NULL, E_WARNING, "Could not initialize xz encoder.");
efree(self);
php_stream_close(stream);
return NULL;
}
} else if (strcmp(mode, "r") == 0 || strcmp(mode, "rb") == 0) {
if (!php_xz_init_decoder(self)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize xz decoder");
+ php_error_docref(NULL, E_WARNING, "Could not initialize xz decoder");
efree(self);
php_stream_close(stream);
return NULL;
}
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only open in read (r) or write (w) mode.");
+ php_error_docref(NULL, E_WARNING, "Can only open in read (r) or write (w) mode.");
efree(self);
php_stream_close(stream);
return NULL;
@@ -358,7 +369,7 @@ php_stream *php_stream_xzopen(php_stream_wrapper *wrapper, char *path, char *mod
efree(self);
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed opening xz stream");
+ php_error_docref(NULL, E_WARNING, "failed opening xz stream");
}
php_stream_close(innerstream);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment