Fix exclusion of long file names when extracting from pax format archives.
[tar.git] / src / utf8.c
blobc9836d6273f661faae0da85029444a7eac3fbc70
1 /* Charset handling for GNU tar.
3 Copyright (C) 2004, 2006, 2007 Free Software Foundation, Inc.
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 3, or (at your option) any later
8 version.
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.
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. */
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
27 #ifndef ICONV_CONST
28 # define ICONV_CONST
29 #endif
31 #ifndef HAVE_ICONV
33 # undef iconv_open
34 # define iconv_open(tocode, fromcode) ((iconv_t) -1)
36 # undef iconv
37 # define iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft) ((size_t) 0)
39 # undef iconv_close
40 # define iconv_close(cd) 0
42 #endif
47 static iconv_t conv_desc[2] = { (iconv_t) -1, (iconv_t) -1 };
49 static iconv_t
50 utf8_init (bool to_utf)
52 if (conv_desc[(int) to_utf] == (iconv_t) -1)
54 if (to_utf)
55 conv_desc[(int) to_utf] = iconv_open ("UTF-8", locale_charset ());
56 else
57 conv_desc[(int) to_utf] = iconv_open (locale_charset (), "UTF-8");
59 return conv_desc[(int) to_utf];
62 bool
63 utf8_convert (bool to_utf, char const *input, char **output)
65 char ICONV_CONST *ib;
66 char *ob;
67 size_t inlen;
68 size_t outlen;
69 size_t rc;
70 iconv_t cd = utf8_init (to_utf);
72 if (cd == 0)
74 *output = xstrdup (input);
75 return true;
77 else if (cd == (iconv_t)-1)
78 return false;
80 inlen = strlen (input) + 1;
81 outlen = inlen * MB_LEN_MAX + 1;
82 ob = *output = xmalloc (outlen);
83 ib = (char ICONV_CONST *) input;
84 rc = iconv (cd, &ib, &inlen, &ob, &outlen);
85 *ob = 0;
86 return rc != -1;
90 bool
91 string_ascii_p (char const *p)
93 for (; *p; p++)
94 if (*p & ~0x7f)
95 return false;
96 return true;