diff options
author | Neil Conway <neilc@samurai.com> | 2005-05-20 01:29:56 +0000 |
---|---|---|
committer | Neil Conway <neilc@samurai.com> | 2005-05-20 01:29:56 +0000 |
commit | f3567eeaf2423c933166edff320ab1a7093f1ac4 (patch) | |
tree | 6a77859582d83cf61881f26fdf3a4f3d5e4aa293 /src/backend | |
parent | ff0c143a3b6a2f0d091133fe64d23544f1157096 (diff) | |
download | postgresql-f3567eeaf2423c933166edff320ab1a7093f1ac4.tar.gz postgresql-f3567eeaf2423c933166edff320ab1a7093f1ac4.zip |
Implement md5(bytea), update regression tests and documentation. Patch
from Abhijit Menon-Sen, minor editorialization from Neil Conway. Also,
improve md5(text) to allocate a constant-sized buffer on the stack
rather than via palloc.
Catalog version bumped.
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/utils/adt/varlena.c | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 0022ab5effd..14dfcb6335e 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.120 2005/05/01 18:56:18 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.121 2005/05/20 01:29:55 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -2308,15 +2308,12 @@ md5_text(PG_FUNCTION_ARGS) { text *in_text = PG_GETARG_TEXT_P(0); size_t len; - char *hexsum; + char hexsum[MD5_HASH_LEN + 1]; text *result_text; /* Calculate the length of the buffer using varlena metadata */ len = VARSIZE(in_text) - VARHDRSZ; - /* leave room for the terminating '\0' */ - hexsum = (char *) palloc(MD5_HASH_LEN + 1); - /* get the hash result */ if (md5_hash(VARDATA(in_text), len, hexsum) == false) ereport(ERROR, @@ -2327,3 +2324,25 @@ md5_text(PG_FUNCTION_ARGS) result_text = PG_STR_GET_TEXT(hexsum); PG_RETURN_TEXT_P(result_text); } + +/* + * Create an md5 hash of a bytea field and return it as a hex string: + * 16-byte md5 digest is represented in 32 hex characters. + */ +Datum +md5_bytea(PG_FUNCTION_ARGS) +{ + bytea *in = PG_GETARG_BYTEA_P(0); + size_t len; + char hexsum[MD5_HASH_LEN + 1]; + text *result_text; + + len = VARSIZE(in) - VARHDRSZ; + if (md5_hash(VARDATA(in), len, hexsum) == false) + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); + + result_text = PG_STR_GET_TEXT(hexsum); + PG_RETURN_TEXT_P(result_text); +} |