PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
conv.c File Reference
#include "postgres.h"
#include "mb/pg_wchar.h"
Include dependency graph for conv.c:

Go to the source code of this file.

Functions

void local2local (const unsigned char *l, unsigned char *p, int len, int src_encoding, int dest_encoding, const unsigned char *tab)
 
void latin2mic (const unsigned char *l, unsigned char *p, int len, int lc, int encoding)
 
void mic2latin (const unsigned char *mic, unsigned char *p, int len, int lc, int encoding)
 
void pg_ascii2mic (const unsigned char *l, unsigned char *p, int len)
 
void pg_mic2ascii (const unsigned char *mic, unsigned char *p, int len)
 
void latin2mic_with_table (const unsigned char *l, unsigned char *p, int len, int lc, int encoding, const unsigned char *tab)
 
void mic2latin_with_table (const unsigned char *mic, unsigned char *p, int len, int lc, int encoding, const unsigned char *tab)
 
static int compare1 (const void *p1, const void *p2)
 
static int compare2 (const void *p1, const void *p2)
 
static int compare3 (const void *p1, const void *p2)
 
static int compare4 (const void *p1, const void *p2)
 
static unsigned char * store_coded_char (unsigned char *dest, uint32 code)
 
void UtfToLocal (const unsigned char *utf, int len, unsigned char *iso, const pg_utf_to_local *map, int mapsize, const pg_utf_to_local_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding)
 
void LocalToUtf (const unsigned char *iso, int len, unsigned char *utf, const pg_local_to_utf *map, int mapsize, const pg_local_to_utf_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding)
 

Function Documentation

static int compare1 ( const void *  p1,
const void *  p2 
)
static

Definition at line 290 of file conv.c.

Referenced by UtfToLocal().

291 {
292  uint32 v1,
293  v2;
294 
295  v1 = *(const uint32 *) p1;
296  v2 = ((const pg_utf_to_local *) p2)->utf;
297  return (v1 > v2) ? 1 : ((v1 == v2) ? 0 : -1);
298 }
unsigned int uint32
Definition: c.h:265
static int compare2 ( const void *  p1,
const void *  p2 
)
static

Definition at line 305 of file conv.c.

Referenced by LocalToUtf().

306 {
307  uint32 v1,
308  v2;
309 
310  v1 = *(const uint32 *) p1;
311  v2 = ((const pg_local_to_utf *) p2)->code;
312  return (v1 > v2) ? 1 : ((v1 == v2) ? 0 : -1);
313 }
unsigned int uint32
Definition: c.h:265
static int compare3 ( const void *  p1,
const void *  p2 
)
static

Definition at line 320 of file conv.c.

References s1, and s2.

Referenced by UtfToLocal().

321 {
322  uint32 s1,
323  s2,
324  d1,
325  d2;
326 
327  s1 = *(const uint32 *) p1;
328  s2 = *((const uint32 *) p1 + 1);
329  d1 = ((const pg_utf_to_local_combined *) p2)->utf1;
330  d2 = ((const pg_utf_to_local_combined *) p2)->utf2;
331  return (s1 > d1 || (s1 == d1 && s2 > d2)) ? 1 : ((s1 == d1 && s2 == d2) ? 0 : -1);
332 }
char * s1
unsigned int uint32
Definition: c.h:265
char * s2
static int compare4 ( const void *  p1,
const void *  p2 
)
static

Definition at line 339 of file conv.c.

Referenced by LocalToUtf().

340 {
341  uint32 v1,
342  v2;
343 
344  v1 = *(const uint32 *) p1;
345  v2 = ((const pg_local_to_utf_combined *) p2)->code;
346  return (v1 > v2) ? 1 : ((v1 == v2) ? 0 : -1);
347 }
unsigned int uint32
Definition: c.h:265
void latin2mic ( const unsigned char *  l,
unsigned char *  p,
int  len,
int  lc,
int  encoding 
)

Definition at line 71 of file conv.c.

References IS_HIGHBIT_SET, and report_invalid_encoding().

Referenced by koi8r_to_mic(), latin1_to_mic(), latin2_to_mic(), latin3_to_mic(), and latin4_to_mic().

