PostgreSQL Source Code  git master
pg_wchar.h File Reference
#include "port/simd.h"
Include dependency graph for pg_wchar.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pg_enc2name
 
struct  pg_enc2gettext
 
struct  pg_wchar_tbl
 
struct  pg_mb_radix_tree
 
struct  pg_utf_to_local_combined
 
struct  pg_local_to_utf_combined
 

Macros

#define MAX_MULTIBYTE_CHAR_LEN   4
 
#define SS2   0x8e /* single shift 2 (JIS0201) */
 
#define SS3   0x8f /* single shift 3 (JIS0212) */
 
#define ISSJISHEAD(c)   (((c) >= 0x81 && (c) <= 0x9f) || ((c) >= 0xe0 && (c) <= 0xfc))
 
#define ISSJISTAIL(c)   (((c) >= 0x40 && (c) <= 0x7e) || ((c) >= 0x80 && (c) <= 0xfc))
 
#define LC_ISO8859_1   0x81 /* ISO8859 Latin 1 */
 
#define LC_ISO8859_2   0x82 /* ISO8859 Latin 2 */
 
#define LC_ISO8859_3   0x83 /* ISO8859 Latin 3 */
 
#define LC_ISO8859_4   0x84 /* ISO8859 Latin 4 */
 
#define LC_TIS620   0x85 /* Thai (not supported yet) */
 
#define LC_ISO8859_7   0x86 /* Greek (not supported yet) */
 
#define LC_ISO8859_6   0x87 /* Arabic (not supported yet) */
 
#define LC_ISO8859_8   0x88 /* Hebrew (not supported yet) */
 
#define LC_JISX0201K   0x89 /* Japanese 1 byte kana */
 
#define LC_JISX0201R   0x8a /* Japanese 1 byte Roman */
 
#define LC_KOI8_R   0x8b /* Cyrillic KOI8-R */
 
#define LC_ISO8859_5   0x8c /* ISO8859 Cyrillic */
 
#define LC_ISO8859_9   0x8d /* ISO8859 Latin 5 (not supported yet) */
 
#define LC_ISO8859_15   0x8e /* ISO8859 Latin 15 (not supported yet) */
 
#define IS_LC1(c)   ((unsigned char)(c) >= 0x81 && (unsigned char)(c) <= 0x8d)
 
#define LC_JISX0208_1978   0x90 /* Japanese Kanji, old JIS (not supported) */
 
#define LC_GB2312_80   0x91 /* Chinese */
 
#define LC_JISX0208   0x92 /* Japanese Kanji (JIS X 0208) */
 
#define LC_KS5601   0x93 /* Korean */
 
#define LC_JISX0212   0x94 /* Japanese Kanji (JIS X 0212) */
 
#define LC_CNS11643_1   0x95 /* CNS 11643-1992 Plane 1 */
 
#define LC_CNS11643_2   0x96 /* CNS 11643-1992 Plane 2 */
 
#define LC_JISX0213_1
 
#define LC_BIG5_1
 
#define LC_BIG5_2
 
#define IS_LC2(c)   ((unsigned char)(c) >= 0x90 && (unsigned char)(c) <= 0x99)
 
#define LCPRV1_A   0x9a
 
#define LCPRV1_B   0x9b
 
#define IS_LCPRV1(c)   ((unsigned char)(c) == LCPRV1_A || (unsigned char)(c) == LCPRV1_B)
 
#define IS_LCPRV1_A_RANGE(c)    ((unsigned char)(c) >= 0xa0 && (unsigned char)(c) <= 0xdf)
 
#define IS_LCPRV1_B_RANGE(c)    ((unsigned char)(c) >= 0xe0 && (unsigned char)(c) <= 0xef)
 
#define LCPRV2_A   0x9c
 
#define LCPRV2_B   0x9d
 
#define IS_LCPRV2(c)   ((unsigned char)(c) == LCPRV2_A || (unsigned char)(c) == LCPRV2_B)
 
#define IS_LCPRV2_A_RANGE(c)    ((unsigned char)(c) >= 0xf0 && (unsigned char)(c) <= 0xf4)
 
#define IS_LCPRV2_B_RANGE(c)    ((unsigned char)(c) >= 0xf5 && (unsigned char)(c) <= 0xfe)
 
#define LC_SISHENG
 
#define LC_IPA
 
#define LC_VISCII_LOWER
 
#define LC_VISCII_UPPER
 
#define LC_ARABIC_DIGIT   0xa4 /* Arabic digit (not supported) */
 
#define LC_ARABIC_1_COLUMN   0xa5 /* Arabic 1-column (not supported) */
 
#define LC_ASCII_RIGHT_TO_LEFT
 
#define LC_LAO
 
#define LC_ARABIC_2_COLUMN   0xa8 /* Arabic 1-column (not supported) */
 
#define LC_INDIAN_1_COLUMN
 
#define LC_TIBETAN_1_COLUMN
 
#define LC_UNICODE_SUBSET_2
 
#define LC_UNICODE_SUBSET_3
 
#define LC_UNICODE_SUBSET
 
#define LC_ETHIOPIC   0xf5 /* Ethiopic characters (not supported) */
 
#define LC_CNS11643_3   0xf6 /* CNS 11643-1992 Plane 3 */
 
#define LC_CNS11643_4   0xf7 /* CNS 11643-1992 Plane 4 */
 
#define LC_CNS11643_5   0xf8 /* CNS 11643-1992 Plane 5 */
 
#define LC_CNS11643_6   0xf9 /* CNS 11643-1992 Plane 6 */
 
#define LC_CNS11643_7   0xfa /* CNS 11643-1992 Plane 7 */
 
#define LC_INDIAN_2_COLUMN
 
#define LC_TIBETAN   0xfc /* Tibetan (not supported) */
 
#define PG_ENCODING_BE_LAST   PG_KOI8U
 
#define PG_VALID_BE_ENCODING(_enc)    ((_enc) >= 0 && (_enc) <= PG_ENCODING_BE_LAST)
 
#define PG_ENCODING_IS_CLIENT_ONLY(_enc)    ((_enc) > PG_ENCODING_BE_LAST && (_enc) < _PG_LAST_ENCODING_)
 
#define PG_VALID_ENCODING(_enc)    ((_enc) >= 0 && (_enc) < _PG_LAST_ENCODING_)
 
#define PG_VALID_FE_ENCODING(_enc)   PG_VALID_ENCODING(_enc)
 
#define MAX_CONVERSION_GROWTH   4
 
#define MAX_CONVERSION_INPUT_LENGTH   16
 
#define MAX_UNICODE_EQUIVALENT_STRING   16
 
#define CHECK_ENCODING_CONVERSION_ARGS(srcencoding, destencoding)
 

Typedefs

typedef unsigned int pg_wchar
 
typedef enum pg_enc pg_enc
 
typedef struct pg_enc2name pg_enc2name
 
typedef struct pg_enc2gettext pg_enc2gettext
 
typedef int(* mb2wchar_with_len_converter) (const unsigned char *from, pg_wchar *to, int len)
 
typedef int(* wchar2mb_with_len_converter) (const pg_wchar *from, unsigned char *to, int len)
 
typedef int(* mblen_converter) (const unsigned char *mbstr)
 
typedef int(* mbdisplaylen_converter) (const unsigned char *mbstr)
 
typedef bool(* mbcharacter_incrementer) (unsigned char *mbstr, int len)
 
typedef int(* mbchar_verifier) (const unsigned char *mbstr, int len)
 
typedef int(* mbstr_verifier) (const unsigned char *mbstr, int len)
 
typedef uint32(* utf_local_conversion_func) (uint32 code)
 

Enumerations

enum  pg_enc {
  PG_SQL_ASCII = 0 , PG_EUC_JP , PG_EUC_CN , PG_EUC_KR ,
  PG_EUC_TW , PG_EUC_JIS_2004 , PG_UTF8 , PG_MULE_INTERNAL ,
  PG_LATIN1 , PG_LATIN2 , PG_LATIN3 , PG_LATIN4 ,
  PG_LATIN5 , PG_LATIN6 , PG_LATIN7 , PG_LATIN8 ,
  PG_LATIN9 , PG_LATIN10 , PG_WIN1256 , PG_WIN1258 ,
  PG_WIN866 , PG_WIN874 , PG_KOI8R , PG_WIN1251 ,
  PG_WIN1252 , PG_ISO_8859_5 , PG_ISO_8859_6 , PG_ISO_8859_7 ,
  PG_ISO_8859_8 , PG_WIN1250 , PG_WIN1253 , PG_WIN1254 ,
  PG_WIN1255 , PG_WIN1257 , PG_KOI8U , PG_SJIS ,
  PG_BIG5 , PG_GBK , PG_UHC , PG_GB18030 ,
  PG_JOHAB , PG_SHIFT_JIS_2004 , _PG_LAST_ENCODING_
}
 

Functions

static bool is_valid_unicode_codepoint (pg_wchar c)
 
static bool is_utf16_surrogate_first (pg_wchar c)
 
static bool is_utf16_surrogate_second (pg_wchar c)
 
static pg_wchar surrogate_pair_to_codepoint (pg_wchar first, pg_wchar second)
 
int pg_char_to_encoding (const char *name)
 
const char * pg_encoding_to_char (int encoding)
 
int pg_valid_server_encoding_id (int encoding)
 
int pg_encoding_mblen (int encoding, const char *mbstr)
 
int pg_encoding_mblen_bounded (int encoding, const char *mbstr)
 
int pg_encoding_dsplen (int encoding, const char *mbstr)
 
int pg_encoding_verifymbchar (int encoding, const char *mbstr, int len)
 
int pg_encoding_verifymbstr (int encoding, const char *mbstr, int len)
 
int pg_encoding_max_length (int encoding)
 
int pg_valid_client_encoding (const char *name)
 
int pg_valid_server_encoding (const char *name)
 
bool is_encoding_supported_by_icu (int encoding)
 
const char * get_encoding_name_for_icu (int encoding)
 
unsigned char * unicode_to_utf8 (pg_wchar c, unsigned char *utf8string)
 
pg_wchar utf8_to_unicode (const unsigned char *c)
 
bool pg_utf8_islegal (const unsigned char *source, int length)
 
int pg_utf_mblen (const unsigned char *s)
 
int pg_mule_mblen (const unsigned char *s)
 
int pg_mb2wchar (const char *from, pg_wchar *to)
 
int pg_mb2wchar_with_len (const char *from, pg_wchar *to, int len)
 
int pg_encoding_mb2wchar_with_len (int encoding, const char *from, pg_wchar *to, int len)
 
int pg_wchar2mb (const pg_wchar *from, char *to)
 
int pg_wchar2mb_with_len (const pg_wchar *from, char *to, int len)
 
