PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mbutils.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "mb/pg_wchar.h"
#include "utils/fmgrprotos.h"
#include "utils/memutils.h"
#include "varatt.h"
Include dependency graph for mbutils.c:

Go to the source code of this file.

Data Structures

struct  ConvProcInfo
 

Typedefs

typedef struct ConvProcInfo ConvProcInfo
 

Functions

static char * perform_default_encoding_conversion (const char *src, int len, bool is_client_to_server)
 
static int cliplen (const char *str, int len, int limit)
 
int PrepareClientEncoding (int encoding)
 
int SetClientEncoding (int encoding)
 
void InitializeClientEncoding (void)
 
int pg_get_client_encoding (void)
 
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)
 
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)
 
Datum pg_convert_to (PG_FUNCTION_ARGS)
 
Datum pg_convert_from (PG_FUNCTION_ARGS)
 
Datum pg_convert (PG_FUNCTION_ARGS)
 
Datum length_in_encoding (PG_FUNCTION_ARGS)
 
Datum pg_encoding_max_length_sql (PG_FUNCTION_ARGS)
 
char * pg_client_to_server (const char *s, int len)
 
char * pg_any_to_server (const char *s, int len, int encoding)
 
char * pg_server_to_client (const char *s, int len)
 
char * pg_server_to_any (const char *s, int len, int encoding)
 
void pg_unicode_to_server (pg_wchar c, unsigned char *s)
 
bool pg_unicode_to_server_noerror (pg_wchar c, unsigned char *s)
 
int pg_mb2wchar (const char *from, pg_wchar *to)
 
int pg_mb2wchar_with_len (const char *from, pg_wchar *to, int len)
 
int pg_encoding_mb2wchar_with_len (int encoding, const char *from, pg_wchar *to, int len)
 
int pg_wchar2mb (const pg_wchar *from, char *to)
 
int pg_wchar2mb_with_len (const pg_wchar *from, char *to, int len)
 
int pg_encoding_wchar2mb_with_len (int encoding, const pg_wchar *from, char *to, int len)
 
int pg_mblen (const char *mbstr)
 
int pg_dsplen (const char *mbstr)
 
int pg_mbstrlen (const char *mbstr)
 
int pg_mbstrlen_with_len (const char *mbstr, int limit)
 
int pg_mbcliplen (const char *mbstr, int len, int limit)
 
int pg_encoding_mbcliplen (int encoding, const char *mbstr, int len, int limit)
 
int pg_mbcharcliplen (const char *mbstr, int len, int limit)
 
void SetDatabaseEncoding (int encoding)
 
void SetMessageEncoding (int encoding)
 
int GetDatabaseEncoding (void)
 
const char * GetDatabaseEncodingName (void)
 
Datum getdatabaseencoding (PG_FUNCTION_ARGS)
 
Datum pg_client_encoding (PG_FUNCTION_ARGS)
 
Datum PG_char_to_encoding (PG_FUNCTION_ARGS)
 
Datum PG_encoding_to_char (PG_FUNCTION_ARGS)
 
int GetMessageEncoding (void)
 
static bool pg_generic_charinc (unsigned char *charptr, int len)
 
static bool pg_utf8_increment (unsigned char *charptr, int length)
 
static bool pg_eucjp_increment (unsigned char *charptr, int length)
 
mbcharacter_incrementer pg_database_encoding_character_incrementer (void)
 
int pg_database_encoding_max_length (void)
 
bool pg_verifymbstr (const char *mbstr, int len, bool noError)
 
bool pg_verify_mbstr (int encoding, const char *mbstr, int len, bool noError)
 
int pg_verify_mbstr_len (int encoding, const char *mbstr, int len, bool noError)
 
void check_encoding_conversion_args (int src_encoding, int dest_encoding, int len, int expected_src_encoding, int expected_dest_encoding)
 
void report_invalid_encoding (int encoding, const char *mbstr, int len)
 
void report_untranslatable_char (int src_encoding, int dest_encoding, const char *mbstr, int len)
 

Variables

static ListConvProcList = NIL
 
static FmgrInfoToServerConvProc = NULL
 
static FmgrInfoToClientConvProc = NULL
 
static FmgrInfoUtf8ToServerConvProc = NULL
 
static const pg_enc2nameClientEncoding = &pg_enc2name_tbl[PG_SQL_ASCII]
 
static const pg_enc2nameDatabaseEncoding = &pg_enc2name_tbl[PG_SQL_ASCII]
 
static const pg_enc2nameMessageEncoding = &pg_enc2name_tbl[PG_SQL_ASCII]
 
static bool backend_startup_complete = false
 
static int pending_client_encoding = PG_SQL_ASCII
 

Typedef Documentation

◆ ConvProcInfo

typedef struct ConvProcInfo ConvProcInfo

Function Documentation

◆ check_encoding_conversion_args()

void check_encoding_conversion_args ( int  src_encoding,
int  dest_encoding,
int  len,
int  expected_src_encoding,
int  expected_dest_encoding 
)

Definition at line 1669 of file mbutils.c.

1674{
1675 if (!PG_VALID_ENCODING(src_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\"",
1679 pg_enc2name_tbl[expected_src_encoding].name,
1680 pg_enc2name_tbl[src_encoding].name);
1681 if (!PG_VALID_ENCODING(dest_encoding))
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\"",
1685 pg_enc2name_tbl[expected_dest_encoding].name,
1686 pg_enc2name_tbl[dest_encoding].name);
1687 if (len < 0)
1688 elog(ERROR, "encoding conversion length must not be negative");
1689}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:308
const void size_t len
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:287
const char * name

References elog, ERROR, len, name, pg_enc2name_tbl, and PG_VALID_ENCODING.

◆ cliplen()

static int cliplen ( const char *  str,
int  len,
int  limit 
)
static

Definition at line 1150 of file mbutils.c.

1151{
1152 int l = 0;
1153
1154 len = Min(len, limit);
1155 while (l < len && str[l])
1156 l++;
1157 return l;
1158}
#define Min(x, y)
Definition: c.h:961
const char * str

References len, Min, and str.

Referenced by pg_encoding_mbcliplen(), pg_mbcharcliplen(), and pgstat_clip_activity().

◆ getdatabaseencoding()

Datum getdatabaseencoding ( PG_FUNCTION_ARGS  )

Definition at line 1273 of file mbutils.c.

1274{
1276}
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
const char * name
Definition: pg_wchar.h:341

References CStringGetDatum(), DatabaseEncoding, DirectFunctionCall1, pg_enc2name::name, and namein().

◆ GetDatabaseEncoding()

◆ GetDatabaseEncodingName()

◆ GetMessageEncoding()

int GetMessageEncoding ( void  )

Definition at line 1308 of file mbutils.c.

1309{
1310 return MessageEncoding->encoding;
1311}
static const pg_enc2name * MessageEncoding
Definition: mbutils.c:82

References pg_enc2name::encoding, and MessageEncoding.

◆ InitializeClientEncoding()

void InitializeClientEncoding ( void  )

Definition at line 281 of file mbutils.c.

