Blame gio/gmemoryoutputstream.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors:
Packit ae235b
 *   Christian Kellner <gicmo@gnome.org>
Packit ae235b
 *   Krzysztof KosiƄski <tweenk.pl@gmail.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "gmemoryoutputstream.h"
Packit ae235b
#include "goutputstream.h"
Packit ae235b
#include "gpollableoutputstream.h"
Packit ae235b
#include "gseekable.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "string.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gmemoryoutputstream
Packit ae235b
 * @short_description: Streaming output operations on memory chunks
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GMemoryInputStream
Packit ae235b
 *
Packit ae235b
 * #GMemoryOutputStream is a class for using arbitrary
Packit ae235b
 * memory chunks as output for GIO streaming output operations.
Packit ae235b
 *
Packit ae235b
 * As of GLib 2.34, #GMemoryOutputStream trivially implements
Packit ae235b
 * #GPollableOutputStream: it always polls as ready.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#define MIN_ARRAY_SIZE  16
Packit ae235b
Packit ae235b
enum {
Packit ae235b
  PROP_0,
Packit ae235b
  PROP_DATA,
Packit ae235b
  PROP_SIZE,
Packit ae235b
  PROP_DATA_SIZE,
Packit ae235b
  PROP_REALLOC_FUNCTION,
Packit ae235b
  PROP_DESTROY_FUNCTION
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GMemoryOutputStreamPrivate
Packit ae235b
{
Packit ae235b
  gpointer       data; /* Write buffer */
Packit ae235b
  gsize          len; /* Current length of the data buffer. Can change with resizing. */
Packit ae235b
  gsize          valid_len; /* The part of data that has been written to */
Packit ae235b
  gsize          pos; /* Current position in the stream. Distinct from valid_len,
Packit ae235b
                         because the stream is seekable. */
Packit ae235b
Packit ae235b
  GReallocFunc   realloc_fn;
Packit ae235b
  GDestroyNotify destroy;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void     g_memory_output_stream_set_property (GObject      *object,
Packit ae235b
                                                     guint         prop_id,
Packit ae235b
                                                     const GValue *value,
Packit ae235b
                                                     GParamSpec   *pspec);
Packit ae235b
static void     g_memory_output_stream_get_property (GObject      *object,
Packit ae235b
                                                     guint         prop_id,
Packit ae235b
                                                     GValue       *value,
Packit ae235b
                                                     GParamSpec   *pspec);
Packit ae235b
static void     g_memory_output_stream_finalize     (GObject      *object);
Packit ae235b
Packit ae235b
static gssize   g_memory_output_stream_write       (GOutputStream *stream,
Packit ae235b
                                                    const void    *buffer,
Packit ae235b
                                                    gsize          count,
Packit ae235b
                                                    GCancellable  *cancellable,
Packit ae235b
                                                    GError       **error);
Packit ae235b
Packit ae235b
static gboolean g_memory_output_stream_close       (GOutputStream  *stream,
Packit ae235b
                                                    GCancellable   *cancellable,
Packit ae235b
                                                    GError        **error);
Packit ae235b
Packit ae235b
static void     g_memory_output_stream_close_async  (GOutputStream        *stream,
Packit ae235b
                                                     int                   io_priority,
Packit ae235b
                                                     GCancellable         *cancellable,
Packit ae235b
                                                     GAsyncReadyCallback   callback,
Packit ae235b
                                                     gpointer              data);
Packit ae235b
static gboolean g_memory_output_stream_close_finish (GOutputStream        *stream,
Packit ae235b
                                                     GAsyncResult         *result,
Packit ae235b
                                                     GError              **error);
Packit ae235b
Packit ae235b
static void     g_memory_output_stream_seekable_iface_init (GSeekableIface  *iface);
Packit ae235b
static goffset  g_memory_output_stream_tell                (GSeekable       *seekable);
Packit ae235b
static gboolean g_memory_output_stream_can_seek            (GSeekable       *seekable);
Packit ae235b
static gboolean g_memory_output_stream_seek                (GSeekable       *seekable,
Packit ae235b
                                                           goffset          offset,
Packit ae235b
                                                           GSeekType        type,
Packit ae235b
                                                           GCancellable    *cancellable,
Packit ae235b
                                                           GError         **error);
Packit ae235b
static gboolean g_memory_output_stream_can_truncate        (GSeekable       *seekable);
Packit ae235b
static gboolean g_memory_output_stream_truncate            (GSeekable       *seekable,
Packit ae235b
                                                           goffset          offset,
Packit ae235b
                                                           GCancellable    *cancellable,
Packit ae235b
                                                           GError         **error);
Packit ae235b
Packit ae235b
static gboolean g_memory_output_stream_is_writable       (GPollableOutputStream *stream);
Packit ae235b
static GSource *g_memory_output_stream_create_source     (GPollableOutputStream *stream,
Packit ae235b
                                                          GCancellable          *cancellable);
Packit ae235b
Packit ae235b
static void g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
Packit ae235b
                         G_ADD_PRIVATE (GMemoryOutputStream)
Packit ae235b
                         G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
Packit ae235b
                                                g_memory_output_stream_seekable_iface_init);
Packit ae235b
                         G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
Packit ae235b
                                                g_memory_output_stream_pollable_iface_init))
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
Packit ae235b
{
Packit ae235b
  GOutputStreamClass *ostream_class;
Packit ae235b
  GObjectClass *gobject_class;
Packit ae235b
Packit ae235b
  gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
  gobject_class->set_property = g_memory_output_stream_set_property;
Packit ae235b
  gobject_class->get_property = g_memory_output_stream_get_property;
Packit ae235b
  gobject_class->finalize     = g_memory_output_stream_finalize;
Packit ae235b
Packit ae235b
  ostream_class = G_OUTPUT_STREAM_CLASS (klass);
Packit ae235b
Packit ae235b
  ostream_class->write_fn = g_memory_output_stream_write;
Packit ae235b
  ostream_class->close_fn = g_memory_output_stream_close;
Packit ae235b
  ostream_class->close_async  = g_memory_output_stream_close_async;
Packit ae235b
  ostream_class->close_finish = g_memory_output_stream_close_finish;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GMemoryOutputStream:data:
Packit ae235b
   *
Packit ae235b
   * Pointer to buffer where data will be written.
Packit ae235b
   *
Packit ae235b
   * Since: 2.24
Packit ae235b
   **/
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
                                   PROP_DATA,
Packit ae235b
                                   g_param_spec_pointer ("data",
Packit ae235b
                                                         P_("Data Buffer"),
Packit ae235b
                                                         P_("Pointer to buffer where data will be written."),
Packit ae235b
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                         G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GMemoryOutputStream:size:
Packit ae235b
   *
Packit ae235b
   * Current size of the data buffer.
Packit ae235b
   *
Packit ae235b
   * Since: 2.24
Packit ae235b
   **/
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
                                   PROP_SIZE,
Packit ae235b
                                   g_param_spec_ulong ("size",
Packit ae235b
                                                       P_("Data Buffer Size"),
Packit ae235b
                                                       P_("Current size of the data buffer."),
Packit ae235b
                                                       0, G_MAXULONG, 0,
Packit ae235b
                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GMemoryOutputStream:data-size:
Packit ae235b
   *
Packit ae235b
   * Size of data written to the buffer.
Packit ae235b
   *
Packit ae235b
   * Since: 2.24
Packit ae235b
   **/
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
                                   PROP_DATA_SIZE,
Packit ae235b
                                   g_param_spec_ulong ("data-size",
Packit ae235b
                                                       P_("Data Size"),
Packit ae235b
                                                       P_("Size of data written to the buffer."),
Packit ae235b
                                                       0, G_MAXULONG, 0,
Packit ae235b
                                                       G_PARAM_READABLE |
Packit ae235b
                                                       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GMemoryOutputStream:realloc-function: (skip)
Packit ae235b
   *
Packit ae235b
   * Function with realloc semantics called to enlarge the buffer.
Packit ae235b
   *
Packit ae235b
   * Since: 2.24
Packit ae235b
   **/
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
                                   PROP_REALLOC_FUNCTION,
Packit ae235b
                                   g_param_spec_pointer ("realloc-function",
Packit ae235b
                                                         P_("Memory Reallocation Function"),
Packit ae235b
                                                         P_("Function with realloc semantics called to enlarge the buffer."),
Packit ae235b
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                         G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GMemoryOutputStream:destroy-function: (skip)
Packit ae235b
   *
Packit ae235b
   * Function called with the buffer as argument when the stream is destroyed.
Packit ae235b
   *
Packit ae235b
   * Since: 2.24
Packit ae235b
   **/
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
                                   PROP_DESTROY_FUNCTION,
Packit ae235b
                                   g_param_spec_pointer ("destroy-function",
Packit ae235b
                                                         P_("Destroy Notification Function"),
Packit ae235b
                                                         P_("Function called with the buffer as argument when the stream is destroyed."),
Packit ae235b
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
                                                         G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
Packit ae235b
{
Packit ae235b
  iface->is_writable = g_memory_output_stream_is_writable;
Packit ae235b
  iface->create_source = g_memory_output_stream_create_source;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_memory_output_stream_set_property (GObject      *object,
Packit ae235b
                                     guint         prop_id,
Packit ae235b
                                     const GValue *value,
Packit ae235b
                                     GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GMemoryOutputStream        *stream;
Packit ae235b
  GMemoryOutputStreamPrivate *priv;
Packit ae235b
Packit ae235b
  stream = G_MEMORY_OUTPUT_STREAM (object);
Packit ae235b
  priv = stream->priv;
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_DATA:
Packit ae235b
      priv->data = g_value_get_pointer (value);
Packit ae235b
      break;
Packit ae235b
    case PROP_SIZE:
Packit ae235b
      priv->len = g_value_get_ulong (value);
Packit ae235b
      break;
Packit ae235b
    case PROP_REALLOC_FUNCTION:
Packit ae235b
      priv->realloc_fn = g_value_get_pointer (value);
Packit ae235b
      break;
Packit ae235b
    case PROP_DESTROY_FUNCTION:
Packit ae235b
      priv->destroy = g_value_get_pointer (value);
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_memory_output_stream_get_property (GObject      *object,
Packit ae235b
                                     guint         prop_id,
Packit ae235b
                                     GValue       *value,
Packit ae235b
                                     GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GMemoryOutputStream        *stream;
Packit ae235b
  GMemoryOutputStreamPrivate *priv;
Packit ae235b
Packit ae235b
  stream = G_MEMORY_OUTPUT_STREAM (object);
Packit ae235b
  priv = stream->priv;
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_DATA:
Packit ae235b
      g_value_set_pointer (value, priv->data);
Packit ae235b
      break;
Packit ae235b
    case PROP_SIZE:
Packit ae235b
      g_value_set_ulong (value, priv->len);
Packit ae235b
      break;
Packit ae235b
    case PROP_DATA_SIZE:
Packit ae235b
      g_value_set_ulong (value, priv->valid_len);
Packit ae235b
      break;
Packit ae235b
    case PROP_REALLOC_FUNCTION:
Packit ae235b
      g_value_set_pointer (value, priv->realloc_fn);
Packit ae235b
      break;
Packit ae235b
    case PROP_DESTROY_FUNCTION:
Packit ae235b
      g_value_set_pointer (value, priv->destroy);
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_memory_output_stream_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GMemoryOutputStream        *stream;
Packit ae235b
  GMemoryOutputStreamPrivate *priv;
Packit ae235b
Packit ae235b
  stream = G_MEMORY_OUTPUT_STREAM (object);
Packit ae235b
  priv = stream->priv;
Packit ae235b
  
Packit ae235b
  if (priv->destroy)
Packit ae235b
    priv->destroy (priv->data);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
Packit ae235b
{
Packit ae235b
  iface->tell         = g_memory_output_stream_tell;
Packit ae235b
  iface->can_seek     = g_memory_output_stream_can_seek;
Packit ae235b
  iface->seek         = g_memory_output_stream_seek;
Packit ae235b
  iface->can_truncate = g_memory_output_stream_can_truncate;
Packit ae235b
  iface->truncate_fn  = g_memory_output_stream_truncate;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_memory_output_stream_init (GMemoryOutputStream *stream)
Packit ae235b
{
Packit ae235b
  stream->priv = g_memory_output_stream_get_instance_private (stream);
Packit ae235b
  stream->priv->pos = 0;
Packit ae235b
  stream->priv->valid_len = 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_memory_output_stream_new: (skip)
Packit ae235b
 * @data: (nullable): pointer to a chunk of memory to use, or %NULL
Packit ae235b
 * @size: the size of @data
Packit ae235b
 * @realloc_function: (nullable): a function with realloc() semantics (like g_realloc())
Packit ae235b
 *     to be called when @data needs to be grown, or %NULL
Packit ae235b
 * @destroy_function: (nullable): a function to be called on @data when the stream is
Packit ae235b
 *     finalized, or %NULL
Packit ae235b
 *
Packit ae235b
 * Creates a new #GMemoryOutputStream.
Packit ae235b
 *
Packit ae235b
 * In most cases this is not the function you want.  See
Packit ae235b
 * g_memory_output_stream_new_resizable() instead.
Packit ae235b
 *
Packit ae235b
 * If @data is non-%NULL, the stream will use that for its internal storage.
Packit ae235b
 *
Packit ae235b
 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
Packit ae235b
 * storage when necessary and the stream will be considered resizable.
Packit ae235b
 * In that case, the stream will start out being (conceptually) empty.
Packit ae235b
 * @size is used only as a hint for how big @data is.  Specifically,
Packit ae235b
 * seeking to the end of a newly-created stream will seek to zero, not
Packit ae235b
 * @size.  Seeking past the end of the stream and then writing will
Packit ae235b
 * introduce a zero-filled gap.
Packit ae235b
 *
Packit ae235b
 * If @realloc_fn is %NULL then the stream is fixed-sized.  Seeking to
Packit ae235b
 * the end will seek to @size exactly.  Writing past the end will give
Packit ae235b
 * an 'out of space' error.  Attempting to seek past the end will fail.
Packit ae235b
 * Unlike the resizable case, seeking to an offset within the stream and
Packit ae235b
 * writing will preserve the bytes passed in as @data before that point
Packit ae235b
 * and will return them as part of g_memory_output_stream_steal_data().
Packit ae235b
 * If you intend to seek you should probably therefore ensure that @data
Packit ae235b
 * is properly initialised.
Packit ae235b
 *
Packit ae235b
 * It is probably only meaningful to provide @data and @size in the case
Packit ae235b
 * that you want a fixed-sized stream.  Put another way: if @realloc_fn
Packit ae235b
 * is non-%NULL then it makes most sense to give @data as %NULL and
Packit ae235b
 * @size as 0 (allowing #GMemoryOutputStream to do the initial
Packit ae235b
 * allocation for itself).
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 * // a stream that can grow
Packit ae235b
 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
Packit ae235b
 *
Packit ae235b
 * // another stream that can grow
Packit ae235b
 * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
Packit ae235b
 *
Packit ae235b
 * // a fixed-size stream
Packit ae235b
 * data = malloc (200);
Packit ae235b
 * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: A newly created #GMemoryOutputStream object.
Packit ae235b
 **/
Packit ae235b
GOutputStream *
Packit ae235b
g_memory_output_stream_new (gpointer       data,
Packit ae235b
                            gsize          size,
Packit ae235b
                            GReallocFunc   realloc_function,
Packit ae235b
                            GDestroyNotify destroy_function)
Packit ae235b
{
Packit ae235b
  GOutputStream *stream;
Packit ae235b
Packit ae235b
  stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
Packit ae235b
                         "data", data,
Packit ae235b
                         "size", size,
Packit ae235b
                         "realloc-function", realloc_function,
Packit ae235b
                         "destroy-function", destroy_function,
Packit ae235b
                         NULL);
Packit ae235b
Packit ae235b
  return stream;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_memory_output_stream_new_resizable:
Packit ae235b
 *
Packit ae235b
 * Creates a new #GMemoryOutputStream, using g_realloc() and g_free()
Packit ae235b
 * for memory allocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
GOutputStream *
Packit ae235b
g_memory_output_stream_new_resizable (void)
Packit ae235b
{
Packit ae235b
  return g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_memory_output_stream_get_data:
Packit ae235b
 * @ostream: a #GMemoryOutputStream
Packit ae235b
 *
Packit ae235b
 * Gets any loaded data from the @ostream.
Packit ae235b
 *
Packit ae235b
 * Note that the returned pointer may become invalid on the next
Packit ae235b
 * write or truncate operation on the stream.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): pointer to the stream's data, or %NULL if the data
Packit ae235b
 *    has been stolen
Packit ae235b
 **/
Packit ae235b
gpointer
Packit ae235b
g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
Packit ae235b
Packit ae235b
  return ostream->priv->data;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_memory_output_stream_get_size:
Packit ae235b
 * @ostream: a #GMemoryOutputStream
Packit ae235b
 *
Packit ae235b
 * Gets the size of the currently allocated data area (available from
Packit ae235b
 * g_memory_output_stream_get_data()).
Packit ae235b
 *
Packit ae235b
 * You probably don't want to use this function on resizable streams.
Packit ae235b
 * See g_memory_output_stream_get_data_size() instead.  For resizable
Packit ae235b
 * streams the size returned by this function is an implementation
Packit ae235b
 * detail and may be change at any time in response to operations on the
Packit ae235b
 * stream.
Packit ae235b
 *
Packit ae235b
 * If the stream is fixed-sized (ie: no realloc was passed to
Packit ae235b
 * g_memory_output_stream_new()) then this is the maximum size of the
Packit ae235b
 * stream and further writes will return %G_IO_ERROR_NO_SPACE.
Packit ae235b
 *
Packit ae235b
 * In any case, if you want the number of bytes currently written to the
Packit ae235b
 * stream, use g_memory_output_stream_get_data_size().
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes allocated for the data buffer
Packit ae235b
 */
Packit ae235b
gsize
Packit ae235b
g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
Packit ae235b
Packit ae235b
  return ostream->priv->len;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_memory_output_stream_get_data_size:
Packit ae235b
 * @ostream: a #GMemoryOutputStream
Packit ae235b
 *
Packit ae235b
 * Returns the number of bytes from the start up to including the last
Packit ae235b
 * byte written in the stream that has not been truncated away.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of bytes written to the stream
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
gsize
Packit ae235b
g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
Packit ae235b
Packit ae235b
  return ostream->priv->valid_len;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_memory_output_stream_steal_data:
Packit ae235b
 * @ostream: a #GMemoryOutputStream
Packit ae235b
 *
Packit ae235b
 * Gets any loaded data from the @ostream. Ownership of the data
Packit ae235b
 * is transferred to the caller; when no longer needed it must be
Packit ae235b
 * freed using the free function set in @ostream's
Packit ae235b
 * #GMemoryOutputStream:destroy-function property.
Packit ae235b
 *
Packit ae235b
 * @ostream must be closed before calling this function.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): the stream's data, or %NULL if it has previously
Packit ae235b
 *    been stolen
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
gpointer
Packit ae235b
g_memory_output_stream_steal_data (GMemoryOutputStream *ostream)
Packit ae235b
{
Packit ae235b
  gpointer data;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
Packit ae235b
  g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
Packit ae235b
Packit ae235b
  data = ostream->priv->data;
Packit ae235b
  ostream->priv->data = NULL;
Packit ae235b
Packit ae235b
  return data;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_memory_output_stream_steal_as_bytes:
Packit ae235b
 * @ostream: a #GMemoryOutputStream
Packit ae235b
 *
Packit ae235b
 * Returns data from the @ostream as a #GBytes. @ostream must be
Packit ae235b
 * closed before calling this function.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): the stream's data
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 **/
Packit ae235b
GBytes *
Packit ae235b
g_memory_output_stream_steal_as_bytes (GMemoryOutputStream *ostream)
Packit ae235b
{
Packit ae235b
  GBytes *result;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
Packit ae235b
  g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
Packit ae235b
Packit ae235b
  result = g_bytes_new_with_free_func (ostream->priv->data,
Packit ae235b
                                       ostream->priv->valid_len,
Packit ae235b
                                       ostream->priv->destroy,
Packit ae235b
                                       ostream->priv->data);
Packit ae235b
  ostream->priv->data = NULL;
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
array_resize (GMemoryOutputStream  *ostream,
Packit ae235b
              gsize                 size,
Packit ae235b
              gboolean              allow_partial,
Packit ae235b
              GError              **error)
Packit ae235b
{
Packit ae235b
  GMemoryOutputStreamPrivate *priv;
Packit ae235b
  gpointer data;
Packit ae235b
  gsize len;
Packit ae235b
Packit ae235b
  priv = ostream->priv;
Packit ae235b
Packit ae235b
  if (priv->len == size)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (!priv->realloc_fn)
Packit ae235b
    {
Packit ae235b
      if (allow_partial &&
Packit ae235b
          priv->pos < priv->len)
Packit ae235b
        return TRUE; /* Short write */
Packit ae235b
Packit ae235b
      g_set_error_literal (error,
Packit ae235b
                           G_IO_ERROR,
Packit ae235b
                           G_IO_ERROR_NO_SPACE,
Packit ae235b
                           _("Memory output stream not resizable"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  len = priv->len;
Packit ae235b
  data = priv->realloc_fn (priv->data, size);
Packit ae235b
Packit ae235b
  if (size > 0 && !data)
Packit ae235b
    {
Packit ae235b
      if (allow_partial &&
Packit ae235b
          priv->pos < priv->len)
Packit ae235b
        return TRUE; /* Short write */
Packit ae235b
Packit ae235b
      g_set_error_literal (error,
Packit ae235b
                           G_IO_ERROR,
Packit ae235b
                           G_IO_ERROR_NO_SPACE,
Packit ae235b
                           _("Failed to resize memory output stream"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (size > len)
Packit ae235b
    memset ((guint8 *)data + len, 0, size - len);
Packit ae235b
Packit ae235b
  priv->data = data;
Packit ae235b
  priv->len = size;
Packit ae235b
Packit ae235b
  if (priv->len < priv->valid_len)
Packit ae235b
    priv->valid_len = priv->len;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gsize
Packit ae235b
g_nearest_pow (gsize num)
Packit ae235b
{
Packit ae235b
  gsize n = 1;
Packit ae235b
Packit ae235b
  while (n < num && n > 0)
Packit ae235b
    n <<= 1;
Packit ae235b
Packit ae235b
  return n;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_memory_output_stream_write (GOutputStream  *stream,
Packit ae235b
                              const void     *buffer,
Packit ae235b
                              gsize           count,
Packit ae235b
                              GCancellable   *cancellable,
Packit ae235b
                              GError        **error)
Packit ae235b
{
Packit ae235b
  GMemoryOutputStream        *ostream;
Packit ae235b
  GMemoryOutputStreamPrivate *priv;
Packit ae235b
  guint8   *dest;
Packit ae235b
  gsize new_size;
Packit ae235b
Packit ae235b
  ostream = G_MEMORY_OUTPUT_STREAM (stream);
Packit ae235b
  priv = ostream->priv;
Packit ae235b
Packit ae235b
  if (count == 0)
Packit ae235b
    return 0;
Packit ae235b
Packit ae235b
  /* Check for address space overflow, but only if the buffer is resizable.
Packit ae235b
     Otherwise we just do a short write and don't worry. */
Packit ae235b
  if (priv->realloc_fn && priv->pos + count < priv->pos)
Packit ae235b
    goto overflow;
Packit ae235b
Packit ae235b
  if (priv->pos + count > priv->len)
Packit ae235b
    {
Packit ae235b
      /* At least enough to fit the write, rounded up for greater than
Packit ae235b
       * linear growth.
Packit ae235b
       *
Packit ae235b
       * Assuming that we're using something like realloc(), the kernel
Packit ae235b
       * will overcommit memory to us, so doubling the size each time
Packit ae235b
       * will keep the number of realloc calls low without wasting too
Packit ae235b
       * much memory.
Packit ae235b
       */
Packit ae235b
      new_size = g_nearest_pow (priv->pos + count);
Packit ae235b
      /* Check for overflow again. We have checked if
Packit ae235b
         pos + count > G_MAXSIZE, but now check if g_nearest_pow () has
Packit ae235b
         overflowed */
Packit ae235b
      if (new_size == 0)
Packit ae235b
        goto overflow;
Packit ae235b
Packit ae235b
      new_size = MAX (new_size, MIN_ARRAY_SIZE);
Packit ae235b
      if (!array_resize (ostream, new_size, TRUE, error))
Packit ae235b
        return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Make sure we handle short writes if the array_resize
Packit ae235b
     only added part of the required memory */
Packit ae235b
  count = MIN (count, priv->len - priv->pos);
Packit ae235b
Packit ae235b
  dest = (guint8 *)priv->data + priv->pos;
Packit ae235b
  memcpy (dest, buffer, count);
Packit ae235b
  priv->pos += count;
Packit ae235b
Packit ae235b
  if (priv->pos > priv->valid_len)
Packit ae235b
    priv->valid_len = priv->pos;
Packit ae235b
Packit ae235b
  return count;
Packit ae235b
Packit ae235b
 overflow:
Packit ae235b
  /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
Packit ae235b
  g_set_error_literal (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_NO_SPACE,
Packit ae235b
                       _("Amount of memory required to process the write is "
Packit ae235b
                         "larger than available address space"));
Packit ae235b
  return -1;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_memory_output_stream_close (GOutputStream  *stream,
Packit ae235b
                              GCancellable   *cancellable,
Packit ae235b
                              GError        **error)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_memory_output_stream_close_async (GOutputStream       *stream,
Packit ae235b
                                    int                  io_priority,
Packit ae235b
                                    GCancellable        *cancellable,
Packit ae235b
                                    GAsyncReadyCallback  callback,
Packit ae235b
                                    gpointer             data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  task = g_task_new (stream, cancellable, callback, data);
Packit ae235b
  g_task_set_source_tag (task, g_memory_output_stream_close_async);
Packit ae235b
Packit ae235b
  /* will always return TRUE */
Packit ae235b
  g_memory_output_stream_close (stream, cancellable, NULL);
Packit ae235b
Packit ae235b
  g_task_return_boolean (task, TRUE);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_memory_output_stream_close_finish (GOutputStream  *stream,
Packit ae235b
                                     GAsyncResult   *result,
Packit ae235b
                                     GError        **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static goffset
Packit ae235b
g_memory_output_stream_tell (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  GMemoryOutputStream *stream;
Packit ae235b
  GMemoryOutputStreamPrivate *priv;
Packit ae235b
Packit ae235b
  stream = G_MEMORY_OUTPUT_STREAM (seekable);
Packit ae235b
  priv = stream->priv;
Packit ae235b
Packit ae235b
  return priv->pos;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_memory_output_stream_can_seek (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_memory_output_stream_seek (GSeekable    *seekable,
Packit ae235b
                             goffset        offset,
Packit ae235b
                             GSeekType      type,
Packit ae235b
                             GCancellable  *cancellable,
Packit ae235b
                             GError       **error)
Packit ae235b
{
Packit ae235b
  GMemoryOutputStream        *stream;
Packit ae235b
  GMemoryOutputStreamPrivate *priv;
Packit ae235b
  goffset absolute;
Packit ae235b
Packit ae235b
  stream = G_MEMORY_OUTPUT_STREAM (seekable);
Packit ae235b
  priv = stream->priv;
Packit ae235b
Packit ae235b
  switch (type)
Packit ae235b
    {
Packit ae235b
    case G_SEEK_CUR:
Packit ae235b
      absolute = priv->pos + offset;
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_SEEK_SET:
Packit ae235b
      absolute = offset;
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_SEEK_END:
Packit ae235b
      /* For resizable streams, we consider the end to be the data
Packit ae235b
       * length.  For fixed-sized streams, we consider the end to be the
Packit ae235b
       * size of the buffer.
Packit ae235b
       */
Packit ae235b
      if (priv->realloc_fn)
Packit ae235b
        absolute = priv->valid_len + offset;
Packit ae235b
      else
Packit ae235b
        absolute = priv->len + offset;
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_set_error_literal (error,
Packit ae235b
                           G_IO_ERROR,
Packit ae235b
                           G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Invalid GSeekType supplied"));
Packit ae235b
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (absolute < 0)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error,
Packit ae235b
                           G_IO_ERROR,
Packit ae235b
                           G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Requested seek before the beginning of the stream"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Can't seek past the end of a fixed-size stream.
Packit ae235b
   *
Packit ae235b
   * Note: seeking to the non-existent byte at the end of a fixed-sized
Packit ae235b
   * stream is valid (eg: a 1-byte fixed sized stream can have position
Packit ae235b
   * 0 or 1).  Therefore '>' is what we want.
Packit ae235b
   * */
Packit ae235b
  if (priv->realloc_fn == NULL && absolute > priv->len)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error,
Packit ae235b
                           G_IO_ERROR,
Packit ae235b
                           G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Requested seek beyond the end of the stream"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  priv->pos = absolute;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_memory_output_stream_can_truncate (GSeekable *seekable)
Packit ae235b
{
Packit ae235b
  GMemoryOutputStream *ostream;
Packit ae235b
  GMemoryOutputStreamPrivate *priv;
Packit ae235b
Packit ae235b
  ostream = G_MEMORY_OUTPUT_STREAM (seekable);
Packit ae235b
  priv = ostream->priv;
Packit ae235b
Packit ae235b
  /* We do not allow truncation of fixed-sized streams */
Packit ae235b
  return priv->realloc_fn != NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_memory_output_stream_truncate (GSeekable     *seekable,
Packit ae235b
                                 goffset        offset,
Packit ae235b
                                 GCancellable  *cancellable,
Packit ae235b
                                 GError       **error)
Packit ae235b
{
Packit ae235b
  GMemoryOutputStream *ostream = G_MEMORY_OUTPUT_STREAM (seekable);
Packit ae235b
Packit ae235b
  if (!array_resize (ostream, offset, FALSE, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  ostream->priv->valid_len = offset;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_memory_output_stream_is_writable (GPollableOutputStream *stream)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GSource *
Packit ae235b
g_memory_output_stream_create_source (GPollableOutputStream *stream,
Packit ae235b
                                      GCancellable          *cancellable)
Packit ae235b
{
Packit ae235b
  GSource *base_source, *pollable_source;
Packit ae235b
Packit ae235b
  base_source = g_timeout_source_new (0);
Packit ae235b
  pollable_source = g_pollable_source_new_full (stream, base_source, cancellable);
Packit ae235b
  g_source_unref (base_source);
Packit ae235b
Packit ae235b
  return pollable_source;
Packit ae235b
}