Replace argument-checking Asserts with regular test-and-elog checks in all
authorTom Lane <[email protected]>
Thu, 29 Jan 2009 19:24:37 +0000 (19:24 +0000)
committerTom Lane <[email protected]>
Thu, 29 Jan 2009 19:24:37 +0000 (19:24 +0000)
encoding conversion functions.  These are not can't-happen cases because
it's possible to create a conversion with the wrong conversion function
for the specified encoding pair.  That would lead to an Assert crash in
an Assert-enabled build, or incorrect conversion otherwise, neither of
which is desirable.  This would be a DOS issue if production databases
were customarily built with asserts enabled, but fortunately that's not so.
Per an observation by Heikki.

Back-patch to all supported branches.

29 files changed:
src/backend/utils/mb/conversion_procs/ascii_and_mic/ascii_and_mic.c
src/backend/utils/mb/conversion_procs/cyrillic_and_mic/cyrillic_and_mic.c
src/backend/utils/mb/conversion_procs/euc_cn_and_mic/euc_cn_and_mic.c
src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c
src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c
src/backend/utils/mb/conversion_procs/euc_tw_and_big5/euc_tw_and_big5.c
src/backend/utils/mb/conversion_procs/latin2_and_win1250/latin2_and_win1250.c
src/backend/utils/mb/conversion_procs/latin_and_mic/latin_and_mic.c
src/backend/utils/mb/conversion_procs/utf8_and_ascii/utf8_and_ascii.c
src/backend/utils/mb/conversion_procs/utf8_and_big5/utf8_and_big5.c
src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c
src/backend/utils/mb/conversion_procs/utf8_and_euc_cn/utf8_and_euc_cn.c
src/backend/utils/mb/conversion_procs/utf8_and_euc_jp/utf8_and_euc_jp.c
src/backend/utils/mb/conversion_procs/utf8_and_euc_kr/utf8_and_euc_kr.c
src/backend/utils/mb/conversion_procs/utf8_and_euc_tw/utf8_and_euc_tw.c
src/backend/utils/mb/conversion_procs/utf8_and_gb18030/utf8_and_gb18030.c
src/backend/utils/mb/conversion_procs/utf8_and_gbk/utf8_and_gbk.c
src/backend/utils/mb/conversion_procs/utf8_and_iso8859/utf8_and_iso8859.c
src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c
src/backend/utils/mb/conversion_procs/utf8_and_johab/utf8_and_johab.c
src/backend/utils/mb/conversion_procs/utf8_and_sjis/utf8_and_sjis.c
src/backend/utils/mb/conversion_procs/utf8_and_uhc/utf8_and_uhc.c
src/backend/utils/mb/conversion_procs/utf8_and_win1250/utf8_and_win1250.c
src/backend/utils/mb/conversion_procs/utf8_and_win1252/utf8_and_win1252.c
src/backend/utils/mb/conversion_procs/utf8_and_win1256/utf8_and_win1256.c
src/backend/utils/mb/conversion_procs/utf8_and_win1258/utf8_and_win1258.c
src/backend/utils/mb/conversion_procs/utf8_and_win874/utf8_and_win874.c
src/backend/utils/mb/wchar.c
src/include/mb/pg_wchar.h

index 5c142d9efc2941822f4f1d22c49871b8a5bec01a..63d7eb573e14aec7d240adef4e55401f42eb6c6c 100644 (file)
@@ -39,9 +39,7 @@ ascii_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_SQL_ASCII);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_SQL_ASCII, PG_MULE_INTERNAL);
 
        pg_ascii2mic(src, dest, len);
 
@@ -55,9 +53,7 @@ mic_to_ascii(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_SQL_ASCII);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_SQL_ASCII);
 
        pg_mic2ascii(src, dest, len);
 
index f1b4961280dd74a9f3fa5543f08eb92892584bdd..94203bd8ad84179f06cc6251a5605759c8e439d0 100644 (file)
@@ -86,9 +86,7 @@ koi8r_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_KOI8R);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_MULE_INTERNAL);
 
        koi8r2mic(src, dest, len);
 
