Use new cstring/text conversion functions in some additional places.

These changes assume that the varchar and xml data types are represented
the same as text.  (I did not, however, accept the portions of the proposed
patch that wanted to assume bytea is the same as text --- tgl.)

Brendan Jurd
This commit is contained in:
Tom Lane 2008-05-04 16:42:41 +00:00
parent 0ff74f03b1
commit 45173ae24e
5 changed files with 24 additions and 99 deletions

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-pgsql.c,v 1.9 2007/02/27 23:48:06 tgl Exp $ * $PostgreSQL: pgsql/contrib/pgcrypto/pgp-pgsql.c,v 1.10 2008/05/04 16:42:41 tgl Exp $
*/ */
#include "postgres.h" #include "postgres.h"
@ -34,6 +34,7 @@
#include "fmgr.h" #include "fmgr.h"
#include "parser/scansup.h" #include "parser/scansup.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "mbuf.h" #include "mbuf.h"
#include "px.h" #include "px.h"
@ -140,7 +141,6 @@ static text *
convert_charset(text *src, int cset_from, int cset_to) convert_charset(text *src, int cset_from, int cset_to)
{ {
int src_len = VARSIZE(src) - VARHDRSZ; int src_len = VARSIZE(src) - VARHDRSZ;
int dst_len;
unsigned char *dst; unsigned char *dst;
unsigned char *csrc = (unsigned char *) VARDATA(src); unsigned char *csrc = (unsigned char *) VARDATA(src);
text *res; text *res;
@ -149,10 +149,7 @@ convert_charset(text *src, int cset_from, int cset_to)
if (dst == csrc) if (dst == csrc)
return src; return src;
dst_len = strlen((char *) dst); res = cstring_to_text((char *) dst);
res = palloc(dst_len + VARHDRSZ);
memcpy(VARDATA(res), dst, dst_len);
SET_VARSIZE(res, dst_len + VARHDRSZ);
pfree(dst); pfree(dst);
return res; return res;
} }

View File