int pg_encoding_wchar2mb_with_len (int encoding, const pg_wchar *from, char *to, int len)
 
int pg_char_and_wchar_strcmp (const char *s1, const pg_wchar *s2)
 
int pg_wchar_strncmp (const pg_wchar *s1, const pg_wchar *s2, size_t n)
 
int pg_char_and_wchar_strncmp (const char *s1, const pg_wchar *s2, size_t n)
 
size_t pg_wchar_strlen (const pg_wchar *str)
 
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)
 
int pg_database_encoding_max_length (void)
 
mbcharacter_incrementer pg_database_encoding_character_incrementer (void)
 
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)
 
void SetDatabaseEncoding (int encoding)
 
int GetDatabaseEncoding (void)
 
const char * GetDatabaseEncodingName (void)
 
void SetMessageEncoding (int encoding)
 
int GetMessageEncoding (void)
 
unsigned char * pg_do_encoding_conversion (unsigned char *src, int len, int src_encoding, int dest_encoding)
 
int pg_do_encoding_conversion_buf (Oid proc, int src_encoding, int dest_encoding, unsigned char *src, int srclen, unsigned char *dest, int destlen, bool noError)
 
char * pg_client_to_server (const char *s, int len)
 
char * pg_server_to_client (const char *s, int len)
 
char * pg_any_to_server (const char *s, int len, int encoding)
 
char * pg_server_to_any (const char *s, int len, int encoding)
 
void pg_unicode_to_server (pg_wchar c, unsigned char *s)
 
bool pg_unicode_to_server_noerror (pg_wchar c, unsigned char *s)
 
unsigned short BIG5toCNS (unsigned short big5, unsigned char *lc)
 
unsigned short CNStoBIG5 (unsigned short cns, unsigned char lc)
 
int UtfToLocal (const unsigned char *utf, int len, unsigned char *iso, const pg_mb_radix_tree *map, const pg_utf_to_local_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding, bool noError)
 
int LocalToUtf (const unsigned char *iso, int len, unsigned char *utf, const pg_mb_radix_tree *map, const pg_local_to_utf_combined *cmap, int cmapsize, utf_local_conversion_func conv_func, int encoding, bool noError)
 
bool pg_verifymbstr (const char *mbstr, int len, bool noError)
 
bool pg_verify_mbstr (int encoding, const char *mbstr, int len, bool noError)
 
int pg_verify_mbstr_len (int encoding, const char *mbstr, int len, bool noError)
 
void check_encoding_conversion_args (int src_encoding, int dest_encoding, int len, int expected_src_encoding, int expected_dest_encoding)
 
void report_invalid_encoding (int encoding, const char *mbstr, int len) pg_attribute_noreturn()
 
void report_untranslatable_char (int src_encoding, int dest_encoding, const char *mbstr, int len) pg_attribute_noreturn()
 
int local2local (const unsigned char *l, unsigned char *p, int len, int src_encoding, int dest_encoding, const unsigned char *tab, bool noError)
 
int latin2mic (const unsigned char *l, unsigned char *p, int len, int lc, int encoding, bool noError)
 
int mic2latin (const unsigned char *mic, unsigned char *p, int len, int lc, int encoding, bool noError)
 
int latin2mic_with_table (const unsigned char *l, unsigned char *p, int len, int lc, int encoding, const unsigned char *tab, bool noError)
 
int mic2latin_with_table (const unsigned char *mic, unsigned char *p, int len, int lc, int encoding, const unsigned char *tab, bool noError)
 
static bool is_valid_ascii (const unsigned char *s, int len)
 

Variables

PGDLLIMPORT const pg_enc2name pg_enc2name_tbl []
 
PGDLLIMPORT const pg_enc2gettext pg_enc2gettext_tbl []
 
PGDLLIMPORT const pg_wchar_tbl pg_wchar_table []
 

Macro Definition Documentation

◆ CHECK_ENCODING_CONVERSION_ARGS

#define CHECK_ENCODING_CONVERSION_ARGS (   srcencoding,
  destencoding 
)
Value:
(srcencoding), \
(destencoding))
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
void check_encoding_conversion_args(int src_encoding, int dest_encoding, int len, int expected_src_encoding, int expected_dest_encoding)
Definition: mbutils.c:1676

Definition at line 513 of file pg_wchar.h.

◆ IS_LC1

#define IS_LC1 (   c)    ((unsigned char)(c) >= 0x81 && (unsigned char)(c) <= 0x8d)

Definition at line 125 of file pg_wchar.h.

◆ IS_LC2

#define IS_LC2 (   c)    ((unsigned char)(c) >= 0x90 && (unsigned char)(c) <= 0x99)

Definition at line 143 of file pg_wchar.h.

◆ IS_LCPRV1

#define IS_LCPRV1 (   c)    ((unsigned char)(c) == LCPRV1_A || (unsigned char)(c) == LCPRV1_B)

Definition at line 151 of file pg_wchar.h.

◆ IS_LCPRV1_A_RANGE

#define IS_LCPRV1_A_RANGE (   c)     ((unsigned char)(c) >= 0xa0 && (unsigned char)(c) <= 0xdf)

Definition at line 152 of file pg_wchar.h.

◆ IS_LCPRV1_B_RANGE

#define IS_LCPRV1_B_RANGE (   c)     ((unsigned char)(c) >= 0xe0 && (unsigned char)(c) <= 0xef)

Definition at line 154 of file pg_wchar.h.

◆ IS_LCPRV2

#define IS_LCPRV2 (   c)    ((unsigned char)(c) == LCPRV2_A || (unsigned char)(c) == LCPRV2_B)

Definition at line 163 of file pg_wchar.h.

◆ IS_LCPRV2_A_RANGE

#define IS_LCPRV2_A_RANGE (   c)     ((unsigned char)(c) >= 0xf0 && (unsigned char)(c) <= 0xf4)

Definition at line 164 of file pg_wchar.h.

◆ IS_LCPRV2_B_RANGE

#define IS_LCPRV2_B_RANGE (   c)     ((unsigned char)(c) >= 0xf5 && (unsigned char)(c) <= 0xfe)

Definition at line 166 of file pg_wchar.h.

◆ ISSJISHEAD

#define ISSJISHEAD (   c)    (((c) >= 0x81 && (c) <= 0x9f) || ((c) >= 0xe0 && (c) <= 0xfc))

Definition at line 43 of file pg_wchar.h.

◆ ISSJISTAIL

#define ISSJISTAIL (   c)    (((c) >= 0x40 && (c) <= 0x7e) || ((c) >= 0x80 && (c) <= 0xfc))

Definition at line 44 of file pg_wchar.h.

◆ LC_ARABIC_1_COLUMN

#define LC_ARABIC_1_COLUMN   0xa5 /* Arabic 1-column (not supported) */

Definition at line 177 of file pg_wchar.h.

◆ LC_ARABIC_2_COLUMN

#define LC_ARABIC_2_COLUMN   0xa8 /* Arabic 1-column (not supported) */

Definition at line 180 of file pg_wchar.h.

◆ LC_ARABIC_DIGIT

#define LC_ARABIC_DIGIT   0xa4 /* Arabic digit (not supported) */

Definition at line 176 of file pg_wchar.h.

◆ LC_ASCII_RIGHT_TO_LEFT

#define LC_ASCII_RIGHT_TO_LEFT
Value:
0xa6 /* ASCII (left half of ISO8859-1) with
* right-to-left direction (not
* supported) */

Definition at line 178 of file pg_wchar.h.

◆ LC_BIG5_1

#define LC_BIG5_1
Value:
0x98 /* Plane 1 Chinese traditional (not
* supported) */

Definition at line 139 of file pg_wchar.h.

◆ LC_BIG5_2

#define LC_BIG5_2
Value:
0x99 /* Plane 1 Chinese traditional (not
* supported) */

Definition at line 140 of file pg_wchar.h.

◆ LC_CNS11643_1

#define LC_CNS11643_1   0x95 /* CNS 11643-1992 Plane 1 */

Definition at line 136 of file pg_wchar.h.

◆ LC_CNS11643_2

#define LC_CNS11643_2   0x96 /* CNS 11643-1992 Plane 2 */

Definition at line 137 of file pg_wchar.h.

◆ LC_CNS11643_3

#define LC_CNS11643_3   0xf6 /* CNS 11643-1992 Plane 3 */

Definition at line 191 of file pg_wchar.h.

◆ LC_CNS11643_4

#define LC_CNS11643_4   0xf7 /* CNS 11643-1992 Plane 4 */

Definition at line 192 of file pg_wchar.h.

◆ LC_CNS11643_5

#define LC_CNS11643_5   0xf8 /* CNS 11643-1992 Plane 5 */

Definition at line 193 of file pg_wchar.h.

◆ LC_CNS11643_6

#define LC_CNS11643_6   0xf9 /* CNS 11643-1992 Plane 6 */

Definition at line 194 of file pg_wchar.h.

◆ LC_CNS11643_7

#define LC_CNS11643_7   0xfa /* CNS 11643-1992 Plane 7 */

Definition at line 195 of file pg_wchar.h.

◆ LC_ETHIOPIC

#define LC_ETHIOPIC   0xf5 /* Ethiopic characters (not supported) */

Definition at line 190 of file pg_wchar.h.

◆ LC_GB2312_80

#define LC_GB2312_80   0x91 /* Chinese */

Definition at line 132 of file pg_wchar.h.

◆ LC_INDIAN_1_COLUMN

#define LC_INDIAN_1_COLUMN
Value:
0xf0 /* Indian charset for 1-column width
* glyphs (not supported) */

Definition at line 185 of file pg_wchar.h.

◆ LC_INDIAN_2_COLUMN

#define LC_INDIAN_2_COLUMN
Value:
0xfb /* Indian charset for 2-column width
* glyphs (not supported) */

Definition at line 196 of file pg_wchar.h.

◆ LC_IPA

#define LC_IPA
Value:
0xa1 /* IPA (International Phonetic
* Association) (not supported) */

Definition at line 173 of file pg_wchar.h.

◆ LC_ISO8859_1

#define LC_ISO8859_1   0x81 /* ISO8859 Latin 1 */

Definition at line 104 of file pg_wchar.h.

◆ LC_ISO8859_15

#define LC_ISO8859_15   0x8e /* ISO8859 Latin 15 (not supported yet) */

Definition at line 121 of file pg_wchar.h.

◆ LC_ISO8859_2

#define LC_ISO8859_2   0x82 /* ISO8859 Latin 2 */

Definition at line 105 of file pg_wchar.h.

◆ LC_ISO8859_3

#define LC_ISO8859_3   0x83 /* ISO8859 Latin 3 */

Definition at line 106 of file pg_wchar.h.

◆ LC_ISO8859_4

#define LC_ISO8859_4   0x84 /* ISO8859 Latin 4 */