@@ -102,9 +100,7 @@ mic_to_koi8r(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_KOI8R);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_KOI8R);
 
        mic2koi8r(src, dest, len);
 
@@ -118,9 +114,7 @@ iso_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_ISO_8859_5);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_ISO_8859_5, PG_MULE_INTERNAL);
 
        iso2mic(src, dest, len);
 
@@ -134,9 +128,7 @@ mic_to_iso(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_ISO_8859_5);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_ISO_8859_5);
 
        mic2iso(src, dest, len);
 
@@ -150,9 +142,7 @@ win1251_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1251);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1251, PG_MULE_INTERNAL);
 
        win12512mic(src, dest, len);
 
@@ -166,9 +156,7 @@ mic_to_win1251(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1251);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_WIN1251);
 
        mic2win1251(src, dest, len);
 
@@ -182,9 +170,7 @@ win866_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN866);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN866, PG_MULE_INTERNAL);
 
        win8662mic(src, dest, len);
 
@@ -198,9 +184,7 @@ mic_to_win866(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_WIN866);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_WIN866);
 
        mic2win866(src, dest, len);
 
@@ -215,9 +199,7 @@ koi8r_to_win1251(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_KOI8R);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1251);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_WIN1251);
 
        buf = palloc(len * ENCODING_GROWTH_RATE);
        koi8r2mic(src, buf, len);
@@ -235,9 +217,7 @@ win1251_to_koi8r(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1251);
-       Assert(PG_GETARG_INT32(1) == PG_KOI8R);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1251, PG_KOI8R);
 
        buf = palloc(len * ENCODING_GROWTH_RATE);
        win12512mic(src, buf, len);
@@ -255,9 +235,7 @@ koi8r_to_win866(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_KOI8R);
-       Assert(PG_GETARG_INT32(1) == PG_WIN866);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_WIN866);
 
        buf = palloc(len * ENCODING_GROWTH_RATE);
        koi8r2mic(src, buf, len);
@@ -275,9 +253,7 @@ win866_to_koi8r(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN866);
-       Assert(PG_GETARG_INT32(1) == PG_KOI8R);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN866, PG_KOI8R);
 
        buf = palloc(len * ENCODING_GROWTH_RATE);
        win8662mic(src, buf, len);
@@ -295,9 +271,7 @@ win866_to_win1251(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN866);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1251);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN866, PG_WIN1251);
 
        /*
         * Note: There are a few characters like the "Numero" sign that exist in
@@ -321,9 +295,7 @@ win1251_to_win866(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1251);
-       Assert(PG_GETARG_INT32(1) == PG_WIN866);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1251, PG_WIN866);
 
        /* Use mic/KOI8R as intermediary, see comment in win866_to_win1251() */
        buf = palloc(len * ENCODING_GROWTH_RATE);
@@ -342,9 +314,7 @@ iso_to_koi8r(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_ISO_8859_5);
-       Assert(PG_GETARG_INT32(1) == PG_KOI8R);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_ISO_8859_5, PG_KOI8R);
 
        buf = palloc(len * ENCODING_GROWTH_RATE);
        iso2mic(src, buf, len);
@@ -362,9 +332,7 @@ koi8r_to_iso(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_KOI8R);
-       Assert(PG_GETARG_INT32(1) == PG_ISO_8859_5);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_ISO_8859_5);
 
        buf = palloc(len * ENCODING_GROWTH_RATE);
        koi8r2mic(src, buf, len);
@@ -382,9 +350,7 @@ iso_to_win1251(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_ISO_8859_5);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1251);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_ISO_8859_5, PG_WIN1251);
 
        /* Use mic/KOI8R as intermediary, see comment in win866_to_win1251() */
        buf = palloc(len * ENCODING_GROWTH_RATE);
@@ -403,9 +369,7 @@ win1251_to_iso(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1251);
-       Assert(PG_GETARG_INT32(1) == PG_ISO_8859_5);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1251, PG_ISO_8859_5);
 
        /* Use mic/KOI8R as intermediary, see comment in win866_to_win1251() */
        buf = palloc(len * ENCODING_GROWTH_RATE);
