1 | /*------------------------------------------------------------------------- |
2 | * |
3 | * UTF8 and Cyrillic |
4 | * |
5 | * Portions Copyright (c) 1996-2019, 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 | PG_MODULE_MAGIC; |
23 | |
24 | PG_FUNCTION_INFO_V1(utf8_to_koi8r); |
25 | PG_FUNCTION_INFO_V1(koi8r_to_utf8); |
26 | |
27 | PG_FUNCTION_INFO_V1(utf8_to_koi8u); |
28 | PG_FUNCTION_INFO_V1(koi8u_to_utf8); |
29 | |
30 | /* ---------- |
31 | * conv_proc( |
32 | * INTEGER, -- source encoding id |
33 | * INTEGER, -- destination encoding id |
34 | * CSTRING, -- source string (null terminated C string) |
35 | * CSTRING, -- destination string (null terminated C string) |
36 | * INTEGER -- source string length |
37 | * ) returns VOID; |
38 | * ---------- |
39 | */ |
40 | |
41 | Datum |
42 | utf8_to_koi8r(PG_FUNCTION_ARGS) |
43 | { |
44 | unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2); |
45 | unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3); |
46 | int len = PG_GETARG_INT32(4); |
47 | |
48 | CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8R); |
49 | |
50 | UtfToLocal(src, len, dest, |
51 | &koi8r_from_unicode_tree, |
52 | NULL, 0, |
53 | NULL, |
54 | PG_KOI8R); |
55 | |
56 | PG_RETURN_VOID(); |
57 | } |
58 | |
59 | Datum |
60 | koi8r_to_utf8(PG_FUNCTION_ARGS) |
61 | { |
62 | unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2); |
63 | unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3); |
64 | int len = PG_GETARG_INT32(4); |
65 | |
66 | CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_UTF8); |
67 | |
68 | LocalToUtf(src, len, dest, |
69 | &koi8r_to_unicode_tree, |
70 | NULL, 0, |
71 | NULL, |
72 | PG_KOI8R); |
73 | |
74 | PG_RETURN_VOID(); |
75 | } |
76 | |
77 | Datum |
78 | utf8_to_koi8u(PG_FUNCTION_ARGS) |
79 | { |
80 | unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2); |
81 | unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3); |
82 | int len = PG_GETARG_INT32(4); |
83 | |
84 | CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8U); |
85 | |
86 | UtfToLocal(src, len, dest, |
87 | &koi8u_from_unicode_tree, |
88 | NULL, 0, |
89 | NULL, |
90 | PG_KOI8U); |
91 | |
92 | PG_RETURN_VOID(); |
93 | } |
94 | |
95 | Datum |
96 | koi8u_to_utf8(PG_FUNCTION_ARGS) |
97 | { |
98 | unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2); |
99 | unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3); |
100 | int len = PG_GETARG_INT32(4); |
101 | |
102 | CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8U, PG_UTF8); |
103 | |
104 | LocalToUtf(src, len, dest, |
105 | &koi8u_to_unicode_tree, |
106 | NULL, 0, |
107 | NULL, |
108 | PG_KOI8U); |
109 | |
110 | PG_RETURN_VOID(); |
111 | } |
112 | |