1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011, 2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "libpspp/encoding-guesser.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/i18n.h"
32 #include "gl/localcharset.h"
33 #include "gl/c-strcase.h"
35 /* http://www.w3.org/TR/REC-xml/#sec-guessing-no-ext-info is a useful source
36 of information about encoding detection.
39 /* Returns the encoding specified by ENCODING, which must be in one of the
40 forms described at the top of encoding-guesser.h. The returned string might
41 be ENCODING itself or a suffix of it, or it might be a statically allocated
44 encoding_guess_parse_encoding (const char *encoding)
49 || !c_strcasecmp (encoding, "auto")
50 || !c_strcasecmp (encoding, "auto,locale")
51 || !c_strcasecmp (encoding, "locale"))
52 fallback = locale_charset ();
53 else if (!c_strncasecmp (encoding, "auto,", 5))
54 fallback = encoding + 5;
58 return is_encoding_utf8 (fallback) ? "windows-1252" : fallback;
61 /* Returns true if ENCODING, which must be in one of the forms described at the
62 top of encoding-guesser.h, is one that performs encoding autodetection,
65 encoding_guess_encoding_is_auto (const char *encoding)
67 return (encoding == NULL
68 || (!c_strncasecmp (encoding, "auto", 4)
69 && (encoding[4] == ',' || encoding[4] == '\0')));
73 get_be16 (const uint8_t *data)
75 return (data[0] << 8) | data[1];
79 get_le16 (const uint8_t *data)
81 return (data[1] << 8) | data[0];
85 get_be32 (const uint8_t *data)
87 return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
92 get_le32 (const uint8_t *data)
94 return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
98 guess_utf16 (const uint8_t *data, size_t n)
100 size_t even_nulls, odd_nulls;
102 if (n < ENCODING_GUESS_MIN && n % 2 != 0)
105 even_nulls = odd_nulls = 0;
108 even_nulls += data[0] == 0;
109 odd_nulls += data[1] == 0;
110 if (data[0] == 0 && data[1] == 0)
117 if (odd_nulls > even_nulls)
119 else if (even_nulls > 0)
126 is_utf32 (const uint8_t *data, size_t n, uint32_t (*get_u32) (const uint8_t *))
128 if (n < ENCODING_GUESS_MIN && n % 4 != 0)
133 uint32_t uc = get_u32 (data);
135 if (uc < 0x09 || uc > 0x10ffff)
145 /* Counts and returns the number of bytes, but no more than N, starting at S
146 that are ASCII text characters. */
148 encoding_guess_count_ascii (const void *s_, size_t n)
150 const uint8_t *s = s_;
153 for (ofs = 0; ofs < n; ofs++)
154 if (!encoding_guess_is_ascii_text (s[ofs]))
160 is_all_utf8_text (const void *s_, size_t n)
162 const uint8_t *s = s_;
171 if (!encoding_guess_is_ascii_text (c))
180 mblen = u8_mbtoucr (&uc, s + ofs, n - ofs);
190 /* Attempts to guess the encoding of a text file based on ENCODING, an encoding
191 name in one of the forms described at the top of encoding-guesser.h, and
192 DATA, which contains the first N bytes of the file. Returns the guessed
193 encoding, which might be ENCODING itself or a suffix of it or a statically
196 Encoding autodetection only takes place if ENCODING actually specifies
197 autodetection. See encoding-guesser.h for details.
199 UTF-8 cannot be distinguished from other ASCII-based encodings until a
200 non-ASCII text character is encountered. If ENCODING specifies
201 autodetection and this function returns "ASCII", then the client should
202 process the input until it encounters an non-ASCII character (as returned by
203 encoding_guess_is_ascii_text()) and then use encoding_guess_tail_encoding()
204 to make a final encoding guess. See encoding-guesser.h for details.
206 N must be at least ENCODING_GUESS_MIN, unless the file is shorter than
209 encoding_guess_head_encoding (const char *encoding,
210 const void *data_, size_t n)
212 const uint8_t *data = data_;
213 const char *fallback_encoding;
216 fallback_encoding = encoding_guess_parse_encoding (encoding);
217 if (!encoding_guess_encoding_is_auto (encoding))
218 return fallback_encoding;
221 return fallback_encoding;
223 if ((n >= ENCODING_GUESS_MIN || n % 4 == 0)
224 && (get_be32 (data) == 0xfeff || get_le32 (data) == 0xfeff))
229 uint32_t x = get_be32 (data);
232 else if (x == 0xdd736673)
236 if ((n >= ENCODING_GUESS_MIN || n % 2 == 0)
237 && (get_be16 (data) == 0xfeff || get_le16 (data) == 0xfeff))
240 if (n >= 3 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf)
243 guess = guess_utf16 (data, n);
247 if (is_utf32 (data, n, get_be32))
249 if (is_utf32 (data, n, get_le32))
252 if (!is_encoding_ascii_compatible (fallback_encoding)
253 || !encoding_guess_tail_is_utf8 (data, n))
254 return fallback_encoding;
259 /* Returns an encoding guess based on ENCODING and the N bytes of text starting
260 at DATA. DATA should start with the first non-ASCII text character (as
261 determined by encoding_guess_is_ascii_text()) found in the input.
263 The return value will either be "UTF-8" or the fallback encoding for
266 See encoding-guesser.h for intended use of this function.
268 N must be at least ENCODING_GUESS_MIN, unless the file has fewer bytes than
269 that starting with the first non-ASCII text character. */
271 encoding_guess_tail_encoding (const char *encoding,
272 const void *data, size_t n)
274 return (encoding_guess_tail_is_utf8 (data, n) != 0
276 : encoding_guess_parse_encoding (encoding));
279 /* Returns an encoding guess based on ENCODING and the N bytes of text starting
280 at DATA. DATA should start with the first non-ASCII text character (as
281 determined by encoding_guess_is_ascii_text()) found in the input.
285 0, if the encoding is definitely not UTF-8 (because the input contains
286 byte sequences that are not valid in UTF-8).
288 1, if the encoding appears to be UTF-8 (because the input contains valid
289 UTF-8 multibyte sequences).
291 -1, if the input contains only ASCII characters. (This means that the
292 input may be treated as UTF-8, since ASCII is a subset of UTF-8.)
294 See encoding-guesser.h for intended use of this function.
296 N must be at least ENCODING_GUESS_MIN, unless the file has fewer bytes than
297 that starting with the first non-ASCII text character. */
299 encoding_guess_tail_is_utf8 (const void *data, size_t n)
301 /* If all the bytes are in the ASCII range, it's just ASCII. */
302 if (encoding_guess_count_ascii (data, n) == n)
305 return (n < ENCODING_GUESS_MIN
306 ? u8_check (data, n) == NULL
307 : is_all_utf8_text (data, n));
310 /* Attempts to guess the encoding of a text file based on ENCODING, an encoding
311 name in one of the forms described at the top of encoding-guesser.h, and the
312 SIZE byts in DATA, which contains the entire contents of the file. Returns
313 the guessed encoding, which might be ENCODING itself or a suffix of it or a
314 statically allocated string.
316 Encoding autodetection only takes place if ENCODING actually specifies
317 autodetection. See encoding-guesser.h for details. */
319 encoding_guess_whole_file (const char *encoding, const void *text, size_t size)
323 guess = encoding_guess_head_encoding (encoding, text, size);
324 if (!strcmp (guess, "ASCII") && encoding_guess_encoding_is_auto (encoding))
325 return encoding_guess_tail_encoding (encoding, text, size);