PostgreSQL Source Code  git master
mbutils.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/syscache.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)
 
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 1618 of file mbutils.c.

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

Referenced by surrogate_pair_to_codepoint().

1623 {
1624  if (!PG_VALID_ENCODING(src_encoding))
1625  elog(ERROR, "invalid source encoding ID: %d", src_encoding);
1626  if (src_encoding != expected_src_encoding && expected_src_encoding >= 0)
1627  elog(ERROR, "expected source encoding \"%s\", but got \"%s\"",
1628  pg_enc2name_tbl[expected_src_encoding].name,
1629  pg_enc2name_tbl[src_encoding].name);
1630  if (!PG_VALID_ENCODING(dest_encoding))
1631  elog(ERROR, "invalid destination encoding ID: %d", dest_encoding);
1632  if (dest_encoding != expected_dest_encoding && expected_dest_encoding >= 0)
1633  elog(ERROR, "expected destination encoding \"%s\", but got \"%s\"",
1634  pg_enc2name_tbl[expected_dest_encoding].name,
1635  pg_enc2name_tbl[dest_encoding].name);
1636  if (len < 0)
1637  elog(ERROR, "encoding conversion length must not be negative");
1638 }
#define ERROR
Definition: elog.h:46
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:301
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
const char * name
Definition: encode.c:561
#define elog(elevel,...)
Definition: elog.h:232

◆ cliplen()

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

Definition at line 1093 of file mbutils.c.

References Min.

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

1094 {
1095  int l = 0;
1096 
1097  len = Min(len, limit);
1098  while (l < len && str[l])
1099  l++;
1100  return l;
1101 }
#define Min(x, y)
Definition: c.h:986

◆ GetDatabaseEncoding()

int GetDatabaseEncoding ( void  )

Definition at line 1210 of file mbutils.c.

References pg_enc2name::encoding.

Referenced by ascii(), BeginCopyFrom(), BeginCopyTo(), char2wchar(), chr(), CollationCreate(), CollationGetCollid(), compareStrings(), convert_from_utf8(), convert_to_utf8(), CopyConversionError(), CopyConvertBuf(), cstr2sv(), dblink_connect(), dblink_get_conn(), DefineCollation(), Generic_Text_IC_like(), GenericMatchText(), get_collation_actual_version(), get_collation_oid(), get_json_object_as_hash(), InitializeClientEncoding(), IsThereCollationInNamespace(), json_recv(), jsonb_from_cstring(), locate_stem_module(), LogicalOutputWrite(), makeJsonLexContext(), p_isspecial(), ParallelWorkerMain(), pg_database_encoding_character_incrementer(), pg_database_encoding_max_length(), pg_generic_charinc(), pg_perm_setlocale(), pg_set_regex_collation(), pg_unicode_to_server(), pg_verifymbstr(), pgss_store(), PLyUnicode_Bytes(), populate_array_json(), PrepareClientEncoding(), read_extension_script_file(), SetClientEncoding(), SetMessageEncoding(), SPI_sql_row_to_xmlelement(), surrogate_pair_to_codepoint(), sv2cstr(), text_position_setup(), to_ascii_default(), type_maximum_size(), unicode_norm_form_from_string(), varstr_abbrev_convert(), varstr_cmp(), varstr_sortsupport(), varstrfastcmp_locale(), wchar2char(), xml_in(), xml_is_document(), xmlparse(), and xpath_exists().

1211 {
1212  return DatabaseEncoding->encoding;
1213 }
pg_enc encoding
Definition: pg_wchar.h:356
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81

◆ getdatabaseencoding()

Datum getdatabaseencoding ( PG_FUNCTION_ARGS  )

Definition at line 1222 of file mbutils.c.

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

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

◆ GetDatabaseEncodingName()

const char* GetDatabaseEncodingName ( void  )

◆ GetMessageEncoding()

int GetMessageEncoding ( void  )

Definition at line 1257 of file mbutils.c.

References pg_enc2name::encoding.

Referenced by DebugFileOpen(), report_untranslatable_char(), SetMessageEncoding(), and surrogate_pair_to_codepoint().

1258 {
1259  return MessageEncoding->encoding;
1260 }
pg_enc encoding
Definition: pg_wchar.h:356
static const pg_enc2name * MessageEncoding
Definition: mbutils.c:82

◆ InitializeClientEncoding()

void InitializeClientEncoding ( void  )

Definition at line 281 of file mbutils.c.

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(), and TopMemoryContext.

Referenced by InitPostgres(), and surrogate_pair_to_codepoint().

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  */
295  ereport(FATAL,
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 }
Definition: fmgr.h:56
static FmgrInfo * Utf8ToServerConvProc
Definition: mbutils.c:75
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
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define FATAL
Definition: elog.h:49
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
int SetClientEncoding(int encoding)
Definition: mbutils.c:208
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
MemoryContext TopMemoryContext
Definition: mcxt.c:48
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1216
Oid FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
Definition: namespace.c:3755
bool IsTransactionState(void)
Definition: xact.c:371
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:909
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863

◆ length_in_encoding()

Datum length_in_encoding ( PG_FUNCTION_ARGS  )

Definition at line 615 of file mbutils.c.

