(root)/
glibc-2.38/
iconv/
tst-iconv9.c
       1  /* Verify that using C.UTF-8 works.
       2  
       3     Copyright (C) 2021-2023 Free Software Foundation, Inc.
       4     This file is part of the GNU C Library.
       5  
       6     The GNU C Library is free software; you can redistribute it and/or
       7     modify it under the terms of the GNU Lesser General Public
       8     License as published by the Free Software Foundation; either
       9     version 2.1 of the License, or (at your option) any later version.
      10  
      11     The GNU C Library is distributed in the hope that it will be useful,
      12     but WITHOUT ANY WARRANTY; without even the implied warranty of
      13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14     Lesser General Public License for more details.
      15  
      16     You should have received a copy of the GNU Lesser General Public
      17     License along with the GNU C Library; if not, see
      18     <https://www.gnu.org/licenses/>.  */
      19  
      20  #include <iconv.h>
      21  #include <stddef.h>
      22  #include <stdio.h>
      23  #include <string.h>
      24  #include <support/support.h>
      25  #include <support/check.h>
      26  
      27  /* This test does two things:
      28     (1) Verify that we have likely included translit_combining in C.UTF-8.
      29     (2) Verify default_missing is '?' as expected.  */
      30  
      31  /* ISO-8859-1 encoding of "für".  */
      32  char iso88591_in[] = { 0x66, 0xfc, 0x72, 0x0 };
      33  /* ASCII transliteration is "fur" with C.UTF-8 translit_combining.  */
      34  char ascii_exp[] = { 0x66, 0x75, 0x72, 0x0 };
      35  
      36  /* First 3-byte UTF-8 code point.  */
      37  char utf8_in[] = { 0xe0, 0xa0, 0x80, 0x0 };
      38  /* There is no ASCII transliteration for SAMARITAN LETTER ALAF
      39     so we get default_missing used which is '?'.  */
      40  char default_missing_exp[] = { 0x3f, 0x0 };
      41  
      42  static int
      43  do_test (void)
      44  {
      45    char ascii_out[5];
      46    iconv_t cd;
      47    char *inbuf;
      48    char *outbuf;
      49    size_t inbytes;
      50    size_t outbytes;
      51    size_t n;
      52  
      53    /* The C.UTF-8 locale should include translit_combining, which provides
      54       the transliteration for "LATIN SMALL LETTER U WITH DIAERESIS" which
      55       is not provided by locale/C-translit.h.in.  */
      56    xsetlocale (LC_ALL, "C.UTF-8");
      57  
      58    /* From ISO-8859-1 to ASCII.  */
      59    cd = iconv_open ("ASCII//TRANSLIT,IGNORE", "ISO-8859-1");
      60    TEST_VERIFY (cd != (iconv_t) -1);
      61    inbuf = iso88591_in;
      62    inbytes = 3;
      63    outbuf = ascii_out;
      64    outbytes = 3;
      65    n = iconv (cd, &inbuf, &inbytes, &outbuf, &outbytes);
      66    TEST_VERIFY (n != -1);
      67    *outbuf = '\0';
      68    TEST_COMPARE_BLOB (ascii_out, 3, ascii_exp, 3);
      69    TEST_VERIFY (iconv_close (cd) == 0);
      70  
      71    /* From UTF-8 to ASCII.  */
      72    cd = iconv_open ("ASCII//TRANSLIT,IGNORE", "UTF-8");
      73    TEST_VERIFY (cd != (iconv_t) -1);
      74    inbuf = utf8_in;
      75    inbytes = 3;
      76    outbuf = ascii_out;
      77    outbytes = 3;
      78    n = iconv (cd, &inbuf, &inbytes, &outbuf, &outbytes);
      79    TEST_VERIFY (n != -1);
      80    *outbuf = '\0';
      81    TEST_COMPARE_BLOB (ascii_out, 1, default_missing_exp, 1);
      82    TEST_VERIFY (iconv_close (cd) == 0);
      83  
      84    return 0;
      85  }
      86  
      87  #include <support/test-driver.c>