Definition at line 107 of file pg_wchar.h.

◆ LC_ISO8859_5

#define LC_ISO8859_5   0x8c /* ISO8859 Cyrillic */

Definition at line 119 of file pg_wchar.h.

◆ LC_ISO8859_6

#define LC_ISO8859_6   0x87 /* Arabic (not supported yet) */

Definition at line 110 of file pg_wchar.h.

◆ LC_ISO8859_7

#define LC_ISO8859_7   0x86 /* Greek (not supported yet) */

Definition at line 109 of file pg_wchar.h.

◆ LC_ISO8859_8

#define LC_ISO8859_8   0x88 /* Hebrew (not supported yet) */

Definition at line 111 of file pg_wchar.h.

◆ LC_ISO8859_9

#define LC_ISO8859_9   0x8d /* ISO8859 Latin 5 (not supported yet) */

Definition at line 120 of file pg_wchar.h.

◆ LC_JISX0201K

#define LC_JISX0201K   0x89 /* Japanese 1 byte kana */

Definition at line 112 of file pg_wchar.h.

◆ LC_JISX0201R

#define LC_JISX0201R   0x8a /* Japanese 1 byte Roman */

Definition at line 113 of file pg_wchar.h.

◆ LC_JISX0208

#define LC_JISX0208   0x92 /* Japanese Kanji (JIS X 0208) */

Definition at line 133 of file pg_wchar.h.

◆ LC_JISX0208_1978

#define LC_JISX0208_1978   0x90 /* Japanese Kanji, old JIS (not supported) */

Definition at line 131 of file pg_wchar.h.

◆ LC_JISX0212

#define LC_JISX0212   0x94 /* Japanese Kanji (JIS X 0212) */

Definition at line 135 of file pg_wchar.h.

◆ LC_JISX0213_1

#define LC_JISX0213_1
Value:
0x97 /* Japanese Kanji (JIS X 0213 Plane 1)
* (not supported) */

Definition at line 138 of file pg_wchar.h.

◆ LC_KOI8_R

#define LC_KOI8_R   0x8b /* Cyrillic KOI8-R */

Definition at line 118 of file pg_wchar.h.

◆ LC_KS5601

#define LC_KS5601   0x93 /* Korean */

Definition at line 134 of file pg_wchar.h.

◆ LC_LAO

#define LC_LAO
Value:
0xa7 /* Lao characters (ISO10646 0E80..0EDF)
* (not supported) */

Definition at line 179 of file pg_wchar.h.

◆ LC_SISHENG

#define LC_SISHENG
Value:
0xa0 /* Chinese SiSheng characters for
* PinYin/ZhuYin (not supported) */

Definition at line 172 of file pg_wchar.h.

◆ LC_TIBETAN

#define LC_TIBETAN   0xfc /* Tibetan (not supported) */

Definition at line 197 of file pg_wchar.h.

◆ LC_TIBETAN_1_COLUMN

#define LC_TIBETAN_1_COLUMN
Value:
0xf1 /* Tibetan 1-column width glyphs (not
* supported) */

Definition at line 186 of file pg_wchar.h.

◆ LC_TIS620

#define LC_TIS620   0x85 /* Thai (not supported yet) */

Definition at line 108 of file pg_wchar.h.

◆ LC_UNICODE_SUBSET

#define LC_UNICODE_SUBSET
Value:
0xf4 /* Unicode characters of the range
* U+0100..U+24FF. (not supported) */

Definition at line 189 of file pg_wchar.h.

◆ LC_UNICODE_SUBSET_2

#define LC_UNICODE_SUBSET_2
Value:
0xf2 /* Unicode characters of the range
* U+2500..U+33FF. (not supported) */

Definition at line 187 of file pg_wchar.h.

◆ LC_UNICODE_SUBSET_3

#define LC_UNICODE_SUBSET_3
Value:
0xf3 /* Unicode characters of the range
* U+E000..U+FFFF. (not supported) */

Definition at line 188 of file pg_wchar.h.

◆ LC_VISCII_LOWER

#define LC_VISCII_LOWER
Value:
0xa2 /* Vietnamese VISCII1.1 lower-case (not
* supported) */

Definition at line 174 of file pg_wchar.h.

◆ LC_VISCII_UPPER

#define LC_VISCII_UPPER
Value:
0xa3 /* Vietnamese VISCII1.1 upper-case (not
* supported) */

Definition at line 175 of file pg_wchar.h.

◆ LCPRV1_A

#define LCPRV1_A   0x9a

Definition at line 149 of file pg_wchar.h.

◆ LCPRV1_B

#define LCPRV1_B   0x9b

Definition at line 150 of file pg_wchar.h.

◆ LCPRV2_A

#define LCPRV2_A   0x9c

Definition at line 161 of file pg_wchar.h.

◆ LCPRV2_B

#define LCPRV2_B   0x9d

Definition at line 162 of file pg_wchar.h.

◆ MAX_CONVERSION_GROWTH

#define MAX_CONVERSION_GROWTH   4

Definition at line 302 of file pg_wchar.h.

◆ MAX_CONVERSION_INPUT_LENGTH

#define MAX_CONVERSION_INPUT_LENGTH   16

Definition at line 320 of file pg_wchar.h.

◆ MAX_MULTIBYTE_CHAR_LEN

#define MAX_MULTIBYTE_CHAR_LEN   4

Definition at line 32 of file pg_wchar.h.

◆ MAX_UNICODE_EQUIVALENT_STRING

#define MAX_UNICODE_EQUIVALENT_STRING   16

Definition at line 329 of file pg_wchar.h.

◆ PG_ENCODING_BE_LAST

#define PG_ENCODING_BE_LAST   PG_KOI8U

Definition at line 275 of file pg_wchar.h.

◆ PG_ENCODING_IS_CLIENT_ONLY

#define PG_ENCODING_IS_CLIENT_ONLY (   _enc)     ((_enc) > PG_ENCODING_BE_LAST && (_enc) < _PG_LAST_ENCODING_)

Definition at line 284 of file pg_wchar.h.

◆ PG_VALID_BE_ENCODING

#define PG_VALID_BE_ENCODING (   _enc)     ((_enc) >= 0 && (_enc) <= PG_ENCODING_BE_LAST)

Definition at line 281 of file pg_wchar.h.

◆ PG_VALID_ENCODING

#define PG_VALID_ENCODING (   _enc)     ((_enc) >= 0 && (_enc) < _PG_LAST_ENCODING_)

Definition at line 287 of file pg_wchar.h.

◆ PG_VALID_FE_ENCODING

#define PG_VALID_FE_ENCODING (   _enc)    PG_VALID_ENCODING(_enc)

Definition at line 291 of file pg_wchar.h.

◆ SS2

#define SS2   0x8e /* single shift 2 (JIS0201) */

Definition at line 37 of file pg_wchar.h.

◆ SS3

#define SS3   0x8f /* single shift 3 (JIS0212) */

Definition at line 38 of file pg_wchar.h.

Typedef Documentation

◆ mb2wchar_with_len_converter

typedef int(* mb2wchar_with_len_converter) (const unsigned char *from, pg_wchar *to, int len)

Definition at line 364 of file pg_wchar.h.

◆ mbchar_verifier

typedef int(* mbchar_verifier) (const unsigned char *mbstr, int len)

Definition at line 378 of file pg_wchar.h.

◆ mbcharacter_incrementer

typedef bool(* mbcharacter_incrementer) (unsigned char *mbstr, int len)

Definition at line 376 of file pg_wchar.h.

◆ mbdisplaylen_converter

typedef int(* mbdisplaylen_converter) (const unsigned char *mbstr)

Definition at line 374 of file pg_wchar.h.

◆ mblen_converter

typedef int(* mblen_converter) (const unsigned char *mbstr)

Definition at line 372 of file pg_wchar.h.

◆ mbstr_verifier

typedef int(* mbstr_verifier) (const unsigned char *mbstr, int len)

Definition at line 380 of file pg_wchar.h.

◆ pg_enc

typedef enum pg_enc pg_enc

◆ pg_enc2gettext

◆ pg_enc2name

typedef struct pg_enc2name pg_enc2name

◆ pg_wchar

typedef unsigned int pg_wchar

Definition at line 27 of file pg_wchar.h.

◆ utf_local_conversion_func

typedef uint32(* utf_local_conversion_func) (uint32 code)

Definition at line 505 of file pg_wchar.h.

◆ wchar2mb_with_len_converter

typedef int(* wchar2mb_with_len_converter) (const pg_wchar *from, unsigned char *to, int len)

Definition at line 368 of file pg_wchar.h.

Enumeration Type Documentation

◆ pg_enc

enum pg_enc
Enumerator
PG_SQL_ASCII 
PG_EUC_JP 
PG_EUC_CN 
PG_EUC_KR 
PG_EUC_TW 
PG_EUC_JIS_2004 
PG_UTF8 
PG_MULE_INTERNAL 
PG_LATIN1 
PG_LATIN2 
PG_LATIN3 
PG_LATIN4 
PG_LATIN5 
PG_LATIN6 
PG_LATIN7 
PG_LATIN8 
PG_LATIN9 
PG_LATIN10 
PG_WIN1256 
PG_WIN1258 
PG_WIN866 
PG_WIN874 
PG_KOI8R 
PG_WIN1251 
PG_WIN1252 
PG_ISO_8859_5 
PG_ISO_8859_6 
PG_ISO_8859_7 
PG_ISO_8859_8 
PG_WIN1250 
PG_WIN1253 
PG_WIN1254 
PG_WIN1255 
PG_WIN1257 
PG_KOI8U 
PG_SJIS 
PG_BIG5 
PG_GBK 
PG_UHC 
PG_GB18030 
PG_JOHAB 
PG_SHIFT_JIS_2004 
_PG_LAST_ENCODING_ 

Definition at line 224 of file pg_wchar.h.

