Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gmemoryoutputstream.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 * 
3
 * Copyright (C) 2006-2007 Red Hat, Inc.
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
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.
11
 *
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.
16
 *
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/>.
19
 *
20
 * Authors:
21
 *   Christian Kellner <gicmo@gnome.org>
22
 *   Krzysztof KosiƄski <tweenk.pl@gmail.com>
23
 */
24
25
#include "config.h"
26
#include "gmemoryoutputstream.h"
27
#include "goutputstream.h"
28
#include "gpollableoutputstream.h"
29
#include "gseekable.h"
30
#include "gtask.h"
31
#include "gioerror.h"
32
#include "string.h"
33
#include "glibintl.h"
34
#include "gutilsprivate.h"
35
36
37
/**
38
 * SECTION:gmemoryoutputstream
39
 * @short_description: Streaming output operations on memory chunks
40
 * @include: gio/gio.h
41
 * @see_also: #GMemoryInputStream
42
 *
43
 * #GMemoryOutputStream is a class for using arbitrary
44
 * memory chunks as output for GIO streaming output operations.
45
 *
46
 * As of GLib 2.34, #GMemoryOutputStream trivially implements
47
 * #GPollableOutputStream: it always polls as ready.
48
 */
49
50
#define MIN_ARRAY_SIZE  16
51
52
enum {
53
  PROP_0,
54
  PROP_DATA,
55
  PROP_SIZE,
56
  PROP_DATA_SIZE,
57
  PROP_REALLOC_FUNCTION,
58
  PROP_DESTROY_FUNCTION
59
};
60
61
struct _GMemoryOutputStreamPrivate
62
{
63
  gpointer       data; /* Write buffer */
64
  gsize          len; /* Current length of the data buffer. Can change with resizing. */
65
  gsize          valid_len; /* The part of data that has been written to */
66
  gsize          pos; /* Current position in the stream. Distinct from valid_len,
67
                         because the stream is seekable. */
68
69
  GReallocFunc   realloc_fn;
70
  GDestroyNotify destroy;
71
};
72
73
static void     g_memory_output_stream_set_property (GObject      *object,
74
                                                     guint         prop_id,
75
                                                     const GValue *value,
76
                                                     GParamSpec   *pspec);
77
static void     g_memory_output_stream_get_property (GObject      *object,
78
                                                     guint         prop_id,
79
                                                     GValue       *value,
80
                                                     GParamSpec   *pspec);
81
static void     g_memory_output_stream_finalize     (GObject      *object);
82
83
static gssize   g_memory_output_stream_write       (GOutputStream *stream,
84
                                                    const void    *buffer,
85
                                                    gsize          count,
86
                                                    GCancellable  *cancellable,
87
                                                    GError       **error);
88
89
static gboolean g_memory_output_stream_close       (GOutputStream  *stream,
90
                                                    GCancellable   *cancellable,
91
                                                    GError        **error);
92
93
static void     g_memory_output_stream_close_async  (GOutputStream        *stream,
94
                                                     int                   io_priority,
95
                                                     GCancellable         *cancellable,
96
                                                     GAsyncReadyCallback   callback,
97
                                                     gpointer              data);
98
static gboolean g_memory_output_stream_close_finish (GOutputStream        *stream,
99
                                                     GAsyncResult         *result,
100
                                                     GError              **error);
101
102
static void     g_memory_output_stream_seekable_iface_init (GSeekableIface  *iface);
103
static goffset  g_memory_output_stream_tell                (GSeekable       *seekable);
104
static gboolean g_memory_output_stream_can_seek            (GSeekable       *seekable);
105
static gboolean g_memory_output_stream_seek                (GSeekable       *seekable,
106
                                                           goffset          offset,
107
                                                           GSeekType        type,
108
                                                           GCancellable    *cancellable,
109
                                                           GError         **error);
110
static gboolean g_memory_output_stream_can_truncate        (GSeekable       *seekable);
111
static gboolean g_memory_output_stream_truncate            (GSeekable       *seekable,
112
                                                           goffset          offset,
113
                                                           GCancellable    *cancellable,
114
                                                           GError         **error);
115
116
static gboolean g_memory_output_stream_is_writable       (GPollableOutputStream *stream);
117
static GSource *g_memory_output_stream_create_source     (GPollableOutputStream *stream,
118
                                                          GCancellable          *cancellable);
