PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
utf8_and_win.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * WIN <--> UTF8
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/utf8_and_win/utf8_and_win.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 #include "postgres.h"
15 #include "fmgr.h"
16 #include "mb/pg_wchar.h"
17 #include "../../Unicode/utf8_to_win1250.map"
18 #include "../../Unicode/utf8_to_win1251.map"
19 #include "../../Unicode/utf8_to_win1252.map"
20 #include "../../Unicode/utf8_to_win1253.map"
21 #include "../../Unicode/utf8_to_win1254.map"
22 #include "../../Unicode/utf8_to_win1255.map"
23 #include "../../Unicode/utf8_to_win1256.map"
24 #include "../../Unicode/utf8_to_win1257.map"
25 #include "../../Unicode/utf8_to_win1258.map"
26 #include "../../Unicode/utf8_to_win866.map"
27 #include "../../Unicode/utf8_to_win874.map"
28 #include "../../Unicode/win1250_to_utf8.map"
29 #include "../../Unicode/win1251_to_utf8.map"
30 #include "../../Unicode/win1252_to_utf8.map"
31 #include "../../Unicode/win1253_to_utf8.map"
32 #include "../../Unicode/win1254_to_utf8.map"
33 #include "../../Unicode/win1255_to_utf8.map"
34 #include "../../Unicode/win1256_to_utf8.map"
35 #include "../../Unicode/win1257_to_utf8.map"
36 #include "../../Unicode/win866_to_utf8.map"
37 #include "../../Unicode/win874_to_utf8.map"
38 #include "../../Unicode/win1258_to_utf8.map"
39 
41 
44 
45 /* ----------
46  * conv_proc(
47  * INTEGER, -- source encoding id
48  * INTEGER, -- destination encoding id
49  * CSTRING, -- source string (null terminated C string)
50  * CSTRING, -- destination string (null terminated C string)
51  * INTEGER -- source string length
52  * ) returns VOID;
53  * ----------
54  */
55 
56 typedef struct
57 {
59  const pg_local_to_utf *map1; /* to UTF8 map name */
60  const pg_utf_to_local *map2; /* from UTF8 map name */
61  int size1; /* size of map1 */
62  int size2; /* size of map2 */
63 } pg_conv_map;
64 
65 static const pg_conv_map maps[] = {
66  {PG_WIN866, LUmapWIN866, ULmapWIN866,
67  lengthof(LUmapWIN866),
68  lengthof(ULmapWIN866)},
69  {PG_WIN874, LUmapWIN874, ULmapWIN874,
70  lengthof(LUmapWIN874),
71  lengthof(ULmapWIN874)},
72  {PG_WIN1250, LUmapWIN1250, ULmapWIN1250,
73  lengthof(LUmapWIN1250),
74  lengthof(ULmapWIN1250)},
75  {PG_WIN1251, LUmapWIN1251, ULmapWIN1251,
76  lengthof(LUmapWIN1251),
77  lengthof(ULmapWIN1251)},
78  {PG_WIN1252, LUmapWIN1252, ULmapWIN1252,
79  lengthof(LUmapWIN1252),
80  lengthof(ULmapWIN1252)},
81  {PG_WIN1253, LUmapWIN1253, ULmapWIN1253,
82  lengthof(LUmapWIN1253),
83  lengthof(ULmapWIN1253)},
84  {PG_WIN1254, LUmapWIN1254, ULmapWIN1254,
85  lengthof(LUmapWIN1254),
86  lengthof(ULmapWIN1254)},
87  {PG_WIN1255, LUmapWIN1255, ULmapWIN1255,
88  lengthof(LUmapWIN1255),
89  lengthof(ULmapWIN1255)},
90  {PG_WIN1256, LUmapWIN1256, ULmapWIN1256,
91  lengthof(LUmapWIN1256),
92  lengthof(ULmapWIN1256)},
93  {PG_WIN1257, LUmapWIN1257, ULmapWIN1257,
94  lengthof(LUmapWIN1257),
95  lengthof(ULmapWIN1257)},
96  {PG_WIN1258, LUmapWIN1258, ULmapWIN1258,
97  lengthof(LUmapWIN1258),
98  lengthof(ULmapWIN1258)},
99 };
100 
101 Datum
103 {
104  int encoding = PG_GETARG_INT32(0);
105  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
106  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
107  int len = PG_GETARG_INT32(4);
108  int i;
109 
111 
112  for (i = 0; i < lengthof(maps); i++)
113  {
114  if (encoding == maps[i].encoding)
115  {
116  LocalToUtf(src, len, dest,
117  maps[i].map1, maps[i].size1,
118  NULL, 0,
119  NULL,
120  encoding);
121  PG_RETURN_VOID();
122  }
123  }
124 
125  ereport(ERROR,
126  (errcode(ERRCODE_INTERNAL_ERROR),
127  errmsg("unexpected encoding ID %d for WIN character sets",
128  encoding)));
129 
130  PG_RETURN_VOID();
131 }
132 
133 Datum
135 {
136  int encoding = PG_GETARG_INT32(1);
137  unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
138  unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
139  int len = PG_GETARG_INT32(4);
140  int i;
141 
143 
144  for (i = 0; i < lengthof(maps); i++)
145  {
146  if (encoding == maps[i].encoding)
147  {
148  UtfToLocal(src, len, dest,
149  maps[i].map2, maps[i].size2,
150  NULL, 0,
151  NULL,
152  encoding);
153  PG_RETURN_VOID();
154  }
155  }
156 
157  ereport(ERROR,
158  (errcode(ERRCODE_INTERNAL_ERROR),
159  errmsg("unexpected encoding ID %d for WIN character sets",
160  encoding)));
161 
162  PG_RETURN_VOID();
163 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
Datum utf8_to_win(PG_FUNCTION_ARGS)
Definition: utf8_and_win.c:134
int errcode(int sqlerrcode)
Definition: elog.c:575
#define lengthof(array)
Definition: c.h:558
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: conv.c:390
PG_MODULE_MAGIC
Definition: utf8_and_win.c:40
Datum win_to_utf8(PG_FUNCTION_ARGS)
Definition: utf8_and_win.c:102
PG_FUNCTION_INFO_V1(win_to_utf8)
#define ERROR
Definition: elog.h:43
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: conv.c:576
static const pg_conv_map maps[]
Definition: utf8_and_win.c:65
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:374
static char * encoding
Definition: initdb.c:121
#define PG_RETURN_VOID()
Definition: fmgr.h:293
#define NULL
Definition: c.h:226
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition: pg_wchar.h:428
pg_enc
Definition: pg_wchar.h:236
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:233
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150