LCOV - code coverage report
Current view: top level - src/backend/utils/mb/conversion_procs/utf8_and_iso8859 - utf8_and_iso8859.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 23 25 92.0 %
Date: 2019-08-24 16:07:17 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  *    ISO 8859 2-16 <--> UTF8
       4             :  *
       5             :  * Portions Copyright (c) 1996-2019, 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           4 : PG_MODULE_MAGIC;
      45             : 
      46           4 : PG_FUNCTION_INFO_V1(iso8859_to_utf8);
      47           4 : PG_FUNCTION_INFO_V1(utf8_to_iso8859);
      48             : 
      49             : /* ----------
      50             :  * conv_proc(
      51             :  *      INTEGER,    -- source encoding id
      52             :  *      INTEGER,    -- destination encoding id
      53             :  *      CSTRING,    -- source string (null terminated C string)
      54             :  *      CSTRING,    -- destination string (null terminated C string)
      55             :  *      INTEGER     -- source string length
      56             :  * ) returns VOID;
      57             :  * ----------
      58             :  */
      59             : 
      60             : typedef struct
      61             : {
      62             :     pg_enc      encoding;
      63             :     const pg_mb_radix_tree *map1;   /* to UTF8 map name */
      64             :     const pg_mb_radix_tree *map2;   /* from UTF8 map name */
      65             : } pg_conv_map;
      66             : 
      67             : static const pg_conv_map maps[] = {
      68             :     {PG_LATIN2, &iso8859_2_to_unicode_tree,
      69             :     &iso8859_2_from_unicode_tree},  /* ISO-8859-2 Latin 2 */
      70             :     {PG_LATIN3, &iso8859_3_to_unicode_tree,
      71             :     &iso8859_3_from_unicode_tree},  /* ISO-8859-3 Latin 3 */
      72             :     {PG_LATIN4, &iso8859_4_to_unicode_tree,
      73             :     &iso8859_4_from_unicode_tree},  /* ISO-8859-4 Latin 4 */
      74             :     {PG_LATIN5, &iso8859_9_to_unicode_tree,
      75             :     &iso8859_9_from_unicode_tree},  /* ISO-8859-9 Latin 5 */
      76             :     {PG_LATIN6, &iso8859_10_to_unicode_tree,
      77             :     &iso8859_10_from_unicode_tree}, /* ISO-8859-10 Latin 6 */
      78             :     {PG_LATIN7, &iso8859_13_to_unicode_tree,
      79             :     &iso8859_13_from_unicode_tree}, /* ISO-8859-13 Latin 7 */
      80             :     {PG_LATIN8, &iso8859_14_to_unicode_tree,
      81             :     &iso8859_14_from_unicode_tree}, /* ISO-8859-14 Latin 8 */
      82             :     {PG_LATIN9, &iso8859_15_to_unicode_tree,
      83             :     &iso8859_15_from_unicode_tree}, /* ISO-8859-15 Latin 9 */
      84             :     {PG_LATIN10, &iso8859_16_to_unicode_tree,
      85             :     &iso8859_16_from_unicode_tree}, /* ISO-8859-16 Latin 10 */
      86             :     {PG_ISO_8859_5, &iso8859_5_to_unicode_tree,
      87             :     &iso8859_5_from_unicode_tree},  /* ISO-8859-5 */
      88             :     {PG_ISO_8859_6, &iso8859_6_to_unicode_tree,
      89             :     &iso8859_6_from_unicode_tree},  /* ISO-8859-6 */
      90             :     {PG_ISO_8859_7, &iso8859_7_to_unicode_tree,
      91             :     &iso8859_7_from_unicode_tree},  /* ISO-8859-7 */
      92             :     {PG_ISO_8859_8, &iso8859_8_to_unicode_tree,
      93             :     &iso8859_8_from_unicode_tree},  /* ISO-8859-8 */
      94             : };
      95             : 
      96             : Datum
      97          52 : iso8859_to_utf8(PG_FUNCTION_ARGS)
      98             : {
      99          52 :     int         encoding = PG_GETARG_INT32(0);
     100          52 :     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
     101          52 :     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
     102          52 :     int         len = PG_GETARG_INT32(4);
     103             :     int         i;
     104             : 
     105          52 :     CHECK_ENCODING_CONVERSION_ARGS(-1, PG_UTF8);
     106             : 
     107         364 :     for (i = 0; i < lengthof(maps); i++)
     108             :     {
     109         364 :         if (encoding == maps[i].encoding)
     110             :         {
     111          52 :             LocalToUtf(src, len, dest,
     112             :                        maps[i].map1,
     113             :                        NULL, 0,
     114             :                        NULL,
     115             :                        encoding);
     116          52 :             PG_RETURN_VOID();
     117             :         }
     118             :     }
     119             : 
     120           0 :     ereport(ERROR,
     121             :             (errcode(ERRCODE_INTERNAL_ERROR),
     122             :              errmsg("unexpected encoding ID %d for ISO 8859 character sets",
     123             :                     encoding)));
     124             : 
     125             :     PG_RETURN_VOID();
     126             : }
     127             : 
     128             : Datum
     129          52 : utf8_to_iso8859(PG_FUNCTION_ARGS)
     130             : {
     131          52 :     int         encoding = PG_GETARG_INT32(1);
     132          52 :     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
     133          52 :     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
     134          52 :     int         len = PG_GETARG_INT32(4);
     135             :     int         i;
     136             : 
     137          52 :     CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, -1);
     138             : 
     139         364 :     for (i = 0; i < lengthof(maps); i++)
     140             :     {
     141         364 :         if (encoding == maps[i].encoding)
     142             :         {
     143          52 :             UtfToLocal(src, len, dest,
     144             :                        maps[i].map2,
     145             :                        NULL, 0,
     146             :                        NULL,
     147             :                        encoding);
     148          52 :             PG_RETURN_VOID();
     149             :         }
     150             :     }
     151             : 
     152           0 :     ereport(ERROR,
     153             :             (errcode(ERRCODE_INTERNAL_ERROR),
     154             :              errmsg("unexpected encoding ID %d for ISO 8859 character sets",
     155             :                     encoding)));
     156             : 
     157             :     PG_RETURN_VOID();
     158             : }

Generated by: LCOV version 1.13