Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * EUC_KR and MULE_INTERNAL
4 : *
5 : * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
6 : * Portions Copyright (c) 1994, Regents of the University of California
7 : *
8 : * IDENTIFICATION
9 : * src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c
10 : *
11 : *-------------------------------------------------------------------------
12 : */
13 :
14 : #include "postgres.h"
15 : #include "fmgr.h"
16 : #include "mb/pg_wchar.h"
17 :
18 4 : PG_MODULE_MAGIC;
19 :
20 6 : PG_FUNCTION_INFO_V1(euc_kr_to_mic);
21 6 : PG_FUNCTION_INFO_V1(mic_to_euc_kr);
22 :
23 : /* ----------
24 : * conv_proc(
25 : * INTEGER, -- source encoding id
26 : * INTEGER, -- destination encoding id
27 : * CSTRING, -- source string (null terminated C string)
28 : * CSTRING, -- destination string (null terminated C string)
29 : * INTEGER -- source string length
30 : * ) returns VOID;
31 : * ----------
32 : */
33 :
34 : static void euc_kr2mic(const unsigned char *euc, unsigned char *p, int len);
35 : static void mic2euc_kr(const unsigned char *mic, unsigned char *p, int len);
36 :
37 : Datum
38 4 : euc_kr_to_mic(PG_FUNCTION_ARGS)
39 : {
40 4 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
41 4 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
42 4 : int len = PG_GETARG_INT32(4);
43 :
44 4 : CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_KR, PG_MULE_INTERNAL);
45 :
46 4 : euc_kr2mic(src, dest, len);
47 :
48 4 : PG_RETURN_VOID();
49 : }
50 :
51 : Datum
52 4 : mic_to_euc_kr(PG_FUNCTION_ARGS)
53 : {
54 4 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
55 4 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
56 4 : int len = PG_GETARG_INT32(4);
57 :
58 4 : CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_EUC_KR);
59 :
60 4 : mic2euc_kr(src, dest, len);
61 :
62 4 : PG_RETURN_VOID();
63 : }
64 :
65 : /*
66 : * EUC_KR ---> MIC
67 : */
68 : static void
69 4 : euc_kr2mic(const unsigned char *euc, unsigned char *p, int len)
70 : {
71 : int c1;
72 : int l;
73 :
74 14 : while (len > 0)
75 : {
76 6 : c1 = *euc;
77 6 : if (IS_HIGHBIT_SET(c1))
78 : {
79 0 : l = pg_encoding_verifymb(PG_EUC_KR, (const char *) euc, len);
80 0 : if (l != 2)
81 0 : report_invalid_encoding(PG_EUC_KR,
82 : (const char *) euc, len);
83 0 : *p++ = LC_KS5601;
84 0 : *p++ = c1;
85 0 : *p++ = euc[1];
86 0 : euc += 2;
87 0 : len -= 2;
88 : }
89 : else
90 : { /* should be ASCII */
91 6 : if (c1 == 0)
92 0 : report_invalid_encoding(PG_EUC_KR,
93 : (const char *) euc, len);
94 6 : *p++ = c1;
95 6 : euc++;
96 6 : len--;
97 : }
98 : }
99 4 : *p = '\0';
100 4 : }
101 :
102 : /*
103 : * MIC ---> EUC_KR
104 : */
105 : static void
106 4 : mic2euc_kr(const unsigned char *mic, unsigned char *p, int len)
107 : {
108 : int c1;
109 : int l;
110 :
111 14 : while (len > 0)
112 : {
113 6 : c1 = *mic;
114 6 : if (!IS_HIGHBIT_SET(c1))
115 : {
116 : /* ASCII */
117 6 : if (c1 == 0)
118 0 : report_invalid_encoding(PG_MULE_INTERNAL,
119 : (const char *) mic, len);
120 6 : *p++ = c1;
121 6 : mic++;
122 6 : len--;
123 6 : continue;
124 : }
125 0 : l = pg_encoding_verifymb(PG_MULE_INTERNAL, (const char *) mic, len);
126 0 : if (l < 0)
127 0 : report_invalid_encoding(PG_MULE_INTERNAL,
128 : (const char *) mic, len);
129 0 : if (c1 == LC_KS5601)
130 : {
131 0 : *p++ = mic[1];
132 0 : *p++ = mic[2];
133 : }
134 : else
135 0 : report_untranslatable_char(PG_MULE_INTERNAL, PG_EUC_KR,
136 : (const char *) mic, len);
137 0 : mic += l;
138 0 : len -= l;
139 : }
140 4 : *p = '\0';
141 4 : }
|