From 220db7ccd8c88aafea4629f00e8be6f9f073ed00 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 25 Mar 2008 22:42:46 +0000 Subject: Simplify and standardize conversions between TEXT datums and ordinary C strings. This patch introduces four support functions cstring_to_text, cstring_to_text_with_len, text_to_cstring, and text_to_cstring_buffer, and two macros CStringGetTextDatum and TextDatumGetCString. A number of existing macros that provided variants on these themes were removed. Most of the places that need to make such conversions now require just one function or macro call, in place of the multiple notational layers that used to be needed. There are no longer any direct calls of textout or textin, and we got most of the places that were using handmade conversions via memcpy (there may be a few still lurking, though). This commit doesn't make any serious effort to eliminate transient memory leaks caused by detoasting toasted text objects before they reach text_to_cstring. We changed PG_GETARG_TEXT_P to PG_GETARG_TEXT_PP in a few places where it was easy, but much more could be done. Brendan Jurd and Tom Lane --- contrib/adminpack/adminpack.c | 9 +-- contrib/chkpass/chkpass.c | 30 +++----- contrib/dblink/dblink.c | 137 +++++++++++++++++----------------- contrib/fuzzystrmatch/dmetaphone.c | 65 +++++----------- contrib/fuzzystrmatch/fuzzystrmatch.c | 52 +++++-------- contrib/fuzzystrmatch/fuzzystrmatch.h | 4 +- contrib/hstore/hstore_op.c | 55 ++++++-------- contrib/intarray/_int_bool.c | 11 +-- contrib/ltree/ltree_op.c | 39 ++++------ contrib/pageinspect/heapfuncs.c | 8 +- contrib/pageinspect/rawpage.c | 4 +- contrib/pgcrypto/pgcrypto.c | 65 ++++------------ contrib/spi/autoinc.c | 3 +- contrib/spi/insert_username.c | 5 +- contrib/spi/timetravel.c | 2 +- contrib/sslinfo/sslinfo.c | 29 ++----- contrib/tablefunc/tablefunc.c | 37 +++++---- contrib/tsearch2/tsearch2.c | 31 ++++---- contrib/uuid-ossp/uuid-ossp.c | 4 +- contrib/xml2/xpath.c | 15 ++-- contrib/xml2/xslt_proc.c | 9 +-- 21 files changed, 223 insertions(+), 391 deletions(-) (limited to 'contrib') diff --git a/contrib/adminpack/adminpack.c b/contrib/adminpack/adminpack.c index e697b5e2a90..879259ee03d 100644 --- a/contrib/adminpack/adminpack.c +++ b/contrib/adminpack/adminpack.c @@ -8,7 +8,7 @@ * Author: Andreas Pflug * * IDENTIFICATION - * $PostgreSQL: pgsql/contrib/adminpack/adminpack.c,v 1.10 2008/01/01 19:45:45 momjian Exp $ + * $PostgreSQL: pgsql/contrib/adminpack/adminpack.c,v 1.11 2008/03/25 22:42:41 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -23,6 +23,7 @@ #include "miscadmin.h" #include "postmaster/syslogger.h" #include "storage/fd.h" +#include "utils/builtins.h" #include "utils/datetime.h" @@ -68,11 +69,7 @@ typedef struct static char * convert_and_check_filename(text *arg, bool logAllowed) { - int input_len = VARSIZE(arg) - VARHDRSZ; - char *filename = palloc(input_len + 1); - - memcpy(filename, VARDATA(arg), input_len); - filename[input_len] = '\0'; + char *filename = text_to_cstring(arg); canonicalize_path(filename); /* filename can change length here */ diff --git a/contrib/chkpass/chkpass.c b/contrib/chkpass/chkpass.c index 18cac99ca6b..ed6a7566b25 100644 --- a/contrib/chkpass/chkpass.c +++ b/contrib/chkpass/chkpass.c @@ -4,7 +4,7 @@ * darcy@druid.net * http://www.druid.net/darcy/ * - * $PostgreSQL: pgsql/contrib/chkpass/chkpass.c,v 1.19 2007/02/27 23:48:05 tgl Exp $ + * $PostgreSQL: pgsql/contrib/chkpass/chkpass.c,v 1.20 2008/03/25 22:42:41 tgl Exp $ * best viewed with tabs set to 4 */ @@ -17,6 +17,7 @@ #endif #include "fmgr.h" +#include "utils/builtins.h" PG_MODULE_MAGIC; @@ -124,15 +125,8 @@ Datum chkpass_rout(PG_FUNCTION_ARGS) { chkpass *password = (chkpass *) PG_GETARG_POINTER(0); - text *result; - int slen; - slen = strlen(password->password); - result = (text *) palloc(VARHDRSZ + slen); - SET_VARSIZE(result, VARHDRSZ + slen); - memcpy(VARDATA(result), password->password, slen); - - PG_RETURN_TEXT_P(result); + PG_RETURN_TEXT_P(cstring_to_text(password->password)); } @@ -145,13 +139,10 @@ Datum chkpass_eq(PG_FUNCTION_ARGS) { chkpass *a1 = (chkpass *) PG_GETARG_POINTER(0); - text *a2 = (text *) PG_GETARG_TEXT_P(1); - char str[10]; - int sz; + text *a2 = PG_GETARG_TEXT_PP(1); + char str[9]; - sz = Min(VARSIZE(a2) - VARHDRSZ, 8); - memcpy(str, VARDATA(a2), sz); - str[sz] = '\0'; + text_to_cstring_buffer(a2, str, sizeof(str)); PG_RETURN_BOOL(strcmp(a1->password, crypt(str, a1->password)) == 0); } @@ -160,12 +151,9 @@ Datum chkpass_ne(PG_FUNCTION_ARGS) { chkpass *a1 = (chkpass *) PG_GETARG_POINTER(0); - text *a2 = (text *) PG_GETARG_TEXT_P(1); - char str[10]; - int sz; + text *a2 = PG_GETARG_TEXT_PP(1); + char str[9]; - sz = Min(VARSIZE(a2) - VARHDRSZ, 8); - memcpy(str, VARDATA(a2), sz); - str[sz] = '\0'; + text_to_cstring_buffer(a2, str, sizeof(str)); PG_RETURN_BOOL(strcmp(a1->password, crypt(str, a1->password)) != 0); } diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c index e09d6f16f69..a306a2a5af3 100644 --- a/contrib/dblink/dblink.c +++ b/contrib/dblink/dblink.c @@ -8,7 +8,7 @@ * Darko Prenosil * Shridhar Daithankar * - * $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.69 2008/01/14 02:49:47 tgl Exp $ + * $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.70 2008/03/25 22:42:41 tgl Exp $ * Copyright (c) 2001-2008, PostgreSQL Global Development Group * ALL RIGHTS RESERVED; * @@ -116,8 +116,6 @@ typedef struct remoteConnHashEnt #define NUMCONN 16 /* general utility */ -#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) -#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) #define xpfree(var_) \ do { \ if (var_ != NULL) \ @@ -171,7 +169,7 @@ typedef struct remoteConnHashEnt #define DBLINK_GET_CONN \ do { \ - char *conname_or_str = GET_STR(PG_GETARG_TEXT_P(0)); \ + char *conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(0)); \ rconn = getConnectionByName(conname_or_str); \ if(rconn) \ { \ @@ -197,7 +195,7 @@ typedef struct remoteConnHashEnt #define DBLINK_GET_NAMED_CONN \ do { \ - char *conname = GET_STR(PG_GETARG_TEXT_P(0)); \ + char *conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \ rconn = getConnectionByName(conname); \ if(rconn) \ conn = rconn->conn; \ @@ -234,11 +232,11 @@ dblink_connect(PG_FUNCTION_ARGS) if (PG_NARGS() == 2) { - connstr = GET_STR(PG_GETARG_TEXT_P(1)); - connname = GET_STR(PG_GETARG_TEXT_P(0)); + connstr = text_to_cstring(PG_GETARG_TEXT_PP(1)); + connname = text_to_cstring(PG_GETARG_TEXT_PP(0)); } else if (PG_NARGS() == 1) - connstr = GET_STR(PG_GETARG_TEXT_P(0)); + connstr = text_to_cstring(PG_GETARG_TEXT_PP(0)); oldcontext = MemoryContextSwitchTo(TopMemoryContext); @@ -272,7 +270,7 @@ dblink_connect(PG_FUNCTION_ARGS) else pconn->conn = conn; - PG_RETURN_TEXT_P(GET_TEXT("OK")); + PG_RETURN_TEXT_P(cstring_to_text("OK")); } /* @@ -290,7 +288,7 @@ dblink_disconnect(PG_FUNCTION_ARGS) if (PG_NARGS() == 1) { - conname = GET_STR(PG_GETARG_TEXT_P(0)); + conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); rconn = getConnectionByName(conname); if (rconn) conn = rconn->conn; @@ -310,7 +308,7 @@ dblink_disconnect(PG_FUNCTION_ARGS) else pconn->conn = NULL; - PG_RETURN_TEXT_P(GET_TEXT("OK")); + PG_RETURN_TEXT_P(cstring_to_text("OK")); } /* @@ -336,8 +334,8 @@ dblink_open(PG_FUNCTION_ARGS) if (PG_NARGS() == 2) { /* text,text */ - curname = GET_STR(PG_GETARG_TEXT_P(0)); - sql = GET_STR(PG_GETARG_TEXT_P(1)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(1)); rconn = pconn; } else if (PG_NARGS() == 3) @@ -345,25 +343,25 @@ dblink_open(PG_FUNCTION_ARGS) /* might be text,text,text or text,text,bool */ if (get_fn_expr_argtype(fcinfo->flinfo, 2) == BOOLOID) { - curname = GET_STR(PG_GETARG_TEXT_P(0)); - sql = GET_STR(PG_GETARG_TEXT_P(1)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(1)); fail = PG_GETARG_BOOL(2); rconn = pconn; } else { - conname = GET_STR(PG_GETARG_TEXT_P(0)); - curname = GET_STR(PG_GETARG_TEXT_P(1)); - sql = GET_STR(PG_GETARG_TEXT_P(2)); + conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(1)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(2)); rconn = getConnectionByName(conname); } } else if (PG_NARGS() == 4) { /* text,text,text,bool */ - conname = GET_STR(PG_GETARG_TEXT_P(0)); - curname = GET_STR(PG_GETARG_TEXT_P(1)); - sql = GET_STR(PG_GETARG_TEXT_P(2)); + conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(1)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(2)); fail = PG_GETARG_BOOL(3); rconn = getConnectionByName(conname); } @@ -403,12 +401,12 @@ dblink_open(PG_FUNCTION_ARGS) else { DBLINK_RES_ERROR_AS_NOTICE("sql error"); - PG_RETURN_TEXT_P(GET_TEXT("ERROR")); + PG_RETURN_TEXT_P(cstring_to_text("ERROR")); } } PQclear(res); - PG_RETURN_TEXT_P(GET_TEXT("OK")); + PG_RETURN_TEXT_P(cstring_to_text("OK")); } /* @@ -433,7 +431,7 @@ dblink_close(PG_FUNCTION_ARGS) if (PG_NARGS() == 1) { /* text */ - curname = GET_STR(PG_GETARG_TEXT_P(0)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(0)); rconn = pconn; } else if (PG_NARGS() == 2) @@ -441,22 +439,22 @@ dblink_close(PG_FUNCTION_ARGS) /* might be text,text or text,bool */ if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID) { - curname = GET_STR(PG_GETARG_TEXT_P(0)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(0)); fail = PG_GETARG_BOOL(1); rconn = pconn; } else { - conname = GET_STR(PG_GETARG_TEXT_P(0)); - curname = GET_STR(PG_GETARG_TEXT_P(1)); + conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(1)); rconn = getConnectionByName(conname); } } if (PG_NARGS() == 3) { /* text,text,bool */ - conname = GET_STR(PG_GETARG_TEXT_P(0)); - curname = GET_STR(PG_GETARG_TEXT_P(1)); + conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(1)); fail = PG_GETARG_BOOL(2); rconn = getConnectionByName(conname); } @@ -477,7 +475,7 @@ dblink_close(PG_FUNCTION_ARGS) else { DBLINK_RES_ERROR_AS_NOTICE("sql error"); - PG_RETURN_TEXT_P(GET_TEXT("ERROR")); + PG_RETURN_TEXT_P(cstring_to_text("ERROR")); } } @@ -500,7 +498,7 @@ dblink_close(PG_FUNCTION_ARGS) } } - PG_RETURN_TEXT_P(GET_TEXT("OK")); + PG_RETURN_TEXT_P(cstring_to_text("OK")); } /* @@ -535,8 +533,8 @@ dblink_fetch(PG_FUNCTION_ARGS) if (PG_NARGS() == 4) { /* text,text,int,bool */ - conname = GET_STR(PG_GETARG_TEXT_P(0)); - curname = GET_STR(PG_GETARG_TEXT_P(1)); + conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(1)); howmany = PG_GETARG_INT32(2); fail = PG_GETARG_BOOL(3); @@ -549,15 +547,15 @@ dblink_fetch(PG_FUNCTION_ARGS) /* text,text,int or text,int,bool */ if (get_fn_expr_argtype(fcinfo->flinfo, 2) == BOOLOID) { - curname = GET_STR(PG_GETARG_TEXT_P(0)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(0)); howmany = PG_GETARG_INT32(1); fail = PG_GETARG_BOOL(2); conn = pconn->conn; } else { - conname = GET_STR(PG_GETARG_TEXT_P(0)); - curname = GET_STR(PG_GETARG_TEXT_P(1)); + conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(1)); howmany = PG_GETARG_INT32(2); rconn = getConnectionByName(conname); @@ -568,7 +566,7 @@ dblink_fetch(PG_FUNCTION_ARGS) else if (PG_NARGS() == 2) { /* text,int */ - curname = GET_STR(PG_GETARG_TEXT_P(0)); + curname = text_to_cstring(PG_GETARG_TEXT_PP(0)); howmany = PG_GETARG_INT32(1); conn = pconn->conn; } @@ -769,7 +767,7 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get) { /* text,text,bool */ DBLINK_GET_CONN; - sql = GET_STR(PG_GETARG_TEXT_P(1)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(1)); fail = PG_GETARG_BOOL(2); } else if (PG_NARGS() == 2) @@ -778,20 +776,20 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get) if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID) { conn = pconn->conn; - sql = GET_STR(PG_GETARG_TEXT_P(0)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(0)); fail = PG_GETARG_BOOL(1); } else { DBLINK_GET_CONN; - sql = GET_STR(PG_GETARG_TEXT_P(1)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(1)); } } else if (PG_NARGS() == 1) { /* text */ conn = pconn->conn; - sql = GET_STR(PG_GETARG_TEXT_P(0)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(0)); } else /* shouldn't happen */ @@ -821,7 +819,7 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get) if (PG_NARGS() == 2) { DBLINK_GET_CONN; - sql = GET_STR(PG_GETARG_TEXT_P(1)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(1)); } else /* shouldn't happen */ @@ -1024,7 +1022,7 @@ dblink_get_connections(PG_FUNCTION_ARGS) { /* stash away current value */ astate = accumArrayResult(astate, - PointerGetDatum(GET_TEXT(hentry->name)), + CStringGetTextDatum(hentry->name), false, TEXTOID, CurrentMemoryContext); } } @@ -1087,9 +1085,9 @@ dblink_cancel_query(PG_FUNCTION_ARGS) PQfreeCancel(cancel); if (res == 1) - PG_RETURN_TEXT_P(GET_TEXT("OK")); + PG_RETURN_TEXT_P(cstring_to_text("OK")); else - PG_RETURN_TEXT_P(GET_TEXT(errbuf)); + PG_RETURN_TEXT_P(cstring_to_text(errbuf)); } @@ -1116,9 +1114,9 @@ dblink_error_message(PG_FUNCTION_ARGS) msg = PQerrorMessage(conn); if (msg == NULL || msg[0] == '\0') - PG_RETURN_TEXT_P(GET_TEXT("OK")); + PG_RETURN_TEXT_P(cstring_to_text("OK")); else - PG_RETURN_TEXT_P(GET_TEXT(msg)); + PG_RETURN_TEXT_P(cstring_to_text(msg)); } /* @@ -1146,7 +1144,7 @@ dblink_exec(PG_FUNCTION_ARGS) { /* must be text,text,bool */ DBLINK_GET_CONN; - sql = GET_STR(PG_GETARG_TEXT_P(1)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(1)); fail = PG_GETARG_BOOL(2); } else if (PG_NARGS() == 2) @@ -1155,20 +1153,20 @@ dblink_exec(PG_FUNCTION_ARGS) if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID) { conn = pconn->conn; - sql = GET_STR(PG_GETARG_TEXT_P(0)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(0)); fail = PG_GETARG_BOOL(1); } else { DBLINK_GET_CONN; - sql = GET_STR(PG_GETARG_TEXT_P(1)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(1)); } } else if (PG_NARGS() == 1) { /* must be single text argument */ conn = pconn->conn; - sql = GET_STR(PG_GETARG_TEXT_P(0)); + sql = text_to_cstring(PG_GETARG_TEXT_PP(0)); } else /* shouldn't happen */ @@ -1196,7 +1194,7 @@ dblink_exec(PG_FUNCTION_ARGS) * and save a copy of the command status string to return as our * result tuple */ - sql_cmd_status = GET_TEXT("ERROR"); + sql_cmd_status = cstring_to_text("ERROR"); } else if (PQresultStatus(res) == PGRES_COMMAND_OK) @@ -1210,7 +1208,7 @@ dblink_exec(PG_FUNCTION_ARGS) * and save a copy of the command status string to return as our * result tuple */ - sql_cmd_status = GET_TEXT(PQcmdStatus(res)); + sql_cmd_status = cstring_to_text(PQcmdStatus(res)); PQclear(res); } else @@ -1267,7 +1265,7 @@ dblink_get_pkey(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_TABLE), errmsg("relation \"%s\" does not exist", - GET_STR(PG_GETARG_TEXT_P(0))))); + text_to_cstring(PG_GETARG_TEXT_PP(0))))); /* * need a tuple descriptor representing one INT and one TEXT column @@ -1387,7 +1385,7 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_TABLE), errmsg("relation \"%s\" does not exist", - GET_STR(relname_text)))); + text_to_cstring(relname_text)))); /* * There should be at least one key attribute @@ -1443,7 +1441,7 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS) /* * And send it */ - PG_RETURN_TEXT_P(GET_TEXT(sql)); + PG_RETURN_TEXT_P(cstring_to_text(sql)); } @@ -1484,7 +1482,7 @@ dblink_build_sql_delete(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_TABLE), errmsg("relation \"%s\" does not exist", - GET_STR(relname_text)))); + text_to_cstring(relname_text)))); /* * There should be at least one key attribute @@ -1525,7 +1523,7 @@ dblink_build_sql_delete(PG_FUNCTION_ARGS) /* * And send it */ - PG_RETURN_TEXT_P(GET_TEXT(sql)); + PG_RETURN_TEXT_P(cstring_to_text(sql)); } @@ -1573,7 +1571,7 @@ dblink_build_sql_update(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_TABLE), errmsg("relation \"%s\" does not exist", - GET_STR(relname_text)))); + text_to_cstring(relname_text)))); /* * There should be one source array key values for each key attnum @@ -1629,7 +1627,7 @@ dblink_build_sql_update(PG_FUNCTION_ARGS) /* * And send it */ - PG_RETURN_TEXT_P(GET_TEXT(sql)); + PG_RETURN_TEXT_P(cstring_to_text(sql)); } /* @@ -1643,7 +1641,7 @@ Datum dblink_current_query(PG_FUNCTION_ARGS) { if (debug_query_string) - PG_RETURN_TEXT_P(GET_TEXT(debug_query_string)); + PG_RETURN_TEXT_P(cstring_to_text(debug_query_string)); else PG_RETURN_NULL(); } @@ -1763,8 +1761,7 @@ get_text_array_contents(ArrayType *array, int *numitems) } else { - values[i] = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(ptr))); + values[i] = TextDatumGetCString(PointerGetDatum(ptr)); ptr = att_addlength_pointer(ptr, typlen, ptr); ptr = (char *) att_align_nominal(ptr, typalign); } @@ -2026,9 +2023,10 @@ quote_literal_cstr(char *rawstr) text *result_text; char *result; - rawstr_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(rawstr))); - result_text = DatumGetTextP(DirectFunctionCall1(quote_literal, PointerGetDatum(rawstr_text))); - result = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(result_text))); + rawstr_text = cstring_to_text(rawstr); + result_text = DatumGetTextP(DirectFunctionCall1(quote_literal, + PointerGetDatum(rawstr_text))); + result = text_to_cstring(result_text); return result; } @@ -2044,9 +2042,10 @@ quote_ident_cstr(char *rawstr) text *result_text; char *result; - rawstr_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(rawstr))); - result_text = DatumGetTextP(DirectFunctionCall1(quote_ident, PointerGetDatum(rawstr_text))); - result = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(result_text))); + rawstr_text = cstring_to_text(rawstr); + result_text = DatumGetTextP(DirectFunctionCall1(quote_ident, + PointerGetDatum(rawstr_text))); + result = text_to_cstring(result_text); return result; } diff --git a/contrib/fuzzystrmatch/dmetaphone.c b/contrib/fuzzystrmatch/dmetaphone.c index acd9d0c6292..5bb07640bf6 100644 --- a/contrib/fuzzystrmatch/dmetaphone.c +++ b/contrib/fuzzystrmatch/dmetaphone.c @@ -1,7 +1,7 @@ /* * This is a port of the Double Metaphone algorithm for use in PostgreSQL. * - * $PostgreSQL: pgsql/contrib/fuzzystrmatch/dmetaphone.c,v 1.11 2007/02/27 23:48:05 tgl Exp $ + * $PostgreSQL: pgsql/contrib/fuzzystrmatch/dmetaphone.c,v 1.12 2008/03/25 22:42:41 tgl Exp $ * * Double Metaphone computes 2 "sounds like" strings - a primary and an * alternate. In most cases they are the same, but for foreign names @@ -101,7 +101,7 @@ The remaining code is authored by Andrew Dunstan and #include "postgres.h" -#include "fmgr.h" +#include "utils/builtins.h" /* turn off assertions for embedded function */ #define NDEBUG @@ -118,14 +118,12 @@ extern Datum dmetaphone(PG_FUNCTION_ARGS); extern Datum dmetaphone_alt(PG_FUNCTION_ARGS); /* prototype for the main function we got from the perl module */ -static void - DoubleMetaphone(char *, char **); +static void DoubleMetaphone(char *, char **); #ifndef DMETAPHONE_MAIN /* * The PostgreSQL visible dmetaphone function. - * */ PG_FUNCTION_INFO_V1(dmetaphone); @@ -133,47 +131,28 @@ PG_FUNCTION_INFO_V1(dmetaphone); Datum dmetaphone(PG_FUNCTION_ARGS) { - text *arg, - *result; - int alen, - rsize; + text *arg; char *aptr, *codes[2], - *code, - *rptr; + *code; #ifdef DMETAPHONE_NOSTRICT if (PG_ARGISNULL(0)) - PG_RETURNNULL(); + PG_RETURN_NULL(); #endif arg = PG_GETARG_TEXT_P(0); - alen = VARSIZE(arg) - VARHDRSZ; - - /* - * Postgres' string values might not have trailing nuls. The VARSIZE will - * not include the nul in any case so we copy things out and add a - * trailing nul. When we copy back we ignore the nul (and we don't make - * space for it). - */ - - aptr = palloc(alen + 1); - memcpy(aptr, VARDATA(arg), alen); - aptr[alen] = 0; + aptr = text_to_cstring(arg); + DoubleMetaphone(aptr, codes); code = codes[0]; if (!code) code = ""; - rsize = VARHDRSZ + strlen(code); - result = (text *) palloc(rsize); - rptr = VARDATA(result); - memcpy(rptr, code, rsize - VARHDRSZ); - SET_VARSIZE(result, rsize); - PG_RETURN_TEXT_P(result); + + PG_RETURN_TEXT_P(cstring_to_text(code)); } /* * The PostgreSQL visible dmetaphone_alt function. - * */ PG_FUNCTION_INFO_V1(dmetaphone_alt); @@ -181,34 +160,24 @@ PG_FUNCTION_INFO_V1(dmetaphone_alt); Datum dmetaphone_alt(PG_FUNCTION_ARGS) { - text *arg, - *result; - int alen, - rsize; + text *arg; char *aptr, *codes[2], - *code, - *rptr; + *code; #ifdef DMETAPHONE_NOSTRICT if (PG_ARGISNULL(0)) - PG_RETURNNULL(); + PG_RETURN_NULL(); #endif arg = PG_GETARG_TEXT_P(0); - alen = VARSIZE(arg) - VARHDRSZ; - aptr = palloc(alen + 1); - memcpy(aptr, VARDATA(arg), alen); - aptr[alen] = 0; + aptr = text_to_cstring(arg); + DoubleMetaphone(aptr, codes); code = codes[1]; if (!code) code = ""; - rsize = VARHDRSZ + strlen(code); - result = (text *) palloc(rsize); - rptr = VARDATA(result); - memcpy(rptr, code, rsize - VARHDRSZ); - SET_VARSIZE(result, rsize); - PG_RETURN_TEXT_P(result); + + PG_RETURN_TEXT_P(cstring_to_text(code)); } diff --git a/contrib/fuzzystrmatch/fuzzystrmatch.c b/contrib/fuzzystrmatch/fuzzystrmatch.c index 9f0898b4274..7a6aae039fe 100644 --- a/contrib/fuzzystrmatch/fuzzystrmatch.c +++ b/contrib/fuzzystrmatch/fuzzystrmatch.c @@ -5,7 +5,7 @@ * * Joe Conway * - * $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.c,v 1.25 2008/01/01 19:45:45 momjian Exp $ + * $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.c,v 1.26 2008/03/25 22:42:41 tgl Exp $ * Copyright (c) 2001-2008, PostgreSQL Global Development Group * ALL RIGHTS RESERVED; * @@ -56,11 +56,11 @@ PG_FUNCTION_INFO_V1(levenshtein); Datum levenshtein(PG_FUNCTION_ARGS) { - char *str_s; + char *str_s = TextDatumGetCString(PG_GETARG_DATUM(0)); + char *str_t = TextDatumGetCString(PG_GETARG_DATUM(1)); + int cols = strlen(str_s) + 1; + int rows = strlen(str_t) + 1; char *str_s0; - char *str_t; - int cols = 0; - int rows = 0; int *u_cells; int *l_cells; int *tmp; @@ -68,16 +68,10 @@ levenshtein(PG_FUNCTION_ARGS) int j; /* - * Fetch the arguments. str_s is referred to as the "source" cols = length - * of source + 1 to allow for the initialization column str_t is referred - * to as the "target", rows = length of target + 1 rows = length of target - * + 1 to allow for the initialization row + * str_s is referred to as the "source", str_t is referred to as the + * "target", cols = length of source + 1 to allow for the initialization + * column, rows = length of target + 1 to allow for the initialization row */ - str_s = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0)))); - str_t = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(1)))); - - cols = strlen(str_s) + 1; - rows = strlen(str_t) + 1; /* * Restrict the length of the strings being compared to something @@ -201,25 +195,19 @@ levenshtein(PG_FUNCTION_ARGS) * Returns number of characters requested * (suggested value is 4) */ -#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) - PG_FUNCTION_INFO_V1(metaphone); Datum metaphone(PG_FUNCTION_ARGS) { + char *str_i = TextDatumGetCString(PG_GETARG_DATUM(0)); + size_t str_i_len = strlen(str_i); int reqlen; - char *str_i; - size_t str_i_len; char *metaph; - text *result_text; int retval; - str_i = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0)))); - str_i_len = strlen(str_i); - /* return an empty string if we receive one */ if (!(str_i_len > 0)) - PG_RETURN_TEXT_P(GET_TEXT("")); + PG_RETURN_TEXT_P(cstring_to_text("")); if (str_i_len > MAX_METAPHONE_STRLEN) ereport(ERROR, @@ -247,18 +235,12 @@ metaphone(PG_FUNCTION_ARGS) retval = _metaphone(str_i, reqlen, &metaph); if (retval == META_SUCCESS) - { - result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(metaph))); - PG_RETURN_TEXT_P(result_text); - } + PG_RETURN_TEXT_P(cstring_to_text(metaph)); else { /* internal error */ elog(ERROR, "metaphone: failure"); - - /* - * Keep the compiler quiet - */ + /* keep the compiler quiet */ PG_RETURN_NULL(); } } @@ -695,11 +677,11 @@ soundex(PG_FUNCTION_ARGS) char outstr[SOUNDEX_LEN + 1]; char *arg; - arg = _textout(PG_GETARG_TEXT_P(0)); + arg = text_to_cstring(PG_GETARG_TEXT_P(0)); _soundex(arg, outstr); - PG_RETURN_TEXT_P(_textin(outstr)); + PG_RETURN_TEXT_P(cstring_to_text(outstr)); } static void @@ -761,8 +743,8 @@ difference(PG_FUNCTION_ARGS) int i, result; - _soundex(_textout(PG_GETARG_TEXT_P(0)), sndx1); - _soundex(_textout(PG_GETARG_TEXT_P(1)), sndx2); + _soundex(text_to_cstring(PG_GETARG_TEXT_P(0)), sndx1); + _soundex(text_to_cstring(PG_GETARG_TEXT_P(1)), sndx2); result = 0; for (i = 0; i < SOUNDEX_LEN; i++) diff --git a/contrib/fuzzystrmatch/fuzzystrmatch.h b/contrib/fuzzystrmatch/fuzzystrmatch.h index 73a88207ee5..22670ba9483 100644 --- a/contrib/fuzzystrmatch/fuzzystrmatch.h +++ b/contrib/fuzzystrmatch/fuzzystrmatch.h @@ -5,7 +5,7 @@ * * Joe Conway * - * $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.h,v 1.16 2008/01/01 19:45:45 momjian Exp $ + * $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.h,v 1.17 2008/03/25 22:42:41 tgl Exp $ * Copyright (c) 2001-2008, PostgreSQL Global Development Group * ALL RIGHTS RESERVED; * @@ -69,8 +69,6 @@ extern Datum difference(PG_FUNCTION_ARGS); static void _soundex(const char *instr, char *outstr); #define SOUNDEX_LEN 4 -#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) -#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */ static const char *soundex_table = "01230120022455012623010202"; diff --git a/contrib/hstore/hstore_op.c b/contrib/hstore/hstore_op.c index bcac30ee6fd..961abfe35c9 100644 --- a/contrib/hstore/hstore_op.c +++ b/contrib/hstore/hstore_op.c @@ -52,13 +52,12 @@ fetchval(PG_FUNCTION_ARGS) PG_RETURN_NULL(); } - out = palloc(VARHDRSZ + entry->vallen); - memcpy(VARDATA(out), STRPTR(hs) + entry->pos + entry->keylen, entry->vallen); - SET_VARSIZE(out, VARHDRSZ + entry->vallen); + out = cstring_to_text_with_len(STRPTR(hs) + entry->pos + entry->keylen, + entry->vallen); PG_FREE_IF_COPY(hs, 0); PG_FREE_IF_COPY(key, 1); - PG_RETURN_POINTER(out); + PG_RETURN_TEXT_P(out); } PG_FUNCTION_INFO_V1(exists); @@ -330,22 +329,19 @@ akeys(PG_FUNCTION_ARGS) d = (Datum *) palloc(sizeof(Datum) * (hs->size + 1)); while (ptr - ARRPTR(hs) < hs->size) { - text *item = (text *) palloc(VARHDRSZ + ptr->keylen); + text *item; - SET_VARSIZE(item, VARHDRSZ + ptr->keylen); - memcpy(VARDATA(item), base + ptr->pos, ptr->keylen); + item = cstring_to_text_with_len(base + ptr->pos, ptr->keylen); d[ptr - ARRPTR(hs)] = PointerGetDatum(item); ptr++; } - a = construct_array( - d, + a = construct_array(d, hs->size, TEXTOID, -1, false, - 'i' - ); + 'i'); ptr = ARRPTR(hs); while (ptr - ARRPTR(hs) < hs->size) @@ -374,23 +370,20 @@ avals(PG_FUNCTION_ARGS) d = (Datum *) palloc(sizeof(Datum) * (hs->size + 1)); while (ptr - ARRPTR(hs) < hs->size) { - int vallen = (ptr->valisnull) ? 0 : ptr->vallen; - text *item = (text *) palloc(VARHDRSZ + vallen); + text *item; - SET_VARSIZE(item, VARHDRSZ + vallen); - memcpy(VARDATA(item), base + ptr->pos + ptr->keylen, vallen); + item = cstring_to_text_with_len(base + ptr->pos + ptr->keylen, + (ptr->valisnull) ? 0 : ptr->vallen); d[ptr - ARRPTR(hs)] = PointerGetDatum(item); ptr++; } - a = construct_array( - d, + a = construct_array(d, hs->size, TEXTOID, -1, false, - 'i' - ); + 'i'); ptr = ARRPTR(hs); while (ptr - ARRPTR(hs) < hs->size) @@ -451,10 +444,10 @@ skeys(PG_FUNCTION_ARGS) if (st->i < st->hs->size) { HEntry *ptr = &(ARRPTR(st->hs)[st->i]); - text *item = (text *) palloc(VARHDRSZ + ptr->keylen); + text *item; - SET_VARSIZE(item, VARHDRSZ + ptr->keylen); - memcpy(VARDATA(item), STRPTR(st->hs) + ptr->pos, ptr->keylen); + item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos, + ptr->keylen); st->i++; SRF_RETURN_NEXT(funcctx, PointerGetDatum(item)); @@ -502,11 +495,10 @@ svals(PG_FUNCTION_ARGS) } else { - int vallen = ptr->vallen; - text *item = (text *) palloc(VARHDRSZ + vallen); + text *item; - SET_VARSIZE(item, VARHDRSZ + vallen); - memcpy(VARDATA(item), STRPTR(st->hs) + ptr->pos + ptr->keylen, vallen); + item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos + ptr->keylen, + ptr->vallen); st->i++; SRF_RETURN_NEXT(funcctx, PointerGetDatum(item)); @@ -617,9 +609,7 @@ each(PG_FUNCTION_ARGS) text *item; HeapTuple tuple; - item = (text *) palloc(VARHDRSZ + ptr->keylen); - SET_VARSIZE(item, VARHDRSZ + ptr->keylen); - memcpy(VARDATA(item), STRPTR(st->hs) + ptr->pos, ptr->keylen); + item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos, ptr->keylen); dvalues[0] = PointerGetDatum(item); if (ptr->valisnull) @@ -629,11 +619,8 @@ each(PG_FUNCTION_ARGS) } else { - int vallen = ptr->vallen; - - item = (text *) palloc(VARHDRSZ + vallen); - SET_VARSIZE(item, VARHDRSZ + vallen); - memcpy(VARDATA(item), STRPTR(st->hs) + ptr->pos + ptr->keylen, vallen); + item = cstring_to_text_with_len(STRPTR(st->hs) + ptr->pos + ptr->keylen, + ptr->vallen); dvalues[1] = PointerGetDatum(item); } st->i++; diff --git a/contrib/intarray/_int_bool.c b/contrib/intarray/_int_bool.c index 2344d0b50be..ef9430901dc 100644 --- a/contrib/intarray/_int_bool.c +++ b/contrib/intarray/_int_bool.c @@ -765,9 +765,7 @@ querytree(PG_FUNCTION_ARGS) if (len == 0) { - res = (text *) palloc(1 + VARHDRSZ); - SET_VARSIZE(res, 1 + VARHDRSZ); - *((char *) VARDATA(res)) = 'T'; + res = cstring_to_text("T"); } else { @@ -776,12 +774,9 @@ querytree(PG_FUNCTION_ARGS) nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen); *(nrm.cur) = '\0'; infix(&nrm, true); - - res = (text *) palloc(nrm.cur - nrm.buf + VARHDRSZ); - SET_VARSIZE(res, nrm.cur - nrm.buf + VARHDRSZ); - memcpy(VARDATA(res), nrm.buf, nrm.cur - nrm.buf); + res = cstring_to_text_with_len(nrm.buf, nrm.cur - nrm.buf); } pfree(q); - PG_RETURN_POINTER(res); + PG_RETURN_TEXT_P(res); } diff --git a/contrib/ltree/ltree_op.c b/contrib/ltree/ltree_op.c index 3eb854fbdc9..5a789865d8f 100644 --- a/contrib/ltree/ltree_op.c +++ b/contrib/ltree/ltree_op.c @@ -1,7 +1,7 @@ /* * op function for ltree * Teodor Sigaev - * $PostgreSQL: pgsql/contrib/ltree/ltree_op.c,v 1.17 2008/03/09 00:32:09 tgl Exp $ + * $PostgreSQL: pgsql/contrib/ltree/ltree_op.c,v 1.18 2008/03/25 22:42:41 tgl Exp $ */ #include "ltree.h" @@ -314,19 +314,15 @@ Datum ltree_addtext(PG_FUNCTION_ARGS) { ltree *a = PG_GETARG_LTREE(0); - text *b = PG_GETARG_TEXT_P(1); + text *b = PG_GETARG_TEXT_PP(1); char *s; ltree *r, *tmp; - s = (char *) palloc(VARSIZE(b) - VARHDRSZ + 1); - memcpy(s, VARDATA(b), VARSIZE(b) - VARHDRSZ); - s[VARSIZE(b) - VARHDRSZ] = '\0'; + s = text_to_cstring(b); - tmp = (ltree *) DatumGetPointer(DirectFunctionCall1( - ltree_in, - PointerGetDatum(s) - )); + tmp = (ltree *) DatumGetPointer(DirectFunctionCall1(ltree_in, + PointerGetDatum(s))); pfree(s); @@ -403,19 +399,15 @@ Datum ltree_textadd(PG_FUNCTION_ARGS) { ltree *a = PG_GETARG_LTREE(1); - text *b = PG_GETARG_TEXT_P(0); + text *b = PG_GETARG_TEXT_PP(0); char *s; ltree *r, *tmp; - s = (char *) palloc(VARSIZE(b) - VARHDRSZ + 1); - memcpy(s, VARDATA(b), VARSIZE(b) - VARHDRSZ); - s[VARSIZE(b) - VARHDRSZ] = '\0'; + s = text_to_cstring(b); - tmp = (ltree *) DatumGetPointer(DirectFunctionCall1( - ltree_in, - PointerGetDatum(s) - )); + tmp = (ltree *) DatumGetPointer(DirectFunctionCall1(ltree_in, + PointerGetDatum(s))); pfree(s); @@ -517,17 +509,14 @@ lca(PG_FUNCTION_ARGS) Datum text2ltree(PG_FUNCTION_ARGS) { - text *in = PG_GETARG_TEXT_P(0); - char *s = (char *) palloc(VARSIZE(in) - VARHDRSZ + 1); + text *in = PG_GETARG_TEXT_PP(0); + char *s; ltree *out; - memcpy(s, VARDATA(in), VARSIZE(in) - VARHDRSZ); - s[VARSIZE(in) - VARHDRSZ] = '\0'; + s = text_to_cstring(in); - out = (ltree *) DatumGetPointer(DirectFunctionCall1( - ltree_in, - PointerGetDatum(s) - )); + out = (ltree *) DatumGetPointer(DirectFunctionCall1(ltree_in, + PointerGetDatum(s))); pfree(s); PG_FREE_IF_COPY(in, 0); PG_RETURN_POINTER(out); diff --git a/contrib/pageinspect/heapfuncs.c b/contrib/pageinspect/heapfuncs.c index 3b82389729b..704161c716d 100644 --- a/contrib/pageinspect/heapfuncs.c +++ b/contrib/pageinspect/heapfuncs.c @@ -18,7 +18,7 @@ * Copyright (c) 2007-2008, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/contrib/pageinspect/heapfuncs.c,v 1.4 2008/01/01 20:31:21 tgl Exp $ + * $PostgreSQL: pgsql/contrib/pageinspect/heapfuncs.c,v 1.5 2008/03/25 22:42:41 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -36,8 +36,6 @@ Datum heap_page_items(PG_FUNCTION_ARGS); -#define GET_TEXT(str_) \ - DirectFunctionCall1(textin, CStringGetDatum(str_)) /* * bits_to_text @@ -190,8 +188,8 @@ heap_page_items(PG_FUNCTION_ARGS) bits_len = tuphdr->t_hoff - (((char *) tuphdr->t_bits) -((char *) tuphdr)); - values[11] = GET_TEXT( - bits_to_text(tuphdr->t_bits, bits_len * 8)); + values[11] = CStringGetTextDatum( + bits_to_text(tuphdr->t_bits, bits_len * 8)); } else nulls[11] = true; diff --git a/contrib/pageinspect/rawpage.c b/contrib/pageinspect/rawpage.c index 230d27133b8..319886d6e6d 100644 --- a/contrib/pageinspect/rawpage.c +++ b/contrib/pageinspect/rawpage.c @@ -8,7 +8,7 @@ * Copyright (c) 2007-2008, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/contrib/pageinspect/rawpage.c,v 1.4 2008/01/01 20:31:21 tgl Exp $ + * $PostgreSQL: pgsql/contrib/pageinspect/rawpage.c,v 1.5 2008/03/25 22:42:41 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -144,7 +144,7 @@ page_header(PG_FUNCTION_ARGS) lsn = PageGetLSN(page); snprintf(lsnchar, sizeof(lsnchar), "%X/%X", lsn.xlogid, lsn.xrecoff); - values[0] = DirectFunctionCall1(textin, CStringGetDatum(lsnchar)); + values[0] = CStringGetTextDatum(lsnchar); values[1] = UInt16GetDatum(PageGetTLI(page)); values[2] = UInt16GetDatum(page->pd_flags); values[3] = UInt16GetDatum(page->pd_lower); diff --git a/contrib/pgcrypto/pgcrypto.c b/contrib/pgcrypto/pgcrypto.c index 6e2963ff247..04c90d8672f 100644 --- a/contrib/pgcrypto/pgcrypto.c +++ b/contrib/pgcrypto/pgcrypto.c @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $PostgreSQL: pgsql/contrib/pgcrypto/pgcrypto.c,v 1.26 2007/02/27 23:48:06 tgl Exp $ + * $PostgreSQL: pgsql/contrib/pgcrypto/pgcrypto.c,v 1.27 2008/03/25 22:42:41 tgl Exp $ */ #include "postgres.h" @@ -35,6 +35,7 @@ #include "fmgr.h" #include "parser/scansup.h" +#include "utils/builtins.h" #include "px.h" #include "px-crypt.h" @@ -132,30 +133,20 @@ PG_FUNCTION_INFO_V1(pg_gen_salt); Datum pg_gen_salt(PG_FUNCTION_ARGS) { - text *arg0; + text *arg0 = PG_GETARG_TEXT_PP(0); int len; - text *res; char buf[PX_MAX_SALT_LEN + 1]; - arg0 = PG_GETARG_TEXT_P(0); - - len = VARSIZE(arg0) - VARHDRSZ; - len = len > PX_MAX_SALT_LEN ? PX_MAX_SALT_LEN : len; - memcpy(buf, VARDATA(arg0), len); - buf[len] = 0; + text_to_cstring_buffer(arg0, buf, sizeof(buf)); len = px_gen_salt(buf, buf, 0); if (len < 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("gen_salt: %s", px_strerror(len)))); - res = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(res, len + VARHDRSZ); - memcpy(VARDATA(res), buf, len); - PG_FREE_IF_COPY(arg0, 0); - PG_RETURN_TEXT_P(res); + PG_RETURN_TEXT_P(cstring_to_text_with_len(buf, len)); } /* SQL function: pg_gen_salt(text, int4) returns text */ @@ -164,32 +155,21 @@ PG_FUNCTION_INFO_V1(pg_gen_salt_rounds); Datum pg_gen_salt_rounds(PG_FUNCTION_ARGS) { - text *arg0; - int rounds; + text *arg0 = PG_GETARG_TEXT_PP(0); + int rounds = PG_GETARG_INT32(1); int len; - text *res; char buf[PX_MAX_SALT_LEN + 1]; - arg0 = PG_GETARG_TEXT_P(0); - rounds = PG_GETARG_INT32(1); - - len = VARSIZE(arg0) - VARHDRSZ; - len = len > PX_MAX_SALT_LEN ? PX_MAX_SALT_LEN : len; - memcpy(buf, VARDATA(arg0), len); - buf[len] = 0; + text_to_cstring_buffer(arg0, buf, sizeof(buf)); len = px_gen_salt(buf, buf, rounds); if (len < 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("gen_salt: %s", px_strerror(len)))); - res = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(res, len + VARHDRSZ); - memcpy(VARDATA(res), buf, len); - PG_FREE_IF_COPY(arg0, 0); - PG_RETURN_TEXT_P(res); + PG_RETURN_TEXT_P(cstring_to_text_with_len(buf, len)); } /* SQL function: pg_crypt(psw:text, salt:text) returns text */ @@ -198,30 +178,16 @@ PG_FUNCTION_INFO_V1(pg_crypt); Datum pg_crypt(PG_FUNCTION_ARGS) { - text *arg0; - text *arg1; - unsigned len0, - len1, - clen; + text *arg0 = PG_GETARG_TEXT_PP(0); + text *arg1 = PG_GETARG_TEXT_PP(1); char *buf0, *buf1, *cres, *resbuf; text *res; - arg0 = PG_GETARG_TEXT_P(0); - arg1 = PG_GETARG_TEXT_P(1); - len0 = VARSIZE(arg0) - VARHDRSZ; - len1 = VARSIZE(arg1) - VARHDRSZ; - - buf0 = palloc(len0 + 1); - buf1 = palloc(len1 + 1); - - memcpy(buf0, VARDATA(arg0), len0); - memcpy(buf1, VARDATA(arg1), len1); - - buf0[len0] = '\0'; - buf1[len1] = '\0'; + buf0 = text_to_cstring(arg0); + buf1 = text_to_cstring(arg1); resbuf = palloc0(PX_MAX_CRYPT); @@ -235,11 +201,8 @@ pg_crypt(PG_FUNCTION_ARGS) (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), errmsg("crypt(3) returned NULL"))); - clen = strlen(cres); + res = cstring_to_text(cres); - res = (text *) palloc(clen + VARHDRSZ); - SET_VARSIZE(res, clen + VARHDRSZ); - memcpy(VARDATA(res), cres, clen); pfree(resbuf); PG_FREE_IF_COPY(arg0, 0); diff --git a/contrib/spi/autoinc.c b/contrib/spi/autoinc.c index c748b6bf1c7..6ca09230a60 100644 --- a/contrib/spi/autoinc.c +++ b/contrib/spi/autoinc.c @@ -88,8 +88,7 @@ autoinc(PG_FUNCTION_ARGS) i++; chattrs[chnattrs] = attnum; - seqname = DirectFunctionCall1(textin, - CStringGetDatum(args[i])); + seqname = CStringGetTextDatum(args[i]); newvals[chnattrs] = DirectFunctionCall1(nextval, seqname); /* nextval now returns int64; coerce down to int32 */ newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs])); diff --git a/contrib/spi/insert_username.c b/contrib/spi/insert_username.c index 5e709f28343..9541677ebde 100644 --- a/contrib/spi/insert_username.c +++ b/contrib/spi/insert_username.c @@ -1,7 +1,7 @@ /* * insert_username.c * $Modified: Thu Oct 16 08:13:42 1997 by brook $ - * $PostgreSQL: pgsql/contrib/spi/insert_username.c,v 1.15 2007/02/01 19:10:23 momjian Exp $ + * $PostgreSQL: pgsql/contrib/spi/insert_username.c,v 1.16 2008/03/25 22:42:42 tgl Exp $ * * insert user name in response to a trigger * usage: insert_username (column_name) @@ -77,8 +77,7 @@ insert_username(PG_FUNCTION_ARGS) args[0], relname))); /* create fields containing name */ - newval = DirectFunctionCall1(textin, - CStringGetDatum(GetUserNameFromId(GetUserId()))); + newval = CStringGetTextDatum(GetUserNameFromId(GetUserId())); /* construct new tuple */ rettuple = SPI_modifytuple(rel, rettuple, 1, &attnum, &newval, NULL); diff --git a/contrib/spi/timetravel.c b/contrib/spi/timetravel.c index 4b238fa425f..0fd5cae09bb 100644 --- a/contrib/spi/timetravel.c +++ b/contrib/spi/timetravel.c @@ -172,7 +172,7 @@ timetravel(PG_FUNCTION_ARGS) } /* create fields containing name */ - newuser = DirectFunctionCall1(textin, CStringGetDatum(GetUserNameFromId(GetUserId()))); + newuser = CStringGetTextDatum(GetUserNameFromId(GetUserId())); nulltext = (Datum) NULL; diff --git a/contrib/sslinfo/sslinfo.c b/contrib/sslinfo/sslinfo.c index 7236203d7d8..a215abb8b6e 100644 --- a/contrib/sslinfo/sslinfo.c +++ b/contrib/sslinfo/sslinfo.c @@ -4,7 +4,7 @@ * Written by Victor B. Wagner , Cryptocom LTD * This file is distributed under BSD-style license. * - * $PostgreSQL: pgsql/contrib/sslinfo/sslinfo.c,v 1.6 2007/02/27 23:48:06 tgl Exp $ + * $PostgreSQL: pgsql/contrib/sslinfo/sslinfo.c,v 1.7 2008/03/25 22:42:42 tgl Exp $ */ #include "postgres.h" @@ -133,10 +133,7 @@ ASN1_STRING_to_text(ASN1_STRING *str) size - 1, PG_UTF8, GetDatabaseEncoding()); - outlen = strlen(dp); - result = palloc(VARHDRSZ + outlen); - memcpy(VARDATA(result), dp, outlen); - SET_VARSIZE(result, VARHDRSZ + outlen); + result = cstring_to_text(dp); if (dp != sp) pfree(dp); @@ -161,21 +158,12 @@ ASN1_STRING_to_text(ASN1_STRING *str) Datum X509_NAME_field_to_text(X509_NAME *name, text *fieldName) { - char *sp; char *string_fieldname; - char *dp; - size_t name_len = VARSIZE(fieldName) - VARHDRSZ; int nid, - index, - i; + index; ASN1_STRING *data; - string_fieldname = palloc(name_len + 1); - sp = VARDATA(fieldName); - dp = string_fieldname; - for (i = 0; i < name_len; i++) - *dp++ = *sp++; - *dp = '\0'; + string_fieldname = text_to_cstring(fieldName); nid = OBJ_txt2nid(string_fieldname); if (nid == NID_undef) ereport(ERROR, @@ -281,10 +269,8 @@ X509_NAME_to_text(X509_NAME *name) count = X509_NAME_entry_count(name); X509_NAME_ENTRY *e; ASN1_STRING *v; - const char *field_name; - size_t size, - outlen; + size_t size; char *sp; char *dp; text *result; @@ -314,10 +300,7 @@ X509_NAME_to_text(X509_NAME *name) GetDatabaseEncoding()); BIO_free(membuf); - outlen = strlen(dp); - result = palloc(VARHDRSZ + outlen); - memcpy(VARDATA(result), dp, outlen); - SET_VARSIZE(result, VARHDRSZ + outlen); + result = cstring_to_text(dp); /* * pg_do_encoding_conversion has annoying habit of returning source diff --git a/contrib/tablefunc/tablefunc.c b/contrib/tablefunc/tablefunc.c index bfac89e4146..793f26ed583 100644 --- a/contrib/tablefunc/tablefunc.c +++ b/contrib/tablefunc/tablefunc.c @@ -95,8 +95,6 @@ typedef struct char *lastrowid; /* rowid of the last tuple sent */ } crosstab_fctx; -#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) -#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) #define xpfree(var_) \ do { \ if (var_ != NULL) \ @@ -370,7 +368,7 @@ crosstab(PG_FUNCTION_ARGS) /* stuff done only on the first call of the function */ if (SRF_IS_FIRSTCALL()) { - char *sql = GET_STR(PG_GETARG_TEXT_P(0)); + char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0)); TupleDesc tupdesc; int ret; int proc; @@ -695,8 +693,8 @@ PG_FUNCTION_INFO_V1(crosstab_hash); Datum crosstab_hash(PG_FUNCTION_ARGS) { - char *sql = GET_STR(PG_GETARG_TEXT_P(0)); - char *cats_sql = GET_STR(PG_GETARG_TEXT_P(1)); + char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0)); + char *cats_sql = text_to_cstring(PG_GETARG_TEXT_PP(1)); ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; TupleDesc tupdesc; MemoryContext per_query_ctx; @@ -1052,10 +1050,10 @@ PG_FUNCTION_INFO_V1(connectby_text); Datum connectby_text(PG_FUNCTION_ARGS) { - char *relname = GET_STR(PG_GETARG_TEXT_P(0)); - char *key_fld = GET_STR(PG_GETARG_TEXT_P(1)); - char *parent_key_fld = GET_STR(PG_GETARG_TEXT_P(2)); - char *start_with = GET_STR(PG_GETARG_TEXT_P(3)); + char *relname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1)); + char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2)); + char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(3)); int max_depth = PG_GETARG_INT32(4); char *branch_delim = NULL; bool show_branch = false; @@ -1079,7 +1077,7 @@ connectby_text(PG_FUNCTION_ARGS) if (fcinfo->nargs == 6) { - branch_delim = GET_STR(PG_GETARG_TEXT_P(5)); + branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(5)); show_branch = true; } else @@ -1129,11 +1127,11 @@ PG_FUNCTION_INFO_V1(connectby_text_serial); Datum connectby_text_serial(PG_FUNCTION_ARGS) { - char *relname = GET_STR(PG_GETARG_TEXT_P(0)); - char *key_fld = GET_STR(PG_GETARG_TEXT_P(1)); - char *parent_key_fld = GET_STR(PG_GETARG_TEXT_P(2)); - char *orderby_fld = GET_STR(PG_GETARG_TEXT_P(3)); - char *start_with = GET_STR(PG_GETARG_TEXT_P(4)); + char *relname = text_to_cstring(PG_GETARG_TEXT_PP(0)); + char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1)); + char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2)); + char *orderby_fld = text_to_cstring(PG_GETARG_TEXT_PP(3)); + char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(4)); int max_depth = PG_GETARG_INT32(5); char *branch_delim = NULL; bool show_branch = false; @@ -1158,7 +1156,7 @@ connectby_text_serial(PG_FUNCTION_ARGS) if (fcinfo->nargs == 7) { - branch_delim = GET_STR(PG_GETARG_TEXT_P(6)); + branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(6)); show_branch = true; } else @@ -1645,9 +1643,10 @@ quote_literal_cstr(char *rawstr) text *result_text; char *result; - rawstr_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(rawstr))); - result_text = DatumGetTextP(DirectFunctionCall1(quote_literal, PointerGetDatum(rawstr_text))); - result = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(result_text))); + rawstr_text = cstring_to_text(rawstr); + result_text = DatumGetTextP(DirectFunctionCall1(quote_literal, + PointerGetDatum(rawstr_text))); + result = text_to_cstring(result_text); return result; } diff --git a/contrib/tsearch2/tsearch2.c b/contrib/tsearch2/tsearch2.c index 9dc5eb3bdbf..7754f574026 100644 --- a/contrib/tsearch2/tsearch2.c +++ b/contrib/tsearch2/tsearch2.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/contrib/tsearch2/tsearch2.c,v 1.5 2008/01/01 19:45:45 momjian Exp $ + * $PostgreSQL: pgsql/contrib/tsearch2/tsearch2.c,v 1.6 2008/03/25 22:42:42 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -41,14 +41,9 @@ static Oid current_parser_oid = InvalidOid; fcinfo->nargs++; \ } while (0) -#define TextPGetCString(t) \ - DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(t))) -#define CStringGetTextP(c) \ - DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(c))) - #define TextGetObjectId(infunction, text) \ DatumGetObjectId(DirectFunctionCall1(infunction, \ - DirectFunctionCall1(textout, PointerGetDatum(text)))) + CStringGetDatum(text_to_cstring(text)))) #define UNSUPPORTED_FUNCTION(name) \ Datum name(PG_FUNCTION_ARGS); \ @@ -151,7 +146,7 @@ UNSUPPORTED_FUNCTION(tsa_get_covers); Datum tsa_lexize_byname(PG_FUNCTION_ARGS) { - text *dictname = PG_GETARG_TEXT_P(0); + text *dictname = PG_GETARG_TEXT_PP(0); Datum arg1 = PG_GETARG_DATUM(1); return DirectFunctionCall2(ts_lexize, @@ -192,10 +187,10 @@ tsa_set_curdict(PG_FUNCTION_ARGS) Datum tsa_set_curdict_byname(PG_FUNCTION_ARGS) { - text *name = PG_GETARG_TEXT_P(0); + text *name = PG_GETARG_TEXT_PP(0); Oid dict_oid; - dict_oid = TSDictionaryGetDictid(stringToQualifiedNameList(TextPGetCString(name)), false); + dict_oid = TSDictionaryGetDictid(stringToQualifiedNameList(text_to_cstring(name)), false); current_dictionary_oid = dict_oid; @@ -231,10 +226,10 @@ tsa_set_curprs(PG_FUNCTION_ARGS) Datum tsa_set_curprs_byname(PG_FUNCTION_ARGS) { - text *name = PG_GETARG_TEXT_P(0); + text *name = PG_GETARG_TEXT_PP(0); Oid parser_oid; - parser_oid = TSParserGetPrsid(stringToQualifiedNameList(TextPGetCString(name)), false); + parser_oid = TSParserGetPrsid(stringToQualifiedNameList(text_to_cstring(name)), false); current_parser_oid = parser_oid; @@ -272,10 +267,10 @@ tsa_set_curcfg(PG_FUNCTION_ARGS) Datum tsa_set_curcfg_byname(PG_FUNCTION_ARGS) { - text *arg0 = PG_GETARG_TEXT_P(0); + text *arg0 = PG_GETARG_TEXT_PP(0); char *name; - name = TextPGetCString(arg0); + name = text_to_cstring(arg0); set_config_option("default_text_search_config", name, PGC_USERSET, @@ -290,7 +285,7 @@ tsa_set_curcfg_byname(PG_FUNCTION_ARGS) Datum tsa_to_tsvector_name(PG_FUNCTION_ARGS) { - text *cfgname = PG_GETARG_TEXT_P(0); + text *cfgname = PG_GETARG_TEXT_PP(0); Datum arg1 = PG_GETARG_DATUM(1); Oid config_oid; @@ -304,7 +299,7 @@ tsa_to_tsvector_name(PG_FUNCTION_ARGS) Datum tsa_to_tsquery_name(PG_FUNCTION_ARGS) { - text *cfgname = PG_GETARG_TEXT_P(0); + text *cfgname = PG_GETARG_TEXT_PP(0); Datum arg1 = PG_GETARG_DATUM(1); Oid config_oid; @@ -319,7 +314,7 @@ tsa_to_tsquery_name(PG_FUNCTION_ARGS) Datum tsa_plainto_tsquery_name(PG_FUNCTION_ARGS) { - text *cfgname = PG_GETARG_TEXT_P(0); + text *cfgname = PG_GETARG_TEXT_PP(0); Datum arg1 = PG_GETARG_DATUM(1); Oid config_oid; @@ -341,7 +336,7 @@ tsa_headline_byname(PG_FUNCTION_ARGS) /* first parameter has to be converted to oid */ config_oid = DatumGetObjectId(DirectFunctionCall1(regconfigin, - DirectFunctionCall1(textout, arg0))); + CStringGetDatum(TextDatumGetCString(arg0)))); if (PG_NARGS() == 3) result = DirectFunctionCall3(ts_headline_byid, diff --git a/contrib/uuid-ossp/uuid-ossp.c b/contrib/uuid-ossp/uuid-ossp.c index 456ec5728b8..7bdf5ee5140 100644 --- a/contrib/uuid-ossp/uuid-ossp.c +++ b/contrib/uuid-ossp/uuid-ossp.c @@ -4,7 +4,7 @@ * * Copyright (c) 2007-2008 PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/contrib/uuid-ossp/uuid-ossp.c,v 1.7 2008/01/01 20:31:21 tgl Exp $ + * $PostgreSQL: pgsql/contrib/uuid-ossp/uuid-ossp.c,v 1.8 2008/03/25 22:42:42 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -214,7 +214,7 @@ uuid_generate_v35_internal(int mode, pg_uuid_t *ns, text *name) result = uuid_generate_internal(mode, ns_uuid, - DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(name)))); + text_to_cstring(name)); rc = uuid_destroy(ns_uuid); if (rc != UUID_RC_OK) diff --git a/contrib/xml2/xpath.c b/contrib/xml2/xpath.c index b02f9577772..52cc3d379fd 100644 --- a/contrib/xml2/xpath.c +++ b/contrib/xml2/xpath.c @@ -55,11 +55,6 @@ Datum xpath_table(PG_FUNCTION_ARGS); char *errbuf; /* per line error buffer */ char *pgxml_errorMsg = NULL; /* overall error message */ -/* Convenience macros */ - -#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) -#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) - #define ERRBUF_SIZE 200 /* memory handling passthrough functions (e.g. palloc, pstrdup are @@ -651,11 +646,11 @@ xpath_table(PG_FUNCTION_ARGS) MemoryContext oldcontext; /* Function parameters */ - char *pkeyfield = GET_STR(PG_GETARG_TEXT_P(0)); - char *xmlfield = GET_STR(PG_GETARG_TEXT_P(1)); - char *relname = GET_STR(PG_GETARG_TEXT_P(2)); - char *xpathset = GET_STR(PG_GETARG_TEXT_P(3)); - char *condition = GET_STR(PG_GETARG_TEXT_P(4)); + char *pkeyfield = text_to_cstring(PG_GETARG_TEXT_PP(0)); + char *xmlfield = text_to_cstring(PG_GETARG_TEXT_PP(1)); + char *relname = text_to_cstring(PG_GETARG_TEXT_PP(2)); + char *xpathset = text_to_cstring(PG_GETARG_TEXT_PP(3)); + char *condition = text_to_cstring(PG_GETARG_TEXT_PP(4)); char **values; xmlChar **xpaths; diff --git a/contrib/xml2/xslt_proc.c b/contrib/xml2/xslt_proc.c index b89102245c5..f15fabcb3c5 100644 --- a/contrib/xml2/xslt_proc.c +++ b/contrib/xml2/xslt_proc.c @@ -22,13 +22,10 @@ /* declarations to come from xpath.c */ - extern void elog_error(int level, char *explain, int force); extern void pgxml_parser_init(); extern xmlChar *pgxml_texttoxmlchar(text *textstring); -#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) - /* local defs */ static void parse_params(const char **params, text *paramstr); @@ -76,7 +73,7 @@ xslt_process(PG_FUNCTION_ARGS) if (VARDATA(doct)[0] == '<') doctree = xmlParseMemory((char *) VARDATA(doct), VARSIZE(doct) - VARHDRSZ); else - doctree = xmlParseFile(GET_STR(doct)); + doctree = xmlParseFile(text_to_cstring(doct)); if (doctree == NULL) { @@ -102,7 +99,7 @@ xslt_process(PG_FUNCTION_ARGS) stylesheet = xsltParseStylesheetDoc(ssdoc); } else - stylesheet = xsltParseStylesheetFile((xmlChar *) GET_STR(ssheet)); + stylesheet = xsltParseStylesheetFile((xmlChar *) text_to_cstring(ssheet)); if (stylesheet == NULL) @@ -145,7 +142,7 @@ parse_params(const char **params, text *paramstr) char *nvsep = "="; char *itsep = ","; - pstr = GET_STR(paramstr); + pstr = text_to_cstring(paramstr); pos = pstr; -- cgit v1.2.3