gresource: Fix potential array overflow if using empty paths
[glib.git] / gio / gdatainputstream.c
blob9f207b158b6ebff1f1bf4a02e86a12eeba7ede57
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2007 Jürg Billeter
5 * Copyright © 2009 Codethink Limited
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>
23 #include "config.h"
24 #include "gdatainputstream.h"
25 #include "gtask.h"
26 #include "gcancellable.h"
27 #include "gioenumtypes.h"
28 #include "gioerror.h"
29 #include "glibintl.h"
31 #include <string.h>
33 /**
34 * SECTION:gdatainputstream
35 * @short_description: Data Input Stream
36 * @include: gio/gio.h
37 * @see_also: #GInputStream
39 * Data input stream implements #GInputStream and includes functions for
40 * reading structured data directly from a binary input stream.
42 **/
44 struct _GDataInputStreamPrivate {
45 GDataStreamByteOrder byte_order;
46 GDataStreamNewlineType newline_type;
49 enum {
50 PROP_0,
51 PROP_BYTE_ORDER,
52 PROP_NEWLINE_TYPE
55 static void g_data_input_stream_set_property (GObject *object,
56 guint prop_id,
57 const GValue *value,
58 GParamSpec *pspec);
59 static void g_data_input_stream_get_property (GObject *object,
60 guint prop_id,
61 GValue *value,
62 GParamSpec *pspec);
64 G_DEFINE_TYPE_WITH_PRIVATE (GDataInputStream,
65 g_data_input_stream,
66 G_TYPE_BUFFERED_INPUT_STREAM)
69 static void
70 g_data_input_stream_class_init (GDataInputStreamClass *klass)
72 GObjectClass *object_class;
74 object_class = G_OBJECT_CLASS (klass);
75 object_class->get_property = g_data_input_stream_get_property;
76 object_class->set_property = g_data_input_stream_set_property;
78 /**
79 * GDataStream:byte-order:
81 * The ::byte-order property determines the byte ordering that
82 * is used when reading multi-byte entities (such as integers)
83 * from the stream.
84 */
85 g_object_class_install_property (object_class,
86 PROP_BYTE_ORDER,
87 g_param_spec_enum ("byte-order",
88 P_("Byte order"),
89 P_("The byte order"),
90 G_TYPE_DATA_STREAM_BYTE_ORDER,
91 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
92 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
94 /**
95 * GDataStream:newline-type:
97 * The :newline-type property determines what is considered
98 * as a line ending when reading complete lines from the stream.
99 */
100 g_object_class_install_property (object_class,
101 PROP_NEWLINE_TYPE,
102 g_param_spec_enum ("newline-type",
103 P_("Newline type"),
104 P_("The accepted types of line ending"),
105 G_TYPE_DATA_STREAM_NEWLINE_TYPE,
106 G_DATA_STREAM_NEWLINE_TYPE_LF,
107 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
110 static void
111 g_data_input_stream_set_property (GObject *object,
112 guint prop_id,
113 const GValue *value,
114 GParamSpec *pspec)
116 GDataInputStream *dstream;
118 dstream = G_DATA_INPUT_STREAM (object);
120 switch (prop_id)
122 case PROP_BYTE_ORDER:
123 g_data_input_stream_set_byte_order (dstream, g_value_get_enum (value));
124 break;
126 case PROP_NEWLINE_TYPE:
127 g_data_input_stream_set_newline_type (dstream, g_value_get_enum (value));
128 break;
130 default:
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132 break;
137 static void
138 g_data_input_stream_get_property (GObject *object,
139 guint prop_id,
140 GValue *value,
141 GParamSpec *pspec)
143 GDataInputStreamPrivate *priv;
144 GDataInputStream *dstream;
146 dstream = G_DATA_INPUT_STREAM (object);
147 priv = dstream->priv;
149 switch (prop_id)
151 case PROP_BYTE_ORDER:
152 g_value_set_enum (value, priv->byte_order);
153 break;
155 case PROP_NEWLINE_TYPE:
156 g_value_set_enum (value, priv->newline_type);
157 break;
159 default:
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161 break;
165 static void
166 g_data_input_stream_init (GDataInputStream *stream)
168 stream->priv = g_data_input_stream_get_instance_private (stream);
169 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
170 stream->priv->newline_type = G_DATA_STREAM_NEWLINE_TYPE_LF;
174 * g_data_input_stream_new:
175 * @base_stream: a #GInputStream.
177 * Creates a new data input stream for the @base_stream.
179 * Returns: a new #GDataInputStream.
181 GDataInputStream *
182 g_data_input_stream_new (GInputStream *base_stream)
184 GDataInputStream *stream;
186 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
188 stream = g_object_new (G_TYPE_DATA_INPUT_STREAM,
189 "base-stream", base_stream,
190 NULL);
192 return stream;
196 * g_data_input_stream_set_byte_order:
197 * @stream: a given #GDataInputStream.
198 * @order: a #GDataStreamByteOrder to set.
200 * This function sets the byte order for the given @stream. All subsequent
201 * reads from the @stream will be read in the given @order.
204 void
205 g_data_input_stream_set_byte_order (GDataInputStream *stream,
206 GDataStreamByteOrder order)
208 GDataInputStreamPrivate *priv;
210 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
212 priv = stream->priv;
214 if (priv->byte_order != order)
216 priv->byte_order = order;
218 g_object_notify (G_OBJECT (stream), "byte-order");
223 * g_data_input_stream_get_byte_order:
224 * @stream: a given #GDataInputStream.
226 * Gets the byte order for the data input stream.
228 * Returns: the @stream's current #GDataStreamByteOrder.
230 GDataStreamByteOrder
231 g_data_input_stream_get_byte_order (GDataInputStream *stream)
233 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
235 return stream->priv->byte_order;
239 * g_data_input_stream_set_newline_type:
240 * @stream: a #GDataInputStream.
241 * @type: the type of new line return as #GDataStreamNewlineType.
243 * Sets the newline type for the @stream.
245 * Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read
246 * chunk ends in "CR" we must read an additional byte to know if this is "CR" or
247 * "CR LF", and this might block if there is no more data available.
250 void
251 g_data_input_stream_set_newline_type (GDataInputStream *stream,
252 GDataStreamNewlineType type)
254 GDataInputStreamPrivate *priv;
256 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
258 priv = stream->priv;
260 if (priv->newline_type != type)
262 priv->newline_type = type;
264 g_object_notify (G_OBJECT (stream), "newline-type");
269 * g_data_input_stream_get_newline_type:
270 * @stream: a given #GDataInputStream.
272 * Gets the current newline type for the @stream.
274 * Returns: #GDataStreamNewlineType for the given @stream.
276 GDataStreamNewlineType
277 g_data_input_stream_get_newline_type (GDataInputStream *stream)
279 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), G_DATA_STREAM_NEWLINE_TYPE_ANY);
281 return stream->priv->newline_type;
284 static gboolean
285 read_data (GDataInputStream *stream,
286 void *buffer,
287 gsize size,
288 GCancellable *cancellable,
289 GError **error)
291 gsize available;
292 gssize res;
294 while ((available = g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (stream))) < size)
296 res = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream),
297 size - available,
298 cancellable, error);
299 if (res < 0)
300 return FALSE;
301 if (res == 0)
303 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
304 _("Unexpected early end-of-stream"));
305 return FALSE;
309 /* This should always succeed, since it's in the buffer */
310 res = g_input_stream_read (G_INPUT_STREAM (stream),
311 buffer, size,
312 NULL, NULL);
313 g_warn_if_fail (res == size);
314 return TRUE;
319 * g_data_input_stream_read_byte:
320 * @stream: a given #GDataInputStream.
321 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
322 * @error: #GError for error reporting.
324 * Reads an unsigned 8-bit/1-byte value from @stream.
326 * Returns: an unsigned 8-bit/1-byte value read from the @stream or %0
327 * if an error occurred.
329 guchar
330 g_data_input_stream_read_byte (GDataInputStream *stream,
331 GCancellable *cancellable,
332 GError **error)
334 guchar c;
336 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), '\0');
338 if (read_data (stream, &c, 1, cancellable, error))
339 return c;
341 return 0;
346 * g_data_input_stream_read_int16:
347 * @stream: a given #GDataInputStream.
348 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
349 * @error: #GError for error reporting.
351 * Reads a 16-bit/2-byte value from @stream.
353 * In order to get the correct byte order for this read operation,
354 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
356 * Returns: a signed 16-bit/2-byte value read from @stream or %0 if
357 * an error occurred.
359 gint16
360 g_data_input_stream_read_int16 (GDataInputStream *stream,
361 GCancellable *cancellable,
362 GError **error)
364 gint16 v;
366 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
368 if (read_data (stream, &v, 2, cancellable, error))
370 switch (stream->priv->byte_order)
372 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
373 v = GINT16_FROM_BE (v);
374 break;
375 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
376 v = GINT16_FROM_LE (v);
377 break;
378 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
379 default:
380 break;
382 return v;
385 return 0;
390 * g_data_input_stream_read_uint16:
391 * @stream: a given #GDataInputStream.
392 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
393 * @error: #GError for error reporting.
395 * Reads an unsigned 16-bit/2-byte value from @stream.
397 * In order to get the correct byte order for this read operation,
398 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
400 * Returns: an unsigned 16-bit/2-byte value read from the @stream or %0 if
401 * an error occurred.
403 guint16
404 g_data_input_stream_read_uint16 (GDataInputStream *stream,
405 GCancellable *cancellable,
406 GError **error)
408 guint16 v;
410 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
412 if (read_data (stream, &v, 2, cancellable, error))
414 switch (stream->priv->byte_order)
416 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
417 v = GUINT16_FROM_BE (v);
418 break;
419 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
420 v = GUINT16_FROM_LE (v);
421 break;
422 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
423 default:
424 break;
426 return v;
429 return 0;
434 * g_data_input_stream_read_int32:
435 * @stream: a given #GDataInputStream.
436 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
437 * @error: #GError for error reporting.
439 * Reads a signed 32-bit/4-byte value from @stream.
441 * In order to get the correct byte order for this read operation,
442 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
444 * If @cancellable is not %NULL, then the operation can be cancelled by
445 * triggering the cancellable object from another thread. If the operation
446 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
448 * Returns: a signed 32-bit/4-byte value read from the @stream or %0 if
449 * an error occurred.
451 gint32
452 g_data_input_stream_read_int32 (GDataInputStream *stream,
453 GCancellable *cancellable,
454 GError **error)
456 gint32 v;
458 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
460 if (read_data (stream, &v, 4, cancellable, error))
462 switch (stream->priv->byte_order)
464 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
465 v = GINT32_FROM_BE (v);
466 break;
467 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
468 v = GINT32_FROM_LE (v);
469 break;
470 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
471 default:
472 break;
474 return v;
477 return 0;
482 * g_data_input_stream_read_uint32:
483 * @stream: a given #GDataInputStream.
484 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
485 * @error: #GError for error reporting.
487 * Reads an unsigned 32-bit/4-byte value from @stream.
489 * In order to get the correct byte order for this read operation,
490 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
492 * If @cancellable is not %NULL, then the operation can be cancelled by
493 * triggering the cancellable object from another thread. If the operation
494 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
496 * Returns: an unsigned 32-bit/4-byte value read from the @stream or %0 if
497 * an error occurred.
499 guint32
500 g_data_input_stream_read_uint32 (GDataInputStream *stream,
501 GCancellable *cancellable,
502 GError **error)
504 guint32 v;
506 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
508 if (read_data (stream, &v, 4, cancellable, error))
510 switch (stream->priv->byte_order)
512 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
513 v = GUINT32_FROM_BE (v);
514 break;
515 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
516 v = GUINT32_FROM_LE (v);
517 break;
518 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
519 default:
520 break;
522 return v;
525 return 0;
530 * g_data_input_stream_read_int64:
531 * @stream: a given #GDataInputStream.
532 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
533 * @error: #GError for error reporting.
535 * Reads a 64-bit/8-byte value from @stream.
537 * In order to get the correct byte order for this read operation,
538 * see g_data_input_stream_get_byte_order() and g_data_input_stream_set_byte_order().
540 * If @cancellable is not %NULL, then the operation can be cancelled by
541 * triggering the cancellable object from another thread. If the operation
542 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
544 * Returns: a signed 64-bit/8-byte value read from @stream or %0 if
545 * an error occurred.
547 gint64
548 g_data_input_stream_read_int64 (GDataInputStream *stream,
549 GCancellable *cancellable,
550 GError **error)
552 gint64 v;
554 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
556 if (read_data (stream, &v, 8, cancellable, error))
558 switch (stream->priv->byte_order)
560 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
561 v = GINT64_FROM_BE (v);
562 break;
563 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
564 v = GINT64_FROM_LE (v);
565 break;
566 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
567 default:
568 break;
570 return v;
573 return 0;
578 * g_data_input_stream_read_uint64:
579 * @stream: a given #GDataInputStream.
580 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
581 * @error: #GError for error reporting.
583 * Reads an unsigned 64-bit/8-byte value from @stream.
585 * In order to get the correct byte order for this read operation,
586 * see g_data_input_stream_get_byte_order().
588 * If @cancellable is not %NULL, then the operation can be cancelled by
589 * triggering the cancellable object from another thread. If the operation
590 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
592 * Returns: an unsigned 64-bit/8-byte read from @stream or %0 if
593 * an error occurred.
595 guint64
596 g_data_input_stream_read_uint64 (GDataInputStream *stream,
597 GCancellable *cancellable,
598 GError **error)
600 guint64 v;
602 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), 0);
604 if (read_data (stream, &v, 8, cancellable, error))
606 switch (stream->priv->byte_order)
608 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
609 v = GUINT64_FROM_BE (v);
610 break;
611 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
612 v = GUINT64_FROM_LE (v);
613 break;
614 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
615 default:
616 break;
618 return v;
621 return 0;
624 static gssize
625 scan_for_newline (GDataInputStream *stream,
626 gsize *checked_out,
627 gboolean *last_saw_cr_out,
628 int *newline_len_out)
630 GBufferedInputStream *bstream;
631 GDataInputStreamPrivate *priv;
632 const char *buffer;
633 gsize start, end, peeked;
634 int i;
635 gssize found_pos;
636 int newline_len;
637 gsize available, checked;
638 gboolean last_saw_cr;
640 priv = stream->priv;
642 bstream = G_BUFFERED_INPUT_STREAM (stream);
644 checked = *checked_out;
645 last_saw_cr = *last_saw_cr_out;
646 found_pos = -1;
647 newline_len = 0;
649 start = checked;
650 buffer = (const char*)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
651 end = available;
652 peeked = end - start;
654 for (i = 0; checked < available && i < peeked; i++)
656 switch (priv->newline_type)
658 case G_DATA_STREAM_NEWLINE_TYPE_LF:
659 if (buffer[i] == 10)
661 found_pos = start + i;
662 newline_len = 1;
664 break;
665 case G_DATA_STREAM_NEWLINE_TYPE_CR:
666 if (buffer[i] == 13)
668 found_pos = start + i;
669 newline_len = 1;
671 break;
672 case G_DATA_STREAM_NEWLINE_TYPE_CR_LF:
673 if (last_saw_cr && buffer[i] == 10)
675 found_pos = start + i - 1;
676 newline_len = 2;
678 break;
679 default:
680 case G_DATA_STREAM_NEWLINE_TYPE_ANY:
681 if (buffer[i] == 10) /* LF */
683 if (last_saw_cr)
685 /* CR LF */
686 found_pos = start + i - 1;
687 newline_len = 2;
689 else
691 /* LF */
692 found_pos = start + i;
693 newline_len = 1;
696 else if (last_saw_cr)
698 /* Last was cr, this is not LF, end is CR */
699 found_pos = start + i - 1;
700 newline_len = 1;
702 /* Don't check for CR here, instead look at last_saw_cr on next byte */
703 break;
706 last_saw_cr = (buffer[i] == 13);
708 if (found_pos != -1)
710 *newline_len_out = newline_len;
711 return found_pos;
715 checked = end;
717 *checked_out = checked;
718 *last_saw_cr_out = last_saw_cr;
719 return -1;
724 * g_data_input_stream_read_line:
725 * @stream: a given #GDataInputStream.
726 * @length: (out) (optional): a #gsize to get the length of the data read in.
727 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
728 * @error: #GError for error reporting.
730 * Reads a line from the data input stream. Note that no encoding
731 * checks or conversion is performed; the input is not guaranteed to
732 * be UTF-8, and may in fact have embedded NUL characters.
734 * If @cancellable is not %NULL, then the operation can be cancelled by
735 * triggering the cancellable object from another thread. If the operation
736 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
738 * Returns: (nullable) (transfer full) (array zero-terminated=1) (element-type guint8):
739 * a NUL terminated byte array with the line that was read in
740 * (without the newlines). Set @length to a #gsize to get the length
741 * of the read line. On an error, it will return %NULL and @error
742 * will be set. If there's no content to read, it will still return
743 * %NULL, but @error won't be set.
745 char *
746 g_data_input_stream_read_line (GDataInputStream *stream,
747 gsize *length,
748 GCancellable *cancellable,
749 GError **error)
751 GBufferedInputStream *bstream;
752 gsize checked;
753 gboolean last_saw_cr;
754 gssize found_pos;
755 gssize res;
756 int newline_len;
757 char *line;
759 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
761 bstream = G_BUFFERED_INPUT_STREAM (stream);
763 newline_len = 0;
764 checked = 0;
765 last_saw_cr = FALSE;
767 while ((found_pos = scan_for_newline (stream, &checked, &last_saw_cr, &newline_len)) == -1)
769 if (g_buffered_input_stream_get_available (bstream) ==
770 g_buffered_input_stream_get_buffer_size (bstream))
771 g_buffered_input_stream_set_buffer_size (bstream,
772 2 * g_buffered_input_stream_get_buffer_size (bstream));
774 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
775 if (res < 0)
776 return NULL;
777 if (res == 0)
779 /* End of stream */
780 if (g_buffered_input_stream_get_available (bstream) == 0)
782 if (length)
783 *length = 0;
784 return NULL;
786 else
788 found_pos = checked;
789 newline_len = 0;
790 break;
795 line = g_malloc (found_pos + newline_len + 1);
797 res = g_input_stream_read (G_INPUT_STREAM (stream),
798 line,
799 found_pos + newline_len,
800 NULL, NULL);
801 if (length)
802 *length = (gsize)found_pos;
803 g_warn_if_fail (res == found_pos + newline_len);
804 line[found_pos] = 0;
806 return line;
810 * g_data_input_stream_read_line_utf8:
811 * @stream: a given #GDataInputStream.
812 * @length: (out) (optional): a #gsize to get the length of the data read in.
813 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
814 * @error: #GError for error reporting.
816 * Reads a UTF-8 encoded line from the data input stream.
818 * If @cancellable is not %NULL, then the operation can be cancelled by
819 * triggering the cancellable object from another thread. If the operation
820 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
822 * Returns: (nullable) (transfer full): a NUL terminated UTF-8 string
823 * with the line that was read in (without the newlines). Set
824 * @length to a #gsize to get the length of the read line. On an
825 * error, it will return %NULL and @error will be set. For UTF-8
826 * conversion errors, the set error domain is %G_CONVERT_ERROR. If
827 * there's no content to read, it will still return %NULL, but @error
828 * won't be set.
830 * Since: 2.30
832 char *
833 g_data_input_stream_read_line_utf8 (GDataInputStream *stream,
834 gsize *length,
835 GCancellable *cancellable,
836 GError **error)
838 char *res;
840 res = g_data_input_stream_read_line (stream, length, cancellable, error);
841 if (!res)
842 return NULL;
844 if (!g_utf8_validate (res, -1, NULL))
846 g_set_error_literal (error, G_CONVERT_ERROR,
847 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
848 _("Invalid byte sequence in conversion input"));
849 g_free (res);
850 return NULL;
852 return res;
855 static gssize
856 scan_for_chars (GDataInputStream *stream,
857 gsize *checked_out,
858 const char *stop_chars,
859 gssize stop_chars_len)
861 GBufferedInputStream *bstream;
862 const char *buffer;
863 gsize start, end, peeked;
864 int i;
865 gsize available, checked;
866 const char *stop_char;
867 const char *stop_end;
869 bstream = G_BUFFERED_INPUT_STREAM (stream);
870 stop_end = stop_chars + stop_chars_len;
872 checked = *checked_out;
874 start = checked;
875 buffer = (const char *)g_buffered_input_stream_peek_buffer (bstream, &available) + start;
876 end = available;
877 peeked = end - start;
879 for (i = 0; checked < available && i < peeked; i++)
881 for (stop_char = stop_chars; stop_char != stop_end; stop_char++)
883 if (buffer[i] == *stop_char)
884 return (start + i);
888 checked = end;
890 *checked_out = checked;
891 return -1;
895 * g_data_input_stream_read_until:
896 * @stream: a given #GDataInputStream.
897 * @stop_chars: characters to terminate the read.
898 * @length: (out) (optional): a #gsize to get the length of the data read in.
899 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
900 * @error: #GError for error reporting.
902 * Reads a string from the data input stream, up to the first
903 * occurrence of any of the stop characters.
905 * Note that, in contrast to g_data_input_stream_read_until_async(),
906 * this function consumes the stop character that it finds.
908 * Don't use this function in new code. Its functionality is
909 * inconsistent with g_data_input_stream_read_until_async(). Both
910 * functions will be marked as deprecated in a future release. Use
911 * g_data_input_stream_read_upto() instead, but note that that function
912 * does not consume the stop character.
914 * Returns: (transfer full): a string with the data that was read
915 * before encountering any of the stop characters. Set @length to
916 * a #gsize to get the length of the string. This function will
917 * return %NULL on an error.
918 * Deprecated: 2.56: Use g_data_input_stream_read_upto() instead, which has more
919 * consistent behaviour regarding the stop character.
921 char *
922 g_data_input_stream_read_until (GDataInputStream *stream,
923 const gchar *stop_chars,
924 gsize *length,
925 GCancellable *cancellable,
926 GError **error)
928 GBufferedInputStream *bstream;
929 gchar *result;
931 bstream = G_BUFFERED_INPUT_STREAM (stream);
933 result = g_data_input_stream_read_upto (stream, stop_chars, -1,
934 length, cancellable, error);
936 /* If we're not at end of stream then we have a stop_char to consume. */
937 if (result != NULL && g_buffered_input_stream_get_available (bstream) > 0)
939 gsize res;
940 gchar b;
942 res = g_input_stream_read (G_INPUT_STREAM (stream), &b, 1, NULL, NULL);
943 g_assert (res == 1);
946 return result;
949 typedef struct
951 gboolean last_saw_cr;
952 gsize checked;
954 gchar *stop_chars;
955 gssize stop_chars_len;
956 gsize length;
957 } GDataInputStreamReadData;
959 static void
960 g_data_input_stream_read_complete (GTask *task,
961 gsize read_length,
962 gsize skip_length)
964 GDataInputStreamReadData *data = g_task_get_task_data (task);
965 GInputStream *stream = g_task_get_source_object (task);
966 char *line = NULL;
968 if (read_length || skip_length)
970 gssize bytes;
972 data->length = read_length;
973 line = g_malloc (read_length + 1);
974 line[read_length] = '\0';
976 /* we already checked the buffer. this shouldn't fail. */
977 bytes = g_input_stream_read (stream, line, read_length, NULL, NULL);
978 g_assert_cmpint (bytes, ==, read_length);
980 bytes = g_input_stream_skip (stream, skip_length, NULL, NULL);
981 g_assert_cmpint (bytes, ==, skip_length);
984 g_task_return_pointer (task, line, g_free);
985 g_object_unref (task);
988 static void
989 g_data_input_stream_read_line_ready (GObject *object,
990 GAsyncResult *result,
991 gpointer user_data)
993 GTask *task = user_data;
994 GDataInputStreamReadData *data = g_task_get_task_data (task);
995 GBufferedInputStream *buffer = g_task_get_source_object (task);
996 gssize found_pos;
997 gint newline_len;
999 if (result)
1000 /* this is a callback. finish the async call. */
1002 GError *error = NULL;
1003 gssize bytes;
1005 bytes = g_buffered_input_stream_fill_finish (buffer, result, &error);
1007 if (bytes <= 0)
1009 if (bytes < 0)
1010 /* stream error. */
1012 g_task_return_error (task, error);
1013 g_object_unref (task);
1014 return;
1017 g_data_input_stream_read_complete (task, data->checked, 0);
1018 return;
1021 /* only proceed if we got more bytes... */
1024 if (data->stop_chars)
1026 found_pos = scan_for_chars (G_DATA_INPUT_STREAM (buffer),
1027 &data->checked,
1028 data->stop_chars,
1029 data->stop_chars_len);
1030 newline_len = 0;
1032 else
1033 found_pos = scan_for_newline (G_DATA_INPUT_STREAM (buffer), &data->checked,
1034 &data->last_saw_cr, &newline_len);
1036 if (found_pos == -1)
1037 /* didn't find a full line; need to buffer some more bytes */
1039 gsize size;
1041 size = g_buffered_input_stream_get_buffer_size (buffer);
1043 if (g_buffered_input_stream_get_available (buffer) == size)
1044 /* need to grow the buffer */
1045 g_buffered_input_stream_set_buffer_size (buffer, size * 2);
1047 /* try again */
1048 g_buffered_input_stream_fill_async (buffer, -1,
1049 g_task_get_priority (task),
1050 g_task_get_cancellable (task),
1051 g_data_input_stream_read_line_ready,
1052 user_data);
1054 else
1056 /* read the line and the EOL. no error is possible. */
1057 g_data_input_stream_read_complete (task, found_pos, newline_len);
1061 static void
1062 g_data_input_stream_read_data_free (gpointer user_data)
1064 GDataInputStreamReadData *data = user_data;
1066 g_free (data->stop_chars);
1067 g_slice_free (GDataInputStreamReadData, data);
1070 static void
1071 g_data_input_stream_read_async (GDataInputStream *stream,
1072 const gchar *stop_chars,
1073 gssize stop_chars_len,
1074 gint io_priority,
1075 GCancellable *cancellable,
1076 GAsyncReadyCallback callback,
1077 gpointer user_data)
1079 GDataInputStreamReadData *data;
1080 GTask *task;
1082 data = g_slice_new0 (GDataInputStreamReadData);
1083 if (stop_chars_len == -1)
1084 stop_chars_len = strlen (stop_chars);
1085 data->stop_chars = g_memdup (stop_chars, stop_chars_len);
1086 data->stop_chars_len = stop_chars_len;
1087 data->last_saw_cr = FALSE;
1089 task = g_task_new (stream, cancellable, callback, user_data);
1090 g_task_set_source_tag (task, g_data_input_stream_read_async);
1091 g_task_set_task_data (task, data, g_data_input_stream_read_data_free);
1092 g_task_set_priority (task, io_priority);
1094 g_data_input_stream_read_line_ready (NULL, NULL, task);
1097 static gchar *
1098 g_data_input_stream_read_finish (GDataInputStream *stream,
1099 GAsyncResult *result,
1100 gsize *length,
1101 GError **error)
1103 GTask *task = G_TASK (result);
1104 gchar *line;
1106 line = g_task_propagate_pointer (task, error);
1108 if (length && line)
1110 GDataInputStreamReadData *data = g_task_get_task_data (task);
1112 *length = data->length;
1115 return line;
1119 * g_data_input_stream_read_line_async:
1120 * @stream: a given #GDataInputStream.
1121 * @io_priority: the [I/O priority][io-priority] of the request
1122 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
1123 * @callback: (scope async): callback to call when the request is satisfied.
1124 * @user_data: (closure): the data to pass to callback function.
1126 * The asynchronous version of g_data_input_stream_read_line(). It is
1127 * an error to have two outstanding calls to this function.
1129 * When the operation is finished, @callback will be called. You
1130 * can then call g_data_input_stream_read_line_finish() to get
1131 * the result of the operation.
1133 * Since: 2.20
1135 void
1136 g_data_input_stream_read_line_async (GDataInputStream *stream,
1137 gint io_priority,
1138 GCancellable *cancellable,
1139 GAsyncReadyCallback callback,
1140 gpointer user_data)
1142 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1143 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1145 g_data_input_stream_read_async (stream, NULL, 0, io_priority,
1146 cancellable, callback, user_data);
1150 * g_data_input_stream_read_until_async:
1151 * @stream: a given #GDataInputStream.
1152 * @stop_chars: characters to terminate the read.
1153 * @io_priority: the [I/O priority][io-priority] of the request
1154 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
1155 * @callback: (scope async): callback to call when the request is satisfied.
1156 * @user_data: (closure): the data to pass to callback function.
1158 * The asynchronous version of g_data_input_stream_read_until().
1159 * It is an error to have two outstanding calls to this function.
1161 * Note that, in contrast to g_data_input_stream_read_until(),
1162 * this function does not consume the stop character that it finds. You
1163 * must read it for yourself.
1165 * When the operation is finished, @callback will be called. You
1166 * can then call g_data_input_stream_read_until_finish() to get
1167 * the result of the operation.
1169 * Don't use this function in new code. Its functionality is
1170 * inconsistent with g_data_input_stream_read_until(). Both functions
1171 * will be marked as deprecated in a future release. Use
1172 * g_data_input_stream_read_upto_async() instead.
1174 * Since: 2.20
1175 * Deprecated: 2.56: Use g_data_input_stream_read_upto_async() instead, which
1176 * has more consistent behaviour regarding the stop character.
1178 void
1179 g_data_input_stream_read_until_async (GDataInputStream *stream,
1180 const gchar *stop_chars,
1181 gint io_priority,
1182 GCancellable *cancellable,
1183 GAsyncReadyCallback callback,
1184 gpointer user_data)
1186 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1187 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1188 g_return_if_fail (stop_chars != NULL);
1190 g_data_input_stream_read_async (stream, stop_chars, -1, io_priority,
1191 cancellable, callback, user_data);
1195 * g_data_input_stream_read_line_finish:
1196 * @stream: a given #GDataInputStream.
1197 * @result: the #GAsyncResult that was provided to the callback.
1198 * @length: (out) (optional): a #gsize to get the length of the data read in.
1199 * @error: #GError for error reporting.
1201 * Finish an asynchronous call started by
1202 * g_data_input_stream_read_line_async(). Note the warning about
1203 * string encoding in g_data_input_stream_read_line() applies here as
1204 * well.
1206 * Returns: (nullable) (transfer full) (array zero-terminated=1) (element-type guint8):
1207 * a NUL-terminated byte array with the line that was read in
1208 * (without the newlines). Set @length to a #gsize to get the length
1209 * of the read line. On an error, it will return %NULL and @error
1210 * will be set. If there's no content to read, it will still return
1211 * %NULL, but @error won't be set.
1213 * Since: 2.20
1215 gchar *
1216 g_data_input_stream_read_line_finish (GDataInputStream *stream,
1217 GAsyncResult *result,
1218 gsize *length,
1219 GError **error)
1221 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
1223 return g_data_input_stream_read_finish (stream, result, length, error);
1227 * g_data_input_stream_read_line_finish_utf8:
1228 * @stream: a given #GDataInputStream.
1229 * @result: the #GAsyncResult that was provided to the callback.
1230 * @length: (out) (optional): a #gsize to get the length of the data read in.
1231 * @error: #GError for error reporting.
1233 * Finish an asynchronous call started by
1234 * g_data_input_stream_read_line_async().
1236 * Returns: (nullable) (transfer full): a string with the line that
1237 * was read in (without the newlines). Set @length to a #gsize to
1238 * get the length of the read line. On an error, it will return
1239 * %NULL and @error will be set. For UTF-8 conversion errors, the set
1240 * error domain is %G_CONVERT_ERROR. If there's no content to read,
1241 * it will still return %NULL, but @error won't be set.
1243 * Since: 2.30
1245 gchar *
1246 g_data_input_stream_read_line_finish_utf8 (GDataInputStream *stream,
1247 GAsyncResult *result,
1248 gsize *length,
1249 GError **error)
1251 gchar *res;
1253 res = g_data_input_stream_read_line_finish (stream, result, length, error);
1254 if (!res)
1255 return NULL;
1257 if (!g_utf8_validate (res, -1, NULL))
1259 g_set_error_literal (error, G_CONVERT_ERROR,
1260 G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1261 _("Invalid byte sequence in conversion input"));
1262 g_free (res);
1263 return NULL;
1265 return res;
1269 * g_data_input_stream_read_until_finish:
1270 * @stream: a given #GDataInputStream.
1271 * @result: the #GAsyncResult that was provided to the callback.
1272 * @length: (out) (optional): a #gsize to get the length of the data read in.
1273 * @error: #GError for error reporting.
1275 * Finish an asynchronous call started by
1276 * g_data_input_stream_read_until_async().
1278 * Since: 2.20
1280 * Returns: (transfer full): a string with the data that was read
1281 * before encountering any of the stop characters. Set @length to
1282 * a #gsize to get the length of the string. This function will
1283 * return %NULL on an error.
1284 * Deprecated: 2.56: Use g_data_input_stream_read_upto_finish() instead, which
1285 * has more consistent behaviour regarding the stop character.
1287 gchar *
1288 g_data_input_stream_read_until_finish (GDataInputStream *stream,
1289 GAsyncResult *result,
1290 gsize *length,
1291 GError **error)
1293 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
1295 return g_data_input_stream_read_finish (stream, result, length, error);
1299 * g_data_input_stream_read_upto:
1300 * @stream: a #GDataInputStream
1301 * @stop_chars: characters to terminate the read
1302 * @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
1303 * nul-terminated
1304 * @length: (out) (optional): a #gsize to get the length of the data read in
1305 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
1306 * @error: #GError for error reporting
1308 * Reads a string from the data input stream, up to the first
1309 * occurrence of any of the stop characters.
1311 * In contrast to g_data_input_stream_read_until(), this function
1312 * does not consume the stop character. You have to use
1313 * g_data_input_stream_read_byte() to get it before calling
1314 * g_data_input_stream_read_upto() again.
1316 * Note that @stop_chars may contain '\0' if @stop_chars_len is
1317 * specified.
1319 * The returned string will always be nul-terminated on success.
1321 * Returns: (transfer full): a string with the data that was read
1322 * before encountering any of the stop characters. Set @length to
1323 * a #gsize to get the length of the string. This function will
1324 * return %NULL on an error
1326 * Since: 2.26
1328 char *
1329 g_data_input_stream_read_upto (GDataInputStream *stream,
1330 const gchar *stop_chars,
1331 gssize stop_chars_len,
1332 gsize *length,
1333 GCancellable *cancellable,
1334 GError **error)
1336 GBufferedInputStream *bstream;
1337 gsize checked;
1338 gssize found_pos;
1339 gssize res;
1340 char *data_until;
1342 g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
1344 if (stop_chars_len < 0)
1345 stop_chars_len = strlen (stop_chars);
1347 bstream = G_BUFFERED_INPUT_STREAM (stream);
1349 checked = 0;
1351 while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len)) == -1)
1353 if (g_buffered_input_stream_get_available (bstream) ==
1354 g_buffered_input_stream_get_buffer_size (bstream))
1355 g_buffered_input_stream_set_buffer_size (bstream,
1356 2 * g_buffered_input_stream_get_buffer_size (bstream));
1358 res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
1359 if (res < 0)
1360 return NULL;
1361 if (res == 0)
1363 /* End of stream */
1364 if (g_buffered_input_stream_get_available (bstream) == 0)
1366 if (length)
1367 *length = 0;
1368 return NULL;
1370 else
1372 found_pos = checked;
1373 break;
1378 data_until = g_malloc (found_pos + 1);
1380 res = g_input_stream_read (G_INPUT_STREAM (stream),
1381 data_until,
1382 found_pos,
1383 NULL, NULL);
1384 if (length)
1385 *length = (gsize)found_pos;
1386 g_warn_if_fail (res == found_pos);
1387 data_until[found_pos] = 0;
1389 return data_until;
1393 * g_data_input_stream_read_upto_async:
1394 * @stream: a #GDataInputStream
1395 * @stop_chars: characters to terminate the read
1396 * @stop_chars_len: length of @stop_chars. May be -1 if @stop_chars is
1397 * nul-terminated
1398 * @io_priority: the [I/O priority][io-priority] of the request
1399 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
1400 * @callback: (scope async): callback to call when the request is satisfied
1401 * @user_data: (closure): the data to pass to callback function
1403 * The asynchronous version of g_data_input_stream_read_upto().
1404 * It is an error to have two outstanding calls to this function.
1406 * In contrast to g_data_input_stream_read_until(), this function
1407 * does not consume the stop character. You have to use
1408 * g_data_input_stream_read_byte() to get it before calling
1409 * g_data_input_stream_read_upto() again.
1411 * Note that @stop_chars may contain '\0' if @stop_chars_len is
1412 * specified.
1414 * When the operation is finished, @callback will be called. You
1415 * can then call g_data_input_stream_read_upto_finish() to get
1416 * the result of the operation.
1418 * Since: 2.26
1420 void
1421 g_data_input_stream_read_upto_async (GDataInputStream *stream,
1422 const gchar *stop_chars,
1423 gssize stop_chars_len,
1424 gint io_priority,
1425 GCancellable *cancellable,
1426 GAsyncReadyCallback callback,
1427 gpointer user_data)
1429 g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
1430 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1431 g_return_if_fail (stop_chars != NULL);
1433 g_data_input_stream_read_async (stream, stop_chars, stop_chars_len, io_priority,
1434 cancellable, callback, user_data);
1438 * g_data_input_stream_read_upto_finish:
1439 * @stream: a #GDataInputStream
1440 * @result: the #GAsyncResult that was provided to the callback
1441 * @length: (out) (optional): a #gsize to get the length of the data read in
1442 * @error: #GError for error reporting
1444 * Finish an asynchronous call started by
1445 * g_data_input_stream_read_upto_async().
1447 * Note that this function does not consume the stop character. You
1448 * have to use g_data_input_stream_read_byte() to get it before calling
1449 * g_data_input_stream_read_upto_async() again.
1451 * The returned string will always be nul-terminated on success.
1453 * Returns: (transfer full): a string with the data that was read
1454 * before encountering any of the stop characters. Set @length to
1455 * a #gsize to get the length of the string. This function will
1456 * return %NULL on an error.
1458 * Since: 2.24
1460 gchar *
1461 g_data_input_stream_read_upto_finish (GDataInputStream *stream,
1462 GAsyncResult *result,
1463 gsize *length,
1464 GError **error)
1466 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
1468 return g_data_input_stream_read_finish (stream, result, length, error);