Add missign single include guards
[platform/upstream/glib.git] / gio / gutf8inputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2009 Paolo Borelli
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Paolo Borelli <pborelli@gnome.org>
21  */
22
23 #include "config.h"
24 #include "gutf8inputstream.h"
25 #include "ginputstream.h"
26 #include "gcancellable.h"
27 #include "gioerror.h"
28 #include "glibintl.h"
29
30 #include "gioalias.h"
31
32 /**
33  * SECTION:gutf8inputstream
34  * @short_description: Input Stream performing UTF8 validation
35  * @include: gio/gio.h
36  * @see_also: #GFilterInputStream, #GInputStream
37  *
38  * utf8 input stream implements #GFilterInputStream and provides
39  * UTF8 validation of the data read from a the stream.
40  * If the supplied buffer is long enough (see below), the returned
41  * data is guaranteed to end at utf8 character boundaries.
42  * <note>
43  *   <para>
44  *     Extra care must be taken when performing "small" reads:
45  *     unless you have control of the data being read, you need
46  *     to always supply a buffer long at least 6 bytes, otherwise
47  *     the returned content may be an incomplete utf8 byte sequence.
48  *   </para>
49  * </note>
50  *
51  * To create an utf8 input stream, use g_utf8_input_stream_new().
52  *
53  **/
54
55
56 #define MAX_UNICHAR_LEN 6
57
58 struct _GUtf8InputStreamPrivate {
59   /* buffer containing trailing partial character not yet returned */
60   char buffer[MAX_UNICHAR_LEN];
61   gsize len;
62
63   /* buffer containing partial character returned in a "small read"
64    * but not yet validated */
65   char small_read_buffer[MAX_UNICHAR_LEN];
66   gsize small_read_len;
67 };
68
69 static gssize g_utf8_input_stream_read        (GInputStream          *stream,
70                                                void                  *buffer,
71                                                gsize                  count,
72                                                GCancellable          *cancellable,
73                                                GError               **error);
74
75 G_DEFINE_TYPE (GUtf8InputStream,
76                g_utf8_input_stream,
77                G_TYPE_FILTER_INPUT_STREAM)
78
79
80 static void
81 g_utf8_input_stream_class_init (GUtf8InputStreamClass *klass)
82 {
83   GInputStreamClass *istream_class;
84
85   g_type_class_add_private (klass, sizeof (GUtf8InputStreamPrivate));
86
87   istream_class = G_INPUT_STREAM_CLASS (klass);
88   istream_class->read_fn = g_utf8_input_stream_read;
89 }
90
91 static void
92 g_utf8_input_stream_init (GUtf8InputStream *stream)
93 {
94   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
95                                               G_TYPE_UTF8_INPUT_STREAM,
96                                               GUtf8InputStreamPrivate);
97 }
98
99 /**
100  * g_utf8_input_stream_new:
101  * @base_stream: a #GInputStream.
102  *
103  * Creates a new #GUtf8InputStream from the given @base_stream.
104  *
105  * Returns: a #GInputStream for the given @base_stream.
106  *
107  * Since: 2.24
108  **/
109 GInputStream *
110 g_utf8_input_stream_new (GInputStream *base_stream)
111 {
112   GInputStream *stream;
113
114   g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
115
116   stream = g_object_new (G_TYPE_UTF8_INPUT_STREAM,
117                          "base-stream", base_stream,
118                          NULL);
119
120   return stream;
121 }
122
123 static void
124 store_remainder (GUtf8InputStream *stream,
125                  const char       *remainder,
126                  gsize             len)
127 {
128   GUtf8InputStreamPrivate *priv;
129   gsize i;
130
131   priv = stream->priv;
132
133   /* we store a remanainder only after having
134    * consumed the previous */
135   g_assert (priv->len == 0);
136
137   for (i = 0; i < len; ++i)
138     priv->buffer[i] = remainder[i];
139   priv->len = i;
140 }
141
142 static gssize
143 get_remainder (GUtf8InputStream *stream,
144                char             *buffer,
145                gsize             count)
146 {
147   GUtf8InputStreamPrivate *priv;
148   gsize i, len;
149   gssize res;
150
151   priv = stream->priv;
152
153   g_assert (priv->len < MAX_UNICHAR_LEN);
154
155   len = MIN (count, priv->len);
156   for (i = 0; i < len; ++i)
157     buffer[i] = priv->buffer[i];
158   res = i;
159
160   /* if there is more remainder, move it at the start */
161   for (i = 0; i < (priv->len - res); ++i)
162     priv->buffer[i] = priv->buffer[res + i];
163   priv->len = i;
164
165   return res;
166 }
167
168 static void
169 store_small_read (GUtf8InputStream *stream,
170                   const char       *buffer,
171                   gsize             len)
172 {
173   GUtf8InputStreamPrivate *priv;
174   gsize i;
175
176   priv = stream->priv;
177
178   /* if we reach MAX_UNICHAR_LEN it is either valid
179    * or invalid, so we should already have removed it
180    * from the buffer */
181   g_assert (priv->small_read_len + len < MAX_UNICHAR_LEN);
182
183   for (i = 0; i < len; ++i)
184     priv->small_read_buffer[priv->small_read_len + i] = buffer[i];
185   priv->small_read_len += i;
186 }
187
188 /* Combines the current "small read" buffer with the new
189  * bytes given, validates the buffer and if needed
190  * flushes it.
191  *
192  * returns:
193  * the number of bytes of buffer that are needed to
194  * make the current small read buffer valid.
195  *
196  * -1 if the small read buffer is invalid
197  *
198  * 0 if it is an incomplete character or if the
199  * small read buffer is empty.
200  */
201 static gssize
202 validate_small_read (GUtf8InputStream *stream,
203                      const char       *buffer,
204                      gsize             len)
205 {
206   GUtf8InputStreamPrivate *priv;
207   gsize i;
208   gunichar c;
209   char *p;
210   gssize res;
211
212   priv = stream->priv;
213
214   if (priv->small_read_len == 0)
215     return 0;
216
217   for (i = 0; i < MIN (len, MAX_UNICHAR_LEN - priv->small_read_len); ++i)
218     priv->small_read_buffer[priv->small_read_len + i] = buffer[i];
219
220   c = g_utf8_get_char_validated (priv->small_read_buffer, priv->small_read_len + i);
221   if (c == (gunichar)-1)
222     {
223       priv->small_read_len = 0;
224       return -1;
225     }
226   if (c == (gunichar)-2)
227     {
228       return 0;
229     }
230
231   p = g_utf8_next_char (priv->small_read_buffer);
232   res = p - (priv->small_read_buffer + priv->small_read_len);
233
234   g_assert (res > 0);
235
236   /* reset the buffer */
237   priv->small_read_len = 0;
238
239   return res;
240 }
241
242 static gssize
243 g_utf8_input_stream_read (GInputStream *stream,
244                           void         *buffer,
245                           gsize         count,
246                           GCancellable *cancellable,
247                           GError      **error)
248 {
249   GUtf8InputStream *ustream;
250   GUtf8InputStreamPrivate *priv;
251   GInputStream *base_stream;
252   gsize nvalid, remainder;
253   gssize oldread, nread, offset;
254   gboolean valid, eof;
255   const gchar *end;
256
257   ustream = G_UTF8_INPUT_STREAM (stream);
258   priv = ustream->priv;
259
260   /* if we had previous incomplete data put it at the start of the buffer */
261   oldread = get_remainder (ustream, buffer, count);
262
263   /* if we have already reached count, it is "small read":
264    * store it to validate later */
265   if (oldread == count)
266     {
267       store_small_read (ustream, buffer, oldread);
268       return oldread;
269     }
270
271   base_stream = g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (stream));
272
273   nread = g_input_stream_read (base_stream,
274                                (char *)buffer + oldread,
275                                count - oldread,
276                                cancellable,
277                                error);
278
279   if (nread < 0)
280     return -1;
281
282   /* take into account bytes we put in the buffer */
283   eof = (nread == 0);
284   nread += oldread;
285
286   /* validate previous small reads */
287   offset = validate_small_read (ustream, buffer, nread);
288   if (offset < 0)
289     goto error;
290
291   /* validate */
292   valid = g_utf8_validate ((char *)buffer + offset, nread - offset, &end);
293   nvalid = end - (char *)buffer;
294
295   if (valid)
296       return nread;
297
298   remainder = nread - nvalid;
299
300   /* if validation failed in the last bytes and the byte 
301    * sequence is an incomplete character and EOF is not reached,
302    * try to read further to see if we stopped in the middle
303    * of a character */
304   if ((remainder < MAX_UNICHAR_LEN) &&
305       (!eof) &&
306       (g_utf8_get_char_validated ((char *)buffer + nvalid, remainder) == (gunichar)-2))
307     {
308       if (nvalid == 0)
309         {
310           /* A "small" read: store it to validate later */
311           store_small_read (ustream, buffer, nread);
312           return nread;
313         }
314
315       store_remainder (ustream, (char *)buffer + nvalid, remainder);
316
317       return nvalid;
318     }
319
320 error:
321   g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
322                _("Invalid UTF-8 sequence in input"));
323   return -1;
324 }
325
326 #define __G_UTF8_INPUT_STREAM_C__
327 #include "gioaliasdef.c"