282{
283 int current_server_encoding;
284
287
290 {
291 /*
292 * Oops, the requested conversion is not available. We couldn't fail
293 * before, but we can now.
294 */
296 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
297 errmsg("conversion between %s and %s is not supported",
300 }
301
302 /*
303 * Also look up the UTF8-to-server conversion function if needed. Since
304 * the server encoding is fixed within any one backend process, we don't
305 * have to do this more than once.
306 */
307 current_server_encoding = GetDatabaseEncoding();
308 if (current_server_encoding != PG_UTF8 &&
309 current_server_encoding != PG_SQL_ASCII)
310 {
311 Oid utf8_to_server_proc;
312
314 utf8_to_server_proc =
316 current_server_encoding);
317 /* If there's no such conversion, just leave the pointer as NULL */
318 if (OidIsValid(utf8_to_server_proc))
319 {
320 FmgrInfo *finfo;
321
323 sizeof(FmgrInfo));
324 fmgr_info_cxt(utf8_to_server_proc, finfo,
326 /* Set Utf8ToServerConvProc only after data is fully valid */
327 Utf8ToServerConvProc = finfo;
328 }
329 }
330}
#define Assert(condition)
Definition: c.h:815
#define OidIsValid(objectId)
Definition: c.h:732
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define FATAL
Definition: elog.h:41
#define ereport(elevel,...)
Definition: elog.h:149
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261
static FmgrInfo * Utf8ToServerConvProc
Definition: mbutils.c:75
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1267
int SetClientEncoding(int encoding)
Definition: mbutils.c:208
int PrepareClientEncoding(int encoding)
Definition: mbutils.c:110
static bool backend_startup_complete
Definition: mbutils.c:90
static int pending_client_encoding
Definition: mbutils.c:91
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
MemoryContext TopMemoryContext
Definition: mcxt.c:149
Oid FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
Definition: namespace.c:4080
@ PG_SQL_ASCII
Definition: pg_wchar.h:226
@ PG_UTF8
Definition: pg_wchar.h:232
unsigned int Oid
Definition: postgres_ext.h:32
Definition: fmgr.h:57
bool IsTransactionState(void)
Definition: xact.c:386

References Assert, backend_startup_complete, ereport, errcode(), errmsg(), FATAL, FindDefaultConversionProc(), fmgr_info_cxt(), GetDatabaseEncoding(), GetDatabaseEncodingName(), IsTransactionState(), MemoryContextAlloc(), name, OidIsValid, pending_client_encoding, pg_enc2name_tbl, PG_SQL_ASCII, PG_UTF8, PrepareClientEncoding(), SetClientEncoding(), TopMemoryContext, and Utf8ToServerConvProc.

Referenced by InitPostgres().

◆ length_in_encoding()

Datum length_in_encoding ( PG_FUNCTION_ARGS  )

Definition at line 615 of file mbutils.c.

616{
617 bytea *string = PG_GETARG_BYTEA_PP(0);
618 char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
619 int src_encoding = pg_char_to_encoding(src_encoding_name);
620 const char *src_str;
621 int len;
622 int retval;
623
624 if (src_encoding < 0)
626 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
627 errmsg("invalid encoding name \"%s\"",
628 src_encoding_name)));
629
630 len = VARSIZE_ANY_EXHDR(string);
631 src_str = VARDATA_ANY(string);
632
633 retval = pg_verify_mbstr_len(src_encoding, src_str, len, false);
634
635 PG_RETURN_INT32(retval);
636}
#define NameStr(name)
Definition: c.h:703
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
Definition: mbutils.c:1597
#define pg_char_to_encoding
Definition: pg_wchar.h:629
Definition: c.h:644
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References ereport, errcode(), errmsg(), ERROR, len, NameStr, pg_char_to_encoding, PG_GETARG_BYTEA_PP, PG_GETARG_NAME, PG_RETURN_INT32, pg_verify_mbstr_len(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ perform_default_encoding_conversion()

static char * perform_default_encoding_conversion ( const char *  src,
int  len,
bool  is_client_to_server 
)
static

Definition at line 783 of file mbutils.c.

785{
786 char *result;
787 int src_encoding,
788 dest_encoding;
789 FmgrInfo *flinfo;
790
791 if (is_client_to_server)
792 {
793 src_encoding = ClientEncoding->encoding;
794 dest_encoding = DatabaseEncoding->encoding;
795 flinfo = ToServerConvProc;
796 }
797 else
798 {
799 src_encoding = DatabaseEncoding->encoding;
800 dest_encoding = ClientEncoding->encoding;
801 flinfo = ToClientConvProc;
802 }
803
804 if (flinfo == NULL)
805 return unconstify(char *, src);
806
807 /*
808 * Allocate space for conversion result, being wary of integer overflow.
809 * See comments in pg_do_encoding_conversion.
810 */
813 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
814 errmsg("out of memory"),
815 errdetail("String of %d bytes is too long for encoding conversion.",
816 len)));
817
818 result = (char *)
821
822 FunctionCall6(flinfo,
823 Int32GetDatum(src_encoding),
824 Int32GetDatum(dest_encoding),
825 CStringGetDatum(src),
826 CStringGetDatum(result),
828 BoolGetDatum(false));
829
830 /*
831 * Release extra space if there might be a lot --- see comments in
832 * pg_do_encoding_conversion.
833 */
834 if (len > 1000000)
835 {
836 Size resultlen = strlen(result);
837
838 if (resultlen >= MaxAllocSize)
840 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
841 errmsg("out of memory"),
842 errdetail("String of %d bytes is too long for encoding conversion.",
843 len)));
844
845 result = (char *) repalloc(result, resultlen + 1);
846 }
847
848 return result;
849}
#define unconstify(underlying_type, expr)
Definition: c.h:1202
size_t Size
Definition: c.h:562
int errdetail(const char *fmt,...)
Definition: elog.c:1203
#define MaxAllocSize
Definition: fe_memutils.h:22
#define FunctionCall6(flinfo, arg1, arg2, arg3, arg4, arg5, arg6)
Definition: fmgr.h:669
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:80
static FmgrInfo * ToServerConvProc
Definition: mbutils.c:67
static FmgrInfo * ToClientConvProc
Definition: mbutils.c:68
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1541
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void * MemoryContextAllocHuge(MemoryContext context, Size size)
Definition: mcxt.c:1640
#define MaxAllocHugeSize
Definition: memutils.h:45
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:302
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217

References BoolGetDatum(), ClientEncoding, CStringGetDatum(), CurrentMemoryContext, DatabaseEncoding, pg_enc2name::encoding, ereport, errcode(), errdetail(), errmsg(), ERROR, FunctionCall6, Int32GetDatum(), len, MAX_CONVERSION_GROWTH, MaxAllocHugeSize, MaxAllocSize, MemoryContextAllocHuge(), repalloc(), ToClientConvProc, ToServerConvProc, and unconstify.

Referenced by pg_any_to_server(), and pg_server_to_any().

◆ pg_any_to_server()

char * pg_any_to_server ( const char *  s,
int  len,
int  encoding 
)

Definition at line 676 of file mbutils.c.

677{
678 if (len <= 0)
679 return unconstify(char *, s); /* empty string is always valid */
680
683 {
684 /*
685 * No conversion is needed, but we must still validate the data.
686 */
687 (void) pg_verify_mbstr(DatabaseEncoding->encoding, s, len, false);
688 return unconstify(char *, s);
689 }
690
692 {
693 /*
694 * No conversion is possible, but we must still validate the data,
695 * because the client-side code might have done string escaping using
696 * the selected client_encoding. If the client encoding is ASCII-safe
697 * then we just do a straight validation under that encoding. For an
698 * ASCII-unsafe encoding we have a problem: we dare not pass such data
699 * to the parser but we have no way to convert it. We compromise by
700 * rejecting the data if it contains any non-ASCII characters.
701 */
703 (void) pg_verify_mbstr(encoding, s, len, false);
704 else
705 {
706 int i;
707
708 for (i = 0; i < len; i++)
709 {
710 if (s[i] == '\0' || IS_HIGHBIT_SET(s[i]))
712 (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
713 errmsg("invalid byte value for encoding \"%s\": 0x%02x",
715 (unsigned char) s[i])));
716 }
717 }
718 return unconstify(char *, s);
719 }
720
721 /* Fast path if we can use cached conversion function */
724
725 /* General case ... will not work outside transactions */
726 return (char *) pg_do_encoding_conversion((unsigned char *) unconstify(char *, s),
727 len,
728 encoding,
730}
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1112
int i
Definition: isn.c:72
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition: mbutils.c:356
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: mbutils.c:1566
static char * perform_default_encoding_conversion(const char *src, int len, bool is_client_to_server)
Definition: mbutils.c:783
int32 encoding
Definition: pg_database.h:41
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:281

