/src/pango/subprojects/glib/gio/gdbusmessage.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GDBus - GLib D-Bus Library |
2 | | * |
3 | | * Copyright (C) 2008-2010 Red Hat, Inc. |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General |
18 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | * |
20 | | * Author: David Zeuthen <davidz@redhat.com> |
21 | | */ |
22 | | |
23 | | /* Uncomment to debug serializer code */ |
24 | | /* #define DEBUG_SERIALIZER */ |
25 | | |
26 | | #include "config.h" |
27 | | |
28 | | #include <string.h> |
29 | | #include <errno.h> |
30 | | #include <sys/types.h> |
31 | | #include <sys/stat.h> |
32 | | |
33 | | #if MAJOR_IN_MKDEV |
34 | | #include <sys/mkdev.h> |
35 | | #elif MAJOR_IN_SYSMACROS |
36 | | #include <sys/sysmacros.h> |
37 | | #elif MAJOR_IN_TYPES |
38 | | #include <sys/types.h> |
39 | | #else |
40 | | #define MAJOR_MINOR_NOT_FOUND 1 |
41 | | #endif |
42 | | |
43 | | #include "gdbusutils.h" |
44 | | #include "gdbusmessage.h" |
45 | | #include "gdbuserror.h" |
46 | | #include "gioenumtypes.h" |
47 | | #include "ginputstream.h" |
48 | | #include "gdatainputstream.h" |
49 | | #include "gmemoryinputstream.h" |
50 | | #include "goutputstream.h" |
51 | | #include "gdataoutputstream.h" |
52 | | #include "gmemoryoutputstream.h" |
53 | | #include "gseekable.h" |
54 | | #include "gioerror.h" |
55 | | #include "gdbusprivate.h" |
56 | | #include "gutilsprivate.h" |
57 | | |
58 | | #ifdef G_OS_UNIX |
59 | | #include "gunixfdlist.h" |
60 | | #endif |
61 | | |
62 | | #include "glibintl.h" |
63 | | |
64 | | /* See https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-signature |
65 | | * This is 64 containers plus 1 value within them. */ |
66 | 0 | #define G_DBUS_MAX_TYPE_DEPTH (64 + 1) |
67 | | |
68 | | typedef struct _GMemoryBuffer GMemoryBuffer; |
69 | | struct _GMemoryBuffer |
70 | | { |
71 | | gsize len; |
72 | | gsize valid_len; |
73 | | gsize pos; |
74 | | gchar *data; |
75 | | GDataStreamByteOrder byte_order; |
76 | | }; |
77 | | |
78 | | static gboolean |
79 | | g_memory_buffer_is_byteswapped (GMemoryBuffer *mbuf) |
80 | 0 | { |
81 | 0 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN |
82 | 0 | return mbuf->byte_order == G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN; |
83 | | #else |
84 | | return mbuf->byte_order == G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN; |
85 | | #endif |
86 | 0 | } |
87 | | |
88 | | static guchar |
89 | | g_memory_buffer_read_byte (GMemoryBuffer *mbuf, |
90 | | GError **error) |
91 | 0 | { |
92 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, 0); |
93 | | |
94 | 0 | if (mbuf->pos >= mbuf->valid_len) |
95 | 0 | { |
96 | 0 | g_set_error (error, |
97 | 0 | G_IO_ERROR, |
98 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
99 | 0 | "Unexpected end of message while reading byte."); |
100 | 0 | return 0; |
101 | 0 | } |
102 | 0 | return mbuf->data [mbuf->pos++]; |
103 | 0 | } |
104 | | |
105 | | static gint16 |
106 | | g_memory_buffer_read_int16 (GMemoryBuffer *mbuf, |
107 | | GError **error) |
108 | 0 | { |
109 | 0 | gint16 v; |
110 | |
|
111 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, -1); |
112 | | |
113 | 0 | if (mbuf->pos > mbuf->valid_len - 2) |
114 | 0 | { |
115 | 0 | g_set_error (error, |
116 | 0 | G_IO_ERROR, |
117 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
118 | 0 | "Unexpected end of message while reading int16."); |
119 | 0 | return 0; |
120 | 0 | } |
121 | | |
122 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 2); |
123 | 0 | mbuf->pos += 2; |
124 | |
|
125 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
126 | 0 | v = GUINT16_SWAP_LE_BE (v); |
127 | |
|
128 | 0 | return v; |
129 | 0 | } |
130 | | |
131 | | static guint16 |
132 | | g_memory_buffer_read_uint16 (GMemoryBuffer *mbuf, |
133 | | GError **error) |
134 | 0 | { |
135 | 0 | guint16 v; |
136 | |
|
137 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, 0); |
138 | | |
139 | 0 | if (mbuf->pos > mbuf->valid_len - 2) |
140 | 0 | { |
141 | 0 | g_set_error (error, |
142 | 0 | G_IO_ERROR, |
143 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
144 | 0 | "Unexpected end of message while reading uint16."); |
145 | 0 | return 0; |
146 | 0 | } |
147 | | |
148 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 2); |
149 | 0 | mbuf->pos += 2; |
150 | |
|
151 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
152 | 0 | v = GUINT16_SWAP_LE_BE (v); |
153 | |
|
154 | 0 | return v; |
155 | 0 | } |
156 | | |
157 | | static gint32 |
158 | | g_memory_buffer_read_int32 (GMemoryBuffer *mbuf, |
159 | | GError **error) |
160 | 0 | { |
161 | 0 | gint32 v; |
162 | |
|
163 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, -1); |
164 | | |
165 | 0 | if (mbuf->pos > mbuf->valid_len - 4) |
166 | 0 | { |
167 | 0 | g_set_error (error, |
168 | 0 | G_IO_ERROR, |
169 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
170 | 0 | "Unexpected end of message while reading int32."); |
171 | 0 | return 0; |
172 | 0 | } |
173 | | |
174 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 4); |
175 | 0 | mbuf->pos += 4; |
176 | |
|
177 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
178 | 0 | v = GUINT32_SWAP_LE_BE (v); |
179 | |
|
180 | 0 | return v; |
181 | 0 | } |
182 | | |
183 | | static guint32 |
184 | | g_memory_buffer_read_uint32 (GMemoryBuffer *mbuf, |
185 | | GError **error) |
186 | 0 | { |
187 | 0 | guint32 v; |
188 | |
|
189 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, 0); |
190 | | |
191 | 0 | if (mbuf->pos > mbuf->valid_len - 4) |
192 | 0 | { |
193 | 0 | g_set_error (error, |
194 | 0 | G_IO_ERROR, |
195 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
196 | 0 | "Unexpected end of message while reading uint32."); |
197 | 0 | return 0; |
198 | 0 | } |
199 | | |
200 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 4); |
201 | 0 | mbuf->pos += 4; |
202 | |
|
203 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
204 | 0 | v = GUINT32_SWAP_LE_BE (v); |
205 | |
|
206 | 0 | return v; |
207 | 0 | } |
208 | | |
209 | | static gint64 |
210 | | g_memory_buffer_read_int64 (GMemoryBuffer *mbuf, |
211 | | GError **error) |
212 | 0 | { |
213 | 0 | gint64 v; |
214 | |
|
215 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, -1); |
216 | | |
217 | 0 | if (mbuf->pos > mbuf->valid_len - 8) |
218 | 0 | { |
219 | 0 | g_set_error (error, |
220 | 0 | G_IO_ERROR, |
221 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
222 | 0 | "Unexpected end of message while reading int64."); |
223 | 0 | return 0; |
224 | 0 | } |
225 | | |
226 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 8); |
227 | 0 | mbuf->pos += 8; |
228 | |
|
229 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
230 | 0 | v = GUINT64_SWAP_LE_BE (v); |
231 | |
|
232 | 0 | return v; |
233 | 0 | } |
234 | | |
235 | | static guint64 |
236 | | g_memory_buffer_read_uint64 (GMemoryBuffer *mbuf, |
237 | | GError **error) |
238 | 0 | { |
239 | 0 | guint64 v; |
240 | |
|
241 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, 0); |
242 | | |
243 | 0 | if (mbuf->pos > mbuf->valid_len - 8) |
244 | 0 | { |
245 | 0 | g_set_error (error, |
246 | 0 | G_IO_ERROR, |
247 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
248 | 0 | "Unexpected end of message while reading uint64."); |
249 | 0 | return 0; |
250 | 0 | } |
251 | | |
252 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 8); |
253 | 0 | mbuf->pos += 8; |
254 | |
|
255 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
256 | 0 | v = GUINT64_SWAP_LE_BE (v); |
257 | |
|
258 | 0 | return v; |
259 | 0 | } |
260 | | |
261 | 0 | #define MIN_ARRAY_SIZE 128 |
262 | | |
263 | | static void |
264 | | array_resize (GMemoryBuffer *mbuf, |
265 | | gsize size) |
266 | 0 | { |
267 | 0 | gpointer data; |
268 | 0 | gsize len; |
269 | |
|
270 | 0 | if (mbuf->len == size) |
271 | 0 | return; |
272 | | |
273 | 0 | len = mbuf->len; |
274 | 0 | data = g_realloc (mbuf->data, size); |
275 | |
|
276 | 0 | if (size > len) |
277 | 0 | memset ((guint8 *)data + len, 0, size - len); |
278 | |
|
279 | 0 | mbuf->data = data; |
280 | 0 | mbuf->len = size; |
281 | |
|
282 | 0 | if (mbuf->len < mbuf->valid_len) |
283 | 0 | mbuf->valid_len = mbuf->len; |
284 | 0 | } |
285 | | |
286 | | static gboolean |
287 | | g_memory_buffer_write (GMemoryBuffer *mbuf, |
288 | | const void *buffer, |
289 | | gsize count) |
290 | 0 | { |
291 | 0 | guint8 *dest; |
292 | 0 | gsize new_size; |
293 | |
|
294 | 0 | if (count == 0) |
295 | 0 | return TRUE; |
296 | | |
297 | | /* Check for address space overflow, but only if the buffer is resizable. |
298 | | Otherwise we just do a short write and don't worry. */ |
299 | 0 | if (mbuf->pos + count < mbuf->pos) |
300 | 0 | return FALSE; |
301 | | |
302 | 0 | if (mbuf->pos + count > mbuf->len) |
303 | 0 | { |
304 | | /* At least enough to fit the write, rounded up |
305 | | for greater than linear growth. |
306 | | TODO: This wastes a lot of memory at large buffer sizes. |
307 | | Figure out a more rational allocation strategy. */ |
308 | 0 | new_size = g_nearest_pow (mbuf->pos + count); |
309 | | /* Check for overflow again. We have checked if |
310 | | pos + count > G_MAXSIZE, but now check if g_nearest_pow () has |
311 | | overflowed */ |
312 | 0 | if (new_size == 0) |
313 | 0 | return FALSE; |
314 | | |
315 | 0 | new_size = MAX (new_size, MIN_ARRAY_SIZE); |
316 | 0 | array_resize (mbuf, new_size); |
317 | 0 | } |
318 | | |
319 | 0 | dest = (guint8 *)mbuf->data + mbuf->pos; |
320 | 0 | memcpy (dest, buffer, count); |
321 | 0 | mbuf->pos += count; |
322 | |
|
323 | 0 | if (mbuf->pos > mbuf->valid_len) |
324 | 0 | mbuf->valid_len = mbuf->pos; |
325 | |
|
326 | 0 | return TRUE; |
327 | 0 | } |
328 | | |
329 | | static gboolean |
330 | | g_memory_buffer_put_byte (GMemoryBuffer *mbuf, |
331 | | guchar data) |
332 | 0 | { |
333 | 0 | return g_memory_buffer_write (mbuf, &data, 1); |
334 | 0 | } |
335 | | |
336 | | static gboolean |
337 | | g_memory_buffer_put_int16 (GMemoryBuffer *mbuf, |
338 | | gint16 data) |
339 | 0 | { |
340 | 0 | switch (mbuf->byte_order) |
341 | 0 | { |
342 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
343 | 0 | data = GINT16_TO_BE (data); |
344 | 0 | break; |
345 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
346 | 0 | data = GINT16_TO_LE (data); |
347 | 0 | break; |
348 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
349 | 0 | default: |
350 | 0 | break; |
351 | 0 | } |
352 | | |
353 | 0 | return g_memory_buffer_write (mbuf, &data, 2); |
354 | 0 | } |
355 | | |
356 | | static gboolean |
357 | | g_memory_buffer_put_uint16 (GMemoryBuffer *mbuf, |
358 | | guint16 data) |
359 | 0 | { |
360 | 0 | switch (mbuf->byte_order) |
361 | 0 | { |
362 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
363 | 0 | data = GUINT16_TO_BE (data); |
364 | 0 | break; |
365 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
366 | 0 | data = GUINT16_TO_LE (data); |
367 | 0 | break; |
368 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
369 | 0 | default: |
370 | 0 | break; |
371 | 0 | } |
372 | | |
373 | 0 | return g_memory_buffer_write (mbuf, &data, 2); |
374 | 0 | } |
375 | | |
376 | | static gboolean |
377 | | g_memory_buffer_put_int32 (GMemoryBuffer *mbuf, |
378 | | gint32 data) |
379 | 0 | { |
380 | 0 | switch (mbuf->byte_order) |
381 | 0 | { |
382 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
383 | 0 | data = GINT32_TO_BE (data); |
384 | 0 | break; |
385 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
386 | 0 | data = GINT32_TO_LE (data); |
387 | 0 | break; |
388 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
389 | 0 | default: |
390 | 0 | break; |
391 | 0 | } |
392 | | |
393 | 0 | return g_memory_buffer_write (mbuf, &data, 4); |
394 | 0 | } |
395 | | |
396 | | static gboolean |
397 | | g_memory_buffer_put_uint32 (GMemoryBuffer *mbuf, |
398 | | guint32 data) |
399 | 0 | { |
400 | 0 | switch (mbuf->byte_order) |
401 | 0 | { |
402 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
403 | 0 | data = GUINT32_TO_BE (data); |
404 | 0 | break; |
405 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
406 | 0 | data = GUINT32_TO_LE (data); |
407 | 0 | break; |
408 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
409 | 0 | default: |
410 | 0 | break; |
411 | 0 | } |
412 | | |
413 | 0 | return g_memory_buffer_write (mbuf, &data, 4); |
414 | 0 | } |
415 | | |
416 | | static gboolean |
417 | | g_memory_buffer_put_int64 (GMemoryBuffer *mbuf, |
418 | | gint64 data) |
419 | 0 | { |
420 | 0 | switch (mbuf->byte_order) |
421 | 0 | { |
422 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
423 | 0 | data = GINT64_TO_BE (data); |
424 | 0 | break; |
425 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
426 | 0 | data = GINT64_TO_LE (data); |
427 | 0 | break; |
428 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
429 | 0 | default: |
430 | 0 | break; |
431 | 0 | } |
432 | | |
433 | 0 | return g_memory_buffer_write (mbuf, &data, 8); |
434 | 0 | } |
435 | | |
436 | | static gboolean |
437 | | g_memory_buffer_put_uint64 (GMemoryBuffer *mbuf, |
438 | | guint64 data) |
439 | 0 | { |
440 | 0 | switch (mbuf->byte_order) |
441 | 0 | { |
442 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
443 | 0 | data = GUINT64_TO_BE (data); |
444 | 0 | break; |
445 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
446 | 0 | data = GUINT64_TO_LE (data); |
447 | 0 | break; |
448 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
449 | 0 | default: |
450 | 0 | break; |
451 | 0 | } |
452 | | |
453 | 0 | return g_memory_buffer_write (mbuf, &data, 8); |
454 | 0 | } |
455 | | |
456 | | static gboolean |
457 | | g_memory_buffer_put_string (GMemoryBuffer *mbuf, |
458 | | const char *str) |
459 | 0 | { |
460 | 0 | g_return_val_if_fail (str != NULL, FALSE); |
461 | | |
462 | 0 | return g_memory_buffer_write (mbuf, str, strlen (str)); |
463 | 0 | } |
464 | | |
465 | | typedef struct _GDBusMessageClass GDBusMessageClass; |
466 | | |
467 | | /** |
468 | | * GDBusMessageClass: |
469 | | * |
470 | | * Class structure for #GDBusMessage. |
471 | | * |
472 | | * Since: 2.26 |
473 | | */ |
474 | | struct _GDBusMessageClass |
475 | | { |
476 | | /*< private >*/ |
477 | | GObjectClass parent_class; |
478 | | }; |
479 | | |
480 | | /** |
481 | | * GDBusMessage: |
482 | | * |
483 | | * A type for representing D-Bus messages that can be sent or received |
484 | | * on a [class@Gio.DBusConnection]. |
485 | | * |
486 | | * Since: 2.26 |
487 | | */ |
488 | | struct _GDBusMessage |
489 | | { |
490 | | /*< private >*/ |
491 | | GObject parent_instance; |
492 | | |
493 | | GDBusMessageType type; |
494 | | GDBusMessageFlags flags; |
495 | | gboolean locked; |
496 | | GDBusMessageByteOrder byte_order; |
497 | | guchar major_protocol_version; |
498 | | guint32 serial; |
499 | | GHashTable *headers; |
500 | | GVariant *body; |
501 | | GVariant *arg0_cache; /* (nullable) (owned) */ |
502 | | #ifdef G_OS_UNIX |
503 | | GUnixFDList *fd_list; |
504 | | #endif |
505 | | }; |
506 | | |
507 | | enum |
508 | | { |
509 | | PROP_0, |
510 | | PROP_LOCKED |
511 | | }; |
512 | | |
513 | | G_DEFINE_TYPE (GDBusMessage, g_dbus_message, G_TYPE_OBJECT) |
514 | | |
515 | | static void |
516 | | g_dbus_message_finalize (GObject *object) |
517 | 0 | { |
518 | 0 | GDBusMessage *message = G_DBUS_MESSAGE (object); |
519 | |
|
520 | 0 | if (message->headers != NULL) |
521 | 0 | g_hash_table_unref (message->headers); |
522 | 0 | if (message->body != NULL) |
523 | 0 | g_variant_unref (message->body); |
524 | 0 | g_clear_pointer (&message->arg0_cache, g_variant_unref); |
525 | 0 | #ifdef G_OS_UNIX |
526 | 0 | if (message->fd_list != NULL) |
527 | 0 | g_object_unref (message->fd_list); |
528 | 0 | #endif |
529 | |
|
530 | 0 | if (G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize != NULL) |
531 | 0 | G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize (object); |
532 | 0 | } |
533 | | |
534 | | static void |
535 | | g_dbus_message_get_property (GObject *object, |
536 | | guint prop_id, |
537 | | GValue *value, |
538 | | GParamSpec *pspec) |
539 | 0 | { |
540 | 0 | GDBusMessage *message = G_DBUS_MESSAGE (object); |
541 | |
|
542 | 0 | switch (prop_id) |
543 | 0 | { |
544 | 0 | case PROP_LOCKED: |
545 | 0 | g_value_set_boolean (value, g_dbus_message_get_locked (message)); |
546 | 0 | break; |
547 | | |
548 | 0 | default: |
549 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
550 | 0 | break; |
551 | 0 | } |
552 | 0 | } |
553 | | |
554 | | static void |
555 | | g_dbus_message_class_init (GDBusMessageClass *klass) |
556 | 0 | { |
557 | 0 | GObjectClass *gobject_class; |
558 | |
|
559 | 0 | gobject_class = G_OBJECT_CLASS (klass); |
560 | 0 | gobject_class->finalize = g_dbus_message_finalize; |
561 | 0 | gobject_class->get_property = g_dbus_message_get_property; |
562 | | |
563 | | /** |
564 | | * GDBusConnection:locked: |
565 | | * |
566 | | * A boolean specifying whether the message is locked. |
567 | | * |
568 | | * Since: 2.26 |
569 | | */ |
570 | 0 | g_object_class_install_property (gobject_class, |
571 | 0 | PROP_LOCKED, |
572 | 0 | g_param_spec_boolean ("locked", NULL, NULL, |
573 | 0 | FALSE, |
574 | 0 | G_PARAM_READABLE | |
575 | 0 | G_PARAM_STATIC_NAME | |
576 | 0 | G_PARAM_STATIC_BLURB | |
577 | 0 | G_PARAM_STATIC_NICK)); |
578 | 0 | } |
579 | | |
580 | | static void |
581 | | g_dbus_message_init (GDBusMessage *message) |
582 | 0 | { |
583 | | /* Any D-Bus implementation is supposed to handle both Big and |
584 | | * Little Endian encodings and the Endianness is part of the D-Bus |
585 | | * message - we prefer to use Big Endian (since it's Network Byte |
586 | | * Order and just easier to read for humans) but if the machine is |
587 | | * Little Endian we use that for performance reasons. |
588 | | */ |
589 | 0 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN |
590 | 0 | message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN; |
591 | | #else |
592 | | /* this could also be G_PDP_ENDIAN */ |
593 | | message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN; |
594 | | #endif |
595 | 0 | message->headers = g_hash_table_new_full (g_direct_hash, |
596 | 0 | g_direct_equal, |
597 | 0 | NULL, |
598 | 0 | (GDestroyNotify) g_variant_unref); |
599 | 0 | } |
600 | | |
601 | | /** |
602 | | * g_dbus_message_new: |
603 | | * |
604 | | * Creates a new empty #GDBusMessage. |
605 | | * |
606 | | * Returns: A #GDBusMessage. Free with g_object_unref(). |
607 | | * |
608 | | * Since: 2.26 |
609 | | */ |
610 | | GDBusMessage * |
611 | | g_dbus_message_new (void) |
612 | 0 | { |
613 | 0 | return g_object_new (G_TYPE_DBUS_MESSAGE, NULL); |
614 | 0 | } |
615 | | |
616 | | /** |
617 | | * g_dbus_message_new_method_call: |
618 | | * @name: (nullable): A valid D-Bus name or %NULL. |
619 | | * @path: A valid object path. |
620 | | * @interface_: (nullable): A valid D-Bus interface name or %NULL. |
621 | | * @method: A valid method name. |
622 | | * |
623 | | * Creates a new #GDBusMessage for a method call. |
624 | | * |
625 | | * Returns: A #GDBusMessage. Free with g_object_unref(). |
626 | | * |
627 | | * Since: 2.26 |
628 | | */ |
629 | | GDBusMessage * |
630 | | g_dbus_message_new_method_call (const gchar *name, |
631 | | const gchar *path, |
632 | | const gchar *interface_, |
633 | | const gchar *method) |
634 | 0 | { |
635 | 0 | GDBusMessage *message; |
636 | |
|
637 | 0 | g_return_val_if_fail (name == NULL || g_dbus_is_name (name), NULL); |
638 | 0 | g_return_val_if_fail (g_variant_is_object_path (path), NULL); |
639 | 0 | g_return_val_if_fail (g_dbus_is_member_name (method), NULL); |
640 | 0 | g_return_val_if_fail (interface_ == NULL || g_dbus_is_interface_name (interface_), NULL); |
641 | | |
642 | 0 | message = g_dbus_message_new (); |
643 | 0 | message->type = G_DBUS_MESSAGE_TYPE_METHOD_CALL; |
644 | |
|
645 | 0 | if (name != NULL) |
646 | 0 | g_dbus_message_set_destination (message, name); |
647 | 0 | g_dbus_message_set_path (message, path); |
648 | 0 | g_dbus_message_set_member (message, method); |
649 | 0 | if (interface_ != NULL) |
650 | 0 | g_dbus_message_set_interface (message, interface_); |
651 | |
|
652 | 0 | return message; |
653 | 0 | } |
654 | | |
655 | | /** |
656 | | * g_dbus_message_new_signal: |
657 | | * @path: A valid object path. |
658 | | * @interface_: A valid D-Bus interface name. |
659 | | * @signal: A valid signal name. |
660 | | * |
661 | | * Creates a new #GDBusMessage for a signal emission. |
662 | | * |
663 | | * Returns: A #GDBusMessage. Free with g_object_unref(). |
664 | | * |
665 | | * Since: 2.26 |
666 | | */ |
667 | | GDBusMessage * |
668 | | g_dbus_message_new_signal (const gchar *path, |
669 | | const gchar *interface_, |
670 | | const gchar *signal) |
671 | 0 | { |
672 | 0 | GDBusMessage *message; |
673 | |
|
674 | 0 | g_return_val_if_fail (g_variant_is_object_path (path), NULL); |
675 | 0 | g_return_val_if_fail (g_dbus_is_member_name (signal), NULL); |
676 | 0 | g_return_val_if_fail (g_dbus_is_interface_name (interface_), NULL); |
677 | | |
678 | 0 | message = g_dbus_message_new (); |
679 | 0 | message->type = G_DBUS_MESSAGE_TYPE_SIGNAL; |
680 | 0 | message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED; |
681 | |
|
682 | 0 | g_dbus_message_set_path (message, path); |
683 | 0 | g_dbus_message_set_member (message, signal); |
684 | 0 | g_dbus_message_set_interface (message, interface_); |
685 | |
|
686 | 0 | return message; |
687 | 0 | } |
688 | | |
689 | | |
690 | | /** |
691 | | * g_dbus_message_new_method_reply: |
692 | | * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to |
693 | | * create a reply message to. |
694 | | * |
695 | | * Creates a new #GDBusMessage that is a reply to @method_call_message. |
696 | | * |
697 | | * Returns: (transfer full): #GDBusMessage. Free with g_object_unref(). |
698 | | * |
699 | | * Since: 2.26 |
700 | | */ |
701 | | GDBusMessage * |
702 | | g_dbus_message_new_method_reply (GDBusMessage *method_call_message) |
703 | 0 | { |
704 | 0 | GDBusMessage *message; |
705 | 0 | const gchar *sender; |
706 | |
|
707 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL); |
708 | 0 | g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL); |
709 | 0 | g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL); |
710 | | |
711 | 0 | message = g_dbus_message_new (); |
712 | 0 | message->type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN; |
713 | 0 | message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED; |
714 | | /* reply with same endianness */ |
715 | 0 | message->byte_order = method_call_message->byte_order; |
716 | |
|
717 | 0 | g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message)); |
718 | 0 | sender = g_dbus_message_get_sender (method_call_message); |
719 | 0 | if (sender != NULL) |
720 | 0 | g_dbus_message_set_destination (message, sender); |
721 | |
|
722 | 0 | return message; |
723 | 0 | } |
724 | | |
725 | | /** |
726 | | * g_dbus_message_new_method_error: |
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_format: The D-Bus error message in a printf() format. |
731 | | * @...: Arguments for @error_message_format. |
732 | | * |
733 | | * Creates a new #GDBusMessage that is an error reply to @method_call_message. |
734 | | * |
735 | | * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref(). |
736 | | * |
737 | | * Since: 2.26 |
738 | | */ |
739 | | GDBusMessage * |
740 | | g_dbus_message_new_method_error (GDBusMessage *method_call_message, |
741 | | const gchar *error_name, |
742 | | const gchar *error_message_format, |
743 | | ...) |
744 | 0 | { |
745 | 0 | GDBusMessage *ret; |
746 | 0 | va_list var_args; |
747 | |
|
748 | 0 | va_start (var_args, error_message_format); |
749 | 0 | ret = g_dbus_message_new_method_error_valist (method_call_message, |
750 | 0 | error_name, |
751 | 0 | error_message_format, |
752 | 0 | var_args); |
753 | 0 | va_end (var_args); |
754 | |
|
755 | 0 | return ret; |
756 | 0 | } |
757 | | |
758 | | /** |
759 | | * g_dbus_message_new_method_error_literal: |
760 | | * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to |
761 | | * create a reply message to. |
762 | | * @error_name: A valid D-Bus error name. |
763 | | * @error_message: The D-Bus error message. |
764 | | * |
765 | | * Creates a new #GDBusMessage that is an error reply to @method_call_message. |
766 | | * |
767 | | * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref(). |
768 | | * |
769 | | * Since: 2.26 |
770 | | */ |
771 | | GDBusMessage * |
772 | | g_dbus_message_new_method_error_literal (GDBusMessage *method_call_message, |
773 | | const gchar *error_name, |
774 | | const gchar *error_message) |
775 | 0 | { |
776 | 0 | GDBusMessage *message; |
777 | 0 | const gchar *sender; |
778 | |
|
779 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL); |
780 | 0 | g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL); |
781 | 0 | g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL); |
782 | 0 | g_return_val_if_fail (g_dbus_is_name (error_name), NULL); |
783 | 0 | g_return_val_if_fail (error_message != NULL, NULL); |
784 | | |
785 | 0 | message = g_dbus_message_new (); |
786 | 0 | message->type = G_DBUS_MESSAGE_TYPE_ERROR; |
787 | 0 | message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED; |
788 | | /* reply with same endianness */ |
789 | 0 | message->byte_order = method_call_message->byte_order; |
790 | |
|
791 | 0 | g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message)); |
792 | 0 | g_dbus_message_set_error_name (message, error_name); |
793 | 0 | g_dbus_message_set_body (message, g_variant_new ("(s)", error_message)); |
794 | |
|
795 | 0 | sender = g_dbus_message_get_sender (method_call_message); |
796 | 0 | if (sender != NULL) |
797 | 0 | g_dbus_message_set_destination (message, sender); |
798 | |
|
799 | 0 | return message; |
800 | 0 | } |
801 | | |
802 | | /** |
803 | | * g_dbus_message_new_method_error_valist: |
804 | | * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to |
805 | | * create a reply message to. |
806 | | * @error_name: A valid D-Bus error name. |
807 | | * @error_message_format: The D-Bus error message in a printf() format. |
808 | | * @var_args: Arguments for @error_message_format. |
809 | | * |
810 | | * Like g_dbus_message_new_method_error() but intended for language bindings. |
811 | | * |
812 | | * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref(). |
813 | | * |
814 | | * Since: 2.26 |
815 | | */ |
816 | | G_GNUC_PRINTF(3, 0) |
817 | | GDBusMessage * |
818 | | g_dbus_message_new_method_error_valist (GDBusMessage *method_call_message, |
819 | | const gchar *error_name, |
820 | | const gchar *error_message_format, |
821 | | va_list var_args) |
822 | 0 | { |
823 | 0 | GDBusMessage *ret; |
824 | 0 | gchar *error_message; |
825 | 0 | error_message = g_strdup_vprintf (error_message_format, var_args); |
826 | 0 | ret = g_dbus_message_new_method_error_literal (method_call_message, |
827 | 0 | error_name, |
828 | 0 | error_message); |
829 | 0 | g_free (error_message); |
830 | 0 | return ret; |
831 | 0 | } |
832 | | |
833 | | /* ---------------------------------------------------------------------------------------------------- */ |
834 | | |
835 | | /** |
836 | | * g_dbus_message_get_byte_order: |
837 | | * @message: A #GDBusMessage. |
838 | | * |
839 | | * Gets the byte order of @message. |
840 | | * |
841 | | * Returns: The byte order. |
842 | | */ |
843 | | GDBusMessageByteOrder |
844 | | g_dbus_message_get_byte_order (GDBusMessage *message) |
845 | 0 | { |
846 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), (GDBusMessageByteOrder) 0); |
847 | 0 | return message->byte_order; |
848 | 0 | } |
849 | | |
850 | | /** |
851 | | * g_dbus_message_set_byte_order: |
852 | | * @message: A #GDBusMessage. |
853 | | * @byte_order: The byte order. |
854 | | * |
855 | | * Sets the byte order of @message. |
856 | | */ |
857 | | void |
858 | | g_dbus_message_set_byte_order (GDBusMessage *message, |
859 | | GDBusMessageByteOrder byte_order) |
860 | 0 | { |
861 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
862 | | |
863 | 0 | if (message->locked) |
864 | 0 | { |
865 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
866 | 0 | return; |
867 | 0 | } |
868 | | |
869 | 0 | message->byte_order = byte_order; |
870 | 0 | } |
871 | | |
872 | | /* ---------------------------------------------------------------------------------------------------- */ |
873 | | |
874 | | /* TODO: need GI annotations to specify that any guchar value goes for the type */ |
875 | | |
876 | | /** |
877 | | * g_dbus_message_get_message_type: |
878 | | * @message: A #GDBusMessage. |
879 | | * |
880 | | * Gets the type of @message. |
881 | | * |
882 | | * Returns: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration). |
883 | | * |
884 | | * Since: 2.26 |
885 | | */ |
886 | | GDBusMessageType |
887 | | g_dbus_message_get_message_type (GDBusMessage *message) |
888 | 0 | { |
889 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_TYPE_INVALID); |
890 | 0 | return message->type; |
891 | 0 | } |
892 | | |
893 | | /** |
894 | | * g_dbus_message_set_message_type: |
895 | | * @message: A #GDBusMessage. |
896 | | * @type: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration). |
897 | | * |
898 | | * Sets @message to be of @type. |
899 | | * |
900 | | * Since: 2.26 |
901 | | */ |
902 | | void |
903 | | g_dbus_message_set_message_type (GDBusMessage *message, |
904 | | GDBusMessageType type) |
905 | 0 | { |
906 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
907 | 0 | g_return_if_fail ((guint) type < 256); |
908 | | |
909 | 0 | if (message->locked) |
910 | 0 | { |
911 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
912 | 0 | return; |
913 | 0 | } |
914 | | |
915 | 0 | message->type = type; |
916 | 0 | } |
917 | | |
918 | | /* ---------------------------------------------------------------------------------------------------- */ |
919 | | |
920 | | /* TODO: need GI annotations to specify that any guchar value goes for flags */ |
921 | | |
922 | | /** |
923 | | * g_dbus_message_get_flags: |
924 | | * @message: A #GDBusMessage. |
925 | | * |
926 | | * Gets the flags for @message. |
927 | | * |
928 | | * Returns: Flags that are set (typically values from the #GDBusMessageFlags enumeration bitwise ORed together). |
929 | | * |
930 | | * Since: 2.26 |
931 | | */ |
932 | | GDBusMessageFlags |
933 | | g_dbus_message_get_flags (GDBusMessage *message) |
934 | 0 | { |
935 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_FLAGS_NONE); |
936 | 0 | return message->flags; |
937 | 0 | } |
938 | | |
939 | | /** |
940 | | * g_dbus_message_set_flags: |
941 | | * @message: A #GDBusMessage. |
942 | | * @flags: Flags for @message that are set (typically values from the #GDBusMessageFlags |
943 | | * enumeration bitwise ORed together). |
944 | | * |
945 | | * Sets the flags to set on @message. |
946 | | * |
947 | | * Since: 2.26 |
948 | | */ |
949 | | void |
950 | | g_dbus_message_set_flags (GDBusMessage *message, |
951 | | GDBusMessageFlags flags) |
952 | 0 | { |
953 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
954 | 0 | g_return_if_fail ((guint) flags < 256); |
955 | | |
956 | 0 | if (message->locked) |
957 | 0 | { |
958 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
959 | 0 | return; |
960 | 0 | } |
961 | | |
962 | 0 | message->flags = flags; |
963 | 0 | } |
964 | | |
965 | | /* ---------------------------------------------------------------------------------------------------- */ |
966 | | |
967 | | /** |
968 | | * g_dbus_message_get_serial: |
969 | | * @message: A #GDBusMessage. |
970 | | * |
971 | | * Gets the serial for @message. |
972 | | * |
973 | | * Returns: A #guint32. |
974 | | * |
975 | | * Since: 2.26 |
976 | | */ |
977 | | guint32 |
978 | | g_dbus_message_get_serial (GDBusMessage *message) |
979 | 0 | { |
980 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0); |
981 | 0 | return message->serial; |
982 | 0 | } |
983 | | |
984 | | /** |
985 | | * g_dbus_message_set_serial: |
986 | | * @message: A #GDBusMessage. |
987 | | * @serial: A #guint32, which must not be zero. |
988 | | * |
989 | | * Sets the serial for @message. |
990 | | * |
991 | | * The [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages) |
992 | | * does not allow the @serial to be zero. |
993 | | * |
994 | | * Since: 2.26 |
995 | | */ |
996 | | void |
997 | | g_dbus_message_set_serial (GDBusMessage *message, |
998 | | guint32 serial) |
999 | 0 | { |
1000 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1001 | | |
1002 | | /* As per https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages, |
1003 | | * this must not be zero. */ |
1004 | 0 | g_return_if_fail (serial != 0); |
1005 | | |
1006 | 0 | if (message->locked) |
1007 | 0 | { |
1008 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1009 | 0 | return; |
1010 | 0 | } |
1011 | | |
1012 | 0 | message->serial = serial; |
1013 | 0 | } |
1014 | | |
1015 | | /* ---------------------------------------------------------------------------------------------------- */ |
1016 | | |
1017 | | /* TODO: need GI annotations to specify that any guchar value goes for header_field */ |
1018 | | |
1019 | | /** |
1020 | | * g_dbus_message_get_header: |
1021 | | * @message: A #GDBusMessage. |
1022 | | * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration) |
1023 | | * |
1024 | | * Gets a header field on @message. |
1025 | | * |
1026 | | * The caller is responsible for checking the type of the returned #GVariant |
1027 | | * matches what is expected. |
1028 | | * |
1029 | | * Returns: (transfer none) (nullable): A #GVariant with the value if the header was found, %NULL |
1030 | | * otherwise. Do not free, it is owned by @message. |
1031 | | * |
1032 | | * Since: 2.26 |
1033 | | */ |
1034 | | GVariant * |
1035 | | g_dbus_message_get_header (GDBusMessage *message, |
1036 | | GDBusMessageHeaderField header_field) |
1037 | 0 | { |
1038 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1039 | 0 | g_return_val_if_fail ((guint) header_field < 256, NULL); |
1040 | 0 | return g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
1041 | 0 | } |
1042 | | |
1043 | | /** |
1044 | | * g_dbus_message_set_header: |
1045 | | * @message: A #GDBusMessage. |
1046 | | * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration) |
1047 | | * @value: (nullable): A #GVariant to set the header field or %NULL to clear the header field. |
1048 | | * |
1049 | | * Sets a header field on @message. |
1050 | | * |
1051 | | * If @value is floating, @message assumes ownership of @value. |
1052 | | * |
1053 | | * Since: 2.26 |
1054 | | */ |
1055 | | void |
1056 | | g_dbus_message_set_header (GDBusMessage *message, |
1057 | | GDBusMessageHeaderField header_field, |
1058 | | GVariant *value) |
1059 | 0 | { |
1060 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1061 | 0 | g_return_if_fail ((guint) header_field < 256); |
1062 | | |
1063 | 0 | if (message->locked) |
1064 | 0 | { |
1065 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1066 | 0 | return; |
1067 | 0 | } |
1068 | | |
1069 | 0 | if (value == NULL) |
1070 | 0 | { |
1071 | 0 | g_hash_table_remove (message->headers, GUINT_TO_POINTER (header_field)); |
1072 | 0 | } |
1073 | 0 | else |
1074 | 0 | { |
1075 | 0 | g_hash_table_insert (message->headers, GUINT_TO_POINTER (header_field), g_variant_ref_sink (value)); |
1076 | 0 | } |
1077 | 0 | } |
1078 | | |
1079 | | /** |
1080 | | * g_dbus_message_get_header_fields: |
1081 | | * @message: A #GDBusMessage. |
1082 | | * |
1083 | | * Gets an array of all header fields on @message that are set. |
1084 | | * |
1085 | | * Returns: (array zero-terminated=1): An array of header fields |
1086 | | * terminated by %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element |
1087 | | * is a #guchar. Free with g_free(). |
1088 | | * |
1089 | | * Since: 2.26 |
1090 | | */ |
1091 | | guchar * |
1092 | | g_dbus_message_get_header_fields (GDBusMessage *message) |
1093 | 0 | { |
1094 | 0 | GPtrArray *keys; |
1095 | 0 | GArray *array; |
1096 | |
|
1097 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1098 | | |
1099 | 0 | keys = g_hash_table_get_keys_as_ptr_array (message->headers); |
1100 | 0 | array = g_array_sized_new (FALSE, FALSE, sizeof (guchar), keys->len + 1); |
1101 | |
|
1102 | 0 | for (guint i = 0; i < keys->len; ++i) |
1103 | 0 | { |
1104 | 0 | guchar val = GPOINTER_TO_UINT (g_ptr_array_index (keys, i)); |
1105 | 0 | g_array_append_val (array, val); |
1106 | 0 | } |
1107 | |
|
1108 | 0 | g_assert (array->len == keys->len); |
1109 | 0 | g_clear_pointer (&keys, g_ptr_array_unref); |
1110 | |
|
1111 | 0 | guchar invalid_field = G_DBUS_MESSAGE_HEADER_FIELD_INVALID; |
1112 | 0 | g_array_append_val (array, invalid_field); |
1113 | |
|
1114 | 0 | return (guchar *) g_array_free (array, FALSE); |
1115 | 0 | } |
1116 | | |
1117 | | /* ---------------------------------------------------------------------------------------------------- */ |
1118 | | |
1119 | | /** |
1120 | | * g_dbus_message_get_body: |
1121 | | * @message: A #GDBusMessage. |
1122 | | * |
1123 | | * Gets the body of a message. |
1124 | | * |
1125 | | * Returns: (nullable) (transfer none): A #GVariant or %NULL if the body is |
1126 | | * empty. Do not free, it is owned by @message. |
1127 | | * |
1128 | | * Since: 2.26 |
1129 | | */ |
1130 | | GVariant * |
1131 | | g_dbus_message_get_body (GDBusMessage *message) |
1132 | 0 | { |
1133 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1134 | 0 | return message->body; |
1135 | 0 | } |
1136 | | |
1137 | | /** |
1138 | | * g_dbus_message_set_body: |
1139 | | * @message: A #GDBusMessage. |
1140 | | * @body: Either %NULL or a #GVariant that is a tuple. |
1141 | | * |
1142 | | * Sets the body @message. As a side-effect the |
1143 | | * %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field is set to the |
1144 | | * type string of @body (or cleared if @body is %NULL). |
1145 | | * |
1146 | | * If @body is floating, @message assumes ownership of @body. |
1147 | | * |
1148 | | * Since: 2.26 |
1149 | | */ |
1150 | | void |
1151 | | g_dbus_message_set_body (GDBusMessage *message, |
1152 | | GVariant *body) |
1153 | 0 | { |
1154 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1155 | 0 | g_return_if_fail ((body == NULL) || g_variant_is_of_type (body, G_VARIANT_TYPE_TUPLE)); |
1156 | | |
1157 | 0 | if (message->locked) |
1158 | 0 | { |
1159 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1160 | 0 | return; |
1161 | 0 | } |
1162 | | |
1163 | 0 | if (message->body != NULL) |
1164 | 0 | g_variant_unref (message->body); |
1165 | |
|
1166 | 0 | g_clear_pointer (&message->arg0_cache, g_variant_unref); |
1167 | |
|
1168 | 0 | if (body == NULL) |
1169 | 0 | { |
1170 | 0 | message->body = NULL; |
1171 | 0 | g_dbus_message_set_signature (message, NULL); |
1172 | 0 | } |
1173 | 0 | else |
1174 | 0 | { |
1175 | 0 | const gchar *type_string; |
1176 | 0 | gsize type_string_len; |
1177 | 0 | gchar *signature; |
1178 | |
|
1179 | 0 | message->body = g_variant_ref_sink (body); |
1180 | |
|
1181 | 0 | if (g_variant_is_of_type (message->body, G_VARIANT_TYPE_TUPLE) && |
1182 | 0 | g_variant_n_children (message->body) > 0) |
1183 | 0 | message->arg0_cache = g_variant_get_child_value (message->body, 0); |
1184 | |
|
1185 | 0 | type_string = g_variant_get_type_string (body); |
1186 | 0 | type_string_len = strlen (type_string); |
1187 | 0 | g_assert (type_string_len >= 2); |
1188 | 0 | signature = g_strndup (type_string + 1, type_string_len - 2); |
1189 | 0 | g_dbus_message_set_signature (message, signature); |
1190 | 0 | g_free (signature); |
1191 | 0 | } |
1192 | 0 | } |
1193 | | |
1194 | | /* ---------------------------------------------------------------------------------------------------- */ |
1195 | | |
1196 | | #ifdef G_OS_UNIX |
1197 | | /** |
1198 | | * g_dbus_message_get_unix_fd_list: |
1199 | | * @message: A #GDBusMessage. |
1200 | | * |
1201 | | * Gets the UNIX file descriptors associated with @message, if any. |
1202 | | * |
1203 | | * This method is only available on UNIX. |
1204 | | * |
1205 | | * The file descriptors normally correspond to %G_VARIANT_TYPE_HANDLE |
1206 | | * values in the body of the message. For example, |
1207 | | * if g_variant_get_handle() returns 5, that is intended to be a reference |
1208 | | * to the file descriptor that can be accessed by |
1209 | | * `g_unix_fd_list_get (list, 5, ...)`. |
1210 | | * |
1211 | | * Returns: (nullable) (transfer none): A #GUnixFDList or %NULL if no file descriptors are |
1212 | | * associated. Do not free, this object is owned by @message. |
1213 | | * |
1214 | | * Since: 2.26 |
1215 | | */ |
1216 | | GUnixFDList * |
1217 | | g_dbus_message_get_unix_fd_list (GDBusMessage *message) |
1218 | 0 | { |
1219 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1220 | 0 | return message->fd_list; |
1221 | 0 | } |
1222 | | |
1223 | | /** |
1224 | | * g_dbus_message_set_unix_fd_list: |
1225 | | * @message: A #GDBusMessage. |
1226 | | * @fd_list: (nullable): A #GUnixFDList or %NULL. |
1227 | | * |
1228 | | * Sets the UNIX file descriptors associated with @message. As a |
1229 | | * side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header |
1230 | | * field is set to the number of fds in @fd_list (or cleared if |
1231 | | * @fd_list is %NULL). |
1232 | | * |
1233 | | * This method is only available on UNIX. |
1234 | | * |
1235 | | * When designing D-Bus APIs that are intended to be interoperable, |
1236 | | * please note that non-GDBus implementations of D-Bus can usually only |
1237 | | * access file descriptors if they are referenced by a value of type |
1238 | | * %G_VARIANT_TYPE_HANDLE in the body of the message. |
1239 | | * |
1240 | | * Since: 2.26 |
1241 | | */ |
1242 | | void |
1243 | | g_dbus_message_set_unix_fd_list (GDBusMessage *message, |
1244 | | GUnixFDList *fd_list) |
1245 | 0 | { |
1246 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1247 | 0 | g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list)); |
1248 | | |
1249 | 0 | if (message->locked) |
1250 | 0 | { |
1251 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1252 | 0 | return; |
1253 | 0 | } |
1254 | | |
1255 | 0 | if (message->fd_list != NULL) |
1256 | 0 | g_object_unref (message->fd_list); |
1257 | 0 | if (fd_list != NULL) |
1258 | 0 | { |
1259 | 0 | message->fd_list = g_object_ref (fd_list); |
1260 | 0 | g_dbus_message_set_num_unix_fds (message, g_unix_fd_list_get_length (fd_list)); |
1261 | 0 | } |
1262 | 0 | else |
1263 | 0 | { |
1264 | 0 | message->fd_list = NULL; |
1265 | 0 | g_dbus_message_set_num_unix_fds (message, 0); |
1266 | 0 | } |
1267 | 0 | } |
1268 | | #endif |
1269 | | |
1270 | | /* ---------------------------------------------------------------------------------------------------- */ |
1271 | | |
1272 | | static guint |
1273 | | get_type_fixed_size (const GVariantType *type) |
1274 | 0 | { |
1275 | | /* NB: we do not treat 'b' as fixed-size here because GVariant and |
1276 | | * D-Bus disagree about the size. |
1277 | | */ |
1278 | 0 | switch (*g_variant_type_peek_string (type)) |
1279 | 0 | { |
1280 | 0 | case 'y': |
1281 | 0 | return 1; |
1282 | 0 | case 'n': case 'q': |
1283 | 0 | return 2; |
1284 | 0 | case 'i': case 'u': case 'h': |
1285 | 0 | return 4; |
1286 | 0 | case 'x': case 't': case 'd': |
1287 | 0 | return 8; |
1288 | 0 | default: |
1289 | 0 | return 0; |
1290 | 0 | } |
1291 | 0 | } |
1292 | | |
1293 | | static const char * |
1294 | | message_type_to_string (GDBusMessageType message_type) |
1295 | 0 | { |
1296 | 0 | switch (message_type) |
1297 | 0 | { |
1298 | 0 | case G_DBUS_MESSAGE_TYPE_INVALID: |
1299 | 0 | return "INVALID"; |
1300 | 0 | case G_DBUS_MESSAGE_TYPE_METHOD_CALL: |
1301 | 0 | return "METHOD_CALL"; |
1302 | 0 | case G_DBUS_MESSAGE_TYPE_METHOD_RETURN: |
1303 | 0 | return "METHOD_RETURN"; |
1304 | 0 | case G_DBUS_MESSAGE_TYPE_ERROR: |
1305 | 0 | return "ERROR"; |
1306 | 0 | case G_DBUS_MESSAGE_TYPE_SIGNAL: |
1307 | 0 | return "SIGNAL"; |
1308 | 0 | default: |
1309 | 0 | return "unknown-type"; |
1310 | 0 | } |
1311 | 0 | } |
1312 | | |
1313 | | static const char * |
1314 | | message_header_field_to_string (GDBusMessageHeaderField field) |
1315 | 0 | { |
1316 | 0 | switch (field) |
1317 | 0 | { |
1318 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_INVALID: |
1319 | 0 | return "INVALID"; |
1320 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_PATH: |
1321 | 0 | return "PATH"; |
1322 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE: |
1323 | 0 | return "INTERFACE"; |
1324 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_MEMBER: |
1325 | 0 | return "MEMBER"; |
1326 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME: |
1327 | 0 | return "ERROR_NAME"; |
1328 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL: |
1329 | 0 | return "REPLY_SERIAL"; |
1330 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION: |
1331 | 0 | return "DESTINATION"; |
1332 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_SENDER: |
1333 | 0 | return "SENDER"; |
1334 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE: |
1335 | 0 | return "SIGNATURE"; |
1336 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS: |
1337 | 0 | return "NUM_UNIX_FDS"; |
1338 | 0 | default: |
1339 | 0 | return "unknown-field"; |
1340 | 0 | } |
1341 | 0 | } |
1342 | | |
1343 | | static gboolean |
1344 | | validate_header (GDBusMessage *message, |
1345 | | GDBusMessageHeaderField field, |
1346 | | GVariant *header_value, |
1347 | | const GVariantType *expected_type, |
1348 | | GError **error) |
1349 | 0 | { |
1350 | 0 | g_assert (header_value != NULL); |
1351 | | |
1352 | 0 | if (!g_variant_is_of_type (header_value, expected_type)) |
1353 | 0 | { |
1354 | 0 | char *expected_type_string = g_variant_type_dup_string (expected_type); |
1355 | 0 | g_set_error (error, |
1356 | 0 | G_IO_ERROR, |
1357 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1358 | 0 | _("%s message: %s header field is invalid; expected a value of type ‘%s’"), |
1359 | 0 | message_type_to_string (message->type), |
1360 | 0 | message_header_field_to_string (field), |
1361 | 0 | expected_type_string); |
1362 | 0 | g_free (expected_type_string); |
1363 | 0 | return FALSE; |
1364 | 0 | } |
1365 | | |
1366 | 0 | return TRUE; |
1367 | 0 | } |
1368 | | |
1369 | | static gboolean |
1370 | | require_header (GDBusMessage *message, |
1371 | | GDBusMessageHeaderField field, |
1372 | | GError **error) |
1373 | 0 | { |
1374 | 0 | GVariant *header_value = g_dbus_message_get_header (message, field); |
1375 | |
|
1376 | 0 | if (header_value == NULL) |
1377 | 0 | { |
1378 | 0 | g_set_error (error, |
1379 | 0 | G_IO_ERROR, |
1380 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1381 | 0 | _("%s message: %s header field is missing or invalid"), |
1382 | 0 | message_type_to_string (message->type), |
1383 | 0 | message_header_field_to_string (field)); |
1384 | 0 | return FALSE; |
1385 | 0 | } |
1386 | | |
1387 | 0 | return TRUE; |
1388 | 0 | } |
1389 | | |
1390 | | /* Implement the validation rules given in |
1391 | | * https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-header-fields */ |
1392 | | static gboolean |
1393 | | validate_headers (GDBusMessage *message, |
1394 | | GError **error) |
1395 | 0 | { |
1396 | 0 | gboolean ret; |
1397 | 0 | GHashTableIter headers_iter; |
1398 | 0 | gpointer key; |
1399 | 0 | GVariant *header_value; |
1400 | |
|
1401 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
1402 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
1403 | | |
1404 | 0 | ret = FALSE; |
1405 | | |
1406 | | /* Validate the types of all known headers. */ |
1407 | 0 | g_hash_table_iter_init (&headers_iter, message->headers); |
1408 | 0 | while (g_hash_table_iter_next (&headers_iter, &key, (gpointer) &header_value)) |
1409 | 0 | { |
1410 | 0 | GDBusMessageHeaderField field_type = GPOINTER_TO_INT (key); |
1411 | |
|
1412 | 0 | switch (field_type) |
1413 | 0 | { |
1414 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_INVALID: |
1415 | | /* The invalid header must be rejected as per |
1416 | | * https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-header-fields */ |
1417 | 0 | g_set_error (error, |
1418 | 0 | G_IO_ERROR, |
1419 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1420 | 0 | _("%s message: INVALID header field supplied"), |
1421 | 0 | message_type_to_string (message->type)); |
1422 | 0 | goto out; |
1423 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_PATH: |
1424 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_OBJECT_PATH, error)) |
1425 | 0 | goto out; |
1426 | 0 | if (g_strcmp0 (g_variant_get_string (header_value, NULL), DBUS_PATH_LOCAL) == 0) |
1427 | 0 | { |
1428 | 0 | g_set_error (error, |
1429 | 0 | G_IO_ERROR, |
1430 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1431 | | /* Translators: The first placeholder is a D-Bus message type, |
1432 | | * the second is the name of a header field and the third is |
1433 | | * a value that is reserved for the given field. */ |
1434 | 0 | _("%s message: %s header field is using the reserved value %s"), |
1435 | 0 | message_type_to_string (message->type), |
1436 | 0 | "PATH", |
1437 | 0 | DBUS_PATH_LOCAL); |
1438 | 0 | goto out; |
1439 | 0 | } |
1440 | 0 | break; |
1441 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE: |
1442 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_STRING, error)) |
1443 | 0 | goto out; |
1444 | 0 | if (!g_dbus_is_interface_name (g_variant_get_string (header_value, NULL))) |
1445 | 0 | { |
1446 | 0 | g_set_error (error, |
1447 | 0 | G_IO_ERROR, |
1448 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1449 | 0 | _("%s message: INTERFACE header field does not contain a valid interface name"), |
1450 | 0 | message_type_to_string (message->type)); |
1451 | 0 | goto out; |
1452 | 0 | } |
1453 | 0 | if (g_strcmp0 (g_variant_get_string (header_value, NULL), DBUS_INTERFACE_LOCAL) == 0) |
1454 | 0 | { |
1455 | 0 | g_set_error (error, |
1456 | 0 | G_IO_ERROR, |
1457 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1458 | 0 | _("%s message: %s header field is using the reserved value %s"), |
1459 | 0 | message_type_to_string (message->type), |
1460 | 0 | "INTERFACE", |
1461 | 0 | DBUS_INTERFACE_LOCAL); |
1462 | 0 | goto out; |
1463 | 0 | } |
1464 | 0 | break; |
1465 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_MEMBER: |
1466 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_STRING, error)) |
1467 | 0 | goto out; |
1468 | 0 | if (!g_dbus_is_member_name (g_variant_get_string (header_value, NULL))) |
1469 | 0 | { |
1470 | 0 | g_set_error (error, |
1471 | 0 | G_IO_ERROR, |
1472 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1473 | 0 | _("%s message: MEMBER header field does not contain a valid member name"), |
1474 | 0 | message_type_to_string (message->type)); |
1475 | 0 | goto out; |
1476 | 0 | } |
1477 | 0 | break; |
1478 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME: |
1479 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_STRING, error)) |
1480 | 0 | goto out; |
1481 | 0 | if (!g_dbus_is_error_name (g_variant_get_string (header_value, NULL))) |
1482 | 0 | { |
1483 | 0 | g_set_error (error, |
1484 | 0 | G_IO_ERROR, |
1485 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1486 | 0 | _("%s message: ERROR_NAME header field does not contain a valid error name"), |
1487 | 0 | message_type_to_string (message->type)); |
1488 | 0 | goto out; |
1489 | 0 | } |
1490 | 0 | break; |
1491 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL: |
1492 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_UINT32, error)) |
1493 | 0 | goto out; |
1494 | 0 | break; |
1495 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION: |
1496 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_STRING, error)) |
1497 | 0 | goto out; |
1498 | 0 | break; |
1499 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_SENDER: |
1500 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_STRING, error)) |
1501 | 0 | goto out; |
1502 | 0 | break; |
1503 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE: |
1504 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_SIGNATURE, error)) |
1505 | 0 | goto out; |
1506 | 0 | break; |
1507 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS: |
1508 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_UINT32, error)) |
1509 | 0 | goto out; |
1510 | 0 | break; |
1511 | 0 | default: |
1512 | | /* Ignore unknown fields as per |
1513 | | * https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-header-fields. */ |
1514 | 0 | continue; |
1515 | 0 | } |
1516 | 0 | } |
1517 | | |
1518 | | /* Check for message-type-specific required headers. */ |
1519 | 0 | switch (message->type) |
1520 | 0 | { |
1521 | 0 | case G_DBUS_MESSAGE_TYPE_INVALID: |
1522 | 0 | g_set_error_literal (error, |
1523 | 0 | G_IO_ERROR, |
1524 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1525 | 0 | _("type is INVALID")); |
1526 | 0 | goto out; |
1527 | | |
1528 | 0 | case G_DBUS_MESSAGE_TYPE_METHOD_CALL: |
1529 | 0 | if (!require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, error) || |
1530 | 0 | !require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, error)) |
1531 | 0 | goto out; |
1532 | 0 | break; |
1533 | | |
1534 | 0 | case G_DBUS_MESSAGE_TYPE_METHOD_RETURN: |
1535 | 0 | if (!require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, error)) |
1536 | 0 | goto out; |
1537 | 0 | break; |
1538 | | |
1539 | 0 | case G_DBUS_MESSAGE_TYPE_ERROR: |
1540 | 0 | if (!require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, error) || |
1541 | 0 | !require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, error)) |
1542 | 0 | goto out; |
1543 | 0 | break; |
1544 | | |
1545 | 0 | case G_DBUS_MESSAGE_TYPE_SIGNAL: |
1546 | 0 | if (!require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, error) || |
1547 | 0 | !require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, error) || |
1548 | 0 | !require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, error)) |
1549 | 0 | goto out; |
1550 | 0 | break; |
1551 | | |
1552 | 0 | default: |
1553 | | /* hitherto unknown type - nothing to check */ |
1554 | 0 | break; |
1555 | 0 | } |
1556 | | |
1557 | 0 | ret = TRUE; |
1558 | |
|
1559 | 0 | out: |
1560 | 0 | g_assert (ret || (error == NULL || *error != NULL)); |
1561 | 0 | return ret; |
1562 | 0 | } |
1563 | | |
1564 | | /* ---------------------------------------------------------------------------------------------------- */ |
1565 | | |
1566 | | static gboolean |
1567 | | ensure_input_padding (GMemoryBuffer *buf, |
1568 | | gsize padding_size) |
1569 | 0 | { |
1570 | 0 | gsize offset; |
1571 | 0 | gsize wanted_offset; |
1572 | |
|
1573 | 0 | offset = buf->pos; |
1574 | 0 | wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size; |
1575 | 0 | buf->pos = wanted_offset; |
1576 | 0 | return TRUE; |
1577 | 0 | } |
1578 | | |
1579 | | static const gchar * |
1580 | | read_string (GMemoryBuffer *mbuf, |
1581 | | gsize len, |
1582 | | GError **error) |
1583 | 0 | { |
1584 | 0 | gchar *str; |
1585 | 0 | const gchar *end_valid; |
1586 | |
|
1587 | 0 | if G_UNLIKELY (mbuf->pos + len >= mbuf->valid_len || mbuf->pos + len < mbuf->pos) |
1588 | 0 | { |
1589 | 0 | mbuf->pos = mbuf->valid_len; |
1590 | | /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */ |
1591 | 0 | g_set_error (error, |
1592 | 0 | G_IO_ERROR, |
1593 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1594 | 0 | g_dngettext (GETTEXT_PACKAGE, |
1595 | 0 | "Wanted to read %lu byte but only got %lu", |
1596 | 0 | "Wanted to read %lu bytes but only got %lu", |
1597 | 0 | (gulong)len), |
1598 | 0 | (gulong)len, |
1599 | 0 | (gulong)(mbuf->valid_len - mbuf->pos)); |
1600 | 0 | return NULL; |
1601 | 0 | } |
1602 | | |
1603 | 0 | if G_UNLIKELY (mbuf->data[mbuf->pos + len] != '\0') |
1604 | 0 | { |
1605 | 0 | str = g_strndup (mbuf->data + mbuf->pos, len); |
1606 | 0 | g_set_error (error, |
1607 | 0 | G_IO_ERROR, |
1608 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1609 | 0 | _("Expected NUL byte after the string “%s” but found byte %d"), |
1610 | 0 | str, mbuf->data[mbuf->pos + len]); |
1611 | 0 | g_free (str); |
1612 | 0 | mbuf->pos += len + 1; |
1613 | 0 | return NULL; |
1614 | 0 | } |
1615 | | |
1616 | 0 | str = mbuf->data + mbuf->pos; |
1617 | 0 | mbuf->pos += len + 1; |
1618 | |
|
1619 | 0 | if G_UNLIKELY (!g_utf8_validate (str, -1, &end_valid)) |
1620 | 0 | { |
1621 | 0 | gint offset; |
1622 | 0 | gchar *valid_str; |
1623 | 0 | offset = (gint) (end_valid - str); |
1624 | 0 | valid_str = g_strndup (str, offset); |
1625 | 0 | g_set_error (error, |
1626 | 0 | G_IO_ERROR, |
1627 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1628 | 0 | _("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). " |
1629 | 0 | "The valid UTF-8 string up until that point was “%s”"), |
1630 | 0 | offset, |
1631 | 0 | (gint) len, |
1632 | 0 | valid_str); |
1633 | 0 | g_free (valid_str); |
1634 | 0 | return NULL; |
1635 | 0 | } |
1636 | | |
1637 | 0 | return str; |
1638 | 0 | } |
1639 | | |
1640 | | static gconstpointer |
1641 | | read_bytes (GMemoryBuffer *mbuf, |
1642 | | gsize len, |
1643 | | GError **error) |
1644 | 0 | { |
1645 | 0 | gconstpointer result; |
1646 | |
|
1647 | 0 | if G_UNLIKELY (mbuf->pos + len > mbuf->valid_len || mbuf->pos + len < mbuf->pos) |
1648 | 0 | { |
1649 | 0 | mbuf->pos = mbuf->valid_len; |
1650 | | /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */ |
1651 | 0 | g_set_error (error, |
1652 | 0 | G_IO_ERROR, |
1653 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1654 | 0 | g_dngettext (GETTEXT_PACKAGE, |
1655 | 0 | "Wanted to read %lu byte but only got %lu", |
1656 | 0 | "Wanted to read %lu bytes but only got %lu", |
1657 | 0 | (gulong)len), |
1658 | 0 | (gulong)len, |
1659 | 0 | (gulong)(mbuf->valid_len - mbuf->pos)); |
1660 | 0 | return NULL; |
1661 | 0 | } |
1662 | | |
1663 | 0 | result = mbuf->data + mbuf->pos; |
1664 | 0 | mbuf->pos += len; |
1665 | |
|
1666 | 0 | return result; |
1667 | 0 | } |
1668 | | |
1669 | | /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */ |
1670 | | |
1671 | | /* returns a non-floating GVariant! */ |
1672 | | static GVariant * |
1673 | | parse_value_from_blob (GMemoryBuffer *buf, |
1674 | | const GVariantType *type, |
1675 | | guint max_depth, |
1676 | | gboolean just_align, |
1677 | | guint indent, |
1678 | | GError **error) |
1679 | 0 | { |
1680 | 0 | GVariant *ret = NULL; |
1681 | 0 | GError *local_error = NULL; |
1682 | | #ifdef DEBUG_SERIALIZER |
1683 | | gboolean is_leaf; |
1684 | | #endif /* DEBUG_SERIALIZER */ |
1685 | 0 | const gchar *type_string; |
1686 | |
|
1687 | 0 | if (max_depth == 0) |
1688 | 0 | { |
1689 | 0 | g_set_error_literal (&local_error, |
1690 | 0 | G_IO_ERROR, |
1691 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1692 | 0 | _("Value nested too deeply")); |
1693 | 0 | goto fail; |
1694 | 0 | } |
1695 | | |
1696 | 0 | type_string = g_variant_type_peek_string (type); |
1697 | |
|
1698 | | #ifdef DEBUG_SERIALIZER |
1699 | | { |
1700 | | gchar *s; |
1701 | | s = g_variant_type_dup_string (type); |
1702 | | g_print ("%*s%s type %s from offset 0x%04x", |
1703 | | indent, "", |
1704 | | just_align ? "Aligning" : "Reading", |
1705 | | s, |
1706 | | (gint) buf->pos); |
1707 | | g_free (s); |
1708 | | } |
1709 | | #endif /* DEBUG_SERIALIZER */ |
1710 | |
|
1711 | | #ifdef DEBUG_SERIALIZER |
1712 | | is_leaf = TRUE; |
1713 | | #endif /* DEBUG_SERIALIZER */ |
1714 | 0 | switch (type_string[0]) |
1715 | 0 | { |
1716 | 0 | case 'b': /* G_VARIANT_TYPE_BOOLEAN */ |
1717 | 0 | ensure_input_padding (buf, 4); |
1718 | 0 | if (!just_align) |
1719 | 0 | { |
1720 | 0 | gboolean v; |
1721 | 0 | v = g_memory_buffer_read_uint32 (buf, &local_error); |
1722 | 0 | if (local_error) |
1723 | 0 | goto fail; |
1724 | 0 | ret = g_variant_new_boolean (v); |
1725 | 0 | } |
1726 | 0 | break; |
1727 | | |
1728 | 0 | case 'y': /* G_VARIANT_TYPE_BYTE */ |
1729 | 0 | if (!just_align) |
1730 | 0 | { |
1731 | 0 | guchar v; |
1732 | 0 | v = g_memory_buffer_read_byte (buf, &local_error); |
1733 | 0 | if (local_error) |
1734 | 0 | goto fail; |
1735 | 0 | ret = g_variant_new_byte (v); |
1736 | 0 | } |
1737 | 0 | break; |
1738 | | |
1739 | 0 | case 'n': /* G_VARIANT_TYPE_INT16 */ |
1740 | 0 | ensure_input_padding (buf, 2); |
1741 | 0 | if (!just_align) |
1742 | 0 | { |
1743 | 0 | gint16 v; |
1744 | 0 | v = g_memory_buffer_read_int16 (buf, &local_error); |
1745 | 0 | if (local_error) |
1746 | 0 | goto fail; |
1747 | 0 | ret = g_variant_new_int16 (v); |
1748 | 0 | } |
1749 | 0 | break; |
1750 | | |
1751 | 0 | case 'q': /* G_VARIANT_TYPE_UINT16 */ |
1752 | 0 | ensure_input_padding (buf, 2); |
1753 | 0 | if (!just_align) |
1754 | 0 | { |
1755 | 0 | guint16 v; |
1756 | 0 | v = g_memory_buffer_read_uint16 (buf, &local_error); |
1757 | 0 | if (local_error) |
1758 | 0 | goto fail; |
1759 | 0 | ret = g_variant_new_uint16 (v); |
1760 | 0 | } |
1761 | 0 | break; |
1762 | | |
1763 | 0 | case 'i': /* G_VARIANT_TYPE_INT32 */ |
1764 | 0 | ensure_input_padding (buf, 4); |
1765 | 0 | if (!just_align) |
1766 | 0 | { |
1767 | 0 | gint32 v; |
1768 | 0 | v = g_memory_buffer_read_int32 (buf, &local_error); |
1769 | 0 | if (local_error) |
1770 | 0 | goto fail; |
1771 | 0 | ret = g_variant_new_int32 (v); |
1772 | 0 | } |
1773 | 0 | break; |
1774 | | |
1775 | 0 | case 'u': /* G_VARIANT_TYPE_UINT32 */ |
1776 | 0 | ensure_input_padding (buf, 4); |
1777 | 0 | if (!just_align) |
1778 | 0 | { |
1779 | 0 | guint32 v; |
1780 | 0 | v = g_memory_buffer_read_uint32 (buf, &local_error); |
1781 | 0 | if (local_error) |
1782 | 0 | goto fail; |
1783 | 0 | ret = g_variant_new_uint32 (v); |
1784 | 0 | } |
1785 | 0 | break; |
1786 | | |
1787 | 0 | case 'x': /* G_VARIANT_TYPE_INT64 */ |
1788 | 0 | ensure_input_padding (buf, 8); |
1789 | 0 | if (!just_align) |
1790 | 0 | { |
1791 | 0 | gint64 v; |
1792 | 0 | v = g_memory_buffer_read_int64 (buf, &local_error); |
1793 | 0 | if (local_error) |
1794 | 0 | goto fail; |
1795 | 0 | ret = g_variant_new_int64 (v); |
1796 | 0 | } |
1797 | 0 | break; |
1798 | | |
1799 | 0 | case 't': /* G_VARIANT_TYPE_UINT64 */ |
1800 | 0 | ensure_input_padding (buf, 8); |
1801 | 0 | if (!just_align) |
1802 | 0 | { |
1803 | 0 | guint64 v; |
1804 | 0 | v = g_memory_buffer_read_uint64 (buf, &local_error); |
1805 | 0 | if (local_error) |
1806 | 0 | goto fail; |
1807 | 0 | ret = g_variant_new_uint64 (v); |
1808 | 0 | } |
1809 | 0 | break; |
1810 | | |
1811 | 0 | case 'd': /* G_VARIANT_TYPE_DOUBLE */ |
1812 | 0 | ensure_input_padding (buf, 8); |
1813 | 0 | if (!just_align) |
1814 | 0 | { |
1815 | 0 | union { |
1816 | 0 | guint64 v_uint64; |
1817 | 0 | gdouble v_double; |
1818 | 0 | } u; |
1819 | 0 | G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64)); |
1820 | 0 | u.v_uint64 = g_memory_buffer_read_uint64 (buf, &local_error); |
1821 | 0 | if (local_error) |
1822 | 0 | goto fail; |
1823 | 0 | ret = g_variant_new_double (u.v_double); |
1824 | 0 | } |
1825 | 0 | break; |
1826 | | |
1827 | 0 | case 's': /* G_VARIANT_TYPE_STRING */ |
1828 | 0 | ensure_input_padding (buf, 4); |
1829 | 0 | if (!just_align) |
1830 | 0 | { |
1831 | 0 | guint32 len; |
1832 | 0 | const gchar *v; |
1833 | 0 | len = g_memory_buffer_read_uint32 (buf, &local_error); |
1834 | 0 | if (local_error) |
1835 | 0 | goto fail; |
1836 | 0 | v = read_string (buf, (gsize) len, &local_error); |
1837 | 0 | if (v == NULL) |
1838 | 0 | goto fail; |
1839 | 0 | ret = g_variant_new_string (v); |
1840 | 0 | } |
1841 | 0 | break; |
1842 | | |
1843 | 0 | case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */ |
1844 | 0 | ensure_input_padding (buf, 4); |
1845 | 0 | if (!just_align) |
1846 | 0 | { |
1847 | 0 | guint32 len; |
1848 | 0 | const gchar *v; |
1849 | 0 | len = g_memory_buffer_read_uint32 (buf, &local_error); |
1850 | 0 | if (local_error) |
1851 | 0 | goto fail; |
1852 | 0 | v = read_string (buf, (gsize) len, &local_error); |
1853 | 0 | if (v == NULL) |
1854 | 0 | goto fail; |
1855 | 0 | if (!g_variant_is_object_path (v)) |
1856 | 0 | { |
1857 | 0 | g_set_error (&local_error, |
1858 | 0 | G_IO_ERROR, |
1859 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1860 | 0 | _("Parsed value “%s” is not a valid D-Bus object path"), |
1861 | 0 | v); |
1862 | 0 | goto fail; |
1863 | 0 | } |
1864 | 0 | ret = g_variant_new_object_path (v); |
1865 | 0 | } |
1866 | 0 | break; |
1867 | | |
1868 | 0 | case 'g': /* G_VARIANT_TYPE_SIGNATURE */ |
1869 | 0 | if (!just_align) |
1870 | 0 | { |
1871 | 0 | guchar len; |
1872 | 0 | const gchar *v; |
1873 | 0 | len = g_memory_buffer_read_byte (buf, &local_error); |
1874 | 0 | if (local_error) |
1875 | 0 | goto fail; |
1876 | 0 | v = read_string (buf, (gsize) len, &local_error); |
1877 | 0 | if (v == NULL) |
1878 | 0 | goto fail; |
1879 | 0 | if (!g_variant_is_signature (v)) |
1880 | 0 | { |
1881 | 0 | g_set_error (&local_error, |
1882 | 0 | G_IO_ERROR, |
1883 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1884 | 0 | _("Parsed value “%s” is not a valid D-Bus signature"), |
1885 | 0 | v); |
1886 | 0 | goto fail; |
1887 | 0 | } |
1888 | 0 | ret = g_variant_new_signature (v); |
1889 | 0 | } |
1890 | 0 | break; |
1891 | | |
1892 | 0 | case 'h': /* G_VARIANT_TYPE_HANDLE */ |
1893 | 0 | ensure_input_padding (buf, 4); |
1894 | 0 | if (!just_align) |
1895 | 0 | { |
1896 | 0 | gint32 v; |
1897 | 0 | v = g_memory_buffer_read_int32 (buf, &local_error); |
1898 | 0 | if (local_error) |
1899 | 0 | goto fail; |
1900 | 0 | ret = g_variant_new_handle (v); |
1901 | 0 | } |
1902 | 0 | break; |
1903 | | |
1904 | 0 | case 'a': /* G_VARIANT_TYPE_ARRAY */ |
1905 | 0 | ensure_input_padding (buf, 4); |
1906 | | |
1907 | | /* If we are only aligning for this array type, it is the child type of |
1908 | | * another array, which is empty. So, we do not need to add padding for |
1909 | | * this nonexistent array's elements: we only need to align for this |
1910 | | * array itself (4 bytes). See |
1911 | | * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>. |
1912 | | */ |
1913 | 0 | if (!just_align) |
1914 | 0 | { |
1915 | 0 | guint32 array_len; |
1916 | 0 | const GVariantType *element_type; |
1917 | 0 | guint fixed_size; |
1918 | |
|
1919 | 0 | array_len = g_memory_buffer_read_uint32 (buf, &local_error); |
1920 | 0 | if (local_error) |
1921 | 0 | goto fail; |
1922 | | |
1923 | | #ifdef DEBUG_SERIALIZER |
1924 | | is_leaf = FALSE; |
1925 | | g_print (": array spans 0x%04x bytes\n", array_len); |
1926 | | #endif /* DEBUG_SERIALIZER */ |
1927 | | |
1928 | 0 | if (array_len > (2<<26)) |
1929 | 0 | { |
1930 | | /* G_GUINT32_FORMAT doesn't work with gettext, so use u */ |
1931 | 0 | g_set_error (&local_error, |
1932 | 0 | G_IO_ERROR, |
1933 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1934 | 0 | g_dngettext (GETTEXT_PACKAGE, |
1935 | 0 | "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).", |
1936 | 0 | "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).", |
1937 | 0 | array_len), |
1938 | 0 | array_len); |
1939 | 0 | goto fail; |
1940 | 0 | } |
1941 | | |
1942 | 0 | element_type = g_variant_type_element (type); |
1943 | 0 | fixed_size = get_type_fixed_size (element_type); |
1944 | | |
1945 | | /* Fast-path the cases like 'ay', etc. */ |
1946 | 0 | if (fixed_size != 0) |
1947 | 0 | { |
1948 | 0 | gconstpointer array_data; |
1949 | |
|
1950 | 0 | if (array_len % fixed_size != 0) |
1951 | 0 | { |
1952 | 0 | g_set_error (&local_error, |
1953 | 0 | G_IO_ERROR, |
1954 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1955 | 0 | _("Encountered array of type “a%c”, expected to have a length a multiple " |
1956 | 0 | "of %u bytes, but found to be %u bytes in length"), |
1957 | 0 | g_variant_type_peek_string (element_type)[0], fixed_size, array_len); |
1958 | 0 | goto fail; |
1959 | 0 | } |
1960 | | |
1961 | 0 | if (max_depth == 1) |
1962 | 0 | { |
1963 | | /* If we had recursed into parse_value_from_blob() again to |
1964 | | * parse the array values, this would have been emitted. */ |
1965 | 0 | g_set_error_literal (&local_error, |
1966 | 0 | G_IO_ERROR, |
1967 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1968 | 0 | _("Value nested too deeply")); |
1969 | 0 | goto fail; |
1970 | 0 | } |
1971 | | |
1972 | 0 | ensure_input_padding (buf, fixed_size); |
1973 | 0 | array_data = read_bytes (buf, array_len, &local_error); |
1974 | 0 | if (array_data == NULL) |
1975 | 0 | goto fail; |
1976 | | |
1977 | 0 | ret = g_variant_new_fixed_array (element_type, array_data, array_len / fixed_size, fixed_size); |
1978 | |
|
1979 | 0 | if (g_memory_buffer_is_byteswapped (buf)) |
1980 | 0 | { |
1981 | 0 | GVariant *tmp = g_variant_ref_sink (ret); |
1982 | 0 | ret = g_variant_byteswap (tmp); |
1983 | 0 | g_variant_unref (tmp); |
1984 | 0 | } |
1985 | 0 | } |
1986 | 0 | else |
1987 | 0 | { |
1988 | 0 | GVariantBuilder builder; |
1989 | 0 | goffset offset; |
1990 | 0 | goffset target; |
1991 | |
|
1992 | 0 | g_variant_builder_init_static (&builder, type); |
1993 | |
|
1994 | 0 | if (array_len == 0) |
1995 | 0 | { |
1996 | 0 | GVariant *item G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */; |
1997 | 0 | item = parse_value_from_blob (buf, |
1998 | 0 | element_type, |
1999 | 0 | max_depth - 1, |
2000 | 0 | TRUE, |
2001 | 0 | indent + 2, |
2002 | 0 | NULL); |
2003 | 0 | g_assert (item == NULL); |
2004 | 0 | } |
2005 | 0 | else |
2006 | 0 | { |
2007 | 0 | offset = buf->pos; |
2008 | 0 | target = offset + array_len; |
2009 | 0 | while (offset < target) |
2010 | 0 | { |
2011 | 0 | GVariant *item; |
2012 | 0 | item = parse_value_from_blob (buf, |
2013 | 0 | element_type, |
2014 | 0 | max_depth - 1, |
2015 | 0 | FALSE, |
2016 | 0 | indent + 2, |
2017 | 0 | &local_error); |
2018 | 0 | if (item == NULL) |
2019 | 0 | { |
2020 | 0 | g_variant_builder_clear (&builder); |
2021 | 0 | goto fail; |
2022 | 0 | } |
2023 | 0 | g_variant_builder_add_value (&builder, item); |
2024 | 0 | g_variant_unref (item); |
2025 | | |
2026 | | /* Array elements must not be zero-length. There are no |
2027 | | * valid zero-length serialisations of any types which |
2028 | | * can be array elements in the D-Bus wire format, so this |
2029 | | * assertion should always hold. |
2030 | | * |
2031 | | * See https://gitlab.gnome.org/GNOME/glib/-/issues/2557 |
2032 | | */ |
2033 | 0 | g_assert (buf->pos > (gsize) offset); |
2034 | | |
2035 | 0 | offset = buf->pos; |
2036 | 0 | } |
2037 | 0 | } |
2038 | | |
2039 | 0 | ret = g_variant_builder_end (&builder); |
2040 | 0 | } |
2041 | 0 | } |
2042 | 0 | break; |
2043 | | |
2044 | 0 | default: |
2045 | 0 | if (g_variant_type_is_dict_entry (type)) |
2046 | 0 | { |
2047 | 0 | const GVariantType *key_type; |
2048 | 0 | const GVariantType *value_type; |
2049 | 0 | GVariant *key; |
2050 | 0 | GVariant *value; |
2051 | |
|
2052 | 0 | ensure_input_padding (buf, 8); |
2053 | |
|
2054 | | #ifdef DEBUG_SERIALIZER |
2055 | | is_leaf = FALSE; |
2056 | | g_print ("\n"); |
2057 | | #endif /* DEBUG_SERIALIZER */ |
2058 | |
|
2059 | 0 | if (!just_align) |
2060 | 0 | { |
2061 | 0 | key_type = g_variant_type_key (type); |
2062 | 0 | key = parse_value_from_blob (buf, |
2063 | 0 | key_type, |
2064 | 0 | max_depth - 1, |
2065 | 0 | FALSE, |
2066 | 0 | indent + 2, |
2067 | 0 | &local_error); |
2068 | 0 | if (key == NULL) |
2069 | 0 | goto fail; |
2070 | 0 | value_type = g_variant_type_value (type); |
2071 | 0 | value = parse_value_from_blob (buf, |
2072 | 0 | value_type, |
2073 | 0 | max_depth - 1, |
2074 | 0 | FALSE, |
2075 | 0 | indent + 2, |
2076 | 0 | &local_error); |
2077 | 0 | if (value == NULL) |
2078 | 0 | { |
2079 | 0 | g_variant_unref (key); |
2080 | 0 | goto fail; |
2081 | 0 | } |
2082 | 0 | ret = g_variant_new_dict_entry (key, value); |
2083 | 0 | g_variant_unref (key); |
2084 | 0 | g_variant_unref (value); |
2085 | 0 | } |
2086 | 0 | } |
2087 | 0 | else if (g_variant_type_is_tuple (type)) |
2088 | 0 | { |
2089 | 0 | ensure_input_padding (buf, 8); |
2090 | |
|
2091 | | #ifdef DEBUG_SERIALIZER |
2092 | | is_leaf = FALSE; |
2093 | | g_print ("\n"); |
2094 | | #endif /* DEBUG_SERIALIZER */ |
2095 | |
|
2096 | 0 | if (!just_align) |
2097 | 0 | { |
2098 | 0 | const GVariantType *element_type; |
2099 | 0 | GVariantBuilder builder; |
2100 | |
|
2101 | 0 | g_variant_builder_init_static (&builder, type); |
2102 | 0 | element_type = g_variant_type_first (type); |
2103 | 0 | if (!element_type) |
2104 | 0 | { |
2105 | 0 | g_variant_builder_clear (&builder); |
2106 | 0 | g_set_error_literal (&local_error, |
2107 | 0 | G_IO_ERROR, |
2108 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2109 | 0 | _("Empty structures (tuples) are not allowed in D-Bus")); |
2110 | 0 | goto fail; |
2111 | 0 | } |
2112 | | |
2113 | 0 | while (element_type != NULL) |
2114 | 0 | { |
2115 | 0 | GVariant *item; |
2116 | 0 | item = parse_value_from_blob (buf, |
2117 | 0 | element_type, |
2118 | 0 | max_depth - 1, |
2119 | 0 | FALSE, |
2120 | 0 | indent + 2, |
2121 | 0 | &local_error); |
2122 | 0 | if (item == NULL) |
2123 | 0 | { |
2124 | 0 | g_variant_builder_clear (&builder); |
2125 | 0 | goto fail; |
2126 | 0 | } |
2127 | 0 | g_variant_builder_add_value (&builder, item); |
2128 | 0 | g_variant_unref (item); |
2129 | |
|
2130 | 0 | element_type = g_variant_type_next (element_type); |
2131 | 0 | } |
2132 | 0 | ret = g_variant_builder_end (&builder); |
2133 | 0 | } |
2134 | 0 | } |
2135 | 0 | else if (g_variant_type_is_variant (type)) |
2136 | 0 | { |
2137 | | #ifdef DEBUG_SERIALIZER |
2138 | | is_leaf = FALSE; |
2139 | | g_print ("\n"); |
2140 | | #endif /* DEBUG_SERIALIZER */ |
2141 | |
|
2142 | 0 | if (!just_align) |
2143 | 0 | { |
2144 | 0 | guchar siglen; |
2145 | 0 | const gchar *sig; |
2146 | 0 | GVariantType *variant_type; |
2147 | 0 | GVariant *value; |
2148 | |
|
2149 | 0 | siglen = g_memory_buffer_read_byte (buf, &local_error); |
2150 | 0 | if (local_error) |
2151 | 0 | goto fail; |
2152 | 0 | sig = read_string (buf, (gsize) siglen, &local_error); |
2153 | 0 | if (sig == NULL) |
2154 | 0 | goto fail; |
2155 | 0 | if (!g_variant_is_signature (sig) || |
2156 | 0 | !g_variant_type_string_is_valid (sig)) |
2157 | 0 | { |
2158 | | /* A D-Bus signature can contain zero or more complete types, |
2159 | | * but a GVariant has to be exactly one complete type. */ |
2160 | 0 | g_set_error (&local_error, |
2161 | 0 | G_IO_ERROR, |
2162 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2163 | 0 | _("Parsed value “%s” for variant is not a valid D-Bus signature"), |
2164 | 0 | sig); |
2165 | 0 | goto fail; |
2166 | 0 | } |
2167 | | |
2168 | 0 | if (max_depth <= g_variant_type_string_get_depth_ (sig)) |
2169 | 0 | { |
2170 | | /* Catch the type nesting being too deep without having to |
2171 | | * parse the data. We don’t have to check this for static |
2172 | | * container types (like arrays and tuples, above) because |
2173 | | * the g_variant_type_string_is_valid() check performed before |
2174 | | * the initial parse_value_from_blob() call should check the |
2175 | | * static type nesting. */ |
2176 | 0 | g_set_error_literal (&local_error, |
2177 | 0 | G_IO_ERROR, |
2178 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2179 | 0 | _("Value nested too deeply")); |
2180 | 0 | goto fail; |
2181 | 0 | } |
2182 | | |
2183 | 0 | variant_type = g_variant_type_new (sig); |
2184 | 0 | value = parse_value_from_blob (buf, |
2185 | 0 | variant_type, |
2186 | 0 | max_depth - 1, |
2187 | 0 | FALSE, |
2188 | 0 | indent + 2, |
2189 | 0 | &local_error); |
2190 | 0 | g_variant_type_free (variant_type); |
2191 | 0 | if (value == NULL) |
2192 | 0 | goto fail; |
2193 | 0 | ret = g_variant_new_variant (value); |
2194 | 0 | g_variant_unref (value); |
2195 | 0 | } |
2196 | 0 | } |
2197 | 0 | else |
2198 | 0 | { |
2199 | 0 | gchar *s; |
2200 | 0 | s = g_variant_type_dup_string (type); |
2201 | 0 | g_set_error (&local_error, |
2202 | 0 | G_IO_ERROR, |
2203 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2204 | 0 | _("Error deserializing GVariant with type string “%s” from the D-Bus wire format"), |
2205 | 0 | s); |
2206 | 0 | g_free (s); |
2207 | 0 | goto fail; |
2208 | 0 | } |
2209 | 0 | break; |
2210 | 0 | } |
2211 | | |
2212 | 0 | g_assert ((just_align && ret == NULL) || (!just_align && ret != NULL)); |
2213 | | |
2214 | | #ifdef DEBUG_SERIALIZER |
2215 | | if (ret != NULL) |
2216 | | { |
2217 | | if (is_leaf) |
2218 | | { |
2219 | | gchar *s; |
2220 | | if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE)) |
2221 | | { |
2222 | | s = g_strdup_printf ("0x%02x '%c'", g_variant_get_byte (ret), g_variant_get_byte (ret)); |
2223 | | } |
2224 | | else |
2225 | | { |
2226 | | s = g_variant_print (ret, FALSE); |
2227 | | } |
2228 | | g_print (": %s\n", s); |
2229 | | g_free (s); |
2230 | | } |
2231 | | } |
2232 | | #endif /* DEBUG_SERIALIZER */ |
2233 | | |
2234 | | /* sink the reference, if floating */ |
2235 | 0 | if (ret != NULL) |
2236 | 0 | g_variant_take_ref (ret); |
2237 | 0 | return ret; |
2238 | | |
2239 | 0 | fail: |
2240 | | #ifdef DEBUG_SERIALIZER |
2241 | | g_print ("\n" |
2242 | | "%*sFAILURE: %s (%s, %d)\n", |
2243 | | indent, "", |
2244 | | local_error->message, |
2245 | | g_quark_to_string (local_error->domain), |
2246 | | local_error->code); |
2247 | | #endif /* DEBUG_SERIALIZER */ |
2248 | 0 | g_propagate_error (error, local_error); |
2249 | 0 | return NULL; |
2250 | 0 | } |
2251 | | |
2252 | | /* ---------------------------------------------------------------------------------------------------- */ |
2253 | | |
2254 | | /* message_header must be at least 16 bytes */ |
2255 | | |
2256 | | /** |
2257 | | * g_dbus_message_bytes_needed: |
2258 | | * @blob: (array length=blob_len) (element-type guint8): A blob representing a binary D-Bus message. |
2259 | | * @blob_len: The length of @blob (must be at least 16). |
2260 | | * @error: Return location for error or %NULL. |
2261 | | * |
2262 | | * Utility function to calculate how many bytes are needed to |
2263 | | * completely deserialize the D-Bus message stored at @blob. |
2264 | | * |
2265 | | * Returns: Number of bytes needed or -1 if @error is set (e.g. if |
2266 | | * @blob contains invalid data or not enough data is available to |
2267 | | * determine the size). |
2268 | | * |
2269 | | * Since: 2.26 |
2270 | | */ |
2271 | | gssize |
2272 | | g_dbus_message_bytes_needed (guchar *blob, |
2273 | | gsize blob_len, |
2274 | | GError **error) |
2275 | 0 | { |
2276 | 0 | gssize ret; |
2277 | |
|
2278 | 0 | ret = -1; |
2279 | |
|
2280 | 0 | g_return_val_if_fail (blob != NULL, -1); |
2281 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, -1); |
2282 | 0 | g_return_val_if_fail (blob_len >= 16, -1); |
2283 | | |
2284 | 0 | if (blob[0] == 'l') |
2285 | 0 | { |
2286 | | /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */ |
2287 | 0 | ret = 12 + 4 + GUINT32_FROM_LE (((guint32 *) blob)[3]); |
2288 | | /* round up so it's a multiple of 8 */ |
2289 | 0 | ret = 8 * ((ret + 7)/8); |
2290 | | /* finally add the body size */ |
2291 | 0 | ret += GUINT32_FROM_LE (((guint32 *) blob)[1]); |
2292 | 0 | } |
2293 | 0 | else if (blob[0] == 'B') |
2294 | 0 | { |
2295 | | /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */ |
2296 | 0 | ret = 12 + 4 + GUINT32_FROM_BE (((guint32 *) blob)[3]); |
2297 | | /* round up so it's a multiple of 8 */ |
2298 | 0 | ret = 8 * ((ret + 7)/8); |
2299 | | /* finally add the body size */ |
2300 | 0 | ret += GUINT32_FROM_BE (((guint32 *) blob)[1]); |
2301 | 0 | } |
2302 | 0 | else |
2303 | 0 | { |
2304 | 0 | g_set_error (error, |
2305 | 0 | G_IO_ERROR, |
2306 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2307 | 0 | "Unable to determine message blob length - given blob is malformed"); |
2308 | 0 | } |
2309 | |
|
2310 | 0 | if (ret > (1<<27)) |
2311 | 0 | { |
2312 | 0 | g_set_error (error, |
2313 | 0 | G_IO_ERROR, |
2314 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2315 | 0 | "Blob indicates that message exceeds maximum message length (128MiB)"); |
2316 | 0 | ret = -1; |
2317 | 0 | } |
2318 | |
|
2319 | 0 | return ret; |
2320 | 0 | } |
2321 | | |
2322 | | /* ---------------------------------------------------------------------------------------------------- */ |
2323 | | |
2324 | | /** |
2325 | | * g_dbus_message_new_from_blob: |
2326 | | * @blob: (array length=blob_len) (element-type guint8): A blob representing a binary D-Bus message. |
2327 | | * @blob_len: The length of @blob. |
2328 | | * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported. |
2329 | | * @error: Return location for error or %NULL. |
2330 | | * |
2331 | | * Creates a new #GDBusMessage from the data stored at @blob. The byte |
2332 | | * order that the message was in can be retrieved using |
2333 | | * g_dbus_message_get_byte_order(). |
2334 | | * |
2335 | | * If the @blob cannot be parsed, contains invalid fields, or contains invalid |
2336 | | * headers, %G_IO_ERROR_INVALID_ARGUMENT will be returned. |
2337 | | * |
2338 | | * Returns: A new #GDBusMessage or %NULL if @error is set. Free with |
2339 | | * g_object_unref(). |
2340 | | * |
2341 | | * Since: 2.26 |
2342 | | */ |
2343 | | GDBusMessage * |
2344 | | g_dbus_message_new_from_blob (guchar *blob, |
2345 | | gsize blob_len, |
2346 | | GDBusCapabilityFlags capabilities, |
2347 | | GError **error) |
2348 | 0 | { |
2349 | 0 | GError *local_error = NULL; |
2350 | 0 | GMemoryBuffer mbuf; |
2351 | 0 | GDBusMessage *message; |
2352 | 0 | guchar endianness; |
2353 | 0 | guchar major_protocol_version; |
2354 | 0 | guint32 message_body_len; |
2355 | 0 | GVariant *headers; |
2356 | 0 | GVariant *item; |
2357 | 0 | GVariantIter iter; |
2358 | 0 | GVariant *signature; |
2359 | | |
2360 | | /* TODO: check against @capabilities */ |
2361 | |
|
2362 | 0 | g_return_val_if_fail (blob != NULL, NULL); |
2363 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
2364 | | |
2365 | 0 | message = g_dbus_message_new (); |
2366 | |
|
2367 | 0 | memset (&mbuf, 0, sizeof (mbuf)); |
2368 | 0 | mbuf.data = (gchar *)blob; |
2369 | 0 | mbuf.len = mbuf.valid_len = blob_len; |
2370 | |
|
2371 | 0 | endianness = g_memory_buffer_read_byte (&mbuf, &local_error); |
2372 | 0 | if (local_error) |
2373 | 0 | goto fail; |
2374 | | |
2375 | 0 | switch (endianness) |
2376 | 0 | { |
2377 | 0 | case 'l': |
2378 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN; |
2379 | 0 | message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN; |
2380 | 0 | break; |
2381 | 0 | case 'B': |
2382 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN; |
2383 | 0 | message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN; |
2384 | 0 | break; |
2385 | 0 | default: |
2386 | 0 | g_set_error (&local_error, |
2387 | 0 | G_IO_ERROR, |
2388 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2389 | 0 | _("Invalid endianness value. Expected 0x6c (“l”) or 0x42 (“B”) but found value 0x%02x"), |
2390 | 0 | endianness); |
2391 | 0 | goto fail; |
2392 | 0 | } |
2393 | | |
2394 | 0 | message->type = g_memory_buffer_read_byte (&mbuf, &local_error); |
2395 | 0 | if (local_error) |
2396 | 0 | goto fail; |
2397 | 0 | message->flags = g_memory_buffer_read_byte (&mbuf, &local_error); |
2398 | 0 | if (local_error) |
2399 | 0 | goto fail; |
2400 | 0 | major_protocol_version = g_memory_buffer_read_byte (&mbuf, &local_error); |
2401 | 0 | if (local_error) |
2402 | 0 | goto fail; |
2403 | 0 | if (major_protocol_version != 1) |
2404 | 0 | { |
2405 | 0 | g_set_error (&local_error, |
2406 | 0 | G_IO_ERROR, |
2407 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2408 | 0 | _("Invalid major protocol version. Expected 1 but found %d"), |
2409 | 0 | major_protocol_version); |
2410 | 0 | goto fail; |
2411 | 0 | } |
2412 | 0 | message_body_len = g_memory_buffer_read_uint32 (&mbuf, &local_error); |
2413 | 0 | if (local_error) |
2414 | 0 | goto fail; |
2415 | 0 | message->serial = g_memory_buffer_read_uint32 (&mbuf, &local_error); |
2416 | 0 | if (local_error) |
2417 | 0 | goto fail; |
2418 | | |
2419 | | #ifdef DEBUG_SERIALIZER |
2420 | | g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint) blob_len); |
2421 | | { |
2422 | | gchar *s; |
2423 | | s = _g_dbus_hexdump ((const gchar *) blob, blob_len, 2); |
2424 | | g_print ("%s\n", s); |
2425 | | g_free (s); |
2426 | | } |
2427 | | #endif /* DEBUG_SERIALIZER */ |
2428 | | |
2429 | | #ifdef DEBUG_SERIALIZER |
2430 | | g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint) blob_len); |
2431 | | #endif /* DEBUG_SERIALIZER */ |
2432 | 0 | headers = parse_value_from_blob (&mbuf, |
2433 | 0 | G_VARIANT_TYPE ("a{yv}"), |
2434 | 0 | G_DBUS_MAX_TYPE_DEPTH + 2 /* for the a{yv} */, |
2435 | 0 | FALSE, |
2436 | 0 | 2, |
2437 | 0 | &local_error); |
2438 | 0 | if (headers == NULL) |
2439 | 0 | goto fail; |
2440 | 0 | g_variant_iter_init (&iter, headers); |
2441 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2442 | 0 | { |
2443 | 0 | guchar header_field; |
2444 | 0 | GVariant *value; |
2445 | 0 | g_variant_get (item, |
2446 | 0 | "{yv}", |
2447 | 0 | &header_field, |
2448 | 0 | &value); |
2449 | 0 | g_dbus_message_set_header (message, header_field, value); |
2450 | 0 | g_variant_unref (value); |
2451 | 0 | g_variant_unref (item); |
2452 | 0 | } |
2453 | 0 | g_variant_unref (headers); |
2454 | |
|
2455 | 0 | signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE); |
2456 | 0 | if (signature != NULL) |
2457 | 0 | { |
2458 | 0 | const gchar *signature_str; |
2459 | 0 | gsize signature_str_len; |
2460 | |
|
2461 | 0 | if (!g_variant_is_of_type (signature, G_VARIANT_TYPE_SIGNATURE)) |
2462 | 0 | { |
2463 | 0 | g_set_error_literal (&local_error, |
2464 | 0 | G_IO_ERROR, |
2465 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2466 | 0 | _("Signature header found but is not of type signature")); |
2467 | 0 | goto fail; |
2468 | 0 | } |
2469 | | |
2470 | 0 | signature_str = g_variant_get_string (signature, &signature_str_len); |
2471 | | |
2472 | | /* signature but no body */ |
2473 | 0 | if (message_body_len == 0 && signature_str_len > 0) |
2474 | 0 | { |
2475 | 0 | g_set_error (&local_error, |
2476 | 0 | G_IO_ERROR, |
2477 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2478 | 0 | _("Signature header with signature “%s” found but message body is empty"), |
2479 | 0 | signature_str); |
2480 | 0 | goto fail; |
2481 | 0 | } |
2482 | 0 | else if (signature_str_len > 0) |
2483 | 0 | { |
2484 | 0 | GVariantType *variant_type; |
2485 | 0 | gchar *tupled_signature_str = g_strdup_printf ("(%s)", signature_str); |
2486 | |
|
2487 | 0 | if (!g_variant_is_signature (signature_str) || |
2488 | 0 | !g_variant_type_string_is_valid (tupled_signature_str)) |
2489 | 0 | { |
2490 | 0 | g_set_error (&local_error, |
2491 | 0 | G_IO_ERROR, |
2492 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2493 | 0 | _("Parsed value “%s” is not a valid D-Bus signature (for body)"), |
2494 | 0 | signature_str); |
2495 | 0 | g_free (tupled_signature_str); |
2496 | 0 | goto fail; |
2497 | 0 | } |
2498 | | |
2499 | 0 | variant_type = g_variant_type_new (tupled_signature_str); |
2500 | 0 | g_free (tupled_signature_str); |
2501 | | #ifdef DEBUG_SERIALIZER |
2502 | | g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint) blob_len); |
2503 | | #endif /* DEBUG_SERIALIZER */ |
2504 | 0 | message->body = parse_value_from_blob (&mbuf, |
2505 | 0 | variant_type, |
2506 | 0 | G_DBUS_MAX_TYPE_DEPTH + 1 /* for the surrounding tuple */, |
2507 | 0 | FALSE, |
2508 | 0 | 2, |
2509 | 0 | &local_error); |
2510 | 0 | g_variant_type_free (variant_type); |
2511 | |
|
2512 | 0 | if (message->body != NULL && |
2513 | 0 | g_variant_is_of_type (message->body, G_VARIANT_TYPE_TUPLE) && |
2514 | 0 | g_variant_n_children (message->body) > 0) |
2515 | 0 | message->arg0_cache = g_variant_get_child_value (message->body, 0); |
2516 | 0 | else |
2517 | 0 | message->arg0_cache = NULL; |
2518 | |
|
2519 | 0 | if (message->body == NULL) |
2520 | 0 | goto fail; |
2521 | 0 | } |
2522 | 0 | } |
2523 | 0 | else |
2524 | 0 | { |
2525 | | /* no signature, this is only OK if the body is empty */ |
2526 | 0 | if (message_body_len != 0) |
2527 | 0 | { |
2528 | | /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */ |
2529 | 0 | g_set_error (&local_error, |
2530 | 0 | G_IO_ERROR, |
2531 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2532 | 0 | g_dngettext (GETTEXT_PACKAGE, |
2533 | 0 | "No signature header in message but the message body is %u byte", |
2534 | 0 | "No signature header in message but the message body is %u bytes", |
2535 | 0 | message_body_len), |
2536 | 0 | message_body_len); |
2537 | 0 | goto fail; |
2538 | 0 | } |
2539 | 0 | } |
2540 | | |
2541 | 0 | if (!validate_headers (message, &local_error)) |
2542 | 0 | { |
2543 | 0 | g_prefix_error (&local_error, _("Cannot deserialize message: ")); |
2544 | 0 | goto fail; |
2545 | 0 | } |
2546 | | |
2547 | 0 | return message; |
2548 | | |
2549 | 0 | fail: |
2550 | 0 | g_clear_object (&message); |
2551 | 0 | g_propagate_error (error, local_error); |
2552 | 0 | return NULL; |
2553 | 0 | } |
2554 | | |
2555 | | /* ---------------------------------------------------------------------------------------------------- */ |
2556 | | |
2557 | | static gsize |
2558 | | ensure_output_padding (GMemoryBuffer *mbuf, |
2559 | | gsize padding_size) |
2560 | 0 | { |
2561 | 0 | gsize offset; |
2562 | 0 | gsize wanted_offset; |
2563 | 0 | gsize padding_needed; |
2564 | 0 | guint n; |
2565 | |
|
2566 | 0 | offset = mbuf->pos; |
2567 | 0 | wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size; |
2568 | 0 | padding_needed = wanted_offset - offset; |
2569 | |
|
2570 | 0 | for (n = 0; n < padding_needed; n++) |
2571 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2572 | |
|
2573 | 0 | return padding_needed; |
2574 | 0 | } |
2575 | | |
2576 | | /* note that value can be NULL for e.g. empty arrays - type is never NULL */ |
2577 | | static gboolean |
2578 | | append_value_to_blob (GVariant *value, |
2579 | | const GVariantType *type, |
2580 | | GMemoryBuffer *mbuf, |
2581 | | gsize *out_padding_added, |
2582 | | GError **error) |
2583 | 0 | { |
2584 | 0 | gsize padding_added; |
2585 | 0 | const gchar *type_string; |
2586 | |
|
2587 | 0 | type_string = g_variant_type_peek_string (type); |
2588 | |
|
2589 | 0 | padding_added = 0; |
2590 | |
|
2591 | 0 | switch (type_string[0]) |
2592 | 0 | { |
2593 | 0 | case 'b': /* G_VARIANT_TYPE_BOOLEAN */ |
2594 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2595 | 0 | if (value != NULL) |
2596 | 0 | { |
2597 | 0 | gboolean v = g_variant_get_boolean (value); |
2598 | 0 | g_memory_buffer_put_uint32 (mbuf, v); |
2599 | 0 | } |
2600 | 0 | break; |
2601 | | |
2602 | 0 | case 'y': /* G_VARIANT_TYPE_BYTE */ |
2603 | 0 | if (value != NULL) |
2604 | 0 | { |
2605 | 0 | guint8 v = g_variant_get_byte (value); |
2606 | 0 | g_memory_buffer_put_byte (mbuf, v); |
2607 | 0 | } |
2608 | 0 | break; |
2609 | | |
2610 | 0 | case 'n': /* G_VARIANT_TYPE_INT16 */ |
2611 | 0 | padding_added = ensure_output_padding (mbuf, 2); |
2612 | 0 | if (value != NULL) |
2613 | 0 | { |
2614 | 0 | gint16 v = g_variant_get_int16 (value); |
2615 | 0 | g_memory_buffer_put_int16 (mbuf, v); |
2616 | 0 | } |
2617 | 0 | break; |
2618 | | |
2619 | 0 | case 'q': /* G_VARIANT_TYPE_UINT16 */ |
2620 | 0 | padding_added = ensure_output_padding (mbuf, 2); |
2621 | 0 | if (value != NULL) |
2622 | 0 | { |
2623 | 0 | guint16 v = g_variant_get_uint16 (value); |
2624 | 0 | g_memory_buffer_put_uint16 (mbuf, v); |
2625 | 0 | } |
2626 | 0 | break; |
2627 | | |
2628 | 0 | case 'i': /* G_VARIANT_TYPE_INT32 */ |
2629 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2630 | 0 | if (value != NULL) |
2631 | 0 | { |
2632 | 0 | gint32 v = g_variant_get_int32 (value); |
2633 | 0 | g_memory_buffer_put_int32 (mbuf, v); |
2634 | 0 | } |
2635 | 0 | break; |
2636 | | |
2637 | 0 | case 'u': /* G_VARIANT_TYPE_UINT32 */ |
2638 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2639 | 0 | if (value != NULL) |
2640 | 0 | { |
2641 | 0 | guint32 v = g_variant_get_uint32 (value); |
2642 | 0 | g_memory_buffer_put_uint32 (mbuf, v); |
2643 | 0 | } |
2644 | 0 | break; |
2645 | | |
2646 | 0 | case 'x': /* G_VARIANT_TYPE_INT64 */ |
2647 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2648 | 0 | if (value != NULL) |
2649 | 0 | { |
2650 | 0 | gint64 v = g_variant_get_int64 (value); |
2651 | 0 | g_memory_buffer_put_int64 (mbuf, v); |
2652 | 0 | } |
2653 | 0 | break; |
2654 | | |
2655 | 0 | case 't': /* G_VARIANT_TYPE_UINT64 */ |
2656 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2657 | 0 | if (value != NULL) |
2658 | 0 | { |
2659 | 0 | guint64 v = g_variant_get_uint64 (value); |
2660 | 0 | g_memory_buffer_put_uint64 (mbuf, v); |
2661 | 0 | } |
2662 | 0 | break; |
2663 | | |
2664 | 0 | case 'd': /* G_VARIANT_TYPE_DOUBLE */ |
2665 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2666 | 0 | if (value != NULL) |
2667 | 0 | { |
2668 | 0 | union { |
2669 | 0 | guint64 v_uint64; |
2670 | 0 | gdouble v_double; |
2671 | 0 | } u; |
2672 | 0 | G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64)); |
2673 | 0 | u.v_double = g_variant_get_double (value); |
2674 | 0 | g_memory_buffer_put_uint64 (mbuf, u.v_uint64); |
2675 | 0 | } |
2676 | 0 | break; |
2677 | | |
2678 | 0 | case 's': /* G_VARIANT_TYPE_STRING */ |
2679 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2680 | 0 | if (value != NULL) |
2681 | 0 | { |
2682 | 0 | gsize len; |
2683 | 0 | const gchar *v; |
2684 | 0 | #ifndef G_DISABLE_ASSERT |
2685 | 0 | const gchar *end; |
2686 | 0 | #endif |
2687 | |
|
2688 | 0 | v = g_variant_get_string (value, &len); |
2689 | 0 | g_assert (g_utf8_validate (v, -1, &end) && (end == v + len)); |
2690 | 0 | g_memory_buffer_put_uint32 (mbuf, len); |
2691 | 0 | g_memory_buffer_put_string (mbuf, v); |
2692 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2693 | 0 | } |
2694 | 0 | break; |
2695 | | |
2696 | 0 | case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */ |
2697 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2698 | 0 | if (value != NULL) |
2699 | 0 | { |
2700 | 0 | gsize len; |
2701 | 0 | const gchar *v = g_variant_get_string (value, &len); |
2702 | 0 | g_assert (g_variant_is_object_path (v)); |
2703 | 0 | g_memory_buffer_put_uint32 (mbuf, len); |
2704 | 0 | g_memory_buffer_put_string (mbuf, v); |
2705 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2706 | 0 | } |
2707 | 0 | break; |
2708 | | |
2709 | 0 | case 'g': /* G_VARIANT_TYPE_SIGNATURE */ |
2710 | 0 | if (value != NULL) |
2711 | 0 | { |
2712 | 0 | gsize len; |
2713 | 0 | const gchar *v = g_variant_get_string (value, &len); |
2714 | 0 | g_assert (g_variant_is_signature (v)); |
2715 | 0 | g_memory_buffer_put_byte (mbuf, len); |
2716 | 0 | g_memory_buffer_put_string (mbuf, v); |
2717 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2718 | 0 | } |
2719 | 0 | break; |
2720 | | |
2721 | 0 | case 'h': /* G_VARIANT_TYPE_HANDLE */ |
2722 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2723 | 0 | if (value != NULL) |
2724 | 0 | { |
2725 | 0 | gint32 v = g_variant_get_handle (value); |
2726 | 0 | g_memory_buffer_put_int32 (mbuf, v); |
2727 | 0 | } |
2728 | 0 | break; |
2729 | | |
2730 | 0 | case 'a': /* G_VARIANT_TYPE_ARRAY */ |
2731 | 0 | { |
2732 | 0 | const GVariantType *element_type; |
2733 | 0 | GVariant *item; |
2734 | 0 | GVariantIter iter; |
2735 | 0 | goffset array_len_offset; |
2736 | 0 | goffset array_payload_begin_offset; |
2737 | 0 | goffset cur_offset; |
2738 | 0 | gsize array_len; |
2739 | 0 | guint fixed_size; |
2740 | |
|
2741 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2742 | 0 | if (value != NULL) |
2743 | 0 | { |
2744 | | /* array length - will be filled in later */ |
2745 | 0 | array_len_offset = mbuf->valid_len; |
2746 | 0 | g_memory_buffer_put_uint32 (mbuf, 0xF00DFACE); |
2747 | | |
2748 | | /* From the D-Bus spec: |
2749 | | * |
2750 | | * "A UINT32 giving the length of the array data in bytes, |
2751 | | * followed by alignment padding to the alignment boundary of |
2752 | | * the array element type, followed by each array element. The |
2753 | | * array length is from the end of the alignment padding to |
2754 | | * the end of the last element, i.e. it does not include the |
2755 | | * padding after the length, or any padding after the last |
2756 | | * element." |
2757 | | * |
2758 | | * Thus, we need to count how much padding the first element |
2759 | | * contributes and subtract that from the array length. |
2760 | | */ |
2761 | 0 | array_payload_begin_offset = mbuf->valid_len; |
2762 | |
|
2763 | 0 | element_type = g_variant_type_element (type); |
2764 | 0 | fixed_size = get_type_fixed_size (element_type); |
2765 | |
|
2766 | 0 | if (g_variant_n_children (value) == 0) |
2767 | 0 | { |
2768 | 0 | gsize padding_added_for_item; |
2769 | 0 | if (!append_value_to_blob (NULL, |
2770 | 0 | element_type, |
2771 | 0 | mbuf, |
2772 | 0 | &padding_added_for_item, |
2773 | 0 | error)) |
2774 | 0 | goto fail; |
2775 | 0 | array_payload_begin_offset += padding_added_for_item; |
2776 | 0 | } |
2777 | 0 | else if (fixed_size != 0) |
2778 | 0 | { |
2779 | 0 | GVariant *use_value; |
2780 | |
|
2781 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
2782 | 0 | use_value = g_variant_byteswap (value); |
2783 | 0 | else |
2784 | 0 | use_value = g_variant_ref (value); |
2785 | |
|
2786 | 0 | array_payload_begin_offset += ensure_output_padding (mbuf, fixed_size); |
2787 | |
|
2788 | 0 | array_len = g_variant_get_size (use_value); |
2789 | 0 | g_memory_buffer_write (mbuf, g_variant_get_data (use_value), array_len); |
2790 | 0 | g_variant_unref (use_value); |
2791 | 0 | } |
2792 | 0 | else |
2793 | 0 | { |
2794 | 0 | guint n; |
2795 | 0 | n = 0; |
2796 | 0 | g_variant_iter_init (&iter, value); |
2797 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2798 | 0 | { |
2799 | 0 | gsize padding_added_for_item; |
2800 | 0 | if (!append_value_to_blob (item, |
2801 | 0 | g_variant_get_type (item), |
2802 | 0 | mbuf, |
2803 | 0 | &padding_added_for_item, |
2804 | 0 | error)) |
2805 | 0 | { |
2806 | 0 | g_variant_unref (item); |
2807 | 0 | goto fail; |
2808 | 0 | } |
2809 | 0 | g_variant_unref (item); |
2810 | 0 | if (n == 0) |
2811 | 0 | { |
2812 | 0 | array_payload_begin_offset += padding_added_for_item; |
2813 | 0 | } |
2814 | 0 | n++; |
2815 | 0 | } |
2816 | 0 | } |
2817 | | |
2818 | 0 | cur_offset = mbuf->valid_len; |
2819 | 0 | array_len = cur_offset - array_payload_begin_offset; |
2820 | 0 | mbuf->pos = array_len_offset; |
2821 | |
|
2822 | 0 | g_memory_buffer_put_uint32 (mbuf, array_len); |
2823 | 0 | mbuf->pos = cur_offset; |
2824 | 0 | } |
2825 | 0 | } |
2826 | 0 | break; |
2827 | | |
2828 | 0 | default: |
2829 | 0 | if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type)) |
2830 | 0 | { |
2831 | 0 | if (!g_variant_type_first (type)) |
2832 | 0 | { |
2833 | 0 | g_set_error_literal (error, |
2834 | 0 | G_IO_ERROR, |
2835 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2836 | 0 | _("Empty structures (tuples) are not allowed in D-Bus")); |
2837 | 0 | goto fail; |
2838 | 0 | } |
2839 | | |
2840 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2841 | 0 | if (value != NULL) |
2842 | 0 | { |
2843 | 0 | GVariant *item; |
2844 | 0 | GVariantIter iter; |
2845 | 0 | g_variant_iter_init (&iter, value); |
2846 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2847 | 0 | { |
2848 | 0 | if (!append_value_to_blob (item, |
2849 | 0 | g_variant_get_type (item), |
2850 | 0 | mbuf, |
2851 | 0 | NULL, |
2852 | 0 | error)) |
2853 | 0 | { |
2854 | 0 | g_variant_unref (item); |
2855 | 0 | goto fail; |
2856 | 0 | } |
2857 | 0 | g_variant_unref (item); |
2858 | 0 | } |
2859 | 0 | } |
2860 | 0 | } |
2861 | 0 | else if (g_variant_type_is_variant (type)) |
2862 | 0 | { |
2863 | 0 | if (value != NULL) |
2864 | 0 | { |
2865 | 0 | GVariant *child; |
2866 | 0 | const gchar *signature; |
2867 | 0 | child = g_variant_get_child_value (value, 0); |
2868 | 0 | signature = g_variant_get_type_string (child); |
2869 | 0 | g_memory_buffer_put_byte (mbuf, (guint8) strlen (signature)); /* signature is already validated to be this short */ |
2870 | 0 | g_memory_buffer_put_string (mbuf, signature); |
2871 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2872 | 0 | if (!append_value_to_blob (child, |
2873 | 0 | g_variant_get_type (child), |
2874 | 0 | mbuf, |
2875 | 0 | NULL, |
2876 | 0 | error)) |
2877 | 0 | { |
2878 | 0 | g_variant_unref (child); |
2879 | 0 | goto fail; |
2880 | 0 | } |
2881 | 0 | g_variant_unref (child); |
2882 | 0 | } |
2883 | 0 | } |
2884 | 0 | else |
2885 | 0 | { |
2886 | 0 | g_set_error (error, |
2887 | 0 | G_IO_ERROR, |
2888 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2889 | 0 | _("Error serializing GVariant with type string “%s” to the D-Bus wire format"), |
2890 | 0 | g_variant_get_type_string (value)); |
2891 | 0 | goto fail; |
2892 | 0 | } |
2893 | 0 | break; |
2894 | 0 | } |
2895 | | |
2896 | 0 | if (out_padding_added != NULL) |
2897 | 0 | *out_padding_added = padding_added; |
2898 | |
|
2899 | 0 | return TRUE; |
2900 | | |
2901 | 0 | fail: |
2902 | 0 | return FALSE; |
2903 | 0 | } |
2904 | | |
2905 | | static gboolean |
2906 | | append_body_to_blob (GVariant *value, |
2907 | | GMemoryBuffer *mbuf, |
2908 | | GError **error) |
2909 | 0 | { |
2910 | 0 | GVariant *item; |
2911 | 0 | GVariantIter iter; |
2912 | |
|
2913 | 0 | if (!g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE)) |
2914 | 0 | { |
2915 | 0 | g_set_error (error, |
2916 | 0 | G_IO_ERROR, |
2917 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2918 | 0 | "Expected a tuple for the body of the GDBusMessage."); |
2919 | 0 | goto fail; |
2920 | 0 | } |
2921 | | |
2922 | 0 | g_variant_iter_init (&iter, value); |
2923 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2924 | 0 | { |
2925 | 0 | if (!append_value_to_blob (item, |
2926 | 0 | g_variant_get_type (item), |
2927 | 0 | mbuf, |
2928 | 0 | NULL, |
2929 | 0 | error)) |
2930 | 0 | { |
2931 | 0 | g_variant_unref (item); |
2932 | 0 | goto fail; |
2933 | 0 | } |
2934 | 0 | g_variant_unref (item); |
2935 | 0 | } |
2936 | 0 | return TRUE; |
2937 | | |
2938 | 0 | fail: |
2939 | 0 | return FALSE; |
2940 | 0 | } |
2941 | | |
2942 | | /* ---------------------------------------------------------------------------------------------------- */ |
2943 | | |
2944 | | /** |
2945 | | * g_dbus_message_to_blob: |
2946 | | * @message: A #GDBusMessage. |
2947 | | * @out_size: Return location for size of generated blob. |
2948 | | * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported. |
2949 | | * @error: Return location for error. |
2950 | | * |
2951 | | * Serializes @message to a blob. The byte order returned by |
2952 | | * g_dbus_message_get_byte_order() will be used. |
2953 | | * |
2954 | | * Returns: (array length=out_size) (transfer full): A pointer to a |
2955 | | * valid binary D-Bus message of @out_size bytes generated by @message |
2956 | | * or %NULL if @error is set. Free with g_free(). |
2957 | | * |
2958 | | * Since: 2.26 |
2959 | | */ |
2960 | | guchar * |
2961 | | g_dbus_message_to_blob (GDBusMessage *message, |
2962 | | gsize *out_size, |
2963 | | GDBusCapabilityFlags capabilities, |
2964 | | GError **error) |
2965 | 0 | { |
2966 | 0 | GMemoryBuffer mbuf; |
2967 | 0 | guchar *ret; |
2968 | 0 | gsize size; |
2969 | 0 | goffset body_len_offset; |
2970 | 0 | goffset body_start_offset; |
2971 | 0 | gsize body_size; |
2972 | 0 | GVariant *header_fields; |
2973 | 0 | GVariantBuilder builder; |
2974 | 0 | GHashTableIter hash_iter; |
2975 | 0 | gpointer key; |
2976 | 0 | GVariant *header_value; |
2977 | 0 | GVariant *signature; |
2978 | 0 | const gchar *signature_str; |
2979 | 0 | gint num_fds_in_message; |
2980 | 0 | gint num_fds_according_to_header; |
2981 | | |
2982 | | /* TODO: check against @capabilities */ |
2983 | |
|
2984 | 0 | ret = NULL; |
2985 | |
|
2986 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
2987 | 0 | g_return_val_if_fail (out_size != NULL, NULL); |
2988 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
2989 | | |
2990 | 0 | memset (&mbuf, 0, sizeof (mbuf)); |
2991 | 0 | mbuf.len = MIN_ARRAY_SIZE; |
2992 | 0 | mbuf.data = g_malloc (mbuf.len); |
2993 | |
|
2994 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN; |
2995 | 0 | switch (message->byte_order) |
2996 | 0 | { |
2997 | 0 | case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN: |
2998 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN; |
2999 | 0 | break; |
3000 | 0 | case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN: |
3001 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN; |
3002 | 0 | break; |
3003 | 0 | } |
3004 | | |
3005 | | /* Core header */ |
3006 | 0 | g_memory_buffer_put_byte (&mbuf, (guchar) message->byte_order); |
3007 | 0 | g_memory_buffer_put_byte (&mbuf, message->type); |
3008 | 0 | g_memory_buffer_put_byte (&mbuf, message->flags); |
3009 | 0 | g_memory_buffer_put_byte (&mbuf, 1); /* major protocol version */ |
3010 | 0 | body_len_offset = mbuf.valid_len; |
3011 | | /* body length - will be filled in later */ |
3012 | 0 | g_memory_buffer_put_uint32 (&mbuf, 0xF00DFACE); |
3013 | 0 | g_memory_buffer_put_uint32 (&mbuf, message->serial); |
3014 | |
|
3015 | 0 | num_fds_in_message = 0; |
3016 | 0 | #ifdef G_OS_UNIX |
3017 | 0 | if (message->fd_list != NULL) |
3018 | 0 | num_fds_in_message = g_unix_fd_list_get_length (message->fd_list); |
3019 | 0 | #endif |
3020 | 0 | num_fds_according_to_header = g_dbus_message_get_num_unix_fds (message); |
3021 | 0 | if (num_fds_in_message != num_fds_according_to_header) |
3022 | 0 | { |
3023 | 0 | g_set_error (error, |
3024 | 0 | G_IO_ERROR, |
3025 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
3026 | 0 | _("Number of file descriptors in message (%d) differs from header field (%d)"), |
3027 | 0 | num_fds_in_message, |
3028 | 0 | num_fds_according_to_header); |
3029 | 0 | goto out; |
3030 | 0 | } |
3031 | | |
3032 | 0 | if (!validate_headers (message, error)) |
3033 | 0 | { |
3034 | 0 | g_prefix_error (error, _("Cannot serialize message: ")); |
3035 | 0 | goto out; |
3036 | 0 | } |
3037 | | |
3038 | 0 | g_variant_builder_init_static (&builder, G_VARIANT_TYPE ("a{yv}")); |
3039 | 0 | g_hash_table_iter_init (&hash_iter, message->headers); |
3040 | 0 | while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value)) |
3041 | 0 | { |
3042 | 0 | g_variant_builder_add (&builder, |
3043 | 0 | "{yv}", |
3044 | 0 | (guchar) GPOINTER_TO_UINT (key), |
3045 | 0 | header_value); |
3046 | 0 | } |
3047 | 0 | header_fields = g_variant_builder_end (&builder); |
3048 | |
|
3049 | 0 | if (!append_value_to_blob (header_fields, |
3050 | 0 | g_variant_get_type (header_fields), |
3051 | 0 | &mbuf, |
3052 | 0 | NULL, |
3053 | 0 | error)) |
3054 | 0 | { |
3055 | 0 | g_variant_unref (header_fields); |
3056 | 0 | goto out; |
3057 | 0 | } |
3058 | 0 | g_variant_unref (header_fields); |
3059 | | |
3060 | | /* header size must be a multiple of 8 */ |
3061 | 0 | ensure_output_padding (&mbuf, 8); |
3062 | |
|
3063 | 0 | body_start_offset = mbuf.valid_len; |
3064 | |
|
3065 | 0 | signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE); |
3066 | |
|
3067 | 0 | if (signature != NULL && !g_variant_is_of_type (signature, G_VARIANT_TYPE_SIGNATURE)) |
3068 | 0 | { |
3069 | 0 | g_set_error_literal (error, |
3070 | 0 | G_IO_ERROR, |
3071 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
3072 | 0 | _("Signature header found but is not of type signature")); |
3073 | 0 | goto out; |
3074 | 0 | } |
3075 | | |
3076 | 0 | signature_str = NULL; |
3077 | 0 | if (signature != NULL) |
3078 | 0 | signature_str = g_variant_get_string (signature, NULL); |
3079 | 0 | if (message->body != NULL) |
3080 | 0 | { |
3081 | 0 | gchar *tupled_signature_str; |
3082 | 0 | if (signature == NULL) |
3083 | 0 | { |
3084 | 0 | g_set_error (error, |
3085 | 0 | G_IO_ERROR, |
3086 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
3087 | 0 | _("Message body has signature “%s” but there is no signature header"), |
3088 | 0 | g_variant_get_type_string (message->body)); |
3089 | 0 | goto out; |
3090 | 0 | } |
3091 | 0 | tupled_signature_str = g_strdup_printf ("(%s)", signature_str); |
3092 | 0 | if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->body)) != 0) |
3093 | 0 | { |
3094 | 0 | g_set_error (error, |
3095 | 0 | G_IO_ERROR, |
3096 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
3097 | 0 | _("Message body has type signature “%s” but signature in the header field is “%s”"), |
3098 | 0 | g_variant_get_type_string (message->body), tupled_signature_str); |
3099 | 0 | g_free (tupled_signature_str); |
3100 | 0 | goto out; |
3101 | 0 | } |
3102 | 0 | g_free (tupled_signature_str); |
3103 | 0 | if (!append_body_to_blob (message->body, &mbuf, error)) |
3104 | 0 | goto out; |
3105 | 0 | } |
3106 | 0 | else |
3107 | 0 | { |
3108 | 0 | if (signature != NULL && strlen (signature_str) > 0) |
3109 | 0 | { |
3110 | 0 | g_set_error (error, |
3111 | 0 | G_IO_ERROR, |
3112 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
3113 | 0 | _("Message body is empty but signature in the header field is “(%s)”"), |
3114 | 0 | signature_str); |
3115 | 0 | goto out; |
3116 | 0 | } |
3117 | 0 | } |
3118 | | |
3119 | | /* OK, we're done writing the message - set the body length */ |
3120 | 0 | size = mbuf.valid_len; |
3121 | 0 | body_size = size - body_start_offset; |
3122 | |
|
3123 | 0 | mbuf.pos = body_len_offset; |
3124 | |
|
3125 | 0 | g_memory_buffer_put_uint32 (&mbuf, body_size); |
3126 | |
|
3127 | 0 | *out_size = size; |
3128 | 0 | ret = (guchar *)mbuf.data; |
3129 | |
|
3130 | 0 | out: |
3131 | 0 | if (ret == NULL) |
3132 | 0 | g_free (mbuf.data); |
3133 | |
|
3134 | 0 | return ret; |
3135 | 0 | } |
3136 | | |
3137 | | /* ---------------------------------------------------------------------------------------------------- */ |
3138 | | |
3139 | | static guint32 |
3140 | | get_uint32_header (GDBusMessage *message, |
3141 | | GDBusMessageHeaderField header_field) |
3142 | 0 | { |
3143 | 0 | GVariant *value; |
3144 | 0 | guint32 ret; |
3145 | |
|
3146 | 0 | ret = 0; |
3147 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
3148 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32)) |
3149 | 0 | ret = g_variant_get_uint32 (value); |
3150 | |
|
3151 | 0 | return ret; |
3152 | 0 | } |
3153 | | |
3154 | | static const gchar * |
3155 | | get_string_header (GDBusMessage *message, |
3156 | | GDBusMessageHeaderField header_field) |
3157 | 0 | { |
3158 | 0 | GVariant *value; |
3159 | 0 | const gchar *ret; |
3160 | |
|
3161 | 0 | ret = NULL; |
3162 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
3163 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) |
3164 | 0 | ret = g_variant_get_string (value, NULL); |
3165 | |
|
3166 | 0 | return ret; |
3167 | 0 | } |
3168 | | |
3169 | | static const gchar * |
3170 | | get_object_path_header (GDBusMessage *message, |
3171 | | GDBusMessageHeaderField header_field) |
3172 | 0 | { |
3173 | 0 | GVariant *value; |
3174 | 0 | const gchar *ret; |
3175 | |
|
3176 | 0 | ret = NULL; |
3177 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
3178 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH)) |
3179 | 0 | ret = g_variant_get_string (value, NULL); |
3180 | |
|
3181 | 0 | return ret; |
3182 | 0 | } |
3183 | | |
3184 | | static const gchar * |
3185 | | get_signature_header (GDBusMessage *message, |
3186 | | GDBusMessageHeaderField header_field) |
3187 | 0 | { |
3188 | 0 | GVariant *value; |
3189 | 0 | const gchar *ret; |
3190 | |
|
3191 | 0 | ret = NULL; |
3192 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
3193 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE)) |
3194 | 0 | ret = g_variant_get_string (value, NULL); |
3195 | |
|
3196 | 0 | return ret; |
3197 | 0 | } |
3198 | | |
3199 | | /* ---------------------------------------------------------------------------------------------------- */ |
3200 | | |
3201 | | static void |
3202 | | set_uint32_header (GDBusMessage *message, |
3203 | | GDBusMessageHeaderField header_field, |
3204 | | guint32 value) |
3205 | 0 | { |
3206 | 0 | g_dbus_message_set_header (message, |
3207 | 0 | header_field, |
3208 | 0 | g_variant_new_uint32 (value)); |
3209 | 0 | } |
3210 | | |
3211 | | static void |
3212 | | set_string_header (GDBusMessage *message, |
3213 | | GDBusMessageHeaderField header_field, |
3214 | | const gchar *value) |
3215 | 0 | { |
3216 | 0 | g_dbus_message_set_header (message, |
3217 | 0 | header_field, |
3218 | 0 | value == NULL ? NULL : g_variant_new_string (value)); |
3219 | 0 | } |
3220 | | |
3221 | | static void |
3222 | | set_object_path_header (GDBusMessage *message, |
3223 | | GDBusMessageHeaderField header_field, |
3224 | | const gchar *value) |
3225 | 0 | { |
3226 | 0 | g_dbus_message_set_header (message, |
3227 | 0 | header_field, |
3228 | 0 | value == NULL ? NULL : g_variant_new_object_path (value)); |
3229 | 0 | } |
3230 | | |
3231 | | static void |
3232 | | set_signature_header (GDBusMessage *message, |
3233 | | GDBusMessageHeaderField header_field, |
3234 | | const gchar *value) |
3235 | 0 | { |
3236 | 0 | g_dbus_message_set_header (message, |
3237 | 0 | header_field, |
3238 | 0 | value == NULL ? NULL : g_variant_new_signature (value)); |
3239 | 0 | } |
3240 | | |
3241 | | /* ---------------------------------------------------------------------------------------------------- */ |
3242 | | |
3243 | | /** |
3244 | | * g_dbus_message_get_reply_serial: |
3245 | | * @message: A #GDBusMessage. |
3246 | | * |
3247 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field. |
3248 | | * |
3249 | | * Returns: The value. |
3250 | | * |
3251 | | * Since: 2.26 |
3252 | | */ |
3253 | | guint32 |
3254 | | g_dbus_message_get_reply_serial (GDBusMessage *message) |
3255 | 0 | { |
3256 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0); |
3257 | 0 | return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL); |
3258 | 0 | } |
3259 | | |
3260 | | /** |
3261 | | * g_dbus_message_set_reply_serial: |
3262 | | * @message: A #GDBusMessage. |
3263 | | * @value: The value to set. |
3264 | | * |
3265 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field. |
3266 | | * |
3267 | | * Since: 2.26 |
3268 | | */ |
3269 | | void |
3270 | | g_dbus_message_set_reply_serial (GDBusMessage *message, |
3271 | | guint32 value) |
3272 | 0 | { |
3273 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3274 | 0 | set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, value); |
3275 | 0 | } |
3276 | | |
3277 | | /* ---------------------------------------------------------------------------------------------------- */ |
3278 | | |
3279 | | /** |
3280 | | * g_dbus_message_get_interface: |
3281 | | * @message: A #GDBusMessage. |
3282 | | * |
3283 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field. |
3284 | | * |
3285 | | * Returns: (nullable): The value. |
3286 | | * |
3287 | | * Since: 2.26 |
3288 | | */ |
3289 | | const gchar * |
3290 | | g_dbus_message_get_interface (GDBusMessage *message) |
3291 | 0 | { |
3292 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3293 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE); |
3294 | 0 | } |
3295 | | |
3296 | | /** |
3297 | | * g_dbus_message_set_interface: |
3298 | | * @message: A #GDBusMessage. |
3299 | | * @value: (nullable): The value to set. |
3300 | | * |
3301 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field. |
3302 | | * |
3303 | | * Since: 2.26 |
3304 | | */ |
3305 | | void |
3306 | | g_dbus_message_set_interface (GDBusMessage *message, |
3307 | | const gchar *value) |
3308 | 0 | { |
3309 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3310 | 0 | g_return_if_fail (value == NULL || g_dbus_is_interface_name (value)); |
3311 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, value); |
3312 | 0 | } |
3313 | | |
3314 | | /* ---------------------------------------------------------------------------------------------------- */ |
3315 | | |
3316 | | /** |
3317 | | * g_dbus_message_get_member: |
3318 | | * @message: A #GDBusMessage. |
3319 | | * |
3320 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field. |
3321 | | * |
3322 | | * Returns: (nullable): The value. |
3323 | | * |
3324 | | * Since: 2.26 |
3325 | | */ |
3326 | | const gchar * |
3327 | | g_dbus_message_get_member (GDBusMessage *message) |
3328 | 0 | { |
3329 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3330 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER); |
3331 | 0 | } |
3332 | | |
3333 | | /** |
3334 | | * g_dbus_message_set_member: |
3335 | | * @message: A #GDBusMessage. |
3336 | | * @value: (nullable): The value to set. |
3337 | | * |
3338 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field. |
3339 | | * |
3340 | | * Since: 2.26 |
3341 | | */ |
3342 | | void |
3343 | | g_dbus_message_set_member (GDBusMessage *message, |
3344 | | const gchar *value) |
3345 | 0 | { |
3346 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3347 | 0 | g_return_if_fail (value == NULL || g_dbus_is_member_name (value)); |
3348 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, value); |
3349 | 0 | } |
3350 | | |
3351 | | /* ---------------------------------------------------------------------------------------------------- */ |
3352 | | |
3353 | | /** |
3354 | | * g_dbus_message_get_path: |
3355 | | * @message: A #GDBusMessage. |
3356 | | * |
3357 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field. |
3358 | | * |
3359 | | * Returns: (nullable): The value. |
3360 | | * |
3361 | | * Since: 2.26 |
3362 | | */ |
3363 | | const gchar * |
3364 | | g_dbus_message_get_path (GDBusMessage *message) |
3365 | 0 | { |
3366 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3367 | 0 | return get_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH); |
3368 | 0 | } |
3369 | | |
3370 | | /** |
3371 | | * g_dbus_message_set_path: |
3372 | | * @message: A #GDBusMessage. |
3373 | | * @value: (nullable): The value to set. |
3374 | | * |
3375 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field. |
3376 | | * |
3377 | | * Since: 2.26 |
3378 | | */ |
3379 | | void |
3380 | | g_dbus_message_set_path (GDBusMessage *message, |
3381 | | const gchar *value) |
3382 | 0 | { |
3383 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3384 | 0 | g_return_if_fail (value == NULL || g_variant_is_object_path (value)); |
3385 | 0 | set_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, value); |
3386 | 0 | } |
3387 | | |
3388 | | /* ---------------------------------------------------------------------------------------------------- */ |
3389 | | |
3390 | | /** |
3391 | | * g_dbus_message_get_sender: |
3392 | | * @message: A #GDBusMessage. |
3393 | | * |
3394 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field. |
3395 | | * |
3396 | | * Returns: (nullable): The value. |
3397 | | * |
3398 | | * Since: 2.26 |
3399 | | */ |
3400 | | const gchar * |
3401 | | g_dbus_message_get_sender (GDBusMessage *message) |
3402 | 0 | { |
3403 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3404 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER); |
3405 | 0 | } |
3406 | | |
3407 | | /** |
3408 | | * g_dbus_message_set_sender: |
3409 | | * @message: A #GDBusMessage. |
3410 | | * @value: (nullable): The value to set. |
3411 | | * |
3412 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field. |
3413 | | * |
3414 | | * Since: 2.26 |
3415 | | */ |
3416 | | void |
3417 | | g_dbus_message_set_sender (GDBusMessage *message, |
3418 | | const gchar *value) |
3419 | 0 | { |
3420 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3421 | 0 | g_return_if_fail (value == NULL || g_dbus_is_name (value)); |
3422 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER, value); |
3423 | 0 | } |
3424 | | |
3425 | | /* ---------------------------------------------------------------------------------------------------- */ |
3426 | | |
3427 | | /** |
3428 | | * g_dbus_message_get_destination: |
3429 | | * @message: A #GDBusMessage. |
3430 | | * |
3431 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field. |
3432 | | * |
3433 | | * Returns: (nullable): The value. |
3434 | | * |
3435 | | * Since: 2.26 |
3436 | | */ |
3437 | | const gchar * |
3438 | | g_dbus_message_get_destination (GDBusMessage *message) |
3439 | 0 | { |
3440 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3441 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION); |
3442 | 0 | } |
3443 | | |
3444 | | /** |
3445 | | * g_dbus_message_set_destination: |
3446 | | * @message: A #GDBusMessage. |
3447 | | * @value: (nullable): The value to set. |
3448 | | * |
3449 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field. |
3450 | | * |
3451 | | * Since: 2.26 |
3452 | | */ |
3453 | | void |
3454 | | g_dbus_message_set_destination (GDBusMessage *message, |
3455 | | const gchar *value) |
3456 | 0 | { |
3457 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3458 | 0 | g_return_if_fail (value == NULL || g_dbus_is_name (value)); |
3459 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION, value); |
3460 | 0 | } |
3461 | | |
3462 | | /* ---------------------------------------------------------------------------------------------------- */ |
3463 | | |
3464 | | /** |
3465 | | * g_dbus_message_get_error_name: |
3466 | | * @message: A #GDBusMessage. |
3467 | | * |
3468 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field. |
3469 | | * |
3470 | | * Returns: (nullable): The value. |
3471 | | * |
3472 | | * Since: 2.26 |
3473 | | */ |
3474 | | const gchar * |
3475 | | g_dbus_message_get_error_name (GDBusMessage *message) |
3476 | 0 | { |
3477 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3478 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME); |
3479 | 0 | } |
3480 | | |
3481 | | /** |
3482 | | * g_dbus_message_set_error_name: |
3483 | | * @message: (nullable): A #GDBusMessage. |
3484 | | * @value: The value to set. |
3485 | | * |
3486 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field. |
3487 | | * |
3488 | | * Since: 2.26 |
3489 | | */ |
3490 | | void |
3491 | | g_dbus_message_set_error_name (GDBusMessage *message, |
3492 | | const gchar *value) |
3493 | 0 | { |
3494 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3495 | 0 | g_return_if_fail (value == NULL || g_dbus_is_error_name (value)); |
3496 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, value); |
3497 | 0 | } |
3498 | | |
3499 | | /* ---------------------------------------------------------------------------------------------------- */ |
3500 | | |
3501 | | /** |
3502 | | * g_dbus_message_get_signature: |
3503 | | * @message: A #GDBusMessage. |
3504 | | * |
3505 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field. |
3506 | | * |
3507 | | * This will always be non-%NULL, but may be an empty string. |
3508 | | * |
3509 | | * Returns: (not nullable): The value. |
3510 | | * |
3511 | | * Since: 2.26 |
3512 | | */ |
3513 | | const gchar * |
3514 | | g_dbus_message_get_signature (GDBusMessage *message) |
3515 | 0 | { |
3516 | 0 | const gchar *ret; |
3517 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3518 | 0 | ret = get_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE); |
3519 | 0 | if (ret == NULL) |
3520 | 0 | ret = ""; |
3521 | 0 | return ret; |
3522 | 0 | } |
3523 | | |
3524 | | /** |
3525 | | * g_dbus_message_set_signature: |
3526 | | * @message: A #GDBusMessage. |
3527 | | * @value: (nullable): The value to set. |
3528 | | * |
3529 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field. |
3530 | | * |
3531 | | * Since: 2.26 |
3532 | | */ |
3533 | | void |
3534 | | g_dbus_message_set_signature (GDBusMessage *message, |
3535 | | const gchar *value) |
3536 | 0 | { |
3537 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3538 | 0 | g_return_if_fail (value == NULL || g_variant_is_signature (value)); |
3539 | 0 | set_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE, value); |
3540 | 0 | } |
3541 | | |
3542 | | /* ---------------------------------------------------------------------------------------------------- */ |
3543 | | |
3544 | | /** |
3545 | | * g_dbus_message_get_arg0: |
3546 | | * @message: A #GDBusMessage. |
3547 | | * |
3548 | | * Convenience to get the first item in the body of @message. |
3549 | | * |
3550 | | * See [method@Gio.DBusMessage.get_arg0_path] for returning object-path-typed |
3551 | | * arg0 values. |
3552 | | * |
3553 | | * Returns: (nullable): The string item or %NULL if the first item in the body of |
3554 | | * @message is not a string. |
3555 | | * |
3556 | | * Since: 2.26 |
3557 | | */ |
3558 | | const gchar * |
3559 | | g_dbus_message_get_arg0 (GDBusMessage *message) |
3560 | 0 | { |
3561 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3562 | | |
3563 | 0 | if (message->arg0_cache != NULL && |
3564 | 0 | g_variant_is_of_type (message->arg0_cache, G_VARIANT_TYPE_STRING)) |
3565 | 0 | return g_variant_get_string (message->arg0_cache, NULL); |
3566 | | |
3567 | 0 | return NULL; |
3568 | 0 | } |
3569 | | |
3570 | | /** |
3571 | | * g_dbus_message_get_arg0_path: |
3572 | | * @message: A `GDBusMessage`. |
3573 | | * |
3574 | | * Convenience to get the first item in the body of @message. |
3575 | | * |
3576 | | * See [method@Gio.DBusMessage.get_arg0] for returning string-typed arg0 values. |
3577 | | * |
3578 | | * Returns: (nullable): The object path item or `NULL` if the first item in the |
3579 | | * body of @message is not an object path. |
3580 | | * |
3581 | | * Since: 2.80 |
3582 | | */ |
3583 | | const gchar * |
3584 | | g_dbus_message_get_arg0_path (GDBusMessage *message) |
3585 | 0 | { |
3586 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3587 | | |
3588 | 0 | if (message->arg0_cache != NULL && |
3589 | 0 | g_variant_is_of_type (message->arg0_cache, G_VARIANT_TYPE_OBJECT_PATH)) |
3590 | 0 | return g_variant_get_string (message->arg0_cache, NULL); |
3591 | | |
3592 | 0 | return NULL; |
3593 | 0 | } |
3594 | | |
3595 | | /* ---------------------------------------------------------------------------------------------------- */ |
3596 | | |
3597 | | /** |
3598 | | * g_dbus_message_get_num_unix_fds: |
3599 | | * @message: A #GDBusMessage. |
3600 | | * |
3601 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field. |
3602 | | * |
3603 | | * Returns: The value. |
3604 | | * |
3605 | | * Since: 2.26 |
3606 | | */ |
3607 | | guint32 |
3608 | | g_dbus_message_get_num_unix_fds (GDBusMessage *message) |
3609 | 0 | { |
3610 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0); |
3611 | 0 | return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS); |
3612 | 0 | } |
3613 | | |
3614 | | /** |
3615 | | * g_dbus_message_set_num_unix_fds: |
3616 | | * @message: A #GDBusMessage. |
3617 | | * @value: The value to set. |
3618 | | * |
3619 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field. |
3620 | | * |
3621 | | * Since: 2.26 |
3622 | | */ |
3623 | | void |
3624 | | g_dbus_message_set_num_unix_fds (GDBusMessage *message, |
3625 | | guint32 value) |
3626 | 0 | { |
3627 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3628 | 0 | set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS, value); |
3629 | 0 | } |
3630 | | |
3631 | | /* ---------------------------------------------------------------------------------------------------- */ |
3632 | | |
3633 | | /** |
3634 | | * g_dbus_message_to_gerror: |
3635 | | * @message: A #GDBusMessage. |
3636 | | * @error: The #GError to set. |
3637 | | * |
3638 | | * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does |
3639 | | * nothing and returns %FALSE. |
3640 | | * |
3641 | | * Otherwise this method encodes the error in @message as a #GError |
3642 | | * using g_dbus_error_set_dbus_error() using the information in the |
3643 | | * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as |
3644 | | * well as the first string item in @message's body. |
3645 | | * |
3646 | | * Returns: %TRUE if @error was set, %FALSE otherwise. |
3647 | | * |
3648 | | * Since: 2.26 |
3649 | | */ |
3650 | | gboolean |
3651 | | g_dbus_message_to_gerror (GDBusMessage *message, |
3652 | | GError **error) |
3653 | 0 | { |
3654 | 0 | gboolean ret; |
3655 | 0 | const gchar *error_name; |
3656 | |
|
3657 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
3658 | | |
3659 | 0 | ret = FALSE; |
3660 | 0 | if (message->type != G_DBUS_MESSAGE_TYPE_ERROR) |
3661 | 0 | goto out; |
3662 | | |
3663 | 0 | error_name = g_dbus_message_get_error_name (message); |
3664 | 0 | if (error_name != NULL) |
3665 | 0 | { |
3666 | 0 | GVariant *body; |
3667 | |
|
3668 | 0 | body = g_dbus_message_get_body (message); |
3669 | |
|
3670 | 0 | if (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(s)"))) |
3671 | 0 | { |
3672 | 0 | const gchar *error_message; |
3673 | 0 | g_variant_get (body, "(&s)", &error_message); |
3674 | 0 | g_dbus_error_set_dbus_error (error, |
3675 | 0 | error_name, |
3676 | 0 | error_message, |
3677 | 0 | NULL); |
3678 | 0 | } |
3679 | 0 | else |
3680 | 0 | { |
3681 | | /* these two situations are valid, yet pretty rare */ |
3682 | 0 | if (body != NULL) |
3683 | 0 | { |
3684 | 0 | g_dbus_error_set_dbus_error (error, |
3685 | 0 | error_name, |
3686 | 0 | "", |
3687 | 0 | _("Error return with body of type “%s”"), |
3688 | 0 | g_variant_get_type_string (body)); |
3689 | 0 | } |
3690 | 0 | else |
3691 | 0 | { |
3692 | 0 | g_dbus_error_set_dbus_error (error, |
3693 | 0 | error_name, |
3694 | 0 | "", |
3695 | 0 | _("Error return with empty body")); |
3696 | 0 | } |
3697 | 0 | } |
3698 | 0 | } |
3699 | 0 | else |
3700 | 0 | { |
3701 | | /* TODO: this shouldn't happen - should check this at message serialization |
3702 | | * time and disconnect the peer. |
3703 | | */ |
3704 | 0 | g_set_error (error, |
3705 | 0 | G_IO_ERROR, |
3706 | 0 | G_IO_ERROR_FAILED, |
3707 | 0 | "Error return without error-name header!"); |
3708 | 0 | } |
3709 | |
|
3710 | 0 | ret = TRUE; |
3711 | |
|
3712 | 0 | out: |
3713 | 0 | return ret; |
3714 | 0 | } |
3715 | | |
3716 | | /* ---------------------------------------------------------------------------------------------------- */ |
3717 | | |
3718 | | static gchar * |
3719 | | flags_to_string (GType flags_type, guint value) |
3720 | 0 | { |
3721 | 0 | GString *s; |
3722 | 0 | GFlagsClass *klass; |
3723 | 0 | guint n; |
3724 | |
|
3725 | 0 | klass = g_type_class_ref (flags_type); |
3726 | 0 | s = g_string_new (NULL); |
3727 | 0 | for (n = 0; n < 32; n++) |
3728 | 0 | { |
3729 | 0 | if ((value & (1<<n)) != 0) |
3730 | 0 | { |
3731 | 0 | GFlagsValue *flags_value; |
3732 | 0 | flags_value = g_flags_get_first_value (klass, (1<<n)); |
3733 | 0 | if (s->len > 0) |
3734 | 0 | g_string_append_c (s, ','); |
3735 | 0 | if (flags_value != NULL) |
3736 | 0 | g_string_append (s, flags_value->value_nick); |
3737 | 0 | else |
3738 | 0 | g_string_append_printf (s, "unknown (bit %d)", n); |
3739 | 0 | } |
3740 | 0 | } |
3741 | 0 | if (s->len == 0) |
3742 | 0 | g_string_append (s, "none"); |
3743 | 0 | g_type_class_unref (klass); |
3744 | 0 | return g_string_free (s, FALSE); |
3745 | 0 | } |
3746 | | |
3747 | | static gint |
3748 | | _sort_keys_func (gconstpointer a, |
3749 | | gconstpointer b) |
3750 | 0 | { |
3751 | 0 | gint ia; |
3752 | 0 | gint ib; |
3753 | |
|
3754 | 0 | ia = GPOINTER_TO_INT (a); |
3755 | 0 | ib = GPOINTER_TO_INT (b); |
3756 | |
|
3757 | 0 | return ia - ib; |
3758 | 0 | } |
3759 | | |
3760 | | /** |
3761 | | * g_dbus_message_print: |
3762 | | * @message: A #GDBusMessage. |
3763 | | * @indent: Indentation level. |
3764 | | * |
3765 | | * Produces a human-readable multi-line description of @message. |
3766 | | * |
3767 | | * The contents of the description has no ABI guarantees, the contents |
3768 | | * and formatting is subject to change at any time. Typical output |
3769 | | * looks something like this: |
3770 | | * ``` |
3771 | | * Type: method-call |
3772 | | * Flags: none |
3773 | | * Version: 0 |
3774 | | * Serial: 4 |
3775 | | * Headers: |
3776 | | * path -> objectpath '/org/gtk/GDBus/TestObject' |
3777 | | * interface -> 'org.gtk.GDBus.TestInterface' |
3778 | | * member -> 'GimmeStdout' |
3779 | | * destination -> ':1.146' |
3780 | | * Body: () |
3781 | | * UNIX File Descriptors: |
3782 | | * (none) |
3783 | | * ``` |
3784 | | * or |
3785 | | * ``` |
3786 | | * Type: method-return |
3787 | | * Flags: no-reply-expected |
3788 | | * Version: 0 |
3789 | | * Serial: 477 |
3790 | | * Headers: |
3791 | | * reply-serial -> uint32 4 |
3792 | | * destination -> ':1.159' |
3793 | | * sender -> ':1.146' |
3794 | | * num-unix-fds -> uint32 1 |
3795 | | * Body: () |
3796 | | * UNIX File Descriptors: |
3797 | | * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635 |
3798 | | * ``` |
3799 | | * |
3800 | | * Returns: (not nullable): A string that should be freed with [func@GLib.free]. |
3801 | | * |
3802 | | * Since: 2.26 |
3803 | | */ |
3804 | | gchar * |
3805 | | g_dbus_message_print (GDBusMessage *message, |
3806 | | guint indent) |
3807 | 0 | { |
3808 | 0 | GString *str; |
3809 | 0 | gchar *s; |
3810 | 0 | GList *keys; |
3811 | 0 | GList *l; |
3812 | |
|
3813 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3814 | | |
3815 | 0 | str = g_string_new (NULL); |
3816 | |
|
3817 | 0 | s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->type); |
3818 | 0 | g_string_append_printf (str, "%*sType: %s\n", indent, "", s); |
3819 | 0 | g_free (s); |
3820 | 0 | s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->flags); |
3821 | 0 | g_string_append_printf (str, "%*sFlags: %s\n", indent, "", s); |
3822 | 0 | g_free (s); |
3823 | 0 | g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->major_protocol_version); |
3824 | 0 | g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->serial); |
3825 | |
|
3826 | 0 | g_string_append_printf (str, "%*sHeaders:\n", indent, ""); |
3827 | 0 | keys = g_hash_table_get_keys (message->headers); |
3828 | 0 | keys = g_list_sort (keys, _sort_keys_func); |
3829 | 0 | if (keys != NULL) |
3830 | 0 | { |
3831 | 0 | for (l = keys; l != NULL; l = l->next) |
3832 | 0 | { |
3833 | 0 | gint key = GPOINTER_TO_INT (l->data); |
3834 | 0 | GVariant *value; |
3835 | 0 | gchar *value_str; |
3836 | |
|
3837 | 0 | value = g_hash_table_lookup (message->headers, l->data); |
3838 | 0 | g_assert (value != NULL); |
3839 | | |
3840 | 0 | s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key); |
3841 | 0 | value_str = g_variant_print (value, TRUE); |
3842 | 0 | g_string_append_printf (str, "%*s %s -> %s\n", indent, "", s, value_str); |
3843 | 0 | g_free (s); |
3844 | 0 | g_free (value_str); |
3845 | 0 | } |
3846 | 0 | } |
3847 | 0 | else |
3848 | 0 | { |
3849 | 0 | g_string_append_printf (str, "%*s (none)\n", indent, ""); |
3850 | 0 | } |
3851 | 0 | g_list_free (keys); |
3852 | 0 | g_string_append_printf (str, "%*sBody: ", indent, ""); |
3853 | 0 | if (message->body != NULL) |
3854 | 0 | { |
3855 | 0 | g_variant_print_string (message->body, |
3856 | 0 | str, |
3857 | 0 | TRUE); |
3858 | 0 | } |
3859 | 0 | else |
3860 | 0 | { |
3861 | 0 | g_string_append (str, "()"); |
3862 | 0 | } |
3863 | 0 | g_string_append (str, "\n"); |
3864 | 0 | #ifdef G_OS_UNIX |
3865 | 0 | g_string_append_printf (str, "%*sUNIX File Descriptors:\n", indent, ""); |
3866 | 0 | if (message->fd_list != NULL) |
3867 | 0 | { |
3868 | 0 | gint num_fds; |
3869 | 0 | const gint *fds; |
3870 | 0 | gint n; |
3871 | |
|
3872 | 0 | fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds); |
3873 | 0 | if (num_fds > 0) |
3874 | 0 | { |
3875 | 0 | for (n = 0; n < num_fds; n++) |
3876 | 0 | { |
3877 | 0 | GString *fs; |
3878 | 0 | struct stat statbuf; |
3879 | 0 | fs = g_string_new (NULL); |
3880 | 0 | if (fstat (fds[n], &statbuf) == 0) |
3881 | 0 | { |
3882 | 0 | #ifndef MAJOR_MINOR_NOT_FOUND |
3883 | 0 | g_string_append_printf (fs, "%s" "dev=%d:%d", fs->len > 0 ? "," : "", |
3884 | 0 | (gint) major (statbuf.st_dev), (gint) minor (statbuf.st_dev)); |
3885 | 0 | #endif |
3886 | 0 | g_string_append_printf (fs, "%s" "mode=0%o", fs->len > 0 ? "," : "", |
3887 | 0 | (guint) statbuf.st_mode); |
3888 | 0 | g_string_append_printf (fs, "%s" "ino=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3889 | 0 | (guint64) statbuf.st_ino); |
3890 | 0 | g_string_append_printf (fs, "%s" "uid=%u", fs->len > 0 ? "," : "", |
3891 | 0 | (guint) statbuf.st_uid); |
3892 | 0 | g_string_append_printf (fs, "%s" "gid=%u", fs->len > 0 ? "," : "", |
3893 | 0 | (guint) statbuf.st_gid); |
3894 | 0 | #ifndef MAJOR_MINOR_NOT_FOUND |
3895 | 0 | g_string_append_printf (fs, "%s" "rdev=%d:%d", fs->len > 0 ? "," : "", |
3896 | 0 | (gint) major (statbuf.st_rdev), (gint) minor (statbuf.st_rdev)); |
3897 | 0 | #endif |
3898 | 0 | g_string_append_printf (fs, "%s" "size=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3899 | 0 | (guint64) statbuf.st_size); |
3900 | 0 | g_string_append_printf (fs, "%s" "atime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3901 | 0 | (guint64) statbuf.st_atime); |
3902 | 0 | g_string_append_printf (fs, "%s" "mtime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3903 | 0 | (guint64) statbuf.st_mtime); |
3904 | 0 | g_string_append_printf (fs, "%s" "ctime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3905 | 0 | (guint64) statbuf.st_ctime); |
3906 | 0 | } |
3907 | 0 | else |
3908 | 0 | { |
3909 | 0 | int errsv = errno; |
3910 | 0 | g_string_append_printf (fs, "(fstat failed: %s)", g_strerror (errsv)); |
3911 | 0 | } |
3912 | 0 | g_string_append_printf (str, "%*s fd %d: %s\n", indent, "", fds[n], fs->str); |
3913 | 0 | g_string_free (fs, TRUE); |
3914 | 0 | } |
3915 | 0 | } |
3916 | 0 | else |
3917 | 0 | { |
3918 | 0 | g_string_append_printf (str, "%*s (empty)\n", indent, ""); |
3919 | 0 | } |
3920 | 0 | } |
3921 | 0 | else |
3922 | 0 | { |
3923 | 0 | g_string_append_printf (str, "%*s (none)\n", indent, ""); |
3924 | 0 | } |
3925 | 0 | #endif |
3926 | |
|
3927 | 0 | return g_string_free (str, FALSE); |
3928 | 0 | } |
3929 | | |
3930 | | /** |
3931 | | * g_dbus_message_get_locked: |
3932 | | * @message: A #GDBusMessage. |
3933 | | * |
3934 | | * Checks whether @message is locked. To monitor changes to this |
3935 | | * value, conncet to the #GObject::notify signal to listen for changes |
3936 | | * on the #GDBusMessage:locked property. |
3937 | | * |
3938 | | * Returns: %TRUE if @message is locked, %FALSE otherwise. |
3939 | | * |
3940 | | * Since: 2.26 |
3941 | | */ |
3942 | | gboolean |
3943 | | g_dbus_message_get_locked (GDBusMessage *message) |
3944 | 0 | { |
3945 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
3946 | 0 | return message->locked; |
3947 | 0 | } |
3948 | | |
3949 | | /** |
3950 | | * g_dbus_message_lock: |
3951 | | * @message: A #GDBusMessage. |
3952 | | * |
3953 | | * If @message is locked, does nothing. Otherwise locks the message. |
3954 | | * |
3955 | | * Since: 2.26 |
3956 | | */ |
3957 | | void |
3958 | | g_dbus_message_lock (GDBusMessage *message) |
3959 | 0 | { |
3960 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3961 | | |
3962 | 0 | if (message->locked) |
3963 | 0 | goto out; |
3964 | | |
3965 | 0 | message->locked = TRUE; |
3966 | 0 | g_object_notify (G_OBJECT (message), "locked"); |
3967 | |
|
3968 | 0 | out: |
3969 | 0 | ; |
3970 | 0 | } |
3971 | | |
3972 | | /** |
3973 | | * g_dbus_message_copy: |
3974 | | * @message: A #GDBusMessage. |
3975 | | * @error: Return location for error or %NULL. |
3976 | | * |
3977 | | * Copies @message. The copy is a deep copy and the returned |
3978 | | * #GDBusMessage is completely identical except that it is guaranteed |
3979 | | * to not be locked. |
3980 | | * |
3981 | | * This operation can fail if e.g. @message contains file descriptors |
3982 | | * and the per-process or system-wide open files limit is reached. |
3983 | | * |
3984 | | * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set. |
3985 | | * Free with g_object_unref(). |
3986 | | * |
3987 | | * Since: 2.26 |
3988 | | */ |
3989 | | GDBusMessage * |
3990 | | g_dbus_message_copy (GDBusMessage *message, |
3991 | | GError **error) |
3992 | 0 | { |
3993 | 0 | GDBusMessage *ret; |
3994 | 0 | GHashTableIter iter; |
3995 | 0 | gpointer header_key; |
3996 | 0 | GVariant *header_value; |
3997 | |
|
3998 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3999 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
4000 | | |
4001 | 0 | ret = g_dbus_message_new (); |
4002 | 0 | ret->type = message->type; |
4003 | 0 | ret->flags = message->flags; |
4004 | 0 | ret->byte_order = message->byte_order; |
4005 | 0 | ret->major_protocol_version = message->major_protocol_version; |
4006 | 0 | ret->serial = message->serial; |
4007 | |
|
4008 | 0 | #ifdef G_OS_UNIX |
4009 | 0 | if (message->fd_list != NULL) |
4010 | 0 | { |
4011 | 0 | gint n; |
4012 | 0 | gint num_fds; |
4013 | 0 | const gint *fds; |
4014 | |
|
4015 | 0 | ret->fd_list = g_unix_fd_list_new (); |
4016 | 0 | fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds); |
4017 | 0 | for (n = 0; n < num_fds; n++) |
4018 | 0 | { |
4019 | 0 | if (g_unix_fd_list_append (ret->fd_list, |
4020 | 0 | fds[n], |
4021 | 0 | error) == -1) |
4022 | 0 | { |
4023 | 0 | g_object_unref (ret); |
4024 | 0 | ret = NULL; |
4025 | 0 | goto out; |
4026 | 0 | } |
4027 | 0 | } |
4028 | 0 | } |
4029 | 0 | #endif |
4030 | | |
4031 | | /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine |
4032 | | * to just ref (as opposed to deep-copying) the GVariant instances |
4033 | | */ |
4034 | 0 | ret->body = message->body != NULL ? g_variant_ref (message->body) : NULL; |
4035 | 0 | ret->arg0_cache = message->arg0_cache != NULL ? g_variant_ref (message->arg0_cache) : NULL; |
4036 | 0 | g_hash_table_iter_init (&iter, message->headers); |
4037 | 0 | while (g_hash_table_iter_next (&iter, &header_key, (gpointer) &header_value)) |
4038 | 0 | g_hash_table_insert (ret->headers, header_key, g_variant_ref (header_value)); |
4039 | |
|
4040 | 0 | #ifdef G_OS_UNIX |
4041 | 0 | out: |
4042 | 0 | #endif |
4043 | 0 | return ret; |
4044 | 0 | } |