References ereport, errcode(), errmsg(), ERROR, 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.

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)
625  ereport(ERROR,
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 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:550
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define NameStr(name)
Definition: c.h:681
Definition: c.h:621
int pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
Definition: mbutils.c:1546
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ 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.

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

Referenced by pg_any_to_server(), and pg_server_to_any().

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  */
811  if ((Size) len >= (MaxAllocHugeSize / (Size) MAX_CONVERSION_GROWTH))
812  ereport(ERROR,
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 *)
820  (Size) len * MAX_CONVERSION_GROWTH + 1);
821 
822  FunctionCall6(flinfo,
823  Int32GetDatum(src_encoding),
824  Int32GetDatum(dest_encoding),
825  CStringGetDatum(src),
826  CStringGetDatum(result),
827  Int32GetDatum(len),
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)
839  ereport(ERROR,
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 }
Definition: fmgr.h:56
static FmgrInfo * ToServerConvProc
Definition: mbutils.c:67
int errcode(int sqlerrcode)
Definition: elog.c:698
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:80
#define MaxAllocHugeSize
Definition: memutils.h:44
#define ERROR
Definition: elog.h:46
void * MemoryContextAllocHuge(MemoryContext context, Size size)
Definition: mcxt.c:1218
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define CStringGetDatum(X)
Definition: postgres.h:622
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define MaxAllocSize
Definition: memutils.h:40
#define unconstify(underlying_type, expr)
Definition: c.h:1243
#define BoolGetDatum(X)
Definition: postgres.h:446
pg_enc encoding
Definition: pg_wchar.h:356
#define ereport(elevel,...)
Definition: elog.h:157
size_t Size
Definition: c.h:540
#define FunctionCall6(flinfo, arg1, arg2, arg3, arg4, arg5, arg6)
Definition: fmgr.h:654
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:316
#define Int32GetDatum(X)
Definition: postgres.h:523
static FmgrInfo * ToClientConvProc
Definition: mbutils.c:68
int errmsg(const char *fmt,...)
Definition: elog.c:909
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81

◆ pg_any_to_server()

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

Definition at line 676 of file mbutils.c.

References pg_enc2name::encoding, ereport, errcode(), errmsg(), ERROR, i, IS_HIGHBIT_SET, 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(), surrogate_pair_to_codepoint(), tsearch_readline(), utf_u2e(), X509_NAME_to_cstring(), and xml_recv().

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]))
711  ereport(ERROR,
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 */
723  return perform_default_encoding_conversion(s, len, true);
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 }
static char * perform_default_encoding_conversion(const char *src, int len, bool is_client_to_server)
Definition: mbutils.c:783
int errcode(int sqlerrcode)
Definition: elog.c:698
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:80
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:1515
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1156
#define ERROR
Definition: elog.h:46
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
#define unconstify(underlying_type, expr)
Definition: c.h:1243
pg_enc encoding
Definition: pg_wchar.h:356
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:295
int32 encoding
Definition: pg_database.h:41
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:909
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81
int i

◆ PG_char_to_encoding()

Datum PG_char_to_encoding ( PG_FUNCTION_ARGS  )

Definition at line 1234 of file mbutils.c.

References NameStr, pg_char_to_encoding(), PG_GETARG_NAME, and PG_RETURN_INT32.

