cvs: pear /PECL/sqlite php_sqlite.h sqlite.c /PECL/sqlite/tests sqlite_017.phpt
| From: | Wez Furlong | Date: | Sat, 21 Jun 2003 12:29:32 +0000 |
| Subject: | cvs: pear /PECL/sqlite php_sqlite.h sqlite.c /PECL/sqlite/tests sqlite_017.phpt | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-279@lists.php.net to get a copy of this message | ||
wez Sat Jun 21 08:29:32 2003 EDT
Added files:
/pear/PECL/sqlite/tests sqlite_017.phpt
Modified files:
/pear/PECL/sqlite php_sqlite.h sqlite.c
Log:
Add a pair of functions to handle the binary coding used by PHP from within
UDF callbacks.
Add test for binary functions.
Fix proto.
Index: pear/PECL/sqlite/php_sqlite.h
diff -u pear/PECL/sqlite/php_sqlite.h:1.20 pear/PECL/sqlite/php_sqlite.h:1.21
--- pear/PECL/sqlite/php_sqlite.h:1.20 Sat Jun 14 14:16:47 2003
+++ pear/PECL/sqlite/php_sqlite.h Sat Jun 21 08:29:32 2003
@@ -17,7 +17,7 @@
| Marcus Boerger <helly@php.net> |
+----------------------------------------------------------------------+
- $Id: php_sqlite.h,v 1.20 2003/06/14 18:16:47 helly Exp $
+ $Id: php_sqlite.h,v 1.21 2003/06/21 12:29:32 wez Exp $
*/
#ifndef PHP_SQLITE_H
@@ -78,6 +78,8 @@
PHP_FUNCTION(sqlite_create_aggregate);
PHP_FUNCTION(sqlite_create_function);
+PHP_FUNCTION(sqlite_udf_decode_binary);
+PHP_FUNCTION(sqlite_udf_encode_binary);
ZEND_BEGIN_MODULE_GLOBALS(sqlite)
long assoc_case;
Index: pear/PECL/sqlite/sqlite.c
diff -u pear/PECL/sqlite/sqlite.c:1.53 pear/PECL/sqlite/sqlite.c:1.54
--- pear/PECL/sqlite/sqlite.c:1.53 Thu Jun 19 16:57:11 2003
+++ pear/PECL/sqlite/sqlite.c Sat Jun 21 08:29:32 2003
@@ -17,7 +17,7 @@
| Marcus Boerger <helly@php.net> |
+----------------------------------------------------------------------+
- $Id: sqlite.c,v 1.53 2003/06/19 20:57:11 wez Exp $
+ $Id: sqlite.c,v 1.54 2003/06/21 12:29:32 wez Exp $
*/
#ifdef HAVE_CONFIG_H
@@ -142,6 +142,8 @@
PHP_FE(sqlite_unbuffered_query, NULL)
PHP_FE(sqlite_create_aggregate, NULL)
PHP_FE(sqlite_create_function, NULL)
+ PHP_FE(sqlite_udf_encode_binary, NULL)
+ PHP_FE(sqlite_udf_decode_binary, NULL)
{NULL, NULL, NULL}
};
@@ -664,7 +666,7 @@
{
php_info_print_table_start();
php_info_print_table_header(2, "SQLite support", "enabled");
- php_info_print_table_row(2, "PECL Module version", PHP_SQLITE_MODULE_VERSION " $Id:
sqlite.c,v 1.53 2003/06/19 20:57:11 wez Exp $");
+ php_info_print_table_row(2, "PECL Module version", PHP_SQLITE_MODULE_VERSION " $Id:
sqlite.c,v 1.54 2003/06/21 12:29:32 wez Exp $");
php_info_print_table_row(2, "SQLite Library", sqlite_libversion());
php_info_print_table_row(2, "SQLite Encoding", sqlite_libencoding());
php_info_print_table_end();
@@ -1371,8 +1373,8 @@
}
/* }}} */
-/* {{{ proto array sqlite_column(resource result, mixed index_or_name [, bool decode_binary])
- Fetches a column from the current row from a result set */
+/* {{{ proto mixed sqlite_column(resource result, mixed index_or_name [, bool decode_binary])
+ Fetches a column from the current row of a result set */
PHP_FUNCTION(sqlite_column)
{
zval *zres;
@@ -1807,6 +1809,65 @@
}
}
/* }}} */
+
+/* {{{ proto string sqlite_udf_encode_binary(string data)
+ Apply binary encoding (if required) to a string to return from an UDF */
+PHP_FUNCTION(sqlite_udf_encode_binary)
+{
+ char *data = NULL;
+ long datalen;
+
+ if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &data,
&datalen)) {
+ return;
+ }
+
+ if (data == NULL) {
+ RETURN_NULL();
+ }
+ if (datalen && (data[0] == '\x01' || memchr(data, '\0', datalen) !=
NULL)) {
+ /* binary string */
+ int enclen;
+ char *ret;
+
+ ret = emalloc( 1 + ((256 * datalen + 1262) / 253) );
+ ret[0] = '\x01';
+ enclen = sqlite_encode_binary((const unsigned char*)data, datalen, ret+1);
+ RETVAL_STRINGL(ret, enclen+1, 0);
+ } else {
+ RETVAL_STRINGL(data, datalen, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto string sqlite_udf_decode_binary(string data)
+ Decode binary encoding on a string parameter passed to an UDF */
+PHP_FUNCTION(sqlite_udf_decode_binary)
+{
+ char *data = NULL;
+ long datalen;
+
+ if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &data,
&datalen)) {
+ return;
+ }
+
+ if (data == NULL) {
+ RETURN_NULL();
+ }
+ if (datalen && data[0] == '\x01') {
+ /* encoded string */
+ int enclen;
+ char *ret;
+
+ ret = emalloc(datalen);
+ enclen = sqlite_decode_binary((const unsigned char*)data+1, ret);
+ ret[enclen] = '\0';
+ RETVAL_STRINGL(ret, enclen, 0);
+ } else {
+ RETVAL_STRINGL(data, datalen, 1);
+ }
+}
+/* }}} */
+
/*
* Local variables:
Index: pear/PECL/sqlite/tests/sqlite_017.phpt
+++ pear/PECL/sqlite/tests/sqlite_017.phpt
--TEST--
sqlite: UDF binary handling functions
--SKIPIF--
<?php # vim:ft=php
if (!extension_loaded("sqlite")) print "skip"; ?>
--FILE--
<?php
$data = array(
"hello there",
"this has a \x00 char in the middle",
"\x01 this has an 0x01 at the start",
"this has \x01 in the middle"
);
foreach ($data as $item) {
$coded = sqlite_udf_encode_binary($item);
echo bin2hex($coded) . "\n";
$decoded = sqlite_udf_decode_binary($coded);
if ($item != $decoded) {
echo "FAIL! $item decoded is $decoded\n";
}
}
echo "OK!\n";
?>
--EXPECT--
68656c6c6f207468657265
0101736768721f6760721f601fff1f626760711f686d1f7367641f6c6863636b64
0102ff1e726667711e665f711e5f6c1e2e762e2f1e5f721e7266631e71725f7072
7468697320686173200120696e20746865206d6964646c65
OK!