References ClientEncoding, DatabaseEncoding, encoding, pg_enc2name::encoding, ereport, errcode(), errmsg(), ERROR, i, IS_HIGHBIT_SET, len, name, perform_default_encoding_conversion(), pg_do_encoding_conversion(), pg_enc2name_tbl, PG_SQL_ASCII, PG_VALID_BE_ENCODING, pg_verify_mbstr(), and unconstify.

Referenced by ASN1_STRING_to_text(), cache_single_string(), db_encoding_convert(), dsnowball_lexize(), pg_client_to_server(), pg_stat_statements_internal(), pgp_armor_headers(), PLyUnicode_Bytes(), read_extension_script_file(), tsearch_readline(), utf_u2e(), X509_NAME_to_cstring(), and xml_recv().

◆ PG_char_to_encoding()

Datum PG_char_to_encoding ( PG_FUNCTION_ARGS  )

Definition at line 1285 of file mbutils.c.

1286{
1287 Name s = PG_GETARG_NAME(0);
1288
1290}
Definition: c.h:698

References NameStr, pg_char_to_encoding, PG_GETARG_NAME, and PG_RETURN_INT32.

◆ pg_client_encoding()

Datum pg_client_encoding ( PG_FUNCTION_ARGS  )

◆ pg_client_to_server()

char * pg_client_to_server ( const char *  s,
int  len 
)

Definition at line 660 of file mbutils.c.

661{
663}
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676

References ClientEncoding, pg_enc2name::encoding, len, and pg_any_to_server().

Referenced by exec_bind_message(), parse_fcall_arguments(), pq_getmsgstring(), and pq_getmsgtext().

◆ pg_convert()

Datum pg_convert ( PG_FUNCTION_ARGS  )

Definition at line 553 of file mbutils.c.

554{
555 bytea *string = PG_GETARG_BYTEA_PP(0);
556 char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
557 int src_encoding = pg_char_to_encoding(src_encoding_name);
558 char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2));
559 int dest_encoding = pg_char_to_encoding(dest_encoding_name);
560 const char *src_str;
561 char *dest_str;
562 bytea *retval;
563 int len;
564
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)));
575
576 /* make sure that source string is valid */
577 len = VARSIZE_ANY_EXHDR(string);
578 src_str = VARDATA_ANY(string);
579 (void) pg_verify_mbstr(src_encoding, src_str, len, false);
580
581 /* perform conversion */
582 dest_str = (char *) pg_do_encoding_conversion((unsigned char *) unconstify(char *, src_str),
583 len,
584 src_encoding,
585 dest_encoding);
586
587
588 /* return source string if no conversion happened */
589 if (dest_str == src_str)
590 PG_RETURN_BYTEA_P(string);
591
592 /*
593 * build bytea data type structure.
594 */
595 len = strlen(dest_str);
596 retval = (bytea *) palloc(len + VARHDRSZ);
597 SET_VARSIZE(retval, len + VARHDRSZ);
598 memcpy(VARDATA(retval), dest_str, len);
599 pfree(dest_str);
600
601 /* free memory if allocated by the toaster */
602 PG_FREE_IF_COPY(string, 0);
603
604 PG_RETURN_BYTEA_P(retval);
605}
#define VARHDRSZ
Definition: c.h:649
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References ereport, errcode(), errmsg(), ERROR, len, NameStr, palloc(), pfree(), pg_char_to_encoding, pg_do_encoding_conversion(), PG_FREE_IF_COPY, PG_GETARG_BYTEA_PP, PG_GETARG_NAME, PG_RETURN_BYTEA_P, pg_verify_mbstr(), SET_VARSIZE, unconstify, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by pg_convert_from(), and pg_convert_to().

◆ pg_convert_from()

Datum pg_convert_from ( PG_FUNCTION_ARGS  )

Definition at line 526 of file mbutils.c.

527{
528 Datum string = PG_GETARG_DATUM(0);
529 Datum src_encoding_name = PG_GETARG_DATUM(1);
530 Datum dest_encoding_name = DirectFunctionCall1(namein,
532 Datum result;
533
534 result = DirectFunctionCall3(pg_convert, string,
535 src_encoding_name, dest_encoding_name);
536
537 /*
538 * pg_convert returns a bytea, which we in turn return as text, relying on
539 * the fact that they are both in fact varlena types, and thus
540 * structurally identical. Although not all bytea values are valid text,
541 * in this case it will be because we've told pg_convert to return one
542 * that is valid as text in the current database encoding.
543 */
544 PG_RETURN_DATUM(result);
545}
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:645
Datum pg_convert(PG_FUNCTION_ARGS)
Definition: mbutils.c:553
uintptr_t Datum
Definition: postgres.h:69

References CStringGetDatum(), DatabaseEncoding, DirectFunctionCall1, DirectFunctionCall3, pg_enc2name::name, namein(), pg_convert(), PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ pg_convert_to()

Datum pg_convert_to ( PG_FUNCTION_ARGS  )

Definition at line 501 of file mbutils.c.

502{
503 Datum string = PG_GETARG_DATUM(0);
504 Datum dest_encoding_name = PG_GETARG_DATUM(1);
505 Datum src_encoding_name = DirectFunctionCall1(namein,
507 Datum result;
508
509 /*
510 * pg_convert expects a bytea as its first argument. We're passing it a
511 * text argument here, relying on the fact that they are both in fact
512 * varlena types, and thus structurally identical.
513 */
514 result = DirectFunctionCall3(pg_convert, string,
515 src_encoding_name, dest_encoding_name);
516
517 PG_RETURN_DATUM(result);
518}

References CStringGetDatum(), DatabaseEncoding, DirectFunctionCall1, DirectFunctionCall3, pg_enc2name::name, namein(), pg_convert(), PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ pg_database_encoding_character_incrementer()

mbcharacter_incrementer pg_database_encoding_character_incrementer ( void  )

Definition at line 1523 of file mbutils.c.

1524{
1525 /*
1526 * Eventually it might be best to add a field to pg_wchar_table[], but for
1527 * now we just use a switch.
1528 */
1529 switch (GetDatabaseEncoding())
1530 {
1531 case PG_UTF8:
1532 return pg_utf8_increment;
1533
1534 case PG_EUC_JP:
1535 return pg_eucjp_increment;
1536
1537 default:
1538 return pg_generic_charinc;
1539 }
1540}
static bool pg_generic_charinc(unsigned char *charptr, int len)
Definition: mbutils.c:1325
static bool pg_utf8_increment(unsigned char *charptr, int length)
Definition: mbutils.c:1359
static bool pg_eucjp_increment(unsigned char *charptr, int length)
Definition: mbutils.c:1437
@ PG_EUC_JP
Definition: pg_wchar.h:227

References GetDatabaseEncoding(), PG_EUC_JP, pg_eucjp_increment(), pg_generic_charinc(), PG_UTF8, and pg_utf8_increment().

Referenced by make_greater_string().

◆ pg_database_encoding_max_length()

◆ pg_do_encoding_conversion()

unsigned char * pg_do_encoding_conversion ( unsigned char *  src,
int  len,
int  src_encoding,
int  dest_encoding 
)

Definition at line 356 of file mbutils.c.

358{
359 unsigned char *result;
360 Oid proc;
361
362 if (len <= 0)
363 return src; /* empty string is always valid */
364
365 if (src_encoding == dest_encoding)
366 return src; /* no conversion required, assume valid */
367
368 if (dest_encoding == PG_SQL_ASCII)
369 return src; /* any string is valid in SQL_ASCII */
370
371 if (src_encoding == PG_SQL_ASCII)
372 {
373 /* No conversion is possible, but we must validate the result */
374 (void) pg_verify_mbstr(dest_encoding, (const char *) src, len, false);
375 return src;
376 }
377
378 if (!IsTransactionState()) /* shouldn't happen */
379 elog(ERROR, "cannot perform encoding conversion outside a transaction");
380
381 proc = FindDefaultConversionProc(src_encoding, dest_encoding);
382 if (!OidIsValid(proc))
384 (errcode(ERRCODE_UNDEFINED_FUNCTION),
385 errmsg("default conversion function for encoding \"%s\" to \"%s\" does not exist",
386 pg_encoding_to_char(src_encoding),
387 pg_encoding_to_char(dest_encoding))));
388
389 /*
390 * Allocate space for conversion result, being wary of integer overflow.
391 *
392 * len * MAX_CONVERSION_GROWTH is typically a vast overestimate of the
393 * required space, so it might exceed MaxAllocSize even though the result
394 * would actually fit. We do not want to hand back a result string that
395 * exceeds MaxAllocSize, because callers might not cope gracefully --- but
396 * if we just allocate more than that, and don't use it, that's fine.
397 */
400 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
401 errmsg("out of memory"),
402 errdetail("String of %d bytes is too long for encoding conversion.",
403 len)));
404
405 result = (unsigned char *)
408
409 (void) OidFunctionCall6(proc,
410 Int32GetDatum(src_encoding),
411 Int32GetDatum(dest_encoding),
412 CStringGetDatum((char *) src),
413 CStringGetDatum((char *) result),
415 BoolGetDatum(false));
416
417 /*
418 * If the result is large, it's worth repalloc'ing to release any extra
419 * space we asked for. The cutoff here is somewhat arbitrary, but we
420 * *must* check when len * MAX_CONVERSION_GROWTH exceeds MaxAllocSize.
421 */
422 if (len > 1000000)
423 {
424 Size resultlen = strlen((char *) result);
425
426 if (resultlen >= MaxAllocSize)
428 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
429 errmsg("out of memory"),
430 errdetail("String of %d bytes is too long for encoding conversion.",
431 len)));
432
433 result = (unsigned char *) repalloc(result, resultlen + 1);
434 }
435
436 return result;
437}
#define OidFunctionCall6(functionId, arg1, arg2, arg3, arg4, arg5, arg6)
Definition: fmgr.h:689
#define pg_encoding_to_char
Definition: pg_wchar.h:630