@@ -424,9 +388,7 @@ iso_to_win866(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_ISO_8859_5);
-       Assert(PG_GETARG_INT32(1) == PG_WIN866);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_ISO_8859_5, PG_WIN866);
 
        /* Use mic/KOI8R as intermediary, see comment in win866_to_win1251() */
        buf = palloc(len * ENCODING_GROWTH_RATE);
@@ -445,9 +407,7 @@ win866_to_iso(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN866);
-       Assert(PG_GETARG_INT32(1) == PG_ISO_8859_5);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN866, PG_ISO_8859_5);
 
        /* Use mic/KOI8R as intermediary, see comment in win866_to_win1251() */
        buf = palloc(len * ENCODING_GROWTH_RATE);
index 26b68d63ea3b77f69c4e07b1689223a3c511023d..c6381b0226d2479b25f49338c8f49acbd9ab1cf8 100644 (file)
@@ -42,9 +42,7 @@ euc_cn_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_EUC_CN);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_CN, PG_MULE_INTERNAL);
 
        euc_cn2mic(src, dest, len);
 
@@ -58,9 +56,7 @@ mic_to_euc_cn(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_CN);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_EUC_CN);
 
        mic2euc_cn(src, dest, len);
 
index 51cf69de6aa9c4d944fb3f8ee784cc0d444fbd38..2dde019e5f625cecf56fb39f5ea830753c33768c 100644 (file)
@@ -68,9 +68,7 @@ euc_jp_to_sjis(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_EUC_JP);
-       Assert(PG_GETARG_INT32(1) == PG_SJIS);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JP, PG_SJIS);
 
        euc_jp2sjis(src, dest, len);
 
@@ -84,9 +82,7 @@ sjis_to_euc_jp(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_SJIS);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_JP);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_SJIS, PG_EUC_JP);
 
        sjis2euc_jp(src, dest, len);
 
@@ -100,9 +96,7 @@ euc_jp_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_EUC_JP);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JP, PG_MULE_INTERNAL);
 
        euc_jp2mic(src, dest, len);
 
@@ -116,9 +110,7 @@ mic_to_euc_jp(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_JP);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_EUC_JP);
 
        mic2euc_jp(src, dest, len);
 
@@ -132,9 +124,7 @@ sjis_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_SJIS);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_SJIS, PG_MULE_INTERNAL);
 
        sjis2mic(src, dest, len);
 
@@ -148,9 +138,7 @@ mic_to_sjis(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_SJIS);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_SJIS);
 
        mic2sjis(src, dest, len);
 
index 0aa0cd7f947cde7c6ff005de30b50eb7c8db9d67..6b2e51715257648613bc9cbad5e83a47ee2ee349 100644 (file)
@@ -42,9 +42,7 @@ euc_kr_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_EUC_KR);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_KR, PG_MULE_INTERNAL);
 
        euc_kr2mic(src, dest, len);
 
@@ -58,9 +56,7 @@ mic_to_euc_kr(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_KR);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_EUC_KR);
 
        mic2euc_kr(src, dest, len);
 
index 2ece3d51c236f3ac8eeac0b2117d8de2e7c78034..be5f18e9a02fbbfa6c2df82741132e2b54be0e51 100644 (file)
@@ -55,9 +55,7 @@ euc_tw_to_big5(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_EUC_TW);
-       Assert(PG_GETARG_INT32(1) == PG_BIG5);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_TW, PG_BIG5);
 
        buf = palloc(len * ENCODING_GROWTH_RATE);
        euc_tw2mic(src, buf, len);
@@ -75,9 +73,7 @@ big5_to_euc_tw(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_BIG5);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_TW);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_BIG5, PG_EUC_TW);
 
        buf = palloc(len * ENCODING_GROWTH_RATE);
        big52mic(src, buf, len);
@@ -94,9 +90,7 @@ euc_tw_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_EUC_TW);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_TW, PG_MULE_INTERNAL);
 
        euc_tw2mic(src, dest, len);
 
