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)
 
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)
 
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)
 
int GetMessageEncoding (void)
 

Variables

static ListConvProcList = NIL
 
static FmgrInfoToServerConvProc = NULL
 
static FmgrInfoToClientConvProc = 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

◆ cliplen()

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

Definition at line 929 of file mbutils.c.

References Min.

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

930 {
931  int l = 0;
932 
933  len = Min(len, limit);
934  while (l < len && str[l])
935  l++;
936  return l;
937 }
#define Min(x, y)
Definition: c.h:904

◆ GetDatabaseEncoding()

◆ getdatabaseencoding()

Datum getdatabaseencoding ( PG_FUNCTION_ARGS  )

Definition at line 1058 of file mbutils.c.

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

1059 {
1061 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
#define CStringGetDatum(X)
Definition: postgres.h:578
const char * name
Definition: pg_wchar.h:328
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74

◆ GetDatabaseEncodingName()

const char* GetDatabaseEncodingName ( void  )

◆ GetMessageEncoding()

int GetMessageEncoding ( void  )

◆ InitializeClientEncoding()

void InitializeClientEncoding ( void  )

Definition at line 274 of file mbutils.c.

References Assert, backend_startup_complete, ereport, errcode(), errmsg(), FATAL, GetDatabaseEncodingName(), name, pending_client_encoding, pg_enc2name_tbl, PrepareClientEncoding(), and SetClientEncoding().

Referenced by InitPostgres().

275 {
278 
281  {
282  /*
283  * Oops, the requested conversion is not available. We couldn't fail
284  * before, but we can now.
285  */
286  ereport(FATAL,
287  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
288  errmsg("conversion between %s and %s is not supported",
291  }
292 }
int PrepareClientEncoding(int encoding)
Definition: mbutils.c:103
static bool backend_startup_complete
Definition: mbutils.c:83
static int pending_client_encoding
Definition: mbutils.c:84
int errcode(int sqlerrcode)
Definition: elog.c:570
#define FATAL
Definition: elog.h:52
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:307
int SetClientEncoding(int encoding)
Definition: mbutils.c:201
#define ereport(elevel, rest)
Definition: elog.h:141
#define Assert(condition)
Definition: c.h:732
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1052
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ length_in_encoding()

Datum length_in_encoding ( PG_FUNCTION_ARGS  )

Definition at line 520 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.

521 {
522  bytea *string = PG_GETARG_BYTEA_PP(0);
523  char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
524  int src_encoding = pg_char_to_encoding(src_encoding_name);
525  const char *src_str;
526  int len;
527  int retval;
528 
529  if (src_encoding < 0)
530  ereport(ERROR,
531  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
532  errmsg("invalid encoding name \"%s\"",
533  src_encoding_name)));
534 
535  len = VARSIZE_ANY_EXHDR(string);
536  src_str = VARDATA_ANY(string);
537 
538  retval = pg_verify_mbstr_len(src_encoding, src_str, len, false);
539 
540  PG_RETURN_INT32(retval);
541 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:551
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
Definition: wchar.c:1942
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define NameStr(name)
Definition: c.h:609
Definition: c.h:549
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ perform_default_encoding_conversion()

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

Definition at line 688 of file mbutils.c.

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

Referenced by pg_any_to_server(), and pg_server_to_any().

690 {
691  char *result;
692  int src_encoding,
693  dest_encoding;
694  FmgrInfo *flinfo;
695 
696  if (is_client_to_server)
697  {
698  src_encoding = ClientEncoding->encoding;
699  dest_encoding = DatabaseEncoding->encoding;
700  flinfo = ToServerConvProc;
701  }
702  else
703  {
704  src_encoding = DatabaseEncoding->encoding;
705  dest_encoding = ClientEncoding->encoding;
706  flinfo = ToClientConvProc;
707  }
708 
709  if (flinfo == NULL)
710  return unconstify(char *, src);
711 
712  /*
713  * Allocate space for conversion result, being wary of integer overflow.
714  * See comments in pg_do_encoding_conversion.
715  */
716  if ((Size) len >= (MaxAllocHugeSize / (Size) MAX_CONVERSION_GROWTH))
717  ereport(ERROR,
718  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
719  errmsg("out of memory"),
720  errdetail("String of %d bytes is too long for encoding conversion.",
721  len)));
722 
723  result = (char *)
725  (Size) len * MAX_CONVERSION_GROWTH + 1);
726 
727  FunctionCall5(flinfo,
728  Int32GetDatum(src_encoding),
729  Int32GetDatum(dest_encoding),
730  CStringGetDatum(src),
731  CStringGetDatum(result),
732  Int32GetDatum(len));
733 
734  /*
735  * Release extra space if there might be a lot --- see comments in
736  * pg_do_encoding_conversion.
737  */
738  if (len > 1000000)
739  {
740  Size resultlen = strlen(result);
741 
742  if (resultlen >= MaxAllocSize)
743  ereport(ERROR,
744  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
745  errmsg("out of memory"),
746  errdetail("String of %d bytes is too long for encoding conversion.",
747  len)));
748 
749  result = (char *) repalloc(result, resultlen + 1);
750  }
751 
752  return result;
753 }
Definition: fmgr.h:56
static FmgrInfo * ToServerConvProc
Definition: mbutils.c:67
int errcode(int sqlerrcode)
Definition: elog.c:570
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:73
#define MaxAllocHugeSize
Definition: memutils.h:44
#define ERROR
Definition: elog.h:43
#define FunctionCall5(flinfo, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:643
void * MemoryContextAllocHuge(MemoryContext context, Size size)
Definition: mcxt.c:1105
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define CStringGetDatum(X)
Definition: postgres.h:578
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define ereport(elevel, rest)
Definition: elog.h:141
#define MaxAllocSize
Definition: memutils.h:40
#define unconstify(underlying_type, expr)
Definition: c.h:1163
pg_enc encoding
Definition: pg_wchar.h:329
size_t Size
Definition: c.h:466
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:316
#define Int32GetDatum(X)
Definition: postgres.h:479
static FmgrInfo * ToClientConvProc
Definition: mbutils.c:68
int errmsg(const char *fmt,...)
Definition: elog.c:784
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74

