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

Function Documentation

◆ cliplen()

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

Definition at line 879 of file mbutils.c.

References Min.

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

880 {
881  int l = 0;
882 
883  len = Min(len, limit);
884  while (l < len && str[l])
885  l++;
886  return l;
887 }
#define Min(x, y)
Definition: c.h:904

◆ GetDatabaseEncoding()

◆ getdatabaseencoding()

Datum getdatabaseencoding ( PG_FUNCTION_ARGS  )

Definition at line 1008 of file mbutils.c.

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

1009 {
1011 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
#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:1002
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 492 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.

493 {
494  bytea *string = PG_GETARG_BYTEA_PP(0);
495  char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
496  int src_encoding = pg_char_to_encoding(src_encoding_name);
497  const char *src_str;
498  int len;
499  int retval;
500 
501  if (src_encoding < 0)
502  ereport(ERROR,
503  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
504  errmsg("invalid encoding name \"%s\"",
505  src_encoding_name)));
506 
507  len = VARSIZE_ANY_EXHDR(string);
508  src_str = VARDATA_ANY(string);
509 
510  retval = pg_verify_mbstr_len(src_encoding, src_str, len, false);
511 
512  PG_RETURN_INT32(retval);
513 }
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 660 of file mbutils.c.

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

Referenced by pg_any_to_server(), and pg_server_to_any().

662 {
663  char *result;
664  int src_encoding,
665  dest_encoding;
666  FmgrInfo *flinfo;
667 
668  if (is_client_to_server)
669  {
670  src_encoding = ClientEncoding->encoding;
671  dest_encoding = DatabaseEncoding->encoding;
672  flinfo = ToServerConvProc;
673  }
674  else
675  {
676  src_encoding = DatabaseEncoding->encoding;
677  dest_encoding = ClientEncoding->encoding;
678  flinfo = ToClientConvProc;
679  }
680 
681  if (flinfo == NULL)
682  return unconstify(char *, src);
683 
684  /*
685  * Allocate space for conversion result, being wary of integer overflow
686  */
687  if ((Size) len >= (MaxAllocSize / (Size) MAX_CONVERSION_GROWTH))
688  ereport(ERROR,
689  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
690  errmsg("out of memory"),
691  errdetail("String of %d bytes is too long for encoding conversion.",
692  len)));
693 
694  result = palloc(len * MAX_CONVERSION_GROWTH + 1);
695 
696  FunctionCall5(flinfo,
697  Int32GetDatum(src_encoding),
698  Int32GetDatum(dest_encoding),
699  CStringGetDatum(src),
700  CStringGetDatum(result),
701  Int32GetDatum(len));
702  return result;
703 }
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 ERROR
Definition: elog.h:43
#define FunctionCall5(flinfo, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:642
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define CStringGetDatum(X)
Definition: postgres.h:578
#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
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:316
#define Int32GetDatum(X)
Definition: postgres.h:479
static FmgrInfo * ToClientConvProc
Definition: mbutils.c:68
void * palloc(Size size)
Definition: mcxt.c:924
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 553 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().

554 {
555  if (len <= 0)
556  return unconstify(char *, s); /* empty string is always valid */
557 
560  {
561  /*
562  * No conversion is needed, but we must still validate the data.
563  */
564  (void) pg_verify_mbstr(DatabaseEncoding->encoding, s, len, false);
565  return unconstify(char *, s);
566  }
567 
569  {
570  /*
571  * No conversion is possible, but we must still validate the data,
572  * because the client-side code might have done string escaping using
573  * the selected client_encoding. If the client encoding is ASCII-safe
574  * then we just do a straight validation under that encoding. For an
575  * ASCII-unsafe encoding we have a problem: we dare not pass such data
576  * to the parser but we have no way to convert it. We compromise by
577  * rejecting the data if it contains any non-ASCII characters.
578  */
580  (void) pg_verify_mbstr(encoding, s, len, false);
581  else
582  {
583  int i;
584 
585  for (i = 0; i < len; i++)
586  {
587  if (s[i] == '\0' || IS_HIGHBIT_SET(s[i]))
588  ereport(ERROR,
589  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
590  errmsg("invalid byte value for encoding \"%s\": 0x%02x",
592  (unsigned char) s[i])));
593  }
594  }
595  return unconstify(char *, s);
596  }
597 
598  /* Fast path if we can use cached conversion function */
600  return perform_default_encoding_conversion(s, len, true);
601 
602  /* General case ... will not work outside transactions */
603  return (char *) pg_do_encoding_conversion((unsigned char *) unconstify(char *, s),
604  len,
605  encoding,
607 }
static char * perform_default_encoding_conversion(const char *src, int len, bool is_client_to_server)
Definition: mbutils.c:660
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 1014 of file mbutils.c.

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