References BoolGetDatum(), CStringGetDatum(), CurrentMemoryContext, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, FindDefaultConversionProc(), Int32GetDatum(), IsTransactionState(), len, MAX_CONVERSION_GROWTH, MaxAllocHugeSize, MaxAllocSize, MemoryContextAllocHuge(), OidFunctionCall6, OidIsValid, pg_encoding_to_char, PG_SQL_ASCII, pg_verify_mbstr(), and repalloc().

Referenced by convert_charset(), pg_any_to_server(), pg_convert(), and pg_server_to_any().

◆ pg_do_encoding_conversion_buf()

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 
)

Definition at line 469 of file mbutils.c.

475{
476 Datum result;
477
478 /*
479 * If the destination buffer is not large enough to hold the result in the
480 * worst case, limit the input size passed to the conversion function.
481 */
482 if ((Size) srclen >= ((destlen - 1) / (Size) MAX_CONVERSION_GROWTH))
483 srclen = ((destlen - 1) / (Size) MAX_CONVERSION_GROWTH);
484
485 result = OidFunctionCall6(proc,
486 Int32GetDatum(src_encoding),
487 Int32GetDatum(dest_encoding),
488 CStringGetDatum((char *) src),
489 CStringGetDatum((char *) dest),
490 Int32GetDatum(srclen),
491 BoolGetDatum(noError));
492 return DatumGetInt32(result);
493}
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:207

References BoolGetDatum(), CStringGetDatum(), DatumGetInt32(), generate_unaccent_rules::dest, Int32GetDatum(), MAX_CONVERSION_GROWTH, and OidFunctionCall6.

Referenced by CopyConversionError(), CopyConvertBuf(), and test_enc_conversion().

◆ pg_dsplen()

int pg_dsplen ( const char *  mbstr)

Definition at line 1030 of file mbutils.c.

1031{
1032 return pg_wchar_table[DatabaseEncoding->encoding].dsplen((const unsigned char *) mbstr);
1033}
mbdisplaylen_converter dsplen
Definition: pg_wchar.h:383

References DatabaseEncoding, pg_wchar_tbl::dsplen, pg_enc2name::encoding, and pg_wchar_table.

Referenced by p_isspecial().

◆ pg_encoding_max_length_sql()

Datum pg_encoding_max_length_sql ( PG_FUNCTION_ARGS  )

Definition at line 644 of file mbutils.c.

645{
646 int encoding = PG_GETARG_INT32(0);
647
650 else
652}
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269

References encoding, PG_GETARG_INT32, PG_RETURN_INT32, PG_RETURN_NULL, PG_VALID_ENCODING, and pg_wchar_table.

◆ pg_encoding_mb2wchar_with_len()

int pg_encoding_mb2wchar_with_len ( int  encoding,
const char *  from,
pg_wchar to,
int  len 
)

Definition at line 993 of file mbutils.c.

995{
996 return pg_wchar_table[encoding].mb2wchar_with_len((const unsigned char *) from, to, len);
997}
mb2wchar_with_len_converter mb2wchar_with_len
Definition: pg_wchar.h:378

References encoding, len, pg_wchar_tbl::mb2wchar_with_len, and pg_wchar_table.

◆ pg_encoding_mbcliplen()

int pg_encoding_mbcliplen ( int  encoding,
const char *  mbstr,
int  len,
int  limit 
)

Definition at line 1093 of file mbutils.c.

1095{
1096 mblen_converter mblen_fn;
1097 int clen = 0;
1098 int l;
1099
1100 /* optimization for single byte encoding */
1102 return cliplen(mbstr, len, limit);
1103
1104 mblen_fn = pg_wchar_table[encoding].mblen;
1105
1106 while (len > 0 && *mbstr)
1107 {
1108 l = (*mblen_fn) ((const unsigned char *) mbstr);
1109 if ((clen + l) > limit)
1110 break;
1111 clen += l;
1112 if (clen == limit)
1113 break;
1114 len -= l;
1115 mbstr += l;
1116 }
1117 return clen;
1118}
static int cliplen(const char *str, int len, int limit)
Definition: mbutils.c:1150
int(* mblen_converter)(const unsigned char *mbstr)
Definition: pg_wchar.h:366
mblen_converter mblen
Definition: pg_wchar.h:382
int pg_encoding_max_length(int encoding)
Definition: wchar.c:2174

References cliplen(), encoding, len, pg_wchar_tbl::mblen, pg_encoding_max_length(), and pg_wchar_table.

Referenced by pg_mbcliplen().

◆ PG_encoding_to_char()

Datum PG_encoding_to_char ( PG_FUNCTION_ARGS  )

Definition at line 1293 of file mbutils.c.

1294{
1296 const char *encoding_name = pg_encoding_to_char(encoding);
1297
1298 return DirectFunctionCall1(namein, CStringGetDatum(encoding_name));
1299}
int32_t int32
Definition: c.h:484

References CStringGetDatum(), DirectFunctionCall1, encoding, namein(), pg_encoding_to_char, and PG_GETARG_INT32.

◆ pg_encoding_wchar2mb_with_len()

int pg_encoding_wchar2mb_with_len ( int  encoding,
const pg_wchar from,
char *  to,
int  len 
)

Definition at line 1015 of file mbutils.c.

1017{
1018 return pg_wchar_table[encoding].wchar2mb_with_len(from, (unsigned char *) to, len);
1019}
wchar2mb_with_len_converter wchar2mb_with_len
Definition: pg_wchar.h:380

References encoding, len, pg_wchar_table, and pg_wchar_tbl::wchar2mb_with_len.

◆ pg_eucjp_increment()

static bool pg_eucjp_increment ( unsigned char *  charptr,
int  length 
)
static

Definition at line 1437 of file mbutils.c.

