1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors:
* Christian Kellner <gicmo@gnome.org>
* Krzysztof Kosiński <tweenk.pl@gmail.com>
*/
#include "config.h"
#include "gmemoryoutputstream.h"
#include "goutputstream.h"
#include "gseekable.h"
#include "gsimpleasyncresult.h"
#include "gioerror.h"
#include "string.h"
#include "glibintl.h"
/**
* SECTION:gmemoryoutputstream
* @short_description: Streaming output operations on memory chunks
* @include: gio/gio.h
* @see_also: #GMemoryInputStream
*
* #GMemoryOutputStream is a class for using arbitrary
* memory chunks as output for GIO streaming output operations.
*
*/
#define MIN_ARRAY_SIZE 16
enum {
PROP_0,
PROP_DATA,
PROP_SIZE,
PROP_DATA_SIZE,
PROP_REALLOC_FUNCTION,
PROP_DESTROY_FUNCTION
};
struct _GMemoryOutputStreamPrivate {
gpointer data; /* Write buffer */
gsize len; /* Current length of the data buffer. Can change with resizing. */
gsize valid_len; /* The part of data that has been written to */
gsize pos; /* Current position in the stream. Distinct from valid_len,
because the stream is seekable. */
GReallocFunc realloc_fn;
GDestroyNotify destroy;
};
static void g_memory_output_stream_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void g_memory_output_stream_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void g_memory_output_stream_finalize (GObject *object);
static gssize g_memory_output_stream_write (GOutputStream *stream,
const void *buffer,
gsize count,
GCancellable *cancellable,
GError **error);
static gboolean g_memory_output_stream_close (GOutputStream *stream,
GCancellable *cancellable,
GError **error);
static void g_memory_output_stream_write_async (GOutputStream *stream,
const void *buffer,
gsize count,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer data);
static gssize g_memory_output_stream_write_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error);
static void g_memory_output_stream_close_async (GOutputStream *stream,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer data);
static gboolean g_memory_output_stream_close_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error);
static void g_memory_output_stream_seekable_iface_init (GSeekableIface *iface);
static goffset g_memory_output_stream_tell (GSeekable *seekable);
static gboolean g_memory_output_stream_can_seek (GSeekable *seekable);
static gboolean g_memory_output_stream_seek (GSeekable *seekable,
goffset offset,
GSeekType type,
GCancellable *cancellable,
GError **error);
static gboolean g_memory_output_stream_can_truncate (GSeekable *seekable);
static gboolean g_memory_output_stream_truncate (GSeekable *seekable,
goffset offset,
GCancellable *cancellable,
GError **error);
G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
g_memory_output_stream_seekable_iface_init))
static void
g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
{
GOutputStreamClass *ostream_class;
GObjectClass *gobject_class;
g_type_class_add_private (klass, sizeof (GMemoryOutputStreamPrivate));
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->set_property = g_memory_output_stream_set_property;
gobject_class->get_property = g_memory_output_stream_get_property;
gobject_class->finalize = g_memory_output_stream_finalize;
ostream_class = G_OUTPUT_STREAM_CLASS (klass);
ostream_class->write_fn = g_memory_output_stream_write;
ostream_class->close_fn = g_memory_output_stream_close;
ostream_class->write_async = g_memory_output_stream_write_async;
ostream_class->write_finish = g_memory_output_stream_write_finish;
ostream_class->close_async = g_memory_output_stream_close_async;
ostream_class->close_finish = g_memory_output_stream_close_finish;
/**
* GMemoryOutputStream:data:
*
* Pointer to buffer where data will be written.
*
* Since: 2.24
**/
g_object_class_install_property (gobject_class,
PROP_DATA,
g_param_spec_pointer ("data",
P_("Data Buffer"),
P_("Pointer to buffer where data will be written."),
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/**
* GMemoryOutputStream:size:
*
* Current size of the data buffer.
*
* Since: 2.24
**/
g_object_class_install_property (gobject_class,
PROP_SIZE,
g_param_spec_ulong ("size",
P_("Data Buffer Size"),
P_("Current size of the data buffer."),
0, G_MAXULONG, 0,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/**
* GMemoryOutputStream:data-size:
*
* Size of data written to the buffer.
*
* Since: 2.24
**/
g_object_class_install_property (gobject_class,
PROP_DATA_SIZE,
g_param_spec_ulong ("data-size",
P_("Data Size"),
P_("Size of data written to the buffer."),
0, G_MAXULONG, 0,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
/**
* GMemoryOutputStream:realloc-function:
*
* Function with realloc semantics called to enlarge the buffer.
*
* Since: 2.24
**/
g_object_class_install_property (gobject_class,
PROP_REALLOC_FUNCTION,
g_param_spec_pointer ("realloc-function",
P_("Memory Reallocation Function"),
P_("Function with realloc semantics called to enlarge the buffer."),
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/**
* GMemoryOutputStream:destroy-function:
*
* Function called with the buffer as argument when the stream is destroyed.
*
* Since: 2.24
**/
g_object_class_install_property (gobject_class,
PROP_DESTROY_FUNCTION,
g_param_spec_pointer ("destroy-function",
P_("Destroy Notification Function"),
P_("Function called with the buffer as argument when the stream is destroyed."),
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
}
static void
g_memory_output_stream_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GMemoryOutputStream *stream;
GMemoryOutputStreamPrivate *priv;
stream = G_MEMORY_OUTPUT_STREAM (object);
priv = stream->priv;
switch (prop_id)
{
case PROP_DATA:
priv->data = g_value_get_pointer (value);
break;
case PROP_SIZE:
priv->len = g_value_get_ulong (value);
break;
case PROP_REALLOC_FUNCTION:
priv->realloc_fn = g_value_get_pointer (value);
break;
case PROP_DESTROY_FUNCTION:
priv->destroy = g_value_get_pointer (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
g_memory_output_stream_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GMemoryOutputStream *stream;
GMemoryOutputStreamPrivate *priv;
stream = G_MEMORY_OUTPUT_STREAM (object);
priv = stream->priv;
switch (prop_id)
{
case PROP_DATA:
g_value_set_pointer (value, priv->data);
break;
case PROP_SIZE:
g_value_set_ulong (value, priv->len);
break;
case PROP_DATA_SIZE:
g_value_set_ulong (value, priv->valid_len);
break;
case PROP_REALLOC_FUNCTION:
g_value_set_pointer (value, priv->realloc_fn);
break;
case PROP_DESTROY_FUNCTION:
g_value_set_pointer (value, priv->destroy);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
g_memory_output_stream_finalize (GObject *object)
{
GMemoryOutputStream *stream;
GMemoryOutputStreamPrivate *priv;
stream = G_MEMORY_OUTPUT_STREAM (object);
priv = stream->priv;
if (priv->destroy)
priv->destroy (priv->data);
G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
}
static void
g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
{
iface->tell = g_memory_output_stream_tell;
iface->can_seek = g_memory_output_stream_can_seek;
iface->seek = g_memory_output_stream_seek;
iface->can_truncate = g_memory_output_stream_can_truncate;
iface->truncate_fn = g_memory_output_stream_truncate;
}
static void
g_memory_output_stream_init (GMemoryOutputStream *stream)
{
stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
G_TYPE_MEMORY_OUTPUT_STREAM,
GMemoryOutputStreamPrivate);
stream->priv->pos = 0;
stream->priv->valid_len = 0;
}
/**
* g_memory_output_stream_new:
* @data: pointer to a chunk of memory to use, or %NULL
* @size: the size of @data
* @realloc_function: a function with realloc() semantics (like g_realloc())
* to be called when @data needs to be grown, or %NULL
* @destroy_function: a function to be called on @data when the stream is
* finalized, or %NULL
*
* Creates a new #GMemoryOutputStream.
*
* If @data is non-%NULL, the stream will use that for its internal storage.
* If @realloc_fn is non-%NULL, it will be used for resizing the internal
* storage when necessary. To construct a fixed-size output stream,
* pass %NULL as @realloc_fn.
*
* |[
* /* a stream that can grow */
* stream = g_memory_output_stream_new (NULL, 0, realloc, free);
*
* /* another stream that can grow */
* stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
*
* /* a fixed-size stream */
* data = malloc (200);
* stream3 = g_memory_output_stream_new (data, 200, NULL, free);
* ]|
*
* Return value: A newly created #GMemoryOutputStream object.
**/
GOutputStream *
g_memory_output_stream_new (gpointer data,
gsize size,
GReallocFunc realloc_function,
GDestroyNotify destroy_function)
{
GOutputStream *stream;
stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
"data", data,
"size", size,
"realloc-function", realloc_function,
"destroy-function", destroy_function,
NULL);
return stream;
}
/**
* g_memory_output_stream_get_data:
* @ostream: a #GMemoryOutputStream
*
* Gets any loaded data from the @ostream.
*
* Note that the returned pointer may become invalid on the next
* write or truncate operation on the stream.
*
* Returns: pointer to the stream's data
**/
gpointer
g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
{
g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
return ostream->priv->data;
}
/**
* g_memory_output_stream_get_size:
* @ostream: a #GMemoryOutputStream
*
* Gets the size of the currently allocated data area (availible from
* g_memory_output_stream_get_data()). If the stream isn't
* growable (no realloc was passed to g_memory_output_stream_new()) then
* this is the maximum size of the stream and further writes
* will return %G_IO_ERROR_NO_SPACE.
*
* Note that for growable streams the returned size may become invalid on
* the next write or truncate operation on the stream.
*
* If you want the number of bytes currently written to the stream, use
* g_memory_output_stream_get_data_size().
*
* Returns: the number of bytes allocated for the data buffer
*/
gsize
g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
{
g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
return ostream->priv->len;
}
/**
* g_memory_output_stream_get_data_size:
* @ostream: a #GMemoryOutputStream
*
* Returns the number of bytes from the start up
* to including the last byte written in the stream
* that has not been truncated away.
*
* Returns: the number of bytes written to the stream
*
* Since: 2.18
*/
gsize
g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
{
g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
return ostream->priv->valid_len;
}
static gboolean
array_resize (GMemoryOutputStream *ostream,
gsize size,
gboolean allow_partial,
GError **error)
{
GMemoryOutputStreamPrivate *priv;
gpointer data;
gsize len;
priv = ostream->priv;
if (priv->len == size)
return TRUE;
if (!priv->realloc_fn)
{
if (allow_partial &&
priv->pos < priv->len)
return TRUE; /* Short write */
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_NO_SPACE,
_("Memory output stream not resizable"));
return FALSE;
}
len = priv->len;
data = priv->realloc_fn (priv->data, size);
if (size > 0 && !data)
{
if (allow_partial &&
priv->pos < priv->len)
return TRUE; /* Short write */
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_NO_SPACE,
_("Failed to resize memory output stream"));
return FALSE;
}
if (size > len)
memset ((guint8 *)data + len, 0, size - len);
priv->data = data;
priv->len = size;
if (priv->len < priv->valid_len)
priv->valid_len = priv->len;
return TRUE;
}
static gint
g_nearest_pow (gint num)
{
gint n = 1;
while (n < num)
n <<= 1;
return n;
}
static gssize
g_memory_output_stream_write (GOutputStream *stream,
const void *buffer,
gsize count,
GCancellable *cancellable,
GError **error)
{
GMemoryOutputStream *ostream;
GMemoryOutputStreamPrivate *priv;
guint8 *dest;
gsize new_size;
ostream = G_MEMORY_OUTPUT_STREAM (stream);
priv = ostream->priv;
if (count == 0)
return 0;
/* Check for address space overflow, but only if the buffer is resizable.
Otherwise we just do a short write and don't worry. */
if (priv->realloc_fn && priv->pos + count < priv->pos)
goto overflow;
if (priv->pos + count > priv->len)
{
/* At least enought to fit the write, rounded up
for greater than linear growth.
TODO: This wastes a lot of memory at large stream sizes.
Figure out a more rational allocation strategy. */
new_size = g_nearest_pow (priv->pos + count);
/* Check for overflow again. We have only checked if
pos + count > G_MAXSIZE, but it only catches the case of writing
more than 4GiB total on a 32-bit system. There's still the problem
of g_nearest_pow overflowing above 0x7fffffff, so we're
effectively limited to 2GiB. */
if (new_size < priv->len)
goto overflow;
new_size = MAX (new_size, MIN_ARRAY_SIZE);
if (!array_resize (ostream, new_size, TRUE, error))
return -1;
}
/* Make sure we handle short writes if the array_resize
only added part of the required memory */
count = MIN (count, priv->len - priv->pos);
dest = (guint8 *)priv->data + priv->pos;
memcpy (dest, buffer, count);
priv->pos += count;
if (priv->pos > priv->valid_len)
priv->valid_len = priv->pos;
return count;
overflow:
/* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_NO_SPACE,
_("Amount of memory required to process the write is "
"larger than available address space"));
return -1;
}
static gboolean
g_memory_output_stream_close (GOutputStream *stream,
GCancellable *cancellable,
GError **error)
{
return TRUE;
}
static void
g_memory_output_stream_write_async (GOutputStream *stream,
const void *buffer,
gsize count,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer data)
{
GSimpleAsyncResult *simple;
gssize nwritten;
nwritten = g_memory_output_stream_write (stream,
buffer,
count,
cancellable,
NULL);
simple = g_simple_async_result_new (G_OBJECT (stream),
callback,
data,
g_memory_output_stream_write_async);
g_simple_async_result_set_op_res_gssize (simple, nwritten);
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
}
static gssize
g_memory_output_stream_write_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
gssize nwritten;
simple = G_SIMPLE_ASYNC_RESULT (result);
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
g_memory_output_stream_write_async);
nwritten = g_simple_async_result_get_op_res_gssize (simple);
return nwritten;
}
static void
g_memory_output_stream_close_async (GOutputStream *stream,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer data)
{
GSimpleAsyncResult *simple;
simple = g_simple_async_result_new (G_OBJECT (stream),
callback,
data,
g_memory_output_stream_close_async);
/* will always return TRUE */
g_memory_output_stream_close (stream, cancellable, NULL);
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
}
static gboolean
g_memory_output_stream_close_finish (GOutputStream *stream,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
simple = G_SIMPLE_ASYNC_RESULT (result);
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
g_memory_output_stream_close_async);
return TRUE;
}
static goffset
g_memory_output_stream_tell (GSeekable *seekable)
{
GMemoryOutputStream *stream;
GMemoryOutputStreamPrivate *priv;
stream = G_MEMORY_OUTPUT_STREAM (seekable);
priv = stream->priv;
return priv->pos;
}
static gboolean
g_memory_output_stream_can_seek (GSeekable *seekable)
{
return TRUE;
}
static gboolean
g_memory_output_stream_seek (GSeekable *seekable,
goffset offset,
GSeekType type,
GCancellable *cancellable,
GError **error)
{
GMemoryOutputStream *stream;
GMemoryOutputStreamPrivate *priv;
goffset absolute;
stream = G_MEMORY_OUTPUT_STREAM (seekable);
priv = stream->priv;
switch (type)
{
case G_SEEK_CUR:
absolute = priv->pos + offset;
break;
case G_SEEK_SET:
absolute = offset;
break;
case G_SEEK_END:
absolute = priv->len + offset;
break;
default:
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
_("Invalid GSeekType supplied"));
return FALSE;
}
if (absolute < 0)
{
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
_("Requested seek before the beginning of the stream"));
return FALSE;
}
if (absolute > priv->len)
{
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
_("Requested seek beyond the end of the stream"));
return FALSE;
}
priv->pos = absolute;
return TRUE;
}
static gboolean
g_memory_output_stream_can_truncate (GSeekable *seekable)
{
GMemoryOutputStream *ostream;
GMemoryOutputStreamPrivate *priv;
ostream = G_MEMORY_OUTPUT_STREAM (seekable);
priv = ostream->priv;
return priv->realloc_fn != NULL;
}
static gboolean
g_memory_output_stream_truncate (GSeekable *seekable,
goffset offset,
GCancellable *cancellable,
GError **error)
{
GMemoryOutputStream *ostream = G_MEMORY_OUTPUT_STREAM (seekable);
if (!array_resize (ostream, offset, FALSE, error))
return FALSE;
return TRUE;
}
|