diff options
author | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2014-09-25 16:32:27 +0300 |
---|---|---|
committer | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2014-09-25 16:36:58 +0300 |
commit | 1dcfb8da09c47d2a7502d1dfab06c8be4b6cf323 (patch) | |
tree | f99b357968ff2c075d11e00d9912b8101e59f0cc /contrib/pgcrypto/pgp-pgsql.c | |
parent | 56a312aac8802c2aa01530101d2d19e63568eeda (diff) | |
download | postgresql-1dcfb8da09c47d2a7502d1dfab06c8be4b6cf323.tar.gz postgresql-1dcfb8da09c47d2a7502d1dfab06c8be4b6cf323.zip |
Refactor space allocation for base64 encoding/decoding in pgcrypto.
Instead of trying to accurately calculate the space needed, use a StringInfo
that's enlarged as needed. This is just moving things around currently - the
old code was not wrong - but this is in preparation for a patch that adds
support for extra armor headers, and would make the space calculation more
complicated.
Marko Tiikkaja
Diffstat (limited to 'contrib/pgcrypto/pgp-pgsql.c')
-rw-r--r-- | contrib/pgcrypto/pgp-pgsql.c | 47 |
1 files changed, 21 insertions, 26 deletions
diff --git a/contrib/pgcrypto/pgp-pgsql.c b/contrib/pgcrypto/pgp-pgsql.c index ad1fd084276..5d2d4655d18 100644 --- a/contrib/pgcrypto/pgp-pgsql.c +++ b/contrib/pgcrypto/pgp-pgsql.c @@ -31,6 +31,7 @@ #include "postgres.h" +#include "lib/stringinfo.h" #include "mb/pg_wchar.h" #include "utils/builtins.h" @@ -820,23 +821,20 @@ pg_armor(PG_FUNCTION_ARGS) { bytea *data; text *res; - int data_len, - res_len, - guess_len; + int data_len; + StringInfoData buf; data = PG_GETARG_BYTEA_P(0); data_len = VARSIZE(data) - VARHDRSZ; - guess_len = pgp_armor_enc_len(data_len); - res = palloc(VARHDRSZ + guess_len); + initStringInfo(&buf); - res_len = pgp_armor_encode((uint8 *) VARDATA(data), data_len, - (uint8 *) VARDATA(res)); - if (res_len > guess_len) - ereport(ERROR, - (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), - errmsg("Overflow - encode estimate too small"))); - SET_VARSIZE(res, VARHDRSZ + res_len); + pgp_armor_encode((uint8 *) VARDATA(data), data_len, &buf); + + res = palloc(VARHDRSZ + buf.len); + SET_VARSIZE(res, VARHDRSZ + buf.len); + memcpy(VARDATA(res), buf.data, buf.len); + pfree(buf.data); PG_FREE_IF_COPY(data, 0); PG_RETURN_TEXT_P(res); @@ -847,27 +845,24 @@ pg_dearmor(PG_FUNCTION_ARGS) { text *data; bytea *res; - int data_len, - res_len, - guess_len; + int data_len; + int ret; + StringInfoData buf; data = PG_GETARG_TEXT_P(0); data_len = VARSIZE(data) - VARHDRSZ; - guess_len = pgp_armor_dec_len(data_len); - res = palloc(VARHDRSZ + guess_len); + initStringInfo(&buf); - res_len = pgp_armor_decode((uint8 *) VARDATA(data), data_len, - (uint8 *) VARDATA(res)); - if (res_len < 0) + ret = pgp_armor_decode((uint8 *) VARDATA(data), data_len, &buf); + if (ret < 0) ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), - errmsg("%s", px_strerror(res_len)))); - if (res_len > guess_len) - ereport(ERROR, - (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION), - errmsg("Overflow - decode estimate too small"))); - SET_VARSIZE(res, VARHDRSZ + res_len); + errmsg("%s", px_strerror(ret)))); + res = palloc(VARHDRSZ + buf.len); + SET_VARSIZE(res, VARHDRSZ + buf.len); + memcpy(VARDATA(res), buf.data, buf.len); + pfree(buf.data); PG_FREE_IF_COPY(data, 0); PG_RETURN_TEXT_P(res); |