LCOV - code coverage report
Current view: top level - src/backend/utils/mb/conversion_procs/utf8_and_win - utf8_and_win.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 93.1 % 29 27
Test Date: 2026-07-12 22:15:32 Functions: 100.0 % 5 5
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 50.0 % 12 6

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  *    WIN <--> UTF8
       4                 :             :  *
       5                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       6                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       7                 :             :  *
       8                 :             :  * IDENTIFICATION
       9                 :             :  *    src/backend/utils/mb/conversion_procs/utf8_and_win/utf8_and_win.c
      10                 :             :  *
      11                 :             :  *-------------------------------------------------------------------------
      12                 :             :  */
      13                 :             : 
      14                 :             : #include "postgres.h"
      15                 :             : #include "fmgr.h"
      16                 :             : #include "mb/pg_wchar.h"
      17                 :             : #include "../../Unicode/utf8_to_win1250.map"
      18                 :             : #include "../../Unicode/utf8_to_win1251.map"
      19                 :             : #include "../../Unicode/utf8_to_win1252.map"
      20                 :             : #include "../../Unicode/utf8_to_win1253.map"
      21                 :             : #include "../../Unicode/utf8_to_win1254.map"
      22                 :             : #include "../../Unicode/utf8_to_win1255.map"
      23                 :             : #include "../../Unicode/utf8_to_win1256.map"
      24                 :             : #include "../../Unicode/utf8_to_win1257.map"
      25                 :             : #include "../../Unicode/utf8_to_win1258.map"
      26                 :             : #include "../../Unicode/utf8_to_win866.map"
      27                 :             : #include "../../Unicode/utf8_to_win874.map"
      28                 :             : #include "../../Unicode/win1250_to_utf8.map"
      29                 :             : #include "../../Unicode/win1251_to_utf8.map"
      30                 :             : #include "../../Unicode/win1252_to_utf8.map"
      31                 :             : #include "../../Unicode/win1253_to_utf8.map"
      32                 :             : #include "../../Unicode/win1254_to_utf8.map"
      33                 :             : #include "../../Unicode/win1255_to_utf8.map"
      34                 :             : #include "../../Unicode/win1256_to_utf8.map"
      35                 :             : #include "../../Unicode/win1257_to_utf8.map"
      36                 :             : #include "../../Unicode/win866_to_utf8.map"
      37                 :             : #include "../../Unicode/win874_to_utf8.map"
      38                 :             : #include "../../Unicode/win1258_to_utf8.map"
      39                 :             : 
      40                 :           4 : PG_MODULE_MAGIC_EXT(
      41                 :             :                     .name = "utf8_and_win",
      42                 :             :                     .version = PG_VERSION
      43                 :             : );
      44                 :             : 
      45                 :           4 : PG_FUNCTION_INFO_V1(win_to_utf8);
      46                 :           4 : PG_FUNCTION_INFO_V1(utf8_to_win);
      47                 :             : 
      48                 :             : /* ----------
      49                 :             :  * conv_proc(
      50                 :             :  *      INTEGER,    -- source encoding id
      51                 :             :  *      INTEGER,    -- destination encoding id
      52                 :             :  *      CSTRING,    -- source string (null terminated C string)
      53                 :             :  *      CSTRING,    -- destination string (null terminated C string)
      54                 :             :  *      INTEGER,    -- source string length
      55                 :             :  *      BOOL        -- if true, don't throw an error if conversion fails
      56                 :             :  * ) returns INTEGER;
      57                 :             :  *
      58                 :             :  * Returns the number of bytes successfully converted.
      59                 :             :  * ----------
      60                 :             :  */
      61                 :             : 
      62                 :             : typedef struct
      63                 :             : {
      64                 :             :     pg_enc      encoding;
      65                 :             :     const pg_mb_radix_tree *map1;   /* to UTF8 map name */
      66                 :             :     const pg_mb_radix_tree *map2;   /* from UTF8 map name */
      67                 :             : } pg_conv_map;
      68                 :             : 
      69                 :             : static const pg_conv_map maps[] = {
      70                 :             :     {PG_WIN866, &win866_to_unicode_tree, &win866_from_unicode_tree},
      71                 :             :     {PG_WIN874, &win874_to_unicode_tree, &win874_from_unicode_tree},
      72                 :             :     {PG_WIN1250, &win1250_to_unicode_tree, &win1250_from_unicode_tree},
      73                 :             :     {PG_WIN1251, &win1251_to_unicode_tree, &win1251_from_unicode_tree},
      74                 :             :     {PG_WIN1252, &win1252_to_unicode_tree, &win1252_from_unicode_tree},
      75                 :             :     {PG_WIN1253, &win1253_to_unicode_tree, &win1253_from_unicode_tree},
      76                 :             :     {PG_WIN1254, &win1254_to_unicode_tree, &win1254_from_unicode_tree},
      77                 :             :     {PG_WIN1255, &win1255_to_unicode_tree, &win1255_from_unicode_tree},
      78                 :             :     {PG_WIN1256, &win1256_to_unicode_tree, &win1256_from_unicode_tree},
      79                 :             :     {PG_WIN1257, &win1257_to_unicode_tree, &win1257_from_unicode_tree},
      80                 :             :     {PG_WIN1258, &win1258_to_unicode_tree, &win1258_from_unicode_tree},
      81                 :             : };
      82                 :             : 
      83                 :             : Datum
      84                 :          44 : win_to_utf8(PG_FUNCTION_ARGS)
      85                 :             : {
      86                 :          44 :     int         encoding = PG_GETARG_INT32(0);
      87                 :          44 :     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
      88                 :          44 :     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
      89                 :          44 :     int         len = PG_GETARG_INT32(4);
      90                 :          44 :     bool        noError = PG_GETARG_BOOL(5);
      91                 :             : 
      92                 :          44 :     CHECK_ENCODING_CONVERSION_ARGS(-1, PG_UTF8);
      93                 :             : 
      94         [ +  - ]:         264 :     for (size_t i = 0; i < lengthof(maps); i++)
      95                 :             :     {
      96         [ +  + ]:         264 :         if (encoding == maps[i].encoding)
      97                 :             :         {
      98                 :             :             int         converted;
      99                 :             : 
     100                 :          44 :             converted = LocalToUtf(src, len, dest,
     101                 :          44 :                                    maps[i].map1,
     102                 :             :                                    NULL, 0,
     103                 :             :                                    NULL,
     104                 :             :                                    encoding,
     105                 :             :                                    noError);
     106                 :          44 :             PG_RETURN_INT32(converted);
     107                 :             :         }
     108                 :             :     }
     109                 :             : 
     110         [ #  # ]:           0 :     ereport(ERROR,
     111                 :             :             (errcode(ERRCODE_INTERNAL_ERROR),
     112                 :             :              errmsg("unexpected encoding ID %d for WIN character sets",
     113                 :             :                     encoding)));
     114                 :             : 
     115                 :             :     PG_RETURN_INT32(0);
     116                 :             : }
     117                 :             : 
     118                 :             : Datum
     119                 :          44 : utf8_to_win(PG_FUNCTION_ARGS)
     120                 :             : {
     121                 :          44 :     int         encoding = PG_GETARG_INT32(1);
     122                 :          44 :     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
     123                 :          44 :     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
     124                 :          44 :     int         len = PG_GETARG_INT32(4);
     125                 :          44 :     bool        noError = PG_GETARG_BOOL(5);
     126                 :             : 
     127                 :          44 :     CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, -1);
     128                 :             : 
     129         [ +  - ]:         264 :     for (size_t i = 0; i < lengthof(maps); i++)
     130                 :             :     {
     131         [ +  + ]:         264 :         if (encoding == maps[i].encoding)
     132                 :             :         {
     133                 :             :             int         converted;
     134                 :             : 
     135                 :          44 :             converted = UtfToLocal(src, len, dest,
     136                 :          44 :                                    maps[i].map2,
     137                 :             :                                    NULL, 0,
     138                 :             :                                    NULL,
     139                 :             :                                    encoding,
     140                 :             :                                    noError);
     141                 :          44 :             PG_RETURN_INT32(converted);
     142                 :             :         }
     143                 :             :     }
     144                 :             : 
     145         [ #  # ]:           0 :     ereport(ERROR,
     146                 :             :             (errcode(ERRCODE_INTERNAL_ERROR),
     147                 :             :              errmsg("unexpected encoding ID %d for WIN character sets",
     148                 :             :                     encoding)));
     149                 :             : 
     150                 :             :     PG_RETURN_INT32(0);
     151                 :             : }
        

Generated by: LCOV version 2.0-1