◆ pg_any_to_server()

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

Definition at line 581 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(), CopyReadLine(), db_encoding_convert(), dsnowball_lexize(), pg_client_to_server(), pg_stat_statements_internal(), pgp_armor_headers(), PLyUnicode_Bytes(), read_extension_script_file(), t_readline(), unicode_to_sqlchar(), utf_u2e(), X509_NAME_to_cstring(), X509_NAME_to_text(), and xml_recv().

582 {
583  if (len <= 0)
584  return unconstify(char *, s); /* empty string is always valid */
585 
588  {
589  /*
590  * No conversion is needed, but we must still validate the data.
591  */
592  (void) pg_verify_mbstr(DatabaseEncoding->encoding, s, len, false);
593  return unconstify(char *, s);
594  }
595 
597  {
598  /*
599  * No conversion is possible, but we must still validate the data,
600  * because the client-side code might have done string escaping using
601  * the selected client_encoding. If the client encoding is ASCII-safe
602  * then we just do a straight validation under that encoding. For an
603  * ASCII-unsafe encoding we have a problem: we dare not pass such data
604  * to the parser but we have no way to convert it. We compromise by
605  * rejecting the data if it contains any non-ASCII characters.
606  */
608  (void) pg_verify_mbstr(encoding, s, len, false);
609  else
610  {
611  int i;
612 
613  for (i = 0; i < len; i++)
614  {
615  if (s[i] == '\0' || IS_HIGHBIT_SET(s[i]))
616  ereport(ERROR,
617  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
618  errmsg("invalid byte value for encoding \"%s\": 0x%02x",
620  (unsigned char) s[i])));
621  }
622  }
623  return unconstify(char *, s);
624  }
625 
626  /* Fast path if we can use cached conversion function */
628  return perform_default_encoding_conversion(s, len, true);
629 
630  /* General case ... will not work outside transactions */
631  return (char *) pg_do_encoding_conversion((unsigned char *) unconstify(char *, s),
632  len,
633  encoding,
635 }
static char * perform_default_encoding_conversion(const char *src, int len, bool is_client_to_server)
Definition: mbutils.c:688
int errcode(int sqlerrcode)
Definition: elog.c:570
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:73
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition: mbutils.c:318
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1075
#define ERROR
Definition: elog.h:43
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:307
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: wchar.c:1925
#define ereport(elevel, rest)
Definition: elog.h:141
#define unconstify(underlying_type, expr)
Definition: c.h:1163
pg_enc encoding
Definition: pg_wchar.h:329
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:295
int32 encoding
Definition: pg_database.h:41
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:784
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74
int i

