Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * UTF8 and Cyrillic
4 : *
5 : * Portions Copyright (c) 1996-2025, 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_cyrillic/utf8_and_cyrillic.c
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_koi8u.map"
20 : #include "../../Unicode/koi8u_to_utf8.map"
21 :
166 tgl@sss.pgh.pa.us 22 CBC 6 : PG_MODULE_MAGIC_EXT(
23 : .name = "utf8_and_cyrillic",
24 : .version = PG_VERSION
25 : );
26 :
8353 27 6 : PG_FUNCTION_INFO_V1(utf8_to_koi8r);
28 3 : PG_FUNCTION_INFO_V1(koi8r_to_utf8);
29 :
6054 peter_e@gmx.net 30 3 : PG_FUNCTION_INFO_V1(utf8_to_koi8u);
31 3 : PG_FUNCTION_INFO_V1(koi8u_to_utf8);
32 :
33 : /* ----------
34 : * conv_proc(
35 : * INTEGER, -- source encoding id
36 : * INTEGER, -- destination encoding id
37 : * CSTRING, -- source string (null terminated C string)
38 : * CSTRING, -- destination string (null terminated C string)
39 : * INTEGER, -- source string length
40 : * BOOL -- if true, don't throw an error if conversion fails
41 : * ) returns INTEGER;
42 : *
43 : * Returns the number of bytes successfully converted.
44 : * ----------
45 : */
46 :
47 : Datum
8426 ishii@postgresql.org 48 219 : utf8_to_koi8r(PG_FUNCTION_ARGS)
49 : {
7289 tgl@sss.pgh.pa.us 50 219 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
51 219 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
8405 bruce@momjian.us 52 219 : int len = PG_GETARG_INT32(4);
1621 heikki.linnakangas@i 53 219 : bool noError = PG_GETARG_BOOL(5);
54 : int converted;
55 :
6066 tgl@sss.pgh.pa.us 56 219 : CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8R);
57 :
1621 heikki.linnakangas@i 58 219 : converted = UtfToLocal(src, len, dest,
59 : &koi8r_from_unicode_tree,
60 : NULL, 0,
61 : NULL,
62 : PG_KOI8R,
63 : noError);
64 :
65 120 : PG_RETURN_INT32(converted);
66 : }
67 :
68 : Datum
8426 ishii@postgresql.org 69 3 : koi8r_to_utf8(PG_FUNCTION_ARGS)
70 : {
7289 tgl@sss.pgh.pa.us 71 3 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
72 3 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
8405 bruce@momjian.us 73 3 : int len = PG_GETARG_INT32(4);
1621 heikki.linnakangas@i 74 3 : bool noError = PG_GETARG_BOOL(5);
75 : int converted;
76 :
6066 tgl@sss.pgh.pa.us 77 3 : CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_UTF8);
78 :
1621 heikki.linnakangas@i 79 3 : converted = LocalToUtf(src, len, dest,
80 : &koi8r_to_unicode_tree,
81 : NULL, 0,
82 : NULL,
83 : PG_KOI8R,
84 : noError);
85 :
86 3 : PG_RETURN_INT32(converted);
87 : }
88 :
89 : Datum
6054 peter_e@gmx.net 90 3 : utf8_to_koi8u(PG_FUNCTION_ARGS)
91 : {
92 3 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
93 3 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
94 3 : int len = PG_GETARG_INT32(4);
1621 heikki.linnakangas@i 95 3 : bool noError = PG_GETARG_BOOL(5);
96 : int converted;
97 :
6054 peter_e@gmx.net 98 3 : CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8U);
99 :
1621 heikki.linnakangas@i 100 3 : converted = UtfToLocal(src, len, dest,
101 : &koi8u_from_unicode_tree,
102 : NULL, 0,
103 : NULL,
104 : PG_KOI8U,
105 : noError);
106 :
107 3 : PG_RETURN_INT32(converted);
108 : }
109 :
110 : Datum
6054 peter_e@gmx.net 111 3 : koi8u_to_utf8(PG_FUNCTION_ARGS)
112 : {
113 3 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
114 3 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
115 3 : int len = PG_GETARG_INT32(4);
1621 heikki.linnakangas@i 116 3 : bool noError = PG_GETARG_BOOL(5);
117 : int converted;
118 :
6054 peter_e@gmx.net 119 3 : CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8U, PG_UTF8);
120 :
1621 heikki.linnakangas@i 121 3 : converted = LocalToUtf(src, len, dest,
122 : &koi8u_to_unicode_tree,
123 : NULL, 0,
124 : NULL,
125 : PG_KOI8U,
126 : noError);
127 :
128 3 : PG_RETURN_INT32(converted);
129 : }
|