@@ -110,9 +104,7 @@ mic_to_euc_tw(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_TW);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_EUC_TW);
 
        mic2euc_tw(src, dest, len);
 
@@ -126,9 +118,7 @@ big5_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_BIG5);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_BIG5, PG_MULE_INTERNAL);
 
        big52mic(src, dest, len);
 
@@ -142,9 +132,7 @@ mic_to_big5(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_BIG5);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_BIG5);
 
        mic2big5(src, dest, len);
 
index b4bee522decdb1991c664ef741aa1aade8f8e770..57e6c4c0c99719ae7f978e3c1fc592b246e5b199 100644 (file)
@@ -54,9 +54,7 @@ latin2_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_LATIN2);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN2, PG_MULE_INTERNAL);
 
        latin22mic(src, dest, len);
 
@@ -70,9 +68,7 @@ mic_to_latin2(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_LATIN2);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_LATIN2);
 
        mic2latin2(src, dest, len);
 
@@ -86,9 +82,7 @@ win1250_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1250);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1250, PG_MULE_INTERNAL);
 
        win12502mic(src, dest, len);
 
@@ -102,9 +96,7 @@ mic_to_win1250(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1250);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_WIN1250);
 
        mic2win1250(src, dest, len);
 
@@ -119,9 +111,7 @@ latin2_to_win1250(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_LATIN2);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1250);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN2, PG_WIN1250);
 
        buf = palloc(len * ENCODING_GROWTH_RATE);
        latin22mic(src, buf, len);
@@ -139,9 +129,7 @@ win1250_to_latin2(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned char *buf;
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1250);
-       Assert(PG_GETARG_INT32(1) == PG_LATIN2);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1250, PG_LATIN2);
 
        buf = palloc(len * ENCODING_GROWTH_RATE);
        win12502mic(src, buf, len);
index 3fb72a727e0ba9954fb0645786cbcac127429a3a..b7e7e71870d97b21cd5a7d636950ef90753c6b7c 100644 (file)
@@ -54,9 +54,7 @@ latin1_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_LATIN1);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN1, PG_MULE_INTERNAL);
 
        latin12mic(src, dest, len);
 
@@ -70,9 +68,7 @@ mic_to_latin1(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_LATIN1);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_LATIN1);
 
        mic2latin1(src, dest, len);
 
@@ -86,9 +82,7 @@ latin3_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_LATIN3);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN3, PG_MULE_INTERNAL);
 
        latin32mic(src, dest, len);
 
@@ -102,9 +96,7 @@ mic_to_latin3(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_LATIN3);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_LATIN3);
 
        mic2latin3(src, dest, len);
 
@@ -118,9 +110,7 @@ latin4_to_mic(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_LATIN4);
-       Assert(PG_GETARG_INT32(1) == PG_MULE_INTERNAL);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN4, PG_MULE_INTERNAL);
 
        latin42mic(src, dest, len);
 
@@ -134,9 +124,7 @@ mic_to_latin4(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_MULE_INTERNAL);
-       Assert(PG_GETARG_INT32(1) == PG_LATIN4);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_LATIN4);
 
        mic2latin4(src, dest, len);
 
index 14369b4cdf65528660f0d8f4725b35a70637d325..c27cfbb11319256e9ca56dd91cb0f544ef3dc59d 100644 (file)
@@ -39,9 +39,7 @@ ascii_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_SQL_ASCII);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_SQL_ASCII, PG_UTF8);
 
        /* this looks wrong, but basically we're just rejecting high-bit-set */
        pg_ascii2mic(src, dest, len);
@@ -56,9 +54,7 @@ utf8_to_ascii(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_SQL_ASCII);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_SQL_ASCII);
 
        /* this looks wrong, but basically we're just rejecting high-bit-set */
        pg_mic2ascii(src, dest, len);
index 42a5bd4411f2439f0fc5a15dadaa998b99ababa3..f4f9c582f31126e0f65e4ab09ff3d1fbf0c24650 100644 (file)
@@ -40,9 +40,7 @@ big5_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_BIG5);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_BIG5, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapBIG5,
                           sizeof(LUmapBIG5) / sizeof(pg_local_to_utf), PG_BIG5, len);
