PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
euc_kr_and_mic.c
Go to the documentation of this file.
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 
19 
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
39 {
40  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
41  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
42  int len = PG_GETARG_INT32(4);
43 
45 
46  euc_kr2mic(src, dest, len);
47 
49 }
50 
51 Datum
53 {
54  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
55  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
56  int len = PG_GETARG_INT32(4);
57 
59 
60  mic2euc_kr(src, dest, len);
61 
63 }
64 
65 /*
66  * EUC_KR ---> MIC
67  */
68 static void
69 euc_kr2mic(const unsigned char *euc, unsigned char *p, int len)
70 {
71  int c1;
72  int l;
73 
74  while (len > 0)
75  {
76  c1 = *euc;
77  if (IS_HIGHBIT_SET(c1))
78  {
79  l = pg_encoding_verifymb(PG_EUC_KR, (const char *) euc, len);
80  if (l != 2)
82  (const char *) euc, len);
83  *p++ = LC_KS5601;
84  *p++ = c1;
85  *p++ = euc[1];
86  euc += 2;
87  len -= 2;
88  }
89  else
90  { /* should be ASCII */
91  if (c1 == 0)
93  (const char *) euc, len);
94  *p++ = c1;
95  euc++;
96  len--;
97  }
98  }
99  *p = '\0';
100 }
101 
102 /*
103  * MIC ---> EUC_KR
104  */
105 static void
106 mic2euc_kr(const unsigned char *mic, unsigned char *p, int len)
107 {
108  int c1;
109  int l;
110 
111  while (len > 0)
112  {
113  c1 = *mic;
114  if (!IS_HIGHBIT_SET(c1))
115  {
116  /* ASCII */
117  if (c1 == 0)
119  (const char *) mic, len);
120  *p++ = c1;
121  mic++;
122  len--;
123  continue;
124  }
125  l = pg_encoding_verifymb(PG_MULE_INTERNAL, (const char *) mic, len);
126  if (l < 0)
128  (const char *) mic, len);
129  if (c1 == LC_KS5601)
130  {
131  *p++ = mic[1];
132  *p++ = mic[2];
133  }
134  else
136  (const char *) mic, len);
137  mic += l;
138  len -= l;
139  }
140  *p = '\0';
141 }
Datum mic_to_euc_kr(PG_FUNCTION_ARGS)
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int pg_encoding_verifymb(int encoding, const char *mbstr, int len)
Definition: wchar.c:1809
PG_MODULE_MAGIC
#define IS_HIGHBIT_SET(ch)
Definition: c.h:973
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: wchar.c:2027
PG_FUNCTION_INFO_V1(euc_kr_to_mic)
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_VOID()
Definition: fmgr.h:309
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition: pg_wchar.h:492
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
static void mic2euc_kr(const unsigned char *mic, unsigned char *p, int len)
static void euc_kr2mic(const unsigned char *euc, unsigned char *p, int len)
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
Datum euc_kr_to_mic(PG_FUNCTION_ARGS)
#define LC_KS5601
Definition: pg_wchar.h:132