PostgreSQL Source Code git master
Loading...
Searching...
No Matches
euc_tw_and_big5.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * EUC_TW and BIG5
4 *
5 * Portions Copyright (c) 1996-2026, 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
19 .name = "euc_tw_and_big5",
20 .version = PG_VERSION
21);
22
25
26/* ----------
27 * conv_proc(
28 * INTEGER, -- source encoding id
29 * INTEGER, -- destination encoding id
30 * CSTRING, -- source string (null terminated C string)
31 * CSTRING, -- destination string (null terminated C string)
32 * INTEGER, -- source string length
33 * BOOL -- if true, don't throw an error if conversion fails
34 * ) returns INTEGER;
35 *
36 * Returns the number of bytes successfully converted.
37 * ----------
38 */
39
40static int euc_tw2big5(const unsigned char *euc, unsigned char *p, int len, bool noError);
41static int big52euc_tw(const unsigned char *big5, unsigned char *p, int len, bool noError);
42
45{
46 unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
47 unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
48 int len = PG_GETARG_INT32(4);
49 bool noError = PG_GETARG_BOOL(5);
50 int converted;
51
53
54 converted = euc_tw2big5(src, dest, len, noError);
55
57}
58
61{
62 unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
63 unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
64 int len = PG_GETARG_INT32(4);
65 bool noError = PG_GETARG_BOOL(5);
66 int converted;
67
69
70 converted = big52euc_tw(src, dest, len, noError);
71
73}
74
75static int
76euc_tw2big5(const unsigned char *euc, unsigned char *p, int len, bool noError)
77{
78 const unsigned char *start = euc;
79 unsigned char c1;
80 unsigned short big5buf,
81 cnsBuf;
82 unsigned char lc;
83 int l;
84
85 while (len > 0)
86 {
87 c1 = *euc;
88 if (IS_HIGHBIT_SET(c1))
89 {
90 /* Verify and decode the next EUC_TW input character */
91 l = pg_encoding_verifymbchar(PG_EUC_TW, (const char *) euc, len);
92 if (l < 0)
93 {
94 if (noError)
95 break;
97 (const char *) euc, len);
98 }
99 if (c1 == SS2)
100 {
101 c1 = euc[1]; /* plane No. */
102 if (c1 == 0xa1)
104 else if (c1 == 0xa2)
106 else
107 lc = c1 - 0xa3 + LC_CNS11643_3;
108 cnsBuf = (euc[2] << 8) | euc[3];
109 }
110 else
111 { /* CNS11643-1 */
113 cnsBuf = (c1 << 8) | euc[1];
114 }
115
116 /* Write it out in Big5 */
118 if (big5buf == 0)
119 {
120 if (noError)
121 break;
123 (const char *) euc, len);
124 }
125 *p++ = (big5buf >> 8) & 0x00ff;
126 *p++ = big5buf & 0x00ff;
127
128 euc += l;
129 len -= l;
130 }
131 else
132 { /* should be ASCII */
133 if (c1 == 0)
134 {
135 if (noError)
136 break;
138 (const char *) euc, len);
139 }
140 *p++ = c1;
141 euc++;
142 len--;
143 }
144 }
145 *p = '\0';
146
147 return euc - start;
148}
149
150/*
151 * Big5 ---> EUC_TW
152 */
153static int
154big52euc_tw(const unsigned char *big5, unsigned char *p, int len, bool noError)
155{
156 const unsigned char *start = big5;
157 unsigned short c1;
158 unsigned short big5buf,
159 cnsBuf;
160 unsigned char lc;
161 int l;
162
163 while (len > 0)
164 {
165 /* Verify and decode the next Big5 input character */
166 c1 = *big5;
167 if (IS_HIGHBIT_SET(c1))
168 {
169 l = pg_encoding_verifymbchar(PG_BIG5, (const char *) big5, len);
170 if (l < 0)
171 {
172 if (noError)
173 break;
175 (const char *) big5, len);
176 }
177 big5buf = (c1 << 8) | big5[1];
179
180 if (lc == LC_CNS11643_1)
181 {
182 *p++ = (cnsBuf >> 8) & 0x00ff;
183 *p++ = cnsBuf & 0x00ff;
184 }
185 else if (lc == LC_CNS11643_2)
186 {
187 *p++ = SS2;
188 *p++ = 0xa2;
189 *p++ = (cnsBuf >> 8) & 0x00ff;
190 *p++ = cnsBuf & 0x00ff;
191 }
192 else if (lc >= LC_CNS11643_3 && lc <= LC_CNS11643_7)
193 {
194 *p++ = SS2;
195 *p++ = lc - LC_CNS11643_3 + 0xa3;
196 *p++ = (cnsBuf >> 8) & 0x00ff;
197 *p++ = cnsBuf & 0x00ff;
198 }
199 else
200 {
201 if (noError)
202 break;
204 (const char *) big5, len);
205 }
206
207 big5 += l;
208 len -= l;
209 }
210 else
211 {
212 /* ASCII */
213 if (c1 == 0)
214 {
215 if (noError)
216 break;
218 (const char *) big5, len);
219 }
220 *p++ = c1;
221 big5++;
222 len--;
223 continue;
224 }
225 }
226 *p = '\0';
227
228 return big5 - start;
229}
unsigned short CNStoBIG5(unsigned short cns, unsigned char lc)
Definition big5.c:344
unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc)
Definition big5.c:291
#define IS_HIGHBIT_SET(ch)
Definition c.h:1244
Datum big5_to_euc_tw(PG_FUNCTION_ARGS)
Datum euc_tw_to_big5(PG_FUNCTION_ARGS)
static int euc_tw2big5(const unsigned char *euc, unsigned char *p, int len, bool noError)
static int big52euc_tw(const unsigned char *big5, unsigned char *p, int len, bool noError)
#define PG_MODULE_MAGIC_EXT(...)
Definition fmgr.h:540
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#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
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition mbutils.c:1869
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition mbutils.c:1824
const void size_t len
#define LC_CNS11643_7
Definition pg_wchar.h:50
@ PG_EUC_TW
Definition pg_wchar.h:80
@ PG_BIG5
Definition pg_wchar.h:115
#define LC_CNS11643_3
Definition pg_wchar.h:46
#define SS2
Definition pg_wchar.h:38
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition pg_wchar.h:360
#define LC_CNS11643_1
Definition pg_wchar.h:44
#define LC_CNS11643_2
Definition pg_wchar.h:45
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
const char * name
int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len)
Definition wchar.c:1988