1438{
1439 unsigned char c1,
1440 c2;
1441 int i;
1442
1443 c1 = *charptr;
1444
1445 switch (c1)
1446 {
1447 case SS2: /* JIS X 0201 */
1448 if (length != 2)
1449 return false;
1450
1451 c2 = charptr[1];
1452
1453 if (c2 >= 0xdf)
1454 charptr[0] = charptr[1] = 0xa1;
1455 else if (c2 < 0xa1)
1456 charptr[1] = 0xa1;
1457 else
1458 charptr[1]++;
1459 break;
1460
1461 case SS3: /* JIS X 0212 */
1462 if (length != 3)
1463 return false;
1464
1465 for (i = 2; i > 0; i--)
1466 {
1467 c2 = charptr[i];
1468 if (c2 < 0xa1)
1469 {
1470 charptr[i] = 0xa1;
1471 return true;
1472 }
1473 else if (c2 < 0xfe)
1474 {
1475 charptr[i]++;
1476 return true;
1477 }
1478 }
1479
1480 /* Out of 3-byte code region */
1481 return false;
1482
1483 default:
1484 if (IS_HIGHBIT_SET(c1)) /* JIS X 0208? */
1485 {
1486 if (length != 2)
1487 return false;
1488
1489 for (i = 1; i >= 0; i--)
1490 {
1491 c2 = charptr[i];
1492 if (c2 < 0xa1)
1493 {
1494 charptr[i] = 0xa1;
1495 return true;
1496 }
1497 else if (c2 < 0xfe)
1498 {
1499 charptr[i]++;
1500 return true;
1501 }
1502 }
1503
1504 /* Out of 2 byte code region */
1505 return false;
1506 }
1507 else
1508 { /* ASCII, single byte */
1509 if (c1 > 0x7e)
1510 return false;
1511 (*charptr)++;
1512 }
1513 break;
1514 }
1515
1516 return true;
1517}
#define SS2
Definition: pg_wchar.h:38
#define SS3
Definition: pg_wchar.h:39

References i, IS_HIGHBIT_SET, SS2, and SS3.

Referenced by pg_database_encoding_character_incrementer().

◆ pg_generic_charinc()

static bool pg_generic_charinc ( unsigned char *  charptr,
int  len 
)
static

Definition at line 1325 of file mbutils.c.

1326{
1327 unsigned char *lastbyte = charptr + len - 1;
1328 mbchar_verifier mbverify;
1329
1330 /* We can just invoke the character verifier directly. */
1332
1333 while (*lastbyte < (unsigned char) 255)
1334 {
1335 (*lastbyte)++;
1336 if ((*mbverify) (charptr, len) == len)
1337 return true;
1338 }
1339
1340 return false;
1341}
int(* mbchar_verifier)(const unsigned char *mbstr, int len)
Definition: pg_wchar.h:372
mbchar_verifier mbverifychar
Definition: pg_wchar.h:384

References GetDatabaseEncoding(), len, pg_wchar_tbl::mbverifychar, and pg_wchar_table.

Referenced by pg_database_encoding_character_incrementer().

◆ pg_get_client_encoding()

int pg_get_client_encoding ( void  )

Definition at line 336 of file mbutils.c.

337{
338 return ClientEncoding->encoding;
339}

References ClientEncoding, and pg_enc2name::encoding.

Referenced by BeginCopyFrom(), BeginCopyTo(), and xml_send().

◆ pg_get_client_encoding_name()

const char * pg_get_client_encoding_name ( void  )

Definition at line 345 of file mbutils.c.

346{
347 return ClientEncoding->name;
348}

References ClientEncoding, and pg_enc2name::name.

◆ pg_mb2wchar()

int pg_mb2wchar ( const char *  from,
pg_wchar to 
)

Definition at line 979 of file mbutils.c.

980{
981 return pg_wchar_table[DatabaseEncoding->encoding].mb2wchar_with_len((const unsigned char *) from, to, strlen(from));
982}

References DatabaseEncoding, pg_enc2name::encoding, pg_wchar_tbl::mb2wchar_with_len, and pg_wchar_table.

◆ pg_mb2wchar_with_len()

◆ pg_mbcharcliplen()

int pg_mbcharcliplen ( const char *  mbstr,
int  len,
int  limit 
)

Definition at line 1125 of file mbutils.c.

1126{
1127 int clen = 0;
1128 int nch = 0;
1129 int l;
1130
1131 /* optimization for single byte encoding */
1133 return cliplen(mbstr, len, limit);
1134
1135 while (len > 0 && *mbstr)
1136 {
1137 l = pg_mblen(mbstr);
1138 nch++;
1139 if (nch > limit)
1140 break;
1141 clen += l;
1142 len -= l;
1143 mbstr += l;
1144 }
1145 return clen;
1146}
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1546
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1023

References cliplen(), len, pg_database_encoding_max_length(), and pg_mblen().

Referenced by bpchar(), bpchar_input(), text_left(), text_right(), varchar(), and varchar_input().

◆ pg_mbcliplen()

◆ pg_mblen()

int pg_mblen ( const char *  mbstr)

Definition at line 1023 of file mbutils.c.

1024{
1025 return pg_wchar_table[DatabaseEncoding->encoding].mblen((const unsigned char *) mbstr);
1026}

References DatabaseEncoding, pg_enc2name::encoding, pg_wchar_tbl::mblen, and pg_wchar_table.

Referenced by addCompoundAffixFlagValue(), bit_in(), charlen_to_bytelen(), DCH_from_char(), dotrim(), find_word(), findchar(), findchar2(), findwrd(), gbt_var_node_cp_len(), get_modifiers(), get_nextfield(), get_wildcard_part(), getlexeme(), getNextFlagFromString(), gettoken_query(), gettoken_query_standard(), gettoken_query_websearch(), gettoken_tsvector(), hex_decode_safe(), infix(), initTrie(), lpad(), make_trigrams(), map_sql_identifier_to_xml_name(), map_xml_name_to_sql_identifier(), match_prosrc_to_literal(), mb_strchr(), NIImportAffixes(), NIImportDictionary(), NIImportOOAffixes(), NUM_eat_non_data_chars(), NUM_processor(), parse_affentry(), parse_format(), parse_lquery(), parse_ltree(), parse_or_operator(), parse_re_flags(), parse_test_flags(), pg_base64_decode(), pg_mbcharcliplen(), pg_mbstrlen(), pg_mbstrlen_with_len(), prssyntaxerror(), readstoplist(), report_json_context(), rpad(), RS_compile(), RS_execute(), RS_isRegis(), similar_escape_internal(), split_text(), t_isalnum(), t_isalpha(), text_format(), text_position_next(), text_position_next_internal(), text_reverse(), text_substring(), text_to_bits(), textregexreplace(), thesaurusRead(), TParserGet(), translate(), ts_stat_sql(), tsvectorout(), unaccent_lexize(), varbit_in(), varstr_levenshtein(), and wchareq().

◆ pg_mbstrlen()

int pg_mbstrlen ( const char *  mbstr)

Definition at line 1037 of file mbutils.c.

1038{
1039 int len = 0;
1040
1041 /* optimization for single byte encoding */
1043 return strlen(mbstr);
1044
1045 while (*mbstr)
1046 {
1047 mbstr += pg_mblen(mbstr);
1048 len++;
1049 }
1050 return len;
1051}

References len, pg_database_encoding_max_length(), and pg_mblen().

Referenced by NUM_processor(), and text_format_append_string().

◆ pg_mbstrlen_with_len()

int pg_mbstrlen_with_len ( const char *  mbstr,
int  limit 
)

Definition at line 1057 of file mbutils.c.

1058{
1059 int len = 0;
1060
1061 /* optimization for single byte encoding */
1063 return limit;
1064
1065 while (limit > 0 && *mbstr)
1066 {
1067 int l = pg_mblen(mbstr);
1068
1069 limit -= l;
1070 mbstr += l;
1071 len++;
1072 }
1073 return len;
1074}

References len, pg_database_encoding_max_length(), and pg_mblen().

Referenced by bpchar(), bpchar_input(), bpcharlen(), executor_errposition(), lpad(), match_prosrc_to_query(), parser_errposition(), plpgsql_scanner_errposition(), rpad(), scanner_errposition(), similar_escape_internal(), text_left(), text_length(), text_position_get_match_pos(), text_right(), text_substring(), unicode_assigned(), unicode_is_normalized(), unicode_normalize_func(), and varstr_levenshtein().

