PostgreSQL Source Code git master
utf8_and_gb18030.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "mb/pg_wchar.h"
#include "../../Unicode/gb18030_to_utf8.map"
#include "../../Unicode/utf8_to_gb18030.map"
Include dependency graph for utf8_and_gb18030.c:

Go to the source code of this file.

Macros

#define conv18030(minunicode, mincode, maxcode)
 
#define convutf8(minunicode, maxunicode, mincode)
 

Functions

 PG_MODULE_MAGIC_EXT (.name="utf8_and_gb18030",.version=PG_VERSION)
 
 PG_FUNCTION_INFO_V1 (gb18030_to_utf8)
 
 PG_FUNCTION_INFO_V1 (utf8_to_gb18030)
 
static uint32 gb_linear (uint32 gb)
 
static uint32 gb_unlinear (uint32 lin)
 
static uint32 unicode_to_utf8word (uint32 c)
 
static uint32 utf8word_to_unicode (uint32 c)
 
static uint32 conv_18030_to_utf8 (uint32 code)
 
static uint32 conv_utf8_to_18030 (uint32 code)
 
Datum gb18030_to_utf8 (PG_FUNCTION_ARGS)
 
Datum utf8_to_gb18030 (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ conv18030

#define conv18030 (   minunicode,
  mincode,
  maxcode 
)
Value:
if (code >= mincode && code <= maxcode) \
return unicode_to_utf8word(gb_linear(code) - gb_linear(mincode) + minunicode)
static uint32 unicode_to_utf8word(uint32 c)
static uint32 gb_linear(uint32 gb)

◆ convutf8

#define convutf8 (   minunicode,
  maxunicode,
  mincode 
)
Value:
if (ucs >= minunicode && ucs <= maxunicode) \
return gb_unlinear(ucs - minunicode + gb_linear(mincode))
static uint32 gb_unlinear(uint32 lin)

Function Documentation

◆ conv_18030_to_utf8()

static uint32 conv_18030_to_utf8 ( uint32  code)
static

Definition at line 136 of file utf8_and_gb18030.c.

137{
138#define conv18030(minunicode, mincode, maxcode) \
139 if (code >= mincode && code <= maxcode) \
140 return unicode_to_utf8word(gb_linear(code) - gb_linear(mincode) + minunicode)
141
142 conv18030(0x0452, 0x8130D330, 0x8136A531);
143 conv18030(0x2643, 0x8137A839, 0x8138FD38);
144 conv18030(0x361B, 0x8230A633, 0x8230F237);
145 conv18030(0x3CE1, 0x8231D438, 0x8232AF32);
146 conv18030(0x4160, 0x8232C937, 0x8232F837);
147 conv18030(0x44D7, 0x8233A339, 0x8233C931);
148 conv18030(0x478E, 0x8233E838, 0x82349638);
149 conv18030(0x49B8, 0x8234A131, 0x8234E733);
150 conv18030(0x9FA6, 0x82358F33, 0x8336C738);
151 conv18030(0xE865, 0x8336D030, 0x84308534);
152 conv18030(0xFA2A, 0x84309C38, 0x84318537);
153 conv18030(0xFFE6, 0x8431A234, 0x8431A439);
154 conv18030(0x10000, 0x90308130, 0xE3329A35);
155 /* No mapping exists */
156 return 0;
157}
#define conv18030(minunicode, mincode, maxcode)

References conv18030.

Referenced by gb18030_to_utf8().

◆ conv_utf8_to_18030()

static uint32 conv_utf8_to_18030 ( uint32  code)
static

Definition at line 163 of file utf8_and_gb18030.c.

164{
165 uint32 ucs = utf8word_to_unicode(code);
166
167#define convutf8(minunicode, maxunicode, mincode) \
168 if (ucs >= minunicode && ucs <= maxunicode) \
169 return gb_unlinear(ucs - minunicode + gb_linear(mincode))
170
171 convutf8(0x0452, 0x200F, 0x8130D330);
172 convutf8(0x2643, 0x2E80, 0x8137A839);
173 convutf8(0x361B, 0x3917, 0x8230A633);
174 convutf8(0x3CE1, 0x4055, 0x8231D438);
175 convutf8(0x4160, 0x4336, 0x8232C937);
176 convutf8(0x44D7, 0x464B, 0x8233A339);
177 convutf8(0x478E, 0x4946, 0x8233E838);
178 convutf8(0x49B8, 0x4C76, 0x8234A131);
179 convutf8(0x9FA6, 0xD7FF, 0x82358F33);
180 convutf8(0xE865, 0xF92B, 0x8336D030);
181 convutf8(0xFA2A, 0xFE2F, 0x84309C38);
182 convutf8(0xFFE6, 0xFFFF, 0x8431A234);
183 convutf8(0x10000, 0x10FFFF, 0x90308130);
184 /* No mapping exists */
185 return 0;
186}
uint32_t uint32
Definition: c.h:541
static uint32 utf8word_to_unicode(uint32 c)
#define convutf8(minunicode, maxunicode, mincode)

References convutf8, and utf8word_to_unicode().

Referenced by utf8_to_gb18030().

◆ gb18030_to_utf8()

Datum gb18030_to_utf8 ( PG_FUNCTION_ARGS  )

Definition at line 202 of file utf8_and_gb18030.c.

