PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-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_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 
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_cn2mic(const unsigned char *euc, unsigned char *p, int len);
35 static void mic2euc_cn(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_cn2mic(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_cn(src, dest, len);
61 
63 }
64 
65 /*
66  * EUC_CN ---> MIC
67  */
68 static void
69 euc_cn2mic(const unsigned char *euc, unsigned char *p, int len)
70 {
71  int c1;
72 
73  while (len > 0)
74  {
75  c1 = *euc;
76  if (IS_HIGHBIT_SET(c1))
77  {
78  if (len < 2 || !IS_HIGHBIT_SET(euc[1]))
79  report_invalid_encoding(PG_EUC_CN, (const char *) euc, len);
80  *p++ = LC_GB2312_80;
81  *p++ = c1;
82  *p++ = euc[1];
83  euc += 2;
84  len -= 2;
85  }
86  else
87  { /* should be ASCII */
88  if (c1 == 0)
89  report_invalid_encoding(PG_EUC_CN, (const char *) euc, len);
90  *p++ = c1;
91  euc++;
92  len--;
93  }
94  }
95  *p = '\0';
96 }
97 
98 /*
99  * MIC ---> EUC_CN
100  */
101 static void
102 mic2euc_cn(const unsigned char *mic, unsigned char *p, int len)
103 {
104  int c1;
105 
106  while (len > 0)
107  {
108  c1 = *mic;
109  if (IS_HIGHBIT_SET(c1))
110  {
111  if (c1 != LC_GB2312_80)
113  (const char *) mic, len);
114  if (len < 3 || !IS_HIGHBIT_SET(mic[1]) || !IS_HIGHBIT_SET(mic[2]))
116  (const char *) mic, len);
117  mic++;
118  *p++ = *mic++;
119  *p++ = *mic++;
120  len -= 3;
121  }
122  else
123  { /* should be ASCII */
124  if (c1 == 0)
126  (const char *) mic, len);
127  *p++ = c1;
128  mic++;
129  len--;
130  }
131  }
132  *p = '\0';
133 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:226
Datum mic_to_euc_cn(PG_FUNCTION_ARGS)
#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
#define LC_GB2312_80
Definition: pg_wchar.h:130
static void euc_cn2mic(const unsigned char *euc, unsigned char *p, int len)
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_VOID()
Definition: fmgr.h:301
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition: pg_wchar.h:490
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
PG_MODULE_MAGIC
Datum euc_cn_to_mic(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(euc_cn_to_mic)
static void mic2euc_cn(const unsigned char *mic, unsigned char *p, int len)
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:234
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150