1235 {
1236  Name s = PG_GETARG_NAME(0);
1237 
1239 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:550
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
Definition: c.h:675
#define NameStr(name)
Definition: c.h:681
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ pg_client_encoding()

Datum pg_client_encoding ( PG_FUNCTION_ARGS  )

Definition at line 1228 of file mbutils.c.

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

1229 {
1231 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:80
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
#define CStringGetDatum(X)
Definition: postgres.h:622
const char * name
Definition: pg_wchar.h:355

◆ pg_client_to_server()

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

Definition at line 660 of file mbutils.c.

References pg_enc2name::encoding, and pg_any_to_server().

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

661 {
662  return pg_any_to_server(s, len, ClientEncoding->encoding);
663 }
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:80
pg_enc encoding
Definition: pg_wchar.h:356
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:676

◆ pg_convert()

Datum pg_convert ( PG_FUNCTION_ARGS  )

Definition at line 553 of file mbutils.c.

References ereport, errcode(), errmsg(), ERROR, 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().

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)
566  ereport(ERROR,
567  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
568  errmsg("invalid source encoding name \"%s\"",
569  src_encoding_name)));
570  if (dest_encoding < 0)
571  ereport(ERROR,
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  /* update len if conversion actually happened */
588  if (dest_str != src_str)
589  len = strlen(dest_str);
590 
591  /*
592  * build bytea data type structure.
593  */
594  retval = (bytea *) palloc(len + VARHDRSZ);
595  SET_VARSIZE(retval, len + VARHDRSZ);
596  memcpy(VARDATA(retval), dest_str, len);
597 
598  if (dest_str != src_str)
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 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:550
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARHDRSZ
Definition: c.h:627
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
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:1515
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
#define unconstify(underlying_type, expr)
Definition: c.h:1243
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define NameStr(name)
Definition: c.h:681
Definition: c.h:621
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ pg_convert_from()

Datum pg_convert_from ( PG_FUNCTION_ARGS  )

Definition at line 526 of file mbutils.c.

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

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 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
#define CStringGetDatum(X)
Definition: postgres.h:622
const char * name
Definition: pg_wchar.h:355
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
Datum pg_convert(PG_FUNCTION_ARGS)
Definition: mbutils.c:553
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81

◆ pg_convert_to()

Datum pg_convert_to ( PG_FUNCTION_ARGS  )

Definition at line 501 of file mbutils.c.

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

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 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
#define CStringGetDatum(X)
Definition: postgres.h:622
const char * name
Definition: pg_wchar.h:355
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:630
uintptr_t Datum
Definition: postgres.h:411
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
Datum pg_convert(PG_FUNCTION_ARGS)
Definition: mbutils.c:553
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81

◆ pg_database_encoding_character_incrementer()

mbcharacter_incrementer pg_database_encoding_character_incrementer ( void  )

Definition at line 1472 of file mbutils.c.

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

Referenced by make_greater_string(), and surrogate_pair_to_codepoint().

1473 {
1474  /*
1475  * Eventually it might be best to add a field to pg_wchar_table[], but for
1476  * now we just use a switch.
1477  */
1478  switch (GetDatabaseEncoding())
1479  {
1480  case PG_UTF8:
1481  return pg_utf8_increment;
1482 
1483  case PG_EUC_JP:
1484  return pg_eucjp_increment;
1485 
1486  default:
1487  return pg_generic_charinc;
1488  }
1489 }
static bool pg_eucjp_increment(unsigned char *charptr, int length)
Definition: mbutils.c:1386
static bool pg_utf8_increment(unsigned char *charptr, int length)
Definition: mbutils.c:1308
static bool pg_generic_charinc(unsigned char *charptr, int len)
Definition: mbutils.c:1274
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210

◆ 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.

References BoolGetDatum, CStringGetDatum, CurrentMemoryContext, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, FindDefaultConversionProc(), Int32GetDatum, IsTransactionState(), 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(), pg_server_to_any(), report_untranslatable_char(), surrogate_pair_to_codepoint(), and xml_is_document().

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))
383  ereport(ERROR,
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  */
398  if ((Size) len >= (MaxAllocHugeSize / (Size) MAX_CONVERSION_GROWTH))
399  ereport(ERROR,
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 *)
407  (Size) len * MAX_CONVERSION_GROWTH + 1);
408 
409  (void) OidFunctionCall6(proc,
410  Int32GetDatum(src_encoding),
411  Int32GetDatum(dest_encoding),
412  CStringGetDatum(src),
413  CStringGetDatum(result),
414  Int32GetDatum(len),
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)
427  ereport(ERROR,
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:674
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define MaxAllocHugeSize
Definition: memutils.h:44
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: mbutils.c:1515
#define ERROR
Definition: elog.h:46
void * MemoryContextAllocHuge(MemoryContext context, Size size)
Definition: mcxt.c:1218
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define CStringGetDatum(X)
Definition: postgres.h:622
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define MaxAllocSize
Definition: memutils.h:40
#define BoolGetDatum(X)
Definition: postgres.h:446
#define ereport(elevel,...)
Definition: elog.h:157
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
size_t Size
Definition: c.h:540
Oid FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
Definition: namespace.c:3755
bool IsTransactionState(void)
Definition: xact.c:371
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:316
#define Int32GetDatum(X)
Definition: postgres.h:523
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ 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.

References BoolGetDatum, CStringGetDatum, DatumGetInt32, Int32GetDatum, MAX_CONVERSION_GROWTH, and OidFunctionCall6.

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

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(src),
490  Int32GetDatum(srclen),
491  BoolGetDatum(noError));
492  return DatumGetInt32(result);
493 }
#define OidFunctionCall6(functionId, arg1, arg2, arg3, arg4, arg5, arg6)
Definition: fmgr.h:674
#define DatumGetInt32(X)
Definition: postgres.h:516
#define CStringGetDatum(X)
Definition: postgres.h:622
uintptr_t Datum
Definition: postgres.h:411
#define BoolGetDatum(X)
Definition: postgres.h:446
size_t Size
Definition: c.h:540
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:316
#define Int32GetDatum(X)
Definition: postgres.h:523

◆ pg_dsplen()

int pg_dsplen ( const char *  mbstr)

Definition at line 973 of file mbutils.c.

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

Referenced by p_isspecial(), and surrogate_pair_to_codepoint().

974 {
975  return pg_wchar_table[DatabaseEncoding->encoding].dsplen((const unsigned char *) mbstr);
976 }
mbdisplaylen_converter dsplen
Definition: pg_wchar.h:403
pg_enc encoding
Definition: pg_wchar.h:356
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81

◆ pg_encoding_max_length_sql()

Datum pg_encoding_max_length_sql ( PG_FUNCTION_ARGS  )

Definition at line 644 of file mbutils.c.

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

