1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011 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 /* Parses and returns the fallback encoding from ENCODING, which must be in one
40 of the forms described at the top of encoding-guesser.h. The returned
41 string might be ENCODING itself or a suffix of it, or it might be a
42 statically allocated string. */
44 encoding_guess_parse_encoding (const char *encoding)
47 || !c_strcasecmp (encoding, "auto")
48 || !c_strcasecmp (encoding, "auto,locale")
49 || !c_strcasecmp (encoding, "locale"))
50 return locale_charset ();
51 else if (!c_strncasecmp (encoding, "auto,", 5))
57 /* Returns true if ENCODING, which must be in one of the forms described at the
58 top of encoding-guesser.h, is one that performs encoding autodetection,
61 encoding_guess_encoding_is_auto (const char *encoding)
63 return (encoding == NULL
64 || (!c_strncasecmp (encoding, "auto", 4)
65 && (encoding[4] == ',' || encoding[4] == '\0')));
69 get_be16 (const uint8_t *data)
71 return (data[0] << 8) | data[1];
75 get_le16 (const uint8_t *data)
77 return (data[1] << 8) | data[0];
81 get_be32 (const uint8_t *data)
83 return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
88 get_le32 (const uint8_t *data)
90 return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
95 guess_utf16 (const uint8_t *data, size_t n)
97 size_t even_nulls, odd_nulls;
99 if (n < ENCODING_GUESS_MIN && n % 2 != 0)
102 even_nulls = odd_nulls = 0;
105 even_nulls += data[0] == 0;
106 odd_nulls += data[1] == 0;
107 if (data[0] == 0 && data[1] == 0)
114 if (odd_nulls > even_nulls)
116 else if (even_nulls > 0)
123 is_utf32 (const uint8_t *data, size_t n, uint32_t (*get_u32) (const uint8_t *))
125 if (n < ENCODING_GUESS_MIN && n % 4 != 0)
130 uint32_t uc = get_u32 (data);
132 if (uc < 0x09 || uc > 0x10ffff)
142 /* Counts and returns the number of bytes, but no more than N, starting at S
143 that are ASCII text characters. */
145 encoding_guess_count_ascii (const void *s_, size_t n)
147 const uint8_t *s = s_;
150 for (ofs = 0; ofs < n; ofs++)
151 if (!encoding_guess_is_ascii_text (s[ofs]))
157 is_all_utf8_text (const void *s_, size_t n)
159 const uint8_t *s = s_;
168 if (!encoding_guess_is_ascii_text (c))
177 mblen = u8_mbtoucr (&uc, s + ofs, n - ofs);
187 /* Attempts to guess the encoding of a text file based on ENCODING, an encoding
188 name in one of the forms described at the top of encoding-guesser.h, and
189 DATA, which contains the first N bytes of the file. Returns the guessed
190 encoding, which might be ENCODING itself or a suffix of it or a statically
193 Encoding autodetection only takes place if ENCODING actually specifies
194 autodetection. See encoding-guesser.h for details.
196 UTF-8 cannot be distinguished from other ASCII-based encodings until a
197 non-ASCII text character is encountered. If ENCODING specifies
198 autodetection and this function returns "ASCII", then the client should
199 process the input until it encounters an non-ASCII character (as returned by
200 encoding_guess_is_ascii_text()) and then use encoding_guess_tail_encoding()
201 to make a final encoding guess. See encoding-guesser.h for details.
203 N must be at least ENCODING_GUESS_MIN, unless the file is shorter than
206 encoding_guess_head_encoding (const char *encoding,
207 const void *data_, size_t n)
209 const uint8_t *data = data_;
210 const char *fallback_encoding;
213 fallback_encoding = encoding_guess_parse_encoding (encoding);
214 if (!encoding_guess_encoding_is_auto (encoding))
215 return fallback_encoding;
218 return fallback_encoding;
220 if ((n >= ENCODING_GUESS_MIN || n % 4 == 0)
221 && (get_be32 (data) == 0xfeff || get_le32 (data) == 0xfeff))
226 uint32_t x = get_be32 (data);
229 else if (x == 0xdd736673)
233 if ((n >= ENCODING_GUESS_MIN || n % 2 == 0)
234 && (get_be16 (data) == 0xfeff || get_le16 (data) == 0xfeff))
237 if (n >= 3 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf)
240 guess = guess_utf16 (data, n);
244 if (is_utf32 (data, n, get_be32))
246 if (is_utf32 (data, n, get_le32))
249 if (!is_encoding_ascii_compatible (fallback_encoding)
250 || !encoding_guess_tail_is_utf8 (data, n))
251 return fallback_encoding;
256 /* Returns an encoding guess based on ENCODING and the N bytes of text starting
257 at DATA. DATA should start with the first non-ASCII text character (as
258 determined by encoding_guess_is_ascii_text()) found in the input.
260 The return value will either be "UTF-8" or the fallback encoding for
263 See encoding-guesser.h for intended use of this function.
265 N must be at least ENCODING_GUESS_MIN, unless the file has fewer bytes than
266 that starting with the first non-ASCII text character. */
268 encoding_guess_tail_encoding (const char *encoding,
269 const void *data, size_t n)
271 return (encoding_guess_tail_is_utf8 (data, n)
273 : encoding_guess_parse_encoding (encoding));
276 /* Same as encoding_guess_tail_encoding() but returns true for UTF-8 or false
277 for the fallback encoding. */
279 encoding_guess_tail_is_utf8 (const void *data, size_t n)
281 return (n < ENCODING_GUESS_MIN
282 ? u8_check (data, n) == NULL
283 : is_all_utf8_text (data, n));
286 /* Attempts to guess the encoding of a text file based on ENCODING, an encoding
287 name in one of the forms described at the top of encoding-guesser.h, and the
288 SIZE byts in DATA, which contains the entire contents of the file. Returns
289 the guessed encoding, which might be ENCODING itself or a suffix of it or a
290 statically allocated string.
292 Encoding autodetection only takes place if ENCODING actually specifies
293 autodetection. See encoding-guesser.h for details. */
295 encoding_guess_whole_file (const char *encoding, const void *text, size_t size)
299 guess = encoding_guess_head_encoding (encoding, text, size);
300 if (!strcmp (guess, "ASCII") && encoding_guess_encoding_is_auto (encoding))
302 size_t ofs = encoding_guess_count_ascii (text, size);
304 return encoding_guess_tail_encoding (encoding,
305 (const char *) text + ofs,
308 return encoding_guess_parse_encoding (encoding);