◆ pg_server_to_any()

char * pg_server_to_any ( const char *  s,
int  len,
int  encoding 
)

Definition at line 749 of file mbutils.c.

750{
751 if (len <= 0)
752 return unconstify(char *, s); /* empty string is always valid */
753
756 return unconstify(char *, s); /* assume data is valid */
757
759 {
760 /* No conversion is possible, but we must validate the result */
761 (void) pg_verify_mbstr(encoding, s, len, false);
762 return unconstify(char *, s);
763 }
764
765 /* Fast path if we can use cached conversion function */
768
769 /* General case ... will not work outside transactions */
770 return (char *) pg_do_encoding_conversion((unsigned char *) unconstify(char *, s),
771 len,
773 encoding);
774}

References ClientEncoding, DatabaseEncoding, encoding, pg_enc2name::encoding, len, perform_default_encoding_conversion(), pg_do_encoding_conversion(), PG_SQL_ASCII, pg_verify_mbstr(), and unconstify.

Referenced by compareStrings(), CopyAttributeOutCSV(), CopyAttributeOutText(), daitch_mokotoff(), DoCopyTo(), dsnowball_lexize(), hv_fetch_string(), hv_store_string(), pg_server_to_client(), PLyUnicode_FromStringAndSize(), and utf_e2u().

◆ pg_server_to_client()

char * pg_server_to_client ( const char *  s,
int  len 
)

Definition at line 738 of file mbutils.c.

739{
741}
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:749

References ClientEncoding, pg_enc2name::encoding, len, and pg_server_to_any().

Referenced by pq_puttextmessage(), pq_sendcountedtext(), pq_sendstring(), pq_sendtext(), and pq_writestring().

◆ pg_unicode_to_server()

void pg_unicode_to_server ( pg_wchar  c,
unsigned char *  s 
)

Definition at line 864 of file mbutils.c.

865{
866 unsigned char c_as_utf8[MAX_MULTIBYTE_CHAR_LEN + 1];
867 int c_as_utf8_len;
868 int server_encoding;
869
870 /*
871 * Complain if invalid Unicode code point. The choice of errcode here is
872 * debatable, but really our caller should have checked this anyway.
873 */
876 (errcode(ERRCODE_SYNTAX_ERROR),
877 errmsg("invalid Unicode code point")));
878
879 /* Otherwise, if it's in ASCII range, conversion is trivial */
880 if (c <= 0x7F)
881 {
882 s[0] = (unsigned char) c;
883 s[1] = '\0';
884 return;
885 }
886
887 /* If the server encoding is UTF-8, we just need to reformat the code */
888 server_encoding = GetDatabaseEncoding();
889 if (server_encoding == PG_UTF8)
890 {
891 unicode_to_utf8(c, s);
892 s[pg_utf_mblen(s)] = '\0';
893 return;
894 }
895
896 /* For all other cases, we must have a conversion function available */
897 if (Utf8ToServerConvProc == NULL)
899 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
900 errmsg("conversion between %s and %s is not supported",
903
904 /* Construct UTF-8 source string */
905 unicode_to_utf8(c, c_as_utf8);
906 c_as_utf8_len = pg_utf_mblen(c_as_utf8);
907 c_as_utf8[c_as_utf8_len] = '\0';
908
909 /* Convert, or throw error if we can't */
912 Int32GetDatum(server_encoding),
913 CStringGetDatum((char *) c_as_utf8),
914 CStringGetDatum((char *) s),
915 Int32GetDatum(c_as_utf8_len),
916 BoolGetDatum(false));
917}
#define MAX_MULTIBYTE_CHAR_LEN
Definition: pg_wchar.h:33
#define pg_utf_mblen
Definition: pg_wchar.h:633
static unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
Definition: pg_wchar.h:575
static bool is_valid_unicode_codepoint(pg_wchar c)
Definition: pg_wchar.h:519
char * c

References BoolGetDatum(), CStringGetDatum(), ereport, errcode(), errmsg(), ERROR, FunctionCall6, GetDatabaseEncoding(), GetDatabaseEncodingName(), Int32GetDatum(), is_valid_unicode_codepoint(), MAX_MULTIBYTE_CHAR_LEN, name, pg_enc2name_tbl, PG_UTF8, pg_utf_mblen, unicode_to_utf8(), and Utf8ToServerConvProc.

Referenced by addunicode(), addUnicodeChar(), map_xml_name_to_sql_identifier(), str_udeescape(), and unistr().

◆ pg_unicode_to_server_noerror()

bool pg_unicode_to_server_noerror ( pg_wchar  c,
unsigned char *  s 
)

Definition at line 926 of file mbutils.c.

927{
928 unsigned char c_as_utf8[MAX_MULTIBYTE_CHAR_LEN + 1];
929 int c_as_utf8_len;
930 int converted_len;
931 int server_encoding;
932
933 /* Fail if invalid Unicode code point */
935 return false;
936
937 /* Otherwise, if it's in ASCII range, conversion is trivial */
938 if (c <= 0x7F)
939 {
940 s[0] = (unsigned char) c;
941 s[1] = '\0';
942 return true;
943 }
944
945 /* If the server encoding is UTF-8, we just need to reformat the code */
946 server_encoding = GetDatabaseEncoding();
947 if (server_encoding == PG_UTF8)
948 {
949 unicode_to_utf8(c, s);
950 s[pg_utf_mblen(s)] = '\0';
951 return true;
952 }
953
954 /* For all other cases, we must have a conversion function available */
955 if (Utf8ToServerConvProc == NULL)
956 return false;
957
958 /* Construct UTF-8 source string */
959 unicode_to_utf8(c, c_as_utf8);
960 c_as_utf8_len = pg_utf_mblen(c_as_utf8);
961 c_as_utf8[c_as_utf8_len] = '\0';
962
963 /* Convert, but without throwing error if we can't */
966 Int32GetDatum(server_encoding),
967 CStringGetDatum((char *) c_as_utf8),
968 CStringGetDatum((char *) s),
969 Int32GetDatum(c_as_utf8_len),
970 BoolGetDatum(true)));
971
972 /* Conversion was successful iff it consumed the whole input */
973 return (converted_len == c_as_utf8_len);
974}

References BoolGetDatum(), CStringGetDatum(), DatumGetInt32(), FunctionCall6, GetDatabaseEncoding(), Int32GetDatum(), is_valid_unicode_codepoint(), MAX_MULTIBYTE_CHAR_LEN, PG_UTF8, pg_utf_mblen, unicode_to_utf8(), and Utf8ToServerConvProc.

Referenced by addUnicodeChar(), and json_lex_string().

◆ pg_utf8_increment()

static bool pg_utf8_increment ( unsigned char *  charptr,
int  length 
)
static

Definition at line 1359 of file mbutils.c.

1360{
1361 unsigned char a;
1362 unsigned char limit;
1363
1364 switch (length)
1365 {
1366 default:
1367 /* reject lengths 5 and 6 for now */
1368 return false;
1369 case 4:
1370 a = charptr[3];
1371 if (a < 0xBF)
1372 {
1373 charptr[3]++;
1374 break;
1375 }
1376 /* FALL THRU */
1377 case 3:
1378 a = charptr[2];
1379 if (a < 0xBF)
1380 {
1381 charptr[2]++;
1382 break;
1383 }
1384 /* FALL THRU */
1385 case 2:
1386 a = charptr[1];
1387 switch (*charptr)
1388 {
1389 case 0xED:
1390 limit = 0x9F;
1391 break;
1392 case 0xF4:
1393 limit = 0x8F;
1394 break;
1395 default:
1396 limit = 0xBF;
1397 break;
1398 }
1399 if (a < limit)
1400 {
1401 charptr[1]++;
1402 break;
1403 }
1404 /* FALL THRU */
1405 case 1:
1406 a = *charptr;
1407 if (a == 0x7F || a == 0xDF || a == 0xEF || a == 0xF4)
1408 return false;
1409 charptr[0]++;
1410 break;
1411 }
1412
1413 return true;
1414}
int a
Definition: isn.c:68

