PostgreSQL Source Code git master
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-2025, 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 .name = "euc_kr_and_mic",
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_kr2mic(const unsigned char *euc, unsigned char *p, int len, bool noError);
41static int mic2euc_kr(const unsigned char *mic, 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_kr2mic(src, dest, len, noError);
55
56 PG_RETURN_INT32(converted);
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 = mic2euc_kr(src, dest, len, noError);
71
72 PG_RETURN_INT32(converted);
73}
74
75/*
76 * EUC_KR ---> MIC
77 */
78static int
79euc_kr2mic(const unsigned char *euc, unsigned char *p, int len, bool noError)
80{
81 const unsigned char *start = euc;
82 int c1;
83 int l;
84
85 while (len > 0)
86 {
87 c1 = *euc;
88 if (IS_HIGHBIT_SET(c1))
89 {
90 l = pg_encoding_verifymbchar(PG_EUC_KR, (const char *) euc, len);
91 if (l != 2)
92 {
93 if (noError)
94 break;
96 (const char *) euc, len);
97 }
98 *p++ = LC_KS5601;
99 *p++ = c1;
100 *p++ = euc[1];
101 euc += 2;
102 len -= 2;
103 }
104 else
105 { /* should be ASCII */
106 if (c1 == 0)
107 {
108 if (noError)
109 break;
111 (const char *) euc, len);
112 }
113 *p++ = c1;
114 euc++;
115 len--;
116 }
117 }
118 *p = '\0';
119
120 return euc - start;
121}
122
123/*
124 * MIC ---> EUC_KR
125 */
126static int
127mic2euc_kr(const unsigned char *mic, unsigned char *p, int len, bool noError)
128{
129 const unsigned char *start = mic;
130 int c1;
131 int l;
132
133 while (len > 0)
134 {
135 c1 = *mic;
136 if (!IS_HIGHBIT_SET(c1))
137 {
138 /* ASCII */
139 if (c1 == 0)
140 {
141 if (noError)
142 break;
144 (const char *) mic, len);
145 }
146 *p++ = c1;
147 mic++;
148 len--;
149 continue;
150 }
151 l = pg_encoding_verifymbchar(PG_MULE_INTERNAL, (const char *) mic, len);
152 if (l < 0)
153 {
154 if (noError)
155 break;
157 (const char *) mic, len);
158 }
159 if (c1 == LC_KS5601)
160 {
161 *p++ = mic[1];
162 *p++ = mic[2];
163 }
164 else
165 {
166 if (noError)
167 break;
169 (const char *) mic, len);
170 }
171 mic += l;
172 len -= l;
173 }
174 *p = '\0';
175
176 return mic - start;
177}
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1126
static int mic2euc_kr(const unsigned char *mic, unsigned char *p, int len, bool noError)
static int euc_kr2mic(const unsigned char *euc, unsigned char *p, int len, bool noError)
PG_MODULE_MAGIC_EXT(.name="euc_kr_and_mic",.version=PG_VERSION)
PG_FUNCTION_INFO_V1(euc_kr_to_mic)
Datum mic_to_euc_kr(PG_FUNCTION_ARGS)
Datum euc_kr_to_mic(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
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: mbutils.c:1730
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: mbutils.c:1698
const void size_t len
@ PG_MULE_INTERNAL
Definition: pg_wchar.h:233
@ PG_EUC_KR
Definition: pg_wchar.h:229
#define LC_KS5601
Definition: pg_wchar.h:135
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition: pg_wchar.h:507
uintptr_t Datum
Definition: postgres.h:69
const char * name
int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len)
Definition: wchar.c:2150