645 {
646  int encoding = PG_GETARG_INT32(0);
647 
648  if (PG_VALID_ENCODING(encoding))
649  PG_RETURN_INT32(pg_wchar_table[encoding].maxmblen);
650  else
651  PG_RETURN_NULL();
652 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:301
int32 encoding
Definition: pg_database.h:41
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_encoding_mb2wchar_with_len()

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

Definition at line 936 of file mbutils.c.

References encoding, pg_wchar_tbl::mb2wchar_with_len, and pg_wchar_table.

Referenced by surrogate_pair_to_codepoint(), and xml_is_document().

938 {
939  return pg_wchar_table[encoding].mb2wchar_with_len((const unsigned char *) from, to, len);
940 }
int32 encoding
Definition: pg_database.h:41
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860
mb2wchar_with_len_converter mb2wchar_with_len
Definition: pg_wchar.h:398

◆ pg_encoding_mbcliplen()

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

Definition at line 1036 of file mbutils.c.

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

Referenced by pg_mbcliplen(), and surrogate_pair_to_codepoint().

1038 {
1039  mblen_converter mblen_fn;
1040  int clen = 0;
1041  int l;
1042 
1043  /* optimization for single byte encoding */
1044  if (pg_encoding_max_length(encoding) == 1)
1045  return cliplen(mbstr, len, limit);
1046 
1047  mblen_fn = pg_wchar_table[encoding].mblen;
1048 
1049  while (len > 0 && *mbstr)
1050  {
1051  l = (*mblen_fn) ((const unsigned char *) mbstr);
1052  if ((clen + l) > limit)
1053  break;
1054  clen += l;
1055  if (clen == limit)
1056  break;
1057  len -= l;
1058  mbstr += l;
1059  }
1060  return clen;
1061 }
int(* mblen_converter)(const unsigned char *mbstr)
Definition: pg_wchar.h:386
int pg_encoding_max_length(int encoding)
Definition: wchar.c:1972
static int cliplen(const char *str, int len, int limit)
Definition: mbutils.c:1093
int32 encoding
Definition: pg_database.h:41
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860
mblen_converter mblen
Definition: pg_wchar.h:402

◆ PG_encoding_to_char()

Datum PG_encoding_to_char ( PG_FUNCTION_ARGS  )

Definition at line 1242 of file mbutils.c.

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

1243 {
1245  const char *encoding_name = pg_encoding_to_char(encoding);
1246 
1247  return DirectFunctionCall1(namein, CStringGetDatum(encoding_name));
1248 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
signed int int32
Definition: c.h:429
#define CStringGetDatum(X)
Definition: postgres.h:622
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
int32 encoding
Definition: pg_database.h:41

◆ pg_encoding_wchar2mb_with_len()

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

Definition at line 958 of file mbutils.c.

References encoding, pg_wchar_table, and pg_wchar_tbl::wchar2mb_with_len.

Referenced by surrogate_pair_to_codepoint().

960 {
961  return pg_wchar_table[encoding].wchar2mb_with_len(from, (unsigned char *) to, len);
962 }
wchar2mb_with_len_converter wchar2mb_with_len
Definition: pg_wchar.h:400
int32 encoding
Definition: pg_database.h:41
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860

◆ pg_eucjp_increment()

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

Definition at line 1386 of file mbutils.c.

References i, IS_HIGHBIT_SET, SS2, and SS3.

Referenced by pg_database_encoding_character_incrementer().

1387 {
1388  unsigned char c1,
1389  c2;
1390  int i;
1391 
1392  c1 = *charptr;
1393 
1394  switch (c1)
1395  {
1396  case SS2: /* JIS X 0201 */
1397  if (length != 2)
1398  return false;
1399 
1400  c2 = charptr[1];
1401 
1402  if (c2 >= 0xdf)
1403  charptr[0] = charptr[1] = 0xa1;
1404  else if (c2 < 0xa1)
1405  charptr[1] = 0xa1;
1406  else
1407  charptr[1]++;
1408  break;
1409 
1410  case SS3: /* JIS X 0212 */
1411  if (length != 3)
1412  return false;
1413 
1414  for (i = 2; i > 0; i--)
1415  {
1416  c2 = charptr[i];
1417  if (c2 < 0xa1)
1418  {
1419  charptr[i] = 0xa1;
1420  return true;
1421  }
1422  else if (c2 < 0xfe)
1423  {
1424  charptr[i]++;
1425  return true;
1426  }
1427  }
1428 
1429  /* Out of 3-byte code region */
1430  return false;
1431 
1432  default:
1433  if (IS_HIGHBIT_SET(c1)) /* JIS X 0208? */
1434  {
1435  if (length != 2)
1436  return false;
1437 
1438  for (i = 1; i >= 0; i--)
1439  {
1440  c2 = charptr[i];
1441  if (c2 < 0xa1)
1442  {
1443  charptr[i] = 0xa1;
1444  return true;
1445  }
1446  else if (c2 < 0xfe)
1447  {
1448  charptr[i]++;
1449  return true;
1450  }
1451  }
1452 
1453  /* Out of 2 byte code region */
1454  return false;
1455  }
1456  else
1457  { /* ASCII, single byte */
1458  if (c1 > 0x7e)
1459  return false;
1460  (*charptr)++;
1461  }
1462  break;
1463  }
1464 
1465  return true;
1466 }
#define SS3
Definition: pg_wchar.h:36
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1156
int i
#define SS2
Definition: pg_wchar.h:35

◆ pg_generic_charinc()

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

Definition at line 1274 of file mbutils.c.

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

Referenced by pg_database_encoding_character_incrementer().

1275 {
1276  unsigned char *lastbyte = charptr + len - 1;
1277  mbchar_verifier mbverify;
1278 
1279  /* We can just invoke the character verifier directly. */
1281 
1282  while (*lastbyte < (unsigned char) 255)
1283  {
1284  (*lastbyte)++;
1285  if ((*mbverify) (charptr, len) == len)
1286  return true;
1287  }
1288 
1289  return false;
1290 }
int(* mbchar_verifier)(const unsigned char *mbstr, int len)
Definition: pg_wchar.h:392
mbchar_verifier mbverifychar
Definition: pg_wchar.h:404
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860

◆ pg_get_client_encoding()

int pg_get_client_encoding ( void  )

Definition at line 336 of file mbutils.c.

References pg_enc2name::encoding.

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

337 {
338  return ClientEncoding->encoding;
339 }
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:80
pg_enc encoding
Definition: pg_wchar.h:356

◆ pg_get_client_encoding_name()

const char* pg_get_client_encoding_name ( void  )

Definition at line 345 of file mbutils.c.

References pg_enc2name::name.

Referenced by surrogate_pair_to_codepoint().

346 {
347  return ClientEncoding->name;
348 }
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:80
const char * name
Definition: pg_wchar.h:355

◆ pg_mb2wchar()

int pg_mb2wchar ( const char *  from,
pg_wchar to 
)

Definition at line 922 of file mbutils.c.

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

Referenced by surrogate_pair_to_codepoint().

923 {
924  return pg_wchar_table[DatabaseEncoding->encoding].mb2wchar_with_len((const unsigned char *) from, to, strlen(from));
925 }
pg_enc encoding
Definition: pg_wchar.h:356
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81
mb2wchar_with_len_converter mb2wchar_with_len
Definition: pg_wchar.h:398

◆ pg_mb2wchar_with_len()

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

Definition at line 929 of file mbutils.c.

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

Referenced by check_ident_usermap(), CheckAffix(), NIAddAffix(), parse_ident_line(), RE_compile(), RE_compile_and_cache(), RE_execute(), replace_text_regexp(), setup_regexp_matches(), setup_test_matches(), surrogate_pair_to_codepoint(), test_re_compile(), and TParserInit().

930 {
931  return pg_wchar_table[DatabaseEncoding->encoding].mb2wchar_with_len((const unsigned char *) from, to, len);
932 }
pg_enc encoding
Definition: pg_wchar.h:356
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81
mb2wchar_with_len_converter mb2wchar_with_len
Definition: pg_wchar.h:398

◆ pg_mbcharcliplen()

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

Definition at line 1068 of file mbutils.c.

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

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

1069 {
1070  int clen = 0;
1071  int nch = 0;
1072  int l;
1073 
1074  /* optimization for single byte encoding */
1076  return cliplen(mbstr, len, limit);
1077 
1078  while (len > 0 && *mbstr)
1079  {
1080  l = pg_mblen(mbstr);
1081  nch++;
1082  if (nch > limit)
1083  break;
1084  clen += l;
1085  len -= l;
1086  mbstr += l;
1087  }
1088  return clen;
1089 }
static int cliplen(const char *str, int len, int limit)
Definition: mbutils.c:1093
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1495

◆ pg_mbcliplen()

◆ pg_mblen()

int pg_mblen ( const char *  mbstr)

Definition at line 966 of file mbutils.c.

References 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_hex(), get_modifiers(), get_nextfield(), get_val(), get_wildcard_part(), getlexeme(), getNextFlagFromString(), gettoken_query(), gettoken_query_standard(), gettoken_query_websearch(), gettoken_tsvector(), 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_hstore(), parse_lquery(), parse_ltree(), parse_or_operator(), parse_re_flags(), parse_test_flags(), pg_base64_decode(), pg_mbcharcliplen(), pg_mbstrlen(), pg_mbstrlen_with_len(), readstoplist(), report_json_context(), rpad(), RS_compile(), RS_execute(), RS_isRegis(), similar_escape_internal(), split_text(), surrogate_pair_to_codepoint(), t_isalpha(), t_isdigit(), t_isprint(), t_isspace(), text_format(), text_position_get_match_pos(), text_position_next(), text_reverse(), text_substring(), text_to_bits(), textregexreplace(), thesaurusRead(), TParserGet(), translate(), ts_stat_sql(), tsvectorout(), unaccent_lexize(), varbit_in(), varstr_levenshtein(), wchareq(), and xml_is_document().

967 {
968  return pg_wchar_table[DatabaseEncoding->encoding].mblen((const unsigned char *) mbstr);
969 }
pg_enc encoding
Definition: pg_wchar.h:356
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81
mblen_converter mblen
Definition: pg_wchar.h:402

◆ pg_mbstrlen()

int pg_mbstrlen ( const char *  mbstr)

Definition at line 980 of file mbutils.c.

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by cache_locale_time(), NUM_processor(), surrogate_pair_to_codepoint(), and text_format_append_string().

981 {
982  int len = 0;
983 
984  /* optimization for single byte encoding */
986  return strlen(mbstr);
987 
988  while (*mbstr)
989  {
990  mbstr += pg_mblen(mbstr);
991  len++;
992  }
993  return len;
994 }
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1495

◆ pg_mbstrlen_with_len()

int pg_mbstrlen_with_len ( const char *  mbstr,
int  limit 
)

Definition at line 1000 of file mbutils.c.

References 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(), similar_escape_internal(), surrogate_pair_to_codepoint(), text_left(), text_length(), text_right(), text_substring(), unicode_is_normalized(), unicode_normalize_func(), and varstr_levenshtein().

1001 {
1002  int len = 0;
1003 
1004  /* optimization for single byte encoding */
1006  return limit;
1007 
1008  while (limit > 0 && *mbstr)
1009  {
1010  int l = pg_mblen(mbstr);
1011 
1012  limit -= l;
1013  mbstr += l;
1014  len++;
1015  }
1016  return len;
1017 }
int pg_mblen(const char *mbstr)
Definition: mbutils.c:966
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1495

◆ pg_server_to_any()

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

Definition at line 749 of file mbutils.c.

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

Referenced by compareStrings(), CopyAttributeOutCSV(), CopyAttributeOutText(), DoCopyTo(), dsnowball_lexize(), hv_fetch_string(), hv_store_string(), pg_server_to_client(), PLyUnicode_AsString(), surrogate_pair_to_codepoint(), utf_e2u(), and xml_is_document().

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 */
767  return perform_default_encoding_conversion(s, len, false);
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 }
static char * perform_default_encoding_conversion(const char *src, int len, bool is_client_to_server)
Definition: mbutils.c:783
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:80
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:1515
#define unconstify(underlying_type, expr)
Definition: c.h:1243
pg_enc encoding
Definition: pg_wchar.h:356
int32 encoding
Definition: pg_database.h:41
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81

◆ pg_server_to_client()

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

Definition at line 738 of file mbutils.c.

References pg_enc2name::encoding, and pg_server_to_any().

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

739 {
740  return pg_server_to_any(s, len, ClientEncoding->encoding);
741 }
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:80
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:749
pg_enc encoding
Definition: pg_wchar.h:356

◆ pg_unicode_to_server()

void pg_unicode_to_server ( pg_wchar  c,
unsigned char *  s 
)

Definition at line 864 of file mbutils.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(), and unicode_to_utf8().

Referenced by json_lex_string(), map_xml_name_to_sql_identifier(), str_udeescape(), surrogate_pair_to_codepoint(), and unistr().

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  */
875  ereport(ERROR,
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)
898  ereport(ERROR,
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(c_as_utf8),
914  CStringGetDatum(s),
915  Int32GetDatum(c_as_utf8_len),
916  BoolGetDatum(false));
917 }
static FmgrInfo * Utf8ToServerConvProc
Definition: mbutils.c:75
unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
Definition: wchar.c:483
static bool is_valid_unicode_codepoint(pg_wchar c)
Definition: pg_wchar.h:539
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
char * c
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
#define CStringGetDatum(X)
Definition: postgres.h:622
#define MAX_MULTIBYTE_CHAR_LEN
Definition: pg_wchar.h:30
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
#define BoolGetDatum(X)
Definition: postgres.h:446
#define ereport(elevel,...)
Definition: elog.h:157
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1216
#define FunctionCall6(flinfo, arg1, arg2, arg3, arg4, arg5, arg6)
Definition: fmgr.h:654
const char * name
Definition: encode.c:561
int pg_utf_mblen(const unsigned char *s)
Definition: wchar.c:549
#define Int32GetDatum(X)
Definition: postgres.h:523
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pg_utf8_increment()

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