References a.

Referenced by pg_database_encoding_character_incrementer().

◆ pg_verify_mbstr()

bool pg_verify_mbstr ( int  encoding,
const char *  mbstr,
int  len,
bool  noError 
)

Definition at line 1566 of file mbutils.c.

1567{
1568 int oklen;
1569
1571
1572 oklen = pg_wchar_table[encoding].mbverifystr((const unsigned char *) mbstr, len);
1573 if (oklen != len)
1574 {
1575 if (noError)
1576 return false;
1577 report_invalid_encoding(encoding, mbstr + oklen, len - oklen);
1578 }
1579 return true;
1580}
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: mbutils.c:1698
mbstr_verifier mbverifystr
Definition: pg_wchar.h:385

References Assert, encoding, len, pg_wchar_tbl::mbverifystr, PG_VALID_ENCODING, pg_wchar_table, and report_invalid_encoding().

Referenced by AddFileToBackupManifest(), LogicalOutputWrite(), pg_any_to_server(), pg_convert(), pg_do_encoding_conversion(), pg_server_to_any(), pg_verifymbstr(), and read_extension_script_file().

◆ pg_verify_mbstr_len()

int pg_verify_mbstr_len ( int  encoding,
const char *  mbstr,
int  len,
bool  noError 
)

Definition at line 1597 of file mbutils.c.

1598{
1599 mbchar_verifier mbverifychar;
1600 int mb_len;
1601
1603
1604 /*
1605 * In single-byte encodings, we need only reject nulls (\0).
1606 */
1608 {
1609 const char *nullpos = memchr(mbstr, 0, len);
1610
1611 if (nullpos == NULL)
1612 return len;
1613 if (noError)
1614 return -1;
1615 report_invalid_encoding(encoding, nullpos, 1);
1616 }
1617
1618 /* fetch function pointer just once */
1619 mbverifychar = pg_wchar_table[encoding].mbverifychar;
1620
1621 mb_len = 0;
1622
1623 while (len > 0)
1624 {
1625 int l;
1626
1627 /* fast path for ASCII-subset characters */
1628 if (!IS_HIGHBIT_SET(*mbstr))
1629 {
1630 if (*mbstr != '\0')
1631 {
1632 mb_len++;
1633 mbstr++;
1634 len--;
1635 continue;
1636 }
1637 if (noError)
1638 return -1;
1640 }
1641
1642 l = (*mbverifychar) ((const unsigned char *) mbstr, len);
1643
1644 if (l < 0)
1645 {
1646 if (noError)
1647 return -1;
1649 }
1650
1651 mbstr += l;
1652 len -= l;
1653 mb_len++;
1654 }
1655 return mb_len;
1656}

References Assert, encoding, IS_HIGHBIT_SET, len, pg_wchar_tbl::mbverifychar, pg_encoding_max_length(), PG_VALID_ENCODING, pg_wchar_table, and report_invalid_encoding().

Referenced by length_in_encoding().

◆ pg_verifymbstr()

bool pg_verifymbstr ( const char *  mbstr,
int  len,
bool  noError 
)

◆ pg_wchar2mb()

int pg_wchar2mb ( const pg_wchar from,
char *  to 
)

Definition at line 1001 of file mbutils.c.

1002{
1003 return pg_wchar_table[DatabaseEncoding->encoding].wchar2mb_with_len(from, (unsigned char *) to, pg_wchar_strlen(from));
1004}
size_t pg_wchar_strlen(const pg_wchar *str)
Definition: wstrncmp.c:70

References DatabaseEncoding, pg_enc2name::encoding, pg_wchar_strlen(), pg_wchar_table, and pg_wchar_tbl::wchar2mb_with_len.

◆ pg_wchar2mb_with_len()

int pg_wchar2mb_with_len ( const pg_wchar from,
char *  to,
int  len 
)

◆ PrepareClientEncoding()

int PrepareClientEncoding ( int  encoding)

Definition at line 110 of file mbutils.c.

