1 | /*------------------------------------------------------------------------- |
2 | * |
3 | * ISO 8859 2-16 <--> UTF8 |
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_iso8859/utf8_and_iso8859.c |
10 | * |
11 | *------------------------------------------------------------------------- |
12 | */ |
13 | |
14 | #include "postgres.h" |
15 | #include "fmgr.h" |
16 | #include "mb/pg_wchar.h" |
17 | #include "../../Unicode/iso8859_10_to_utf8.map" |
18 | #include "../../Unicode/iso8859_13_to_utf8.map" |
19 | #include "../../Unicode/iso8859_14_to_utf8.map" |
20 | #include "../../Unicode/iso8859_15_to_utf8.map" |
21 | #include "../../Unicode/iso8859_2_to_utf8.map" |
22 | #include "../../Unicode/iso8859_3_to_utf8.map" |
23 | #include "../../Unicode/iso8859_4_to_utf8.map" |
24 | #include "../../Unicode/iso8859_5_to_utf8.map" |
25 | #include "../../Unicode/iso8859_6_to_utf8.map" |
26 | #include "../../Unicode/iso8859_7_to_utf8.map" |
27 | #include "../../Unicode/iso8859_8_to_utf8.map" |
28 | #include "../../Unicode/iso8859_9_to_utf8.map" |
29 | #include "../../Unicode/utf8_to_iso8859_10.map" |
30 | #include "../../Unicode/utf8_to_iso8859_13.map" |
31 | #include "../../Unicode/utf8_to_iso8859_14.map" |
32 | #include "../../Unicode/utf8_to_iso8859_15.map" |
33 | #include "../../Unicode/utf8_to_iso8859_16.map" |
34 | #include "../../Unicode/utf8_to_iso8859_2.map" |
35 | #include "../../Unicode/utf8_to_iso8859_3.map" |
36 | #include "../../Unicode/utf8_to_iso8859_4.map" |
37 | #include "../../Unicode/utf8_to_iso8859_5.map" |
38 | #include "../../Unicode/utf8_to_iso8859_6.map" |
39 | #include "../../Unicode/utf8_to_iso8859_7.map" |
40 | #include "../../Unicode/utf8_to_iso8859_8.map" |
41 | #include "../../Unicode/utf8_to_iso8859_9.map" |
42 | #include "../../Unicode/iso8859_16_to_utf8.map" |
43 | |
44 | PG_MODULE_MAGIC; |
45 | |
46 | PG_FUNCTION_INFO_V1(iso8859_to_utf8); |
47 | PG_FUNCTION_INFO_V1(utf8_to_iso8859); |
48 | |
49 | /* ---------- |
50 | * conv_proc( |
51 | * INTEGER, -- source encoding id |
52 | * INTEGER, -- destination encoding id |
53 | * CSTRING, -- source string (null terminated C string) |
54 | * CSTRING, -- destination string (null terminated C string) |
55 | * INTEGER -- source string length |
56 | * ) returns VOID; |
57 | * ---------- |
58 | */ |
59 | |
60 | typedef struct |
61 | { |
62 | pg_enc encoding; |
63 | const pg_mb_radix_tree *map1; /* to UTF8 map name */ |
64 | const pg_mb_radix_tree *map2; /* from UTF8 map name */ |
65 | } pg_conv_map; |
66 | |
67 | static const pg_conv_map maps[] = { |
68 | {PG_LATIN2, &iso8859_2_to_unicode_tree, |
69 | &iso8859_2_from_unicode_tree}, /* ISO-8859-2 Latin 2 */ |
70 | {PG_LATIN3, &iso8859_3_to_unicode_tree, |
71 | &iso8859_3_from_unicode_tree}, /* ISO-8859-3 Latin 3 */ |
72 | {PG_LATIN4, &iso8859_4_to_unicode_tree, |
73 | &iso8859_4_from_unicode_tree}, /* ISO-8859-4 Latin 4 */ |
74 | {PG_LATIN5, &iso8859_9_to_unicode_tree, |
75 | &iso8859_9_from_unicode_tree}, /* ISO-8859-9 Latin 5 */ |
76 | {PG_LATIN6, &iso8859_10_to_unicode_tree, |
77 | &iso8859_10_from_unicode_tree}, /* ISO-8859-10 Latin 6 */ |
78 | {PG_LATIN7, &iso8859_13_to_unicode_tree, |
79 | &iso8859_13_from_unicode_tree}, /* ISO-8859-13 Latin 7 */ |
80 | {PG_LATIN8, &iso8859_14_to_unicode_tree, |
81 | &iso8859_14_from_unicode_tree}, /* ISO-8859-14 Latin 8 */ |
82 | {PG_LATIN9, &iso8859_15_to_unicode_tree, |
83 | &iso8859_15_from_unicode_tree}, /* ISO-8859-15 Latin 9 */ |
84 | {PG_LATIN10, &iso8859_16_to_unicode_tree, |
85 | &iso8859_16_from_unicode_tree}, /* ISO-8859-16 Latin 10 */ |
86 | {PG_ISO_8859_5, &iso8859_5_to_unicode_tree, |
87 | &iso8859_5_from_unicode_tree}, /* ISO-8859-5 */ |
88 | {PG_ISO_8859_6, &iso8859_6_to_unicode_tree, |
89 | &iso8859_6_from_unicode_tree}, /* ISO-8859-6 */ |
90 | {PG_ISO_8859_7, &iso8859_7_to_unicode_tree, |
91 | &iso8859_7_from_unicode_tree}, /* ISO-8859-7 */ |
92 | {PG_ISO_8859_8, &iso8859_8_to_unicode_tree, |
93 | &iso8859_8_from_unicode_tree}, /* ISO-8859-8 */ |
94 | }; |
95 | |
96 | Datum |
97 | iso8859_to_utf8(PG_FUNCTION_ARGS) |
98 | { |
99 | int encoding = PG_GETARG_INT32(0); |
100 | unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2); |
101 | unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3); |
102 | int len = PG_GETARG_INT32(4); |
103 | int i; |
104 | |
105 | CHECK_ENCODING_CONVERSION_ARGS(-1, PG_UTF8); |
106 | |
107 | for (i = 0; i < lengthof(maps); i++) |
108 | { |
109 | if (encoding == maps[i].encoding) |
110 | { |
111 | LocalToUtf(src, len, dest, |
112 | maps[i].map1, |
113 | NULL, 0, |
114 | NULL, |
115 | encoding); |
116 | PG_RETURN_VOID(); |
117 | } |
118 | } |
119 | |
120 | ereport(ERROR, |
121 | (errcode(ERRCODE_INTERNAL_ERROR), |
122 | errmsg("unexpected encoding ID %d for ISO 8859 character sets" , |
123 | encoding))); |
124 | |
125 | PG_RETURN_VOID(); |
126 | } |
127 | |
128 | Datum |
129 | utf8_to_iso8859(PG_FUNCTION_ARGS) |
130 | { |
131 | int encoding = PG_GETARG_INT32(1); |
132 | unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2); |
133 | unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3); |
134 | int len = PG_GETARG_INT32(4); |
135 | int i; |
136 | |
137 | CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, -1); |
138 | |
139 | for (i = 0; i < lengthof(maps); i++) |
140 | { |
141 | if (encoding == maps[i].encoding) |
142 | { |
143 | UtfToLocal(src, len, dest, |
144 | maps[i].map2, |
145 | NULL, 0, |
146 | NULL, |
147 | encoding); |
148 | PG_RETURN_VOID(); |
149 | } |
150 | } |
151 | |
152 | ereport(ERROR, |
153 | (errcode(ERRCODE_INTERNAL_ERROR), |
154 | errmsg("unexpected encoding ID %d for ISO 8859 character sets" , |
155 | encoding))); |
156 | |
157 | PG_RETURN_VOID(); |
158 | } |
159 | |