◆ pg_client_encoding()

Datum pg_client_encoding ( PG_FUNCTION_ARGS  )

Definition at line 1064 of file mbutils.c.

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

1065 {
1067 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:73
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
#define CStringGetDatum(X)
Definition: postgres.h:578
const char * name
Definition: pg_wchar.h:328

◆ pg_client_to_server()

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

Definition at line 565 of file mbutils.c.

References pg_enc2name::encoding, and pg_any_to_server().

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

566 {
567  return pg_any_to_server(s, len, ClientEncoding->encoding);
568 }
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:73
pg_enc encoding
Definition: pg_wchar.h:329
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:581

◆ pg_convert()

Datum pg_convert ( PG_FUNCTION_ARGS  )

Definition at line 458 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_len(), SET_VARSIZE, unconstify, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by pg_convert_from(), and pg_convert_to().

459 {
460  bytea *string = PG_GETARG_BYTEA_PP(0);
461  char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
462  int src_encoding = pg_char_to_encoding(src_encoding_name);
463  char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2));
464  int dest_encoding = pg_char_to_encoding(dest_encoding_name);
465  const char *src_str;
466  char *dest_str;
467  bytea *retval;
468  int len;
469 
470  if (src_encoding < 0)
471  ereport(ERROR,
472  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
473  errmsg("invalid source encoding name \"%s\"",
474  src_encoding_name)));
475  if (dest_encoding < 0)
476  ereport(ERROR,
477  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
478  errmsg("invalid destination encoding name \"%s\"",
479  dest_encoding_name)));
480 
481  /* make sure that source string is valid */
482  len = VARSIZE_ANY_EXHDR(string);
483  src_str = VARDATA_ANY(string);
484  pg_verify_mbstr_len(src_encoding, src_str, len, false);
485 
486  /* perform conversion */
487  dest_str = (char *) pg_do_encoding_conversion((unsigned char *) unconstify(char *, src_str),
488  len,
489  src_encoding,
490  dest_encoding);
491 
492  /* update len if conversion actually happened */
493  if (dest_str != src_str)
494  len = strlen(dest_str);
495 
496  /*
497  * build bytea data type structure.
498  */
499  retval = (bytea *) palloc(len + VARHDRSZ);
500  SET_VARSIZE(retval, len + VARHDRSZ);
501  memcpy(VARDATA(retval), dest_str, len);
502 
503  if (dest_str != src_str)
504  pfree(dest_str);
505 
506  /* free memory if allocated by the toaster */
507  PG_FREE_IF_COPY(string, 0);
508 
509  PG_RETURN_BYTEA_P(retval);
510 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:551
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:555
int pg_verify_mbstr_len(int encoding, const char *mbstr, int len, bool noError)
Definition: wchar.c:1942
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition: mbutils.c:318
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define unconstify(underlying_type, expr)
Definition: c.h:1163
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define NameStr(name)
Definition: c.h:609
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ pg_convert_from()

Datum pg_convert_from ( PG_FUNCTION_ARGS  )

Definition at line 431 of file mbutils.c.

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