Definition at line 1308 of file mbutils.c.

Referenced by pg_database_encoding_character_incrementer().

1309 {
1310  unsigned char a;
1311  unsigned char limit;
1312 
1313  switch (length)
1314  {
1315  default:
1316  /* reject lengths 5 and 6 for now */
1317  return false;
1318  case 4:
1319  a = charptr[3];
1320  if (a < 0xBF)
1321  {
1322  charptr[3]++;
1323  break;
1324  }
1325  /* FALL THRU */
1326  case 3:
1327  a = charptr[2];
1328  if (a < 0xBF)
1329  {
1330  charptr[2]++;
1331  break;
1332  }
1333  /* FALL THRU */
1334  case 2:
1335  a = charptr[1];
1336  switch (*charptr)
1337  {
1338  case 0xED:
1339  limit = 0x9F;
1340  break;
1341  case 0xF4:
1342  limit = 0x8F;
1343  break;
1344  default:
1345  limit = 0xBF;
1346  break;
1347  }
1348  if (a < limit)
1349  {
1350  charptr[1]++;
1351  break;
1352  }
1353  /* FALL THRU */
1354  case 1:
1355  a = *charptr;
1356  if (a == 0x7F || a == 0xDF || a == 0xEF || a == 0xF4)
1357  return false;
1358  charptr[0]++;
1359  break;
1360  }
1361 
1362  return true;
1363 }

