git.postgresql.org Git - postgresql.git/commitdiff

git projects / postgresql.git / commitdiff
? search:
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ff0c143)
Implement md5(bytea), update regression tests and documentation. Patch
2005年5月20日 01:29:56 +0000 (01:29 +0000)
2005年5月20日 01:29:56 +0000 (01:29 +0000)
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.


diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 1b031c0c31524fc2276f44239f43d54844640885..8c6eebc86c588365abe4e87531ea48cd79537aa0 100644 (file)
--- 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\000円omas'::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 0022ab5effd2962355f927e71d74a714f19a4608..14dfcb6335e2c4796ce0589c554b5214115ec1b9 100644 (file)
--- 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 dd1c29ab02b906bf3bd44384661d8a430c668654..97817e9229820fd612a1fc7e8e270e8dac63f100 100644 (file)
--- 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 a3f50ea89024ffc83138c34e0ed90ff95161cf6c..ebf15f8c199b25ee9b42e3f438b57980d74f9633 100644 (file)
--- 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 96e33786eec36e3ccb366b9d52805ec8b91f1c55..865c673f512419be6becfafd06b1e80a20f6bfb3 100644 (file)
--- 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 3421b875227e933d291cacca1d9045bed06c61a5..ab4cd6a7979b16dbd288cda61ec56dabf3b66c8d 100644 (file)
--- 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 d2368d6ba534ab537f868996e5238ed285e684dd..a59b39cf99ef5e360fb1fe1120b357a11c733c30 100644 (file)
--- 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";
This is the main PostgreSQL git repository.
RSS Atom

AltStyle によって変換されたページ (->オリジナル) /