#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
-#ifdef HAVE_SYS_MKDEV_H
+
+#if MAJOR_IN_MKDEV
#include <sys/mkdev.h>
-#endif
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+#elif MAJOR_IN_SYSMACROS
+#include <sys/sysmacros.h>
#endif
#include "gdbusutils.h"
#include "glibintl.h"
+typedef struct _GMemoryBuffer GMemoryBuffer;
+struct _GMemoryBuffer
+{
+ gsize len;
+ gsize valid_len;
+ gsize pos;
+ gchar *data;
+ GDataStreamByteOrder byte_order;
+};
+
+static guchar
+g_memory_buffer_read_byte (GMemoryBuffer *mbuf,
+ GError **error)
+{
+ if (mbuf->pos >= mbuf->valid_len)
+ return 0;
+ return mbuf->data [mbuf->pos++];
+}
+
+static gint16
+g_memory_buffer_read_int16 (GMemoryBuffer *mbuf,
+ GError **error)
+{
+ gint16 v;
+
+ if (mbuf->pos > mbuf->valid_len - 2)
+ {
+ mbuf->pos = mbuf->valid_len;
+ return 0;
+ }
+
+ memcpy (&v, mbuf->data + mbuf->pos, 2);
+ mbuf->pos += 2;
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ v = GINT16_FROM_BE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ v = GINT16_FROM_LE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+ return v;
+}
+
+static guint16
+g_memory_buffer_read_uint16 (GMemoryBuffer *mbuf,
+ GError **error)
+{
+ guint16 v;
+
+ if (mbuf->pos > mbuf->valid_len - 2)
+ {
+ mbuf->pos = mbuf->valid_len;
+ return 0;
+ }
+
+ memcpy (&v, mbuf->data + mbuf->pos, 2);
+ mbuf->pos += 2;
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ v = GINT16_FROM_BE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ v = GINT16_FROM_LE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+ return v;
+}
+
+static gint32
+g_memory_buffer_read_int32 (GMemoryBuffer *mbuf,
+ GError **error)
+{
+ gint32 v;
+
+ if (mbuf->pos > mbuf->valid_len - 4)
+ {
+ mbuf->pos = mbuf->valid_len;
+ return 0;
+ }
+
+ memcpy (&v, mbuf->data + mbuf->pos, 4);
+ mbuf->pos += 4;
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ v = GINT32_FROM_BE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ v = GINT32_FROM_LE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+ return v;
+}
+
+static guint32
+g_memory_buffer_read_uint32 (GMemoryBuffer *mbuf,
+ GError **error)
+{
+ guint32 v;
+
+ if (mbuf->pos > mbuf->valid_len - 4)
+ {
+ mbuf->pos = mbuf->valid_len;
+ return 0;
+ }
+
+ memcpy (&v, mbuf->data + mbuf->pos, 4);
+ mbuf->pos += 4;
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ v = GUINT32_FROM_BE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ v = GUINT32_FROM_LE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+ return v;
+}
+
+static gint64
+g_memory_buffer_read_int64 (GMemoryBuffer *mbuf,
+ GError **error)
+{
+ gint64 v;
+
+ if (mbuf->pos > mbuf->valid_len - 8)
+ {
+ mbuf->pos = mbuf->valid_len;
+ return 0;
+ }
+
+ memcpy (&v, mbuf->data + mbuf->pos, 8);
+ mbuf->pos += 8;
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ v = GINT64_FROM_BE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ v = GINT64_FROM_LE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+ return v;
+}
+
+static guint64
+g_memory_buffer_read_uint64 (GMemoryBuffer *mbuf,
+ GError **error)
+{
+ guint64 v;
+
+ if (mbuf->pos > mbuf->valid_len - 8)
+ {
+ mbuf->pos = mbuf->valid_len;
+ return 0;
+ }
+
+ memcpy (&v, mbuf->data + mbuf->pos, 8);
+ mbuf->pos += 8;
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ v = GUINT64_FROM_BE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ v = GUINT64_FROM_LE (v);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+ return v;
+}
+
+#define MIN_ARRAY_SIZE 128
+
+static gint
+g_nearest_pow (gint num)
+{
+ gint n = 1;
+
+ while (n < num)
+ n <<= 1;
+
+ return n;
+}
+
+static void
+array_resize (GMemoryBuffer *mbuf,
+ gsize size)
+{
+ gpointer data;
+ gsize len;
+
+ if (mbuf->len == size)
+ return;
+
+ len = mbuf->len;
+ data = g_realloc (mbuf->data, size);
+
+ if (size > len)
+ memset ((guint8 *)data + len, 0, size - len);
+
+ mbuf->data = data;
+ mbuf->len = size;
+
+ if (mbuf->len < mbuf->valid_len)
+ mbuf->valid_len = mbuf->len;
+}
+
+static gboolean
+g_memory_buffer_write (GMemoryBuffer *mbuf,
+ const void *buffer,
+ gsize count)
+{
+ guint8 *dest;
+ gsize new_size;
+
+ if (count == 0)
+ return TRUE;
+
+ /* Check for address space overflow, but only if the buffer is resizable.
+ Otherwise we just do a short write and don't worry. */
+ if (mbuf->pos + count < mbuf->pos)
+ return FALSE;
+
+ if (mbuf->pos + count > mbuf->len)
+ {
+ /* At least enought to fit the write, rounded up
+ for greater than linear growth.
+ TODO: This wastes a lot of memory at large buffer sizes.
+ Figure out a more rational allocation strategy. */
+ new_size = g_nearest_pow (mbuf->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 < mbuf->len)
+ return FALSE;
+
+ new_size = MAX (new_size, MIN_ARRAY_SIZE);
+ array_resize (mbuf, new_size);
+ }
+
+ dest = (guint8 *)mbuf->data + mbuf->pos;
+ memcpy (dest, buffer, count);
+ mbuf->pos += count;
+
+ if (mbuf->pos > mbuf->valid_len)
+ mbuf->valid_len = mbuf->pos;
+
+ return TRUE;
+}
+
+static gboolean
+g_memory_buffer_put_byte (GMemoryBuffer *mbuf,
+ guchar data)
+{
+ return g_memory_buffer_write (mbuf, &data, 1);
+}
+
+static gboolean
+g_memory_buffer_put_int16 (GMemoryBuffer *mbuf,
+ gint16 data)
+{
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ data = GINT16_TO_BE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ data = GINT16_TO_LE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+
+ return g_memory_buffer_write (mbuf, &data, 2);
+}
+
+static gboolean
+g_memory_buffer_put_uint16 (GMemoryBuffer *mbuf,
+ guint16 data)
+{
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ data = GUINT16_TO_BE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ data = GUINT16_TO_LE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+
+ return g_memory_buffer_write (mbuf, &data, 2);
+}
+
+static gboolean
+g_memory_buffer_put_int32 (GMemoryBuffer *mbuf,
+ gint32 data)
+{
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ data = GINT32_TO_BE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ data = GINT32_TO_LE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+
+ return g_memory_buffer_write (mbuf, &data, 4);
+}
+
+static gboolean
+g_memory_buffer_put_uint32 (GMemoryBuffer *mbuf,
+ guint32 data)
+{
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ data = GUINT32_TO_BE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ data = GUINT32_TO_LE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+
+ return g_memory_buffer_write (mbuf, &data, 4);
+}
+
+static gboolean
+g_memory_buffer_put_int64 (GMemoryBuffer *mbuf,
+ gint64 data)
+{
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ data = GINT64_TO_BE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ data = GINT64_TO_LE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+
+ return g_memory_buffer_write (mbuf, &data, 8);
+}
+
+static gboolean
+g_memory_buffer_put_uint64 (GMemoryBuffer *mbuf,
+ guint64 data)
+{
+ switch (mbuf->byte_order)
+ {
+ case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
+ data = GUINT64_TO_BE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
+ data = GUINT64_TO_LE (data);
+ break;
+ case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
+ default:
+ break;
+ }
+
+ return g_memory_buffer_write (mbuf, &data, 8);
+}
+
+static gboolean
+g_memory_buffer_put_string (GMemoryBuffer *mbuf,
+ const char *str)
+{
+ g_return_val_if_fail (str != NULL, FALSE);
+
+ return g_memory_buffer_write (mbuf, str, strlen (str));
+}
+
+
/**
* SECTION:gdbusmessage
* @short_description: D-Bus Message
/**
* g_dbus_message_new_method_call:
- * @name: A valid D-Bus name or %NULL.
+ * @name: (allow-none): A valid D-Bus name or %NULL.
* @path: A valid object path.
- * @interface_: A valid D-Bus interface name or %NULL.
+ * @interface_: (allow-none): A valid D-Bus interface name or %NULL.
* @method: A valid method name.
*
* Creates a new #GDBusMessage for a method call.
*
* Since: 2.26
*/
+G_GNUC_PRINTF(3, 0)
GDBusMessage *
g_dbus_message_new_method_error_valist (GDBusMessage *method_call_message,
const gchar *error_name,
* g_dbus_message_set_header:
* @message: A #GDBusMessage.
* @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
- * @value: A #GVariant to set the header field or %NULL to clear the header field.
+ * @value: (allow-none): A #GVariant to set the header field or %NULL to clear the header field.
*
* Sets a header field on @message.
*
*
* Gets an array of all header fields on @message that are set.
*
- * Returns: An array of header fields terminated by
- * %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element is a
- * #guchar. Free with g_free().
+ * Returns: (array zero-terminated=1): An array of header fields
+ * terminated by %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element
+ * is a #guchar. Free with g_free().
*
* Since: 2.26
*/
/* ---------------------------------------------------------------------------------------------------- */
static gboolean
-ensure_input_padding (GMemoryInputStream *mis,
+ensure_input_padding (GMemoryBuffer *buf,
gsize padding_size,
GError **error)
{
gsize offset;
gsize wanted_offset;
- offset = g_seekable_tell (G_SEEKABLE (mis));
+ offset = buf->pos;
wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
-
- if (offset != wanted_offset)
- {
- return g_seekable_seek (G_SEEKABLE (mis), wanted_offset, G_SEEK_SET, NULL, error);
- }
- else
- {
- return TRUE;
- }
+ buf->pos = wanted_offset;
+ return TRUE;
}
-static gchar *
-read_string (GMemoryInputStream *mis,
- GDataInputStream *dis,
+static const gchar *
+read_string (GMemoryBuffer *mbuf,
gsize len,
GError **error)
{
- GString *s;
- gchar buf[256];
- gsize remaining;
- guchar nul;
- GError *local_error;
+ gchar *str;
const gchar *end_valid;
- s = g_string_new (NULL);
-
- remaining = len;
- while (remaining > 0)
+ if (mbuf->pos + len >= mbuf->valid_len || mbuf->pos + len < mbuf->pos)
{
- gsize to_read;
- gssize num_read;
-
- to_read = MIN (remaining, sizeof (buf));
- num_read = g_input_stream_read (G_INPUT_STREAM (mis),
- buf,
- to_read,
- NULL,
- error);
- if (num_read < 0)
- goto fail;
- if (num_read == 0)
- {
- /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
- g_set_error (error,
- G_IO_ERROR,
- G_IO_ERROR_INVALID_ARGUMENT,
- _("Wanted to read %lu bytes but got EOF"),
- (gulong)to_read);
- goto fail;
- }
-
- remaining -= num_read;
- g_string_append_len (s, buf, num_read);
+ mbuf->pos = mbuf->valid_len;
+ /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ g_dngettext (GETTEXT_PACKAGE,
+ "Wanted to read %lu byte but only got %lu",
+ "Wanted to read %lu bytes but only got %lu",
+ (gulong)len),
+ (gulong)len,
+ (gulong)(mbuf->valid_len - mbuf->pos));
+ mbuf->pos = mbuf->valid_len;
+ return NULL;
}
- local_error = NULL;
- nul = g_data_input_stream_read_byte (dis, NULL, &local_error);
- if (local_error != NULL)
+ if (mbuf->data[mbuf->pos + len] != '\0')
{
- g_propagate_error (error, local_error);
- goto fail;
+ str = g_strndup (mbuf->data + mbuf->pos, len);
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ _("Expected NUL byte after the string '%s' but found byte %d"),
+ str, mbuf->data[mbuf->pos + len]);
+ g_free (str);
+ mbuf->pos += len + 1;
+ return NULL;
}
- if (!g_utf8_validate (s->str, -1, &end_valid))
+
+ str = mbuf->data + mbuf->pos;
+ mbuf->pos += len + 1;
+
+ if (!g_utf8_validate (str, -1, &end_valid))
{
gint offset;
gchar *valid_str;
- offset = (gint) (end_valid - s->str);
- valid_str = g_strndup (s->str, offset);
+ offset = (gint) (end_valid - str);
+ valid_str = g_strndup (str, offset);
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
_("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). "
- "The valid UTF-8 string up until that point was `%s'"),
+ "The valid UTF-8 string up until that point was '%s'"),
offset,
- (gint) s->len,
+ (gint) len,
valid_str);
g_free (valid_str);
- goto fail;
- }
- if (nul != '\0')
- {
- g_set_error (error,
- G_IO_ERROR,
- G_IO_ERROR_INVALID_ARGUMENT,
- _("Expected NUL byte after the string `%s' but found byte %d"),
- s->str, nul);
- goto fail;
+ return NULL;
}
- return g_string_free (s, FALSE);
-
- fail:
- g_string_free (s, TRUE);
- return NULL;
+ return str;
}
/* if just_align==TRUE, don't read a value, just align the input stream wrt padding */
/* returns a non-floating GVariant! */
static GVariant *
-parse_value_from_blob (GMemoryInputStream *mis,
- GDataInputStream *dis,
+parse_value_from_blob (GMemoryBuffer *buf,
const GVariantType *type,
gboolean just_align,
guint indent,
type_string = g_variant_type_peek_string (type);
#ifdef DEBUG_SERIALIZER
- if (!just_align)
{
gchar *s;
s = g_variant_type_dup_string (type);
- g_print ("%*sReading type %s from offset 0x%04x",
+ g_print ("%*s%s type %s from offset 0x%04x",
indent, "",
+ just_align ? "Aligning" : "Reading",
s,
- (gint) g_seekable_tell (G_SEEKABLE (mis)));
+ (gint) g_seekable_tell (G_SEEKABLE (buf)));
g_free (s);
}
#endif /* DEBUG_SERIALIZER */
switch (type_string[0])
{
case 'b': /* G_VARIANT_TYPE_BOOLEAN */
- if (!ensure_input_padding (mis, 4, &local_error))
+ if (!ensure_input_padding (buf, 4, &local_error))
goto fail;
if (!just_align)
{
gboolean v;
- v = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
+ v = g_memory_buffer_read_uint32 (buf, &local_error);
if (local_error != NULL)
goto fail;
ret = g_variant_new_boolean (v);
if (!just_align)
{
guchar v;
- v = g_data_input_stream_read_byte (dis, NULL, &local_error);
+ v = g_memory_buffer_read_byte (buf, &local_error);
if (local_error != NULL)
goto fail;
ret = g_variant_new_byte (v);
break;
case 'n': /* G_VARIANT_TYPE_INT16 */
- if (!ensure_input_padding (mis, 2, &local_error))
+ if (!ensure_input_padding (buf, 2, &local_error))
goto fail;
if (!just_align)
{
gint16 v;
- v = g_data_input_stream_read_int16 (dis, NULL, &local_error);
+ v = g_memory_buffer_read_int16 (buf, &local_error);
if (local_error != NULL)
goto fail;
ret = g_variant_new_int16 (v);
break;
case 'q': /* G_VARIANT_TYPE_UINT16 */
- if (!ensure_input_padding (mis, 2, &local_error))
+ if (!ensure_input_padding (buf, 2, &local_error))
goto fail;
if (!just_align)
{
guint16 v;
- v = g_data_input_stream_read_uint16 (dis, NULL, &local_error);
+ v = g_memory_buffer_read_uint16 (buf, &local_error);
if (local_error != NULL)
goto fail;
ret = g_variant_new_uint16 (v);
break;
case 'i': /* G_VARIANT_TYPE_INT32 */
- if (!ensure_input_padding (mis, 4, &local_error))
+ if (!ensure_input_padding (buf, 4, &local_error))
goto fail;
if (!just_align)
{
gint32 v;
- v = g_data_input_stream_read_int32 (dis, NULL, &local_error);
+ v = g_memory_buffer_read_int32 (buf, &local_error);
if (local_error != NULL)
goto fail;
ret = g_variant_new_int32 (v);
break;
case 'u': /* G_VARIANT_TYPE_UINT32 */
- if (!ensure_input_padding (mis, 4, &local_error))
+ if (!ensure_input_padding (buf, 4, &local_error))
goto fail;
if (!just_align)
{
guint32 v;
- v = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
+ v = g_memory_buffer_read_uint32 (buf, &local_error);
if (local_error != NULL)
goto fail;
ret = g_variant_new_uint32 (v);
break;
case 'x': /* G_VARIANT_TYPE_INT64 */
- if (!ensure_input_padding (mis, 8, &local_error))
+ if (!ensure_input_padding (buf, 8, &local_error))
goto fail;
if (!just_align)
{
gint64 v;
- v = g_data_input_stream_read_int64 (dis, NULL, &local_error);
+ v = g_memory_buffer_read_int64 (buf, &local_error);
if (local_error != NULL)
goto fail;
ret = g_variant_new_int64 (v);
break;
case 't': /* G_VARIANT_TYPE_UINT64 */
- if (!ensure_input_padding (mis, 8, &local_error))
+ if (!ensure_input_padding (buf, 8, &local_error))
goto fail;
if (!just_align)
{
guint64 v;
- v = g_data_input_stream_read_uint64 (dis, NULL, &local_error);
+ v = g_memory_buffer_read_uint64 (buf, &local_error);
if (local_error != NULL)
goto fail;
ret = g_variant_new_uint64 (v);
break;
case 'd': /* G_VARIANT_TYPE_DOUBLE */
- if (!ensure_input_padding (mis, 8, &local_error))
+ if (!ensure_input_padding (buf, 8, &local_error))
goto fail;
if (!just_align)
{
gdouble v_double;
} u;
G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
- u.v_uint64 = g_data_input_stream_read_uint64 (dis, NULL, &local_error);
+ u.v_uint64 = g_memory_buffer_read_uint64 (buf, &local_error);
if (local_error != NULL)
goto fail;
ret = g_variant_new_double (u.v_double);
break;
case 's': /* G_VARIANT_TYPE_STRING */
- if (!ensure_input_padding (mis, 4, &local_error))
+ if (!ensure_input_padding (buf, 4, &local_error))
goto fail;
if (!just_align)
{
guint32 len;
- gchar *v;
- len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
+ const gchar *v;
+ len = g_memory_buffer_read_uint32 (buf, &local_error);
if (local_error != NULL)
goto fail;
- v = read_string (mis, dis, (gsize) len, &local_error);
+ v = read_string (buf, (gsize) len, &local_error);
if (v == NULL)
goto fail;
ret = g_variant_new_string (v);
- g_free (v);
}
break;
case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
- if (!ensure_input_padding (mis, 4, &local_error))
+ if (!ensure_input_padding (buf, 4, &local_error))
goto fail;
if (!just_align)
{
guint32 len;
- gchar *v;
- len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
+ const gchar *v;
+ len = g_memory_buffer_read_uint32 (buf, &local_error);
if (local_error != NULL)
goto fail;
- v = read_string (mis, dis, (gsize) len, &local_error);
+ v = read_string (buf, (gsize) len, &local_error);
if (v == NULL)
goto fail;
if (!g_variant_is_object_path (v))
g_set_error (&local_error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Parsed value `%s' is not a valid D-Bus object path"),
+ _("Parsed value '%s' is not a valid D-Bus object path"),
v);
- g_free (v);
goto fail;
}
ret = g_variant_new_object_path (v);
- g_free (v);
}
break;
if (!just_align)
{
guchar len;
- gchar *v;
- len = g_data_input_stream_read_byte (dis, NULL, &local_error);
+ const gchar *v;
+ len = g_memory_buffer_read_byte (buf, &local_error);
if (local_error != NULL)
goto fail;
- v = read_string (mis, dis, (gsize) len, &local_error);
+ v = read_string (buf, (gsize) len, &local_error);
if (v == NULL)
goto fail;
if (!g_variant_is_signature (v))
g_set_error (&local_error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Parsed value `%s' is not a valid D-Bus signature"),
+ _("Parsed value '%s' is not a valid D-Bus signature"),
v);
- g_free (v);
goto fail;
}
ret = g_variant_new_signature (v);
- g_free (v);
}
break;
case 'h': /* G_VARIANT_TYPE_HANDLE */
- if (!ensure_input_padding (mis, 4, &local_error))
+ if (!ensure_input_padding (buf, 4, &local_error))
goto fail;
if (!just_align)
{
gint32 v;
- v = g_data_input_stream_read_int32 (dis, NULL, &local_error);
+ v = g_memory_buffer_read_int32 (buf, &local_error);
if (local_error != NULL)
goto fail;
ret = g_variant_new_handle (v);
break;
case 'a': /* G_VARIANT_TYPE_ARRAY */
- {
- guint32 array_len;
- goffset offset;
- goffset target;
- const GVariantType *element_type;
- GVariantBuilder builder;
+ if (!ensure_input_padding (buf, 4, &local_error))
+ goto fail;
- if (!ensure_input_padding (mis, 4, &local_error))
- goto fail;
+ /* If we are only aligning for this array type, it is the child type of
+ * another array, which is empty. So, we do not need to add padding for
+ * this nonexistent array's elements: we only need to align for this
+ * array itself (4 bytes). See
+ * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>.
+ */
+ if (!just_align)
+ {
+ guint32 array_len;
+ goffset offset;
+ goffset target;
+ const GVariantType *element_type;
+ GVariantBuilder builder;
- if (just_align)
- {
- array_len = 0;
- }
- else
- {
- array_len = g_data_input_stream_read_uint32 (dis, NULL, &local_error);
- if (local_error != NULL)
- goto fail;
+ array_len = g_memory_buffer_read_uint32 (buf, &local_error);
+ if (local_error != NULL)
+ goto fail;
- is_leaf = FALSE;
+ is_leaf = FALSE;
#ifdef DEBUG_SERIALIZER
- g_print (": array spans 0x%04x bytes\n", array_len);
+ g_print (": array spans 0x%04x bytes\n", array_len);
#endif /* DEBUG_SERIALIZER */
- if (array_len > (2<<26))
- {
- /* G_GUINT32_FORMAT doesn't work with gettext, so use u */
- g_set_error (&local_error,
- G_IO_ERROR,
- G_IO_ERROR_INVALID_ARGUMENT,
- _("Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB)."),
- array_len);
- goto fail;
- }
- }
+ if (array_len > (2<<26))
+ {
+ /* G_GUINT32_FORMAT doesn't work with gettext, so use u */
+ g_set_error (&local_error,
+ G_IO_ERROR,
+ G_IO_ERROR_INVALID_ARGUMENT,
+ g_dngettext (GETTEXT_PACKAGE,
+ "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).",
+ "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).",
+ array_len),
+ array_len);
+ goto fail;
+ }
- g_variant_builder_init (&builder, type);
- element_type = g_variant_type_element (type);
+ g_variant_builder_init (&builder, type);
+ element_type = g_variant_type_element (type);
- if (array_len == 0)
- {
- GVariant *item;
- item = parse_value_from_blob (mis,
- dis,
- element_type,
- TRUE,
- indent + 2,
- &local_error);
- g_assert (item == NULL);
- }
- else
- {
- /* TODO: optimize array of primitive types */
- offset = g_seekable_tell (G_SEEKABLE (mis));
- target = offset + array_len;
- while (offset < target)
- {
- GVariant *item;
- item = parse_value_from_blob (mis,
- dis,
- element_type,
- FALSE,
- indent + 2,
- &local_error);
- if (item == NULL)
- {
- g_variant_builder_clear (&builder);
- goto fail;
- }
- g_variant_builder_add_value (&builder, item);
- g_variant_unref (item);
- offset = g_seekable_tell (G_SEEKABLE (mis));
- }
- }
+ if (array_len == 0)
+ {
+ GVariant *item;
+ item = parse_value_from_blob (buf,
+ element_type,
+ TRUE,
+ indent + 2,
+ NULL);
+ g_assert (item == NULL);
+ }
+ else
+ {
+ /* TODO: optimize array of primitive types */
+ offset = buf->pos;
+ target = offset + array_len;
+ while (offset < target)
+ {
+ GVariant *item;
+ item = parse_value_from_blob (buf,
+ element_type,
+ FALSE,
+ indent + 2,
+ &local_error);
+ if (item == NULL)
+ {
+ g_variant_builder_clear (&builder);
+ goto fail;
+ }
+ g_variant_builder_add_value (&builder, item);
+ g_variant_unref (item);
+ offset = buf->pos;
+ }
+ }
- if (!just_align)
- {
- ret = g_variant_builder_end (&builder);
- }
- else
- {
- g_variant_builder_clear (&builder);
- }
- }
+ ret = g_variant_builder_end (&builder);
+ }
break;
default:
GVariant *key;
GVariant *value;
- if (!ensure_input_padding (mis, 8, &local_error))
+ if (!ensure_input_padding (buf, 8, &local_error))
goto fail;
is_leaf = FALSE;
if (!just_align)
{
key_type = g_variant_type_key (type);
- key = parse_value_from_blob (mis,
- dis,
+ key = parse_value_from_blob (buf,
key_type,
FALSE,
indent + 2,
if (key == NULL)
goto fail;
value_type = g_variant_type_value (type);
- value = parse_value_from_blob (mis,
- dis,
+ value = parse_value_from_blob (buf,
value_type,
FALSE,
indent + 2,
}
else if (g_variant_type_is_tuple (type))
{
- if (!ensure_input_padding (mis, 8, &local_error))
+ if (!ensure_input_padding (buf, 8, &local_error))
goto fail;
is_leaf = FALSE;
while (element_type != NULL)
{
GVariant *item;
- item = parse_value_from_blob (mis,
- dis,
+ item = parse_value_from_blob (buf,
element_type,
FALSE,
indent + 2,
if (!just_align)
{
guchar siglen;
- gchar *sig;
+ const gchar *sig;
GVariantType *variant_type;
GVariant *value;
- siglen = g_data_input_stream_read_byte (dis, NULL, &local_error);
+ siglen = g_memory_buffer_read_byte (buf, &local_error);
if (local_error != NULL)
goto fail;
- sig = read_string (mis, dis, (gsize) siglen, &local_error);
+ sig = read_string (buf, (gsize) siglen, &local_error);
if (sig == NULL)
goto fail;
if (!g_variant_is_signature (sig))
g_set_error (&local_error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Parsed value `%s' for variant is not a valid D-Bus signature"),
+ _("Parsed value '%s' for variant is not a valid D-Bus signature"),
sig);
- g_free (sig);
goto fail;
}
variant_type = g_variant_type_new (sig);
- g_free (sig);
- value = parse_value_from_blob (mis,
- dis,
+ value = parse_value_from_blob (buf,
variant_type,
FALSE,
indent + 2,
g_set_error (&local_error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Error deserializing GVariant with type string `%s' from the D-Bus wire format"),
+ _("Error deserializing GVariant with type string '%s' from the D-Bus wire format"),
s);
g_free (s);
goto fail;
/**
* g_dbus_message_bytes_needed:
- * @blob: A blob represent a binary D-Bus message.
+ * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
* @blob_len: The length of @blob (must be at least 16).
* @error: Return location for error or %NULL.
*
/**
* g_dbus_message_new_from_blob:
- * @blob: A blob represent a binary D-Bus message.
+ * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
* @blob_len: The length of @blob.
* @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
* @error: Return location for error or %NULL.
GError **error)
{
gboolean ret;
- GMemoryInputStream *mis;
- GDataInputStream *dis;
+ GMemoryBuffer mbuf;
GDBusMessage *message;
guchar endianness;
guchar major_protocol_version;
- GDataStreamByteOrder byte_order;
guint32 message_body_len;
GVariant *headers;
GVariant *item;
message = g_dbus_message_new ();
- mis = G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (blob, blob_len, NULL));
- dis = g_data_input_stream_new (G_INPUT_STREAM (mis));
+ memset (&mbuf, 0, sizeof (mbuf));
+ mbuf.data = (gchar *)blob;
+ mbuf.len = mbuf.valid_len = blob_len;
- endianness = g_data_input_stream_read_byte (dis, NULL, NULL);
+ endianness = g_memory_buffer_read_byte (&mbuf, NULL);
switch (endianness)
{
case 'l':
- byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
+ mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
break;
case 'B':
- byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
+ mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
break;
default:
endianness);
goto out;
}
- g_data_input_stream_set_byte_order (dis, byte_order);
- message->type = g_data_input_stream_read_byte (dis, NULL, NULL);
- message->flags = g_data_input_stream_read_byte (dis, NULL, NULL);
- major_protocol_version = g_data_input_stream_read_byte (dis, NULL, NULL);
+ message->type = g_memory_buffer_read_byte (&mbuf, NULL);
+ message->flags = g_memory_buffer_read_byte (&mbuf, NULL);
+ major_protocol_version = g_memory_buffer_read_byte (&mbuf, NULL);
if (major_protocol_version != 1)
{
g_set_error (error,
major_protocol_version);
goto out;
}
- message_body_len = g_data_input_stream_read_uint32 (dis, NULL, NULL);
- message->serial = g_data_input_stream_read_uint32 (dis, NULL, NULL);
+ message_body_len = g_memory_buffer_read_uint32 (&mbuf, NULL);
+ message->serial = g_memory_buffer_read_uint32 (&mbuf, NULL);
#ifdef DEBUG_SERIALIZER
g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint) blob_len);
#ifdef DEBUG_SERIALIZER
g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint) blob_len);
#endif /* DEBUG_SERIALIZER */
- headers = parse_value_from_blob (mis,
- dis,
+ headers = parse_value_from_blob (&mbuf,
G_VARIANT_TYPE ("a{yv}"),
FALSE,
2,
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Signature header with signature `%s' found but message body is empty"),
+ _("Signature header with signature '%s' found but message body is empty"),
signature_str);
goto out;
}
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Parsed value `%s' is not a valid D-Bus signature (for body)"),
+ _("Parsed value '%s' is not a valid D-Bus signature (for body)"),
signature_str);
goto out;
}
#ifdef DEBUG_SERIALIZER
g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint) blob_len);
#endif /* DEBUG_SERIALIZER */
- message->body = parse_value_from_blob (mis,
- dis,
+ message->body = parse_value_from_blob (&mbuf,
variant_type,
FALSE,
2,
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("No signature header in message but the message body is %u bytes"),
+ g_dngettext (GETTEXT_PACKAGE,
+ "No signature header in message but the message body is %u byte",
+ "No signature header in message but the message body is %u bytes",
+ message_body_len),
message_body_len);
goto out;
}
ret = TRUE;
out:
- g_object_unref (dis);
- g_object_unref (mis);
-
if (ret)
{
return message;
/* ---------------------------------------------------------------------------------------------------- */
static gsize
-ensure_output_padding (GMemoryOutputStream *mos,
- GDataOutputStream *dos,
+ensure_output_padding (GMemoryBuffer *mbuf,
gsize padding_size)
{
gsize offset;
gsize padding_needed;
guint n;
- offset = g_memory_output_stream_get_data_size (mos);
+ offset = mbuf->pos;
wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
padding_needed = wanted_offset - offset;
for (n = 0; n < padding_needed; n++)
- g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
+ g_memory_buffer_put_byte (mbuf, '\0');
return padding_needed;
}
static gboolean
append_value_to_blob (GVariant *value,
const GVariantType *type,
- GMemoryOutputStream *mos,
- GDataOutputStream *dos,
+ GMemoryBuffer *mbuf,
gsize *out_padding_added,
GError **error)
{
switch (type_string[0])
{
case 'b': /* G_VARIANT_TYPE_BOOLEAN */
- padding_added = ensure_output_padding (mos, dos, 4);
+ padding_added = ensure_output_padding (mbuf, 4);
if (value != NULL)
{
gboolean v = g_variant_get_boolean (value);
- g_data_output_stream_put_uint32 (dos, v, NULL, NULL);
+ g_memory_buffer_put_uint32 (mbuf, v);
}
break;
if (value != NULL)
{
guint8 v = g_variant_get_byte (value);
- g_data_output_stream_put_byte (dos, v, NULL, NULL);
+ g_memory_buffer_put_byte (mbuf, v);
}
break;
case 'n': /* G_VARIANT_TYPE_INT16 */
- padding_added = ensure_output_padding (mos, dos, 2);
+ padding_added = ensure_output_padding (mbuf, 2);
if (value != NULL)
{
gint16 v = g_variant_get_int16 (value);
- g_data_output_stream_put_int16 (dos, v, NULL, NULL);
+ g_memory_buffer_put_int16 (mbuf, v);
}
break;
case 'q': /* G_VARIANT_TYPE_UINT16 */
- padding_added = ensure_output_padding (mos, dos, 2);
+ padding_added = ensure_output_padding (mbuf, 2);
if (value != NULL)
{
guint16 v = g_variant_get_uint16 (value);
- g_data_output_stream_put_uint16 (dos, v, NULL, NULL);
+ g_memory_buffer_put_uint16 (mbuf, v);
}
break;
case 'i': /* G_VARIANT_TYPE_INT32 */
- padding_added = ensure_output_padding (mos, dos, 4);
+ padding_added = ensure_output_padding (mbuf, 4);
if (value != NULL)
{
gint32 v = g_variant_get_int32 (value);
- g_data_output_stream_put_int32 (dos, v, NULL, NULL);
+ g_memory_buffer_put_int32 (mbuf, v);
}
break;
case 'u': /* G_VARIANT_TYPE_UINT32 */
- padding_added = ensure_output_padding (mos, dos, 4);
+ padding_added = ensure_output_padding (mbuf, 4);
if (value != NULL)
{
guint32 v = g_variant_get_uint32 (value);
- g_data_output_stream_put_uint32 (dos, v, NULL, NULL);
+ g_memory_buffer_put_uint32 (mbuf, v);
}
break;
case 'x': /* G_VARIANT_TYPE_INT64 */
- padding_added = ensure_output_padding (mos, dos, 8);
+ padding_added = ensure_output_padding (mbuf, 8);
if (value != NULL)
{
gint64 v = g_variant_get_int64 (value);
- g_data_output_stream_put_int64 (dos, v, NULL, NULL);
+ g_memory_buffer_put_int64 (mbuf, v);
}
break;
case 't': /* G_VARIANT_TYPE_UINT64 */
- padding_added = ensure_output_padding (mos, dos, 8);
+ padding_added = ensure_output_padding (mbuf, 8);
if (value != NULL)
{
guint64 v = g_variant_get_uint64 (value);
- g_data_output_stream_put_uint64 (dos, v, NULL, NULL);
+ g_memory_buffer_put_uint64 (mbuf, v);
}
break;
case 'd': /* G_VARIANT_TYPE_DOUBLE */
- padding_added = ensure_output_padding (mos, dos, 8);
+ padding_added = ensure_output_padding (mbuf, 8);
if (value != NULL)
{
union {
} u;
G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
u.v_double = g_variant_get_double (value);
- g_data_output_stream_put_uint64 (dos, u.v_uint64, NULL, NULL);
+ g_memory_buffer_put_uint64 (mbuf, u.v_uint64);
}
break;
case 's': /* G_VARIANT_TYPE_STRING */
- padding_added = ensure_output_padding (mos, dos, 4);
+ padding_added = ensure_output_padding (mbuf, 4);
if (value != NULL)
{
gsize len;
const gchar *end;
v = g_variant_get_string (value, &len);
g_assert (g_utf8_validate (v, -1, &end) && (end == v + len));
- g_data_output_stream_put_uint32 (dos, len, NULL, NULL);
- g_data_output_stream_put_string (dos, v, NULL, NULL);
- g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
+ g_memory_buffer_put_uint32 (mbuf, len);
+ g_memory_buffer_put_string (mbuf, v);
+ g_memory_buffer_put_byte (mbuf, '\0');
}
break;
case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
- padding_added = ensure_output_padding (mos, dos, 4);
+ padding_added = ensure_output_padding (mbuf, 4);
if (value != NULL)
{
gsize len;
const gchar *v = g_variant_get_string (value, &len);
g_assert (g_variant_is_object_path (v));
- g_data_output_stream_put_uint32 (dos, len, NULL, NULL);
- g_data_output_stream_put_string (dos, v, NULL, NULL);
- g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
+ g_memory_buffer_put_uint32 (mbuf, len);
+ g_memory_buffer_put_string (mbuf, v);
+ g_memory_buffer_put_byte (mbuf, '\0');
}
break;
gsize len;
const gchar *v = g_variant_get_string (value, &len);
g_assert (g_variant_is_signature (v));
- g_data_output_stream_put_byte (dos, len, NULL, NULL);
- g_data_output_stream_put_string (dos, v, NULL, NULL);
- g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
+ g_memory_buffer_put_byte (mbuf, len);
+ g_memory_buffer_put_string (mbuf, v);
+ g_memory_buffer_put_byte (mbuf, '\0');
}
break;
case 'h': /* G_VARIANT_TYPE_HANDLE */
- padding_added = ensure_output_padding (mos, dos, 4);
+ padding_added = ensure_output_padding (mbuf, 4);
if (value != NULL)
{
gint32 v = g_variant_get_handle (value);
- g_data_output_stream_put_int32 (dos, v, NULL, NULL);
+ g_memory_buffer_put_int32 (mbuf, v);
}
break;
goffset cur_offset;
gsize array_len;
- padding_added = ensure_output_padding (mos, dos, 4);
+ padding_added = ensure_output_padding (mbuf, 4);
if (value != NULL)
{
/* array length - will be filled in later */
- array_len_offset = g_memory_output_stream_get_data_size (mos);
- g_data_output_stream_put_uint32 (dos, 0xF00DFACE, NULL, NULL);
+ array_len_offset = mbuf->valid_len;
+ g_memory_buffer_put_uint32 (mbuf, 0xF00DFACE);
/* From the D-Bus spec:
*
* Thus, we need to count how much padding the first element
* contributes and subtract that from the array length.
*/
- array_payload_begin_offset = g_memory_output_stream_get_data_size (mos);
+ array_payload_begin_offset = mbuf->valid_len;
if (g_variant_n_children (value) == 0)
{
gsize padding_added_for_item;
if (!append_value_to_blob (NULL,
g_variant_type_element (type),
- mos,
- dos,
+ mbuf,
&padding_added_for_item,
error))
goto fail;
gsize padding_added_for_item;
if (!append_value_to_blob (item,
g_variant_get_type (item),
- mos,
- dos,
+ mbuf,
&padding_added_for_item,
error))
{
}
}
- cur_offset = g_memory_output_stream_get_data_size (mos);
+ cur_offset = mbuf->valid_len;
array_len = cur_offset - array_payload_begin_offset;
- if (!g_seekable_seek (G_SEEKABLE (mos), array_len_offset, G_SEEK_SET, NULL, error))
- goto fail;
+ mbuf->pos = array_len_offset;
- g_data_output_stream_put_uint32 (dos, array_len, NULL, NULL);
- if (!g_seekable_seek (G_SEEKABLE (mos), cur_offset, G_SEEK_SET, NULL, error))
- goto fail;
+ g_memory_buffer_put_uint32 (mbuf, array_len);
+ mbuf->pos = cur_offset;
}
}
break;
default:
if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type))
{
- padding_added = ensure_output_padding (mos, dos, 8);
+ padding_added = ensure_output_padding (mbuf, 8);
if (value != NULL)
{
GVariant *item;
{
if (!append_value_to_blob (item,
g_variant_get_type (item),
- mos,
- dos,
+ mbuf,
NULL,
error))
{
const gchar *signature;
child = g_variant_get_child_value (value, 0);
signature = g_variant_get_type_string (child);
- g_data_output_stream_put_byte (dos, strlen (signature), NULL, NULL);
- g_data_output_stream_put_string (dos, signature, NULL, NULL);
- g_data_output_stream_put_byte (dos, '\0', NULL, NULL);
+ g_memory_buffer_put_byte (mbuf, strlen (signature));
+ g_memory_buffer_put_string (mbuf, signature);
+ g_memory_buffer_put_byte (mbuf, '\0');
if (!append_value_to_blob (child,
g_variant_get_type (child),
- mos,
- dos,
+ mbuf,
NULL,
error))
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Error serializing GVariant with type string `%s' to the D-Bus wire format"),
+ _("Error serializing GVariant with type string '%s' to the D-Bus wire format"),
g_variant_get_type_string (value));
goto fail;
}
static gboolean
append_body_to_blob (GVariant *value,
- GMemoryOutputStream *mos,
- GDataOutputStream *dos,
+ GMemoryBuffer *mbuf,
GError **error)
{
GVariant *item;
{
if (!append_value_to_blob (item,
g_variant_get_type (item),
- mos,
- dos,
+ mbuf,
NULL,
error))
{
* Serializes @message to a blob. The byte order returned by
* g_dbus_message_get_byte_order() will be used.
*
- * Returns: A pointer to a valid binary D-Bus message of @out_size bytes
- * generated by @message or %NULL if @error is set. Free with g_free().
+ * Returns: (array length=out_size) (transfer full): A pointer to a
+ * valid binary D-Bus message of @out_size bytes generated by @message
+ * or %NULL if @error is set. Free with g_free().
*
* Since: 2.26
*/
GDBusCapabilityFlags capabilities,
GError **error)
{
- GMemoryOutputStream *mos;
- GDataOutputStream *dos;
+ GMemoryBuffer mbuf;
guchar *ret;
gsize size;
- GDataStreamByteOrder byte_order;
goffset body_len_offset;
goffset body_start_offset;
gsize body_size;
g_return_val_if_fail (out_size != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
- mos = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (NULL, 0, g_realloc, g_free));
- dos = g_data_output_stream_new (G_OUTPUT_STREAM (mos));
+ memset (&mbuf, 0, sizeof (mbuf));
+ mbuf.len = MIN_ARRAY_SIZE;
+ mbuf.data = g_malloc (mbuf.len);
- byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN;
+ mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN;
switch (message->byte_order)
{
case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN:
- byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
+ mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
break;
case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN:
- byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
+ mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
break;
}
- g_data_output_stream_set_byte_order (dos, byte_order);
/* Core header */
- g_data_output_stream_put_byte (dos, (guchar) message->byte_order, NULL, NULL);
- g_data_output_stream_put_byte (dos, message->type, NULL, NULL);
- g_data_output_stream_put_byte (dos, message->flags, NULL, NULL);
- g_data_output_stream_put_byte (dos, 1, NULL, NULL); /* major protocol version */
- body_len_offset = g_memory_output_stream_get_data_size (mos);
+ g_memory_buffer_put_byte (&mbuf, (guchar) message->byte_order);
+ g_memory_buffer_put_byte (&mbuf, message->type);
+ g_memory_buffer_put_byte (&mbuf, message->flags);
+ g_memory_buffer_put_byte (&mbuf, 1); /* major protocol version */
+ body_len_offset = mbuf.valid_len;
/* body length - will be filled in later */
- g_data_output_stream_put_uint32 (dos, 0xF00DFACE, NULL, NULL);
- g_data_output_stream_put_uint32 (dos, message->serial, NULL, NULL);
+ g_memory_buffer_put_uint32 (&mbuf, 0xF00DFACE);
+ g_memory_buffer_put_uint32 (&mbuf, message->serial);
num_fds_in_message = 0;
#ifdef G_OS_UNIX
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Message has %d fds but the header field indicates %d fds"),
+ _("Message has %d file descriptors but the header field indicates %d file descriptors"),
num_fds_in_message,
num_fds_according_to_header);
goto out;
if (!append_value_to_blob (header_fields,
g_variant_get_type (header_fields),
- mos, dos,
+ &mbuf,
NULL,
error))
{
g_variant_unref (header_fields);
/* header size must be a multiple of 8 */
- ensure_output_padding (mos, dos, 8);
+ ensure_output_padding (&mbuf, 8);
- body_start_offset = g_memory_output_stream_get_data_size (mos);
+ body_start_offset = mbuf.valid_len;
signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
signature_str = NULL;
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Message body has signature `%s' but there is no signature header"),
+ _("Message body has signature '%s' but there is no signature header"),
signature_str);
g_free (tupled_signature_str);
goto out;
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Message body has type signature `%s' but signature in the header field is `%s'"),
+ _("Message body has type signature '%s' but signature in the header field is '%s'"),
tupled_signature_str, g_variant_get_type_string (message->body));
g_free (tupled_signature_str);
goto out;
}
g_free (tupled_signature_str);
- if (!append_body_to_blob (message->body, mos, dos, error))
+ if (!append_body_to_blob (message->body, &mbuf, error))
goto out;
}
else
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Message body is empty but signature in the header field is `(%s)'"),
+ _("Message body is empty but signature in the header field is '(%s)'"),
signature_str);
goto out;
}
}
/* OK, we're done writing the message - set the body length */
- size = g_memory_output_stream_get_data_size (mos);
+ size = mbuf.valid_len;
body_size = size - body_start_offset;
- if (!g_seekable_seek (G_SEEKABLE (mos), body_len_offset, G_SEEK_SET, NULL, error))
- goto out;
+ mbuf.pos = body_len_offset;
- g_data_output_stream_put_uint32 (dos, body_size, NULL, NULL);
-
- if (!g_output_stream_close (G_OUTPUT_STREAM (dos), NULL, error))
- goto out;
+ g_memory_buffer_put_uint32 (&mbuf, body_size);
*out_size = size;
- ret = g_memory_output_stream_steal_data (mos);
+ ret = (guchar *)mbuf.data;
out:
- g_object_unref (dos);
- g_object_unref (mos);
-
return ret;
}
g_dbus_error_set_dbus_error (error,
error_name,
"",
- _("Error return with body of type `%s'"),
+ _("Error return with body of type '%s'"),
g_variant_get_type_string (body));
}
else
* and formatting is subject to change at any time. Typical output
* looks something like this:
* <programlisting>
- * Type: method-call
- * Flags: none
- * Version: 0
- * Serial: 4
- * Headers:
+ * Type: method-call
+ * Flags: none
+ * Version: 0
+ * Serial: 4
+ * Headers:
* path -> objectpath '/org/gtk/GDBus/TestObject'
* interface -> 'org.gtk.GDBus.TestInterface'
* member -> 'GimmeStdout'
* destination -> ':1.146'
- * Body: ()
+ * Body: ()
* UNIX File Descriptors:
* (none)
* </programlisting>
* or
* <programlisting>
- * Type: method-return
- * Flags: no-reply-expected
- * Version: 0
- * Serial: 477
- * Headers:
+ * Type: method-return
+ * Flags: no-reply-expected
+ * Version: 0
+ * Serial: 477
+ * Headers:
* reply-serial -> uint32 4
* destination -> ':1.159'
* sender -> ':1.146'
* num-unix-fds -> uint32 1
- * Body: ()
- * UNIX File Descriptors:
+ * Body: ()
+ * UNIX File Descriptors:
* fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635
* </programlisting>
*
statbuf.st_mode);
g_string_append_printf (fs, "%s" "ino=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
(guint64) statbuf.st_ino);
- g_string_append_printf (fs, "%s" "uid=%d", fs->len > 0 ? "," : "",
- statbuf.st_uid);
- g_string_append_printf (fs, "%s" "gid=%d", fs->len > 0 ? "," : "",
- statbuf.st_gid);
+ g_string_append_printf (fs, "%s" "uid=%u", fs->len > 0 ? "," : "",
+ (guint) statbuf.st_uid);
+ g_string_append_printf (fs, "%s" "gid=%u", fs->len > 0 ? "," : "",
+ (guint) statbuf.st_gid);
g_string_append_printf (fs, "%s" "rdev=%d:%d", fs->len > 0 ? "," : "",
major (statbuf.st_rdev), minor (statbuf.st_rdev));
g_string_append_printf (fs, "%s" "size=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
while (g_hash_table_iter_next (&iter, &header_key, (gpointer) &header_value))
g_hash_table_insert (ret->headers, header_key, g_variant_ref (header_value));
+#ifdef G_OS_UNIX
out:
+#endif
return ret;
}