PostgreSQL Source Code git master
euc_cn_and_mic.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * EUC_CN 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_cn_and_mic/euc_cn_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_cn_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_cn2mic(const unsigned char *euc, unsigned char *p, int len, bool noError);
41static int mic2euc_cn(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_cn2mic(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_cn(src, dest, len, noError);
71
72 PG_RETURN_INT32(converted);
73}
74
75/*
76 * EUC_CN ---> MIC
77 */
78static int
79euc_cn2mic(const unsigned char *euc, unsigned char *p, int len, bool noError)
80{
81 const unsigned char *start = euc;
82 int c1;
83
84 while (len > 0)
85 {
86 c1 = *euc;
87 if (IS_HIGHBIT_SET(c1))
88 {
89 if (len < 2 || !IS_HIGHBIT_SET(euc[1]))
90 {
91 if (noError)
92 break;
93 report_invalid_encoding(PG_EUC_CN, (const char *) euc, len);
94 }
95 *p++ = LC_GB2312_80;
96 *p++ = c1;
97 *p++ = euc[1];
98 euc += 2;
99 len -= 2;
100 }
101 else
102 { /* should be ASCII */
103 if (c1 == 0)
104 {
105 if (noError)
106 break;
107 report_invalid_encoding(PG_EUC_CN, (const char *) euc, len);
108 }
109 *p++ = c1;
110 euc++;
111 len--;
112 }
113 }
114 *p = '\0';
115
116 return euc - start;
117}
118
119/*
120 * MIC ---> EUC_CN
121 */
122static int
123mic2euc_cn(const unsigned char *mic, unsigned char *p, int len, bool noError)
124{
125 const unsigned char *start = mic;
126 int c1;
127
128 while (len > 0)
129 {
130 c1 = *mic;
131 if (IS_HIGHBIT_SET(c1))
132 {
133 if (c1 != LC_GB2312_80)
134 {
135 if (noError)
136 break;
138 (const char *) mic, len);
139 }
140 if (len < 3 || !IS_HIGHBIT_SET(mic[1]) || !IS_HIGHBIT_SET(mic[2]))
141 {
142 if (noError)
143 break;
145 (const char *) mic, len);
146 }
147 mic++;
148 *p++ = *mic++;
149 *p++ = *mic++;
150 len -= 3;
151 }
152 else
153 { /* should be ASCII */
154 if (c1 == 0)
155 {
156 if (noError)
157 break;
159 (const char *) mic, len);
160 }
161 *p++ = c1;
162 mic++;
163 len--;
164 }
165 }
166 *p = '\0';
167
168 return mic - start;
169}
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1126
Datum mic_to_euc_cn(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(euc_cn_to_mic)
static int mic2euc_cn(const unsigned char *mic, unsigned char *p, int len, bool noError)
Datum euc_cn_to_mic(PG_FUNCTION_ARGS)
PG_MODULE_MAGIC_EXT(.name="euc_cn_and_mic",.version=PG_VERSION)
static int euc_cn2mic(const unsigned char *euc, unsigned char *p, int len, bool noError)
#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_CN
Definition: pg_wchar.h:228
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition: pg_wchar.h:507
#define LC_GB2312_80
Definition: pg_wchar.h:133
uintptr_t Datum
Definition: postgres.h:69
const char * name