1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011, 2012, 2013 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 "u8-istream.h"
33 #include "libpspp/assertion.h"
34 #include "libpspp/cast.h"
35 #include "libpspp/compiler.h"
36 #include "libpspp/encoding-guesser.h"
37 #include "libpspp/i18n.h"
39 #include "gl/c-strcase.h"
40 #include "gl/localcharset.h"
41 #include "gl/minmax.h"
45 S_AUTO, /* Stream encoding not yet known. */
46 S_UTF8, /* Stream encoding is known to be UTF-8. */
47 S_CONVERT /* Stream encoding is known but not UTF-8. */
54 enum u8_istream_state state;
64 static ssize_t fill_buffer (struct u8_istream *);
66 /* Opens FILENAME, which is encoded in FROMCODE, for reading as an UTF-8
67 stream, passing FLAGS to the open() function. Returns a new u8_istream if
68 successful, otherwise returns NULL and sets errno to an appropriate value.
70 The accepted forms for FROMCODE are listed at the top of
71 encoding-guesser.h. */
73 u8_istream_for_file (const char *fromcode, const char *filename, int flags)
75 struct u8_istream *is;
78 assert (!(flags & O_CREAT));
80 fd = open (filename, flags);
84 is = u8_istream_for_fd (fromcode, fd);
87 int save_errno = errno;
95 /* Creates and returns a new u8_istream that reads its input from FD. Returns
96 a new u8_istream if successful, otherwise returns NULL and sets errno to an
99 The accepted forms for FROMCODE are listed at the top of
100 encoding-guesser.h. */
102 u8_istream_for_fd (const char *fromcode, int fd)
104 struct u8_istream *is;
105 const char *encoding;
107 is = malloc (sizeof *is);
112 is->converter = (iconv_t) -1;
113 is->buffer = malloc (U8_ISTREAM_BUFFER_SIZE);
114 if (is->buffer == NULL)
116 is->head = is->buffer;
120 if (fill_buffer (is) < 0)
123 encoding = encoding_guess_head_encoding (fromcode, is->buffer, is->length);
124 if (is_encoding_utf8 (encoding))
126 unsigned int bom_len;
129 bom_len = encoding_guess_bom_length (encoding, is->buffer, is->length);
131 is->length -= bom_len;
135 if (encoding_guess_encoding_is_auto (fromcode)
136 && !strcmp (encoding, "ASCII"))
139 is->state = S_CONVERT;
141 is->converter = iconv_open ("UTF-8",
142 encoding_guess_parse_encoding (fromcode));
143 if (is->converter == (iconv_t) -1)
150 u8_istream_free (is);
154 /* Closes IS and its underlying file descriptor and frees all associated
155 resources. Returns the return value from close(). */
157 u8_istream_close (struct u8_istream *is)
162 u8_istream_free (is);
168 /* Frees IS and associated resources, but does not close the underlying file
169 descriptor. (Thus, the client must close the file descriptor when it is no
172 u8_istream_free (struct u8_istream *is)
176 if (is->converter != (iconv_t) -1)
177 iconv_close (is->converter);
184 substitute_invalid_input_byte (struct u8_istream *is)
186 assert (is->outlen == 0);
189 is->outlen = u8_uctomb (CHAR_CAST (uint8_t *, is->outbuf),
190 0xfffd, sizeof is->outbuf);
194 fill_buffer (struct u8_istream *is)
198 /* Move any unused bytes to the beginning of the input buffer. */
199 if (is->length > 0 && is->buffer != is->head)
200 memmove (is->buffer, is->head, is->length);
201 is->head = is->buffer;
203 /* Read more input. */
206 n = read (is->fd, is->buffer + is->length,
207 U8_ISTREAM_BUFFER_SIZE - is->length);
209 while (n < 0 && errno == EINTR);
216 read_auto (struct u8_istream *is, char *buffer, size_t size)
218 size_t original_size = size;
227 n_ascii = encoding_guess_count_ascii (is->head,
228 MIN (is->length, size));
230 memcpy (buffer, is->head, n_ascii);
235 is->length -= n_ascii;
243 retval = fill_buffer (is);
250 /* is->head points to a byte that isn't a printable ASCII character.
251 Fill up the buffer and check for UTF-8. */
253 is->state = (encoding_guess_tail_is_utf8 (is->head, is->length)
254 ? S_UTF8 : S_CONVERT);
255 if (size == original_size)
256 return u8_istream_read (is, buffer, size);
260 return original_size - size;
264 convert_iconv (iconv_t converter,
265 char **inbufp, size_t *inbytesleft,
266 char **outbufp, size_t *outbytesleft)
268 size_t n = iconv (converter, (ICONV_CONST char **) inbufp, inbytesleft,
269 outbufp, outbytesleft);
270 return n == SIZE_MAX ? errno : 0;
274 convert_utf8 (iconv_t converter UNUSED,
275 char **inbufp, size_t *inbytesleft,
276 char **outbufp, size_t *outbytesleft)
278 const uint8_t *in = CHAR_CAST (const uint8_t *, *inbufp);
279 size_t n = MIN (*inbytesleft, *outbytesleft);
290 error = ofs < *inbytesleft ? E2BIG : 0;
294 mblen = u8_mbtouc (&uc, in + ofs, n - ofs);
297 int retval = u8_mbtoucr (&uc, in + ofs, *inbytesleft - ofs);
300 /* There's an actual U+FFFD in the input stream. Carry on. */
304 error = (retval == -1 ? EILSEQ
305 : retval == -2 ? EINVAL
316 memcpy (*outbufp, *inbufp, ofs);
320 *outbytesleft -= ofs;
327 read_convert (struct u8_istream *is,
328 int (*convert) (iconv_t converter,
329 char **inbufp, size_t *inbytesleft,
330 char **outbufp, size_t *outbytesleft),
331 char *buffer, size_t size)
333 size_t original_size = size;
341 size_t n = MIN (size, is->outlen);
343 memcpy (buffer, is->outbuf, n);
346 memmove (is->outbuf, is->outbuf + n, is->outlen);
357 int error = convert (is->converter,
358 &is->head, &is->length,
366 /* Converted all of the input into output, possibly with space
367 for output left over.
373 substitute_invalid_input_byte (is);
377 /* Incomplete byte sequence at end of input. Read more
382 /* A real error of some kind (ENOMEM?). */
386 /* Ran out of room for output.
387 Convert into outbuf and copy from there instead. */
389 char *outptr = is->outbuf;
390 size_t outleft = sizeof is->outbuf;
392 error = convert (is->converter,
393 &is->head, &is->length,
395 is->outlen = outptr - is->outbuf;
402 substitute_invalid_input_byte (is);
410 /* A real error of some kind (ENOMEM?). */
417 assert (is->length <= MB_LEN_MAX);
418 n_read = fill_buffer (is);
421 if (original_size != size)
423 /* We produced some output so don't report EOF or error yet. */
426 else if (n_read == 0 && is->length != 0)
428 /* Incomplete byte sequence at end of file. */
429 substitute_invalid_input_byte (is);
433 /* Propagate end-of-file or error to caller. */
439 return original_size - size;
442 /* Reads up to SIZE bytes of UTF-8 text from IS into BUFFER. Returns the
443 number of bytes read if successful, 0 at end of file, or -1 if an error
444 occurred before any data could be read. Upon error, sets errno to an
445 appropriate value. */
447 u8_istream_read (struct u8_istream *is, char *buffer, size_t size)
452 return read_convert (is, convert_iconv, buffer, size);
455 return read_auto (is, buffer, size);
458 return read_convert (is, convert_utf8, buffer, size);
464 /* Returns the file descriptor underlying IS. */
466 u8_istream_fileno (const struct u8_istream *is)
473 These functions are probably useful only for white-box testing. */
475 /* Returns true if the encoding of the file being read by IS is not yet
478 u8_istream_is_auto (const struct u8_istream *is)
480 return is->state == S_AUTO;
483 /* Returns true if the encoding of the file being read by IS has been
484 determined to be UTF-8. */
486 u8_istream_is_utf8 (const struct u8_istream *is)
488 return is->state == S_UTF8;