119
120
static void g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
121
122
G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
123
                         G_ADD_PRIVATE (GMemoryOutputStream)
124
                         G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
125
                                                g_memory_output_stream_seekable_iface_init);
126
                         G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
127
                                                g_memory_output_stream_pollable_iface_init))
128
129
130
static void
131
g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
132
1
{
133
1
  GOutputStreamClass *ostream_class;
134
1
  GObjectClass *gobject_class;
135
136
1
  gobject_class = G_OBJECT_CLASS (klass);
137
1
  gobject_class->set_property = g_memory_output_stream_set_property;
138
1
  gobject_class->get_property = g_memory_output_stream_get_property;
139
1
  gobject_class->finalize     = g_memory_output_stream_finalize;
140
141
1
  ostream_class = G_OUTPUT_STREAM_CLASS (klass);
142
143
1
  ostream_class->write_fn = g_memory_output_stream_write;
144
1
  ostream_class->close_fn = g_memory_output_stream_close;
145
1
  ostream_class->close_async  = g_memory_output_stream_close_async;
146
1
  ostream_class->close_finish = g_memory_output_stream_close_finish;
147
148
  /**
149
   * GMemoryOutputStream:data:
150
   *
151
   * Pointer to buffer where data will be written.
152
   *
153
   * Since: 2.24
154
   **/
155
1
  g_object_class_install_property (gobject_class,
156
1
                                   PROP_DATA,
157
1
                                   g_param_spec_pointer ("data",
158
1
                                                         P_("Data Buffer"),
159
1
                                                         P_("Pointer to buffer where data will be written."),
160
1
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
161
1
                                                         G_PARAM_STATIC_STRINGS));
162
163
  /**
164
   * GMemoryOutputStream:size:
165
   *
166
   * Current size of the data buffer.
167
   *
168
   * Since: 2.24
169
   **/
170
1
  g_object_class_install_property (gobject_class,
171
1
                                   PROP_SIZE,
172
1
                                   g_param_spec_ulong ("size",
173
1
                                                       P_("Data Buffer Size"),
174
1
                                                       P_("Current size of the data buffer."),
175
1
                                                       0, G_MAXULONG, 0,
176
1
                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
177
1
                                                       G_PARAM_STATIC_STRINGS));
178
179
  /**
180
   * GMemoryOutputStream:data-size:
181
   *
182
   * Size of data written to the buffer.
183
   *
184
   * Since: 2.24
185
   **/
186
1
  g_object_class_install_property (gobject_class,
187
1
                                   PROP_DATA_SIZE,
188
1
                                   g_param_spec_ulong ("data-size",
189
1
                                                       P_("Data Size"),
190
1
                                                       P_("Size of data written to the buffer."),
191
1
                                                       0, G_MAXULONG, 0,
192
1
                                                       G_PARAM_READABLE |
193
1
                                                       G_PARAM_STATIC_STRINGS));
194
195
  /**
196
   * GMemoryOutputStream:realloc-function: (skip)
197
   *
198
   * Function with realloc semantics called to enlarge the buffer.
199
   *
200
   * Since: 2.24
201
   **/
202
1
  g_object_class_install_property (gobject_class,
203
1
                                   PROP_REALLOC_FUNCTION,
204
1
                                   g_param_spec_pointer ("realloc-function",
205
1
                                                         P_("Memory Reallocation Function"),
206
1
                                                         P_("Function with realloc semantics called to enlarge the buffer."),
207
1
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
208
1
                                                         G_PARAM_STATIC_STRINGS));
209
210
  /**
211
   * GMemoryOutputStream:destroy-function: (skip)
212
   *
213
   * Function called with the buffer as argument when the stream is destroyed.
214
   *
215
   * Since: 2.24
216
   **/
217
1
  g_object_class_install_property (gobject_class,
218
1
                                   PROP_DESTROY_FUNCTION,
219
1
                                   g_param_spec_pointer ("destroy-function",
220
1
                                                         P_("Destroy Notification Function"),
221
1
                                                         P_("Function called with the buffer as argument when the stream is destroyed."),
222
1
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
223
1
                                                         G_PARAM_STATIC_STRINGS));
224
1
}
225
226
static void
227
g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
228
1
{
229
1
  iface->is_writable = g_memory_output_stream_is_writable;
230
1
  iface->create_source = g_memory_output_stream_create_source;
231
1
}
232
233
static void
234
g_memory_output_stream_set_property (GObject      *object,
235
                                     guint         prop_id,
236
                                     const GValue *value,
237
                                     GParamSpec   *pspec)