73 {
74  int c1;
75 
76  while (len > 0)
77  {
78  c1 = *l;
79  if (c1 == 0)
80  report_invalid_encoding(encoding, (const char *) l, len);
81  if (IS_HIGHBIT_SET(c1))
82  *p++ = lc;
83  *p++ = c1;
84  l++;
85  len--;
86  }
87  *p = '\0';
88 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:969
static char * encoding
Definition: initdb.c:120
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
void latin2mic_with_table ( const unsigned char *  l,
unsigned char *  p,
int  len,
int  lc,
int  encoding,
const unsigned char *  tab 
)

Definition at line 193 of file conv.c.

References HIGHBIT, IS_HIGHBIT_SET, PG_MULE_INTERNAL, report_invalid_encoding(), and report_untranslatable_char().

Referenced by iso_to_mic(), win1250_to_mic(), win1251_to_mic(), and win866_to_mic().

199 {
200  unsigned char c1,
201  c2;
202 
203  while (len > 0)
204  {
205  c1 = *l;
206  if (c1 == 0)
207  report_invalid_encoding(encoding, (const char *) l, len);
208  if (!IS_HIGHBIT_SET(c1))
209  *p++ = c1;
210  else
211  {
212  c2 = tab[c1 - HIGHBIT];
213  if (c2)
214  {
215  *p++ = lc;
216  *p++ = c2;
217  }
218  else
220  (const char *) l, len);
221  }
222  l++;
223  len--;
224  }
225  *p = '\0';
226 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:969
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: wchar.c:2027
#define HIGHBIT
Definition: c.h:968
static char * encoding
Definition: initdb.c:120
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
void local2local ( const unsigned char *  l,
unsigned char *  p,
int  len,
int  src_encoding,
int  dest_encoding,
const unsigned char *  tab 
)

Definition at line 30 of file conv.c.

References HIGHBIT, IS_HIGHBIT_SET, report_invalid_encoding(), and report_untranslatable_char().

Referenced by iso_to_koi8r(), iso_to_win1251(), iso_to_win866(), koi8r_to_iso(), koi8r_to_win1251(), koi8r_to_win866(), latin2_to_win1250(), win1250_to_latin2(), win1251_to_iso(), win1251_to_koi8r(), win1251_to_win866(), win866_to_iso(), win866_to_koi8r(), and win866_to_win1251().

36 {
37  unsigned char c1,
38  c2;
39 
40  while (len > 0)
41  {
42  c1 = *l;
43  if (c1 == 0)
44  report_invalid_encoding(src_encoding, (const char *) l, len);
45  if (!IS_HIGHBIT_SET(c1))
46  *p++ = c1;
47  else
48  {
49  c2 = tab[c1 - HIGHBIT];
50  if (c2)
51  *p++ = c2;
52  else
53  report_untranslatable_char(src_encoding, dest_encoding,
54  (const char *) l, len);
55  }
56  l++;
57  len--;
58  }
59  *p = '\0';
60 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:969
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: wchar.c:2027
#define HIGHBIT
Definition: c.h:968
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
void LocalToUtf ( const unsigned char *  iso,
int  len,
unsigned char *  utf,
const pg_local_to_utf map,
int  mapsize,
const pg_local_to_utf_combined cmap,
int  cmapsize,
utf_local_conversion_func  conv_func,
int  encoding 
)

Definition at line 576 of file conv.c.

References compare2(), compare4(), elog, ereport, errcode(), errmsg(), ERROR, IS_HIGHBIT_SET, pg_encoding_verifymb(), PG_UTF8, PG_VALID_ENCODING, report_invalid_encoding(), report_untranslatable_char(), store_coded_char(), pg_local_to_utf::utf, pg_local_to_utf_combined::utf1, and pg_local_to_utf_combined::utf2.

Referenced by big5_to_utf8(), euc_cn_to_utf8(), euc_jis_2004_to_utf8(), euc_jp_to_utf8(), euc_kr_to_utf8(), euc_tw_to_utf8(), gb18030_to_utf8(), gbk_to_utf8(), iso8859_to_utf8(), johab_to_utf8(), koi8r_to_utf8(), koi8u_to_utf8(), shift_jis_2004_to_utf8(), sjis_to_utf8(), uhc_to_utf8(), and win_to_utf8().

582 {
583  uint32 iiso;
584  int l;
585  const pg_local_to_utf *p;
586  const pg_local_to_utf_combined *cp;
587 
589  ereport(ERROR,
590  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
591  errmsg("invalid encoding number: %d", encoding)));
592 
593  for (; len > 0; len -= l)
594  {
595  /* "break" cases all represent errors */
596  if (*iso == '\0')
597  break;
598 
599  if (!IS_HIGHBIT_SET(*iso))
600  {
601  /* ASCII case is easy, assume it's one-to-one conversion */
602  *utf++ = *iso++;
603  l = 1;
604  continue;
605  }
606 
607  l = pg_encoding_verifymb(encoding, (const char *) iso, len);
608  if (l < 0)
609  break;
610 
611  /* collect coded char of length l */
612  if (l == 1)
613  iiso = *iso++;
614  else if (l == 2)
615  {
616  iiso = *iso++ << 8;
617  iiso |= *iso++;
618  }
619  else if (l == 3)
620  {
621  iiso = *iso++ << 16;
622  iiso |= *iso++ << 8;
623  iiso |= *iso++;
624  }
625  else if (l == 4)
626  {
627  iiso = *iso++ << 24;
628  iiso |= *iso++ << 16;
629  iiso |= *iso++ << 8;
630  iiso |= *iso++;
631  }
632  else
633  {
634  elog(ERROR, "unsupported character length %d", l);
635  iiso = 0; /* keep compiler quiet */
636  }
637 
638  /* First check ordinary map */
639  p = bsearch(&iiso, map, mapsize,
640  sizeof(pg_local_to_utf), compare2);
641 
642  if (p)
643  {
644  utf = store_coded_char(utf, p->utf);
645  continue;
646  }
647 
648  /* If there's a combined character map, try that */
649  if (cmap)
650  {
651  cp = bsearch(&iiso, cmap, cmapsize,
653 
654  if (cp)
655  {
656  utf = store_coded_char(utf, cp->utf1);
657  utf = store_coded_char(utf, cp->utf2);
658  continue;
659  }
660  }
661 
662  /* if there's a conversion function, try that */
663  if (conv_func)
664  {
665  uint32 converted = (*conv_func) (iiso);
666 
667  if (converted)
668  {
669  utf = store_coded_char(utf, converted);
670  continue;
671  }
672  }
673 
674  /* failed to translate this character */
676  (const char *) (iso - l), len);
677  }
678 
679  /* if we broke out of loop early, must be invalid input */
680  if (len > 0)
681  report_invalid_encoding(encoding, (const char *) iso, len);
682 
683  *utf = '\0';
684 }
static int compare4(const void *p1, const void *p2)
Definition: conv.c:339
int errcode(int sqlerrcode)
Definition: elog.c:575
static unsigned char * store_coded_char(unsigned char *dest, uint32 code)
Definition: conv.c:353
int pg_encoding_verifymb(int encoding, const char *mbstr, int len)
Definition: wchar.c:1809
#define IS_HIGHBIT_SET(ch)
Definition: c.h:969
#define ERROR
Definition: elog.h:43
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: wchar.c:2027
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:299
unsigned int uint32
Definition: c.h:265
#define ereport(elevel, rest)
Definition: elog.h:122
static char * encoding
Definition: initdb.c:120
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static int compare2(const void *p1, const void *p2)
Definition: conv.c:305
void mic2latin ( const unsigned char *  mic,
unsigned char *  p,
int  len,
int  lc,
int  encoding 
)

Definition at line 99 of file conv.c.

References IS_HIGHBIT_SET, pg_mic_mblen(), PG_MULE_INTERNAL, report_invalid_encoding(), and report_untranslatable_char().

Referenced by mic_to_koi8r(), mic_to_latin1(), mic_to_latin2(), mic_to_latin3(), and mic_to_latin4().

101 {
102  int c1;
103 
104  while (len > 0)
105  {
106  c1 = *mic;
107  if (c1 == 0)
108  report_invalid_encoding(PG_MULE_INTERNAL, (const char *) mic, len);
109  if (!IS_HIGHBIT_SET(c1))
110  {
111  /* easy for ASCII */
112  *p++ = c1;
113  mic++;
114  len--;
115  }
116  else
117  {
118  int l = pg_mic_mblen(mic);
119 
120  if (len < l)
121  report_invalid_encoding(PG_MULE_INTERNAL, (const char *) mic,
122  len);
123  if (l != 2 || c1 != lc || !IS_HIGHBIT_SET(mic[1]))
125  (const char *) mic, len);
126  *p++ = mic[1];
127  mic += 2;
128  len -= 2;
129  }
130  }
131  *p = '\0';
132 }
int pg_mic_mblen(const unsigned char *mbstr)
Definition: wchar.c:1776
#define IS_HIGHBIT_SET(ch)
Definition: c.h:969
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: wchar.c:2027
static char * encoding
Definition: initdb.c:120
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
void mic2latin_with_table ( const unsigned char *  mic,
unsigned char *  p,
int  len,
int  lc,
int  encoding,
const unsigned char *  tab 
)

