PostgreSQL Source Code git master
Loading...
Searching...
No Matches
utf8_and_win.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * WIN <--> 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_win/utf8_and_win.c
10 *
11 *-------------------------------------------------------------------------
12 */
13
14#include "postgres.h"
15#include "fmgr.h"
16#include "mb/pg_wchar.h"
17#include "../../Unicode/utf8_to_win1250.map"
18#include "../../Unicode/utf8_to_win1251.map"
19#include "../../Unicode/utf8_to_win1252.map"
20#include "../../Unicode/utf8_to_win1253.map"
21#include "../../Unicode/utf8_to_win1254.map"
22#include "../../Unicode/utf8_to_win1255.map"
23#include "../../Unicode/utf8_to_win1256.map"
24#include "../../Unicode/utf8_to_win1257.map"
25#include "../../Unicode/utf8_to_win1258.map"
26#include "../../Unicode/utf8_to_win866.map"
27#include "../../Unicode/utf8_to_win874.map"
28#include "../../Unicode/win1250_to_utf8.map"
29#include "../../Unicode/win1251_to_utf8.map"
30#include "../../Unicode/win1252_to_utf8.map"
31#include "../../Unicode/win1253_to_utf8.map"
32#include "../../Unicode/win1254_to_utf8.map"
33#include "../../Unicode/win1255_to_utf8.map"
34#include "../../Unicode/win1256_to_utf8.map"
35#include "../../Unicode/win1257_to_utf8.map"
36#include "../../Unicode/win866_to_utf8.map"
37#include "../../Unicode/win874_to_utf8.map"
38#include "../../Unicode/win1258_to_utf8.map"
39
41 .name = "utf8_and_win",
42 .version = PG_VERSION
43);
44
47
48/* ----------
49 * conv_proc(
50 * INTEGER, -- source encoding id
51 * INTEGER, -- destination encoding id
52 * CSTRING, -- source string (null terminated C string)
53 * CSTRING, -- destination string (null terminated C string)
54 * INTEGER, -- source string length
55 * BOOL -- if true, don't throw an error if conversion fails
56 * ) returns INTEGER;
57 *
58 * Returns the number of bytes successfully converted.
59 * ----------
60 */
61
62typedef struct
63{
65 const pg_mb_radix_tree *map1; /* to UTF8 map name */
66 const pg_mb_radix_tree *map2; /* from UTF8 map name */
68
82
85{
87 unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
88 unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
89 int len = PG_GETARG_INT32(4);
90 bool noError = PG_GETARG_BOOL(5);
91 int i;
92
94
95 for (i = 0; i < lengthof(maps); i++)
96 {
97 if (encoding == maps[i].encoding)
98 {
99 int converted;
100
101 converted = LocalToUtf(src, len, dest,
102 maps[i].map1,
103 NULL, 0,
104 NULL,
105 encoding,
106 noError);
108 }
109 }
110
113 errmsg("unexpected encoding ID %d for WIN character sets",
114 encoding)));
115
117}
118
119Datum
121{
122 int encoding = PG_GETARG_INT32(1);
123 unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
124 unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
125 int len = PG_GETARG_INT32(4);
126 bool noError = PG_GETARG_BOOL(5);
127 int i;
128
130
131 for (i = 0; i < lengthof(maps); i++)
132 {
133 if (encoding == maps[i].encoding)
134 {
135 int converted;
136
137 converted = UtfToLocal(src, len, dest,
138 maps[i].map2,
139 NULL, 0,
140 NULL,
141 encoding,
142 noError);
144 }
145 }
146
149 errmsg("unexpected encoding ID %d for WIN character sets",
150 encoding)));
151
153}
#define lengthof(array)
Definition c.h:803
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:507
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:717
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#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
const void size_t len
pg_enc
Definition pg_wchar.h:225
@ PG_WIN1254
Definition pg_wchar.h:257
@ PG_WIN1253
Definition pg_wchar.h:256
@ PG_WIN1250
Definition pg_wchar.h:255
@ PG_WIN1256
Definition pg_wchar.h:244
@ PG_WIN1258
Definition pg_wchar.h:245
@ PG_WIN1252
Definition pg_wchar.h:250
@ PG_WIN1255
Definition pg_wchar.h:258
@ PG_WIN1257
Definition pg_wchar.h:259
@ PG_WIN1251
Definition pg_wchar.h:249
@ PG_WIN866
Definition pg_wchar.h:246
@ PG_WIN874
Definition pg_wchar.h:247
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
Definition pg_wchar.h:507
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
Datum utf8_to_win(PG_FUNCTION_ARGS)
static const pg_conv_map maps[]
Datum win_to_utf8(PG_FUNCTION_ARGS)
const char * name