PostgreSQL Source Code  git master
euc2004_sjis2004.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * EUC_JIS_2004, SHIFT_JIS_2004
4  *
5  * Copyright (c) 2007-2024, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/backend/utils/mb/conversion_procs/euc2004_sjis2004/euc2004_sjis2004.c
9  *
10  *-------------------------------------------------------------------------
11  */
12 
13 #include "postgres.h"
14 #include "fmgr.h"
15 #include "mb/pg_wchar.h"
16 
18 
21 
22 static int euc_jis_20042shift_jis_2004(const unsigned char *euc, unsigned char *p, int len, bool noError);
23 static int shift_jis_20042euc_jis_2004(const unsigned char *sjis, unsigned char *p, int len, bool noError);
24 
25 /* ----------
26  * conv_proc(
27  * INTEGER, -- source encoding id
28  * INTEGER, -- destination encoding id
29  * CSTRING, -- source string (null terminated C string)
30  * CSTRING, -- destination string (null terminated C string)
31  * INTEGER, -- source string length
32  * BOOL -- if true, don't throw an error if conversion fails
33  * ) returns INTEGER;
34  *
35  * Returns the number of bytes successfully converted.
36  * ----------
37  */
38 
39 Datum
41 {
42  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
43  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
44  int len = PG_GETARG_INT32(4);
45  bool noError = PG_GETARG_BOOL(5);
46  int converted;
47 
49 
50  converted = euc_jis_20042shift_jis_2004(src, dest, len, noError);
51 
52  PG_RETURN_INT32(converted);
53 }
54 
55 Datum
57 {
58  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
59  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
60  int len = PG_GETARG_INT32(4);
61  bool noError = PG_GETARG_BOOL(5);
62  int converted;
63 
65 
66  converted = shift_jis_20042euc_jis_2004(src, dest, len, noError);
67 
68  PG_RETURN_INT32(converted);
69 }
70 
71 /*
72  * EUC_JIS_2004 -> SHIFT_JIS_2004
73  */
74 static int
75 euc_jis_20042shift_jis_2004(const unsigned char *euc, unsigned char *p, int len, bool noError)
76 {
77  const unsigned char *start = euc;
78  int c1,
79  ku,
80  ten;
81  int l;
82 
83  while (len > 0)
84  {
85  c1 = *euc;
86  if (!IS_HIGHBIT_SET(c1))
87  {
88  /* ASCII */
89  if (c1 == 0)
90  {
91  if (noError)
92  break;
94  (const char *) euc, len);
95  }
96  *p++ = c1;
97  euc++;
98  len--;
99  continue;
100  }
101 
102  l = pg_encoding_verifymbchar(PG_EUC_JIS_2004, (const char *) euc, len);
103 
104  if (l < 0)
105  {
106  if (noError)
107  break;
109  (const char *) euc, len);
110  }
111 
112  if (c1 == SS2 && l == 2) /* JIS X 0201 kana? */
113  {
114  *p++ = euc[1];
115  }
116  else if (c1 == SS3 && l == 3) /* JIS X 0213 plane 2? */
117  {
118  ku = euc[1] - 0xa0;
119  ten = euc[2] - 0xa0;
120 
121  switch (ku)
122  {
123  case 1:
124  case 3:
125  case 4:
126  case 5:
127  case 8:
128  case 12:
129  case 13:
130  case 14:
131  case 15:
132  *p++ = ((ku + 0x1df) >> 1) - (ku >> 3) * 3;
133  break;
134  default:
135  if (ku >= 78 && ku <= 94)
136  {
137  *p++ = (ku + 0x19b) >> 1;
138  }
139  else
140  {
141  if (noError)
142  break;
144  (const char *) euc, len);
145  }
146  }
147 
148  if (ku % 2)
149  {
150  if (ten >= 1 && ten <= 63)
151  *p++ = ten + 0x3f;
152  else if (ten >= 64 && ten <= 94)
153  *p++ = ten + 0x40;
154  else
155  {
156  if (noError)
157  break;
159  (const char *) euc, len);
160  }
161  }
162  else
163  *p++ = ten + 0x9e;
164  }
165 
166  else if (l == 2) /* JIS X 0213 plane 1? */
167  {
168  ku = c1 - 0xa0;
169  ten = euc[1] - 0xa0;
170 
171  if (ku >= 1 && ku <= 62)
172  *p++ = (ku + 0x101) >> 1;
173  else if (ku >= 63 && ku <= 94)
174  *p++ = (ku + 0x181) >> 1;
175  else
176  {
177  if (noError)
178  break;
180  (const char *) euc, len);
181  }
182 
183  if (ku % 2)
184  {
185  if (ten >= 1 && ten <= 63)
186  *p++ = ten + 0x3f;
187  else if (ten >= 64 && ten <= 94)
188  *p++ = ten + 0x40;
189  else
190  {
191  if (noError)
192  break;
194  (const char *) euc, len);
195  }
196  }
197  else
198  *p++ = ten + 0x9e;
199  }
200  else
201  {
202  if (noError)
203  break;
205  (const char *) euc, len);
206  }
207 
208  euc += l;
209  len -= l;
210  }
211  *p = '\0';
212 
213  return euc - start;
214 }
215 
216 /*
217  * returns SHIFT_JIS_2004 "ku" code indicated by second byte
218  * *ku = 0: "ku" = even
219  * *ku = 1: "ku" = odd
220  */
221 static int
222 get_ten(int b, int *ku)
223 {
224  int ten;
225 
226  if (b >= 0x40 && b <= 0x7e)
227  {
228  ten = b - 0x3f;
229  *ku = 1;
230  }
231  else if (b >= 0x80 && b <= 0x9e)
232  {
233  ten = b - 0x40;
234  *ku = 1;
235  }
236  else if (b >= 0x9f && b <= 0xfc)
237  {
238  ten = b - 0x9e;
239  *ku = 0;
240  }
241  else
242  {
243  ten = -1; /* error */
244  *ku = 0; /* keep compiler quiet */
245  }
246  return ten;
247 }
248 
249 /*
250  * SHIFT_JIS_2004 ---> EUC_JIS_2004
251  */
252 
253 static int
254 shift_jis_20042euc_jis_2004(const unsigned char *sjis, unsigned char *p, int len, bool noError)
255 {
256  const unsigned char *start = sjis;
257  int c1;
258  int ku,
259  ten,
260  kubun;
261  int plane;
262  int l;
263 
264  while (len > 0)
265  {
266  c1 = *sjis;
267 
268  if (!IS_HIGHBIT_SET(c1))
269  {
270  /* ASCII */
271  if (c1 == 0)
272  {
273  if (noError)
274  break;
276  (const char *) sjis, len);
277  }
278  *p++ = c1;
279  sjis++;
280  len--;
281  continue;
282  }
283 
284  l = pg_encoding_verifymbchar(PG_SHIFT_JIS_2004, (const char *) sjis, len);
285 
286  if (l < 0 || l > len)
287  {
288  if (noError)
289  break;
291  (const char *) sjis, len);
292  }
293 
294  if (c1 >= 0xa1 && c1 <= 0xdf && l == 1)
295  {
296  /* JIS X0201 (1 byte kana) */
297  *p++ = SS2;
298  *p++ = c1;
299  }
300  else if (l == 2)
301  {
302  int c2 = sjis[1];
303 
304  plane = 1;
305  ku = 1;
306  ten = 1;
307 
308  /*
309  * JIS X 0213
310  */
311  if (c1 >= 0x81 && c1 <= 0x9f) /* plane 1 1ku-62ku */
312  {
313  ku = (c1 << 1) - 0x100;
314  ten = get_ten(c2, &kubun);
315  if (ten < 0)
316  {
317  if (noError)
318  break;
320  (const char *) sjis, len);
321  }
322  ku -= kubun;
323  }
324  else if (c1 >= 0xe0 && c1 <= 0xef) /* plane 1 62ku-94ku */
325  {
326  ku = (c1 << 1) - 0x180;
327  ten = get_ten(c2, &kubun);
328  if (ten < 0)
329  {
330  if (noError)
331  break;
333  (const char *) sjis, len);
334  }
335  ku -= kubun;
336  }
337  else if (c1 >= 0xf0 && c1 <= 0xf3) /* plane 2
338  * 1,3,4,5,8,12,13,14,15 ku */
339  {
340  plane = 2;
341  ten = get_ten(c2, &kubun);
342  if (ten < 0)
343  {
344  if (noError)
345  break;
347  (const char *) sjis, len);
348  }
349  switch (c1)
350  {
351  case 0xf0:
352  ku = kubun == 0 ? 8 : 1;
353  break;
354  case 0xf1:
355  ku = kubun == 0 ? 4 : 3;
356  break;
357  case 0xf2:
358  ku = kubun == 0 ? 12 : 5;
359  break;
360  default:
361  ku = kubun == 0 ? 14 : 13;
362  break;
363  }
364  }
365  else if (c1 >= 0xf4 && c1 <= 0xfc) /* plane 2 78-94ku */
366  {
367  plane = 2;
368  ten = get_ten(c2, &kubun);
369  if (ten < 0)
370  {
371  if (noError)
372  break;
374  (const char *) sjis, len);
375  }
376  if (c1 == 0xf4 && kubun == 1)
377  ku = 15;
378  else
379  ku = (c1 << 1) - 0x19a - kubun;
380  }
381  else
382  {
383  if (noError)
384  break;
386  (const char *) sjis, len);
387  }
388 
389  if (plane == 2)
390  *p++ = SS3;
391 
392  *p++ = ku + 0xa0;
393  *p++ = ten + 0xa0;
394  }
395  sjis += l;
396  len -= l;
397  }
398  *p = '\0';
399 
400  return sjis - start;
401 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1134
static int euc_jis_20042shift_jis_2004(const unsigned char *euc, unsigned char *p, int len, bool noError)
static int get_ten(int b, int *ku)
PG_MODULE_MAGIC
Datum shift_jis_2004_to_euc_jis_2004(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(euc_jis_2004_to_shift_jis_2004)
static int shift_jis_20042euc_jis_2004(const unsigned char *sjis, unsigned char *p, int len, bool noError)
Datum euc_jis_2004_to_shift_jis_2004(PG_FUNCTION_ARGS)
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
return str start
int b
Definition: isn.c:69
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: mbutils.c:1698
const void size_t len
@ PG_SHIFT_JIS_2004
Definition: pg_wchar.h:270
@ PG_EUC_JIS_2004
Definition: pg_wchar.h:231
#define SS2
Definition: pg_wchar.h:38
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition: pg_wchar.h:507
#define SS3
Definition: pg_wchar.h:39
uintptr_t Datum
Definition: postgres.h:64
int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len)
Definition: wchar.c:2103