Replace argument-checking Asserts with regular test-and-elog checks in all
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 29 Jan 2009 19:23:42 +0000 (19:23 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 29 Jan 2009 19:23:42 +0000 (19:23 +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.

28 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_jis_2004_and_shift_jis_2004/euc_jis_2004_and_shift_jis_2004.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_jis_2004/utf8_and_euc_jis_2004.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_shift_jis_2004/utf8_and_shift_jis_2004.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_win/utf8_and_win.c
src/backend/utils/mb/wchar.c
src/include/mb/pg_wchar.h

index ad34b13945c662fc4a77449188c20870cbc4a94b..dad976cbad62d097e326c5157de4283205dc93aa 100644 (file)
@@ -41,9 +41,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);
 
@@ -57,9 +55,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 7af6a4bfa3f8c03a74f0419539cb18c1d564400a..11612701d11aeb88d057b0645de36d121a7b2f8b 100644 (file)
@@ -88,9 +88,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);
 
@@ -104,9 +102,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);
 
@@ -120,9 +116,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);
 
@@ -136,9 +130,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);
 
@@ -152,9 +144,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);
 
@@ -168,9 +158,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);
 
@@ -184,9 +172,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);
 
@@ -200,9 +186,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);
 
@@ -217,9 +201,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);
@@ -237,9 +219,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);
@@ -257,9 +237,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);
@@ -277,9 +255,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);
@@ -297,9 +273,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
@@ -323,9 +297,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);
@@ -344,9 +316,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);
@@ -364,9 +334,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);
@@ -384,9 +352,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);
@@ -405,9 +371,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);
@@ -426,9 +390,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);
@@ -447,9 +409,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 3905b540504f54422c4ba1f744a14529d1eab2ad..b23b2f90e197d75b80e9da93a4291bc120a803d9 100644 (file)
@@ -44,9 +44,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);
 
@@ -60,9 +58,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 f8b81b225f40deeb7380d7037077e439bdb0813b..a98bd1e99f2511bbf8937191d54365d8553a0e4d 100644 (file)
@@ -43,9 +43,7 @@ euc_jis_2004_to_shift_jis_2004(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_JIS_2004);
-       Assert(PG_GETARG_INT32(1) == PG_SHIFT_JIS_2004);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JIS_2004, PG_SHIFT_JIS_2004);
 
        euc_jis_20042shift_jis_2004(src, dest, len);
 
@@ -59,9 +57,7 @@ shift_jis_2004_to_euc_jis_2004(PG_FUNCTION_ARGS)
        unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
        int                     len = PG_GETARG_INT32(4);
 
-       Assert(PG_GETARG_INT32(0) == PG_SHIFT_JIS_2004);
-       Assert(PG_GETARG_INT32(1) == PG_EUC_JIS_2004);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_SHIFT_JIS_2004, PG_EUC_JIS_2004);
 
        shift_jis_20042euc_jis_2004(src, dest, len);
 
index fd4c80aa7155dea71c9a608895a54e71115af814..b2267ab16702963f46e3e70c34aa6fd0e33fcaee 100644 (file)
@@ -70,9 +70,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);
 
@@ -86,9 +84,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);
 
@@ -102,9 +98,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);
 
@@ -118,9 +112,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);
 
@@ -134,9 +126,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);
 
@@ -150,9 +140,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 d254fdf0bf89579e6a88942a5ac8188a5e39be3c..b540c3d8cdde6796d47f645e76f279ec4e3f7cb8 100644 (file)
@@ -44,9 +44,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);
 
@@ -60,9 +58,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 57ef534d3df53fa390722312ef6e24b13afd5ee2..396c4fabe5da1d43834b60c8ccd04fb4a78833b5 100644 (file)
@@ -57,9 +57,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);
@@ -77,9 +75,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);
@@ -96,9 +92,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);
 
@@ -112,9 +106,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);
 
@@ -128,9 +120,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);
 
@@ -144,9 +134,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 bba161e245597609e1a842ef2059b7435e8de233..f3ebfb76abd478cd93ef4c4083c28fea6f098029 100644 (file)
@@ -56,9 +56,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);
 
@@ -72,9 +70,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);
 
@@ -88,9 +84,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);
 
@@ -104,9 +98,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);
 
@@ -121,9 +113,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);
@@ -141,9 +131,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 8fcffc96ca70a5bd30a74f6b5f92e156aff11c85..74ad7ea53616c242ce6ad1f63cba51b097489746 100644 (file)
@@ -56,9 +56,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);
 
@@ -72,9 +70,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);
 
@@ -88,9 +84,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);
 