238
4.63k
{
239
4.63k
  GMemoryOutputStream        *stream;
240
4.63k
  GMemoryOutputStreamPrivate *priv;
241
242
4.63k
  stream = G_MEMORY_OUTPUT_STREAM (object);
243
4.63k
  priv = stream->priv;
244
245
4.63k
  switch (prop_id)
246
4.63k
    {
247
1.15k
    case PROP_DATA:
248
1.15k
      priv->data = g_value_get_pointer (value);
249
1.15k
      break;
250
1.15k
    case PROP_SIZE:
251
1.15k
      priv->len = g_value_get_ulong (value);
252
1.15k
      break;
253
1.15k
    case PROP_REALLOC_FUNCTION:
254
1.15k
      priv->realloc_fn = g_value_get_pointer (value);
255
1.15k
      break;
256
1.15k
    case PROP_DESTROY_FUNCTION:
257
1.15k
      priv->destroy = g_value_get_pointer (value);
258
1.15k
      break;
259
0
    default:
260
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261
0
      break;
262
4.63k
    }
263
4.63k
}
264
265
static void
266
g_memory_output_stream_get_property (GObject      *object,
267
                                     guint         prop_id,
268
                                     GValue       *value,
269
                                     GParamSpec   *pspec)
270
0
{
271
0
  GMemoryOutputStream        *stream;
272
0
  GMemoryOutputStreamPrivate *priv;
273
274
0
  stream = G_MEMORY_OUTPUT_STREAM (object);
275
0
  priv = stream->priv;
276
277
0
  switch (prop_id)
278
0
    {
279
0
    case PROP_DATA:
280
0
      g_value_set_pointer (value, priv->data);
281
0
      break;
282
0
    case PROP_SIZE:
283
0
      g_value_set_ulong (value, priv->len);
284
0
      break;
285
0
    case PROP_DATA_SIZE:
286
0
      g_value_set_ulong (value, priv->valid_len);
287
0
      break;
288
0
    case PROP_REALLOC_FUNCTION:
289
0
      g_value_set_pointer (value, priv->realloc_fn);
290
0
      break;
291
0
    case PROP_DESTROY_FUNCTION:
292
0
      g_value_set_pointer (value, priv->destroy);
293
0
      break;
294
0
    default:
295
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296
0
      break;
297
0
    }
298
0
}
299
300
static void
301
g_memory_output_stream_finalize (GObject *object)
302
1.15k
{
303
1.15k
  GMemoryOutputStream        *stream;
304
1.15k
  GMemoryOutputStreamPrivate *priv;
305
306
1.15k
  stream = G_MEMORY_OUTPUT_STREAM (object);
307
1.15k
  priv = stream->priv;
308
  
309
1.15k
  if (priv->destroy)
310
1.15k
    priv->destroy (priv->data);
311
312
1.15k
  G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
313
1.15k
}
314
315
static void
316
g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
317
1
{
318
1
  iface->tell         = g_memory_output_stream_tell;
319
1
  iface->can_seek     = g_memory_output_stream_can_seek;
320
1
  iface->seek         = g_memory_output_stream_seek;
321
1
  iface->can_truncate = g_memory_output_stream_can_truncate;
322
1
  iface->truncate_fn  = g_memory_output_stream_truncate;
323
1
}
324
325
326
static void
327
g_memory_output_stream_init (GMemoryOutputStream *stream)
328
1.15k
{
329
1.15k
  stream->priv = g_memory_output_stream_get_instance_private (stream);
330
1.15k
  stream->priv->pos = 0;
331
1.15k
  stream->priv->valid_len = 0;
332
1.15k
}
333
334
/**
335
 * g_memory_output_stream_new: (skip)
336
 * @data: (nullable): pointer to a chunk of memory to use, or %NULL
337
 * @size: the size of @data
338
 * @realloc_function: (nullable): a function with realloc() semantics (like g_realloc())
339
 *     to be called when @data needs to be grown, or %NULL
340
 * @destroy_function: (nullable): a function to be called on @data when the stream is
341
 *     finalized, or %NULL
342
 *
343
 * Creates a new #GMemoryOutputStream.
344
 *
345
 * In most cases this is not the function you want.  See
346
 * g_memory_output_stream_new_resizable() instead.
347
 *
348
 * If @data is non-%NULL, the stream will use that for its internal storage.
349
 *
350
 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
351
 * storage when necessary and the stream will be considered resizable.
352
 * In that case, the stream will start out being (conceptually) empty.
353
 * @size is used only as a hint for how big @data is.  Specifically,
354
 * seeking to the end of a newly-created stream will seek to zero, not
355
 * @size.  Seeking past the end of the stream and then writing will
356
 * introduce a zero-filled gap.
357
 *
358
 * If @realloc_fn is %NULL then the stream is fixed-sized.  Seeking to
359
 * the end will seek to @size exactly.  Writing past the end will give
360
 * an 'out of space' error.  Attempting to seek past the end will fail.
361
 * Unlike the resizable case, seeking to an offset within the stream and
362
 * writing will preserve the bytes passed in as @data before that point
363
 * and will return them as part of g_memory_output_stream_steal_data().
364
 * If you intend to seek you should probably therefore ensure that @data
365
 * is properly initialised.
366
 *
367
 * It is probably only meaningful to provide @data and @size in the case
368
 * that you want a fixed-sized stream.  Put another way: if @realloc_fn
369
 * is non-%NULL then it makes most sense to give @data as %NULL and
370
 * @size as 0 (allowing #GMemoryOutputStream to do the initial
371
 * allocation for itself).
372
 *
373
 * |[<!-- language="C" -->
374
 * // a stream that can grow
375
 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
376
 *
377
 * // another stream that can grow
378
 * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
379
 *
380
 * // a fixed-size stream
381
 * data = malloc (200);
382
 * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
383
 * ]|
384
 *
385
 * Returns: A newly created #GMemoryOutputStream object.
386
 **/
