From: Christopher Kings-Lynne Date: Fri, 25 Mar 2005 06:26:31 +0000 (+0000) Subject: (PHP pg_unescape_bytea) Use libpq version of PQunescapeBytea if it exists. X-Git-Tag: php-5.0.1b1~685 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=030ebaaa458f534871fe62a20fcea7e8ee1d5a57;p=php (PHP pg_unescape_bytea) Use libpq version of PQunescapeBytea if it exists. # The version in libpq is newer and faster than the one in PHP, but it is # necessary for me to add a string copy for freeing purposes. This copy # is only needed in Windows AFAIK, how can I detect that? --- diff --git a/NEWS b/NEWS index d887f07e1c..915931d895 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,8 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 2004, PHP 5.1.0 +- Changed pg_unescape_bytea() to use the libpq version of PQunescapeBytea if + it is available. - Moved extensions to PECL: . ext/cpdf (Tony, Derick) . ext/dio (Jani, Derick) diff --git a/ext/pgsql/config.m4 b/ext/pgsql/config.m4 index da9badb807..1da8115c90 100644 --- a/ext/pgsql/config.m4 +++ b/ext/pgsql/config.m4 @@ -69,6 +69,7 @@ if test "$PHP_PGSQL" != "no"; then old_LDFLAGS=$LDFLAGS LDFLAGS="$LDFLAGS -L$PGSQL_LIBDIR" AC_CHECK_LIB(pq, PQescapeString,AC_DEFINE(HAVE_PQESCAPE,1,[PostgreSQL 7.2.0 or later])) + AC_CHECK_LIB(pq, PQunescapeBytea,AC_DEFINE(HAVE_PQUNESCAPEBYTEA,1,[PostgreSQL 7.3.0 or later])) AC_CHECK_LIB(pq, PQsetnonblocking,AC_DEFINE(HAVE_PQSETNONBLOCKING,1,[PostgreSQL 7.0.x or later])) AC_CHECK_LIB(pq, PQcmdTuples,AC_DEFINE(HAVE_PQCMDTUPLES,1,[Broken libpq under windows])) AC_CHECK_LIB(pq, PQoidValue,AC_DEFINE(HAVE_PQOIDVALUE,1,[Older PostgreSQL])) diff --git a/ext/pgsql/config.w32 b/ext/pgsql/config.w32 index 835883e478..8f4b8a955b 100644 --- a/ext/pgsql/config.w32 +++ b/ext/pgsql/config.w32 @@ -8,7 +8,7 @@ if (PHP_PGSQL != "no") { CHECK_HEADER_ADD_INCLUDE("libpq-fe.h", "CFLAGS_PGSQL", PHP_PHP_BUILD + "\\include\\pgsql;" + PHP_PGSQL)) { EXTENSION("pgsql", "pgsql.c"); AC_DEFINE('HAVE_PGSQL', 1, 'Have PostgreSQL library'); - ADD_FLAG("CFLAGS_PGSQL", "/D HAVE_PG_CONFIG_H /D PGSQL_EXPORTS /D HAVE_PQSETNONBLOCKING /D HAVE_PQCMDTUPLES /D HAVE_PQCLIENTENCODING /D HAVE_PQESCAPE /D HAVE_PQPARAMETERSTATUS /D HAVE_PGTRANSACTIONSTATUS /D HAVE_PQEXECPARAMS /D HAVE_PQPREPARE /D HAVE_PQEXECPREPARED /D HAVE_PQRESULTERRORFIELD /D HAVE_PQSENDQUERYPARAMS /D HAVE_PQSENDPREPARE /D HAVE_PQSENDQUERYPREPARED /D HAVE_PQPUTCOPYDATA /D HAVE_PQPUTCOPYEND /D HAVE_PQGETCOPYDATA /D HAVE_PQSETERRORVERBOSITY"); + ADD_FLAG("CFLAGS_PGSQL", "/D HAVE_PG_CONFIG_H /D PGSQL_EXPORTS /D HAVE_PQSETNONBLOCKING /D HAVE_PQCMDTUPLES /D HAVE_PQCLIENTENCODING /D HAVE_PQESCAPE /D HAVE_PQPARAMETERSTATUS /D HAVE_PGTRANSACTIONSTATUS /D HAVE_PQEXECPARAMS /D HAVE_PQPREPARE /D HAVE_PQEXECPREPARED /D HAVE_PQRESULTERRORFIELD /D HAVE_PQSENDQUERYPARAMS /D HAVE_PQSENDPREPARE /D HAVE_PQSENDQUERYPREPARED /D HAVE_PQPUTCOPYDATA /D HAVE_PQPUTCOPYEND /D HAVE_PQGETCOPYDATA /D HAVE_PQSETERRORVERBOSITY /D HAVE_PQUNESCAPEBYTEA"); } else { WARNING("pgsql not enabled; libraries and headers not found"); } diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index f90f8fa66c..37402cd06a 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -3414,6 +3414,7 @@ PHP_FUNCTION(pg_escape_bytea) } /* }}} */ +#if !HAVE_PQUNESCAPEBYTEA /* PQunescapeBytea() from PostgreSQL 7.3 to provide bytea unescape feature to 7.2 users. Renamed to php_pgsql_unescape_bytea() */ /* @@ -3517,12 +3518,13 @@ static unsigned char * php_pgsql_unescape_bytea(unsigned char *strtext, size_t * *retbuflen = buflen; return buffer; } +#endif /* {{{ proto string pg_unescape_bytea(string data) Unescape binary for bytea type */ PHP_FUNCTION(pg_unescape_bytea) { - char *from = NULL, *to = NULL; + char *from = NULL, *to = NULL, *tmp = NULL; size_t to_len; int from_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", @@ -3530,7 +3532,13 @@ PHP_FUNCTION(pg_unescape_bytea) return; } +#if HAVE_PQUNESCAPEBYTEA + tmp = (char *)PQunescapeBytea((unsigned char*)from, &to_len); + to = estrndup(tmp, to_len); + PQfreemem(tmp); +#else to = (char *)php_pgsql_unescape_bytea((unsigned char*)from, &to_len); +#endif if (!to) { RETURN_FALSE; } diff --git a/ext/pgsql/tests/08escape.phpt b/ext/pgsql/tests/08escape.phpt index c5a3877e37..cf23b50e3a 100644 --- a/ext/pgsql/tests/08escape.phpt +++ b/ext/pgsql/tests/08escape.phpt @@ -51,14 +51,12 @@ $sql = "SELECT bin::bytea FROM ".$table_name." WHERE num = -9999"; $result = pg_query($db, $sql); $row = pg_fetch_array($result, 0, PGSQL_ASSOC); -// Compare -// Need to wait PostgreSQL 7.3.x for PQunescapeBytea() -// if ($data === pg_unescape_bytea($row['bin'])) { -// echo "pg_escape_bytea() actually works with databse\n"; -// } -// else { -// echo "pg_escape_bytea() is broken\n"; -// } +if ($data === pg_unescape_bytea($row['bin'])) { + echo "pg_escape_bytea() actually works with database\n"; +} +else { + echo "pg_escape_bytea() is broken\n"; +} ?> --EXPECT-- @@ -67,3 +65,4 @@ string(9) "ABC\ABC\'" string(12) "ABC\\ABC\\''" string(10) "ABC\\ABC\'" pg_escape_bytea() is Ok +pg_escape_bytea() actually works with database diff --git a/ext/pgsql/tests/25async_query_params.phpt b/ext/pgsql/tests/25async_query_params.phpt index 3244532094..6e7dafe34b 100644 --- a/ext/pgsql/tests/25async_query_params.phpt +++ b/ext/pgsql/tests/25async_query_params.phpt @@ -62,7 +62,7 @@ if ($version['protocol'] >= 3) { pg_last_oid($result); pg_free_result($result); } -pg_close($db) +pg_close($db); echo "OK"; ?>