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 : 11586710 : g_variant_lock (GVariant *value)
274 : : {
275 : 11586710 : g_bit_lock (&value->state, 0);
276 : 11586710 : }
277 : :
278 : : /* < private >
279 : : * g_variant_unlock:
280 : : * @value: a #GVariant
281 : : *
282 : : * Unlocks @value after performing sensitive operations.
283 : : */
284 : : static void
285 : 11586710 : g_variant_unlock (GVariant *value)
286 : : {
287 : 11586710 : g_bit_unlock (&value->state, 0);
288 : 11586710 : }
289 : :
290 : : static inline gboolean
291 : 28811054 : g_variant_is_serialised (GVariant *value)
292 : : {
293 : 28811054 : return (g_atomic_int_get (&value->state) & STATE_SERIALISED) != 0;
294 : : }
295 : :
296 : : /* < private >
297 : : * g_variant_release_children:
298 : : * @value: a #GVariant
299 : : *
300 : : * Releases the reference held on each child in the 'children' array of
301 : : * @value and frees the array itself. @value must be in tree form.
302 : : *
303 : : * This is done when freeing a tree-form instance or converting it to
304 : : * serialized form.
305 : : *
306 : : * The current thread must hold the lock on @value.
307 : : */
308 : : static void
309 : 681359 : g_variant_release_children (GVariant *value)
310 : : {
311 : : gsize i;
312 : :
313 : 681359 : g_assert (value->state & STATE_LOCKED);
314 : 681359 : g_assert (~value->state & STATE_SERIALISED);
315 : :
316 : 5735759 : for (i = 0; i < value->contents.tree.n_children; i++)
317 : 5054400 : g_variant_unref (value->contents.tree.children[i]);
318 : :
319 : 681359 : g_free (value->contents.tree.children);
320 : 681359 : }
321 : :
322 : : /* This begins the main body of the recursive serializer.
323 : : *
324 : : * There are 3 functions here that work as a team with the serializer to
325 : : * get things done. g_variant_store() has a trivial role, but as a
326 : : * public API function, it has its definition elsewhere.
327 : : *
328 : : * Note that "serialization" of an instance does not mean that the
329 : : * instance is converted to serialized form -- it means that the
330 : : * serialized form of an instance is written to an external buffer.
331 : : * g_variant_ensure_serialised() (which is not part of this set of
332 : : * functions) is the function that is responsible for converting an
333 : : * instance to serialized form.
334 : : *
335 : : * We are only concerned here with container types since non-container
336 : : * instances are always in serialized form. For these instances,
337 : : * storing their serialized form merely involves a memcpy().
338 : : *
339 : : * Serialization is a two-step process. First, the size of the
340 : : * serialized data must be calculated so that an appropriately-sized
341 : : * buffer can be allocated. Second, the data is written into the
342 : : * buffer.
343 : : *
344 : : * Determining the size:
345 : : * The process of determining the size is triggered by a call to
346 : : * g_variant_ensure_size() on a container. This invokes the
347 : : * serializer code to determine the size. The serializer is passed
348 : : * g_variant_fill_gvs() as a callback.
349 : : *
350 : : * g_variant_fill_gvs() is called by the serializer on each child of
351 : : * the container which, in turn, calls g_variant_ensure_size() on
352 : : * itself and fills in the result of its own size calculation.
353 : : *
354 : : * The serializer uses the size information from the children to
355 : : * calculate the size needed for the entire container.
356 : : *
357 : : * Writing the data:
358 : : * After the buffer has been allocated, g_variant_serialise() is
359 : : * called on the container. This invokes the serializer code to write
360 : : * the bytes to the container. The serializer is, again, passed
361 : : * g_variant_fill_gvs() as a callback.
362 : : *
363 : : * This time, when g_variant_fill_gvs() is called for each child, the
364 : : * child is given a pointer to a sub-region of the allocated buffer
365 : : * where it should write its data. This is done by calling
366 : : * g_variant_store(). In the event that the instance is in serialized
367 : : * form this means a memcpy() of the serialized data into the
368 : : * allocated buffer. In the event that the instance is in tree form
369 : : * this means a recursive call back into g_variant_serialise().
370 : : *
371 : : *
372 : : * The forward declaration here allows corecursion via callback:
373 : : */
374 : : static void g_variant_fill_gvs (GVariantSerialised *, gpointer);
375 : :
376 : : /* < private >
377 : : * g_variant_ensure_size:
378 : : * @value: a #GVariant
379 : : *
380 : : * Ensures that the ->size field of @value is filled in properly. This
381 : : * must be done as a precursor to any serialization of the value in
382 : : * order to know how large of a buffer is needed to store the data.
383 : : *
384 : : * The current thread must hold the lock on @value.
385 : : */
386 : : static void
387 : 2230750 : g_variant_ensure_size (GVariant *value)
388 : : {
389 : 2230750 : g_assert (value->state & STATE_LOCKED);
390 : :
391 : 2230750 : if (value->size == (gsize) -1)
392 : : {
393 : : gpointer *children;
394 : : gsize n_children;
395 : :
396 : 69201 : children = (gpointer *) value->contents.tree.children;
397 : 69201 : n_children = value->contents.tree.n_children;
398 : 88266 : value->size = g_variant_serialiser_needed_size (value->type_info,
399 : : g_variant_fill_gvs,
400 : 19065 : children, n_children);
401 : 19065 : }
402 : 2230750 : }
403 : :
404 : : /* < private >
405 : : * g_variant_to_serialised:
406 : : * @value: a #GVariant
407 : : *
408 : : * Gets a GVariantSerialised for a GVariant in state STATE_SERIALISED.
409 : : */
410 : : inline static GVariantSerialised
411 : 9717716 : g_variant_to_serialised (GVariant *value)
412 : : {
413 : 9717716 : g_assert (value->state & STATE_SERIALISED);
414 : : {
415 : 39570464 : GVariantSerialised serialised = {
416 : 9717716 : value->type_info,
417 : 9717716 : (gpointer) value->contents.serialised.data,
418 : 9717716 : value->size,
419 : 9717716 : value->depth,
420 : 9717716 : value->contents.serialised.ordered_offsets_up_to,
421 : 9717716 : value->contents.serialised.checked_offsets_up_to,
422 : : };
423 : 9717716 : return serialised;
424 : : }
425 : : }
426 : :
427 : : /* < private >
428 : : * g_variant_serialise:
429 : : * @value: a #GVariant
430 : : * @data: an appropriately-sized buffer
431 : : *
432 : : * Serializes @value into @data. @value must be in tree form.
433 : : *
434 : : * No change is made to @value.
435 : : *
436 : : * The current thread must hold the lock on @value.
437 : : */
438 : : static void
439 : 69461 : g_variant_serialise (GVariant *value,
440 : : gpointer data)
441 : : {
442 : 69461 : GVariantSerialised serialised = { 0, };
443 : : gpointer *children;
444 : : gsize n_children;
445 : :
446 : 69461 : g_assert (~value->state & STATE_SERIALISED);
447 : 69461 : g_assert (value->state & STATE_LOCKED);
448 : :
449 : 69461 : serialised.type_info = value->type_info;
450 : 69461 : serialised.size = value->size;
451 : 69461 : serialised.data = data;
452 : 69461 : serialised.depth = value->depth;
453 : 69461 : serialised.ordered_offsets_up_to = 0;
454 : 69461 : serialised.checked_offsets_up_to = 0;
455 : :
456 : 69461 : children = (gpointer *) value->contents.tree.children;
457 : 69461 : n_children = value->contents.tree.n_children;
458 : :
459 : 69461 : g_variant_serialiser_serialise (serialised, g_variant_fill_gvs,
460 : 19197 : children, n_children);
461 : 69461 : }
462 : :
463 : : /* < private >
464 : : * g_variant_fill_gvs:
465 : : * @serialised: a pointer to a #GVariantSerialised
466 : : * @data: a #GVariant instance
467 : : *
468 : : * This is the callback that is passed by a tree-form container instance
469 : : * to the serializer. This callback gets called on each child of the
470 : : * container. Each child is responsible for performing the following
471 : : * actions:
472 : : *
473 : : * - reporting its type
474 : : *
475 : : * - reporting its serialized size (requires knowing the size first)
476 : : *
477 : : * - possibly storing its serialized form into the provided buffer
478 : : */
479 : : static void
480 : 2218872 : g_variant_fill_gvs (GVariantSerialised *serialised,
481 : : gpointer data)
482 : : {
483 : 2218872 : GVariant *value = data;
484 : :
485 : 2218872 : g_variant_lock (value);
486 : 2218872 : g_variant_ensure_size (value);
487 : 2218872 : g_variant_unlock (value);
488 : :
489 : 2218872 : if (serialised->type_info == NULL)
490 : 234355 : serialised->type_info = value->type_info;
491 : 2218872 : g_assert (serialised->type_info == value->type_info);
492 : :
493 : 2218872 : if (serialised->size == 0)
494 : 233303 : serialised->size = value->size;
495 : 2218872 : g_assert (serialised->size == value->size);
496 : 2218872 : serialised->depth = value->depth;
497 : :
498 : 2218872 : if (value->state & STATE_SERIALISED)
499 : : {
500 : 2112885 : serialised->ordered_offsets_up_to = value->contents.serialised.ordered_offsets_up_to;
501 : 2112885 : serialised->checked_offsets_up_to = value->contents.serialised.checked_offsets_up_to;
502 : 846873 : }
503 : : else
504 : : {
505 : 105987 : serialised->ordered_offsets_up_to = 0;
506 : 105987 : serialised->checked_offsets_up_to = 0;
507 : : }
508 : :
509 : 2218872 : if (serialised->data)
510 : : /* g_variant_store() is a public API, so it
511 : : * it will reacquire the lock if it needs to.
512 : : */
513 : 2121293 : g_variant_store (value, serialised->data);
514 : 2218872 : }
515 : :
516 : : /* this ends the main body of the recursive serializer */
517 : :
518 : : /* < private >
519 : : * g_variant_ensure_serialised:
520 : : * @value: a #GVariant
521 : : *
522 : : * Ensures that @value is in serialized form.
523 : : *
524 : : * If @value is in tree form then this function ensures that the
525 : : * serialized size is known and then allocates a buffer of that size and
526 : : * serializes the instance into the buffer. The 'children' array is
527 : : * then released and the instance is set to serialized form based on the
528 : : * contents of the buffer.
529 : : *
530 : : * The current thread must hold the lock on @value.
531 : : */
532 : : static void
533 : 10849 : g_variant_ensure_serialised (GVariant *value)
534 : : {
535 : 10849 : g_assert (value->state & STATE_LOCKED);
536 : :
537 : 10849 : if (~value->state & STATE_SERIALISED)
538 : : {
539 : : GBytes *bytes;
540 : : gpointer data;
541 : :
542 : 10580 : TRACE(GLIB_VARIANT_START_SERIALISE(value, value->type_info));
543 : 10849 : g_variant_ensure_size (value);
544 : 10849 : data = g_malloc (value->size);
545 : 10849 : g_variant_serialise (value, data);
546 : :
547 : 10849 : g_variant_release_children (value);
548 : :
549 : 10849 : bytes = g_bytes_new_take (data, value->size);
550 : 10849 : value->contents.serialised.data = g_bytes_get_data (bytes, NULL);
551 : 10849 : value->contents.serialised.bytes = bytes;
552 : 10849 : value->contents.serialised.ordered_offsets_up_to = G_MAXSIZE;
553 : 10849 : value->contents.serialised.checked_offsets_up_to = G_MAXSIZE;
554 : 10849 : value->state |= STATE_SERIALISED;
555 : 10580 : TRACE(GLIB_VARIANT_END_SERIALISE(value, value->type_info));
556 : 269 : }
557 : 10849 : }
558 : :
559 : : /* < private >
560 : : * g_variant_alloc:
561 : : * @type: the type of the new instance
562 : : * @serialised: if the instance will be in serialised form
563 : : * @trusted: if the instance will be trusted
564 : : * @suffix_size: amount of extra bytes to add to allocation
565 : : *
566 : : * Allocates a #GVariant instance and does some common work (such as
567 : : * looking up and filling in the type info), setting the state field,
568 : : * and setting the ref_count to 1.
569 : : *
570 : : * Use @suffix_size when you want to store data inside of the GVariant
571 : : * without having to add an additional GBytes allocation.
572 : : *
573 : : * Returns: a new #GVariant with a floating reference
574 : : */
575 : : static GVariant *
576 : 5008597 : g_variant_alloc (const GVariantType *type,
577 : : gboolean serialised,
578 : : gboolean trusted,
579 : : gsize suffix_size)
580 : : {
581 : : G_GNUC_UNUSED gboolean size_check;
582 : : GVariant *value;
583 : : gsize size;
584 : :
585 : 5008597 : size_check = g_size_checked_add (&size, sizeof *value, suffix_size);
586 : 5008597 : g_assert (size_check);
587 : :
588 : 5008597 : value = g_malloc (size);
589 : 5008597 : value->type_info = g_variant_type_info_get (type);
590 : 7001547 : value->state = (serialised ? STATE_SERIALISED : 0) |
591 : 6005072 : (trusted ? STATE_TRUSTED : 0) |
592 : : STATE_FLOATING;
593 : 5008597 : value->size = (gssize) -1;
594 : 5008597 : g_atomic_ref_count_init (&value->ref_count);
595 : 5008597 : value->depth = 0;
596 : :
597 : 5008597 : return value;
598 : : }
599 : :
600 : : /**
601 : : * g_variant_new_from_bytes:
602 : : * @type: a #GVariantType
603 : : * @bytes: a #GBytes
604 : : * @trusted: if the contents of @bytes are trusted
605 : : *
606 : : * Constructs a new serialized-mode #GVariant instance. This is the
607 : : * inner interface for creation of new serialized values that gets
608 : : * called from various functions in gvariant.c.
609 : : *
610 : : * A reference is taken on @bytes.
611 : : *
612 : : * The data in @bytes must be aligned appropriately for the @type being loaded.
613 : : * Otherwise this function will internally create a copy of the memory (since
614 : : * GLib 2.60) or (in older versions) fail and exit the process.
615 : : *
616 : : * Returns: (transfer none): a new #GVariant with a floating reference
617 : : *
618 : : * Since: 2.36
619 : : */
620 : : GVariant *
621 : 7341 : g_variant_new_from_bytes (const GVariantType *type,
622 : : GBytes *bytes,
623 : : gboolean trusted)
624 : : {
625 : 7341 : return g_variant_new_take_bytes (type, g_bytes_ref (bytes), trusted);
626 : : }
627 : :
628 : : /* -- internal -- */
629 : :
630 : : /* < internal >
631 : : * g_variant_new_preallocated_trusted:
632 : : * @data: data to copy
633 : : * @size: the size of data
634 : : *
635 : : * Creates a new #GVariant for simple types such as int32, double, or
636 : : * bytes.
637 : : *
638 : : * Instead of allocating a GBytes, the data will be stored at the tail of
639 : : * the GVariant structures allocation. This can save considerable malloc
640 : : * overhead.
641 : : *
642 : : * The data is always aligned to the maximum alignment GVariant provides
643 : : * which is 8 bytes and therefore does not need to verify alignment based
644 : : * on the the @type provided.
645 : : *
646 : : * This should only be used for creating GVariant with trusted data.
647 : : *
648 : : * Returns: a new #GVariant with a floating reference
649 : : */
650 : : GVariant *
651 : 4264097 : g_variant_new_preallocated_trusted (const GVariantType *type,
652 : : gconstpointer data,
653 : : gsize size)
654 : : {
655 : : GVariant *value;
656 : : gsize expected_size;
657 : : guint alignment;
658 : :
659 : 4264097 : value = g_variant_alloc (type, TRUE, TRUE, size);
660 : :
661 : 4264097 : g_variant_type_info_query (value->type_info, &alignment, &expected_size);
662 : :
663 : 4264097 : g_assert (expected_size == 0 || size == expected_size);
664 : :
665 : 4264097 : value->contents.serialised.ordered_offsets_up_to = G_MAXSIZE;
666 : 4264097 : value->contents.serialised.checked_offsets_up_to = G_MAXSIZE;
667 : 4264097 : value->contents.serialised.bytes = NULL;
668 : 4264097 : value->contents.serialised.data = value->suffix;
669 : 4264097 : value->size = size;
670 : :
671 : 4264097 : memcpy (value->suffix, data, size);
672 : :
673 : 3343853 : TRACE(GLIB_VARIANT_FROM_BUFFER(value, value->type_info, value->ref_count, value->state));
674 : :
675 : 4264097 : return value;
676 : : }
677 : :
678 : : /* < internal >
679 : : * g_variant_new_take_bytes:
680 : : * @bytes: (transfer full): a #GBytes
681 : : * @trusted: if the contents of @bytes are trusted
682 : : *
683 : : * The same as g_variant_new_from_bytes() but takes ownership
684 : : * of @bytes.
685 : : *
686 : : * Returns: a new #GVariant with a floating reference
687 : : */
688 : : GVariant *
689 : 63082 : g_variant_new_take_bytes (const GVariantType *type,
690 : : GBytes *bytes,
691 : : gboolean trusted)
692 : : {
693 : : GVariant *value;
694 : : guint alignment;
695 : : gsize size;
696 : 63082 : GBytes *owned_bytes = NULL;
697 : : GVariantSerialised serialised;
698 : :
699 : 63082 : value = g_variant_alloc (type, TRUE, trusted, 0);
700 : :
701 : 63082 : g_variant_type_info_query (value->type_info,
702 : : &alignment, &size);
703 : :
704 : : /* Ensure the alignment is correct. This is a huge performance hit if it’s
705 : : * not correct, but that’s better than aborting if a caller provides data
706 : : * with the wrong alignment (which is likely to happen very occasionally, and
707 : : * only cause an abort on some architectures — so is unlikely to be caught
708 : : * in testing). Callers can always actively ensure they use the correct
709 : : * alignment to avoid the performance hit. */
710 : 63082 : serialised.type_info = value->type_info;
711 : 63082 : serialised.data = (guchar *) g_bytes_get_data (bytes, &serialised.size);
712 : 63082 : serialised.depth = 0;
713 : 63082 : serialised.ordered_offsets_up_to = trusted ? G_MAXSIZE : 0;
714 : 63082 : serialised.checked_offsets_up_to = trusted ? G_MAXSIZE : 0;
715 : :
716 : 63082 : if (!g_variant_serialised_check (serialised))
717 : : {
718 : : #ifdef HAVE_POSIX_MEMALIGN
719 : 84 : gpointer aligned_data = NULL;
720 : 84 : gsize aligned_size = g_bytes_get_size (bytes);
721 : :
722 : : /* posix_memalign() requires the alignment to be a multiple of
723 : : * sizeof(void*), and a power of 2. See g_variant_type_info_query() for
724 : : * details on the alignment format.
725 : : *
726 : : * While calling posix_memalign() with aligned_size==0 is safe on glibc,
727 : : * POSIX specifies that the behaviour is implementation-defined, so avoid
728 : : * that and leave aligned_data==NULL in that case.
729 : : * See https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html */
730 : 84 : if (aligned_size != 0 &&
731 : 60 : posix_memalign (&aligned_data, MAX (sizeof (void *), alignment + 1),
732 : : aligned_size) != 0)
733 : 0 : g_error ("posix_memalign failed");
734 : :
735 : 84 : if (aligned_size != 0)
736 : 60 : memcpy (aligned_data, g_bytes_get_data (bytes, NULL), aligned_size);
737 : :
738 : 84 : owned_bytes = bytes;
739 : 84 : bytes = g_bytes_new_with_free_func (aligned_data,
740 : : aligned_size,
741 : : free, aligned_data);
742 : 84 : aligned_data = NULL;
743 : : #else
744 : : /* NOTE: there may be platforms that lack posix_memalign() and also
745 : : * have malloc() that returns non-8-aligned. if so, we need to try
746 : : * harder here.
747 : : */
748 : 68 : owned_bytes = bytes;
749 : 136 : bytes = g_bytes_new (g_bytes_get_data (bytes, NULL),
750 : 68 : g_bytes_get_size (bytes));
751 : : #endif
752 : 68 : }
753 : :
754 : 63082 : value->contents.serialised.bytes = bytes;
755 : :
756 : 63082 : if (size && g_bytes_get_size (bytes) != size)
757 : : {
758 : : /* Creating a fixed-sized GVariant with a bytes of the wrong
759 : : * size.
760 : : *
761 : : * We should do the equivalent of pulling a fixed-sized child out
762 : : * of a brozen container (ie: data is NULL size is equal to the correct
763 : : * fixed size).
764 : : */
765 : 128 : value->contents.serialised.data = NULL;
766 : 128 : value->size = size;
767 : 56 : }
768 : : else
769 : : {
770 : 62954 : value->contents.serialised.data = g_bytes_get_data (bytes, &value->size);
771 : : }
772 : :
773 : 63082 : value->contents.serialised.ordered_offsets_up_to = trusted ? G_MAXSIZE : 0;
774 : 63082 : value->contents.serialised.checked_offsets_up_to = trusted ? G_MAXSIZE : 0;
775 : :
776 : 63082 : g_clear_pointer (&owned_bytes, g_bytes_unref);
777 : :
778 : 24673 : TRACE(GLIB_VARIANT_FROM_BUFFER(value, value->type_info, value->ref_count, value->state));
779 : :
780 : 63082 : return value;
781 : : }
782 : :
783 : : /* < internal >
784 : : * g_variant_new_from_children:
785 : : * @type: a #GVariantType
786 : : * @children: an array of #GVariant pointers. Consumed.
787 : : * @n_children: the length of @children
788 : : * @trusted: %TRUE if every child in @children is trusted
789 : : *
790 : : * Constructs a new tree-mode #GVariant instance. This is the inner
791 : : * interface for creation of new serialized values that gets called from
792 : : * various functions in gvariant.c.
793 : : *
794 : : * @children is consumed by this function. g_free() will be called on
795 : : * it some time later.
796 : : *
797 : : * Returns: a new #GVariant with a floating reference
798 : : */
799 : : GVariant *
800 : 681418 : g_variant_new_from_children (const GVariantType *type,
801 : : GVariant **children,
802 : : gsize n_children,
803 : : gboolean trusted)
804 : : {
805 : : GVariant *value;
806 : :
807 : 681418 : value = g_variant_alloc (type, FALSE, trusted, 0);
808 : 681418 : value->contents.tree.children = children;
809 : 681418 : value->contents.tree.n_children = n_children;
810 : 643596 : TRACE(GLIB_VARIANT_FROM_CHILDREN(value, value->type_info, value->ref_count, value->state));
811 : :
812 : 681418 : return value;
813 : : }
814 : :
815 : : /* < internal >
816 : : * g_variant_get_type_info:
817 : : * @value: a #GVariant
818 : : *
819 : : * Returns the #GVariantTypeInfo corresponding to the type of @value. A
820 : : * reference is not added, so the return value is only good for the
821 : : * duration of the life of @value.
822 : : *
823 : : * Returns: the #GVariantTypeInfo for @value
824 : : */
825 : : GVariantTypeInfo *
826 : 74178125 : g_variant_get_type_info (GVariant *value)
827 : : {
828 : 74178125 : return value->type_info;
829 : : }
830 : :
831 : : /* < internal >
832 : : * g_variant_is_trusted:
833 : : * @value: a #GVariant
834 : : *
835 : : * Determines if @value is trusted by #GVariant to contain only
836 : : * fully-valid data. All values constructed solely via #GVariant APIs
837 : : * are trusted, but values containing data read in from other sources
838 : : * are usually not trusted.
839 : : *
840 : : * The main advantage of trusted data is that certain checks can be
841 : : * skipped. For example, we don't need to check that a string is
842 : : * properly nul-terminated or that an object path is actually a
843 : : * properly-formatted object path.
844 : : *
845 : : * Returns: if @value is trusted
846 : : */
847 : : gboolean
848 : 11436987 : g_variant_is_trusted (GVariant *value)
849 : : {
850 : 11436987 : return (value->state & STATE_TRUSTED) != 0;
851 : : }
852 : :
853 : : /* < internal >
854 : : * g_variant_get_depth:
855 : : * @value: a #GVariant
856 : : *
857 : : * Gets the nesting depth of a #GVariant. This is 0 for a #GVariant with no
858 : : * children.
859 : : *
860 : : * Returns: nesting depth of @value
861 : : */
862 : : gsize
863 : 309 : g_variant_get_depth (GVariant *value)
864 : : {
865 : 309 : return value->depth;
866 : : }
867 : :
868 : : /* -- public -- */
869 : :
870 : : /**
871 : : * g_variant_unref:
872 : : * @value: a #GVariant
873 : : *
874 : : * Decreases the reference count of @value. When its reference count
875 : : * drops to 0, the memory used by the variant is freed.
876 : : *
877 : : * Since: 2.24
878 : : **/
879 : : void
880 : 21538320 : g_variant_unref (GVariant *value)
881 : : {
882 : 21538320 : g_return_if_fail (value != NULL);
883 : :
884 : 15340160 : TRACE(GLIB_VARIANT_UNREF(value, value->type_info, value->ref_count, value->state));
885 : :
886 : 21538320 : if (g_atomic_ref_count_dec (&value->ref_count))
887 : : {
888 : 12785857 : if G_UNLIKELY (value->state & STATE_LOCKED)
889 : 0 : g_critical ("attempting to free a locked GVariant instance. "
890 : : "This should never happen.");
891 : :
892 : 12785857 : value->state |= STATE_LOCKED;
893 : :
894 : 12785857 : g_variant_type_info_unref (value->type_info);
895 : :
896 : 12785857 : if (value->state & STATE_SERIALISED)
897 : 12115347 : g_bytes_unref (value->contents.serialised.bytes);
898 : : else
899 : 670510 : g_variant_release_children (value);
900 : :
901 : 12785857 : memset (value, 0, sizeof (GVariant));
902 : 12785857 : g_free (value);
903 : 4474641 : }
904 : 6198160 : }
905 : :
906 : : /**
907 : : * g_variant_ref:
908 : : * @value: a #GVariant
909 : : *
910 : : * Increases the reference count of @value.
911 : : *
912 : : * Returns: the same @value
913 : : *
914 : : * Since: 2.24
915 : : **/
916 : : GVariant *
917 : 6964442 : g_variant_ref (GVariant *value)
918 : : {
919 : 6964442 : g_return_val_if_fail (value != NULL, NULL);
920 : :
921 : 5926172 : TRACE(GLIB_VARIANT_REF(value, value->type_info, value->ref_count, value->state));
922 : :
923 : 6964442 : g_atomic_ref_count_inc (&value->ref_count);
924 : :
925 : 6964442 : return value;
926 : 1038270 : }
927 : :
928 : : /**
929 : : * g_variant_ref_sink:
930 : : * @value: a #GVariant
931 : : *
932 : : * #GVariant uses a floating reference count system. All functions with
933 : : * names starting with `g_variant_new_` return floating
934 : : * references.
935 : : *
936 : : * Calling g_variant_ref_sink() on a #GVariant with a floating reference
937 : : * will convert the floating reference into a full reference. Calling
938 : : * g_variant_ref_sink() on a non-floating #GVariant results in an
939 : : * additional normal reference being added.
940 : : *
941 : : * In other words, if the @value is floating, then this call "assumes
942 : : * ownership" of the floating reference, converting it to a normal
943 : : * reference. If the @value is not floating, then this call adds a
944 : : * new normal reference increasing the reference count by one.
945 : : *
946 : : * All calls that result in a #GVariant instance being inserted into a
947 : : * container will call g_variant_ref_sink() on the instance. This means
948 : : * that if the value was just created (and has only its floating
949 : : * reference) then the container will assume sole ownership of the value
950 : : * at that point and the caller will not need to unreference it. This
951 : : * makes certain common styles of programming much easier while still
952 : : * maintaining normal refcounting semantics in situations where values
953 : : * are not floating.
954 : : *
955 : : * Returns: the same @value
956 : : *
957 : : * Since: 2.24
958 : : **/
959 : : GVariant *
960 : 6039083 : g_variant_ref_sink (GVariant *value)
961 : : {
962 : : int old_state;
963 : :
964 : 6039083 : g_return_val_if_fail (value != NULL, NULL);
965 : 6039083 : g_return_val_if_fail (!g_atomic_ref_count_compare (&value->ref_count, 0), NULL);
966 : :
967 : 4366062 : TRACE(GLIB_VARIANT_REF_SINK(value, value->type_info, value->ref_count, value->state, value->state & STATE_FLOATING));
968 : :
969 : 6039083 : old_state = value->state;
970 : :
971 : 6039083 : while (old_state & STATE_FLOATING)
972 : : {
973 : 4251038 : int new_state = old_state & ~STATE_FLOATING;
974 : :
975 : 4251038 : if (g_atomic_int_compare_and_exchange_full (&value->state, old_state, new_state, &old_state))
976 : 4251038 : return value;
977 : : }
978 : :
979 : 1788045 : g_atomic_ref_count_inc (&value->ref_count);
980 : :
981 : 1788045 : return value;
982 : 1673021 : }
983 : :
984 : : /**
985 : : * g_variant_take_ref:
986 : : * @value: a #GVariant
987 : : *
988 : : * If @value is floating, sink it. Otherwise, do nothing.
989 : : *
990 : : * Typically you want to use g_variant_ref_sink() in order to
991 : : * automatically do the correct thing with respect to floating or
992 : : * non-floating references, but there is one specific scenario where
993 : : * this function is helpful.
994 : : *
995 : : * The situation where this function is helpful is when creating an API
996 : : * that allows the user to provide a callback function that returns a
997 : : * #GVariant. We certainly want to allow the user the flexibility to
998 : : * return a non-floating reference from this callback (for the case
999 : : * where the value that is being returned already exists).
1000 : : *
1001 : : * At the same time, the style of the #GVariant API makes it likely that
1002 : : * for newly-created #GVariant instances, the user can be saved some
1003 : : * typing if they are allowed to return a #GVariant with a floating
1004 : : * reference.
1005 : : *
1006 : : * Using this function on the return value of the user's callback allows
1007 : : * the user to do whichever is more convenient for them. The caller
1008 : : * will always receives exactly one full reference to the value: either
1009 : : * the one that was returned in the first place, or a floating reference
1010 : : * that has been converted to a full reference.
1011 : : *
1012 : : * This function has an odd interaction when combined with
1013 : : * g_variant_ref_sink() running at the same time in another thread on
1014 : : * the same #GVariant instance. If g_variant_ref_sink() runs first then
1015 : : * the result will be that the floating reference is converted to a hard
1016 : : * reference. If g_variant_take_ref() runs first then the result will
1017 : : * be that the floating reference is converted to a hard reference and
1018 : : * an additional reference on top of that one is added. It is best to
1019 : : * avoid this situation.
1020 : : *
1021 : : * Returns: the same @value
1022 : : **/
1023 : : GVariant *
1024 : 730431 : g_variant_take_ref (GVariant *value)
1025 : : {
1026 : 730431 : g_return_val_if_fail (value != NULL, NULL);
1027 : 730431 : g_return_val_if_fail (!g_atomic_ref_count_compare (&value->ref_count, 0), NULL);
1028 : :
1029 : 729582 : TRACE(GLIB_VARIANT_TAKE_REF(value, value->type_info, value->ref_count, value->state, value->state & STATE_FLOATING));
1030 : 730431 : g_atomic_int_and (&value->state, ~STATE_FLOATING);
1031 : :
1032 : 730431 : return value;
1033 : 849 : }
1034 : :
1035 : : /**
1036 : : * g_variant_is_floating:
1037 : : * @value: a #GVariant
1038 : : *
1039 : : * Checks whether @value has a floating reference count.
1040 : : *
1041 : : * This function should only ever be used to assert that a given variant
1042 : : * is or is not floating, or for debug purposes. To acquire a reference
1043 : : * to a variant that might be floating, always use g_variant_ref_sink()
1044 : : * or g_variant_take_ref().
1045 : : *
1046 : : * See g_variant_ref_sink() for more information about floating reference
1047 : : * counts.
1048 : : *
1049 : : * Returns: whether @value is floating
1050 : : *
1051 : : * Since: 2.26
1052 : : **/
1053 : : gboolean
1054 : 1235 : g_variant_is_floating (GVariant *value)
1055 : : {
1056 : 1235 : g_return_val_if_fail (value != NULL, FALSE);
1057 : :
1058 : 1235 : return (value->state & STATE_FLOATING) != 0;
1059 : 20 : }
1060 : :
1061 : : /**
1062 : : * g_variant_get_size:
1063 : : * @value: a #GVariant instance
1064 : : *
1065 : : * Determines the number of bytes that would be required to store @value
1066 : : * with g_variant_store().
1067 : : *
1068 : : * If @value has a fixed-sized type then this function always returned
1069 : : * that fixed size.
1070 : : *
1071 : : * In the case that @value is already in serialized form or the size has
1072 : : * already been calculated (ie: this function has been called before)
1073 : : * then this function is O(1). Otherwise, the size is calculated, an
1074 : : * operation which is approximately O(n) in the number of values
1075 : : * involved.
1076 : : *
1077 : : * Returns: the serialized size of @value
1078 : : *
1079 : : * Since: 2.24
1080 : : **/
1081 : : gsize
1082 : 6385132 : g_variant_get_size (GVariant *value)
1083 : : {
1084 : 6385132 : if (g_variant_is_serialised (value))
1085 : 6384103 : return value->size;
1086 : :
1087 : 1029 : g_variant_lock (value);
1088 : 1029 : g_variant_ensure_size (value);
1089 : 1029 : g_variant_unlock (value);
1090 : :
1091 : 1029 : return value->size;
1092 : 1760359 : }
1093 : :
1094 : : /**
1095 : : * g_variant_get_data:
1096 : : * @value: a #GVariant instance
1097 : : *
1098 : : * Returns a pointer to the serialized form of a #GVariant instance.
1099 : : * The returned data may not be in fully-normalised form if read from an
1100 : : * untrusted source. The returned data must not be freed; it remains
1101 : : * valid for as long as @value exists.
1102 : : *
1103 : : * If @value is a fixed-sized value that was deserialized from a
1104 : : * corrupted serialized container then %NULL may be returned. In this
1105 : : * case, the proper thing to do is typically to use the appropriate
1106 : : * number of nul bytes in place of @value. If @value is not fixed-sized
1107 : : * then %NULL is never returned.
1108 : : *
1109 : : * In the case that @value is already in serialized form, this function
1110 : : * is O(1). If the value is not already in serialized form,
1111 : : * serialization occurs implicitly and is approximately O(n) in the size
1112 : : * of the result.
1113 : : *
1114 : : * To deserialize the data returned by this function, in addition to the
1115 : : * serialized data, you must know the type of the #GVariant, and (if the
1116 : : * machine might be different) the endianness of the machine that stored
1117 : : * it. As a result, file formats or network messages that incorporate
1118 : : * serialized #GVariants must include this information either
1119 : : * implicitly (for instance "the file always contains a
1120 : : * %G_VARIANT_TYPE_VARIANT and it is always in little-endian order") or
1121 : : * explicitly (by storing the type and/or endianness in addition to the
1122 : : * serialized data).
1123 : : *
1124 : : * Returns: (transfer none): the serialized form of @value, or %NULL
1125 : : *
1126 : : * Since: 2.24
1127 : : **/
1128 : : gconstpointer
1129 : 14426692 : g_variant_get_data (GVariant *value)
1130 : : {
1131 : 14426692 : if (g_variant_is_serialised (value))
1132 : 14415855 : return value->contents.serialised.data;
1133 : :
1134 : 10837 : g_variant_lock (value);
1135 : 10837 : g_variant_ensure_serialised (value);
1136 : 10837 : g_variant_unlock (value);
1137 : :
1138 : 10837 : return value->contents.serialised.data;
1139 : 4644794 : }
1140 : :
1141 : : /**
1142 : : * g_variant_get_data_as_bytes:
1143 : : * @value: a #GVariant
1144 : : *
1145 : : * Returns a pointer to the serialized form of a #GVariant instance.
1146 : : * The semantics of this function are exactly the same as
1147 : : * g_variant_get_data(), except that the returned #GBytes holds
1148 : : * a reference to the variant data.
1149 : : *
1150 : : * Returns: (transfer full): A new #GBytes representing the variant data
1151 : : *
1152 : : * Since: 2.36
1153 : : */
1154 : : GBytes *
1155 : 72 : g_variant_get_data_as_bytes (GVariant *value)
1156 : : {
1157 : : const gchar *bytes_data;
1158 : : const gchar *data;
1159 : 72 : gsize bytes_size = 0;
1160 : : gsize size;
1161 : :
1162 : 72 : if (!g_variant_is_serialised (value))
1163 : : {
1164 : 12 : g_variant_lock (value);
1165 : 12 : g_variant_ensure_serialised (value);
1166 : 12 : g_variant_unlock (value);
1167 : 6 : }
1168 : :
1169 : 72 : if (value->contents.serialised.bytes != NULL)
1170 : 24 : bytes_data = g_bytes_get_data (value->contents.serialised.bytes, &bytes_size);
1171 : : else
1172 : 48 : bytes_data = NULL;
1173 : :
1174 : 72 : data = value->contents.serialised.data;
1175 : 72 : size = value->size;
1176 : :
1177 : 72 : if (data == NULL)
1178 : : {
1179 : 4 : g_assert (size == 0);
1180 : 4 : data = bytes_data;
1181 : 2 : }
1182 : :
1183 : 72 : if (bytes_data != NULL && data == bytes_data && size == bytes_size)
1184 : 16 : return g_bytes_ref (value->contents.serialised.bytes);
1185 : 56 : else if (bytes_data != NULL)
1186 : 9 : return g_bytes_new_from_bytes (value->contents.serialised.bytes,
1187 : 6 : data - bytes_data, size);
1188 : : else
1189 : 50 : return g_bytes_new (value->contents.serialised.data, size);
1190 : 36 : }
1191 : :
1192 : :
1193 : : /**
1194 : : * g_variant_n_children:
1195 : : * @value: a container #GVariant
1196 : : *
1197 : : * Determines the number of children in a container #GVariant instance.
1198 : : * This includes variants, maybes, arrays, tuples and dictionary
1199 : : * entries. It is an error to call this function on any other type of
1200 : : * #GVariant.
1201 : : *
1202 : : * For variants, the return value is always 1. For values with maybe
1203 : : * types, it is always zero or one. For arrays, it is the length of the
1204 : : * array. For tuples it is the number of tuple items (which depends
1205 : : * only on the type). For dictionary entries, it is always 2
1206 : : *
1207 : : * This function is O(1).
1208 : : *
1209 : : * Returns: the number of children in the container
1210 : : *
1211 : : * Since: 2.24
1212 : : **/
1213 : : gsize
1214 : 5870691 : g_variant_n_children (GVariant *value)
1215 : : {
1216 : : gsize n_children;
1217 : :
1218 : 5870691 : if (g_variant_is_serialised (value))
1219 : 1065909 : return g_variant_serialised_n_children (g_variant_to_serialised (value));
1220 : :
1221 : 4804782 : g_variant_lock (value);
1222 : :
1223 : 4804782 : if (value->state & STATE_SERIALISED)
1224 : : /* Another thread may have serialized @value after the fast-path check. */
1225 : 0 : n_children = g_variant_serialised_n_children (g_variant_to_serialised (value));
1226 : : else
1227 : 4804782 : n_children = value->contents.tree.n_children;
1228 : :
1229 : 4804782 : g_variant_unlock (value);
1230 : :
1231 : 4804782 : return n_children;
1232 : 1872145 : }
1233 : :
1234 : : /**
1235 : : * g_variant_get_child_value:
1236 : : * @value: a container #GVariant
1237 : : * @index_: the index of the child to fetch
1238 : : *
1239 : : * Reads a child item out of a container #GVariant instance. This
1240 : : * includes variants, maybes, arrays, tuples and dictionary
1241 : : * entries. It is an error to call this function on any other type of
1242 : : * #GVariant.
1243 : : *
1244 : : * It is an error if @index_ is greater than the number of child items
1245 : : * in the container. See g_variant_n_children().
1246 : : *
1247 : : * The returned value is never floating. You should free it with
1248 : : * g_variant_unref() when you're done with it.
1249 : : *
1250 : : * Note that values borrowed from the returned child are not guaranteed to
1251 : : * still be valid after the child is freed even if you still hold a reference
1252 : : * to @value, if @value has not been serialized at the time this function is
1253 : : * called. To avoid this, you can serialize @value by calling
1254 : : * g_variant_get_data() and optionally ignoring the return value.
1255 : : *
1256 : : * There may be implementation specific restrictions on deeply nested values,
1257 : : * which would result in the unit tuple being returned as the child value,
1258 : : * instead of further nested children. #GVariant is guaranteed to handle
1259 : : * nesting up to at least 64 levels.
1260 : : *
1261 : : * This function is O(1).
1262 : : *
1263 : : * Returns: (transfer full): the child at the specified index
1264 : : *
1265 : : * Since: 2.24
1266 : : **/
1267 : : GVariant *
1268 : 12269921 : g_variant_get_child_value (GVariant *value,
1269 : : gsize index_)
1270 : : {
1271 : 12269921 : g_return_val_if_fail (value->depth < G_MAXSIZE, NULL);
1272 : :
1273 : 12269921 : if (~g_atomic_int_get (&value->state) & STATE_SERIALISED)
1274 : : {
1275 : : /* g_variant_serialised_get_child() does its own checks on index_ */
1276 : 4491557 : g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
1277 : :
1278 : 4491557 : g_variant_lock (value);
1279 : :
1280 : 4491557 : if (~value->state & STATE_SERIALISED)
1281 : : {
1282 : : GVariant *child;
1283 : :
1284 : 4491557 : child = g_variant_ref (value->contents.tree.children[index_]);
1285 : 4491557 : g_variant_unlock (value);
1286 : :
1287 : 4491557 : return child;
1288 : : }
1289 : :
1290 : 0 : g_variant_unlock (value);
1291 : 0 : }
1292 : :
1293 : : {
1294 : 7778364 : GVariantSerialised serialised = g_variant_to_serialised (value);
1295 : : GVariantSerialised s_child;
1296 : : GVariant *child;
1297 : :
1298 : : /* get the serializer to extract the serialized data for the child
1299 : : * from the serialized data for the container
1300 : : */
1301 : 7778364 : s_child = g_variant_serialised_get_child (serialised, index_);
1302 : :
1303 : : /* Update the cached ordered_offsets_up_to, since @serialised will be thrown away when this function exits */
1304 : 7778364 : value->contents.serialised.ordered_offsets_up_to = MAX (value->contents.serialised.ordered_offsets_up_to, serialised.ordered_offsets_up_to);
1305 : 7778364 : value->contents.serialised.checked_offsets_up_to = MAX (value->contents.serialised.checked_offsets_up_to, serialised.checked_offsets_up_to);
1306 : :
1307 : : /* Check whether this would cause nesting too deep. If so, return a fake
1308 : : * child. The only situation we expect this to happen in is with a variant,
1309 : : * as all other deeply-nested types have a static type, and hence should
1310 : : * have been rejected earlier. In the case of a variant whose nesting plus
1311 : : * the depth of its child is too great, return a unit variant () instead of
1312 : : * the real child. */
1313 : 7778364 : if (!(value->state & STATE_TRUSTED) &&
1314 : 3504707 : g_variant_type_info_query_depth (s_child.type_info) >=
1315 : 2129542 : G_VARIANT_MAX_RECURSION_DEPTH - value->depth)
1316 : : {
1317 : 0 : g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT));
1318 : 0 : g_variant_type_info_unref (s_child.type_info);
1319 : 0 : return g_variant_new_tuple (NULL, 0);
1320 : : }
1321 : :
1322 : : /* create a new serialized instance out of it */
1323 : 7778364 : child = g_new (GVariant, 1);
1324 : 7778364 : child->type_info = s_child.type_info;
1325 : 7778364 : child->state = (value->state & STATE_TRUSTED) |
1326 : : STATE_SERIALISED;
1327 : 7778364 : child->size = s_child.size;
1328 : 7778364 : g_atomic_ref_count_init (&child->ref_count);
1329 : 7778364 : child->depth = value->depth + 1;
1330 : 7778364 : if (value->contents.serialised.bytes != NULL)
1331 : 7778356 : child->contents.serialised.bytes =
1332 : 7778356 : g_bytes_ref (value->contents.serialised.bytes);
1333 : : else
1334 : 8 : child->contents.serialised.bytes = NULL;
1335 : 7778364 : child->contents.serialised.data = s_child.data;
1336 : 7778364 : child->contents.serialised.ordered_offsets_up_to = (value->state & STATE_TRUSTED) ? G_MAXSIZE : s_child.ordered_offsets_up_to;
1337 : 7778364 : child->contents.serialised.checked_offsets_up_to = (value->state & STATE_TRUSTED) ? G_MAXSIZE : s_child.checked_offsets_up_to;
1338 : :
1339 : 4300186 : TRACE(GLIB_VARIANT_FROM_PARENT(child, child->type_info, child->ref_count, child->state, value));
1340 : :
1341 : 7778364 : return child;
1342 : : }
1343 : 4491538 : }
1344 : :
1345 : : /**
1346 : : * g_variant_maybe_get_child_value:
1347 : : * @value: a container #GVariant
1348 : : * @index_: the index of the child to fetch
1349 : : *
1350 : : * Reads a child item out of a container #GVariant instance, if it is in normal
1351 : : * form. If it is not in normal form, return %NULL.
1352 : : *
1353 : : * This function behaves the same as g_variant_get_child_value(), except that it
1354 : : * returns %NULL if the child is not in normal form. g_variant_get_child_value()
1355 : : * would instead return a new default value of the correct type.
1356 : : *
1357 : : * This is intended to be used internally to avoid unnecessary #GVariant
1358 : : * allocations.
1359 : : *
1360 : : * The returned value is never floating. You should free it with
1361 : : * g_variant_unref() when you're done with it.
1362 : : *
1363 : : * This function is O(1).
1364 : : *
1365 : : * Returns: (transfer full): the child at the specified index
1366 : : *
1367 : : * Since: 2.74
1368 : : */
1369 : : GVariant *
1370 : 872434 : g_variant_maybe_get_child_value (GVariant *value,
1371 : : gsize index_)
1372 : : {
1373 : 872434 : g_return_val_if_fail (value->depth < G_MAXSIZE, NULL);
1374 : :
1375 : 872434 : if (~g_atomic_int_get (&value->state) & STATE_SERIALISED)
1376 : : {
1377 : : /* g_variant_serialised_get_child() does its own checks on index_ */
1378 : 0 : g_return_val_if_fail (index_ < g_variant_n_children (value), NULL);
1379 : :
1380 : 0 : g_variant_lock (value);
1381 : :
1382 : 0 : if (~value->state & STATE_SERIALISED)
1383 : : {
1384 : : GVariant *child;
1385 : :
1386 : 0 : child = g_variant_ref (value->contents.tree.children[index_]);
1387 : 0 : g_variant_unlock (value);
1388 : :
1389 : 0 : return child;
1390 : : }
1391 : :
1392 : 0 : g_variant_unlock (value);
1393 : 0 : }
1394 : :
1395 : : {
1396 : 872434 : GVariantSerialised serialised = g_variant_to_serialised (value);
1397 : : GVariantSerialised s_child;
1398 : :
1399 : : /* get the serializer to extract the serialized data for the child
1400 : : * from the serialized data for the container
1401 : : */
1402 : 872434 : s_child = g_variant_serialised_get_child (serialised, index_);
1403 : :
1404 : 872434 : if (!(value->state & STATE_TRUSTED) && s_child.data == NULL)
1405 : : {
1406 : 872286 : g_variant_type_info_unref (s_child.type_info);
1407 : 872286 : return NULL;
1408 : : }
1409 : :
1410 : 148 : g_variant_type_info_unref (s_child.type_info);
1411 : 148 : return g_variant_get_child_value (value, index_);
1412 : : }
1413 : 684254 : }
1414 : :
1415 : : /**
1416 : : * g_variant_store:
1417 : : * @value: the #GVariant to store
1418 : : * @data: (not nullable): the location to store the serialized data at
1419 : : *
1420 : : * Stores the serialized form of @value at @data. @data should be
1421 : : * large enough. See g_variant_get_size().
1422 : : *
1423 : : * The stored data is in machine native byte order but may not be in
1424 : : * fully-normalised form if read from an untrusted source. See
1425 : : * g_variant_get_normal_form() for a solution.
1426 : : *
1427 : : * As with g_variant_get_data(), to be able to deserialize the
1428 : : * serialized variant successfully, its type and (if the destination
1429 : : * machine might be different) its endianness must also be available.
1430 : : *
1431 : : * This function is approximately O(n) in the size of @data.
1432 : : *
1433 : : * Since: 2.24
1434 : : **/
1435 : : void
1436 : 2128467 : g_variant_store (GVariant *value,
1437 : : gpointer data)
1438 : : {
1439 : 2128467 : g_return_if_fail (data != NULL);
1440 : :
1441 : 2128467 : if (g_variant_is_serialised (value))
1442 : : {
1443 : 2069855 : if (value->contents.serialised.data != NULL)
1444 : 2069821 : memcpy (data, value->contents.serialised.data, value->size);
1445 : : else
1446 : 34 : memset (data, 0, value->size);
1447 : 2069855 : return;
1448 : : }
1449 : :
1450 : 58612 : g_variant_lock (value);
1451 : :
1452 : 58612 : if (value->state & STATE_SERIALISED)
1453 : : {
1454 : : /* Another thread may have serialized @value after the fast-path check. */
1455 : 0 : if (value->contents.serialised.data != NULL)
1456 : 0 : memcpy (data, value->contents.serialised.data, value->size);
1457 : : else
1458 : 0 : memset (data, 0, value->size);
1459 : 0 : }
1460 : : else
1461 : 58612 : g_variant_serialise (value, data);
1462 : :
1463 : 58612 : g_variant_unlock (value);
1464 : 864346 : }
1465 : :
1466 : : /**
1467 : : * g_variant_is_normal_form:
1468 : : * @value: a #GVariant instance
1469 : : *
1470 : : * Checks if @value is in normal form.
1471 : : *
1472 : : * The main reason to do this is to detect if a given chunk of
1473 : : * serialized data is in normal form: load the data into a #GVariant
1474 : : * using g_variant_new_from_data() and then use this function to
1475 : : * check.
1476 : : *
1477 : : * If @value is found to be in normal form then it will be marked as
1478 : : * being trusted. If the value was already marked as being trusted then
1479 : : * this function will immediately return %TRUE.
1480 : : *
1481 : : * There may be implementation specific restrictions on deeply nested values.
1482 : : * GVariant is guaranteed to handle nesting up to at least 64 levels.
1483 : : *
1484 : : * Returns: %TRUE if @value is in normal form
1485 : : *
1486 : : * Since: 2.24
1487 : : **/
1488 : : gboolean
1489 : 1127 : g_variant_is_normal_form (GVariant *value)
1490 : : {
1491 : 1127 : if (value->state & STATE_TRUSTED)
1492 : 118 : return TRUE;
1493 : :
1494 : 1009 : g_variant_lock (value);
1495 : :
1496 : 1009 : if (value->depth >= G_VARIANT_MAX_RECURSION_DEPTH)
1497 : 0 : return FALSE;
1498 : :
1499 : 1009 : if (value->state & STATE_SERIALISED)
1500 : : {
1501 : 1009 : if (g_variant_serialised_is_normal (g_variant_to_serialised (value)))
1502 : 511 : value->state |= STATE_TRUSTED;
1503 : 445 : }
1504 : : else
1505 : : {
1506 : 0 : gboolean normal = TRUE;
1507 : : gsize i;
1508 : :
1509 : 0 : for (i = 0; i < value->contents.tree.n_children; i++)
1510 : 0 : normal &= g_variant_is_normal_form (value->contents.tree.children[i]);
1511 : :
1512 : 0 : if (normal)
1513 : 0 : value->state |= STATE_TRUSTED;
1514 : : }
1515 : :
1516 : 1009 : g_variant_unlock (value);
1517 : :
1518 : 1009 : return (value->state & STATE_TRUSTED) != 0;
1519 : 467 : }
|