OSDN Git Service

Update copyright to 2004.
[pg-rex/syncrep.git] / src / backend / utils / mb / conversion_procs / utf8_and_cyrillic / utf8_and_cyrillic.c
1 /*-------------------------------------------------------------------------
2  *
3  *        UTF8 and Cyrillic
4  *
5  * Portions Copyright (c) 1996-2004, 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_cyrillic/utf8_and_cyrillic.c,v 1.8 2004/08/29 04:12:57 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_koi8r.map"
18 #include "../../Unicode/koi8r_to_utf8.map"
19 #include "../../Unicode/utf8_to_win1251.map"
20 #include "../../Unicode/win1251_to_utf8.map"
21 #include "../../Unicode/utf8_to_alt.map"
22 #include "../../Unicode/alt_to_utf8.map"
23
24 PG_FUNCTION_INFO_V1(utf8_to_koi8r);
25 PG_FUNCTION_INFO_V1(koi8r_to_utf8);
26 PG_FUNCTION_INFO_V1(utf8_to_win1251);
27 PG_FUNCTION_INFO_V1(win1251_to_utf8);
28 PG_FUNCTION_INFO_V1(utf8_to_alt);
29 PG_FUNCTION_INFO_V1(alt_to_utf8);
30
31 extern Datum utf8_to_koi8r(PG_FUNCTION_ARGS);
32 extern Datum koi8r_to_utf8(PG_FUNCTION_ARGS);
33 extern Datum utf8_to_win1251(PG_FUNCTION_ARGS);
34 extern Datum win1251_to_utf8(PG_FUNCTION_ARGS);
35 extern Datum utf8_to_alt(PG_FUNCTION_ARGS);
36 extern Datum alt_to_utf8(PG_FUNCTION_ARGS);
37
38 /* ----------
39  * conv_proc(
40  *              INTEGER,        -- source encoding id
41  *              INTEGER,        -- destination encoding id
42  *              CSTRING,        -- source string (null terminated C string)
43  *              CSTRING,        -- destination string (null terminated C string)
44  *              INTEGER         -- source string length
45  * ) returns VOID;
46  * ----------
47  */
48
49 Datum
50 utf8_to_koi8r(PG_FUNCTION_ARGS)
51 {
52         unsigned char *src = PG_GETARG_CSTRING(2);
53         unsigned char *dest = PG_GETARG_CSTRING(3);
54         int                     len = PG_GETARG_INT32(4);
55
56         Assert(PG_GETARG_INT32(0) == PG_UTF8);
57         Assert(PG_GETARG_INT32(1) == PG_KOI8R);
58         Assert(len >= 0);
59
60         UtfToLocal(src, dest, ULmap_KOI8R,
61                            sizeof(ULmap_KOI8R) / sizeof(pg_utf_to_local), len);
62
63         PG_RETURN_VOID();
64 }
65
66 Datum
67 koi8r_to_utf8(PG_FUNCTION_ARGS)
68 {
69         unsigned char *src = PG_GETARG_CSTRING(2);
70         unsigned char *dest = PG_GETARG_CSTRING(3);
71         int                     len = PG_GETARG_INT32(4);
72
73         Assert(PG_GETARG_INT32(0) == PG_KOI8R);
74         Assert(PG_GETARG_INT32(1) == PG_UTF8);
75         Assert(len >= 0);
76
77         LocalToUtf(src, dest, LUmapKOI8R,
78                         sizeof(LUmapKOI8R) / sizeof(pg_local_to_utf), PG_KOI8R, len);
79
80         PG_RETURN_VOID();
81 }
82
83 Datum
84 utf8_to_win1251(PG_FUNCTION_ARGS)
85 {
86         unsigned char *src = PG_GETARG_CSTRING(2);
87         unsigned char *dest = PG_GETARG_CSTRING(3);
88         int                     len = PG_GETARG_INT32(4);
89
90         Assert(PG_GETARG_INT32(0) == PG_UTF8);
91         Assert(PG_GETARG_INT32(1) == PG_WIN1251);
92         Assert(len >= 0);
93
94         UtfToLocal(src, dest, ULmap_WIN1251,
95                            sizeof(ULmap_WIN1251) / sizeof(pg_utf_to_local), len);
96
97         PG_RETURN_VOID();
98 }
99
100 Datum
101 win1251_to_utf8(PG_FUNCTION_ARGS)
102 {
103         unsigned char *src = PG_GETARG_CSTRING(2);
104         unsigned char *dest = PG_GETARG_CSTRING(3);
105         int                     len = PG_GETARG_INT32(4);
106
107         Assert(PG_GETARG_INT32(0) == PG_WIN1251);
108         Assert(PG_GETARG_INT32(1) == PG_UTF8);
109         Assert(len >= 0);
110
111         LocalToUtf(src, dest, LUmapWIN1251,
112                 sizeof(LUmapWIN1251) / sizeof(pg_local_to_utf), PG_WIN1251, len);
113
114         PG_RETURN_VOID();
115 }
116
117 Datum
118 utf8_to_alt(PG_FUNCTION_ARGS)
119 {
120         unsigned char *src = PG_GETARG_CSTRING(2);
121         unsigned char *dest = PG_GETARG_CSTRING(3);
122         int                     len = PG_GETARG_INT32(4);
123
124         Assert(PG_GETARG_INT32(0) == PG_UTF8);
125         Assert(PG_GETARG_INT32(1) == PG_ALT);
126         Assert(len >= 0);
127
128         UtfToLocal(src, dest, ULmap_ALT,
129                            sizeof(ULmap_ALT) / sizeof(pg_utf_to_local), len);
130
131         PG_RETURN_VOID();
132 }
133
134 Datum
135 alt_to_utf8(PG_FUNCTION_ARGS)
136 {
137         unsigned char *src = PG_GETARG_CSTRING(2);
138         unsigned char *dest = PG_GETARG_CSTRING(3);
139         int                     len = PG_GETARG_INT32(4);
140
141         Assert(PG_GETARG_INT32(0) == PG_ALT);
142         Assert(PG_GETARG_INT32(1) == PG_UTF8);
143         Assert(len >= 0);
144
145         LocalToUtf(src, dest, LUmapALT,
146                            sizeof(LUmapALT) / sizeof(pg_local_to_utf), PG_ALT, len);
147
148         PG_RETURN_VOID();
149 }