@@ -57,9 +55,7 @@ utf8_to_big5(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_BIG5);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_BIG5);
 
        UtfToLocal(src, dest, ULmapBIG5,
                           sizeof(ULmapBIG5) / sizeof(pg_utf_to_local), PG_BIG5, len);
index 98715e075bf6bec72bf8116cf4c241d147a0fe00..c9f1ea98119f86aa5361a331261f487636ca7f3f 100644 (file)
@@ -53,9 +53,7 @@ utf8_to_koi8r(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_KOI8R);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8R);
 
        UtfToLocal(src, dest, ULmap_KOI8R,
                           sizeof(ULmap_KOI8R) / sizeof(pg_utf_to_local), PG_KOI8R, len);
@@ -70,9 +68,7 @@ koi8r_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_KOI8R);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapKOI8R,
                           sizeof(LUmapKOI8R) / sizeof(pg_local_to_utf), PG_KOI8R, len);
@@ -87,9 +83,7 @@ utf8_to_win1251(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1251);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_WIN1251);
 
        UtfToLocal(src, dest, ULmap_WIN1251,
                           sizeof(ULmap_WIN1251) / sizeof(pg_utf_to_local), PG_WIN1251, len);
@@ -104,9 +98,7 @@ win1251_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1251);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1251, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapWIN1251,
                        sizeof(LUmapWIN1251) / sizeof(pg_local_to_utf), PG_WIN1251, len);
@@ -121,9 +113,7 @@ utf8_to_win866(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_WIN866);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_WIN866);
 
        UtfToLocal(src, dest, ULmap_WIN866,
                           sizeof(ULmap_WIN866) / sizeof(pg_utf_to_local), PG_WIN866, len);
@@ -138,9 +128,7 @@ win866_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN866);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN866, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapWIN866,
                           sizeof(LUmapWIN866) / sizeof(pg_local_to_utf), PG_WIN866, len);
index ff5a995ff94fb5dad11b9fc8b309469689e088cb..027232f50bd4a939757b592b3c73d61206d39fe7 100644 (file)
@@ -40,9 +40,7 @@ euc_cn_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_EUC_CN);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_CN, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapEUC_CN,
                           sizeof(LUmapEUC_CN) / sizeof(pg_local_to_utf), PG_EUC_CN, len);
@@ -57,9 +55,7 @@ utf8_to_euc_cn(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_CN);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_EUC_CN);
 
        UtfToLocal(src, dest, ULmapEUC_CN,
                           sizeof(ULmapEUC_CN) / sizeof(pg_utf_to_local), PG_EUC_CN, len);
index bd07c9c3da1f1dd71d2d6572c5e32de3b3081cd5..bb3f1c8f2ec49d1c90dea959bc97c873131e0256 100644 (file)
@@ -40,9 +40,7 @@ euc_jp_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_EUC_JP);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JP, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapEUC_JP,
                           sizeof(LUmapEUC_JP) / sizeof(pg_local_to_utf), PG_EUC_JP, len);
@@ -57,9 +55,7 @@ utf8_to_euc_jp(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_JP);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_EUC_JP);
 
        UtfToLocal(src, dest, ULmapEUC_JP,
                           sizeof(ULmapEUC_JP) / sizeof(pg_utf_to_local), PG_EUC_JP, len);
index bee3fd99f7d204f9221df8e178f380a600e6c071..04df59e3ea88a5b659f5f06e9d48765902d8616a 100644 (file)
@@ -40,9 +40,7 @@ euc_kr_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_EUC_KR);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_KR, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapEUC_KR,
                           sizeof(LUmapEUC_KR) / sizeof(pg_local_to_utf), PG_EUC_KR, len);
@@ -57,9 +55,7 @@ utf8_to_euc_kr(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_KR);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_EUC_KR);
 
        UtfToLocal(src, dest, ULmapEUC_KR,
                           sizeof(ULmapEUC_KR) / sizeof(pg_utf_to_local), PG_EUC_KR, len);