Definition at line 241 of file conv.c.

References HIGHBIT, IS_HIGHBIT_SET, pg_mic_mblen(), PG_MULE_INTERNAL, report_invalid_encoding(), and report_untranslatable_char().

Referenced by mic_to_iso(), mic_to_win1250(), mic_to_win1251(), and mic_to_win866().

247 {
248  unsigned char c1,
249  c2;
250 
251  while (len > 0)
252  {
253  c1 = *mic;
254  if (c1 == 0)
255  report_invalid_encoding(PG_MULE_INTERNAL, (const char *) mic, len);
256  if (!IS_HIGHBIT_SET(c1))
257  {
258  /* easy for ASCII */
259  *p++ = c1;
260  mic++;
261  len--;
262  }
263  else
264  {
265  int l = pg_mic_mblen(mic);
266 
267  if (len < l)
268  report_invalid_encoding(PG_MULE_INTERNAL, (const char *) mic,
269  len);
270  if (l != 2 || c1 != lc || !IS_HIGHBIT_SET(mic[1]) ||
271  (c2 = tab[mic[1] - HIGHBIT]) == 0)
272  {
274  (const char *) mic, len);
275  break; /* keep compiler quiet */
276  }
277  *p++ = c2;
278  mic += 2;
279  len -= 2;
280  }
281  }
282  *p = '\0';
283 }
int pg_mic_mblen(const unsigned char *mbstr)
Definition: wchar.c:1776
#define IS_HIGHBIT_SET(ch)
Definition: c.h:969
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: wchar.c:2027
#define HIGHBIT
Definition: c.h:968
static char * encoding
Definition: initdb.c:120
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
void pg_ascii2mic ( const unsigned char *  l,
unsigned char *  p,
int  len 
)