@@ -104,9 +98,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);
 
@@ -120,9 +112,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);
 
@@ -136,9 +126,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 44163932ba31b9166fe2f23ec4a94d775929386d..84233b6286cc1c5695ed510e9b6d1842b452b4b7 100644 (file)
@@ -41,9 +41,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);
@@ -58,9 +56,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 4aeeb1b5213d361163963fa41d37382c6c23d71e..3c97edecac90d8fe885ba674b7814c241e4bea70 100644 (file)
@@ -42,9 +42,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, NULL,
                           sizeof(LUmapBIG5) / sizeof(pg_local_to_utf), 0, PG_BIG5, len);
@@ -59,9 +57,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, NULL,
                           sizeof(ULmapBIG5) / sizeof(pg_utf_to_local), 0, PG_BIG5, len);
index 0787ed5e352132b45889b214914d2291eb88c2ce..81eb0d6c3fc0634a33ac9666a61b8858ece7e506 100644 (file)
@@ -43,9 +43,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, ULmapKOI8R, NULL,
                         sizeof(ULmapKOI8R) / sizeof(pg_utf_to_local), 0, PG_KOI8R, len);
@@ -60,9 +58,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, NULL,
                         sizeof(LUmapKOI8R) / sizeof(pg_local_to_utf), 0, PG_KOI8R, len);
index c1c141107823baa8277b2b7d0fc24d3b6340cfd2..a8f293bfa4f00e36429aa6e9ab64b338e11cd646 100644 (file)
@@ -42,9 +42,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, NULL,
                   sizeof(LUmapEUC_CN) / sizeof(pg_local_to_utf), 0, PG_EUC_CN, len);
@@ -59,9 +57,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, NULL,
                   sizeof(ULmapEUC_CN) / sizeof(pg_utf_to_local), 0, PG_EUC_CN, len);
index f9fc6a67121613983bdacdd55614090ef6451260..e1e0d989e22aa29c329e2c0af48f27cfde92cf9c 100644 (file)
@@ -44,9 +44,7 @@ euc_jis_2004_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_JIS_2004);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JIS_2004, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapEUC_JIS_2004, LUmapEUC_JIS_2004_combined,
                           sizeof(LUmapEUC_JIS_2004) / sizeof(pg_local_to_utf),
@@ -63,9 +61,7 @@ utf8_to_euc_jis_2004(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_JIS_2004);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_EUC_JIS_2004);
 
        UtfToLocal(src, dest, ULmapEUC_JIS_2004, ULmapEUC_JIS_2004_combined,
                           sizeof(ULmapEUC_JIS_2004) / sizeof(pg_utf_to_local),
index bba1a01cad4e35c3a3275465b2ad532d934f783e..edc8796efde7b9c7db71bf3360495fd78ada2a70 100644 (file)
@@ -42,9 +42,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, NULL,
                   sizeof(LUmapEUC_JP) / sizeof(pg_local_to_utf), 0, PG_EUC_JP, len);
@@ -59,9 +57,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, NULL,
                   sizeof(ULmapEUC_JP) / sizeof(pg_utf_to_local), 0, PG_EUC_JP, len);
index 3aea319eccc5da9a71a5bccb40c6ccad2ab35b09..3f29775aa5619487699f3531ac1a703fd1164ea5 100644 (file)
@@ -42,9 +42,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, NULL,
                   sizeof(LUmapEUC_KR) / sizeof(pg_local_to_utf), 0, PG_EUC_KR, len);
@@ -59,9 +57,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, NULL,
                   sizeof(ULmapEUC_KR) / sizeof(pg_utf_to_local), 0, PG_EUC_KR, len);
index c2f1974b62c0a1731f6e9373077acbcf2508bc83..25e5c4f89016f93a64d5c27f0eea45683f0f4e5c 100644 (file)
@@ -42,9 +42,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, NULL,
                   sizeof(LUmapEUC_TW) / sizeof(pg_local_to_utf), 0, PG_EUC_TW, len);
@@ -59,9 +57,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, NULL,
                   sizeof(ULmapEUC_TW) / sizeof(pg_utf_to_local), 0, PG_EUC_TW, len);
index 91e52f417a24a3ee5851c7e8fd43f720170920d6..b59212e59523938bdda2be6fcdc86531a7523cad 100644 (file)
@@ -42,9 +42,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, NULL,
                 sizeof(LUmapGB18030) / sizeof(pg_local_to_utf), 0, PG_GB18030, len);
@@ -59,9 +57,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, NULL,
                 sizeof(ULmapGB18030) / sizeof(pg_utf_to_local), 0, PG_GB18030, len);