226  : the order of this enum must be same as order of entries
227  * in the pg_enc2name_tbl[] array (in src/common/encnames.c), and
228  * in the pg_wchar_table[] array (in src/common/wchar.c)!
229  *
230  * If you add some encoding don't forget to check
231  * PG_ENCODING_BE_LAST macro.
232  *
233  * PG_SQL_ASCII is default encoding and must be = 0.
234  *
235  * XXX We must avoid renumbering any backend encoding until libpq's major
236  * version number is increased beyond 5; it turns out that the backend
237  * encoding IDs are effectively part of libpq's ABI as far as 8.2 initdb and
238  * psql are concerned.
239  */
240 typedef enum pg_enc
241 {
242  PG_SQL_ASCII = 0, /* SQL/ASCII */
243  PG_EUC_JP, /* EUC for Japanese */
244  PG_EUC_CN, /* EUC for Chinese */
245  PG_EUC_KR, /* EUC for Korean */
246  PG_EUC_TW, /* EUC for Taiwan */
247  PG_EUC_JIS_2004, /* EUC-JIS-2004 */
248  PG_UTF8, /* Unicode UTF8 */
249  PG_MULE_INTERNAL, /* Mule internal code */
250  PG_LATIN1, /* ISO-8859-1 Latin 1 */
251  PG_LATIN2, /* ISO-8859-2 Latin 2 */
252  PG_LATIN3, /* ISO-8859-3 Latin 3 */
253  PG_LATIN4, /* ISO-8859-4 Latin 4 */
254  PG_LATIN5, /* ISO-8859-9 Latin 5 */
255  PG_LATIN6, /* ISO-8859-10 Latin6 */
256  PG_LATIN7, /* ISO-8859-13 Latin7 */
257  PG_LATIN8, /* ISO-8859-14 Latin8 */
258  PG_LATIN9, /* ISO-8859-15 Latin9 */
259  PG_LATIN10, /* ISO-8859-16 Latin10 */
260  PG_WIN1256, /* windows-1256 */
261  PG_WIN1258, /* Windows-1258 */
262  PG_WIN866, /* (MS-DOS CP866) */
263  PG_WIN874, /* windows-874 */
264  PG_KOI8R, /* KOI8-R */
265  PG_WIN1251, /* windows-1251 */
266  PG_WIN1252, /* windows-1252 */
267  PG_ISO_8859_5, /* ISO-8859-5 */
268  PG_ISO_8859_6, /* ISO-8859-6 */
269  PG_ISO_8859_7, /* ISO-8859-7 */
270  PG_ISO_8859_8, /* ISO-8859-8 */
271  PG_WIN1250, /* windows-1250 */
272  PG_WIN1253, /* windows-1253 */
273  PG_WIN1254, /* windows-1254 */
int32 encoding
Definition: pg_database.h:41
PGDLLIMPORT const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
PGDLLIMPORT const pg_wchar_tbl pg_wchar_table[]
Definition: wchar.c:2076

Function Documentation

◆ BIG5toCNS()

unsigned short BIG5toCNS ( unsigned short  big5,
unsigned char *  lc 
)

Definition at line 292 of file big5.c.

293 {
294  unsigned short cns = 0;
295  int i;
296 
297  if (big5 < 0xc940U)
298  {
299  /* level 1 */
300 
301  for (i = 0; i < sizeof(b1c4) / (sizeof(unsigned short) * 2); i++)
302  {
303  if (b1c4[i][0] == big5)
304  {
305  *lc = LC_CNS11643_4;
306  return (b1c4[i][1] | 0x8080U);
307  }
308  }
309 
310  if (0 < (cns = BinarySearchRange(big5Level1ToCnsPlane1, 23, big5)))
311  *lc = LC_CNS11643_1;
312  }
313  else if (big5 == 0xc94aU)
314  {
315  /* level 2 */
316  *lc = LC_CNS11643_1;
317  cns = 0x4442;
318  }
319  else
320  {
321  /* level 2 */
322  for (i = 0; i < sizeof(b2c3) / (sizeof(unsigned short) * 2); i++)
323  {
324  if (b2c3[i][0] == big5)
325  {
326  *lc = LC_CNS11643_3;
327  return (b2c3[i][1] | 0x8080U);
328  }
329  }
330 
331  if (0 < (cns = BinarySearchRange(big5Level2ToCnsPlane2, 46, big5)))
332  *lc = LC_CNS11643_2;
333  }
334 
335  if (0 == cns)
336  { /* no mapping Big5 to CNS 11643-1992 */
337  *lc = 0;
338  return (unsigned short) '?';
339  }
340 
341  return cns | 0x8080;
342 }
static const codes_t big5Level1ToCnsPlane1[25]
Definition: big5.c:25
static const codes_t big5Level2ToCnsPlane2[48]
Definition: big5.c:84
static unsigned short BinarySearchRange(const codes_t *array, int high, unsigned short code)
Definition: big5.c:208
static const unsigned short b2c3[][2]
Definition: big5.c:197
static const unsigned short b1c4[][2]
Definition: big5.c:189
int i
Definition: isn.c:73
#define LC_CNS11643_3
Definition: pg_wchar.h:191
#define LC_CNS11643_1
Definition: pg_wchar.h:136
#define LC_CNS11643_4
Definition: pg_wchar.h:192
#define LC_CNS11643_2
Definition: pg_wchar.h:137

References b1c4, b2c3, big5Level1ToCnsPlane1, big5Level2ToCnsPlane2, BinarySearchRange(), i, LC_CNS11643_1, LC_CNS11643_2, LC_CNS11643_3, and LC_CNS11643_4.

Referenced by big52euc_tw(), and big52mic().

◆ check_encoding_conversion_args()

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

Definition at line 1676 of file mbutils.c.

1681 {
1682  if (!PG_VALID_ENCODING(src_encoding))
1683  elog(ERROR, "invalid source encoding ID: %d", src_encoding);
1684  if (src_encoding != expected_src_encoding && expected_src_encoding >= 0)
1685  elog(ERROR, "expected source encoding \"%s\", but got \"%s\"",
1686  pg_enc2name_tbl[expected_src_encoding].name,
1687  pg_enc2name_tbl[src_encoding].name);
1688  if (!PG_VALID_ENCODING(dest_encoding))
1689  elog(ERROR, "invalid destination encoding ID: %d", dest_encoding);
1690  if (dest_encoding != expected_dest_encoding && expected_dest_encoding >= 0)
1691  elog(ERROR, "expected destination encoding \"%s\", but got \"%s\"",
1692  pg_enc2name_tbl[expected_dest_encoding].name,
1693  pg_enc2name_tbl[dest_encoding].name);
1694  if (len < 0)
1695  elog(ERROR, "encoding conversion length must not be negative");
1696 }
#define ERROR
Definition: elog.h:39
const pg_enc2name pg_enc2name_tbl[]
Definition: encnames.c:309
const void size_t len
#define PG_VALID_ENCODING(_enc)
Definition: pg_wchar.h:287
const char * name

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

◆ CNStoBIG5()

unsigned short CNStoBIG5 ( unsigned short  cns,
unsigned char  lc 
)

Definition at line 345 of file big5.c.

346 {
347  int i;
348  unsigned int big5 = 0;
349 
350  cns &= 0x7f7f;
351 
352  switch (lc)
353  {
354  case LC_CNS11643_1:
355  big5 = BinarySearchRange(cnsPlane1ToBig5Level1, 24, cns);
356  break;
357  case LC_CNS11643_2:
358  big5 = BinarySearchRange(cnsPlane2ToBig5Level2, 47, cns);
359  break;
360  case LC_CNS11643_3:
361  for (i = 0; i < sizeof(b2c3) / (sizeof(unsigned short) * 2); i++)
362  {
363  if (b2c3[i][1] == cns)
364  return b2c3[i][0];
365  }
366  break;
367  case LC_CNS11643_4:
368  for (i = 0; i < sizeof(b1c4) / (sizeof(unsigned short) * 2); i++)
369  {
370  if (b1c4[i][1] == cns)
371  return b1c4[i][0];
372  }
373  default:
374  break;
375  }
376  return big5;
377 }
static const codes_t cnsPlane2ToBig5Level2[49]
Definition: big5.c:136
static const codes_t cnsPlane1ToBig5Level1[26]
Definition: big5.c:54

References b1c4, b2c3, BinarySearchRange(), cnsPlane1ToBig5Level1, cnsPlane2ToBig5Level2, i, LC_CNS11643_1, LC_CNS11643_2, LC_CNS11643_3, and LC_CNS11643_4.

Referenced by euc_tw2big5(), and mic2big5().

◆ get_encoding_name_for_icu()

const char* get_encoding_name_for_icu ( int  encoding)

Definition at line 473 of file encnames.c.

474 {
476  return NULL;
477  return pg_enc2icu_tbl[encoding];
478 }
static const char *const pg_enc2icu_tbl[]
Definition: encnames.c:415
#define PG_VALID_BE_ENCODING(_enc)
Definition: pg_wchar.h:281

References encoding, pg_enc2icu_tbl, and PG_VALID_BE_ENCODING.

◆ GetDatabaseEncoding()

int GetDatabaseEncoding ( void  )

◆ GetDatabaseEncodingName()

◆ GetMessageEncoding()

int GetMessageEncoding ( void  )

Definition at line 1315 of file mbutils.c.

1316 {
1317  return MessageEncoding->encoding;
1318 }
static const pg_enc2name * MessageEncoding
Definition: mbutils.c:83

References pg_enc2name::encoding, and MessageEncoding.

◆ InitializeClientEncoding()

void InitializeClientEncoding ( void  )

Definition at line 282 of file mbutils.c.

283 {
284  int current_server_encoding;
285 
288 
291  {
292  /*
293  * Oops, the requested conversion is not available. We couldn't fail
294  * before, but we can now.
295  */
296  ereport(FATAL,
297  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
298  errmsg("conversion between %s and %s is not supported",
301  }
302 
303  /*
304  * Also look up the UTF8-to-server conversion function if needed. Since
305  * the server encoding is fixed within any one backend process, we don't
306  * have to do this more than once.
307  */
308  current_server_encoding = GetDatabaseEncoding();
309  if (current_server_encoding != PG_UTF8 &&
310  current_server_encoding != PG_SQL_ASCII)
311  {
312  Oid utf8_to_server_proc;
313 
315  utf8_to_server_proc =
317  current_server_encoding);
318  /* If there's no such conversion, just leave the pointer as NULL */
319  if (OidIsValid(utf8_to_server_proc))
320  {
321  FmgrInfo *finfo;
322 
324  sizeof(FmgrInfo));
325  fmgr_info_cxt(utf8_to_server_proc, finfo,
327  /* Set Utf8ToServerConvProc only after data is fully valid */
328  Utf8ToServerConvProc = finfo;
329  }
330  }
331 }
#define OidIsValid(objectId)
Definition: c.h:764
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define FATAL
Definition: elog.h:41
#define ereport(elevel,...)
Definition: elog.h:149
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
Assert(fmt[strlen(fmt) - 1] !='\n')
int GetDatabaseEncoding(void)
Definition: mbutils.c:1268
static FmgrInfo * Utf8ToServerConvProc
Definition: mbutils.c:76
int SetClientEncoding(int encoding)
Definition: mbutils.c:209
int PrepareClientEncoding(int encoding)
Definition: mbutils.c:111
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1274
static bool backend_startup_complete
Definition: mbutils.c:91
static int pending_client_encoding
Definition: mbutils.c:92
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1021
Oid FindDefaultConversionProc(int32 for_encoding, int32 to_encoding)
Definition: namespace.c:3612
@ PG_SQL_ASCII
Definition: pg_wchar.h:226
@ PG_UTF8
Definition: pg_wchar.h:232
unsigned int Oid
Definition: postgres_ext.h:31
Definition: fmgr.h:57
bool IsTransactionState(void)
Definition: xact.c:378

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

Referenced by InitPostgres().

◆ is_encoding_supported_by_icu()

bool is_encoding_supported_by_icu ( int  encoding)

Definition at line 462 of file encnames.c.

463 {
465  return false;
466  return (pg_enc2icu_tbl[encoding] != NULL);
467 }

References encoding, pg_enc2icu_tbl, and PG_VALID_BE_ENCODING.

Referenced by check_icu_locale_encoding(), createdb(), DefineCollation(), and lookup_collation().

◆ is_utf16_surrogate_first()

static bool is_utf16_surrogate_first ( pg_wchar  c)
inlinestatic

Definition at line 531 of file pg_wchar.h.

542 {

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

◆ is_utf16_surrogate_second()

static bool is_utf16_surrogate_second ( pg_wchar  c)
inlinestatic

Definition at line 537 of file pg_wchar.h.

542 {

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

◆ is_valid_ascii()

static bool is_valid_ascii ( const unsigned char *  s,
int  len 
)
inlinestatic

Definition at line 697 of file pg_wchar.h.

714 {
715  const unsigned char *const s_end = s + len;
716  Vector8 chunk;
717  Vector8 highbit_cum = vector8_broadcast(0);
718 #ifdef USE_NO_SIMD
719  Vector8 zero_cum = vector8_broadcast(0x80);
720 #endif
721 
722  Assert(len % sizeof(chunk) == 0);
723 
724  while (s < s_end)
725  {
726  vector8_load(&chunk, s);
727 
728  /* Capture any zero bytes in this chunk. */
729 #ifdef USE_NO_SIMD
730 
731  /*
732  * First, add 0x7f to each byte. This sets the high bit in each byte,
733  * unless it was a zero. If any resulting high bits are zero, the
734  * corresponding high bits in the zero accumulator will be cleared.
735  *
736  * If none of the bytes in the chunk had the high bit set, the max
737  * value each byte can have after the addition is 0x7f + 0x7f = 0xfe,
738  * and we don't need to worry about carrying over to the next byte. If
739  * any input bytes did have the high bit set, it doesn't matter
740  * because we check for those separately.
741  */
742  zero_cum &= (chunk + vector8_broadcast(0x7F));
743 #else
744 
745  /*
746  * Set all bits in each lane of the highbit accumulator where input
747  * bytes are zero.
748  */
749  highbit_cum = vector8_or(highbit_cum,
750  vector8_eq(chunk, vector8_broadcast(0)));
751 #endif
752 
753  /* Capture all set bits in this chunk. */
754  highbit_cum = vector8_or(highbit_cum, chunk);
static Vector8 vector8_broadcast(const uint8 c)
Definition: simd.h:133
static void vector8_load(Vector8 *v, const uint8 *s)
Definition: simd.h:106
static Vector8 vector8_or(const Vector8 v1, const Vector8 v2)
Definition: simd.h:306
uint64 Vector8
Definition: simd.h:60

Referenced by pg_utf8_verifystr().

◆ is_valid_unicode_codepoint()

static bool is_valid_unicode_codepoint ( pg_wchar  c)
inlinestatic

Definition at line 525 of file pg_wchar.h.

542 {

Referenced by check_unicode_value(), pg_unicode_to_server(), pg_unicode_to_server_noerror(), and unistr().

◆ latin2mic()

int latin2mic ( const unsigned char *  l,
unsigned char *  p,
int  len,
int  lc,
int  encoding,
bool  noError 
)

Definition at line 89 of file conv.c.

91 {
92  const unsigned char *start = l;
93  int c1;
94 
95  while (len > 0)
96  {
97  c1 = *l;
98  if (c1 == 0)
99  {
100  if (noError)
101  break;
102  report_invalid_encoding(encoding, (const char *) l, len);
103  }
104  if (IS_HIGHBIT_SET(c1))
105  *p++ = lc;
106  *p++ = c1;
107  l++;
108  len--;
109  }
110  *p = '\0';
111 
112  return l - start;
113 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1168
void report_invalid_encoding(int encoding, const char *mbstr, int len)
Definition: mbutils.c:1705

References encoding, IS_HIGHBIT_SET, len, and report_invalid_encoding().

Referenced by koi8r_to_mic(), latin1_to_mic(), latin2_to_mic(), latin3_to_mic(), and latin4_to_mic().

◆ latin2mic_with_table()

int latin2mic_with_table ( const unsigned char *  l,
unsigned char *  p,
int  len,
int  lc,
int  encoding,
const unsigned char *  tab,
bool  noError 
)

Definition at line 194 of file conv.c.

201 {
202  const unsigned char *start = l;
203  unsigned char c1,
204  c2;
205 
206  while (len > 0)
207  {
208  c1 = *l;
209  if (c1 == 0)
210  {
211  if (noError)
212  break;
213  report_invalid_encoding(encoding, (const char *) l, len);
214  }
215  if (!IS_HIGHBIT_SET(c1))
216  *p++ = c1;
217  else
218  {
219  c2 = tab[c1 - HIGHBIT];
220  if (c2)
221  {
222  *p++ = lc;
223  *p++ = c2;
224  }
225  else
226  {
227  if (noError)
228  break;
230  (const char *) l, len);
231  }
232  }
233  l++;
234  len--;
235  }
236  *p = '\0';
237 
238  return l - start;
239 }
#define HIGHBIT
Definition: c.h:1167
void report_untranslatable_char(int src_encoding, int dest_encoding, const char *mbstr, int len)
Definition: mbutils.c:1737
@ PG_MULE_INTERNAL
Definition: pg_wchar.h:233

References encoding, HIGHBIT, IS_HIGHBIT_SET, len, PG_MULE_INTERNAL, report_invalid_encoding(), and report_untranslatable_char().

Referenced by iso_to_mic(), win1250_to_mic(), win1251_to_mic(), and win866_to_mic().

◆ local2local()

int local2local ( const unsigned char *  l,
unsigned char *  p,
int  len,
int  src_encoding,
int  dest_encoding,
const unsigned char *  tab,
bool  noError 
)

Definition at line 33 of file conv.c.

40 {
41  const unsigned char *start = l;
42  unsigned char c1,
43  c2;
44 
45  while (len > 0)
46  {
47  c1 = *l;
48  if (c1 == 0)
49  {
50  if (noError)
51  break;
52  report_invalid_encoding(src_encoding, (const char *) l, len);
53  }
54  if (!IS_HIGHBIT_SET(c1))
55  *p++ = c1;
56  else
57  {
58  c2 = tab[c1 - HIGHBIT];
59  if (c2)
60  *p++ = c2;
61  else
62  {
63  if (noError)
64  break;
65  report_untranslatable_char(src_encoding, dest_encoding,
66  (const char *) l, len);
67  }
68  }
69  l++;
70  len--;
71  }
72  *p = '\0';
73 
74  return l - start;
75 }

References HIGHBIT, IS_HIGHBIT_SET, len, report_invalid_encoding(), and report_untranslatable_char().

Referenced by iso_to_koi8r(), iso_to_win1251(), iso_to_win866(), koi8r_to_iso(), koi8r_to_win1251(), koi8r_to_win866(), latin2_to_win1250(), win1250_to_latin2(), win1251_to_iso(), win1251_to_koi8r(), win1251_to_win866(), win866_to_iso(), win866_to_koi8r(), and win866_to_win1251().

◆ LocalToUtf()

int LocalToUtf ( const unsigned char *  iso,
int  len,
unsigned char *  utf,
const pg_mb_radix_tree map,
const pg_local_to_utf_combined cmap,
int  cmapsize,
utf_local_conversion_func  conv_func,
int  encoding,
bool  noError 
)

Definition at line 717 of file conv.c.

724 {
725  uint32 iiso;
726  int l;
727  const pg_local_to_utf_combined *cp;
728  const unsigned char *start = iso;
729 
731  ereport(ERROR,
732  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
733  errmsg("invalid encoding number: %d", encoding)));
734 
735  for (; len > 0; len -= l)
736  {
737  unsigned char b1 = 0;
738  unsigned char b2 = 0;
739  unsigned char b3 = 0;
740  unsigned char b4 = 0;
741 
742  /* "break" cases all represent errors */
743  if (*iso == '\0')
744  break;
745 
746  if (!IS_HIGHBIT_SET(*iso))
747  {
748  /* ASCII case is easy, assume it's one-to-one conversion */
749  *utf++ = *iso++;
750  l = 1;
751  continue;
752  }
753 
754  l = pg_encoding_verifymbchar(encoding, (const char *) iso, len);
755  if (l < 0)
756  break;
757 
758  /* collect coded char of length l */
759  if (l == 1)
760  b4 = *iso++;
761  else if (l == 2)
762  {
763  b3 = *iso++;
764  b4 = *iso++;
765  }
766  else if (l == 3)
767  {
768  b2 = *iso++;
769  b3 = *iso++;
770  b4 = *iso++;
771  }
772  else if (l == 4)
773  {
774  b1 = *iso++;
775  b2 = *iso++;
776  b3 = *iso++;
777  b4 = *iso++;
778  }
779  else
780  {
781  elog(ERROR, "unsupported character length %d", l);
782  iiso = 0; /* keep compiler quiet */
783  }
784  iiso = (b1 << 24 | b2 << 16 | b3 << 8 | b4);
785 
786  if (map)
787  {
788  uint32 converted = pg_mb_radix_conv(map, l, b1, b2, b3, b4);
789 
790  if (converted)
791  {
792  utf = store_coded_char(utf, converted);
793  continue;
794  }
795 
796  /* If there's a combined character map, try that */
797  if (cmap)
798  {
799  cp = bsearch(&iiso, cmap, cmapsize,
801 
802  if (cp)
803  {
804  utf = store_coded_char(utf, cp->utf1);
805  utf = store_coded_char(utf, cp->utf2);
806  continue;
807  }
808  }
809  }
810 
811  /* if there's a conversion function, try that */
812  if (conv_func)
813  {
814  uint32 converted = (*conv_func) (iiso);
815 
816  if (converted)
817  {
818  utf = store_coded_char(utf, converted);
819  continue;
820  }
821  }
822 
823  /* failed to translate this character */
824  iso -= l;
825  if (noError)
826  break;
828  (const char *) iso, len);
829  }
830 
831  /* if we broke out of loop early, must be invalid input */
832  if (len > 0 && !noError)
833  report_invalid_encoding(encoding, (const char *) iso, len);
834 
835  *utf = '\0';
836 
837  return iso - start;
838 }
unsigned int uint32
Definition: c.h:495
static uint32 pg_mb_radix_conv(const pg_mb_radix_tree *rt, int l, unsigned char b1, unsigned char b2, unsigned char b3, unsigned char b4)
Definition: conv.c:373
static int compare4(const void *p1, const void *p2)
Definition: conv.c:339
static unsigned char * store_coded_char(unsigned char *dest, uint32 code)
Definition: conv.c:353
int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len)
Definition: wchar.c:2164

References compare4(), elog(), encoding, ereport, errcode(), errmsg(), ERROR, IS_HIGHBIT_SET, len, pg_encoding_verifymbchar(), pg_mb_radix_conv(), PG_UTF8, PG_VALID_ENCODING, report_invalid_encoding(), report_untranslatable_char(), store_coded_char(), pg_local_to_utf_combined::utf1, and pg_local_to_utf_combined::utf2.

Referenced by big5_to_utf8(), euc_cn_to_utf8(), euc_jis_2004_to_utf8(), euc_jp_to_utf8(), euc_kr_to_utf8(), euc_tw_to_utf8(), gb18030_to_utf8(), gbk_to_utf8(), iso8859_to_utf8(), johab_to_utf8(), koi8r_to_utf8(), koi8u_to_utf8(), shift_jis_2004_to_utf8(), sjis_to_utf8(), uhc_to_utf8(), and win_to_utf8().

◆ mic2latin()

int mic2latin ( const unsigned char *  mic,
unsigned char *  p,
int  len,
int  lc,
int  encoding,
bool  noError 
)

Definition at line 127 of file conv.c.

129 {
130  const unsigned char *start = mic;
131  int c1;
132 
133  while (len > 0)
134  {
135  c1 = *mic;
136  if (c1 == 0)
137  {
138  if (noError)
139  break;
140  report_invalid_encoding(PG_MULE_INTERNAL, (const char *) mic, len);
141  }
142  if (!IS_HIGHBIT_SET(c1))
143  {
144  /* easy for ASCII */
145  *p++ = c1;
146  mic++;
147  len--;
148  }
149  else
150  {
151  int l = pg_mule_mblen(mic);
152 
153  if (len < l)
154  {
155  if (noError)
156  break;
157  report_invalid_encoding(PG_MULE_INTERNAL, (const char *) mic,
158  len);
159  }
160  if (l != 2 || c1 != lc || !IS_HIGHBIT_SET(mic[1]))
161  {
162  if (noError)
163  break;
165  (const char *) mic, len);
166  }
167  *p++ = mic[1];
168  mic += 2;
169  len -= 2;
170  }
171  }
172  *p = '\0';
173 
174  return mic - start;
175 }
int pg_mule_mblen(const unsigned char *s)
Definition: wchar.c:832

References encoding, IS_HIGHBIT_SET, len, PG_MULE_INTERNAL, pg_mule_mblen(), report_invalid_encoding(), and report_untranslatable_char().

Referenced by mic_to_koi8r(), mic_to_latin1(), mic_to_latin2(), mic_to_latin3(), and mic_to_latin4().

◆ mic2latin_with_table()

int mic2latin_with_table ( const unsigned char *  mic,
unsigned char *  p,
int  len,
int  lc,
int  encoding,
const unsigned char *  tab,
bool  noError 
)

Definition at line 257 of file conv.c.

264 {
265  const unsigned char *start = mic;
266  unsigned char c1,
267  c2;
268 
269  while (len > 0)
270  {
271  c1 = *mic;
272  if (c1 == 0)
273  {
274  if (noError)
275  break;
276  report_invalid_encoding(PG_MULE_INTERNAL, (const char *) mic, len);
277  }
278  if (!IS_HIGHBIT_SET(c1))
279  {
280  /* easy for ASCII */
281  *p++ = c1;
282  mic++;
283  len--;
284  }
285  else
286  {
287  int l = pg_mule_mblen(mic);
288 
289  if (len < l)
290  {
291  if (noError)
292  break;
293  report_invalid_encoding(PG_MULE_INTERNAL, (const char *) mic,
294  len);
295  }
296  if (l != 2 || c1 != lc || !IS_HIGHBIT_SET(mic[1]) ||
297  (c2 = tab[mic[1] - HIGHBIT]) == 0)
298  {
299  if (noError)
300  break;
302  (const char *) mic, len);
303  break; /* keep compiler quiet */
304  }
305  *p++ = c2;
306  mic += 2;
307  len -= 2;
308  }
309  }
310  *p = '\0';
311 
312  return mic - start;
313 }

References encoding, HIGHBIT, IS_HIGHBIT_SET, len, PG_MULE_INTERNAL, pg_mule_mblen(), report_invalid_encoding(), and report_untranslatable_char().

Referenced by mic_to_iso(), mic_to_win1250(), mic_to_win1251(), and mic_to_win866().

◆ pg_any_to_server()

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

Definition at line 677 of file mbutils.c.

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

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

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

◆ pg_char_and_wchar_strcmp()

int pg_char_and_wchar_strcmp ( const char *  s1,
const pg_wchar s2 
)

Definition at line 41 of file wstrcmp.c.

42 {
43  while ((pg_wchar) *s1 == *s2++)
44  if (*s1++ == 0)
45  return 0;
46  return *(const unsigned char *) s1 - *(const pg_wchar *) (s2 - 1);
47 }
unsigned int pg_wchar
Definition: mbprint.c:31
char * s1
char * s2

References s1, and s2.

◆ pg_char_and_wchar_strncmp()

int pg_char_and_wchar_strncmp ( const char *  s1,
const pg_wchar s2,
size_t  n 
)

Definition at line 55 of file wstrncmp.c.

56 {
57  if (n == 0)
58  return 0;
59  do
60  {
61  if ((pg_wchar) ((unsigned char) *s1) != *s2++)
62  return ((pg_wchar) ((unsigned char) *s1) - *(s2 - 1));
63  if (*s1++ == 0)
64  break;
65  } while (--n != 0);
66  return 0;
67 }

References s1, and s2.

Referenced by element().

◆ pg_char_to_encoding()

int pg_char_to_encoding ( const char *  name)

Definition at line 550 of file encnames.c.

551 {
552  unsigned int nel = lengthof(pg_encname_tbl);
553  const pg_encname *base = pg_encname_tbl,
554  *last = base + nel - 1,
555  *position;
556  int result;
557  char buff[NAMEDATALEN],
558  *key;
559 
560  if (name == NULL || *name == '\0')
561  return -1;
562 
563  if (strlen(name) >= NAMEDATALEN)
564  return -1; /* it's certainly not in the table */
565 
566  key = clean_encoding_name(name, buff);
567 
568  while (last >= base)
569  {
570  position = base + ((last - base) >> 1);
571  result = key[0] - position->name[0];
572 
573  if (result == 0)
574  {
575  result = strcmp(key, position->name);
576  if (result == 0)
577  return position->encoding;
578  }
579  if (result < 0)
580  last = position - 1;
581  else
582  base = position + 1;
583  }
584  return -1;
585 }
#define lengthof(array)
Definition: c.h:777
static const pg_encname pg_encname_tbl[]
Definition: encnames.c:39
static char * clean_encoding_name(const char *key, char *newkey)
Definition: encnames.c:525
#define NAMEDATALEN

◆ pg_client_to_server()

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

Definition at line 661 of file mbutils.c.

662 {
664 }
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:677

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

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

◆ pg_database_encoding_character_incrementer()

mbcharacter_incrementer pg_database_encoding_character_incrementer ( void  )

Definition at line 1530 of file mbutils.c.

1531 {
1532  /*
1533  * Eventually it might be best to add a field to pg_wchar_table[], but for
1534  * now we just use a switch.
1535  */
1536  switch (GetDatabaseEncoding())
1537  {
1538  case PG_UTF8:
1539  return pg_utf8_increment;
1540 
1541  case PG_EUC_JP:
1542  return pg_eucjp_increment;
1543 
1544  default:
1545  return pg_generic_charinc;
1546  }
1547 }
static bool pg_generic_charinc(unsigned char *charptr, int len)
Definition: mbutils.c:1332
static bool pg_utf8_increment(unsigned char *charptr, int length)
Definition: mbutils.c:1366
static bool pg_eucjp_increment(unsigned char *charptr, int length)
Definition: mbutils.c:1444
@ PG_EUC_JP
Definition: pg_wchar.h:227

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

Referenced by make_greater_string().

◆ pg_database_encoding_max_length()

◆ pg_do_encoding_conversion()

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

Definition at line 357 of file mbutils.c.

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

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

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

◆ pg_do_encoding_conversion_buf()

int pg_do_encoding_conversion_buf ( Oid  proc,
int  src_encoding,
int  dest_encoding,
unsigned char *  src,
int  srclen,
unsigned char *  dest,
int  destlen,
bool  noError 
)

Definition at line 470 of file mbutils.c.

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

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

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

◆ pg_dsplen()

int pg_dsplen ( const char *  mbstr)

Definition at line 1031 of file mbutils.c.

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

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

Referenced by p_isspecial().

◆ pg_encoding_dsplen()

int pg_encoding_dsplen ( int  encoding,
const char *  mbstr 
)

Definition at line 2151 of file wchar.c.

2152 {
2153  return (PG_VALID_ENCODING(encoding) ?
2154  pg_wchar_table[encoding].dsplen((const unsigned char *) mbstr) :
2155  pg_wchar_table[PG_SQL_ASCII].dsplen((const unsigned char *) mbstr));
2156 }

References encoding, PG_SQL_ASCII, PG_VALID_ENCODING, and pg_wchar_table.

Referenced by PQdsplen(), and reportErrorPosition().

◆ pg_encoding_max_length()

int pg_encoding_max_length ( int  encoding)

◆ pg_encoding_mb2wchar_with_len()

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

Definition at line 994 of file mbutils.c.

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

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

◆ pg_encoding_mbcliplen()

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

Definition at line 1094 of file mbutils.c.

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

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

Referenced by pg_mbcliplen().

◆ pg_encoding_mblen()

int pg_encoding_mblen ( int  encoding,
const char *  mbstr 
)

Definition at line 2130 of file wchar.c.

2131 {
2132  return (PG_VALID_ENCODING(encoding) ?
2133  pg_wchar_table[encoding].mblen((const unsigned char *) mbstr) :
2134  pg_wchar_table[PG_SQL_ASCII].mblen((const unsigned char *) mbstr));
2135 }

References encoding, PG_SQL_ASCII, PG_VALID_ENCODING, and pg_wchar_table.

Referenced by CopyAttributeOutCSV(), CopyAttributeOutText(), pg_encoding_mblen_bounded(), PQescapeInternal(), PQescapeStringInternal(), PQmblen(), PQmblenBounded(), report_invalid_encoding(), and report_untranslatable_char().

◆ pg_encoding_mblen_bounded()

int pg_encoding_mblen_bounded ( int  encoding,
const char *  mbstr 
)

Definition at line 2142 of file wchar.c.

2143 {
2144  return strnlen(mbstr, pg_encoding_mblen(encoding, mbstr));
2145 }
size_t strnlen(const char *str, size_t maxlen)
Definition: strnlen.c:26
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:2130

References encoding, pg_encoding_mblen(), and strnlen().

◆ pg_encoding_to_char()

const char* pg_encoding_to_char ( int  encoding)

Definition at line 588 of file encnames.c.

589 {
591  {
592  const pg_enc2name *p = &pg_enc2name_tbl[encoding];
593 
594  Assert(encoding == p->encoding);
595  return p->name;
596  }
597  return "";
598 }

◆ pg_encoding_verifymbchar()

int pg_encoding_verifymbchar ( int  encoding,
const char *  mbstr,
int  len 
)

Definition at line 2164 of file wchar.c.

2165 {
2166  return (PG_VALID_ENCODING(encoding) ?
2167  pg_wchar_table[encoding].mbverifychar((const unsigned char *) mbstr, len) :
2168  pg_wchar_table[PG_SQL_ASCII].mbverifychar((const unsigned char *) mbstr, len));
2169 }

References encoding, len, PG_SQL_ASCII, PG_VALID_ENCODING, and pg_wchar_table.

Referenced by big52euc_tw(), big52mic(), euc_jis_20042shift_jis_2004(), euc_jp2mic(), euc_jp2sjis(), euc_kr2mic(), euc_tw2big5(), euc_tw2mic(), LocalToUtf(), mic2big5(), mic2euc_jp(), mic2euc_kr(), mic2euc_tw(), mic2sjis(), shift_jis_20042euc_jis_2004(), and sjis2euc_jp().

◆ pg_encoding_verifymbstr()

int pg_encoding_verifymbstr ( int  encoding,
const char *  mbstr,
int  len 
)

Definition at line 2177 of file wchar.c.

2178 {
2179  return (PG_VALID_ENCODING(encoding) ?
2180  pg_wchar_table[encoding].mbverifystr((const unsigned char *) mbstr, len) :
2181  pg_wchar_table[PG_SQL_ASCII].mbverifystr((const unsigned char *) mbstr, len));
2182 }

References encoding, len, PG_SQL_ASCII, PG_VALID_ENCODING, and pg_wchar_table.

Referenced by CopyConvertBuf(), and test_enc_conversion().

◆ pg_encoding_wchar2mb_with_len()

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

Definition at line 1016 of file mbutils.c.

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

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

◆ pg_get_client_encoding()

int pg_get_client_encoding ( void  )

Definition at line 337 of file mbutils.c.

338 {
339  return ClientEncoding->encoding;
340 }

References ClientEncoding, and pg_enc2name::encoding.

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

◆ pg_get_client_encoding_name()

const char* pg_get_client_encoding_name ( void  )

Definition at line 346 of file mbutils.c.

347 {
348  return ClientEncoding->name;
349 }

References ClientEncoding, and pg_enc2name::name.

◆ pg_mb2wchar()

int pg_mb2wchar ( const char *  from,
pg_wchar to 
)

Definition at line 980 of file mbutils.c.

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

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

◆ pg_mb2wchar_with_len()

◆ pg_mbcharcliplen()

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

Definition at line 1126 of file mbutils.c.

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

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

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

◆ pg_mbcliplen()

◆ pg_mblen()

int pg_mblen ( const char *  mbstr)

Definition at line 1024 of file mbutils.c.

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

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

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

◆ pg_mbstrlen()

int pg_mbstrlen ( const char *  mbstr)

Definition at line 1038 of file mbutils.c.

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

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

Referenced by NUM_processor(), and text_format_append_string().

◆ pg_mbstrlen_with_len()

int pg_mbstrlen_with_len ( const char *  mbstr,
int  limit 
)

Definition at line 1058 of file mbutils.c.

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

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

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

◆ pg_mule_mblen()

int pg_mule_mblen ( const unsigned char *  s)

Definition at line 832 of file wchar.c.

833 {
834  int len;
835 
836  if (IS_LC1(*s))
837  len = 2;
838  else if (IS_LCPRV1(*s))
839  len = 3;
840  else if (IS_LC2(*s))
841  len = 3;
842  else if (IS_LCPRV2(*s))
843  len = 4;
844  else
845  len = 1; /* assume ASCII */
846  return len;
847 }
#define IS_LCPRV2(c)
Definition: pg_wchar.h:163
#define IS_LC2(c)
Definition: pg_wchar.h:143
#define IS_LCPRV1(c)
Definition: pg_wchar.h:151
#define IS_LC1(c)
Definition: pg_wchar.h:125

References IS_LC1, IS_LC2, IS_LCPRV1, IS_LCPRV2, and len.

Referenced by mic2latin(), mic2latin_with_table(), and pg_mule_verifychar().

◆ pg_server_to_any()

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

Definition at line 750 of file mbutils.c.

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

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

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

◆ pg_server_to_client()

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

Definition at line 739 of file mbutils.c.

740 {
742 }
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:750

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

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

◆ pg_unicode_to_server()

void pg_unicode_to_server ( pg_wchar  c,
unsigned char *  s 
)

Definition at line 865 of file mbutils.c.

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

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

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

◆ pg_unicode_to_server_noerror()

bool pg_unicode_to_server_noerror ( pg_wchar  c,
unsigned char *  s 
)

Definition at line 927 of file mbutils.c.

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

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

Referenced by json_lex_string().

◆ pg_utf8_islegal()

bool pg_utf8_islegal ( const unsigned char *  source,
int  length 
)

Definition at line 2013 of file wchar.c.

2014 {
2015  unsigned char a;
2016 
2017  switch (length)
2018  {
2019  default:
2020  /* reject lengths 5 and 6 for now */
2021  return false;
2022  case 4:
2023  a = source[3];
2024  if (a < 0x80 || a > 0xBF)
2025  return false;
2026  /* FALL THRU */
2027  case 3:
2028  a = source[2];
2029  if (a < 0x80 || a > 0xBF)
2030  return false;
2031  /* FALL THRU */
2032  case 2:
2033  a = source[1];
2034  switch (*source)
2035  {
2036  case 0xE0:
2037  if (a < 0xA0 || a > 0xBF)
2038  return false;
2039  break;
2040  case 0xED:
2041  if (a < 0x80 || a > 0x9F)
2042  return false;
2043  break;
2044  case 0xF0:
2045  if (a < 0x90 || a > 0xBF)
2046  return false;
2047  break;
2048  case 0xF4:
2049  if (a < 0x80 || a > 0x8F)
2050  return false;
2051  break;
2052  default:
2053  if (a < 0x80 || a > 0xBF)
2054  return false;
2055  break;
2056  }
2057  /* FALL THRU */
2058  case 1:
2059  a = *source;
2060  if (a >= 0x80 && a < 0xC2)
2061  return false;
2062  if (a > 0xF4)
2063  return false;
2064  break;
2065  }
2066  return true;
2067 }
int a
Definition: isn.c:69
static rewind_source * source
Definition: pg_rewind.c:89

References a, and source.

Referenced by chr(), pg_utf8_string_len(), pg_utf8_verifychar(), utf8_to_iso8859_1(), and UtfToLocal().

◆ pg_utf_mblen()

int pg_utf_mblen ( const unsigned char *  s)

Definition at line 549 of file wchar.c.

550 {
551  int len;
552 
553  if ((*s & 0x80) == 0)
554  len = 1;
555  else if ((*s & 0xe0) == 0xc0)
556  len = 2;
557  else if ((*s & 0xf0) == 0xe0)
558  len = 3;
559  else if ((*s & 0xf8) == 0xf0)
560  len = 4;
561 #ifdef NOT_USED
562  else if ((*s & 0xfc) == 0xf8)
563  len = 5;
564  else if ((*s & 0xfe) == 0xfc)
565  len = 6;
566 #endif
567  else
568  len = 1;
569  return len;
570 }

References len.

Referenced by json_lex_string(), pg_saslprep(), pg_unicode_to_server(), pg_unicode_to_server_noerror(), pg_utf8_string_len(), pg_utf8_verifystr(), pg_wchar2utf_with_len(), read_char(), unicode_is_normalized(), unicode_normalize_func(), utf8_to_iso8859_1(), and UtfToLocal().

◆ pg_valid_client_encoding()

int pg_valid_client_encoding ( const char *  name)

Definition at line 486 of file encnames.c.

487 {
488  int enc;
489 
490  if ((enc = pg_char_to_encoding(name)) < 0)
491  return -1;
492 
494  return -1;
495 
496  return enc;
497 }
enc
int pg_char_to_encoding(const char *name)
Definition: encnames.c:550
#define PG_VALID_FE_ENCODING(_enc)
Definition: pg_wchar.h:291

References enc, name, pg_char_to_encoding(), and PG_VALID_FE_ENCODING.

Referenced by check_client_encoding().

◆ pg_valid_server_encoding()

int pg_valid_server_encoding ( const char *  name)

Definition at line 500 of file encnames.c.

501 {
502  int enc;
503 
504  if ((enc = pg_char_to_encoding(name)) < 0)
505  return -1;
506 
508  return -1;
509 
510  return enc;
511 }

References enc, name, pg_char_to_encoding(), and PG_VALID_BE_ENCODING.

Referenced by createdb(), get_encoding_id(), and parse_extension_control_file().

◆ pg_valid_server_encoding_id()

int pg_valid_server_encoding_id ( int  encoding)

Definition at line 514 of file encnames.c.

515 {
517 }

◆ pg_verify_mbstr()

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

Definition at line 1573 of file mbutils.c.

1574 {
1575  int oklen;
1576 
1578 
1579  oklen = pg_wchar_table[encoding].mbverifystr((const unsigned char *) mbstr, len);
1580  if (oklen != len)
1581  {
1582  if (noError)
1583  return false;
1584  report_invalid_encoding(encoding, mbstr + oklen, len - oklen);
1585  }
1586  return true;
1587 }
mbstr_verifier mbverifystr
Definition: pg_wchar.h:391

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

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

◆ pg_verify_mbstr_len()

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

Definition at line 1604 of file mbutils.c.

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

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

Referenced by length_in_encoding().

◆ pg_verifymbstr()

◆ pg_wchar2mb()

int pg_wchar2mb ( const pg_wchar from,
char *  to 
)

Definition at line 1002 of file mbutils.c.

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

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

◆ pg_wchar2mb_with_len()

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

◆ pg_wchar_strlen()

size_t pg_wchar_strlen ( const pg_wchar str)

Definition at line 70 of file wstrncmp.c.

71 {
72  const pg_wchar *s;
73 
74  for (s = str; *s; ++s)
75  ;
76  return (s - str);
77 }

References generate_unaccent_rules::str.

Referenced by pg_wchar2mb().

◆ pg_wchar_strncmp()

int pg_wchar_strncmp ( const pg_wchar s1,
const pg_wchar s2,
size_t  n 
)

Definition at line 40 of file wstrncmp.c.

41 {
42  if (n == 0)
43  return 0;
44  do
45  {
46  if (*s1 != *s2++)
47  return (*s1 - *(s2 - 1));
48  if (*s1++ == 0)
49  break;
50  } while (--n != 0);
51  return 0;
52 }

References s1, and s2.

◆ PrepareClientEncoding()

int PrepareClientEncoding ( int  encoding)

Definition at line 111 of file mbutils.c.

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

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

Referenced by check_client_encoding(), and InitializeClientEncoding().

◆ report_invalid_encoding()

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

Definition at line 1705 of file mbutils.c.

1706 {
1707  int l = pg_encoding_mblen(encoding, mbstr);
1708  char buf[8 * 5 + 1];
1709  char *p = buf;
1710  int j,
1711  jlimit;
1712 
1713  jlimit = Min(l, len);
1714  jlimit = Min(jlimit, 8); /* prevent buffer overrun */
1715 
1716  for (j = 0; j < jlimit; j++)
1717  {
1718  p += sprintf(p, "0x%02x", (unsigned char) mbstr[j]);
1719  if (j < jlimit - 1)
1720  p += sprintf(p, " ");
1721  }
1722 
1723  ereport(ERROR,
1724  (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
1725  errmsg("invalid byte sequence for encoding \"%s\": %s",
1727  buf)));
1728 }
#define Min(x, y)
Definition: c.h:993
int j
Definition: isn.c:74
static char * buf
Definition: pg_test_fsync.c:67
#define sprintf
Definition: port.h:240

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

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

◆ report_untranslatable_char()

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

Definition at line 1737 of file mbutils.c.

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

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

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

◆ SetClientEncoding()

int SetClientEncoding ( int  encoding)

Definition at line 209 of file mbutils.c.

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

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

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

◆ SetDatabaseEncoding()

void SetDatabaseEncoding ( int  encoding)

Definition at line 1162 of file mbutils.c.

1163 {
1165  elog(ERROR, "invalid database encoding: %d", encoding);
1166 
1169 }

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

Referenced by CheckMyDatabase().

◆ SetMessageEncoding()

void SetMessageEncoding ( int  encoding)

Definition at line 1172 of file mbutils.c.

1173 {
1174  /* Some calls happen before we can elog()! */
1176 
1179 }

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

Referenced by pg_perm_setlocale().

◆ surrogate_pair_to_codepoint()

static pg_wchar surrogate_pair_to_codepoint ( pg_wchar  first,
pg_wchar  second 
)
inlinestatic

Definition at line 543 of file pg_wchar.h.

548 {

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

◆ unicode_to_utf8()

unsigned char* unicode_to_utf8 ( pg_wchar  c,
unsigned char *  utf8string 
)

Definition at line 483 of file wchar.c.

484 {
485  if (c <= 0x7F)
486  {
487  utf8string[0] = c;
488  }
489  else if (c <= 0x7FF)
490  {
491  utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
492  utf8string[1] = 0x80 | (c & 0x3F);
493  }
494  else if (c <= 0xFFFF)
495  {
496  utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
497  utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
498  utf8string[2] = 0x80 | (c & 0x3F);
499  }
500  else
501  {
502  utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
503  utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
504  utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
505  utf8string[3] = 0x80 | (c & 0x3F);
506  }
507 
508  return utf8string;
509 }

Referenced by json_lex_string(), pg_saslprep(), pg_unicode_to_server(), pg_unicode_to_server_noerror(), pg_wchar2utf_with_len(), and unicode_normalize_func().

◆ utf8_to_unicode()

pg_wchar utf8_to_unicode ( const unsigned char *  c)

Definition at line 679 of file wchar.c.

680 {
681  if ((*c & 0x80) == 0)
682  return (pg_wchar) c[0];
683  else if ((*c & 0xe0) == 0xc0)
684  return (pg_wchar) (((c[0] & 0x1f) << 6) |
685  (c[1] & 0x3f));
686  else if ((*c & 0xf0) == 0xe0)
687  return (pg_wchar) (((c[0] & 0x0f) << 12) |
688  ((c[1] & 0x3f) << 6) |
689  (c[2] & 0x3f));
690  else if ((*c & 0xf8) == 0xf0)
691  return (pg_wchar) (((c[0] & 0x07) << 18) |
692  ((c[1] & 0x3f) << 12) |
693  ((c[2] & 0x3f) << 6) |
694  (c[3] & 0x3f));
695  else
696  /* that is an invalid code on purpose */
697  return 0xffffffff;
698 }

Referenced by pg_saslprep(), pg_utf_dsplen(), read_char(), unicode_is_normalized(), and unicode_normalize_func().

◆ UtfToLocal()

int UtfToLocal ( const unsigned char *  utf,
int  len,
unsigned char *  iso,
const pg_mb_radix_tree map,
const pg_utf_to_local_combined cmap,
int  cmapsize,
utf_local_conversion_func  conv_func,
int  encoding,
bool  noError 
)

Definition at line 507 of file conv.c.

513 {
514  uint32 iutf;
515  int l;
516  const pg_utf_to_local_combined *cp;
517  const unsigned char *start = utf;
518 
520  ereport(ERROR,
521  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
522  errmsg("invalid encoding number: %d", encoding)));
523 
524  for (; len > 0; len -= l)
525  {
526  unsigned char b1 = 0;
527  unsigned char b2 = 0;
528  unsigned char b3 = 0;
529  unsigned char b4 = 0;
530 
531  /* "break" cases all represent errors */
532  if (*utf == '\0')
533  break;
534 
535  l = pg_utf_mblen(utf);
536  if (len < l)
537  break;
538 
539  if (!pg_utf8_islegal(utf, l))
540  break;
541 
542  if (l == 1)
543  {
544  /* ASCII case is easy, assume it's one-to-one conversion */
545  *iso++ = *utf++;
546  continue;
547  }
548 
549  /* collect coded char of length l */
550  if (l == 2)
551  {
552  b3 = *utf++;
553  b4 = *utf++;
554  }
555  else if (l == 3)
556  {
557  b2 = *utf++;
558  b3 = *utf++;
559  b4 = *utf++;
560  }
561  else if (l == 4)
562  {
563  b1 = *utf++;
564  b2 = *utf++;
565  b3 = *utf++;
566  b4 = *utf++;
567  }
568  else
569  {
570  elog(ERROR, "unsupported character length %d", l);
571  iutf = 0; /* keep compiler quiet */
572  }
573  iutf = (b1 << 24 | b2 << 16 | b3 << 8 | b4);
574 
575  /* First, try with combined map if possible */
576  if (cmap && len > l)
577  {
578  const unsigned char *utf_save = utf;
579  int len_save = len;
580  int l_save = l;
581 
582  /* collect next character, same as above */
583  len -= l;
584 
585  l = pg_utf_mblen(utf);
586  if (len < l)
587  {
588  /* need more data to decide if this is a combined char */
589  utf -= l_save;
590  break;
591  }
592 
593  if (!pg_utf8_islegal(utf, l))
594  {
595  if (!noError)
596  report_invalid_encoding(PG_UTF8, (const char *) utf, len);
597  utf -= l_save;
598  break;
599  }
600 
601  /* We assume ASCII character cannot be in combined map */
602  if (l > 1)
603  {
604  uint32 iutf2;
605  uint32 cutf[2];
606 
607  if (l == 2)
608  {
609  iutf2 = *utf++ << 8;
610  iutf2 |= *utf++;
611  }
612  else if (l == 3)
613  {
614  iutf2 = *utf++ << 16;
615  iutf2 |= *utf++ << 8;
616  iutf2 |= *utf++;
617  }
618  else if (l == 4)
619  {
620  iutf2 = *utf++ << 24;
621  iutf2 |= *utf++ << 16;
622  iutf2 |= *utf++ << 8;
623  iutf2 |= *utf++;
624  }
625  else
626  {
627  elog(ERROR, "unsupported character length %d", l);
628  iutf2 = 0; /* keep compiler quiet */
629  }
630 
631  cutf[0] = iutf;
632  cutf[1] = iutf2;
633 
634  cp = bsearch(cutf, cmap, cmapsize,
636 
637  if (cp)
638  {
639  iso = store_coded_char(iso, cp->code);
640  continue;
641  }
642  }
643 
644  /* fail, so back up to reprocess second character next time */
645  utf = utf_save;
646  len = len_save;
647  l = l_save;
648  }
649 
650  /* Now check ordinary map */
651  if (map)
652  {
653  uint32 converted = pg_mb_radix_conv(map, l, b1, b2, b3, b4);
654 
655  if (converted)
656  {
657  iso = store_coded_char(iso, converted);
658  continue;
659  }
660  }
661 
662  /* if there's a conversion function, try that */
663  if (conv_func)
664  {
665  uint32 converted = (*conv_func) (iutf);
666 
667  if (converted)
668  {
669  iso = store_coded_char(iso, converted);
670  continue;
671  }
672  }
673 
674  /* failed to translate this character */
675  utf -= l;
676  if (noError)
677  break;
679  (const char *) utf, len);
680  }
681 
682  /* if we broke out of loop early, must be invalid input */
683  if (len > 0 && !noError)
684  report_invalid_encoding(PG_UTF8, (const char *) utf, len);
685 
686  *iso = '\0';
687 
688  return utf - start;
689 }
static int compare3(const void *p1, const void *p2)
Definition: conv.c:320
bool pg_utf8_islegal(const unsigned char *source, int length)
Definition: wchar.c:2013

References pg_utf_to_local_combined::code, compare3(), elog(), encoding, ereport, errcode(), errmsg(), ERROR, len, pg_mb_radix_conv(), PG_UTF8, pg_utf8_islegal(), pg_utf_mblen(), PG_VALID_ENCODING, report_invalid_encoding(), report_untranslatable_char(), and store_coded_char().

Referenced by utf8_to_big5(), utf8_to_euc_cn(), utf8_to_euc_jis_2004(), utf8_to_euc_jp(), utf8_to_euc_kr(), utf8_to_euc_tw(), utf8_to_gb18030(), utf8_to_gbk(), utf8_to_iso8859(), utf8_to_johab(), utf8_to_koi8r(), utf8_to_koi8u(), utf8_to_shift_jis_2004(), utf8_to_sjis(), utf8_to_uhc(), and utf8_to_win().

Variable Documentation

◆ pg_enc2gettext_tbl

PGDLLIMPORT const pg_enc2gettext pg_enc2gettext_tbl[]
extern

Definition at line 361 of file encnames.c.

◆ pg_enc2name_tbl

◆ pg_wchar_table