index 175bede0c6ca12c7cf4f6796e0e391bd063a1a4e..17c2f53edaaad1f65ab8a58de66a0cce63d3eedc 100644 (file)
@@ -40,9 +40,7 @@ euc_tw_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_EUC_TW);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_TW, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapEUC_TW,
                           sizeof(LUmapEUC_TW) / sizeof(pg_local_to_utf), PG_EUC_TW, len);
@@ -57,9 +55,7 @@ utf8_to_euc_tw(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_TW);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_EUC_TW);
 
        UtfToLocal(src, dest, ULmapEUC_TW,
                           sizeof(ULmapEUC_TW) / sizeof(pg_utf_to_local), PG_EUC_TW, len);
index a675cddabf5017df3f3cfcd47a31c78c49e5649f..0a7e38458b5f7d7355815294a224d4eeb44add53 100644 (file)
@@ -40,9 +40,7 @@ gb18030_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_GB18030);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_GB18030, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapGB18030,
                        sizeof(LUmapGB18030) / sizeof(pg_local_to_utf), PG_GB18030, len);
@@ -57,9 +55,7 @@ utf8_to_gb18030(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_GB18030);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_GB18030);
 
        UtfToLocal(src, dest, ULmapGB18030,
                           sizeof(ULmapGB18030) / sizeof(pg_utf_to_local), PG_GB18030, len);
index 3cbbaa985bf7c6102cf71caa158271511ac5a0f1..bc638adc6ef3d725f459094163be101af19087dd 100644 (file)
@@ -40,9 +40,7 @@ gbk_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_GBK);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_GBK, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapGBK,
                           sizeof(LUmapGBK) / sizeof(pg_local_to_utf), PG_GBK, len);
@@ -57,9 +55,7 @@ utf8_to_gbk(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_GBK);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_GBK);
 
        UtfToLocal(src, dest, ULmapGBK,
                           sizeof(ULmapGBK) / sizeof(pg_utf_to_local), PG_GBK, len);
index f4cc007daecf18f466608c615367844f1563ca13..614a779e138ae593be6d01ce45a1ee797be25eaa 100644 (file)
@@ -118,8 +118,7 @@ iso8859_to_utf8(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        int     i;
 
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(-1, PG_UTF8);
 
        for (i=0;i<sizeof(maps)/sizeof(pg_conv_map);i++)
        {
@@ -146,8 +145,7 @@ utf8_to_iso8859(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        int     i;
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, -1);
 
        for (i=0;i<sizeof(maps)/sizeof(pg_conv_map);i++)
        {
index ae38859a7e046449537fc45d78fbd1080f993536..a8ec8a7984c0ee0e663c43163851d6aea81834e2 100644 (file)
@@ -40,9 +40,7 @@ iso8859_1_to_utf8(PG_FUNCTION_ARGS)
        int                     len = PG_GETARG_INT32(4);
        unsigned short c;
 
-       Assert(PG_GETARG_INT32(0) == PG_LATIN1);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN1, PG_UTF8);
 
        while (len > 0)
        {
@@ -73,9 +71,7 @@ utf8_to_iso8859_1(PG_FUNCTION_ARGS)
        unsigned short c,
                                c1;
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_LATIN1);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_LATIN1);
 
        while (len > 0)
        {
index 2ad3319d3cc34c445b47f75e2263007c544c66d5..8ef030659d361ee50110bde43b4a2a145eb3b4bd 100644 (file)
@@ -40,9 +40,7 @@ johab_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_JOHAB);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_JOHAB, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapJOHAB,
                           sizeof(LUmapJOHAB) / sizeof(pg_local_to_utf), PG_JOHAB, len);
@@ -57,9 +55,7 @@ utf8_to_johab(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_JOHAB);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_JOHAB);
 
        UtfToLocal(src, dest, ULmapJOHAB,
                           sizeof(ULmapJOHAB) / sizeof(pg_utf_to_local), PG_JOHAB, len);
