PostgreSQL Source Code git master
Loading...
Searching...
No Matches
utf8_and_iso8859.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * ISO 8859 2-16 <--> UTF8
4 *
5 * Portions Copyright (c) 1996-2026, 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
45 .name = "utf8_and_iso8859",
46 .version = PG_VERSION
47);
48
51
52/* ----------
53 * conv_proc(
54 * INTEGER, -- source encoding id
55 * INTEGER, -- destination encoding id
56 * CSTRING, -- source string (null terminated C string)
57 * CSTRING, -- destination string (null terminated C string)
58 * INTEGER, -- source string length
59 * BOOL -- if true, don't throw an error if conversion fails
60 * ) returns INTEGER;
61 *
62 * Returns the number of bytes successfully converted.
63 * ----------
64 */
65
66typedef struct
67{
69 const pg_mb_radix_tree *map1; /* to UTF8 map name */
70 const pg_mb_radix_tree *map2; /* from UTF8 map name */
72
101
102Datum
104{
105 int encoding = PG_GETARG_INT32(0);
106 unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
107 unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
108 int len = PG_GETARG_INT32(4);
109 bool noError = PG_GETARG_BOOL(5);
110
112
113 for (size_t i = 0; i < lengthof(maps); i++)
114 {
115 if (encoding == maps[i].encoding)
116 {
117 int converted;
118
119 converted = LocalToUtf(src, len, dest,
120 maps[i].map1,
121 NULL, 0,
122 NULL,
123 encoding,
124 noError);
126 }
127 }
128
131 errmsg("unexpected encoding ID %d for ISO 8859 character sets",
132 encoding)));
133
135}
136
137Datum
139{
140 int encoding = PG_GETARG_INT32(1);
141 unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
142 unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
143 int len = PG_GETARG_INT32(4);
144 bool noError = PG_GETARG_BOOL(5);
145
147
148 for (size_t i = 0; i < lengthof(maps); i++)
149 {
150 if (encoding == maps[i].encoding)
151 {
152 int converted;
153
154 converted = UtfToLocal(src, len, dest,
155 maps[i].map2,
156 NULL, 0,
157 NULL,
158 encoding,
159 noError);
161 }
162 }
163
166 errmsg("unexpected encoding ID %d for ISO 8859 character sets",
167 encoding)));
168
170}
#define lengthof(array)
Definition c.h:932
int UtfToLocal(const unsigned char *utf, int len, unsigned char *iso, const pg_mb_radix_tree *map, const pg_utf_to_local_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding, bool noError)
Definition conv.c:269
int LocalToUtf(const unsigned char *iso, int len, unsigned char *utf, const pg_mb_radix_tree *map, const pg_local_to_utf_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding, bool noError)
Definition conv.c:479
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
#define PG_MODULE_MAGIC_EXT(...)
Definition fmgr.h:540
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
static char * encoding
Definition initdb.c:139
int i
Definition isn.c:77
#define PG_UTF8
Definition mbprint.c:43
static char * errmsg
const void size_t len
pg_enc
Definition pg_wchar.h:75
@ PG_LATIN4
Definition pg_wchar.h:87
@ PG_LATIN9
Definition pg_wchar.h:92
@ PG_ISO_8859_6
Definition pg_wchar.h:102
@ PG_LATIN6
Definition pg_wchar.h:89
@ PG_LATIN5
Definition pg_wchar.h:88
@ PG_LATIN2
Definition pg_wchar.h:85
@ PG_ISO_8859_5
Definition pg_wchar.h:101
@ PG_LATIN10
Definition pg_wchar.h:93
@ PG_ISO_8859_7
Definition pg_wchar.h:103
@ PG_LATIN8
Definition pg_wchar.h:91
@ PG_LATIN3
Definition pg_wchar.h:86
@ PG_LATIN7
Definition pg_wchar.h:90
@ PG_ISO_8859_8
Definition pg_wchar.h:104
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition pg_wchar.h:360
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
const pg_mb_radix_tree * map2
const pg_mb_radix_tree * map1
static const pg_conv_map maps[]
Datum utf8_to_iso8859(PG_FUNCTION_ARGS)
Datum iso8859_to_utf8(PG_FUNCTION_ARGS)
const char * name