From 193ce8529d885672266969260b70c9bb7ae6db82 Mon Sep 17 00:00:00 2001 From: Ilya Staroverov Date: Fri, 27 Nov 2026 09:12:02 +0300 Subject: [PATCH v1 1/2] Make built-in local-to-UTF8 conversions available in the backend Move the forward maps into the backend and have the existing conversion modules use them. This lets logging convert before catalog access is available, while keeping a single copy of each table. SQL conversions continue to use the existing conversion functions and error handling. Author: Ilya Staroverov --- src/backend/utils/mb/Makefile | 1 + .../utf8_and_big5/utf8_and_big5.c | 8 +- .../utf8_and_cyrillic/utf8_and_cyrillic.c | 16 +- .../utf8_and_euc2004/utf8_and_euc2004.c | 8 +- .../utf8_and_euc_cn/utf8_and_euc_cn.c | 8 +- .../utf8_and_euc_jp/utf8_and_euc_jp.c | 8 +- .../utf8_and_euc_kr/utf8_and_euc_kr.c | 8 +- .../utf8_and_euc_tw/utf8_and_euc_tw.c | 8 +- .../utf8_and_gb18030/utf8_and_gb18030.c | 95 +-------- .../utf8_and_gbk/utf8_and_gbk.c | 8 +- .../utf8_and_iso8859/utf8_and_iso8859.c | 60 ++---- .../utf8_and_iso8859_1/utf8_and_iso8859_1.c | 25 +-- .../utf8_and_johab/utf8_and_johab.c | 8 +- .../utf8_and_sjis/utf8_and_sjis.c | 8 +- .../utf8_and_sjis2004/utf8_and_sjis2004.c | 8 +- .../utf8_and_uhc/utf8_and_uhc.c | 8 +- .../utf8_and_win/utf8_and_win.c | 41 ++-- src/backend/utils/mb/gb18030.h | 34 +++ src/backend/utils/mb/local_utf8.c | 200 ++++++++++++++++++ src/backend/utils/mb/meson.build | 1 + src/include/mb/pg_wchar.h | 2 + 21 files changed, 281 insertions(+), 282 deletions(-) create mode 100644 src/backend/utils/mb/gb18030.h create mode 100644 src/backend/utils/mb/local_utf8.c diff --git a/src/backend/utils/mb/Makefile b/src/backend/utils/mb/Makefile index bbde71b5aa..a7a8359f28 100644 --- a/src/backend/utils/mb/Makefile +++ b/src/backend/utils/mb/Makefile @@ -14,6 +14,7 @@ include $(top_builddir)/src/Makefile.global OBJS = \ conv.o \ + local_utf8.o \ mbutils.o \ stringinfo_mb.o \ wstrcmp.o \ diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c b/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c index e66c626924..2498f86d79 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/big5_to_utf8.map" #include "../../Unicode/utf8_to_big5.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ big5_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_BIG5, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &big5_to_unicode_tree, - NULL, 0, - NULL, - PG_BIG5, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_BIG5, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c b/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c index 3b116cdd83..e8a77d4aaa 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c @@ -15,9 +15,7 @@ #include "fmgr.h" #include "mb/pg_wchar.h" #include "../../Unicode/utf8_to_koi8r.map" -#include "../../Unicode/koi8r_to_utf8.map" #include "../../Unicode/utf8_to_koi8u.map" -#include "../../Unicode/koi8u_to_utf8.map" PG_MODULE_MAGIC_EXT( .name = "utf8_and_cyrillic", @@ -76,12 +74,7 @@ koi8r_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &koi8r_to_unicode_tree, - NULL, 0, - NULL, - PG_KOI8R, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_KOI8R, noError); PG_RETURN_INT32(converted); } @@ -118,12 +111,7 @@ koi8u_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8U, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &koi8u_to_unicode_tree, - NULL, 0, - NULL, - PG_KOI8U, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_KOI8U, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c index f20d80e719..0676af1017 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc2004/utf8_and_euc2004.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/euc_jis_2004_to_utf8.map" #include "../../Unicode/utf8_to_euc_jis_2004.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ euc_jis_2004_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JIS_2004, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &euc_jis_2004_to_unicode_tree, - LUmapEUC_JIS_2004_combined, lengthof(LUmapEUC_JIS_2004_combined), - NULL, - PG_EUC_JIS_2004, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_EUC_JIS_2004, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c index f397e61db6..4be8b057b8 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/euc_cn_to_utf8.map" #include "../../Unicode/utf8_to_euc_cn.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ euc_cn_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_CN, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &euc_cn_to_unicode_tree, - NULL, 0, - NULL, - PG_EUC_CN, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_EUC_CN, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c index 6f4787dfc0..b02fe91cc7 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/euc_jp_to_utf8.map" #include "../../Unicode/utf8_to_euc_jp.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ euc_jp_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JP, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &euc_jp_to_unicode_tree, - NULL, 0, - NULL, - PG_EUC_JP, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_EUC_JP, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c index fabd2d13a7..42a94e27a0 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/euc_kr_to_utf8.map" #include "../../Unicode/utf8_to_euc_kr.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ euc_kr_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_KR, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &euc_kr_to_unicode_tree, - NULL, 0, - NULL, - PG_EUC_KR, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_EUC_KR, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c b/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c index e3dfac553a..2f2f4a393d 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/euc_tw_to_utf8.map" #include "../../Unicode/utf8_to_euc_tw.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ euc_tw_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_TW, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &euc_tw_to_unicode_tree, - NULL, 0, - NULL, - PG_EUC_TW, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_EUC_TW, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c b/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c index 8c98fcc91a..9536d39b24 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c @@ -14,7 +14,7 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/gb18030_to_utf8.map" +#include "../../gb18030.h" #include "../../Unicode/utf8_to_gb18030.map" PG_MODULE_MAGIC_EXT( @@ -25,24 +25,6 @@ PG_MODULE_MAGIC_EXT( PG_FUNCTION_INFO_V1(gb18030_to_utf8); PG_FUNCTION_INFO_V1(utf8_to_gb18030); -/* - * Convert 4-byte GB18030 characters to and from a linear code space - * - * The first and third bytes can range from 0x81 to 0xfe (126 values), - * while the second and fourth bytes can range from 0x30 to 0x39 (10 values). - */ -static inline uint32 -gb_linear(uint32 gb) -{ - uint32 b0 = (gb & 0xff000000) >> 24; - uint32 b1 = (gb & 0x00ff0000) >> 16; - uint32 b2 = (gb & 0x0000ff00) >> 8; - uint32 b3 = (gb & 0x000000ff); - - return b0 * 12600 + b1 * 1260 + b2 * 10 + b3 - - (0x81 * 12600 + 0x30 * 1260 + 0x81 * 10 + 0x30); -} - static inline uint32 gb_unlinear(uint32 lin) { @@ -55,41 +37,10 @@ gb_unlinear(uint32 lin) } /* - * Convert word-formatted UTF8 to and from Unicode code points + * Convert word-formatted UTF8 to Unicode code points * * Probably this should be somewhere else ... */ -static inline uint32 -unicode_to_utf8word(uint32 c) -{ - uint32 word; - - if (c <= 0x7F) - { - word = c; - } - else if (c <= 0x7FF) - { - word = (0xC0 | ((c >> 6) & 0x1F)) << 8; - word |= 0x80 | (c & 0x3F); - } - else if (c <= 0xFFFF) - { - word = (0xE0 | ((c >> 12) & 0x0F)) << 16; - word |= (0x80 | ((c >> 6) & 0x3F)) << 8; - word |= 0x80 | (c & 0x3F); - } - else - { - word = (0xF0 | ((c >> 18) & 0x07)) << 24; - word |= (0x80 | ((c >> 12) & 0x3F)) << 16; - word |= (0x80 | ((c >> 6) & 0x3F)) << 8; - word |= 0x80 | (c & 0x3F); - } - - return word; -} - static inline uint32 utf8word_to_unicode(uint32 c) { @@ -121,41 +72,6 @@ utf8word_to_unicode(uint32 c) return ucs; } -/* - * Perform mapping of GB18030 ranges to UTF8 - * - * General description, and the range we need to convert for U+10000 and up: - * https://htmlpreview.github.io/?https://github.com/unicode-org/icu-data/blob/main/charset/source/gb18030/gb18030.html - * - * Ranges up to U+FFFF: - * https://github.com/unicode-org/icu-data/blob/main/charset/source/gb18030/ranges.txt - * - * All are ranges of 4-byte GB18030 codes. - */ -static uint32 -conv_18030_to_utf8(uint32 code) -{ -#define conv18030(minunicode, mincode, maxcode) \ - if (code >= mincode && code <= maxcode) \ - return unicode_to_utf8word(gb_linear(code) - gb_linear(mincode) + minunicode) - - conv18030(0x0452, 0x8130D330, 0x8136A531); - conv18030(0x2643, 0x8137A839, 0x8138FD38); - conv18030(0x361B, 0x8230A633, 0x8230F237); - conv18030(0x3CE1, 0x8231D438, 0x8232AF32); - conv18030(0x4160, 0x8232C937, 0x8232F837); - conv18030(0x44D7, 0x8233A339, 0x8233C931); - conv18030(0x478E, 0x8233E838, 0x82349638); - conv18030(0x49B8, 0x8234A131, 0x8234E733); - conv18030(0x9FA6, 0x82358F33, 0x8336C738); - conv18030(0xE865, 0x8336D030, 0x84308534); - conv18030(0xFA2A, 0x84309C38, 0x84318537); - conv18030(0xFFE6, 0x8431A234, 0x8431A439); - conv18030(0x10000, 0x90308130, 0xE3329A35); - /* No mapping exists */ - return 0; -} - /* * Perform mapping of UTF8 ranges to GB18030 */ @@ -209,12 +125,7 @@ gb18030_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_GB18030, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &gb18030_to_unicode_tree, - NULL, 0, - conv_18030_to_utf8, - PG_GB18030, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_GB18030, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c b/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c index fa9595e142..a195d0dfd7 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/gbk_to_utf8.map" #include "../../Unicode/utf8_to_gbk.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ gbk_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_GBK, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &gbk_to_unicode_tree, - NULL, 0, - NULL, - PG_GBK, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_GBK, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c index e6166451a7..1f797a2497 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c @@ -14,18 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/iso8859_10_to_utf8.map" -#include "../../Unicode/iso8859_13_to_utf8.map" -#include "../../Unicode/iso8859_14_to_utf8.map" -#include "../../Unicode/iso8859_15_to_utf8.map" -#include "../../Unicode/iso8859_2_to_utf8.map" -#include "../../Unicode/iso8859_3_to_utf8.map" -#include "../../Unicode/iso8859_4_to_utf8.map" -#include "../../Unicode/iso8859_5_to_utf8.map" -#include "../../Unicode/iso8859_6_to_utf8.map" -#include "../../Unicode/iso8859_7_to_utf8.map" -#include "../../Unicode/iso8859_8_to_utf8.map" -#include "../../Unicode/iso8859_9_to_utf8.map" #include "../../Unicode/utf8_to_iso8859_10.map" #include "../../Unicode/utf8_to_iso8859_13.map" #include "../../Unicode/utf8_to_iso8859_14.map" @@ -39,7 +27,6 @@ #include "../../Unicode/utf8_to_iso8859_7.map" #include "../../Unicode/utf8_to_iso8859_8.map" #include "../../Unicode/utf8_to_iso8859_9.map" -#include "../../Unicode/iso8859_16_to_utf8.map" PG_MODULE_MAGIC_EXT( .name = "utf8_and_iso8859", @@ -66,37 +53,23 @@ PG_FUNCTION_INFO_V1(utf8_to_iso8859); typedef struct { pg_enc encoding; - const pg_mb_radix_tree *map1; /* to UTF8 map name */ const pg_mb_radix_tree *map2; /* from UTF8 map name */ } pg_conv_map; static const pg_conv_map maps[] = { - {PG_LATIN2, &iso8859_2_to_unicode_tree, - &iso8859_2_from_unicode_tree}, /* ISO-8859-2 Latin 2 */ - {PG_LATIN3, &iso8859_3_to_unicode_tree, - &iso8859_3_from_unicode_tree}, /* ISO-8859-3 Latin 3 */ - {PG_LATIN4, &iso8859_4_to_unicode_tree, - &iso8859_4_from_unicode_tree}, /* ISO-8859-4 Latin 4 */ - {PG_LATIN5, &iso8859_9_to_unicode_tree, - &iso8859_9_from_unicode_tree}, /* ISO-8859-9 Latin 5 */ - {PG_LATIN6, &iso8859_10_to_unicode_tree, - &iso8859_10_from_unicode_tree}, /* ISO-8859-10 Latin 6 */ - {PG_LATIN7, &iso8859_13_to_unicode_tree, - &iso8859_13_from_unicode_tree}, /* ISO-8859-13 Latin 7 */ - {PG_LATIN8, &iso8859_14_to_unicode_tree, - &iso8859_14_from_unicode_tree}, /* ISO-8859-14 Latin 8 */ - {PG_LATIN9, &iso8859_15_to_unicode_tree, - &iso8859_15_from_unicode_tree}, /* ISO-8859-15 Latin 9 */ - {PG_LATIN10, &iso8859_16_to_unicode_tree, - &iso8859_16_from_unicode_tree}, /* ISO-8859-16 Latin 10 */ - {PG_ISO_8859_5, &iso8859_5_to_unicode_tree, - &iso8859_5_from_unicode_tree}, /* ISO-8859-5 */ - {PG_ISO_8859_6, &iso8859_6_to_unicode_tree, - &iso8859_6_from_unicode_tree}, /* ISO-8859-6 */ - {PG_ISO_8859_7, &iso8859_7_to_unicode_tree, - &iso8859_7_from_unicode_tree}, /* ISO-8859-7 */ - {PG_ISO_8859_8, &iso8859_8_to_unicode_tree, - &iso8859_8_from_unicode_tree}, /* ISO-8859-8 */ + {PG_LATIN2, &iso8859_2_from_unicode_tree}, /* ISO-8859-2 Latin 2 */ + {PG_LATIN3, &iso8859_3_from_unicode_tree}, /* ISO-8859-3 Latin 3 */ + {PG_LATIN4, &iso8859_4_from_unicode_tree}, /* ISO-8859-4 Latin 4 */ + {PG_LATIN5, &iso8859_9_from_unicode_tree}, /* ISO-8859-9 Latin 5 */ + {PG_LATIN6, &iso8859_10_from_unicode_tree}, /* ISO-8859-10 Latin 6 */ + {PG_LATIN7, &iso8859_13_from_unicode_tree}, /* ISO-8859-13 Latin 7 */ + {PG_LATIN8, &iso8859_14_from_unicode_tree}, /* ISO-8859-14 Latin 8 */ + {PG_LATIN9, &iso8859_15_from_unicode_tree}, /* ISO-8859-15 Latin 9 */ + {PG_LATIN10, &iso8859_16_from_unicode_tree}, /* ISO-8859-16 Latin 10 */ + {PG_ISO_8859_5, &iso8859_5_from_unicode_tree}, /* ISO-8859-5 */ + {PG_ISO_8859_6, &iso8859_6_from_unicode_tree}, /* ISO-8859-6 */ + {PG_ISO_8859_7, &iso8859_7_from_unicode_tree}, /* ISO-8859-7 */ + {PG_ISO_8859_8, &iso8859_8_from_unicode_tree}, /* ISO-8859-8 */ }; Datum @@ -116,12 +89,7 @@ iso8859_to_utf8(PG_FUNCTION_ARGS) { int converted; - converted = LocalToUtf(src, len, dest, - maps[i].map1, - NULL, 0, - NULL, - encoding, - noError); + converted = pg_local_to_utf8(src, len, dest, encoding, noError); PG_RETURN_INT32(converted); } } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c index 4d829e7eb0..7b0d196f47 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c @@ -44,33 +44,10 @@ iso8859_1_to_utf8(PG_FUNCTION_ARGS) unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3); int len = PG_GETARG_INT32(4); bool noError = PG_GETARG_BOOL(5); - unsigned char *start = src; - unsigned short c; CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN1, PG_UTF8); - while (len > 0) - { - c = *src; - if (c == 0) - { - if (noError) - break; - report_invalid_encoding(PG_LATIN1, (const char *) src, len); - } - if (!IS_HIGHBIT_SET(c)) - *dest++ = c; - else - { - *dest++ = (c >> 6) | 0xc0; - *dest++ = (c & 0x003f) | HIGHBIT; - } - src++; - len--; - } - *dest = '\0'; - - PG_RETURN_INT32(src - start); + PG_RETURN_INT32(pg_local_to_utf8(src, len, dest, PG_LATIN1, noError)); } Datum diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c b/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c index 52045b039a..e47cc14034 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/johab_to_utf8.map" #include "../../Unicode/utf8_to_johab.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ johab_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_JOHAB, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &johab_to_unicode_tree, - NULL, 0, - NULL, - PG_JOHAB, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_JOHAB, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c b/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c index bca4ea2489..67bcf8a4ea 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/sjis_to_utf8.map" #include "../../Unicode/utf8_to_sjis.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ sjis_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_SJIS, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &sjis_to_unicode_tree, - NULL, 0, - NULL, - PG_SJIS, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_SJIS, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c b/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c index e585377b4f..5a9f71ad44 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_sjis2004/utf8_and_sjis2004.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/shift_jis_2004_to_utf8.map" #include "../../Unicode/utf8_to_shift_jis_2004.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ shift_jis_2004_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_SHIFT_JIS_2004, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &shift_jis_2004_to_unicode_tree, - LUmapSHIFT_JIS_2004_combined, lengthof(LUmapSHIFT_JIS_2004_combined), - NULL, - PG_SHIFT_JIS_2004, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_SHIFT_JIS_2004, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c b/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c index a60319857d..f25d94fca8 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c @@ -14,7 +14,6 @@ #include "postgres.h" #include "fmgr.h" #include "mb/pg_wchar.h" -#include "../../Unicode/uhc_to_utf8.map" #include "../../Unicode/utf8_to_uhc.map" PG_MODULE_MAGIC_EXT( @@ -49,12 +48,7 @@ uhc_to_utf8(PG_FUNCTION_ARGS) CHECK_ENCODING_CONVERSION_ARGS(PG_UHC, PG_UTF8); - converted = LocalToUtf(src, len, dest, - &uhc_to_unicode_tree, - NULL, 0, - NULL, - PG_UHC, - noError); + converted = pg_local_to_utf8(src, len, dest, PG_UHC, noError); PG_RETURN_INT32(converted); } diff --git a/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c b/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c index e169a1d796..42cbd2d653 100644 --- a/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c +++ b/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c @@ -25,17 +25,6 @@ #include "../../Unicode/utf8_to_win1258.map" #include "../../Unicode/utf8_to_win866.map" #include "../../Unicode/utf8_to_win874.map" -#include "../../Unicode/win1250_to_utf8.map" -#include "../../Unicode/win1251_to_utf8.map" -#include "../../Unicode/win1252_to_utf8.map" -#include "../../Unicode/win1253_to_utf8.map" -#include "../../Unicode/win1254_to_utf8.map" -#include "../../Unicode/win1255_to_utf8.map" -#include "../../Unicode/win1256_to_utf8.map" -#include "../../Unicode/win1257_to_utf8.map" -#include "../../Unicode/win866_to_utf8.map" -#include "../../Unicode/win874_to_utf8.map" -#include "../../Unicode/win1258_to_utf8.map" PG_MODULE_MAGIC_EXT( .name = "utf8_and_win", @@ -62,22 +51,21 @@ PG_FUNCTION_INFO_V1(utf8_to_win); typedef struct { pg_enc encoding; - const pg_mb_radix_tree *map1; /* to UTF8 map name */ const pg_mb_radix_tree *map2; /* from UTF8 map name */ } pg_conv_map; static const pg_conv_map maps[] = { - {PG_WIN866, &win866_to_unicode_tree, &win866_from_unicode_tree}, - {PG_WIN874, &win874_to_unicode_tree, &win874_from_unicode_tree}, - {PG_WIN1250, &win1250_to_unicode_tree, &win1250_from_unicode_tree}, - {PG_WIN1251, &win1251_to_unicode_tree, &win1251_from_unicode_tree}, - {PG_WIN1252, &win1252_to_unicode_tree, &win1252_from_unicode_tree}, - {PG_WIN1253, &win1253_to_unicode_tree, &win1253_from_unicode_tree}, - {PG_WIN1254, &win1254_to_unicode_tree, &win1254_from_unicode_tree}, - {PG_WIN1255, &win1255_to_unicode_tree, &win1255_from_unicode_tree}, - {PG_WIN1256, &win1256_to_unicode_tree, &win1256_from_unicode_tree}, - {PG_WIN1257, &win1257_to_unicode_tree, &win1257_from_unicode_tree}, - {PG_WIN1258, &win1258_to_unicode_tree, &win1258_from_unicode_tree}, + {PG_WIN866, &win866_from_unicode_tree}, + {PG_WIN874, &win874_from_unicode_tree}, + {PG_WIN1250, &win1250_from_unicode_tree}, + {PG_WIN1251, &win1251_from_unicode_tree}, + {PG_WIN1252, &win1252_from_unicode_tree}, + {PG_WIN1253, &win1253_from_unicode_tree}, + {PG_WIN1254, &win1254_from_unicode_tree}, + {PG_WIN1255, &win1255_from_unicode_tree}, + {PG_WIN1256, &win1256_from_unicode_tree}, + {PG_WIN1257, &win1257_from_unicode_tree}, + {PG_WIN1258, &win1258_from_unicode_tree}, }; Datum @@ -97,12 +85,7 @@ win_to_utf8(PG_FUNCTION_ARGS) { int converted; - converted = LocalToUtf(src, len, dest, - maps[i].map1, - NULL, 0, - NULL, - encoding, - noError); + converted = pg_local_to_utf8(src, len, dest, encoding, noError); PG_RETURN_INT32(converted); } } diff --git a/src/backend/utils/mb/gb18030.h b/src/backend/utils/mb/gb18030.h new file mode 100644 index 0000000000..11136dafcd --- /dev/null +++ b/src/backend/utils/mb/gb18030.h @@ -0,0 +1,34 @@ +/*------------------------------------------------------------------------- + * + * gb18030.h + * Helpers for GB18030 conversions. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/backend/utils/mb/gb18030.h + * + *------------------------------------------------------------------------- + */ +#ifndef GB18030_H +#define GB18030_H + +/* + * Convert a 4-byte GB18030 character to an offset in a linear code space. + * + * The first and third bytes can range from 0x81 to 0xfe (126 values), + * while the second and fourth bytes can range from 0x30 to 0x39 (10 values). + */ +static inline uint32 +gb_linear(uint32 gb) +{ + uint32 b0 = (gb & 0xff000000) >> 24; + uint32 b1 = (gb & 0x00ff0000) >> 16; + uint32 b2 = (gb & 0x0000ff00) >> 8; + uint32 b3 = (gb & 0x000000ff); + + return b0 * 12600 + b1 * 1260 + b2 * 10 + b3 - + (0x81 * 12600 + 0x30 * 1260 + 0x81 * 10 + 0x30); +} + +#endif diff --git a/src/backend/utils/mb/local_utf8.c b/src/backend/utils/mb/local_utf8.c new file mode 100644 index 0000000000..a54a8f39eb --- /dev/null +++ b/src/backend/utils/mb/local_utf8.c @@ -0,0 +1,200 @@ +/*------------------------------------------------------------------------- + * + * local_utf8.c + * Direct conversions from PostgreSQL encodings to UTF8. + * + * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * src/backend/utils/mb/local_utf8.c + * + *------------------------------------------------------------------------- + */ + +#include "postgres.h" + +#include "mb/pg_wchar.h" + +#include "gb18030.h" + +#include "Unicode/euc_jp_to_utf8.map" +#include "Unicode/euc_cn_to_utf8.map" +#include "Unicode/euc_kr_to_utf8.map" +#include "Unicode/euc_tw_to_utf8.map" +#include "Unicode/euc_jis_2004_to_utf8.map" +#include "Unicode/iso8859_2_to_utf8.map" +#include "Unicode/iso8859_3_to_utf8.map" +#include "Unicode/iso8859_4_to_utf8.map" +#include "Unicode/iso8859_9_to_utf8.map" +#include "Unicode/iso8859_10_to_utf8.map" +#include "Unicode/iso8859_13_to_utf8.map" +#include "Unicode/iso8859_14_to_utf8.map" +#include "Unicode/iso8859_15_to_utf8.map" +#include "Unicode/iso8859_16_to_utf8.map" +#include "Unicode/win1256_to_utf8.map" +#include "Unicode/win1258_to_utf8.map" +#include "Unicode/win866_to_utf8.map" +#include "Unicode/win874_to_utf8.map" +#include "Unicode/koi8r_to_utf8.map" +#include "Unicode/win1251_to_utf8.map" +#include "Unicode/win1252_to_utf8.map" +#include "Unicode/iso8859_5_to_utf8.map" +#include "Unicode/iso8859_6_to_utf8.map" +#include "Unicode/iso8859_7_to_utf8.map" +#include "Unicode/iso8859_8_to_utf8.map" +#include "Unicode/win1250_to_utf8.map" +#include "Unicode/win1253_to_utf8.map" +#include "Unicode/win1254_to_utf8.map" +#include "Unicode/win1255_to_utf8.map" +#include "Unicode/win1257_to_utf8.map" +#include "Unicode/koi8u_to_utf8.map" +#include "Unicode/sjis_to_utf8.map" +#include "Unicode/big5_to_utf8.map" +#include "Unicode/gb18030_to_utf8.map" +#include "Unicode/gbk_to_utf8.map" +#include "Unicode/uhc_to_utf8.map" +#include "Unicode/johab_to_utf8.map" +#include "Unicode/shift_jis_2004_to_utf8.map" + +/* + * Keep the local-to-UTF8 tables in one place for SQL conversions and + * callers that must work without access to the catalogs, such as logging. + */ +static const struct +{ + const pg_mb_radix_tree *map; + const pg_local_to_utf_combined *combined; + int ncombined; +} local_utf8_maps[_PG_LAST_ENCODING_] = { + [PG_EUC_JP] = {&euc_jp_to_unicode_tree, NULL, 0}, + [PG_EUC_CN] = {&euc_cn_to_unicode_tree, NULL, 0}, + [PG_EUC_KR] = {&euc_kr_to_unicode_tree, NULL, 0}, + [PG_EUC_TW] = {&euc_tw_to_unicode_tree, NULL, 0}, + [PG_EUC_JIS_2004] = {&euc_jis_2004_to_unicode_tree, + LUmapEUC_JIS_2004_combined, lengthof(LUmapEUC_JIS_2004_combined)}, + [PG_LATIN2] = {&iso8859_2_to_unicode_tree, NULL, 0}, + [PG_LATIN3] = {&iso8859_3_to_unicode_tree, NULL, 0}, + [PG_LATIN4] = {&iso8859_4_to_unicode_tree, NULL, 0}, + [PG_LATIN5] = {&iso8859_9_to_unicode_tree, NULL, 0}, + [PG_LATIN6] = {&iso8859_10_to_unicode_tree, NULL, 0}, + [PG_LATIN7] = {&iso8859_13_to_unicode_tree, NULL, 0}, + [PG_LATIN8] = {&iso8859_14_to_unicode_tree, NULL, 0}, + [PG_LATIN9] = {&iso8859_15_to_unicode_tree, NULL, 0}, + [PG_LATIN10] = {&iso8859_16_to_unicode_tree, NULL, 0}, + [PG_WIN1256] = {&win1256_to_unicode_tree, NULL, 0}, + [PG_WIN1258] = {&win1258_to_unicode_tree, NULL, 0}, + [PG_WIN866] = {&win866_to_unicode_tree, NULL, 0}, + [PG_WIN874] = {&win874_to_unicode_tree, NULL, 0}, + [PG_KOI8R] = {&koi8r_to_unicode_tree, NULL, 0}, + [PG_WIN1251] = {&win1251_to_unicode_tree, NULL, 0}, + [PG_WIN1252] = {&win1252_to_unicode_tree, NULL, 0}, + [PG_ISO_8859_5] = {&iso8859_5_to_unicode_tree, NULL, 0}, + [PG_ISO_8859_6] = {&iso8859_6_to_unicode_tree, NULL, 0}, + [PG_ISO_8859_7] = {&iso8859_7_to_unicode_tree, NULL, 0}, + [PG_ISO_8859_8] = {&iso8859_8_to_unicode_tree, NULL, 0}, + [PG_WIN1250] = {&win1250_to_unicode_tree, NULL, 0}, + [PG_WIN1253] = {&win1253_to_unicode_tree, NULL, 0}, + [PG_WIN1254] = {&win1254_to_unicode_tree, NULL, 0}, + [PG_WIN1255] = {&win1255_to_unicode_tree, NULL, 0}, + [PG_WIN1257] = {&win1257_to_unicode_tree, NULL, 0}, + [PG_KOI8U] = {&koi8u_to_unicode_tree, NULL, 0}, + [PG_SJIS] = {&sjis_to_unicode_tree, NULL, 0}, + [PG_BIG5] = {&big5_to_unicode_tree, NULL, 0}, + [PG_GBK] = {&gbk_to_unicode_tree, NULL, 0}, + [PG_UHC] = {&uhc_to_unicode_tree, NULL, 0}, + [PG_GB18030] = {&gb18030_to_unicode_tree, NULL, 0}, + [PG_JOHAB] = {&johab_to_unicode_tree, NULL, 0}, + [PG_SHIFT_JIS_2004] = {&shift_jis_2004_to_unicode_tree, + LUmapSHIFT_JIS_2004_combined, lengthof(LUmapSHIFT_JIS_2004_combined)}, +}; + +/* + * LocalToUtf expects its callback to return UTF-8 bytes packed into a word. + */ +static uint32 +unicode_to_utf8word(char32_t code) +{ + unsigned char utf8[4]; + int len = unicode_utf8len(code); + uint32 word = 0; + + unicode_to_utf8(code, utf8); + for (int i = 0; i < len; i++) + word = (word << 8) | utf8[i]; + + return word; +} + +/* + * Perform mapping of GB18030 ranges to UTF8 + * + * General description, and the range we need to convert for U+10000 and up: + * https://htmlpreview.github.io/?https://github.com/unicode-org/icu-data/blob/main/charset/source/gb18030/gb18030.html + * + * Ranges up to U+FFFF: + * https://github.com/unicode-org/icu-data/blob/main/charset/source/gb18030/ranges.txt + * + * All are ranges of 4-byte GB18030 codes. + */ +static uint32 +conv_18030_to_utf8(uint32 code) +{ +#define conv18030(minunicode, mincode, maxcode) \ + if (code >= mincode && code <= maxcode) \ + return unicode_to_utf8word(gb_linear(code) - gb_linear(mincode) + minunicode) + + conv18030(0x0452, 0x8130D330, 0x8136A531); + conv18030(0x2643, 0x8137A839, 0x8138FD38); + conv18030(0x361B, 0x8230A633, 0x8230F237); + conv18030(0x3CE1, 0x8231D438, 0x8232AF32); + conv18030(0x4160, 0x8232C937, 0x8232F837); + conv18030(0x44D7, 0x8233A339, 0x8233C931); + conv18030(0x478E, 0x8233E838, 0x82349638); + conv18030(0x49B8, 0x8234A131, 0x8234E733); + conv18030(0x9FA6, 0x82358F33, 0x8336C738); + conv18030(0xE865, 0x8336D030, 0x84308534); + conv18030(0xFA2A, 0x84309C38, 0x84318537); + conv18030(0xFFE6, 0x8431A234, 0x8431A439); + conv18030(0x10000, 0x90308130, 0xE3329A35); + /* No mapping exists */ + return 0; +} + +/* + * Convert directly to UTF8 without fmgr or catalog lookups. As with + * LocalToUtf(), the result is NUL terminated and the return value is the + * number of input bytes converted. + */ +int +pg_local_to_utf8(const unsigned char *src, int len, unsigned char *dest, + int encoding, bool noError) +{ + if (encoding == PG_LATIN1) + { + const unsigned char *start = src; + + for (; len > 0 && *src; len--, src++) + { + unicode_to_utf8(*src, dest); + dest += IS_HIGHBIT_SET(*src) ? 2 : 1; + } + *dest = '\0'; + + if (len > 0 && !noError) + report_invalid_encoding(encoding, (const char *) src, len); + return src - start; + } + + if (PG_VALID_ENCODING(encoding) && local_utf8_maps[encoding].map) + return LocalToUtf(src, len, dest, local_utf8_maps[encoding].map, + local_utf8_maps[encoding].combined, + local_utf8_maps[encoding].ncombined, + encoding == PG_GB18030 ? conv_18030_to_utf8 : NULL, + encoding, noError); + + if (!noError) + elog(ERROR, "no direct UTF8 conversion for encoding %d", encoding); + *dest = '\0'; + return 0; +} diff --git a/src/backend/utils/mb/meson.build b/src/backend/utils/mb/meson.build index 79bb89069f..a86ab549dd 100644 --- a/src/backend/utils/mb/meson.build +++ b/src/backend/utils/mb/meson.build @@ -2,6 +2,7 @@ backend_sources += files( 'conv.c', + 'local_utf8.c', 'mbutils.c', 'stringinfo_mb.c', 'wstrcmp.c', diff --git a/src/include/mb/pg_wchar.h b/src/include/mb/pg_wchar.h index 4f8d059b61..fc954a94d7 100644 --- a/src/include/mb/pg_wchar.h +++ b/src/include/mb/pg_wchar.h @@ -673,6 +673,8 @@ extern int UtfToLocal(const unsigned char *utf, int len, const pg_utf_to_local_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding, bool noError); +extern int pg_local_to_utf8(const unsigned char *src, int len, + unsigned char *dest, int encoding, bool noError); extern int LocalToUtf(const unsigned char *iso, int len, unsigned char *utf, const pg_mb_radix_tree *map, base-commit: ad33a140c76f53d66f37330edfa47c5d96e2d6ce