Do not expand autoptr macros when running introspection
[glib.git] / gio / gdbusmessage.c
blob717f2697ac674063cea200d61845d9b3dd64e332
1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 /* Uncomment to debug serializer code */
22 /* #define DEBUG_SERIALIZER */
24 #include "config.h"
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
31 #if MAJOR_IN_MKDEV
32 #include <sys/mkdev.h>
33 #elif MAJOR_IN_SYSMACROS
34 #include <sys/sysmacros.h>
35 #else
36 #define MAJOR_MINOR_NOT_FOUND 1
37 #endif
39 #include "gdbusutils.h"
40 #include "gdbusmessage.h"
41 #include "gdbuserror.h"
42 #include "gioenumtypes.h"
43 #include "ginputstream.h"
44 #include "gdatainputstream.h"
45 #include "gmemoryinputstream.h"
46 #include "goutputstream.h"
47 #include "gdataoutputstream.h"
48 #include "gmemoryoutputstream.h"
49 #include "gseekable.h"
50 #include "gioerror.h"
51 #include "gdbusprivate.h"
53 #ifdef G_OS_UNIX
54 #include "gunixfdlist.h"
55 #endif
57 #include "glibintl.h"
59 typedef struct _GMemoryBuffer GMemoryBuffer;
60 struct _GMemoryBuffer
62 gsize len;
63 gsize valid_len;
64 gsize pos;
65 gchar *data;
66 GDataStreamByteOrder byte_order;
69 static gboolean
70 g_memory_buffer_is_byteswapped (GMemoryBuffer *mbuf)
72 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
73 return mbuf->byte_order == G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
74 #else
75 return mbuf->byte_order == G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
76 #endif
79 static guchar
80 g_memory_buffer_read_byte (GMemoryBuffer *mbuf)
82 if (mbuf->pos >= mbuf->valid_len)
83 return 0;
84 return mbuf->data [mbuf->pos++];
87 static gint16
88 g_memory_buffer_read_int16 (GMemoryBuffer *mbuf)
90 gint16 v;
92 if (mbuf->pos > mbuf->valid_len - 2)
94 mbuf->pos = mbuf->valid_len;
95 return 0;
98 memcpy (&v, mbuf->data + mbuf->pos, 2);
99 mbuf->pos += 2;
101 if (g_memory_buffer_is_byteswapped (mbuf))
102 v = GUINT16_SWAP_LE_BE (v);
104 return v;
107 static guint16
108 g_memory_buffer_read_uint16 (GMemoryBuffer *mbuf)
110 guint16 v;
112 if (mbuf->pos > mbuf->valid_len - 2)
114 mbuf->pos = mbuf->valid_len;
115 return 0;
118 memcpy (&v, mbuf->data + mbuf->pos, 2);
119 mbuf->pos += 2;
121 if (g_memory_buffer_is_byteswapped (mbuf))
122 v = GUINT16_SWAP_LE_BE (v);
124 return v;
127 static gint32
128 g_memory_buffer_read_int32 (GMemoryBuffer *mbuf)
130 gint32 v;
132 if (mbuf->pos > mbuf->valid_len - 4)
134 mbuf->pos = mbuf->valid_len;
135 return 0;
138 memcpy (&v, mbuf->data + mbuf->pos, 4);
139 mbuf->pos += 4;
141 if (g_memory_buffer_is_byteswapped (mbuf))
142 v = GUINT32_SWAP_LE_BE (v);
144 return v;
147 static guint32
148 g_memory_buffer_read_uint32 (GMemoryBuffer *mbuf)
150 guint32 v;
152 if (mbuf->pos > mbuf->valid_len - 4)
154 mbuf->pos = mbuf->valid_len;
155 return 0;
158 memcpy (&v, mbuf->data + mbuf->pos, 4);
159 mbuf->pos += 4;
161 if (g_memory_buffer_is_byteswapped (mbuf))
162 v = GUINT32_SWAP_LE_BE (v);
164 return v;
167 static gint64
168 g_memory_buffer_read_int64 (GMemoryBuffer *mbuf)
170 gint64 v;
172 if (mbuf->pos > mbuf->valid_len - 8)
174 mbuf->pos = mbuf->valid_len;
175 return 0;
178 memcpy (&v, mbuf->data + mbuf->pos, 8);
179 mbuf->pos += 8;
181 if (g_memory_buffer_is_byteswapped (mbuf))
182 v = GUINT64_SWAP_LE_BE (v);
184 return v;
187 static guint64
188 g_memory_buffer_read_uint64 (GMemoryBuffer *mbuf)
190 guint64 v;
192 if (mbuf->pos > mbuf->valid_len - 8)
194 mbuf->pos = mbuf->valid_len;
195 return 0;
198 memcpy (&v, mbuf->data + mbuf->pos, 8);
199 mbuf->pos += 8;
201 if (g_memory_buffer_is_byteswapped (mbuf))
202 v = GUINT64_SWAP_LE_BE (v);
204 return v;
207 #define MIN_ARRAY_SIZE 128
209 static gsize
210 g_nearest_pow (gsize num)
212 gsize n = 1;
214 while (n < num && n > 0)
215 n <<= 1;
217 return n;
220 static void
221 array_resize (GMemoryBuffer *mbuf,
222 gsize size)
224 gpointer data;
225 gsize len;
227 if (mbuf->len == size)
228 return;
230 len = mbuf->len;
231 data = g_realloc (mbuf->data, size);
233 if (size > len)
234 memset ((guint8 *)data + len, 0, size - len);
236 mbuf->data = data;
237 mbuf->len = size;
239 if (mbuf->len < mbuf->valid_len)
240 mbuf->valid_len = mbuf->len;
243 static gboolean
244 g_memory_buffer_write (GMemoryBuffer *mbuf,
245 const void *buffer,
246 gsize count)
248 guint8 *dest;
249 gsize new_size;
251 if (count == 0)
252 return TRUE;
254 /* Check for address space overflow, but only if the buffer is resizable.
255 Otherwise we just do a short write and don't worry. */
256 if (mbuf->pos + count < mbuf->pos)
257 return FALSE;
259 if (mbuf->pos + count > mbuf->len)
261 /* At least enought to fit the write, rounded up
262 for greater than linear growth.
263 TODO: This wastes a lot of memory at large buffer sizes.
264 Figure out a more rational allocation strategy. */
265 new_size = g_nearest_pow (mbuf->pos + count);
266 /* Check for overflow again. We have checked if
267 pos + count > G_MAXSIZE, but now check if g_nearest_pow () has
268 overflowed */
269 if (new_size == 0)
270 return FALSE;
272 new_size = MAX (new_size, MIN_ARRAY_SIZE);
273 array_resize (mbuf, new_size);
276 dest = (guint8 *)mbuf->data + mbuf->pos;
277 memcpy (dest, buffer, count);
278 mbuf->pos += count;
280 if (mbuf->pos > mbuf->valid_len)
281 mbuf->valid_len = mbuf->pos;
283 return TRUE;
286 static gboolean
287 g_memory_buffer_put_byte (GMemoryBuffer *mbuf,
288 guchar data)
290 return g_memory_buffer_write (mbuf, &data, 1);
293 static gboolean
294 g_memory_buffer_put_int16 (GMemoryBuffer *mbuf,
295 gint16 data)
297 switch (mbuf->byte_order)
299 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
300 data = GINT16_TO_BE (data);
301 break;
302 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
303 data = GINT16_TO_LE (data);
304 break;
305 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
306 default:
307 break;
310 return g_memory_buffer_write (mbuf, &data, 2);
313 static gboolean
314 g_memory_buffer_put_uint16 (GMemoryBuffer *mbuf,
315 guint16 data)
317 switch (mbuf->byte_order)
319 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
320 data = GUINT16_TO_BE (data);
321 break;
322 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
323 data = GUINT16_TO_LE (data);
324 break;
325 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
326 default:
327 break;
330 return g_memory_buffer_write (mbuf, &data, 2);
333 static gboolean
334 g_memory_buffer_put_int32 (GMemoryBuffer *mbuf,
335 gint32 data)
337 switch (mbuf->byte_order)
339 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
340 data = GINT32_TO_BE (data);
341 break;
342 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
343 data = GINT32_TO_LE (data);
344 break;
345 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
346 default:
347 break;
350 return g_memory_buffer_write (mbuf, &data, 4);
353 static gboolean
354 g_memory_buffer_put_uint32 (GMemoryBuffer *mbuf,
355 guint32 data)
357 switch (mbuf->byte_order)
359 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
360 data = GUINT32_TO_BE (data);
361 break;
362 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
363 data = GUINT32_TO_LE (data);
364 break;
365 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
366 default:
367 break;
370 return g_memory_buffer_write (mbuf, &data, 4);
373 static gboolean
374 g_memory_buffer_put_int64 (GMemoryBuffer *mbuf,
375 gint64 data)
377 switch (mbuf->byte_order)
379 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
380 data = GINT64_TO_BE (data);
381 break;
382 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
383 data = GINT64_TO_LE (data);
384 break;
385 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
386 default:
387 break;
390 return g_memory_buffer_write (mbuf, &data, 8);
393 static gboolean
394 g_memory_buffer_put_uint64 (GMemoryBuffer *mbuf,
395 guint64 data)
397 switch (mbuf->byte_order)
399 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
400 data = GUINT64_TO_BE (data);
401 break;
402 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
403 data = GUINT64_TO_LE (data);
404 break;
405 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
406 default:
407 break;
410 return g_memory_buffer_write (mbuf, &data, 8);
413 static gboolean
414 g_memory_buffer_put_string (GMemoryBuffer *mbuf,
415 const char *str)
417 g_return_val_if_fail (str != NULL, FALSE);
419 return g_memory_buffer_write (mbuf, str, strlen (str));
424 * SECTION:gdbusmessage
425 * @short_description: D-Bus Message
426 * @include: gio/gio.h
428 * A type for representing D-Bus messages that can be sent or received
429 * on a #GDBusConnection.
432 typedef struct _GDBusMessageClass GDBusMessageClass;
435 * GDBusMessageClass:
437 * Class structure for #GDBusMessage.
439 * Since: 2.26
441 struct _GDBusMessageClass
443 /*< private >*/
444 GObjectClass parent_class;
448 * GDBusMessage:
450 * The #GDBusMessage structure contains only private data and should
451 * only be accessed using the provided API.
453 * Since: 2.26
455 struct _GDBusMessage
457 /*< private >*/
458 GObject parent_instance;
460 GDBusMessageType type;
461 GDBusMessageFlags flags;
462 gboolean locked;
463 GDBusMessageByteOrder byte_order;
464 guchar major_protocol_version;
465 guint32 serial;
466 GHashTable *headers;
467 GVariant *body;
468 #ifdef G_OS_UNIX
469 GUnixFDList *fd_list;
470 #endif
473 enum
475 PROP_0,
476 PROP_LOCKED
479 G_DEFINE_TYPE (GDBusMessage, g_dbus_message, G_TYPE_OBJECT)
481 static void
482 g_dbus_message_finalize (GObject *object)
484 GDBusMessage *message = G_DBUS_MESSAGE (object);
486 if (message->headers != NULL)
487 g_hash_table_unref (message->headers);
488 if (message->body != NULL)
489 g_variant_unref (message->body);
490 #ifdef G_OS_UNIX
491 if (message->fd_list != NULL)
492 g_object_unref (message->fd_list);
493 #endif
495 if (G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize != NULL)
496 G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize (object);
499 static void
500 g_dbus_message_get_property (GObject *object,
501 guint prop_id,
502 GValue *value,
503 GParamSpec *pspec)
505 GDBusMessage *message = G_DBUS_MESSAGE (object);
507 switch (prop_id)
509 case PROP_LOCKED:
510 g_value_set_boolean (value, g_dbus_message_get_locked (message));
511 break;
513 default:
514 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
515 break;
519 static void
520 g_dbus_message_class_init (GDBusMessageClass *klass)
522 GObjectClass *gobject_class;
524 gobject_class = G_OBJECT_CLASS (klass);
525 gobject_class->finalize = g_dbus_message_finalize;
526 gobject_class->get_property = g_dbus_message_get_property;
529 * GDBusConnection:locked:
531 * A boolean specifying whether the message is locked.
533 * Since: 2.26
535 g_object_class_install_property (gobject_class,
536 PROP_LOCKED,
537 g_param_spec_boolean ("locked",
538 P_("Locked"),
539 P_("Whether the message is locked"),
540 FALSE,
541 G_PARAM_READABLE |
542 G_PARAM_STATIC_NAME |
543 G_PARAM_STATIC_BLURB |
544 G_PARAM_STATIC_NICK));
547 static void
548 g_dbus_message_init (GDBusMessage *message)
550 /* Any D-Bus implementation is supposed to handle both Big and
551 * Little Endian encodings and the Endianness is part of the D-Bus
552 * message - we prefer to use Big Endian (since it's Network Byte
553 * Order and just easier to read for humans) but if the machine is
554 * Little Endian we use that for performance reasons.
556 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
557 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
558 #else
559 /* this could also be G_PDP_ENDIAN */
560 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
561 #endif
562 message->headers = g_hash_table_new_full (g_direct_hash,
563 g_direct_equal,
564 NULL,
565 (GDestroyNotify) g_variant_unref);
569 * g_dbus_message_new:
571 * Creates a new empty #GDBusMessage.
573 * Returns: A #GDBusMessage. Free with g_object_unref().
575 * Since: 2.26
577 GDBusMessage *
578 g_dbus_message_new (void)
580 return g_object_new (G_TYPE_DBUS_MESSAGE, NULL);
584 * g_dbus_message_new_method_call:
585 * @name: (nullable): A valid D-Bus name or %NULL.
586 * @path: A valid object path.
587 * @interface_: (nullable): A valid D-Bus interface name or %NULL.
588 * @method: A valid method name.
590 * Creates a new #GDBusMessage for a method call.
592 * Returns: A #GDBusMessage. Free with g_object_unref().
594 * Since: 2.26
596 GDBusMessage *
597 g_dbus_message_new_method_call (const gchar *name,
598 const gchar *path,
599 const gchar *interface_,
600 const gchar *method)
602 GDBusMessage *message;
604 g_return_val_if_fail (name == NULL || g_dbus_is_name (name), NULL);
605 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
606 g_return_val_if_fail (g_dbus_is_member_name (method), NULL);
607 g_return_val_if_fail (interface_ == NULL || g_dbus_is_interface_name (interface_), NULL);
609 message = g_dbus_message_new ();
610 message->type = G_DBUS_MESSAGE_TYPE_METHOD_CALL;
612 if (name != NULL)
613 g_dbus_message_set_destination (message, name);
614 g_dbus_message_set_path (message, path);
615 g_dbus_message_set_member (message, method);
616 if (interface_ != NULL)
617 g_dbus_message_set_interface (message, interface_);
619 return message;
623 * g_dbus_message_new_signal:
624 * @path: A valid object path.
625 * @interface_: A valid D-Bus interface name.
626 * @signal: A valid signal name.
628 * Creates a new #GDBusMessage for a signal emission.
630 * Returns: A #GDBusMessage. Free with g_object_unref().
632 * Since: 2.26
634 GDBusMessage *
635 g_dbus_message_new_signal (const gchar *path,
636 const gchar *interface_,
637 const gchar *signal)
639 GDBusMessage *message;
641 g_return_val_if_fail (g_variant_is_object_path (path), NULL);
642 g_return_val_if_fail (g_dbus_is_member_name (signal), NULL);
643 g_return_val_if_fail (g_dbus_is_interface_name (interface_), NULL);
645 message = g_dbus_message_new ();
646 message->type = G_DBUS_MESSAGE_TYPE_SIGNAL;
647 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
649 g_dbus_message_set_path (message, path);
650 g_dbus_message_set_member (message, signal);
651 g_dbus_message_set_interface (message, interface_);
653 return message;
658 * g_dbus_message_new_method_reply:
659 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
660 * create a reply message to.
662 * Creates a new #GDBusMessage that is a reply to @method_call_message.
664 * Returns: (transfer full): #GDBusMessage. Free with g_object_unref().
666 * Since: 2.26
668 GDBusMessage *
669 g_dbus_message_new_method_reply (GDBusMessage *method_call_message)
671 GDBusMessage *message;
672 const gchar *sender;
674 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL);
675 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL);
676 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
678 message = g_dbus_message_new ();
679 message->type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN;
680 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
681 /* reply with same endianness */
682 message->byte_order = method_call_message->byte_order;
684 g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
685 sender = g_dbus_message_get_sender (method_call_message);
686 if (sender != NULL)
687 g_dbus_message_set_destination (message, sender);
689 return message;
693 * g_dbus_message_new_method_error:
694 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
695 * create a reply message to.
696 * @error_name: A valid D-Bus error name.
697 * @error_message_format: The D-Bus error message in a printf() format.
698 * @...: Arguments for @error_message_format.
700 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
702 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
704 * Since: 2.26
706 GDBusMessage *
707 g_dbus_message_new_method_error (GDBusMessage *method_call_message,
708 const gchar *error_name,
709 const gchar *error_message_format,
710 ...)
712 GDBusMessage *ret;
713 va_list var_args;
715 va_start (var_args, error_message_format);
716 ret = g_dbus_message_new_method_error_valist (method_call_message,
717 error_name,
718 error_message_format,
719 var_args);
720 va_end (var_args);
722 return ret;
726 * g_dbus_message_new_method_error_literal:
727 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
728 * create a reply message to.
729 * @error_name: A valid D-Bus error name.
730 * @error_message: The D-Bus error message.
732 * Creates a new #GDBusMessage that is an error reply to @method_call_message.
734 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
736 * Since: 2.26
738 GDBusMessage *
739 g_dbus_message_new_method_error_literal (GDBusMessage *method_call_message,
740 const gchar *error_name,
741 const gchar *error_message)
743 GDBusMessage *message;
744 const gchar *sender;
746 g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL);
747 g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL);
748 g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL);
749 g_return_val_if_fail (g_dbus_is_name (error_name), NULL);
750 g_return_val_if_fail (error_message != NULL, NULL);
752 message = g_dbus_message_new ();
753 message->type = G_DBUS_MESSAGE_TYPE_ERROR;
754 message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
755 /* reply with same endianness */
756 message->byte_order = method_call_message->byte_order;
758 g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message));
759 g_dbus_message_set_error_name (message, error_name);
760 g_dbus_message_set_body (message, g_variant_new ("(s)", error_message));
762 sender = g_dbus_message_get_sender (method_call_message);
763 if (sender != NULL)
764 g_dbus_message_set_destination (message, sender);
766 return message;
770 * g_dbus_message_new_method_error_valist:
771 * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to
772 * create a reply message to.
773 * @error_name: A valid D-Bus error name.
774 * @error_message_format: The D-Bus error message in a printf() format.
775 * @var_args: Arguments for @error_message_format.
777 * Like g_dbus_message_new_method_error() but intended for language bindings.
779 * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref().
781 * Since: 2.26
783 G_GNUC_PRINTF(3, 0)
784 GDBusMessage *
785 g_dbus_message_new_method_error_valist (GDBusMessage *method_call_message,
786 const gchar *error_name,
787 const gchar *error_message_format,
788 va_list var_args)
790 GDBusMessage *ret;
791 gchar *error_message;
792 error_message = g_strdup_vprintf (error_message_format, var_args);
793 ret = g_dbus_message_new_method_error_literal (method_call_message,
794 error_name,
795 error_message);
796 g_free (error_message);
797 return ret;
800 /* ---------------------------------------------------------------------------------------------------- */
803 * g_dbus_message_get_byte_order:
804 * @message: A #GDBusMessage.
806 * Gets the byte order of @message.
808 * Returns: The byte order.
810 GDBusMessageByteOrder
811 g_dbus_message_get_byte_order (GDBusMessage *message)
813 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), (GDBusMessageByteOrder) 0);
814 return message->byte_order;
818 * g_dbus_message_set_byte_order:
819 * @message: A #GDBusMessage.
820 * @byte_order: The byte order.
822 * Sets the byte order of @message.
824 void
825 g_dbus_message_set_byte_order (GDBusMessage *message,
826 GDBusMessageByteOrder byte_order)
828 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
830 if (message->locked)
832 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
833 return;
836 message->byte_order = byte_order;
839 /* ---------------------------------------------------------------------------------------------------- */
841 /* TODO: need GI annotations to specify that any guchar value goes for the type */
844 * g_dbus_message_get_message_type:
845 * @message: A #GDBusMessage.
847 * Gets the type of @message.
849 * Returns: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
851 * Since: 2.26
853 GDBusMessageType
854 g_dbus_message_get_message_type (GDBusMessage *message)
856 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_TYPE_INVALID);
857 return message->type;
861 * g_dbus_message_set_message_type:
862 * @message: A #GDBusMessage.
863 * @type: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration).
865 * Sets @message to be of @type.
867 * Since: 2.26
869 void
870 g_dbus_message_set_message_type (GDBusMessage *message,
871 GDBusMessageType type)
873 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
874 g_return_if_fail (type >=0 && type < 256);
876 if (message->locked)
878 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
879 return;
882 message->type = type;
885 /* ---------------------------------------------------------------------------------------------------- */
887 /* TODO: need GI annotations to specify that any guchar value goes for flags */
890 * g_dbus_message_get_flags:
891 * @message: A #GDBusMessage.
893 * Gets the flags for @message.
895 * Returns: Flags that are set (typically values from the #GDBusMessageFlags enumeration bitwise ORed together).
897 * Since: 2.26
899 GDBusMessageFlags
900 g_dbus_message_get_flags (GDBusMessage *message)
902 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_FLAGS_NONE);
903 return message->flags;
907 * g_dbus_message_set_flags:
908 * @message: A #GDBusMessage.
909 * @flags: Flags for @message that are set (typically values from the #GDBusMessageFlags
910 * enumeration bitwise ORed together).
912 * Sets the flags to set on @message.
914 * Since: 2.26
916 void
917 g_dbus_message_set_flags (GDBusMessage *message,
918 GDBusMessageFlags flags)
920 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
921 g_return_if_fail (flags >=0 && flags < 256);
923 if (message->locked)
925 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
926 return;
929 message->flags = flags;
932 /* ---------------------------------------------------------------------------------------------------- */
935 * g_dbus_message_get_serial:
936 * @message: A #GDBusMessage.
938 * Gets the serial for @message.
940 * Returns: A #guint32.
942 * Since: 2.26
944 guint32
945 g_dbus_message_get_serial (GDBusMessage *message)
947 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
948 return message->serial;
952 * g_dbus_message_set_serial:
953 * @message: A #GDBusMessage.
954 * @serial: A #guint32.
956 * Sets the serial for @message.
958 * Since: 2.26
960 void
961 g_dbus_message_set_serial (GDBusMessage *message,
962 guint32 serial)
964 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
966 if (message->locked)
968 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
969 return;
972 message->serial = serial;
975 /* ---------------------------------------------------------------------------------------------------- */
977 /* TODO: need GI annotations to specify that any guchar value goes for header_field */
980 * g_dbus_message_get_header:
981 * @message: A #GDBusMessage.
982 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
984 * Gets a header field on @message.
986 * Returns: A #GVariant with the value if the header was found, %NULL
987 * otherwise. Do not free, it is owned by @message.
989 * Since: 2.26
991 GVariant *
992 g_dbus_message_get_header (GDBusMessage *message,
993 GDBusMessageHeaderField header_field)
995 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
996 g_return_val_if_fail (header_field >=0 && header_field < 256, NULL);
997 return g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
1001 * g_dbus_message_set_header:
1002 * @message: A #GDBusMessage.
1003 * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration)
1004 * @value: (nullable): A #GVariant to set the header field or %NULL to clear the header field.
1006 * Sets a header field on @message.
1008 * If @value is floating, @message assumes ownership of @value.
1010 * Since: 2.26
1012 void
1013 g_dbus_message_set_header (GDBusMessage *message,
1014 GDBusMessageHeaderField header_field,
1015 GVariant *value)
1017 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1018 g_return_if_fail (header_field >=0 && header_field < 256);
1020 if (message->locked)
1022 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1023 return;
1026 if (value == NULL)
1028 g_hash_table_remove (message->headers, GUINT_TO_POINTER (header_field));
1030 else
1032 g_hash_table_insert (message->headers, GUINT_TO_POINTER (header_field), g_variant_ref_sink (value));
1037 * g_dbus_message_get_header_fields:
1038 * @message: A #GDBusMessage.
1040 * Gets an array of all header fields on @message that are set.
1042 * Returns: (array zero-terminated=1): An array of header fields
1043 * terminated by %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element
1044 * is a #guchar. Free with g_free().
1046 * Since: 2.26
1048 guchar *
1049 g_dbus_message_get_header_fields (GDBusMessage *message)
1051 GList *keys;
1052 guchar *ret;
1053 guint num_keys;
1054 GList *l;
1055 guint n;
1057 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1059 keys = g_hash_table_get_keys (message->headers);
1060 num_keys = g_list_length (keys);
1061 ret = g_new (guchar, num_keys + 1);
1062 for (l = keys, n = 0; l != NULL; l = l->next, n++)
1063 ret[n] = GPOINTER_TO_UINT (l->data);
1064 g_assert (n == num_keys);
1065 ret[n] = G_DBUS_MESSAGE_HEADER_FIELD_INVALID;
1066 g_list_free (keys);
1068 return ret;
1071 /* ---------------------------------------------------------------------------------------------------- */
1074 * g_dbus_message_get_body:
1075 * @message: A #GDBusMessage.
1077 * Gets the body of a message.
1079 * Returns: (transfer none): A #GVariant or %NULL if the body is
1080 * empty. Do not free, it is owned by @message.
1082 * Since: 2.26
1084 GVariant *
1085 g_dbus_message_get_body (GDBusMessage *message)
1087 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1088 return message->body;
1092 * g_dbus_message_set_body:
1093 * @message: A #GDBusMessage.
1094 * @body: Either %NULL or a #GVariant that is a tuple.
1096 * Sets the body @message. As a side-effect the
1097 * %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field is set to the
1098 * type string of @body (or cleared if @body is %NULL).
1100 * If @body is floating, @message assumes ownership of @body.
1102 * Since: 2.26
1104 void
1105 g_dbus_message_set_body (GDBusMessage *message,
1106 GVariant *body)
1108 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1109 g_return_if_fail ((body == NULL) || g_variant_is_of_type (body, G_VARIANT_TYPE_TUPLE));
1111 if (message->locked)
1113 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1114 return;
1117 if (message->body != NULL)
1118 g_variant_unref (message->body);
1119 if (body == NULL)
1121 message->body = NULL;
1122 g_dbus_message_set_signature (message, NULL);
1124 else
1126 const gchar *type_string;
1127 gsize type_string_len;
1128 gchar *signature;
1130 message->body = g_variant_ref_sink (body);
1132 type_string = g_variant_get_type_string (body);
1133 type_string_len = strlen (type_string);
1134 g_assert (type_string_len >= 2);
1135 signature = g_strndup (type_string + 1, type_string_len - 2);
1136 g_dbus_message_set_signature (message, signature);
1137 g_free (signature);
1141 /* ---------------------------------------------------------------------------------------------------- */
1143 #ifdef G_OS_UNIX
1145 * g_dbus_message_get_unix_fd_list:
1146 * @message: A #GDBusMessage.
1148 * Gets the UNIX file descriptors associated with @message, if any.
1150 * This method is only available on UNIX.
1152 * Returns: (transfer none):A #GUnixFDList or %NULL if no file descriptors are
1153 * associated. Do not free, this object is owned by @message.
1155 * Since: 2.26
1157 GUnixFDList *
1158 g_dbus_message_get_unix_fd_list (GDBusMessage *message)
1160 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
1161 return message->fd_list;
1165 * g_dbus_message_set_unix_fd_list:
1166 * @message: A #GDBusMessage.
1167 * @fd_list: (nullable): A #GUnixFDList or %NULL.
1169 * Sets the UNIX file descriptors associated with @message. As a
1170 * side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header
1171 * field is set to the number of fds in @fd_list (or cleared if
1172 * @fd_list is %NULL).
1174 * This method is only available on UNIX.
1176 * Since: 2.26
1178 void
1179 g_dbus_message_set_unix_fd_list (GDBusMessage *message,
1180 GUnixFDList *fd_list)
1182 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
1183 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
1185 if (message->locked)
1187 g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);
1188 return;
1191 if (message->fd_list != NULL)
1192 g_object_unref (message->fd_list);
1193 if (fd_list != NULL)
1195 message->fd_list = g_object_ref (fd_list);
1196 g_dbus_message_set_num_unix_fds (message, g_unix_fd_list_get_length (fd_list));
1198 else
1200 message->fd_list = NULL;
1201 g_dbus_message_set_num_unix_fds (message, 0);
1204 #endif
1206 /* ---------------------------------------------------------------------------------------------------- */
1208 static guint
1209 get_type_fixed_size (const GVariantType *type)
1211 /* NB: we do not treat 'b' as fixed-size here because GVariant and
1212 * D-Bus disagree about the size.
1214 switch (*g_variant_type_peek_string (type))
1216 case 'y':
1217 return 1;
1218 case 'n': case 'q':
1219 return 2;
1220 case 'i': case 'u': case 'h':
1221 return 4;
1222 case 'x': case 't': case 'd':
1223 return 8;
1224 default:
1225 return 0;
1229 static gboolean
1230 validate_headers (GDBusMessage *message,
1231 GError **error)
1233 gboolean ret;
1235 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
1236 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1238 ret = FALSE;
1240 switch (message->type)
1242 case G_DBUS_MESSAGE_TYPE_INVALID:
1243 g_set_error_literal (error,
1244 G_IO_ERROR,
1245 G_IO_ERROR_INVALID_ARGUMENT,
1246 _("type is INVALID"));
1247 goto out;
1248 break;
1250 case G_DBUS_MESSAGE_TYPE_METHOD_CALL:
1251 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL ||
1252 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL)
1254 g_set_error_literal (error,
1255 G_IO_ERROR,
1256 G_IO_ERROR_INVALID_ARGUMENT,
1257 _("METHOD_CALL message: PATH or MEMBER header field is missing"));
1258 goto out;
1260 break;
1262 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN:
1263 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL)
1265 g_set_error_literal (error,
1266 G_IO_ERROR,
1267 G_IO_ERROR_INVALID_ARGUMENT,
1268 _("METHOD_RETURN message: REPLY_SERIAL header field is missing"));
1269 goto out;
1271 break;
1273 case G_DBUS_MESSAGE_TYPE_ERROR:
1274 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME) == NULL ||
1275 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL)
1277 g_set_error_literal (error,
1278 G_IO_ERROR,
1279 G_IO_ERROR_INVALID_ARGUMENT,
1280 _("ERROR message: REPLY_SERIAL or ERROR_NAME header field is missing"));
1281 goto out;
1283 break;
1285 case G_DBUS_MESSAGE_TYPE_SIGNAL:
1286 if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL ||
1287 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE) == NULL ||
1288 g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL)
1290 g_set_error_literal (error,
1291 G_IO_ERROR,
1292 G_IO_ERROR_INVALID_ARGUMENT,
1293 _("SIGNAL message: PATH, INTERFACE or MEMBER header field is missing"));
1294 goto out;
1296 if (g_strcmp0 (g_dbus_message_get_path (message), "/org/freedesktop/DBus/Local") == 0)
1298 g_set_error_literal (error,
1299 G_IO_ERROR,
1300 G_IO_ERROR_INVALID_ARGUMENT,
1301 _("SIGNAL message: The PATH header field is using the reserved value /org/freedesktop/DBus/Local"));
1302 goto out;
1304 if (g_strcmp0 (g_dbus_message_get_interface (message), "org.freedesktop.DBus.Local") == 0)
1306 g_set_error_literal (error,
1307 G_IO_ERROR,
1308 G_IO_ERROR_INVALID_ARGUMENT,
1309 _("SIGNAL message: The INTERFACE header field is using the reserved value org.freedesktop.DBus.Local"));
1310 goto out;
1312 break;
1314 default:
1315 /* hitherto unknown type - nothing to check */
1316 break;
1319 ret = TRUE;
1321 out:
1322 g_assert (ret || (error == NULL || *error != NULL));
1323 return ret;
1326 /* ---------------------------------------------------------------------------------------------------- */
1328 static gboolean
1329 ensure_input_padding (GMemoryBuffer *buf,
1330 gsize padding_size)
1332 gsize offset;
1333 gsize wanted_offset;
1335 offset = buf->pos;
1336 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
1337 buf->pos = wanted_offset;
1338 return TRUE;
1341 static const gchar *
1342 read_string (GMemoryBuffer *mbuf,
1343 gsize len,
1344 GError **error)
1346 gchar *str;
1347 const gchar *end_valid;
1349 if G_UNLIKELY (mbuf->pos + len >= mbuf->valid_len || mbuf->pos + len < mbuf->pos)
1351 mbuf->pos = mbuf->valid_len;
1352 /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
1353 g_set_error (error,
1354 G_IO_ERROR,
1355 G_IO_ERROR_INVALID_ARGUMENT,
1356 g_dngettext (GETTEXT_PACKAGE,
1357 "Wanted to read %lu byte but only got %lu",
1358 "Wanted to read %lu bytes but only got %lu",
1359 (gulong)len),
1360 (gulong)len,
1361 (gulong)(mbuf->valid_len - mbuf->pos));
1362 return NULL;
1365 if G_UNLIKELY (mbuf->data[mbuf->pos + len] != '\0')
1367 str = g_strndup (mbuf->data + mbuf->pos, len);
1368 g_set_error (error,
1369 G_IO_ERROR,
1370 G_IO_ERROR_INVALID_ARGUMENT,
1371 _("Expected NUL byte after the string “%s” but found byte %d"),
1372 str, mbuf->data[mbuf->pos + len]);
1373 g_free (str);
1374 mbuf->pos += len + 1;
1375 return NULL;
1378 str = mbuf->data + mbuf->pos;
1379 mbuf->pos += len + 1;
1381 if G_UNLIKELY (!g_utf8_validate (str, -1, &end_valid))
1383 gint offset;
1384 gchar *valid_str;
1385 offset = (gint) (end_valid - str);
1386 valid_str = g_strndup (str, offset);
1387 g_set_error (error,
1388 G_IO_ERROR,
1389 G_IO_ERROR_INVALID_ARGUMENT,
1390 _("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). "
1391 "The valid UTF-8 string up until that point was “%s”"),
1392 offset,
1393 (gint) len,
1394 valid_str);
1395 g_free (valid_str);
1396 return NULL;
1399 return str;
1402 static gconstpointer
1403 read_bytes (GMemoryBuffer *mbuf,
1404 gsize len,
1405 GError **error)
1407 gconstpointer result;
1409 if G_UNLIKELY (mbuf->pos + len > mbuf->valid_len || mbuf->pos + len < mbuf->pos)
1411 mbuf->pos = mbuf->valid_len;
1412 /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */
1413 g_set_error (error,
1414 G_IO_ERROR,
1415 G_IO_ERROR_INVALID_ARGUMENT,
1416 g_dngettext (GETTEXT_PACKAGE,
1417 "Wanted to read %lu byte but only got %lu",
1418 "Wanted to read %lu bytes but only got %lu",
1419 (gulong)len),
1420 (gulong)len,
1421 (gulong)(mbuf->valid_len - mbuf->pos));
1422 return NULL;
1425 result = mbuf->data + mbuf->pos;
1426 mbuf->pos += len;
1428 return result;
1431 /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */
1433 /* returns a non-floating GVariant! */
1434 static GVariant *
1435 parse_value_from_blob (GMemoryBuffer *buf,
1436 const GVariantType *type,
1437 gboolean just_align,
1438 guint indent,
1439 GError **error)
1441 GVariant *ret;
1442 GError *local_error;
1443 #ifdef DEBUG_SERIALIZER
1444 gboolean is_leaf;
1445 #endif /* DEBUG_SERIALIZER */
1446 const gchar *type_string;
1448 type_string = g_variant_type_peek_string (type);
1450 #ifdef DEBUG_SERIALIZER
1452 gchar *s;
1453 s = g_variant_type_dup_string (type);
1454 g_print ("%*s%s type %s from offset 0x%04x",
1455 indent, "",
1456 just_align ? "Aligning" : "Reading",
1458 (gint) buf->pos);
1459 g_free (s);
1461 #endif /* DEBUG_SERIALIZER */
1463 ret = NULL;
1465 #ifdef DEBUG_SERIALIZER
1466 is_leaf = TRUE;
1467 #endif /* DEBUG_SERIALIZER */
1468 local_error = NULL;
1469 switch (type_string[0])
1471 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
1472 ensure_input_padding (buf, 4);
1473 if (!just_align)
1475 gboolean v;
1476 v = g_memory_buffer_read_uint32 (buf);
1477 ret = g_variant_new_boolean (v);
1479 break;
1481 case 'y': /* G_VARIANT_TYPE_BYTE */
1482 if (!just_align)
1484 guchar v;
1485 v = g_memory_buffer_read_byte (buf);
1486 ret = g_variant_new_byte (v);
1488 break;
1490 case 'n': /* G_VARIANT_TYPE_INT16 */
1491 ensure_input_padding (buf, 2);
1492 if (!just_align)
1494 gint16 v;
1495 v = g_memory_buffer_read_int16 (buf);
1496 ret = g_variant_new_int16 (v);
1498 break;
1500 case 'q': /* G_VARIANT_TYPE_UINT16 */
1501 ensure_input_padding (buf, 2);
1502 if (!just_align)
1504 guint16 v;
1505 v = g_memory_buffer_read_uint16 (buf);
1506 ret = g_variant_new_uint16 (v);
1508 break;
1510 case 'i': /* G_VARIANT_TYPE_INT32 */
1511 ensure_input_padding (buf, 4);
1512 if (!just_align)
1514 gint32 v;
1515 v = g_memory_buffer_read_int32 (buf);
1516 ret = g_variant_new_int32 (v);
1518 break;
1520 case 'u': /* G_VARIANT_TYPE_UINT32 */
1521 ensure_input_padding (buf, 4);
1522 if (!just_align)
1524 guint32 v;
1525 v = g_memory_buffer_read_uint32 (buf);
1526 ret = g_variant_new_uint32 (v);
1528 break;
1530 case 'x': /* G_VARIANT_TYPE_INT64 */
1531 ensure_input_padding (buf, 8);
1532 if (!just_align)
1534 gint64 v;
1535 v = g_memory_buffer_read_int64 (buf);
1536 ret = g_variant_new_int64 (v);
1538 break;
1540 case 't': /* G_VARIANT_TYPE_UINT64 */
1541 ensure_input_padding (buf, 8);
1542 if (!just_align)
1544 guint64 v;
1545 v = g_memory_buffer_read_uint64 (buf);
1546 ret = g_variant_new_uint64 (v);
1548 break;
1550 case 'd': /* G_VARIANT_TYPE_DOUBLE */
1551 ensure_input_padding (buf, 8);
1552 if (!just_align)
1554 union {
1555 guint64 v_uint64;
1556 gdouble v_double;
1557 } u;
1558 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
1559 u.v_uint64 = g_memory_buffer_read_uint64 (buf);
1560 ret = g_variant_new_double (u.v_double);
1562 break;
1564 case 's': /* G_VARIANT_TYPE_STRING */
1565 ensure_input_padding (buf, 4);
1566 if (!just_align)
1568 guint32 len;
1569 const gchar *v;
1570 len = g_memory_buffer_read_uint32 (buf);
1571 v = read_string (buf, (gsize) len, &local_error);
1572 if (v == NULL)
1573 goto fail;
1574 ret = g_variant_new_string (v);
1576 break;
1578 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
1579 ensure_input_padding (buf, 4);
1580 if (!just_align)
1582 guint32 len;
1583 const gchar *v;
1584 len = g_memory_buffer_read_uint32 (buf);
1585 v = read_string (buf, (gsize) len, &local_error);
1586 if (v == NULL)
1587 goto fail;
1588 if (!g_variant_is_object_path (v))
1590 g_set_error (&local_error,
1591 G_IO_ERROR,
1592 G_IO_ERROR_INVALID_ARGUMENT,
1593 _("Parsed value “%s” is not a valid D-Bus object path"),
1595 goto fail;
1597 ret = g_variant_new_object_path (v);
1599 break;
1601 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
1602 if (!just_align)
1604 guchar len;
1605 const gchar *v;
1606 len = g_memory_buffer_read_byte (buf);
1607 v = read_string (buf, (gsize) len, &local_error);
1608 if (v == NULL)
1609 goto fail;
1610 if (!g_variant_is_signature (v))
1612 g_set_error (&local_error,
1613 G_IO_ERROR,
1614 G_IO_ERROR_INVALID_ARGUMENT,
1615 _("Parsed value “%s” is not a valid D-Bus signature"),
1617 goto fail;
1619 ret = g_variant_new_signature (v);
1621 break;
1623 case 'h': /* G_VARIANT_TYPE_HANDLE */
1624 ensure_input_padding (buf, 4);
1625 if (!just_align)
1627 gint32 v;
1628 v = g_memory_buffer_read_int32 (buf);
1629 ret = g_variant_new_handle (v);
1631 break;
1633 case 'a': /* G_VARIANT_TYPE_ARRAY */
1634 ensure_input_padding (buf, 4);
1636 /* If we are only aligning for this array type, it is the child type of
1637 * another array, which is empty. So, we do not need to add padding for
1638 * this nonexistent array's elements: we only need to align for this
1639 * array itself (4 bytes). See
1640 * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>.
1642 if (!just_align)
1644 guint32 array_len;
1645 const GVariantType *element_type;
1646 guint fixed_size;
1648 array_len = g_memory_buffer_read_uint32 (buf);
1650 #ifdef DEBUG_SERIALIZER
1651 is_leaf = FALSE;
1652 g_print (": array spans 0x%04x bytes\n", array_len);
1653 #endif /* DEBUG_SERIALIZER */
1655 if (array_len > (2<<26))
1657 /* G_GUINT32_FORMAT doesn't work with gettext, so use u */
1658 g_set_error (&local_error,
1659 G_IO_ERROR,
1660 G_IO_ERROR_INVALID_ARGUMENT,
1661 g_dngettext (GETTEXT_PACKAGE,
1662 "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).",
1663 "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).",
1664 array_len),
1665 array_len);
1666 goto fail;
1669 element_type = g_variant_type_element (type);
1670 fixed_size = get_type_fixed_size (element_type);
1672 /* Fast-path the cases like 'ay', etc. */
1673 if (fixed_size != 0)
1675 gconstpointer array_data;
1677 if (array_len % fixed_size != 0)
1679 g_set_error (&local_error,
1680 G_IO_ERROR,
1681 G_IO_ERROR_INVALID_ARGUMENT,
1682 _("Encountered array of type “a%c”, expected to have a length a multiple "
1683 "of %u bytes, but found to be %u bytes in length"),
1684 g_variant_type_peek_string (element_type)[0], fixed_size, array_len);
1685 goto fail;
1688 ensure_input_padding (buf, fixed_size);
1689 array_data = read_bytes (buf, array_len, &local_error);
1690 if (array_data == NULL)
1691 goto fail;
1693 ret = g_variant_new_fixed_array (element_type, array_data, array_len / fixed_size, fixed_size);
1695 if (g_memory_buffer_is_byteswapped (buf))
1697 GVariant *tmp = g_variant_ref_sink (ret);
1698 ret = g_variant_byteswap (tmp);
1699 g_variant_unref (tmp);
1702 else
1704 GVariantBuilder builder;
1705 goffset offset;
1706 goffset target;
1708 g_variant_builder_init (&builder, type);
1710 if (array_len == 0)
1712 GVariant *item;
1713 item = parse_value_from_blob (buf,
1714 element_type,
1715 TRUE,
1716 indent + 2,
1717 NULL);
1718 g_assert (item == NULL);
1720 else
1722 offset = buf->pos;
1723 target = offset + array_len;
1724 while (offset < target)
1726 GVariant *item;
1727 item = parse_value_from_blob (buf,
1728 element_type,
1729 FALSE,
1730 indent + 2,
1731 &local_error);
1732 if (item == NULL)
1734 g_variant_builder_clear (&builder);
1735 goto fail;
1737 g_variant_builder_add_value (&builder, item);
1738 g_variant_unref (item);
1739 offset = buf->pos;
1743 ret = g_variant_builder_end (&builder);
1746 break;
1748 default:
1749 if (g_variant_type_is_dict_entry (type))
1751 const GVariantType *key_type;
1752 const GVariantType *value_type;
1753 GVariant *key;
1754 GVariant *value;
1756 ensure_input_padding (buf, 8);
1758 #ifdef DEBUG_SERIALIZER
1759 is_leaf = FALSE;
1760 g_print ("\n");
1761 #endif /* DEBUG_SERIALIZER */
1763 if (!just_align)
1765 key_type = g_variant_type_key (type);
1766 key = parse_value_from_blob (buf,
1767 key_type,
1768 FALSE,
1769 indent + 2,
1770 &local_error);
1771 if (key == NULL)
1772 goto fail;
1773 value_type = g_variant_type_value (type);
1774 value = parse_value_from_blob (buf,
1775 value_type,
1776 FALSE,
1777 indent + 2,
1778 &local_error);
1779 if (value == NULL)
1781 g_variant_unref (key);
1782 goto fail;
1784 ret = g_variant_new_dict_entry (key, value);
1785 g_variant_unref (key);
1786 g_variant_unref (value);
1789 else if (g_variant_type_is_tuple (type))
1791 ensure_input_padding (buf, 8);
1793 #ifdef DEBUG_SERIALIZER
1794 is_leaf = FALSE;
1795 g_print ("\n");
1796 #endif /* DEBUG_SERIALIZER */
1798 if (!just_align)
1800 const GVariantType *element_type;
1801 GVariantBuilder builder;
1803 g_variant_builder_init (&builder, type);
1804 element_type = g_variant_type_first (type);
1805 while (element_type != NULL)
1807 GVariant *item;
1808 item = parse_value_from_blob (buf,
1809 element_type,
1810 FALSE,
1811 indent + 2,
1812 &local_error);
1813 if (item == NULL)
1815 g_variant_builder_clear (&builder);
1816 goto fail;
1818 g_variant_builder_add_value (&builder, item);
1819 g_variant_unref (item);
1821 element_type = g_variant_type_next (element_type);
1823 ret = g_variant_builder_end (&builder);
1826 else if (g_variant_type_is_variant (type))
1828 #ifdef DEBUG_SERIALIZER
1829 is_leaf = FALSE;
1830 g_print ("\n");
1831 #endif /* DEBUG_SERIALIZER */
1833 if (!just_align)
1835 guchar siglen;
1836 const gchar *sig;
1837 GVariantType *variant_type;
1838 GVariant *value;
1840 siglen = g_memory_buffer_read_byte (buf);
1841 sig = read_string (buf, (gsize) siglen, &local_error);
1842 if (sig == NULL)
1843 goto fail;
1844 if (!g_variant_is_signature (sig))
1846 g_set_error (&local_error,
1847 G_IO_ERROR,
1848 G_IO_ERROR_INVALID_ARGUMENT,
1849 _("Parsed value “%s” for variant is not a valid D-Bus signature"),
1850 sig);
1851 goto fail;
1853 variant_type = g_variant_type_new (sig);
1854 value = parse_value_from_blob (buf,
1855 variant_type,
1856 FALSE,
1857 indent + 2,
1858 &local_error);
1859 g_variant_type_free (variant_type);
1860 if (value == NULL)
1861 goto fail;
1862 ret = g_variant_new_variant (value);
1863 g_variant_unref (value);
1866 else
1868 gchar *s;
1869 s = g_variant_type_dup_string (type);
1870 g_set_error (&local_error,
1871 G_IO_ERROR,
1872 G_IO_ERROR_INVALID_ARGUMENT,
1873 _("Error deserializing GVariant with type string “%s” from the D-Bus wire format"),
1875 g_free (s);
1876 goto fail;
1878 break;
1881 g_assert ((just_align && ret == NULL) || (!just_align && ret != NULL));
1883 #ifdef DEBUG_SERIALIZER
1884 if (ret != NULL)
1886 if (is_leaf)
1888 gchar *s;
1889 if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
1891 s = g_strdup_printf ("0x%02x '%c'", g_variant_get_byte (ret), g_variant_get_byte (ret));
1893 else
1895 s = g_variant_print (ret, FALSE);
1897 g_print (": %s\n", s);
1898 g_free (s);
1901 #endif /* DEBUG_SERIALIZER */
1903 /* sink the reference, if floating */
1904 if (ret != NULL)
1905 g_variant_take_ref (ret);
1906 return ret;
1908 fail:
1909 #ifdef DEBUG_SERIALIZER
1910 g_print ("\n"
1911 "%*sFAILURE: %s (%s, %d)\n",
1912 indent, "",
1913 local_error->message,
1914 g_quark_to_string (local_error->domain),
1915 local_error->code);
1916 #endif /* DEBUG_SERIALIZER */
1917 g_propagate_error (error, local_error);
1918 return NULL;
1921 /* ---------------------------------------------------------------------------------------------------- */
1923 /* message_header must be at least 16 bytes */
1926 * g_dbus_message_bytes_needed:
1927 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1928 * @blob_len: The length of @blob (must be at least 16).
1929 * @error: Return location for error or %NULL.
1931 * Utility function to calculate how many bytes are needed to
1932 * completely deserialize the D-Bus message stored at @blob.
1934 * Returns: Number of bytes needed or -1 if @error is set (e.g. if
1935 * @blob contains invalid data or not enough data is available to
1936 * determine the size).
1938 * Since: 2.26
1940 gssize
1941 g_dbus_message_bytes_needed (guchar *blob,
1942 gsize blob_len,
1943 GError **error)
1945 gssize ret;
1947 ret = -1;
1949 g_return_val_if_fail (blob != NULL, -1);
1950 g_return_val_if_fail (error == NULL || *error == NULL, -1);
1951 g_return_val_if_fail (blob_len >= 16, -1);
1953 if (blob[0] == 'l')
1955 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1956 ret = 12 + 4 + GUINT32_FROM_LE (((guint32 *) blob)[3]);
1957 /* round up so it's a multiple of 8 */
1958 ret = 8 * ((ret + 7)/8);
1959 /* finally add the body size */
1960 ret += GUINT32_FROM_LE (((guint32 *) blob)[1]);
1962 else if (blob[0] == 'B')
1964 /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */
1965 ret = 12 + 4 + GUINT32_FROM_BE (((guint32 *) blob)[3]);
1966 /* round up so it's a multiple of 8 */
1967 ret = 8 * ((ret + 7)/8);
1968 /* finally add the body size */
1969 ret += GUINT32_FROM_BE (((guint32 *) blob)[1]);
1971 else
1973 g_set_error (error,
1974 G_IO_ERROR,
1975 G_IO_ERROR_INVALID_ARGUMENT,
1976 "Unable to determine message blob length - given blob is malformed");
1979 if (ret > (2<<27))
1981 g_set_error (error,
1982 G_IO_ERROR,
1983 G_IO_ERROR_INVALID_ARGUMENT,
1984 "Blob indicates that message exceeds maximum message length (128MiB)");
1985 ret = -1;
1988 return ret;
1991 /* ---------------------------------------------------------------------------------------------------- */
1994 * g_dbus_message_new_from_blob:
1995 * @blob: (array length=blob_len) (element-type guint8): A blob represent a binary D-Bus message.
1996 * @blob_len: The length of @blob.
1997 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
1998 * @error: Return location for error or %NULL.
2000 * Creates a new #GDBusMessage from the data stored at @blob. The byte
2001 * order that the message was in can be retrieved using
2002 * g_dbus_message_get_byte_order().
2004 * Returns: A new #GDBusMessage or %NULL if @error is set. Free with
2005 * g_object_unref().
2007 * Since: 2.26
2009 GDBusMessage *
2010 g_dbus_message_new_from_blob (guchar *blob,
2011 gsize blob_len,
2012 GDBusCapabilityFlags capabilities,
2013 GError **error)
2015 gboolean ret;
2016 GMemoryBuffer mbuf;
2017 GDBusMessage *message;
2018 guchar endianness;
2019 guchar major_protocol_version;
2020 guint32 message_body_len;
2021 GVariant *headers;
2022 GVariant *item;
2023 GVariantIter iter;
2024 GVariant *signature;
2026 /* TODO: check against @capabilities */
2028 ret = FALSE;
2030 g_return_val_if_fail (blob != NULL, NULL);
2031 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2032 g_return_val_if_fail (blob_len >= 12, NULL);
2034 message = g_dbus_message_new ();
2036 memset (&mbuf, 0, sizeof (mbuf));
2037 mbuf.data = (gchar *)blob;
2038 mbuf.len = mbuf.valid_len = blob_len;
2040 endianness = g_memory_buffer_read_byte (&mbuf);
2041 switch (endianness)
2043 case 'l':
2044 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
2045 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN;
2046 break;
2047 case 'B':
2048 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
2049 message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN;
2050 break;
2051 default:
2052 g_set_error (error,
2053 G_IO_ERROR,
2054 G_IO_ERROR_INVALID_ARGUMENT,
2055 _("Invalid endianness value. Expected 0x6c (“l”) or 0x42 (“B”) but found value 0x%02x"),
2056 endianness);
2057 goto out;
2060 message->type = g_memory_buffer_read_byte (&mbuf);
2061 message->flags = g_memory_buffer_read_byte (&mbuf);
2062 major_protocol_version = g_memory_buffer_read_byte (&mbuf);
2063 if (major_protocol_version != 1)
2065 g_set_error (error,
2066 G_IO_ERROR,
2067 G_IO_ERROR_INVALID_ARGUMENT,
2068 _("Invalid major protocol version. Expected 1 but found %d"),
2069 major_protocol_version);
2070 goto out;
2072 message_body_len = g_memory_buffer_read_uint32 (&mbuf);
2073 message->serial = g_memory_buffer_read_uint32 (&mbuf);
2075 #ifdef DEBUG_SERIALIZER
2076 g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint) blob_len);
2078 gchar *s;
2079 s = _g_dbus_hexdump ((const gchar *) blob, blob_len, 2);
2080 g_print ("%s\n", s);
2081 g_free (s);
2083 #endif /* DEBUG_SERIALIZER */
2085 #ifdef DEBUG_SERIALIZER
2086 g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint) blob_len);
2087 #endif /* DEBUG_SERIALIZER */
2088 headers = parse_value_from_blob (&mbuf,
2089 G_VARIANT_TYPE ("a{yv}"),
2090 FALSE,
2092 error);
2093 if (headers == NULL)
2094 goto out;
2095 g_variant_iter_init (&iter, headers);
2096 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2098 guchar header_field;
2099 GVariant *value;
2100 g_variant_get (item,
2101 "{yv}",
2102 &header_field,
2103 &value);
2104 g_dbus_message_set_header (message, header_field, value);
2105 g_variant_unref (value);
2106 g_variant_unref (item);
2108 g_variant_unref (headers);
2110 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
2111 if (signature != NULL)
2113 const gchar *signature_str;
2114 gsize signature_str_len;
2116 signature_str = g_variant_get_string (signature, &signature_str_len);
2118 /* signature but no body */
2119 if (message_body_len == 0 && signature_str_len > 0)
2121 g_set_error (error,
2122 G_IO_ERROR,
2123 G_IO_ERROR_INVALID_ARGUMENT,
2124 _("Signature header with signature “%s” found but message body is empty"),
2125 signature_str);
2126 goto out;
2128 else if (signature_str_len > 0)
2130 GVariantType *variant_type;
2131 gchar *tupled_signature_str;
2133 if (!g_variant_is_signature (signature_str))
2135 g_set_error (error,
2136 G_IO_ERROR,
2137 G_IO_ERROR_INVALID_ARGUMENT,
2138 _("Parsed value “%s” is not a valid D-Bus signature (for body)"),
2139 signature_str);
2140 goto out;
2142 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
2143 variant_type = g_variant_type_new (tupled_signature_str);
2144 g_free (tupled_signature_str);
2145 #ifdef DEBUG_SERIALIZER
2146 g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint) blob_len);
2147 #endif /* DEBUG_SERIALIZER */
2148 message->body = parse_value_from_blob (&mbuf,
2149 variant_type,
2150 FALSE,
2152 error);
2153 g_variant_type_free (variant_type);
2154 if (message->body == NULL)
2155 goto out;
2158 else
2160 /* no signature, this is only OK if the body is empty */
2161 if (message_body_len != 0)
2163 /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */
2164 g_set_error (error,
2165 G_IO_ERROR,
2166 G_IO_ERROR_INVALID_ARGUMENT,
2167 g_dngettext (GETTEXT_PACKAGE,
2168 "No signature header in message but the message body is %u byte",
2169 "No signature header in message but the message body is %u bytes",
2170 message_body_len),
2171 message_body_len);
2172 goto out;
2176 if (!validate_headers (message, error))
2178 g_prefix_error (error, _("Cannot deserialize message: "));
2179 goto out;
2182 ret = TRUE;
2184 out:
2185 if (ret)
2187 return message;
2189 else
2191 if (message != NULL)
2192 g_object_unref (message);
2193 return NULL;
2197 /* ---------------------------------------------------------------------------------------------------- */
2199 static gsize
2200 ensure_output_padding (GMemoryBuffer *mbuf,
2201 gsize padding_size)
2203 gsize offset;
2204 gsize wanted_offset;
2205 gsize padding_needed;
2206 guint n;
2208 offset = mbuf->pos;
2209 wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size;
2210 padding_needed = wanted_offset - offset;
2212 for (n = 0; n < padding_needed; n++)
2213 g_memory_buffer_put_byte (mbuf, '\0');
2215 return padding_needed;
2218 /* note that value can be NULL for e.g. empty arrays - type is never NULL */
2219 static gboolean
2220 append_value_to_blob (GVariant *value,
2221 const GVariantType *type,
2222 GMemoryBuffer *mbuf,
2223 gsize *out_padding_added,
2224 GError **error)
2226 gsize padding_added;
2227 const gchar *type_string;
2229 type_string = g_variant_type_peek_string (type);
2231 padding_added = 0;
2233 switch (type_string[0])
2235 case 'b': /* G_VARIANT_TYPE_BOOLEAN */
2236 padding_added = ensure_output_padding (mbuf, 4);
2237 if (value != NULL)
2239 gboolean v = g_variant_get_boolean (value);
2240 g_memory_buffer_put_uint32 (mbuf, v);
2242 break;
2244 case 'y': /* G_VARIANT_TYPE_BYTE */
2245 if (value != NULL)
2247 guint8 v = g_variant_get_byte (value);
2248 g_memory_buffer_put_byte (mbuf, v);
2250 break;
2252 case 'n': /* G_VARIANT_TYPE_INT16 */
2253 padding_added = ensure_output_padding (mbuf, 2);
2254 if (value != NULL)
2256 gint16 v = g_variant_get_int16 (value);
2257 g_memory_buffer_put_int16 (mbuf, v);
2259 break;
2261 case 'q': /* G_VARIANT_TYPE_UINT16 */
2262 padding_added = ensure_output_padding (mbuf, 2);
2263 if (value != NULL)
2265 guint16 v = g_variant_get_uint16 (value);
2266 g_memory_buffer_put_uint16 (mbuf, v);
2268 break;
2270 case 'i': /* G_VARIANT_TYPE_INT32 */
2271 padding_added = ensure_output_padding (mbuf, 4);
2272 if (value != NULL)
2274 gint32 v = g_variant_get_int32 (value);
2275 g_memory_buffer_put_int32 (mbuf, v);
2277 break;
2279 case 'u': /* G_VARIANT_TYPE_UINT32 */
2280 padding_added = ensure_output_padding (mbuf, 4);
2281 if (value != NULL)
2283 guint32 v = g_variant_get_uint32 (value);
2284 g_memory_buffer_put_uint32 (mbuf, v);
2286 break;
2288 case 'x': /* G_VARIANT_TYPE_INT64 */
2289 padding_added = ensure_output_padding (mbuf, 8);
2290 if (value != NULL)
2292 gint64 v = g_variant_get_int64 (value);
2293 g_memory_buffer_put_int64 (mbuf, v);
2295 break;
2297 case 't': /* G_VARIANT_TYPE_UINT64 */
2298 padding_added = ensure_output_padding (mbuf, 8);
2299 if (value != NULL)
2301 guint64 v = g_variant_get_uint64 (value);
2302 g_memory_buffer_put_uint64 (mbuf, v);
2304 break;
2306 case 'd': /* G_VARIANT_TYPE_DOUBLE */
2307 padding_added = ensure_output_padding (mbuf, 8);
2308 if (value != NULL)
2310 union {
2311 guint64 v_uint64;
2312 gdouble v_double;
2313 } u;
2314 G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64));
2315 u.v_double = g_variant_get_double (value);
2316 g_memory_buffer_put_uint64 (mbuf, u.v_uint64);
2318 break;
2320 case 's': /* G_VARIANT_TYPE_STRING */
2321 padding_added = ensure_output_padding (mbuf, 4);
2322 if (value != NULL)
2324 gsize len;
2325 const gchar *v;
2326 const gchar *end;
2327 v = g_variant_get_string (value, &len);
2328 g_assert (g_utf8_validate (v, -1, &end) && (end == v + len));
2329 g_memory_buffer_put_uint32 (mbuf, len);
2330 g_memory_buffer_put_string (mbuf, v);
2331 g_memory_buffer_put_byte (mbuf, '\0');
2333 break;
2335 case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */
2336 padding_added = ensure_output_padding (mbuf, 4);
2337 if (value != NULL)
2339 gsize len;
2340 const gchar *v = g_variant_get_string (value, &len);
2341 g_assert (g_variant_is_object_path (v));
2342 g_memory_buffer_put_uint32 (mbuf, len);
2343 g_memory_buffer_put_string (mbuf, v);
2344 g_memory_buffer_put_byte (mbuf, '\0');
2346 break;
2348 case 'g': /* G_VARIANT_TYPE_SIGNATURE */
2349 if (value != NULL)
2351 gsize len;
2352 const gchar *v = g_variant_get_string (value, &len);
2353 g_assert (g_variant_is_signature (v));
2354 g_memory_buffer_put_byte (mbuf, len);
2355 g_memory_buffer_put_string (mbuf, v);
2356 g_memory_buffer_put_byte (mbuf, '\0');
2358 break;
2360 case 'h': /* G_VARIANT_TYPE_HANDLE */
2361 padding_added = ensure_output_padding (mbuf, 4);
2362 if (value != NULL)
2364 gint32 v = g_variant_get_handle (value);
2365 g_memory_buffer_put_int32 (mbuf, v);
2367 break;
2369 case 'a': /* G_VARIANT_TYPE_ARRAY */
2371 const GVariantType *element_type;
2372 GVariant *item;
2373 GVariantIter iter;
2374 goffset array_len_offset;
2375 goffset array_payload_begin_offset;
2376 goffset cur_offset;
2377 gsize array_len;
2378 guint fixed_size;
2380 padding_added = ensure_output_padding (mbuf, 4);
2381 if (value != NULL)
2383 /* array length - will be filled in later */
2384 array_len_offset = mbuf->valid_len;
2385 g_memory_buffer_put_uint32 (mbuf, 0xF00DFACE);
2387 /* From the D-Bus spec:
2389 * "A UINT32 giving the length of the array data in bytes,
2390 * followed by alignment padding to the alignment boundary of
2391 * the array element type, followed by each array element. The
2392 * array length is from the end of the alignment padding to
2393 * the end of the last element, i.e. it does not include the
2394 * padding after the length, or any padding after the last
2395 * element."
2397 * Thus, we need to count how much padding the first element
2398 * contributes and subtract that from the array length.
2400 array_payload_begin_offset = mbuf->valid_len;
2402 element_type = g_variant_type_element (type);
2403 fixed_size = get_type_fixed_size (element_type);
2405 if (g_variant_n_children (value) == 0)
2407 gsize padding_added_for_item;
2408 if (!append_value_to_blob (NULL,
2409 element_type,
2410 mbuf,
2411 &padding_added_for_item,
2412 error))
2413 goto fail;
2414 array_payload_begin_offset += padding_added_for_item;
2416 else if (fixed_size != 0)
2418 GVariant *use_value;
2420 if (g_memory_buffer_is_byteswapped (mbuf))
2421 use_value = g_variant_byteswap (value);
2422 else
2423 use_value = g_variant_ref (value);
2425 array_payload_begin_offset += ensure_output_padding (mbuf, fixed_size);
2427 array_len = g_variant_get_size (use_value);
2428 g_memory_buffer_write (mbuf, g_variant_get_data (use_value), array_len);
2429 g_variant_unref (use_value);
2431 else
2433 guint n;
2434 n = 0;
2435 g_variant_iter_init (&iter, value);
2436 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2438 gsize padding_added_for_item;
2439 if (!append_value_to_blob (item,
2440 g_variant_get_type (item),
2441 mbuf,
2442 &padding_added_for_item,
2443 error))
2445 g_variant_unref (item);
2446 goto fail;
2448 g_variant_unref (item);
2449 if (n == 0)
2451 array_payload_begin_offset += padding_added_for_item;
2453 n++;
2457 cur_offset = mbuf->valid_len;
2458 array_len = cur_offset - array_payload_begin_offset;
2459 mbuf->pos = array_len_offset;
2461 g_memory_buffer_put_uint32 (mbuf, array_len);
2462 mbuf->pos = cur_offset;
2465 break;
2467 default:
2468 if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type))
2470 padding_added = ensure_output_padding (mbuf, 8);
2471 if (value != NULL)
2473 GVariant *item;
2474 GVariantIter iter;
2475 g_variant_iter_init (&iter, value);
2476 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2478 if (!append_value_to_blob (item,
2479 g_variant_get_type (item),
2480 mbuf,
2481 NULL,
2482 error))
2484 g_variant_unref (item);
2485 goto fail;
2487 g_variant_unref (item);
2491 else if (g_variant_type_is_variant (type))
2493 if (value != NULL)
2495 GVariant *child;
2496 const gchar *signature;
2497 child = g_variant_get_child_value (value, 0);
2498 signature = g_variant_get_type_string (child);
2499 g_memory_buffer_put_byte (mbuf, strlen (signature));
2500 g_memory_buffer_put_string (mbuf, signature);
2501 g_memory_buffer_put_byte (mbuf, '\0');
2502 if (!append_value_to_blob (child,
2503 g_variant_get_type (child),
2504 mbuf,
2505 NULL,
2506 error))
2508 g_variant_unref (child);
2509 goto fail;
2511 g_variant_unref (child);
2514 else
2516 g_set_error (error,
2517 G_IO_ERROR,
2518 G_IO_ERROR_INVALID_ARGUMENT,
2519 _("Error serializing GVariant with type string “%s” to the D-Bus wire format"),
2520 g_variant_get_type_string (value));
2521 goto fail;
2523 break;
2526 if (out_padding_added != NULL)
2527 *out_padding_added = padding_added;
2529 return TRUE;
2531 fail:
2532 return FALSE;
2535 static gboolean
2536 append_body_to_blob (GVariant *value,
2537 GMemoryBuffer *mbuf,
2538 GError **error)
2540 GVariant *item;
2541 GVariantIter iter;
2543 if (!g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE))
2545 g_set_error (error,
2546 G_IO_ERROR,
2547 G_IO_ERROR_INVALID_ARGUMENT,
2548 "Expected a tuple for the body of the GDBusMessage.");
2549 goto fail;
2552 g_variant_iter_init (&iter, value);
2553 while ((item = g_variant_iter_next_value (&iter)) != NULL)
2555 if (!append_value_to_blob (item,
2556 g_variant_get_type (item),
2557 mbuf,
2558 NULL,
2559 error))
2561 g_variant_unref (item);
2562 goto fail;
2564 g_variant_unref (item);
2566 return TRUE;
2568 fail:
2569 return FALSE;
2572 /* ---------------------------------------------------------------------------------------------------- */
2575 * g_dbus_message_to_blob:
2576 * @message: A #GDBusMessage.
2577 * @out_size: Return location for size of generated blob.
2578 * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported.
2579 * @error: Return location for error.
2581 * Serializes @message to a blob. The byte order returned by
2582 * g_dbus_message_get_byte_order() will be used.
2584 * Returns: (array length=out_size) (transfer full): A pointer to a
2585 * valid binary D-Bus message of @out_size bytes generated by @message
2586 * or %NULL if @error is set. Free with g_free().
2588 * Since: 2.26
2590 guchar *
2591 g_dbus_message_to_blob (GDBusMessage *message,
2592 gsize *out_size,
2593 GDBusCapabilityFlags capabilities,
2594 GError **error)
2596 GMemoryBuffer mbuf;
2597 guchar *ret;
2598 gsize size;
2599 goffset body_len_offset;
2600 goffset body_start_offset;
2601 gsize body_size;
2602 GVariant *header_fields;
2603 GVariantBuilder builder;
2604 GHashTableIter hash_iter;
2605 gpointer key;
2606 GVariant *header_value;
2607 GVariant *signature;
2608 const gchar *signature_str;
2609 gint num_fds_in_message;
2610 gint num_fds_according_to_header;
2612 /* TODO: check against @capabilities */
2614 ret = NULL;
2616 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2617 g_return_val_if_fail (out_size != NULL, NULL);
2618 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2620 memset (&mbuf, 0, sizeof (mbuf));
2621 mbuf.len = MIN_ARRAY_SIZE;
2622 mbuf.data = g_malloc (mbuf.len);
2624 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN;
2625 switch (message->byte_order)
2627 case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN:
2628 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
2629 break;
2630 case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN:
2631 mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
2632 break;
2635 /* Core header */
2636 g_memory_buffer_put_byte (&mbuf, (guchar) message->byte_order);
2637 g_memory_buffer_put_byte (&mbuf, message->type);
2638 g_memory_buffer_put_byte (&mbuf, message->flags);
2639 g_memory_buffer_put_byte (&mbuf, 1); /* major protocol version */
2640 body_len_offset = mbuf.valid_len;
2641 /* body length - will be filled in later */
2642 g_memory_buffer_put_uint32 (&mbuf, 0xF00DFACE);
2643 g_memory_buffer_put_uint32 (&mbuf, message->serial);
2645 num_fds_in_message = 0;
2646 #ifdef G_OS_UNIX
2647 if (message->fd_list != NULL)
2648 num_fds_in_message = g_unix_fd_list_get_length (message->fd_list);
2649 #endif
2650 num_fds_according_to_header = g_dbus_message_get_num_unix_fds (message);
2651 if (num_fds_in_message != num_fds_according_to_header)
2653 g_set_error (error,
2654 G_IO_ERROR,
2655 G_IO_ERROR_INVALID_ARGUMENT,
2656 _("Message has %d file descriptors but the header field indicates %d file descriptors"),
2657 num_fds_in_message,
2658 num_fds_according_to_header);
2659 goto out;
2662 if (!validate_headers (message, error))
2664 g_prefix_error (error, _("Cannot serialize message: "));
2665 goto out;
2668 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{yv}"));
2669 g_hash_table_iter_init (&hash_iter, message->headers);
2670 while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value))
2672 g_variant_builder_add (&builder,
2673 "{yv}",
2674 (guchar) GPOINTER_TO_UINT (key),
2675 header_value);
2677 header_fields = g_variant_builder_end (&builder);
2679 if (!append_value_to_blob (header_fields,
2680 g_variant_get_type (header_fields),
2681 &mbuf,
2682 NULL,
2683 error))
2685 g_variant_unref (header_fields);
2686 goto out;
2688 g_variant_unref (header_fields);
2690 /* header size must be a multiple of 8 */
2691 ensure_output_padding (&mbuf, 8);
2693 body_start_offset = mbuf.valid_len;
2695 signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
2696 signature_str = NULL;
2697 if (signature != NULL)
2698 signature_str = g_variant_get_string (signature, NULL);
2699 if (message->body != NULL)
2701 gchar *tupled_signature_str;
2702 tupled_signature_str = g_strdup_printf ("(%s)", signature_str);
2703 if (signature == NULL)
2705 g_set_error (error,
2706 G_IO_ERROR,
2707 G_IO_ERROR_INVALID_ARGUMENT,
2708 _("Message body has signature “%s” but there is no signature header"),
2709 signature_str);
2710 g_free (tupled_signature_str);
2711 goto out;
2713 else if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->body)) != 0)
2715 g_set_error (error,
2716 G_IO_ERROR,
2717 G_IO_ERROR_INVALID_ARGUMENT,
2718 _("Message body has type signature “%s” but signature in the header field is “%s”"),
2719 tupled_signature_str, g_variant_get_type_string (message->body));
2720 g_free (tupled_signature_str);
2721 goto out;
2723 g_free (tupled_signature_str);
2724 if (!append_body_to_blob (message->body, &mbuf, error))
2725 goto out;
2727 else
2729 if (signature != NULL && strlen (signature_str) > 0)
2731 g_set_error (error,
2732 G_IO_ERROR,
2733 G_IO_ERROR_INVALID_ARGUMENT,
2734 _("Message body is empty but signature in the header field is “(%s)”"),
2735 signature_str);
2736 goto out;
2740 /* OK, we're done writing the message - set the body length */
2741 size = mbuf.valid_len;
2742 body_size = size - body_start_offset;
2744 mbuf.pos = body_len_offset;
2746 g_memory_buffer_put_uint32 (&mbuf, body_size);
2748 *out_size = size;
2749 ret = (guchar *)mbuf.data;
2751 out:
2752 if (ret == NULL)
2753 g_free (mbuf.data);
2755 return ret;
2758 /* ---------------------------------------------------------------------------------------------------- */
2760 static guint32
2761 get_uint32_header (GDBusMessage *message,
2762 GDBusMessageHeaderField header_field)
2764 GVariant *value;
2765 guint32 ret;
2767 ret = 0;
2768 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2769 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32))
2770 ret = g_variant_get_uint32 (value);
2772 return ret;
2775 static const gchar *
2776 get_string_header (GDBusMessage *message,
2777 GDBusMessageHeaderField header_field)
2779 GVariant *value;
2780 const gchar *ret;
2782 ret = NULL;
2783 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2784 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
2785 ret = g_variant_get_string (value, NULL);
2787 return ret;
2790 static const gchar *
2791 get_object_path_header (GDBusMessage *message,
2792 GDBusMessageHeaderField header_field)
2794 GVariant *value;
2795 const gchar *ret;
2797 ret = NULL;
2798 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2799 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH))
2800 ret = g_variant_get_string (value, NULL);
2802 return ret;
2805 static const gchar *
2806 get_signature_header (GDBusMessage *message,
2807 GDBusMessageHeaderField header_field)
2809 GVariant *value;
2810 const gchar *ret;
2812 ret = NULL;
2813 value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field));
2814 if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE))
2815 ret = g_variant_get_string (value, NULL);
2817 return ret;
2820 /* ---------------------------------------------------------------------------------------------------- */
2822 static void
2823 set_uint32_header (GDBusMessage *message,
2824 GDBusMessageHeaderField header_field,
2825 guint32 value)
2827 g_dbus_message_set_header (message,
2828 header_field,
2829 g_variant_new_uint32 (value));
2832 static void
2833 set_string_header (GDBusMessage *message,
2834 GDBusMessageHeaderField header_field,
2835 const gchar *value)
2837 g_dbus_message_set_header (message,
2838 header_field,
2839 value == NULL ? NULL : g_variant_new_string (value));
2842 static void
2843 set_object_path_header (GDBusMessage *message,
2844 GDBusMessageHeaderField header_field,
2845 const gchar *value)
2847 g_dbus_message_set_header (message,
2848 header_field,
2849 value == NULL ? NULL : g_variant_new_object_path (value));
2852 static void
2853 set_signature_header (GDBusMessage *message,
2854 GDBusMessageHeaderField header_field,
2855 const gchar *value)
2857 g_dbus_message_set_header (message,
2858 header_field,
2859 value == NULL ? NULL : g_variant_new_signature (value));
2862 /* ---------------------------------------------------------------------------------------------------- */
2865 * g_dbus_message_get_reply_serial:
2866 * @message: A #GDBusMessage.
2868 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2870 * Returns: The value.
2872 * Since: 2.26
2874 guint32
2875 g_dbus_message_get_reply_serial (GDBusMessage *message)
2877 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
2878 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL);
2882 * g_dbus_message_set_reply_serial:
2883 * @message: A #GDBusMessage.
2884 * @value: The value to set.
2886 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field.
2888 * Since: 2.26
2890 void
2891 g_dbus_message_set_reply_serial (GDBusMessage *message,
2892 guint32 value)
2894 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2895 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, value);
2898 /* ---------------------------------------------------------------------------------------------------- */
2901 * g_dbus_message_get_interface:
2902 * @message: A #GDBusMessage.
2904 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2906 * Returns: The value.
2908 * Since: 2.26
2910 const gchar *
2911 g_dbus_message_get_interface (GDBusMessage *message)
2913 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2914 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE);
2918 * g_dbus_message_set_interface:
2919 * @message: A #GDBusMessage.
2920 * @value: The value to set.
2922 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field.
2924 * Since: 2.26
2926 void
2927 g_dbus_message_set_interface (GDBusMessage *message,
2928 const gchar *value)
2930 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2931 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
2932 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, value);
2935 /* ---------------------------------------------------------------------------------------------------- */
2938 * g_dbus_message_get_member:
2939 * @message: A #GDBusMessage.
2941 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2943 * Returns: The value.
2945 * Since: 2.26
2947 const gchar *
2948 g_dbus_message_get_member (GDBusMessage *message)
2950 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2951 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER);
2955 * g_dbus_message_set_member:
2956 * @message: A #GDBusMessage.
2957 * @value: The value to set.
2959 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field.
2961 * Since: 2.26
2963 void
2964 g_dbus_message_set_member (GDBusMessage *message,
2965 const gchar *value)
2967 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2968 g_return_if_fail (value == NULL || g_dbus_is_member_name (value));
2969 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, value);
2972 /* ---------------------------------------------------------------------------------------------------- */
2975 * g_dbus_message_get_path:
2976 * @message: A #GDBusMessage.
2978 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2980 * Returns: The value.
2982 * Since: 2.26
2984 const gchar *
2985 g_dbus_message_get_path (GDBusMessage *message)
2987 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2988 return get_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH);
2992 * g_dbus_message_set_path:
2993 * @message: A #GDBusMessage.
2994 * @value: The value to set.
2996 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field.
2998 * Since: 2.26
3000 void
3001 g_dbus_message_set_path (GDBusMessage *message,
3002 const gchar *value)
3004 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3005 g_return_if_fail (value == NULL || g_variant_is_object_path (value));
3006 set_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, value);
3009 /* ---------------------------------------------------------------------------------------------------- */
3012 * g_dbus_message_get_sender:
3013 * @message: A #GDBusMessage.
3015 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
3017 * Returns: The value.
3019 * Since: 2.26
3021 const gchar *
3022 g_dbus_message_get_sender (GDBusMessage *message)
3024 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3025 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER);
3029 * g_dbus_message_set_sender:
3030 * @message: A #GDBusMessage.
3031 * @value: The value to set.
3033 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field.
3035 * Since: 2.26
3037 void
3038 g_dbus_message_set_sender (GDBusMessage *message,
3039 const gchar *value)
3041 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3042 g_return_if_fail (value == NULL || g_dbus_is_name (value));
3043 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER, value);
3046 /* ---------------------------------------------------------------------------------------------------- */
3049 * g_dbus_message_get_destination:
3050 * @message: A #GDBusMessage.
3052 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3054 * Returns: The value.
3056 * Since: 2.26
3058 const gchar *
3059 g_dbus_message_get_destination (GDBusMessage *message)
3061 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3062 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION);
3066 * g_dbus_message_set_destination:
3067 * @message: A #GDBusMessage.
3068 * @value: The value to set.
3070 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field.
3072 * Since: 2.26
3074 void
3075 g_dbus_message_set_destination (GDBusMessage *message,
3076 const gchar *value)
3078 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3079 g_return_if_fail (value == NULL || g_dbus_is_name (value));
3080 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION, value);
3083 /* ---------------------------------------------------------------------------------------------------- */
3086 * g_dbus_message_get_error_name:
3087 * @message: A #GDBusMessage.
3089 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3091 * Returns: The value.
3093 * Since: 2.26
3095 const gchar *
3096 g_dbus_message_get_error_name (GDBusMessage *message)
3098 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3099 return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME);
3103 * g_dbus_message_set_error_name:
3104 * @message: A #GDBusMessage.
3105 * @value: The value to set.
3107 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field.
3109 * Since: 2.26
3111 void
3112 g_dbus_message_set_error_name (GDBusMessage *message,
3113 const gchar *value)
3115 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3116 g_return_if_fail (value == NULL || g_dbus_is_interface_name (value));
3117 set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, value);
3120 /* ---------------------------------------------------------------------------------------------------- */
3123 * g_dbus_message_get_signature:
3124 * @message: A #GDBusMessage.
3126 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3128 * Returns: The value.
3130 * Since: 2.26
3132 const gchar *
3133 g_dbus_message_get_signature (GDBusMessage *message)
3135 const gchar *ret;
3136 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3137 ret = get_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE);
3138 if (ret == NULL)
3139 ret = "";
3140 return ret;
3144 * g_dbus_message_set_signature:
3145 * @message: A #GDBusMessage.
3146 * @value: The value to set.
3148 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field.
3150 * Since: 2.26
3152 void
3153 g_dbus_message_set_signature (GDBusMessage *message,
3154 const gchar *value)
3156 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3157 g_return_if_fail (value == NULL || g_variant_is_signature (value));
3158 set_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE, value);
3161 /* ---------------------------------------------------------------------------------------------------- */
3164 * g_dbus_message_get_arg0:
3165 * @message: A #GDBusMessage.
3167 * Convenience to get the first item in the body of @message.
3169 * Returns: The string item or %NULL if the first item in the body of
3170 * @message is not a string.
3172 * Since: 2.26
3174 const gchar *
3175 g_dbus_message_get_arg0 (GDBusMessage *message)
3177 const gchar *ret;
3179 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3181 ret = NULL;
3183 if (message->body != NULL && g_variant_is_of_type (message->body, G_VARIANT_TYPE_TUPLE))
3185 GVariant *item;
3186 item = g_variant_get_child_value (message->body, 0);
3187 if (g_variant_is_of_type (item, G_VARIANT_TYPE_STRING))
3188 ret = g_variant_get_string (item, NULL);
3189 g_variant_unref (item);
3192 return ret;
3195 /* ---------------------------------------------------------------------------------------------------- */
3198 * g_dbus_message_get_num_unix_fds:
3199 * @message: A #GDBusMessage.
3201 * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3203 * Returns: The value.
3205 * Since: 2.26
3207 guint32
3208 g_dbus_message_get_num_unix_fds (GDBusMessage *message)
3210 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0);
3211 return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS);
3215 * g_dbus_message_set_num_unix_fds:
3216 * @message: A #GDBusMessage.
3217 * @value: The value to set.
3219 * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field.
3221 * Since: 2.26
3223 void
3224 g_dbus_message_set_num_unix_fds (GDBusMessage *message,
3225 guint32 value)
3227 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3228 set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS, value);
3231 /* ---------------------------------------------------------------------------------------------------- */
3234 * g_dbus_message_to_gerror:
3235 * @message: A #GDBusMessage.
3236 * @error: The #GError to set.
3238 * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does
3239 * nothing and returns %FALSE.
3241 * Otherwise this method encodes the error in @message as a #GError
3242 * using g_dbus_error_set_dbus_error() using the information in the
3243 * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as
3244 * well as the first string item in @message's body.
3246 * Returns: %TRUE if @error was set, %FALSE otherwise.
3248 * Since: 2.26
3250 gboolean
3251 g_dbus_message_to_gerror (GDBusMessage *message,
3252 GError **error)
3254 gboolean ret;
3255 const gchar *error_name;
3257 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
3259 ret = FALSE;
3260 if (message->type != G_DBUS_MESSAGE_TYPE_ERROR)
3261 goto out;
3263 error_name = g_dbus_message_get_error_name (message);
3264 if (error_name != NULL)
3266 GVariant *body;
3268 body = g_dbus_message_get_body (message);
3270 if (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(s)")))
3272 const gchar *error_message;
3273 g_variant_get (body, "(&s)", &error_message);
3274 g_dbus_error_set_dbus_error (error,
3275 error_name,
3276 error_message,
3277 NULL);
3279 else
3281 /* these two situations are valid, yet pretty rare */
3282 if (body != NULL)
3284 g_dbus_error_set_dbus_error (error,
3285 error_name,
3287 _("Error return with body of type “%s”"),
3288 g_variant_get_type_string (body));
3290 else
3292 g_dbus_error_set_dbus_error (error,
3293 error_name,
3295 _("Error return with empty body"));
3299 else
3301 /* TOOD: this shouldn't happen - should check this at message serialization
3302 * time and disconnect the peer.
3304 g_set_error (error,
3305 G_IO_ERROR,
3306 G_IO_ERROR_FAILED,
3307 "Error return without error-name header!");
3310 ret = TRUE;
3312 out:
3313 return ret;
3316 /* ---------------------------------------------------------------------------------------------------- */
3318 static gchar *
3319 flags_to_string (GType flags_type, guint value)
3321 GString *s;
3322 GFlagsClass *klass;
3323 guint n;
3325 klass = g_type_class_ref (flags_type);
3326 s = g_string_new (NULL);
3327 for (n = 0; n < 32; n++)
3329 if ((value & (1<<n)) != 0)
3331 GFlagsValue *flags_value;
3332 flags_value = g_flags_get_first_value (klass, (1<<n));
3333 if (s->len > 0)
3334 g_string_append_c (s, ',');
3335 if (flags_value != NULL)
3336 g_string_append (s, flags_value->value_nick);
3337 else
3338 g_string_append_printf (s, "unknown (bit %d)", n);
3341 if (s->len == 0)
3342 g_string_append (s, "none");
3343 g_type_class_unref (klass);
3344 return g_string_free (s, FALSE);
3347 static gint
3348 _sort_keys_func (gconstpointer a,
3349 gconstpointer b)
3351 gint ia;
3352 gint ib;
3354 ia = GPOINTER_TO_INT (a);
3355 ib = GPOINTER_TO_INT (b);
3357 return ia - ib;
3361 * g_dbus_message_print:
3362 * @message: A #GDBusMessage.
3363 * @indent: Indentation level.
3365 * Produces a human-readable multi-line description of @message.
3367 * The contents of the description has no ABI guarantees, the contents
3368 * and formatting is subject to change at any time. Typical output
3369 * looks something like this:
3370 * |[
3371 * Type: method-call
3372 * Flags: none
3373 * Version: 0
3374 * Serial: 4
3375 * Headers:
3376 * path -> objectpath '/org/gtk/GDBus/TestObject'
3377 * interface -> 'org.gtk.GDBus.TestInterface'
3378 * member -> 'GimmeStdout'
3379 * destination -> ':1.146'
3380 * Body: ()
3381 * UNIX File Descriptors:
3382 * (none)
3383 * ]|
3384 * or
3385 * |[
3386 * Type: method-return
3387 * Flags: no-reply-expected
3388 * Version: 0
3389 * Serial: 477
3390 * Headers:
3391 * reply-serial -> uint32 4
3392 * destination -> ':1.159'
3393 * sender -> ':1.146'
3394 * num-unix-fds -> uint32 1
3395 * Body: ()
3396 * UNIX File Descriptors:
3397 * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635
3398 * ]|
3400 * Returns: A string that should be freed with g_free().
3402 * Since: 2.26
3404 gchar *
3405 g_dbus_message_print (GDBusMessage *message,
3406 guint indent)
3408 GString *str;
3409 gchar *s;
3410 GList *keys;
3411 GList *l;
3413 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3415 str = g_string_new (NULL);
3417 s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->type);
3418 g_string_append_printf (str, "%*sType: %s\n", indent, "", s);
3419 g_free (s);
3420 s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->flags);
3421 g_string_append_printf (str, "%*sFlags: %s\n", indent, "", s);
3422 g_free (s);
3423 g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->major_protocol_version);
3424 g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->serial);
3426 g_string_append_printf (str, "%*sHeaders:\n", indent, "");
3427 keys = g_hash_table_get_keys (message->headers);
3428 keys = g_list_sort (keys, _sort_keys_func);
3429 if (keys != NULL)
3431 for (l = keys; l != NULL; l = l->next)
3433 gint key = GPOINTER_TO_INT (l->data);
3434 GVariant *value;
3435 gchar *value_str;
3437 value = g_hash_table_lookup (message->headers, l->data);
3438 g_assert (value != NULL);
3440 s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key);
3441 value_str = g_variant_print (value, TRUE);
3442 g_string_append_printf (str, "%*s %s -> %s\n", indent, "", s, value_str);
3443 g_free (s);
3444 g_free (value_str);
3447 else
3449 g_string_append_printf (str, "%*s (none)\n", indent, "");
3451 g_list_free (keys);
3452 g_string_append_printf (str, "%*sBody: ", indent, "");
3453 if (message->body != NULL)
3455 g_variant_print_string (message->body,
3456 str,
3457 TRUE);
3459 else
3461 g_string_append (str, "()");
3463 g_string_append (str, "\n");
3464 #ifdef G_OS_UNIX
3465 g_string_append_printf (str, "%*sUNIX File Descriptors:\n", indent, "");
3466 if (message->fd_list != NULL)
3468 gint num_fds;
3469 const gint *fds;
3470 gint n;
3472 fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds);
3473 if (num_fds > 0)
3475 for (n = 0; n < num_fds; n++)
3477 GString *fs;
3478 struct stat statbuf;
3479 fs = g_string_new (NULL);
3480 if (fstat (fds[n], &statbuf) == 0)
3482 #ifndef MAJOR_MINOR_NOT_FOUND
3483 g_string_append_printf (fs, "%s" "dev=%d:%d", fs->len > 0 ? "," : "",
3484 (gint) major (statbuf.st_dev), (gint) minor (statbuf.st_dev));
3485 #endif
3486 g_string_append_printf (fs, "%s" "mode=0%o", fs->len > 0 ? "," : "",
3487 (guint) statbuf.st_mode);
3488 g_string_append_printf (fs, "%s" "ino=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3489 (guint64) statbuf.st_ino);
3490 g_string_append_printf (fs, "%s" "uid=%u", fs->len > 0 ? "," : "",
3491 (guint) statbuf.st_uid);
3492 g_string_append_printf (fs, "%s" "gid=%u", fs->len > 0 ? "," : "",
3493 (guint) statbuf.st_gid);
3494 #ifndef MAJOR_MINOR_NOT_FOUND
3495 g_string_append_printf (fs, "%s" "rdev=%d:%d", fs->len > 0 ? "," : "",
3496 (gint) major (statbuf.st_rdev), (gint) minor (statbuf.st_rdev));
3497 #endif
3498 g_string_append_printf (fs, "%s" "size=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3499 (guint64) statbuf.st_size);
3500 g_string_append_printf (fs, "%s" "atime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3501 (guint64) statbuf.st_atime);
3502 g_string_append_printf (fs, "%s" "mtime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3503 (guint64) statbuf.st_mtime);
3504 g_string_append_printf (fs, "%s" "ctime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "",
3505 (guint64) statbuf.st_ctime);
3507 else
3509 int errsv = errno;
3510 g_string_append_printf (fs, "(fstat failed: %s)", g_strerror (errsv));
3512 g_string_append_printf (str, "%*s fd %d: %s\n", indent, "", fds[n], fs->str);
3513 g_string_free (fs, TRUE);
3516 else
3518 g_string_append_printf (str, "%*s (empty)\n", indent, "");
3521 else
3523 g_string_append_printf (str, "%*s (none)\n", indent, "");
3525 #endif
3527 return g_string_free (str, FALSE);
3531 * g_dbus_message_get_locked:
3532 * @message: A #GDBusMessage.
3534 * Checks whether @message is locked. To monitor changes to this
3535 * value, conncet to the #GObject::notify signal to listen for changes
3536 * on the #GDBusMessage:locked property.
3538 * Returns: %TRUE if @message is locked, %FALSE otherwise.
3540 * Since: 2.26
3542 gboolean
3543 g_dbus_message_get_locked (GDBusMessage *message)
3545 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
3546 return message->locked;
3550 * g_dbus_message_lock:
3551 * @message: A #GDBusMessage.
3553 * If @message is locked, does nothing. Otherwise locks the message.
3555 * Since: 2.26
3557 void
3558 g_dbus_message_lock (GDBusMessage *message)
3560 g_return_if_fail (G_IS_DBUS_MESSAGE (message));
3562 if (message->locked)
3563 goto out;
3565 message->locked = TRUE;
3566 g_object_notify (G_OBJECT (message), "locked");
3568 out:
3573 * g_dbus_message_copy:
3574 * @message: A #GDBusMessage.
3575 * @error: Return location for error or %NULL.
3577 * Copies @message. The copy is a deep copy and the returned
3578 * #GDBusMessage is completely identical except that it is guaranteed
3579 * to not be locked.
3581 * This operation can fail if e.g. @message contains file descriptors
3582 * and the per-process or system-wide open files limit is reached.
3584 * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set.
3585 * Free with g_object_unref().
3587 * Since: 2.26
3589 GDBusMessage *
3590 g_dbus_message_copy (GDBusMessage *message,
3591 GError **error)
3593 GDBusMessage *ret;
3594 GHashTableIter iter;
3595 gpointer header_key;
3596 GVariant *header_value;
3598 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
3599 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3601 ret = g_dbus_message_new ();
3602 ret->type = message->type;
3603 ret->flags = message->flags;
3604 ret->byte_order = message->byte_order;
3605 ret->major_protocol_version = message->major_protocol_version;
3606 ret->serial = message->serial;
3608 #ifdef G_OS_UNIX
3609 if (message->fd_list != NULL)
3611 gint n;
3612 gint num_fds;
3613 const gint *fds;
3615 ret->fd_list = g_unix_fd_list_new ();
3616 fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds);
3617 for (n = 0; n < num_fds; n++)
3619 if (g_unix_fd_list_append (ret->fd_list,
3620 fds[n],
3621 error) == -1)
3623 g_object_unref (ret);
3624 ret = NULL;
3625 goto out;
3629 #endif
3631 /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine
3632 * to just ref (as opposed to deep-copying) the GVariant instances
3634 ret->body = message->body != NULL ? g_variant_ref (message->body) : NULL;
3635 g_hash_table_iter_init (&iter, message->headers);
3636 while (g_hash_table_iter_next (&iter, &header_key, (gpointer) &header_value))
3637 g_hash_table_insert (ret->headers, header_key, g_variant_ref (header_value));
3639 #ifdef G_OS_UNIX
3640 out:
3641 #endif
3642 return ret;