1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "gconverterinputstream.h"
28 #include "gpollableinputstream.h"
29 #include "gcancellable.h"
30 #include "gioenumtypes.h"
36 * GConverterInputStream:
38 * Converter input stream implements [class@Gio.InputStream] and allows
39 * conversion of data of various types during reading.
41 * As of GLib 2.34, `GConverterInputStream` implements
42 * [iface@Gio.PollableInputStream].
45 #define INITIAL_BUFFER_SIZE 4096
54 struct _GConverterInputStreamPrivate {
55 gboolean at_input_end;
58 GConverter *converter;
60 Buffer converted_buffer;
68 static void g_converter_input_stream_set_property (GObject *object,
72 static void g_converter_input_stream_get_property (GObject *object,
76 static void g_converter_input_stream_finalize (GObject *object);
77 static gssize g_converter_input_stream_read (GInputStream *stream,
80 GCancellable *cancellable,
83 static gboolean g_converter_input_stream_can_poll (GPollableInputStream *stream);
84 static gboolean g_converter_input_stream_is_readable (GPollableInputStream *stream);
85 static gssize g_converter_input_stream_read_nonblocking (GPollableInputStream *stream,
90 static GSource *g_converter_input_stream_create_source (GPollableInputStream *stream,
91 GCancellable *cancellable);
93 static void g_converter_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
95 G_DEFINE_TYPE_WITH_CODE (GConverterInputStream,
96 g_converter_input_stream,
97 G_TYPE_FILTER_INPUT_STREAM,
98 G_ADD_PRIVATE (GConverterInputStream)
99 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
100 g_converter_input_stream_pollable_iface_init))
103 g_converter_input_stream_class_init (GConverterInputStreamClass *klass)
105 GObjectClass *object_class;
106 GInputStreamClass *istream_class;
108 object_class = G_OBJECT_CLASS (klass);
109 object_class->get_property = g_converter_input_stream_get_property;
110 object_class->set_property = g_converter_input_stream_set_property;
111 object_class->finalize = g_converter_input_stream_finalize;
113 istream_class = G_INPUT_STREAM_CLASS (klass);
114 istream_class->read_fn = g_converter_input_stream_read;
117 * GConverterInputStream:converter:
119 * The converter object.
121 g_object_class_install_property (object_class,
123 g_param_spec_object ("converter", NULL, NULL,
126 G_PARAM_CONSTRUCT_ONLY|
127 G_PARAM_STATIC_STRINGS));
132 g_converter_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
134 iface->can_poll = g_converter_input_stream_can_poll;
135 iface->is_readable = g_converter_input_stream_is_readable;
136 iface->read_nonblocking = g_converter_input_stream_read_nonblocking;
137 iface->create_source = g_converter_input_stream_create_source;
141 g_converter_input_stream_finalize (GObject *object)
143 GConverterInputStreamPrivate *priv;
144 GConverterInputStream *stream;
146 stream = G_CONVERTER_INPUT_STREAM (object);
149 g_free (priv->input_buffer.data);
150 g_free (priv->converted_buffer.data);
152 g_object_unref (priv->converter);
154 G_OBJECT_CLASS (g_converter_input_stream_parent_class)->finalize (object);
158 g_converter_input_stream_set_property (GObject *object,
163 GConverterInputStream *cstream;
165 cstream = G_CONVERTER_INPUT_STREAM (object);
170 cstream->priv->converter = g_value_dup_object (value);
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 g_converter_input_stream_get_property (GObject *object,
186 GConverterInputStreamPrivate *priv;
187 GConverterInputStream *cstream;
189 cstream = G_CONVERTER_INPUT_STREAM (object);
190 priv = cstream->priv;
195 g_value_set_object (value, priv->converter);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 g_converter_input_stream_init (GConverterInputStream *stream)
207 stream->priv = g_converter_input_stream_get_instance_private (stream);
211 * g_converter_input_stream_new:
212 * @base_stream: a #GInputStream
213 * @converter: a #GConverter
215 * Creates a new converter input stream for the @base_stream.
217 * Returns: a new #GInputStream.
220 g_converter_input_stream_new (GInputStream *base_stream,
221 GConverter *converter)
223 GInputStream *stream;
225 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
227 stream = g_object_new (G_TYPE_CONVERTER_INPUT_STREAM,
228 "base-stream", base_stream,
229 "converter", converter,
236 buffer_data_size (Buffer *buffer)
238 return buffer->end - buffer->start;
242 buffer_tailspace (Buffer *buffer)
244 return buffer->size - buffer->end;
248 buffer_data (Buffer *buffer)
250 return buffer->data + buffer->start;
254 buffer_consumed (Buffer *buffer,
257 buffer->start += count;
258 if (buffer->start == buffer->end)
259 buffer->start = buffer->end = 0;
263 buffer_read (Buffer *buffer,
268 memcpy (dest, buffer->data + buffer->start, count);
270 buffer_consumed (buffer, count);
274 compact_buffer (Buffer *buffer)
278 in_buffer = buffer_data_size (buffer);
279 memmove (buffer->data,
280 buffer->data + buffer->start,
282 buffer->end -= buffer->start;
287 grow_buffer (Buffer *buffer)
290 gsize size, in_buffer;
292 if (buffer->size == 0)
293 size = INITIAL_BUFFER_SIZE;
295 size = buffer->size * 2;
297 data = g_malloc (size);
298 in_buffer = buffer_data_size (buffer);
302 buffer->data + buffer->start,
305 g_free (buffer->data);
307 buffer->end -= buffer->start;
312 /* Ensures that the buffer can fit at_least_size bytes,
313 * *including* the current in-buffer data */
315 buffer_ensure_space (Buffer *buffer,
318 gsize in_buffer, left_to_fill;
320 in_buffer = buffer_data_size (buffer);
322 if (in_buffer >= at_least_size)
325 left_to_fill = buffer_tailspace (buffer);
327 if (in_buffer + left_to_fill >= at_least_size)
329 /* We fit in remaining space at end */
330 /* If the copy is small, compact now anyway so we can fill more */
332 compact_buffer (buffer);
334 else if (buffer->size >= at_least_size)
336 /* We fit, but only if we compact */
337 compact_buffer (buffer);
341 /* Need to grow buffer */
342 while (buffer->size < at_least_size)
343 grow_buffer (buffer);
348 fill_input_buffer (GConverterInputStream *stream,
351 GCancellable *cancellable,
354 GConverterInputStreamPrivate *priv;
355 GInputStream *base_stream;
360 buffer_ensure_space (&priv->input_buffer, at_least_size);
362 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
363 nread = g_pollable_stream_read (base_stream,
364 priv->input_buffer.data + priv->input_buffer.end,
365 buffer_tailspace (&priv->input_buffer),
372 priv->input_buffer.end += nread;
373 priv->need_input = FALSE;
381 read_internal (GInputStream *stream,
385 GCancellable *cancellable,
388 GConverterInputStream *cstream;
389 GConverterInputStreamPrivate *priv;
390 gsize available, total_bytes_read;
392 GConverterResult res;
398 cstream = G_CONVERTER_INPUT_STREAM (stream);
399 priv = cstream->priv;
401 available = buffer_data_size (&priv->converted_buffer);
406 /* Converted data available, return that */
407 buffer_read (&priv->converted_buffer, buffer, count);
411 /* Full request not available, read all currently available and request
412 refill/conversion for more */
414 buffer_read (&priv->converted_buffer, buffer, available);
416 total_bytes_read = available;
417 buffer = (char *) buffer + available;
420 /* If there is no data to convert, and no pre-converted data,
421 do some i/o for more input */
422 if (buffer_data_size (&priv->input_buffer) == 0 &&
423 total_bytes_read == 0 &&
426 nread = fill_input_buffer (cstream, count, blocking, cancellable, error);
430 priv->at_input_end = TRUE;
433 /* First try to convert any available data (or state) directly to the user buffer: */
437 res = g_converter_convert (priv->converter,
438 buffer_data (&priv->input_buffer),
439 buffer_data_size (&priv->input_buffer),
441 priv->at_input_end ? G_CONVERTER_INPUT_AT_END : 0,
445 if (res != G_CONVERTER_ERROR)
447 total_bytes_read += bytes_written;
448 buffer_consumed (&priv->input_buffer, bytes_read);
449 if (res == G_CONVERTER_FINISHED)
450 priv->finished = TRUE; /* We're done converting */
452 else if (total_bytes_read == 0 &&
453 !g_error_matches (my_error,
455 G_IO_ERROR_PARTIAL_INPUT) &&
456 !g_error_matches (my_error,
458 G_IO_ERROR_NO_SPACE))
460 /* No previously read data and no "special" error, return error */
461 g_propagate_error (error, my_error);
465 g_error_free (my_error);
468 /* We had some pre-converted data and/or we converted directly to the
470 if (total_bytes_read > 0)
471 return total_bytes_read;
473 /* If there is no more to convert, return EOF */
476 g_assert (buffer_data_size (&priv->converted_buffer) == 0);
480 /* There was "complexity" in the straight-to-buffer conversion,
481 * convert to our own buffer and write from that.
482 * At this point we didn't produce any data into @buffer.
485 /* Ensure we have *some* initial target space */
486 buffer_ensure_space (&priv->converted_buffer, count);
490 g_assert (!priv->finished);
492 /* Try to convert to our buffer */
494 res = g_converter_convert (priv->converter,
495 buffer_data (&priv->input_buffer),
496 buffer_data_size (&priv->input_buffer),
497 buffer_data (&priv->converted_buffer),
498 buffer_tailspace (&priv->converted_buffer),
499 priv->at_input_end ? G_CONVERTER_INPUT_AT_END : 0,
503 if (res != G_CONVERTER_ERROR)
505 priv->converted_buffer.end += bytes_written;
506 buffer_consumed (&priv->input_buffer, bytes_read);
508 /* Maybe we consumed without producing any output */
509 if (buffer_data_size (&priv->converted_buffer) == 0 && res != G_CONVERTER_FINISHED)
510 continue; /* Convert more */
512 if (res == G_CONVERTER_FINISHED)
513 priv->finished = TRUE;
515 total_bytes_read = MIN (count, buffer_data_size (&priv->converted_buffer));
516 buffer_read (&priv->converted_buffer, buffer, total_bytes_read);
518 g_assert (priv->finished || total_bytes_read > 0);
520 return total_bytes_read;
523 /* There was some kind of error filling our buffer */
525 if (g_error_matches (my_error,
527 G_IO_ERROR_PARTIAL_INPUT) &&
532 nread = fill_input_buffer (cstream,
533 buffer_data_size (&priv->input_buffer) + 4096,
539 /* Can't read any more data, return that error */
540 g_error_free (my_error);
541 g_propagate_error (error, my_error2);
542 priv->need_input = TRUE;
547 /* End of file, try INPUT_AT_END */
548 priv->at_input_end = TRUE;
550 g_error_free (my_error);
554 if (g_error_matches (my_error,
556 G_IO_ERROR_NO_SPACE))
558 /* Need more destination space, grow it
559 * Note: if we actually grow the buffer (as opposed to compacting it),
560 * this will double the size, not just add one byte. */
561 buffer_ensure_space (&priv->converted_buffer,
562 priv->converted_buffer.size + 1);
563 g_error_free (my_error);
567 /* Any other random error, return it */
568 g_propagate_error (error, my_error);
572 g_assert_not_reached ();
576 g_converter_input_stream_read (GInputStream *stream,
579 GCancellable *cancellable,
582 return read_internal (stream, buffer, count, TRUE, cancellable, error);
586 g_converter_input_stream_can_poll (GPollableInputStream *stream)
588 GInputStream *base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
590 return (G_IS_POLLABLE_INPUT_STREAM (base_stream) &&
591 g_pollable_input_stream_can_poll (G_POLLABLE_INPUT_STREAM (base_stream)));
595 g_converter_input_stream_is_readable (GPollableInputStream *stream)
597 GInputStream *base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
598 GConverterInputStream *cstream = G_CONVERTER_INPUT_STREAM (stream);
600 if (buffer_data_size (&cstream->priv->converted_buffer))
602 else if (buffer_data_size (&cstream->priv->input_buffer) &&
603 !cstream->priv->need_input)
606 return g_pollable_input_stream_is_readable (G_POLLABLE_INPUT_STREAM (base_stream));
610 g_converter_input_stream_read_nonblocking (GPollableInputStream *stream,
615 return read_internal (G_INPUT_STREAM (stream), buffer, count,
620 g_converter_input_stream_create_source (GPollableInputStream *stream,
621 GCancellable *cancellable)
623 GInputStream *base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
624 GSource *base_source, *pollable_source;
626 if (g_pollable_input_stream_is_readable (stream))
627 base_source = g_timeout_source_new (0);
629 base_source = g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM (base_stream), NULL);
631 pollable_source = g_pollable_source_new_full (stream, base_source,
633 g_source_unref (base_source);
635 return pollable_source;
640 * g_converter_input_stream_get_converter:
641 * @converter_stream: a #GConverterInputStream
643 * Gets the #GConverter that is used by @converter_stream.
645 * Returns: (transfer none): the converter of the converter input stream
650 g_converter_input_stream_get_converter (GConverterInputStream *converter_stream)
652 return converter_stream->priv->converter;