Definition at line 143 of file conv.c.

References IS_HIGHBIT_SET, PG_SQL_ASCII, and report_invalid_encoding().

Referenced by ascii_to_mic(), and ascii_to_utf8().

144 {
145  int c1;
146 
147  while (len > 0)
148  {
149  c1 = *l;
150  if (c1 == 0 || IS_HIGHBIT_SET(c1))
151  report_invalid_encoding(PG_SQL_ASCII, (const char *) l, len);
152  *p++ = c1;
153  l++;
154  len--;
155  }
156  *p = '\0';
157 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:969
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
void pg_mic2ascii ( const unsigned char *  mic,
unsigned char *  p,
int  len 
)

Definition at line 163 of file conv.c.

References IS_HIGHBIT_SET, PG_MULE_INTERNAL, PG_SQL_ASCII, and report_untranslatable_char().

Referenced by mic_to_ascii(), and utf8_to_ascii().

164 {
165  int c1;
166 
167  while (len > 0)
168  {
169  c1 = *mic;
170  if (c1 == 0 || IS_HIGHBIT_SET(c1))
172  (const char *) mic, len);
173  *p++ = c1;
174  mic++;
175  len--;
176  }
177  *p = '\0';
178 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:969
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: wchar.c:2027
static unsigned char* store_coded_char ( unsigned char *  dest,
uint32  code 
)
inlinestatic

Definition at line 353 of file conv.c.

Referenced by LocalToUtf(), and UtfToLocal().

354 {
355  if (code & 0xff000000)
356  *dest++ = code >> 24;
357  if (code & 0x00ff0000)
358  *dest++ = code >> 16;
359  if (code & 0x0000ff00)
360  *dest++ = code >> 8;
361  if (code & 0x000000ff)
362  *dest++ = code;
363  return dest;
364 }
void UtfToLocal ( const unsigned char *  utf,
int  len,
unsigned char *  iso,
const pg_utf_to_local map,
int  mapsize,
const pg_utf_to_local_combined cmap,
int  cmapsize,
utf_local_conversion_func  conv_func,
int  encoding 
)

Definition at line 390 of file conv.c.

References pg_utf_to_local::code, pg_utf_to_local_combined::code, compare1(), compare3(), elog, ereport, errcode(), errmsg(), ERROR, PG_UTF8, pg_utf8_islegal(), pg_utf_mblen(), PG_VALID_ENCODING, report_invalid_encoding(), report_untranslatable_char(), and store_coded_char().

Referenced by utf8_to_big5(), utf8_to_euc_cn(), utf8_to_euc_jis_2004(), utf8_to_euc_jp(), utf8_to_euc_kr(), utf8_to_euc_tw(), utf8_to_gb18030(), utf8_to_gbk(), utf8_to_iso8859(), utf8_to_johab(), utf8_to_koi8r(), utf8_to_koi8u(), utf8_to_shift_jis_2004(), utf8_to_sjis(), utf8_to_uhc(), and utf8_to_win().

396 {
397  uint32 iutf;
398  int l;
399  const pg_utf_to_local *p;
400  const pg_utf_to_local_combined *cp;
401 
403  ereport(ERROR,
404  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
405  errmsg("invalid encoding number: %d", encoding)));
406 
407  for (; len > 0; len -= l)
408  {
409  /* "break" cases all represent errors */
410  if (*utf == '\0')
411  break;
412 
413  l = pg_utf_mblen(utf);
414  if (len < l)
415  break;
416 
417  if (!pg_utf8_islegal(utf, l))
418  break;
419 
420  if (l == 1)
421  {
422  /* ASCII case is easy, assume it's one-to-one conversion */
423  *iso++ = *utf++;
424  continue;
425  }
426 
427  /* collect coded char of length l */
428  if (l == 2)
429  {
430  iutf = *utf++ << 8;
431  iutf |= *utf++;
432  }
433  else if (l == 3)
434  {
435  iutf = *utf++ << 16;
436  iutf |= *utf++ << 8;
437  iutf |= *utf++;
438  }
439  else if (l == 4)
440  {
441  iutf = *utf++ << 24;
442  iutf |= *utf++ << 16;
443  iutf |= *utf++ << 8;
444  iutf |= *utf++;
445  }
446  else
447  {
448  elog(ERROR, "unsupported character length %d", l);
449  iutf = 0; /* keep compiler quiet */
450  }
451 
452  /* First, try with combined map if possible */
453  if (cmap && len > l)
454  {
455  const unsigned char *utf_save = utf;
456  int len_save = len;
457  int l_save = l;
458 
459  /* collect next character, same as above */
460  len -= l;
461 
462  l = pg_utf_mblen(utf);
463  if (len < l)
464  break;
465 
466  if (!pg_utf8_islegal(utf, l))
467  break;
468 
469  /* We assume ASCII character cannot be in combined map */
470  if (l > 1)
471  {
472  uint32 iutf2;
473  uint32 cutf[2];
474 
475  if (l == 2)
476  {
477  iutf2 = *utf++ << 8;
478  iutf2 |= *utf++;
479  }
480  else if (l == 3)
481  {
482  iutf2 = *utf++ << 16;
483  iutf2 |= *utf++ << 8;
484  iutf2 |= *utf++;
485  }
486  else if (l == 4)
487  {
488  iutf2 = *utf++ << 24;
489  iutf2 |= *utf++ << 16;
490  iutf2 |= *utf++ << 8;
491  iutf2 |= *utf++;
492  }
493  else
494  {
495  elog(ERROR, "unsupported character length %d", l);
496  iutf2 = 0; /* keep compiler quiet */
497  }
498 
499  cutf[0] = iutf;
500  cutf[1] = iutf2;
501 
502  cp = bsearch(cutf, cmap, cmapsize,
504 
505  if (cp)
506  {
507  iso = store_coded_char(iso, cp->code);
508  continue;
509  }
510  }
511 
512  /* fail, so back up to reprocess second character next time */
513  utf = utf_save;
514  len = len_save;
515  l = l_save;
516  }
517 
518  /* Now check ordinary map */
519  p = bsearch(&iutf, map, mapsize,
520  sizeof(pg_utf_to_local), compare1);
521 
522  if (p)
523  {
524  iso = store_coded_char(iso, p->code);
525  continue;
526  }
527 
528  /* if there's a conversion function, try that */
529  if (conv_func)
530  {
531  uint32 converted = (*conv_func) (iutf);
532 
533  if (converted)
534  {
535  iso = store_coded_char(iso, converted);
536  continue;
537  }
538  }
539 
540  /* failed to translate this character */
542  (const char *) (utf - l), len);
543  }
544 
545  /* if we broke out of loop early, must be invalid input */
546  if (len > 0)
547  report_invalid_encoding(PG_UTF8, (const char *) utf, len);
548 
549  *iso = '\0';
550 }
bool pg_utf8_islegal(const unsigned char *source, int length)
Definition: wchar.c:1458
int errcode(int sqlerrcode)
Definition: elog.c:575
static unsigned char * store_coded_char(unsigned char *dest, uint32 code)
Definition: conv.c:353
#define ERROR
Definition: elog.h:43
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: wchar.c:2027
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:299
unsigned int uint32
Definition: c.h:265
#define ereport(elevel, rest)
Definition: elog.h:122
static int compare3(const void *p1, const void *p2)
Definition: conv.c:320
static char * encoding
Definition: initdb.c:120
static int compare1(const void *p1, const void *p2)
Definition: conv.c:290
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: wchar.c:1995
int pg_utf_mblen(const unsigned char *s)
Definition: wchar.c:541
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219