LCOV - code coverage report
Current view: top level - src/backend/utils/mb/conversion_procs/utf8_and_iso8859_1 - utf8_and_iso8859_1.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 40 47 85.1 %
Date: 2019-08-24 15:07:19 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  *    ISO8859_1 <--> 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_1/utf8_and_iso8859_1.c
      10             :  *
      11             :  *-------------------------------------------------------------------------
      12             :  */
      13             : 
      14             : #include "postgres.h"
      15             : #include "fmgr.h"
      16             : #include "mb/pg_wchar.h"
      17             : 
      18          30 : PG_MODULE_MAGIC;
      19             : 
      20          30 : PG_FUNCTION_INFO_V1(iso8859_1_to_utf8);
      21          10 : PG_FUNCTION_INFO_V1(utf8_to_iso8859_1);
      22             : 
      23             : /* ----------
      24             :  * conv_proc(
      25             :  *      INTEGER,    -- source encoding id
      26             :  *      INTEGER,    -- destination encoding id
      27             :  *      CSTRING,    -- source string (null terminated C string)
      28             :  *      CSTRING,    -- destination string (null terminated C string)
      29             :  *      INTEGER     -- source string length
      30             :  * ) returns VOID;
      31             :  * ----------
      32             :  */
      33             : 
      34             : Datum
      35          68 : iso8859_1_to_utf8(PG_FUNCTION_ARGS)
      36             : {
      37          68 :     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
      38          68 :     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
      39          68 :     int         len = PG_GETARG_INT32(4);
      40             :     unsigned short c;
      41             : 
      42          68 :     CHECK_ENCODING_CONVERSION_ARGS(PG_LATIN1, PG_UTF8);
      43             : 
      44        2144 :     while (len > 0)
      45             :     {
      46        2008 :         c = *src;
      47        2008 :         if (c == 0)
      48           0 :             report_invalid_encoding(PG_LATIN1, (const char *) src, len);
      49        2008 :         if (!IS_HIGHBIT_SET(c))
      50        2008 :             *dest++ = c;
      51             :         else
      52             :         {
      53           0 :             *dest++ = (c >> 6) | 0xc0;
      54           0 :             *dest++ = (c & 0x003f) | HIGHBIT;
      55             :         }
      56        2008 :         src++;
      57        2008 :         len--;
      58             :     }
      59          68 :     *dest = '\0';
      60             : 
      61          68 :     PG_RETURN_VOID();
      62             : }
      63             : 
      64             : Datum
      65         230 : utf8_to_iso8859_1(PG_FUNCTION_ARGS)
      66             : {
      67         230 :     unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
      68         230 :     unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
      69         230 :     int         len = PG_GETARG_INT32(4);
      70             :     unsigned short c,
      71             :                 c1;
      72             : 
      73         230 :     CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_LATIN1);
      74             : 
      75         230 :     while (len > 0)
      76             :     {
      77        2096 :         c = *src;
      78        2096 :         if (c == 0)
      79           0 :             report_invalid_encoding(PG_UTF8, (const char *) src, len);
      80             :         /* fast path for ASCII-subset characters */
      81        2096 :         if (!IS_HIGHBIT_SET(c))
      82             :         {
      83        2094 :             *dest++ = c;
      84        2094 :             src++;
      85        2094 :             len--;
      86             :         }
      87             :         else
      88             :         {
      89           2 :             int         l = pg_utf_mblen(src);
      90             : 
      91           2 :             if (l > len || !pg_utf8_islegal(src, l))
      92           0 :                 report_invalid_encoding(PG_UTF8, (const char *) src, len);
      93           2 :             if (l != 2)
      94           0 :                 report_untranslatable_char(PG_UTF8, PG_LATIN1,
      95             :                                            (const char *) src, len);
      96           2 :             c1 = src[1] & 0x3f;
      97           2 :             c = ((c & 0x1f) << 6) | c1;
      98           2 :             if (c >= 0x80 && c <= 0xff)
      99             :             {
     100           2 :                 *dest++ = (unsigned char) c;
     101           2 :                 src += 2;
     102           2 :                 len -= 2;
     103             :             }
     104             :             else
     105           0 :                 report_untranslatable_char(PG_UTF8, PG_LATIN1,
     106             :                                            (const char *) src, len);
     107             :         }
     108             :     }
     109         230 :     *dest = '\0';
     110             : 
     111         230 :     PG_RETURN_VOID();
     112             : }

Generated by: LCOV version 1.13