1 | /* Charset handling for GNU tar.
|
---|
2 |
|
---|
3 | Copyright (C) 2004 Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | This program is free software; you can redistribute it and/or modify it
|
---|
6 | under the terms of the GNU General Public License as published by the
|
---|
7 | Free Software Foundation; either version 2, or (at your option) any later
|
---|
8 | version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful, but
|
---|
11 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
---|
13 | Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License along
|
---|
16 | with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
17 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
---|
18 |
|
---|
19 | #include <system.h>
|
---|
20 | #include <quotearg.h>
|
---|
21 | #include <localcharset.h>
|
---|
22 | #include "common.h"
|
---|
23 | #ifdef HAVE_ICONV_H
|
---|
24 | # include <iconv.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #ifndef ICONV_CONST
|
---|
28 | # define ICONV_CONST
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #ifndef HAVE_ICONV
|
---|
32 |
|
---|
33 | # undef iconv_open
|
---|
34 | # define iconv_open(tocode, fromcode) ((iconv_t) -1)
|
---|
35 |
|
---|
36 | # undef iconv
|
---|
37 | # define iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft) ((size_t) 0)
|
---|
38 |
|
---|
39 | # undef iconv_close
|
---|
40 | # define iconv_close(cd) 0
|
---|
41 |
|
---|
42 | #endif
|
---|
43 |
|
---|
44 |
|
---|
45 | |
---|
46 |
|
---|
47 |
|
---|
48 | static iconv_t conv_desc[2] = { (iconv_t) -1, (iconv_t) -1 };
|
---|
49 |
|
---|
50 | static iconv_t
|
---|
51 | utf8_init (bool to_utf)
|
---|
52 | {
|
---|
53 | if (conv_desc[(int) to_utf] == (iconv_t) -1)
|
---|
54 | {
|
---|
55 | if (to_utf)
|
---|
56 | conv_desc[(int) to_utf] = iconv_open ("UTF-8", locale_charset ());
|
---|
57 | else
|
---|
58 | conv_desc[(int) to_utf] = iconv_open (locale_charset (), "UTF-8");
|
---|
59 | }
|
---|
60 | return conv_desc[(int) to_utf];
|
---|
61 | }
|
---|
62 |
|
---|
63 | bool
|
---|
64 | utf8_convert (bool to_utf, char const *input, char **output)
|
---|
65 | {
|
---|
66 | char ICONV_CONST *ib;
|
---|
67 | char *ob;
|
---|
68 | size_t inlen;
|
---|
69 | size_t outlen;
|
---|
70 | size_t rc;
|
---|
71 | iconv_t cd = utf8_init (to_utf);
|
---|
72 |
|
---|
73 | if (cd == 0)
|
---|
74 | {
|
---|
75 | *output = xstrdup (input);
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 | else if (cd == (iconv_t)-1)
|
---|
79 | return false;
|
---|
80 |
|
---|
81 | inlen = strlen (input) + 1;
|
---|
82 | outlen = inlen * MB_LEN_MAX + 1;
|
---|
83 | ob = *output = xmalloc (outlen);
|
---|
84 | ib = (char ICONV_CONST *) input;
|
---|
85 | rc = iconv (cd, &ib, &inlen, &ob, &outlen);
|
---|
86 | *ob = 0;
|
---|
87 | return rc != -1;
|
---|
88 | }
|
---|
89 | |
---|
90 |
|
---|
91 |
|
---|
92 | bool
|
---|
93 | string_ascii_p (const char *str)
|
---|
94 | {
|
---|
95 | const unsigned char *p = (const unsigned char *)str;
|
---|
96 | for (; *p; p++)
|
---|
97 | if (*p > 127)
|
---|
98 | return false;
|
---|
99 | return true;
|
---|
100 | }
|
---|