LCOV - code coverage report
Current view: top level - src/include/mb - pg_wchar.h (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 95.7 % 47 45
Test Date: 2026-07-25 22:15:46 Functions: 100.0 % 7 7
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 90.6 % 32 29

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * pg_wchar.h
       4                 :             :  *    multibyte-character support
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  * src/include/mb/pg_wchar.h
      10                 :             :  *
      11                 :             :  *  NOTES
      12                 :             :  *      This is used both by the backend and by frontends, but should not be
      13                 :             :  *      included by libpq client programs.  In particular, a libpq client
      14                 :             :  *      should not assume that the encoding IDs used by the version of libpq
      15                 :             :  *      it's linked to match up with the IDs declared here.
      16                 :             :  *      To help prevent mistakes, relevant functions that are exported by
      17                 :             :  *      libpq have a physically different name when being referenced
      18                 :             :  *      statically.
      19                 :             :  *
      20                 :             :  *-------------------------------------------------------------------------
      21                 :             :  */
      22                 :             : #ifndef PG_WCHAR_H
      23                 :             : #define PG_WCHAR_H
      24                 :             : 
      25                 :             : /*
      26                 :             :  * The pg_wchar type
      27                 :             :  */
      28                 :             : typedef unsigned int pg_wchar;
      29                 :             : 
      30                 :             : /*
      31                 :             :  * Maximum byte length of multibyte characters in any backend encoding
      32                 :             :  */
      33                 :             : #define MAX_MULTIBYTE_CHAR_LEN  4
      34                 :             : 
      35                 :             : /*
      36                 :             :  * various definitions for EUC
      37                 :             :  */
      38                 :             : #define SS2 0x8e                /* single shift 2 (JIS0201) */
      39                 :             : #define SS3 0x8f                /* single shift 3 (JIS0212) */
      40                 :             : 
      41                 :             : /*
      42                 :             :  * EUC_TW planes
      43                 :             :  */
      44                 :             : #define LC_CNS11643_1       0x95    /* CNS 11643-1992 Plane 1 */
      45                 :             : #define LC_CNS11643_2       0x96    /* CNS 11643-1992 Plane 2 */
      46                 :             : #define LC_CNS11643_3       0xf6    /* CNS 11643-1992 Plane 3 */
      47                 :             : #define LC_CNS11643_4       0xf7    /* CNS 11643-1992 Plane 4 */
      48                 :             : #define LC_CNS11643_5       0xf8    /* CNS 11643-1992 Plane 5 */
      49                 :             : #define LC_CNS11643_6       0xf9    /* CNS 11643-1992 Plane 6 */
      50                 :             : #define LC_CNS11643_7       0xfa    /* CNS 11643-1992 Plane 7 */
      51                 :             : 
      52                 :             : /*
      53                 :             :  * SJIS validation macros
      54                 :             :  */
      55                 :             : #define ISSJISHEAD(c) (((c) >= 0x81 && (c) <= 0x9f) || ((c) >= 0xe0 && (c) <= 0xfc))
      56                 :             : #define ISSJISTAIL(c) (((c) >= 0x40 && (c) <= 0x7e) || ((c) >= 0x80 && (c) <= 0xfc))
      57                 :             : 
      58                 :             : /*
      59                 :             :  * PostgreSQL encoding identifiers
      60                 :             :  *
      61                 :             :  * WARNING: If you add some encoding don't forget to update
      62                 :             :  *          the pg_enc2name_tbl[] array (in src/common/encnames.c),
      63                 :             :  *          the pg_enc2gettext_tbl[] array (in src/common/encnames.c) and
      64                 :             :  *          the pg_wchar_table[] array (in src/common/wchar.c) and to check
      65                 :             :  *          PG_ENCODING_BE_LAST macro.
      66                 :             :  *
      67                 :             :  * PG_SQL_ASCII is default encoding and must be = 0.
      68                 :             :  *
      69                 :             :  * XXX  We must avoid renumbering any backend encoding until libpq's major
      70                 :             :  * version number is increased beyond 5; it turns out that the backend
      71                 :             :  * encoding IDs are effectively part of libpq's ABI as far as 8.2 initdb and
      72                 :             :  * psql are concerned.
      73                 :             :  */
      74                 :             : typedef enum pg_enc
      75                 :             : {
      76                 :             :     PG_SQL_ASCII = 0,           /* SQL/ASCII */
      77                 :             :     PG_EUC_JP,                  /* EUC for Japanese */
      78                 :             :     PG_EUC_CN,                  /* EUC for Chinese */
      79                 :             :     PG_EUC_KR,                  /* EUC for Korean */
      80                 :             :     PG_EUC_TW,                  /* EUC for Taiwan */
      81                 :             :     PG_EUC_JIS_2004,            /* EUC-JIS-2004 */
      82                 :             :     PG_UTF8,                    /* Unicode UTF8 */
      83                 :             :     PG_UNUSED_1,                /* (Was Mule internal code) */
      84                 :             :     PG_LATIN1,                  /* ISO-8859-1 Latin 1 */
      85                 :             :     PG_LATIN2,                  /* ISO-8859-2 Latin 2 */
      86                 :             :     PG_LATIN3,                  /* ISO-8859-3 Latin 3 */
      87                 :             :     PG_LATIN4,                  /* ISO-8859-4 Latin 4 */
      88                 :             :     PG_LATIN5,                  /* ISO-8859-9 Latin 5 */
      89                 :             :     PG_LATIN6,                  /* ISO-8859-10 Latin6 */
      90                 :             :     PG_LATIN7,                  /* ISO-8859-13 Latin7 */
      91                 :             :     PG_LATIN8,                  /* ISO-8859-14 Latin8 */
      92                 :             :     PG_LATIN9,                  /* ISO-8859-15 Latin9 */
      93                 :             :     PG_LATIN10,                 /* ISO-8859-16 Latin10 */
      94                 :             :     PG_WIN1256,                 /* windows-1256 */
      95                 :             :     PG_WIN1258,                 /* Windows-1258 */
      96                 :             :     PG_WIN866,                  /* (MS-DOS CP866) */
      97                 :             :     PG_WIN874,                  /* windows-874 */
      98                 :             :     PG_KOI8R,                   /* KOI8-R */
      99                 :             :     PG_WIN1251,                 /* windows-1251 */
     100                 :             :     PG_WIN1252,                 /* windows-1252 */
     101                 :             :     PG_ISO_8859_5,              /* ISO-8859-5 */
     102                 :             :     PG_ISO_8859_6,              /* ISO-8859-6 */
     103                 :             :     PG_ISO_8859_7,              /* ISO-8859-7 */
     104                 :             :     PG_ISO_8859_8,              /* ISO-8859-8 */
     105                 :             :     PG_WIN1250,                 /* windows-1250 */
     106                 :             :     PG_WIN1253,                 /* windows-1253 */
     107                 :             :     PG_WIN1254,                 /* windows-1254 */
     108                 :             :     PG_WIN1255,                 /* windows-1255 */
     109                 :             :     PG_WIN1257,                 /* windows-1257 */
     110                 :             :     PG_KOI8U,                   /* KOI8-U */
     111                 :             :     /* PG_ENCODING_BE_LAST points to the above entry */
     112                 :             : 
     113                 :             :     /* followings are for client encoding only */
     114                 :             :     PG_SJIS,                    /* Shift JIS (Windows-932) */
     115                 :             :     PG_BIG5,                    /* Big5 (Windows-950) */
     116                 :             :     PG_GBK,                     /* GBK (Windows-936) */
     117                 :             :     PG_UHC,                     /* UHC (Windows-949) */
     118                 :             :     PG_GB18030,                 /* GB18030 */
     119                 :             :     PG_JOHAB,                   /* EUC for Korean JOHAB */
     120                 :             :     PG_SHIFT_JIS_2004,          /* Shift-JIS-2004 */
     121                 :             :     _PG_LAST_ENCODING_          /* mark only */
     122                 :             : 
     123                 :             : } pg_enc;
     124                 :             : 
     125                 :             : #define PG_ENCODING_BE_LAST PG_KOI8U
     126                 :             : 
     127                 :             : #define PG_UNUSED_ENCODING(_enc) \
     128                 :             :     ((_enc) == PG_UNUSED_1)
     129                 :             : 
     130                 :             : /*
     131                 :             :  * Please use these tests before access to pg_enc2name_tbl[]
     132                 :             :  * or to other places...
     133                 :             :  */
     134                 :             : #define PG_VALID_BE_ENCODING(_enc) \
     135                 :             :         ((_enc) >= 0 && (_enc) <= PG_ENCODING_BE_LAST && !PG_UNUSED_ENCODING(_enc))
     136                 :             : 
     137                 :             : #define PG_ENCODING_IS_CLIENT_ONLY(_enc) \
     138                 :             :         ((_enc) > PG_ENCODING_BE_LAST && (_enc) < _PG_LAST_ENCODING_)
     139                 :             : 
     140                 :             : #define PG_VALID_ENCODING(_enc) \
     141                 :             :         ((_enc) >= 0 && (_enc) < _PG_LAST_ENCODING_ && !PG_UNUSED_ENCODING(_enc))
     142                 :             : 
     143                 :             : /* On FE are possible all encodings */
     144                 :             : #define PG_VALID_FE_ENCODING(_enc)  PG_VALID_ENCODING(_enc)
     145                 :             : 
     146                 :             : /*
     147                 :             :  * When converting strings between different encodings, we assume that space
     148                 :             :  * for converted result is 4-to-1 growth in the worst case.  The rate for
     149                 :             :  * currently supported encoding pairs are within 3 (SJIS JIS X0201 half width
     150                 :             :  * kana -> UTF8 is the worst case).  So "4" should be enough for the moment.
     151                 :             :  *
     152                 :             :  * Note that this is not the same as the maximum character width in any
     153                 :             :  * particular encoding.
     154                 :             :  */
     155                 :             : #define MAX_CONVERSION_GROWTH  4
     156                 :             : 
     157                 :             : /*
     158                 :             :  * Maximum byte length of a string that's required in any encoding to convert
     159                 :             :  * at least one character to any other encoding.  In other words, if you feed
     160                 :             :  * MAX_CONVERSION_INPUT_LENGTH bytes to any encoding conversion function, it
     161                 :             :  * is guaranteed to be able to convert something without needing more input
     162                 :             :  * (assuming the input is valid).
     163                 :             :  *
     164                 :             :  * Currently, the maximum case is the conversion UTF8 -> SJIS JIS X0201 half
     165                 :             :  * width kana, where a pair of UTF-8 characters is converted into a single
     166                 :             :  * SHIFT_JIS_2004 character (the reverse of the worst case for
     167                 :             :  * MAX_CONVERSION_GROWTH).  It needs 6 bytes of input.  In theory, a
     168                 :             :  * user-defined conversion function might have more complicated cases, although
     169                 :             :  * for the reverse mapping you would probably also need to bump up
     170                 :             :  * MAX_CONVERSION_GROWTH.  But there is no need to be stingy here, so make it
     171                 :             :  * generous.
     172                 :             :  */
     173                 :             : #define MAX_CONVERSION_INPUT_LENGTH 16
     174                 :             : 
     175                 :             : /*
     176                 :             :  * Maximum byte length of the string equivalent to any one Unicode code point,
     177                 :             :  * in any backend encoding.  The current value assumes that a 4-byte UTF-8
     178                 :             :  * character might expand by MAX_CONVERSION_GROWTH, which is a huge
     179                 :             :  * overestimate.  But in current usage we don't allocate large multiples of
     180                 :             :  * this, so there's little point in being stingy.
     181                 :             :  */
     182                 :             : #define MAX_UNICODE_EQUIVALENT_STRING   16
     183                 :             : 
     184                 :             : /*
     185                 :             :  * Table for mapping an encoding number to official encoding name and
     186                 :             :  * possibly other subsidiary data.  Be careful to check encoding number
     187                 :             :  * before accessing a table entry!
     188                 :             :  *
     189                 :             :  * if (PG_VALID_ENCODING(encoding))
     190                 :             :  *      pg_enc2name_tbl[ encoding ];
     191                 :             :  */
     192                 :             : typedef struct pg_enc2name
     193                 :             : {
     194                 :             :     const char *name;
     195                 :             :     pg_enc      encoding;
     196                 :             : #ifdef WIN32
     197                 :             :     unsigned    codepage;       /* codepage for WIN32 */
     198                 :             : #endif
     199                 :             : } pg_enc2name;
     200                 :             : 
     201                 :             : extern PGDLLIMPORT const pg_enc2name pg_enc2name_tbl[];
     202                 :             : 
     203                 :             : /*
     204                 :             :  * Encoding names for gettext
     205                 :             :  */
     206                 :             : extern PGDLLIMPORT const char *pg_enc2gettext_tbl[];
     207                 :             : 
     208                 :             : /*
     209                 :             :  * pg_wchar stuff
     210                 :             :  */
     211                 :             : typedef int (*mb2wchar_with_len_converter) (const unsigned char *from,
     212                 :             :                                             pg_wchar *to,
     213                 :             :                                             int len);
     214                 :             : 
     215                 :             : typedef int (*wchar2mb_with_len_converter) (const pg_wchar *from,
     216                 :             :                                             unsigned char *to,
     217                 :             :                                             int len);
     218                 :             : 
     219                 :             : typedef int (*mblen_converter) (const unsigned char *mbstr);
     220                 :             : 
     221                 :             : typedef int (*mbdisplaylen_converter) (const unsigned char *mbstr);
     222                 :             : 
     223                 :             : typedef bool (*mbcharacter_incrementer) (unsigned char *mbstr, int len);
     224                 :             : 
     225                 :             : typedef int (*mbchar_verifier) (const unsigned char *mbstr, int len);
     226                 :             : 
     227                 :             : typedef int (*mbstr_verifier) (const unsigned char *mbstr, int len);
     228                 :             : 
     229                 :             : typedef struct
     230                 :             : {
     231                 :             :     mb2wchar_with_len_converter mb2wchar_with_len;  /* convert a multibyte
     232                 :             :                                                      * string to a wchar */
     233                 :             :     wchar2mb_with_len_converter wchar2mb_with_len;  /* convert a wchar string
     234                 :             :                                                      * to a multibyte */
     235                 :             :     mblen_converter mblen;      /* get byte length of a char */
     236                 :             :     mbdisplaylen_converter dsplen;  /* get display width of a char */
     237                 :             :     mbchar_verifier mbverifychar;   /* verify multibyte character */
     238                 :             :     mbstr_verifier mbverifystr; /* verify multibyte string */
     239                 :             :     int         maxmblen;       /* max bytes for a char in this encoding */
     240                 :             : } pg_wchar_tbl;
     241                 :             : 
     242                 :             : extern PGDLLIMPORT const pg_wchar_tbl pg_wchar_table[];
     243                 :             : 
     244                 :             : /*
     245                 :             :  * Data structures for conversions between UTF-8 and other encodings
     246                 :             :  * (UtfToLocal() and LocalToUtf()).  In these data structures, characters of
     247                 :             :  * either encoding are represented by uint32 words; hence we can only support
     248                 :             :  * characters up to 4 bytes long.  For example, the byte sequence 0xC2 0x89
     249                 :             :  * would be represented by 0x0000C289, and 0xE8 0xA2 0xB4 by 0x00E8A2B4.
     250                 :             :  *
     251                 :             :  * There are three possible ways a character can be mapped:
     252                 :             :  *
     253                 :             :  * 1. Using a radix tree, from source to destination code.
     254                 :             :  * 2. Using a sorted array of source -> destination code pairs. This
     255                 :             :  *    method is used for "combining" characters. There are so few of
     256                 :             :  *    them that building a radix tree would be wasteful.
     257                 :             :  * 3. Using a conversion function.
     258                 :             :  */
     259                 :             : 
     260                 :             : /*
     261                 :             :  * Radix tree for character conversion.
     262                 :             :  *
     263                 :             :  * Logically, this is actually four different radix trees, for 1-byte,
     264                 :             :  * 2-byte, 3-byte and 4-byte inputs. The 1-byte tree is a simple lookup
     265                 :             :  * table from source to target code. The 2-byte tree consists of two levels:
     266                 :             :  * one lookup table for the first byte, where the value in the lookup table
     267                 :             :  * points to a lookup table for the second byte. And so on.
     268                 :             :  *
     269                 :             :  * Physically, all the trees are stored in one big array, in 'chars16' or
     270                 :             :  * 'chars32', depending on the maximum value that needs to be represented. For
     271                 :             :  * each level in each tree, we also store lower and upper bound of allowed
     272                 :             :  * values - values outside those bounds are considered invalid, and are left
     273                 :             :  * out of the tables.
     274                 :             :  *
     275                 :             :  * In the intermediate levels of the trees, the values stored are offsets
     276                 :             :  * into the chars[16|32] array.
     277                 :             :  *
     278                 :             :  * In the beginning of the chars[16|32] array, there is always a number of
     279                 :             :  * zeros, so that you safely follow an index from an intermediate table
     280                 :             :  * without explicitly checking for a zero. Following a zero any number of
     281                 :             :  * times will always bring you to the dummy, all-zeros table in the
     282                 :             :  * beginning. This helps to shave some cycles when looking up values.
     283                 :             :  */
     284                 :             : typedef struct
     285                 :             : {
     286                 :             :     /*
     287                 :             :      * Array containing all the values. Only one of chars16 or chars32 is
     288                 :             :      * used, depending on how wide the values we need to represent are.
     289                 :             :      */
     290                 :             :     const uint16 *chars16;
     291                 :             :     const uint32 *chars32;
     292                 :             : 
     293                 :             :     /* Radix tree for 1-byte inputs */
     294                 :             :     uint32      b1root;         /* offset of table in the chars[16|32] array */
     295                 :             :     uint8       b1_lower;       /* min allowed value for a single byte input */
     296                 :             :     uint8       b1_upper;       /* max allowed value for a single byte input */
     297                 :             : 
     298                 :             :     /* Radix tree for 2-byte inputs */
     299                 :             :     uint32      b2root;         /* offset of 1st byte's table */
     300                 :             :     uint8       b2_1_lower;     /* min/max allowed value for 1st input byte */
     301                 :             :     uint8       b2_1_upper;
     302                 :             :     uint8       b2_2_lower;     /* min/max allowed value for 2nd input byte */
     303                 :             :     uint8       b2_2_upper;
     304                 :             : 
     305                 :             :     /* Radix tree for 3-byte inputs */
     306                 :             :     uint32      b3root;         /* offset of 1st byte's table */
     307                 :             :     uint8       b3_1_lower;     /* min/max allowed value for 1st input byte */
     308                 :             :     uint8       b3_1_upper;
     309                 :             :     uint8       b3_2_lower;     /* min/max allowed value for 2nd input byte */
     310                 :             :     uint8       b3_2_upper;
     311                 :             :     uint8       b3_3_lower;     /* min/max allowed value for 3rd input byte */
     312                 :             :     uint8       b3_3_upper;
     313                 :             : 
     314                 :             :     /* Radix tree for 4-byte inputs */
     315                 :             :     uint32      b4root;         /* offset of 1st byte's table */
     316                 :             :     uint8       b4_1_lower;     /* min/max allowed value for 1st input byte */
     317                 :             :     uint8       b4_1_upper;
     318                 :             :     uint8       b4_2_lower;     /* min/max allowed value for 2nd input byte */
     319                 :             :     uint8       b4_2_upper;
     320                 :             :     uint8       b4_3_lower;     /* min/max allowed value for 3rd input byte */
     321                 :             :     uint8       b4_3_upper;
     322                 :             :     uint8       b4_4_lower;     /* min/max allowed value for 4th input byte */
     323                 :             :     uint8       b4_4_upper;
     324                 :             : 
     325                 :             : } pg_mb_radix_tree;
     326                 :             : 
     327                 :             : /*
     328                 :             :  * UTF-8 to local code conversion map (for combined characters)
     329                 :             :  */
     330                 :             : typedef struct
     331                 :             : {
     332                 :             :     uint32      utf1;           /* UTF-8 code 1 */
     333                 :             :     uint32      utf2;           /* UTF-8 code 2 */
     334                 :             :     uint32      code;           /* local code */
     335                 :             : } pg_utf_to_local_combined;
     336                 :             : 
     337                 :             : /*
     338                 :             :  * local code to UTF-8 conversion map (for combined characters)
     339                 :             :  */
     340                 :             : typedef struct
     341                 :             : {
     342                 :             :     uint32      code;           /* local code */
     343                 :             :     uint32      utf1;           /* UTF-8 code 1 */
     344                 :             :     uint32      utf2;           /* UTF-8 code 2 */
     345                 :             : } pg_local_to_utf_combined;
     346                 :             : 
     347                 :             : /*
     348                 :             :  * callback function for algorithmic encoding conversions (in either direction)
     349                 :             :  *
     350                 :             :  * if function returns zero, it does not know how to convert the code
     351                 :             :  */
     352                 :             : typedef uint32 (*utf_local_conversion_func) (uint32 code);
     353                 :             : 
     354                 :             : /*
     355                 :             :  * Support macro for encoding conversion functions to validate their
     356                 :             :  * arguments.  (This could be made more compact if we included fmgr.h
     357                 :             :  * here, but we don't want to do that because this header file is also
     358                 :             :  * used by frontends.)
     359                 :             :  */
     360                 :             : #define CHECK_ENCODING_CONVERSION_ARGS(srcencoding,destencoding) \
     361                 :             :     check_encoding_conversion_args(PG_GETARG_INT32(0), \
     362                 :             :                                    PG_GETARG_INT32(1), \
     363                 :             :                                    PG_GETARG_INT32(4), \
     364                 :             :                                    (srcencoding), \
     365                 :             :                                    (destencoding))
     366                 :             : 
     367                 :             : 
     368                 :             : /*
     369                 :             :  * Some handy functions for Unicode-specific tests.
     370                 :             :  */
     371                 :             : static inline bool
     372                 :        1793 : is_valid_unicode_codepoint(char32_t c)
     373                 :             : {
     374   [ +  -  +  + ]:        1793 :     return (c > 0 && c <= 0x10FFFF);
     375                 :             : }
     376                 :             : 
     377                 :             : static inline bool
     378                 :        1224 : is_utf16_surrogate_first(char32_t c)
     379                 :             : {
     380   [ +  +  +  + ]:        1224 :     return (c >= 0xD800 && c <= 0xDBFF);
     381                 :             : }
     382                 :             : 
     383                 :             : static inline bool
     384                 :        1082 : is_utf16_surrogate_second(char32_t c)
     385                 :             : {
     386   [ +  +  +  + ]:        1082 :     return (c >= 0xDC00 && c <= 0xDFFF);
     387                 :             : }
     388                 :             : 
     389                 :             : static inline char32_t
     390                 :          42 : surrogate_pair_to_codepoint(char16_t first, char16_t second)
     391                 :             : {
     392                 :          42 :     return ((first & 0x3FF) << 10) + 0x10000 + (second & 0x3FF);
     393                 :             : }
     394                 :             : 
     395                 :             : /*
     396                 :             :  * Convert a UTF-8 character to a Unicode code point.
     397                 :             :  * This is a one-character version of pg_utf2wchar_with_len.
     398                 :             :  *
     399                 :             :  * No error checks here, c must point to a long-enough string.
     400                 :             :  */
     401                 :             : static inline char32_t
     402                 :    30613575 : utf8_to_unicode(const unsigned char *c)
     403                 :             : {
     404         [ +  + ]:    30613575 :     if ((*c & 0x80) == 0)
     405                 :    30606262 :         return (char32_t) c[0];
     406         [ +  + ]:        7313 :     else if ((*c & 0xe0) == 0xc0)
     407                 :        6093 :         return (char32_t) (((c[0] & 0x1f) << 6) |
     408                 :        6093 :                            (c[1] & 0x3f));
     409         [ +  + ]:        1220 :     else if ((*c & 0xf0) == 0xe0)
     410                 :        1155 :         return (char32_t) (((c[0] & 0x0f) << 12) |
     411                 :        1155 :                            ((c[1] & 0x3f) << 6) |
     412                 :        1155 :                            (c[2] & 0x3f));
     413         [ +  - ]:          65 :     else if ((*c & 0xf8) == 0xf0)
     414                 :          65 :         return (char32_t) (((c[0] & 0x07) << 18) |
     415                 :          65 :                            ((c[1] & 0x3f) << 12) |
     416                 :          65 :                            ((c[2] & 0x3f) << 6) |
     417                 :          65 :                            (c[3] & 0x3f));
     418                 :             :     else
     419                 :             :         /* that is an invalid code on purpose */
     420                 :           0 :         return 0xffffffff;
     421                 :             : }
     422                 :             : 
     423                 :             : /*
     424                 :             :  * Map a Unicode code point to UTF-8.  utf8string must have at least
     425                 :             :  * unicode_utf8len(c) bytes available.
     426                 :             :  */
     427                 :             : static inline unsigned char *
     428                 :     8299259 : unicode_to_utf8(char32_t c, unsigned char *utf8string)
     429                 :             : {
     430         [ +  + ]:     8299259 :     if (c <= 0x7F)
     431                 :             :     {
     432                 :     8296794 :         utf8string[0] = c;
     433                 :             :     }
     434         [ +  + ]:        2465 :     else if (c <= 0x7FF)
     435                 :             :     {
     436                 :        1572 :         utf8string[0] = 0xC0 | ((c >> 6) & 0x1F);
     437                 :        1572 :         utf8string[1] = 0x80 | (c & 0x3F);
     438                 :             :     }
     439         [ +  + ]:         893 :     else if (c <= 0xFFFF)
     440                 :             :     {
     441                 :         839 :         utf8string[0] = 0xE0 | ((c >> 12) & 0x0F);
     442                 :         839 :         utf8string[1] = 0x80 | ((c >> 6) & 0x3F);
     443                 :         839 :         utf8string[2] = 0x80 | (c & 0x3F);
     444                 :             :     }
     445                 :             :     else
     446                 :             :     {
     447                 :          54 :         utf8string[0] = 0xF0 | ((c >> 18) & 0x07);
     448                 :          54 :         utf8string[1] = 0x80 | ((c >> 12) & 0x3F);
     449                 :          54 :         utf8string[2] = 0x80 | ((c >> 6) & 0x3F);
     450                 :          54 :         utf8string[3] = 0x80 | (c & 0x3F);
     451                 :             :     }
     452                 :             : 
     453                 :     8299259 :     return utf8string;
     454                 :             : }
     455                 :             : 
     456                 :             : /*
     457                 :             :  * Number of bytes needed to represent the given char in UTF8.
     458                 :             :  */
     459                 :             : static inline int
     460                 :      329105 : unicode_utf8len(char32_t c)
     461                 :             : {
     462         [ +  + ]:      329105 :     if (c <= 0x7F)
     463                 :      327801 :         return 1;
     464         [ +  + ]:        1304 :     else if (c <= 0x7FF)
     465                 :         984 :         return 2;
     466         [ +  - ]:         320 :     else if (c <= 0xFFFF)
     467                 :         320 :         return 3;
     468                 :             :     else
     469                 :           0 :         return 4;
     470                 :             : }
     471                 :             : 
     472                 :             : /*
     473                 :             :  * The functions in this list are exported by libpq, and we need to be sure
     474                 :             :  * that we know which calls are satisfied by libpq and which are satisfied
     475                 :             :  * by static linkage to libpgcommon.  (This is because we might be using a
     476                 :             :  * libpq.so that's of a different major version and has encoding IDs that
     477                 :             :  * differ from the current version's.)  The nominal function names are what
     478                 :             :  * are actually used in and exported by libpq, while the names exported by
     479                 :             :  * libpgcommon.a and libpgcommon_srv.a end in "_private".
     480                 :             :  */
     481                 :             : #if defined(USE_PRIVATE_ENCODING_FUNCS) || !defined(FRONTEND)
     482                 :             : #define pg_char_to_encoding         pg_char_to_encoding_private
     483                 :             : #define pg_encoding_to_char         pg_encoding_to_char_private
     484                 :             : #define pg_valid_server_encoding    pg_valid_server_encoding_private
     485                 :             : #define pg_valid_server_encoding_id pg_valid_server_encoding_id_private
     486                 :             : #define pg_utf_mblen                pg_utf_mblen_private
     487                 :             : #endif
     488                 :             : 
     489                 :             : /*
     490                 :             :  * These functions are considered part of libpq's exported API and
     491                 :             :  * are also declared in libpq-fe.h.
     492                 :             :  */
     493                 :             : extern int  pg_char_to_encoding(const char *name);
     494                 :             : extern const char *pg_encoding_to_char(int encoding);
     495                 :             : extern int  pg_valid_server_encoding_id(int encoding);
     496                 :             : 
     497                 :             : /*
     498                 :             :  * These functions are available to frontend code that links with libpgcommon
     499                 :             :  * (in addition to the ones just above).  The constant tables declared
     500                 :             :  * earlier in this file are also available from libpgcommon.
     501                 :             :  */
     502                 :             : extern void pg_encoding_set_invalid(int encoding, char *dst);
     503                 :             : extern int  pg_encoding_mblen(int encoding, const char *mbstr);
     504                 :             : extern int  pg_encoding_mblen_or_incomplete(int encoding, const char *mbstr,
     505                 :             :                                             size_t remaining);
     506                 :             : extern int  pg_encoding_mblen_bounded(int encoding, const char *mbstr);
     507                 :             : extern int  pg_encoding_dsplen(int encoding, const char *mbstr);
     508                 :             : extern int  pg_encoding_verifymbchar(int encoding, const char *mbstr, int len);
     509                 :             : extern int  pg_encoding_verifymbstr(int encoding, const char *mbstr, int len);
     510                 :             : extern int  pg_encoding_max_length(int encoding);
     511                 :             : extern int  pg_valid_client_encoding(const char *name);
     512                 :             : extern int  pg_valid_server_encoding(const char *name);
     513                 :             : extern bool is_encoding_supported_by_icu(int encoding);
     514                 :             : extern const char *get_encoding_name_for_icu(int encoding);
     515                 :             : 
     516                 :             : extern bool pg_utf8_islegal(const unsigned char *source, int length);
     517                 :             : extern int  pg_utf_mblen(const unsigned char *s);
     518                 :             : 
     519                 :             : /*
     520                 :             :  * The remaining functions are backend-only.
     521                 :             :  */
     522                 :             : extern int  pg_mb2wchar(const char *from, pg_wchar *to);
     523                 :             : extern int  pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len);
     524                 :             : extern int  pg_encoding_mb2wchar_with_len(int encoding,
     525                 :             :                                           const char *from, pg_wchar *to, int len);
     526                 :             : extern int  pg_wchar2mb(const pg_wchar *from, char *to);
     527                 :             : extern int  pg_wchar2mb_with_len(const pg_wchar *from, char *to, int len);
     528                 :             : extern int  pg_encoding_wchar2mb_with_len(int encoding,
     529                 :             :                                           const pg_wchar *from, char *to, int len);
     530                 :             : extern int  pg_char_and_wchar_strcmp(const char *s1, const pg_wchar *s2);
     531                 :             : extern int  pg_wchar_strncmp(const pg_wchar *s1, const pg_wchar *s2, size_t n);
     532                 :             : extern int  pg_char_and_wchar_strncmp(const char *s1, const pg_wchar *s2, size_t n);
     533                 :             : extern size_t pg_wchar_strlen(const pg_wchar *str);
     534                 :             : extern int  pg_mblen_cstr(const char *mbstr);
     535                 :             : extern int  pg_mblen_range(const char *mbstr, const char *end);
     536                 :             : extern int  pg_mblen_with_len(const char *mbstr, int limit);
     537                 :             : extern int  pg_mblen_unbounded(const char *mbstr);
     538                 :             : 
     539                 :             : /* deprecated */
     540                 :             : extern int  pg_mblen(const char *mbstr);
     541                 :             : 
     542                 :             : extern int  pg_dsplen(const char *mbstr);
     543                 :             : extern int  pg_mbstrlen(const char *mbstr);
     544                 :             : extern int  pg_mbstrlen_with_len(const char *mbstr, int limit);
     545                 :             : extern int  pg_mbcliplen(const char *mbstr, int len, int limit);
     546                 :             : extern int  pg_encoding_mbcliplen(int encoding, const char *mbstr,
     547                 :             :                                   int len, int limit);
     548                 :             : extern int  pg_mbcharcliplen(const char *mbstr, int len, int limit);
     549                 :             : extern int  pg_database_encoding_max_length(void);
     550                 :             : extern mbcharacter_incrementer pg_database_encoding_character_incrementer(void);
     551                 :             : 
     552                 :             : extern int  PrepareClientEncoding(int encoding);
     553                 :             : extern int  SetClientEncoding(int encoding);
     554                 :             : extern void InitializeClientEncoding(void);
     555                 :             : extern int  pg_get_client_encoding(void);
     556                 :             : extern const char *pg_get_client_encoding_name(void);
     557                 :             : 
     558                 :             : extern void SetDatabaseEncoding(int encoding);
     559                 :             : extern int  GetDatabaseEncoding(void);
     560                 :             : extern const char *GetDatabaseEncodingName(void);
     561                 :             : extern void SetMessageEncoding(int encoding);
     562                 :             : extern int  GetMessageEncoding(void);
     563                 :             : 
     564                 :             : #ifdef ENABLE_NLS
     565                 :             : extern int  pg_bind_textdomain_codeset(const char *domainname);
     566                 :             : #endif
     567                 :             : 
     568                 :             : extern unsigned char *pg_do_encoding_conversion(unsigned char *src, int len,
     569                 :             :                                                 int src_encoding,
     570                 :             :                                                 int dest_encoding);
     571                 :             : extern int  pg_do_encoding_conversion_buf(Oid proc,
     572                 :             :                                           int src_encoding,
     573                 :             :                                           int dest_encoding,
     574                 :             :                                           unsigned char *src, int srclen,
     575                 :             :                                           unsigned char *dest, int destlen,
     576                 :             :                                           bool noError);
     577                 :             : 
     578                 :             : extern char *pg_client_to_server(const char *s, int len);
     579                 :             : extern char *pg_server_to_client(const char *s, int len);
     580                 :             : extern char *pg_any_to_server(const char *s, int len, int encoding);
     581                 :             : extern char *pg_server_to_any(const char *s, int len, int encoding);
     582                 :             : 
     583                 :             : extern void pg_unicode_to_server(char32_t c, unsigned char *s);
     584                 :             : extern bool pg_unicode_to_server_noerror(char32_t c, unsigned char *s);
     585                 :             : 
     586                 :             : extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc);
     587                 :             : extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc);
     588                 :             : 
     589                 :             : extern int  UtfToLocal(const unsigned char *utf, int len,
     590                 :             :                        unsigned char *iso,
     591                 :             :                        const pg_mb_radix_tree *map,
     592                 :             :                        const pg_utf_to_local_combined *cmap, int cmapsize,
     593                 :             :                        utf_local_conversion_func conv_func,
     594                 :             :                        int encoding, bool noError);
     595                 :             : extern int  LocalToUtf(const unsigned char *iso, int len,
     596                 :             :                        unsigned char *utf,
     597                 :             :                        const pg_mb_radix_tree *map,
     598                 :             :                        const pg_local_to_utf_combined *cmap, int cmapsize,
     599                 :             :                        utf_local_conversion_func conv_func,
     600                 :             :                        int encoding, bool noError);
     601                 :             : 
     602                 :             : extern bool pg_verifymbstr(const char *mbstr, int len, bool noError);
     603                 :             : extern bool pg_verify_mbstr(int encoding, const char *mbstr, int len,
     604                 :             :                             bool noError);
     605                 :             : extern int  pg_verify_mbstr_len(int encoding, const char *mbstr, int len,
     606                 :             :                                 bool noError);
     607                 :             : 
     608                 :             : extern void check_encoding_conversion_args(int src_encoding,
     609                 :             :                                            int dest_encoding,
     610                 :             :                                            int len,
     611                 :             :                                            int expected_src_encoding,
     612                 :             :                                            int expected_dest_encoding);
     613                 :             : 
     614                 :             : pg_noreturn extern void report_invalid_encoding(int encoding, const char *mbstr, int len);
     615                 :             : pg_noreturn extern void report_untranslatable_char(int src_encoding, int dest_encoding,
     616                 :             :                                                    const char *mbstr, int len);
     617                 :             : 
     618                 :             : extern int  local2local(const unsigned char *l, unsigned char *p, int len,
     619                 :             :                         int src_encoding, int dest_encoding,
     620                 :             :                         const unsigned char *tab, bool noError);
     621                 :             : 
     622                 :             : #ifdef WIN32
     623                 :             : extern WCHAR *pgwin32_message_to_UTF16(const char *str, int len, int *utf16len);
     624                 :             : #endif
     625                 :             : 
     626                 :             : #endif                          /* PG_WCHAR_H */
        

Generated by: LCOV version 2.0-1