387
GOutputStream *
388
g_memory_output_stream_new (gpointer       data,
389
                            gsize          size,
390
                            GReallocFunc   realloc_function,
391
                            GDestroyNotify destroy_function)
392
1.15k
{
393
1.15k
  GOutputStream *stream;
394
395
1.15k
  stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
396
1.15k
                         "data", data,
397
1.15k
                         "size", size,
398
1.15k
                         "realloc-function", realloc_function,
399
1.15k
                         "destroy-function", destroy_function,
400
1.15k
                         NULL);
401
402
1.15k
  return stream;
403
1.15k
}
404
405
/**
406
 * g_memory_output_stream_new_resizable:
407
 *
408
 * Creates a new #GMemoryOutputStream, using g_realloc() and g_free()
409
 * for memory allocation.
410
 *
411
 * Since: 2.36
412
 */
413
GOutputStream *
414
g_memory_output_stream_new_resizable (void)
415
1.15k
{
416
1.15k
  return g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
417
1.15k
}
418
419
/**
420
 * g_memory_output_stream_get_data:
421
 * @ostream: a #GMemoryOutputStream
422
 *
423
 * Gets any loaded data from the @ostream.
424
 *
425
 * Note that the returned pointer may become invalid on the next
426
 * write or truncate operation on the stream.
427
 *
428
 * Returns: (transfer none): pointer to the stream's data, or %NULL if the data
429
 *    has been stolen
430
 **/
431
gpointer
432
g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
433
0
{
434
0
  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
435
436
0
  return ostream->priv->data;
437
0
}
438
439
/**
440
 * g_memory_output_stream_get_size:
441
 * @ostream: a #GMemoryOutputStream
442
 *
443
 * Gets the size of the currently allocated data area (available from
444
 * g_memory_output_stream_get_data()).
445
 *
446
 * You probably don't want to use this function on resizable streams.
447
 * See g_memory_output_stream_get_data_size() instead.  For resizable
448
 * streams the size returned by this function is an implementation
449
 * detail and may be change at any time in response to operations on the
450
 * stream.
451
 *
452
 * If the stream is fixed-sized (ie: no realloc was passed to
453
 * g_memory_output_stream_new()) then this is the maximum size of the
454
 * stream and further writes will return %G_IO_ERROR_NO_SPACE.
455
 *
456
 * In any case, if you want the number of bytes currently written to the
457
 * stream, use g_memory_output_stream_get_data_size().
458
 *
459
 * Returns: the number of bytes allocated for the data buffer
460
 */
