]> granicus.if.org Git - postgresql/blob - src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c
f732cce720e71cd305dc804a1fa987ceb32ed611
[postgresql] / 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-2005, 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.13 2006/02/18 16:15:22 petere 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
20 PG_FUNCTION_INFO_V1(utf8_to_koi8r);
21 PG_FUNCTION_INFO_V1(koi8r_to_utf8);
22
23 extern Datum utf8_to_koi8r(PG_FUNCTION_ARGS);
24 extern Datum koi8r_to_utf8(PG_FUNCTION_ARGS);
25
26 /* ----------
27  * conv_proc(
28  *              INTEGER,        -- source encoding id
29  *              INTEGER,        -- destination encoding id
30  *              CSTRING,        -- source string (null terminated C string)
31  *              CSTRING,        -- destination string (null terminated C string)
32  *              INTEGER         -- source string length
33  * ) returns VOID;
34  * ----------
35  */
36
37 Datum
38 utf8_to_koi8r(PG_FUNCTION_ARGS)
39 {
40         unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
41         unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
42         int                     len = PG_GETARG_INT32(4);
43
44         Assert(PG_GETARG_INT32(0) == PG_UTF8);
45         Assert(PG_GETARG_INT32(1) == PG_KOI8R);
46         Assert(len >= 0);
47
48         UtfToLocal(src, dest, ULmapKOI8R,
49                            sizeof(ULmapKOI8R) / sizeof(pg_utf_to_local), len);
50
51         PG_RETURN_VOID();
52 }
53
54 Datum
55 koi8r_to_utf8(PG_FUNCTION_ARGS)
56 {
57         unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
58         unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
59         int                     len = PG_GETARG_INT32(4);
60
61         Assert(PG_GETARG_INT32(0) == PG_KOI8R);
62         Assert(PG_GETARG_INT32(1) == PG_UTF8);
63         Assert(len >= 0);
64
65         LocalToUtf(src, dest, LUmapKOI8R,
66                            sizeof(LUmapKOI8R) / sizeof(pg_local_to_utf), PG_KOI8R, len);
67
68         PG_RETURN_VOID();
69 }
70