/src/glib-2.88.3/glib/gvariant-core.c
Line | Count | Source |
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 | 0 | #define STATE_LOCKED 1 |
261 | 0 | #define STATE_SERIALISED 2 |
262 | 0 | #define STATE_TRUSTED 4 |
263 | 0 | #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 | | g_variant_lock (GVariant *value) |
274 | 0 | { |
275 | 0 | g_bit_lock (&value->state, 0); |
276 | 0 | } |
277 | | |
278 | | /* < private > |
279 | | * g_variant_unlock: |
280 | | * @value: a #GVariant |
281 | | * |
282 | | * Unlocks @value after performing sensitive operations. |
283 | | */ |
284 | | static void |
285 | | g_variant_unlock (GVariant *value) |
286 | 0 | { |
287 | 0 | g_bit_unlock (&value->state, 0); |
288 | 0 | } |
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 | | g_variant_release_children (GVariant *value) |
304 | 0 | { |
305 | 0 | gsize i; |
306 | |
|
307 | 0 | g_assert (value->state & STATE_LOCKED); |
308 | 0 | g_assert (~value->state & STATE_SERIALISED); |
309 | | |
310 | 0 | for (i = 0; i < value->contents.tree.n_children; i++) |
311 | 0 | g_variant_unref (value->contents.tree.children[i]); |
312 | |
|
313 | 0 | g_free (value->contents.tree.children); |
314 | 0 | } |
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 | | g_variant_ensure_size (GVariant *value) |
382 | 0 | { |
383 | 0 | g_assert (value->state & STATE_LOCKED); |
384 | | |
385 | 0 | if (value->size == (gsize) -1) |
386 | 0 | { |
387 | 0 | gpointer *children; |
388 | 0 | gsize n_children; |
389 | |
|
390 | 0 | children = (gpointer *) value->contents.tree.children; |
391 | 0 | n_children = value->contents.tree.n_children; |
392 | 0 | value->size = g_variant_serialiser_needed_size (value->type_info, |
393 | 0 | g_variant_fill_gvs, |
394 | 0 | children, n_children); |
395 | 0 | } |
396 | 0 | } |
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 | | g_variant_to_serialised (GVariant *value) |
406 | 0 | { |
407 | 0 | g_assert (value->state & STATE_SERIALISED); |
408 | 0 | { |
409 | 0 | GVariantSerialised serialised = { |
410 | 0 | value->type_info, |
411 | 0 | (gpointer) value->contents.serialised.data, |
412 | 0 | value->size, |
413 | 0 | value->depth, |
414 | 0 | value->contents.serialised.ordered_offsets_up_to, |
415 | 0 | value->contents.serialised.checked_offsets_up_to, |
416 | 0 | }; |
417 | 0 | return serialised; |
418 | 0 | } |
419 | 0 | } |
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 | | g_variant_serialise (GVariant *value, |
434 | | gpointer data) |
435 | 0 | { |
436 | 0 | GVariantSerialised serialised = { 0, }; |
437 | 0 | gpointer *children; |
438 | 0 | gsize n_children; |
439 | |
|
440 | 0 | g_assert (~value->state & STATE_SERIALISED); |
441 | 0 | g_assert (value->state & STATE_LOCKED); |
442 | | |
443 | 0 | serialised.type_info = value->type_info; |
444 | 0 | serialised.size = value->size; |
445 | 0 | serialised.data = data; |
446 | 0 | serialised.depth = value->depth; |
447 | 0 | serialised.ordered_offsets_up_to = 0; |
448 | 0 | serialised.checked_offsets_up_to = 0; |
449 | |
|
450 | 0 | children = (gpointer *) value->contents.tree.children; |
451 | 0 | n_children = value->contents.tree.n_children; |
452 | |
|
453 | 0 | g_variant_serialiser_serialise (serialised, g_variant_fill_gvs, |
454 | 0 | children, n_children); |
455 | 0 | } |
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 | | g_variant_fill_gvs (GVariantSerialised *serialised, |
475 | | gpointer data) |
476 | 0 | { |
477 | 0 | GVariant *value = data; |
478 | |
|
479 | 0 | g_variant_lock (value); |
480 | 0 | g_variant_ensure_size (value); |
481 | 0 | g_variant_unlock (value); |
482 | |
|
483 | 0 | if (serialised->type_info == NULL) |
484 | 0 | serialised->type_info = value->type_info; |
485 | 0 | g_assert (serialised->type_info == value->type_info); |
486 | | |
487 | 0 | if (serialised->size == 0) |
488 | 0 | serialised->size = value->size; |
489 | 0 | g_assert (serialised->size == value->size); |
490 | 0 | serialised->depth = value->depth; |
491 | |
|
492 | 0 | if (value->state & STATE_SERIALISED) |
493 | 0 | { |
494 | 0 | serialised->ordered_offsets_up_to = value->contents.serialised.ordered_offsets_up_to; |
495 | 0 | serialised->checked_offsets_up_to = value->contents.serialised.checked_offsets_up_to; |
496 | 0 | } |
497 | 0 | else |
498 | 0 | { |
499 | 0 | serialised->ordered_offsets_up_to = 0; |
500 | 0 | serialised->checked_offsets_up_to = 0; |
501 | 0 | } |
502 | |
|
503 | 0 | 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 | 0 | g_variant_store (value, serialised->data); |
508 | 0 | } |
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 | | g_variant_ensure_serialised (GVariant *value) |
528 | 0 | { |
529 | 0 | g_assert (value->state & STATE_LOCKED); |
530 | | |
531 | 0 | if (~value->state & STATE_SERIALISED) |
532 | 0 | { |
533 | 0 | GBytes *bytes; |
534 | 0 | gpointer data; |
535 | |
|
536 | 0 | TRACE(GLIB_VARIANT_START_SERIALISE(value, value->type_info)); |
537 | 0 | g_variant_ensure_size (value); |
538 | 0 | data = g_malloc (value->size); |
539 | 0 | g_variant_serialise (value, data); |
540 | |
|
541 | 0 | g_variant_release_children (value); |
542 | |
|
543 | 0 | bytes = g_bytes_new_take (data, value->size); |
544 | 0 | value->contents.serialised.data = g_bytes_get_data (bytes, NULL); |
545 | 0 | value->contents.serialised.bytes = bytes; |
546 | 0 | value->contents.serialised.ordered_offsets_up_to = G_MAXSIZE; |
547 | 0 | value->contents.serialised.checked_offsets_up_to = G_MAXSIZE; |
548 | 0 | value->state |= STATE_SERIALISED; |
549 | 0 | TRACE(GLIB_VARIANT_END_SERIALISE(value, value->type_info)); |
550 | 0 | } |
551 | 0 | } |
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 | | g_variant_alloc (const GVariantType *type, |
571 | | gboolean serialised, |
572 | | gboolean trusted, |
573 | | gsize suffix_size) |
574 | 0 | { |
575 | 0 | G_GNUC_UNUSED gboolean size_check; |
576 | 0 | GVariant *value; |
577 | 0 | gsize size; |
578 | |
|
579 | 0 | size_check = g_size_checked_add (&size, sizeof *value, suffix_size); |
580 | 0 | g_assert (size_check); |
581 | | |
582 | 0 | value = g_malloc (size); |
583 | 0 | value->type_info = g_variant_type_info_get (type); |
584 | 0 | value->state = (serialised ? STATE_SERIALISED : 0) | |
585 | 0 | (trusted ? STATE_TRUSTED : 0) | |
586 | 0 | STATE_FLOATING; |
587 | 0 | value->size = (gssize) -1; |
588 | 0 | g_atomic_ref_count_init (&value->ref_count); |
589 | 0 | value->depth = 0; |
590 | |
|
591 | 0 | return value; |
592 | 0 | } |
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 | | g_variant_new_from_bytes (const GVariantType *type, |
616 | | GBytes *bytes, |
617 | | gboolean trusted) |
618 | 0 | { |
619 | 0 | return g_variant_new_take_bytes (type, g_bytes_ref (bytes), trusted); |
620 | 0 | } |
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 | | g_variant_new_preallocated_trusted (const GVariantType *type, |
646 | | gconstpointer data, |
647 | | gsize size) |
648 | 0 | { |
649 | 0 | GVariant *value; |
650 | 0 | gsize expected_size; |
651 | 0 | guint alignment; |
652 | |
|
653 | 0 | value = g_variant_alloc (type, TRUE, TRUE, size); |
654 | |
|
655 | 0 | g_variant_type_info_query (value->type_info, &alignment, &expected_size); |
656 | |
|
657 | 0 | g_assert (expected_size == 0 || size == expected_size); |
658 | | |
659 | 0 | value->contents.serialised.ordered_offsets_up_to = G_MAXSIZE; |
660 | 0 | value->contents.serialised.checked_offsets_up_to = G_MAXSIZE; |
661 | 0 | value->contents.serialised.bytes = NULL; |
662 | 0 | value->contents.serialised.data = value->suffix; |
663 | 0 | value->size = size; |
664 | |
|
665 | 0 | memcpy (value->suffix, data, size); |
666 | |
|
667 | 0 | TRACE(GLIB_VARIANT_FROM_BUFFER(value, value->type_info, value->ref_count, value->state)); |
668 | |
|
669 | 0 | return value; |
670 | 0 | } |
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 | | g_variant_new_take_bytes (const GVariantType *type, |
684 | | GBytes *bytes, |
685 | | gboolean trusted) |
686 | 0 | { |
687 | 0 | GVariant *value; |
688 | 0 | guint alignment; |
689 | 0 | gsize size; |
690 | 0 | GBytes *owned_bytes = NULL; |
691 | 0 | GVariantSerialised serialised; |
692 | |
|
693 | 0 | value = g_variant_alloc (type, TRUE, trusted, 0); |
694 | |
|
695 | 0 | g_variant_type_info_query (value->type_info, |
696 | 0 | &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 | 0 | serialised.type_info = value->type_info; |
705 | 0 | serialised.data = (guchar *) g_bytes_get_data (bytes, &serialised.size); |
706 | 0 | serialised.depth = 0; |
707 | 0 | serialised.ordered_offsets_up_to = trusted ? G_MAXSIZE : 0; |
708 | 0 | serialised.checked_offsets_up_to = trusted ? G_MAXSIZE : 0; |
709 | |
|
710 | 0 | if (!g_variant_serialised_check (serialised)) |
711 | 0 | { |
712 | 0 | #ifdef HAVE_POSIX_MEMALIGN |
713 | 0 | gpointer aligned_data = NULL; |
714 | 0 | 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 | 0 | if (aligned_size != 0 && |
725 | 0 | posix_memalign (&aligned_data, MAX (sizeof (void *), alignment + 1), |
726 | 0 | aligned_size) != 0) |
727 | 0 | g_error ("posix_memalign failed"); |
728 | |
|
729 | 0 | if (aligned_size != 0) |
730 | 0 | memcpy (aligned_data, g_bytes_get_data (bytes, NULL), aligned_size); |
731 | |
|
732 | 0 | owned_bytes = bytes; |
733 | 0 | bytes = g_bytes_new_with_free_func (aligned_data, |
734 | 0 | aligned_size, |
735 | 0 | free, aligned_data); |
736 | 0 | 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 | 0 | } |
747 | |
|
748 | 0 | value->contents.serialised.bytes = bytes; |
749 | |
|
750 | 0 | if (size && g_bytes_get_size (bytes) != size) |
751 | 0 | { |
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 | 0 | value->contents.serialised.data = NULL; |
760 | 0 | value->size = size; |
761 | 0 | } |
762 | 0 | else |
763 | 0 | { |
764 | 0 | value->contents.serialised.data = g_bytes_get_data (bytes, &value->size); |
765 | 0 | } |
766 | |
|
767 | 0 | value->contents.serialised.ordered_offsets_up_to = trusted ? G_MAXSIZE : 0; |
768 | 0 | value->contents.serialised.checked_offsets_up_to = trusted ? G_MAXSIZE : 0; |
769 | |
|
770 | 0 | g_clear_pointer (&owned_bytes, g_bytes_unref); |
771 | |
|
772 | 0 | TRACE(GLIB_VARIANT_FROM_BUFFER(value, value->type_info, value->ref_count, value->state)); |
773 | |
|
774 | 0 | return value; |
775 | 0 | } |
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 | | g_variant_new_from_children (const GVariantType *type, |
795 | | GVariant **children, |
796 | | gsize n_children, |
797 | | gboolean trusted) |
798 | 0 | { |
799 | 0 | GVariant *value; |
800 | |
|
801 | 0 | value = g_variant_alloc (type, FALSE, trusted, 0); |
802 | 0 | value->contents.tree.children = children; |
803 | 0 | value->contents.tree.n_children = n_children; |
804 | 0 | TRACE(GLIB_VARIANT_FROM_CHILDREN(value, value->type_info, value->ref_count, value->state)); |
805 | |
|
806 | 0 | return value; |
807 | 0 | } |
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 | | g_variant_get_type_info (GVariant *value) |
821 | 0 | { |
822 | 0 | return value->type_info; |
823 | 0 | } |
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 | | g_variant_is_trusted (GVariant *value) |
843 | 0 | { |
844 | 0 | return (value->state & STATE_TRUSTED) != 0; |
845 | 0 | } |
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 | | g_variant_get_depth (GVariant *value) |
858 | 0 | { |
859 | 0 | return value->depth; |
860 | 0 | } |
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 | | g_variant_unref (GVariant *value) |
875 | 0 | { |
876 | 0 | g_return_if_fail (value != NULL); |
877 | | |
878 | 0 | TRACE(GLIB_VARIANT_UNREF(value, value->type_info, value->ref_count, value->state)); |
879 | |
|
880 | 0 | if (g_atomic_ref_count_dec (&value->ref_count)) |
881 | 0 | { |
882 | 0 | if G_UNLIKELY (value->state & STATE_LOCKED) |
883 | 0 | g_critical ("attempting to free a locked GVariant instance. " |
884 | 0 | "This should never happen."); |
885 | |
|
886 | 0 | value->state |= STATE_LOCKED; |
887 | |
|
888 | 0 | g_variant_type_info_unref (value->type_info); |
889 | |
|
890 | 0 | if (value->state & STATE_SERIALISED) |
891 | 0 | g_bytes_unref (value->contents.serialised.bytes); |
892 | 0 | else |
893 | 0 | g_variant_release_children (value); |
894 | |
|
895 | 0 | memset (value, 0, sizeof (GVariant)); |
896 | 0 | g_free (value); |
897 | 0 | } |
898 | 0 | } |
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 | | g_variant_ref (GVariant *value) |
912 | 0 | { |
913 | 0 | g_return_val_if_fail (value != NULL, NULL); |
914 | | |
915 | 0 | TRACE(GLIB_VARIANT_REF(value, value->type_info, value->ref_count, value->state)); |
916 | |
|
917 | 0 | g_atomic_ref_count_inc (&value->ref_count); |
918 | |
|
919 | 0 | return value; |
920 | 0 | } |
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 | | g_variant_ref_sink (GVariant *value) |
955 | 0 | { |
956 | 0 | int old_state; |
957 | |
|
958 | 0 | g_return_val_if_fail (value != NULL, NULL); |
959 | 0 | g_return_val_if_fail (!g_atomic_ref_count_compare (&value->ref_count, 0), NULL); |
960 | | |
961 | 0 | TRACE(GLIB_VARIANT_REF_SINK(value, value->type_info, value->ref_count, value->state, value->state & STATE_FLOATING)); |
962 | |
|
963 | 0 | old_state = value->state; |
964 | |
|
965 | 0 | while (old_state & STATE_FLOATING) |
966 | 0 | { |
967 | 0 | int new_state = old_state & ~STATE_FLOATING; |
968 | |
|
969 | 0 | if (g_atomic_int_compare_and_exchange_full (&value->state, old_state, new_state, &old_state)) |
970 | 0 | return value; |
971 | 0 | } |
972 | | |
973 | 0 | g_atomic_ref_count_inc (&value->ref_count); |
974 | |
|
975 | 0 | return value; |
976 | 0 | } |
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 | | g_variant_take_ref (GVariant *value) |
1019 | 0 | { |
1020 | 0 | g_return_val_if_fail (value != NULL, NULL); |
1021 | 0 | g_return_val_if_fail (!g_atomic_ref_count_compare (&value->ref_count, 0), NULL); |
1022 | | |
1023 | 0 | TRACE(GLIB_VARIANT_TAKE_REF(value, value->type_info, value->ref_count, value->state, value->state & STATE_FLOATING)); |
1024 | 0 | g_atomic_int_and (&value->state, ~STATE_FLOATING); |
1025 | |
|
1026 | 0 | return value; |
1027 | 0 | } |
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 | | g_variant_is_floating (GVariant *value) |
1049 | 0 | { |
1050 | 0 | g_return_val_if_fail (value != NULL, FALSE); |
1051 | | |
1052 | 0 | return (value->state & STATE_FLOATING) != 0; |
1053 | 0 | } |
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 | | g_variant_get_size (GVariant *value) |
1077 | 0 | { |
1078 | 0 | g_variant_lock (value); |
1079 | 0 | g_variant_ensure_size (value); |
1080 | 0 | g_variant_unlock (value); |
1081 | |
|
1082 | 0 | return value->size; |
1083 | 0 | } |
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 | | g_variant_get_data (GVariant *value) |
1121 | 0 | { |
1122 | 0 | g_variant_lock (value); |
1123 | 0 | g_variant_ensure_serialised (value); |
1124 | 0 | g_variant_unlock (value); |
1125 | |
|
1126 | 0 | return value->contents.serialised.data; |
1127 | 0 | } |
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 | | g_variant_get_data_as_bytes (GVariant *value) |
1144 | 0 | { |
1145 | 0 | const gchar *bytes_data; |
1146 | 0 | const gchar *data; |
1147 | 0 | gsize bytes_size = 0; |
1148 | 0 | gsize size; |
1149 | |
|
1150 | 0 | g_variant_lock (value); |
1151 | 0 | g_variant_ensure_serialised (value); |
1152 | 0 | g_variant_unlock (value); |
1153 | |
|
1154 | 0 | if (value->contents.serialised.bytes != NULL) |
1155 | 0 | bytes_data = g_bytes_get_data (value->contents.serialised.bytes, &bytes_size); |
1156 | 0 | else |
1157 | 0 | bytes_data = NULL; |
1158 | |
|
1159 | 0 | data = value->contents.serialised.data; |
1160 | 0 | size = value->size; |
1161 | |
|
1162 | 0 | if (data == NULL) |
1163 | 0 | { |
1164 | 0 | g_assert (size == 0); |
1165 | 0 | data = bytes_data; |
1166 | 0 | } |
1167 | | |
1168 | 0 | if (bytes_data != NULL && data == bytes_data && size == bytes_size) |
1169 | 0 | return g_bytes_ref (value->contents.serialised.bytes); |
1170 | 0 | else if (bytes_data != NULL) |
1171 | 0 | return g_bytes_new_from_bytes (value->contents.serialised.bytes, |
1172 | 0 | data - bytes_data, size); |
1173 | 0 | else |
1174 | 0 | return g_bytes_new (value->contents.serialised.data, size); |
1175 | 0 | } |
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 | | g_variant_n_children (GVariant *value) |
1200 | 0 | { |
1201 | 0 | gsize n_children; |
1202 | |
|
1203 | 0 | g_variant_lock (value); |
1204 | |
|
1205 | 0 | if (value->state & STATE_SERIALISED) |
1206 | 0 | n_children = g_variant_serialised_n_children ( |
1207 | 0 | g_variant_to_serialised (value)); |
1208 | 0 | else |
1209 | 0 | n_children = value->contents.tree.n_children; |
1210 | |
|
1211 | 0 | g_variant_unlock (value); |
1212 | |
|
1213 | 0 | return n_children; |
1214 | 0 | } |
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 | | g_variant_get_child_value (GVariant *value, |
1251 | | gsize index_) |
1252 | 0 | { |
1253 | 0 | g_return_val_if_fail (value->depth < G_MAXSIZE, NULL); |
1254 | | |
1255 | 0 | if (~g_atomic_int_get (&value->state) & STATE_SERIALISED) |
1256 | 0 | { |
1257 | | /* g_variant_serialised_get_child() does its own checks on index_ */ |
1258 | 0 | g_return_val_if_fail (index_ < g_variant_n_children (value), NULL); |
1259 | | |
1260 | 0 | g_variant_lock (value); |
1261 | |
|
1262 | 0 | if (~value->state & STATE_SERIALISED) |
1263 | 0 | { |
1264 | 0 | GVariant *child; |
1265 | |
|
1266 | 0 | child = g_variant_ref (value->contents.tree.children[index_]); |
1267 | 0 | g_variant_unlock (value); |
1268 | |
|
1269 | 0 | return child; |
1270 | 0 | } |
1271 | | |
1272 | 0 | g_variant_unlock (value); |
1273 | 0 | } |
1274 | | |
1275 | 0 | { |
1276 | 0 | GVariantSerialised serialised = g_variant_to_serialised (value); |
1277 | 0 | GVariantSerialised s_child; |
1278 | 0 | 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 | 0 | 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 | 0 | value->contents.serialised.ordered_offsets_up_to = MAX (value->contents.serialised.ordered_offsets_up_to, serialised.ordered_offsets_up_to); |
1287 | 0 | 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 | 0 | if (!(value->state & STATE_TRUSTED) && |
1296 | 0 | g_variant_type_info_query_depth (s_child.type_info) >= |
1297 | 0 | G_VARIANT_MAX_RECURSION_DEPTH - value->depth) |
1298 | 0 | { |
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 | 0 | } |
1303 | | |
1304 | | /* create a new serialized instance out of it */ |
1305 | 0 | child = g_new (GVariant, 1); |
1306 | 0 | child->type_info = s_child.type_info; |
1307 | 0 | child->state = (value->state & STATE_TRUSTED) | |
1308 | 0 | STATE_SERIALISED; |
1309 | 0 | child->size = s_child.size; |
1310 | 0 | g_atomic_ref_count_init (&child->ref_count); |
1311 | 0 | child->depth = value->depth + 1; |
1312 | 0 | if (value->contents.serialised.bytes != NULL) |
1313 | 0 | child->contents.serialised.bytes = |
1314 | 0 | g_bytes_ref (value->contents.serialised.bytes); |
1315 | 0 | else |
1316 | 0 | child->contents.serialised.bytes = NULL; |
1317 | 0 | child->contents.serialised.data = s_child.data; |
1318 | 0 | child->contents.serialised.ordered_offsets_up_to = (value->state & STATE_TRUSTED) ? G_MAXSIZE : s_child.ordered_offsets_up_to; |
1319 | 0 | child->contents.serialised.checked_offsets_up_to = (value->state & STATE_TRUSTED) ? G_MAXSIZE : s_child.checked_offsets_up_to; |
1320 | |
|
1321 | 0 | TRACE(GLIB_VARIANT_FROM_PARENT(child, child->type_info, child->ref_count, child->state, value)); |
1322 | |
|
1323 | 0 | return child; |
1324 | 0 | } |
1325 | 0 | } |
1326 | | |
1327 | | /** |
1328 | | * g_variant_maybe_get_child_value: |
1329 | | * @value: a container #GVariant |
1330 | | * @index_: the index of the child to fetch |
1331 | | * |
1332 | | * Reads a child item out of a container #GVariant instance, if it is in normal |
1333 | | * form. If it is not in normal form, return %NULL. |
1334 | | * |
1335 | | * This function behaves the same as g_variant_get_child_value(), except that it |
1336 | | * returns %NULL if the child is not in normal form. g_variant_get_child_value() |
1337 | | * would instead return a new default value of the correct type. |
1338 | | * |
1339 | | * This is intended to be used internally to avoid unnecessary #GVariant |
1340 | | * allocations. |
1341 | | * |
1342 | | * The returned value is never floating. You should free it with |
1343 | | * g_variant_unref() when you're done with it. |
1344 | | * |
1345 | | * This function is O(1). |
1346 | | * |
1347 | | * Returns: (transfer full): the child at the specified index |
1348 | | * |
1349 | | * Since: 2.74 |
1350 | | */ |
1351 | | GVariant * |
1352 | | g_variant_maybe_get_child_value (GVariant *value, |
1353 | | gsize index_) |
1354 | 0 | { |
1355 | 0 | g_return_val_if_fail (value->depth < G_MAXSIZE, NULL); |
1356 | | |
1357 | 0 | if (~g_atomic_int_get (&value->state) & STATE_SERIALISED) |
1358 | 0 | { |
1359 | | /* g_variant_serialised_get_child() does its own checks on index_ */ |
1360 | 0 | g_return_val_if_fail (index_ < g_variant_n_children (value), NULL); |
1361 | | |
1362 | 0 | g_variant_lock (value); |
1363 | |
|
1364 | 0 | if (~value->state & STATE_SERIALISED) |
1365 | 0 | { |
1366 | 0 | GVariant *child; |
1367 | |
|
1368 | 0 | child = g_variant_ref (value->contents.tree.children[index_]); |
1369 | 0 | g_variant_unlock (value); |
1370 | |
|
1371 | 0 | return child; |
1372 | 0 | } |
1373 | | |
1374 | 0 | g_variant_unlock (value); |
1375 | 0 | } |
1376 | | |
1377 | 0 | { |
1378 | 0 | GVariantSerialised serialised = g_variant_to_serialised (value); |
1379 | 0 | GVariantSerialised s_child; |
1380 | | |
1381 | | /* get the serializer to extract the serialized data for the child |
1382 | | * from the serialized data for the container |
1383 | | */ |
1384 | 0 | s_child = g_variant_serialised_get_child (serialised, index_); |
1385 | |
|
1386 | 0 | if (!(value->state & STATE_TRUSTED) && s_child.data == NULL) |
1387 | 0 | { |
1388 | 0 | g_variant_type_info_unref (s_child.type_info); |
1389 | 0 | return NULL; |
1390 | 0 | } |
1391 | | |
1392 | 0 | g_variant_type_info_unref (s_child.type_info); |
1393 | 0 | return g_variant_get_child_value (value, index_); |
1394 | 0 | } |
1395 | 0 | } |
1396 | | |
1397 | | /** |
1398 | | * g_variant_store: |
1399 | | * @value: the #GVariant to store |
1400 | | * @data: (not nullable): the location to store the serialized data at |
1401 | | * |
1402 | | * Stores the serialized form of @value at @data. @data should be |
1403 | | * large enough. See g_variant_get_size(). |
1404 | | * |
1405 | | * The stored data is in machine native byte order but may not be in |
1406 | | * fully-normalised form if read from an untrusted source. See |
1407 | | * g_variant_get_normal_form() for a solution. |
1408 | | * |
1409 | | * As with g_variant_get_data(), to be able to deserialize the |
1410 | | * serialized variant successfully, its type and (if the destination |
1411 | | * machine might be different) its endianness must also be available. |
1412 | | * |
1413 | | * This function is approximately O(n) in the size of @data. |
1414 | | * |
1415 | | * Since: 2.24 |
1416 | | **/ |
1417 | | void |
1418 | | g_variant_store (GVariant *value, |
1419 | | gpointer data) |
1420 | 0 | { |
1421 | 0 | g_return_if_fail (data != NULL); |
1422 | | |
1423 | 0 | g_variant_lock (value); |
1424 | |
|
1425 | 0 | if (value->state & STATE_SERIALISED) |
1426 | 0 | { |
1427 | 0 | if (value->contents.serialised.data != NULL) |
1428 | 0 | memcpy (data, value->contents.serialised.data, value->size); |
1429 | 0 | else |
1430 | 0 | memset (data, 0, value->size); |
1431 | 0 | } |
1432 | 0 | else |
1433 | 0 | g_variant_serialise (value, data); |
1434 | |
|
1435 | 0 | g_variant_unlock (value); |
1436 | 0 | } |
1437 | | |
1438 | | /** |
1439 | | * g_variant_is_normal_form: |
1440 | | * @value: a #GVariant instance |
1441 | | * |
1442 | | * Checks if @value is in normal form. |
1443 | | * |
1444 | | * The main reason to do this is to detect if a given chunk of |
1445 | | * serialized data is in normal form: load the data into a #GVariant |
1446 | | * using g_variant_new_from_data() and then use this function to |
1447 | | * check. |
1448 | | * |
1449 | | * If @value is found to be in normal form then it will be marked as |
1450 | | * being trusted. If the value was already marked as being trusted then |
1451 | | * this function will immediately return %TRUE. |
1452 | | * |
1453 | | * There may be implementation specific restrictions on deeply nested values. |
1454 | | * GVariant is guaranteed to handle nesting up to at least 64 levels. |
1455 | | * |
1456 | | * Returns: %TRUE if @value is in normal form |
1457 | | * |
1458 | | * Since: 2.24 |
1459 | | **/ |
1460 | | gboolean |
1461 | | g_variant_is_normal_form (GVariant *value) |
1462 | 0 | { |
1463 | 0 | if (value->state & STATE_TRUSTED) |
1464 | 0 | return TRUE; |
1465 | | |
1466 | 0 | g_variant_lock (value); |
1467 | |
|
1468 | 0 | if (value->depth >= G_VARIANT_MAX_RECURSION_DEPTH) |
1469 | 0 | return FALSE; |
1470 | | |
1471 | 0 | if (value->state & STATE_SERIALISED) |
1472 | 0 | { |
1473 | 0 | if (g_variant_serialised_is_normal (g_variant_to_serialised (value))) |
1474 | 0 | value->state |= STATE_TRUSTED; |
1475 | 0 | } |
1476 | 0 | else |
1477 | 0 | { |
1478 | 0 | gboolean normal = TRUE; |
1479 | 0 | gsize i; |
1480 | |
|
1481 | 0 | for (i = 0; i < value->contents.tree.n_children; i++) |
1482 | 0 | normal &= g_variant_is_normal_form (value->contents.tree.children[i]); |
1483 | |
|
1484 | 0 | if (normal) |
1485 | 0 | value->state |= STATE_TRUSTED; |
1486 | 0 | } |
1487 | |
|
1488 | 0 | g_variant_unlock (value); |
1489 | |
|
1490 | 0 | return (value->state & STATE_TRUSTED) != 0; |
1491 | 0 | } |