461
gsize
462
g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
463
0
{
464
0
  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
465
466
0
  return ostream->priv->len;
467
0
}
468
469
/**
470
 * g_memory_output_stream_get_data_size:
471
 * @ostream: a #GMemoryOutputStream
472
 *
473
 * Returns the number of bytes from the start up to including the last
474
 * byte written in the stream that has not been truncated away.
475
 *
476
 * Returns: the number of bytes written to the stream
477
 *
478
 * Since: 2.18
479
 */
480
gsize
481
g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
482
0
{
483
0
  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
484
485
0
  return ostream->priv->valid_len;
486
0
}
487
488
/**
489
 * g_memory_output_stream_steal_data:
490
 * @ostream: a #GMemoryOutputStream
491
 *
492
 * Gets any loaded data from the @ostream. Ownership of the data
493
 * is transferred to the caller; when no longer needed it must be
494
 * freed using the free function set in @ostream's
495
 * #GMemoryOutputStream:destroy-function property.
496
 *
497
 * @ostream must be closed before calling this function.
498
 *
499
 * Returns: (transfer full): the stream's data, or %NULL if it has previously
500
 *    been stolen
501
 *
502
 * Since: 2.26
503
 **/
504
gpointer
505
g_memory_output_stream_steal_data (GMemoryOutputStream *ostream)
506
0
{
507
0
  gpointer data;
508
509
0
  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
510
0
  g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
511
512
0
  data = ostream->priv->data;
513
0
  ostream->priv->data = NULL;
514
515
0
  return data;
516
0
}
517
518
/**
519
 * g_memory_output_stream_steal_as_bytes:
520
 * @ostream: a #GMemoryOutputStream
521
 *
522
 * Returns data from the @ostream as a #GBytes. @ostream must be
523
 * closed before calling this function.
524
 *
525
 * Returns: (transfer full): the stream's data
526
 *
527
 * Since: 2.34
528
 **/
529
GBytes *
530
g_memory_output_stream_steal_as_bytes (GMemoryOutputStream *ostream)
531
0
{
532
0
  GBytes *result;
533
534
0
  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
535
0
  g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
536
537
0
  result = g_bytes_new_with_free_func (ostream->priv->data,
538
0
                                       ostream->priv->valid_len,
539
0
                                       ostream->priv->destroy,
540
0
                                       ostream->priv->data);
541
0
  ostream->priv->data = NULL;
542
543
0
  return result;
544
0
}
545
546
static gboolean
547
array_resize (GMemoryOutputStream  *ostream,
548
              gsize                 size,
549
              gboolean              allow_partial,
550
              GError              **error)
551
3.94k
{
552
3.94k
  GMemoryOutputStreamPrivate *priv;
553
3.94k
  gpointer data;
554
3.94k
  gsize len;
555
556
3.94k
  priv = ostream->priv;
557
558
3.94k
  if (priv->len == size)
559
0
    return TRUE;
560
561
3.94k
  if (!priv->realloc_fn)
562
0
    {
563
0
      if (allow_partial &&
564
0
          priv->pos < priv->len)
565
0
        return TRUE; /* Short write */
566
567
0
      g_set_error_literal (error,
568
0
                           G_IO_ERROR,
569
0
                           G_IO_ERROR_NO_SPACE,
570
0
                           _("Memory output stream not resizable"));
571
0
      return FALSE;
572
0
    }
573
574
3.94k
  len = priv->len;
575
3.94k
  data = priv->realloc_fn (priv->data, size);
576
577
3.94k
  if (size > 0 && !data)
578
0
    {
579
0
      if (allow_partial &&
580
0
          priv->pos < priv->len)
581
0
        return TRUE; /* Short write */
582
583
0
      g_set_error_literal (error,
584
0
                           G_IO_ERROR,
585
0
                           G_IO_ERROR_NO_SPACE,
586
0
                           _("Failed to resize memory output stream"));
587
0
      return FALSE;
588
0
    }
589
590
3.94k
  if (size > len)
591
3.94k
    memset ((guint8 *)data + len, 0, size - len);
592
593
3.94k
  priv->data = data;
594
3.94k
  priv->len = size;
595
596
3.94k
  if (priv->len < priv->valid_len)
597
0
    priv->valid_len = priv->len;
598
599
3.94k
  return TRUE;
600
3.94k
}
601
602
static gssize
603
g_memory_output_stream_write (GOutputStream  *stream,
604
                              const void     *buffer,
605
                              gsize           count,
606
                              GCancellable   *cancellable,
607
                              GError        **error)