◆ pg_verify_mbstr()

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

Definition at line 1515 of file mbutils.c.

References Assert, encoding, 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(), read_extension_script_file(), and surrogate_pair_to_codepoint().

1516 {
1517  int oklen;
1518 
1520 
1521  oklen = pg_wchar_table[encoding].mbverifystr((const unsigned char *) mbstr, len);
1522  if (oklen != len)
1523  {
1524  if (noError)
1525  return false;
1526  report_invalid_encoding(encoding, mbstr + oklen, len - oklen);
1527  }
1528  return true;
1529 }
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: mbutils.c:1647
mbstr_verifier mbverifystr
Definition: pg_wchar.h:405
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:301
#define Assert(condition)
Definition: c.h:804
int32 encoding
Definition: pg_database.h:41
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860

◆ pg_verify_mbstr_len()

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

Definition at line 1546 of file mbutils.c.

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

Referenced by length_in_encoding(), and surrogate_pair_to_codepoint().

1547 {
1548  mbchar_verifier mbverifychar;
1549  int mb_len;
1550 
1552 
1553  /*
1554  * In single-byte encodings, we need only reject nulls (\0).
1555  */
1556  if (pg_encoding_max_length(encoding) <= 1)
1557  {
1558  const char *nullpos = memchr(mbstr, 0, len);
1559 
1560  if (nullpos == NULL)
1561  return len;
1562  if (noError)
1563  return -1;
1564  report_invalid_encoding(encoding, nullpos, 1);
1565  }
1566 
1567  /* fetch function pointer just once */
1568  mbverifychar = pg_wchar_table[encoding].mbverifychar;
1569 
1570  mb_len = 0;
1571 
1572  while (len > 0)
1573  {
1574  int l;
1575 
1576  /* fast path for ASCII-subset characters */
1577  if (!IS_HIGHBIT_SET(*mbstr))
1578  {
1579  if (*mbstr != '\0')
1580  {
1581  mb_len++;
1582  mbstr++;
1583  len--;
1584  continue;
1585  }
1586  if (noError)
1587  return -1;
1588  report_invalid_encoding(encoding, mbstr, len);
1589  }
1590 
1591  l = (*mbverifychar) ((const unsigned char *) mbstr, len);
1592 
1593  if (l < 0)
1594  {
1595  if (noError)
1596  return -1;
1597  report_invalid_encoding(encoding, mbstr, len);
1598  }
1599 
1600  mbstr += l;
1601  len -= l;
1602  mb_len++;
1603  }
1604  return mb_len;
1605 }
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: mbutils.c:1647
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1156
int(* mbchar_verifier)(const unsigned char *mbstr, int len)
Definition: pg_wchar.h:392
int pg_encoding_max_length(int encoding)
Definition: wchar.c:1972
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:301
mbchar_verifier mbverifychar
Definition: pg_wchar.h:404
#define Assert(condition)
Definition: c.h:804
int32 encoding
Definition: pg_database.h:41
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860

