OSDN Git Service

67b4a6cbe616939eae4061ca63775a599faecf74
[pg-rex/syncrep.git] / src / backend / utils / mb / conversion_procs / utf8_and_win / utf8_and_win.c
1 /*-------------------------------------------------------------------------
2  *
3  *        WIN <--> UTF8
4  *
5  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
6  * Portions Copyright (c) 1994, Regents of the University of California
7  *
8  * IDENTIFICATION
9  *        $PostgreSQL: pgsql/src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c,v 1.13 2009/01/01 17:23:53 momjian Exp $
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
40 PG_MODULE_MAGIC;
41
42 PG_FUNCTION_INFO_V1(win_to_utf8);
43 PG_FUNCTION_INFO_V1(utf8_to_win);
44
45 extern Datum win_to_utf8(PG_FUNCTION_ARGS);
46 extern Datum utf8_to_win(PG_FUNCTION_ARGS);
47
48 /* ----------
49  * conv_proc(
50  *              INTEGER,        -- source encoding id
51  *              INTEGER,        -- destination encoding id
52  *              CSTRING,        -- source string (null terminated C string)
53  *              CSTRING,        -- destination string (null terminated C string)
54  *              INTEGER         -- source string length
55  * ) returns VOID;
56  * ----------
57  */
58
59 typedef struct
60 {
61         pg_enc          encoding;
62         pg_local_to_utf *map1;          /* to UTF8 map name */
63         pg_utf_to_local *map2;          /* from UTF8 map name */
64         int                     size1;                  /* size of map1 */
65         int                     size2;                  /* size of map2 */
66 } pg_conv_map;
67
68 static pg_conv_map maps[] = {
69         {PG_WIN866, LUmapWIN866, ULmapWIN866,
70                 sizeof(LUmapWIN866) / sizeof(pg_local_to_utf),
71         sizeof(ULmapWIN866) / sizeof(pg_utf_to_local)},
72         {PG_WIN874, LUmapWIN874, ULmapWIN874,
73                 sizeof(LUmapWIN874) / sizeof(pg_local_to_utf),
74         sizeof(ULmapWIN874) / sizeof(pg_utf_to_local)},
75         {PG_WIN1250, LUmapWIN1250, ULmapWIN1250,
76                 sizeof(LUmapWIN1250) / sizeof(pg_local_to_utf),
77         sizeof(ULmapWIN1250) / sizeof(pg_utf_to_local)},
78         {PG_WIN1251, LUmapWIN1251, ULmapWIN1251,
79                 sizeof(LUmapWIN1251) / sizeof(pg_local_to_utf),
80         sizeof(ULmapWIN1251) / sizeof(pg_utf_to_local)},
81         {PG_WIN1252, LUmapWIN1252, ULmapWIN1252,
82                 sizeof(LUmapWIN1252) / sizeof(pg_local_to_utf),
83         sizeof(ULmapWIN1252) / sizeof(pg_utf_to_local)},
84         {PG_WIN1253, LUmapWIN1253, ULmapWIN1253,
85                 sizeof(LUmapWIN1253) / sizeof(pg_local_to_utf),
86         sizeof(ULmapWIN1253) / sizeof(pg_utf_to_local)},
87         {PG_WIN1254, LUmapWIN1254, ULmapWIN1254,
88                 sizeof(LUmapWIN1254) / sizeof(pg_local_to_utf),
89         sizeof(ULmapWIN1254) / sizeof(pg_utf_to_local)},
90         {PG_WIN1255, LUmapWIN1255, ULmapWIN1255,
91                 sizeof(LUmapWIN1255) / sizeof(pg_local_to_utf),
92         sizeof(ULmapWIN1255) / sizeof(pg_utf_to_local)},
93         {PG_WIN1256, LUmapWIN1256, ULmapWIN1256,
94                 sizeof(LUmapWIN1256) / sizeof(pg_local_to_utf),
95         sizeof(ULmapWIN1256) / sizeof(pg_utf_to_local)},
96         {PG_WIN1257, LUmapWIN1257, ULmapWIN1257,
97                 sizeof(LUmapWIN1257) / sizeof(pg_local_to_utf),
98         sizeof(ULmapWIN1257) / sizeof(pg_utf_to_local)},
99         {PG_WIN1258, LUmapWIN1258, ULmapWIN1258,
100                 sizeof(LUmapWIN1258) / sizeof(pg_local_to_utf),
101         sizeof(ULmapWIN1258) / sizeof(pg_utf_to_local)},
102 };
103
104 Datum
105 win_to_utf8(PG_FUNCTION_ARGS)
106 {
107         int                     encoding = PG_GETARG_INT32(0);
108         unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
109         unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
110         int                     len = PG_GETARG_INT32(4);
111         int                     i;
112
113         Assert(PG_GETARG_INT32(1) == PG_UTF8);
114         Assert(len >= 0);
115
116         for (i = 0; i < sizeof(maps) / sizeof(pg_conv_map); i++)
117         {
118                 if (encoding == maps[i].encoding)
119                 {
120                         LocalToUtf(src, dest, maps[i].map1, NULL, maps[i].size1, 0, encoding, len);
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", encoding)));
128
129         PG_RETURN_VOID();
130 }
131
132 Datum
133 utf8_to_win(PG_FUNCTION_ARGS)
134 {
135         int                     encoding = PG_GETARG_INT32(1);
136         unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
137         unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
138         int                     len = PG_GETARG_INT32(4);
139         int                     i;
140
141         Assert(PG_GETARG_INT32(0) == PG_UTF8);
142         Assert(len >= 0);
143
144         for (i = 0; i < sizeof(maps) / sizeof(pg_conv_map); i++)
145         {
146                 if (encoding == maps[i].encoding)
147                 {
148                         UtfToLocal(src, dest, maps[i].map2, NULL, maps[i].size2, 0, encoding, len);
149                         PG_RETURN_VOID();
150                 }
151         }
152
153         ereport(ERROR,
154                         (errcode(ERRCODE_INTERNAL_ERROR),
155           errmsg("unexpected encoding ID %d for WIN character sets", encoding)));
156
157         PG_RETURN_VOID();
158 }