From f3567eeaf2423c933166edff320ab1a7093f1ac4 Mon Sep 17 00:00:00 2001
From: Neil Conway <neilc@samurai.com>
Date: Fri, 20 May 2005 01:29:56 +0000
Subject: [PATCH] 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.
---
 doc/src/sgml/func.sgml                | 13 ++++++++-
 src/backend/utils/adt/varlena.c       | 29 ++++++++++++++----
 src/include/catalog/catversion.h      |  4 +--
 src/include/catalog/pg_proc.h         |  4 ++-
 src/include/utils/builtins.h          |  3 +-
 src/test/regress/expected/strings.out | 42 +++++++++++++++++++++++++++
 src/test/regress/sql/strings.sql      | 14 +++++++++
 7 files changed, 99 insertions(+), 10 deletions(-)

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 1b031c0c315..8c6eebc86c5 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.247 2005/05/11 13:58:50 neilc Exp $
+$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.248 2005/05/20 01:29:55 neilc Exp $
 PostgreSQL documentation
 -->
 
@@ -2328,6 +2328,17 @@ PostgreSQL documentation
       <entry><literal>5</literal></entry>
      </row>
 
+     <row>
+      <entry><literal><function>md5</function>(<parameter>string</parameter>)</literal></entry>
+      <entry><type>text</type></entry>
+      <entry>
+       Calculates the MD5 hash of <parameter>string</parameter>,
+       returning the result in hexadecimal.
+      </entry>
+      <entry><literal>md5('Th\\000omas'::bytea)</literal></entry>
+      <entry><literal>8ab2d3c9689aaf18 b4958c334c82d8b1</literal></entry>
+     </row>
+
      <row>
       <entry>
        <literal><function>decode</function>(<parameter>string</parameter> <type>text</type>,
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);
+}
diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h
index dd1c29ab02b..97817e92298 100644
--- a/src/include/catalog/catversion.h
+++ b/src/include/catalog/catversion.h
@@ -37,7 +37,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.268 2005/05/17 21:46:10 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.269 2005/05/20 01:29:55 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,6 +53,6 @@
  */
 
 /*							yyyymmddN */
-#define CATALOG_VERSION_NO	200505171
+#define CATALOG_VERSION_NO	200505201
 
 #endif
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index a3f50ea8902..ebf15f8c199 100644
--- a/src/include/catalog/pg_proc.h
+++ b/src/include/catalog/pg_proc.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.362 2005/05/17 21:46:10 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.363 2005/05/20 01:29:55 neilc Exp $
  *
  * NOTES
  *	  The script catalog/genbki.sh reads this file and generates .bki
@@ -3269,6 +3269,8 @@ DESCR("I/O");
 /* cryptographic */
 DATA(insert OID =  2311 (  md5	   PGNSP PGUID 12 f f t f i 1 25 "25" _null_ _null_ _null_  md5_text - _null_ ));
 DESCR("calculates md5 hash");
+DATA(insert OID =  2321 (  md5	   PGNSP PGUID 12 f f t f i 1 25 "17" _null_ _null_ _null_  md5_bytea - _null_ ));
+DESCR("calculates md5 hash");
 
 /* crosstype operations for date vs. timestamp and timestamptz */
 DATA(insert OID = 2338 (  date_lt_timestamp		   PGNSP PGUID 12 f f t f i 2 16 "1082 1114" _null_ _null_ _null_ date_lt_timestamp - _null_ ));
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index 96e33786eec..865c673f512 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.255 2005/04/12 04:26:32 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.256 2005/05/20 01:29:55 neilc Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -572,6 +572,7 @@ extern Datum array_to_text(PG_FUNCTION_ARGS);
 extern Datum to_hex32(PG_FUNCTION_ARGS);
 extern Datum to_hex64(PG_FUNCTION_ARGS);
 extern Datum md5_text(PG_FUNCTION_ARGS);
+extern Datum md5_bytea(PG_FUNCTION_ARGS);
 
 extern Datum unknownin(PG_FUNCTION_ARGS);
 extern Datum unknownout(PG_FUNCTION_ARGS);
diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out
index 3421b875227..ab4cd6a7979 100644
--- a/src/test/regress/expected/strings.out
+++ b/src/test/regress/expected/strings.out
@@ -825,3 +825,45 @@ select md5('12345678901234567890123456789012345678901234567890123456789012345678
  t
 (1 row)
 
+select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/strings.sql
index d2368d6ba53..a59b39cf99e 100644
--- a/src/test/regress/sql/strings.sql
+++ b/src/test/regress/sql/strings.sql
@@ -331,3 +331,17 @@ select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS
 select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
 
 select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
+
+select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
+
+select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
+
+select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
+
+select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
+
+select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
+
+select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
+
+select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
-- 
GitLab