LCOV - code coverage report
Current view: top level - src/backend/utils/mb/conversion_procs/utf8_and_win - utf8_and_win.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 25 27 92.6 %
Date: 2024-04-19 18:11:10 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  *    WIN <--> UTF8
       4             :  *
       5             :  * Portions Copyright (c) 1996-2024, 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           6 : PG_MODULE_MAGIC;
      41             : 
      42           6 : PG_FUNCTION_INFO_V1(win_to_utf8);
      43           6 : PG_FUNCTION_INFO_V1(utf8_to_win);
      44             : 
      45             : /* ----------
      46             :  * conv_proc(
      47             :  *      INTEGER,    -- source encoding id
      48             :  *      INTEGER,    -- destination encoding id
      49             :  *      CSTRING,    -- source string (null terminated C string)
      50             :  *      CSTRING,    -- destination string (null terminated C string)
      51             :  *      INTEGER,    -- source string length
      52             :  *      BOOL        -- if true, don't throw an error if conversion fails
      53             :  * ) returns INTEGER;
      54             :  *
      55             :  * Returns the number of bytes successfully converted.
      56             :  * ----------
      57             :  */
      58             : 
      59             : typedef struct
      60             : {
      61             :     pg_enc      encoding;
      62             :     const pg_mb_radix_tree *map1;   /* to UTF8 map name */
      63             :     const pg_mb_radix_tree *map2;   /* from UTF8 map name */
      64             : } pg_conv_map;
      65             : 
      66             : static const pg_conv_map maps[] = {
      67             :     {PG_WIN866, &win866_to_unicode_tree, &win866_from_unicode_tree},
      68             :     {PG_WIN874, &win874_to_unicode_tree, &win874_from_unicode_tree},
      69             :     {PG_WIN1250, &win1250_to_unicode_tree, &win1250_from_unicode_tree},
      70             :     {PG_WIN1251, &win1251_to_unicode_tree, &win1251_from_unicode_tree},
      71             :     {PG_WIN1252, &win1252_to_unicode_tree, &win1252_from_unicode_tree},
      72             :     {PG_WIN1253, &win1253_to_unicode_tree, &win1253_from_unicode_tree},
      73             :     {PG_WIN1254, &win1254_to_unicode_tree, &win1254_from_unicode_tree},
      74             :     {PG_WIN1255, &win1255_to_unicode_tree, &win1255_from_unicode_tree},
      75             :     {PG_WIN1256, &win1256_to_unicode_tree, &win1256_from_unicode_tree},
      76             :     {PG_WIN1257, &win1257_to_unicode_tree, &win1257_from_unicode_tree},
      77             :     {PG_WIN1258, &win1258_to_unicode_tree, &win1258_from_unicode_tree},
      78             : };
      79             : 
      80             : Datum
      81          66 : win_to_utf8(PG_FUNCTION_ARGS)
      82             : {
      83          66 :     int         encoding = PG_GETARG_INT32(0);
      84          66 :     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
      85          66 :     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
      86          66 :     int         len = PG_GETARG_INT32(4);
      87          66 :     bool        noError = PG_GETARG_BOOL(5);
      88             :     int         i;
      89             : 
      90          66 :     CHECK_ENCODING_CONVERSION_ARGS(-1, PG_UTF8);
      91             : 
      92         396 :     for (i = 0; i < lengthof(maps); i++)
      93             :     {
      94         396 :         if (encoding == maps[i].encoding)
      95             :         {
      96             :             int         converted;
      97             : 
      98          66 :             converted = LocalToUtf(src, len, dest,
      99             :                                    maps[i].map1,
     100             :                                    NULL, 0,
     101             :                                    NULL,
     102             :                                    encoding,
     103             :                                    noError);
     104          66 :             PG_RETURN_INT32(converted);
     105             :         }
     106             :     }
     107             : 
     108           0 :     ereport(ERROR,
     109             :             (errcode(ERRCODE_INTERNAL_ERROR),
     110             :              errmsg("unexpected encoding ID %d for WIN character sets",
     111             :                     encoding)));
     112             : 
     113             :     PG_RETURN_INT32(0);
     114             : }
     115             : 
     116             : Datum
     117          66 : utf8_to_win(PG_FUNCTION_ARGS)
     118             : {
     119          66 :     int         encoding = PG_GETARG_INT32(1);
     120          66 :     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
     121          66 :     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
     122          66 :     int         len = PG_GETARG_INT32(4);
     123          66 :     bool        noError = PG_GETARG_BOOL(5);
     124             :     int         i;
     125             : 
     126          66 :     CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, -1);
     127             : 
     128         396 :     for (i = 0; i < lengthof(maps); i++)
     129             :     {
     130         396 :         if (encoding == maps[i].encoding)
     131             :         {
     132             :             int         converted;
     133             : 
     134          66 :             converted = UtfToLocal(src, len, dest,
     135             :                                    maps[i].map2,
     136             :                                    NULL, 0,
     137             :                                    NULL,
     138             :                                    encoding,
     139             :                                    noError);
     140          66 :             PG_RETURN_INT32(converted);
     141             :         }
     142             :     }
     143             : 
     144           0 :     ereport(ERROR,
     145             :             (errcode(ERRCODE_INTERNAL_ERROR),
     146             :              errmsg("unexpected encoding ID %d for WIN character sets",
     147             :                     encoding)));
     148             : 
     149             :     PG_RETURN_INT32(0);
     150             : }

Generated by: LCOV version 1.14