1 /* Charset handling for GNU tar.
3 Copyright 2004, 2006-2007, 2013-2014, 2016-2017 Free Software
6 This file is part of GNU tar.
8 GNU tar is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GNU tar is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <localcharset.h>
36 # define iconv_open(tocode, fromcode) ((iconv_t) -1)
39 # define iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft) ((size_t) 0)
42 # define iconv_close(cd) 0
49 static iconv_t conv_desc
[2] = { (iconv_t
) -1, (iconv_t
) -1 };
52 utf8_init (bool to_utf
)
54 if (conv_desc
[(int) to_utf
] == (iconv_t
) -1)
57 conv_desc
[(int) to_utf
] = iconv_open ("UTF-8", locale_charset ());
59 conv_desc
[(int) to_utf
] = iconv_open (locale_charset (), "UTF-8");
61 return conv_desc
[(int) to_utf
];
65 utf8_convert (bool to_utf
, char const *input
, char **output
)
71 iconv_t cd
= utf8_init (to_utf
);
75 *output
= xstrdup (input
);
78 else if (cd
== (iconv_t
)-1)
81 inlen
= strlen (input
) + 1;
82 outlen
= inlen
* MB_LEN_MAX
+ 1;
83 ob
= ret
= xmalloc (outlen
);
84 ib
= (char ICONV_CONST
*) input
;
85 if (iconv (cd
, &ib
, &inlen
, &ob
, &outlen
) == -1)
97 string_ascii_p (char const *p
)