index 864d9daddc345ea15e4328042885c0ceb26e7292..dc4f1ce75fd588a522f2cf573e23ea227c37219b 100644 (file)
@@ -40,9 +40,7 @@ sjis_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_SJIS);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_SJIS, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapSJIS,
                           sizeof(LUmapSJIS) / sizeof(pg_local_to_utf), PG_SJIS, len);
@@ -57,9 +55,7 @@ utf8_to_sjis(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_SJIS);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_SJIS);
 
        UtfToLocal(src, dest, ULmapSJIS,
                           sizeof(ULmapSJIS) / sizeof(pg_utf_to_local), PG_SJIS, len);
index 84ca5c0abe38ff96b04a768443d2ed691b05cc3a..8e5f63ac7852cd619adf4f84ac7b0f1f18142e46 100644 (file)
@@ -40,9 +40,7 @@ uhc_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UHC);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UHC, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapUHC,
                           sizeof(LUmapUHC) / sizeof(pg_local_to_utf), PG_UHC, len);
@@ -57,9 +55,7 @@ utf8_to_uhc(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_UHC);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_UHC);
 
        UtfToLocal(src, dest, ULmapUHC,
                           sizeof(ULmapUHC) / sizeof(pg_utf_to_local), PG_UHC, len);
index ca35da82ca6cce7a74ef1c10f58d933ca07f311f..8caf80f1d75157e1f28aeffc4b2868e1f45554d8 100644 (file)
@@ -41,9 +41,7 @@ utf8_to_win1250(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1250);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_WIN1250);
 
        UtfToLocal(src, dest, ULmapWIN1250,
                           sizeof(ULmapWIN1250) / sizeof(pg_utf_to_local), PG_WIN1250, len);
@@ -58,9 +56,7 @@ win1250_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1250);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1250, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapWIN1250,
                        sizeof(LUmapWIN1250) / sizeof(pg_local_to_utf), PG_WIN1250, len);
index 61ecbb481c5cf30ee3b582b317b441bb3e5451d5..fd1cb5306994bf077ad472d3d6c6f9144955ecfe 100644 (file)
@@ -41,9 +41,7 @@ utf8_to_win1252(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1252);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_WIN1252);
 
        UtfToLocal(src, dest, ULmapWIN1252,
                           sizeof(ULmapWIN1252) / sizeof(pg_utf_to_local), PG_WIN1252, len);
@@ -58,9 +56,7 @@ win1252_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1252);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1252, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapWIN1252,
                        sizeof(LUmapWIN1252) / sizeof(pg_local_to_utf), PG_WIN1252, len);
index e704634a2c81f2d4cadc5d3d10e2ba9fab07106b..9f24ef190a0fdf30218c25ede5f00a0b3829ff80 100644 (file)
@@ -41,9 +41,7 @@ utf8_to_win1256(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1256);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_WIN1256);
 
        UtfToLocal(src, dest, ULmapWIN1256,
                           sizeof(ULmapWIN1256) / sizeof(pg_utf_to_local), PG_WIN1256, len);
@@ -58,9 +56,7 @@ win1256_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1256);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1256, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapWIN1256,
                        sizeof(LUmapWIN1256) / sizeof(pg_local_to_utf), PG_WIN1256, len);
index 238c5eca8c36600382e1b770269ae6bfc0cbe18c..22db5fbe7f686b850ee86b0d7c844c29b37550d9 100644 (file)
@@ -40,9 +40,7 @@ win1258_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN1258);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN1258, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapWIN1258,
                        sizeof(LUmapWIN1258) / sizeof(pg_local_to_utf), PG_WIN1258, len);
@@ -57,9 +55,7 @@ utf8_to_win1258(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_WIN1258);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_WIN1258);
 
        UtfToLocal(src, dest, ULmapWIN1258,
                           sizeof(ULmapWIN1258) / sizeof(pg_utf_to_local), PG_WIN1258, len);