◆ pg_verifymbstr()

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

Definition at line 1505 of file mbutils.c.

References GetDatabaseEncoding(), and pg_verify_mbstr().

Referenced by char2wchar(), CopyReadAttributesText(), plperl_spi_exec(), plperl_spi_prepare(), plperl_spi_query(), PLy_cursor_query(), PLy_output(), PLy_spi_execute_query(), PLy_spi_prepare(), PLyObject_AsString(), read_text_file(), spg_text_leaf_consistent(), and surrogate_pair_to_codepoint().

1506 {
1507  return pg_verify_mbstr(GetDatabaseEncoding(), mbstr, len, noError);
1508 }
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: mbutils.c:1515
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210

◆ pg_wchar2mb()

int pg_wchar2mb ( const pg_wchar from,
char *  to 
)

Definition at line 944 of file mbutils.c.

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

Referenced by surrogate_pair_to_codepoint().

945 {
946  return pg_wchar_table[DatabaseEncoding->encoding].wchar2mb_with_len(from, (unsigned char *) to, pg_wchar_strlen(from));
947 }
size_t pg_wchar_strlen(const pg_wchar *str)
Definition: wstrncmp.c:70
wchar2mb_with_len_converter wchar2mb_with_len
Definition: pg_wchar.h:400
pg_enc encoding
Definition: pg_wchar.h:356
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81

◆ pg_wchar2mb_with_len()

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

Definition at line 951 of file mbutils.c.

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

Referenced by build_regexp_match_result(), build_regexp_split_result(), build_test_match_result(), convertPgWchar(), regexp_fixed_prefix(), and surrogate_pair_to_codepoint().

952 {
953  return pg_wchar_table[DatabaseEncoding->encoding].wchar2mb_with_len(from, (unsigned char *) to, len);
954 }
wchar2mb_with_len_converter wchar2mb_with_len
Definition: pg_wchar.h:400
pg_enc encoding
Definition: pg_wchar.h:356
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1860
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81

◆ PrepareClientEncoding()

int PrepareClientEncoding ( int  encoding)

Definition at line 110 of file mbutils.c.

References backend_startup_complete, ConvProcInfo::c_encoding, 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(), InitializeClientEncoding(), and surrogate_pair_to_codepoint().

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 }
FmgrInfo to_server_info
Definition: mbutils.c:57
FmgrInfo to_client_info
Definition: mbutils.c:58
static bool backend_startup_complete
Definition: mbutils.c:90
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
int s_encoding
Definition: mbutils.c:55
static List * ConvProcList
Definition: mbutils.c:61
#define PG_VALID_FE_ENCODING(_enc)
Definition: pg_wchar.h:305
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
MemoryContext TopMemoryContext
Definition: mcxt.c:48
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
List * lcons(void *datum, List *list)
Definition: list.c:468
#define lfirst(lc)
Definition: pg_list.h:169
Oid FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
Definition: namespace.c:3755
bool IsTransactionState(void)
Definition: xact.c:371
int32 encoding
Definition: pg_database.h:41
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
int c_encoding
Definition: mbutils.c:56

◆ report_invalid_encoding()

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

Definition at line 1647 of file mbutils.c.

References buf, ereport, errcode(), errmsg(), ERROR, 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(), surrogate_pair_to_codepoint(), test_enc_conversion(), utf8_to_iso8859_1(), and UtfToLocal().

1648 {
1649  int l = pg_encoding_mblen(encoding, mbstr);
1650  char buf[8 * 5 + 1];
1651  char *p = buf;
1652  int j,
1653  jlimit;
1654 
1655  jlimit = Min(l, len);
1656  jlimit = Min(jlimit, 8); /* prevent buffer overrun */
1657 
1658  for (j = 0; j < jlimit; j++)
1659  {
1660  p += sprintf(p, "0x%02x", (unsigned char) mbstr[j]);
1661  if (j < jlimit - 1)
1662  p += sprintf(p, " ");
1663  }
1664 
1665  ereport(ERROR,
1666  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
1667  errmsg("invalid byte sequence for encoding \"%s\": %s",
1669  buf)));
1670 }
#define Min(x, y)
Definition: c.h:986
int errcode(int sqlerrcode)
Definition: elog.c:698
#define sprintf
Definition: port.h:219
#define ERROR
Definition: elog.h:46
static char * buf
Definition: pg_test_fsync.c:68
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:1914
#define ereport(elevel,...)
Definition: elog.h:157
int32 encoding
Definition: pg_database.h:41
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ report_untranslatable_char()

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

Definition at line 1679 of file mbutils.c.

