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 :
22 12 : PG_MODULE_MAGIC_EXT(
23 : .name = "utf8_and_cyrillic",
24 : .version = PG_VERSION
25 : );
26 :
27 12 : PG_FUNCTION_INFO_V1(utf8_to_koi8r);
28 6 : PG_FUNCTION_INFO_V1(koi8r_to_utf8);
29 :
30 6 : PG_FUNCTION_INFO_V1(utf8_to_koi8u);
31 6 : 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
48 438 : utf8_to_koi8r(PG_FUNCTION_ARGS)
49 : {
50 438 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
51 438 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
52 438 : int len = PG_GETARG_INT32(4);
53 438 : bool noError = PG_GETARG_BOOL(5);
54 : int converted;
55 :
56 438 : CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8R);
57 :
58 438 : converted = UtfToLocal(src, len, dest,
59 : &koi8r_from_unicode_tree,
60 : NULL, 0,
61 : NULL,
62 : PG_KOI8R,
63 : noError);
64 :
65 240 : PG_RETURN_INT32(converted);
66 : }
67 :
68 : Datum
69 6 : koi8r_to_utf8(PG_FUNCTION_ARGS)
70 : {
71 6 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
72 6 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
73 6 : int len = PG_GETARG_INT32(4);
74 6 : bool noError = PG_GETARG_BOOL(5);
75 : int converted;
76 :
77 6 : CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_UTF8);
78 :
79 6 : converted = LocalToUtf(src, len, dest,
80 : &koi8r_to_unicode_tree,
81 : NULL, 0,
82 : NULL,
83 : PG_KOI8R,
84 : noError);
85 :
86 6 : PG_RETURN_INT32(converted);
87 : }
88 :
89 : Datum
90 6 : utf8_to_koi8u(PG_FUNCTION_ARGS)
91 : {
92 6 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
93 6 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
94 6 : int len = PG_GETARG_INT32(4);
95 6 : bool noError = PG_GETARG_BOOL(5);
96 : int converted;
97 :
98 6 : CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8U);
99 :
100 6 : converted = UtfToLocal(src, len, dest,
101 : &koi8u_from_unicode_tree,
102 : NULL, 0,
103 : NULL,
104 : PG_KOI8U,
105 : noError);
106 :
107 6 : PG_RETURN_INT32(converted);
108 : }
109 :
110 : Datum
111 6 : koi8u_to_utf8(PG_FUNCTION_ARGS)
112 : {
113 6 : unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
114 6 : unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
115 6 : int len = PG_GETARG_INT32(4);
116 6 : bool noError = PG_GETARG_BOOL(5);
117 : int converted;
118 :
119 6 : CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8U, PG_UTF8);
120 :
121 6 : converted = LocalToUtf(src, len, dest,
122 : &koi8u_to_unicode_tree,
123 : NULL, 0,
124 : NULL,
125 : PG_KOI8U,
126 : noError);
127 :
128 6 : PG_RETURN_INT32(converted);
129 : }
|