1015 {
1017 }
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:616
#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 537 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().

538 {
539  return pg_any_to_server(s, len, ClientEncoding->encoding);
540 }
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:553

◆ pg_convert()

Datum pg_convert ( PG_FUNCTION_ARGS  )

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

431 {
432  bytea *string = PG_GETARG_BYTEA_PP(0);
433  char *src_encoding_name = NameStr(*PG_GETARG_NAME(1));
434  int src_encoding = pg_char_to_encoding(src_encoding_name);
435  char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2));
436  int dest_encoding = pg_char_to_encoding(dest_encoding_name);
437  const char *src_str;
438  char *dest_str;
439  bytea *retval;
440  int len;
441 
442  if (src_encoding < 0)
443  ereport(ERROR,
444  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
445  errmsg("invalid source encoding name \"%s\"",
446  src_encoding_name)));
447  if (dest_encoding < 0)
448  ereport(ERROR,
449  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
450  errmsg("invalid destination encoding name \"%s\"",
451  dest_encoding_name)));
452 
453  /* make sure that source string is valid */
454  len = VARSIZE_ANY_EXHDR(string);
455  src_str = VARDATA_ANY(string);
456  pg_verify_mbstr_len(src_encoding, src_str, len, false);
457 
458  /* perform conversion */
459  dest_str = (char *) pg_do_encoding_conversion((unsigned char *) unconstify(char *, src_str),
460  len,
461  src_encoding,
462  dest_encoding);
463 
464  /* update len if conversion actually happened */
465  if (dest_str != src_str)
466  len = strlen(dest_str);
467 
468  /*
469  * build bytea data type structure.
470  */
471  retval = (bytea *) palloc(len + VARHDRSZ);
472  SET_VARSIZE(retval, len + VARHDRSZ);
473  memcpy(VARDATA(retval), dest_str, len);
474 
475  if (dest_str != src_str)
476  pfree(dest_str);
477 
478  /* free memory if allocated by the toaster */
479  PG_FREE_IF_COPY(string, 0);
480 
481  PG_RETURN_BYTEA_P(retval);
482 }
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:1031
#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:924
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 403 of file mbutils.c.

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

