PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
euc_tw_and_big5.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * EUC_TW, BIG5 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_tw_and_big5/euc_tw_and_big5.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 #include "postgres.h"
15 #include "fmgr.h"
16 #include "mb/pg_wchar.h"
17 
18 #define ENCODING_GROWTH_RATE 4
19 
21 
28 
29 /* ----------
30  * conv_proc(
31  * INTEGER, -- source encoding id
32  * INTEGER, -- destination encoding id
33  * CSTRING, -- source string (null terminated C string)
34  * CSTRING, -- destination string (null terminated C string)
35  * INTEGER -- source string length
36  * ) returns VOID;
37  * ----------
38  */
39 
40 static void big52mic(const unsigned char *big5, unsigned char *p, int len);
41 static void mic2big5(const unsigned char *mic, unsigned char *p, int len);
42 static void euc_tw2mic(const unsigned char *euc, unsigned char *p, int len);
43 static void mic2euc_tw(const unsigned char *mic, unsigned char *p, int len);
44 
45 Datum
47 {
48  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
49  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
50  int len = PG_GETARG_INT32(4);
51  unsigned char *buf;
52 
54 
55  buf = palloc(len * ENCODING_GROWTH_RATE + 1);
56  euc_tw2mic(src, buf, len);
57  mic2big5(buf, dest, strlen((char *) buf));
58  pfree(buf);
59 
61 }
62 
63 Datum
65 {
66  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
67  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
68  int len = PG_GETARG_INT32(4);
69  unsigned char *buf;
70 
72 
73  buf = palloc(len * ENCODING_GROWTH_RATE + 1);
74  big52mic(src, buf, len);
75  mic2euc_tw(buf, dest, strlen((char *) buf));
76  pfree(buf);
77 
79 }
80 
81 Datum
83 {
84  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
85  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
86  int len = PG_GETARG_INT32(4);
87 
89 
90  euc_tw2mic(src, dest, len);
91 
93 }
94 
95 Datum
97 {
98  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
99  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
100  int len = PG_GETARG_INT32(4);
101 
103 
104  mic2euc_tw(src, dest, len);
105 
106  PG_RETURN_VOID();
107 }
108 
109 Datum
111 {
112  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
113  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
114  int len = PG_GETARG_INT32(4);
115 
117 
118  big52mic(src, dest, len);
119 
120  PG_RETURN_VOID();
121 }
122 
123 Datum
125 {
126  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
127  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
128  int len = PG_GETARG_INT32(4);
129 
131 
132  mic2big5(src, dest, len);
133 
134  PG_RETURN_VOID();
135 }
136 
137 /*
138  * EUC_TW ---> MIC
139  */
140 static void
141 euc_tw2mic(const unsigned char *euc, unsigned char *p, int len)
142 {
143  int c1;
144  int l;
145 
146  while (len > 0)
147  {
148  c1 = *euc;
149  if (IS_HIGHBIT_SET(c1))
150  {
151  l = pg_encoding_verifymb(PG_EUC_TW, (const char *) euc, len);
152  if (l < 0)
154  (const char *) euc, len);
155  if (c1 == SS2)
156  {
157  c1 = euc[1]; /* plane No. */
158  if (c1 == 0xa1)
159  *p++ = LC_CNS11643_1;
160  else if (c1 == 0xa2)
161  *p++ = LC_CNS11643_2;
162  else
163  {
164  /* other planes are MULE private charsets */
165  *p++ = LCPRV2_B;
166  *p++ = c1 - 0xa3 + LC_CNS11643_3;
167  }
168  *p++ = euc[2];
169  *p++ = euc[3];
170  }
171  else
172  { /* CNS11643-1 */
173  *p++ = LC_CNS11643_1;
174  *p++ = c1;
175  *p++ = euc[1];
176  }
177  euc += l;
178  len -= l;
179  }
180  else
181  { /* should be ASCII */
182  if (c1 == 0)
184  (const char *) euc, len);
185  *p++ = c1;
186  euc++;
187  len--;
188  }
189  }
190  *p = '\0';
191 }
192 
193 /*
194  * MIC ---> EUC_TW
195  */
196 static void
197 mic2euc_tw(const unsigned char *mic, unsigned char *p, int len)
198 {
199  int c1;
200  int l;
201 
202  while (len > 0)
203  {
204  c1 = *mic;
205  if (!IS_HIGHBIT_SET(c1))
206  {
207  /* ASCII */
208  if (c1 == 0)
210  (const char *) mic, len);
211  *p++ = c1;
212  mic++;
213  len--;
214  continue;
215  }
216  l = pg_encoding_verifymb(PG_MULE_INTERNAL, (const char *) mic, len);
217  if (l < 0)
219  (const char *) mic, len);
220  if (c1 == LC_CNS11643_1)
221  {
222  *p++ = mic[1];
223  *p++ = mic[2];
224  }
225  else if (c1 == LC_CNS11643_2)
226  {
227  *p++ = SS2;
228  *p++ = 0xa2;
229  *p++ = mic[1];
230  *p++ = mic[2];
231  }
232  else if (c1 == LCPRV2_B &&
233  mic[1] >= LC_CNS11643_3 && mic[1] <= LC_CNS11643_7)
234  {
235  *p++ = SS2;
236  *p++ = mic[1] - LC_CNS11643_3 + 0xa3;
237  *p++ = mic[2];
238  *p++ = mic[3];
239  }
240  else
242  (const char *) mic, len);
243  mic += l;
244  len -= l;
245  }
246  *p = '\0';
247 }
248 
249 /*
250  * Big5 ---> MIC
251  */
252 static void
253 big52mic(const unsigned char *big5, unsigned char *p, int len)
254 {
255  unsigned short c1;
256  unsigned short big5buf,
257  cnsBuf;
258  unsigned char lc;
259  int l;
260 
261  while (len > 0)
262  {
263  c1 = *big5;
264  if (!IS_HIGHBIT_SET(c1))
265  {
266  /* ASCII */
267  if (c1 == 0)
269  (const char *) big5, len);
270  *p++ = c1;
271  big5++;
272  len--;
273  continue;
274  }
275  l = pg_encoding_verifymb(PG_BIG5, (const char *) big5, len);
276  if (l < 0)
278  (const char *) big5, len);
279  big5buf = (c1 << 8) | big5[1];
280  cnsBuf = BIG5toCNS(big5buf, &lc);
281  if (lc != 0)
282  {
283  /* Planes 3 and 4 are MULE private charsets */
284  if (lc == LC_CNS11643_3 || lc == LC_CNS11643_4)
285  *p++ = LCPRV2_B;
286  *p++ = lc; /* Plane No. */
287  *p++ = (cnsBuf >> 8) & 0x00ff;
288  *p++ = cnsBuf & 0x00ff;
289  }
290  else
292  (const char *) big5, len);
293  big5 += l;
294  len -= l;
295  }
296  *p = '\0';
297 }
298 
299 /*
300  * MIC ---> Big5
301  */
302 static void
303 mic2big5(const unsigned char *mic, unsigned char *p, int len)
304 {
305  unsigned short c1;
306  unsigned short big5buf,
307  cnsBuf;
308  int l;
309 
310  while (len > 0)
311  {
312  c1 = *mic;
313  if (!IS_HIGHBIT_SET(c1))
314  {
315  /* ASCII */
316  if (c1 == 0)
318  (const char *) mic, len);
319  *p++ = c1;
320  mic++;
321  len--;
322  continue;
323  }
324  l = pg_encoding_verifymb(PG_MULE_INTERNAL, (const char *) mic, len);
325  if (l < 0)
327  (const char *) mic, len);
328  if (c1 == LC_CNS11643_1 || c1 == LC_CNS11643_2 || c1 == LCPRV2_B)
329  {
330  if (c1 == LCPRV2_B)
331  {
332  c1 = mic[1]; /* get plane no. */
333  cnsBuf = (mic[2] << 8) | mic[3];
334  }
335  else
336  {
337  cnsBuf = (mic[1] << 8) | mic[2];
338  }
339  big5buf = CNStoBIG5(cnsBuf, c1);
340  if (big5buf == 0)
342  (const char *) mic, len);
343  *p++ = (big5buf >> 8) & 0x00ff;
344  *p++ = big5buf & 0x00ff;
345  }
346  else
348  (const char *) mic, len);
349  mic += l;
350  len -= l;
351  }
352  *p = '\0';
353 }
Datum mic_to_euc_tw(PG_FUNCTION_ARGS)
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define LC_CNS11643_1
Definition: pg_wchar.h:134
#define LC_CNS11643_4
Definition: pg_wchar.h:203
static void big52mic(const unsigned char *big5, unsigned char *p, int len)
Datum euc_tw_to_big5(PG_FUNCTION_ARGS)
#define ENCODING_GROWTH_RATE
int pg_encoding_verifymb(int encoding, const char *mbstr, int len)
Definition: wchar.c:1809
PG_FUNCTION_INFO_V1(euc_tw_to_big5)
Datum big5_to_euc_tw(PG_FUNCTION_ARGS)
void pfree(void *pointer)
Definition: mcxt.c:992
#define IS_HIGHBIT_SET(ch)
Definition: c.h:969
#define LC_CNS11643_7
Definition: pg_wchar.h:206
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: wchar.c:2027
unsigned short CNStoBIG5(unsigned short cns, unsigned char lc)
Definition: big5.c:345
static char * buf
Definition: pg_test_fsync.c:65
static void mic2euc_tw(const unsigned char *mic, unsigned char *p, int len)
static void mic2big5(const unsigned char *mic, unsigned char *p, int len)
uintptr_t Datum
Definition: postgres.h:374
unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc)
Definition: big5.c:292
#define LCPRV2_B
Definition: pg_wchar.h:161
#define PG_RETURN_VOID()
Definition: fmgr.h:293
#define LC_CNS11643_2
Definition: pg_wchar.h:135
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition: pg_wchar.h:428
#define LC_CNS11643_3
Definition: pg_wchar.h:202
static void euc_tw2mic(const unsigned char *euc, unsigned char *p, int len)
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
PG_MODULE_MAGIC
void * palloc(Size size)
Definition: mcxt.c:891
Datum big5_to_mic(PG_FUNCTION_ARGS)
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:233
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
Datum euc_tw_to_mic(PG_FUNCTION_ARGS)
Datum mic_to_big5(PG_FUNCTION_ARGS)
#define SS2
Definition: pg_wchar.h:35