@ -194,7 +194,6 @@ xml_encode_special_chars(PG_FUNCTION_ARGS)
{ {
text *tin = PG_GETARG_TEXT_P(0); text *tin = PG_GETARG_TEXT_P(0);
text *tout; text *tout;
int32 ressize;
xmlChar *ts, xmlChar *ts,
*tt; *tt;
@ -204,10 +203,7 @@ xml_encode_special_chars(PG_FUNCTION_ARGS)
pfree(ts); pfree(ts);
ressize = strlen((char *) tt); tout = cstring_to_text((char *) tt);
tout = (text *) palloc(ressize + VARHDRSZ);
memcpy(VARDATA(tout), tt, ressize);
SET_VARSIZE(tout, ressize + VARHDRSZ);
xmlFree(tt); xmlFree(tt);
@ -306,14 +302,7 @@ pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
xmlChar * xmlChar *
pgxml_texttoxmlchar(text *textstring) pgxml_texttoxmlchar(text *textstring)
{ {
xmlChar *res; return (xmlChar *) text_to_cstring(textstring);
int32 txsize;
txsize = VARSIZE(textstring) - VARHDRSZ;
res = (xmlChar *) palloc(txsize + 1);
memcpy((char *) res, VARDATA(textstring), txsize);
res[txsize] = '\0';
return res;
} }
/* Public visible XPath functions */ /* Public visible XPath functions */
@ -577,7 +566,6 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
xmlChar * plainsep) xmlChar * plainsep)
{ {
xmlChar *xpresstr; xmlChar *xpresstr;
int32 ressize;
text *xpres; text *xpres;
if (res == NULL) if (res == NULL)
@ -604,10 +592,7 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
/* Now convert this result back to text */ /* Now convert this result back to text */
ressize = strlen((char *) xpresstr); xpres = cstring_to_text((char *) xpresstr);
xpres = (text *) palloc(ressize + VARHDRSZ);
memcpy(VARDATA(xpres), xpresstr, ressize);
SET_VARSIZE(xpres, ressize + VARHDRSZ);
/* Free various storage */ /* Free various storage */
xmlCleanupParser(); xmlCleanupParser();

View File

@ -39,8 +39,9 @@ PG_FUNCTION_INFO_V1(xslt_process);
Datum Datum
xslt_process(PG_FUNCTION_ARGS) xslt_process(PG_FUNCTION_ARGS)
{ {
text *doct = PG_GETARG_TEXT_P(0);
text *ssheet = PG_GETARG_TEXT_P(1);
text *paramstr;
const char *params[MAXPARAMS + 1]; /* +1 for the terminator */ const char *params[MAXPARAMS + 1]; /* +1 for the terminator */
xsltStylesheetPtr stylesheet = NULL; xsltStylesheetPtr stylesheet = NULL;
xmlDocPtr doctree; xmlDocPtr doctree;
@ -50,12 +51,6 @@ xslt_process(PG_FUNCTION_ARGS)
int resstat; int resstat;
int reslen; int reslen;
text *doct = PG_GETARG_TEXT_P(0);
text *ssheet = PG_GETARG_TEXT_P(1);
text *paramstr;
text *tres;
if (fcinfo->nargs == 3) if (fcinfo->nargs == 3)
{ {
paramstr = PG_GETARG_TEXT_P(2); paramstr = PG_GETARG_TEXT_P(2);
@ -124,11 +119,7 @@ xslt_process(PG_FUNCTION_ARGS)
if (resstat < 0) if (resstat < 0)
PG_RETURN_NULL(); PG_RETURN_NULL();
tres = palloc(reslen + VARHDRSZ); PG_RETURN_TEXT_P(cstring_to_text_with_len(resstr, reslen));
memcpy(VARDATA(tres), resstr, reslen);
SET_VARSIZE(tres, reslen + VARHDRSZ);
PG_RETURN_TEXT_P(tres);
} }

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.127 2008/03/25 22:42:44 tgl Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.128 2008/05/04 16:42:41 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -566,7 +566,6 @@ varchar(PG_FUNCTION_ARGS)
VarChar *source = PG_GETARG_VARCHAR_PP(0); VarChar *source = PG_GETARG_VARCHAR_PP(0);
int32 typmod = PG_GETARG_INT32(1); int32 typmod = PG_GETARG_INT32(1);
bool isExplicit = PG_GETARG_BOOL(2); bool isExplicit = PG_GETARG_BOOL(2);
VarChar *result;
int32 len, int32 len,
maxlen; maxlen;
size_t maxmblen; size_t maxmblen;
@ -596,11 +595,8 @@ varchar(PG_FUNCTION_ARGS)
maxlen))); maxlen)));
} }
result = palloc(maxmblen + VARHDRSZ); PG_RETURN_VARCHAR_P((VarChar *) cstring_to_text_with_len(s_data,
SET_VARSIZE(result, maxmblen + VARHDRSZ); maxmblen));
memcpy(VARDATA(result), s_data, maxmblen);
PG_RETURN_VARCHAR_P(result);
} }
Datum Datum

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.72 2008/04/04 08:33:15 mha Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.73 2008/05/04 16:42:41 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -319,13 +319,7 @@ xml_recv(PG_FUNCTION_ARGS)
if (newstr != str) if (newstr != str)
{ {
pfree(result); pfree(result);
result = (xmltype *) cstring_to_text(newstr);
nbytes = strlen(newstr);
result = palloc(nbytes + VARHDRSZ);
SET_VARSIZE(result, nbytes + VARHDRSZ);
memcpy(VARDATA(result), newstr, nbytes);
pfree(newstr); pfree(newstr);
} }
@ -369,30 +363,14 @@ appendStringInfoText(StringInfo str, const text *t)
static xmltype * static xmltype *
stringinfo_to_xmltype(StringInfo buf) stringinfo_to_xmltype(StringInfo buf)
{ {
int32 len; return (xmltype *) cstring_to_text_with_len(buf->data, buf->len);
xmltype *result;
len = buf->len + VARHDRSZ;
result = palloc(len);
SET_VARSIZE(result, len);
memcpy(VARDATA(result), buf->data, buf->len);
return result;
} }
static xmltype * static xmltype *
cstring_to_xmltype(const char *string) cstring_to_xmltype(const char *string)
{ {
int32 len; return (xmltype *) cstring_to_text(string);
xmltype *result;
len = strlen(string) + VARHDRSZ;
result = palloc(len);
SET_VARSIZE(result, len);
memcpy(VARDATA(result), string, len - VARHDRSZ);
return result;
} }
@ -400,15 +378,8 @@ cstring_to_xmltype(const char *string)
static xmltype * static xmltype *
xmlBuffer_to_xmltype(xmlBufferPtr buf) xmlBuffer_to_xmltype(xmlBufferPtr buf)
{ {
int32 len; return (xmltype *) cstring_to_text_with_len((char *) xmlBufferContent(buf),
xmltype *result; xmlBufferLength(buf));
len = xmlBufferLength(buf) + VARHDRSZ;
result = palloc(len);
SET_VARSIZE(result, len);
memcpy(VARDATA(result), xmlBufferContent(buf), len - VARHDRSZ);
return result;
} }
#endif #endif
@ -474,9 +445,7 @@ xmlconcat(List *args)
char *str; char *str;
len = VARSIZE(x) - VARHDRSZ; len = VARSIZE(x) - VARHDRSZ;
str = palloc(len + 1); str = text_to_cstring((text *) x);
memcpy(str, VARDATA(x), len);
str[len] = '\0';
parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone); parse_xml_decl((xmlChar *) str, &len, &version, NULL, &standalone);
@ -751,9 +720,7 @@ xmlroot(xmltype *data, text *version, int standalone)
StringInfoData buf; StringInfoData buf;
len = VARSIZE(data) - VARHDRSZ; len = VARSIZE(data) - VARHDRSZ;
str = palloc(len + 1); str = text_to_cstring((text *) data);
memcpy(str, VARDATA(data), len);
str[len] = '\0';
parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone); parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
@ -1237,19 +1204,12 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
/* /*
* xmlChar<->text convertions * xmlChar<->text conversions
*/ */
static xmlChar * static xmlChar *
xml_text2xmlChar(text *in) xml_text2xmlChar(text *in)
{ {
int32 len = VARSIZE(in) - VARHDRSZ; return (xmlChar *) text_to_cstring(in);
xmlChar *res;
res = palloc(len + 1);
memcpy(res, VARDATA(in), len);
res[len] = '\0';
return (res);
} }
@ -3188,7 +3148,6 @@ xml_xmlnodetoxmltype(xmlNodePtr cur)
{ {
xmlChar *str; xmlChar *str;
xmltype *result; xmltype *result;
size_t len;
xmlBufferPtr buf; xmlBufferPtr buf;
if (cur->type == XML_ELEMENT_NODE) if (cur->type == XML_ELEMENT_NODE)
@ -3201,10 +3160,7 @@ xml_xmlnodetoxmltype(xmlNodePtr cur)
else else
{ {
str = xmlXPathCastNodeToString(cur); str = xmlXPathCastNodeToString(cur);
len = strlen((char *) str); result = (xmltype *) cstring_to_text((char *) str);
result = (xmltype *) palloc(len + VARHDRSZ);
SET_VARSIZE(result, len + VARHDRSZ);
memcpy(VARDATA(result), str, len);
} }
return result; return result;