111{
112 int current_server_encoding;
113 ListCell *lc;
114
116 return -1;
117
118 /* Can't do anything during startup, per notes above */
120 return 0;
121
122 current_server_encoding = GetDatabaseEncoding();
123
124 /*
125 * Check for cases that require no conversion function.
126 */
127 if (current_server_encoding == encoding ||
128 current_server_encoding == PG_SQL_ASCII ||
130 return 0;
131
132 if (IsTransactionState())
133 {
134 /*
135 * If we're in a live transaction, it's safe to access the catalogs,
136 * so look up the functions. We repeat the lookup even if the info is
137 * already cached, so that we can react to changes in the contents of
138 * pg_conversion.
139 */
140 Oid to_server_proc,
141 to_client_proc;
142 ConvProcInfo *convinfo;
143 MemoryContext oldcontext;
144
145 to_server_proc = FindDefaultConversionProc(encoding,
146 current_server_encoding);
147 if (!OidIsValid(to_server_proc))
148 return -1;
149 to_client_proc = FindDefaultConversionProc(current_server_encoding,
150 encoding);
151 if (!OidIsValid(to_client_proc))
152 return -1;
153
154 /*
155 * Load the fmgr info into TopMemoryContext (could still fail here)
156 */
158 sizeof(ConvProcInfo));
159 convinfo->s_encoding = current_server_encoding;
160 convinfo->c_encoding = encoding;
161 fmgr_info_cxt(to_server_proc, &convinfo->to_server_info,
163 fmgr_info_cxt(to_client_proc, &convinfo->to_client_info,
165
166 /* Attach new info to head of list */
168 ConvProcList = lcons(convinfo, ConvProcList);
169 MemoryContextSwitchTo(oldcontext);
170
171 /*
172 * We cannot yet remove any older entry for the same encoding pair,
173 * since it could still be in use. SetClientEncoding will clean up.
174 */
175
176 return 0; /* success */
177 }
178 else
179 {
180 /*
181 * If we're not in a live transaction, the only thing we can do is
182 * restore a previous setting using the cache. This covers all
183 * transaction-rollback cases. The only case it might not work for is
184 * trying to change client_encoding on the fly by editing
185 * postgresql.conf and SIGHUP'ing. Which would probably be a stupid
186 * thing to do anyway.
187 */
188 foreach(lc, ConvProcList)
189 {
190 ConvProcInfo *oldinfo = (ConvProcInfo *) lfirst(lc);
191
192 if (oldinfo->s_encoding == current_server_encoding &&
193 oldinfo->c_encoding == encoding)
194 return 0;
195 }
196
197 return -1; /* it's not cached, so fail */
198 }
199}
List * lcons(void *datum, List *list)
Definition: list.c:495
static List * ConvProcList
Definition: mbutils.c:61
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define lfirst(lc)
Definition: pg_list.h:172
#define PG_VALID_FE_ENCODING(_enc)
Definition: pg_wchar.h:291
int s_encoding
Definition: mbutils.c:55
FmgrInfo to_client_info
Definition: mbutils.c:58
int c_encoding
Definition: mbutils.c:56
FmgrInfo to_server_info
Definition: mbutils.c:57

References backend_startup_complete, ConvProcInfo::c_encoding, ConvProcList, encoding, FindDefaultConversionProc(), fmgr_info_cxt(), GetDatabaseEncoding(), IsTransactionState(), lcons(), lfirst, MemoryContextAlloc(), MemoryContextSwitchTo(), OidIsValid, PG_SQL_ASCII, PG_VALID_FE_ENCODING, ConvProcInfo::s_encoding, ConvProcInfo::to_client_info, ConvProcInfo::to_server_info, and TopMemoryContext.

Referenced by check_client_encoding(), and InitializeClientEncoding().

◆ report_invalid_encoding()

void report_invalid_encoding ( int  encoding,
const char *  mbstr,
int  len 
)

Definition at line 1698 of file mbutils.c.

1699{
1700 int l = pg_encoding_mblen(encoding, mbstr);
1701 char buf[8 * 5 + 1];
1702 char *p = buf;
1703 int j,
1704 jlimit;
1705
1706 jlimit = Min(l, len);
1707 jlimit = Min(jlimit, 8); /* prevent buffer overrun */
1708
1709 for (j = 0; j < jlimit; j++)
1710 {
1711 p += sprintf(p, "0x%02x", (unsigned char) mbstr[j]);
1712 if (j < jlimit - 1)
1713 p += sprintf(p, " ");
1714 }
1715
1716 ereport(ERROR,
1717 (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
1718 errmsg("invalid byte sequence for encoding \"%s\": %s",
1720 buf)));
1721}
int j
Definition: isn.c:73
static char * buf
Definition: pg_test_fsync.c:72
#define sprintf
Definition: port.h:241
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:2116

References buf, encoding, ereport, errcode(), errmsg(), ERROR, j, len, Min, name, pg_enc2name_tbl, pg_encoding_mblen(), and sprintf.

Referenced by big52euc_tw(), big52mic(), CopyConversionError(), euc_cn2mic(), euc_jis_20042shift_jis_2004(), euc_jp2mic(), euc_jp2sjis(), euc_kr2mic(), euc_tw2big5(), euc_tw2mic(), iso8859_1_to_utf8(), latin2mic(), latin2mic_with_table(), local2local(), LocalToUtf(), mic2big5(), mic2euc_cn(), mic2euc_jp(), mic2euc_kr(), mic2euc_tw(), mic2latin(), mic2latin_with_table(), mic2sjis(), pg_verify_mbstr(), pg_verify_mbstr_len(), shift_jis_20042euc_jis_2004(), sjis2euc_jp(), sjis2mic(), test_enc_conversion(), utf8_to_iso8859_1(), and UtfToLocal().

◆ report_untranslatable_char()

void report_untranslatable_char ( int  src_encoding,
int  dest_encoding,
const char *  mbstr,
int  len 
)

Definition at line 1730 of file mbutils.c.

1732{
1733 int l = pg_encoding_mblen(src_encoding, mbstr);
1734 char buf[8 * 5 + 1];
1735 char *p = buf;
1736 int j,
1737 jlimit;
1738
1739 jlimit = Min(l, len);
1740 jlimit = Min(jlimit, 8); /* prevent buffer overrun */
1741
1742 for (j = 0; j < jlimit; j++)
1743 {
1744 p += sprintf(p, "0x%02x", (unsigned char) mbstr[j]);
1745 if (j < jlimit - 1)
1746 p += sprintf(p, " ");
1747 }
1748
1749 ereport(ERROR,
1750 (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
1751 errmsg("character with byte sequence %s in encoding \"%s\" has no equivalent in encoding \"%s\"",
1752 buf,
1753 pg_enc2name_tbl[src_encoding].name,
1754 pg_enc2name_tbl[dest_encoding].name)));
1755}

References buf, ereport, errcode(), errmsg(), ERROR, j, len, Min, name, pg_enc2name_tbl, pg_encoding_mblen(), and sprintf.

Referenced by big52euc_tw(), big52mic(), euc_tw2big5(), latin2mic_with_table(), local2local(), LocalToUtf(), mic2big5(), mic2euc_cn(), mic2euc_jp(), mic2euc_kr(), mic2euc_tw(), mic2latin(), mic2latin_with_table(), mic2sjis(), utf8_to_iso8859_1(), and UtfToLocal().

◆ SetClientEncoding()

int SetClientEncoding ( int  encoding)

Definition at line 208 of file mbutils.c.

209{
210 int current_server_encoding;
211 bool found;
212 ListCell *lc;
213
215 return -1;
216
217 /* Can't do anything during startup, per notes above */
219 {
221 return 0;
222 }
223
224 current_server_encoding = GetDatabaseEncoding();
225
226 /*
227 * Check for cases that require no conversion function.
228 */
229 if (current_server_encoding == encoding ||
230 current_server_encoding == PG_SQL_ASCII ||
232 {
234 ToServerConvProc = NULL;
235 ToClientConvProc = NULL;
236 return 0;
237 }
238
239 /*
240 * Search the cache for the entry previously prepared by
241 * PrepareClientEncoding; if there isn't one, we lose. While at it,
242 * release any duplicate entries so that repeated Prepare/Set cycles don't
243 * leak memory.
244 */
245 found = false;
246 foreach(lc, ConvProcList)
247 {
248 ConvProcInfo *convinfo = (ConvProcInfo *) lfirst(lc);
249
250 if (convinfo->s_encoding == current_server_encoding &&
251 convinfo->c_encoding == encoding)
252 {
253 if (!found)
254 {
255 /* Found newest entry, so set up */
257 ToServerConvProc = &convinfo->to_server_info;
258 ToClientConvProc = &convinfo->to_client_info;
259 found = true;
260 }
261 else
262 {
263 /* Duplicate entry, release it */
265 pfree(convinfo);
266 }
267 }
268 }
269
270 if (found)
271 return 0; /* success */
272 else
273 return -1; /* it's not cached, so fail */
274}
#define foreach_delete_current(lst, var_or_cell)
Definition: pg_list.h:391

References backend_startup_complete, ConvProcInfo::c_encoding, ClientEncoding, ConvProcList, encoding, foreach_delete_current, GetDatabaseEncoding(), lfirst, pending_client_encoding, pfree(), pg_enc2name_tbl, PG_SQL_ASCII, PG_VALID_FE_ENCODING, ConvProcInfo::s_encoding, ConvProcInfo::to_client_info, ConvProcInfo::to_server_info, ToClientConvProc, and ToServerConvProc.

Referenced by assign_client_encoding(), InitializeClientEncoding(), and ParallelWorkerMain().

◆ SetDatabaseEncoding()

void SetDatabaseEncoding ( int  encoding)

Definition at line 1161 of file mbutils.c.

1162{
1164 elog(ERROR, "invalid database encoding: %d", encoding);
1165
1168}

References Assert, DatabaseEncoding, elog, encoding, pg_enc2name::encoding, ERROR, pg_enc2name_tbl, and PG_VALID_BE_ENCODING.

Referenced by CheckMyDatabase().

◆ SetMessageEncoding()

void SetMessageEncoding ( int  encoding)

Definition at line 1171 of file mbutils.c.

1172{
1173 /* Some calls happen before we can elog()! */
1175
1178}

References Assert, encoding, pg_enc2name::encoding, MessageEncoding, pg_enc2name_tbl, and PG_VALID_ENCODING.

Referenced by pg_perm_setlocale().

Variable Documentation

◆ backend_startup_complete

bool backend_startup_complete = false
static

Definition at line 90 of file mbutils.c.

Referenced by InitializeClientEncoding(), PrepareClientEncoding(), and SetClientEncoding().

◆ ClientEncoding

◆ ConvProcList

List* ConvProcList = NIL
static

Definition at line 61 of file mbutils.c.

Referenced by PrepareClientEncoding(), and SetClientEncoding().

◆ DatabaseEncoding

◆ MessageEncoding

const pg_enc2name* MessageEncoding = &pg_enc2name_tbl[PG_SQL_ASCII]
static

Definition at line 82 of file mbutils.c.

Referenced by GetMessageEncoding(), and SetMessageEncoding().

◆ pending_client_encoding

int pending_client_encoding = PG_SQL_ASCII
static

Definition at line 91 of file mbutils.c.

Referenced by InitializeClientEncoding(), and SetClientEncoding().

◆ ToClientConvProc

FmgrInfo* ToClientConvProc = NULL
static

Definition at line 68 of file mbutils.c.

Referenced by perform_default_encoding_conversion(), and SetClientEncoding().

◆ ToServerConvProc

FmgrInfo* ToServerConvProc = NULL
static

Definition at line 67 of file mbutils.c.

Referenced by perform_default_encoding_conversion(), and SetClientEncoding().

◆ Utf8ToServerConvProc

FmgrInfo* Utf8ToServerConvProc = NULL
static