LCOV - code coverage report
Current view: top level - src/backend/utils/mb/conversion_procs/utf8_and_iso8859 - utf8_and_iso8859.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 93.1 % 29 27
Test Date: 2026-07-03 19:57:34 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                 :             :  *    ISO 8859 2-16 <--> 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_iso8859/utf8_and_iso8859.c
      10                 :             :  *
      11                 :             :  *-------------------------------------------------------------------------
      12                 :             :  */
      13                 :             : 
      14                 :             : #include "postgres.h"
      15                 :             : #include "fmgr.h"
      16                 :             : #include "mb/pg_wchar.h"
      17                 :             : #include "../../Unicode/iso8859_10_to_utf8.map"
      18                 :             : #include "../../Unicode/iso8859_13_to_utf8.map"
      19                 :             : #include "../../Unicode/iso8859_14_to_utf8.map"
      20                 :             : #include "../../Unicode/iso8859_15_to_utf8.map"
      21                 :             : #include "../../Unicode/iso8859_2_to_utf8.map"
      22                 :             : #include "../../Unicode/iso8859_3_to_utf8.map"
      23                 :             : #include "../../Unicode/iso8859_4_to_utf8.map"
      24                 :             : #include "../../Unicode/iso8859_5_to_utf8.map"
      25                 :             : #include "../../Unicode/iso8859_6_to_utf8.map"
      26                 :             : #include "../../Unicode/iso8859_7_to_utf8.map"
      27                 :             : #include "../../Unicode/iso8859_8_to_utf8.map"
      28                 :             : #include "../../Unicode/iso8859_9_to_utf8.map"
      29                 :             : #include "../../Unicode/utf8_to_iso8859_10.map"
      30                 :             : #include "../../Unicode/utf8_to_iso8859_13.map"
      31                 :             : #include "../../Unicode/utf8_to_iso8859_14.map"
      32                 :             : #include "../../Unicode/utf8_to_iso8859_15.map"
      33                 :             : #include "../../Unicode/utf8_to_iso8859_16.map"
      34                 :             : #include "../../Unicode/utf8_to_iso8859_2.map"
      35                 :             : #include "../../Unicode/utf8_to_iso8859_3.map"
      36                 :             : #include "../../Unicode/utf8_to_iso8859_4.map"
      37                 :             : #include "../../Unicode/utf8_to_iso8859_5.map"
      38                 :             : #include "../../Unicode/utf8_to_iso8859_6.map"
      39                 :             : #include "../../Unicode/utf8_to_iso8859_7.map"
      40                 :             : #include "../../Unicode/utf8_to_iso8859_8.map"
      41                 :             : #include "../../Unicode/utf8_to_iso8859_9.map"
      42                 :             : #include "../../Unicode/iso8859_16_to_utf8.map"
      43                 :             : 
      44                 :           8 : PG_MODULE_MAGIC_EXT(
      45                 :             :                     .name = "utf8_and_iso8859",
      46                 :             :                     .version = PG_VERSION
      47                 :             : );
      48                 :             : 
      49                 :           8 : PG_FUNCTION_INFO_V1(iso8859_to_utf8);
      50                 :           8 : PG_FUNCTION_INFO_V1(utf8_to_iso8859);
      51                 :             : 
      52                 :             : /* ----------
      53                 :             :  * conv_proc(
      54                 :             :  *      INTEGER,    -- source encoding id
      55                 :             :  *      INTEGER,    -- destination encoding id
      56                 :             :  *      CSTRING,    -- source string (null terminated C string)
      57                 :             :  *      CSTRING,    -- destination string (null terminated C string)
      58                 :             :  *      INTEGER,    -- source string length
      59                 :             :  *      BOOL        -- if true, don't throw an error if conversion fails
      60                 :             :  * ) returns INTEGER;
      61                 :             :  *
      62                 :             :  * Returns the number of bytes successfully converted.
      63                 :             :  * ----------
      64                 :             :  */
      65                 :             : 
      66                 :             : typedef struct
      67                 :             : {
      68                 :             :     pg_enc      encoding;
      69                 :             :     const pg_mb_radix_tree *map1;   /* to UTF8 map name */
      70                 :             :     const pg_mb_radix_tree *map2;   /* from UTF8 map name */
      71                 :             : } pg_conv_map;
      72                 :             : 
      73                 :             : static const pg_conv_map maps[] = {
      74                 :             :     {PG_LATIN2, &iso8859_2_to_unicode_tree,
      75                 :             :     &iso8859_2_from_unicode_tree},  /* ISO-8859-2 Latin 2 */
      76                 :             :     {PG_LATIN3, &iso8859_3_to_unicode_tree,
      77                 :             :     &iso8859_3_from_unicode_tree},  /* ISO-8859-3 Latin 3 */
      78                 :             :     {PG_LATIN4, &iso8859_4_to_unicode_tree,
      79                 :             :     &iso8859_4_from_unicode_tree},  /* ISO-8859-4 Latin 4 */
      80                 :             :     {PG_LATIN5, &iso8859_9_to_unicode_tree,
      81                 :             :     &iso8859_9_from_unicode_tree},  /* ISO-8859-9 Latin 5 */
      82                 :             :     {PG_LATIN6, &iso8859_10_to_unicode_tree,
      83                 :             :     &iso8859_10_from_unicode_tree}, /* ISO-8859-10 Latin 6 */
      84                 :             :     {PG_LATIN7, &iso8859_13_to_unicode_tree,
      85                 :             :     &iso8859_13_from_unicode_tree}, /* ISO-8859-13 Latin 7 */
      86                 :             :     {PG_LATIN8, &iso8859_14_to_unicode_tree,
      87                 :             :     &iso8859_14_from_unicode_tree}, /* ISO-8859-14 Latin 8 */
      88                 :             :     {PG_LATIN9, &iso8859_15_to_unicode_tree,
      89                 :             :     &iso8859_15_from_unicode_tree}, /* ISO-8859-15 Latin 9 */
      90                 :             :     {PG_LATIN10, &iso8859_16_to_unicode_tree,
      91                 :             :     &iso8859_16_from_unicode_tree}, /* ISO-8859-16 Latin 10 */
      92                 :             :     {PG_ISO_8859_5, &iso8859_5_to_unicode_tree,
      93                 :             :     &iso8859_5_from_unicode_tree},  /* ISO-8859-5 */
      94                 :             :     {PG_ISO_8859_6, &iso8859_6_to_unicode_tree,
      95                 :             :     &iso8859_6_from_unicode_tree},  /* ISO-8859-6 */
      96                 :             :     {PG_ISO_8859_7, &iso8859_7_to_unicode_tree,
      97                 :             :     &iso8859_7_from_unicode_tree},  /* ISO-8859-7 */
      98                 :             :     {PG_ISO_8859_8, &iso8859_8_to_unicode_tree,
      99                 :             :     &iso8859_8_from_unicode_tree},  /* ISO-8859-8 */
     100                 :             : };
     101                 :             : 
     102                 :             : Datum
     103                 :         148 : iso8859_to_utf8(PG_FUNCTION_ARGS)
     104                 :             : {
     105                 :         148 :     int         encoding = PG_GETARG_INT32(0);
     106                 :         148 :     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
     107                 :         148 :     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
     108                 :         148 :     int         len = PG_GETARG_INT32(4);
     109                 :         148 :     bool        noError = PG_GETARG_BOOL(5);
     110                 :             :     int         i;
     111                 :             : 
     112                 :         148 :     CHECK_ENCODING_CONVERSION_ARGS(-1, PG_UTF8);
     113                 :             : 
     114         [ +  - ]:        1324 :     for (i = 0; i < lengthof(maps); i++)
     115                 :             :     {
     116         [ +  + ]:        1324 :         if (encoding == maps[i].encoding)
     117                 :             :         {
     118                 :             :             int         converted;
     119                 :             : 
     120                 :         148 :             converted = LocalToUtf(src, len, dest,
     121                 :         148 :                                    maps[i].map1,
     122                 :             :                                    NULL, 0,
     123                 :             :                                    NULL,
     124                 :             :                                    encoding,
     125                 :             :                                    noError);
     126                 :         112 :             PG_RETURN_INT32(converted);
     127                 :             :         }
     128                 :             :     }
     129                 :             : 
     130         [ #  # ]:           0 :     ereport(ERROR,
     131                 :             :             (errcode(ERRCODE_INTERNAL_ERROR),
     132                 :             :              errmsg("unexpected encoding ID %d for ISO 8859 character sets",
     133                 :             :                     encoding)));
     134                 :             : 
     135                 :             :     PG_RETURN_INT32(0);
     136                 :             : }
     137                 :             : 
     138                 :             : Datum
     139                 :         628 : utf8_to_iso8859(PG_FUNCTION_ARGS)
     140                 :             : {
     141                 :         628 :     int         encoding = PG_GETARG_INT32(1);
     142                 :         628 :     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
     143                 :         628 :     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
     144                 :         628 :     int         len = PG_GETARG_INT32(4);
     145                 :         628 :     bool        noError = PG_GETARG_BOOL(5);
     146                 :             :     int         i;
     147                 :             : 
     148                 :         628 :     CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, -1);
     149                 :             : 
     150         [ +  - ]:        1804 :     for (i = 0; i < lengthof(maps); i++)
     151                 :             :     {
     152         [ +  + ]:        1804 :         if (encoding == maps[i].encoding)
     153                 :             :         {
     154                 :             :             int         converted;
     155                 :             : 
     156                 :         628 :             converted = UtfToLocal(src, len, dest,
     157                 :         628 :                                    maps[i].map2,
     158                 :             :                                    NULL, 0,
     159                 :             :                                    NULL,
     160                 :             :                                    encoding,
     161                 :             :                                    noError);
     162                 :         364 :             PG_RETURN_INT32(converted);
     163                 :             :         }
     164                 :             :     }
     165                 :             : 
     166         [ #  # ]:           0 :     ereport(ERROR,
     167                 :             :             (errcode(ERRCODE_INTERNAL_ERROR),
     168                 :             :              errmsg("unexpected encoding ID %d for ISO 8859 character sets",
     169                 :             :                     encoding)));
     170                 :             : 
     171                 :             :     PG_RETURN_INT32(0);
     172                 :             : }
        

Generated by: LCOV version 2.0-1