404 {
405  Datum string = PG_GETARG_DATUM(0);
406  Datum src_encoding_name = PG_GETARG_DATUM(1);
407  Datum dest_encoding_name = DirectFunctionCall1(namein,
409  Datum result;
410 
411  result = DirectFunctionCall3(pg_convert, string,
412  src_encoding_name, dest_encoding_name);
413 
414  /*
415  * pg_convert returns a bytea, which we in turn return as text, relying on
416  * the fact that they are both in fact varlena types, and thus
417  * structurally identical. Although not all bytea values are valid text,
418  * in this case it will be because we've told pg_convert to return one
419  * that is valid as text in the current database encoding.
420  */
421  PG_RETURN_DATUM(result);
422 }
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:616
#define CStringGetDatum(X)
Definition: postgres.h:578
const char * name
Definition: pg_wchar.h:328
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:620
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:430
static const pg_enc2name * DatabaseEncoding
Definition: mbutils.c:74

◆ pg_convert_to()

Datum pg_convert_to ( PG_FUNCTION_ARGS  )

Definition at line 378 of file mbutils.c.

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

379 {
380  Datum string = PG_GETARG_DATUM(0);
381  Datum dest_encoding_name = PG_GETARG_DATUM(1);
382  Datum src_encoding_name = DirectFunctionCall1(namein,
384  Datum result;
385 
386  /*
387  * pg_convert expects a bytea as its first argument. We're passing it a
388  * text argument here, relying on the fact that they are both in fact
389  * varlena types, and thus structurally identical.
390  */
391  result = DirectFunctionCall3(pg_convert, string,
392  src_encoding_name, dest_encoding_name);
393 
394  PG_RETURN_DATUM(result);
395 }
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:616
#define CStringGetDatum(X)
Definition: postgres.h:578
const char * name
Definition: pg_wchar.h:328
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:620
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:430
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, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, FindDefaultConversionProc(), Int32GetDatum, IsTransactionState(), MAX_CONVERSION_GROWTH, MaxAllocSize, OidFunctionCall5, OidIsValid, palloc(), pg_encoding_to_char(), PG_SQL_ASCII, and pg_verify_mbstr().

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  if ((Size) len >= (MaxAllocSize / (Size) MAX_CONVERSION_GROWTH))
355  ereport(ERROR,
356  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
357  errmsg("out of memory"),
358  errdetail("String of %d bytes is too long for encoding conversion.",
359  len)));
360 
361  result = palloc(len * MAX_CONVERSION_GROWTH + 1);
362 
363  OidFunctionCall5(proc,
364  Int32GetDatum(src_encoding),
365  Int32GetDatum(dest_encoding),
366  CStringGetDatum(src),
367  CStringGetDatum(result),
368  Int32GetDatum(len));
369  return result;
370 }
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define CStringGetDatum(X)
Definition: postgres.h:578
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
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:316
#define Int32GetDatum(X)
Definition: postgres.h:479
void * palloc(Size size)
Definition: mcxt.c:924
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:662

◆ pg_dsplen()

int pg_dsplen ( const char *  mbstr)

Definition at line 759 of file mbutils.c.

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

Referenced by p_isspecial().

760 {
761  return pg_wchar_table[DatabaseEncoding->encoding].dsplen((const unsigned char *) mbstr);
762 }
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 521 of file mbutils.c.

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

522 {
523  int encoding = PG_GETARG_INT32(0);
524 
525  if (PG_VALID_ENCODING(encoding))
526  PG_RETURN_INT32(pg_wchar_table[encoding].maxmblen);
527  else
528  PG_RETURN_NULL();
529 }
#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 722 of file mbutils.c.

References encoding, pg_wchar_tbl::mb2wchar_with_len, and pg_wchar_table.

Referenced by xml_is_document().

724 {
725  return pg_wchar_table[encoding].mb2wchar_with_len((const unsigned char *) from, to, len);
726 }
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 822 of file mbutils.c.

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

Referenced by pg_mbcliplen().

824 {
825  mblen_converter mblen_fn;
826  int clen = 0;
827  int l;
828 
829  /* optimization for single byte encoding */
831  return cliplen(mbstr, len, limit);
832 
833  mblen_fn = pg_wchar_table[encoding].mblen;
834 
835  while (len > 0 && *mbstr)
836  {
837  l = (*mblen_fn) ((const unsigned char *) mbstr);
838  if ((clen + l) > limit)
839  break;
840  clen += l;
841  if (clen == limit)
842  break;
843  len -= l;
844  mbstr += l;
845  }
846  return clen;
847 }
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:879
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 744 of file mbutils.c.

References encoding, pg_wchar_table, and pg_wchar_tbl::wchar2mb_with_len.

746 {
747  return pg_wchar_table[encoding].wchar2mb_with_len(from, (unsigned char *) to, len);
748 }
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 708 of file mbutils.c.

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

709 {
710  return pg_wchar_table[DatabaseEncoding->encoding].mb2wchar_with_len((const unsigned char *) from, to, strlen(from));
711 }
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 715 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().

