Header And Logo

PostgreSQL
| The world's most advanced open source database.

euc_kr_and_mic.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  *    EUC_KR and MULE_INTERNAL
00004  *
00005  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00006  * Portions Copyright (c) 1994, Regents of the University of California
00007  *
00008  * IDENTIFICATION
00009  *    src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 
00014 #include "postgres.h"
00015 #include "fmgr.h"
00016 #include "mb/pg_wchar.h"
00017 
00018 PG_MODULE_MAGIC;
00019 
00020 PG_FUNCTION_INFO_V1(euc_kr_to_mic);
00021 PG_FUNCTION_INFO_V1(mic_to_euc_kr);
00022 
00023 extern Datum euc_kr_to_mic(PG_FUNCTION_ARGS);
00024 extern Datum mic_to_euc_kr(PG_FUNCTION_ARGS);
00025 
00026 /* ----------
00027  * conv_proc(
00028  *      INTEGER,    -- source encoding id
00029  *      INTEGER,    -- destination encoding id
00030  *      CSTRING,    -- source string (null terminated C string)
00031  *      CSTRING,    -- destination string (null terminated C string)
00032  *      INTEGER     -- source string length
00033  * ) returns VOID;
00034  * ----------
00035  */
00036 
00037 static void euc_kr2mic(const unsigned char *euc, unsigned char *p, int len);
00038 static void mic2euc_kr(const unsigned char *mic, unsigned char *p, int len);
00039 
00040 Datum
00041 euc_kr_to_mic(PG_FUNCTION_ARGS)
00042 {
00043     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00044     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00045     int         len = PG_GETARG_INT32(4);
00046 
00047     CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_KR, PG_MULE_INTERNAL);
00048 
00049     euc_kr2mic(src, dest, len);
00050 
00051     PG_RETURN_VOID();
00052 }
00053 
00054 Datum
00055 mic_to_euc_kr(PG_FUNCTION_ARGS)
00056 {
00057     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
00058     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
00059     int         len = PG_GETARG_INT32(4);
00060 
00061     CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_EUC_KR);
00062 
00063     mic2euc_kr(src, dest, len);
00064 
00065     PG_RETURN_VOID();
00066 }
00067 
00068 /*
00069  * EUC_KR ---> MIC
00070  */
00071 static void
00072 euc_kr2mic(const unsigned char *euc, unsigned char *p, int len)
00073 {
00074     int         c1;
00075     int         l;
00076 
00077     while (len > 0)
00078     {
00079         c1 = *euc;
00080         if (IS_HIGHBIT_SET(c1))
00081         {
00082             l = pg_encoding_verifymb(PG_EUC_KR, (const char *) euc, len);
00083             if (l != 2)
00084                 report_invalid_encoding(PG_EUC_KR,
00085                                         (const char *) euc, len);
00086             *p++ = LC_KS5601;
00087             *p++ = c1;
00088             *p++ = euc[1];
00089             euc += 2;
00090             len -= 2;
00091         }
00092         else
00093         {                       /* should be ASCII */
00094             if (c1 == 0)
00095                 report_invalid_encoding(PG_EUC_KR,
00096                                         (const char *) euc, len);
00097             *p++ = c1;
00098             euc++;
00099             len--;
00100         }
00101     }
00102     *p = '\0';
00103 }
00104 
00105 /*
00106  * MIC ---> EUC_KR
00107  */
00108 static void
00109 mic2euc_kr(const unsigned char *mic, unsigned char *p, int len)
00110 {
00111     int         c1;
00112     int         l;
00113 
00114     while (len > 0)
00115     {
00116         c1 = *mic;
00117         if (!IS_HIGHBIT_SET(c1))
00118         {
00119             /* ASCII */
00120             if (c1 == 0)
00121                 report_invalid_encoding(PG_MULE_INTERNAL,
00122                                         (const char *) mic, len);
00123             *p++ = c1;
00124             mic++;
00125             len--;
00126             continue;
00127         }
00128         l = pg_encoding_verifymb(PG_MULE_INTERNAL, (const char *) mic, len);
00129         if (l < 0)
00130             report_invalid_encoding(PG_MULE_INTERNAL,
00131                                     (const char *) mic, len);
00132         if (c1 == LC_KS5601)
00133         {
00134             *p++ = mic[1];
00135             *p++ = mic[2];
00136         }
00137         else
00138             report_untranslatable_char(PG_MULE_INTERNAL, PG_EUC_KR,
00139                                        (const char *) mic, len);
00140         mic += l;
00141         len -= l;
00142     }
00143     *p = '\0';
00144 }