summaryrefslogtreecommitdiff
path: root/src/utf8.c
blob: 22dc92b9dbc9a21727167f3e2c57642c512bae72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Charset handling for GNU tar.

   Copyright 2004-2021 Free Software Foundation, Inc.

   This file is part of GNU tar.

   GNU tar is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   GNU tar is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <system.h>
#include <quotearg.h>
#include <localcharset.h>
#include "common.h"
#ifdef HAVE_ICONV_H
# include <iconv.h>
#endif

#ifndef ICONV_CONST
# define ICONV_CONST
#endif

#ifndef HAVE_ICONV

# undef iconv_open
# define iconv_open(tocode, fromcode) ((iconv_t) -1)

# undef iconv
# define iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft) (errno = ENOSYS, (size_t) -1)

# undef iconv_close
# define iconv_close(cd) 0

# undef iconv_t
# define iconv_t int

#endif




static iconv_t conv_desc[2] = { (iconv_t) -1, (iconv_t) -1 };

static iconv_t
utf8_init (bool to_utf)
{
  if (conv_desc[(int) to_utf] == (iconv_t) -1)
    {
      if (to_utf)
	conv_desc[(int) to_utf] = iconv_open ("UTF-8", locale_charset ());
      else
	conv_desc[(int) to_utf] = iconv_open (locale_charset (), "UTF-8");
    }
  return conv_desc[(int) to_utf];
}

bool
utf8_convert (bool to_utf, char const *input, char **output)
{
  char ICONV_CONST *ib;
  char *ob, *ret;
  size_t inlen;
  size_t outlen;
  iconv_t cd = utf8_init (to_utf);

  if (cd == 0)
    {
      *output = xstrdup (input);
      return true;
    }
  else if (cd == (iconv_t)-1)
    return false;

  inlen = strlen (input) + 1;
  outlen = inlen * MB_LEN_MAX + 1;
  ob = ret = xmalloc (outlen);
  ib = (char ICONV_CONST *) input;
  /* According to POSIX, "if iconv() encounters a character in the input
     buffer that is valid, but for which an identical character does not
     exist in the target codeset, iconv() shall perform an
     implementation-defined conversion on this character." It will "update
     the variables pointed to by the arguments to reflect the extent of the
     conversion and return the number of non-identical conversions performed".
     On error, it returns -1. 
     In other words, non-zero return always indicates failure, either because
     the input was not fully converted, or because it was converted in a
     non-reversible way.
   */
  if (iconv (cd, &ib, &inlen, &ob, &outlen) != 0)
    {
      free (ret);
      return false;
    }
  *ob = 0;
  *output = ret;
  return true;
}


bool
string_ascii_p (char const *p)
{
  for (; *p; p++)
    if (*p & ~0x7f)
      return false;
  return true;
}