From f989be4e90c19c959d03867c9ad43a52b27d9447 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Thu, 28 Jan 2021 18:42:28 +0200
Subject: [PATCH 1/1] Custom encoding conversion routine, for testing.

Note: this routine uses the v13 signature, so it won't load into a v14
cluster. This is because I wrote this to test the check in pg_upgrade,
and in order to test that, the encoding conversion needs to be installed
in the old cluster.
---
 contrib/test_conversion/Makefile              | 17 +++++++
 .../test_conversion/test_conversion--1.0.sql  | 14 ++++++
 contrib/test_conversion/test_conversion.c     | 49 +++++++++++++++++++
 .../test_conversion/test_conversion.control   |  6 +++
 4 files changed, 86 insertions(+)
 create mode 100644 contrib/test_conversion/Makefile
 create mode 100644 contrib/test_conversion/test_conversion--1.0.sql
 create mode 100644 contrib/test_conversion/test_conversion.c
 create mode 100644 contrib/test_conversion/test_conversion.control

diff --git a/contrib/test_conversion/Makefile b/contrib/test_conversion/Makefile
new file mode 100644
index 00000000000..3e27f4db3a7
--- /dev/null
+++ b/contrib/test_conversion/Makefile
@@ -0,0 +1,17 @@
+MODULE_big = test_conversion
+OBJS = test_conversion.o
+
+EXTENSION = test_conversion
+DATA = test_conversion--1.0.sql
+PGFILEDESC = "test_conversion - sample user-defined encoding conversion"
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = contrib/test_conversion
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/contrib/test_conversion/test_conversion--1.0.sql b/contrib/test_conversion/test_conversion--1.0.sql
new file mode 100644
index 00000000000..1c45e461b3b
--- /dev/null
+++ b/contrib/test_conversion/test_conversion--1.0.sql
@@ -0,0 +1,14 @@
+/* contrib/test_conversion/test_conversion--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION test_conversion" to load this file. \quit
+
+CREATE FUNCTION latin1_to_latin2(
+    INTEGER,  -- source encoding id
+    INTEGER,  -- destination encoding id
+    CSTRING,  -- source string (null terminated C string)
+    CSTRING,  -- destination string (null terminated C string)
+    INTEGER   -- source string length
+) returns VOID IMMUTABLE STRICT;
+
+CREATE DEFAULT CONVERSION FOR 'latin1' TO 'latin2' FROM 'latin1_to_latin2';
diff --git a/contrib/test_conversion/test_conversion.c b/contrib/test_conversion/test_conversion.c
new file mode 100644
index 00000000000..d91bd4e273d
--- /dev/null
+++ b/contrib/test_conversion/test_conversion.c
@@ -0,0 +1,49 @@
+/*-------------------------------------------------------------------------
+ *
+ *	  test_conversion
+ *
+ * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *	  contrib/test_conversion/test_conversion.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+#include "fmgr.h"
+#include "mb/pg_wchar.h"
+
+PG_MODULE_MAGIC;
+
+PG_FUNCTION_INFO_V1(latin1_to_latin2);
+
+/* ----------
+ * conv_proc(
+ *		INTEGER,	-- source encoding id
+ *		INTEGER,	-- destination encoding id
+ *		CSTRING,	-- source string (null terminated C string)
+ *		CSTRING,	-- destination string (null terminated C string)
+ *		INTEGER		-- source string length
+ * ) returns VOID;
+ * ----------
+ */
+
+
+Datum
+latin1_to_latin2(PG_FUNCTION_ARGS)
+{
+	unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
+	unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
+	int			len = PG_GETARG_INT32(4);
+
+	CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN1, PG_LATIN2);
+
+	for (int i = 0; i < len; i++)
+	{
+		dest[i] = src[len - i - 1];
+	}
+
+	PG_RETURN_VOID();
+}
diff --git a/contrib/test_conversion/test_conversion.control b/contrib/test_conversion/test_conversion.control
new file mode 100644
index 00000000000..e9f264825f4
--- /dev/null
+++ b/contrib/test_conversion/test_conversion.control
@@ -0,0 +1,6 @@
+# test_converion
+
+comment = 'sample encoding conversion function'
+default_version = '1.0'
+module_pathname = '$libdir/test_conversion'
+relocatable = true
-- 
2.29.2

