/src/tinysparql/subprojects/glib-2.80.3/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. |
988 | | * |
989 | | * Sets the serial for @message. |
990 | | * |
991 | | * Since: 2.26 |
992 | | */ |
993 | | void |
994 | | g_dbus_message_set_serial (GDBusMessage *message, |
995 | | guint32 serial) |
996 | 0 | { |
997 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
998 | | |
999 | 0 | if (message->locked) |
1000 | 0 | { |
1001 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1002 | 0 | return; |
1003 | 0 | } |
1004 | | |
1005 | 0 | message->serial = serial; |
1006 | 0 | } |
1007 | | |
1008 | | /* ---------------------------------------------------------------------------------------------------- */ |
1009 | | |
1010 | | /* TODO: need GI annotations to specify that any guchar value goes for header_field */ |
1011 | | |
1012 | | /** |
1013 | | * g_dbus_message_get_header: |
1014 | | * @message: A #GDBusMessage. |
1015 | | * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration) |
1016 | | * |
1017 | | * Gets a header field on @message. |
1018 | | * |
1019 | | * The caller is responsible for checking the type of the returned #GVariant |
1020 | | * matches what is expected. |
1021 | | * |
1022 | | * Returns: (transfer none) (nullable): A #GVariant with the value if the header was found, %NULL |
1023 | | * otherwise. Do not free, it is owned by @message. |
1024 | | * |
1025 | | * Since: 2.26 |
1026 | | */ |
1027 | | GVariant * |
1028 | | g_dbus_message_get_header (GDBusMessage *message, |
1029 | | GDBusMessageHeaderField header_field) |
1030 | 0 | { |
1031 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1032 | 0 | g_return_val_if_fail ((guint) header_field < 256, NULL); |
1033 | 0 | return g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
1034 | 0 | } |
1035 | | |
1036 | | /** |
1037 | | * g_dbus_message_set_header: |
1038 | | * @message: A #GDBusMessage. |
1039 | | * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration) |
1040 | | * @value: (nullable): A #GVariant to set the header field or %NULL to clear the header field. |
1041 | | * |
1042 | | * Sets a header field on @message. |
1043 | | * |
1044 | | * If @value is floating, @message assumes ownership of @value. |
1045 | | * |
1046 | | * Since: 2.26 |
1047 | | */ |
1048 | | void |
1049 | | g_dbus_message_set_header (GDBusMessage *message, |
1050 | | GDBusMessageHeaderField header_field, |
1051 | | GVariant *value) |
1052 | 0 | { |
1053 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1054 | 0 | g_return_if_fail ((guint) header_field < 256); |
1055 | | |
1056 | 0 | if (message->locked) |
1057 | 0 | { |
1058 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1059 | 0 | return; |
1060 | 0 | } |
1061 | | |
1062 | 0 | if (value == NULL) |
1063 | 0 | { |
1064 | 0 | g_hash_table_remove (message->headers, GUINT_TO_POINTER (header_field)); |
1065 | 0 | } |
1066 | 0 | else |
1067 | 0 | { |
1068 | 0 | g_hash_table_insert (message->headers, GUINT_TO_POINTER (header_field), g_variant_ref_sink (value)); |
1069 | 0 | } |
1070 | 0 | } |
1071 | | |
1072 | | /** |
1073 | | * g_dbus_message_get_header_fields: |
1074 | | * @message: A #GDBusMessage. |
1075 | | * |
1076 | | * Gets an array of all header fields on @message that are set. |
1077 | | * |
1078 | | * Returns: (array zero-terminated=1): An array of header fields |
1079 | | * terminated by %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element |
1080 | | * is a #guchar. Free with g_free(). |
1081 | | * |
1082 | | * Since: 2.26 |
1083 | | */ |
1084 | | guchar * |
1085 | | g_dbus_message_get_header_fields (GDBusMessage *message) |
1086 | 0 | { |
1087 | 0 | GPtrArray *keys; |
1088 | 0 | GArray *array; |
1089 | |
|
1090 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1091 | | |
1092 | 0 | keys = g_hash_table_get_keys_as_ptr_array (message->headers); |
1093 | 0 | array = g_array_sized_new (FALSE, FALSE, sizeof (guchar), keys->len + 1); |
1094 | |
|
1095 | 0 | for (guint i = 0; i < keys->len; ++i) |
1096 | 0 | { |
1097 | 0 | guchar val = GPOINTER_TO_UINT (g_ptr_array_index (keys, i)); |
1098 | 0 | g_array_append_val (array, val); |
1099 | 0 | } |
1100 | |
|
1101 | 0 | g_assert (array->len == keys->len); |
1102 | 0 | g_clear_pointer (&keys, g_ptr_array_unref); |
1103 | |
|
1104 | 0 | guchar invalid_field = G_DBUS_MESSAGE_HEADER_FIELD_INVALID; |
1105 | 0 | g_array_append_val (array, invalid_field); |
1106 | |
|
1107 | 0 | return (guchar *) g_array_free (array, FALSE); |
1108 | 0 | } |
1109 | | |
1110 | | /* ---------------------------------------------------------------------------------------------------- */ |
1111 | | |
1112 | | /** |
1113 | | * g_dbus_message_get_body: |
1114 | | * @message: A #GDBusMessage. |
1115 | | * |
1116 | | * Gets the body of a message. |
1117 | | * |
1118 | | * Returns: (nullable) (transfer none): A #GVariant or %NULL if the body is |
1119 | | * empty. Do not free, it is owned by @message. |
1120 | | * |
1121 | | * Since: 2.26 |
1122 | | */ |
1123 | | GVariant * |
1124 | | g_dbus_message_get_body (GDBusMessage *message) |
1125 | 0 | { |
1126 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1127 | 0 | return message->body; |
1128 | 0 | } |
1129 | | |
1130 | | /** |
1131 | | * g_dbus_message_set_body: |
1132 | | * @message: A #GDBusMessage. |
1133 | | * @body: Either %NULL or a #GVariant that is a tuple. |
1134 | | * |
1135 | | * Sets the body @message. As a side-effect the |
1136 | | * %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field is set to the |
1137 | | * type string of @body (or cleared if @body is %NULL). |
1138 | | * |
1139 | | * If @body is floating, @message assumes ownership of @body. |
1140 | | * |
1141 | | * Since: 2.26 |
1142 | | */ |
1143 | | void |
1144 | | g_dbus_message_set_body (GDBusMessage *message, |
1145 | | GVariant *body) |
1146 | 0 | { |
1147 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1148 | 0 | g_return_if_fail ((body == NULL) || g_variant_is_of_type (body, G_VARIANT_TYPE_TUPLE)); |
1149 | | |
1150 | 0 | if (message->locked) |
1151 | 0 | { |
1152 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1153 | 0 | return; |
1154 | 0 | } |
1155 | | |
1156 | 0 | if (message->body != NULL) |
1157 | 0 | g_variant_unref (message->body); |
1158 | |
|
1159 | 0 | g_clear_pointer (&message->arg0_cache, g_variant_unref); |
1160 | |
|
1161 | 0 | if (body == NULL) |
1162 | 0 | { |
1163 | 0 | message->body = NULL; |
1164 | 0 | g_dbus_message_set_signature (message, NULL); |
1165 | 0 | } |
1166 | 0 | else |
1167 | 0 | { |
1168 | 0 | const gchar *type_string; |
1169 | 0 | gsize type_string_len; |
1170 | 0 | gchar *signature; |
1171 | |
|
1172 | 0 | message->body = g_variant_ref_sink (body); |
1173 | |
|
1174 | 0 | if (g_variant_is_of_type (message->body, G_VARIANT_TYPE_TUPLE) && |
1175 | 0 | g_variant_n_children (message->body) > 0) |
1176 | 0 | message->arg0_cache = g_variant_get_child_value (message->body, 0); |
1177 | |
|
1178 | 0 | type_string = g_variant_get_type_string (body); |
1179 | 0 | type_string_len = strlen (type_string); |
1180 | 0 | g_assert (type_string_len >= 2); |
1181 | 0 | signature = g_strndup (type_string + 1, type_string_len - 2); |
1182 | 0 | g_dbus_message_set_signature (message, signature); |
1183 | 0 | g_free (signature); |
1184 | 0 | } |
1185 | 0 | } |
1186 | | |
1187 | | /* ---------------------------------------------------------------------------------------------------- */ |
1188 | | |
1189 | | #ifdef G_OS_UNIX |
1190 | | /** |
1191 | | * g_dbus_message_get_unix_fd_list: |
1192 | | * @message: A #GDBusMessage. |
1193 | | * |
1194 | | * Gets the UNIX file descriptors associated with @message, if any. |
1195 | | * |
1196 | | * This method is only available on UNIX. |
1197 | | * |
1198 | | * The file descriptors normally correspond to %G_VARIANT_TYPE_HANDLE |
1199 | | * values in the body of the message. For example, |
1200 | | * if g_variant_get_handle() returns 5, that is intended to be a reference |
1201 | | * to the file descriptor that can be accessed by |
1202 | | * `g_unix_fd_list_get (list, 5, ...)`. |
1203 | | * |
1204 | | * Returns: (nullable) (transfer none): A #GUnixFDList or %NULL if no file descriptors are |
1205 | | * associated. Do not free, this object is owned by @message. |
1206 | | * |
1207 | | * Since: 2.26 |
1208 | | */ |
1209 | | GUnixFDList * |
1210 | | g_dbus_message_get_unix_fd_list (GDBusMessage *message) |
1211 | 0 | { |
1212 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1213 | 0 | return message->fd_list; |
1214 | 0 | } |
1215 | | |
1216 | | /** |
1217 | | * g_dbus_message_set_unix_fd_list: |
1218 | | * @message: A #GDBusMessage. |
1219 | | * @fd_list: (nullable): A #GUnixFDList or %NULL. |
1220 | | * |
1221 | | * Sets the UNIX file descriptors associated with @message. As a |
1222 | | * side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header |
1223 | | * field is set to the number of fds in @fd_list (or cleared if |
1224 | | * @fd_list is %NULL). |
1225 | | * |
1226 | | * This method is only available on UNIX. |
1227 | | * |
1228 | | * When designing D-Bus APIs that are intended to be interoperable, |
1229 | | * please note that non-GDBus implementations of D-Bus can usually only |
1230 | | * access file descriptors if they are referenced by a value of type |
1231 | | * %G_VARIANT_TYPE_HANDLE in the body of the message. |
1232 | | * |
1233 | | * Since: 2.26 |
1234 | | */ |
1235 | | void |
1236 | | g_dbus_message_set_unix_fd_list (GDBusMessage *message, |
1237 | | GUnixFDList *fd_list) |
1238 | 0 | { |
1239 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1240 | 0 | g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list)); |
1241 | | |
1242 | 0 | if (message->locked) |
1243 | 0 | { |
1244 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1245 | 0 | return; |
1246 | 0 | } |
1247 | | |
1248 | 0 | if (message->fd_list != NULL) |
1249 | 0 | g_object_unref (message->fd_list); |
1250 | 0 | if (fd_list != NULL) |
1251 | 0 | { |
1252 | 0 | message->fd_list = g_object_ref (fd_list); |
1253 | 0 | g_dbus_message_set_num_unix_fds (message, g_unix_fd_list_get_length (fd_list)); |
1254 | 0 | } |
1255 | 0 | else |
1256 | 0 | { |
1257 | 0 | message->fd_list = NULL; |
1258 | 0 | g_dbus_message_set_num_unix_fds (message, 0); |
1259 | 0 | } |
1260 | 0 | } |
1261 | | #endif |
1262 | | |
1263 | | /* ---------------------------------------------------------------------------------------------------- */ |
1264 | | |
1265 | | static guint |
1266 | | get_type_fixed_size (const GVariantType *type) |
1267 | 0 | { |
1268 | | /* NB: we do not treat 'b' as fixed-size here because GVariant and |
1269 | | * D-Bus disagree about the size. |
1270 | | */ |
1271 | 0 | switch (*g_variant_type_peek_string (type)) |
1272 | 0 | { |
1273 | 0 | case 'y': |
1274 | 0 | return 1; |
1275 | 0 | case 'n': case 'q': |
1276 | 0 | return 2; |
1277 | 0 | case 'i': case 'u': case 'h': |
1278 | 0 | return 4; |
1279 | 0 | case 'x': case 't': case 'd': |
1280 | 0 | return 8; |
1281 | 0 | default: |
1282 | 0 | return 0; |
1283 | 0 | } |
1284 | 0 | } |
1285 | | |
1286 | | static const char * |
1287 | | message_type_to_string (GDBusMessageType message_type) |
1288 | 0 | { |
1289 | 0 | switch (message_type) |
1290 | 0 | { |
1291 | 0 | case G_DBUS_MESSAGE_TYPE_INVALID: |
1292 | 0 | return "INVALID"; |
1293 | 0 | case G_DBUS_MESSAGE_TYPE_METHOD_CALL: |
1294 | 0 | return "METHOD_CALL"; |
1295 | 0 | case G_DBUS_MESSAGE_TYPE_METHOD_RETURN: |
1296 | 0 | return "METHOD_RETURN"; |
1297 | 0 | case G_DBUS_MESSAGE_TYPE_ERROR: |
1298 | 0 | return "ERROR"; |
1299 | 0 | case G_DBUS_MESSAGE_TYPE_SIGNAL: |
1300 | 0 | return "SIGNAL"; |
1301 | 0 | default: |
1302 | 0 | return "unknown-type"; |
1303 | 0 | } |
1304 | 0 | } |
1305 | | |
1306 | | static const char * |
1307 | | message_header_field_to_string (GDBusMessageHeaderField field) |
1308 | 0 | { |
1309 | 0 | switch (field) |
1310 | 0 | { |
1311 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_INVALID: |
1312 | 0 | return "INVALID"; |
1313 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_PATH: |
1314 | 0 | return "PATH"; |
1315 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE: |
1316 | 0 | return "INTERFACE"; |
1317 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_MEMBER: |
1318 | 0 | return "MEMBER"; |
1319 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME: |
1320 | 0 | return "ERROR_NAME"; |
1321 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL: |
1322 | 0 | return "REPLY_SERIAL"; |
1323 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION: |
1324 | 0 | return "DESTINATION"; |
1325 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_SENDER: |
1326 | 0 | return "SENDER"; |
1327 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE: |
1328 | 0 | return "SIGNATURE"; |
1329 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS: |
1330 | 0 | return "NUM_UNIX_FDS"; |
1331 | 0 | default: |
1332 | 0 | return "unknown-field"; |
1333 | 0 | } |
1334 | 0 | } |
1335 | | |
1336 | | static gboolean |
1337 | | validate_header (GDBusMessage *message, |
1338 | | GDBusMessageHeaderField field, |
1339 | | GVariant *header_value, |
1340 | | const GVariantType *expected_type, |
1341 | | GError **error) |
1342 | 0 | { |
1343 | 0 | g_assert (header_value != NULL); |
1344 | | |
1345 | 0 | if (!g_variant_is_of_type (header_value, expected_type)) |
1346 | 0 | { |
1347 | 0 | char *expected_type_string = g_variant_type_dup_string (expected_type); |
1348 | 0 | g_set_error (error, |
1349 | 0 | G_IO_ERROR, |
1350 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1351 | 0 | _("%s message: %s header field is invalid; expected a value of type ‘%s’"), |
1352 | 0 | message_type_to_string (message->type), |
1353 | 0 | message_header_field_to_string (field), |
1354 | 0 | expected_type_string); |
1355 | 0 | g_free (expected_type_string); |
1356 | 0 | return FALSE; |
1357 | 0 | } |
1358 | | |
1359 | 0 | return TRUE; |
1360 | 0 | } |
1361 | | |
1362 | | static gboolean |
1363 | | require_header (GDBusMessage *message, |
1364 | | GDBusMessageHeaderField field, |
1365 | | GError **error) |
1366 | 0 | { |
1367 | 0 | GVariant *header_value = g_dbus_message_get_header (message, field); |
1368 | |
|
1369 | 0 | if (header_value == NULL) |
1370 | 0 | { |
1371 | 0 | g_set_error (error, |
1372 | 0 | G_IO_ERROR, |
1373 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1374 | 0 | _("%s message: %s header field is missing or invalid"), |
1375 | 0 | message_type_to_string (message->type), |
1376 | 0 | message_header_field_to_string (field)); |
1377 | 0 | return FALSE; |
1378 | 0 | } |
1379 | | |
1380 | 0 | return TRUE; |
1381 | 0 | } |
1382 | | |
1383 | | /* Implement the validation rules given in |
1384 | | * https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-header-fields */ |
1385 | | static gboolean |
1386 | | validate_headers (GDBusMessage *message, |
1387 | | GError **error) |
1388 | 0 | { |
1389 | 0 | gboolean ret; |
1390 | 0 | GHashTableIter headers_iter; |
1391 | 0 | gpointer key; |
1392 | 0 | GVariant *header_value; |
1393 | |
|
1394 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
1395 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
1396 | | |
1397 | 0 | ret = FALSE; |
1398 | | |
1399 | | /* Validate the types of all known headers. */ |
1400 | 0 | g_hash_table_iter_init (&headers_iter, message->headers); |
1401 | 0 | while (g_hash_table_iter_next (&headers_iter, &key, (gpointer) &header_value)) |
1402 | 0 | { |
1403 | 0 | GDBusMessageHeaderField field_type = GPOINTER_TO_INT (key); |
1404 | |
|
1405 | 0 | switch (field_type) |
1406 | 0 | { |
1407 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_INVALID: |
1408 | | /* The invalid header must be rejected as per |
1409 | | * https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-header-fields */ |
1410 | 0 | g_set_error (error, |
1411 | 0 | G_IO_ERROR, |
1412 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1413 | 0 | _("%s message: INVALID header field supplied"), |
1414 | 0 | message_type_to_string (message->type)); |
1415 | 0 | goto out; |
1416 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_PATH: |
1417 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_OBJECT_PATH, error)) |
1418 | 0 | goto out; |
1419 | 0 | if (g_strcmp0 (g_variant_get_string (header_value, NULL), "/org/freedesktop/DBus/Local") == 0) |
1420 | 0 | { |
1421 | 0 | g_set_error (error, |
1422 | 0 | G_IO_ERROR, |
1423 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1424 | 0 | _("%s message: PATH header field is using the reserved value /org/freedesktop/DBus/Local"), |
1425 | 0 | message_type_to_string (message->type)); |
1426 | 0 | goto out; |
1427 | 0 | } |
1428 | 0 | break; |
1429 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE: |
1430 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_STRING, error)) |
1431 | 0 | goto out; |
1432 | 0 | if (!g_dbus_is_interface_name (g_variant_get_string (header_value, NULL))) |
1433 | 0 | { |
1434 | 0 | g_set_error (error, |
1435 | 0 | G_IO_ERROR, |
1436 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1437 | 0 | _("%s message: INTERFACE header field does not contain a valid interface name"), |
1438 | 0 | message_type_to_string (message->type)); |
1439 | 0 | goto out; |
1440 | 0 | } |
1441 | 0 | if (g_strcmp0 (g_variant_get_string (header_value, NULL), "org.freedesktop.DBus.Local") == 0) |
1442 | 0 | { |
1443 | 0 | g_set_error (error, |
1444 | 0 | G_IO_ERROR, |
1445 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1446 | 0 | _("%s message: INTERFACE header field is using the reserved value org.freedesktop.DBus.Local"), |
1447 | 0 | message_type_to_string (message->type)); |
1448 | 0 | goto out; |
1449 | 0 | } |
1450 | 0 | break; |
1451 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_MEMBER: |
1452 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_STRING, error)) |
1453 | 0 | goto out; |
1454 | 0 | if (!g_dbus_is_member_name (g_variant_get_string (header_value, NULL))) |
1455 | 0 | { |
1456 | 0 | g_set_error (error, |
1457 | 0 | G_IO_ERROR, |
1458 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1459 | 0 | _("%s message: MEMBER header field does not contain a valid member name"), |
1460 | 0 | message_type_to_string (message->type)); |
1461 | 0 | goto out; |
1462 | 0 | } |
1463 | 0 | break; |
1464 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME: |
1465 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_STRING, error)) |
1466 | 0 | goto out; |
1467 | 0 | if (!g_dbus_is_error_name (g_variant_get_string (header_value, NULL))) |
1468 | 0 | { |
1469 | 0 | g_set_error (error, |
1470 | 0 | G_IO_ERROR, |
1471 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1472 | 0 | _("%s message: ERROR_NAME header field does not contain a valid error name"), |
1473 | 0 | message_type_to_string (message->type)); |
1474 | 0 | goto out; |
1475 | 0 | } |
1476 | 0 | break; |
1477 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL: |
1478 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_UINT32, error)) |
1479 | 0 | goto out; |
1480 | 0 | break; |
1481 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION: |
1482 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_STRING, error)) |
1483 | 0 | goto out; |
1484 | 0 | break; |
1485 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_SENDER: |
1486 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_STRING, error)) |
1487 | 0 | goto out; |
1488 | 0 | break; |
1489 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE: |
1490 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_SIGNATURE, error)) |
1491 | 0 | goto out; |
1492 | 0 | break; |
1493 | 0 | case G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS: |
1494 | 0 | if (!validate_header (message, field_type, header_value, G_VARIANT_TYPE_UINT32, error)) |
1495 | 0 | goto out; |
1496 | 0 | break; |
1497 | 0 | default: |
1498 | | /* Ignore unknown fields as per |
1499 | | * https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-header-fields. */ |
1500 | 0 | continue; |
1501 | 0 | } |
1502 | 0 | } |
1503 | | |
1504 | | /* Check for message-type-specific required headers. */ |
1505 | 0 | switch (message->type) |
1506 | 0 | { |
1507 | 0 | case G_DBUS_MESSAGE_TYPE_INVALID: |
1508 | 0 | g_set_error_literal (error, |
1509 | 0 | G_IO_ERROR, |
1510 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1511 | 0 | _("type is INVALID")); |
1512 | 0 | goto out; |
1513 | | |
1514 | 0 | case G_DBUS_MESSAGE_TYPE_METHOD_CALL: |
1515 | 0 | if (!require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, error) || |
1516 | 0 | !require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, error)) |
1517 | 0 | goto out; |
1518 | 0 | break; |
1519 | | |
1520 | 0 | case G_DBUS_MESSAGE_TYPE_METHOD_RETURN: |
1521 | 0 | if (!require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, error)) |
1522 | 0 | goto out; |
1523 | 0 | break; |
1524 | | |
1525 | 0 | case G_DBUS_MESSAGE_TYPE_ERROR: |
1526 | 0 | if (!require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, error) || |
1527 | 0 | !require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, error)) |
1528 | 0 | goto out; |
1529 | 0 | break; |
1530 | | |
1531 | 0 | case G_DBUS_MESSAGE_TYPE_SIGNAL: |
1532 | 0 | if (!require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, error) || |
1533 | 0 | !require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, error) || |
1534 | 0 | !require_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, error)) |
1535 | 0 | goto out; |
1536 | 0 | break; |
1537 | | |
1538 | 0 | default: |
1539 | | /* hitherto unknown type - nothing to check */ |
1540 | 0 | break; |
1541 | 0 | } |
1542 | | |
1543 | 0 | ret = TRUE; |
1544 | |
|
1545 | 0 | out: |
1546 | 0 | g_assert (ret || (error == NULL || *error != NULL)); |
1547 | 0 | return ret; |
1548 | 0 | } |
1549 | | |
1550 | | /* ---------------------------------------------------------------------------------------------------- */ |
1551 | | |
1552 | | static gboolean |
1553 | | ensure_input_padding (GMemoryBuffer *buf, |
1554 | | gsize padding_size) |
1555 | 0 | { |
1556 | 0 | gsize offset; |
1557 | 0 | gsize wanted_offset; |
1558 | |
|
1559 | 0 | offset = buf->pos; |
1560 | 0 | wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size; |
1561 | 0 | buf->pos = wanted_offset; |
1562 | 0 | return TRUE; |
1563 | 0 | } |
1564 | | |
1565 | | static const gchar * |
1566 | | read_string (GMemoryBuffer *mbuf, |
1567 | | gsize len, |
1568 | | GError **error) |
1569 | 0 | { |
1570 | 0 | gchar *str; |
1571 | 0 | const gchar *end_valid; |
1572 | |
|
1573 | 0 | if G_UNLIKELY (mbuf->pos + len >= mbuf->valid_len || mbuf->pos + len < mbuf->pos) |
1574 | 0 | { |
1575 | 0 | mbuf->pos = mbuf->valid_len; |
1576 | | /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */ |
1577 | 0 | g_set_error (error, |
1578 | 0 | G_IO_ERROR, |
1579 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1580 | 0 | g_dngettext (GETTEXT_PACKAGE, |
1581 | 0 | "Wanted to read %lu byte but only got %lu", |
1582 | 0 | "Wanted to read %lu bytes but only got %lu", |
1583 | 0 | (gulong)len), |
1584 | 0 | (gulong)len, |
1585 | 0 | (gulong)(mbuf->valid_len - mbuf->pos)); |
1586 | 0 | return NULL; |
1587 | 0 | } |
1588 | | |
1589 | 0 | if G_UNLIKELY (mbuf->data[mbuf->pos + len] != '\0') |
1590 | 0 | { |
1591 | 0 | str = g_strndup (mbuf->data + mbuf->pos, len); |
1592 | 0 | g_set_error (error, |
1593 | 0 | G_IO_ERROR, |
1594 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1595 | 0 | _("Expected NUL byte after the string “%s” but found byte %d"), |
1596 | 0 | str, mbuf->data[mbuf->pos + len]); |
1597 | 0 | g_free (str); |
1598 | 0 | mbuf->pos += len + 1; |
1599 | 0 | return NULL; |
1600 | 0 | } |
1601 | | |
1602 | 0 | str = mbuf->data + mbuf->pos; |
1603 | 0 | mbuf->pos += len + 1; |
1604 | |
|
1605 | 0 | if G_UNLIKELY (!g_utf8_validate (str, -1, &end_valid)) |
1606 | 0 | { |
1607 | 0 | gint offset; |
1608 | 0 | gchar *valid_str; |
1609 | 0 | offset = (gint) (end_valid - str); |
1610 | 0 | valid_str = g_strndup (str, offset); |
1611 | 0 | g_set_error (error, |
1612 | 0 | G_IO_ERROR, |
1613 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1614 | 0 | _("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). " |
1615 | 0 | "The valid UTF-8 string up until that point was “%s”"), |
1616 | 0 | offset, |
1617 | 0 | (gint) len, |
1618 | 0 | valid_str); |
1619 | 0 | g_free (valid_str); |
1620 | 0 | return NULL; |
1621 | 0 | } |
1622 | | |
1623 | 0 | return str; |
1624 | 0 | } |
1625 | | |
1626 | | static gconstpointer |
1627 | | read_bytes (GMemoryBuffer *mbuf, |
1628 | | gsize len, |
1629 | | GError **error) |
1630 | 0 | { |
1631 | 0 | gconstpointer result; |
1632 | |
|
1633 | 0 | if G_UNLIKELY (mbuf->pos + len > mbuf->valid_len || mbuf->pos + len < mbuf->pos) |
1634 | 0 | { |
1635 | 0 | mbuf->pos = mbuf->valid_len; |
1636 | | /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */ |
1637 | 0 | g_set_error (error, |
1638 | 0 | G_IO_ERROR, |
1639 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1640 | 0 | g_dngettext (GETTEXT_PACKAGE, |
1641 | 0 | "Wanted to read %lu byte but only got %lu", |
1642 | 0 | "Wanted to read %lu bytes but only got %lu", |
1643 | 0 | (gulong)len), |
1644 | 0 | (gulong)len, |
1645 | 0 | (gulong)(mbuf->valid_len - mbuf->pos)); |
1646 | 0 | return NULL; |
1647 | 0 | } |
1648 | | |
1649 | 0 | result = mbuf->data + mbuf->pos; |
1650 | 0 | mbuf->pos += len; |
1651 | |
|
1652 | 0 | return result; |
1653 | 0 | } |
1654 | | |
1655 | | /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */ |
1656 | | |
1657 | | /* returns a non-floating GVariant! */ |
1658 | | static GVariant * |
1659 | | parse_value_from_blob (GMemoryBuffer *buf, |
1660 | | const GVariantType *type, |
1661 | | guint max_depth, |
1662 | | gboolean just_align, |
1663 | | guint indent, |
1664 | | GError **error) |
1665 | 0 | { |
1666 | 0 | GVariant *ret = NULL; |
1667 | 0 | GError *local_error = NULL; |
1668 | | #ifdef DEBUG_SERIALIZER |
1669 | | gboolean is_leaf; |
1670 | | #endif /* DEBUG_SERIALIZER */ |
1671 | 0 | const gchar *type_string; |
1672 | |
|
1673 | 0 | if (max_depth == 0) |
1674 | 0 | { |
1675 | 0 | g_set_error_literal (&local_error, |
1676 | 0 | G_IO_ERROR, |
1677 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1678 | 0 | _("Value nested too deeply")); |
1679 | 0 | goto fail; |
1680 | 0 | } |
1681 | | |
1682 | 0 | type_string = g_variant_type_peek_string (type); |
1683 | |
|
1684 | | #ifdef DEBUG_SERIALIZER |
1685 | | { |
1686 | | gchar *s; |
1687 | | s = g_variant_type_dup_string (type); |
1688 | | g_print ("%*s%s type %s from offset 0x%04x", |
1689 | | indent, "", |
1690 | | just_align ? "Aligning" : "Reading", |
1691 | | s, |
1692 | | (gint) buf->pos); |
1693 | | g_free (s); |
1694 | | } |
1695 | | #endif /* DEBUG_SERIALIZER */ |
1696 | |
|
1697 | | #ifdef DEBUG_SERIALIZER |
1698 | | is_leaf = TRUE; |
1699 | | #endif /* DEBUG_SERIALIZER */ |
1700 | 0 | switch (type_string[0]) |
1701 | 0 | { |
1702 | 0 | case 'b': /* G_VARIANT_TYPE_BOOLEAN */ |
1703 | 0 | ensure_input_padding (buf, 4); |
1704 | 0 | if (!just_align) |
1705 | 0 | { |
1706 | 0 | gboolean v; |
1707 | 0 | v = g_memory_buffer_read_uint32 (buf, &local_error); |
1708 | 0 | if (local_error) |
1709 | 0 | goto fail; |
1710 | 0 | ret = g_variant_new_boolean (v); |
1711 | 0 | } |
1712 | 0 | break; |
1713 | | |
1714 | 0 | case 'y': /* G_VARIANT_TYPE_BYTE */ |
1715 | 0 | if (!just_align) |
1716 | 0 | { |
1717 | 0 | guchar v; |
1718 | 0 | v = g_memory_buffer_read_byte (buf, &local_error); |
1719 | 0 | if (local_error) |
1720 | 0 | goto fail; |
1721 | 0 | ret = g_variant_new_byte (v); |
1722 | 0 | } |
1723 | 0 | break; |
1724 | | |
1725 | 0 | case 'n': /* G_VARIANT_TYPE_INT16 */ |
1726 | 0 | ensure_input_padding (buf, 2); |
1727 | 0 | if (!just_align) |
1728 | 0 | { |
1729 | 0 | gint16 v; |
1730 | 0 | v = g_memory_buffer_read_int16 (buf, &local_error); |
1731 | 0 | if (local_error) |
1732 | 0 | goto fail; |
1733 | 0 | ret = g_variant_new_int16 (v); |
1734 | 0 | } |
1735 | 0 | break; |
1736 | | |
1737 | 0 | case 'q': /* G_VARIANT_TYPE_UINT16 */ |
1738 | 0 | ensure_input_padding (buf, 2); |
1739 | 0 | if (!just_align) |
1740 | 0 | { |
1741 | 0 | guint16 v; |
1742 | 0 | v = g_memory_buffer_read_uint16 (buf, &local_error); |
1743 | 0 | if (local_error) |
1744 | 0 | goto fail; |
1745 | 0 | ret = g_variant_new_uint16 (v); |
1746 | 0 | } |
1747 | 0 | break; |
1748 | | |
1749 | 0 | case 'i': /* G_VARIANT_TYPE_INT32 */ |
1750 | 0 | ensure_input_padding (buf, 4); |
1751 | 0 | if (!just_align) |
1752 | 0 | { |
1753 | 0 | gint32 v; |
1754 | 0 | v = g_memory_buffer_read_int32 (buf, &local_error); |
1755 | 0 | if (local_error) |
1756 | 0 | goto fail; |
1757 | 0 | ret = g_variant_new_int32 (v); |
1758 | 0 | } |
1759 | 0 | break; |
1760 | | |
1761 | 0 | case 'u': /* G_VARIANT_TYPE_UINT32 */ |
1762 | 0 | ensure_input_padding (buf, 4); |
1763 | 0 | if (!just_align) |
1764 | 0 | { |
1765 | 0 | guint32 v; |
1766 | 0 | v = g_memory_buffer_read_uint32 (buf, &local_error); |
1767 | 0 | if (local_error) |
1768 | 0 | goto fail; |
1769 | 0 | ret = g_variant_new_uint32 (v); |
1770 | 0 | } |
1771 | 0 | break; |
1772 | | |
1773 | 0 | case 'x': /* G_VARIANT_TYPE_INT64 */ |
1774 | 0 | ensure_input_padding (buf, 8); |
1775 | 0 | if (!just_align) |
1776 | 0 | { |
1777 | 0 | gint64 v; |
1778 | 0 | v = g_memory_buffer_read_int64 (buf, &local_error); |
1779 | 0 | if (local_error) |
1780 | 0 | goto fail; |
1781 | 0 | ret = g_variant_new_int64 (v); |
1782 | 0 | } |
1783 | 0 | break; |
1784 | | |
1785 | 0 | case 't': /* G_VARIANT_TYPE_UINT64 */ |
1786 | 0 | ensure_input_padding (buf, 8); |
1787 | 0 | if (!just_align) |
1788 | 0 | { |
1789 | 0 | guint64 v; |
1790 | 0 | v = g_memory_buffer_read_uint64 (buf, &local_error); |
1791 | 0 | if (local_error) |
1792 | 0 | goto fail; |
1793 | 0 | ret = g_variant_new_uint64 (v); |
1794 | 0 | } |
1795 | 0 | break; |
1796 | | |
1797 | 0 | case 'd': /* G_VARIANT_TYPE_DOUBLE */ |
1798 | 0 | ensure_input_padding (buf, 8); |
1799 | 0 | if (!just_align) |
1800 | 0 | { |
1801 | 0 | union { |
1802 | 0 | guint64 v_uint64; |
1803 | 0 | gdouble v_double; |
1804 | 0 | } u; |
1805 | 0 | G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64)); |
1806 | 0 | u.v_uint64 = g_memory_buffer_read_uint64 (buf, &local_error); |
1807 | 0 | if (local_error) |
1808 | 0 | goto fail; |
1809 | 0 | ret = g_variant_new_double (u.v_double); |
1810 | 0 | } |
1811 | 0 | break; |
1812 | | |
1813 | 0 | case 's': /* G_VARIANT_TYPE_STRING */ |
1814 | 0 | ensure_input_padding (buf, 4); |
1815 | 0 | if (!just_align) |
1816 | 0 | { |
1817 | 0 | guint32 len; |
1818 | 0 | const gchar *v; |
1819 | 0 | len = g_memory_buffer_read_uint32 (buf, &local_error); |
1820 | 0 | if (local_error) |
1821 | 0 | goto fail; |
1822 | 0 | v = read_string (buf, (gsize) len, &local_error); |
1823 | 0 | if (v == NULL) |
1824 | 0 | goto fail; |
1825 | 0 | ret = g_variant_new_string (v); |
1826 | 0 | } |
1827 | 0 | break; |
1828 | | |
1829 | 0 | case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */ |
1830 | 0 | ensure_input_padding (buf, 4); |
1831 | 0 | if (!just_align) |
1832 | 0 | { |
1833 | 0 | guint32 len; |
1834 | 0 | const gchar *v; |
1835 | 0 | len = g_memory_buffer_read_uint32 (buf, &local_error); |
1836 | 0 | if (local_error) |
1837 | 0 | goto fail; |
1838 | 0 | v = read_string (buf, (gsize) len, &local_error); |
1839 | 0 | if (v == NULL) |
1840 | 0 | goto fail; |
1841 | 0 | if (!g_variant_is_object_path (v)) |
1842 | 0 | { |
1843 | 0 | g_set_error (&local_error, |
1844 | 0 | G_IO_ERROR, |
1845 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1846 | 0 | _("Parsed value “%s” is not a valid D-Bus object path"), |
1847 | 0 | v); |
1848 | 0 | goto fail; |
1849 | 0 | } |
1850 | 0 | ret = g_variant_new_object_path (v); |
1851 | 0 | } |
1852 | 0 | break; |
1853 | | |
1854 | 0 | case 'g': /* G_VARIANT_TYPE_SIGNATURE */ |
1855 | 0 | if (!just_align) |
1856 | 0 | { |
1857 | 0 | guchar len; |
1858 | 0 | const gchar *v; |
1859 | 0 | len = g_memory_buffer_read_byte (buf, &local_error); |
1860 | 0 | if (local_error) |
1861 | 0 | goto fail; |
1862 | 0 | v = read_string (buf, (gsize) len, &local_error); |
1863 | 0 | if (v == NULL) |
1864 | 0 | goto fail; |
1865 | 0 | if (!g_variant_is_signature (v)) |
1866 | 0 | { |
1867 | 0 | g_set_error (&local_error, |
1868 | 0 | G_IO_ERROR, |
1869 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1870 | 0 | _("Parsed value “%s” is not a valid D-Bus signature"), |
1871 | 0 | v); |
1872 | 0 | goto fail; |
1873 | 0 | } |
1874 | 0 | ret = g_variant_new_signature (v); |
1875 | 0 | } |
1876 | 0 | break; |
1877 | | |
1878 | 0 | case 'h': /* G_VARIANT_TYPE_HANDLE */ |
1879 | 0 | ensure_input_padding (buf, 4); |
1880 | 0 | if (!just_align) |
1881 | 0 | { |
1882 | 0 | gint32 v; |
1883 | 0 | v = g_memory_buffer_read_int32 (buf, &local_error); |
1884 | 0 | if (local_error) |
1885 | 0 | goto fail; |
1886 | 0 | ret = g_variant_new_handle (v); |
1887 | 0 | } |
1888 | 0 | break; |
1889 | | |
1890 | 0 | case 'a': /* G_VARIANT_TYPE_ARRAY */ |
1891 | 0 | ensure_input_padding (buf, 4); |
1892 | | |
1893 | | /* If we are only aligning for this array type, it is the child type of |
1894 | | * another array, which is empty. So, we do not need to add padding for |
1895 | | * this nonexistent array's elements: we only need to align for this |
1896 | | * array itself (4 bytes). See |
1897 | | * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>. |
1898 | | */ |
1899 | 0 | if (!just_align) |
1900 | 0 | { |
1901 | 0 | guint32 array_len; |
1902 | 0 | const GVariantType *element_type; |
1903 | 0 | guint fixed_size; |
1904 | |
|
1905 | 0 | array_len = g_memory_buffer_read_uint32 (buf, &local_error); |
1906 | 0 | if (local_error) |
1907 | 0 | goto fail; |
1908 | | |
1909 | | #ifdef DEBUG_SERIALIZER |
1910 | | is_leaf = FALSE; |
1911 | | g_print (": array spans 0x%04x bytes\n", array_len); |
1912 | | #endif /* DEBUG_SERIALIZER */ |
1913 | | |
1914 | 0 | if (array_len > (2<<26)) |
1915 | 0 | { |
1916 | | /* G_GUINT32_FORMAT doesn't work with gettext, so use u */ |
1917 | 0 | g_set_error (&local_error, |
1918 | 0 | G_IO_ERROR, |
1919 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1920 | 0 | g_dngettext (GETTEXT_PACKAGE, |
1921 | 0 | "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).", |
1922 | 0 | "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).", |
1923 | 0 | array_len), |
1924 | 0 | array_len); |
1925 | 0 | goto fail; |
1926 | 0 | } |
1927 | | |
1928 | 0 | element_type = g_variant_type_element (type); |
1929 | 0 | fixed_size = get_type_fixed_size (element_type); |
1930 | | |
1931 | | /* Fast-path the cases like 'ay', etc. */ |
1932 | 0 | if (fixed_size != 0) |
1933 | 0 | { |
1934 | 0 | gconstpointer array_data; |
1935 | |
|
1936 | 0 | if (array_len % fixed_size != 0) |
1937 | 0 | { |
1938 | 0 | g_set_error (&local_error, |
1939 | 0 | G_IO_ERROR, |
1940 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1941 | 0 | _("Encountered array of type “a%c”, expected to have a length a multiple " |
1942 | 0 | "of %u bytes, but found to be %u bytes in length"), |
1943 | 0 | g_variant_type_peek_string (element_type)[0], fixed_size, array_len); |
1944 | 0 | goto fail; |
1945 | 0 | } |
1946 | | |
1947 | 0 | if (max_depth == 1) |
1948 | 0 | { |
1949 | | /* If we had recursed into parse_value_from_blob() again to |
1950 | | * parse the array values, this would have been emitted. */ |
1951 | 0 | g_set_error_literal (&local_error, |
1952 | 0 | G_IO_ERROR, |
1953 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1954 | 0 | _("Value nested too deeply")); |
1955 | 0 | goto fail; |
1956 | 0 | } |
1957 | | |
1958 | 0 | ensure_input_padding (buf, fixed_size); |
1959 | 0 | array_data = read_bytes (buf, array_len, &local_error); |
1960 | 0 | if (array_data == NULL) |
1961 | 0 | goto fail; |
1962 | | |
1963 | 0 | ret = g_variant_new_fixed_array (element_type, array_data, array_len / fixed_size, fixed_size); |
1964 | |
|
1965 | 0 | if (g_memory_buffer_is_byteswapped (buf)) |
1966 | 0 | { |
1967 | 0 | GVariant *tmp = g_variant_ref_sink (ret); |
1968 | 0 | ret = g_variant_byteswap (tmp); |
1969 | 0 | g_variant_unref (tmp); |
1970 | 0 | } |
1971 | 0 | } |
1972 | 0 | else |
1973 | 0 | { |
1974 | 0 | GVariantBuilder builder; |
1975 | 0 | goffset offset; |
1976 | 0 | goffset target; |
1977 | |
|
1978 | 0 | g_variant_builder_init (&builder, type); |
1979 | |
|
1980 | 0 | if (array_len == 0) |
1981 | 0 | { |
1982 | 0 | GVariant *item G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */; |
1983 | 0 | item = parse_value_from_blob (buf, |
1984 | 0 | element_type, |
1985 | 0 | max_depth - 1, |
1986 | 0 | TRUE, |
1987 | 0 | indent + 2, |
1988 | 0 | NULL); |
1989 | 0 | g_assert (item == NULL); |
1990 | 0 | } |
1991 | 0 | else |
1992 | 0 | { |
1993 | 0 | offset = buf->pos; |
1994 | 0 | target = offset + array_len; |
1995 | 0 | while (offset < target) |
1996 | 0 | { |
1997 | 0 | GVariant *item; |
1998 | 0 | item = parse_value_from_blob (buf, |
1999 | 0 | element_type, |
2000 | 0 | max_depth - 1, |
2001 | 0 | FALSE, |
2002 | 0 | indent + 2, |
2003 | 0 | &local_error); |
2004 | 0 | if (item == NULL) |
2005 | 0 | { |
2006 | 0 | g_variant_builder_clear (&builder); |
2007 | 0 | goto fail; |
2008 | 0 | } |
2009 | 0 | g_variant_builder_add_value (&builder, item); |
2010 | 0 | g_variant_unref (item); |
2011 | | |
2012 | | /* Array elements must not be zero-length. There are no |
2013 | | * valid zero-length serialisations of any types which |
2014 | | * can be array elements in the D-Bus wire format, so this |
2015 | | * assertion should always hold. |
2016 | | * |
2017 | | * See https://gitlab.gnome.org/GNOME/glib/-/issues/2557 |
2018 | | */ |
2019 | 0 | g_assert (buf->pos > (gsize) offset); |
2020 | | |
2021 | 0 | offset = buf->pos; |
2022 | 0 | } |
2023 | 0 | } |
2024 | | |
2025 | 0 | ret = g_variant_builder_end (&builder); |
2026 | 0 | } |
2027 | 0 | } |
2028 | 0 | break; |
2029 | | |
2030 | 0 | default: |
2031 | 0 | if (g_variant_type_is_dict_entry (type)) |
2032 | 0 | { |
2033 | 0 | const GVariantType *key_type; |
2034 | 0 | const GVariantType *value_type; |
2035 | 0 | GVariant *key; |
2036 | 0 | GVariant *value; |
2037 | |
|
2038 | 0 | ensure_input_padding (buf, 8); |
2039 | |
|
2040 | | #ifdef DEBUG_SERIALIZER |
2041 | | is_leaf = FALSE; |
2042 | | g_print ("\n"); |
2043 | | #endif /* DEBUG_SERIALIZER */ |
2044 | |
|
2045 | 0 | if (!just_align) |
2046 | 0 | { |
2047 | 0 | key_type = g_variant_type_key (type); |
2048 | 0 | key = parse_value_from_blob (buf, |
2049 | 0 | key_type, |
2050 | 0 | max_depth - 1, |
2051 | 0 | FALSE, |
2052 | 0 | indent + 2, |
2053 | 0 | &local_error); |
2054 | 0 | if (key == NULL) |
2055 | 0 | goto fail; |
2056 | 0 | value_type = g_variant_type_value (type); |
2057 | 0 | value = parse_value_from_blob (buf, |
2058 | 0 | value_type, |
2059 | 0 | max_depth - 1, |
2060 | 0 | FALSE, |
2061 | 0 | indent + 2, |
2062 | 0 | &local_error); |
2063 | 0 | if (value == NULL) |
2064 | 0 | { |
2065 | 0 | g_variant_unref (key); |
2066 | 0 | goto fail; |
2067 | 0 | } |
2068 | 0 | ret = g_variant_new_dict_entry (key, value); |
2069 | 0 | g_variant_unref (key); |
2070 | 0 | g_variant_unref (value); |
2071 | 0 | } |
2072 | 0 | } |
2073 | 0 | else if (g_variant_type_is_tuple (type)) |
2074 | 0 | { |
2075 | 0 | ensure_input_padding (buf, 8); |
2076 | |
|
2077 | | #ifdef DEBUG_SERIALIZER |
2078 | | is_leaf = FALSE; |
2079 | | g_print ("\n"); |
2080 | | #endif /* DEBUG_SERIALIZER */ |
2081 | |
|
2082 | 0 | if (!just_align) |
2083 | 0 | { |
2084 | 0 | const GVariantType *element_type; |
2085 | 0 | GVariantBuilder builder; |
2086 | |
|
2087 | 0 | g_variant_builder_init (&builder, type); |
2088 | 0 | element_type = g_variant_type_first (type); |
2089 | 0 | if (!element_type) |
2090 | 0 | { |
2091 | 0 | g_variant_builder_clear (&builder); |
2092 | 0 | g_set_error_literal (&local_error, |
2093 | 0 | G_IO_ERROR, |
2094 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2095 | 0 | _("Empty structures (tuples) are not allowed in D-Bus")); |
2096 | 0 | goto fail; |
2097 | 0 | } |
2098 | | |
2099 | 0 | while (element_type != NULL) |
2100 | 0 | { |
2101 | 0 | GVariant *item; |
2102 | 0 | item = parse_value_from_blob (buf, |
2103 | 0 | element_type, |
2104 | 0 | max_depth - 1, |
2105 | 0 | FALSE, |
2106 | 0 | indent + 2, |
2107 | 0 | &local_error); |
2108 | 0 | if (item == NULL) |
2109 | 0 | { |
2110 | 0 | g_variant_builder_clear (&builder); |
2111 | 0 | goto fail; |
2112 | 0 | } |
2113 | 0 | g_variant_builder_add_value (&builder, item); |
2114 | 0 | g_variant_unref (item); |
2115 | |
|
2116 | 0 | element_type = g_variant_type_next (element_type); |
2117 | 0 | } |
2118 | 0 | ret = g_variant_builder_end (&builder); |
2119 | 0 | } |
2120 | 0 | } |
2121 | 0 | else if (g_variant_type_is_variant (type)) |
2122 | 0 | { |
2123 | | #ifdef DEBUG_SERIALIZER |
2124 | | is_leaf = FALSE; |
2125 | | g_print ("\n"); |
2126 | | #endif /* DEBUG_SERIALIZER */ |
2127 | |
|
2128 | 0 | if (!just_align) |
2129 | 0 | { |
2130 | 0 | guchar siglen; |
2131 | 0 | const gchar *sig; |
2132 | 0 | GVariantType *variant_type; |
2133 | 0 | GVariant *value; |
2134 | |
|
2135 | 0 | siglen = g_memory_buffer_read_byte (buf, &local_error); |
2136 | 0 | if (local_error) |
2137 | 0 | goto fail; |
2138 | 0 | sig = read_string (buf, (gsize) siglen, &local_error); |
2139 | 0 | if (sig == NULL) |
2140 | 0 | goto fail; |
2141 | 0 | if (!g_variant_is_signature (sig) || |
2142 | 0 | !g_variant_type_string_is_valid (sig)) |
2143 | 0 | { |
2144 | | /* A D-Bus signature can contain zero or more complete types, |
2145 | | * but a GVariant has to be exactly one complete type. */ |
2146 | 0 | g_set_error (&local_error, |
2147 | 0 | G_IO_ERROR, |
2148 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2149 | 0 | _("Parsed value “%s” for variant is not a valid D-Bus signature"), |
2150 | 0 | sig); |
2151 | 0 | goto fail; |
2152 | 0 | } |
2153 | | |
2154 | 0 | if (max_depth <= g_variant_type_string_get_depth_ (sig)) |
2155 | 0 | { |
2156 | | /* Catch the type nesting being too deep without having to |
2157 | | * parse the data. We don’t have to check this for static |
2158 | | * container types (like arrays and tuples, above) because |
2159 | | * the g_variant_type_string_is_valid() check performed before |
2160 | | * the initial parse_value_from_blob() call should check the |
2161 | | * static type nesting. */ |
2162 | 0 | g_set_error_literal (&local_error, |
2163 | 0 | G_IO_ERROR, |
2164 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2165 | 0 | _("Value nested too deeply")); |
2166 | 0 | goto fail; |
2167 | 0 | } |
2168 | | |
2169 | 0 | variant_type = g_variant_type_new (sig); |
2170 | 0 | value = parse_value_from_blob (buf, |
2171 | 0 | variant_type, |
2172 | 0 | max_depth - 1, |
2173 | 0 | FALSE, |
2174 | 0 | indent + 2, |
2175 | 0 | &local_error); |
2176 | 0 | g_variant_type_free (variant_type); |
2177 | 0 | if (value == NULL) |
2178 | 0 | goto fail; |
2179 | 0 | ret = g_variant_new_variant (value); |
2180 | 0 | g_variant_unref (value); |
2181 | 0 | } |
2182 | 0 | } |
2183 | 0 | else |
2184 | 0 | { |
2185 | 0 | gchar *s; |
2186 | 0 | s = g_variant_type_dup_string (type); |
2187 | 0 | g_set_error (&local_error, |
2188 | 0 | G_IO_ERROR, |
2189 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2190 | 0 | _("Error deserializing GVariant with type string “%s” from the D-Bus wire format"), |
2191 | 0 | s); |
2192 | 0 | g_free (s); |
2193 | 0 | goto fail; |
2194 | 0 | } |
2195 | 0 | break; |
2196 | 0 | } |
2197 | | |
2198 | 0 | g_assert ((just_align && ret == NULL) || (!just_align && ret != NULL)); |
2199 | | |
2200 | | #ifdef DEBUG_SERIALIZER |
2201 | | if (ret != NULL) |
2202 | | { |
2203 | | if (is_leaf) |
2204 | | { |
2205 | | gchar *s; |
2206 | | if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE)) |
2207 | | { |
2208 | | s = g_strdup_printf ("0x%02x '%c'", g_variant_get_byte (ret), g_variant_get_byte (ret)); |
2209 | | } |
2210 | | else |
2211 | | { |
2212 | | s = g_variant_print (ret, FALSE); |
2213 | | } |
2214 | | g_print (": %s\n", s); |
2215 | | g_free (s); |
2216 | | } |
2217 | | } |
2218 | | #endif /* DEBUG_SERIALIZER */ |
2219 | | |
2220 | | /* sink the reference, if floating */ |
2221 | 0 | if (ret != NULL) |
2222 | 0 | g_variant_take_ref (ret); |
2223 | 0 | return ret; |
2224 | | |
2225 | 0 | fail: |
2226 | | #ifdef DEBUG_SERIALIZER |
2227 | | g_print ("\n" |
2228 | | "%*sFAILURE: %s (%s, %d)\n", |
2229 | | indent, "", |
2230 | | local_error->message, |
2231 | | g_quark_to_string (local_error->domain), |
2232 | | local_error->code); |
2233 | | #endif /* DEBUG_SERIALIZER */ |
2234 | 0 | g_propagate_error (error, local_error); |
2235 | 0 | return NULL; |
2236 | 0 | } |
2237 | | |
2238 | | /* ---------------------------------------------------------------------------------------------------- */ |
2239 | | |
2240 | | /* message_header must be at least 16 bytes */ |
2241 | | |
2242 | | /** |
2243 | | * g_dbus_message_bytes_needed: |
2244 | | * @blob: (array length=blob_len) (element-type guint8): A blob representing a binary D-Bus message. |
2245 | | * @blob_len: The length of @blob (must be at least 16). |
2246 | | * @error: Return location for error or %NULL. |
2247 | | * |
2248 | | * Utility function to calculate how many bytes are needed to |
2249 | | * completely deserialize the D-Bus message stored at @blob. |
2250 | | * |
2251 | | * Returns: Number of bytes needed or -1 if @error is set (e.g. if |
2252 | | * @blob contains invalid data or not enough data is available to |
2253 | | * determine the size). |
2254 | | * |
2255 | | * Since: 2.26 |
2256 | | */ |
2257 | | gssize |
2258 | | g_dbus_message_bytes_needed (guchar *blob, |
2259 | | gsize blob_len, |
2260 | | GError **error) |
2261 | 0 | { |
2262 | 0 | gssize ret; |
2263 | |
|
2264 | 0 | ret = -1; |
2265 | |
|
2266 | 0 | g_return_val_if_fail (blob != NULL, -1); |
2267 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, -1); |
2268 | 0 | g_return_val_if_fail (blob_len >= 16, -1); |
2269 | | |
2270 | 0 | if (blob[0] == 'l') |
2271 | 0 | { |
2272 | | /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */ |
2273 | 0 | ret = 12 + 4 + GUINT32_FROM_LE (((guint32 *) blob)[3]); |
2274 | | /* round up so it's a multiple of 8 */ |
2275 | 0 | ret = 8 * ((ret + 7)/8); |
2276 | | /* finally add the body size */ |
2277 | 0 | ret += GUINT32_FROM_LE (((guint32 *) blob)[1]); |
2278 | 0 | } |
2279 | 0 | else if (blob[0] == 'B') |
2280 | 0 | { |
2281 | | /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */ |
2282 | 0 | ret = 12 + 4 + GUINT32_FROM_BE (((guint32 *) blob)[3]); |
2283 | | /* round up so it's a multiple of 8 */ |
2284 | 0 | ret = 8 * ((ret + 7)/8); |
2285 | | /* finally add the body size */ |
2286 | 0 | ret += GUINT32_FROM_BE (((guint32 *) blob)[1]); |
2287 | 0 | } |
2288 | 0 | else |
2289 | 0 | { |
2290 | 0 | g_set_error (error, |
2291 | 0 | G_IO_ERROR, |
2292 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2293 | 0 | "Unable to determine message blob length - given blob is malformed"); |
2294 | 0 | } |
2295 | |
|
2296 | 0 | if (ret > (1<<27)) |
2297 | 0 | { |
2298 | 0 | g_set_error (error, |
2299 | 0 | G_IO_ERROR, |
2300 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2301 | 0 | "Blob indicates that message exceeds maximum message length (128MiB)"); |
2302 | 0 | ret = -1; |
2303 | 0 | } |
2304 | |
|
2305 | 0 | return ret; |
2306 | 0 | } |
2307 | | |
2308 | | /* ---------------------------------------------------------------------------------------------------- */ |
2309 | | |
2310 | | /** |
2311 | | * g_dbus_message_new_from_blob: |
2312 | | * @blob: (array length=blob_len) (element-type guint8): A blob representing a binary D-Bus message. |
2313 | | * @blob_len: The length of @blob. |
2314 | | * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported. |
2315 | | * @error: Return location for error or %NULL. |
2316 | | * |
2317 | | * Creates a new #GDBusMessage from the data stored at @blob. The byte |
2318 | | * order that the message was in can be retrieved using |
2319 | | * g_dbus_message_get_byte_order(). |
2320 | | * |
2321 | | * If the @blob cannot be parsed, contains invalid fields, or contains invalid |
2322 | | * headers, %G_IO_ERROR_INVALID_ARGUMENT will be returned. |
2323 | | * |
2324 | | * Returns: A new #GDBusMessage or %NULL if @error is set. Free with |
2325 | | * g_object_unref(). |
2326 | | * |
2327 | | * Since: 2.26 |
2328 | | */ |
2329 | | GDBusMessage * |
2330 | | g_dbus_message_new_from_blob (guchar *blob, |
2331 | | gsize blob_len, |
2332 | | GDBusCapabilityFlags capabilities, |
2333 | | GError **error) |
2334 | 0 | { |
2335 | 0 | GError *local_error = NULL; |
2336 | 0 | GMemoryBuffer mbuf; |
2337 | 0 | GDBusMessage *message; |
2338 | 0 | guchar endianness; |
2339 | 0 | guchar major_protocol_version; |
2340 | 0 | guint32 message_body_len; |
2341 | 0 | GVariant *headers; |
2342 | 0 | GVariant *item; |
2343 | 0 | GVariantIter iter; |
2344 | 0 | GVariant *signature; |
2345 | | |
2346 | | /* TODO: check against @capabilities */ |
2347 | |
|
2348 | 0 | g_return_val_if_fail (blob != NULL, NULL); |
2349 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
2350 | | |
2351 | 0 | message = g_dbus_message_new (); |
2352 | |
|
2353 | 0 | memset (&mbuf, 0, sizeof (mbuf)); |
2354 | 0 | mbuf.data = (gchar *)blob; |
2355 | 0 | mbuf.len = mbuf.valid_len = blob_len; |
2356 | |
|
2357 | 0 | endianness = g_memory_buffer_read_byte (&mbuf, &local_error); |
2358 | 0 | if (local_error) |
2359 | 0 | goto fail; |
2360 | | |
2361 | 0 | switch (endianness) |
2362 | 0 | { |
2363 | 0 | case 'l': |
2364 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN; |
2365 | 0 | message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN; |
2366 | 0 | break; |
2367 | 0 | case 'B': |
2368 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN; |
2369 | 0 | message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN; |
2370 | 0 | break; |
2371 | 0 | default: |
2372 | 0 | g_set_error (&local_error, |
2373 | 0 | G_IO_ERROR, |
2374 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2375 | 0 | _("Invalid endianness value. Expected 0x6c (“l”) or 0x42 (“B”) but found value 0x%02x"), |
2376 | 0 | endianness); |
2377 | 0 | goto fail; |
2378 | 0 | } |
2379 | | |
2380 | 0 | message->type = g_memory_buffer_read_byte (&mbuf, &local_error); |
2381 | 0 | if (local_error) |
2382 | 0 | goto fail; |
2383 | 0 | message->flags = g_memory_buffer_read_byte (&mbuf, &local_error); |
2384 | 0 | if (local_error) |
2385 | 0 | goto fail; |
2386 | 0 | major_protocol_version = g_memory_buffer_read_byte (&mbuf, &local_error); |
2387 | 0 | if (local_error) |
2388 | 0 | goto fail; |
2389 | 0 | if (major_protocol_version != 1) |
2390 | 0 | { |
2391 | 0 | g_set_error (&local_error, |
2392 | 0 | G_IO_ERROR, |
2393 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2394 | 0 | _("Invalid major protocol version. Expected 1 but found %d"), |
2395 | 0 | major_protocol_version); |
2396 | 0 | goto fail; |
2397 | 0 | } |
2398 | 0 | message_body_len = g_memory_buffer_read_uint32 (&mbuf, &local_error); |
2399 | 0 | if (local_error) |
2400 | 0 | goto fail; |
2401 | 0 | message->serial = g_memory_buffer_read_uint32 (&mbuf, &local_error); |
2402 | 0 | if (local_error) |
2403 | 0 | goto fail; |
2404 | | |
2405 | | #ifdef DEBUG_SERIALIZER |
2406 | | g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint) blob_len); |
2407 | | { |
2408 | | gchar *s; |
2409 | | s = _g_dbus_hexdump ((const gchar *) blob, blob_len, 2); |
2410 | | g_print ("%s\n", s); |
2411 | | g_free (s); |
2412 | | } |
2413 | | #endif /* DEBUG_SERIALIZER */ |
2414 | | |
2415 | | #ifdef DEBUG_SERIALIZER |
2416 | | g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint) blob_len); |
2417 | | #endif /* DEBUG_SERIALIZER */ |
2418 | 0 | headers = parse_value_from_blob (&mbuf, |
2419 | 0 | G_VARIANT_TYPE ("a{yv}"), |
2420 | 0 | G_DBUS_MAX_TYPE_DEPTH + 2 /* for the a{yv} */, |
2421 | 0 | FALSE, |
2422 | 0 | 2, |
2423 | 0 | &local_error); |
2424 | 0 | if (headers == NULL) |
2425 | 0 | goto fail; |
2426 | 0 | g_variant_iter_init (&iter, headers); |
2427 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2428 | 0 | { |
2429 | 0 | guchar header_field; |
2430 | 0 | GVariant *value; |
2431 | 0 | g_variant_get (item, |
2432 | 0 | "{yv}", |
2433 | 0 | &header_field, |
2434 | 0 | &value); |
2435 | 0 | g_dbus_message_set_header (message, header_field, value); |
2436 | 0 | g_variant_unref (value); |
2437 | 0 | g_variant_unref (item); |
2438 | 0 | } |
2439 | 0 | g_variant_unref (headers); |
2440 | |
|
2441 | 0 | signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE); |
2442 | 0 | if (signature != NULL) |
2443 | 0 | { |
2444 | 0 | const gchar *signature_str; |
2445 | 0 | gsize signature_str_len; |
2446 | |
|
2447 | 0 | if (!g_variant_is_of_type (signature, G_VARIANT_TYPE_SIGNATURE)) |
2448 | 0 | { |
2449 | 0 | g_set_error_literal (&local_error, |
2450 | 0 | G_IO_ERROR, |
2451 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2452 | 0 | _("Signature header found but is not of type signature")); |
2453 | 0 | goto fail; |
2454 | 0 | } |
2455 | | |
2456 | 0 | signature_str = g_variant_get_string (signature, &signature_str_len); |
2457 | | |
2458 | | /* signature but no body */ |
2459 | 0 | if (message_body_len == 0 && signature_str_len > 0) |
2460 | 0 | { |
2461 | 0 | g_set_error (&local_error, |
2462 | 0 | G_IO_ERROR, |
2463 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2464 | 0 | _("Signature header with signature “%s” found but message body is empty"), |
2465 | 0 | signature_str); |
2466 | 0 | goto fail; |
2467 | 0 | } |
2468 | 0 | else if (signature_str_len > 0) |
2469 | 0 | { |
2470 | 0 | GVariantType *variant_type; |
2471 | 0 | gchar *tupled_signature_str = g_strdup_printf ("(%s)", signature_str); |
2472 | |
|
2473 | 0 | if (!g_variant_is_signature (signature_str) || |
2474 | 0 | !g_variant_type_string_is_valid (tupled_signature_str)) |
2475 | 0 | { |
2476 | 0 | g_set_error (&local_error, |
2477 | 0 | G_IO_ERROR, |
2478 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2479 | 0 | _("Parsed value “%s” is not a valid D-Bus signature (for body)"), |
2480 | 0 | signature_str); |
2481 | 0 | g_free (tupled_signature_str); |
2482 | 0 | goto fail; |
2483 | 0 | } |
2484 | | |
2485 | 0 | variant_type = g_variant_type_new (tupled_signature_str); |
2486 | 0 | g_free (tupled_signature_str); |
2487 | | #ifdef DEBUG_SERIALIZER |
2488 | | g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint) blob_len); |
2489 | | #endif /* DEBUG_SERIALIZER */ |
2490 | 0 | message->body = parse_value_from_blob (&mbuf, |
2491 | 0 | variant_type, |
2492 | 0 | G_DBUS_MAX_TYPE_DEPTH + 1 /* for the surrounding tuple */, |
2493 | 0 | FALSE, |
2494 | 0 | 2, |
2495 | 0 | &local_error); |
2496 | 0 | g_variant_type_free (variant_type); |
2497 | |
|
2498 | 0 | if (message->body != NULL && |
2499 | 0 | g_variant_is_of_type (message->body, G_VARIANT_TYPE_TUPLE) && |
2500 | 0 | g_variant_n_children (message->body) > 0) |
2501 | 0 | message->arg0_cache = g_variant_get_child_value (message->body, 0); |
2502 | 0 | else |
2503 | 0 | message->arg0_cache = NULL; |
2504 | |
|
2505 | 0 | if (message->body == NULL) |
2506 | 0 | goto fail; |
2507 | 0 | } |
2508 | 0 | } |
2509 | 0 | else |
2510 | 0 | { |
2511 | | /* no signature, this is only OK if the body is empty */ |
2512 | 0 | if (message_body_len != 0) |
2513 | 0 | { |
2514 | | /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */ |
2515 | 0 | g_set_error (&local_error, |
2516 | 0 | G_IO_ERROR, |
2517 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2518 | 0 | g_dngettext (GETTEXT_PACKAGE, |
2519 | 0 | "No signature header in message but the message body is %u byte", |
2520 | 0 | "No signature header in message but the message body is %u bytes", |
2521 | 0 | message_body_len), |
2522 | 0 | message_body_len); |
2523 | 0 | goto fail; |
2524 | 0 | } |
2525 | 0 | } |
2526 | | |
2527 | 0 | if (!validate_headers (message, &local_error)) |
2528 | 0 | { |
2529 | 0 | g_prefix_error (&local_error, _("Cannot deserialize message: ")); |
2530 | 0 | goto fail; |
2531 | 0 | } |
2532 | | |
2533 | 0 | return message; |
2534 | | |
2535 | 0 | fail: |
2536 | 0 | g_clear_object (&message); |
2537 | 0 | g_propagate_error (error, local_error); |
2538 | 0 | return NULL; |
2539 | 0 | } |
2540 | | |
2541 | | /* ---------------------------------------------------------------------------------------------------- */ |
2542 | | |
2543 | | static gsize |
2544 | | ensure_output_padding (GMemoryBuffer *mbuf, |
2545 | | gsize padding_size) |
2546 | 0 | { |
2547 | 0 | gsize offset; |
2548 | 0 | gsize wanted_offset; |
2549 | 0 | gsize padding_needed; |
2550 | 0 | guint n; |
2551 | |
|
2552 | 0 | offset = mbuf->pos; |
2553 | 0 | wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size; |
2554 | 0 | padding_needed = wanted_offset - offset; |
2555 | |
|
2556 | 0 | for (n = 0; n < padding_needed; n++) |
2557 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2558 | |
|
2559 | 0 | return padding_needed; |
2560 | 0 | } |
2561 | | |
2562 | | /* note that value can be NULL for e.g. empty arrays - type is never NULL */ |
2563 | | static gboolean |
2564 | | append_value_to_blob (GVariant *value, |
2565 | | const GVariantType *type, |
2566 | | GMemoryBuffer *mbuf, |
2567 | | gsize *out_padding_added, |
2568 | | GError **error) |
2569 | 0 | { |
2570 | 0 | gsize padding_added; |
2571 | 0 | const gchar *type_string; |
2572 | |
|
2573 | 0 | type_string = g_variant_type_peek_string (type); |
2574 | |
|
2575 | 0 | padding_added = 0; |
2576 | |
|
2577 | 0 | switch (type_string[0]) |
2578 | 0 | { |
2579 | 0 | case 'b': /* G_VARIANT_TYPE_BOOLEAN */ |
2580 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2581 | 0 | if (value != NULL) |
2582 | 0 | { |
2583 | 0 | gboolean v = g_variant_get_boolean (value); |
2584 | 0 | g_memory_buffer_put_uint32 (mbuf, v); |
2585 | 0 | } |
2586 | 0 | break; |
2587 | | |
2588 | 0 | case 'y': /* G_VARIANT_TYPE_BYTE */ |
2589 | 0 | if (value != NULL) |
2590 | 0 | { |
2591 | 0 | guint8 v = g_variant_get_byte (value); |
2592 | 0 | g_memory_buffer_put_byte (mbuf, v); |
2593 | 0 | } |
2594 | 0 | break; |
2595 | | |
2596 | 0 | case 'n': /* G_VARIANT_TYPE_INT16 */ |
2597 | 0 | padding_added = ensure_output_padding (mbuf, 2); |
2598 | 0 | if (value != NULL) |
2599 | 0 | { |
2600 | 0 | gint16 v = g_variant_get_int16 (value); |
2601 | 0 | g_memory_buffer_put_int16 (mbuf, v); |
2602 | 0 | } |
2603 | 0 | break; |
2604 | | |
2605 | 0 | case 'q': /* G_VARIANT_TYPE_UINT16 */ |
2606 | 0 | padding_added = ensure_output_padding (mbuf, 2); |
2607 | 0 | if (value != NULL) |
2608 | 0 | { |
2609 | 0 | guint16 v = g_variant_get_uint16 (value); |
2610 | 0 | g_memory_buffer_put_uint16 (mbuf, v); |
2611 | 0 | } |
2612 | 0 | break; |
2613 | | |
2614 | 0 | case 'i': /* G_VARIANT_TYPE_INT32 */ |
2615 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2616 | 0 | if (value != NULL) |
2617 | 0 | { |
2618 | 0 | gint32 v = g_variant_get_int32 (value); |
2619 | 0 | g_memory_buffer_put_int32 (mbuf, v); |
2620 | 0 | } |
2621 | 0 | break; |
2622 | | |
2623 | 0 | case 'u': /* G_VARIANT_TYPE_UINT32 */ |
2624 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2625 | 0 | if (value != NULL) |
2626 | 0 | { |
2627 | 0 | guint32 v = g_variant_get_uint32 (value); |
2628 | 0 | g_memory_buffer_put_uint32 (mbuf, v); |
2629 | 0 | } |
2630 | 0 | break; |
2631 | | |
2632 | 0 | case 'x': /* G_VARIANT_TYPE_INT64 */ |
2633 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2634 | 0 | if (value != NULL) |
2635 | 0 | { |
2636 | 0 | gint64 v = g_variant_get_int64 (value); |
2637 | 0 | g_memory_buffer_put_int64 (mbuf, v); |
2638 | 0 | } |
2639 | 0 | break; |
2640 | | |
2641 | 0 | case 't': /* G_VARIANT_TYPE_UINT64 */ |
2642 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2643 | 0 | if (value != NULL) |
2644 | 0 | { |
2645 | 0 | guint64 v = g_variant_get_uint64 (value); |
2646 | 0 | g_memory_buffer_put_uint64 (mbuf, v); |
2647 | 0 | } |
2648 | 0 | break; |
2649 | | |
2650 | 0 | case 'd': /* G_VARIANT_TYPE_DOUBLE */ |
2651 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2652 | 0 | if (value != NULL) |
2653 | 0 | { |
2654 | 0 | union { |
2655 | 0 | guint64 v_uint64; |
2656 | 0 | gdouble v_double; |
2657 | 0 | } u; |
2658 | 0 | G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64)); |
2659 | 0 | u.v_double = g_variant_get_double (value); |
2660 | 0 | g_memory_buffer_put_uint64 (mbuf, u.v_uint64); |
2661 | 0 | } |
2662 | 0 | break; |
2663 | | |
2664 | 0 | case 's': /* G_VARIANT_TYPE_STRING */ |
2665 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2666 | 0 | if (value != NULL) |
2667 | 0 | { |
2668 | 0 | gsize len; |
2669 | 0 | const gchar *v; |
2670 | 0 | #ifndef G_DISABLE_ASSERT |
2671 | 0 | const gchar *end; |
2672 | 0 | #endif |
2673 | |
|
2674 | 0 | v = g_variant_get_string (value, &len); |
2675 | 0 | g_assert (g_utf8_validate (v, -1, &end) && (end == v + len)); |
2676 | 0 | g_memory_buffer_put_uint32 (mbuf, len); |
2677 | 0 | g_memory_buffer_put_string (mbuf, v); |
2678 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2679 | 0 | } |
2680 | 0 | break; |
2681 | | |
2682 | 0 | case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */ |
2683 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2684 | 0 | if (value != NULL) |
2685 | 0 | { |
2686 | 0 | gsize len; |
2687 | 0 | const gchar *v = g_variant_get_string (value, &len); |
2688 | 0 | g_assert (g_variant_is_object_path (v)); |
2689 | 0 | g_memory_buffer_put_uint32 (mbuf, len); |
2690 | 0 | g_memory_buffer_put_string (mbuf, v); |
2691 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2692 | 0 | } |
2693 | 0 | break; |
2694 | | |
2695 | 0 | case 'g': /* G_VARIANT_TYPE_SIGNATURE */ |
2696 | 0 | if (value != NULL) |
2697 | 0 | { |
2698 | 0 | gsize len; |
2699 | 0 | const gchar *v = g_variant_get_string (value, &len); |
2700 | 0 | g_assert (g_variant_is_signature (v)); |
2701 | 0 | g_memory_buffer_put_byte (mbuf, len); |
2702 | 0 | g_memory_buffer_put_string (mbuf, v); |
2703 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2704 | 0 | } |
2705 | 0 | break; |
2706 | | |
2707 | 0 | case 'h': /* G_VARIANT_TYPE_HANDLE */ |
2708 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2709 | 0 | if (value != NULL) |
2710 | 0 | { |
2711 | 0 | gint32 v = g_variant_get_handle (value); |
2712 | 0 | g_memory_buffer_put_int32 (mbuf, v); |
2713 | 0 | } |
2714 | 0 | break; |
2715 | | |
2716 | 0 | case 'a': /* G_VARIANT_TYPE_ARRAY */ |
2717 | 0 | { |
2718 | 0 | const GVariantType *element_type; |
2719 | 0 | GVariant *item; |
2720 | 0 | GVariantIter iter; |
2721 | 0 | goffset array_len_offset; |
2722 | 0 | goffset array_payload_begin_offset; |
2723 | 0 | goffset cur_offset; |
2724 | 0 | gsize array_len; |
2725 | 0 | guint fixed_size; |
2726 | |
|
2727 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2728 | 0 | if (value != NULL) |
2729 | 0 | { |
2730 | | /* array length - will be filled in later */ |
2731 | 0 | array_len_offset = mbuf->valid_len; |
2732 | 0 | g_memory_buffer_put_uint32 (mbuf, 0xF00DFACE); |
2733 | | |
2734 | | /* From the D-Bus spec: |
2735 | | * |
2736 | | * "A UINT32 giving the length of the array data in bytes, |
2737 | | * followed by alignment padding to the alignment boundary of |
2738 | | * the array element type, followed by each array element. The |
2739 | | * array length is from the end of the alignment padding to |
2740 | | * the end of the last element, i.e. it does not include the |
2741 | | * padding after the length, or any padding after the last |
2742 | | * element." |
2743 | | * |
2744 | | * Thus, we need to count how much padding the first element |
2745 | | * contributes and subtract that from the array length. |
2746 | | */ |
2747 | 0 | array_payload_begin_offset = mbuf->valid_len; |
2748 | |
|
2749 | 0 | element_type = g_variant_type_element (type); |
2750 | 0 | fixed_size = get_type_fixed_size (element_type); |
2751 | |
|
2752 | 0 | if (g_variant_n_children (value) == 0) |
2753 | 0 | { |
2754 | 0 | gsize padding_added_for_item; |
2755 | 0 | if (!append_value_to_blob (NULL, |
2756 | 0 | element_type, |
2757 | 0 | mbuf, |
2758 | 0 | &padding_added_for_item, |
2759 | 0 | error)) |
2760 | 0 | goto fail; |
2761 | 0 | array_payload_begin_offset += padding_added_for_item; |
2762 | 0 | } |
2763 | 0 | else if (fixed_size != 0) |
2764 | 0 | { |
2765 | 0 | GVariant *use_value; |
2766 | |
|
2767 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
2768 | 0 | use_value = g_variant_byteswap (value); |
2769 | 0 | else |
2770 | 0 | use_value = g_variant_ref (value); |
2771 | |
|
2772 | 0 | array_payload_begin_offset += ensure_output_padding (mbuf, fixed_size); |
2773 | |
|
2774 | 0 | array_len = g_variant_get_size (use_value); |
2775 | 0 | g_memory_buffer_write (mbuf, g_variant_get_data (use_value), array_len); |
2776 | 0 | g_variant_unref (use_value); |
2777 | 0 | } |
2778 | 0 | else |
2779 | 0 | { |
2780 | 0 | guint n; |
2781 | 0 | n = 0; |
2782 | 0 | g_variant_iter_init (&iter, value); |
2783 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2784 | 0 | { |
2785 | 0 | gsize padding_added_for_item; |
2786 | 0 | if (!append_value_to_blob (item, |
2787 | 0 | g_variant_get_type (item), |
2788 | 0 | mbuf, |
2789 | 0 | &padding_added_for_item, |
2790 | 0 | error)) |
2791 | 0 | { |
2792 | 0 | g_variant_unref (item); |
2793 | 0 | goto fail; |
2794 | 0 | } |
2795 | 0 | g_variant_unref (item); |
2796 | 0 | if (n == 0) |
2797 | 0 | { |
2798 | 0 | array_payload_begin_offset += padding_added_for_item; |
2799 | 0 | } |
2800 | 0 | n++; |
2801 | 0 | } |
2802 | 0 | } |
2803 | | |
2804 | 0 | cur_offset = mbuf->valid_len; |
2805 | 0 | array_len = cur_offset - array_payload_begin_offset; |
2806 | 0 | mbuf->pos = array_len_offset; |
2807 | |
|
2808 | 0 | g_memory_buffer_put_uint32 (mbuf, array_len); |
2809 | 0 | mbuf->pos = cur_offset; |
2810 | 0 | } |
2811 | 0 | } |
2812 | 0 | break; |
2813 | | |
2814 | 0 | default: |
2815 | 0 | if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type)) |
2816 | 0 | { |
2817 | 0 | if (!g_variant_type_first (type)) |
2818 | 0 | { |
2819 | 0 | g_set_error_literal (error, |
2820 | 0 | G_IO_ERROR, |
2821 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2822 | 0 | _("Empty structures (tuples) are not allowed in D-Bus")); |
2823 | 0 | goto fail; |
2824 | 0 | } |
2825 | | |
2826 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2827 | 0 | if (value != NULL) |
2828 | 0 | { |
2829 | 0 | GVariant *item; |
2830 | 0 | GVariantIter iter; |
2831 | 0 | g_variant_iter_init (&iter, value); |
2832 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2833 | 0 | { |
2834 | 0 | if (!append_value_to_blob (item, |
2835 | 0 | g_variant_get_type (item), |
2836 | 0 | mbuf, |
2837 | 0 | NULL, |
2838 | 0 | error)) |
2839 | 0 | { |
2840 | 0 | g_variant_unref (item); |
2841 | 0 | goto fail; |
2842 | 0 | } |
2843 | 0 | g_variant_unref (item); |
2844 | 0 | } |
2845 | 0 | } |
2846 | 0 | } |
2847 | 0 | else if (g_variant_type_is_variant (type)) |
2848 | 0 | { |
2849 | 0 | if (value != NULL) |
2850 | 0 | { |
2851 | 0 | GVariant *child; |
2852 | 0 | const gchar *signature; |
2853 | 0 | child = g_variant_get_child_value (value, 0); |
2854 | 0 | signature = g_variant_get_type_string (child); |
2855 | 0 | g_memory_buffer_put_byte (mbuf, strlen (signature)); |
2856 | 0 | g_memory_buffer_put_string (mbuf, signature); |
2857 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2858 | 0 | if (!append_value_to_blob (child, |
2859 | 0 | g_variant_get_type (child), |
2860 | 0 | mbuf, |
2861 | 0 | NULL, |
2862 | 0 | error)) |
2863 | 0 | { |
2864 | 0 | g_variant_unref (child); |
2865 | 0 | goto fail; |
2866 | 0 | } |
2867 | 0 | g_variant_unref (child); |
2868 | 0 | } |
2869 | 0 | } |
2870 | 0 | else |
2871 | 0 | { |
2872 | 0 | g_set_error (error, |
2873 | 0 | G_IO_ERROR, |
2874 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2875 | 0 | _("Error serializing GVariant with type string “%s” to the D-Bus wire format"), |
2876 | 0 | g_variant_get_type_string (value)); |
2877 | 0 | goto fail; |
2878 | 0 | } |
2879 | 0 | break; |
2880 | 0 | } |
2881 | | |
2882 | 0 | if (out_padding_added != NULL) |
2883 | 0 | *out_padding_added = padding_added; |
2884 | |
|
2885 | 0 | return TRUE; |
2886 | | |
2887 | 0 | fail: |
2888 | 0 | return FALSE; |
2889 | 0 | } |
2890 | | |
2891 | | static gboolean |
2892 | | append_body_to_blob (GVariant *value, |
2893 | | GMemoryBuffer *mbuf, |
2894 | | GError **error) |
2895 | 0 | { |
2896 | 0 | GVariant *item; |
2897 | 0 | GVariantIter iter; |
2898 | |
|
2899 | 0 | if (!g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE)) |
2900 | 0 | { |
2901 | 0 | g_set_error (error, |
2902 | 0 | G_IO_ERROR, |
2903 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2904 | 0 | "Expected a tuple for the body of the GDBusMessage."); |
2905 | 0 | goto fail; |
2906 | 0 | } |
2907 | | |
2908 | 0 | g_variant_iter_init (&iter, value); |
2909 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2910 | 0 | { |
2911 | 0 | if (!append_value_to_blob (item, |
2912 | 0 | g_variant_get_type (item), |
2913 | 0 | mbuf, |
2914 | 0 | NULL, |
2915 | 0 | error)) |
2916 | 0 | { |
2917 | 0 | g_variant_unref (item); |
2918 | 0 | goto fail; |
2919 | 0 | } |
2920 | 0 | g_variant_unref (item); |
2921 | 0 | } |
2922 | 0 | return TRUE; |
2923 | | |
2924 | 0 | fail: |
2925 | 0 | return FALSE; |
2926 | 0 | } |
2927 | | |
2928 | | /* ---------------------------------------------------------------------------------------------------- */ |
2929 | | |
2930 | | /** |
2931 | | * g_dbus_message_to_blob: |
2932 | | * @message: A #GDBusMessage. |
2933 | | * @out_size: Return location for size of generated blob. |
2934 | | * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported. |
2935 | | * @error: Return location for error. |
2936 | | * |
2937 | | * Serializes @message to a blob. The byte order returned by |
2938 | | * g_dbus_message_get_byte_order() will be used. |
2939 | | * |
2940 | | * Returns: (array length=out_size) (transfer full): A pointer to a |
2941 | | * valid binary D-Bus message of @out_size bytes generated by @message |
2942 | | * or %NULL if @error is set. Free with g_free(). |
2943 | | * |
2944 | | * Since: 2.26 |
2945 | | */ |
2946 | | guchar * |
2947 | | g_dbus_message_to_blob (GDBusMessage *message, |
2948 | | gsize *out_size, |
2949 | | GDBusCapabilityFlags capabilities, |
2950 | | GError **error) |
2951 | 0 | { |
2952 | 0 | GMemoryBuffer mbuf; |
2953 | 0 | guchar *ret; |
2954 | 0 | gsize size; |
2955 | 0 | goffset body_len_offset; |
2956 | 0 | goffset body_start_offset; |
2957 | 0 | gsize body_size; |
2958 | 0 | GVariant *header_fields; |
2959 | 0 | GVariantBuilder builder; |
2960 | 0 | GHashTableIter hash_iter; |
2961 | 0 | gpointer key; |
2962 | 0 | GVariant *header_value; |
2963 | 0 | GVariant *signature; |
2964 | 0 | const gchar *signature_str; |
2965 | 0 | gint num_fds_in_message; |
2966 | 0 | gint num_fds_according_to_header; |
2967 | | |
2968 | | /* TODO: check against @capabilities */ |
2969 | |
|
2970 | 0 | ret = NULL; |
2971 | |
|
2972 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
2973 | 0 | g_return_val_if_fail (out_size != NULL, NULL); |
2974 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
2975 | | |
2976 | 0 | memset (&mbuf, 0, sizeof (mbuf)); |
2977 | 0 | mbuf.len = MIN_ARRAY_SIZE; |
2978 | 0 | mbuf.data = g_malloc (mbuf.len); |
2979 | |
|
2980 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN; |
2981 | 0 | switch (message->byte_order) |
2982 | 0 | { |
2983 | 0 | case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN: |
2984 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN; |
2985 | 0 | break; |
2986 | 0 | case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN: |
2987 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN; |
2988 | 0 | break; |
2989 | 0 | } |
2990 | | |
2991 | | /* Core header */ |
2992 | 0 | g_memory_buffer_put_byte (&mbuf, (guchar) message->byte_order); |
2993 | 0 | g_memory_buffer_put_byte (&mbuf, message->type); |
2994 | 0 | g_memory_buffer_put_byte (&mbuf, message->flags); |
2995 | 0 | g_memory_buffer_put_byte (&mbuf, 1); /* major protocol version */ |
2996 | 0 | body_len_offset = mbuf.valid_len; |
2997 | | /* body length - will be filled in later */ |
2998 | 0 | g_memory_buffer_put_uint32 (&mbuf, 0xF00DFACE); |
2999 | 0 | g_memory_buffer_put_uint32 (&mbuf, message->serial); |
3000 | |
|
3001 | 0 | num_fds_in_message = 0; |
3002 | 0 | #ifdef G_OS_UNIX |
3003 | 0 | if (message->fd_list != NULL) |
3004 | 0 | num_fds_in_message = g_unix_fd_list_get_length (message->fd_list); |
3005 | 0 | #endif |
3006 | 0 | num_fds_according_to_header = g_dbus_message_get_num_unix_fds (message); |
3007 | 0 | if (num_fds_in_message != num_fds_according_to_header) |
3008 | 0 | { |
3009 | 0 | g_set_error (error, |
3010 | 0 | G_IO_ERROR, |
3011 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
3012 | 0 | _("Number of file descriptors in message (%d) differs from header field (%d)"), |
3013 | 0 | num_fds_in_message, |
3014 | 0 | num_fds_according_to_header); |
3015 | 0 | goto out; |
3016 | 0 | } |
3017 | | |
3018 | 0 | if (!validate_headers (message, error)) |
3019 | 0 | { |
3020 | 0 | g_prefix_error (error, _("Cannot serialize message: ")); |
3021 | 0 | goto out; |
3022 | 0 | } |
3023 | | |
3024 | 0 | g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{yv}")); |
3025 | 0 | g_hash_table_iter_init (&hash_iter, message->headers); |
3026 | 0 | while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value)) |
3027 | 0 | { |
3028 | 0 | g_variant_builder_add (&builder, |
3029 | 0 | "{yv}", |
3030 | 0 | (guchar) GPOINTER_TO_UINT (key), |
3031 | 0 | header_value); |
3032 | 0 | } |
3033 | 0 | header_fields = g_variant_builder_end (&builder); |
3034 | |
|
3035 | 0 | if (!append_value_to_blob (header_fields, |
3036 | 0 | g_variant_get_type (header_fields), |
3037 | 0 | &mbuf, |
3038 | 0 | NULL, |
3039 | 0 | error)) |
3040 | 0 | { |
3041 | 0 | g_variant_unref (header_fields); |
3042 | 0 | goto out; |
3043 | 0 | } |
3044 | 0 | g_variant_unref (header_fields); |
3045 | | |
3046 | | /* header size must be a multiple of 8 */ |
3047 | 0 | ensure_output_padding (&mbuf, 8); |
3048 | |
|
3049 | 0 | body_start_offset = mbuf.valid_len; |
3050 | |
|
3051 | 0 | signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE); |
3052 | |
|
3053 | 0 | if (signature != NULL && !g_variant_is_of_type (signature, G_VARIANT_TYPE_SIGNATURE)) |
3054 | 0 | { |
3055 | 0 | g_set_error_literal (error, |
3056 | 0 | G_IO_ERROR, |
3057 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
3058 | 0 | _("Signature header found but is not of type signature")); |
3059 | 0 | goto out; |
3060 | 0 | } |
3061 | | |
3062 | 0 | signature_str = NULL; |
3063 | 0 | if (signature != NULL) |
3064 | 0 | signature_str = g_variant_get_string (signature, NULL); |
3065 | 0 | if (message->body != NULL) |
3066 | 0 | { |
3067 | 0 | gchar *tupled_signature_str; |
3068 | 0 | if (signature == NULL) |
3069 | 0 | { |
3070 | 0 | g_set_error (error, |
3071 | 0 | G_IO_ERROR, |
3072 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
3073 | 0 | _("Message body has signature “%s” but there is no signature header"), |
3074 | 0 | g_variant_get_type_string (message->body)); |
3075 | 0 | goto out; |
3076 | 0 | } |
3077 | 0 | tupled_signature_str = g_strdup_printf ("(%s)", signature_str); |
3078 | 0 | if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->body)) != 0) |
3079 | 0 | { |
3080 | 0 | g_set_error (error, |
3081 | 0 | G_IO_ERROR, |
3082 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
3083 | 0 | _("Message body has type signature “%s” but signature in the header field is “%s”"), |
3084 | 0 | g_variant_get_type_string (message->body), tupled_signature_str); |
3085 | 0 | g_free (tupled_signature_str); |
3086 | 0 | goto out; |
3087 | 0 | } |
3088 | 0 | g_free (tupled_signature_str); |
3089 | 0 | if (!append_body_to_blob (message->body, &mbuf, error)) |
3090 | 0 | goto out; |
3091 | 0 | } |
3092 | 0 | else |
3093 | 0 | { |
3094 | 0 | if (signature != NULL && strlen (signature_str) > 0) |
3095 | 0 | { |
3096 | 0 | g_set_error (error, |
3097 | 0 | G_IO_ERROR, |
3098 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
3099 | 0 | _("Message body is empty but signature in the header field is “(%s)”"), |
3100 | 0 | signature_str); |
3101 | 0 | goto out; |
3102 | 0 | } |
3103 | 0 | } |
3104 | | |
3105 | | /* OK, we're done writing the message - set the body length */ |
3106 | 0 | size = mbuf.valid_len; |
3107 | 0 | body_size = size - body_start_offset; |
3108 | |
|
3109 | 0 | mbuf.pos = body_len_offset; |
3110 | |
|
3111 | 0 | g_memory_buffer_put_uint32 (&mbuf, body_size); |
3112 | |
|
3113 | 0 | *out_size = size; |
3114 | 0 | ret = (guchar *)mbuf.data; |
3115 | |
|
3116 | 0 | out: |
3117 | 0 | if (ret == NULL) |
3118 | 0 | g_free (mbuf.data); |
3119 | |
|
3120 | 0 | return ret; |
3121 | 0 | } |
3122 | | |
3123 | | /* ---------------------------------------------------------------------------------------------------- */ |
3124 | | |
3125 | | static guint32 |
3126 | | get_uint32_header (GDBusMessage *message, |
3127 | | GDBusMessageHeaderField header_field) |
3128 | 0 | { |
3129 | 0 | GVariant *value; |
3130 | 0 | guint32 ret; |
3131 | |
|
3132 | 0 | ret = 0; |
3133 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
3134 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32)) |
3135 | 0 | ret = g_variant_get_uint32 (value); |
3136 | |
|
3137 | 0 | return ret; |
3138 | 0 | } |
3139 | | |
3140 | | static const gchar * |
3141 | | get_string_header (GDBusMessage *message, |
3142 | | GDBusMessageHeaderField header_field) |
3143 | 0 | { |
3144 | 0 | GVariant *value; |
3145 | 0 | const gchar *ret; |
3146 | |
|
3147 | 0 | ret = NULL; |
3148 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
3149 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) |
3150 | 0 | ret = g_variant_get_string (value, NULL); |
3151 | |
|
3152 | 0 | return ret; |
3153 | 0 | } |
3154 | | |
3155 | | static const gchar * |
3156 | | get_object_path_header (GDBusMessage *message, |
3157 | | GDBusMessageHeaderField header_field) |
3158 | 0 | { |
3159 | 0 | GVariant *value; |
3160 | 0 | const gchar *ret; |
3161 | |
|
3162 | 0 | ret = NULL; |
3163 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
3164 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH)) |
3165 | 0 | ret = g_variant_get_string (value, NULL); |
3166 | |
|
3167 | 0 | return ret; |
3168 | 0 | } |
3169 | | |
3170 | | static const gchar * |
3171 | | get_signature_header (GDBusMessage *message, |
3172 | | GDBusMessageHeaderField header_field) |
3173 | 0 | { |
3174 | 0 | GVariant *value; |
3175 | 0 | const gchar *ret; |
3176 | |
|
3177 | 0 | ret = NULL; |
3178 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
3179 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE)) |
3180 | 0 | ret = g_variant_get_string (value, NULL); |
3181 | |
|
3182 | 0 | return ret; |
3183 | 0 | } |
3184 | | |
3185 | | /* ---------------------------------------------------------------------------------------------------- */ |
3186 | | |
3187 | | static void |
3188 | | set_uint32_header (GDBusMessage *message, |
3189 | | GDBusMessageHeaderField header_field, |
3190 | | guint32 value) |
3191 | 0 | { |
3192 | 0 | g_dbus_message_set_header (message, |
3193 | 0 | header_field, |
3194 | 0 | g_variant_new_uint32 (value)); |
3195 | 0 | } |
3196 | | |
3197 | | static void |
3198 | | set_string_header (GDBusMessage *message, |
3199 | | GDBusMessageHeaderField header_field, |
3200 | | const gchar *value) |
3201 | 0 | { |
3202 | 0 | g_dbus_message_set_header (message, |
3203 | 0 | header_field, |
3204 | 0 | value == NULL ? NULL : g_variant_new_string (value)); |
3205 | 0 | } |
3206 | | |
3207 | | static void |
3208 | | set_object_path_header (GDBusMessage *message, |
3209 | | GDBusMessageHeaderField header_field, |
3210 | | const gchar *value) |
3211 | 0 | { |
3212 | 0 | g_dbus_message_set_header (message, |
3213 | 0 | header_field, |
3214 | 0 | value == NULL ? NULL : g_variant_new_object_path (value)); |
3215 | 0 | } |
3216 | | |
3217 | | static void |
3218 | | set_signature_header (GDBusMessage *message, |
3219 | | GDBusMessageHeaderField header_field, |
3220 | | const gchar *value) |
3221 | 0 | { |
3222 | 0 | g_dbus_message_set_header (message, |
3223 | 0 | header_field, |
3224 | 0 | value == NULL ? NULL : g_variant_new_signature (value)); |
3225 | 0 | } |
3226 | | |
3227 | | /* ---------------------------------------------------------------------------------------------------- */ |
3228 | | |
3229 | | /** |
3230 | | * g_dbus_message_get_reply_serial: |
3231 | | * @message: A #GDBusMessage. |
3232 | | * |
3233 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field. |
3234 | | * |
3235 | | * Returns: The value. |
3236 | | * |
3237 | | * Since: 2.26 |
3238 | | */ |
3239 | | guint32 |
3240 | | g_dbus_message_get_reply_serial (GDBusMessage *message) |
3241 | 0 | { |
3242 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0); |
3243 | 0 | return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL); |
3244 | 0 | } |
3245 | | |
3246 | | /** |
3247 | | * g_dbus_message_set_reply_serial: |
3248 | | * @message: A #GDBusMessage. |
3249 | | * @value: The value to set. |
3250 | | * |
3251 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field. |
3252 | | * |
3253 | | * Since: 2.26 |
3254 | | */ |
3255 | | void |
3256 | | g_dbus_message_set_reply_serial (GDBusMessage *message, |
3257 | | guint32 value) |
3258 | 0 | { |
3259 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3260 | 0 | set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, value); |
3261 | 0 | } |
3262 | | |
3263 | | /* ---------------------------------------------------------------------------------------------------- */ |
3264 | | |
3265 | | /** |
3266 | | * g_dbus_message_get_interface: |
3267 | | * @message: A #GDBusMessage. |
3268 | | * |
3269 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field. |
3270 | | * |
3271 | | * Returns: (nullable): The value. |
3272 | | * |
3273 | | * Since: 2.26 |
3274 | | */ |
3275 | | const gchar * |
3276 | | g_dbus_message_get_interface (GDBusMessage *message) |
3277 | 0 | { |
3278 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3279 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE); |
3280 | 0 | } |
3281 | | |
3282 | | /** |
3283 | | * g_dbus_message_set_interface: |
3284 | | * @message: A #GDBusMessage. |
3285 | | * @value: (nullable): The value to set. |
3286 | | * |
3287 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field. |
3288 | | * |
3289 | | * Since: 2.26 |
3290 | | */ |
3291 | | void |
3292 | | g_dbus_message_set_interface (GDBusMessage *message, |
3293 | | const gchar *value) |
3294 | 0 | { |
3295 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3296 | 0 | g_return_if_fail (value == NULL || g_dbus_is_interface_name (value)); |
3297 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, value); |
3298 | 0 | } |
3299 | | |
3300 | | /* ---------------------------------------------------------------------------------------------------- */ |
3301 | | |
3302 | | /** |
3303 | | * g_dbus_message_get_member: |
3304 | | * @message: A #GDBusMessage. |
3305 | | * |
3306 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field. |
3307 | | * |
3308 | | * Returns: (nullable): The value. |
3309 | | * |
3310 | | * Since: 2.26 |
3311 | | */ |
3312 | | const gchar * |
3313 | | g_dbus_message_get_member (GDBusMessage *message) |
3314 | 0 | { |
3315 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3316 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER); |
3317 | 0 | } |
3318 | | |
3319 | | /** |
3320 | | * g_dbus_message_set_member: |
3321 | | * @message: A #GDBusMessage. |
3322 | | * @value: (nullable): The value to set. |
3323 | | * |
3324 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field. |
3325 | | * |
3326 | | * Since: 2.26 |
3327 | | */ |
3328 | | void |
3329 | | g_dbus_message_set_member (GDBusMessage *message, |
3330 | | const gchar *value) |
3331 | 0 | { |
3332 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3333 | 0 | g_return_if_fail (value == NULL || g_dbus_is_member_name (value)); |
3334 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, value); |
3335 | 0 | } |
3336 | | |
3337 | | /* ---------------------------------------------------------------------------------------------------- */ |
3338 | | |
3339 | | /** |
3340 | | * g_dbus_message_get_path: |
3341 | | * @message: A #GDBusMessage. |
3342 | | * |
3343 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field. |
3344 | | * |
3345 | | * Returns: (nullable): The value. |
3346 | | * |
3347 | | * Since: 2.26 |
3348 | | */ |
3349 | | const gchar * |
3350 | | g_dbus_message_get_path (GDBusMessage *message) |
3351 | 0 | { |
3352 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3353 | 0 | return get_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH); |
3354 | 0 | } |
3355 | | |
3356 | | /** |
3357 | | * g_dbus_message_set_path: |
3358 | | * @message: A #GDBusMessage. |
3359 | | * @value: (nullable): The value to set. |
3360 | | * |
3361 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field. |
3362 | | * |
3363 | | * Since: 2.26 |
3364 | | */ |
3365 | | void |
3366 | | g_dbus_message_set_path (GDBusMessage *message, |
3367 | | const gchar *value) |
3368 | 0 | { |
3369 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3370 | 0 | g_return_if_fail (value == NULL || g_variant_is_object_path (value)); |
3371 | 0 | set_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, value); |
3372 | 0 | } |
3373 | | |
3374 | | /* ---------------------------------------------------------------------------------------------------- */ |
3375 | | |
3376 | | /** |
3377 | | * g_dbus_message_get_sender: |
3378 | | * @message: A #GDBusMessage. |
3379 | | * |
3380 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field. |
3381 | | * |
3382 | | * Returns: (nullable): The value. |
3383 | | * |
3384 | | * Since: 2.26 |
3385 | | */ |
3386 | | const gchar * |
3387 | | g_dbus_message_get_sender (GDBusMessage *message) |
3388 | 0 | { |
3389 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3390 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER); |
3391 | 0 | } |
3392 | | |
3393 | | /** |
3394 | | * g_dbus_message_set_sender: |
3395 | | * @message: A #GDBusMessage. |
3396 | | * @value: (nullable): The value to set. |
3397 | | * |
3398 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field. |
3399 | | * |
3400 | | * Since: 2.26 |
3401 | | */ |
3402 | | void |
3403 | | g_dbus_message_set_sender (GDBusMessage *message, |
3404 | | const gchar *value) |
3405 | 0 | { |
3406 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3407 | 0 | g_return_if_fail (value == NULL || g_dbus_is_name (value)); |
3408 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER, value); |
3409 | 0 | } |
3410 | | |
3411 | | /* ---------------------------------------------------------------------------------------------------- */ |
3412 | | |
3413 | | /** |
3414 | | * g_dbus_message_get_destination: |
3415 | | * @message: A #GDBusMessage. |
3416 | | * |
3417 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field. |
3418 | | * |
3419 | | * Returns: (nullable): The value. |
3420 | | * |
3421 | | * Since: 2.26 |
3422 | | */ |
3423 | | const gchar * |
3424 | | g_dbus_message_get_destination (GDBusMessage *message) |
3425 | 0 | { |
3426 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3427 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION); |
3428 | 0 | } |
3429 | | |
3430 | | /** |
3431 | | * g_dbus_message_set_destination: |
3432 | | * @message: A #GDBusMessage. |
3433 | | * @value: (nullable): The value to set. |
3434 | | * |
3435 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field. |
3436 | | * |
3437 | | * Since: 2.26 |
3438 | | */ |
3439 | | void |
3440 | | g_dbus_message_set_destination (GDBusMessage *message, |
3441 | | const gchar *value) |
3442 | 0 | { |
3443 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3444 | 0 | g_return_if_fail (value == NULL || g_dbus_is_name (value)); |
3445 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION, value); |
3446 | 0 | } |
3447 | | |
3448 | | /* ---------------------------------------------------------------------------------------------------- */ |
3449 | | |
3450 | | /** |
3451 | | * g_dbus_message_get_error_name: |
3452 | | * @message: A #GDBusMessage. |
3453 | | * |
3454 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field. |
3455 | | * |
3456 | | * Returns: (nullable): The value. |
3457 | | * |
3458 | | * Since: 2.26 |
3459 | | */ |
3460 | | const gchar * |
3461 | | g_dbus_message_get_error_name (GDBusMessage *message) |
3462 | 0 | { |
3463 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3464 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME); |
3465 | 0 | } |
3466 | | |
3467 | | /** |
3468 | | * g_dbus_message_set_error_name: |
3469 | | * @message: (nullable): A #GDBusMessage. |
3470 | | * @value: The value to set. |
3471 | | * |
3472 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field. |
3473 | | * |
3474 | | * Since: 2.26 |
3475 | | */ |
3476 | | void |
3477 | | g_dbus_message_set_error_name (GDBusMessage *message, |
3478 | | const gchar *value) |
3479 | 0 | { |
3480 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3481 | 0 | g_return_if_fail (value == NULL || g_dbus_is_error_name (value)); |
3482 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, value); |
3483 | 0 | } |
3484 | | |
3485 | | /* ---------------------------------------------------------------------------------------------------- */ |
3486 | | |
3487 | | /** |
3488 | | * g_dbus_message_get_signature: |
3489 | | * @message: A #GDBusMessage. |
3490 | | * |
3491 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field. |
3492 | | * |
3493 | | * This will always be non-%NULL, but may be an empty string. |
3494 | | * |
3495 | | * Returns: (not nullable): The value. |
3496 | | * |
3497 | | * Since: 2.26 |
3498 | | */ |
3499 | | const gchar * |
3500 | | g_dbus_message_get_signature (GDBusMessage *message) |
3501 | 0 | { |
3502 | 0 | const gchar *ret; |
3503 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3504 | 0 | ret = get_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE); |
3505 | 0 | if (ret == NULL) |
3506 | 0 | ret = ""; |
3507 | 0 | return ret; |
3508 | 0 | } |
3509 | | |
3510 | | /** |
3511 | | * g_dbus_message_set_signature: |
3512 | | * @message: A #GDBusMessage. |
3513 | | * @value: (nullable): The value to set. |
3514 | | * |
3515 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field. |
3516 | | * |
3517 | | * Since: 2.26 |
3518 | | */ |
3519 | | void |
3520 | | g_dbus_message_set_signature (GDBusMessage *message, |
3521 | | const gchar *value) |
3522 | 0 | { |
3523 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3524 | 0 | g_return_if_fail (value == NULL || g_variant_is_signature (value)); |
3525 | 0 | set_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE, value); |
3526 | 0 | } |
3527 | | |
3528 | | /* ---------------------------------------------------------------------------------------------------- */ |
3529 | | |
3530 | | /** |
3531 | | * g_dbus_message_get_arg0: |
3532 | | * @message: A #GDBusMessage. |
3533 | | * |
3534 | | * Convenience to get the first item in the body of @message. |
3535 | | * |
3536 | | * See [method@Gio.DBusMessage.get_arg0_path] for returning object-path-typed |
3537 | | * arg0 values. |
3538 | | * |
3539 | | * Returns: (nullable): The string item or %NULL if the first item in the body of |
3540 | | * @message is not a string. |
3541 | | * |
3542 | | * Since: 2.26 |
3543 | | */ |
3544 | | const gchar * |
3545 | | g_dbus_message_get_arg0 (GDBusMessage *message) |
3546 | 0 | { |
3547 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3548 | | |
3549 | 0 | if (message->arg0_cache != NULL && |
3550 | 0 | g_variant_is_of_type (message->arg0_cache, G_VARIANT_TYPE_STRING)) |
3551 | 0 | return g_variant_get_string (message->arg0_cache, NULL); |
3552 | | |
3553 | 0 | return NULL; |
3554 | 0 | } |
3555 | | |
3556 | | /** |
3557 | | * g_dbus_message_get_arg0_path: |
3558 | | * @message: A `GDBusMessage`. |
3559 | | * |
3560 | | * Convenience to get the first item in the body of @message. |
3561 | | * |
3562 | | * See [method@Gio.DBusMessage.get_arg0] for returning string-typed arg0 values. |
3563 | | * |
3564 | | * Returns: (nullable): The object path item or `NULL` if the first item in the |
3565 | | * body of @message is not an object path. |
3566 | | * |
3567 | | * Since: 2.80 |
3568 | | */ |
3569 | | const gchar * |
3570 | | g_dbus_message_get_arg0_path (GDBusMessage *message) |
3571 | 0 | { |
3572 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3573 | | |
3574 | 0 | if (message->arg0_cache != NULL && |
3575 | 0 | g_variant_is_of_type (message->arg0_cache, G_VARIANT_TYPE_OBJECT_PATH)) |
3576 | 0 | return g_variant_get_string (message->arg0_cache, NULL); |
3577 | | |
3578 | 0 | return NULL; |
3579 | 0 | } |
3580 | | |
3581 | | /* ---------------------------------------------------------------------------------------------------- */ |
3582 | | |
3583 | | /** |
3584 | | * g_dbus_message_get_num_unix_fds: |
3585 | | * @message: A #GDBusMessage. |
3586 | | * |
3587 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field. |
3588 | | * |
3589 | | * Returns: The value. |
3590 | | * |
3591 | | * Since: 2.26 |
3592 | | */ |
3593 | | guint32 |
3594 | | g_dbus_message_get_num_unix_fds (GDBusMessage *message) |
3595 | 0 | { |
3596 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0); |
3597 | 0 | return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS); |
3598 | 0 | } |
3599 | | |
3600 | | /** |
3601 | | * g_dbus_message_set_num_unix_fds: |
3602 | | * @message: A #GDBusMessage. |
3603 | | * @value: The value to set. |
3604 | | * |
3605 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field. |
3606 | | * |
3607 | | * Since: 2.26 |
3608 | | */ |
3609 | | void |
3610 | | g_dbus_message_set_num_unix_fds (GDBusMessage *message, |
3611 | | guint32 value) |
3612 | 0 | { |
3613 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3614 | 0 | set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS, value); |
3615 | 0 | } |
3616 | | |
3617 | | /* ---------------------------------------------------------------------------------------------------- */ |
3618 | | |
3619 | | /** |
3620 | | * g_dbus_message_to_gerror: |
3621 | | * @message: A #GDBusMessage. |
3622 | | * @error: The #GError to set. |
3623 | | * |
3624 | | * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does |
3625 | | * nothing and returns %FALSE. |
3626 | | * |
3627 | | * Otherwise this method encodes the error in @message as a #GError |
3628 | | * using g_dbus_error_set_dbus_error() using the information in the |
3629 | | * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as |
3630 | | * well as the first string item in @message's body. |
3631 | | * |
3632 | | * Returns: %TRUE if @error was set, %FALSE otherwise. |
3633 | | * |
3634 | | * Since: 2.26 |
3635 | | */ |
3636 | | gboolean |
3637 | | g_dbus_message_to_gerror (GDBusMessage *message, |
3638 | | GError **error) |
3639 | 0 | { |
3640 | 0 | gboolean ret; |
3641 | 0 | const gchar *error_name; |
3642 | |
|
3643 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
3644 | | |
3645 | 0 | ret = FALSE; |
3646 | 0 | if (message->type != G_DBUS_MESSAGE_TYPE_ERROR) |
3647 | 0 | goto out; |
3648 | | |
3649 | 0 | error_name = g_dbus_message_get_error_name (message); |
3650 | 0 | if (error_name != NULL) |
3651 | 0 | { |
3652 | 0 | GVariant *body; |
3653 | |
|
3654 | 0 | body = g_dbus_message_get_body (message); |
3655 | |
|
3656 | 0 | if (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(s)"))) |
3657 | 0 | { |
3658 | 0 | const gchar *error_message; |
3659 | 0 | g_variant_get (body, "(&s)", &error_message); |
3660 | 0 | g_dbus_error_set_dbus_error (error, |
3661 | 0 | error_name, |
3662 | 0 | error_message, |
3663 | 0 | NULL); |
3664 | 0 | } |
3665 | 0 | else |
3666 | 0 | { |
3667 | | /* these two situations are valid, yet pretty rare */ |
3668 | 0 | if (body != NULL) |
3669 | 0 | { |
3670 | 0 | g_dbus_error_set_dbus_error (error, |
3671 | 0 | error_name, |
3672 | 0 | "", |
3673 | 0 | _("Error return with body of type “%s”"), |
3674 | 0 | g_variant_get_type_string (body)); |
3675 | 0 | } |
3676 | 0 | else |
3677 | 0 | { |
3678 | 0 | g_dbus_error_set_dbus_error (error, |
3679 | 0 | error_name, |
3680 | 0 | "", |
3681 | 0 | _("Error return with empty body")); |
3682 | 0 | } |
3683 | 0 | } |
3684 | 0 | } |
3685 | 0 | else |
3686 | 0 | { |
3687 | | /* TODO: this shouldn't happen - should check this at message serialization |
3688 | | * time and disconnect the peer. |
3689 | | */ |
3690 | 0 | g_set_error (error, |
3691 | 0 | G_IO_ERROR, |
3692 | 0 | G_IO_ERROR_FAILED, |
3693 | 0 | "Error return without error-name header!"); |
3694 | 0 | } |
3695 | |
|
3696 | 0 | ret = TRUE; |
3697 | |
|
3698 | 0 | out: |
3699 | 0 | return ret; |
3700 | 0 | } |
3701 | | |
3702 | | /* ---------------------------------------------------------------------------------------------------- */ |
3703 | | |
3704 | | static gchar * |
3705 | | flags_to_string (GType flags_type, guint value) |
3706 | 0 | { |
3707 | 0 | GString *s; |
3708 | 0 | GFlagsClass *klass; |
3709 | 0 | guint n; |
3710 | |
|
3711 | 0 | klass = g_type_class_ref (flags_type); |
3712 | 0 | s = g_string_new (NULL); |
3713 | 0 | for (n = 0; n < 32; n++) |
3714 | 0 | { |
3715 | 0 | if ((value & (1<<n)) != 0) |
3716 | 0 | { |
3717 | 0 | GFlagsValue *flags_value; |
3718 | 0 | flags_value = g_flags_get_first_value (klass, (1<<n)); |
3719 | 0 | if (s->len > 0) |
3720 | 0 | g_string_append_c (s, ','); |
3721 | 0 | if (flags_value != NULL) |
3722 | 0 | g_string_append (s, flags_value->value_nick); |
3723 | 0 | else |
3724 | 0 | g_string_append_printf (s, "unknown (bit %d)", n); |
3725 | 0 | } |
3726 | 0 | } |
3727 | 0 | if (s->len == 0) |
3728 | 0 | g_string_append (s, "none"); |
3729 | 0 | g_type_class_unref (klass); |
3730 | 0 | return g_string_free (s, FALSE); |
3731 | 0 | } |
3732 | | |
3733 | | static gint |
3734 | | _sort_keys_func (gconstpointer a, |
3735 | | gconstpointer b) |
3736 | 0 | { |
3737 | 0 | gint ia; |
3738 | 0 | gint ib; |
3739 | |
|
3740 | 0 | ia = GPOINTER_TO_INT (a); |
3741 | 0 | ib = GPOINTER_TO_INT (b); |
3742 | |
|
3743 | 0 | return ia - ib; |
3744 | 0 | } |
3745 | | |
3746 | | /** |
3747 | | * g_dbus_message_print: |
3748 | | * @message: A #GDBusMessage. |
3749 | | * @indent: Indentation level. |
3750 | | * |
3751 | | * Produces a human-readable multi-line description of @message. |
3752 | | * |
3753 | | * The contents of the description has no ABI guarantees, the contents |
3754 | | * and formatting is subject to change at any time. Typical output |
3755 | | * looks something like this: |
3756 | | * ``` |
3757 | | * Type: method-call |
3758 | | * Flags: none |
3759 | | * Version: 0 |
3760 | | * Serial: 4 |
3761 | | * Headers: |
3762 | | * path -> objectpath '/org/gtk/GDBus/TestObject' |
3763 | | * interface -> 'org.gtk.GDBus.TestInterface' |
3764 | | * member -> 'GimmeStdout' |
3765 | | * destination -> ':1.146' |
3766 | | * Body: () |
3767 | | * UNIX File Descriptors: |
3768 | | * (none) |
3769 | | * ``` |
3770 | | * or |
3771 | | * ``` |
3772 | | * Type: method-return |
3773 | | * Flags: no-reply-expected |
3774 | | * Version: 0 |
3775 | | * Serial: 477 |
3776 | | * Headers: |
3777 | | * reply-serial -> uint32 4 |
3778 | | * destination -> ':1.159' |
3779 | | * sender -> ':1.146' |
3780 | | * num-unix-fds -> uint32 1 |
3781 | | * Body: () |
3782 | | * UNIX File Descriptors: |
3783 | | * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635 |
3784 | | * ``` |
3785 | | * |
3786 | | * Returns: (not nullable): A string that should be freed with [func@GLib.free]. |
3787 | | * |
3788 | | * Since: 2.26 |
3789 | | */ |
3790 | | gchar * |
3791 | | g_dbus_message_print (GDBusMessage *message, |
3792 | | guint indent) |
3793 | 0 | { |
3794 | 0 | GString *str; |
3795 | 0 | gchar *s; |
3796 | 0 | GList *keys; |
3797 | 0 | GList *l; |
3798 | |
|
3799 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3800 | | |
3801 | 0 | str = g_string_new (NULL); |
3802 | |
|
3803 | 0 | s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->type); |
3804 | 0 | g_string_append_printf (str, "%*sType: %s\n", indent, "", s); |
3805 | 0 | g_free (s); |
3806 | 0 | s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->flags); |
3807 | 0 | g_string_append_printf (str, "%*sFlags: %s\n", indent, "", s); |
3808 | 0 | g_free (s); |
3809 | 0 | g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->major_protocol_version); |
3810 | 0 | g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->serial); |
3811 | |
|
3812 | 0 | g_string_append_printf (str, "%*sHeaders:\n", indent, ""); |
3813 | 0 | keys = g_hash_table_get_keys (message->headers); |
3814 | 0 | keys = g_list_sort (keys, _sort_keys_func); |
3815 | 0 | if (keys != NULL) |
3816 | 0 | { |
3817 | 0 | for (l = keys; l != NULL; l = l->next) |
3818 | 0 | { |
3819 | 0 | gint key = GPOINTER_TO_INT (l->data); |
3820 | 0 | GVariant *value; |
3821 | 0 | gchar *value_str; |
3822 | |
|
3823 | 0 | value = g_hash_table_lookup (message->headers, l->data); |
3824 | 0 | g_assert (value != NULL); |
3825 | | |
3826 | 0 | s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key); |
3827 | 0 | value_str = g_variant_print (value, TRUE); |
3828 | 0 | g_string_append_printf (str, "%*s %s -> %s\n", indent, "", s, value_str); |
3829 | 0 | g_free (s); |
3830 | 0 | g_free (value_str); |
3831 | 0 | } |
3832 | 0 | } |
3833 | 0 | else |
3834 | 0 | { |
3835 | 0 | g_string_append_printf (str, "%*s (none)\n", indent, ""); |
3836 | 0 | } |
3837 | 0 | g_list_free (keys); |
3838 | 0 | g_string_append_printf (str, "%*sBody: ", indent, ""); |
3839 | 0 | if (message->body != NULL) |
3840 | 0 | { |
3841 | 0 | g_variant_print_string (message->body, |
3842 | 0 | str, |
3843 | 0 | TRUE); |
3844 | 0 | } |
3845 | 0 | else |
3846 | 0 | { |
3847 | 0 | g_string_append (str, "()"); |
3848 | 0 | } |
3849 | 0 | g_string_append (str, "\n"); |
3850 | 0 | #ifdef G_OS_UNIX |
3851 | 0 | g_string_append_printf (str, "%*sUNIX File Descriptors:\n", indent, ""); |
3852 | 0 | if (message->fd_list != NULL) |
3853 | 0 | { |
3854 | 0 | gint num_fds; |
3855 | 0 | const gint *fds; |
3856 | 0 | gint n; |
3857 | |
|
3858 | 0 | fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds); |
3859 | 0 | if (num_fds > 0) |
3860 | 0 | { |
3861 | 0 | for (n = 0; n < num_fds; n++) |
3862 | 0 | { |
3863 | 0 | GString *fs; |
3864 | 0 | struct stat statbuf; |
3865 | 0 | fs = g_string_new (NULL); |
3866 | 0 | if (fstat (fds[n], &statbuf) == 0) |
3867 | 0 | { |
3868 | 0 | #ifndef MAJOR_MINOR_NOT_FOUND |
3869 | 0 | g_string_append_printf (fs, "%s" "dev=%d:%d", fs->len > 0 ? "," : "", |
3870 | 0 | (gint) major (statbuf.st_dev), (gint) minor (statbuf.st_dev)); |
3871 | 0 | #endif |
3872 | 0 | g_string_append_printf (fs, "%s" "mode=0%o", fs->len > 0 ? "," : "", |
3873 | 0 | (guint) statbuf.st_mode); |
3874 | 0 | g_string_append_printf (fs, "%s" "ino=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3875 | 0 | (guint64) statbuf.st_ino); |
3876 | 0 | g_string_append_printf (fs, "%s" "uid=%u", fs->len > 0 ? "," : "", |
3877 | 0 | (guint) statbuf.st_uid); |
3878 | 0 | g_string_append_printf (fs, "%s" "gid=%u", fs->len > 0 ? "," : "", |
3879 | 0 | (guint) statbuf.st_gid); |
3880 | 0 | #ifndef MAJOR_MINOR_NOT_FOUND |
3881 | 0 | g_string_append_printf (fs, "%s" "rdev=%d:%d", fs->len > 0 ? "," : "", |
3882 | 0 | (gint) major (statbuf.st_rdev), (gint) minor (statbuf.st_rdev)); |
3883 | 0 | #endif |
3884 | 0 | g_string_append_printf (fs, "%s" "size=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3885 | 0 | (guint64) statbuf.st_size); |
3886 | 0 | g_string_append_printf (fs, "%s" "atime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3887 | 0 | (guint64) statbuf.st_atime); |
3888 | 0 | g_string_append_printf (fs, "%s" "mtime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3889 | 0 | (guint64) statbuf.st_mtime); |
3890 | 0 | g_string_append_printf (fs, "%s" "ctime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3891 | 0 | (guint64) statbuf.st_ctime); |
3892 | 0 | } |
3893 | 0 | else |
3894 | 0 | { |
3895 | 0 | int errsv = errno; |
3896 | 0 | g_string_append_printf (fs, "(fstat failed: %s)", g_strerror (errsv)); |
3897 | 0 | } |
3898 | 0 | g_string_append_printf (str, "%*s fd %d: %s\n", indent, "", fds[n], fs->str); |
3899 | 0 | g_string_free (fs, TRUE); |
3900 | 0 | } |
3901 | 0 | } |
3902 | 0 | else |
3903 | 0 | { |
3904 | 0 | g_string_append_printf (str, "%*s (empty)\n", indent, ""); |
3905 | 0 | } |
3906 | 0 | } |
3907 | 0 | else |
3908 | 0 | { |
3909 | 0 | g_string_append_printf (str, "%*s (none)\n", indent, ""); |
3910 | 0 | } |
3911 | 0 | #endif |
3912 | |
|
3913 | 0 | return g_string_free (str, FALSE); |
3914 | 0 | } |
3915 | | |
3916 | | /** |
3917 | | * g_dbus_message_get_locked: |
3918 | | * @message: A #GDBusMessage. |
3919 | | * |
3920 | | * Checks whether @message is locked. To monitor changes to this |
3921 | | * value, conncet to the #GObject::notify signal to listen for changes |
3922 | | * on the #GDBusMessage:locked property. |
3923 | | * |
3924 | | * Returns: %TRUE if @message is locked, %FALSE otherwise. |
3925 | | * |
3926 | | * Since: 2.26 |
3927 | | */ |
3928 | | gboolean |
3929 | | g_dbus_message_get_locked (GDBusMessage *message) |
3930 | 0 | { |
3931 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
3932 | 0 | return message->locked; |
3933 | 0 | } |
3934 | | |
3935 | | /** |
3936 | | * g_dbus_message_lock: |
3937 | | * @message: A #GDBusMessage. |
3938 | | * |
3939 | | * If @message is locked, does nothing. Otherwise locks the message. |
3940 | | * |
3941 | | * Since: 2.26 |
3942 | | */ |
3943 | | void |
3944 | | g_dbus_message_lock (GDBusMessage *message) |
3945 | 0 | { |
3946 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3947 | | |
3948 | 0 | if (message->locked) |
3949 | 0 | goto out; |
3950 | | |
3951 | 0 | message->locked = TRUE; |
3952 | 0 | g_object_notify (G_OBJECT (message), "locked"); |
3953 | |
|
3954 | 0 | out: |
3955 | 0 | ; |
3956 | 0 | } |
3957 | | |
3958 | | /** |
3959 | | * g_dbus_message_copy: |
3960 | | * @message: A #GDBusMessage. |
3961 | | * @error: Return location for error or %NULL. |
3962 | | * |
3963 | | * Copies @message. The copy is a deep copy and the returned |
3964 | | * #GDBusMessage is completely identical except that it is guaranteed |
3965 | | * to not be locked. |
3966 | | * |
3967 | | * This operation can fail if e.g. @message contains file descriptors |
3968 | | * and the per-process or system-wide open files limit is reached. |
3969 | | * |
3970 | | * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set. |
3971 | | * Free with g_object_unref(). |
3972 | | * |
3973 | | * Since: 2.26 |
3974 | | */ |
3975 | | GDBusMessage * |
3976 | | g_dbus_message_copy (GDBusMessage *message, |
3977 | | GError **error) |
3978 | 0 | { |
3979 | 0 | GDBusMessage *ret; |
3980 | 0 | GHashTableIter iter; |
3981 | 0 | gpointer header_key; |
3982 | 0 | GVariant *header_value; |
3983 | |
|
3984 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3985 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
3986 | | |
3987 | 0 | ret = g_dbus_message_new (); |
3988 | 0 | ret->type = message->type; |
3989 | 0 | ret->flags = message->flags; |
3990 | 0 | ret->byte_order = message->byte_order; |
3991 | 0 | ret->major_protocol_version = message->major_protocol_version; |
3992 | 0 | ret->serial = message->serial; |
3993 | |
|
3994 | 0 | #ifdef G_OS_UNIX |
3995 | 0 | if (message->fd_list != NULL) |
3996 | 0 | { |
3997 | 0 | gint n; |
3998 | 0 | gint num_fds; |
3999 | 0 | const gint *fds; |
4000 | |
|
4001 | 0 | ret->fd_list = g_unix_fd_list_new (); |
4002 | 0 | fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds); |
4003 | 0 | for (n = 0; n < num_fds; n++) |
4004 | 0 | { |
4005 | 0 | if (g_unix_fd_list_append (ret->fd_list, |
4006 | 0 | fds[n], |
4007 | 0 | error) == -1) |
4008 | 0 | { |
4009 | 0 | g_object_unref (ret); |
4010 | 0 | ret = NULL; |
4011 | 0 | goto out; |
4012 | 0 | } |
4013 | 0 | } |
4014 | 0 | } |
4015 | 0 | #endif |
4016 | | |
4017 | | /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine |
4018 | | * to just ref (as opposed to deep-copying) the GVariant instances |
4019 | | */ |
4020 | 0 | ret->body = message->body != NULL ? g_variant_ref (message->body) : NULL; |
4021 | 0 | ret->arg0_cache = message->arg0_cache != NULL ? g_variant_ref (message->arg0_cache) : NULL; |
4022 | 0 | g_hash_table_iter_init (&iter, message->headers); |
4023 | 0 | while (g_hash_table_iter_next (&iter, &header_key, (gpointer) &header_value)) |
4024 | 0 | g_hash_table_insert (ret->headers, header_key, g_variant_ref (header_value)); |
4025 | |
|
4026 | 0 | #ifdef G_OS_UNIX |
4027 | 0 | out: |
4028 | 0 | #endif |
4029 | 0 | return ret; |
4030 | 0 | } |