432 {
433  Datum string = PG_GETARG_DATUM(0);
434  Datum src_encoding_name = PG_GETARG_DATUM(1);
435  Datum dest_encoding_name = DirectFunctionCall1(namein,
437  Datum result;
438 
439  result = DirectFunctionCall3(pg_convert, string,
440  src_encoding_name, dest_encoding_name);
441 
442  /*
443  * pg_convert returns a bytea, which we in turn return as text, relying on
444  * the fact that they are both in fact varlena types, and thus
445  * structurally identical. Although not all bytea values are valid text,
446  * in this case it will be because we've told pg_convert to return one
447  * that is valid as text in the current database encoding.
448  */
449  PG_RETURN_DATUM(result);
450 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
#define CStringGetDatum(X)
Definition: postgres.h:578
const char * name
Definition: pg_wchar.h:328
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:621
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Datum pg_convert(PG_FUNCTION_ARGS)
Definition: mbutils.c:458
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74

◆ pg_convert_to()

Datum pg_convert_to ( PG_FUNCTION_ARGS  )

Definition at line 406 of file mbutils.c.

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

407 {
408  Datum string = PG_GETARG_DATUM(0);
409  Datum dest_encoding_name = PG_GETARG_DATUM(1);
410  Datum src_encoding_name = DirectFunctionCall1(namein,
412  Datum result;
413 
414  /*
415  * pg_convert expects a bytea as its first argument. We're passing it a
416  * text argument here, relying on the fact that they are both in fact
417  * varlena types, and thus structurally identical.
418  */
419  result = DirectFunctionCall3(pg_convert, string,
420  src_encoding_name, dest_encoding_name);
421 
422  PG_RETURN_DATUM(result);
423 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
#define CStringGetDatum(X)
Definition: postgres.h:578
const char * name
Definition: pg_wchar.h:328
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:621
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Datum pg_convert(PG_FUNCTION_ARGS)
Definition: mbutils.c:458
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74

◆ pg_do_encoding_conversion()

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

Definition at line 318 of file mbutils.c.

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

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

320 {
321  unsigned char *result;
322  Oid proc;
323 
324  if (len <= 0)
325  return src; /* empty string is always valid */
326 
327  if (src_encoding == dest_encoding)
328  return src; /* no conversion required, assume valid */
329 
330  if (dest_encoding == PG_SQL_ASCII)
331  return src; /* any string is valid in SQL_ASCII */
332 
333  if (src_encoding == PG_SQL_ASCII)
334  {
335  /* No conversion is possible, but we must validate the result */
336  (void) pg_verify_mbstr(dest_encoding, (const char *) src, len, false);
337  return src;
338  }
339 
340  if (!IsTransactionState()) /* shouldn't happen */
341  elog(ERROR, "cannot perform encoding conversion outside a transaction");
342 
343  proc = FindDefaultConversionProc(src_encoding, dest_encoding);
344  if (!OidIsValid(proc))
345  ereport(ERROR,
346  (errcode(ERRCODE_UNDEFINED_FUNCTION),
347  errmsg("default conversion function for encoding \"%s\" to \"%s\" does not exist",
348  pg_encoding_to_char(src_encoding),
349  pg_encoding_to_char(dest_encoding))));
350 
351  /*
352  * Allocate space for conversion result, being wary of integer overflow.
353  *
354  * len * MAX_CONVERSION_GROWTH is typically a vast overestimate of the
355  * required space, so it might exceed MaxAllocSize even though the result
356  * would actually fit. We do not want to hand back a result string that
357  * exceeds MaxAllocSize, because callers might not cope gracefully --- but
358  * if we just allocate more than that, and don't use it, that's fine.
359  */
360  if ((Size) len >= (MaxAllocHugeSize / (Size) MAX_CONVERSION_GROWTH))
361  ereport(ERROR,
362  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
363  errmsg("out of memory"),
364  errdetail("String of %d bytes is too long for encoding conversion.",
365  len)));
366 
367  result = (unsigned char *)
369  (Size) len * MAX_CONVERSION_GROWTH + 1);
370 
371  OidFunctionCall5(proc,
372  Int32GetDatum(src_encoding),
373  Int32GetDatum(dest_encoding),
374  CStringGetDatum(src),
375  CStringGetDatum(result),
376  Int32GetDatum(len));
377 
378  /*
379  * If the result is large, it's worth repalloc'ing to release any extra
380  * space we asked for. The cutoff here is somewhat arbitrary, but we
381  * *must* check when len * MAX_CONVERSION_GROWTH exceeds MaxAllocSize.
382  */
383  if (len > 1000000)
384  {
385  Size resultlen = strlen((char *) result);
386 
387  if (resultlen >= MaxAllocSize)
388  ereport(ERROR,
389  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
390  errmsg("out of memory"),
391  errdetail("String of %d bytes is too long for encoding conversion.",
392  len)));
393 
394  result = (unsigned char *) repalloc(result, resultlen + 1);
395  }
396 
397  return result;
398 }
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define MaxAllocHugeSize
Definition: memutils.h:44
#define ERROR
Definition: elog.h:43
void * MemoryContextAllocHuge(MemoryContext context, Size size)
Definition: mcxt.c:1105
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define CStringGetDatum(X)
Definition: postgres.h:578
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: wchar.c:1925
#define ereport(elevel, rest)
Definition: elog.h:141
#define MaxAllocSize
Definition: memutils.h:40
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
size_t Size
Definition: c.h:466
Oid FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
Definition: namespace.c:3673
bool IsTransactionState(void)
Definition: xact.c:356
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:316
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define OidFunctionCall5(functionId, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:663

◆ pg_dsplen()

int pg_dsplen ( const char *  mbstr)

Definition at line 809 of file mbutils.c.

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

Referenced by p_isspecial().

810 {
811  return pg_wchar_table[DatabaseEncoding->encoding].dsplen((const unsigned char *) mbstr);
812 }
mbdisplaylen_converter dsplen
Definition: pg_wchar.h:380
pg_enc encoding
Definition: pg_wchar.h:329
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1777
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74

◆ pg_encoding_max_length_sql()

Datum pg_encoding_max_length_sql ( PG_FUNCTION_ARGS  )

Definition at line 549 of file mbutils.c.

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

550 {
551  int encoding = PG_GETARG_INT32(0);
552 
553  if (PG_VALID_ENCODING(encoding))
554  PG_RETURN_INT32(pg_wchar_table[encoding].maxmblen);
555  else
556  PG_RETURN_NULL();
557 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#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:1777
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ pg_encoding_mb2wchar_with_len()

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

Definition at line 772 of file mbutils.c.

References encoding, pg_wchar_tbl::mb2wchar_with_len, and pg_wchar_table.

Referenced by xml_is_document().

774 {
775  return pg_wchar_table[encoding].mb2wchar_with_len((const unsigned char *) from, to, len);
776 }
int32 encoding
Definition: pg_database.h:41
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1777
mb2wchar_with_len_converter mb2wchar_with_len
Definition: pg_wchar.h:375

◆ pg_encoding_mbcliplen()

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

Definition at line 872 of file mbutils.c.

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

Referenced by pg_mbcliplen().

874 {
875  mblen_converter mblen_fn;
876  int clen = 0;
877  int l;
878 
879  /* optimization for single byte encoding */
881  return cliplen(mbstr, len, limit);
882 
883  mblen_fn = pg_wchar_table[encoding].mblen;
884 
885  while (len > 0 && *mbstr)
886  {
887  l = (*mblen_fn) ((const unsigned char *) mbstr);
888  if ((clen + l) > limit)
889  break;
890  clen += l;
891  if (clen == limit)
892  break;
893  len -= l;
894  mbstr += l;
895  }
896  return clen;
897 }
int(* mblen_converter)(const unsigned char *mbstr)
Definition: pg_wchar.h:365
int pg_encoding_max_length(int encoding)
Definition: wchar.c:1868
static int cliplen(const char *str, int len, int limit)
Definition: mbutils.c:929
int32 encoding
Definition: pg_database.h:41
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1777
mblen_converter mblen
Definition: pg_wchar.h:379

◆ pg_encoding_wchar2mb_with_len()

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

Definition at line 794 of file mbutils.c.

References encoding, pg_wchar_table, and pg_wchar_tbl::wchar2mb_with_len.

796 {
797  return pg_wchar_table[encoding].wchar2mb_with_len(from, (unsigned char *) to, len);
798 }
wchar2mb_with_len_converter wchar2mb_with_len
Definition: pg_wchar.h:377
int32 encoding
Definition: pg_database.h:41
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1777

◆ pg_get_client_encoding()

int pg_get_client_encoding ( void  )

Definition at line 298 of file mbutils.c.

References pg_enc2name::encoding.

Referenced by BeginCopy(), and xml_send().

299 {
300  return ClientEncoding->encoding;
301 }
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:73
pg_enc encoding
Definition: pg_wchar.h:329

◆ pg_get_client_encoding_name()

const char* pg_get_client_encoding_name ( void  )

Definition at line 307 of file mbutils.c.

References pg_enc2name::name.

308 {
309  return ClientEncoding->name;
310 }
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:73
const char * name
Definition: pg_wchar.h:328

◆ pg_mb2wchar()

int pg_mb2wchar ( const char *  from,
pg_wchar to 
)

Definition at line 758 of file mbutils.c.

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

759 {
760  return pg_wchar_table[DatabaseEncoding->encoding].mb2wchar_with_len((const unsigned char *) from, to, strlen(from));
761 }
pg_enc encoding
Definition: pg_wchar.h:329
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1777
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74
mb2wchar_with_len_converter mb2wchar_with_len
Definition: pg_wchar.h:375

◆ pg_mb2wchar_with_len()

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

Definition at line 765 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(), and TParserInit().

766 {
767  return pg_wchar_table[DatabaseEncoding->encoding].mb2wchar_with_len((const unsigned char *) from, to, len);
768 }
pg_enc encoding
Definition: pg_wchar.h:329
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1777
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74
mb2wchar_with_len_converter mb2wchar_with_len
Definition: pg_wchar.h:375

◆ pg_mbcharcliplen()

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

Definition at line 904 of file mbutils.c.

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

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

905 {
906  int clen = 0;
907  int nch = 0;
908  int l;
909 
910  /* optimization for single byte encoding */
912  return cliplen(mbstr, len, limit);
913 
914  while (len > 0 && *mbstr)
915  {
916  l = pg_mblen(mbstr);
917  nch++;
918  if (nch > limit)
919  break;
920  clen += l;
921  len -= l;
922  mbstr += l;
923  }
924  return clen;
925 }
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
static int cliplen(const char *str, int len, int limit)
Definition: mbutils.c:929
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802

◆ pg_mbcliplen()

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

◆ pg_mblen()

int pg_mblen ( const char *  mbstr)

Definition at line 802 of file mbutils.c.

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

Referenced by addCompoundAffixFlagValue(), appendStringInfoRegexpSubstr(), charlen_to_bytelen(), check_replace_text_has_escape_char(), DCH_from_char(), dotrim(), extract_mb_char(), 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(), infix(), initTrie(), json_lex_string(), lpad(), lquery_in(), ltree_in(), 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_or_operator(), pg_mbcharcliplen(), pg_mbstrlen(), pg_mbstrlen_with_len(), readstoplist(), report_json_context(), rpad(), RS_compile(), RS_execute(), RS_isRegis(), similar_escape_internal(), t_isalpha(), t_isdigit(), t_isprint(), t_isspace(), text_position_get_match_pos(), text_position_next(), text_reverse(), text_substring(), text_to_array_internal(), thesaurusRead(), TParserGet(), translate(), ts_stat_sql(), tsvectorout(), unaccent_lexize(), varstr_levenshtein(), wchareq(), and xml_is_document().

803 {
804  return pg_wchar_table[DatabaseEncoding->encoding].mblen((const unsigned char *) mbstr);
805 }
pg_enc encoding
Definition: pg_wchar.h:329
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1777
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74
mblen_converter mblen
Definition: pg_wchar.h:379

◆ pg_mbstrlen()

int pg_mbstrlen ( const char *  mbstr)

Definition at line 816 of file mbutils.c.

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by NUM_processor(), and text_format_append_string().

817 {
818  int len = 0;
819 
820  /* optimization for single byte encoding */
822  return strlen(mbstr);
823 
824  while (*mbstr)
825  {
826  mbstr += pg_mblen(mbstr);
827  len++;
828  }
829  return len;
830 }
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802

◆ pg_mbstrlen_with_len()

int pg_mbstrlen_with_len ( const char *  mbstr,
int  limit 
)

Definition at line 836 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(), text_left(), text_length(), text_right(), text_substring(), and varstr_levenshtein().

837 {
838  int len = 0;
839 
840  /* optimization for single byte encoding */
842  return limit;
843 
844  while (limit > 0 && *mbstr)
845  {
846  int l = pg_mblen(mbstr);
847 
848  limit -= l;
849  mbstr += l;
850  len++;
851  }
852  return len;
853 }
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
int pg_mblen(const char *mbstr)
Definition: mbutils.c:802

◆ pg_server_to_any()

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

Definition at line 654 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(), CopyTo(), dsnowball_lexize(), hv_fetch_string(), hv_store_string(), pg_server_to_client(), PLyUnicode_AsString(), utf_e2u(), and xml_is_document().

655 {
656  if (len <= 0)
657  return unconstify(char *, s); /* empty string is always valid */
658 
661  return unconstify(char *, s); /* assume data is valid */
662 
664  {
665  /* No conversion is possible, but we must validate the result */
666  (void) pg_verify_mbstr(encoding, s, len, false);
667  return unconstify(char *, s);
668  }
669 
670  /* Fast path if we can use cached conversion function */
672  return perform_default_encoding_conversion(s, len, false);
673 
674  /* General case ... will not work outside transactions */
675  return (char *) pg_do_encoding_conversion((unsigned char *) unconstify(char *, s),
676  len,
678  encoding);
679 }
static char * perform_default_encoding_conversion(const char *src, int len, bool is_client_to_server)
Definition: mbutils.c:688
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:73
unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)
Definition: mbutils.c:318
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: wchar.c:1925
#define unconstify(underlying_type, expr)
Definition: c.h:1163
pg_enc encoding
Definition: pg_wchar.h:329
int32 encoding
Definition: pg_database.h:41
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74

◆ pg_server_to_client()

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

Definition at line 643 of file mbutils.c.

References pg_enc2name::encoding, and pg_server_to_any().

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

644 {
645  return pg_server_to_any(s, len, ClientEncoding->encoding);
646 }
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:73
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:654
pg_enc encoding
Definition: pg_wchar.h:329

◆ pg_wchar2mb()

int pg_wchar2mb ( const pg_wchar from,
char *  to 
)

Definition at line 780 of file mbutils.c.

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

781 {
782  return pg_wchar_table[DatabaseEncoding->encoding].wchar2mb_with_len(from, (unsigned char *) to, pg_wchar_strlen(from));
783 }
size_t pg_wchar_strlen(const pg_wchar *str)
Definition: wstrncmp.c:70
wchar2mb_with_len_converter wchar2mb_with_len
Definition: pg_wchar.h:377
pg_enc encoding
Definition: pg_wchar.h:329
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1777
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74

◆ pg_wchar2mb_with_len()

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

Definition at line 787 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(), convertPgWchar(), and regexp_fixed_prefix().

788 {
789  return pg_wchar_table[DatabaseEncoding->encoding].wchar2mb_with_len(from, (unsigned char *) to, len);
790 }
wchar2mb_with_len_converter wchar2mb_with_len
Definition: pg_wchar.h:377
pg_enc encoding
Definition: pg_wchar.h:329
const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:1777
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74

◆ PrepareClientEncoding()

int PrepareClientEncoding ( int  encoding)

Definition at line 103 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(), and InitializeClientEncoding().

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

◆ SetClientEncoding()

int SetClientEncoding ( int  encoding)

Definition at line 201 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(), and ParallelWorkerMain().

202 {
203  int current_server_encoding;
204  bool found;
205  ListCell *lc;
206 
208  return -1;
209 
210  /* Can't do anything during startup, per notes above */
212  {
214  return 0;
215  }
216 
217  current_server_encoding = GetDatabaseEncoding();
218 
219  /*
220  * Check for cases that require no conversion function.
221  */
222  if (current_server_encoding == encoding ||
223  current_server_encoding == PG_SQL_ASCII ||
225  {
227  ToServerConvProc = NULL;
228  ToClientConvProc = NULL;
229  return 0;
230  }
231 
232  /*
233  * Search the cache for the entry previously prepared by
234  * PrepareClientEncoding; if there isn't one, we lose. While at it,
235  * release any duplicate entries so that repeated Prepare/Set cycles don't
236  * leak memory.
237  */
238  found = false;
239  foreach(lc, ConvProcList)
240  {
241  ConvProcInfo *convinfo = (ConvProcInfo *) lfirst(lc);
242 
243  if (convinfo->s_encoding == current_server_encoding &&
244  convinfo->c_encoding == encoding)
245  {
246  if (!found)
247  {
248  /* Found newest entry, so set up */
250  ToServerConvProc = &convinfo->to_server_info;
251  ToClientConvProc = &convinfo->to_client_info;
252  found = true;
253  }
254  else
255  {
256  /* Duplicate entry, release it */
258  pfree(convinfo);
259  }
260  }
261  }
262 
263  if (found)
264  return 0; /* success */
265  else
266  return -1; /* it's not cached, so fail */
267 }
FmgrInfo to_server_info
Definition: mbutils.c:57
FmgrInfo to_client_info
Definition: mbutils.c:58
static bool backend_startup_complete
Definition: mbutils.c:83
static int pending_client_encoding
Definition: mbutils.c:84
static FmgrInfo * ToServerConvProc
Definition: mbutils.c:67
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:73
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:368
int s_encoding
Definition: mbutils.c:55
void pfree(void *pointer)
Definition: mcxt.c:1056
static List * ConvProcList
Definition: mbutils.c:61
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:307
#define PG_VALID_FE_ENCODING(_enc)
Definition: pg_wchar.h:305
int GetDatabaseEncoding(void)
Definition: mbutils.c:1046
#define lfirst(lc)
Definition: pg_list.h:190
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 940 of file mbutils.c.

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

Referenced by CheckMyDatabase().

941 {
943  elog(ERROR, "invalid database encoding: %d", encoding);
944 
947 }
#define ERROR
Definition: elog.h:43
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:307
pg_enc encoding
Definition: pg_wchar.h:329
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:295
#define Assert(condition)
Definition: c.h:732
int32 encoding
Definition: pg_database.h:41
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74
#define elog(elevel,...)
Definition: elog.h:226

◆ SetMessageEncoding()

void SetMessageEncoding ( int  encoding)

Definition at line 950 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().

951 {
952  /* Some calls happen before we can elog()! */
954 
957 }
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:301
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:307
pg_enc encoding
Definition: pg_wchar.h:329
#define Assert(condition)
Definition: c.h:732
int32 encoding
Definition: pg_database.h:41
static const pg_enc2name * MessageEncoding
Definition: mbutils.c:75

Variable Documentation

◆ backend_startup_complete

bool backend_startup_complete = false
static

Definition at line 83 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 73 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 74 of file mbutils.c.

◆ MessageEncoding

const pg_enc2name* MessageEncoding = &pg_enc2name_tbl[PG_SQL_ASCII]
static

Definition at line 75 of file mbutils.c.

◆ pending_client_encoding

int pending_client_encoding = PG_SQL_ASCII
static

Definition at line 84 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().