PostgreSQL Source Code  git master
euc_cn_and_mic.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "mb/pg_wchar.h"
Include dependency graph for euc_cn_and_mic.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (euc_cn_to_mic)
 
 PG_FUNCTION_INFO_V1 (mic_to_euc_cn)
 
static int euc_cn2mic (const unsigned char *euc, unsigned char *p, int len, bool noError)
 
static int mic2euc_cn (const unsigned char *mic, unsigned char *p, int len, bool noError)
 
Datum euc_cn_to_mic (PG_FUNCTION_ARGS)
 
Datum mic_to_euc_cn (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ euc_cn2mic()

static int euc_cn2mic ( const unsigned char *  euc,
unsigned char *  p,
int  len,
bool  noError 
)
static

Definition at line 76 of file euc_cn_and_mic.c.

77 {
78  const unsigned char *start = euc;
79  int c1;
80 
81  while (len > 0)
82  {
83  c1 = *euc;
84  if (IS_HIGHBIT_SET(c1))
85  {
86  if (len < 2 || !IS_HIGHBIT_SET(euc[1]))
87  {
88  if (noError)
89  break;
90  report_invalid_encoding(PG_EUC_CN, (const char *) euc, len);
91  }
92  *p++ = LC_GB2312_80;
93  *p++ = c1;
94  *p++ = euc[1];
95  euc += 2;
96  len -= 2;
97  }
98  else
99  { /* should be ASCII */
100  if (c1 == 0)
101  {
102  if (noError)
103  break;
104  report_invalid_encoding(PG_EUC_CN, (const char *) euc, len);
105  }
106  *p++ = c1;
107  euc++;
108  len--;
109  }
110  }
111  *p = '\0';
112 
113  return euc - start;
114 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1158
return str start
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: mbutils.c:1698
const void size_t len
@ PG_EUC_CN
Definition: pg_wchar.h:228
#define LC_GB2312_80
Definition: pg_wchar.h:133

References IS_HIGHBIT_SET, LC_GB2312_80, len, PG_EUC_CN, report_invalid_encoding(), and start.

Referenced by euc_cn_to_mic().

◆ euc_cn_to_mic()

Datum euc_cn_to_mic ( PG_FUNCTION_ARGS  )

Definition at line 41 of file euc_cn_and_mic.c.

42 {
43  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
44  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
45  int len = PG_GETARG_INT32(4);
46  bool noError = PG_GETARG_BOOL(5);
47  int converted;
48 
50 
51  converted = euc_cn2mic(src, dest, len, noError);
52 
53  PG_RETURN_INT32(converted);
54 }
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
@ PG_MULE_INTERNAL
Definition: pg_wchar.h:233
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition: pg_wchar.h:507

References CHECK_ENCODING_CONVERSION_ARGS, generate_unaccent_rules::dest, euc_cn2mic(), len, PG_EUC_CN, PG_GETARG_BOOL, PG_GETARG_CSTRING, PG_GETARG_INT32, PG_MULE_INTERNAL, and PG_RETURN_INT32.

◆ mic2euc_cn()

static int mic2euc_cn ( const unsigned char *  mic,
unsigned char *  p,
int  len,
bool  noError 
)
static

Definition at line 120 of file euc_cn_and_mic.c.

121 {
122  const unsigned char *start = mic;
123  int c1;
124 
125  while (len > 0)
126  {
127  c1 = *mic;
128  if (IS_HIGHBIT_SET(c1))
129  {
130  if (c1 != LC_GB2312_80)
131  {
132  if (noError)
133  break;
135  (const char *) mic, len);
136  }
137  if (len < 3 || !IS_HIGHBIT_SET(mic[1]) || !IS_HIGHBIT_SET(mic[2]))
138  {
139  if (noError)
140  break;
142  (const char *) mic, len);
143  }
144  mic++;
145  *p++ = *mic++;
146  *p++ = *mic++;
147  len -= 3;
148  }
149  else
150  { /* should be ASCII */
151  if (c1 == 0)
152  {
153  if (noError)
154  break;
156  (const char *) mic, len);
157  }
158  *p++ = c1;
159  mic++;
160  len--;
161  }
162  }
163  *p = '\0';
164 
165  return mic - start;
166 }
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: mbutils.c:1730

References IS_HIGHBIT_SET, LC_GB2312_80, len, PG_EUC_CN, PG_MULE_INTERNAL, report_invalid_encoding(), report_untranslatable_char(), and start.

Referenced by mic_to_euc_cn().

◆ mic_to_euc_cn()

Datum mic_to_euc_cn ( PG_FUNCTION_ARGS  )

Definition at line 57 of file euc_cn_and_mic.c.

58 {
59  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
60  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
61  int len = PG_GETARG_INT32(4);
62  bool noError = PG_GETARG_BOOL(5);
63  int converted;
64 
66 
67  converted = mic2euc_cn(src, dest, len, noError);
68 
69  PG_RETURN_INT32(converted);
70 }
static int mic2euc_cn(const unsigned char *mic, unsigned char *p, int len, bool noError)

References CHECK_ENCODING_CONVERSION_ARGS, generate_unaccent_rules::dest, len, mic2euc_cn(), PG_EUC_CN, PG_GETARG_BOOL, PG_GETARG_CSTRING, PG_GETARG_INT32, PG_MULE_INTERNAL, and PG_RETURN_INT32.

◆ PG_FUNCTION_INFO_V1() [1/2]

PG_FUNCTION_INFO_V1 ( euc_cn_to_mic  )

◆ PG_FUNCTION_INFO_V1() [2/2]

PG_FUNCTION_INFO_V1 ( mic_to_euc_cn  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 18 of file euc_cn_and_mic.c.