index 1792753332ec65f3db95f34fb63de7ed071c8dab..fc6adadb97bbcb407577008bf29cab8ca49aa09c 100644 (file)
@@ -42,9 +42,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, NULL,
                           sizeof(LUmapGBK) / sizeof(pg_local_to_utf), 0, PG_GBK, len);
@@ -59,9 +57,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, NULL,
                           sizeof(ULmapGBK) / sizeof(pg_utf_to_local), 0, PG_GBK, len);
index e568147da22a6855d6de5f6876b63a6f408f445f..5c278b50b380550dd2fb40dc27d01d11633660c7 100644 (file)
@@ -120,8 +120,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++)
        {
@@ -148,8 +147,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 b3f2a0bd17eb3ec9b8f39cccb3d429bebce628d1..fd0b6080605533a02c31cc9a871c9486e185ad91 100644 (file)
@@ -42,9 +42,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)
        {
@@ -75,9 +73,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 bc49a7acffd796a41a1db981ebdb7681cdd18d85..2902a74aa0b9ec9c6d7e721b56f27148ea50dd4b 100644 (file)
@@ -42,9 +42,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, NULL,
                         sizeof(LUmapJOHAB) / sizeof(pg_local_to_utf), 0, PG_JOHAB, len);
@@ -59,9 +57,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, NULL,
                         sizeof(ULmapJOHAB) / sizeof(pg_utf_to_local), 0, PG_JOHAB, len);
index 5e37bad4af7ffceaad4f925daf259dd3c52412e8..cfd7506cc5aa3a916f12d5781b5a8dacf4cdb529 100644 (file)
@@ -44,9 +44,7 @@ shift_jis_2004_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_SHIFT_JIS_2004);
-       Assert(PG_GETARG_INT32(1) == PG_UTF8);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_SHIFT_JIS_2004, PG_UTF8);
 
        LocalToUtf(src, dest, LUmapSHIFT_JIS_2004, LUmapSHIFT_JIS_2004_combined,
                           sizeof(LUmapSHIFT_JIS_2004) / sizeof(pg_local_to_utf),
@@ -63,9 +61,7 @@ utf8_to_shift_jis_2004(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_SHIFT_JIS_2004);
-       Assert(len >= 0);
+       CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_SHIFT_JIS_2004);
 
        UtfToLocal(src, dest, ULmapSHIFT_JIS_2004, ULmapSHIFT_JIS_2004_combined,
                           sizeof(ULmapSHIFT_JIS_2004) / sizeof(pg_utf_to_local),
index 34515a135a163ccd911b2b24842a22711618efe2..0f120813bfcf78a2dd9802396c32f1afaeeb2b49 100644 (file)
@@ -42,9 +42,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, NULL,
                           sizeof(LUmapSJIS) / sizeof(pg_local_to_utf), 0, PG_SJIS, len);
@@ -59,9 +57,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, NULL,
                           sizeof(ULmapSJIS) / sizeof(pg_utf_to_local), 0, PG_SJIS, len);
index 12a0aed8aa5b02065381b2f4eeceda2978dc74bc..68dd9ed1763074b6c29cb89a821b6cdd412eee91 100644 (file)
@@ -42,9 +42,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, NULL,
                           sizeof(LUmapUHC) / sizeof(pg_local_to_utf), 0, PG_UHC, len);
@@ -59,9 +57,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, NULL,
                           sizeof(ULmapUHC) / sizeof(pg_utf_to_local), 0, PG_UHC, len);
index 3676a51a08a4f9eea3316644d2ed73cca47c82b1..8be5f68978d74addd2602df4e34176c950fdd785 100644 (file)
@@ -110,8 +110,7 @@ win_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++)
        {
@@ -138,8 +137,7 @@ utf8_to_win(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 9a6dc00ccc492750f16318ad0c1d0394310a20dc..5df4b18b6541472ce6fd0c033d459185807c971b 100644 (file)
@@ -1549,6 +1549,39 @@ pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
        return mb_len;
 }
 
+/*
+ * 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 27c686f90454dcc3d6bf07c0f32608dce3b901bd..2a1009f06b2b0661cbb1e30302a7f1e63ccf579b 100644 (file)
@@ -325,6 +325,19 @@ typedef struct
        uint32          utf2;                   /* UTF-8 code 2 */
 } pg_local_to_utf_combined;
 
+/*
+ * 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))
+
 
 /*
  * These functions are considered part of libpq's exported API and
@@ -408,6 +421,12 @@ extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len,
 extern int pg_verify_mbstr_len(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);