Branch data Line data Source code
1 : : /*
2 : : * Copyright © 2007, 2008 Ryan Lortie
3 : : * Copyright © 2010 Codethink Limited
4 : : * Copyright © 2022 Endless OS Foundation, LLC
5 : : *
6 : : * SPDX-License-Identifier: LGPL-2.1-or-later
7 : : *
8 : : * This library is free software; you can redistribute it and/or
9 : : * modify it under the terms of the GNU Lesser General Public
10 : : * License as published by the Free Software Foundation; either
11 : : * version 2.1 of the License, or (at your option) any later version.
12 : : *
13 : : * This library is distributed in the hope that it will be useful,
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : : * Lesser General Public License for more details.
17 : : *
18 : : * You should have received a copy of the GNU Lesser General Public
19 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include <glib/gvariant-core.h>
25 : :
26 : : #include <glib/gvariant-internal.h>
27 : : #include <glib/gvariant-serialiser.h>
28 : : #include <glib/gtestutils.h>
29 : : #include <glib/gbitlock.h>
30 : : #include <glib/gatomic.h>
31 : : #include <glib/gbytes.h>
32 : : #include <glib/gslice.h>
33 : : #include <glib/gmem.h>
34 : : #include <glib/grefcount.h>
35 : : #include <string.h>
36 : :
37 : : #include "glib_trace.h"
38 : :
39 : : /*
40 : : * This file includes the structure definition for GVariant and a small
41 : : * set of functions that are allowed to access the structure directly.
42 : : *
43 : : * This minimises the amount of code that can possibly touch a GVariant
44 : : * structure directly to a few simple fundamental operations. These few
45 : : * operations are written to be completely threadsafe with respect to
46 : : * all possible outside access. This means that we only need to be
47 : : * concerned about thread safety issues in this one small file.
48 : : *
49 : : * Most GVariant API functions are in gvariant.c.
50 : : */
51 : :
52 : : struct _GVariant
53 : : /* see below for field member documentation */
54 : : {
55 : : GVariantTypeInfo *type_info;
56 : : gsize size;
57 : :
58 : : union
59 : : {
60 : : struct
61 : : {
62 : : GBytes *bytes;
63 : : gconstpointer data;
64 : : gsize ordered_offsets_up_to;
65 : : gsize checked_offsets_up_to;
66 : : } serialised;
67 : :
68 : : struct
69 : : {
70 : : GVariant **children;
71 : : gsize n_children;
72 : : } tree;
73 : : } contents;
74 : :
75 : : gint state;
76 : : gatomicrefcount ref_count;
77 : : gsize depth;
78 : :
79 : : #if GLIB_SIZEOF_VOID_P == 4
80 : : /* Keep suffix aligned to 8 bytes */
81 : : guint _padding;
82 : : #endif
83 : :
84 : : guint8 suffix[];
85 : : };
86 : :
87 : : /* Ensure our suffix data aligns to largest guaranteed offset
88 : : * within GVariant, of 8 bytes.
89 : : */
90 : : G_STATIC_ASSERT (G_STRUCT_OFFSET (GVariant, suffix) % 8 == 0);
91 : :
92 : : /* struct GVariant:
93 : : *
94 : : * There are two primary forms of GVariant instances: "serialized form"
95 : : * and "tree form".
96 : : *
97 : : * "serialized form": A serialized GVariant instance stores its value in
98 : : * the GVariant serialization format. All
99 : : * basic-typed instances (ie: non-containers) are in
100 : : * serialized format, as are some containers.
101 : : *
102 : : * "tree form": Some containers are in "tree form". In this case,
103 : : * instead of containing the serialized data for the
104 : : * container, the instance contains an array of pointers to
105 : : * the child values of the container (thus forming a tree).
106 : : *
107 : : * It is possible for an instance to transition from tree form to
108 : : * serialized form. This happens, implicitly, if the serialized data is
109 : : * requested (eg: via g_variant_get_data()). Serialized form instances
110 : : * never transition into tree form.
111 : : *
112 : : *
113 : : * The fields of the structure are documented here:
114 : : *
115 : : * type_info: this is a reference to a GVariantTypeInfo describing the
116 : : * type of the instance. When the instance is freed, this
117 : : * reference must be released with g_variant_type_info_unref().
118 : : *
119 : : * The type_info field never changes during the life of the
120 : : * instance, so it can be accessed without a lock.
121 : : *
122 : : * size: this is the size of the serialized form for the instance, if it
123 : : * is known. If the instance is in serialized form then it is, by
124 : : * definition, known. If the instance is in tree form then it may
125 : : * be unknown (in which case it is -1). It is possible for the
126 : : * size to be known when in tree form if, for example, the user
127 : : * has called g_variant_get_size() without calling
128 : : * g_variant_get_data(). Additionally, even when the user calls
129 : : * g_variant_get_data() the size of the data must first be
130 : : * determined so that a large enough buffer can be allocated for
131 : : * the data.
132 : : *
133 : : * Once the size is known, it can never become unknown again.
134 : : * g_variant_ensure_size() is used to ensure that the size is in
135 : : * the known state -- it calculates the size if needed. After
136 : : * that, the size field can be accessed without a lock.
137 : : *
138 : : * contents: a union containing either the information associated with
139 : : * holding a value in serialized form or holding a value in
140 : : * tree form.
141 : : *
142 : : * .serialised: Only valid when the instance is in serialized form.
143 : : *
144 : : * Since an instance can never transition away from
145 : : * serialized form, once these fields are set, they will
146 : : * never be changed. It is therefore valid to access
147 : : * them without holding a lock.
148 : : *
149 : : * .bytes: the #GBytes that contains the memory pointed to by
150 : : * .data, or %NULL if .data is %NULL. In the event that
151 : : * the instance was deserialized from another instance,
152 : : * then the bytes will be shared by both of them. When
153 : : * the instance is freed, this reference must be released
154 : : * with g_bytes_unref().
155 : : *
156 : : * .data: the serialized data (of size 'size') of the instance.
157 : : * This pointer should not be freed or modified in any way.
158 : : * #GBytes is responsible for memory management.
159 : : *
160 : : * This pointer may be %NULL in two cases:
161 : : *
162 : : * - if the serialized size of the instance is 0
163 : : *
164 : : * - if the instance is of a fixed-sized type and was
165 : : * deserialized out of a corrupted container such that
166 : : * the container contains too few bytes to point to the
167 : : * entire proper fixed-size of this instance. In this
168 : : * case, 'size' will still be equal to the proper fixed
169 : : * size, but this pointer will be %NULL. This is exactly
170 : : * the reason that g_variant_get_data() sometimes returns
171 : : * %NULL. For all other calls, the effect should be as
172 : : * if .data pointed to the appropriate number of nul
173 : : * bytes.
174 : : *
175 : : * .ordered_offsets_up_to: If ordered_offsets_up_to == n this means that all
176 : : * the frame offsets up to and including the frame
177 : : * offset determining the end of element n are in
178 : : * order. This guarantees that the bytes of element
179 : : * n don't overlap with any previous element.
180 : : *
181 : : * For trusted data this is set to G_MAXSIZE and we
182 : : * don't check that the frame offsets are in order.
183 : : *
184 : : * Note: This doesn't imply the offsets are good in
185 : : * any way apart from their ordering. In particular
186 : : * offsets may be out of bounds for this value or
187 : : * may imply that the data overlaps the frame
188 : : * offsets themselves.
189 : : *
190 : : * This field is only relevant for arrays of non
191 : : * fixed width types and for tuples.
192 : : *
193 : : * .checked_offsets_up_to: Similarly to .ordered_offsets_up_to, this stores
194 : : * the index of the highest element, n, whose frame
195 : : * offsets (and all the preceding frame offsets)
196 : : * have been checked for validity.
197 : : *
198 : : * It is always the case that
199 : : * .checked_offsets_up_to ≥ .ordered_offsets_up_to.
200 : : *
201 : : * If .checked_offsets_up_to == .ordered_offsets_up_to,
202 : : * then a bad offset has not been found so far.
203 : : *
204 : : * If .checked_offsets_up_to > .ordered_offsets_up_to,
205 : : * then a bad offset has been found at
206 : : * (.ordered_offsets_up_to + 1).
207 : : *
208 : : * This field is only relevant for arrays of non
209 : : * fixed width types and for tuples.
210 : : *
211 : : * .tree: Only valid when the instance is in tree form.
212 : : *
213 : : * Note that accesses from other threads could result in
214 : : * conversion of the instance from tree form to serialized form
215 : : * at any time. For this reason, the instance lock must always
216 : : * be held while performing any operations on 'contents.tree'.
217 : : *
218 : : * .children: the array of the child instances of this instance.
219 : : * When the instance is freed (or converted to serialized
220 : : * form) then each child must have g_variant_unref()
221 : : * called on it and the array must be freed using
222 : : * g_free().
223 : : *
224 : : * .n_children: the number of items in the .children array.
225 : : *
226 : : * state: a bitfield describing the state of the instance. It is a
227 : : * bitwise-or of the following STATE_* constants:
228 : : *
229 : : * STATE_LOCKED: the instance lock is held. This is the bit used by
230 : : * g_bit_lock().
231 : : *
232 : : * STATE_SERIALISED: the instance is in serialized form. If this
233 : : * flag is not set then the instance is in tree
234 : : * form.
235 : : *
236 : : * STATE_TRUSTED: for serialized form instances, this means that the
237 : : * serialized data is known to be in normal form (ie:
238 : : * not corrupted).
239 : : *
240 : : * For tree form instances, this means that all of the
241 : : * child instances in the contents.tree.children array
242 : : * are trusted. This means that if the container is
243 : : * serialized then the resulting data will be in
244 : : * normal form.
245 : : *
246 : : * If this flag is unset it does not imply that the
247 : : * data is corrupted. It merely means that we're not
248 : : * sure that it's valid. See g_variant_is_trusted().
249 : : *
250 : : * STATE_FLOATING: if this flag is set then the object has a floating
251 : : * reference. See g_variant_ref_sink().
252 : : *
253 : : * ref_count: the reference count of the instance
254 : : *
255 : : * depth: the depth of the GVariant in a hierarchy of nested containers,
256 : : * increasing with the level of nesting. The top-most GVariant has depth
257 : : * zero. This is used to avoid recursing too deeply and overflowing the
258 : : * stack when handling deeply nested untrusted serialized GVariants.
259 : : */
260 : : #define STATE_LOCKED 1
261 : : #define STATE_SERIALISED 2
262 : : #define STATE_TRUSTED 4
263 : : #define STATE_FLOATING 8
264 : :
265 : : /* -- private -- */
266 : : /* < private >
267 : : * g_variant_lock:
268 : : * @value: a #GVariant
269 : : *
270 : : * Locks @value for performing sensitive operations.
271 : : */
272 : : static void
273 : 15699648 : g_variant_lock (GVariant *value)
274 : : {
275 : 15699648 : g_bit_lock (&value->state, 0);
276 : 15699648 : }
277 : :
278 : : /* < private >
279 : : * g_variant_unlock:
280 : : * @value: a #GVariant
281 : : *
282 : : * Unlocks @value after performing sensitive operations.
283 : : */
284 : : static void
285 : 15699648 : g_variant_unlock (GVariant *value)
286 : : {
287 : 15699648 : g_bit_unlock (&value->state, 0);
288 : 15699648 : }
289 : :
290 : : /* < private >
291 : : * g_variant_release_children:
292 : : * @value: a #GVariant
293 : : *
294 : : * Releases the reference held on each child in the 'children' array of
295 : : * @value and frees the array itself. @value must be in tree form.
296 : : *
297 : : * This is done when freeing a tree-form instance or converting it to
298 : : * serialized form.
299 : : *
300 : : * The current thread must hold the lock on @value.
301 : : */
302 : : static void
303 : 544182 : g_variant_release_children (GVariant *value)
304 : : {
305 : : gsize i;
306 : :
307 : 544182 : g_assert (value->state & STATE_LOCKED);
308 : 544182 : g_assert (~value->state & STATE_SERIALISED);
309 : :
310 : 2414666 : for (i = 0; i < value->contents.tree.n_children; i++)
311 : 1870484 : g_variant_unref (value->contents.tree.children[i]);
312 : :
313 : 544182 : g_free (value->contents.tree.children);
314 : 544182 : }
315 : :
316 : : /* This begins the main body of the recursive serializer.
317 : : *
318 : : * There are 3 functions here that work as a team with the serializer to
319 : : * get things done. g_variant_store() has a trivial role, but as a
320 : : * public API function, it has its definition elsewhere.
321 : : *
322 : : * Note that "serialization" of an instance does not mean that the
323 : : * instance is converted to serialized form -- it means that the
324 : : * serialized form of an instance is written to an external buffer.
325 : : * g_variant_ensure_serialised() (which is not part of this set of
326 : : * functions) is the function that is responsible for converting an
327 : : * instance to serialized form.
328 : : *
329 : : * We are only concerned here with container types since non-container
330 : : * instances are always in serialized form. For these instances,
331 : : * storing their serialized form merely involves a memcpy().
332 : : *
333 : : * Serialization is a two-step process. First, the size of the
334 : : * serialized data must be calculated so that an appropriately-sized
335 : : * buffer can be allocated. Second, the data is written into the
336 : : * buffer.
337 : : *
338 : : * Determining the size:
339 : : * The process of determining the size is triggered by a call to
340 : : * g_variant_ensure_size() on a container. This invokes the
341 : : * serializer code to determine the size. The serializer is passed
342 : : * g_variant_fill_gvs() as a callback.
343 : : *
344 : : * g_variant_fill_gvs() is called by the serializer on each child of
345 : : * the container which, in turn, calls g_variant_ensure_size() on
346 : : * itself and fills in the result of its own size calculation.
347 : : *
348 : : * The serializer uses the size information from the children to
349 : : * calculate the size needed for the entire container.
350 : : *
351 : : * Writing the data:
352 : : * After the buffer has been allocated, g_variant_serialise() is
353 : : * called on the container. This invokes the serializer code to write
354 : : * the bytes to the container. The serializer is, again, passed
355 : : * g_variant_fill_gvs() as a callback.
356 : : *
357 : : * This time, when g_variant_fill_gvs() is called for each child, the
358 : : * child is given a pointer to a sub-region of the allocated buffer
359 : : * where it should write its data. This is done by calling
360 : : * g_variant_store(). In the event that the instance is in serialized
361 : : * form this means a memcpy() of the serialized data into the
362 : : * allocated buffer. In the event that the instance is in tree form
363 : : * this means a recursive call back into g_variant_serialise().
364 : : *
365 : : *
366 : : * The forward declaration here allows corecursion via callback:
367 : : */
368 : : static void g_variant_fill_gvs (GVariantSerialised *, gpointer);
369 : :
370 : : /* < private >
371 : : * g_variant_ensure_size:
372 : : * @value: a #GVariant
373 : : *
374 : : * Ensures that the ->size field of @value is filled in properly. This
375 : : * must be done as a precursor to any serialization of the value in
376 : : * order to know how large of a buffer is needed to store the data.
377 : : *
378 : : * The current thread must hold the lock on @value.
379 : : */
380 : : static void
381 : 4585080 : g_variant_ensure_size (GVariant *value)
382 : : {
383 : 4585080 : g_assert (value->state & STATE_LOCKED);
384 : :
385 : 4585080 : if (value->size == (gsize) -1)
386 : : {
387 : : gpointer *children;
388 : : gsize n_children;
389 : :
390 : 56737 : children = (gpointer *) value->contents.tree.children;
391 : 56737 : n_children = value->contents.tree.n_children;
392 : 56737 : value->size = g_variant_serialiser_needed_size (value->type_info,
393 : : g_variant_fill_gvs,
394 : : children, n_children);
395 : : }
396 : 4585080 : }
397 : :
398 : : /* < private >
399 : : * g_variant_to_serialised:
400 : : * @value: a #GVariant
401 : : *
402 : : * Gets a GVariantSerialised for a GVariant in state STATE_SERIALISED.
403 : : */
404 : : inline static GVariantSerialised
405 : 2335216 : g_variant_to_serialised (GVariant *value)
406 : : {
407 : 2335216 : g_assert (value->state & STATE_SERIALISED);
408 : : {
409 : 2335216 : GVariantSerialised serialised = {
410 : 2335216 : value->type_info,
411 : 2335216 : (gpointer) value->contents.serialised.data,
412 : 2335216 : value->size,
413 : 2335216 : value->depth,
414 : 2335216 : value->contents.serialised.ordered_offsets_up_to,
415 : 2335216 : value->contents.serialised.checked_offsets_up_to,
416 : : };
417 : 2335216 : return serialised;
418 : : }
419 : : }
420 : :
421 : : /* < private >
422 : : * g_variant_serialise:
423 : : * @value: a #GVariant
424 : : * @data: an appropriately-sized buffer
425 : : *
426 : : * Serializes @value into @data. @value must be in tree form.
427 : : *
428 : : * No change is made to @value.
429 : : *
430 : : * The current thread must hold the lock on @value.
431 : : */
432 : : static void
433 : 56865 : g_variant_serialise (GVariant *value,
434 : : gpointer data)
435 : : {
436 : 56865 : GVariantSerialised serialised = { 0, };
437 : : gpointer *children;
438 : : gsize n_children;
439 : :
440 : 56865 : g_assert (~value->state & STATE_SERIALISED);
441 : 56865 : g_assert (value->state & STATE_LOCKED);
442 : :
443 : 56865 : serialised.type_info = value->type_info;
444 : 56865 : serialised.size = value->size;
445 : 56865 : serialised.data = data;
446 : 56865 : serialised.depth = value->depth;
447 : 56865 : serialised.ordered_offsets_up_to = 0;
448 : 56865 : serialised.checked_offsets_up_to = 0;
449 : :
450 : 56865 : children = (gpointer *) value->contents.tree.children;
451 : 56865 : n_children = value->contents.tree.n_children;
452 : :
453 : 56865 : g_variant_serialiser_serialise (serialised, g_variant_fill_gvs,
454 : : children, n_children);
455 : 56865 : }
456 : :
457 : : /* < private >
458 : : * g_variant_fill_gvs:
459 : : * @serialised: a pointer to a #GVariantSerialised
460 : : * @data: a #GVariant instance
461 : : *
462 : : * This is the callback that is passed by a tree-form container instance
463 : : * to the serializer. This callback gets called on each child of the
464 : : * container. Each child is responsible for performing the following
465 : : * actions:
466 : : *
467 : : * - reporting its type
468 : : *
469 : : * - reporting its serialized size (requires knowing the size first)
470 : : *
471 : : * - possibly storing its serialized form into the provided buffer
472 : : */
473 : : static void
474 : 1021602 : g_variant_fill_gvs (GVariantSerialised *serialised,
475 : : gpointer data)
476 : : {
477 : 1021602 : GVariant *value = data;
478 : :
479 : 1021602 : g_variant_lock (value);
480 : 1021602 : g_variant_ensure_size (value);
481 : 1021602 : g_variant_unlock (value);
482 : :
483 : 1021602 : if (serialised->type_info == NULL)
484 : 191417 : serialised->type_info = value->type_info;
485 : 1021602 : g_assert (serialised->type_info == value->type_info);
486 : :
487 : 1021602 : if (serialised->size == 0)
488 : 187279 : serialised->size = value->size;
489 : 1021602 : g_assert (serialised->size == value->size);
490 : 1021602 : serialised->depth = value->depth;
491 : :
492 : 1021602 : if (value->state & STATE_SERIALISED)
493 : : {
494 : 928163 : serialised->ordered_offsets_up_to = value->contents.serialised.ordered_offsets_up_to;
495 : 928163 : serialised->checked_offsets_up_to = value->contents.serialised.checked_offsets_up_to;
496 : : }
497 : : else
498 : : {
499 : 93439 : serialised->ordered_offsets_up_to = 0;
500 : 93439 : serialised->checked_offsets_up_to = 0;
501 : : }
502 : :
503 : 1021602 : if (serialised->data)
504 : : /* g_variant_store() is a public API, so it
505 : : * it will reacquire the lock if it needs to.
506 : : */
507 : 931131 : g_variant_store (value, serialised->data);
508 : 1021602 : }
509 : :
510 : : /* this ends the main body of the recursive serializer */
511 : :
512 : : /* < private >
513 : : * g_variant_ensure_serialised:
514 : : * @value: a #GVariant
515 : : *
516 : : * Ensures that @value is in serialized form.
517 : : *
518 : : * If @value is in tree form then this function ensures that the
519 : : * serialized size is known and then allocates a buffer of that size and
520 : : * serializes the instance into the buffer. The 'children' array is
521 : : * then released and the instance is set to serialized form based on the
522 : : * contents of the buffer.
523 : : *
524 : : * The current thread must hold the lock on @value.
525 : : */
526 : : static void
527 : 6231954 : g_variant_ensure_serialised (GVariant *value)
528 : : {
529 : 6231954 : g_assert (value->state & STATE_LOCKED);
530 : :
531 : 6231954 : if (~value->state & STATE_SERIALISED)
532 : : {
533 : : GBytes *bytes;
534 : : gpointer data;
535 : :
536 : 9738 : TRACE(GLIB_VARIANT_START_SERIALISE(value, value->type_info));
537 : 9738 : g_variant_ensure_size (value);
538 : 9738 : data = g_malloc (value->size);
539 : 9738 : g_variant_serialise (value, data);
540 : :
541 : 9738 : g_variant_release_children (value);
542 : :
543 : 9738 : bytes = g_bytes_new_take (data, value->size);
544 : 9738 : value->contents.serialised.data = g_bytes_get_data (bytes, NULL);
545 : 9738 : value->contents.serialised.bytes = bytes;
546 : 9738 : value->contents.serialised.ordered_offsets_up_to = G_MAXSIZE;
547 : 9738 : value->contents.serialised.checked_offsets_up_to = G_MAXSIZE;
548 : 9738 : value->state |= STATE_SERIALISED;
549 : 9738 : TRACE(GLIB_VARIANT_END_SERIALISE(value, value->type_info));
550 : : }
551 : 6231954 : }
552 : :
553 : : /* < private >
554 : : * g_variant_alloc:
555 : : * @type: the type of the new instance
556 : : * @serialised: if the instance will be in serialised form
557 : : * @trusted: if the instance will be trusted
558 : : * @suffix_size: amount of extra bytes to add to allocation
559 : : *
560 : : * Allocates a #GVariant instance and does some common work (such as
561 : : * looking up and filling in the type info), setting the state field,
562 : : * and setting the ref_count to 1.
563 : : *
564 : : * Use @suffix_size when you want to store data inside of the GVariant
565 : : * without having to add an additional GBytes allocation.
566 : : *
567 : : * Returns: a new #GVariant with a floating reference
568 : : */
569 : : static GVariant *
570 : 2642507 : g_variant_alloc (const GVariantType *type,
571 : : gboolean serialised,
572 : : gboolean trusted,
573 : : gsize suffix_size)
574 : : {
575 : : G_GNUC_UNUSED gboolean size_check;
576 : : GVariant *value;
577 : : gsize size;
578 : :
579 : 2642507 : size_check = g_size_checked_add (&size, sizeof *value, suffix_size);
580 : 2642507 : g_assert (size_check);
581 : :
582 : 2642507 : value = g_malloc (size);
583 : 2642507 : value->type_info = g_variant_type_info_get (type);
584 : 2642507 : value->state = (serialised ? STATE_SERIALISED : 0) |
585 : 2642507 : (trusted ? STATE_TRUSTED : 0) |
586 : : STATE_FLOATING;
587 : 2642507 : value->size = (gssize) -1;
588 : 2642507 : g_atomic_ref_count_init (&value->ref_count);
589 : 2642507 : value->depth = 0;
590 : :
591 : 2642507 : return value;
592 : : }
593 : :
594 : : /**
595 : : * g_variant_new_from_bytes:
596 : : * @type: a #GVariantType
597 : : * @bytes: a #GBytes
598 : : * @trusted: if the contents of @bytes are trusted
599 : : *
600 : : * Constructs a new serialized-mode #GVariant instance. This is the
601 : : * inner interface for creation of new serialized values that gets
602 : : * called from various functions in gvariant.c.
603 : : *
604 : : * A reference is taken on @bytes.
605 : : *
606 : : * The data in @bytes must be aligned appropriately for the @type being loaded.
607 : : * Otherwise this function will internally create a copy of the memory (since
608 : : * GLib 2.60) or (in older versions) fail and exit the process.
609 : : *
610 : : * Returns: (transfer none): a new #GVariant with a floating reference
611 : : *
612 : : * Since: 2.36
613 : : */
614 : : GVariant *
615 : 577 : g_variant_new_from_bytes (const GVariantType *type,
616 : : GBytes *bytes,
617 : : gboolean trusted)
618 : : {
619 : 577 : return g_variant_new_take_bytes (type, g_bytes_ref (bytes), trusted);
620 : : }
621 : :
622 : : /* -- internal -- */
623 : :
624 : : /* < internal >
625 : : * g_variant_new_preallocated_trusted:
626 : : * @data: data to copy
627 : : * @size: the size of data
628 : : *
629 : : * Creates a new #GVariant for simple types such as int32, double, or
630 : : * bytes.
631 : : *
632 : : * Instead of allocating a GBytes, the data will be stored at the tail of
633 : : * the GVariant structures allocation. This can save considerable malloc
634 : : * overhead.
635 : : *
636 : : * The data is always aligned to the maximum alignment GVariant provides
637 : : * which is 8 bytes and therefore does not need to verify alignment based
638 : : * on the the @type provided.
639 : : *
640 : : * This should only be used for creating GVariant with trusted data.
641 : : *
642 : : * Returns: a new #GVariant with a floating reference
643 : : */
644 : : GVariant *
645 : 2064100 : g_variant_new_preallocated_trusted (const GVariantType *type,
646 : : gconstpointer data,
647 : : gsize size)
648 : : {
649 : : GVariant *value;
650 : : gsize expected_size;
651 : : guint alignment;
652 : :
653 : 2064100 : value = g_variant_alloc (type, TRUE, TRUE, size);
654 : :
655 : 2064100 : g_variant_type_info_query (value->type_info, &alignment, &expected_size);
656 : :
657 : 2064100 : g_assert (expected_size == 0 || size == expected_size);
658 : :
659 : 2064100 : value->contents.serialised.ordered_offsets_up_to = G_MAXSIZE;
660 : 2064100 : value->contents.serialised.checked_offsets_up_to = G_MAXSIZE;
661 : 2064100 : value->contents.serialised.bytes = NULL;
662 : 2064100 : value->contents.serialised.data = value->suffix;
663 : 2064100 : value->size = size;
664 : :
665 : 2064100 : memcpy (value->suffix, data, size);
666 : :
667 : 2064100 : TRACE(GLIB_VARIANT_FROM_BUFFER(value, value->type_info, value->ref_count, value->state));
668 : :
669 : 2064100 : return value;
670 : : }
671 : :
672 : : /* < internal >
673 : : * g_variant_new_take_bytes:
674 : : * @bytes: (transfer full): a #GBytes
675 : : * @trusted: if the contents of @bytes are trusted
676 : : *
677 : : * The same as g_variant_new_from_bytes() but takes ownership
678 : : * of @bytes.
679 : : *
680 : : * Returns: a new #GVariant with a floating reference
681 : : */
682 : : GVariant *
683 : 34177 : g_variant_new_take_bytes (const GVariantType *type,
684 : : GBytes *bytes,
685 : : gboolean trusted)
686 : : {
687 : : GVariant *value;
688 : : guint alignment;
689 : : gsize size;
690 : 34177 : GBytes *owned_bytes = NULL;
691 : : GVariantSerialised serialised;
692 : :
693 : 34177 : value = g_variant_alloc (type, TRUE, trusted, 0);
694 : :
695 : 34177 : g_variant_type_info_query (value->type_info,
696 : : &alignment, &size);
697 : :
698 : : /* Ensure the alignment is correct. This is a huge performance hit if it’s
699 : : * not correct, but that’s better than aborting if a caller provides data
700 : : * with the wrong alignment (which is likely to happen very occasionally, and
701 : : * only cause an abort on some architectures — so is unlikely to be caught
702 : : * in testing). Callers can always actively ensure they use the correct
703 : : * alignment to avoid the performance hit. */
704 : 34177 : serialised.type_info = value->type_info;
705 : 34177 : serialised.data = (guchar *) g_bytes_get_data (bytes, &serialised.size);
706 : 34177 : serialised.depth = 0;
707 : 34177 : serialised.ordered_offsets_up_to = trusted ? G_MAXSIZE : 0;
708 : 34177 : serialised.checked_offsets_up_to = trusted ? G_MAXSIZE : 0;
709 : :
710 : 34177 : if (!g_variant_serialised_check (serialised))
711 : : {
712 : : #ifdef HAVE_POSIX_MEMALIGN
713 : 101 : gpointer aligned_data = NULL;
714 : 101 : gsize aligned_size = g_bytes_get_size (bytes);
715 : :
716 : : /* posix_memalign() requires the alignment to be a multiple of
717 : : * sizeof(void*), and a power of 2. See g_variant_type_info_query() for
718 : : * details on the alignment format.
719 : : *
720 : : * While calling posix_memalign() with aligned_size==0 is safe on glibc,
721 : : * POSIX specifies that the behaviour is implementation-defined, so avoid
722 : : * that and leave aligned_data==NULL in that case.
723 : : * See https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html */
724 : 101 : if (aligned_size != 0 &&
725 : 77 : posix_memalign (&aligned_data, MAX (sizeof (void *), alignment + 1),
726 : : aligned_size) != 0)
727 : 0 : g_error ("posix_memalign failed");
728 : :
729 : 101 : if (aligned_size != 0)
730 : 77 : memcpy (aligned_data, g_bytes_get_data (bytes, NULL), aligned_size);
731 : :
732 : 101 : owned_bytes = bytes;
733 : 101 : bytes = g_bytes_new_with_free_func (aligned_data,
734 : : aligned_size,
735 : : free, aligned_data);
736 : 101 : aligned_data = NULL;
737 : : #else
738 : : /* NOTE: there may be platforms that lack posix_memalign() and also
739 : : * have malloc() that returns non-8-aligned. if so, we need to try
740 : : * harder here.
741 : : */
742 : : owned_bytes = bytes;
743 : : bytes = g_bytes_new (g_bytes_get_data (bytes, NULL),
744 : : g_bytes_get_size (bytes));
745 : : #endif
746 : : }
747 : :
748 : 34177 : value->contents.serialised.bytes = bytes;
749 : :
750 : 34177 : if (size && g_bytes_get_size (bytes) != size)
751 : : {
752 : : /* Creating a fixed-sized GVariant with a bytes of the wrong
753 : : * size.
754 : : *
755 : : * We should do the equivalent of pulling a fixed-sized child out
756 : : * of a brozen container (ie: data is NULL size is equal to the correct
757 : : * fixed size).
758 : : */
759 : 89 : value->contents.serialised.data = NULL;
760 : 89 : value->size = size;
761 : : }
762 : : else
763 : : {
764 : 34088 : value->contents.serialised.data = g_bytes_get_data (bytes, &value->size);
765 : : }
766 : :
767 : 34177 : value->contents.serialised.ordered_offsets_up_to = trusted ? G_MAXSIZE : 0;
768 : 34177 : value->contents.serialised.checked_offsets_up_to = trusted ? G_MAXSIZE : 0;
769 : :
770 : 34177 : g_clear_pointer (&owned_bytes, g_bytes_unref);
771 : :
772 : 34177 : TRACE(GLIB_VARIANT_FROM_BUFFER(value, value->type_info, value->ref_count, value->state));
773 : :
774 : 34177 : return value;
775 : : }
776 : :
777 : : /* < internal >
778 : : * g_variant_new_from_children:
779 : : * @type: a #GVariantType
780 : : * @children: an array of #GVariant pointers. Consumed.
781 : : * @n_children: the length of @children
782 : : * @trusted: %TRUE if every child in @children is trusted
783 : : *
784 : : * Constructs a new tree-mode #GVariant instance. This is the inner
785 : : * interface for creation of new serialized values that gets called from
786 : : * various functions in gvariant.c.
787 : : *
788 : : * @children is consumed by this function. g_free() will be called on
789 : : * it some time later.
790 : : *
791 : : * Returns: a new #GVariant with a floating reference
792 : : */
793 : : GVariant *
794 : 544230 : g_variant_new_from_children (const GVariantType *type,
795 : : GVariant **children,
796 : : gsize n_children,
797 : : gboolean trusted)
798 : : {
799 : : GVariant *value;
800 : :
801 : 544230 : value = g_variant_alloc (type, FALSE, trusted, 0);
802 : 544230 : value->contents.tree.children = children;
803 : 544230 : value->contents.tree.n_children = n_children;
804 : 544230 : TRACE(GLIB_VARIANT_FROM_CHILDREN(value, value->type_info, value->ref_count, value->state));
805 : :
806 : 544230 : return value;
807 : : }
808 : :
809 : : /* < internal >
810 : : * g_variant_get_type_info:
811 : : * @value: a #GVariant
812 : : *
813 : : * Returns the #GVariantTypeInfo corresponding to the type of @value. A
814 : : * reference is not added, so the return value is only good for the
815 : : * duration of the life of @value.
816 : : *
817 : : * Returns: the #GVariantTypeInfo for @value
818 : : */
819 : : GVariantTypeInfo *
820 : 30921294 : g_variant_get_type_info (GVariant *value)
821 : : {
822 : 30921294 : return value->type_info;
823 : : }
824 : :
825 : : /* < internal >
826 : : * g_variant_is_trusted:
827 : : * @value: a #GVariant
828 : : *
829 : : * Determines if @value is trusted by #GVariant to contain only
830 : : * fully-valid data. All values constructed solely via #GVariant APIs
831 : : * are trusted, but values containing data read in from other sources
832 : : * are usually not trusted.
833 : : *
834 : : * The main advantage of trusted data is that certain checks can be
835 : : * skipped. For example, we don't need to check that a string is
836 : : * properly nul-terminated or that an object path is actually a
837 : : * properly-formatted object path.
838 : : *
839 : : * Returns: if @value is trusted
840 : : */
841 : : gboolean
842 : 5422551 : g_variant_is_trusted (GVariant *value)
843 : : {
844 : 5422551 : return (value->state & STATE_TRUSTED) != 0;
845 : : }
846 : :
847 : : /* < internal >
848 : : * g_variant_get_depth:
849 : : * @value: a #GVariant
850 : : *
851 : : * Gets the nesting depth of a #GVariant. This is 0 for a #GVariant with no
852 : : * children.
853 : : *
854 : : * Returns: nesting depth of @value
855 : : */
856 : : gsize
857 : 169 : g_variant_get_depth (GVariant *value)
858 : : {
859 : 169 : return value->depth;
860 : : }
861 : :
862 : : /* -- public -- */
863 : :
864 : : /**
865 : : * g_variant_unref:
866 : : * @value: a #GVariant
867 : : *
868 : : * Decreases the reference count of @value. When its reference count
869 : : * drops to 0, the memory used by the variant is freed.
870 : : *
871 : : * Since: 2.24
872 : : **/
873 : : void
874 : 9874555 : g_variant_unref (GVariant *value)
875 : : {
876 : 9874555 : g_return_if_fail (value != NULL);
877 : :
878 : 9874555 : TRACE(GLIB_VARIANT_UNREF(value, value->type_info, value->ref_count, value->state));
879 : :
880 : 9874555 : if (g_atomic_ref_count_dec (&value->ref_count))
881 : : {
882 : 4865023 : if G_UNLIKELY (value->state & STATE_LOCKED)
883 : 0 : g_critical ("attempting to free a locked GVariant instance. "
884 : : "This should never happen.");
885 : :
886 : 4865023 : value->state |= STATE_LOCKED;
887 : :
888 : 4865023 : g_variant_type_info_unref (value->type_info);
889 : :
890 : 4865023 : if (value->state & STATE_SERIALISED)
891 : 4330579 : g_bytes_unref (value->contents.serialised.bytes);
892 : : else
893 : 534444 : g_variant_release_children (value);
894 : :
895 : 4865023 : memset (value, 0, sizeof (GVariant));
896 : 4865023 : g_free (value);
897 : : }
898 : : }
899 : :
900 : : /**
901 : : * g_variant_ref:
902 : : * @value: a #GVariant
903 : : *
904 : : * Increases the reference count of @value.
905 : : *
906 : : * Returns: the same @value
907 : : *
908 : : * Since: 2.24
909 : : **/
910 : : GVariant *
911 : 4183875 : g_variant_ref (GVariant *value)
912 : : {
913 : 4183875 : g_return_val_if_fail (value != NULL, NULL);
914 : :
915 : 4183875 : TRACE(GLIB_VARIANT_REF(value, value->type_info, value->ref_count, value->state));
916 : :
917 : 4183875 : g_atomic_ref_count_inc (&value->ref_count);
918 : :
919 : 4183875 : return value;
920 : : }
921 : :
922 : : /**
923 : : * g_variant_ref_sink:
924 : : * @value: a #GVariant
925 : : *
926 : : * #GVariant uses a floating reference count system. All functions with
927 : : * names starting with `g_variant_new_` return floating
928 : : * references.
929 : : *
930 : : * Calling g_variant_ref_sink() on a #GVariant with a floating reference
931 : : * will convert the floating reference into a full reference. Calling
932 : : * g_variant_ref_sink() on a non-floating #GVariant results in an
933 : : * additional normal reference being added.
934 : : *
935 : : * In other words, if the @value is floating, then this call "assumes
936 : : * ownership" of the floating reference, converting it to a normal
937 : : * reference. If the @value is not floating, then this call adds a
938 : : * new normal reference increasing the reference count by one.
939 : : *
940 : : * All calls that result in a #GVariant instance being inserted into a
941 : : * container will call g_variant_ref_sink() on the instance. This means
942 : : * that if the value was just created (and has only its floating
943 : : * reference) then the container will assume sole ownership of the value
944 : : * at that point and the caller will not need to unreference it. This
945 : : * makes certain common styles of programming much easier while still
946 : : * maintaining normal refcounting semantics in situations where values
947 : : * are not floating.
948 : : *
949 : : * Returns: the same @value
950 : : *
951 : : * Since: 2.24
952 : : **/
953 : : GVariant *
954 : 2808560 : g_variant_ref_sink (GVariant *value)
955 : : {
956 : : int old_state;
957 : :
958 : 2808560 : g_return_val_if_fail (value != NULL, NULL);
959 : 2808560 : g_return_val_if_fail (!g_atomic_ref_count_compare (&value->ref_count, 0), NULL);
960 : :
961 : 2808560 : TRACE(GLIB_VARIANT_REF_SINK(value, value->type_info, value->ref_count, value->state, value->state & STATE_FLOATING));
962 : :
963 : 2808560 : old_state = value->state;
964 : :
965 : 2808560 : while (old_state & STATE_FLOATING)
966 : : {
967 : 1982890 : int new_state = old_state & ~STATE_FLOATING;
968 : :
969 : 1982890 : if (g_atomic_int_compare_and_exchange_full (&value->state, old_state, new_state, &old_state))
970 : 1982890 : return value;
971 : : }
972 : :
973 : 825670 : g_atomic_ref_count_inc (&value->ref_count);
974 : :
975 : 825670 : return value;
976 : : }
977 : :
978 : : /**
979 : : * g_variant_take_ref:
980 : : * @value: a #GVariant
981 : : *
982 : : * If @value is floating, sink it. Otherwise, do nothing.
983 : : *
984 : : * Typically you want to use g_variant_ref_sink() in order to
985 : : * automatically do the correct thing with respect to floating or
986 : : * non-floating references, but there is one specific scenario where
987 : : * this function is helpful.
988 : : *
989 : : * The situation where this function is helpful is when creating an API
990 : : * that allows the user to provide a callback function that returns a
991 : : * #GVariant. We certainly want to allow the user the flexibility to
992 : : * return a non-floating reference from this callback (for the case
993 : : * where the value that is being returned already exists).
994 : : *
995 : : * At the same time, the style of the #GVariant API makes it likely that
996 : : * for newly-created #GVariant instances, the user can be saved some
997 : : * typing if they are allowed to return a #GVariant with a floating
998 : : * reference.
999 : : *
1000 : : * Using this function on the return value of the user's callback allows
1001 : : * the user to do whichever is more convenient for them. The caller
1002 : : * will always receives exactly one full reference to the value: either
1003 : : * the one that was returned in the first place, or a floating reference
1004 : : * that has been converted to a full reference.
1005 : : *
1006 : : * This function has an odd interaction when combined with
1007 : : * g_variant_ref_sink() running at the same time in another thread on
1008 : : * the same #GVariant instance. If g_variant_ref_sink() runs first then
1009 : : * the result will be that the floating reference is converted to a hard
1010 : : * reference. If g_variant_take_ref() runs first then the result will
1011 : : * be that the floating reference is converted to a hard reference and
1012 : : * an additional reference on top of that one is added. It is best to
1013 : : * avoid this situation.
1014 : : *
1015 : : * Returns: the same @value
1016 : : **/
1017 : : GVariant *
1018 : 644097 : g_variant_take_ref (GVariant *value)
1019 : : {
1020 : 644097 : g_return_val_if_fail (value != NULL, NULL);
1021 : 644097 : g_return_val_if_fail (!g_atomic_ref_count_compare (&value->ref_count, 0), NULL);
1022 : :
1023 : 644097 : TRACE(GLIB_VARIANT_TAKE_REF(value, value->type_info, value->ref_count, value->state, value->state & STATE_FLOATING));
1024 : 644097 : g_atomic_int_and (&value->state, ~STATE_FLOATING);
1025 : :
1026 : 644097 : return value;
1027 : : }
1028 : :
1029 : : /**
1030 : : * g_variant_is_floating:
1031 : : * @value: a #GVariant
1032 : : *
1033 : : * Checks whether @value has a floating reference count.
1034 : : *
1035 : : * This function should only ever be used to assert that a given variant
1036 : : * is or is not floating, or for debug purposes. To acquire a reference
1037 : : * to a variant that might be floating, always use g_variant_ref_sink()
1038 : : * or g_variant_take_ref().
1039 : : *
1040 : : * See g_variant_ref_sink() for more information about floating reference
1041 : : * counts.
1042 : : *
1043 : : * Returns: whether @value is floating
1044 : : *
1045 : : * Since: 2.26
1046 : : **/
1047 : : gboolean
1048 : 1215 : g_variant_is_floating (GVariant *value)
1049 : : {
1050 : 1215 : g_return_val_if_fail (value != NULL, FALSE);
1051 : :
1052 : 1215 : return (value->state & STATE_FLOATING) != 0;
1053 : : }
1054 : :
1055 : : /**
1056 : : * g_variant_get_size:
1057 : : * @value: a #GVariant instance
1058 : : *
1059 : : * Determines the number of bytes that would be required to store @value
1060 : : * with g_variant_store().
1061 : : *
1062 : : * If @value has a fixed-sized type then this function always returned
1063 : : * that fixed size.
1064 : : *
1065 : : * In the case that @value is already in serialized form or the size has
1066 : : * already been calculated (ie: this function has been called before)
1067 : : * then this function is O(1). Otherwise, the size is calculated, an
1068 : : * operation which is approximately O(n) in the number of values
1069 : : * involved.
1070 : : *
1071 : : * Returns: the serialized size of @value
1072 : : *
1073 : : * Since: 2.24
1074 : : **/
1075 : : gsize
1076 : 3553740 : g_variant_get_size (GVariant *value)
1077 : : {
1078 : 3553740 : g_variant_lock (value);
1079 : 3553740 : g_variant_ensure_size (value);
1080 : 3553740 : g_variant_unlock (value);
1081 : :
1082 : 3553740 : return value->size;
1083 : : }
1084 : :
1085 : : /**
1086 : : * g_variant_get_data:
1087 : : * @value: a #GVariant instance
1088 : : *
1089 : : * Returns a pointer to the serialized form of a #GVariant instance.
1090 : : * The returned data may not be in fully-normalised form if read from an
1091 : : * untrusted source. The returned data must not be freed; it remains
1092 : : * valid for as long as @value exists.
1093 : : *
1094 : : * If @value is a fixed-sized value that was deserialized from a
1095 : : * corrupted serialized container then %NULL may be returned. In this
1096 : : * case, the proper thing to do is typically to use the appropriate
1097 : : * number of nul bytes in place of @value. If @value is not fixed-sized
1098 : : * then %NULL is never returned.
1099 : : *
1100 : : * In the case that @value is already in serialized form, this function
1101 : : * is O(1). If the value is not already in serialized form,
1102 : : * serialization occurs implicitly and is approximately O(n) in the size
1103 : : * of the result.
1104 : : *
1105 : : * To deserialize the data returned by this function, in addition to the
1106 : : * serialized data, you must know the type of the #GVariant, and (if the
1107 : : * machine might be different) the endianness of the machine that stored
1108 : : * it. As a result, file formats or network messages that incorporate
1109 : : * serialized #GVariants must include this information either
1110 : : * implicitly (for instance "the file always contains a
1111 : : * %G_VARIANT_TYPE_VARIANT and it is always in little-endian order") or
1112 : : * explicitly (by storing the type and/or endianness in addition to the
1113 : : * serialized data).
1114 : : *
1115 : : * Returns: (transfer none): the serialized form of @value, or %NULL
1116 : : *
1117 : : * Since: 2.24
1118 : : **/
1119 : : gconstpointer
1120 : 6231918 : g_variant_get_data (GVariant *value)
1121 : : {
1122 : 6231918 : g_variant_lock (value);
1123 : 6231918 : g_variant_ensure_serialised (value);
1124 : 6231918 : g_variant_unlock (value);
1125 : :
1126 : 6231918 : return value->contents.serialised.data;
1127 : : }
1128 : :
1129 : : /**
1130 : : * g_variant_get_data_as_bytes:
1131 : : * @value: a #GVariant
1132 : : *
1133 : : * Returns a pointer to the serialized form of a #GVariant instance.
1134 : : * The semantics of this function are exactly the same as
1135 : : * g_variant_get_data(), except that the returned #GBytes holds
1136 : : * a reference to the variant data.
1137 : : *
1138 : : * Returns: (transfer full): A new #GBytes representing the variant data
1139 : : *
1140 : : * Since: 2.36
1141 : : */
1142 : : GBytes *
1143 : 36 : g_variant_get_data_as_bytes (GVariant *value)
1144 : : {
1145 : : const gchar *bytes_data;
1146 : : const gchar *data;
1147 : 36 : gsize bytes_size = 0;
1148 : : gsize size;
1149 : :
1150 : 36 : g_variant_lock (value);
1151 : 36 : g_variant_ensure_serialised (value);
1152 : 36 : g_variant_unlock (value);
1153 : :
1154 : 36 : if (value->contents.serialised.bytes != NULL)
1155 : 12 : bytes_data = g_bytes_get_data (value->contents.serialised.bytes, &bytes_size);
1156 : : else
1157 : 24 : bytes_data = NULL;
1158 : :
1159 : 36 : data = value->contents.serialised.data;
1160 : 36 : size = value->size;
1161 : :
1162 : 36 : if (data == NULL)
1163 : : {
1164 : 2 : g_assert (size == 0);
1165 : 2 : data = bytes_data;
1166 : : }
1167 : :
1168 : 36 : if (bytes_data != NULL && data == bytes_data && size == bytes_size)
1169 : 8 : return g_bytes_ref (value->contents.serialised.bytes);
1170 : 28 : else if (bytes_data != NULL)
1171 : 3 : return g_bytes_new_from_bytes (value->contents.serialised.bytes,
1172 : 3 : data - bytes_data, size);
1173 : : else
1174 : 25 : return g_bytes_new (value->contents.serialised.data, size);
1175 : : }
1176 : :
1177 : :
1178 : : /**
1179 : : * g_variant_n_children:
1180 : : * @value: a container #GVariant
1181 : : *
1182 : : * Determines the number of children in a container #GVariant instance.
1183 : : * This includes variants, maybes, arrays, tuples and dictionary
1184 : : * entries. It is an error to call this function on any other type of
1185 : : * #GVariant.
1186 : : *
1187 : : * For variants, the return value is always 1. For values with maybe
1188 : : * types, it is always zero or one. For arrays, it is the length of the
1189 : : * array. For tuples it is the number of tuple items (which depends
1190 : : * only on the type). For dictionary entries, it is always 2
1191 : : *
1192 : : * This function is O(1).
1193 : : *
1194 : : * Returns: the number of children in the container
1195 : : *
1196 : : * Since: 2.24
1197 : : **/
1198 : : gsize
1199 : 2129722 : g_variant_n_children (GVariant *value)
1200 : : {
1201 : : gsize n_children;
1202 : :
1203 : 2129722 : g_variant_lock (value);
1204 : :
1205 : 2129722 : if (value->state & STATE_SERIALISED)
1206 : 89225 : n_children = g_variant_serialised_n_children (
1207 : : g_variant_to_serialised (value));
1208 : : else
1209 : 2040497 : n_children = value->contents.tree.n_children;
1210 : :
1211 : 2129722 : g_variant_unlock (value);
1212 : :
1213 : 2129722 : return n_children;
1214 : : }
1215 : :
1216 : : /**
1217 : : * g_variant_get_child_value:
1218 : : * @value: a container #GVariant
1219 : : * @index_: the index of the child to fetch
1220 : : *
1221 : : * Reads a child item out of a container #GVariant instance. This
1222 : : * includes variants, maybes, arrays, tuples and dictionary
1223 : : * entries. It is an error to call this function on any other type of
1224 : : * #GVariant.
1225 : : *
1226 : : * It is an error if @index_ is greater than the number of child items
1227 : : * in the container. See g_variant_n_children().
1228 : : *
1229 : : * The returned value is never floating. You should free it with
1230 : : * g_variant_unref() when you're done with it.
1231 : : *
1232 : : * Note that values borrowed from the returned child are not guaranteed to
1233 : : * still be valid after the child is freed even if you still hold a reference
1234 : : * to @value, if @value has not been serialized at the time this function is
1235 : : * called. To avoid this, you can serialize @value by calling
1236 : : * g_variant_get_data() and optionally ignoring the return value.
1237 : : *
1238 : : * There may be implementation specific restrictions on deeply nested values,
1239 : : * which would result in the unit tuple being returned as the child value,
1240 : : * instead of further nested children. #GVariant is guaranteed to handle
1241 : : * nesting up to at least 64 levels.
1242 : : *
1243 : : * This function is O(1).
1244 : : *
1245 : : * Returns: (transfer full): the child at the specified index
1246 : : *
1247 : : * Since: 2.24
1248 : : **/
1249 : : GVariant *
1250 : 4053969 : g_variant_get_child_value (GVariant *value,
1251 : : gsize index_)
1252 : : {
1253 : 4053969 : g_return_val_if_fail (value->depth < G_MAXSIZE, NULL);
1254 : :
1255 : 4053969 : if (~g_atomic_int_get (&value->state) & STATE_SERIALISED)
1256 : : {
1257 : : /* g_variant_serialised_get_child() does its own checks on index_ */
1258 : 1830574 : g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
1259 : :
1260 : 1830574 : g_variant_lock (value);
1261 : :
1262 : 1830574 : if (~value->state & STATE_SERIALISED)
1263 : : {
1264 : : GVariant *child;
1265 : :
1266 : 1830574 : child = g_variant_ref (value->contents.tree.children[index_]);
1267 : 1830574 : g_variant_unlock (value);
1268 : :
1269 : 1830574 : return child;
1270 : : }
1271 : :
1272 : 0 : g_variant_unlock (value);
1273 : : }
1274 : :
1275 : : {
1276 : 2223395 : GVariantSerialised serialised = g_variant_to_serialised (value);
1277 : : GVariantSerialised s_child;
1278 : : GVariant *child;
1279 : :
1280 : : /* get the serializer to extract the serialized data for the child
1281 : : * from the serialized data for the container
1282 : : */
1283 : 2223395 : s_child = g_variant_serialised_get_child (serialised, index_);
1284 : :
1285 : : /* Update the cached ordered_offsets_up_to, since @serialised will be thrown away when this function exits */
1286 : 2223395 : value->contents.serialised.ordered_offsets_up_to = MAX (value->contents.serialised.ordered_offsets_up_to, serialised.ordered_offsets_up_to);
1287 : 2223395 : value->contents.serialised.checked_offsets_up_to = MAX (value->contents.serialised.checked_offsets_up_to, serialised.checked_offsets_up_to);
1288 : :
1289 : : /* Check whether this would cause nesting too deep. If so, return a fake
1290 : : * child. The only situation we expect this to happen in is with a variant,
1291 : : * as all other deeply-nested types have a static type, and hence should
1292 : : * have been rejected earlier. In the case of a variant whose nesting plus
1293 : : * the depth of its child is too great, return a unit variant () instead of
1294 : : * the real child. */
1295 : 2223395 : if (!(value->state & STATE_TRUSTED) &&
1296 : 149558 : g_variant_type_info_query_depth (s_child.type_info) >=
1297 : 149558 : G_VARIANT_MAX_RECURSION_DEPTH - value->depth)
1298 : : {
1299 : 0 : g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT));
1300 : 0 : g_variant_type_info_unref (s_child.type_info);
1301 : 0 : return g_variant_new_tuple (NULL, 0);
1302 : : }
1303 : :
1304 : : /* create a new serialized instance out of it */
1305 : 2223395 : child = g_new (GVariant, 1);
1306 : 2223395 : child->type_info = s_child.type_info;
1307 : 2223395 : child->state = (value->state & STATE_TRUSTED) |
1308 : : STATE_SERIALISED;
1309 : 2223395 : child->size = s_child.size;
1310 : 2223395 : g_atomic_ref_count_init (&child->ref_count);
1311 : 2223395 : child->depth = value->depth + 1;
1312 : 2223395 : child->contents.serialised.bytes =
1313 : 2223395 : g_bytes_ref (value->contents.serialised.bytes);
1314 : 2223395 : child->contents.serialised.data = s_child.data;
1315 : 2223395 : child->contents.serialised.ordered_offsets_up_to = (value->state & STATE_TRUSTED) ? G_MAXSIZE : s_child.ordered_offsets_up_to;
1316 : 2223395 : child->contents.serialised.checked_offsets_up_to = (value->state & STATE_TRUSTED) ? G_MAXSIZE : s_child.checked_offsets_up_to;
1317 : :
1318 : 2223395 : TRACE(GLIB_VARIANT_FROM_PARENT(child, child->type_info, child->ref_count, child->state, value));
1319 : :
1320 : 2223395 : return child;
1321 : : }
1322 : : }
1323 : :
1324 : : /**
1325 : : * g_variant_maybe_get_child_value:
1326 : : * @value: a container #GVariant
1327 : : * @index_: the index of the child to fetch
1328 : : *
1329 : : * Reads a child item out of a container #GVariant instance, if it is in normal
1330 : : * form. If it is not in normal form, return %NULL.
1331 : : *
1332 : : * This function behaves the same as g_variant_get_child_value(), except that it
1333 : : * returns %NULL if the child is not in normal form. g_variant_get_child_value()
1334 : : * would instead return a new default value of the correct type.
1335 : : *
1336 : : * This is intended to be used internally to avoid unnecessary #GVariant
1337 : : * allocations.
1338 : : *
1339 : : * The returned value is never floating. You should free it with
1340 : : * g_variant_unref() when you're done with it.
1341 : : *
1342 : : * This function is O(1).
1343 : : *
1344 : : * Returns: (transfer full): the child at the specified index
1345 : : *
1346 : : * Since: 2.74
1347 : : */
1348 : : GVariant *
1349 : 22025 : g_variant_maybe_get_child_value (GVariant *value,
1350 : : gsize index_)
1351 : : {
1352 : 22025 : g_return_val_if_fail (value->depth < G_MAXSIZE, NULL);
1353 : :
1354 : 22025 : if (~g_atomic_int_get (&value->state) & STATE_SERIALISED)
1355 : : {
1356 : : /* g_variant_serialised_get_child() does its own checks on index_ */
1357 : 0 : g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
1358 : :
1359 : 0 : g_variant_lock (value);
1360 : :
1361 : 0 : if (~value->state & STATE_SERIALISED)
1362 : : {
1363 : : GVariant *child;
1364 : :
1365 : 0 : child = g_variant_ref (value->contents.tree.children[index_]);
1366 : 0 : g_variant_unlock (value);
1367 : :
1368 : 0 : return child;
1369 : : }
1370 : :
1371 : 0 : g_variant_unlock (value);
1372 : : }
1373 : :
1374 : : {
1375 : 22025 : GVariantSerialised serialised = g_variant_to_serialised (value);
1376 : : GVariantSerialised s_child;
1377 : :
1378 : : /* get the serializer to extract the serialized data for the child
1379 : : * from the serialized data for the container
1380 : : */
1381 : 22025 : s_child = g_variant_serialised_get_child (serialised, index_);
1382 : :
1383 : 22025 : if (!(value->state & STATE_TRUSTED) && s_child.data == NULL)
1384 : : {
1385 : 22015 : g_variant_type_info_unref (s_child.type_info);
1386 : 22015 : return NULL;
1387 : : }
1388 : :
1389 : 10 : g_variant_type_info_unref (s_child.type_info);
1390 : 10 : return g_variant_get_child_value (value, index_);
1391 : : }
1392 : : }
1393 : :
1394 : : /**
1395 : : * g_variant_store:
1396 : : * @value: the #GVariant to store
1397 : : * @data: (not nullable): the location to store the serialized data at
1398 : : *
1399 : : * Stores the serialized form of @value at @data. @data should be
1400 : : * large enough. See g_variant_get_size().
1401 : : *
1402 : : * The stored data is in machine native byte order but may not be in
1403 : : * fully-normalised form if read from an untrusted source. See
1404 : : * g_variant_get_normal_form() for a solution.
1405 : : *
1406 : : * As with g_variant_get_data(), to be able to deserialize the
1407 : : * serialized variant successfully, its type and (if the destination
1408 : : * machine might be different) its endianness must also be available.
1409 : : *
1410 : : * This function is approximately O(n) in the size of @data.
1411 : : *
1412 : : * Since: 2.24
1413 : : **/
1414 : : void
1415 : 931485 : g_variant_store (GVariant *value,
1416 : : gpointer data)
1417 : : {
1418 : 931485 : g_variant_lock (value);
1419 : :
1420 : 931485 : if (value->state & STATE_SERIALISED)
1421 : : {
1422 : 884358 : if (value->contents.serialised.data != NULL)
1423 : 884318 : memcpy (data, value->contents.serialised.data, value->size);
1424 : : else
1425 : 40 : memset (data, 0, value->size);
1426 : : }
1427 : : else
1428 : 47127 : g_variant_serialise (value, data);
1429 : :
1430 : 931485 : g_variant_unlock (value);
1431 : 931485 : }
1432 : :
1433 : : /**
1434 : : * g_variant_is_normal_form:
1435 : : * @value: a #GVariant instance
1436 : : *
1437 : : * Checks if @value is in normal form.
1438 : : *
1439 : : * The main reason to do this is to detect if a given chunk of
1440 : : * serialized data is in normal form: load the data into a #GVariant
1441 : : * using g_variant_new_from_data() and then use this function to
1442 : : * check.
1443 : : *
1444 : : * If @value is found to be in normal form then it will be marked as
1445 : : * being trusted. If the value was already marked as being trusted then
1446 : : * this function will immediately return %TRUE.
1447 : : *
1448 : : * There may be implementation specific restrictions on deeply nested values.
1449 : : * GVariant is guaranteed to handle nesting up to at least 64 levels.
1450 : : *
1451 : : * Returns: %TRUE if @value is in normal form
1452 : : *
1453 : : * Since: 2.24
1454 : : **/
1455 : : gboolean
1456 : 666 : g_variant_is_normal_form (GVariant *value)
1457 : : {
1458 : 666 : if (value->state & STATE_TRUSTED)
1459 : 95 : return TRUE;
1460 : :
1461 : 571 : g_variant_lock (value);
1462 : :
1463 : 571 : if (value->depth >= G_VARIANT_MAX_RECURSION_DEPTH)
1464 : 0 : return FALSE;
1465 : :
1466 : 571 : if (value->state & STATE_SERIALISED)
1467 : : {
1468 : 571 : if (g_variant_serialised_is_normal (g_variant_to_serialised (value)))
1469 : 297 : value->state |= STATE_TRUSTED;
1470 : : }
1471 : : else
1472 : : {
1473 : 0 : gboolean normal = TRUE;
1474 : : gsize i;
1475 : :
1476 : 0 : for (i = 0; i < value->contents.tree.n_children; i++)
1477 : 0 : normal &= g_variant_is_normal_form (value->contents.tree.children[i]);
1478 : :
1479 : 0 : if (normal)
1480 : 0 : value->state |= STATE_TRUSTED;
1481 : : }
1482 : :
1483 : 571 : g_variant_unlock (value);
1484 : :
1485 : 571 : return (value->state & STATE_TRUSTED) != 0;
1486 : : }
|