40 #include "utils/fmgrprotos.h"
96 int len,
bool is_client_to_server);
112 int current_server_encoding;
127 if (current_server_encoding ==
encoding ||
146 current_server_encoding);
159 convinfo->
s_encoding = current_server_encoding;
192 if (oldinfo->
s_encoding == current_server_encoding &&
210 int current_server_encoding;
229 if (current_server_encoding ==
encoding ||
250 if (convinfo->
s_encoding == current_server_encoding &&
283 int current_server_encoding;
296 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
297 errmsg(
"conversion between %s and %s is not supported",
308 if (current_server_encoding !=
PG_UTF8 &&
311 Oid utf8_to_server_proc;
314 utf8_to_server_proc =
316 current_server_encoding);
357 int src_encoding,
int dest_encoding)
359 unsigned char *result;
365 if (src_encoding == dest_encoding)
379 elog(
ERROR,
"cannot perform encoding conversion outside a transaction");
384 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
385 errmsg(
"default conversion function for encoding \"%s\" to \"%s\" does not exist",
400 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
402 errdetail(
"String of %d bytes is too long for encoding conversion.",
405 result = (
unsigned char *)
424 Size resultlen = strlen((
char *) result);
428 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
430 errdetail(
"String of %d bytes is too long for encoding conversion.",
433 result = (
unsigned char *)
repalloc(result, resultlen + 1);
472 unsigned char *src,
int srclen,
473 unsigned char *
dest,
int destlen,
515 src_encoding_name, dest_encoding_name);
535 src_encoding_name, dest_encoding_name);
565 if (src_encoding < 0)
567 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
568 errmsg(
"invalid source encoding name \"%s\"",
569 src_encoding_name)));
570 if (dest_encoding < 0)
572 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
573 errmsg(
"invalid destination encoding name \"%s\"",
574 dest_encoding_name)));
589 if (dest_str == src_str)
595 len = strlen(dest_str);
624 if (src_encoding < 0)
626 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
627 errmsg(
"invalid encoding name \"%s\"",
628 src_encoding_name)));
712 (
errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
713 errmsg(
"invalid byte value for encoding \"%s\": 0x%02x",
715 (
unsigned char) s[
i])));
784 bool is_client_to_server)
791 if (is_client_to_server)
813 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
815 errdetail(
"String of %d bytes is too long for encoding conversion.",
836 Size resultlen = strlen(result);
840 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
842 errdetail(
"String of %d bytes is too long for encoding conversion.",
845 result = (
char *)
repalloc(result, resultlen + 1);
876 (
errcode(ERRCODE_SYNTAX_ERROR),
877 errmsg(
"invalid Unicode code point")));
882 s[0] = (
unsigned char)
c;
889 if (server_encoding ==
PG_UTF8)
899 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
900 errmsg(
"conversion between %s and %s is not supported",
907 c_as_utf8[c_as_utf8_len] =
'\0';
940 s[0] = (
unsigned char)
c;
947 if (server_encoding ==
PG_UTF8)
961 c_as_utf8[c_as_utf8_len] =
'\0';
973 return (converted_len == c_as_utf8_len);
1043 return strlen(mbstr);
1065 while (limit > 0 && *mbstr)
1106 while (
len > 0 && *mbstr)
1108 l = (*mblen_fn) ((
const unsigned char *) mbstr);
1109 if ((clen + l) > limit)
1135 while (
len > 0 && *mbstr)
1155 while (l <
len &&
str[l])
1187 raw_pg_bind_textdomain_codeset(
const char *domainname,
int encoding)
1194 if (bind_textdomain_codeset(domainname,
1199 elog(
LOG,
"bind_textdomain_codeset failed");
1226 pg_bind_textdomain_codeset(
const char *domainname)
1233 const char *ctype =
setlocale(LC_CTYPE, NULL);
1238 raw_pg_bind_textdomain_codeset(domainname,
encoding))
1246 if (!raw_pg_bind_textdomain_codeset(domainname, new_msgenc))
1327 unsigned char *lastbyte = charptr +
len - 1;
1333 while (*lastbyte < (
unsigned char) 255)
1336 if ((*mbverify) (charptr,
len) ==
len)
1362 unsigned char limit;
1407 if (
a == 0x7F ||
a == 0xDF ||
a == 0xEF ||
a == 0xF4)
1454 charptr[0] = charptr[1] = 0xa1;
1465 for (
i = 2;
i > 0;
i--)
1489 for (
i = 1;
i >= 0;
i--)
1609 const char *nullpos = memchr(mbstr, 0,
len);
1611 if (nullpos == NULL)
1642 l = (*mbverifychar) ((
const unsigned char *) mbstr,
len);
1672 int expected_src_encoding,
1673 int expected_dest_encoding)
1676 elog(
ERROR,
"invalid source encoding ID: %d", src_encoding);
1677 if (src_encoding != expected_src_encoding && expected_src_encoding >= 0)
1678 elog(
ERROR,
"expected source encoding \"%s\", but got \"%s\"",
1682 elog(
ERROR,
"invalid destination encoding ID: %d", dest_encoding);
1683 if (dest_encoding != expected_dest_encoding && expected_dest_encoding >= 0)
1684 elog(
ERROR,
"expected destination encoding \"%s\", but got \"%s\"",
1688 elog(
ERROR,
"encoding conversion length must not be negative");
1701 char buf[8 * 5 + 1];
1707 jlimit =
Min(jlimit, 8);
1709 for (
j = 0;
j < jlimit;
j++)
1711 p +=
sprintf(p,
"0x%02x", (
unsigned char) mbstr[
j]);
1717 (
errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
1718 errmsg(
"invalid byte sequence for encoding \"%s\": %s",
1731 const char *mbstr,
int len)
1734 char buf[8 * 5 + 1];
1740 jlimit =
Min(jlimit, 8);
1742 for (
j = 0;
j < jlimit;
j++)
1744 p +=
sprintf(p,
"0x%02x", (
unsigned char) mbstr[
j]);
1750 (
errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
1751 errmsg(
"character with byte sequence %s in encoding \"%s\" has no equivalent in encoding \"%s\"",
1766 pgwin32_message_to_UTF16(
const char *
str,
int len,
int *utf16len)
1786 utf16 = (WCHAR *)
palloc(
sizeof(WCHAR) * (
len + 1));
1787 dstlen = MultiByteToWideChar(codepage, 0,
str,
len, utf16,
len);
1788 utf16[dstlen] = (WCHAR) 0;
1808 utf8 = (
char *)
str;
1810 utf16 = (WCHAR *)
palloc(
sizeof(WCHAR) * (
len + 1));
1811 dstlen = MultiByteToWideChar(CP_UTF8, 0, utf8,
len, utf16,
len);
1812 utf16[dstlen] = (WCHAR) 0;
1818 if (dstlen == 0 &&
len > 0)
#define write_stderr(str)
#define unconstify(underlying_type, expr)
#define IS_HIGHBIT_SET(ch)
#define Assert(condition)
#define OidIsValid(objectId)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
const char * pg_enc2gettext_tbl[]
const pg_enc2name pg_enc2name_tbl[]
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
#define PG_FREE_IF_COPY(ptr, n)
#define PG_GETARG_BYTEA_PP(n)
#define PG_RETURN_BYTEA_P(x)
#define DirectFunctionCall1(func, arg1)
#define PG_GETARG_DATUM(n)
#define PG_GETARG_NAME(n)
#define OidFunctionCall6(functionId, arg1, arg2, arg3, arg4, arg5, arg6)
#define PG_RETURN_INT32(x)
#define PG_GETARG_INT32(n)
#define PG_RETURN_DATUM(x)
#define DirectFunctionCall3(func, arg1, arg2, arg3)
#define FunctionCall6(flinfo, arg1, arg2, arg3, arg4, arg5, arg6)
List * lcons(void *datum, List *list)
const char * pg_get_client_encoding_name(void)
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s)
char * pg_any_to_server(const char *s, int len, int encoding)
int GetDatabaseEncoding(void)
Datum pg_convert_to(PG_FUNCTION_ARGS)
struct ConvProcInfo ConvProcInfo
int pg_encoding_wchar2mb_with_len(int encoding, const pg_wchar *from, char *to, int len)
static bool pg_generic_charinc(unsigned char *charptr, int len)
static const pg_enc2name * ClientEncoding
static FmgrInfo * ToServerConvProc
static FmgrInfo * ToClientConvProc
int pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
void InitializeClientEncoding(void)
int pg_dsplen(const char *mbstr)
int pg_mbstrlen_with_len(const char *mbstr, int limit)
mbcharacter_incrementer pg_database_encoding_character_incrementer(void)
char * pg_client_to_server(const char *s, int len)
int pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len)
static FmgrInfo * Utf8ToServerConvProc
static List * ConvProcList
int pg_mb2wchar(const char *from, pg_wchar *to)
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Datum PG_char_to_encoding(PG_FUNCTION_ARGS)
static const pg_enc2name * MessageEncoding
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
int pg_wchar2mb(const pg_wchar *from, char *to)
int pg_mbstrlen(const char *mbstr)
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
static char * perform_default_encoding_conversion(const char *src, int len, bool is_client_to_server)
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
int pg_mbcliplen(const char *mbstr, int len, int limit)
int GetMessageEncoding(void)
Datum pg_encoding_max_length_sql(PG_FUNCTION_ARGS)
Datum getdatabaseencoding(PG_FUNCTION_ARGS)
int pg_do_encoding_conversion_buf(Oid proc, int src_encoding, int dest_encoding, unsigned char *src, int srclen, unsigned char *dest, int destlen, bool noError)
void report_invalid_encoding(int encoding, const char *mbstr, int len)
int SetClientEncoding(int encoding)
void SetMessageEncoding(int encoding)
void pg_unicode_to_server(pg_wchar c, unsigned char *s)
Datum pg_convert(PG_FUNCTION_ARGS)
void check_encoding_conversion_args(int src_encoding, int dest_encoding, int len, int expected_src_encoding, int expected_dest_encoding)
int pg_database_encoding_max_length(void)
int PrepareClientEncoding(int encoding)
const char * GetDatabaseEncodingName(void)
static bool backend_startup_complete
char * pg_server_to_client(const char *s, int len)
Datum pg_convert_from(PG_FUNCTION_ARGS)
int pg_get_client_encoding(void)
static bool pg_utf8_increment(unsigned char *charptr, int length)
Datum length_in_encoding(PG_FUNCTION_ARGS)
static int cliplen(const char *str, int len, int limit)
static int pending_client_encoding
void SetDatabaseEncoding(int encoding)
int pg_encoding_mbcliplen(int encoding, const char *mbstr, int len, int limit)
Datum pg_client_encoding(PG_FUNCTION_ARGS)
Datum PG_encoding_to_char(PG_FUNCTION_ARGS)
int pg_encoding_mb2wchar_with_len(int encoding, const char *from, pg_wchar *to, int len)
char * pg_server_to_any(const char *s, int len, int encoding)
static bool pg_eucjp_increment(unsigned char *charptr, int length)
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
static const pg_enc2name * DatabaseEncoding
int pg_mblen(const char *mbstr)
void pfree(void *pointer)
MemoryContext TopMemoryContext
MemoryContext CurrentMemoryContext
void * repalloc(void *pointer, Size size)
void * MemoryContextAlloc(MemoryContext context, Size size)
void * MemoryContextAllocHuge(MemoryContext context, Size size)
Datum namein(PG_FUNCTION_ARGS)
Oid FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
#define foreach_delete_current(lst, var_or_cell)
#define MAX_MULTIBYTE_CHAR_LEN
#define MAX_CONVERSION_GROWTH
static unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
bool(* mbcharacter_incrementer)(unsigned char *mbstr, int len)
int(* mbchar_verifier)(const unsigned char *mbstr, int len)
#define PG_VALID_ENCODING(_enc)
#define PG_VALID_FE_ENCODING(_enc)
static bool is_valid_unicode_codepoint(pg_wchar c)
#define PG_VALID_BE_ENCODING(_enc)
#define pg_encoding_to_char
#define pg_char_to_encoding
int(* mblen_converter)(const unsigned char *mbstr)
int pg_strcasecmp(const char *s1, const char *s2)
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
static Datum BoolGetDatum(bool X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
static int32 DatumGetInt32(Datum X)
MemoryContextSwitchTo(old_ctx)
mbstr_verifier mbverifystr
wchar2mb_with_len_converter wchar2mb_with_len
mb2wchar_with_len_converter mb2wchar_with_len
mbdisplaylen_converter dsplen
mbchar_verifier mbverifychar
#define SET_VARSIZE(PTR, len)
#define VARSIZE_ANY_EXHDR(PTR)
const pg_wchar_tbl pg_wchar_table[]
int pg_encoding_max_length(int encoding)
int pg_encoding_mblen(int encoding, const char *mbstr)
size_t pg_wchar_strlen(const pg_wchar *str)
bool IsTransactionState(void)