716 {
717  return pg_wchar_table[DatabaseEncoding->encoding].mb2wchar_with_len((const unsigned char *) from, to, len);
718 }
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 854 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().

855 {
856  int clen = 0;
857  int nch = 0;
858  int l;
859 
860  /* optimization for single byte encoding */
862  return cliplen(mbstr, len, limit);
863 
864  while (len > 0 && *mbstr)
865  {
866  l = pg_mblen(mbstr);
867  nch++;
868  if (nch > limit)
869  break;
870  clen += l;
871  len -= l;
872  mbstr += l;
873  }
874  return clen;
875 }
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
static int cliplen(const char *str, int len, int limit)
Definition: mbutils.c:879
int pg_mblen(const char *mbstr)
Definition: mbutils.c:752

◆ pg_mbcliplen()

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

◆ pg_mblen()

int pg_mblen ( const char *  mbstr)

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

753 {
754  return pg_wchar_table[DatabaseEncoding->encoding].mblen((const unsigned char *) mbstr);
755 }
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 766 of file mbutils.c.

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by NUM_processor(), and text_format_append_string().

767 {
768  int len = 0;
769 
770  /* optimization for single byte encoding */
772  return strlen(mbstr);
773 
774  while (*mbstr)
775  {
776  mbstr += pg_mblen(mbstr);
777  len++;
778  }
779  return len;
780 }
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
int pg_mblen(const char *mbstr)
Definition: mbutils.c:752

◆ pg_mbstrlen_with_len()

int pg_mbstrlen_with_len ( const char *  mbstr,
int  limit 
)

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

787 {
788  int len = 0;
789 
790  /* optimization for single byte encoding */
792  return limit;
793 
794  while (limit > 0 && *mbstr)
795  {
796  int l = pg_mblen(mbstr);
797 
798  limit -= l;
799  mbstr += l;
800  len++;
801  }
802  return len;
803 }
int pg_database_encoding_max_length(void)
Definition: wchar.c:1881
int pg_mblen(const char *mbstr)
Definition: mbutils.c:752

◆ pg_server_to_any()

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

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

627 {
628  if (len <= 0)
629  return unconstify(char *, s); /* empty string is always valid */
630 
633  return unconstify(char *, s); /* assume data is valid */
634 
636  {
637  /* No conversion is possible, but we must validate the result */
638  (void) pg_verify_mbstr(encoding, s, len, false);
639  return unconstify(char *, s);
640  }
641 
642  /* Fast path if we can use cached conversion function */
644  return perform_default_encoding_conversion(s, len, false);
645 
646  /* General case ... will not work outside transactions */
647  return (char *) pg_do_encoding_conversion((unsigned char *) unconstify(char *, s),
648  len,
650  encoding);
651 }
static char * perform_default_encoding_conversion(const char *src, int len, bool is_client_to_server)
Definition: mbutils.c:660
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 615 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().

616 {
617  return pg_server_to_any(s, len, ClientEncoding->encoding);
618 }
static const pg_enc2name * ClientEncoding
Definition: mbutils.c:73
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:626
pg_enc encoding
Definition: pg_wchar.h:329

◆ pg_wchar2mb()

int pg_wchar2mb ( const pg_wchar from,
char *  to 
)

Definition at line 730 of file mbutils.c.

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

731 {
732  return pg_wchar_table[DatabaseEncoding->encoding].wchar2mb_with_len(from, (unsigned char *) to, pg_wchar_strlen(from));
733 }
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 737 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().

738 {
739  return pg_wchar_table[DatabaseEncoding->encoding].wchar2mb_with_len(from, (unsigned char *) to, len);
740 }
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:996
List * lcons(void *datum, List *list)
Definition: list.c:453
#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:771
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:1031
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:996
#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 890 of file mbutils.c.

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

Referenced by CheckMyDatabase().

891 {
893  elog(ERROR, "invalid database encoding: %d", encoding);
894 
897 }
#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 900 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().

901 {
902  /* Some calls happen before we can elog()! */
904 
907 }
#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().