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