References buf, ereport, errcode(), errmsg(), ERROR, GetMessageEncoding(), IsTransactionState(), Min, name, palloc(), pfree(), pg_do_encoding_conversion(), pg_enc2name_tbl, pg_encoding_mblen(), PG_SQL_ASCII, PG_UTF8, sprintf, and generate_unaccent_rules::str.

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(), surrogate_pair_to_codepoint(), utf8_to_iso8859_1(), and UtfToLocal().

1681 {
1682  int l = pg_encoding_mblen(src_encoding, mbstr);
1683  char buf[8 * 5 + 1];
1684  char *p = buf;
1685  int j,
1686  jlimit;
1687 
1688  jlimit = Min(l, len);
1689  jlimit = Min(jlimit, 8); /* prevent buffer overrun */
1690 
1691  for (j = 0; j < jlimit; j++)
1692  {
1693  p += sprintf(p, "0x%02x", (unsigned char) mbstr[j]);
1694  if (j < jlimit - 1)
1695  p += sprintf(p, " ");
1696  }
1697 
1698  ereport(ERROR,
1699  (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
1700  errmsg("character with byte sequence %s in encoding \"%s\" has no equivalent in encoding \"%s\"",
1701  buf,
1702  pg_enc2name_tbl[src_encoding].name,
1703  pg_enc2name_tbl[dest_encoding].name)));
1704 }
#define Min(x, y)
Definition: c.h:986
int errcode(int sqlerrcode)
Definition: elog.c:698
#define sprintf
Definition: port.h:219
#define ERROR
Definition: elog.h:46
static char * buf
Definition: pg_test_fsync.c:68
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:1914
#define ereport(elevel,...)
Definition: elog.h:157
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ SetClientEncoding()

int SetClientEncoding ( int  encoding)

Definition at line 208 of file mbutils.c.

References backend_startup_complete, ConvProcInfo::c_encoding, 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, and ConvProcInfo::to_server_info.

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

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 }
FmgrInfo to_server_info
Definition: mbutils.c:57
FmgrInfo to_client_info
Definition: mbutils.c:58
static bool backend_startup_complete
Definition: mbutils.c:90
static int pending_client_encoding
Definition: mbutils.c:91
static FmgrInfo * ToServerConvProc
Definition: mbutils.c:67
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:80
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:369
int s_encoding
Definition: mbutils.c:55
void pfree(void *pointer)
Definition: mcxt.c:1169
static List * ConvProcList
Definition: mbutils.c:61
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
#define PG_VALID_FE_ENCODING(_enc)
Definition: pg_wchar.h:305
int GetDatabaseEncoding(void)
Definition: mbutils.c:1210
#define lfirst(lc)
Definition: pg_list.h:169
int32 encoding
Definition: pg_database.h:41
static FmgrInfo * ToClientConvProc
Definition: mbutils.c:68
int c_encoding
Definition: mbutils.c:56

◆ SetDatabaseEncoding()

void SetDatabaseEncoding ( int  encoding)

Definition at line 1104 of file mbutils.c.

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

Referenced by CheckMyDatabase(), and surrogate_pair_to_codepoint().

1105 {
1107  elog(ERROR, "invalid database encoding: %d", encoding);
1108 
1111 }
#define ERROR
Definition: elog.h:46
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
pg_enc encoding
Definition: pg_wchar.h:356
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:295
#define Assert(condition)
Definition: c.h:804
int32 encoding
Definition: pg_database.h:41
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:81
#define elog(elevel,...)
Definition: elog.h:232

◆ SetMessageEncoding()

void SetMessageEncoding ( int  encoding)

Definition at line 1114 of file mbutils.c.

References Assert, CurrentMemoryContext, elog, encoding, pg_enc2name::encoding, GetDatabaseEncoding(), GetMessageEncoding(), i, LOG, pg_enc2gettext::name, name, pg_enc2gettext_tbl, pg_enc2name_tbl, pg_get_encoding_from_locale(), PG_SQL_ASCII, pg_strcasecmp(), PG_VALID_ENCODING, setlocale, and write_stderr.

Referenced by pg_perm_setlocale(), and surrogate_pair_to_codepoint().

1115 {
1116  /* Some calls happen before we can elog()! */
1118 
1121 }
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:301
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
pg_enc encoding
Definition: pg_wchar.h:356
#define Assert(condition)
Definition: c.h:804
int32 encoding
Definition: pg_database.h:41
static const pg_enc2name * MessageEncoding
Definition: mbutils.c:82

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

const pg_enc2name* ClientEncoding = &pg_enc2name_tbl[PG_SQL_ASCII]
static

Definition at line 80 of file mbutils.c.

◆ ConvProcList

List* ConvProcList = NIL
static

Definition at line 61 of file mbutils.c.

◆ DatabaseEncoding

const pg_enc2name* DatabaseEncoding = &pg_enc2name_tbl[PG_SQL_ASCII]
static

Definition at line 81 of file mbutils.c.

◆ MessageEncoding

const pg_enc2name* MessageEncoding = &pg_enc2name_tbl[PG_SQL_ASCII]
static

Definition at line 82 of file mbutils.c.

◆ 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().

◆ ToServerConvProc

FmgrInfo* ToServerConvProc = NULL
static

Definition at line 67 of file mbutils.c.

Referenced by perform_default_encoding_conversion().

◆ Utf8ToServerConvProc

FmgrInfo* Utf8ToServerConvProc = NULL
static

Definition at line 75 of file mbutils.c.