608
12.6k
{
609
12.6k
  GMemoryOutputStream        *ostream;
610
12.6k
  GMemoryOutputStreamPrivate *priv;
611
12.6k
  guint8   *dest;
612
12.6k
  gsize new_size;
613
614
12.6k
  ostream = G_MEMORY_OUTPUT_STREAM (stream);
615
12.6k
  priv = ostream->priv;
616
617
12.6k
  if (count == 0)
618
0
    return 0;
619
620
  /* Check for address space overflow, but only if the buffer is resizable.
621
     Otherwise we just do a short write and don't worry. */
622
12.6k
  if (priv->realloc_fn && priv->pos + count < priv->pos)
623
0
    goto overflow;
624
625
12.6k
  if (priv->pos + count > priv->len)
626
3.94k
    {
627
      /* At least enough to fit the write, rounded up for greater than
628
       * linear growth.
629
       *
630
       * Assuming that we're using something like realloc(), the kernel
631
       * will overcommit memory to us, so doubling the size each time
632
       * will keep the number of realloc calls low without wasting too
633
       * much memory.
634
       */
635
3.94k
      new_size = g_nearest_pow (priv->pos + count);
636
      /* Check for overflow again. We have checked if
637
         pos + count > G_MAXSIZE, but now check if g_nearest_pow () has
638
         overflowed */
639
3.94k
      if (new_size == 0)
640
0
        goto overflow;
641
642
3.94k
      new_size = MAX (new_size, MIN_ARRAY_SIZE);
643
3.94k
      if (!array_resize (ostream, new_size, TRUE, error))
644
0
        return -1;
645
3.94k
    }
646
647
  /* Make sure we handle short writes if the array_resize
648
     only added part of the required memory */
649
12.6k
  count = MIN (count, priv->len - priv->pos);
650
651
12.6k
  dest = (guint8 *)priv->data + priv->pos;
652
12.6k
  memcpy (dest, buffer, count);
653
12.6k
  priv->pos += count;
654
655
12.6k
  if (priv->pos > priv->valid_len)
656
12.6k
    priv->valid_len = priv->pos;
657
658
12.6k
  return count;
659
660
0
 overflow:
661
  /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
662
0
  g_set_error_literal (error,
663
0
                       G_IO_ERROR,
664
0
                       G_IO_ERROR_NO_SPACE,
665
0
                       _("Amount of memory required to process the write is "
666
0
                         "larger than available address space"));
667
0
  return -1;
668
12.6k
}
669
670
static gboolean
671
g_memory_output_stream_close (GOutputStream  *stream,
672
                              GCancellable   *cancellable,
673
                              GError        **error)
674
1.15k
{
675
1.15k
  return TRUE;
676
1.15k
}
677
678
static void
679
g_memory_output_stream_close_async (GOutputStream       *stream,
680
                                    int                  io_priority,
681
                                    GCancellable        *cancellable,
682
                                    GAsyncReadyCallback  callback,
683
                                    gpointer             data)
684
0
{
685
0
  GTask *task;
686
687
0
  task = g_task_new (stream, cancellable, callback, data);
688
0
  g_task_set_source_tag (task, g_memory_output_stream_close_async);
689
690
  /* will always return TRUE */
691
0
  g_memory_output_stream_close (stream, cancellable, NULL);
692
693
0
  g_task_return_boolean (task, TRUE);
694
0
  g_object_unref (task);
695
0
}
696
697
static gboolean
698
g_memory_output_stream_close_finish (GOutputStream  *stream,
699
                                     GAsyncResult   *result,
700
                                     GError        **error)
701
0
{
702
0
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
703
704
0
  return g_task_propagate_boolean (G_TASK (result), error);
705
0
}
706
707
static goffset
708
g_memory_output_stream_tell (GSeekable *seekable)
709
0
{
710
0
  GMemoryOutputStream *stream;
711
0
  GMemoryOutputStreamPrivate *priv;
712
713
0
  stream = G_MEMORY_OUTPUT_STREAM (seekable);
714
0
  priv = stream->priv;
715
716
0
  return priv->pos;
717
0
}
718
719
static gboolean
720
g_memory_output_stream_can_seek (GSeekable *seekable)
721
0
{
722
0
  return TRUE;
723
0
}
724
725
static gboolean
726
g_memory_output_stream_seek (GSeekable    *seekable,
727
                             goffset        offset,
728
                             GSeekType      type,
729
                             GCancellable  *cancellable,
730
                             GError       **error)