index b75c6c52037908ba37ee6769bb8622b22adf6e7f..3726d259a6b92600a2998396e3f0bf2d57b722ef 100644 (file)
@@ -41,9 +41,7 @@ utf8_to_win874(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_UTF8);
-       Assert(PG_GETARG_INT32(1) == PG_WIN874);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_WIN874);
 
        UtfToLocal(src, dest, ULmapWIN874,
                           sizeof(ULmapWIN874) / sizeof(pg_utf_to_local), PG_WIN874, len);
@@ -58,9 +56,7 @@ win874_to_utf8(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_WIN874);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_WIN874, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapWIN874,
                           sizeof(LUmapWIN874) / sizeof(pg_local_to_utf), PG_WIN874, len);
index a187f76526e75f554b66e41a53974a1b3042ff16..793d5d6e657a2eb355621c501b9499fa338ae6bd 100644 (file)
@@ -1326,6 +1326,39 @@ pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
        return true;
 }
 
+/*
+ * check_encoding_conversion_args: check arguments of a conversion function
+ *
+ * "expected" arguments can be either an encoding ID or -1 to indicate that
+ * the caller will check whether it accepts the ID.
+ *
+ * Note: the errors here are not really user-facing, so elog instead of
+ * ereport seems sufficient.  Also, we trust that the "expected" encoding
+ * arguments are valid encoding IDs, but we don't trust the actuals.
+ */
+void
+check_encoding_conversion_args(int src_encoding,
+                                                          int dest_encoding,
+                                                          int len,
+                                                          int expected_src_encoding,
+                                                          int expected_dest_encoding)
+{
+       if (!PG_VALID_ENCODING(src_encoding))
+               elog(ERROR, "invalid source encoding ID: %d", src_encoding);
+       if (src_encoding != expected_src_encoding && expected_src_encoding >= 0)
+               elog(ERROR, "expected source encoding \"%s\", but got \"%s\"",
+                        pg_enc2name_tbl[expected_src_encoding].name,
+                        pg_enc2name_tbl[src_encoding].name);
+       if (!PG_VALID_ENCODING(dest_encoding))
+               elog(ERROR, "invalid destination encoding ID: %d", dest_encoding);
+       if (dest_encoding != expected_dest_encoding && expected_dest_encoding >= 0)
+               elog(ERROR, "expected destination encoding \"%s\", but got \"%s\"",
+                        pg_enc2name_tbl[expected_dest_encoding].name,
+                        pg_enc2name_tbl[dest_encoding].name);
+       if (len < 0)
+               elog(ERROR, "encoding conversion length must not be negative");
+}
+
 /*
  * report_invalid_encoding: complain about invalid multibyte character
  *
index cb2c66a53ed60636b52b4d8f4a88e462a5558f8c..1d8ea0f78d2d7797c7e8ee0b5603028bb220f432 100644 (file)
@@ -286,6 +286,19 @@ typedef struct
        unsigned int utf;                       /* UTF8 */
 } pg_local_to_utf;
 
+/*
+ * Support macro for encoding conversion functions to validate their
+ * arguments.  (This could be made more compact if we included fmgr.h
+ * here, but we don't want to do that because this header file is also
+ * used by frontends.)
+ */
+#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding,destencoding) \
+       check_encoding_conversion_args(PG_GETARG_INT32(0), \
+                                                                  PG_GETARG_INT32(1), \
+                                                                  PG_GETARG_INT32(4), \
+                                                                  (srcencoding), \
+                                                                  (destencoding))
+
 extern int     pg_mb2wchar(const char *from, pg_wchar *to);
 extern int     pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len);
 extern int     pg_char_and_wchar_strcmp(const char *s1, const pg_wchar *s2);
@@ -340,6 +353,12 @@ extern bool pg_verifymbstr(const char *mbstr, int len, bool noError);
 extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len,
                                                        bool noError);
 
+extern void check_encoding_conversion_args(int src_encoding,
+                                                                                  int dest_encoding,
+                                                                                  int len,
+                                                                                  int expected_src_encoding,
+                                                                                  int expected_dest_encoding);
+
 extern void report_invalid_encoding(int encoding, const char *mbstr, int len);
 extern void report_untranslatable_char(int src_encoding, int dest_encoding,
                                                                           const char *mbstr, int len);