Skip to content

Commit 0e94c7a

Browse files
committed
Initial commit of xz PHP extension
0 parents  commit 0e94c7a

File tree

5 files changed

+613
-0
lines changed

5 files changed

+613
-0
lines changed

config.m4

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
dnl $Id$
2+
dnl config.m4 for extension xz
3+
4+
dnl Comments in this file start with the string 'dnl'.
5+
dnl Remove where necessary. This file will not work
6+
dnl without editing.
7+
8+
dnl If your extension references something external, use with:
9+
10+
PHP_ARG_WITH(xz, for xz support,
11+
[ --with-xz Include xz support])
12+
13+
dnl Otherwise use enable:
14+
15+
dnl PHP_ARG_ENABLE(xz, whether to enable xz support,
16+
dnl Make sure that the comment is aligned:
17+
dnl [ --enable-xz Enable xz support])
18+
19+
if test "$PHP_XZ" != "no"; then
20+
dnl Write more examples of tests here...
21+
22+
dnl # --with-xz -> check with-path
23+
dnl SEARCH_PATH="/usr/local /usr" # you might want to change this
24+
dnl SEARCH_FOR="/include/xz.h" # you most likely want to change this
25+
dnl if test -r $PHP_XZ/$SEARCH_FOR; then # path given as parameter
26+
dnl XZ_DIR=$PHP_XZ
27+
dnl else # search default path list
28+
dnl AC_MSG_CHECKING([for xz files in default path])
29+
dnl for i in $SEARCH_PATH ; do
30+
dnl if test -r $i/$SEARCH_FOR; then
31+
dnl XZ_DIR=$i
32+
dnl AC_MSG_RESULT(found in $i)
33+
dnl fi
34+
dnl done
35+
dnl fi
36+
dnl
37+
dnl if test -z "$XZ_DIR"; then
38+
dnl AC_MSG_RESULT([not found])
39+
dnl AC_MSG_ERROR([Please reinstall the xz distribution])
40+
dnl fi
41+
42+
dnl # --with-xz -> add include path
43+
dnl PHP_ADD_INCLUDE($XZ_DIR/include)
44+
45+
dnl # --with-xz -> check for lib and symbol presence
46+
LIBNAME=lzma # you may want to change this
47+
LIBSYMBOL=lzma_stream_encoder # you most likely want to change this
48+
49+
PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
50+
[
51+
PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $XZ_DIR/lib, XZ_SHARED_LIBADD)
52+
AC_DEFINE(HAVE_XZLIB,1,[ ])
53+
],[
54+
AC_MSG_ERROR([wrong xz lib version or lib not found])
55+
],[
56+
-L$XZ_DIR/lib -lm
57+
])
58+
PHP_SUBST(XZ_SHARED_LIBADD)
59+
60+
PHP_NEW_EXTENSION(xz, xz.c xz_fopen_wrapper.c, $ext_shared)
61+
fi

config.w32

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// $Id$
2+
// vim:ft=javascript
3+
4+
// If your extension references something external, use ARG_WITH
5+
// ARG_WITH("xz", "for xz support", "no");
6+
7+
// Otherwise, use ARG_ENABLE
8+
// ARG_ENABLE("xz", "enable xz support", "no");
9+
10+
if (PHP_XZ != "no") {
11+
EXTENSION("xz", "xz.c");
12+
}
13+

php_xz.h

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2013 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Payden Sutherland <[email protected]> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifndef PHP_XZ_H
22+
#define PHP_XZ_H
23+
24+
extern zend_module_entry xz_module_entry;
25+
#define phpext_xz_ptr &xz_module_entry
26+
27+
#define XZ_INBUF_SIZE 4096
28+
#define XZ_OUTBUF_SIZE 4096
29+
30+
#ifdef PHP_WIN32
31+
# define PHP_XZ_API __declspec(dllexport)
32+
#elif defined(__GNUC__) && __GNUC__ >= 4
33+
# define PHP_XZ_API __attribute__ ((visibility("default")))
34+
#else
35+
# define PHP_XZ_API
36+
#endif
37+
38+
#ifdef ZTS
39+
#include "TSRM.h"
40+
#endif
41+
42+
PHP_MINIT_FUNCTION(xz);
43+
PHP_MSHUTDOWN_FUNCTION(xz);
44+
PHP_RINIT_FUNCTION(xz);
45+
PHP_RSHUTDOWN_FUNCTION(xz);
46+
PHP_MINFO_FUNCTION(xz);
47+
48+
PHP_FUNCTION(xzopen);
49+
/*
50+
Declare any global variables you may need between the BEGIN
51+
and END macros here:
52+
53+
ZEND_BEGIN_MODULE_GLOBALS(xz)
54+
long global_value;
55+
char *global_string;
56+
ZEND_END_MODULE_GLOBALS(xz)
57+
*/
58+
59+
/* In every utility function you add that needs to use variables
60+
in php_xz_globals, call TSRMLS_FETCH(); after declaring other
61+
variables used by that function, or better yet, pass in TSRMLS_CC
62+
after the last function argument and declare your utility function
63+
with TSRMLS_DC after the last declared argument. Always refer to
64+
the globals in your function as XZ_G(variable). You are
65+
encouraged to rename these macros something shorter, see
66+
examples in any other php module directory.
67+
*/
68+
69+
#ifdef ZTS
70+
#define XZ_G(v) TSRMG(xz_globals_id, zend_xz_globals *, v)
71+
#else
72+
#define XZ_G(v) (xz_globals.v)
73+
#endif
74+
75+
#endif /* PHP_XZ_H */
76+
77+
78+
/*
79+
* Local variables:
80+
* tab-width: 4
81+
* c-basic-offset: 4
82+
* End:
83+
* vim600: noet sw=4 ts=4 fdm=marker
84+
* vim<600: noet sw=4 ts=4
85+
*/

