1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 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 "u8-istream.h"
32 #include "libpspp/assertion.h"
33 #include "libpspp/cast.h"
34 #include "libpspp/compiler.h"
35 #include "libpspp/encoding-guesser.h"
37 #include "gl/c-strcase.h"
38 #include "gl/localcharset.h"
39 #include "gl/minmax.h"
43 S_AUTO, /* Stream encoding not yet known. */
44 S_UTF8, /* Stream encoding is known to be UTF-8. */
45 S_CONVERT /* Stream encoding is known but not UTF-8. */
52 enum u8_istream_state state;
62 static ssize_t fill_buffer (struct u8_istream *);
64 /* Opens FILENAME, which is encoded in FROMCODE, for reading as an UTF-8
65 stream, passing FLAGS to the open() function. Returns a new u8_istream if
66 successful, otherwise returns NULL and sets errno to an appropriate value.
68 The accepted forms for FROMCODE are listed at the top of
69 encoding-guesser.h. */
71 u8_istream_for_file (const char *fromcode, const char *filename, int flags)
73 struct u8_istream *is;
76 assert (!(flags & O_CREAT));
78 fd = open (filename, flags);
82 is = u8_istream_for_fd (fromcode, fd);
85 int save_errno = errno;
93 /* Creates and returns a new u8_istream that reads its input from FD. Returns
94 a new u8_istream if successful, otherwise returns NULL and sets errno to an
97 The accepted forms for FROMCODE are listed at the top of
98 encoding-guesser.h. */
100 u8_istream_for_fd (const char *fromcode, int fd)
102 struct u8_istream *is;
103 const char *encoding;
105 is = malloc (sizeof *is);
110 is->converter = (iconv_t) -1;
111 is->buffer = malloc (U8_ISTREAM_BUFFER_SIZE);
112 if (is->buffer == NULL)
114 is->head = is->buffer;
118 if (fill_buffer (is) < 0)
121 encoding = encoding_guess_head_encoding (fromcode, is->buffer, is->length);
122 if (!strcmp (encoding, "UTF-8"))
126 if (encoding_guess_encoding_is_auto (fromcode)
127 && !strcmp (encoding, "ASCII"))
130 is->state = S_CONVERT;
132 is->converter = iconv_open ("UTF-8",
133 encoding_guess_parse_encoding (fromcode));
134 if (is->converter == (iconv_t) -1)
141 u8_istream_free (is);
145 /* Closes IS and its underlying file descriptor and frees all associated
146 resources. Returns the return value from close(). */
148 u8_istream_close (struct u8_istream *is)
153 u8_istream_free (is);
159 /* Frees IS and associated resources, but does not close the underlying file
160 descriptor. (Thus, the client must close the file descriptor when it is no
163 u8_istream_free (struct u8_istream *is)
167 if (is->converter != (iconv_t) -1)
168 iconv_close (is->converter);
175 substitute_invalid_input_byte (struct u8_istream *is)
177 assert (is->outlen == 0);
180 is->outlen = u8_uctomb (CHAR_CAST (uint8_t *, is->outbuf),
181 0xfffd, sizeof is->outbuf);
185 fill_buffer (struct u8_istream *is)
189 /* Move any unused bytes to the beginning of the input buffer. */
190 if (is->length > 0 && is->buffer != is->head)
191 memmove (is->buffer, is->head, is->length);
192 is->head = is->buffer;
194 /* Read more input. */
195 n = read (is->fd, is->buffer + is->length,
196 U8_ISTREAM_BUFFER_SIZE - is->length);
203 read_auto (struct u8_istream *is, char *buffer, size_t size)
205 size_t original_size = size;
214 n_ascii = encoding_guess_count_ascii (is->head,
215 MIN (is->length, size));
217 memcpy (buffer, is->head, n_ascii);
222 is->length -= n_ascii;
230 retval = fill_buffer (is);
237 /* is->head points to a byte that isn't a printable ASCII character.
238 Fill up the buffer and check for UTF-8. */
240 is->state = (encoding_guess_tail_is_utf8 (is->head, is->length)
241 ? S_UTF8 : S_CONVERT);
242 if (size == original_size)
243 return u8_istream_read (is, buffer, size);
247 return original_size - size;
251 convert_iconv (iconv_t converter,
252 char **inbufp, size_t *inbytesleft,
253 char **outbufp, size_t *outbytesleft)
255 size_t n = iconv (converter, inbufp, inbytesleft, outbufp, outbytesleft);
256 return n == SIZE_MAX ? errno : 0;
260 convert_utf8 (iconv_t converter UNUSED,
261 char **inbufp, size_t *inbytesleft,
262 char **outbufp, size_t *outbytesleft)
264 const uint8_t *in = CHAR_CAST (const uint8_t *, *inbufp);
265 size_t n = MIN (*inbytesleft, *outbytesleft);
276 error = ofs < *inbytesleft ? E2BIG : 0;
280 mblen = u8_mbtouc (&uc, in + ofs, n - ofs);
283 int retval = u8_mbtoucr (&uc, in + ofs, *inbytesleft - ofs);
286 /* There's an actual U+FFFD in the input stream. Carry on. */
290 error = (retval == -1 ? EILSEQ
291 : retval == -2 ? EINVAL
302 memcpy (*outbufp, *inbufp, ofs);
306 *outbytesleft -= ofs;
313 read_convert (struct u8_istream *is,
314 int (*convert) (iconv_t converter,
315 char **inbufp, size_t *inbytesleft,
316 char **outbufp, size_t *outbytesleft),
317 char *buffer, size_t size)
319 size_t original_size = size;
327 size_t n = MIN (size, is->outlen);
329 memcpy (buffer, is->outbuf, n);
332 memmove (is->outbuf, is->outbuf + n, is->outlen);
343 int error = convert (is->converter,
344 &is->head, &is->length,
352 /* Converted all of the input into output, possibly with space
353 for output left over.
359 substitute_invalid_input_byte (is);
363 /* Incomplete byte sequence at end of input. Read more
368 /* A real error of some kind (ENOMEM?). */
372 /* Ran out of room for output.
373 Convert into outbuf and copy from there instead. */
375 char *outptr = is->outbuf;
376 size_t outleft = sizeof is->outbuf;
378 error = convert (is->converter,
379 &is->head, &is->length,
381 is->outlen = outptr - is->outbuf;
388 substitute_invalid_input_byte (is);
396 /* A real error of some kind (ENOMEM?). */
403 assert (is->length <= MB_LEN_MAX);
404 n_read = fill_buffer (is);
407 if (original_size != size)
409 /* We produced some output so don't report EOF or error yet. */
412 else if (n_read == 0 && is->length != 0)
414 /* Incomplete byte sequence at end of file. */
415 substitute_invalid_input_byte (is);
419 /* Propagate end-of-file or error to caller. */
425 return original_size - size;
428 /* Reads up to SIZE bytes of UTF-8 text from IS into BUFFER. Returns the
429 number of bytes read if successful, 0 at end of file, or -1 if an error
430 occurred before any data could be read. Upon error, sets errno to an
431 appropriate value. */
433 u8_istream_read (struct u8_istream *is, char *buffer, size_t size)
438 return read_convert (is, convert_iconv, buffer, size);
441 return read_auto (is, buffer, size);
444 return read_convert (is, convert_utf8, buffer, size);
450 /* Returns the file descriptor underlying IS. */
452 u8_istream_fileno (const struct u8_istream *is)
459 These functions are probably useful only for white-box testing. */
461 /* Returns true if the encoding of the file being read by IS is not yet
464 u8_istream_is_auto (const struct u8_istream *is)
466 return is->state == S_AUTO;
469 /* Returns true if the encoding of the file being read by IS has been
470 determined to be UTF-8. */
472 u8_istream_is_utf8 (const struct u8_istream *is)
474 return is->state == S_UTF8;