1
/*-------------------------------------------------------------------------
5
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
6
* Portions Copyright (c) 1994, Regents of the University of California
9
* src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1/utf8_and_iso8859_1.c
11
*-------------------------------------------------------------------------
16
#include "mb/pg_wchar.h"
20
PG_FUNCTION_INFO_V1(iso8859_1_to_utf8);
21
PG_FUNCTION_INFO_V1(utf8_to_iso8859_1);
23
extern Datum iso8859_1_to_utf8(PG_FUNCTION_ARGS);
24
extern Datum utf8_to_iso8859_1(PG_FUNCTION_ARGS);
28
* INTEGER, -- source encoding id
29
* INTEGER, -- destination encoding id
30
* CSTRING, -- source string (null terminated C string)
31
* CSTRING, -- destination string (null terminated C string)
32
* INTEGER -- source string length
38
iso8859_1_to_utf8(PG_FUNCTION_ARGS)
40
unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
41
unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
42
int len = PG_GETARG_INT32(4);
45
CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN1, PG_UTF8);
51
report_invalid_encoding(PG_LATIN1, (const char *) src, len);
52
if (!IS_HIGHBIT_SET(c))
56
*dest++ = (c >> 6) | 0xc0;
57
*dest++ = (c & 0x003f) | HIGHBIT;
68
utf8_to_iso8859_1(PG_FUNCTION_ARGS)
70
unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
71
unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
72
int len = PG_GETARG_INT32(4);
76
CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_LATIN1);
82
report_invalid_encoding(PG_UTF8, (const char *) src, len);
83
/* fast path for ASCII-subset characters */
84
if (!IS_HIGHBIT_SET(c))
92
int l = pg_utf_mblen(src);
94
if (l > len || !pg_utf8_islegal(src, l))
95
report_invalid_encoding(PG_UTF8, (const char *) src, len);
97
report_untranslatable_char(PG_UTF8, PG_LATIN1,
98
(const char *) src, len);
100
c = ((c & 0x1f) << 6) | c1;
101
if (c >= 0x80 && c <= 0xff)
103
*dest++ = (unsigned char) c;
108
report_untranslatable_char(PG_UTF8, PG_LATIN1,
109
(const char *) src, len);