731
0
{
732
0
  GMemoryOutputStream        *stream;
733
0
  GMemoryOutputStreamPrivate *priv;
734
0
  goffset absolute;
735
736
0
  stream = G_MEMORY_OUTPUT_STREAM (seekable);
737
0
  priv = stream->priv;
738
739
0
  switch (type)
740
0
    {
741
0
    case G_SEEK_CUR:
742
0
      absolute = priv->pos + offset;
743
0
      break;
744
745
0
    case G_SEEK_SET:
746
0
      absolute = offset;
747
0
      break;
748
749
0
    case G_SEEK_END:
750
      /* For resizable streams, we consider the end to be the data
751
       * length.  For fixed-sized streams, we consider the end to be the
752
       * size of the buffer.
753
       */
754
0
      if (priv->realloc_fn)
755
0
        absolute = priv->valid_len + offset;
756
0
      else
757
0
        absolute = priv->len + offset;
758
0
      break;
759
760
0
    default:
761
0
      g_set_error_literal (error,
762
0
                           G_IO_ERROR,
763
0
                           G_IO_ERROR_INVALID_ARGUMENT,
764
0
                           _("Invalid GSeekType supplied"));
765
766
0
      return FALSE;
767
0
    }
768
769
0
  if (absolute < 0)
770
0
    {
771
0
      g_set_error_literal (error,
772
0
                           G_IO_ERROR,
773
0
                           G_IO_ERROR_INVALID_ARGUMENT,
774
0
                           _("Requested seek before the beginning of the stream"));
775
0
      return FALSE;
776
0
    }
777
778
  /* Can't seek past the end of a fixed-size stream.
779
   *
780
   * Note: seeking to the non-existent byte at the end of a fixed-sized
781
   * stream is valid (eg: a 1-byte fixed sized stream can have position
782
   * 0 or 1).  Therefore '>' is what we want.
783
   * */
784
0
  if (priv->realloc_fn == NULL && (gsize) absolute > priv->len)
785
0
    {
786
0
      g_set_error_literal (error,
787
0
                           G_IO_ERROR,
788
0
                           G_IO_ERROR_INVALID_ARGUMENT,
789
0
                           _("Requested seek beyond the end of the stream"));
790
0
      return FALSE;
791
0
    }
792
793
0
  priv->pos = absolute;
794
795
0
  return TRUE;
796
0
}
797
798
static gboolean
799
g_memory_output_stream_can_truncate (GSeekable *seekable)
800
0
{
801
0
  GMemoryOutputStream *ostream;
802
0
  GMemoryOutputStreamPrivate *priv;
803
804
0
  ostream = G_MEMORY_OUTPUT_STREAM (seekable);
805
0
  priv = ostream->priv;
806
807
  /* We do not allow truncation of fixed-sized streams */
808
0
  return priv->realloc_fn != NULL;
809
0
}
810
811
static gboolean
812
g_memory_output_stream_truncate (GSeekable     *seekable,
813
                                 goffset        offset,
814
                                 GCancellable  *cancellable,
815
                                 GError       **error)
816
0
{
817
0
  GMemoryOutputStream *ostream = G_MEMORY_OUTPUT_STREAM (seekable);
818
819
0
  if (!array_resize (ostream, offset, FALSE, error))
820
0
    return FALSE;
821
822
0
  ostream->priv->valid_len = offset;
823
824
0
  return TRUE;
825
0
}
826
827
static gboolean
828
g_memory_output_stream_is_writable (GPollableOutputStream *stream)
829
0
{
830
0
  return TRUE;
831
0
}
832
833
static GSource *
834
g_memory_output_stream_create_source (GPollableOutputStream *stream,
835
                                      GCancellable          *cancellable)
836
0
{
837
0
  GSource *base_source, *pollable_source;
838
839
0
  base_source = g_timeout_source_new (0);
840
0
  pollable_source = g_pollable_source_new_full (stream, base_source, cancellable);
841
0
  g_source_unref (base_source);
842
843
0
  return pollable_source;
844
0
}