203{
204 unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
205 unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
206 int len = PG_GETARG_INT32(4);
207 bool noError = PG_GETARG_BOOL(5);
208 int converted;
209
211
212 converted = LocalToUtf(src, len, dest,
213 &gb18030_to_unicode_tree,
214 NULL, 0,
217 noError);
218
219 PG_RETURN_INT32(converted);
220}
int LocalToUtf(const unsigned char *iso, int len, unsigned char *utf, const pg_mb_radix_tree *map, const pg_local_to_utf_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding, bool noError)
Definition: conv.c:717
#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
const void size_t len
@ PG_GB18030
Definition: pg_wchar.h:268
@ PG_UTF8
Definition: pg_wchar.h:232
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition: pg_wchar.h:507
static uint32 conv_18030_to_utf8(uint32 code)

References CHECK_ENCODING_CONVERSION_ARGS, conv_18030_to_utf8(), generate_unaccent_rules::dest, len, LocalToUtf(), PG_GB18030, PG_GETARG_BOOL, PG_GETARG_CSTRING, PG_GETARG_INT32, PG_RETURN_INT32, and PG_UTF8.

◆ gb_linear()

static uint32 gb_linear ( uint32  gb)
inlinestatic

Definition at line 35 of file utf8_and_gb18030.c.

36{
37 uint32 b0 = (gb & 0xff000000) >> 24;
38 uint32 b1 = (gb & 0x00ff0000) >> 16;
39 uint32 b2 = (gb & 0x0000ff00) >> 8;
40 uint32 b3 = (gb & 0x000000ff);
41
42 return b0 * 12600 + b1 * 1260 + b2 * 10 + b3 -
43 (0x81 * 12600 + 0x30 * 1260 + 0x81 * 10 + 0x30);
44}

◆ gb_unlinear()

static uint32 gb_unlinear ( uint32  lin)
inlinestatic

Definition at line 47 of file utf8_and_gb18030.c.

48{
49 uint32 r0 = 0x81 + lin / 12600;
50 uint32 r1 = 0x30 + (lin / 1260) % 10;
51 uint32 r2 = 0x81 + (lin / 10) % 126;
52 uint32 r3 = 0x30 + lin % 10;
53
54 return (r0 << 24) | (r1 << 16) | (r2 << 8) | r3;
55}

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( gb18030_to_utf8  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( utf8_to_gb18030  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "utf8_and_gb18030",
version = PG_VERSION 
)

◆ unicode_to_utf8word()

static uint32 unicode_to_utf8word ( uint32  c)
inlinestatic

Definition at line 63 of file utf8_and_gb18030.c.

64{
66
67 if (c <= 0x7F)
68 {
69 word = c;
70 }
71 else if (c <= 0x7FF)
72 {
73 word = (0xC0 | ((c >> 6) & 0x1F)) << 8;
74 word |= 0x80 | (c & 0x3F);
75 }
76 else if (c <= 0xFFFF)
77 {
78 word = (0xE0 | ((c >> 12) & 0x0F)) << 16;
79 word |= (0x80 | ((c >> 6) & 0x3F)) << 8;
80 word |= 0x80 | (c & 0x3F);
81 }
82 else
83 {
84 word = (0xF0 | ((c >> 18) & 0x07)) << 24;
85 word |= (0x80 | ((c >> 12) & 0x3F)) << 16;
86 word |= (0x80 | ((c >> 6) & 0x3F)) << 8;
87 word |= 0x80 | (c & 0x3F);
88 }
89
90 return word;
91}
char * c
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
Definition: regcomp.c:1476

References word().

◆ utf8_to_gb18030()

Datum utf8_to_gb18030 ( PG_FUNCTION_ARGS  )

Definition at line 223 of file utf8_and_gb18030.c.

224{
225 unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
226 unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
227 int len = PG_GETARG_INT32(4);
228 bool noError = PG_GETARG_BOOL(5);
229 int converted;
230
232
233 converted = UtfToLocal(src, len, dest,
234 &gb18030_from_unicode_tree,
235 NULL, 0,
238 noError);
239
240 PG_RETURN_INT32(converted);
241}
int UtfToLocal(const unsigned char *utf, int len, unsigned char *iso, const pg_mb_radix_tree *map, const pg_utf_to_local_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding, bool noError)
Definition: conv.c:507
static uint32 conv_utf8_to_18030(uint32 code)

References CHECK_ENCODING_CONVERSION_ARGS, conv_utf8_to_18030(), generate_unaccent_rules::dest, len, PG_GB18030, PG_GETARG_BOOL, PG_GETARG_CSTRING, PG_GETARG_INT32, PG_RETURN_INT32, PG_UTF8, and UtfToLocal().

◆ utf8word_to_unicode()

static uint32 utf8word_to_unicode ( uint32  c)
inlinestatic

Definition at line 94 of file utf8_and_gb18030.c.

95{
96 uint32 ucs;
97
98 if (c <= 0x7F)
99 {
100 ucs = c;
101 }
102 else if (c <= 0xFFFF)
103 {
104 ucs = ((c >> 8) & 0x1F) << 6;
105 ucs |= c & 0x3F;
106 }
107 else if (c <= 0xFFFFFF)
108 {
109 ucs = ((c >> 16) & 0x0F) << 12;
110 ucs |= ((c >> 8) & 0x3F) << 6;
111 ucs |= c & 0x3F;
112 }
113 else
114 {
115 ucs = ((c >> 24) & 0x07) << 18;
116 ucs |= ((c >> 16) & 0x3F) << 12;
117 ucs |= ((c >> 8) & 0x3F) << 6;
118 ucs |= c & 0x3F;
119 }
120
121 return ucs;
122}

Referenced by conv_utf8_to_18030().