xz.c

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2013 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifdef HAVE_CONFIG_H
22+
#include "config.h"
23+
#endif
24+
25+
#include "php.h"
26+
#include "php_ini.h"
27+
#include "ext/standard/info.h"
28+
#include "php_xz.h"
29+
30+
/* If you declare any globals in php_xz.h uncomment this:
31+
ZEND_DECLARE_MODULE_GLOBALS(xz)
32+
*/
33+
34+
/* True global resources - no need for thread safety here */
35+
static int le_xz;
36+
37+
/* {{{ xz_functions[]
38+
*
39+
* Every user visible function must have an entry in xz_functions[].
40+
*/
41+
const zend_function_entry xz_functions[] = {
42+
PHP_FE(xzopen, NULL)
43+
PHP_FE_END /* Must be the last line in xz_functions[] */
44+
};
45+
/* }}} */
46+
47+
/* {{{ xz_module_entry
48+
*/
49+
zend_module_entry xz_module_entry = {
50+
#if ZEND_MODULE_API_NO >= 20010901
51+
STANDARD_MODULE_HEADER,
52+
#endif
53+
"xz",
54+
xz_functions,
55+
PHP_MINIT(xz),
56+
PHP_MSHUTDOWN(xz),
57+
PHP_RINIT(xz), /* Replace with NULL if there's nothing to do at request start */
58+
PHP_RSHUTDOWN(xz), /* Replace with NULL if there's nothing to do at request end */
59+
PHP_MINFO(xz),
60+
#if ZEND_MODULE_API_NO >= 20010901
61+
"0.1", /* Replace with version number for your extension */
62+
#endif
63+
STANDARD_MODULE_PROPERTIES
64+
};
65+
/* }}} */
66+
67+
#ifdef COMPILE_DL_XZ
68+
ZEND_GET_MODULE(xz)
69+
#endif
70+
71+
/* {{{ PHP_INI
72+
*/
73+
/* Remove comments and fill if you need to have entries in php.ini
74+
PHP_INI_BEGIN()
75+
STD_PHP_INI_ENTRY("xz.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_xz_globals, xz_globals)
76+
STD_PHP_INI_ENTRY("xz.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_xz_globals, xz_globals)
77+
PHP_INI_END()
78+
*/
79+
/* }}} */
80+
81+
/* {{{ php_xz_init_globals
82+
*/
83+
/* Uncomment this function if you have INI entries
84+
static void php_xz_init_globals(zend_xz_globals *xz_globals)
85+
{
86+
xz_globals->global_value = 0;
87+
xz_globals->global_string = NULL;
88+
}
89+
*/
90+
/* }}} */
91+
92+
/* {{{ PHP_MINIT_FUNCTION
93+
*/
94+
PHP_MINIT_FUNCTION(xz)
95+
{
96+
/* If you have INI entries, uncomment these lines
97+
REGISTER_INI_ENTRIES();
98+
*/
99+
return SUCCESS;
100+
}
101+
/* }}} */
102+
103+
/* {{{ PHP_MSHUTDOWN_FUNCTION
104+
*/
105+
PHP_MSHUTDOWN_FUNCTION(xz)
106+
{
107+
/* uncomment this line if you have INI entries
108+
UNREGISTER_INI_ENTRIES();
109+
*/
110+
return SUCCESS;
111+
}
112+
/* }}} */
113+
114+
/* Remove if there's nothing to do at request start */
115+
/* {{{ PHP_RINIT_FUNCTION
116+
*/
117+
PHP_RINIT_FUNCTION(xz)
118+
{
119+
return SUCCESS;
120+
}
121+
/* }}} */
122+
123+
/* Remove if there's nothing to do at request end */
124+
/* {{{ PHP_RSHUTDOWN_FUNCTION
125+
*/
126+
PHP_RSHUTDOWN_FUNCTION(xz)
127+
{
128+
return SUCCESS;
129+
}
130+
/* }}} */
131+
132+
/* {{{ PHP_MINFO_FUNCTION
133+
*/
134+
PHP_MINFO_FUNCTION(xz)
135+
{
136+
php_info_print_table_start();
137+
php_info_print_table_header(2, "xz support", "enabled");
138+
php_info_print_table_end();
139+
140+
/* Remove comments if you have entries in php.ini
141+
DISPLAY_INI_ENTRIES();
142+
*/
143+
}
144+
/* }}} */
145+
146+
147+
PHP_FUNCTION(xzopen)
148+
{
149+
char *filename;
150+
char *mode;
151+
int filename_len, mode_len;
152+
php_stream *stream;
153+
154+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &filename, &filename_len, &mode, &mode_len) == FAILURE) {
155+
return;
156+
}
157+
158+
stream = php_stream_xzopen(NULL, filename, mode, NULL, NULL STREAMS_CC TSRMLS_CC);
159+
160+
if (!stream) {
161+
RETURN_FALSE;
162+
}
163+
164+
php_stream_to_zval(stream, return_value);
165+
}
166+
167+
168+
/*
169+
* Local variables:
170+
* tab-width: 4
171+
* c-basic-offset: 4
172+
* End:
173+
* vim600: noet sw=4 ts=4 fdm=marker
174+
* vim<600: noet sw=4 ts=4
175+
*/

0 commit comments

Comments
 (0)