/src/rauc/subprojects/glib-2.76.5/glib/gvariant.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright © 2007, 2008 Ryan Lortie |
3 | | * Copyright © 2010 Codethink Limited |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | * |
20 | | * Author: Ryan Lortie <desrt@desrt.ca> |
21 | | */ |
22 | | |
23 | | /* Prologue {{{1 */ |
24 | | |
25 | | #include "config.h" |
26 | | |
27 | | #include <glib/gvariant-serialiser.h> |
28 | | #include "gvariant-internal.h" |
29 | | #include <glib/gvariant-core.h> |
30 | | #include <glib/gtestutils.h> |
31 | | #include <glib/gstrfuncs.h> |
32 | | #include <glib/gslice.h> |
33 | | #include <glib/ghash.h> |
34 | | #include <glib/gmem.h> |
35 | | |
36 | | #include <string.h> |
37 | | |
38 | | /** |
39 | | * SECTION:gvariant |
40 | | * @title: GVariant |
41 | | * @short_description: strongly typed value datatype |
42 | | * @see_also: GVariantType |
43 | | * |
44 | | * #GVariant is a variant datatype; it can contain one or more values |
45 | | * along with information about the type of the values. |
46 | | * |
47 | | * A #GVariant may contain simple types, like an integer, or a boolean value; |
48 | | * or complex types, like an array of two strings, or a dictionary of key |
49 | | * value pairs. A #GVariant is also immutable: once it's been created neither |
50 | | * its type nor its content can be modified further. |
51 | | * |
52 | | * GVariant is useful whenever data needs to be serialized, for example when |
53 | | * sending method parameters in D-Bus, or when saving settings using GSettings. |
54 | | * |
55 | | * When creating a new #GVariant, you pass the data you want to store in it |
56 | | * along with a string representing the type of data you wish to pass to it. |
57 | | * |
58 | | * For instance, if you want to create a #GVariant holding an integer value you |
59 | | * can use: |
60 | | * |
61 | | * |[<!-- language="C" --> |
62 | | * GVariant *v = g_variant_new ("u", 40); |
63 | | * ]| |
64 | | * |
65 | | * The string "u" in the first argument tells #GVariant that the data passed to |
66 | | * the constructor (40) is going to be an unsigned integer. |
67 | | * |
68 | | * More advanced examples of #GVariant in use can be found in documentation for |
69 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
70 | | * |
71 | | * The range of possible values is determined by the type. |
72 | | * |
73 | | * The type system used by #GVariant is #GVariantType. |
74 | | * |
75 | | * #GVariant instances always have a type and a value (which are given |
76 | | * at construction time). The type and value of a #GVariant instance |
77 | | * can never change other than by the #GVariant itself being |
78 | | * destroyed. A #GVariant cannot contain a pointer. |
79 | | * |
80 | | * #GVariant is reference counted using g_variant_ref() and |
81 | | * g_variant_unref(). #GVariant also has floating reference counts -- |
82 | | * see g_variant_ref_sink(). |
83 | | * |
84 | | * #GVariant is completely threadsafe. A #GVariant instance can be |
85 | | * concurrently accessed in any way from any number of threads without |
86 | | * problems. |
87 | | * |
88 | | * #GVariant is heavily optimised for dealing with data in serialized |
89 | | * form. It works particularly well with data located in memory-mapped |
90 | | * files. It can perform nearly all deserialization operations in a |
91 | | * small constant time, usually touching only a single memory page. |
92 | | * Serialized #GVariant data can also be sent over the network. |
93 | | * |
94 | | * #GVariant is largely compatible with D-Bus. Almost all types of |
95 | | * #GVariant instances can be sent over D-Bus. See #GVariantType for |
96 | | * exceptions. (However, #GVariant's serialization format is not the same |
97 | | * as the serialization format of a D-Bus message body: use #GDBusMessage, |
98 | | * in the gio library, for those.) |
99 | | * |
100 | | * For space-efficiency, the #GVariant serialization format does not |
101 | | * automatically include the variant's length, type or endianness, |
102 | | * which must either be implied from context (such as knowledge that a |
103 | | * particular file format always contains a little-endian |
104 | | * %G_VARIANT_TYPE_VARIANT which occupies the whole length of the file) |
105 | | * or supplied out-of-band (for instance, a length, type and/or endianness |
106 | | * indicator could be placed at the beginning of a file, network message |
107 | | * or network stream). |
108 | | * |
109 | | * A #GVariant's size is limited mainly by any lower level operating |
110 | | * system constraints, such as the number of bits in #gsize. For |
111 | | * example, it is reasonable to have a 2GB file mapped into memory |
112 | | * with #GMappedFile, and call g_variant_new_from_data() on it. |
113 | | * |
114 | | * For convenience to C programmers, #GVariant features powerful |
115 | | * varargs-based value construction and destruction. This feature is |
116 | | * designed to be embedded in other libraries. |
117 | | * |
118 | | * There is a Python-inspired text language for describing #GVariant |
119 | | * values. #GVariant includes a printer for this language and a parser |
120 | | * with type inferencing. |
121 | | * |
122 | | * ## Memory Use |
123 | | * |
124 | | * #GVariant tries to be quite efficient with respect to memory use. |
125 | | * This section gives a rough idea of how much memory is used by the |
126 | | * current implementation. The information here is subject to change |
127 | | * in the future. |
128 | | * |
129 | | * The memory allocated by #GVariant can be grouped into 4 broad |
130 | | * purposes: memory for serialized data, memory for the type |
131 | | * information cache, buffer management memory and memory for the |
132 | | * #GVariant structure itself. |
133 | | * |
134 | | * ## Serialized Data Memory |
135 | | * |
136 | | * This is the memory that is used for storing GVariant data in |
137 | | * serialized form. This is what would be sent over the network or |
138 | | * what would end up on disk, not counting any indicator of the |
139 | | * endianness, or of the length or type of the top-level variant. |
140 | | * |
141 | | * The amount of memory required to store a boolean is 1 byte. 16, |
142 | | * 32 and 64 bit integers and double precision floating point numbers |
143 | | * use their "natural" size. Strings (including object path and |
144 | | * signature strings) are stored with a nul terminator, and as such |
145 | | * use the length of the string plus 1 byte. |
146 | | * |
147 | | * Maybe types use no space at all to represent the null value and |
148 | | * use the same amount of space (sometimes plus one byte) as the |
149 | | * equivalent non-maybe-typed value to represent the non-null case. |
150 | | * |
151 | | * Arrays use the amount of space required to store each of their |
152 | | * members, concatenated. Additionally, if the items stored in an |
153 | | * array are not of a fixed-size (ie: strings, other arrays, etc) |
154 | | * then an additional framing offset is stored for each item. The |
155 | | * size of this offset is either 1, 2 or 4 bytes depending on the |
156 | | * overall size of the container. Additionally, extra padding bytes |
157 | | * are added as required for alignment of child values. |
158 | | * |
159 | | * Tuples (including dictionary entries) use the amount of space |
160 | | * required to store each of their members, concatenated, plus one |
161 | | * framing offset (as per arrays) for each non-fixed-sized item in |
162 | | * the tuple, except for the last one. Additionally, extra padding |
163 | | * bytes are added as required for alignment of child values. |
164 | | * |
165 | | * Variants use the same amount of space as the item inside of the |
166 | | * variant, plus 1 byte, plus the length of the type string for the |
167 | | * item inside the variant. |
168 | | * |
169 | | * As an example, consider a dictionary mapping strings to variants. |
170 | | * In the case that the dictionary is empty, 0 bytes are required for |
171 | | * the serialization. |
172 | | * |
173 | | * If we add an item "width" that maps to the int32 value of 500 then |
174 | | * we will use 4 byte to store the int32 (so 6 for the variant |
175 | | * containing it) and 6 bytes for the string. The variant must be |
176 | | * aligned to 8 after the 6 bytes of the string, so that's 2 extra |
177 | | * bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used |
178 | | * for the dictionary entry. An additional 1 byte is added to the |
179 | | * array as a framing offset making a total of 15 bytes. |
180 | | * |
181 | | * If we add another entry, "title" that maps to a nullable string |
182 | | * that happens to have a value of null, then we use 0 bytes for the |
183 | | * null value (and 3 bytes for the variant to contain it along with |
184 | | * its type string) plus 6 bytes for the string. Again, we need 2 |
185 | | * padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes. |
186 | | * |
187 | | * We now require extra padding between the two items in the array. |
188 | | * After the 14 bytes of the first item, that's 2 bytes required. |
189 | | * We now require 2 framing offsets for an extra two |
190 | | * bytes. 14 + 2 + 11 + 2 = 29 bytes to encode the entire two-item |
191 | | * dictionary. |
192 | | * |
193 | | * ## Type Information Cache |
194 | | * |
195 | | * For each GVariant type that currently exists in the program a type |
196 | | * information structure is kept in the type information cache. The |
197 | | * type information structure is required for rapid deserialization. |
198 | | * |
199 | | * Continuing with the above example, if a #GVariant exists with the |
200 | | * type "a{sv}" then a type information struct will exist for |
201 | | * "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type |
202 | | * will share the same type information. Additionally, all |
203 | | * single-digit types are stored in read-only static memory and do |
204 | | * not contribute to the writable memory footprint of a program using |
205 | | * #GVariant. |
206 | | * |
207 | | * Aside from the type information structures stored in read-only |
208 | | * memory, there are two forms of type information. One is used for |
209 | | * container types where there is a single element type: arrays and |
210 | | * maybe types. The other is used for container types where there |
211 | | * are multiple element types: tuples and dictionary entries. |
212 | | * |
213 | | * Array type info structures are 6 * sizeof (void *), plus the |
214 | | * memory required to store the type string itself. This means that |
215 | | * on 32-bit systems, the cache entry for "a{sv}" would require 30 |
216 | | * bytes of memory (plus malloc overhead). |
217 | | * |
218 | | * Tuple type info structures are 6 * sizeof (void *), plus 4 * |
219 | | * sizeof (void *) for each item in the tuple, plus the memory |
220 | | * required to store the type string itself. A 2-item tuple, for |
221 | | * example, would have a type information structure that consumed |
222 | | * writable memory in the size of 14 * sizeof (void *) (plus type |
223 | | * string) This means that on 32-bit systems, the cache entry for |
224 | | * "{sv}" would require 61 bytes of memory (plus malloc overhead). |
225 | | * |
226 | | * This means that in total, for our "a{sv}" example, 91 bytes of |
227 | | * type information would be allocated. |
228 | | * |
229 | | * The type information cache, additionally, uses a #GHashTable to |
230 | | * store and look up the cached items and stores a pointer to this |
231 | | * hash table in static storage. The hash table is freed when there |
232 | | * are zero items in the type cache. |
233 | | * |
234 | | * Although these sizes may seem large it is important to remember |
235 | | * that a program will probably only have a very small number of |
236 | | * different types of values in it and that only one type information |
237 | | * structure is required for many different values of the same type. |
238 | | * |
239 | | * ## Buffer Management Memory |
240 | | * |
241 | | * #GVariant uses an internal buffer management structure to deal |
242 | | * with the various different possible sources of serialized data |
243 | | * that it uses. The buffer is responsible for ensuring that the |
244 | | * correct call is made when the data is no longer in use by |
245 | | * #GVariant. This may involve a g_free() or a g_slice_free() or |
246 | | * even g_mapped_file_unref(). |
247 | | * |
248 | | * One buffer management structure is used for each chunk of |
249 | | * serialized data. The size of the buffer management structure |
250 | | * is 4 * (void *). On 32-bit systems, that's 16 bytes. |
251 | | * |
252 | | * ## GVariant structure |
253 | | * |
254 | | * The size of a #GVariant structure is 6 * (void *). On 32-bit |
255 | | * systems, that's 24 bytes. |
256 | | * |
257 | | * #GVariant structures only exist if they are explicitly created |
258 | | * with API calls. For example, if a #GVariant is constructed out of |
259 | | * serialized data for the example given above (with the dictionary) |
260 | | * then although there are 9 individual values that comprise the |
261 | | * entire dictionary (two keys, two values, two variants containing |
262 | | * the values, two dictionary entries, plus the dictionary itself), |
263 | | * only 1 #GVariant instance exists -- the one referring to the |
264 | | * dictionary. |
265 | | * |
266 | | * If calls are made to start accessing the other values then |
267 | | * #GVariant instances will exist for those values only for as long |
268 | | * as they are in use (ie: until you call g_variant_unref()). The |
269 | | * type information is shared. The serialized data and the buffer |
270 | | * management structure for that serialized data is shared by the |
271 | | * child. |
272 | | * |
273 | | * ## Summary |
274 | | * |
275 | | * To put the entire example together, for our dictionary mapping |
276 | | * strings to variants (with two entries, as given above), we are |
277 | | * using 91 bytes of memory for type information, 29 bytes of memory |
278 | | * for the serialized data, 16 bytes for buffer management and 24 |
279 | | * bytes for the #GVariant instance, or a total of 160 bytes, plus |
280 | | * malloc overhead. If we were to use g_variant_get_child_value() to |
281 | | * access the two dictionary entries, we would use an additional 48 |
282 | | * bytes. If we were to have other dictionaries of the same type, we |
283 | | * would use more memory for the serialized data and buffer |
284 | | * management for those dictionaries, but the type information would |
285 | | * be shared. |
286 | | */ |
287 | | |
288 | | /* definition of GVariant structure is in gvariant-core.c */ |
289 | | |
290 | | /* this is a g_return_val_if_fail() for making |
291 | | * sure a (GVariant *) has the required type. |
292 | | */ |
293 | | #define TYPE_CHECK(value, TYPE, val) \ |
294 | 0 | if G_UNLIKELY (!g_variant_is_of_type (value, TYPE)) { \ |
295 | 0 | g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, \ |
296 | 0 | "g_variant_is_of_type (" #value \ |
297 | 0 | ", " #TYPE ")"); \ |
298 | 0 | return val; \ |
299 | 0 | } |
300 | | |
301 | | /* Numeric Type Constructor/Getters {{{1 */ |
302 | | /* < private > |
303 | | * g_variant_new_from_trusted: |
304 | | * @type: the #GVariantType |
305 | | * @data: the data to use |
306 | | * @size: the size of @data |
307 | | * |
308 | | * Constructs a new trusted #GVariant instance from the provided data. |
309 | | * This is used to implement g_variant_new_* for all the basic types. |
310 | | * |
311 | | * Note: @data must be backed by memory that is aligned appropriately for the |
312 | | * @type being loaded. Otherwise this function will internally create a copy of |
313 | | * the memory (since GLib 2.60) or (in older versions) fail and exit the |
314 | | * process. |
315 | | * |
316 | | * Returns: a new floating #GVariant |
317 | | */ |
318 | | static GVariant * |
319 | | g_variant_new_from_trusted (const GVariantType *type, |
320 | | gconstpointer data, |
321 | | gsize size) |
322 | 0 | { |
323 | 0 | GVariant *value; |
324 | 0 | GBytes *bytes; |
325 | |
|
326 | 0 | bytes = g_bytes_new (data, size); |
327 | 0 | value = g_variant_new_from_bytes (type, bytes, TRUE); |
328 | 0 | g_bytes_unref (bytes); |
329 | |
|
330 | 0 | return value; |
331 | 0 | } |
332 | | |
333 | | /** |
334 | | * g_variant_new_boolean: |
335 | | * @value: a #gboolean value |
336 | | * |
337 | | * Creates a new boolean #GVariant instance -- either %TRUE or %FALSE. |
338 | | * |
339 | | * Returns: (transfer none): a floating reference to a new boolean #GVariant instance |
340 | | * |
341 | | * Since: 2.24 |
342 | | **/ |
343 | | GVariant * |
344 | | g_variant_new_boolean (gboolean value) |
345 | 0 | { |
346 | 0 | guchar v = value; |
347 | |
|
348 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_BOOLEAN, &v, 1); |
349 | 0 | } |
350 | | |
351 | | /** |
352 | | * g_variant_get_boolean: |
353 | | * @value: a boolean #GVariant instance |
354 | | * |
355 | | * Returns the boolean value of @value. |
356 | | * |
357 | | * It is an error to call this function with a @value of any type |
358 | | * other than %G_VARIANT_TYPE_BOOLEAN. |
359 | | * |
360 | | * Returns: %TRUE or %FALSE |
361 | | * |
362 | | * Since: 2.24 |
363 | | **/ |
364 | | gboolean |
365 | | g_variant_get_boolean (GVariant *value) |
366 | 0 | { |
367 | 0 | const guchar *data; |
368 | |
|
369 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_BOOLEAN, FALSE); |
370 | |
|
371 | 0 | data = g_variant_get_data (value); |
372 | |
|
373 | 0 | return data != NULL ? *data != 0 : FALSE; |
374 | 0 | } |
375 | | |
376 | | /* the constructors and accessors for byte, int{16,32,64}, handles and |
377 | | * doubles all look pretty much exactly the same, so we reduce |
378 | | * copy/pasting here. |
379 | | */ |
380 | | #define NUMERIC_TYPE(TYPE, type, ctype) \ |
381 | 0 | GVariant *g_variant_new_##type (ctype value) { \ |
382 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_##TYPE, \ |
383 | 0 | &value, sizeof value); \ |
384 | 0 | } \ Unexecuted instantiation: g_variant_new_byte Unexecuted instantiation: g_variant_new_int16 Unexecuted instantiation: g_variant_new_uint16 Unexecuted instantiation: g_variant_new_int32 Unexecuted instantiation: g_variant_new_uint32 Unexecuted instantiation: g_variant_new_int64 Unexecuted instantiation: g_variant_new_uint64 Unexecuted instantiation: g_variant_new_handle Unexecuted instantiation: g_variant_new_double |
385 | 0 | ctype g_variant_get_##type (GVariant *value) { \ |
386 | 0 | const ctype *data; \ |
387 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_ ## TYPE, 0); \ |
388 | 0 | data = g_variant_get_data (value); \ |
389 | 0 | return data != NULL ? *data : 0; \ |
390 | 0 | } Unexecuted instantiation: g_variant_get_byte Unexecuted instantiation: g_variant_get_int16 Unexecuted instantiation: g_variant_get_uint16 Unexecuted instantiation: g_variant_get_int32 Unexecuted instantiation: g_variant_get_uint32 Unexecuted instantiation: g_variant_get_int64 Unexecuted instantiation: g_variant_get_uint64 Unexecuted instantiation: g_variant_get_handle Unexecuted instantiation: g_variant_get_double |
391 | | |
392 | | |
393 | | /** |
394 | | * g_variant_new_byte: |
395 | | * @value: a #guint8 value |
396 | | * |
397 | | * Creates a new byte #GVariant instance. |
398 | | * |
399 | | * Returns: (transfer none): a floating reference to a new byte #GVariant instance |
400 | | * |
401 | | * Since: 2.24 |
402 | | **/ |
403 | | /** |
404 | | * g_variant_get_byte: |
405 | | * @value: a byte #GVariant instance |
406 | | * |
407 | | * Returns the byte value of @value. |
408 | | * |
409 | | * It is an error to call this function with a @value of any type |
410 | | * other than %G_VARIANT_TYPE_BYTE. |
411 | | * |
412 | | * Returns: a #guint8 |
413 | | * |
414 | | * Since: 2.24 |
415 | | **/ |
416 | | NUMERIC_TYPE (BYTE, byte, guint8) |
417 | | |
418 | | /** |
419 | | * g_variant_new_int16: |
420 | | * @value: a #gint16 value |
421 | | * |
422 | | * Creates a new int16 #GVariant instance. |
423 | | * |
424 | | * Returns: (transfer none): a floating reference to a new int16 #GVariant instance |
425 | | * |
426 | | * Since: 2.24 |
427 | | **/ |
428 | | /** |
429 | | * g_variant_get_int16: |
430 | | * @value: an int16 #GVariant instance |
431 | | * |
432 | | * Returns the 16-bit signed integer value of @value. |
433 | | * |
434 | | * It is an error to call this function with a @value of any type |
435 | | * other than %G_VARIANT_TYPE_INT16. |
436 | | * |
437 | | * Returns: a #gint16 |
438 | | * |
439 | | * Since: 2.24 |
440 | | **/ |
441 | | NUMERIC_TYPE (INT16, int16, gint16) |
442 | | |
443 | | /** |
444 | | * g_variant_new_uint16: |
445 | | * @value: a #guint16 value |
446 | | * |
447 | | * Creates a new uint16 #GVariant instance. |
448 | | * |
449 | | * Returns: (transfer none): a floating reference to a new uint16 #GVariant instance |
450 | | * |
451 | | * Since: 2.24 |
452 | | **/ |
453 | | /** |
454 | | * g_variant_get_uint16: |
455 | | * @value: a uint16 #GVariant instance |
456 | | * |
457 | | * Returns the 16-bit unsigned integer value of @value. |
458 | | * |
459 | | * It is an error to call this function with a @value of any type |
460 | | * other than %G_VARIANT_TYPE_UINT16. |
461 | | * |
462 | | * Returns: a #guint16 |
463 | | * |
464 | | * Since: 2.24 |
465 | | **/ |
466 | | NUMERIC_TYPE (UINT16, uint16, guint16) |
467 | | |
468 | | /** |
469 | | * g_variant_new_int32: |
470 | | * @value: a #gint32 value |
471 | | * |
472 | | * Creates a new int32 #GVariant instance. |
473 | | * |
474 | | * Returns: (transfer none): a floating reference to a new int32 #GVariant instance |
475 | | * |
476 | | * Since: 2.24 |
477 | | **/ |
478 | | /** |
479 | | * g_variant_get_int32: |
480 | | * @value: an int32 #GVariant instance |
481 | | * |
482 | | * Returns the 32-bit signed integer value of @value. |
483 | | * |
484 | | * It is an error to call this function with a @value of any type |
485 | | * other than %G_VARIANT_TYPE_INT32. |
486 | | * |
487 | | * Returns: a #gint32 |
488 | | * |
489 | | * Since: 2.24 |
490 | | **/ |
491 | | NUMERIC_TYPE (INT32, int32, gint32) |
492 | | |
493 | | /** |
494 | | * g_variant_new_uint32: |
495 | | * @value: a #guint32 value |
496 | | * |
497 | | * Creates a new uint32 #GVariant instance. |
498 | | * |
499 | | * Returns: (transfer none): a floating reference to a new uint32 #GVariant instance |
500 | | * |
501 | | * Since: 2.24 |
502 | | **/ |
503 | | /** |
504 | | * g_variant_get_uint32: |
505 | | * @value: a uint32 #GVariant instance |
506 | | * |
507 | | * Returns the 32-bit unsigned integer value of @value. |
508 | | * |
509 | | * It is an error to call this function with a @value of any type |
510 | | * other than %G_VARIANT_TYPE_UINT32. |
511 | | * |
512 | | * Returns: a #guint32 |
513 | | * |
514 | | * Since: 2.24 |
515 | | **/ |
516 | | NUMERIC_TYPE (UINT32, uint32, guint32) |
517 | | |
518 | | /** |
519 | | * g_variant_new_int64: |
520 | | * @value: a #gint64 value |
521 | | * |
522 | | * Creates a new int64 #GVariant instance. |
523 | | * |
524 | | * Returns: (transfer none): a floating reference to a new int64 #GVariant instance |
525 | | * |
526 | | * Since: 2.24 |
527 | | **/ |
528 | | /** |
529 | | * g_variant_get_int64: |
530 | | * @value: an int64 #GVariant instance |
531 | | * |
532 | | * Returns the 64-bit signed integer value of @value. |
533 | | * |
534 | | * It is an error to call this function with a @value of any type |
535 | | * other than %G_VARIANT_TYPE_INT64. |
536 | | * |
537 | | * Returns: a #gint64 |
538 | | * |
539 | | * Since: 2.24 |
540 | | **/ |
541 | | NUMERIC_TYPE (INT64, int64, gint64) |
542 | | |
543 | | /** |
544 | | * g_variant_new_uint64: |
545 | | * @value: a #guint64 value |
546 | | * |
547 | | * Creates a new uint64 #GVariant instance. |
548 | | * |
549 | | * Returns: (transfer none): a floating reference to a new uint64 #GVariant instance |
550 | | * |
551 | | * Since: 2.24 |
552 | | **/ |
553 | | /** |
554 | | * g_variant_get_uint64: |
555 | | * @value: a uint64 #GVariant instance |
556 | | * |
557 | | * Returns the 64-bit unsigned integer value of @value. |
558 | | * |
559 | | * It is an error to call this function with a @value of any type |
560 | | * other than %G_VARIANT_TYPE_UINT64. |
561 | | * |
562 | | * Returns: a #guint64 |
563 | | * |
564 | | * Since: 2.24 |
565 | | **/ |
566 | | NUMERIC_TYPE (UINT64, uint64, guint64) |
567 | | |
568 | | /** |
569 | | * g_variant_new_handle: |
570 | | * @value: a #gint32 value |
571 | | * |
572 | | * Creates a new handle #GVariant instance. |
573 | | * |
574 | | * By convention, handles are indexes into an array of file descriptors |
575 | | * that are sent alongside a D-Bus message. If you're not interacting |
576 | | * with D-Bus, you probably don't need them. |
577 | | * |
578 | | * Returns: (transfer none): a floating reference to a new handle #GVariant instance |
579 | | * |
580 | | * Since: 2.24 |
581 | | **/ |
582 | | /** |
583 | | * g_variant_get_handle: |
584 | | * @value: a handle #GVariant instance |
585 | | * |
586 | | * Returns the 32-bit signed integer value of @value. |
587 | | * |
588 | | * It is an error to call this function with a @value of any type other |
589 | | * than %G_VARIANT_TYPE_HANDLE. |
590 | | * |
591 | | * By convention, handles are indexes into an array of file descriptors |
592 | | * that are sent alongside a D-Bus message. If you're not interacting |
593 | | * with D-Bus, you probably don't need them. |
594 | | * |
595 | | * Returns: a #gint32 |
596 | | * |
597 | | * Since: 2.24 |
598 | | **/ |
599 | | NUMERIC_TYPE (HANDLE, handle, gint32) |
600 | | |
601 | | /** |
602 | | * g_variant_new_double: |
603 | | * @value: a #gdouble floating point value |
604 | | * |
605 | | * Creates a new double #GVariant instance. |
606 | | * |
607 | | * Returns: (transfer none): a floating reference to a new double #GVariant instance |
608 | | * |
609 | | * Since: 2.24 |
610 | | **/ |
611 | | /** |
612 | | * g_variant_get_double: |
613 | | * @value: a double #GVariant instance |
614 | | * |
615 | | * Returns the double precision floating point value of @value. |
616 | | * |
617 | | * It is an error to call this function with a @value of any type |
618 | | * other than %G_VARIANT_TYPE_DOUBLE. |
619 | | * |
620 | | * Returns: a #gdouble |
621 | | * |
622 | | * Since: 2.24 |
623 | | **/ |
624 | | NUMERIC_TYPE (DOUBLE, double, gdouble) |
625 | | |
626 | | /* Container type Constructor / Deconstructors {{{1 */ |
627 | | /** |
628 | | * g_variant_new_maybe: |
629 | | * @child_type: (nullable): the #GVariantType of the child, or %NULL |
630 | | * @child: (nullable): the child value, or %NULL |
631 | | * |
632 | | * Depending on if @child is %NULL, either wraps @child inside of a |
633 | | * maybe container or creates a Nothing instance for the given @type. |
634 | | * |
635 | | * At least one of @child_type and @child must be non-%NULL. |
636 | | * If @child_type is non-%NULL then it must be a definite type. |
637 | | * If they are both non-%NULL then @child_type must be the type |
638 | | * of @child. |
639 | | * |
640 | | * If @child is a floating reference (see g_variant_ref_sink()), the new |
641 | | * instance takes ownership of @child. |
642 | | * |
643 | | * Returns: (transfer none): a floating reference to a new #GVariant maybe instance |
644 | | * |
645 | | * Since: 2.24 |
646 | | **/ |
647 | | GVariant * |
648 | | g_variant_new_maybe (const GVariantType *child_type, |
649 | | GVariant *child) |
650 | 0 | { |
651 | 0 | GVariantType *maybe_type; |
652 | 0 | GVariant *value; |
653 | |
|
654 | 0 | g_return_val_if_fail (child_type == NULL || g_variant_type_is_definite |
655 | 0 | (child_type), 0); |
656 | 0 | g_return_val_if_fail (child_type != NULL || child != NULL, NULL); |
657 | 0 | g_return_val_if_fail (child_type == NULL || child == NULL || |
658 | 0 | g_variant_is_of_type (child, child_type), |
659 | 0 | NULL); |
660 | | |
661 | 0 | if (child_type == NULL) |
662 | 0 | child_type = g_variant_get_type (child); |
663 | |
|
664 | 0 | maybe_type = g_variant_type_new_maybe (child_type); |
665 | |
|
666 | 0 | if (child != NULL) |
667 | 0 | { |
668 | 0 | GVariant **children; |
669 | 0 | gboolean trusted; |
670 | |
|
671 | 0 | children = g_new (GVariant *, 1); |
672 | 0 | children[0] = g_variant_ref_sink (child); |
673 | 0 | trusted = g_variant_is_trusted (children[0]); |
674 | |
|
675 | 0 | value = g_variant_new_from_children (maybe_type, children, 1, trusted); |
676 | 0 | } |
677 | 0 | else |
678 | 0 | value = g_variant_new_from_children (maybe_type, NULL, 0, TRUE); |
679 | |
|
680 | 0 | g_variant_type_free (maybe_type); |
681 | |
|
682 | 0 | return value; |
683 | 0 | } |
684 | | |
685 | | /** |
686 | | * g_variant_get_maybe: |
687 | | * @value: a maybe-typed value |
688 | | * |
689 | | * Given a maybe-typed #GVariant instance, extract its value. If the |
690 | | * value is Nothing, then this function returns %NULL. |
691 | | * |
692 | | * Returns: (nullable) (transfer full): the contents of @value, or %NULL |
693 | | * |
694 | | * Since: 2.24 |
695 | | **/ |
696 | | GVariant * |
697 | | g_variant_get_maybe (GVariant *value) |
698 | 0 | { |
699 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_MAYBE, NULL); |
700 | |
|
701 | 0 | if (g_variant_n_children (value)) |
702 | 0 | return g_variant_get_child_value (value, 0); |
703 | | |
704 | 0 | return NULL; |
705 | 0 | } |
706 | | |
707 | | /** |
708 | | * g_variant_new_variant: (constructor) |
709 | | * @value: a #GVariant instance |
710 | | * |
711 | | * Boxes @value. The result is a #GVariant instance representing a |
712 | | * variant containing the original value. |
713 | | * |
714 | | * If @child is a floating reference (see g_variant_ref_sink()), the new |
715 | | * instance takes ownership of @child. |
716 | | * |
717 | | * Returns: (transfer none): a floating reference to a new variant #GVariant instance |
718 | | * |
719 | | * Since: 2.24 |
720 | | **/ |
721 | | GVariant * |
722 | | g_variant_new_variant (GVariant *value) |
723 | 0 | { |
724 | 0 | g_return_val_if_fail (value != NULL, NULL); |
725 | | |
726 | 0 | g_variant_ref_sink (value); |
727 | |
|
728 | 0 | return g_variant_new_from_children (G_VARIANT_TYPE_VARIANT, |
729 | 0 | g_memdup2 (&value, sizeof value), |
730 | 0 | 1, g_variant_is_trusted (value)); |
731 | 0 | } |
732 | | |
733 | | /** |
734 | | * g_variant_get_variant: |
735 | | * @value: a variant #GVariant instance |
736 | | * |
737 | | * Unboxes @value. The result is the #GVariant instance that was |
738 | | * contained in @value. |
739 | | * |
740 | | * Returns: (transfer full): the item contained in the variant |
741 | | * |
742 | | * Since: 2.24 |
743 | | **/ |
744 | | GVariant * |
745 | | g_variant_get_variant (GVariant *value) |
746 | 0 | { |
747 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_VARIANT, NULL); |
748 | |
|
749 | 0 | return g_variant_get_child_value (value, 0); |
750 | 0 | } |
751 | | |
752 | | /** |
753 | | * g_variant_new_array: |
754 | | * @child_type: (nullable): the element type of the new array |
755 | | * @children: (nullable) (array length=n_children): an array of |
756 | | * #GVariant pointers, the children |
757 | | * @n_children: the length of @children |
758 | | * |
759 | | * Creates a new #GVariant array from @children. |
760 | | * |
761 | | * @child_type must be non-%NULL if @n_children is zero. Otherwise, the |
762 | | * child type is determined by inspecting the first element of the |
763 | | * @children array. If @child_type is non-%NULL then it must be a |
764 | | * definite type. |
765 | | * |
766 | | * The items of the array are taken from the @children array. No entry |
767 | | * in the @children array may be %NULL. |
768 | | * |
769 | | * All items in the array must have the same type, which must be the |
770 | | * same as @child_type, if given. |
771 | | * |
772 | | * If the @children are floating references (see g_variant_ref_sink()), the |
773 | | * new instance takes ownership of them as if via g_variant_ref_sink(). |
774 | | * |
775 | | * Returns: (transfer none): a floating reference to a new #GVariant array |
776 | | * |
777 | | * Since: 2.24 |
778 | | **/ |
779 | | GVariant * |
780 | | g_variant_new_array (const GVariantType *child_type, |
781 | | GVariant * const *children, |
782 | | gsize n_children) |
783 | 0 | { |
784 | 0 | GVariantType *array_type; |
785 | 0 | GVariant **my_children; |
786 | 0 | gboolean trusted; |
787 | 0 | GVariant *value; |
788 | 0 | gsize i; |
789 | |
|
790 | 0 | g_return_val_if_fail (n_children > 0 || child_type != NULL, NULL); |
791 | 0 | g_return_val_if_fail (n_children == 0 || children != NULL, NULL); |
792 | 0 | g_return_val_if_fail (child_type == NULL || |
793 | 0 | g_variant_type_is_definite (child_type), NULL); |
794 | | |
795 | 0 | my_children = g_new (GVariant *, n_children); |
796 | 0 | trusted = TRUE; |
797 | |
|
798 | 0 | if (child_type == NULL) |
799 | 0 | child_type = g_variant_get_type (children[0]); |
800 | 0 | array_type = g_variant_type_new_array (child_type); |
801 | |
|
802 | 0 | for (i = 0; i < n_children; i++) |
803 | 0 | { |
804 | 0 | gboolean is_of_child_type = g_variant_is_of_type (children[i], child_type); |
805 | 0 | if G_UNLIKELY (!is_of_child_type) |
806 | 0 | { |
807 | 0 | while (i != 0) |
808 | 0 | g_variant_unref (my_children[--i]); |
809 | 0 | g_free (my_children); |
810 | 0 | g_return_val_if_fail (is_of_child_type, NULL); |
811 | 0 | } |
812 | 0 | my_children[i] = g_variant_ref_sink (children[i]); |
813 | 0 | trusted &= g_variant_is_trusted (children[i]); |
814 | 0 | } |
815 | | |
816 | 0 | value = g_variant_new_from_children (array_type, my_children, |
817 | 0 | n_children, trusted); |
818 | 0 | g_variant_type_free (array_type); |
819 | |
|
820 | 0 | return value; |
821 | 0 | } |
822 | | |
823 | | /*< private > |
824 | | * g_variant_make_tuple_type: |
825 | | * @children: (array length=n_children): an array of GVariant * |
826 | | * @n_children: the length of @children |
827 | | * |
828 | | * Return the type of a tuple containing @children as its items. |
829 | | **/ |
830 | | static GVariantType * |
831 | | g_variant_make_tuple_type (GVariant * const *children, |
832 | | gsize n_children) |
833 | 0 | { |
834 | 0 | const GVariantType **types; |
835 | 0 | GVariantType *type; |
836 | 0 | gsize i; |
837 | |
|
838 | 0 | types = g_new (const GVariantType *, n_children); |
839 | |
|
840 | 0 | for (i = 0; i < n_children; i++) |
841 | 0 | types[i] = g_variant_get_type (children[i]); |
842 | |
|
843 | 0 | type = g_variant_type_new_tuple (types, n_children); |
844 | 0 | g_free (types); |
845 | |
|
846 | 0 | return type; |
847 | 0 | } |
848 | | |
849 | | /** |
850 | | * g_variant_new_tuple: |
851 | | * @children: (array length=n_children): the items to make the tuple out of |
852 | | * @n_children: the length of @children |
853 | | * |
854 | | * Creates a new tuple #GVariant out of the items in @children. The |
855 | | * type is determined from the types of @children. No entry in the |
856 | | * @children array may be %NULL. |
857 | | * |
858 | | * If @n_children is 0 then the unit tuple is constructed. |
859 | | * |
860 | | * If the @children are floating references (see g_variant_ref_sink()), the |
861 | | * new instance takes ownership of them as if via g_variant_ref_sink(). |
862 | | * |
863 | | * Returns: (transfer none): a floating reference to a new #GVariant tuple |
864 | | * |
865 | | * Since: 2.24 |
866 | | **/ |
867 | | GVariant * |
868 | | g_variant_new_tuple (GVariant * const *children, |
869 | | gsize n_children) |
870 | 0 | { |
871 | 0 | GVariantType *tuple_type; |
872 | 0 | GVariant **my_children; |
873 | 0 | gboolean trusted; |
874 | 0 | GVariant *value; |
875 | 0 | gsize i; |
876 | |
|
877 | 0 | g_return_val_if_fail (n_children == 0 || children != NULL, NULL); |
878 | | |
879 | 0 | my_children = g_new (GVariant *, n_children); |
880 | 0 | trusted = TRUE; |
881 | |
|
882 | 0 | for (i = 0; i < n_children; i++) |
883 | 0 | { |
884 | 0 | my_children[i] = g_variant_ref_sink (children[i]); |
885 | 0 | trusted &= g_variant_is_trusted (children[i]); |
886 | 0 | } |
887 | |
|
888 | 0 | tuple_type = g_variant_make_tuple_type (children, n_children); |
889 | 0 | value = g_variant_new_from_children (tuple_type, my_children, |
890 | 0 | n_children, trusted); |
891 | 0 | g_variant_type_free (tuple_type); |
892 | |
|
893 | 0 | return value; |
894 | 0 | } |
895 | | |
896 | | /*< private > |
897 | | * g_variant_make_dict_entry_type: |
898 | | * @key: a #GVariant, the key |
899 | | * @val: a #GVariant, the value |
900 | | * |
901 | | * Return the type of a dictionary entry containing @key and @val as its |
902 | | * children. |
903 | | **/ |
904 | | static GVariantType * |
905 | | g_variant_make_dict_entry_type (GVariant *key, |
906 | | GVariant *val) |
907 | 0 | { |
908 | 0 | return g_variant_type_new_dict_entry (g_variant_get_type (key), |
909 | 0 | g_variant_get_type (val)); |
910 | 0 | } |
911 | | |
912 | | /** |
913 | | * g_variant_new_dict_entry: (constructor) |
914 | | * @key: a basic #GVariant, the key |
915 | | * @value: a #GVariant, the value |
916 | | * |
917 | | * Creates a new dictionary entry #GVariant. @key and @value must be |
918 | | * non-%NULL. @key must be a value of a basic type (ie: not a container). |
919 | | * |
920 | | * If the @key or @value are floating references (see g_variant_ref_sink()), |
921 | | * the new instance takes ownership of them as if via g_variant_ref_sink(). |
922 | | * |
923 | | * Returns: (transfer none): a floating reference to a new dictionary entry #GVariant |
924 | | * |
925 | | * Since: 2.24 |
926 | | **/ |
927 | | GVariant * |
928 | | g_variant_new_dict_entry (GVariant *key, |
929 | | GVariant *value) |
930 | 0 | { |
931 | 0 | GVariantType *dict_type; |
932 | 0 | GVariant **children; |
933 | 0 | gboolean trusted; |
934 | |
|
935 | 0 | g_return_val_if_fail (key != NULL && value != NULL, NULL); |
936 | 0 | g_return_val_if_fail (!g_variant_is_container (key), NULL); |
937 | | |
938 | 0 | children = g_new (GVariant *, 2); |
939 | 0 | children[0] = g_variant_ref_sink (key); |
940 | 0 | children[1] = g_variant_ref_sink (value); |
941 | 0 | trusted = g_variant_is_trusted (key) && g_variant_is_trusted (value); |
942 | |
|
943 | 0 | dict_type = g_variant_make_dict_entry_type (key, value); |
944 | 0 | value = g_variant_new_from_children (dict_type, children, 2, trusted); |
945 | 0 | g_variant_type_free (dict_type); |
946 | |
|
947 | 0 | return value; |
948 | 0 | } |
949 | | |
950 | | /** |
951 | | * g_variant_lookup: (skip) |
952 | | * @dictionary: a dictionary #GVariant |
953 | | * @key: the key to look up in the dictionary |
954 | | * @format_string: a GVariant format string |
955 | | * @...: the arguments to unpack the value into |
956 | | * |
957 | | * Looks up a value in a dictionary #GVariant. |
958 | | * |
959 | | * This function is a wrapper around g_variant_lookup_value() and |
960 | | * g_variant_get(). In the case that %NULL would have been returned, |
961 | | * this function returns %FALSE. Otherwise, it unpacks the returned |
962 | | * value and returns %TRUE. |
963 | | * |
964 | | * @format_string determines the C types that are used for unpacking |
965 | | * the values and also determines if the values are copied or borrowed, |
966 | | * see the section on |
967 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
968 | | * |
969 | | * This function is currently implemented with a linear scan. If you |
970 | | * plan to do many lookups then #GVariantDict may be more efficient. |
971 | | * |
972 | | * Returns: %TRUE if a value was unpacked |
973 | | * |
974 | | * Since: 2.28 |
975 | | */ |
976 | | gboolean |
977 | | g_variant_lookup (GVariant *dictionary, |
978 | | const gchar *key, |
979 | | const gchar *format_string, |
980 | | ...) |
981 | 0 | { |
982 | 0 | GVariantType *type; |
983 | 0 | GVariant *value; |
984 | | |
985 | | /* flatten */ |
986 | 0 | g_variant_get_data (dictionary); |
987 | |
|
988 | 0 | type = g_variant_format_string_scan_type (format_string, NULL, NULL); |
989 | 0 | value = g_variant_lookup_value (dictionary, key, type); |
990 | 0 | g_variant_type_free (type); |
991 | |
|
992 | 0 | if (value) |
993 | 0 | { |
994 | 0 | va_list ap; |
995 | |
|
996 | 0 | va_start (ap, format_string); |
997 | 0 | g_variant_get_va (value, format_string, NULL, &ap); |
998 | 0 | g_variant_unref (value); |
999 | 0 | va_end (ap); |
1000 | |
|
1001 | 0 | return TRUE; |
1002 | 0 | } |
1003 | | |
1004 | 0 | else |
1005 | 0 | return FALSE; |
1006 | 0 | } |
1007 | | |
1008 | | /** |
1009 | | * g_variant_lookup_value: |
1010 | | * @dictionary: a dictionary #GVariant |
1011 | | * @key: the key to look up in the dictionary |
1012 | | * @expected_type: (nullable): a #GVariantType, or %NULL |
1013 | | * |
1014 | | * Looks up a value in a dictionary #GVariant. |
1015 | | * |
1016 | | * This function works with dictionaries of the type a{s*} (and equally |
1017 | | * well with type a{o*}, but we only further discuss the string case |
1018 | | * for sake of clarity). |
1019 | | * |
1020 | | * In the event that @dictionary has the type a{sv}, the @expected_type |
1021 | | * string specifies what type of value is expected to be inside of the |
1022 | | * variant. If the value inside the variant has a different type then |
1023 | | * %NULL is returned. In the event that @dictionary has a value type other |
1024 | | * than v then @expected_type must directly match the value type and it is |
1025 | | * used to unpack the value directly or an error occurs. |
1026 | | * |
1027 | | * In either case, if @key is not found in @dictionary, %NULL is returned. |
1028 | | * |
1029 | | * If the key is found and the value has the correct type, it is |
1030 | | * returned. If @expected_type was specified then any non-%NULL return |
1031 | | * value will have this type. |
1032 | | * |
1033 | | * This function is currently implemented with a linear scan. If you |
1034 | | * plan to do many lookups then #GVariantDict may be more efficient. |
1035 | | * |
1036 | | * Returns: (transfer full): the value of the dictionary key, or %NULL |
1037 | | * |
1038 | | * Since: 2.28 |
1039 | | */ |
1040 | | GVariant * |
1041 | | g_variant_lookup_value (GVariant *dictionary, |
1042 | | const gchar *key, |
1043 | | const GVariantType *expected_type) |
1044 | 0 | { |
1045 | 0 | GVariantIter iter; |
1046 | 0 | GVariant *entry; |
1047 | 0 | GVariant *value; |
1048 | |
|
1049 | 0 | g_return_val_if_fail (g_variant_is_of_type (dictionary, |
1050 | 0 | G_VARIANT_TYPE ("a{s*}")) || |
1051 | 0 | g_variant_is_of_type (dictionary, |
1052 | 0 | G_VARIANT_TYPE ("a{o*}")), |
1053 | 0 | NULL); |
1054 | | |
1055 | 0 | g_variant_iter_init (&iter, dictionary); |
1056 | |
|
1057 | 0 | while ((entry = g_variant_iter_next_value (&iter))) |
1058 | 0 | { |
1059 | 0 | GVariant *entry_key; |
1060 | 0 | gboolean matches; |
1061 | |
|
1062 | 0 | entry_key = g_variant_get_child_value (entry, 0); |
1063 | 0 | matches = strcmp (g_variant_get_string (entry_key, NULL), key) == 0; |
1064 | 0 | g_variant_unref (entry_key); |
1065 | |
|
1066 | 0 | if (matches) |
1067 | 0 | break; |
1068 | | |
1069 | 0 | g_variant_unref (entry); |
1070 | 0 | } |
1071 | |
|
1072 | 0 | if (entry == NULL) |
1073 | 0 | return NULL; |
1074 | | |
1075 | 0 | value = g_variant_get_child_value (entry, 1); |
1076 | 0 | g_variant_unref (entry); |
1077 | |
|
1078 | 0 | if (g_variant_is_of_type (value, G_VARIANT_TYPE_VARIANT)) |
1079 | 0 | { |
1080 | 0 | GVariant *tmp; |
1081 | |
|
1082 | 0 | tmp = g_variant_get_variant (value); |
1083 | 0 | g_variant_unref (value); |
1084 | |
|
1085 | 0 | if (expected_type && !g_variant_is_of_type (tmp, expected_type)) |
1086 | 0 | { |
1087 | 0 | g_variant_unref (tmp); |
1088 | 0 | tmp = NULL; |
1089 | 0 | } |
1090 | |
|
1091 | 0 | value = tmp; |
1092 | 0 | } |
1093 | |
|
1094 | 0 | g_return_val_if_fail (expected_type == NULL || value == NULL || |
1095 | 0 | g_variant_is_of_type (value, expected_type), NULL); |
1096 | | |
1097 | 0 | return value; |
1098 | 0 | } |
1099 | | |
1100 | | /** |
1101 | | * g_variant_get_fixed_array: |
1102 | | * @value: a #GVariant array with fixed-sized elements |
1103 | | * @n_elements: (out): a pointer to the location to store the number of items |
1104 | | * @element_size: the size of each element |
1105 | | * |
1106 | | * Provides access to the serialized data for an array of fixed-sized |
1107 | | * items. |
1108 | | * |
1109 | | * @value must be an array with fixed-sized elements. Numeric types are |
1110 | | * fixed-size, as are tuples containing only other fixed-sized types. |
1111 | | * |
1112 | | * @element_size must be the size of a single element in the array, |
1113 | | * as given by the section on |
1114 | | * [serialized data memory][gvariant-serialized-data-memory]. |
1115 | | * |
1116 | | * In particular, arrays of these fixed-sized types can be interpreted |
1117 | | * as an array of the given C type, with @element_size set to the size |
1118 | | * the appropriate type: |
1119 | | * - %G_VARIANT_TYPE_INT16 (etc.): #gint16 (etc.) |
1120 | | * - %G_VARIANT_TYPE_BOOLEAN: #guchar (not #gboolean!) |
1121 | | * - %G_VARIANT_TYPE_BYTE: #guint8 |
1122 | | * - %G_VARIANT_TYPE_HANDLE: #guint32 |
1123 | | * - %G_VARIANT_TYPE_DOUBLE: #gdouble |
1124 | | * |
1125 | | * For example, if calling this function for an array of 32-bit integers, |
1126 | | * you might say `sizeof(gint32)`. This value isn't used except for the purpose |
1127 | | * of a double-check that the form of the serialized data matches the caller's |
1128 | | * expectation. |
1129 | | * |
1130 | | * @n_elements, which must be non-%NULL, is set equal to the number of |
1131 | | * items in the array. |
1132 | | * |
1133 | | * Returns: (array length=n_elements) (transfer none): a pointer to |
1134 | | * the fixed array |
1135 | | * |
1136 | | * Since: 2.24 |
1137 | | **/ |
1138 | | gconstpointer |
1139 | | g_variant_get_fixed_array (GVariant *value, |
1140 | | gsize *n_elements, |
1141 | | gsize element_size) |
1142 | 0 | { |
1143 | 0 | GVariantTypeInfo *array_info; |
1144 | 0 | gsize array_element_size; |
1145 | 0 | gconstpointer data; |
1146 | 0 | gsize size; |
1147 | |
|
1148 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_ARRAY, NULL); |
1149 | |
|
1150 | 0 | g_return_val_if_fail (n_elements != NULL, NULL); |
1151 | 0 | g_return_val_if_fail (element_size > 0, NULL); |
1152 | | |
1153 | 0 | array_info = g_variant_get_type_info (value); |
1154 | 0 | g_variant_type_info_query_element (array_info, NULL, &array_element_size); |
1155 | |
|
1156 | 0 | g_return_val_if_fail (array_element_size, NULL); |
1157 | | |
1158 | 0 | if G_UNLIKELY (array_element_size != element_size) |
1159 | 0 | { |
1160 | 0 | if (array_element_size) |
1161 | 0 | g_critical ("g_variant_get_fixed_array: assertion " |
1162 | 0 | "'g_variant_array_has_fixed_size (value, element_size)' " |
1163 | 0 | "failed: array size %"G_GSIZE_FORMAT" does not match " |
1164 | 0 | "given element_size %"G_GSIZE_FORMAT".", |
1165 | 0 | array_element_size, element_size); |
1166 | 0 | else |
1167 | 0 | g_critical ("g_variant_get_fixed_array: assertion " |
1168 | 0 | "'g_variant_array_has_fixed_size (value, element_size)' " |
1169 | 0 | "failed: array does not have fixed size."); |
1170 | 0 | } |
1171 | |
|
1172 | 0 | data = g_variant_get_data (value); |
1173 | 0 | size = g_variant_get_size (value); |
1174 | |
|
1175 | 0 | if (size % element_size) |
1176 | 0 | *n_elements = 0; |
1177 | 0 | else |
1178 | 0 | *n_elements = size / element_size; |
1179 | |
|
1180 | 0 | if (*n_elements) |
1181 | 0 | return data; |
1182 | | |
1183 | 0 | return NULL; |
1184 | 0 | } |
1185 | | |
1186 | | /** |
1187 | | * g_variant_new_fixed_array: |
1188 | | * @element_type: the #GVariantType of each element |
1189 | | * @elements: a pointer to the fixed array of contiguous elements |
1190 | | * @n_elements: the number of elements |
1191 | | * @element_size: the size of each element |
1192 | | * |
1193 | | * Constructs a new array #GVariant instance, where the elements are |
1194 | | * of @element_type type. |
1195 | | * |
1196 | | * @elements must be an array with fixed-sized elements. Numeric types are |
1197 | | * fixed-size as are tuples containing only other fixed-sized types. |
1198 | | * |
1199 | | * @element_size must be the size of a single element in the array. |
1200 | | * For example, if calling this function for an array of 32-bit integers, |
1201 | | * you might say sizeof(gint32). This value isn't used except for the purpose |
1202 | | * of a double-check that the form of the serialized data matches the caller's |
1203 | | * expectation. |
1204 | | * |
1205 | | * @n_elements must be the length of the @elements array. |
1206 | | * |
1207 | | * Returns: (transfer none): a floating reference to a new array #GVariant instance |
1208 | | * |
1209 | | * Since: 2.32 |
1210 | | **/ |
1211 | | GVariant * |
1212 | | g_variant_new_fixed_array (const GVariantType *element_type, |
1213 | | gconstpointer elements, |
1214 | | gsize n_elements, |
1215 | | gsize element_size) |
1216 | 0 | { |
1217 | 0 | GVariantType *array_type; |
1218 | 0 | gsize array_element_size; |
1219 | 0 | GVariantTypeInfo *array_info; |
1220 | 0 | GVariant *value; |
1221 | 0 | gpointer data; |
1222 | |
|
1223 | 0 | g_return_val_if_fail (g_variant_type_is_definite (element_type), NULL); |
1224 | 0 | g_return_val_if_fail (element_size > 0, NULL); |
1225 | | |
1226 | 0 | array_type = g_variant_type_new_array (element_type); |
1227 | 0 | array_info = g_variant_type_info_get (array_type); |
1228 | 0 | g_variant_type_info_query_element (array_info, NULL, &array_element_size); |
1229 | 0 | if G_UNLIKELY (array_element_size != element_size) |
1230 | 0 | { |
1231 | 0 | if (array_element_size) |
1232 | 0 | g_critical ("g_variant_new_fixed_array: array size %" G_GSIZE_FORMAT |
1233 | 0 | " does not match given element_size %" G_GSIZE_FORMAT ".", |
1234 | 0 | array_element_size, element_size); |
1235 | 0 | else |
1236 | 0 | g_critical ("g_variant_get_fixed_array: array does not have fixed size."); |
1237 | 0 | return NULL; |
1238 | 0 | } |
1239 | | |
1240 | 0 | data = g_memdup2 (elements, n_elements * element_size); |
1241 | 0 | value = g_variant_new_from_data (array_type, data, |
1242 | 0 | n_elements * element_size, |
1243 | 0 | FALSE, g_free, data); |
1244 | |
|
1245 | 0 | g_variant_type_free (array_type); |
1246 | 0 | g_variant_type_info_unref (array_info); |
1247 | |
|
1248 | 0 | return value; |
1249 | 0 | } |
1250 | | |
1251 | | /* String type constructor/getters/validation {{{1 */ |
1252 | | /** |
1253 | | * g_variant_new_string: |
1254 | | * @string: a normal UTF-8 nul-terminated string |
1255 | | * |
1256 | | * Creates a string #GVariant with the contents of @string. |
1257 | | * |
1258 | | * @string must be valid UTF-8, and must not be %NULL. To encode |
1259 | | * potentially-%NULL strings, use g_variant_new() with `ms` as the |
1260 | | * [format string][gvariant-format-strings-maybe-types]. |
1261 | | * |
1262 | | * Returns: (transfer none): a floating reference to a new string #GVariant instance |
1263 | | * |
1264 | | * Since: 2.24 |
1265 | | **/ |
1266 | | GVariant * |
1267 | | g_variant_new_string (const gchar *string) |
1268 | 0 | { |
1269 | 0 | g_return_val_if_fail (string != NULL, NULL); |
1270 | 0 | g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL); |
1271 | | |
1272 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_STRING, |
1273 | 0 | string, strlen (string) + 1); |
1274 | 0 | } |
1275 | | |
1276 | | /** |
1277 | | * g_variant_new_take_string: (skip) |
1278 | | * @string: a normal UTF-8 nul-terminated string |
1279 | | * |
1280 | | * Creates a string #GVariant with the contents of @string. |
1281 | | * |
1282 | | * @string must be valid UTF-8, and must not be %NULL. To encode |
1283 | | * potentially-%NULL strings, use this with g_variant_new_maybe(). |
1284 | | * |
1285 | | * This function consumes @string. g_free() will be called on @string |
1286 | | * when it is no longer required. |
1287 | | * |
1288 | | * You must not modify or access @string in any other way after passing |
1289 | | * it to this function. It is even possible that @string is immediately |
1290 | | * freed. |
1291 | | * |
1292 | | * Returns: (transfer none): a floating reference to a new string |
1293 | | * #GVariant instance |
1294 | | * |
1295 | | * Since: 2.38 |
1296 | | **/ |
1297 | | GVariant * |
1298 | | g_variant_new_take_string (gchar *string) |
1299 | 0 | { |
1300 | 0 | GVariant *value; |
1301 | 0 | GBytes *bytes; |
1302 | |
|
1303 | 0 | g_return_val_if_fail (string != NULL, NULL); |
1304 | 0 | g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL); |
1305 | | |
1306 | 0 | bytes = g_bytes_new_take (string, strlen (string) + 1); |
1307 | 0 | value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE); |
1308 | 0 | g_bytes_unref (bytes); |
1309 | |
|
1310 | 0 | return value; |
1311 | 0 | } |
1312 | | |
1313 | | /** |
1314 | | * g_variant_new_printf: (skip) |
1315 | | * @format_string: a printf-style format string |
1316 | | * @...: arguments for @format_string |
1317 | | * |
1318 | | * Creates a string-type GVariant using printf formatting. |
1319 | | * |
1320 | | * This is similar to calling g_strdup_printf() and then |
1321 | | * g_variant_new_string() but it saves a temporary variable and an |
1322 | | * unnecessary copy. |
1323 | | * |
1324 | | * Returns: (transfer none): a floating reference to a new string |
1325 | | * #GVariant instance |
1326 | | * |
1327 | | * Since: 2.38 |
1328 | | **/ |
1329 | | GVariant * |
1330 | | g_variant_new_printf (const gchar *format_string, |
1331 | | ...) |
1332 | 0 | { |
1333 | 0 | GVariant *value; |
1334 | 0 | GBytes *bytes; |
1335 | 0 | gchar *string; |
1336 | 0 | va_list ap; |
1337 | |
|
1338 | 0 | g_return_val_if_fail (format_string != NULL, NULL); |
1339 | | |
1340 | 0 | va_start (ap, format_string); |
1341 | 0 | string = g_strdup_vprintf (format_string, ap); |
1342 | 0 | va_end (ap); |
1343 | |
|
1344 | 0 | bytes = g_bytes_new_take (string, strlen (string) + 1); |
1345 | 0 | value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE); |
1346 | 0 | g_bytes_unref (bytes); |
1347 | |
|
1348 | 0 | return value; |
1349 | 0 | } |
1350 | | |
1351 | | /** |
1352 | | * g_variant_new_object_path: |
1353 | | * @object_path: a normal C nul-terminated string |
1354 | | * |
1355 | | * Creates a D-Bus object path #GVariant with the contents of @string. |
1356 | | * @string must be a valid D-Bus object path. Use |
1357 | | * g_variant_is_object_path() if you're not sure. |
1358 | | * |
1359 | | * Returns: (transfer none): a floating reference to a new object path #GVariant instance |
1360 | | * |
1361 | | * Since: 2.24 |
1362 | | **/ |
1363 | | GVariant * |
1364 | | g_variant_new_object_path (const gchar *object_path) |
1365 | 0 | { |
1366 | 0 | g_return_val_if_fail (g_variant_is_object_path (object_path), NULL); |
1367 | | |
1368 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_OBJECT_PATH, |
1369 | 0 | object_path, strlen (object_path) + 1); |
1370 | 0 | } |
1371 | | |
1372 | | /** |
1373 | | * g_variant_is_object_path: |
1374 | | * @string: a normal C nul-terminated string |
1375 | | * |
1376 | | * Determines if a given string is a valid D-Bus object path. You |
1377 | | * should ensure that a string is a valid D-Bus object path before |
1378 | | * passing it to g_variant_new_object_path(). |
1379 | | * |
1380 | | * A valid object path starts with `/` followed by zero or more |
1381 | | * sequences of characters separated by `/` characters. Each sequence |
1382 | | * must contain only the characters `[A-Z][a-z][0-9]_`. No sequence |
1383 | | * (including the one following the final `/` character) may be empty. |
1384 | | * |
1385 | | * Returns: %TRUE if @string is a D-Bus object path |
1386 | | * |
1387 | | * Since: 2.24 |
1388 | | **/ |
1389 | | gboolean |
1390 | | g_variant_is_object_path (const gchar *string) |
1391 | 0 | { |
1392 | 0 | g_return_val_if_fail (string != NULL, FALSE); |
1393 | | |
1394 | 0 | return g_variant_serialiser_is_object_path (string, strlen (string) + 1); |
1395 | 0 | } |
1396 | | |
1397 | | /** |
1398 | | * g_variant_new_signature: |
1399 | | * @signature: a normal C nul-terminated string |
1400 | | * |
1401 | | * Creates a D-Bus type signature #GVariant with the contents of |
1402 | | * @string. @string must be a valid D-Bus type signature. Use |
1403 | | * g_variant_is_signature() if you're not sure. |
1404 | | * |
1405 | | * Returns: (transfer none): a floating reference to a new signature #GVariant instance |
1406 | | * |
1407 | | * Since: 2.24 |
1408 | | **/ |
1409 | | GVariant * |
1410 | | g_variant_new_signature (const gchar *signature) |
1411 | 0 | { |
1412 | 0 | g_return_val_if_fail (g_variant_is_signature (signature), NULL); |
1413 | | |
1414 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_SIGNATURE, |
1415 | 0 | signature, strlen (signature) + 1); |
1416 | 0 | } |
1417 | | |
1418 | | /** |
1419 | | * g_variant_is_signature: |
1420 | | * @string: a normal C nul-terminated string |
1421 | | * |
1422 | | * Determines if a given string is a valid D-Bus type signature. You |
1423 | | * should ensure that a string is a valid D-Bus type signature before |
1424 | | * passing it to g_variant_new_signature(). |
1425 | | * |
1426 | | * D-Bus type signatures consist of zero or more definite #GVariantType |
1427 | | * strings in sequence. |
1428 | | * |
1429 | | * Returns: %TRUE if @string is a D-Bus type signature |
1430 | | * |
1431 | | * Since: 2.24 |
1432 | | **/ |
1433 | | gboolean |
1434 | | g_variant_is_signature (const gchar *string) |
1435 | 0 | { |
1436 | 0 | g_return_val_if_fail (string != NULL, FALSE); |
1437 | | |
1438 | 0 | return g_variant_serialiser_is_signature (string, strlen (string) + 1); |
1439 | 0 | } |
1440 | | |
1441 | | /** |
1442 | | * g_variant_get_string: |
1443 | | * @value: a string #GVariant instance |
1444 | | * @length: (optional) (default 0) (out): a pointer to a #gsize, |
1445 | | * to store the length |
1446 | | * |
1447 | | * Returns the string value of a #GVariant instance with a string |
1448 | | * type. This includes the types %G_VARIANT_TYPE_STRING, |
1449 | | * %G_VARIANT_TYPE_OBJECT_PATH and %G_VARIANT_TYPE_SIGNATURE. |
1450 | | * |
1451 | | * The string will always be UTF-8 encoded, will never be %NULL, and will never |
1452 | | * contain nul bytes. |
1453 | | * |
1454 | | * If @length is non-%NULL then the length of the string (in bytes) is |
1455 | | * returned there. For trusted values, this information is already |
1456 | | * known. Untrusted values will be validated and, if valid, a strlen() will be |
1457 | | * performed. If invalid, a default value will be returned — for |
1458 | | * %G_VARIANT_TYPE_OBJECT_PATH, this is `"/"`, and for other types it is the |
1459 | | * empty string. |
1460 | | * |
1461 | | * It is an error to call this function with a @value of any type |
1462 | | * other than those three. |
1463 | | * |
1464 | | * The return value remains valid as long as @value exists. |
1465 | | * |
1466 | | * Returns: (transfer none): the constant string, UTF-8 encoded |
1467 | | * |
1468 | | * Since: 2.24 |
1469 | | **/ |
1470 | | const gchar * |
1471 | | g_variant_get_string (GVariant *value, |
1472 | | gsize *length) |
1473 | 0 | { |
1474 | 0 | gconstpointer data; |
1475 | 0 | gsize size; |
1476 | |
|
1477 | 0 | g_return_val_if_fail (value != NULL, NULL); |
1478 | 0 | g_return_val_if_fail ( |
1479 | 0 | g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) || |
1480 | 0 | g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH) || |
1481 | 0 | g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE), NULL); |
1482 | | |
1483 | 0 | data = g_variant_get_data (value); |
1484 | 0 | size = g_variant_get_size (value); |
1485 | |
|
1486 | 0 | if (!g_variant_is_trusted (value)) |
1487 | 0 | { |
1488 | 0 | switch (g_variant_classify (value)) |
1489 | 0 | { |
1490 | 0 | case G_VARIANT_CLASS_STRING: |
1491 | 0 | if (g_variant_serialiser_is_string (data, size)) |
1492 | 0 | break; |
1493 | | |
1494 | 0 | data = ""; |
1495 | 0 | size = 1; |
1496 | 0 | break; |
1497 | | |
1498 | 0 | case G_VARIANT_CLASS_OBJECT_PATH: |
1499 | 0 | if (g_variant_serialiser_is_object_path (data, size)) |
1500 | 0 | break; |
1501 | | |
1502 | 0 | data = "/"; |
1503 | 0 | size = 2; |
1504 | 0 | break; |
1505 | | |
1506 | 0 | case G_VARIANT_CLASS_SIGNATURE: |
1507 | 0 | if (g_variant_serialiser_is_signature (data, size)) |
1508 | 0 | break; |
1509 | | |
1510 | 0 | data = ""; |
1511 | 0 | size = 1; |
1512 | 0 | break; |
1513 | | |
1514 | 0 | default: |
1515 | 0 | g_assert_not_reached (); |
1516 | 0 | } |
1517 | 0 | } |
1518 | | |
1519 | 0 | if (length) |
1520 | 0 | *length = size - 1; |
1521 | |
|
1522 | 0 | return data; |
1523 | 0 | } |
1524 | | |
1525 | | /** |
1526 | | * g_variant_dup_string: |
1527 | | * @value: a string #GVariant instance |
1528 | | * @length: (out): a pointer to a #gsize, to store the length |
1529 | | * |
1530 | | * Similar to g_variant_get_string() except that instead of returning |
1531 | | * a constant string, the string is duplicated. |
1532 | | * |
1533 | | * The string will always be UTF-8 encoded. |
1534 | | * |
1535 | | * The return value must be freed using g_free(). |
1536 | | * |
1537 | | * Returns: (transfer full): a newly allocated string, UTF-8 encoded |
1538 | | * |
1539 | | * Since: 2.24 |
1540 | | **/ |
1541 | | gchar * |
1542 | | g_variant_dup_string (GVariant *value, |
1543 | | gsize *length) |
1544 | 0 | { |
1545 | 0 | return g_strdup (g_variant_get_string (value, length)); |
1546 | 0 | } |
1547 | | |
1548 | | /** |
1549 | | * g_variant_new_strv: |
1550 | | * @strv: (array length=length) (element-type utf8): an array of strings |
1551 | | * @length: the length of @strv, or -1 |
1552 | | * |
1553 | | * Constructs an array of strings #GVariant from the given array of |
1554 | | * strings. |
1555 | | * |
1556 | | * If @length is -1 then @strv is %NULL-terminated. |
1557 | | * |
1558 | | * Returns: (transfer none): a new floating #GVariant instance |
1559 | | * |
1560 | | * Since: 2.24 |
1561 | | **/ |
1562 | | GVariant * |
1563 | | g_variant_new_strv (const gchar * const *strv, |
1564 | | gssize length) |
1565 | 0 | { |
1566 | 0 | GVariant **strings; |
1567 | 0 | gsize i, length_unsigned; |
1568 | |
|
1569 | 0 | g_return_val_if_fail (length == 0 || strv != NULL, NULL); |
1570 | | |
1571 | 0 | if (length < 0) |
1572 | 0 | length = g_strv_length ((gchar **) strv); |
1573 | 0 | length_unsigned = length; |
1574 | |
|
1575 | 0 | strings = g_new (GVariant *, length_unsigned); |
1576 | 0 | for (i = 0; i < length_unsigned; i++) |
1577 | 0 | strings[i] = g_variant_ref_sink (g_variant_new_string (strv[i])); |
1578 | |
|
1579 | 0 | return g_variant_new_from_children (G_VARIANT_TYPE_STRING_ARRAY, |
1580 | 0 | strings, length_unsigned, TRUE); |
1581 | 0 | } |
1582 | | |
1583 | | /** |
1584 | | * g_variant_get_strv: |
1585 | | * @value: an array of strings #GVariant |
1586 | | * @length: (out) (optional): the length of the result, or %NULL |
1587 | | * |
1588 | | * Gets the contents of an array of strings #GVariant. This call |
1589 | | * makes a shallow copy; the return result should be released with |
1590 | | * g_free(), but the individual strings must not be modified. |
1591 | | * |
1592 | | * If @length is non-%NULL then the number of elements in the result |
1593 | | * is stored there. In any case, the resulting array will be |
1594 | | * %NULL-terminated. |
1595 | | * |
1596 | | * For an empty array, @length will be set to 0 and a pointer to a |
1597 | | * %NULL pointer will be returned. |
1598 | | * |
1599 | | * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings |
1600 | | * |
1601 | | * Since: 2.24 |
1602 | | **/ |
1603 | | const gchar ** |
1604 | | g_variant_get_strv (GVariant *value, |
1605 | | gsize *length) |
1606 | 0 | { |
1607 | 0 | const gchar **strv; |
1608 | 0 | gsize n; |
1609 | 0 | gsize i; |
1610 | |
|
1611 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_STRING_ARRAY, NULL); |
1612 | |
|
1613 | 0 | g_variant_get_data (value); |
1614 | 0 | n = g_variant_n_children (value); |
1615 | 0 | strv = g_new (const gchar *, n + 1); |
1616 | |
|
1617 | 0 | for (i = 0; i < n; i++) |
1618 | 0 | { |
1619 | 0 | GVariant *string; |
1620 | |
|
1621 | 0 | string = g_variant_get_child_value (value, i); |
1622 | 0 | strv[i] = g_variant_get_string (string, NULL); |
1623 | 0 | g_variant_unref (string); |
1624 | 0 | } |
1625 | 0 | strv[i] = NULL; |
1626 | |
|
1627 | 0 | if (length) |
1628 | 0 | *length = n; |
1629 | |
|
1630 | 0 | return strv; |
1631 | 0 | } |
1632 | | |
1633 | | /** |
1634 | | * g_variant_dup_strv: |
1635 | | * @value: an array of strings #GVariant |
1636 | | * @length: (out) (optional): the length of the result, or %NULL |
1637 | | * |
1638 | | * Gets the contents of an array of strings #GVariant. This call |
1639 | | * makes a deep copy; the return result should be released with |
1640 | | * g_strfreev(). |
1641 | | * |
1642 | | * If @length is non-%NULL then the number of elements in the result |
1643 | | * is stored there. In any case, the resulting array will be |
1644 | | * %NULL-terminated. |
1645 | | * |
1646 | | * For an empty array, @length will be set to 0 and a pointer to a |
1647 | | * %NULL pointer will be returned. |
1648 | | * |
1649 | | * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings |
1650 | | * |
1651 | | * Since: 2.24 |
1652 | | **/ |
1653 | | gchar ** |
1654 | | g_variant_dup_strv (GVariant *value, |
1655 | | gsize *length) |
1656 | 0 | { |
1657 | 0 | gchar **strv; |
1658 | 0 | gsize n; |
1659 | 0 | gsize i; |
1660 | |
|
1661 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_STRING_ARRAY, NULL); |
1662 | |
|
1663 | 0 | n = g_variant_n_children (value); |
1664 | 0 | strv = g_new (gchar *, n + 1); |
1665 | |
|
1666 | 0 | for (i = 0; i < n; i++) |
1667 | 0 | { |
1668 | 0 | GVariant *string; |
1669 | |
|
1670 | 0 | string = g_variant_get_child_value (value, i); |
1671 | 0 | strv[i] = g_variant_dup_string (string, NULL); |
1672 | 0 | g_variant_unref (string); |
1673 | 0 | } |
1674 | 0 | strv[i] = NULL; |
1675 | |
|
1676 | 0 | if (length) |
1677 | 0 | *length = n; |
1678 | |
|
1679 | 0 | return strv; |
1680 | 0 | } |
1681 | | |
1682 | | /** |
1683 | | * g_variant_new_objv: |
1684 | | * @strv: (array length=length) (element-type utf8): an array of strings |
1685 | | * @length: the length of @strv, or -1 |
1686 | | * |
1687 | | * Constructs an array of object paths #GVariant from the given array of |
1688 | | * strings. |
1689 | | * |
1690 | | * Each string must be a valid #GVariant object path; see |
1691 | | * g_variant_is_object_path(). |
1692 | | * |
1693 | | * If @length is -1 then @strv is %NULL-terminated. |
1694 | | * |
1695 | | * Returns: (transfer none): a new floating #GVariant instance |
1696 | | * |
1697 | | * Since: 2.30 |
1698 | | **/ |
1699 | | GVariant * |
1700 | | g_variant_new_objv (const gchar * const *strv, |
1701 | | gssize length) |
1702 | 0 | { |
1703 | 0 | GVariant **strings; |
1704 | 0 | gsize i, length_unsigned; |
1705 | |
|
1706 | 0 | g_return_val_if_fail (length == 0 || strv != NULL, NULL); |
1707 | | |
1708 | 0 | if (length < 0) |
1709 | 0 | length = g_strv_length ((gchar **) strv); |
1710 | 0 | length_unsigned = length; |
1711 | |
|
1712 | 0 | strings = g_new (GVariant *, length_unsigned); |
1713 | 0 | for (i = 0; i < length_unsigned; i++) |
1714 | 0 | strings[i] = g_variant_ref_sink (g_variant_new_object_path (strv[i])); |
1715 | |
|
1716 | 0 | return g_variant_new_from_children (G_VARIANT_TYPE_OBJECT_PATH_ARRAY, |
1717 | 0 | strings, length_unsigned, TRUE); |
1718 | 0 | } |
1719 | | |
1720 | | /** |
1721 | | * g_variant_get_objv: |
1722 | | * @value: an array of object paths #GVariant |
1723 | | * @length: (out) (optional): the length of the result, or %NULL |
1724 | | * |
1725 | | * Gets the contents of an array of object paths #GVariant. This call |
1726 | | * makes a shallow copy; the return result should be released with |
1727 | | * g_free(), but the individual strings must not be modified. |
1728 | | * |
1729 | | * If @length is non-%NULL then the number of elements in the result |
1730 | | * is stored there. In any case, the resulting array will be |
1731 | | * %NULL-terminated. |
1732 | | * |
1733 | | * For an empty array, @length will be set to 0 and a pointer to a |
1734 | | * %NULL pointer will be returned. |
1735 | | * |
1736 | | * Returns: (array length=length zero-terminated=1) (transfer container): an array of constant strings |
1737 | | * |
1738 | | * Since: 2.30 |
1739 | | **/ |
1740 | | const gchar ** |
1741 | | g_variant_get_objv (GVariant *value, |
1742 | | gsize *length) |
1743 | 0 | { |
1744 | 0 | const gchar **strv; |
1745 | 0 | gsize n; |
1746 | 0 | gsize i; |
1747 | |
|
1748 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, NULL); |
1749 | |
|
1750 | 0 | g_variant_get_data (value); |
1751 | 0 | n = g_variant_n_children (value); |
1752 | 0 | strv = g_new (const gchar *, n + 1); |
1753 | |
|
1754 | 0 | for (i = 0; i < n; i++) |
1755 | 0 | { |
1756 | 0 | GVariant *string; |
1757 | |
|
1758 | 0 | string = g_variant_get_child_value (value, i); |
1759 | 0 | strv[i] = g_variant_get_string (string, NULL); |
1760 | 0 | g_variant_unref (string); |
1761 | 0 | } |
1762 | 0 | strv[i] = NULL; |
1763 | |
|
1764 | 0 | if (length) |
1765 | 0 | *length = n; |
1766 | |
|
1767 | 0 | return strv; |
1768 | 0 | } |
1769 | | |
1770 | | /** |
1771 | | * g_variant_dup_objv: |
1772 | | * @value: an array of object paths #GVariant |
1773 | | * @length: (out) (optional): the length of the result, or %NULL |
1774 | | * |
1775 | | * Gets the contents of an array of object paths #GVariant. This call |
1776 | | * makes a deep copy; the return result should be released with |
1777 | | * g_strfreev(). |
1778 | | * |
1779 | | * If @length is non-%NULL then the number of elements in the result |
1780 | | * is stored there. In any case, the resulting array will be |
1781 | | * %NULL-terminated. |
1782 | | * |
1783 | | * For an empty array, @length will be set to 0 and a pointer to a |
1784 | | * %NULL pointer will be returned. |
1785 | | * |
1786 | | * Returns: (array length=length zero-terminated=1) (transfer full): an array of strings |
1787 | | * |
1788 | | * Since: 2.30 |
1789 | | **/ |
1790 | | gchar ** |
1791 | | g_variant_dup_objv (GVariant *value, |
1792 | | gsize *length) |
1793 | 0 | { |
1794 | 0 | gchar **strv; |
1795 | 0 | gsize n; |
1796 | 0 | gsize i; |
1797 | |
|
1798 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY, NULL); |
1799 | |
|
1800 | 0 | n = g_variant_n_children (value); |
1801 | 0 | strv = g_new (gchar *, n + 1); |
1802 | |
|
1803 | 0 | for (i = 0; i < n; i++) |
1804 | 0 | { |
1805 | 0 | GVariant *string; |
1806 | |
|
1807 | 0 | string = g_variant_get_child_value (value, i); |
1808 | 0 | strv[i] = g_variant_dup_string (string, NULL); |
1809 | 0 | g_variant_unref (string); |
1810 | 0 | } |
1811 | 0 | strv[i] = NULL; |
1812 | |
|
1813 | 0 | if (length) |
1814 | 0 | *length = n; |
1815 | |
|
1816 | 0 | return strv; |
1817 | 0 | } |
1818 | | |
1819 | | |
1820 | | /** |
1821 | | * g_variant_new_bytestring: |
1822 | | * @string: (array zero-terminated=1) (element-type guint8): a normal |
1823 | | * nul-terminated string in no particular encoding |
1824 | | * |
1825 | | * Creates an array-of-bytes #GVariant with the contents of @string. |
1826 | | * This function is just like g_variant_new_string() except that the |
1827 | | * string need not be valid UTF-8. |
1828 | | * |
1829 | | * The nul terminator character at the end of the string is stored in |
1830 | | * the array. |
1831 | | * |
1832 | | * Returns: (transfer none): a floating reference to a new bytestring #GVariant instance |
1833 | | * |
1834 | | * Since: 2.26 |
1835 | | **/ |
1836 | | GVariant * |
1837 | | g_variant_new_bytestring (const gchar *string) |
1838 | 0 | { |
1839 | 0 | g_return_val_if_fail (string != NULL, NULL); |
1840 | | |
1841 | 0 | return g_variant_new_from_trusted (G_VARIANT_TYPE_BYTESTRING, |
1842 | 0 | string, strlen (string) + 1); |
1843 | 0 | } |
1844 | | |
1845 | | /** |
1846 | | * g_variant_get_bytestring: |
1847 | | * @value: an array-of-bytes #GVariant instance |
1848 | | * |
1849 | | * Returns the string value of a #GVariant instance with an |
1850 | | * array-of-bytes type. The string has no particular encoding. |
1851 | | * |
1852 | | * If the array does not end with a nul terminator character, the empty |
1853 | | * string is returned. For this reason, you can always trust that a |
1854 | | * non-%NULL nul-terminated string will be returned by this function. |
1855 | | * |
1856 | | * If the array contains a nul terminator character somewhere other than |
1857 | | * the last byte then the returned string is the string, up to the first |
1858 | | * such nul character. |
1859 | | * |
1860 | | * g_variant_get_fixed_array() should be used instead if the array contains |
1861 | | * arbitrary data that could not be nul-terminated or could contain nul bytes. |
1862 | | * |
1863 | | * It is an error to call this function with a @value that is not an |
1864 | | * array of bytes. |
1865 | | * |
1866 | | * The return value remains valid as long as @value exists. |
1867 | | * |
1868 | | * Returns: (transfer none) (array zero-terminated=1) (element-type guint8): |
1869 | | * the constant string |
1870 | | * |
1871 | | * Since: 2.26 |
1872 | | **/ |
1873 | | const gchar * |
1874 | | g_variant_get_bytestring (GVariant *value) |
1875 | 0 | { |
1876 | 0 | const gchar *string; |
1877 | 0 | gsize size; |
1878 | |
|
1879 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING, NULL); |
1880 | | |
1881 | | /* Won't be NULL since this is an array type */ |
1882 | 0 | string = g_variant_get_data (value); |
1883 | 0 | size = g_variant_get_size (value); |
1884 | |
|
1885 | 0 | if (size && string[size - 1] == '\0') |
1886 | 0 | return string; |
1887 | 0 | else |
1888 | 0 | return ""; |
1889 | 0 | } |
1890 | | |
1891 | | /** |
1892 | | * g_variant_dup_bytestring: |
1893 | | * @value: an array-of-bytes #GVariant instance |
1894 | | * @length: (out) (optional) (default NULL): a pointer to a #gsize, to store |
1895 | | * the length (not including the nul terminator) |
1896 | | * |
1897 | | * Similar to g_variant_get_bytestring() except that instead of |
1898 | | * returning a constant string, the string is duplicated. |
1899 | | * |
1900 | | * The return value must be freed using g_free(). |
1901 | | * |
1902 | | * Returns: (transfer full) (array zero-terminated=1 length=length) (element-type guint8): |
1903 | | * a newly allocated string |
1904 | | * |
1905 | | * Since: 2.26 |
1906 | | **/ |
1907 | | gchar * |
1908 | | g_variant_dup_bytestring (GVariant *value, |
1909 | | gsize *length) |
1910 | 0 | { |
1911 | 0 | const gchar *original = g_variant_get_bytestring (value); |
1912 | 0 | gsize size; |
1913 | | |
1914 | | /* don't crash in case get_bytestring() had an assert failure */ |
1915 | 0 | if (original == NULL) |
1916 | 0 | return NULL; |
1917 | | |
1918 | 0 | size = strlen (original); |
1919 | |
|
1920 | 0 | if (length) |
1921 | 0 | *length = size; |
1922 | |
|
1923 | 0 | return g_memdup2 (original, size + 1); |
1924 | 0 | } |
1925 | | |
1926 | | /** |
1927 | | * g_variant_new_bytestring_array: |
1928 | | * @strv: (array length=length): an array of strings |
1929 | | * @length: the length of @strv, or -1 |
1930 | | * |
1931 | | * Constructs an array of bytestring #GVariant from the given array of |
1932 | | * strings. |
1933 | | * |
1934 | | * If @length is -1 then @strv is %NULL-terminated. |
1935 | | * |
1936 | | * Returns: (transfer none): a new floating #GVariant instance |
1937 | | * |
1938 | | * Since: 2.26 |
1939 | | **/ |
1940 | | GVariant * |
1941 | | g_variant_new_bytestring_array (const gchar * const *strv, |
1942 | | gssize length) |
1943 | 0 | { |
1944 | 0 | GVariant **strings; |
1945 | 0 | gsize i, length_unsigned; |
1946 | |
|
1947 | 0 | g_return_val_if_fail (length == 0 || strv != NULL, NULL); |
1948 | | |
1949 | 0 | if (length < 0) |
1950 | 0 | length = g_strv_length ((gchar **) strv); |
1951 | 0 | length_unsigned = length; |
1952 | |
|
1953 | 0 | strings = g_new (GVariant *, length_unsigned); |
1954 | 0 | for (i = 0; i < length_unsigned; i++) |
1955 | 0 | strings[i] = g_variant_ref_sink (g_variant_new_bytestring (strv[i])); |
1956 | |
|
1957 | 0 | return g_variant_new_from_children (G_VARIANT_TYPE_BYTESTRING_ARRAY, |
1958 | 0 | strings, length_unsigned, TRUE); |
1959 | 0 | } |
1960 | | |
1961 | | /** |
1962 | | * g_variant_get_bytestring_array: |
1963 | | * @value: an array of array of bytes #GVariant ('aay') |
1964 | | * @length: (out) (optional): the length of the result, or %NULL |
1965 | | * |
1966 | | * Gets the contents of an array of array of bytes #GVariant. This call |
1967 | | * makes a shallow copy; the return result should be released with |
1968 | | * g_free(), but the individual strings must not be modified. |
1969 | | * |
1970 | | * If @length is non-%NULL then the number of elements in the result is |
1971 | | * stored there. In any case, the resulting array will be |
1972 | | * %NULL-terminated. |
1973 | | * |
1974 | | * For an empty array, @length will be set to 0 and a pointer to a |
1975 | | * %NULL pointer will be returned. |
1976 | | * |
1977 | | * Returns: (array length=length) (transfer container): an array of constant strings |
1978 | | * |
1979 | | * Since: 2.26 |
1980 | | **/ |
1981 | | const gchar ** |
1982 | | g_variant_get_bytestring_array (GVariant *value, |
1983 | | gsize *length) |
1984 | 0 | { |
1985 | 0 | const gchar **strv; |
1986 | 0 | gsize n; |
1987 | 0 | gsize i; |
1988 | |
|
1989 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL); |
1990 | |
|
1991 | 0 | g_variant_get_data (value); |
1992 | 0 | n = g_variant_n_children (value); |
1993 | 0 | strv = g_new (const gchar *, n + 1); |
1994 | |
|
1995 | 0 | for (i = 0; i < n; i++) |
1996 | 0 | { |
1997 | 0 | GVariant *string; |
1998 | |
|
1999 | 0 | string = g_variant_get_child_value (value, i); |
2000 | 0 | strv[i] = g_variant_get_bytestring (string); |
2001 | 0 | g_variant_unref (string); |
2002 | 0 | } |
2003 | 0 | strv[i] = NULL; |
2004 | |
|
2005 | 0 | if (length) |
2006 | 0 | *length = n; |
2007 | |
|
2008 | 0 | return strv; |
2009 | 0 | } |
2010 | | |
2011 | | /** |
2012 | | * g_variant_dup_bytestring_array: |
2013 | | * @value: an array of array of bytes #GVariant ('aay') |
2014 | | * @length: (out) (optional): the length of the result, or %NULL |
2015 | | * |
2016 | | * Gets the contents of an array of array of bytes #GVariant. This call |
2017 | | * makes a deep copy; the return result should be released with |
2018 | | * g_strfreev(). |
2019 | | * |
2020 | | * If @length is non-%NULL then the number of elements in the result is |
2021 | | * stored there. In any case, the resulting array will be |
2022 | | * %NULL-terminated. |
2023 | | * |
2024 | | * For an empty array, @length will be set to 0 and a pointer to a |
2025 | | * %NULL pointer will be returned. |
2026 | | * |
2027 | | * Returns: (array length=length) (transfer full): an array of strings |
2028 | | * |
2029 | | * Since: 2.26 |
2030 | | **/ |
2031 | | gchar ** |
2032 | | g_variant_dup_bytestring_array (GVariant *value, |
2033 | | gsize *length) |
2034 | 0 | { |
2035 | 0 | gchar **strv; |
2036 | 0 | gsize n; |
2037 | 0 | gsize i; |
2038 | |
|
2039 | 0 | TYPE_CHECK (value, G_VARIANT_TYPE_BYTESTRING_ARRAY, NULL); |
2040 | |
|
2041 | 0 | g_variant_get_data (value); |
2042 | 0 | n = g_variant_n_children (value); |
2043 | 0 | strv = g_new (gchar *, n + 1); |
2044 | |
|
2045 | 0 | for (i = 0; i < n; i++) |
2046 | 0 | { |
2047 | 0 | GVariant *string; |
2048 | |
|
2049 | 0 | string = g_variant_get_child_value (value, i); |
2050 | 0 | strv[i] = g_variant_dup_bytestring (string, NULL); |
2051 | 0 | g_variant_unref (string); |
2052 | 0 | } |
2053 | 0 | strv[i] = NULL; |
2054 | |
|
2055 | 0 | if (length) |
2056 | 0 | *length = n; |
2057 | |
|
2058 | 0 | return strv; |
2059 | 0 | } |
2060 | | |
2061 | | /* Type checking and querying {{{1 */ |
2062 | | /** |
2063 | | * g_variant_get_type: |
2064 | | * @value: a #GVariant |
2065 | | * |
2066 | | * Determines the type of @value. |
2067 | | * |
2068 | | * The return value is valid for the lifetime of @value and must not |
2069 | | * be freed. |
2070 | | * |
2071 | | * Returns: a #GVariantType |
2072 | | * |
2073 | | * Since: 2.24 |
2074 | | **/ |
2075 | | const GVariantType * |
2076 | | g_variant_get_type (GVariant *value) |
2077 | 0 | { |
2078 | 0 | GVariantTypeInfo *type_info; |
2079 | |
|
2080 | 0 | g_return_val_if_fail (value != NULL, NULL); |
2081 | | |
2082 | 0 | type_info = g_variant_get_type_info (value); |
2083 | |
|
2084 | 0 | return (GVariantType *) g_variant_type_info_get_type_string (type_info); |
2085 | 0 | } |
2086 | | |
2087 | | /** |
2088 | | * g_variant_get_type_string: |
2089 | | * @value: a #GVariant |
2090 | | * |
2091 | | * Returns the type string of @value. Unlike the result of calling |
2092 | | * g_variant_type_peek_string(), this string is nul-terminated. This |
2093 | | * string belongs to #GVariant and must not be freed. |
2094 | | * |
2095 | | * Returns: the type string for the type of @value |
2096 | | * |
2097 | | * Since: 2.24 |
2098 | | **/ |
2099 | | const gchar * |
2100 | | g_variant_get_type_string (GVariant *value) |
2101 | 0 | { |
2102 | 0 | GVariantTypeInfo *type_info; |
2103 | |
|
2104 | 0 | g_return_val_if_fail (value != NULL, NULL); |
2105 | | |
2106 | 0 | type_info = g_variant_get_type_info (value); |
2107 | |
|
2108 | 0 | return g_variant_type_info_get_type_string (type_info); |
2109 | 0 | } |
2110 | | |
2111 | | /** |
2112 | | * g_variant_is_of_type: |
2113 | | * @value: a #GVariant instance |
2114 | | * @type: a #GVariantType |
2115 | | * |
2116 | | * Checks if a value has a type matching the provided type. |
2117 | | * |
2118 | | * Returns: %TRUE if the type of @value matches @type |
2119 | | * |
2120 | | * Since: 2.24 |
2121 | | **/ |
2122 | | gboolean |
2123 | | g_variant_is_of_type (GVariant *value, |
2124 | | const GVariantType *type) |
2125 | 0 | { |
2126 | 0 | return g_variant_type_is_subtype_of (g_variant_get_type (value), type); |
2127 | 0 | } |
2128 | | |
2129 | | /** |
2130 | | * g_variant_is_container: |
2131 | | * @value: a #GVariant instance |
2132 | | * |
2133 | | * Checks if @value is a container. |
2134 | | * |
2135 | | * Returns: %TRUE if @value is a container |
2136 | | * |
2137 | | * Since: 2.24 |
2138 | | */ |
2139 | | gboolean |
2140 | | g_variant_is_container (GVariant *value) |
2141 | 0 | { |
2142 | 0 | return g_variant_type_is_container (g_variant_get_type (value)); |
2143 | 0 | } |
2144 | | |
2145 | | |
2146 | | /** |
2147 | | * g_variant_classify: |
2148 | | * @value: a #GVariant |
2149 | | * |
2150 | | * Classifies @value according to its top-level type. |
2151 | | * |
2152 | | * Returns: the #GVariantClass of @value |
2153 | | * |
2154 | | * Since: 2.24 |
2155 | | **/ |
2156 | | /** |
2157 | | * GVariantClass: |
2158 | | * @G_VARIANT_CLASS_BOOLEAN: The #GVariant is a boolean. |
2159 | | * @G_VARIANT_CLASS_BYTE: The #GVariant is a byte. |
2160 | | * @G_VARIANT_CLASS_INT16: The #GVariant is a signed 16 bit integer. |
2161 | | * @G_VARIANT_CLASS_UINT16: The #GVariant is an unsigned 16 bit integer. |
2162 | | * @G_VARIANT_CLASS_INT32: The #GVariant is a signed 32 bit integer. |
2163 | | * @G_VARIANT_CLASS_UINT32: The #GVariant is an unsigned 32 bit integer. |
2164 | | * @G_VARIANT_CLASS_INT64: The #GVariant is a signed 64 bit integer. |
2165 | | * @G_VARIANT_CLASS_UINT64: The #GVariant is an unsigned 64 bit integer. |
2166 | | * @G_VARIANT_CLASS_HANDLE: The #GVariant is a file handle index. |
2167 | | * @G_VARIANT_CLASS_DOUBLE: The #GVariant is a double precision floating |
2168 | | * point value. |
2169 | | * @G_VARIANT_CLASS_STRING: The #GVariant is a normal string. |
2170 | | * @G_VARIANT_CLASS_OBJECT_PATH: The #GVariant is a D-Bus object path |
2171 | | * string. |
2172 | | * @G_VARIANT_CLASS_SIGNATURE: The #GVariant is a D-Bus signature string. |
2173 | | * @G_VARIANT_CLASS_VARIANT: The #GVariant is a variant. |
2174 | | * @G_VARIANT_CLASS_MAYBE: The #GVariant is a maybe-typed value. |
2175 | | * @G_VARIANT_CLASS_ARRAY: The #GVariant is an array. |
2176 | | * @G_VARIANT_CLASS_TUPLE: The #GVariant is a tuple. |
2177 | | * @G_VARIANT_CLASS_DICT_ENTRY: The #GVariant is a dictionary entry. |
2178 | | * |
2179 | | * The range of possible top-level types of #GVariant instances. |
2180 | | * |
2181 | | * Since: 2.24 |
2182 | | **/ |
2183 | | GVariantClass |
2184 | | g_variant_classify (GVariant *value) |
2185 | 0 | { |
2186 | 0 | g_return_val_if_fail (value != NULL, 0); |
2187 | | |
2188 | 0 | return *g_variant_get_type_string (value); |
2189 | 0 | } |
2190 | | |
2191 | | /* Pretty printer {{{1 */ |
2192 | | /* This function is not introspectable because if @string is NULL, |
2193 | | @returns is (transfer full), otherwise it is (transfer none), which |
2194 | | is not supported by GObjectIntrospection */ |
2195 | | /** |
2196 | | * g_variant_print_string: (skip) |
2197 | | * @value: a #GVariant |
2198 | | * @string: (nullable) (default NULL): a #GString, or %NULL |
2199 | | * @type_annotate: %TRUE if type information should be included in |
2200 | | * the output |
2201 | | * |
2202 | | * Behaves as g_variant_print(), but operates on a #GString. |
2203 | | * |
2204 | | * If @string is non-%NULL then it is appended to and returned. Else, |
2205 | | * a new empty #GString is allocated and it is returned. |
2206 | | * |
2207 | | * Returns: a #GString containing the string |
2208 | | * |
2209 | | * Since: 2.24 |
2210 | | **/ |
2211 | | GString * |
2212 | | g_variant_print_string (GVariant *value, |
2213 | | GString *string, |
2214 | | gboolean type_annotate) |
2215 | 0 | { |
2216 | 0 | const gchar *value_type_string = g_variant_get_type_string (value); |
2217 | |
|
2218 | 0 | if G_UNLIKELY (string == NULL) |
2219 | 0 | string = g_string_new (NULL); |
2220 | |
|
2221 | 0 | switch (value_type_string[0]) |
2222 | 0 | { |
2223 | 0 | case G_VARIANT_CLASS_MAYBE: |
2224 | 0 | if (type_annotate) |
2225 | 0 | g_string_append_printf (string, "@%s ", value_type_string); |
2226 | |
|
2227 | 0 | if (g_variant_n_children (value)) |
2228 | 0 | { |
2229 | 0 | const GVariantType *base_type; |
2230 | 0 | guint i, depth; |
2231 | 0 | GVariant *element = NULL; |
2232 | | |
2233 | | /* Nested maybes: |
2234 | | * |
2235 | | * Consider the case of the type "mmi". In this case we could |
2236 | | * write "just just 4", but "4" alone is totally unambiguous, |
2237 | | * so we try to drop "just" where possible. |
2238 | | * |
2239 | | * We have to be careful not to always drop "just", though, |
2240 | | * since "nothing" needs to be distinguishable from "just |
2241 | | * nothing". The case where we need to ensure we keep the |
2242 | | * "just" is actually exactly the case where we have a nested |
2243 | | * Nothing. |
2244 | | * |
2245 | | * Search for the nested Nothing, to save a lot of recursion if there |
2246 | | * are multiple levels of maybes. |
2247 | | */ |
2248 | 0 | for (depth = 0, base_type = g_variant_get_type (value); |
2249 | 0 | g_variant_type_is_maybe (base_type); |
2250 | 0 | depth++, base_type = g_variant_type_element (base_type)); |
2251 | |
|
2252 | 0 | element = g_variant_ref (value); |
2253 | 0 | for (i = 0; i < depth && element != NULL; i++) |
2254 | 0 | { |
2255 | 0 | GVariant *new_element = g_variant_n_children (element) ? g_variant_get_child_value (element, 0) : NULL; |
2256 | 0 | g_variant_unref (element); |
2257 | 0 | element = g_steal_pointer (&new_element); |
2258 | 0 | } |
2259 | |
|
2260 | 0 | if (element == NULL) |
2261 | 0 | { |
2262 | | /* One of the maybes was Nothing, so print out the right number of |
2263 | | * justs. */ |
2264 | 0 | for (; i > 1; i--) |
2265 | 0 | g_string_append (string, "just "); |
2266 | 0 | g_string_append (string, "nothing"); |
2267 | 0 | } |
2268 | 0 | else |
2269 | 0 | { |
2270 | | /* There are no Nothings, so print out the child with no prefixes. */ |
2271 | 0 | g_variant_print_string (element, string, FALSE); |
2272 | 0 | } |
2273 | |
|
2274 | 0 | g_clear_pointer (&element, g_variant_unref); |
2275 | 0 | } |
2276 | 0 | else |
2277 | 0 | g_string_append (string, "nothing"); |
2278 | |
|
2279 | 0 | break; |
2280 | | |
2281 | 0 | case G_VARIANT_CLASS_ARRAY: |
2282 | | /* it's an array so the first character of the type string is 'a' |
2283 | | * |
2284 | | * if the first two characters are 'ay' then it's a bytestring. |
2285 | | * under certain conditions we print those as strings. |
2286 | | */ |
2287 | 0 | if (value_type_string[1] == 'y') |
2288 | 0 | { |
2289 | 0 | const gchar *str; |
2290 | 0 | gsize size; |
2291 | 0 | gsize i; |
2292 | | |
2293 | | /* first determine if it is a byte string. |
2294 | | * that's when there's a single nul character: at the end. |
2295 | | */ |
2296 | 0 | str = g_variant_get_data (value); |
2297 | 0 | size = g_variant_get_size (value); |
2298 | |
|
2299 | 0 | for (i = 0; i < size; i++) |
2300 | 0 | if (str[i] == '\0') |
2301 | 0 | break; |
2302 | | |
2303 | | /* first nul byte is the last byte -> it's a byte string. */ |
2304 | 0 | if (i == size - 1) |
2305 | 0 | { |
2306 | 0 | gchar *escaped = g_strescape (str, NULL); |
2307 | | |
2308 | | /* use double quotes only if a ' is in the string */ |
2309 | 0 | if (strchr (str, '\'')) |
2310 | 0 | g_string_append_printf (string, "b\"%s\"", escaped); |
2311 | 0 | else |
2312 | 0 | g_string_append_printf (string, "b'%s'", escaped); |
2313 | |
|
2314 | 0 | g_free (escaped); |
2315 | 0 | break; |
2316 | 0 | } |
2317 | | |
2318 | 0 | else |
2319 | 0 | { |
2320 | | /* fall through and handle normally... */ |
2321 | 0 | } |
2322 | 0 | } |
2323 | | |
2324 | | /* |
2325 | | * if the first two characters are 'a{' then it's an array of |
2326 | | * dictionary entries (ie: a dictionary) so we print that |
2327 | | * differently. |
2328 | | */ |
2329 | 0 | if (value_type_string[1] == '{') |
2330 | | /* dictionary */ |
2331 | 0 | { |
2332 | 0 | const gchar *comma = ""; |
2333 | 0 | gsize n, i; |
2334 | |
|
2335 | 0 | if ((n = g_variant_n_children (value)) == 0) |
2336 | 0 | { |
2337 | 0 | if (type_annotate) |
2338 | 0 | g_string_append_printf (string, "@%s ", value_type_string); |
2339 | 0 | g_string_append (string, "{}"); |
2340 | 0 | break; |
2341 | 0 | } |
2342 | | |
2343 | 0 | g_string_append_c (string, '{'); |
2344 | 0 | for (i = 0; i < n; i++) |
2345 | 0 | { |
2346 | 0 | GVariant *entry, *key, *val; |
2347 | |
|
2348 | 0 | g_string_append (string, comma); |
2349 | 0 | comma = ", "; |
2350 | |
|
2351 | 0 | entry = g_variant_get_child_value (value, i); |
2352 | 0 | key = g_variant_get_child_value (entry, 0); |
2353 | 0 | val = g_variant_get_child_value (entry, 1); |
2354 | 0 | g_variant_unref (entry); |
2355 | |
|
2356 | 0 | g_variant_print_string (key, string, type_annotate); |
2357 | 0 | g_variant_unref (key); |
2358 | 0 | g_string_append (string, ": "); |
2359 | 0 | g_variant_print_string (val, string, type_annotate); |
2360 | 0 | g_variant_unref (val); |
2361 | 0 | type_annotate = FALSE; |
2362 | 0 | } |
2363 | 0 | g_string_append_c (string, '}'); |
2364 | 0 | } |
2365 | 0 | else |
2366 | | /* normal (non-dictionary) array */ |
2367 | 0 | { |
2368 | 0 | const gchar *comma = ""; |
2369 | 0 | gsize n, i; |
2370 | |
|
2371 | 0 | if ((n = g_variant_n_children (value)) == 0) |
2372 | 0 | { |
2373 | 0 | if (type_annotate) |
2374 | 0 | g_string_append_printf (string, "@%s ", value_type_string); |
2375 | 0 | g_string_append (string, "[]"); |
2376 | 0 | break; |
2377 | 0 | } |
2378 | | |
2379 | 0 | g_string_append_c (string, '['); |
2380 | 0 | for (i = 0; i < n; i++) |
2381 | 0 | { |
2382 | 0 | GVariant *element; |
2383 | |
|
2384 | 0 | g_string_append (string, comma); |
2385 | 0 | comma = ", "; |
2386 | |
|
2387 | 0 | element = g_variant_get_child_value (value, i); |
2388 | |
|
2389 | 0 | g_variant_print_string (element, string, type_annotate); |
2390 | 0 | g_variant_unref (element); |
2391 | 0 | type_annotate = FALSE; |
2392 | 0 | } |
2393 | 0 | g_string_append_c (string, ']'); |
2394 | 0 | } |
2395 | | |
2396 | 0 | break; |
2397 | | |
2398 | 0 | case G_VARIANT_CLASS_TUPLE: |
2399 | 0 | { |
2400 | 0 | gsize n, i; |
2401 | |
|
2402 | 0 | n = g_variant_n_children (value); |
2403 | |
|
2404 | 0 | g_string_append_c (string, '('); |
2405 | 0 | for (i = 0; i < n; i++) |
2406 | 0 | { |
2407 | 0 | GVariant *element; |
2408 | |
|
2409 | 0 | element = g_variant_get_child_value (value, i); |
2410 | 0 | g_variant_print_string (element, string, type_annotate); |
2411 | 0 | g_string_append (string, ", "); |
2412 | 0 | g_variant_unref (element); |
2413 | 0 | } |
2414 | | |
2415 | | /* for >1 item: remove final ", " |
2416 | | * for 1 item: remove final " ", but leave the "," |
2417 | | * for 0 items: there is only "(", so remove nothing |
2418 | | */ |
2419 | 0 | g_string_truncate (string, string->len - (n > 0) - (n > 1)); |
2420 | 0 | g_string_append_c (string, ')'); |
2421 | 0 | } |
2422 | 0 | break; |
2423 | | |
2424 | 0 | case G_VARIANT_CLASS_DICT_ENTRY: |
2425 | 0 | { |
2426 | 0 | GVariant *element; |
2427 | |
|
2428 | 0 | g_string_append_c (string, '{'); |
2429 | |
|
2430 | 0 | element = g_variant_get_child_value (value, 0); |
2431 | 0 | g_variant_print_string (element, string, type_annotate); |
2432 | 0 | g_variant_unref (element); |
2433 | |
|
2434 | 0 | g_string_append (string, ", "); |
2435 | |
|
2436 | 0 | element = g_variant_get_child_value (value, 1); |
2437 | 0 | g_variant_print_string (element, string, type_annotate); |
2438 | 0 | g_variant_unref (element); |
2439 | |
|
2440 | 0 | g_string_append_c (string, '}'); |
2441 | 0 | } |
2442 | 0 | break; |
2443 | | |
2444 | 0 | case G_VARIANT_CLASS_VARIANT: |
2445 | 0 | { |
2446 | 0 | GVariant *child = g_variant_get_variant (value); |
2447 | | |
2448 | | /* Always annotate types in nested variants, because they are |
2449 | | * (by nature) of variable type. |
2450 | | */ |
2451 | 0 | g_string_append_c (string, '<'); |
2452 | 0 | g_variant_print_string (child, string, TRUE); |
2453 | 0 | g_string_append_c (string, '>'); |
2454 | |
|
2455 | 0 | g_variant_unref (child); |
2456 | 0 | } |
2457 | 0 | break; |
2458 | | |
2459 | 0 | case G_VARIANT_CLASS_BOOLEAN: |
2460 | 0 | if (g_variant_get_boolean (value)) |
2461 | 0 | g_string_append (string, "true"); |
2462 | 0 | else |
2463 | 0 | g_string_append (string, "false"); |
2464 | 0 | break; |
2465 | | |
2466 | 0 | case G_VARIANT_CLASS_STRING: |
2467 | 0 | { |
2468 | 0 | const gchar *str = g_variant_get_string (value, NULL); |
2469 | 0 | gunichar quote = strchr (str, '\'') ? '"' : '\''; |
2470 | |
|
2471 | 0 | g_string_append_c (string, quote); |
2472 | |
|
2473 | 0 | while (*str) |
2474 | 0 | { |
2475 | 0 | gunichar c = g_utf8_get_char (str); |
2476 | |
|
2477 | 0 | if (c == quote || c == '\\') |
2478 | 0 | g_string_append_c (string, '\\'); |
2479 | |
|
2480 | 0 | if (g_unichar_isprint (c)) |
2481 | 0 | g_string_append_unichar (string, c); |
2482 | | |
2483 | 0 | else |
2484 | 0 | { |
2485 | 0 | g_string_append_c (string, '\\'); |
2486 | 0 | if (c < 0x10000) |
2487 | 0 | switch (c) |
2488 | 0 | { |
2489 | 0 | case '\a': |
2490 | 0 | g_string_append_c (string, 'a'); |
2491 | 0 | break; |
2492 | | |
2493 | 0 | case '\b': |
2494 | 0 | g_string_append_c (string, 'b'); |
2495 | 0 | break; |
2496 | | |
2497 | 0 | case '\f': |
2498 | 0 | g_string_append_c (string, 'f'); |
2499 | 0 | break; |
2500 | | |
2501 | 0 | case '\n': |
2502 | 0 | g_string_append_c (string, 'n'); |
2503 | 0 | break; |
2504 | | |
2505 | 0 | case '\r': |
2506 | 0 | g_string_append_c (string, 'r'); |
2507 | 0 | break; |
2508 | | |
2509 | 0 | case '\t': |
2510 | 0 | g_string_append_c (string, 't'); |
2511 | 0 | break; |
2512 | | |
2513 | 0 | case '\v': |
2514 | 0 | g_string_append_c (string, 'v'); |
2515 | 0 | break; |
2516 | | |
2517 | 0 | default: |
2518 | 0 | g_string_append_printf (string, "u%04x", c); |
2519 | 0 | break; |
2520 | 0 | } |
2521 | 0 | else |
2522 | 0 | g_string_append_printf (string, "U%08x", c); |
2523 | 0 | } |
2524 | | |
2525 | 0 | str = g_utf8_next_char (str); |
2526 | 0 | } |
2527 | | |
2528 | 0 | g_string_append_c (string, quote); |
2529 | 0 | } |
2530 | 0 | break; |
2531 | | |
2532 | 0 | case G_VARIANT_CLASS_BYTE: |
2533 | 0 | if (type_annotate) |
2534 | 0 | g_string_append (string, "byte "); |
2535 | 0 | g_string_append_printf (string, "0x%02x", |
2536 | 0 | g_variant_get_byte (value)); |
2537 | 0 | break; |
2538 | | |
2539 | 0 | case G_VARIANT_CLASS_INT16: |
2540 | 0 | if (type_annotate) |
2541 | 0 | g_string_append (string, "int16 "); |
2542 | 0 | g_string_append_printf (string, "%"G_GINT16_FORMAT, |
2543 | 0 | g_variant_get_int16 (value)); |
2544 | 0 | break; |
2545 | | |
2546 | 0 | case G_VARIANT_CLASS_UINT16: |
2547 | 0 | if (type_annotate) |
2548 | 0 | g_string_append (string, "uint16 "); |
2549 | 0 | g_string_append_printf (string, "%"G_GUINT16_FORMAT, |
2550 | 0 | g_variant_get_uint16 (value)); |
2551 | 0 | break; |
2552 | | |
2553 | 0 | case G_VARIANT_CLASS_INT32: |
2554 | | /* Never annotate this type because it is the default for numbers |
2555 | | * (and this is a *pretty* printer) |
2556 | | */ |
2557 | 0 | g_string_append_printf (string, "%"G_GINT32_FORMAT, |
2558 | 0 | g_variant_get_int32 (value)); |
2559 | 0 | break; |
2560 | | |
2561 | 0 | case G_VARIANT_CLASS_HANDLE: |
2562 | 0 | if (type_annotate) |
2563 | 0 | g_string_append (string, "handle "); |
2564 | 0 | g_string_append_printf (string, "%"G_GINT32_FORMAT, |
2565 | 0 | g_variant_get_handle (value)); |
2566 | 0 | break; |
2567 | | |
2568 | 0 | case G_VARIANT_CLASS_UINT32: |
2569 | 0 | if (type_annotate) |
2570 | 0 | g_string_append (string, "uint32 "); |
2571 | 0 | g_string_append_printf (string, "%"G_GUINT32_FORMAT, |
2572 | 0 | g_variant_get_uint32 (value)); |
2573 | 0 | break; |
2574 | | |
2575 | 0 | case G_VARIANT_CLASS_INT64: |
2576 | 0 | if (type_annotate) |
2577 | 0 | g_string_append (string, "int64 "); |
2578 | 0 | g_string_append_printf (string, "%"G_GINT64_FORMAT, |
2579 | 0 | g_variant_get_int64 (value)); |
2580 | 0 | break; |
2581 | | |
2582 | 0 | case G_VARIANT_CLASS_UINT64: |
2583 | 0 | if (type_annotate) |
2584 | 0 | g_string_append (string, "uint64 "); |
2585 | 0 | g_string_append_printf (string, "%"G_GUINT64_FORMAT, |
2586 | 0 | g_variant_get_uint64 (value)); |
2587 | 0 | break; |
2588 | | |
2589 | 0 | case G_VARIANT_CLASS_DOUBLE: |
2590 | 0 | { |
2591 | 0 | gchar buffer[100]; |
2592 | 0 | gint i; |
2593 | |
|
2594 | 0 | g_ascii_dtostr (buffer, sizeof buffer, g_variant_get_double (value)); |
2595 | |
|
2596 | 0 | for (i = 0; buffer[i]; i++) |
2597 | 0 | if (buffer[i] == '.' || buffer[i] == 'e' || |
2598 | 0 | buffer[i] == 'n' || buffer[i] == 'N') |
2599 | 0 | break; |
2600 | | |
2601 | | /* if there is no '.' or 'e' in the float then add one */ |
2602 | 0 | if (buffer[i] == '\0') |
2603 | 0 | { |
2604 | 0 | buffer[i++] = '.'; |
2605 | 0 | buffer[i++] = '0'; |
2606 | 0 | buffer[i++] = '\0'; |
2607 | 0 | } |
2608 | |
|
2609 | 0 | g_string_append (string, buffer); |
2610 | 0 | } |
2611 | 0 | break; |
2612 | | |
2613 | 0 | case G_VARIANT_CLASS_OBJECT_PATH: |
2614 | 0 | if (type_annotate) |
2615 | 0 | g_string_append (string, "objectpath "); |
2616 | 0 | g_string_append_printf (string, "\'%s\'", |
2617 | 0 | g_variant_get_string (value, NULL)); |
2618 | 0 | break; |
2619 | | |
2620 | 0 | case G_VARIANT_CLASS_SIGNATURE: |
2621 | 0 | if (type_annotate) |
2622 | 0 | g_string_append (string, "signature "); |
2623 | 0 | g_string_append_printf (string, "\'%s\'", |
2624 | 0 | g_variant_get_string (value, NULL)); |
2625 | 0 | break; |
2626 | | |
2627 | 0 | default: |
2628 | 0 | g_assert_not_reached (); |
2629 | 0 | } |
2630 | | |
2631 | 0 | return string; |
2632 | 0 | } |
2633 | | |
2634 | | /** |
2635 | | * g_variant_print: |
2636 | | * @value: a #GVariant |
2637 | | * @type_annotate: %TRUE if type information should be included in |
2638 | | * the output |
2639 | | * |
2640 | | * Pretty-prints @value in the format understood by g_variant_parse(). |
2641 | | * |
2642 | | * The format is described [here][gvariant-text]. |
2643 | | * |
2644 | | * If @type_annotate is %TRUE, then type information is included in |
2645 | | * the output. |
2646 | | * |
2647 | | * Returns: (transfer full): a newly-allocated string holding the result. |
2648 | | * |
2649 | | * Since: 2.24 |
2650 | | */ |
2651 | | gchar * |
2652 | | g_variant_print (GVariant *value, |
2653 | | gboolean type_annotate) |
2654 | 0 | { |
2655 | 0 | return g_string_free (g_variant_print_string (value, NULL, type_annotate), |
2656 | 0 | FALSE); |
2657 | 0 | } |
2658 | | |
2659 | | /* Hash, Equal, Compare {{{1 */ |
2660 | | /** |
2661 | | * g_variant_hash: |
2662 | | * @value: (type GVariant): a basic #GVariant value as a #gconstpointer |
2663 | | * |
2664 | | * Generates a hash value for a #GVariant instance. |
2665 | | * |
2666 | | * The output of this function is guaranteed to be the same for a given |
2667 | | * value only per-process. It may change between different processor |
2668 | | * architectures or even different versions of GLib. Do not use this |
2669 | | * function as a basis for building protocols or file formats. |
2670 | | * |
2671 | | * The type of @value is #gconstpointer only to allow use of this |
2672 | | * function with #GHashTable. @value must be a #GVariant. |
2673 | | * |
2674 | | * Returns: a hash value corresponding to @value |
2675 | | * |
2676 | | * Since: 2.24 |
2677 | | **/ |
2678 | | guint |
2679 | | g_variant_hash (gconstpointer value_) |
2680 | 0 | { |
2681 | 0 | GVariant *value = (GVariant *) value_; |
2682 | |
|
2683 | 0 | switch (g_variant_classify (value)) |
2684 | 0 | { |
2685 | 0 | case G_VARIANT_CLASS_STRING: |
2686 | 0 | case G_VARIANT_CLASS_OBJECT_PATH: |
2687 | 0 | case G_VARIANT_CLASS_SIGNATURE: |
2688 | 0 | return g_str_hash (g_variant_get_string (value, NULL)); |
2689 | | |
2690 | 0 | case G_VARIANT_CLASS_BOOLEAN: |
2691 | | /* this is a very odd thing to hash... */ |
2692 | 0 | return g_variant_get_boolean (value); |
2693 | | |
2694 | 0 | case G_VARIANT_CLASS_BYTE: |
2695 | 0 | return g_variant_get_byte (value); |
2696 | | |
2697 | 0 | case G_VARIANT_CLASS_INT16: |
2698 | 0 | case G_VARIANT_CLASS_UINT16: |
2699 | 0 | { |
2700 | 0 | const guint16 *ptr; |
2701 | |
|
2702 | 0 | ptr = g_variant_get_data (value); |
2703 | |
|
2704 | 0 | if (ptr) |
2705 | 0 | return *ptr; |
2706 | 0 | else |
2707 | 0 | return 0; |
2708 | 0 | } |
2709 | | |
2710 | 0 | case G_VARIANT_CLASS_INT32: |
2711 | 0 | case G_VARIANT_CLASS_UINT32: |
2712 | 0 | case G_VARIANT_CLASS_HANDLE: |
2713 | 0 | { |
2714 | 0 | const guint *ptr; |
2715 | |
|
2716 | 0 | ptr = g_variant_get_data (value); |
2717 | |
|
2718 | 0 | if (ptr) |
2719 | 0 | return *ptr; |
2720 | 0 | else |
2721 | 0 | return 0; |
2722 | 0 | } |
2723 | | |
2724 | 0 | case G_VARIANT_CLASS_INT64: |
2725 | 0 | case G_VARIANT_CLASS_UINT64: |
2726 | 0 | case G_VARIANT_CLASS_DOUBLE: |
2727 | | /* need a separate case for these guys because otherwise |
2728 | | * performance could be quite bad on big endian systems |
2729 | | */ |
2730 | 0 | { |
2731 | 0 | const guint *ptr; |
2732 | |
|
2733 | 0 | ptr = g_variant_get_data (value); |
2734 | |
|
2735 | 0 | if (ptr) |
2736 | 0 | return ptr[0] + ptr[1]; |
2737 | 0 | else |
2738 | 0 | return 0; |
2739 | 0 | } |
2740 | | |
2741 | 0 | default: |
2742 | 0 | g_return_val_if_fail (!g_variant_is_container (value), 0); |
2743 | 0 | g_assert_not_reached (); |
2744 | 0 | } |
2745 | 0 | } |
2746 | | |
2747 | | /** |
2748 | | * g_variant_equal: |
2749 | | * @one: (type GVariant): a #GVariant instance |
2750 | | * @two: (type GVariant): a #GVariant instance |
2751 | | * |
2752 | | * Checks if @one and @two have the same type and value. |
2753 | | * |
2754 | | * The types of @one and @two are #gconstpointer only to allow use of |
2755 | | * this function with #GHashTable. They must each be a #GVariant. |
2756 | | * |
2757 | | * Returns: %TRUE if @one and @two are equal |
2758 | | * |
2759 | | * Since: 2.24 |
2760 | | **/ |
2761 | | gboolean |
2762 | | g_variant_equal (gconstpointer one, |
2763 | | gconstpointer two) |
2764 | 0 | { |
2765 | 0 | gboolean equal; |
2766 | |
|
2767 | 0 | g_return_val_if_fail (one != NULL && two != NULL, FALSE); |
2768 | | |
2769 | 0 | if (g_variant_get_type_info ((GVariant *) one) != |
2770 | 0 | g_variant_get_type_info ((GVariant *) two)) |
2771 | 0 | return FALSE; |
2772 | | |
2773 | | /* if both values are trusted to be in their canonical serialized form |
2774 | | * then a simple memcmp() of their serialized data will answer the |
2775 | | * question. |
2776 | | * |
2777 | | * if not, then this might generate a false negative (since it is |
2778 | | * possible for two different byte sequences to represent the same |
2779 | | * value). for now we solve this by pretty-printing both values and |
2780 | | * comparing the result. |
2781 | | */ |
2782 | 0 | if (g_variant_is_trusted ((GVariant *) one) && |
2783 | 0 | g_variant_is_trusted ((GVariant *) two)) |
2784 | 0 | { |
2785 | 0 | gconstpointer data_one, data_two; |
2786 | 0 | gsize size_one, size_two; |
2787 | |
|
2788 | 0 | size_one = g_variant_get_size ((GVariant *) one); |
2789 | 0 | size_two = g_variant_get_size ((GVariant *) two); |
2790 | |
|
2791 | 0 | if (size_one != size_two) |
2792 | 0 | return FALSE; |
2793 | | |
2794 | 0 | data_one = g_variant_get_data ((GVariant *) one); |
2795 | 0 | data_two = g_variant_get_data ((GVariant *) two); |
2796 | |
|
2797 | 0 | if (size_one) |
2798 | 0 | equal = memcmp (data_one, data_two, size_one) == 0; |
2799 | 0 | else |
2800 | 0 | equal = TRUE; |
2801 | 0 | } |
2802 | 0 | else |
2803 | 0 | { |
2804 | 0 | gchar *strone, *strtwo; |
2805 | |
|
2806 | 0 | strone = g_variant_print ((GVariant *) one, FALSE); |
2807 | 0 | strtwo = g_variant_print ((GVariant *) two, FALSE); |
2808 | 0 | equal = strcmp (strone, strtwo) == 0; |
2809 | 0 | g_free (strone); |
2810 | 0 | g_free (strtwo); |
2811 | 0 | } |
2812 | | |
2813 | 0 | return equal; |
2814 | 0 | } |
2815 | | |
2816 | | /** |
2817 | | * g_variant_compare: |
2818 | | * @one: (type GVariant): a basic-typed #GVariant instance |
2819 | | * @two: (type GVariant): a #GVariant instance of the same type |
2820 | | * |
2821 | | * Compares @one and @two. |
2822 | | * |
2823 | | * The types of @one and @two are #gconstpointer only to allow use of |
2824 | | * this function with #GTree, #GPtrArray, etc. They must each be a |
2825 | | * #GVariant. |
2826 | | * |
2827 | | * Comparison is only defined for basic types (ie: booleans, numbers, |
2828 | | * strings). For booleans, %FALSE is less than %TRUE. Numbers are |
2829 | | * ordered in the usual way. Strings are in ASCII lexographical order. |
2830 | | * |
2831 | | * It is a programmer error to attempt to compare container values or |
2832 | | * two values that have types that are not exactly equal. For example, |
2833 | | * you cannot compare a 32-bit signed integer with a 32-bit unsigned |
2834 | | * integer. Also note that this function is not particularly |
2835 | | * well-behaved when it comes to comparison of doubles; in particular, |
2836 | | * the handling of incomparable values (ie: NaN) is undefined. |
2837 | | * |
2838 | | * If you only require an equality comparison, g_variant_equal() is more |
2839 | | * general. |
2840 | | * |
2841 | | * Returns: negative value if a < b; |
2842 | | * zero if a = b; |
2843 | | * positive value if a > b. |
2844 | | * |
2845 | | * Since: 2.26 |
2846 | | **/ |
2847 | | gint |
2848 | | g_variant_compare (gconstpointer one, |
2849 | | gconstpointer two) |
2850 | 0 | { |
2851 | 0 | GVariant *a = (GVariant *) one; |
2852 | 0 | GVariant *b = (GVariant *) two; |
2853 | |
|
2854 | 0 | g_return_val_if_fail (g_variant_classify (a) == g_variant_classify (b), 0); |
2855 | | |
2856 | 0 | switch (g_variant_classify (a)) |
2857 | 0 | { |
2858 | 0 | case G_VARIANT_CLASS_BOOLEAN: |
2859 | 0 | return g_variant_get_boolean (a) - |
2860 | 0 | g_variant_get_boolean (b); |
2861 | | |
2862 | 0 | case G_VARIANT_CLASS_BYTE: |
2863 | 0 | return ((gint) g_variant_get_byte (a)) - |
2864 | 0 | ((gint) g_variant_get_byte (b)); |
2865 | | |
2866 | 0 | case G_VARIANT_CLASS_INT16: |
2867 | 0 | return ((gint) g_variant_get_int16 (a)) - |
2868 | 0 | ((gint) g_variant_get_int16 (b)); |
2869 | | |
2870 | 0 | case G_VARIANT_CLASS_UINT16: |
2871 | 0 | return ((gint) g_variant_get_uint16 (a)) - |
2872 | 0 | ((gint) g_variant_get_uint16 (b)); |
2873 | | |
2874 | 0 | case G_VARIANT_CLASS_INT32: |
2875 | 0 | { |
2876 | 0 | gint32 a_val = g_variant_get_int32 (a); |
2877 | 0 | gint32 b_val = g_variant_get_int32 (b); |
2878 | |
|
2879 | 0 | return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1; |
2880 | 0 | } |
2881 | | |
2882 | 0 | case G_VARIANT_CLASS_UINT32: |
2883 | 0 | { |
2884 | 0 | guint32 a_val = g_variant_get_uint32 (a); |
2885 | 0 | guint32 b_val = g_variant_get_uint32 (b); |
2886 | |
|
2887 | 0 | return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1; |
2888 | 0 | } |
2889 | | |
2890 | 0 | case G_VARIANT_CLASS_INT64: |
2891 | 0 | { |
2892 | 0 | gint64 a_val = g_variant_get_int64 (a); |
2893 | 0 | gint64 b_val = g_variant_get_int64 (b); |
2894 | |
|
2895 | 0 | return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1; |
2896 | 0 | } |
2897 | | |
2898 | 0 | case G_VARIANT_CLASS_UINT64: |
2899 | 0 | { |
2900 | 0 | guint64 a_val = g_variant_get_uint64 (a); |
2901 | 0 | guint64 b_val = g_variant_get_uint64 (b); |
2902 | |
|
2903 | 0 | return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1; |
2904 | 0 | } |
2905 | | |
2906 | 0 | case G_VARIANT_CLASS_DOUBLE: |
2907 | 0 | { |
2908 | 0 | gdouble a_val = g_variant_get_double (a); |
2909 | 0 | gdouble b_val = g_variant_get_double (b); |
2910 | |
|
2911 | 0 | return (a_val == b_val) ? 0 : (a_val > b_val) ? 1 : -1; |
2912 | 0 | } |
2913 | | |
2914 | 0 | case G_VARIANT_CLASS_STRING: |
2915 | 0 | case G_VARIANT_CLASS_OBJECT_PATH: |
2916 | 0 | case G_VARIANT_CLASS_SIGNATURE: |
2917 | 0 | return strcmp (g_variant_get_string (a, NULL), |
2918 | 0 | g_variant_get_string (b, NULL)); |
2919 | | |
2920 | 0 | default: |
2921 | 0 | g_return_val_if_fail (!g_variant_is_container (a), 0); |
2922 | 0 | g_assert_not_reached (); |
2923 | 0 | } |
2924 | 0 | } |
2925 | | |
2926 | | /* GVariantIter {{{1 */ |
2927 | | /** |
2928 | | * GVariantIter: (skip) |
2929 | | * |
2930 | | * #GVariantIter is an opaque data structure and can only be accessed |
2931 | | * using the following functions. |
2932 | | **/ |
2933 | | struct stack_iter |
2934 | | { |
2935 | | GVariant *value; |
2936 | | gssize n, i; |
2937 | | |
2938 | | const gchar *loop_format; |
2939 | | |
2940 | | gsize padding[3]; |
2941 | | gsize magic; |
2942 | | }; |
2943 | | |
2944 | | G_STATIC_ASSERT (sizeof (struct stack_iter) <= sizeof (GVariantIter)); |
2945 | | |
2946 | | struct heap_iter |
2947 | | { |
2948 | | struct stack_iter iter; |
2949 | | |
2950 | | GVariant *value_ref; |
2951 | | gsize magic; |
2952 | | }; |
2953 | | |
2954 | 0 | #define GVSI(i) ((struct stack_iter *) (i)) |
2955 | 0 | #define GVHI(i) ((struct heap_iter *) (i)) |
2956 | 0 | #define GVSI_MAGIC ((gsize) 3579507750u) |
2957 | 0 | #define GVHI_MAGIC ((gsize) 1450270775u) |
2958 | | #define is_valid_iter(i) (i != NULL && \ |
2959 | | GVSI(i)->magic == GVSI_MAGIC) |
2960 | | #define is_valid_heap_iter(i) (is_valid_iter(i) && \ |
2961 | | GVHI(i)->magic == GVHI_MAGIC) |
2962 | | |
2963 | | /** |
2964 | | * g_variant_iter_new: |
2965 | | * @value: a container #GVariant |
2966 | | * |
2967 | | * Creates a heap-allocated #GVariantIter for iterating over the items |
2968 | | * in @value. |
2969 | | * |
2970 | | * Use g_variant_iter_free() to free the return value when you no longer |
2971 | | * need it. |
2972 | | * |
2973 | | * A reference is taken to @value and will be released only when |
2974 | | * g_variant_iter_free() is called. |
2975 | | * |
2976 | | * Returns: (transfer full): a new heap-allocated #GVariantIter |
2977 | | * |
2978 | | * Since: 2.24 |
2979 | | **/ |
2980 | | GVariantIter * |
2981 | | g_variant_iter_new (GVariant *value) |
2982 | 0 | { |
2983 | 0 | GVariantIter *iter; |
2984 | |
|
2985 | 0 | iter = (GVariantIter *) g_slice_new (struct heap_iter); |
2986 | 0 | GVHI(iter)->value_ref = g_variant_ref (value); |
2987 | 0 | GVHI(iter)->magic = GVHI_MAGIC; |
2988 | |
|
2989 | 0 | g_variant_iter_init (iter, value); |
2990 | |
|
2991 | 0 | return iter; |
2992 | 0 | } |
2993 | | |
2994 | | /** |
2995 | | * g_variant_iter_init: (skip) |
2996 | | * @iter: a pointer to a #GVariantIter |
2997 | | * @value: a container #GVariant |
2998 | | * |
2999 | | * Initialises (without allocating) a #GVariantIter. @iter may be |
3000 | | * completely uninitialised prior to this call; its old value is |
3001 | | * ignored. |
3002 | | * |
3003 | | * The iterator remains valid for as long as @value exists, and need not |
3004 | | * be freed in any way. |
3005 | | * |
3006 | | * Returns: the number of items in @value |
3007 | | * |
3008 | | * Since: 2.24 |
3009 | | **/ |
3010 | | gsize |
3011 | | g_variant_iter_init (GVariantIter *iter, |
3012 | | GVariant *value) |
3013 | 0 | { |
3014 | 0 | GVSI(iter)->magic = GVSI_MAGIC; |
3015 | 0 | GVSI(iter)->value = value; |
3016 | 0 | GVSI(iter)->n = g_variant_n_children (value); |
3017 | 0 | GVSI(iter)->i = -1; |
3018 | 0 | GVSI(iter)->loop_format = NULL; |
3019 | |
|
3020 | 0 | return GVSI(iter)->n; |
3021 | 0 | } |
3022 | | |
3023 | | /** |
3024 | | * g_variant_iter_copy: |
3025 | | * @iter: a #GVariantIter |
3026 | | * |
3027 | | * Creates a new heap-allocated #GVariantIter to iterate over the |
3028 | | * container that was being iterated over by @iter. Iteration begins on |
3029 | | * the new iterator from the current position of the old iterator but |
3030 | | * the two copies are independent past that point. |
3031 | | * |
3032 | | * Use g_variant_iter_free() to free the return value when you no longer |
3033 | | * need it. |
3034 | | * |
3035 | | * A reference is taken to the container that @iter is iterating over |
3036 | | * and will be related only when g_variant_iter_free() is called. |
3037 | | * |
3038 | | * Returns: (transfer full): a new heap-allocated #GVariantIter |
3039 | | * |
3040 | | * Since: 2.24 |
3041 | | **/ |
3042 | | GVariantIter * |
3043 | | g_variant_iter_copy (GVariantIter *iter) |
3044 | 0 | { |
3045 | 0 | GVariantIter *copy; |
3046 | |
|
3047 | 0 | g_return_val_if_fail (is_valid_iter (iter), 0); |
3048 | | |
3049 | 0 | copy = g_variant_iter_new (GVSI(iter)->value); |
3050 | 0 | GVSI(copy)->i = GVSI(iter)->i; |
3051 | |
|
3052 | 0 | return copy; |
3053 | 0 | } |
3054 | | |
3055 | | /** |
3056 | | * g_variant_iter_n_children: |
3057 | | * @iter: a #GVariantIter |
3058 | | * |
3059 | | * Queries the number of child items in the container that we are |
3060 | | * iterating over. This is the total number of items -- not the number |
3061 | | * of items remaining. |
3062 | | * |
3063 | | * This function might be useful for preallocation of arrays. |
3064 | | * |
3065 | | * Returns: the number of children in the container |
3066 | | * |
3067 | | * Since: 2.24 |
3068 | | **/ |
3069 | | gsize |
3070 | | g_variant_iter_n_children (GVariantIter *iter) |
3071 | 0 | { |
3072 | 0 | g_return_val_if_fail (is_valid_iter (iter), 0); |
3073 | | |
3074 | 0 | return GVSI(iter)->n; |
3075 | 0 | } |
3076 | | |
3077 | | /** |
3078 | | * g_variant_iter_free: |
3079 | | * @iter: (transfer full): a heap-allocated #GVariantIter |
3080 | | * |
3081 | | * Frees a heap-allocated #GVariantIter. Only call this function on |
3082 | | * iterators that were returned by g_variant_iter_new() or |
3083 | | * g_variant_iter_copy(). |
3084 | | * |
3085 | | * Since: 2.24 |
3086 | | **/ |
3087 | | void |
3088 | | g_variant_iter_free (GVariantIter *iter) |
3089 | 0 | { |
3090 | 0 | g_return_if_fail (is_valid_heap_iter (iter)); |
3091 | | |
3092 | 0 | g_variant_unref (GVHI(iter)->value_ref); |
3093 | 0 | GVHI(iter)->magic = 0; |
3094 | |
|
3095 | 0 | g_slice_free (struct heap_iter, GVHI(iter)); |
3096 | 0 | } |
3097 | | |
3098 | | /** |
3099 | | * g_variant_iter_next_value: |
3100 | | * @iter: a #GVariantIter |
3101 | | * |
3102 | | * Gets the next item in the container. If no more items remain then |
3103 | | * %NULL is returned. |
3104 | | * |
3105 | | * Use g_variant_unref() to drop your reference on the return value when |
3106 | | * you no longer need it. |
3107 | | * |
3108 | | * Here is an example for iterating with g_variant_iter_next_value(): |
3109 | | * |[<!-- language="C" --> |
3110 | | * // recursively iterate a container |
3111 | | * void |
3112 | | * iterate_container_recursive (GVariant *container) |
3113 | | * { |
3114 | | * GVariantIter iter; |
3115 | | * GVariant *child; |
3116 | | * |
3117 | | * g_variant_iter_init (&iter, container); |
3118 | | * while ((child = g_variant_iter_next_value (&iter))) |
3119 | | * { |
3120 | | * g_print ("type '%s'\n", g_variant_get_type_string (child)); |
3121 | | * |
3122 | | * if (g_variant_is_container (child)) |
3123 | | * iterate_container_recursive (child); |
3124 | | * |
3125 | | * g_variant_unref (child); |
3126 | | * } |
3127 | | * } |
3128 | | * ]| |
3129 | | * |
3130 | | * Returns: (nullable) (transfer full): a #GVariant, or %NULL |
3131 | | * |
3132 | | * Since: 2.24 |
3133 | | **/ |
3134 | | GVariant * |
3135 | | g_variant_iter_next_value (GVariantIter *iter) |
3136 | 0 | { |
3137 | 0 | g_return_val_if_fail (is_valid_iter (iter), FALSE); |
3138 | | |
3139 | 0 | if G_UNLIKELY (GVSI(iter)->i >= GVSI(iter)->n) |
3140 | 0 | { |
3141 | 0 | g_critical ("g_variant_iter_next_value: must not be called again " |
3142 | 0 | "after NULL has already been returned."); |
3143 | 0 | return NULL; |
3144 | 0 | } |
3145 | | |
3146 | 0 | GVSI(iter)->i++; |
3147 | |
|
3148 | 0 | if (GVSI(iter)->i < GVSI(iter)->n) |
3149 | 0 | return g_variant_get_child_value (GVSI(iter)->value, GVSI(iter)->i); |
3150 | | |
3151 | 0 | return NULL; |
3152 | 0 | } |
3153 | | |
3154 | | /* GVariantBuilder {{{1 */ |
3155 | | /** |
3156 | | * GVariantBuilder: |
3157 | | * |
3158 | | * A utility type for constructing container-type #GVariant instances. |
3159 | | * |
3160 | | * This is an opaque structure and may only be accessed using the |
3161 | | * following functions. |
3162 | | * |
3163 | | * #GVariantBuilder is not threadsafe in any way. Do not attempt to |
3164 | | * access it from more than one thread. |
3165 | | **/ |
3166 | | |
3167 | | struct stack_builder |
3168 | | { |
3169 | | GVariantBuilder *parent; |
3170 | | GVariantType *type; |
3171 | | |
3172 | | /* type constraint explicitly specified by 'type'. |
3173 | | * for tuple types, this moves along as we add more items. |
3174 | | */ |
3175 | | const GVariantType *expected_type; |
3176 | | |
3177 | | /* type constraint implied by previous array item. |
3178 | | */ |
3179 | | const GVariantType *prev_item_type; |
3180 | | |
3181 | | /* constraints on the number of children. max = -1 for unlimited. */ |
3182 | | gsize min_items; |
3183 | | gsize max_items; |
3184 | | |
3185 | | /* dynamically-growing pointer array */ |
3186 | | GVariant **children; |
3187 | | gsize allocated_children; |
3188 | | gsize offset; |
3189 | | |
3190 | | /* set to '1' if all items in the container will have the same type |
3191 | | * (ie: maybe, array, variant) '0' if not (ie: tuple, dict entry) |
3192 | | */ |
3193 | | guint uniform_item_types : 1; |
3194 | | |
3195 | | /* set to '1' initially and changed to '0' if an untrusted value is |
3196 | | * added |
3197 | | */ |
3198 | | guint trusted : 1; |
3199 | | |
3200 | | gsize magic; |
3201 | | }; |
3202 | | |
3203 | | G_STATIC_ASSERT (sizeof (struct stack_builder) <= sizeof (GVariantBuilder)); |
3204 | | |
3205 | | struct heap_builder |
3206 | | { |
3207 | | GVariantBuilder builder; |
3208 | | gsize magic; |
3209 | | |
3210 | | gint ref_count; |
3211 | | }; |
3212 | | |
3213 | 0 | #define GVSB(b) ((struct stack_builder *) (b)) |
3214 | 0 | #define GVHB(b) ((struct heap_builder *) (b)) |
3215 | 0 | #define GVSB_MAGIC ((gsize) 1033660112u) |
3216 | 0 | #define GVSB_MAGIC_PARTIAL ((gsize) 2942751021u) |
3217 | 0 | #define GVHB_MAGIC ((gsize) 3087242682u) |
3218 | 0 | #define is_valid_builder(b) (GVSB(b)->magic == GVSB_MAGIC) |
3219 | | #define is_valid_heap_builder(b) (GVHB(b)->magic == GVHB_MAGIC) |
3220 | | |
3221 | | /* Just to make sure that by adding a union to GVariantBuilder, we |
3222 | | * didn't accidentally change ABI. */ |
3223 | | G_STATIC_ASSERT (sizeof (GVariantBuilder) == sizeof (gsize[16])); |
3224 | | |
3225 | | static gboolean |
3226 | | ensure_valid_builder (GVariantBuilder *builder) |
3227 | 0 | { |
3228 | 0 | if (builder == NULL) |
3229 | 0 | return FALSE; |
3230 | 0 | else if (is_valid_builder (builder)) |
3231 | 0 | return TRUE; |
3232 | 0 | if (builder->u.s.partial_magic == GVSB_MAGIC_PARTIAL) |
3233 | 0 | { |
3234 | 0 | static GVariantBuilder cleared_builder; |
3235 | | |
3236 | | /* Make sure that only first two fields were set and the rest is |
3237 | | * zeroed to avoid messing up the builder that had parent |
3238 | | * address equal to GVSB_MAGIC_PARTIAL. */ |
3239 | 0 | if (memcmp (cleared_builder.u.s.y, builder->u.s.y, sizeof cleared_builder.u.s.y)) |
3240 | 0 | return FALSE; |
3241 | | |
3242 | 0 | g_variant_builder_init (builder, builder->u.s.type); |
3243 | 0 | } |
3244 | 0 | return is_valid_builder (builder); |
3245 | 0 | } |
3246 | | |
3247 | | /* return_if_invalid_builder (b) is like |
3248 | | * g_return_if_fail (ensure_valid_builder (b)), except that |
3249 | | * the side effects of ensure_valid_builder are evaluated |
3250 | | * regardless of whether G_DISABLE_CHECKS is defined or not. */ |
3251 | 0 | #define return_if_invalid_builder(b) G_STMT_START { \ |
3252 | 0 | gboolean valid_builder G_GNUC_UNUSED = ensure_valid_builder (b); \ |
3253 | 0 | g_return_if_fail (valid_builder); \ |
3254 | 0 | } G_STMT_END |
3255 | | |
3256 | | /* return_val_if_invalid_builder (b, val) is like |
3257 | | * g_return_val_if_fail (ensure_valid_builder (b), val), except that |
3258 | | * the side effects of ensure_valid_builder are evaluated |
3259 | | * regardless of whether G_DISABLE_CHECKS is defined or not. */ |
3260 | 0 | #define return_val_if_invalid_builder(b, val) G_STMT_START { \ |
3261 | 0 | gboolean valid_builder G_GNUC_UNUSED = ensure_valid_builder (b); \ |
3262 | 0 | g_return_val_if_fail (valid_builder, val); \ |
3263 | 0 | } G_STMT_END |
3264 | | |
3265 | | /** |
3266 | | * g_variant_builder_new: |
3267 | | * @type: a container type |
3268 | | * |
3269 | | * Allocates and initialises a new #GVariantBuilder. |
3270 | | * |
3271 | | * You should call g_variant_builder_unref() on the return value when it |
3272 | | * is no longer needed. The memory will not be automatically freed by |
3273 | | * any other call. |
3274 | | * |
3275 | | * In most cases it is easier to place a #GVariantBuilder directly on |
3276 | | * the stack of the calling function and initialise it with |
3277 | | * g_variant_builder_init(). |
3278 | | * |
3279 | | * Returns: (transfer full): a #GVariantBuilder |
3280 | | * |
3281 | | * Since: 2.24 |
3282 | | **/ |
3283 | | GVariantBuilder * |
3284 | | g_variant_builder_new (const GVariantType *type) |
3285 | 0 | { |
3286 | 0 | GVariantBuilder *builder; |
3287 | |
|
3288 | 0 | builder = (GVariantBuilder *) g_slice_new (struct heap_builder); |
3289 | 0 | g_variant_builder_init (builder, type); |
3290 | 0 | GVHB(builder)->magic = GVHB_MAGIC; |
3291 | 0 | GVHB(builder)->ref_count = 1; |
3292 | |
|
3293 | 0 | return builder; |
3294 | 0 | } |
3295 | | |
3296 | | /** |
3297 | | * g_variant_builder_unref: |
3298 | | * @builder: (transfer full): a #GVariantBuilder allocated by g_variant_builder_new() |
3299 | | * |
3300 | | * Decreases the reference count on @builder. |
3301 | | * |
3302 | | * In the event that there are no more references, releases all memory |
3303 | | * associated with the #GVariantBuilder. |
3304 | | * |
3305 | | * Don't call this on stack-allocated #GVariantBuilder instances or bad |
3306 | | * things will happen. |
3307 | | * |
3308 | | * Since: 2.24 |
3309 | | **/ |
3310 | | void |
3311 | | g_variant_builder_unref (GVariantBuilder *builder) |
3312 | 0 | { |
3313 | 0 | g_return_if_fail (is_valid_heap_builder (builder)); |
3314 | | |
3315 | 0 | if (--GVHB(builder)->ref_count) |
3316 | 0 | return; |
3317 | | |
3318 | 0 | g_variant_builder_clear (builder); |
3319 | 0 | GVHB(builder)->magic = 0; |
3320 | |
|
3321 | 0 | g_slice_free (struct heap_builder, GVHB(builder)); |
3322 | 0 | } |
3323 | | |
3324 | | /** |
3325 | | * g_variant_builder_ref: |
3326 | | * @builder: a #GVariantBuilder allocated by g_variant_builder_new() |
3327 | | * |
3328 | | * Increases the reference count on @builder. |
3329 | | * |
3330 | | * Don't call this on stack-allocated #GVariantBuilder instances or bad |
3331 | | * things will happen. |
3332 | | * |
3333 | | * Returns: (transfer full): a new reference to @builder |
3334 | | * |
3335 | | * Since: 2.24 |
3336 | | **/ |
3337 | | GVariantBuilder * |
3338 | | g_variant_builder_ref (GVariantBuilder *builder) |
3339 | 0 | { |
3340 | 0 | g_return_val_if_fail (is_valid_heap_builder (builder), NULL); |
3341 | | |
3342 | 0 | GVHB(builder)->ref_count++; |
3343 | |
|
3344 | 0 | return builder; |
3345 | 0 | } |
3346 | | |
3347 | | /** |
3348 | | * g_variant_builder_clear: (skip) |
3349 | | * @builder: a #GVariantBuilder |
3350 | | * |
3351 | | * Releases all memory associated with a #GVariantBuilder without |
3352 | | * freeing the #GVariantBuilder structure itself. |
3353 | | * |
3354 | | * It typically only makes sense to do this on a stack-allocated |
3355 | | * #GVariantBuilder if you want to abort building the value part-way |
3356 | | * through. This function need not be called if you call |
3357 | | * g_variant_builder_end() and it also doesn't need to be called on |
3358 | | * builders allocated with g_variant_builder_new() (see |
3359 | | * g_variant_builder_unref() for that). |
3360 | | * |
3361 | | * This function leaves the #GVariantBuilder structure set to all-zeros. |
3362 | | * It is valid to call this function on either an initialised |
3363 | | * #GVariantBuilder or one that is set to all-zeros but it is not valid |
3364 | | * to call this function on uninitialised memory. |
3365 | | * |
3366 | | * Since: 2.24 |
3367 | | **/ |
3368 | | void |
3369 | | g_variant_builder_clear (GVariantBuilder *builder) |
3370 | 0 | { |
3371 | 0 | gsize i; |
3372 | |
|
3373 | 0 | if (GVSB(builder)->magic == 0) |
3374 | | /* all-zeros or partial case */ |
3375 | 0 | return; |
3376 | | |
3377 | 0 | return_if_invalid_builder (builder); |
3378 | | |
3379 | 0 | g_variant_type_free (GVSB(builder)->type); |
3380 | |
|
3381 | 0 | for (i = 0; i < GVSB(builder)->offset; i++) |
3382 | 0 | g_variant_unref (GVSB(builder)->children[i]); |
3383 | |
|
3384 | 0 | g_free (GVSB(builder)->children); |
3385 | |
|
3386 | 0 | if (GVSB(builder)->parent) |
3387 | 0 | { |
3388 | 0 | g_variant_builder_clear (GVSB(builder)->parent); |
3389 | 0 | g_slice_free (GVariantBuilder, GVSB(builder)->parent); |
3390 | 0 | } |
3391 | |
|
3392 | 0 | memset (builder, 0, sizeof (GVariantBuilder)); |
3393 | 0 | } |
3394 | | |
3395 | | /** |
3396 | | * g_variant_builder_init: (skip) |
3397 | | * @builder: a #GVariantBuilder |
3398 | | * @type: a container type |
3399 | | * |
3400 | | * Initialises a #GVariantBuilder structure. |
3401 | | * |
3402 | | * @type must be non-%NULL. It specifies the type of container to |
3403 | | * construct. It can be an indefinite type such as |
3404 | | * %G_VARIANT_TYPE_ARRAY or a definite type such as "as" or "(ii)". |
3405 | | * Maybe, array, tuple, dictionary entry and variant-typed values may be |
3406 | | * constructed. |
3407 | | * |
3408 | | * After the builder is initialised, values are added using |
3409 | | * g_variant_builder_add_value() or g_variant_builder_add(). |
3410 | | * |
3411 | | * After all the child values are added, g_variant_builder_end() frees |
3412 | | * the memory associated with the builder and returns the #GVariant that |
3413 | | * was created. |
3414 | | * |
3415 | | * This function completely ignores the previous contents of @builder. |
3416 | | * On one hand this means that it is valid to pass in completely |
3417 | | * uninitialised memory. On the other hand, this means that if you are |
3418 | | * initialising over top of an existing #GVariantBuilder you need to |
3419 | | * first call g_variant_builder_clear() in order to avoid leaking |
3420 | | * memory. |
3421 | | * |
3422 | | * You must not call g_variant_builder_ref() or |
3423 | | * g_variant_builder_unref() on a #GVariantBuilder that was initialised |
3424 | | * with this function. If you ever pass a reference to a |
3425 | | * #GVariantBuilder outside of the control of your own code then you |
3426 | | * should assume that the person receiving that reference may try to use |
3427 | | * reference counting; you should use g_variant_builder_new() instead of |
3428 | | * this function. |
3429 | | * |
3430 | | * Since: 2.24 |
3431 | | **/ |
3432 | | void |
3433 | | g_variant_builder_init (GVariantBuilder *builder, |
3434 | | const GVariantType *type) |
3435 | 0 | { |
3436 | 0 | g_return_if_fail (type != NULL); |
3437 | 0 | g_return_if_fail (g_variant_type_is_container (type)); |
3438 | | |
3439 | 0 | memset (builder, 0, sizeof (GVariantBuilder)); |
3440 | |
|
3441 | 0 | GVSB(builder)->type = g_variant_type_copy (type); |
3442 | 0 | GVSB(builder)->magic = GVSB_MAGIC; |
3443 | 0 | GVSB(builder)->trusted = TRUE; |
3444 | |
|
3445 | 0 | switch (*(const gchar *) type) |
3446 | 0 | { |
3447 | 0 | case G_VARIANT_CLASS_VARIANT: |
3448 | 0 | GVSB(builder)->uniform_item_types = TRUE; |
3449 | 0 | GVSB(builder)->allocated_children = 1; |
3450 | 0 | GVSB(builder)->expected_type = NULL; |
3451 | 0 | GVSB(builder)->min_items = 1; |
3452 | 0 | GVSB(builder)->max_items = 1; |
3453 | 0 | break; |
3454 | | |
3455 | 0 | case G_VARIANT_CLASS_ARRAY: |
3456 | 0 | GVSB(builder)->uniform_item_types = TRUE; |
3457 | 0 | GVSB(builder)->allocated_children = 8; |
3458 | 0 | GVSB(builder)->expected_type = |
3459 | 0 | g_variant_type_element (GVSB(builder)->type); |
3460 | 0 | GVSB(builder)->min_items = 0; |
3461 | 0 | GVSB(builder)->max_items = -1; |
3462 | 0 | break; |
3463 | | |
3464 | 0 | case G_VARIANT_CLASS_MAYBE: |
3465 | 0 | GVSB(builder)->uniform_item_types = TRUE; |
3466 | 0 | GVSB(builder)->allocated_children = 1; |
3467 | 0 | GVSB(builder)->expected_type = |
3468 | 0 | g_variant_type_element (GVSB(builder)->type); |
3469 | 0 | GVSB(builder)->min_items = 0; |
3470 | 0 | GVSB(builder)->max_items = 1; |
3471 | 0 | break; |
3472 | | |
3473 | 0 | case G_VARIANT_CLASS_DICT_ENTRY: |
3474 | 0 | GVSB(builder)->uniform_item_types = FALSE; |
3475 | 0 | GVSB(builder)->allocated_children = 2; |
3476 | 0 | GVSB(builder)->expected_type = |
3477 | 0 | g_variant_type_key (GVSB(builder)->type); |
3478 | 0 | GVSB(builder)->min_items = 2; |
3479 | 0 | GVSB(builder)->max_items = 2; |
3480 | 0 | break; |
3481 | | |
3482 | 0 | case 'r': /* G_VARIANT_TYPE_TUPLE was given */ |
3483 | 0 | GVSB(builder)->uniform_item_types = FALSE; |
3484 | 0 | GVSB(builder)->allocated_children = 8; |
3485 | 0 | GVSB(builder)->expected_type = NULL; |
3486 | 0 | GVSB(builder)->min_items = 0; |
3487 | 0 | GVSB(builder)->max_items = -1; |
3488 | 0 | break; |
3489 | | |
3490 | 0 | case G_VARIANT_CLASS_TUPLE: /* a definite tuple type was given */ |
3491 | 0 | GVSB(builder)->allocated_children = g_variant_type_n_items (type); |
3492 | 0 | GVSB(builder)->expected_type = |
3493 | 0 | g_variant_type_first (GVSB(builder)->type); |
3494 | 0 | GVSB(builder)->min_items = GVSB(builder)->allocated_children; |
3495 | 0 | GVSB(builder)->max_items = GVSB(builder)->allocated_children; |
3496 | 0 | GVSB(builder)->uniform_item_types = FALSE; |
3497 | 0 | break; |
3498 | | |
3499 | 0 | default: |
3500 | 0 | g_assert_not_reached (); |
3501 | 0 | } |
3502 | | |
3503 | 0 | #ifdef G_ANALYZER_ANALYZING |
3504 | | /* Static analysers can’t couple the code in g_variant_builder_init() to the |
3505 | | * code in g_variant_builder_end() by GVariantType, so end up assuming that |
3506 | | * @offset and @children mismatch and that uninitialised memory is accessed |
3507 | | * from @children. At runtime, this is caught by the preconditions at the top |
3508 | | * of g_variant_builder_end(). Help the analyser by zero-initialising the |
3509 | | * memory to avoid a false positive. */ |
3510 | 0 | GVSB(builder)->children = g_new0 (GVariant *, |
3511 | 0 | GVSB(builder)->allocated_children); |
3512 | | #else |
3513 | | GVSB(builder)->children = g_new (GVariant *, |
3514 | | GVSB(builder)->allocated_children); |
3515 | | #endif |
3516 | 0 | } |
3517 | | |
3518 | | static void |
3519 | | g_variant_builder_make_room (struct stack_builder *builder) |
3520 | 0 | { |
3521 | 0 | if (builder->offset == builder->allocated_children) |
3522 | 0 | { |
3523 | 0 | builder->allocated_children *= 2; |
3524 | 0 | builder->children = g_renew (GVariant *, builder->children, |
3525 | 0 | builder->allocated_children); |
3526 | 0 | } |
3527 | 0 | } |
3528 | | |
3529 | | /** |
3530 | | * g_variant_builder_add_value: |
3531 | | * @builder: a #GVariantBuilder |
3532 | | * @value: a #GVariant |
3533 | | * |
3534 | | * Adds @value to @builder. |
3535 | | * |
3536 | | * It is an error to call this function in any way that would create an |
3537 | | * inconsistent value to be constructed. Some examples of this are |
3538 | | * putting different types of items into an array, putting the wrong |
3539 | | * types or number of items in a tuple, putting more than one value into |
3540 | | * a variant, etc. |
3541 | | * |
3542 | | * If @value is a floating reference (see g_variant_ref_sink()), |
3543 | | * the @builder instance takes ownership of @value. |
3544 | | * |
3545 | | * Since: 2.24 |
3546 | | **/ |
3547 | | void |
3548 | | g_variant_builder_add_value (GVariantBuilder *builder, |
3549 | | GVariant *value) |
3550 | 0 | { |
3551 | 0 | return_if_invalid_builder (builder); |
3552 | 0 | g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items); |
3553 | 0 | g_return_if_fail (!GVSB(builder)->expected_type || |
3554 | 0 | g_variant_is_of_type (value, |
3555 | 0 | GVSB(builder)->expected_type)); |
3556 | 0 | g_return_if_fail (!GVSB(builder)->prev_item_type || |
3557 | 0 | g_variant_is_of_type (value, |
3558 | 0 | GVSB(builder)->prev_item_type)); |
3559 | | |
3560 | 0 | GVSB(builder)->trusted &= g_variant_is_trusted (value); |
3561 | |
|
3562 | 0 | if (!GVSB(builder)->uniform_item_types) |
3563 | 0 | { |
3564 | | /* advance our expected type pointers */ |
3565 | 0 | if (GVSB(builder)->expected_type) |
3566 | 0 | GVSB(builder)->expected_type = |
3567 | 0 | g_variant_type_next (GVSB(builder)->expected_type); |
3568 | |
|
3569 | 0 | if (GVSB(builder)->prev_item_type) |
3570 | 0 | GVSB(builder)->prev_item_type = |
3571 | 0 | g_variant_type_next (GVSB(builder)->prev_item_type); |
3572 | 0 | } |
3573 | 0 | else |
3574 | 0 | GVSB(builder)->prev_item_type = g_variant_get_type (value); |
3575 | |
|
3576 | 0 | g_variant_builder_make_room (GVSB(builder)); |
3577 | |
|
3578 | 0 | GVSB(builder)->children[GVSB(builder)->offset++] = |
3579 | 0 | g_variant_ref_sink (value); |
3580 | 0 | } |
3581 | | |
3582 | | /** |
3583 | | * g_variant_builder_open: |
3584 | | * @builder: a #GVariantBuilder |
3585 | | * @type: the #GVariantType of the container |
3586 | | * |
3587 | | * Opens a subcontainer inside the given @builder. When done adding |
3588 | | * items to the subcontainer, g_variant_builder_close() must be called. @type |
3589 | | * is the type of the container: so to build a tuple of several values, @type |
3590 | | * must include the tuple itself. |
3591 | | * |
3592 | | * It is an error to call this function in any way that would cause an |
3593 | | * inconsistent value to be constructed (ie: adding too many values or |
3594 | | * a value of an incorrect type). |
3595 | | * |
3596 | | * Example of building a nested variant: |
3597 | | * |[<!-- language="C" --> |
3598 | | * GVariantBuilder builder; |
3599 | | * guint32 some_number = get_number (); |
3600 | | * g_autoptr (GHashTable) some_dict = get_dict (); |
3601 | | * GHashTableIter iter; |
3602 | | * const gchar *key; |
3603 | | * const GVariant *value; |
3604 | | * g_autoptr (GVariant) output = NULL; |
3605 | | * |
3606 | | * g_variant_builder_init (&builder, G_VARIANT_TYPE ("(ua{sv})")); |
3607 | | * g_variant_builder_add (&builder, "u", some_number); |
3608 | | * g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}")); |
3609 | | * |
3610 | | * g_hash_table_iter_init (&iter, some_dict); |
3611 | | * while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) |
3612 | | * { |
3613 | | * g_variant_builder_open (&builder, G_VARIANT_TYPE ("{sv}")); |
3614 | | * g_variant_builder_add (&builder, "s", key); |
3615 | | * g_variant_builder_add (&builder, "v", value); |
3616 | | * g_variant_builder_close (&builder); |
3617 | | * } |
3618 | | * |
3619 | | * g_variant_builder_close (&builder); |
3620 | | * |
3621 | | * output = g_variant_builder_end (&builder); |
3622 | | * ]| |
3623 | | * |
3624 | | * Since: 2.24 |
3625 | | **/ |
3626 | | void |
3627 | | g_variant_builder_open (GVariantBuilder *builder, |
3628 | | const GVariantType *type) |
3629 | 0 | { |
3630 | 0 | GVariantBuilder *parent; |
3631 | |
|
3632 | 0 | return_if_invalid_builder (builder); |
3633 | 0 | g_return_if_fail (GVSB(builder)->offset < GVSB(builder)->max_items); |
3634 | 0 | g_return_if_fail (!GVSB(builder)->expected_type || |
3635 | 0 | g_variant_type_is_subtype_of (type, |
3636 | 0 | GVSB(builder)->expected_type)); |
3637 | 0 | g_return_if_fail (!GVSB(builder)->prev_item_type || |
3638 | 0 | g_variant_type_is_subtype_of (GVSB(builder)->prev_item_type, |
3639 | 0 | type)); |
3640 | | |
3641 | 0 | parent = g_slice_dup (GVariantBuilder, builder); |
3642 | 0 | g_variant_builder_init (builder, type); |
3643 | 0 | GVSB(builder)->parent = parent; |
3644 | | |
3645 | | /* push the prev_item_type down into the subcontainer */ |
3646 | 0 | if (GVSB(parent)->prev_item_type) |
3647 | 0 | { |
3648 | 0 | if (!GVSB(builder)->uniform_item_types) |
3649 | | /* tuples and dict entries */ |
3650 | 0 | GVSB(builder)->prev_item_type = |
3651 | 0 | g_variant_type_first (GVSB(parent)->prev_item_type); |
3652 | | |
3653 | 0 | else if (!g_variant_type_is_variant (GVSB(builder)->type)) |
3654 | | /* maybes and arrays */ |
3655 | 0 | GVSB(builder)->prev_item_type = |
3656 | 0 | g_variant_type_element (GVSB(parent)->prev_item_type); |
3657 | 0 | } |
3658 | 0 | } |
3659 | | |
3660 | | /** |
3661 | | * g_variant_builder_close: |
3662 | | * @builder: a #GVariantBuilder |
3663 | | * |
3664 | | * Closes the subcontainer inside the given @builder that was opened by |
3665 | | * the most recent call to g_variant_builder_open(). |
3666 | | * |
3667 | | * It is an error to call this function in any way that would create an |
3668 | | * inconsistent value to be constructed (ie: too few values added to the |
3669 | | * subcontainer). |
3670 | | * |
3671 | | * Since: 2.24 |
3672 | | **/ |
3673 | | void |
3674 | | g_variant_builder_close (GVariantBuilder *builder) |
3675 | 0 | { |
3676 | 0 | GVariantBuilder *parent; |
3677 | |
|
3678 | 0 | return_if_invalid_builder (builder); |
3679 | 0 | g_return_if_fail (GVSB(builder)->parent != NULL); |
3680 | | |
3681 | 0 | parent = GVSB(builder)->parent; |
3682 | 0 | GVSB(builder)->parent = NULL; |
3683 | |
|
3684 | 0 | g_variant_builder_add_value (parent, g_variant_builder_end (builder)); |
3685 | 0 | *builder = *parent; |
3686 | |
|
3687 | 0 | g_slice_free (GVariantBuilder, parent); |
3688 | 0 | } |
3689 | | |
3690 | | /*< private > |
3691 | | * g_variant_make_maybe_type: |
3692 | | * @element: a #GVariant |
3693 | | * |
3694 | | * Return the type of a maybe containing @element. |
3695 | | */ |
3696 | | static GVariantType * |
3697 | | g_variant_make_maybe_type (GVariant *element) |
3698 | 0 | { |
3699 | 0 | return g_variant_type_new_maybe (g_variant_get_type (element)); |
3700 | 0 | } |
3701 | | |
3702 | | /*< private > |
3703 | | * g_variant_make_array_type: |
3704 | | * @element: a #GVariant |
3705 | | * |
3706 | | * Return the type of an array containing @element. |
3707 | | */ |
3708 | | static GVariantType * |
3709 | | g_variant_make_array_type (GVariant *element) |
3710 | 0 | { |
3711 | 0 | return g_variant_type_new_array (g_variant_get_type (element)); |
3712 | 0 | } |
3713 | | |
3714 | | /** |
3715 | | * g_variant_builder_end: |
3716 | | * @builder: a #GVariantBuilder |
3717 | | * |
3718 | | * Ends the builder process and returns the constructed value. |
3719 | | * |
3720 | | * It is not permissible to use @builder in any way after this call |
3721 | | * except for reference counting operations (in the case of a |
3722 | | * heap-allocated #GVariantBuilder) or by reinitialising it with |
3723 | | * g_variant_builder_init() (in the case of stack-allocated). This |
3724 | | * means that for the stack-allocated builders there is no need to |
3725 | | * call g_variant_builder_clear() after the call to |
3726 | | * g_variant_builder_end(). |
3727 | | * |
3728 | | * It is an error to call this function in any way that would create an |
3729 | | * inconsistent value to be constructed (ie: insufficient number of |
3730 | | * items added to a container with a specific number of children |
3731 | | * required). It is also an error to call this function if the builder |
3732 | | * was created with an indefinite array or maybe type and no children |
3733 | | * have been added; in this case it is impossible to infer the type of |
3734 | | * the empty array. |
3735 | | * |
3736 | | * Returns: (transfer none): a new, floating, #GVariant |
3737 | | * |
3738 | | * Since: 2.24 |
3739 | | **/ |
3740 | | GVariant * |
3741 | | g_variant_builder_end (GVariantBuilder *builder) |
3742 | 0 | { |
3743 | 0 | GVariantType *my_type; |
3744 | 0 | GVariant *value; |
3745 | |
|
3746 | 0 | return_val_if_invalid_builder (builder, NULL); |
3747 | 0 | g_return_val_if_fail (GVSB(builder)->offset >= GVSB(builder)->min_items, |
3748 | 0 | NULL); |
3749 | 0 | g_return_val_if_fail (!GVSB(builder)->uniform_item_types || |
3750 | 0 | GVSB(builder)->prev_item_type != NULL || |
3751 | 0 | g_variant_type_is_definite (GVSB(builder)->type), |
3752 | 0 | NULL); |
3753 | | |
3754 | 0 | if (g_variant_type_is_definite (GVSB(builder)->type)) |
3755 | 0 | my_type = g_variant_type_copy (GVSB(builder)->type); |
3756 | | |
3757 | 0 | else if (g_variant_type_is_maybe (GVSB(builder)->type)) |
3758 | 0 | my_type = g_variant_make_maybe_type (GVSB(builder)->children[0]); |
3759 | | |
3760 | 0 | else if (g_variant_type_is_array (GVSB(builder)->type)) |
3761 | 0 | my_type = g_variant_make_array_type (GVSB(builder)->children[0]); |
3762 | | |
3763 | 0 | else if (g_variant_type_is_tuple (GVSB(builder)->type)) |
3764 | 0 | my_type = g_variant_make_tuple_type (GVSB(builder)->children, |
3765 | 0 | GVSB(builder)->offset); |
3766 | | |
3767 | 0 | else if (g_variant_type_is_dict_entry (GVSB(builder)->type)) |
3768 | 0 | my_type = g_variant_make_dict_entry_type (GVSB(builder)->children[0], |
3769 | 0 | GVSB(builder)->children[1]); |
3770 | 0 | else |
3771 | 0 | g_assert_not_reached (); |
3772 | | |
3773 | 0 | value = g_variant_new_from_children (my_type, |
3774 | 0 | g_renew (GVariant *, |
3775 | 0 | GVSB(builder)->children, |
3776 | 0 | GVSB(builder)->offset), |
3777 | 0 | GVSB(builder)->offset, |
3778 | 0 | GVSB(builder)->trusted); |
3779 | 0 | GVSB(builder)->children = NULL; |
3780 | 0 | GVSB(builder)->offset = 0; |
3781 | |
|
3782 | 0 | g_variant_builder_clear (builder); |
3783 | 0 | g_variant_type_free (my_type); |
3784 | |
|
3785 | 0 | return value; |
3786 | 0 | } |
3787 | | |
3788 | | /* GVariantDict {{{1 */ |
3789 | | |
3790 | | /** |
3791 | | * GVariantDict: |
3792 | | * |
3793 | | * #GVariantDict is a mutable interface to #GVariant dictionaries. |
3794 | | * |
3795 | | * It can be used for doing a sequence of dictionary lookups in an |
3796 | | * efficient way on an existing #GVariant dictionary or it can be used |
3797 | | * to construct new dictionaries with a hashtable-like interface. It |
3798 | | * can also be used for taking existing dictionaries and modifying them |
3799 | | * in order to create new ones. |
3800 | | * |
3801 | | * #GVariantDict can only be used with %G_VARIANT_TYPE_VARDICT |
3802 | | * dictionaries. |
3803 | | * |
3804 | | * It is possible to use #GVariantDict allocated on the stack or on the |
3805 | | * heap. When using a stack-allocated #GVariantDict, you begin with a |
3806 | | * call to g_variant_dict_init() and free the resources with a call to |
3807 | | * g_variant_dict_clear(). |
3808 | | * |
3809 | | * Heap-allocated #GVariantDict follows normal refcounting rules: you |
3810 | | * allocate it with g_variant_dict_new() and use g_variant_dict_ref() |
3811 | | * and g_variant_dict_unref(). |
3812 | | * |
3813 | | * g_variant_dict_end() is used to convert the #GVariantDict back into a |
3814 | | * dictionary-type #GVariant. When used with stack-allocated instances, |
3815 | | * this also implicitly frees all associated memory, but for |
3816 | | * heap-allocated instances, you must still call g_variant_dict_unref() |
3817 | | * afterwards. |
3818 | | * |
3819 | | * You will typically want to use a heap-allocated #GVariantDict when |
3820 | | * you expose it as part of an API. For most other uses, the |
3821 | | * stack-allocated form will be more convenient. |
3822 | | * |
3823 | | * Consider the following two examples that do the same thing in each |
3824 | | * style: take an existing dictionary and look up the "count" uint32 |
3825 | | * key, adding 1 to it if it is found, or returning an error if the |
3826 | | * key is not found. Each returns the new dictionary as a floating |
3827 | | * #GVariant. |
3828 | | * |
3829 | | * ## Using a stack-allocated GVariantDict |
3830 | | * |
3831 | | * |[<!-- language="C" --> |
3832 | | * GVariant * |
3833 | | * add_to_count (GVariant *orig, |
3834 | | * GError **error) |
3835 | | * { |
3836 | | * GVariantDict dict; |
3837 | | * guint32 count; |
3838 | | * |
3839 | | * g_variant_dict_init (&dict, orig); |
3840 | | * if (!g_variant_dict_lookup (&dict, "count", "u", &count)) |
3841 | | * { |
3842 | | * g_set_error (...); |
3843 | | * g_variant_dict_clear (&dict); |
3844 | | * return NULL; |
3845 | | * } |
3846 | | * |
3847 | | * g_variant_dict_insert (&dict, "count", "u", count + 1); |
3848 | | * |
3849 | | * return g_variant_dict_end (&dict); |
3850 | | * } |
3851 | | * ]| |
3852 | | * |
3853 | | * ## Using heap-allocated GVariantDict |
3854 | | * |
3855 | | * |[<!-- language="C" --> |
3856 | | * GVariant * |
3857 | | * add_to_count (GVariant *orig, |
3858 | | * GError **error) |
3859 | | * { |
3860 | | * GVariantDict *dict; |
3861 | | * GVariant *result; |
3862 | | * guint32 count; |
3863 | | * |
3864 | | * dict = g_variant_dict_new (orig); |
3865 | | * |
3866 | | * if (g_variant_dict_lookup (dict, "count", "u", &count)) |
3867 | | * { |
3868 | | * g_variant_dict_insert (dict, "count", "u", count + 1); |
3869 | | * result = g_variant_dict_end (dict); |
3870 | | * } |
3871 | | * else |
3872 | | * { |
3873 | | * g_set_error (...); |
3874 | | * result = NULL; |
3875 | | * } |
3876 | | * |
3877 | | * g_variant_dict_unref (dict); |
3878 | | * |
3879 | | * return result; |
3880 | | * } |
3881 | | * ]| |
3882 | | * |
3883 | | * Since: 2.40 |
3884 | | **/ |
3885 | | struct stack_dict |
3886 | | { |
3887 | | GHashTable *values; |
3888 | | gsize magic; |
3889 | | }; |
3890 | | |
3891 | | G_STATIC_ASSERT (sizeof (struct stack_dict) <= sizeof (GVariantDict)); |
3892 | | |
3893 | | struct heap_dict |
3894 | | { |
3895 | | struct stack_dict dict; |
3896 | | gint ref_count; |
3897 | | gsize magic; |
3898 | | }; |
3899 | | |
3900 | 0 | #define GVSD(d) ((struct stack_dict *) (d)) |
3901 | 0 | #define GVHD(d) ((struct heap_dict *) (d)) |
3902 | 0 | #define GVSD_MAGIC ((gsize) 2579507750u) |
3903 | 0 | #define GVSD_MAGIC_PARTIAL ((gsize) 3488698669u) |
3904 | 0 | #define GVHD_MAGIC ((gsize) 2450270775u) |
3905 | 0 | #define is_valid_dict(d) (GVSD(d)->magic == GVSD_MAGIC) |
3906 | | #define is_valid_heap_dict(d) (GVHD(d)->magic == GVHD_MAGIC) |
3907 | | |
3908 | | /* Just to make sure that by adding a union to GVariantDict, we didn't |
3909 | | * accidentally change ABI. */ |
3910 | | G_STATIC_ASSERT (sizeof (GVariantDict) == sizeof (gsize[16])); |
3911 | | |
3912 | | static gboolean |
3913 | | ensure_valid_dict (GVariantDict *dict) |
3914 | 0 | { |
3915 | 0 | if (dict == NULL) |
3916 | 0 | return FALSE; |
3917 | 0 | else if (is_valid_dict (dict)) |
3918 | 0 | return TRUE; |
3919 | 0 | if (dict->u.s.partial_magic == GVSD_MAGIC_PARTIAL) |
3920 | 0 | { |
3921 | 0 | static GVariantDict cleared_dict; |
3922 | | |
3923 | | /* Make sure that only first two fields were set and the rest is |
3924 | | * zeroed to avoid messing up the builder that had parent |
3925 | | * address equal to GVSB_MAGIC_PARTIAL. */ |
3926 | 0 | if (memcmp (cleared_dict.u.s.y, dict->u.s.y, sizeof cleared_dict.u.s.y)) |
3927 | 0 | return FALSE; |
3928 | | |
3929 | 0 | g_variant_dict_init (dict, dict->u.s.asv); |
3930 | 0 | } |
3931 | 0 | return is_valid_dict (dict); |
3932 | 0 | } |
3933 | | |
3934 | | /* return_if_invalid_dict (d) is like |
3935 | | * g_return_if_fail (ensure_valid_dict (d)), except that |
3936 | | * the side effects of ensure_valid_dict are evaluated |
3937 | | * regardless of whether G_DISABLE_CHECKS is defined or not. */ |
3938 | 0 | #define return_if_invalid_dict(d) G_STMT_START { \ |
3939 | 0 | gboolean valid_dict G_GNUC_UNUSED = ensure_valid_dict (d); \ |
3940 | 0 | g_return_if_fail (valid_dict); \ |
3941 | 0 | } G_STMT_END |
3942 | | |
3943 | | /* return_val_if_invalid_dict (d, val) is like |
3944 | | * g_return_val_if_fail (ensure_valid_dict (d), val), except that |
3945 | | * the side effects of ensure_valid_dict are evaluated |
3946 | | * regardless of whether G_DISABLE_CHECKS is defined or not. */ |
3947 | 0 | #define return_val_if_invalid_dict(d, val) G_STMT_START { \ |
3948 | 0 | gboolean valid_dict G_GNUC_UNUSED = ensure_valid_dict (d); \ |
3949 | 0 | g_return_val_if_fail (valid_dict, val); \ |
3950 | 0 | } G_STMT_END |
3951 | | |
3952 | | /** |
3953 | | * g_variant_dict_new: |
3954 | | * @from_asv: (nullable): the #GVariant with which to initialise the |
3955 | | * dictionary |
3956 | | * |
3957 | | * Allocates and initialises a new #GVariantDict. |
3958 | | * |
3959 | | * You should call g_variant_dict_unref() on the return value when it |
3960 | | * is no longer needed. The memory will not be automatically freed by |
3961 | | * any other call. |
3962 | | * |
3963 | | * In some cases it may be easier to place a #GVariantDict directly on |
3964 | | * the stack of the calling function and initialise it with |
3965 | | * g_variant_dict_init(). This is particularly useful when you are |
3966 | | * using #GVariantDict to construct a #GVariant. |
3967 | | * |
3968 | | * Returns: (transfer full): a #GVariantDict |
3969 | | * |
3970 | | * Since: 2.40 |
3971 | | **/ |
3972 | | GVariantDict * |
3973 | | g_variant_dict_new (GVariant *from_asv) |
3974 | 0 | { |
3975 | 0 | GVariantDict *dict; |
3976 | |
|
3977 | 0 | dict = g_slice_alloc (sizeof (struct heap_dict)); |
3978 | 0 | g_variant_dict_init (dict, from_asv); |
3979 | 0 | GVHD(dict)->magic = GVHD_MAGIC; |
3980 | 0 | GVHD(dict)->ref_count = 1; |
3981 | |
|
3982 | 0 | return dict; |
3983 | 0 | } |
3984 | | |
3985 | | /** |
3986 | | * g_variant_dict_init: (skip) |
3987 | | * @dict: a #GVariantDict |
3988 | | * @from_asv: (nullable): the initial value for @dict |
3989 | | * |
3990 | | * Initialises a #GVariantDict structure. |
3991 | | * |
3992 | | * If @from_asv is given, it is used to initialise the dictionary. |
3993 | | * |
3994 | | * This function completely ignores the previous contents of @dict. On |
3995 | | * one hand this means that it is valid to pass in completely |
3996 | | * uninitialised memory. On the other hand, this means that if you are |
3997 | | * initialising over top of an existing #GVariantDict you need to first |
3998 | | * call g_variant_dict_clear() in order to avoid leaking memory. |
3999 | | * |
4000 | | * You must not call g_variant_dict_ref() or g_variant_dict_unref() on a |
4001 | | * #GVariantDict that was initialised with this function. If you ever |
4002 | | * pass a reference to a #GVariantDict outside of the control of your |
4003 | | * own code then you should assume that the person receiving that |
4004 | | * reference may try to use reference counting; you should use |
4005 | | * g_variant_dict_new() instead of this function. |
4006 | | * |
4007 | | * Since: 2.40 |
4008 | | **/ |
4009 | | void |
4010 | | g_variant_dict_init (GVariantDict *dict, |
4011 | | GVariant *from_asv) |
4012 | 0 | { |
4013 | 0 | GVariantIter iter; |
4014 | 0 | gchar *key; |
4015 | 0 | GVariant *value; |
4016 | |
|
4017 | 0 | GVSD(dict)->values = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref); |
4018 | 0 | GVSD(dict)->magic = GVSD_MAGIC; |
4019 | |
|
4020 | 0 | if (from_asv) |
4021 | 0 | { |
4022 | 0 | g_variant_iter_init (&iter, from_asv); |
4023 | 0 | while (g_variant_iter_next (&iter, "{sv}", &key, &value)) |
4024 | 0 | g_hash_table_insert (GVSD(dict)->values, key, value); |
4025 | 0 | } |
4026 | 0 | } |
4027 | | |
4028 | | /** |
4029 | | * g_variant_dict_lookup: |
4030 | | * @dict: a #GVariantDict |
4031 | | * @key: the key to look up in the dictionary |
4032 | | * @format_string: a GVariant format string |
4033 | | * @...: the arguments to unpack the value into |
4034 | | * |
4035 | | * Looks up a value in a #GVariantDict. |
4036 | | * |
4037 | | * This function is a wrapper around g_variant_dict_lookup_value() and |
4038 | | * g_variant_get(). In the case that %NULL would have been returned, |
4039 | | * this function returns %FALSE and does not modify the values of the arguments |
4040 | | * passed in to @.... Otherwise, it unpacks the returned |
4041 | | * value and returns %TRUE. |
4042 | | * |
4043 | | * @format_string determines the C types that are used for unpacking the |
4044 | | * values and also determines if the values are copied or borrowed, see the |
4045 | | * section on [GVariant format strings][gvariant-format-strings-pointers]. |
4046 | | * |
4047 | | * Returns: %TRUE if a value was unpacked |
4048 | | * |
4049 | | * Since: 2.40 |
4050 | | **/ |
4051 | | gboolean |
4052 | | g_variant_dict_lookup (GVariantDict *dict, |
4053 | | const gchar *key, |
4054 | | const gchar *format_string, |
4055 | | ...) |
4056 | 0 | { |
4057 | 0 | GVariant *value; |
4058 | 0 | va_list ap; |
4059 | |
|
4060 | 0 | return_val_if_invalid_dict (dict, FALSE); |
4061 | 0 | g_return_val_if_fail (key != NULL, FALSE); |
4062 | 0 | g_return_val_if_fail (format_string != NULL, FALSE); |
4063 | | |
4064 | 0 | value = g_hash_table_lookup (GVSD(dict)->values, key); |
4065 | |
|
4066 | 0 | if (value == NULL || !g_variant_check_format_string (value, format_string, FALSE)) |
4067 | 0 | return FALSE; |
4068 | | |
4069 | 0 | va_start (ap, format_string); |
4070 | 0 | g_variant_get_va (value, format_string, NULL, &ap); |
4071 | 0 | va_end (ap); |
4072 | |
|
4073 | 0 | return TRUE; |
4074 | 0 | } |
4075 | | |
4076 | | /** |
4077 | | * g_variant_dict_lookup_value: |
4078 | | * @dict: a #GVariantDict |
4079 | | * @key: the key to look up in the dictionary |
4080 | | * @expected_type: (nullable): a #GVariantType, or %NULL |
4081 | | * |
4082 | | * Looks up a value in a #GVariantDict. |
4083 | | * |
4084 | | * If @key is not found in @dictionary, %NULL is returned. |
4085 | | * |
4086 | | * The @expected_type string specifies what type of value is expected. |
4087 | | * If the value associated with @key has a different type then %NULL is |
4088 | | * returned. |
4089 | | * |
4090 | | * If the key is found and the value has the correct type, it is |
4091 | | * returned. If @expected_type was specified then any non-%NULL return |
4092 | | * value will have this type. |
4093 | | * |
4094 | | * Returns: (transfer full) (nullable): the value of the dictionary key, or %NULL |
4095 | | * |
4096 | | * Since: 2.40 |
4097 | | **/ |
4098 | | GVariant * |
4099 | | g_variant_dict_lookup_value (GVariantDict *dict, |
4100 | | const gchar *key, |
4101 | | const GVariantType *expected_type) |
4102 | 0 | { |
4103 | 0 | GVariant *result; |
4104 | |
|
4105 | 0 | return_val_if_invalid_dict (dict, NULL); |
4106 | 0 | g_return_val_if_fail (key != NULL, NULL); |
4107 | | |
4108 | 0 | result = g_hash_table_lookup (GVSD(dict)->values, key); |
4109 | |
|
4110 | 0 | if (result && (!expected_type || g_variant_is_of_type (result, expected_type))) |
4111 | 0 | return g_variant_ref (result); |
4112 | | |
4113 | 0 | return NULL; |
4114 | 0 | } |
4115 | | |
4116 | | /** |
4117 | | * g_variant_dict_contains: |
4118 | | * @dict: a #GVariantDict |
4119 | | * @key: the key to look up in the dictionary |
4120 | | * |
4121 | | * Checks if @key exists in @dict. |
4122 | | * |
4123 | | * Returns: %TRUE if @key is in @dict |
4124 | | * |
4125 | | * Since: 2.40 |
4126 | | **/ |
4127 | | gboolean |
4128 | | g_variant_dict_contains (GVariantDict *dict, |
4129 | | const gchar *key) |
4130 | 0 | { |
4131 | 0 | return_val_if_invalid_dict (dict, FALSE); |
4132 | 0 | g_return_val_if_fail (key != NULL, FALSE); |
4133 | | |
4134 | 0 | return g_hash_table_contains (GVSD(dict)->values, key); |
4135 | 0 | } |
4136 | | |
4137 | | /** |
4138 | | * g_variant_dict_insert: |
4139 | | * @dict: a #GVariantDict |
4140 | | * @key: the key to insert a value for |
4141 | | * @format_string: a #GVariant varargs format string |
4142 | | * @...: arguments, as per @format_string |
4143 | | * |
4144 | | * Inserts a value into a #GVariantDict. |
4145 | | * |
4146 | | * This call is a convenience wrapper that is exactly equivalent to |
4147 | | * calling g_variant_new() followed by g_variant_dict_insert_value(). |
4148 | | * |
4149 | | * Since: 2.40 |
4150 | | **/ |
4151 | | void |
4152 | | g_variant_dict_insert (GVariantDict *dict, |
4153 | | const gchar *key, |
4154 | | const gchar *format_string, |
4155 | | ...) |
4156 | 0 | { |
4157 | 0 | va_list ap; |
4158 | |
|
4159 | 0 | return_if_invalid_dict (dict); |
4160 | 0 | g_return_if_fail (key != NULL); |
4161 | 0 | g_return_if_fail (format_string != NULL); |
4162 | | |
4163 | 0 | va_start (ap, format_string); |
4164 | 0 | g_variant_dict_insert_value (dict, key, g_variant_new_va (format_string, NULL, &ap)); |
4165 | 0 | va_end (ap); |
4166 | 0 | } |
4167 | | |
4168 | | /** |
4169 | | * g_variant_dict_insert_value: |
4170 | | * @dict: a #GVariantDict |
4171 | | * @key: the key to insert a value for |
4172 | | * @value: the value to insert |
4173 | | * |
4174 | | * Inserts (or replaces) a key in a #GVariantDict. |
4175 | | * |
4176 | | * @value is consumed if it is floating. |
4177 | | * |
4178 | | * Since: 2.40 |
4179 | | **/ |
4180 | | void |
4181 | | g_variant_dict_insert_value (GVariantDict *dict, |
4182 | | const gchar *key, |
4183 | | GVariant *value) |
4184 | 0 | { |
4185 | 0 | return_if_invalid_dict (dict); |
4186 | 0 | g_return_if_fail (key != NULL); |
4187 | 0 | g_return_if_fail (value != NULL); |
4188 | | |
4189 | 0 | g_hash_table_insert (GVSD(dict)->values, g_strdup (key), g_variant_ref_sink (value)); |
4190 | 0 | } |
4191 | | |
4192 | | /** |
4193 | | * g_variant_dict_remove: |
4194 | | * @dict: a #GVariantDict |
4195 | | * @key: the key to remove |
4196 | | * |
4197 | | * Removes a key and its associated value from a #GVariantDict. |
4198 | | * |
4199 | | * Returns: %TRUE if the key was found and removed |
4200 | | * |
4201 | | * Since: 2.40 |
4202 | | **/ |
4203 | | gboolean |
4204 | | g_variant_dict_remove (GVariantDict *dict, |
4205 | | const gchar *key) |
4206 | 0 | { |
4207 | 0 | return_val_if_invalid_dict (dict, FALSE); |
4208 | 0 | g_return_val_if_fail (key != NULL, FALSE); |
4209 | | |
4210 | 0 | return g_hash_table_remove (GVSD(dict)->values, key); |
4211 | 0 | } |
4212 | | |
4213 | | /** |
4214 | | * g_variant_dict_clear: |
4215 | | * @dict: a #GVariantDict |
4216 | | * |
4217 | | * Releases all memory associated with a #GVariantDict without freeing |
4218 | | * the #GVariantDict structure itself. |
4219 | | * |
4220 | | * It typically only makes sense to do this on a stack-allocated |
4221 | | * #GVariantDict if you want to abort building the value part-way |
4222 | | * through. This function need not be called if you call |
4223 | | * g_variant_dict_end() and it also doesn't need to be called on dicts |
4224 | | * allocated with g_variant_dict_new (see g_variant_dict_unref() for |
4225 | | * that). |
4226 | | * |
4227 | | * It is valid to call this function on either an initialised |
4228 | | * #GVariantDict or one that was previously cleared by an earlier call |
4229 | | * to g_variant_dict_clear() but it is not valid to call this function |
4230 | | * on uninitialised memory. |
4231 | | * |
4232 | | * Since: 2.40 |
4233 | | **/ |
4234 | | void |
4235 | | g_variant_dict_clear (GVariantDict *dict) |
4236 | 0 | { |
4237 | 0 | if (GVSD(dict)->magic == 0) |
4238 | | /* all-zeros case */ |
4239 | 0 | return; |
4240 | | |
4241 | 0 | return_if_invalid_dict (dict); |
4242 | | |
4243 | 0 | g_hash_table_unref (GVSD(dict)->values); |
4244 | 0 | GVSD(dict)->values = NULL; |
4245 | |
|
4246 | 0 | GVSD(dict)->magic = 0; |
4247 | 0 | } |
4248 | | |
4249 | | /** |
4250 | | * g_variant_dict_end: |
4251 | | * @dict: a #GVariantDict |
4252 | | * |
4253 | | * Returns the current value of @dict as a #GVariant of type |
4254 | | * %G_VARIANT_TYPE_VARDICT, clearing it in the process. |
4255 | | * |
4256 | | * It is not permissible to use @dict in any way after this call except |
4257 | | * for reference counting operations (in the case of a heap-allocated |
4258 | | * #GVariantDict) or by reinitialising it with g_variant_dict_init() (in |
4259 | | * the case of stack-allocated). |
4260 | | * |
4261 | | * Returns: (transfer none): a new, floating, #GVariant |
4262 | | * |
4263 | | * Since: 2.40 |
4264 | | **/ |
4265 | | GVariant * |
4266 | | g_variant_dict_end (GVariantDict *dict) |
4267 | 0 | { |
4268 | 0 | GVariantBuilder builder; |
4269 | 0 | GHashTableIter iter; |
4270 | 0 | gpointer key, value; |
4271 | |
|
4272 | 0 | return_val_if_invalid_dict (dict, NULL); |
4273 | | |
4274 | 0 | g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); |
4275 | |
|
4276 | 0 | g_hash_table_iter_init (&iter, GVSD(dict)->values); |
4277 | 0 | while (g_hash_table_iter_next (&iter, &key, &value)) |
4278 | 0 | g_variant_builder_add (&builder, "{sv}", (const gchar *) key, (GVariant *) value); |
4279 | |
|
4280 | 0 | g_variant_dict_clear (dict); |
4281 | |
|
4282 | 0 | return g_variant_builder_end (&builder); |
4283 | 0 | } |
4284 | | |
4285 | | /** |
4286 | | * g_variant_dict_ref: |
4287 | | * @dict: a heap-allocated #GVariantDict |
4288 | | * |
4289 | | * Increases the reference count on @dict. |
4290 | | * |
4291 | | * Don't call this on stack-allocated #GVariantDict instances or bad |
4292 | | * things will happen. |
4293 | | * |
4294 | | * Returns: (transfer full): a new reference to @dict |
4295 | | * |
4296 | | * Since: 2.40 |
4297 | | **/ |
4298 | | GVariantDict * |
4299 | | g_variant_dict_ref (GVariantDict *dict) |
4300 | 0 | { |
4301 | 0 | g_return_val_if_fail (is_valid_heap_dict (dict), NULL); |
4302 | | |
4303 | 0 | GVHD(dict)->ref_count++; |
4304 | |
|
4305 | 0 | return dict; |
4306 | 0 | } |
4307 | | |
4308 | | /** |
4309 | | * g_variant_dict_unref: |
4310 | | * @dict: (transfer full): a heap-allocated #GVariantDict |
4311 | | * |
4312 | | * Decreases the reference count on @dict. |
4313 | | * |
4314 | | * In the event that there are no more references, releases all memory |
4315 | | * associated with the #GVariantDict. |
4316 | | * |
4317 | | * Don't call this on stack-allocated #GVariantDict instances or bad |
4318 | | * things will happen. |
4319 | | * |
4320 | | * Since: 2.40 |
4321 | | **/ |
4322 | | void |
4323 | | g_variant_dict_unref (GVariantDict *dict) |
4324 | 0 | { |
4325 | 0 | g_return_if_fail (is_valid_heap_dict (dict)); |
4326 | | |
4327 | 0 | if (--GVHD(dict)->ref_count == 0) |
4328 | 0 | { |
4329 | 0 | g_variant_dict_clear (dict); |
4330 | 0 | g_slice_free (struct heap_dict, (struct heap_dict *) dict); |
4331 | 0 | } |
4332 | 0 | } |
4333 | | |
4334 | | |
4335 | | /* Format strings {{{1 */ |
4336 | | /*< private > |
4337 | | * g_variant_format_string_scan: |
4338 | | * @string: a string that may be prefixed with a format string |
4339 | | * @limit: (nullable) (default NULL): a pointer to the end of @string, |
4340 | | * or %NULL |
4341 | | * @endptr: (nullable) (default NULL): location to store the end pointer, |
4342 | | * or %NULL |
4343 | | * |
4344 | | * Checks the string pointed to by @string for starting with a properly |
4345 | | * formed #GVariant varargs format string. If no valid format string is |
4346 | | * found then %FALSE is returned. |
4347 | | * |
4348 | | * If @string does start with a valid format string then %TRUE is |
4349 | | * returned. If @endptr is non-%NULL then it is updated to point to the |
4350 | | * first character after the format string. |
4351 | | * |
4352 | | * If @limit is non-%NULL then @limit (and any character after it) will |
4353 | | * not be accessed and the effect is otherwise equivalent to if the |
4354 | | * character at @limit were nul. |
4355 | | * |
4356 | | * See the section on [GVariant format strings][gvariant-format-strings]. |
4357 | | * |
4358 | | * Returns: %TRUE if there was a valid format string |
4359 | | * |
4360 | | * Since: 2.24 |
4361 | | */ |
4362 | | gboolean |
4363 | | g_variant_format_string_scan (const gchar *string, |
4364 | | const gchar *limit, |
4365 | | const gchar **endptr) |
4366 | 0 | { |
4367 | 0 | #define next_char() (string == limit ? '\0' : *(string++)) |
4368 | 0 | #define peek_char() (string == limit ? '\0' : *string) |
4369 | 0 | char c; |
4370 | |
|
4371 | 0 | switch (next_char()) |
4372 | 0 | { |
4373 | 0 | case 'b': case 'y': case 'n': case 'q': case 'i': case 'u': |
4374 | 0 | case 'x': case 't': case 'h': case 'd': case 's': case 'o': |
4375 | 0 | case 'g': case 'v': case '*': case '?': case 'r': |
4376 | 0 | break; |
4377 | | |
4378 | 0 | case 'm': |
4379 | 0 | return g_variant_format_string_scan (string, limit, endptr); |
4380 | | |
4381 | 0 | case 'a': |
4382 | 0 | case '@': |
4383 | 0 | return g_variant_type_string_scan (string, limit, endptr); |
4384 | | |
4385 | 0 | case '(': |
4386 | 0 | while (peek_char() != ')') |
4387 | 0 | if (!g_variant_format_string_scan (string, limit, &string)) |
4388 | 0 | return FALSE; |
4389 | | |
4390 | 0 | next_char(); /* consume ')' */ |
4391 | 0 | break; |
4392 | | |
4393 | 0 | case '{': |
4394 | 0 | c = next_char(); |
4395 | |
|
4396 | 0 | if (c == '&') |
4397 | 0 | { |
4398 | 0 | c = next_char (); |
4399 | |
|
4400 | 0 | if (c != 's' && c != 'o' && c != 'g') |
4401 | 0 | return FALSE; |
4402 | 0 | } |
4403 | 0 | else |
4404 | 0 | { |
4405 | 0 | if (c == '@') |
4406 | 0 | c = next_char (); |
4407 | | |
4408 | | /* ISO/IEC 9899:1999 (C99) §7.21.5.2: |
4409 | | * The terminating null character is considered to be |
4410 | | * part of the string. |
4411 | | */ |
4412 | 0 | if (c != '\0' && strchr ("bynqiuxthdsog?", c) == NULL) |
4413 | 0 | return FALSE; |
4414 | 0 | } |
4415 | | |
4416 | 0 | if (!g_variant_format_string_scan (string, limit, &string)) |
4417 | 0 | return FALSE; |
4418 | | |
4419 | 0 | if (next_char() != '}') |
4420 | 0 | return FALSE; |
4421 | | |
4422 | 0 | break; |
4423 | | |
4424 | 0 | case '^': |
4425 | 0 | if ((c = next_char()) == 'a') |
4426 | 0 | { |
4427 | 0 | if ((c = next_char()) == '&') |
4428 | 0 | { |
4429 | 0 | if ((c = next_char()) == 'a') |
4430 | 0 | { |
4431 | 0 | if ((c = next_char()) == 'y') |
4432 | 0 | break; /* '^a&ay' */ |
4433 | 0 | } |
4434 | | |
4435 | 0 | else if (c == 's' || c == 'o') |
4436 | 0 | break; /* '^a&s', '^a&o' */ |
4437 | 0 | } |
4438 | | |
4439 | 0 | else if (c == 'a') |
4440 | 0 | { |
4441 | 0 | if ((c = next_char()) == 'y') |
4442 | 0 | break; /* '^aay' */ |
4443 | 0 | } |
4444 | | |
4445 | 0 | else if (c == 's' || c == 'o') |
4446 | 0 | break; /* '^as', '^ao' */ |
4447 | | |
4448 | 0 | else if (c == 'y') |
4449 | 0 | break; /* '^ay' */ |
4450 | 0 | } |
4451 | 0 | else if (c == '&') |
4452 | 0 | { |
4453 | 0 | if ((c = next_char()) == 'a') |
4454 | 0 | { |
4455 | 0 | if ((c = next_char()) == 'y') |
4456 | 0 | break; /* '^&ay' */ |
4457 | 0 | } |
4458 | 0 | } |
4459 | | |
4460 | 0 | return FALSE; |
4461 | | |
4462 | 0 | case '&': |
4463 | 0 | c = next_char(); |
4464 | |
|
4465 | 0 | if (c != 's' && c != 'o' && c != 'g') |
4466 | 0 | return FALSE; |
4467 | | |
4468 | 0 | break; |
4469 | | |
4470 | 0 | default: |
4471 | 0 | return FALSE; |
4472 | 0 | } |
4473 | | |
4474 | 0 | if (endptr != NULL) |
4475 | 0 | *endptr = string; |
4476 | |
|
4477 | 0 | #undef next_char |
4478 | 0 | #undef peek_char |
4479 | |
|
4480 | 0 | return TRUE; |
4481 | 0 | } |
4482 | | |
4483 | | /** |
4484 | | * g_variant_check_format_string: |
4485 | | * @value: a #GVariant |
4486 | | * @format_string: a valid #GVariant format string |
4487 | | * @copy_only: %TRUE to ensure the format string makes deep copies |
4488 | | * |
4489 | | * Checks if calling g_variant_get() with @format_string on @value would |
4490 | | * be valid from a type-compatibility standpoint. @format_string is |
4491 | | * assumed to be a valid format string (from a syntactic standpoint). |
4492 | | * |
4493 | | * If @copy_only is %TRUE then this function additionally checks that it |
4494 | | * would be safe to call g_variant_unref() on @value immediately after |
4495 | | * the call to g_variant_get() without invalidating the result. This is |
4496 | | * only possible if deep copies are made (ie: there are no pointers to |
4497 | | * the data inside of the soon-to-be-freed #GVariant instance). If this |
4498 | | * check fails then a g_critical() is printed and %FALSE is returned. |
4499 | | * |
4500 | | * This function is meant to be used by functions that wish to provide |
4501 | | * varargs accessors to #GVariant values of uncertain values (eg: |
4502 | | * g_variant_lookup() or g_menu_model_get_item_attribute()). |
4503 | | * |
4504 | | * Returns: %TRUE if @format_string is safe to use |
4505 | | * |
4506 | | * Since: 2.34 |
4507 | | */ |
4508 | | gboolean |
4509 | | g_variant_check_format_string (GVariant *value, |
4510 | | const gchar *format_string, |
4511 | | gboolean copy_only) |
4512 | 0 | { |
4513 | 0 | const gchar *original_format = format_string; |
4514 | 0 | const gchar *type_string; |
4515 | | |
4516 | | /* Interesting factoid: assuming a format string is valid, it can be |
4517 | | * converted to a type string by removing all '@' '&' and '^' |
4518 | | * characters. |
4519 | | * |
4520 | | * Instead of doing that, we can just skip those characters when |
4521 | | * comparing it to the type string of @value. |
4522 | | * |
4523 | | * For the copy-only case we can just drop the '&' from the list of |
4524 | | * characters to skip over. A '&' will never appear in a type string |
4525 | | * so we know that it won't be possible to return %TRUE if it is in a |
4526 | | * format string. |
4527 | | */ |
4528 | 0 | type_string = g_variant_get_type_string (value); |
4529 | |
|
4530 | 0 | while (*type_string || *format_string) |
4531 | 0 | { |
4532 | 0 | gchar format = *format_string++; |
4533 | |
|
4534 | 0 | switch (format) |
4535 | 0 | { |
4536 | 0 | case '&': |
4537 | 0 | if G_UNLIKELY (copy_only) |
4538 | 0 | { |
4539 | | /* for the love of all that is good, please don't mark this string for translation... */ |
4540 | 0 | g_critical ("g_variant_check_format_string() is being called by a function with a GVariant varargs " |
4541 | 0 | "interface to validate the passed format string for type safety. The passed format " |
4542 | 0 | "(%s) contains a '&' character which would result in a pointer being returned to the " |
4543 | 0 | "data inside of a GVariant instance that may no longer exist by the time the function " |
4544 | 0 | "returns. Modify your code to use a format string without '&'.", original_format); |
4545 | 0 | return FALSE; |
4546 | 0 | } |
4547 | | |
4548 | 0 | G_GNUC_FALLTHROUGH; |
4549 | 0 | case '^': |
4550 | 0 | case '@': |
4551 | | /* ignore these 2 (or 3) */ |
4552 | 0 | continue; |
4553 | | |
4554 | 0 | case '?': |
4555 | | /* attempt to consume one of 'bynqiuxthdsog' */ |
4556 | 0 | { |
4557 | 0 | char s = *type_string++; |
4558 | |
|
4559 | 0 | if (s == '\0' || strchr ("bynqiuxthdsog", s) == NULL) |
4560 | 0 | return FALSE; |
4561 | 0 | } |
4562 | 0 | continue; |
4563 | | |
4564 | 0 | case 'r': |
4565 | | /* ensure it's a tuple */ |
4566 | 0 | if (*type_string != '(') |
4567 | 0 | return FALSE; |
4568 | | |
4569 | 0 | G_GNUC_FALLTHROUGH; |
4570 | 0 | case '*': |
4571 | | /* consume a full type string for the '*' or 'r' */ |
4572 | 0 | if (!g_variant_type_string_scan (type_string, NULL, &type_string)) |
4573 | 0 | return FALSE; |
4574 | | |
4575 | 0 | continue; |
4576 | | |
4577 | 0 | default: |
4578 | | /* attempt to consume exactly one character equal to the format */ |
4579 | 0 | if (format != *type_string++) |
4580 | 0 | return FALSE; |
4581 | 0 | } |
4582 | 0 | } |
4583 | | |
4584 | 0 | return TRUE; |
4585 | 0 | } |
4586 | | |
4587 | | /*< private > |
4588 | | * g_variant_format_string_scan_type: |
4589 | | * @string: a string that may be prefixed with a format string |
4590 | | * @limit: (nullable) (default NULL): a pointer to the end of @string, |
4591 | | * or %NULL |
4592 | | * @endptr: (nullable) (default NULL): location to store the end pointer, |
4593 | | * or %NULL |
4594 | | * |
4595 | | * If @string starts with a valid format string then this function will |
4596 | | * return the type that the format string corresponds to. Otherwise |
4597 | | * this function returns %NULL. |
4598 | | * |
4599 | | * Use g_variant_type_free() to free the return value when you no longer |
4600 | | * need it. |
4601 | | * |
4602 | | * This function is otherwise exactly like |
4603 | | * g_variant_format_string_scan(). |
4604 | | * |
4605 | | * Returns: (nullable): a #GVariantType if there was a valid format string |
4606 | | * |
4607 | | * Since: 2.24 |
4608 | | */ |
4609 | | GVariantType * |
4610 | | g_variant_format_string_scan_type (const gchar *string, |
4611 | | const gchar *limit, |
4612 | | const gchar **endptr) |
4613 | 0 | { |
4614 | 0 | const gchar *my_end; |
4615 | 0 | gchar *dest; |
4616 | 0 | gchar *new; |
4617 | |
|
4618 | 0 | if (endptr == NULL) |
4619 | 0 | endptr = &my_end; |
4620 | |
|
4621 | 0 | if (!g_variant_format_string_scan (string, limit, endptr)) |
4622 | 0 | return NULL; |
4623 | | |
4624 | 0 | dest = new = g_malloc (*endptr - string + 1); |
4625 | 0 | while (string != *endptr) |
4626 | 0 | { |
4627 | 0 | if (*string != '@' && *string != '&' && *string != '^') |
4628 | 0 | *dest++ = *string; |
4629 | 0 | string++; |
4630 | 0 | } |
4631 | 0 | *dest = '\0'; |
4632 | |
|
4633 | 0 | return (GVariantType *) G_VARIANT_TYPE (new); |
4634 | 0 | } |
4635 | | |
4636 | | static gboolean |
4637 | | valid_format_string (const gchar *format_string, |
4638 | | gboolean single, |
4639 | | GVariant *value) |
4640 | 0 | { |
4641 | 0 | const gchar *endptr; |
4642 | 0 | GVariantType *type; |
4643 | |
|
4644 | 0 | type = g_variant_format_string_scan_type (format_string, NULL, &endptr); |
4645 | |
|
4646 | 0 | if G_UNLIKELY (type == NULL || (single && *endptr != '\0')) |
4647 | 0 | { |
4648 | 0 | if (single) |
4649 | 0 | g_critical ("'%s' is not a valid GVariant format string", |
4650 | 0 | format_string); |
4651 | 0 | else |
4652 | 0 | g_critical ("'%s' does not have a valid GVariant format " |
4653 | 0 | "string as a prefix", format_string); |
4654 | |
|
4655 | 0 | if (type != NULL) |
4656 | 0 | g_variant_type_free (type); |
4657 | |
|
4658 | 0 | return FALSE; |
4659 | 0 | } |
4660 | | |
4661 | 0 | if G_UNLIKELY (value && !g_variant_is_of_type (value, type)) |
4662 | 0 | { |
4663 | 0 | gchar *fragment; |
4664 | 0 | gchar *typestr; |
4665 | |
|
4666 | 0 | fragment = g_strndup (format_string, endptr - format_string); |
4667 | 0 | typestr = g_variant_type_dup_string (type); |
4668 | |
|
4669 | 0 | g_critical ("the GVariant format string '%s' has a type of " |
4670 | 0 | "'%s' but the given value has a type of '%s'", |
4671 | 0 | fragment, typestr, g_variant_get_type_string (value)); |
4672 | |
|
4673 | 0 | g_variant_type_free (type); |
4674 | 0 | g_free (fragment); |
4675 | 0 | g_free (typestr); |
4676 | |
|
4677 | 0 | return FALSE; |
4678 | 0 | } |
4679 | | |
4680 | 0 | g_variant_type_free (type); |
4681 | |
|
4682 | 0 | return TRUE; |
4683 | 0 | } |
4684 | | |
4685 | | /* Variable Arguments {{{1 */ |
4686 | | /* We consider 2 main classes of format strings: |
4687 | | * |
4688 | | * - recursive format strings |
4689 | | * these are ones that result in recursion and the collection of |
4690 | | * possibly more than one argument. Maybe types, tuples, |
4691 | | * dictionary entries. |
4692 | | * |
4693 | | * - leaf format string |
4694 | | * these result in the collection of a single argument. |
4695 | | * |
4696 | | * Leaf format strings are further subdivided into two categories: |
4697 | | * |
4698 | | * - single non-null pointer ("nnp") |
4699 | | * these either collect or return a single non-null pointer. |
4700 | | * |
4701 | | * - other |
4702 | | * these collect or return something else (bool, number, etc). |
4703 | | * |
4704 | | * Based on the above, the varargs handling code is split into 4 main parts: |
4705 | | * |
4706 | | * - nnp handling code |
4707 | | * - leaf handling code (which may invoke nnp code) |
4708 | | * - generic handling code (may be recursive, may invoke leaf code) |
4709 | | * - user-facing API (which invokes the generic code) |
4710 | | * |
4711 | | * Each section implements some of the following functions: |
4712 | | * |
4713 | | * - skip: |
4714 | | * collect the arguments for the format string as if |
4715 | | * g_variant_new() had been called, but do nothing with them. used |
4716 | | * for skipping over arguments when constructing a Nothing maybe |
4717 | | * type. |
4718 | | * |
4719 | | * - new: |
4720 | | * create a GVariant * |
4721 | | * |
4722 | | * - get: |
4723 | | * unpack a GVariant * |
4724 | | * |
4725 | | * - free (nnp only): |
4726 | | * free a previously allocated item |
4727 | | */ |
4728 | | |
4729 | | static gboolean |
4730 | | g_variant_format_string_is_leaf (const gchar *str) |
4731 | 0 | { |
4732 | 0 | return str[0] != 'm' && str[0] != '(' && str[0] != '{'; |
4733 | 0 | } |
4734 | | |
4735 | | static gboolean |
4736 | | g_variant_format_string_is_nnp (const gchar *str) |
4737 | 0 | { |
4738 | 0 | return str[0] == 'a' || str[0] == 's' || str[0] == 'o' || str[0] == 'g' || |
4739 | 0 | str[0] == '^' || str[0] == '@' || str[0] == '*' || str[0] == '?' || |
4740 | 0 | str[0] == 'r' || str[0] == 'v' || str[0] == '&'; |
4741 | 0 | } |
4742 | | |
4743 | | /* Single non-null pointer ("nnp") {{{2 */ |
4744 | | static void |
4745 | | g_variant_valist_free_nnp (const gchar *str, |
4746 | | gpointer ptr) |
4747 | 0 | { |
4748 | 0 | switch (*str) |
4749 | 0 | { |
4750 | 0 | case 'a': |
4751 | 0 | g_variant_iter_free (ptr); |
4752 | 0 | break; |
4753 | | |
4754 | 0 | case '^': |
4755 | 0 | if (g_str_has_suffix (str, "y")) |
4756 | 0 | { |
4757 | 0 | if (str[2] != 'a') /* '^a&ay', '^ay' */ |
4758 | 0 | g_free (ptr); |
4759 | 0 | else if (str[1] == 'a') /* '^aay' */ |
4760 | 0 | g_strfreev (ptr); |
4761 | 0 | break; /* '^&ay' */ |
4762 | 0 | } |
4763 | 0 | else if (str[2] != '&') /* '^as', '^ao' */ |
4764 | 0 | g_strfreev (ptr); |
4765 | 0 | else /* '^a&s', '^a&o' */ |
4766 | 0 | g_free (ptr); |
4767 | 0 | break; |
4768 | | |
4769 | 0 | case 's': |
4770 | 0 | case 'o': |
4771 | 0 | case 'g': |
4772 | 0 | g_free (ptr); |
4773 | 0 | break; |
4774 | | |
4775 | 0 | case '@': |
4776 | 0 | case '*': |
4777 | 0 | case '?': |
4778 | 0 | case 'v': |
4779 | 0 | g_variant_unref (ptr); |
4780 | 0 | break; |
4781 | | |
4782 | 0 | case '&': |
4783 | 0 | break; |
4784 | | |
4785 | 0 | default: |
4786 | 0 | g_assert_not_reached (); |
4787 | 0 | } |
4788 | 0 | } |
4789 | | |
4790 | | static gchar |
4791 | | g_variant_scan_convenience (const gchar **str, |
4792 | | gboolean *constant, |
4793 | | guint *arrays) |
4794 | 0 | { |
4795 | 0 | *constant = FALSE; |
4796 | 0 | *arrays = 0; |
4797 | |
|
4798 | 0 | for (;;) |
4799 | 0 | { |
4800 | 0 | char c = *(*str)++; |
4801 | |
|
4802 | 0 | if (c == '&') |
4803 | 0 | *constant = TRUE; |
4804 | | |
4805 | 0 | else if (c == 'a') |
4806 | 0 | (*arrays)++; |
4807 | | |
4808 | 0 | else |
4809 | 0 | return c; |
4810 | 0 | } |
4811 | 0 | } |
4812 | | |
4813 | | static GVariant * |
4814 | | g_variant_valist_new_nnp (const gchar **str, |
4815 | | gpointer ptr) |
4816 | 0 | { |
4817 | 0 | if (**str == '&') |
4818 | 0 | (*str)++; |
4819 | |
|
4820 | 0 | switch (*(*str)++) |
4821 | 0 | { |
4822 | 0 | case 'a': |
4823 | 0 | if (ptr != NULL) |
4824 | 0 | { |
4825 | 0 | const GVariantType *type; |
4826 | 0 | GVariant *value; |
4827 | |
|
4828 | 0 | value = g_variant_builder_end (ptr); |
4829 | 0 | type = g_variant_get_type (value); |
4830 | |
|
4831 | 0 | if G_UNLIKELY (!g_variant_type_is_array (type)) |
4832 | 0 | g_error ("g_variant_new: expected array GVariantBuilder but " |
4833 | 0 | "the built value has type '%s'", |
4834 | 0 | g_variant_get_type_string (value)); |
4835 | |
|
4836 | 0 | type = g_variant_type_element (type); |
4837 | |
|
4838 | 0 | if G_UNLIKELY (!g_variant_type_is_subtype_of (type, (GVariantType *) *str)) |
4839 | 0 | { |
4840 | 0 | gchar *type_string = g_variant_type_dup_string ((GVariantType *) *str); |
4841 | 0 | g_error ("g_variant_new: expected GVariantBuilder array element " |
4842 | 0 | "type '%s' but the built value has element type '%s'", |
4843 | 0 | type_string, g_variant_get_type_string (value) + 1); |
4844 | 0 | g_free (type_string); |
4845 | 0 | } |
4846 | |
|
4847 | 0 | g_variant_type_string_scan (*str, NULL, str); |
4848 | |
|
4849 | 0 | return value; |
4850 | 0 | } |
4851 | 0 | else |
4852 | | |
4853 | | /* special case: NULL pointer for empty array */ |
4854 | 0 | { |
4855 | 0 | const GVariantType *type = (GVariantType *) *str; |
4856 | |
|
4857 | 0 | g_variant_type_string_scan (*str, NULL, str); |
4858 | |
|
4859 | 0 | if G_UNLIKELY (!g_variant_type_is_definite (type)) |
4860 | 0 | g_error ("g_variant_new: NULL pointer given with indefinite " |
4861 | 0 | "array type; unable to determine which type of empty " |
4862 | 0 | "array to construct."); |
4863 | |
|
4864 | 0 | return g_variant_new_array (type, NULL, 0); |
4865 | 0 | } |
4866 | | |
4867 | 0 | case 's': |
4868 | 0 | { |
4869 | 0 | GVariant *value; |
4870 | |
|
4871 | 0 | value = g_variant_new_string (ptr); |
4872 | |
|
4873 | 0 | if (value == NULL) |
4874 | 0 | value = g_variant_new_string ("[Invalid UTF-8]"); |
4875 | |
|
4876 | 0 | return value; |
4877 | 0 | } |
4878 | | |
4879 | 0 | case 'o': |
4880 | 0 | return g_variant_new_object_path (ptr); |
4881 | | |
4882 | 0 | case 'g': |
4883 | 0 | return g_variant_new_signature (ptr); |
4884 | | |
4885 | 0 | case '^': |
4886 | 0 | { |
4887 | 0 | gboolean constant; |
4888 | 0 | guint arrays; |
4889 | 0 | gchar type; |
4890 | |
|
4891 | 0 | type = g_variant_scan_convenience (str, &constant, &arrays); |
4892 | |
|
4893 | 0 | if (type == 's') |
4894 | 0 | return g_variant_new_strv (ptr, -1); |
4895 | | |
4896 | 0 | if (type == 'o') |
4897 | 0 | return g_variant_new_objv (ptr, -1); |
4898 | | |
4899 | 0 | if (arrays > 1) |
4900 | 0 | return g_variant_new_bytestring_array (ptr, -1); |
4901 | | |
4902 | 0 | return g_variant_new_bytestring (ptr); |
4903 | 0 | } |
4904 | | |
4905 | 0 | case '@': |
4906 | 0 | if G_UNLIKELY (!g_variant_is_of_type (ptr, (GVariantType *) *str)) |
4907 | 0 | { |
4908 | 0 | gchar *type_string = g_variant_type_dup_string ((GVariantType *) *str); |
4909 | 0 | g_error ("g_variant_new: expected GVariant of type '%s' but " |
4910 | 0 | "received value has type '%s'", |
4911 | 0 | type_string, g_variant_get_type_string (ptr)); |
4912 | 0 | g_free (type_string); |
4913 | 0 | } |
4914 | |
|
4915 | 0 | g_variant_type_string_scan (*str, NULL, str); |
4916 | |
|
4917 | 0 | return ptr; |
4918 | | |
4919 | 0 | case '*': |
4920 | 0 | return ptr; |
4921 | | |
4922 | 0 | case '?': |
4923 | 0 | if G_UNLIKELY (!g_variant_type_is_basic (g_variant_get_type (ptr))) |
4924 | 0 | g_error ("g_variant_new: format string '?' expects basic-typed " |
4925 | 0 | "GVariant, but received value has type '%s'", |
4926 | 0 | g_variant_get_type_string (ptr)); |
4927 | |
|
4928 | 0 | return ptr; |
4929 | | |
4930 | 0 | case 'r': |
4931 | 0 | if G_UNLIKELY (!g_variant_type_is_tuple (g_variant_get_type (ptr))) |
4932 | 0 | g_error ("g_variant_new: format string 'r' expects tuple-typed " |
4933 | 0 | "GVariant, but received value has type '%s'", |
4934 | 0 | g_variant_get_type_string (ptr)); |
4935 | |
|
4936 | 0 | return ptr; |
4937 | | |
4938 | 0 | case 'v': |
4939 | 0 | return g_variant_new_variant (ptr); |
4940 | | |
4941 | 0 | default: |
4942 | 0 | g_assert_not_reached (); |
4943 | 0 | } |
4944 | 0 | } |
4945 | | |
4946 | | static gpointer |
4947 | | g_variant_valist_get_nnp (const gchar **str, |
4948 | | GVariant *value) |
4949 | 0 | { |
4950 | 0 | switch (*(*str)++) |
4951 | 0 | { |
4952 | 0 | case 'a': |
4953 | 0 | g_variant_type_string_scan (*str, NULL, str); |
4954 | 0 | return g_variant_iter_new (value); |
4955 | | |
4956 | 0 | case '&': |
4957 | 0 | (*str)++; |
4958 | 0 | return (gchar *) g_variant_get_string (value, NULL); |
4959 | | |
4960 | 0 | case 's': |
4961 | 0 | case 'o': |
4962 | 0 | case 'g': |
4963 | 0 | return g_variant_dup_string (value, NULL); |
4964 | | |
4965 | 0 | case '^': |
4966 | 0 | { |
4967 | 0 | gboolean constant; |
4968 | 0 | guint arrays; |
4969 | 0 | gchar type; |
4970 | |
|
4971 | 0 | type = g_variant_scan_convenience (str, &constant, &arrays); |
4972 | |
|
4973 | 0 | if (type == 's') |
4974 | 0 | { |
4975 | 0 | if (constant) |
4976 | 0 | return g_variant_get_strv (value, NULL); |
4977 | 0 | else |
4978 | 0 | return g_variant_dup_strv (value, NULL); |
4979 | 0 | } |
4980 | | |
4981 | 0 | else if (type == 'o') |
4982 | 0 | { |
4983 | 0 | if (constant) |
4984 | 0 | return g_variant_get_objv (value, NULL); |
4985 | 0 | else |
4986 | 0 | return g_variant_dup_objv (value, NULL); |
4987 | 0 | } |
4988 | | |
4989 | 0 | else if (arrays > 1) |
4990 | 0 | { |
4991 | 0 | if (constant) |
4992 | 0 | return g_variant_get_bytestring_array (value, NULL); |
4993 | 0 | else |
4994 | 0 | return g_variant_dup_bytestring_array (value, NULL); |
4995 | 0 | } |
4996 | | |
4997 | 0 | else |
4998 | 0 | { |
4999 | 0 | if (constant) |
5000 | 0 | return (gchar *) g_variant_get_bytestring (value); |
5001 | 0 | else |
5002 | 0 | return g_variant_dup_bytestring (value, NULL); |
5003 | 0 | } |
5004 | 0 | } |
5005 | | |
5006 | 0 | case '@': |
5007 | 0 | g_variant_type_string_scan (*str, NULL, str); |
5008 | 0 | G_GNUC_FALLTHROUGH; |
5009 | |
|
5010 | 0 | case '*': |
5011 | 0 | case '?': |
5012 | 0 | case 'r': |
5013 | 0 | return g_variant_ref (value); |
5014 | | |
5015 | 0 | case 'v': |
5016 | 0 | return g_variant_get_variant (value); |
5017 | | |
5018 | 0 | default: |
5019 | 0 | g_assert_not_reached (); |
5020 | 0 | } |
5021 | 0 | } |
5022 | | |
5023 | | /* Leaves {{{2 */ |
5024 | | static void |
5025 | | g_variant_valist_skip_leaf (const gchar **str, |
5026 | | va_list *app) |
5027 | 0 | { |
5028 | 0 | if (g_variant_format_string_is_nnp (*str)) |
5029 | 0 | { |
5030 | 0 | g_variant_format_string_scan (*str, NULL, str); |
5031 | 0 | va_arg (*app, gpointer); |
5032 | 0 | return; |
5033 | 0 | } |
5034 | | |
5035 | 0 | switch (*(*str)++) |
5036 | 0 | { |
5037 | 0 | case 'b': |
5038 | 0 | case 'y': |
5039 | 0 | case 'n': |
5040 | 0 | case 'q': |
5041 | 0 | case 'i': |
5042 | 0 | case 'u': |
5043 | 0 | case 'h': |
5044 | 0 | va_arg (*app, int); |
5045 | 0 | return; |
5046 | | |
5047 | 0 | case 'x': |
5048 | 0 | case 't': |
5049 | 0 | va_arg (*app, guint64); |
5050 | 0 | return; |
5051 | | |
5052 | 0 | case 'd': |
5053 | 0 | va_arg (*app, gdouble); |
5054 | 0 | return; |
5055 | | |
5056 | 0 | default: |
5057 | 0 | g_assert_not_reached (); |
5058 | 0 | } |
5059 | 0 | } |
5060 | | |
5061 | | static GVariant * |
5062 | | g_variant_valist_new_leaf (const gchar **str, |
5063 | | va_list *app) |
5064 | 0 | { |
5065 | 0 | if (g_variant_format_string_is_nnp (*str)) |
5066 | 0 | return g_variant_valist_new_nnp (str, va_arg (*app, gpointer)); |
5067 | | |
5068 | 0 | switch (*(*str)++) |
5069 | 0 | { |
5070 | 0 | case 'b': |
5071 | 0 | return g_variant_new_boolean (va_arg (*app, gboolean)); |
5072 | | |
5073 | 0 | case 'y': |
5074 | 0 | return g_variant_new_byte (va_arg (*app, guint)); |
5075 | | |
5076 | 0 | case 'n': |
5077 | 0 | return g_variant_new_int16 (va_arg (*app, gint)); |
5078 | | |
5079 | 0 | case 'q': |
5080 | 0 | return g_variant_new_uint16 (va_arg (*app, guint)); |
5081 | | |
5082 | 0 | case 'i': |
5083 | 0 | return g_variant_new_int32 (va_arg (*app, gint)); |
5084 | | |
5085 | 0 | case 'u': |
5086 | 0 | return g_variant_new_uint32 (va_arg (*app, guint)); |
5087 | | |
5088 | 0 | case 'x': |
5089 | 0 | return g_variant_new_int64 (va_arg (*app, gint64)); |
5090 | | |
5091 | 0 | case 't': |
5092 | 0 | return g_variant_new_uint64 (va_arg (*app, guint64)); |
5093 | | |
5094 | 0 | case 'h': |
5095 | 0 | return g_variant_new_handle (va_arg (*app, gint)); |
5096 | | |
5097 | 0 | case 'd': |
5098 | 0 | return g_variant_new_double (va_arg (*app, gdouble)); |
5099 | | |
5100 | 0 | default: |
5101 | 0 | g_assert_not_reached (); |
5102 | 0 | } |
5103 | 0 | } |
5104 | | |
5105 | | /* The code below assumes this */ |
5106 | | G_STATIC_ASSERT (sizeof (gboolean) == sizeof (guint32)); |
5107 | | G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64)); |
5108 | | |
5109 | | static void |
5110 | | g_variant_valist_get_leaf (const gchar **str, |
5111 | | GVariant *value, |
5112 | | gboolean free, |
5113 | | va_list *app) |
5114 | 0 | { |
5115 | 0 | gpointer ptr = va_arg (*app, gpointer); |
5116 | |
|
5117 | 0 | if (ptr == NULL) |
5118 | 0 | { |
5119 | 0 | g_variant_format_string_scan (*str, NULL, str); |
5120 | 0 | return; |
5121 | 0 | } |
5122 | | |
5123 | 0 | if (g_variant_format_string_is_nnp (*str)) |
5124 | 0 | { |
5125 | 0 | gpointer *nnp = (gpointer *) ptr; |
5126 | |
|
5127 | 0 | if (free && *nnp != NULL) |
5128 | 0 | g_variant_valist_free_nnp (*str, *nnp); |
5129 | |
|
5130 | 0 | *nnp = NULL; |
5131 | |
|
5132 | 0 | if (value != NULL) |
5133 | 0 | *nnp = g_variant_valist_get_nnp (str, value); |
5134 | 0 | else |
5135 | 0 | g_variant_format_string_scan (*str, NULL, str); |
5136 | |
|
5137 | 0 | return; |
5138 | 0 | } |
5139 | | |
5140 | 0 | if (value != NULL) |
5141 | 0 | { |
5142 | 0 | switch (*(*str)++) |
5143 | 0 | { |
5144 | 0 | case 'b': |
5145 | 0 | *(gboolean *) ptr = g_variant_get_boolean (value); |
5146 | 0 | return; |
5147 | | |
5148 | 0 | case 'y': |
5149 | 0 | *(guint8 *) ptr = g_variant_get_byte (value); |
5150 | 0 | return; |
5151 | | |
5152 | 0 | case 'n': |
5153 | 0 | *(gint16 *) ptr = g_variant_get_int16 (value); |
5154 | 0 | return; |
5155 | | |
5156 | 0 | case 'q': |
5157 | 0 | *(guint16 *) ptr = g_variant_get_uint16 (value); |
5158 | 0 | return; |
5159 | | |
5160 | 0 | case 'i': |
5161 | 0 | *(gint32 *) ptr = g_variant_get_int32 (value); |
5162 | 0 | return; |
5163 | | |
5164 | 0 | case 'u': |
5165 | 0 | *(guint32 *) ptr = g_variant_get_uint32 (value); |
5166 | 0 | return; |
5167 | | |
5168 | 0 | case 'x': |
5169 | 0 | *(gint64 *) ptr = g_variant_get_int64 (value); |
5170 | 0 | return; |
5171 | | |
5172 | 0 | case 't': |
5173 | 0 | *(guint64 *) ptr = g_variant_get_uint64 (value); |
5174 | 0 | return; |
5175 | | |
5176 | 0 | case 'h': |
5177 | 0 | *(gint32 *) ptr = g_variant_get_handle (value); |
5178 | 0 | return; |
5179 | | |
5180 | 0 | case 'd': |
5181 | 0 | *(gdouble *) ptr = g_variant_get_double (value); |
5182 | 0 | return; |
5183 | 0 | } |
5184 | 0 | } |
5185 | 0 | else |
5186 | 0 | { |
5187 | 0 | switch (*(*str)++) |
5188 | 0 | { |
5189 | 0 | case 'y': |
5190 | 0 | *(guint8 *) ptr = 0; |
5191 | 0 | return; |
5192 | | |
5193 | 0 | case 'n': |
5194 | 0 | case 'q': |
5195 | 0 | *(guint16 *) ptr = 0; |
5196 | 0 | return; |
5197 | | |
5198 | 0 | case 'i': |
5199 | 0 | case 'u': |
5200 | 0 | case 'h': |
5201 | 0 | case 'b': |
5202 | 0 | *(guint32 *) ptr = 0; |
5203 | 0 | return; |
5204 | | |
5205 | 0 | case 'x': |
5206 | 0 | case 't': |
5207 | 0 | case 'd': |
5208 | 0 | *(guint64 *) ptr = 0; |
5209 | 0 | return; |
5210 | 0 | } |
5211 | 0 | } |
5212 | | |
5213 | 0 | g_assert_not_reached (); |
5214 | 0 | } |
5215 | | |
5216 | | /* Generic (recursive) {{{2 */ |
5217 | | static void |
5218 | | g_variant_valist_skip (const gchar **str, |
5219 | | va_list *app) |
5220 | 0 | { |
5221 | 0 | if (g_variant_format_string_is_leaf (*str)) |
5222 | 0 | g_variant_valist_skip_leaf (str, app); |
5223 | | |
5224 | 0 | else if (**str == 'm') /* maybe */ |
5225 | 0 | { |
5226 | 0 | (*str)++; |
5227 | |
|
5228 | 0 | if (!g_variant_format_string_is_nnp (*str)) |
5229 | 0 | va_arg (*app, gboolean); |
5230 | |
|
5231 | 0 | g_variant_valist_skip (str, app); |
5232 | 0 | } |
5233 | 0 | else /* tuple, dictionary entry */ |
5234 | 0 | { |
5235 | 0 | g_assert (**str == '(' || **str == '{'); |
5236 | 0 | (*str)++; |
5237 | 0 | while (**str != ')' && **str != '}') |
5238 | 0 | g_variant_valist_skip (str, app); |
5239 | 0 | (*str)++; |
5240 | 0 | } |
5241 | 0 | } |
5242 | | |
5243 | | static GVariant * |
5244 | | g_variant_valist_new (const gchar **str, |
5245 | | va_list *app) |
5246 | 0 | { |
5247 | 0 | if (g_variant_format_string_is_leaf (*str)) |
5248 | 0 | return g_variant_valist_new_leaf (str, app); |
5249 | | |
5250 | 0 | if (**str == 'm') /* maybe */ |
5251 | 0 | { |
5252 | 0 | GVariantType *type = NULL; |
5253 | 0 | GVariant *value = NULL; |
5254 | |
|
5255 | 0 | (*str)++; |
5256 | |
|
5257 | 0 | if (g_variant_format_string_is_nnp (*str)) |
5258 | 0 | { |
5259 | 0 | gpointer nnp = va_arg (*app, gpointer); |
5260 | |
|
5261 | 0 | if (nnp != NULL) |
5262 | 0 | value = g_variant_valist_new_nnp (str, nnp); |
5263 | 0 | else |
5264 | 0 | type = g_variant_format_string_scan_type (*str, NULL, str); |
5265 | 0 | } |
5266 | 0 | else |
5267 | 0 | { |
5268 | 0 | gboolean just = va_arg (*app, gboolean); |
5269 | |
|
5270 | 0 | if (just) |
5271 | 0 | value = g_variant_valist_new (str, app); |
5272 | 0 | else |
5273 | 0 | { |
5274 | 0 | type = g_variant_format_string_scan_type (*str, NULL, NULL); |
5275 | 0 | g_variant_valist_skip (str, app); |
5276 | 0 | } |
5277 | 0 | } |
5278 | |
|
5279 | 0 | value = g_variant_new_maybe (type, value); |
5280 | |
|
5281 | 0 | if (type != NULL) |
5282 | 0 | g_variant_type_free (type); |
5283 | |
|
5284 | 0 | return value; |
5285 | 0 | } |
5286 | 0 | else /* tuple, dictionary entry */ |
5287 | 0 | { |
5288 | 0 | GVariantBuilder b; |
5289 | |
|
5290 | 0 | if (**str == '(') |
5291 | 0 | g_variant_builder_init (&b, G_VARIANT_TYPE_TUPLE); |
5292 | 0 | else |
5293 | 0 | { |
5294 | 0 | g_assert (**str == '{'); |
5295 | 0 | g_variant_builder_init (&b, G_VARIANT_TYPE_DICT_ENTRY); |
5296 | 0 | } |
5297 | | |
5298 | 0 | (*str)++; /* '(' */ |
5299 | 0 | while (**str != ')' && **str != '}') |
5300 | 0 | g_variant_builder_add_value (&b, g_variant_valist_new (str, app)); |
5301 | 0 | (*str)++; /* ')' */ |
5302 | |
|
5303 | 0 | return g_variant_builder_end (&b); |
5304 | 0 | } |
5305 | 0 | } |
5306 | | |
5307 | | static void |
5308 | | g_variant_valist_get (const gchar **str, |
5309 | | GVariant *value, |
5310 | | gboolean free, |
5311 | | va_list *app) |
5312 | 0 | { |
5313 | 0 | if (g_variant_format_string_is_leaf (*str)) |
5314 | 0 | g_variant_valist_get_leaf (str, value, free, app); |
5315 | | |
5316 | 0 | else if (**str == 'm') |
5317 | 0 | { |
5318 | 0 | (*str)++; |
5319 | |
|
5320 | 0 | if (value != NULL) |
5321 | 0 | value = g_variant_get_maybe (value); |
5322 | |
|
5323 | 0 | if (!g_variant_format_string_is_nnp (*str)) |
5324 | 0 | { |
5325 | 0 | gboolean *ptr = va_arg (*app, gboolean *); |
5326 | |
|
5327 | 0 | if (ptr != NULL) |
5328 | 0 | *ptr = value != NULL; |
5329 | 0 | } |
5330 | |
|
5331 | 0 | g_variant_valist_get (str, value, free, app); |
5332 | |
|
5333 | 0 | if (value != NULL) |
5334 | 0 | g_variant_unref (value); |
5335 | 0 | } |
5336 | | |
5337 | 0 | else /* tuple, dictionary entry */ |
5338 | 0 | { |
5339 | 0 | gint index = 0; |
5340 | |
|
5341 | 0 | g_assert (**str == '(' || **str == '{'); |
5342 | | |
5343 | 0 | (*str)++; |
5344 | 0 | while (**str != ')' && **str != '}') |
5345 | 0 | { |
5346 | 0 | if (value != NULL) |
5347 | 0 | { |
5348 | 0 | GVariant *child = g_variant_get_child_value (value, index++); |
5349 | 0 | g_variant_valist_get (str, child, free, app); |
5350 | 0 | g_variant_unref (child); |
5351 | 0 | } |
5352 | 0 | else |
5353 | 0 | g_variant_valist_get (str, NULL, free, app); |
5354 | 0 | } |
5355 | 0 | (*str)++; |
5356 | 0 | } |
5357 | 0 | } |
5358 | | |
5359 | | /* User-facing API {{{2 */ |
5360 | | /** |
5361 | | * g_variant_new: (skip) |
5362 | | * @format_string: a #GVariant format string |
5363 | | * @...: arguments, as per @format_string |
5364 | | * |
5365 | | * Creates a new #GVariant instance. |
5366 | | * |
5367 | | * Think of this function as an analogue to g_strdup_printf(). |
5368 | | * |
5369 | | * The type of the created instance and the arguments that are expected |
5370 | | * by this function are determined by @format_string. See the section on |
5371 | | * [GVariant format strings][gvariant-format-strings]. Please note that |
5372 | | * the syntax of the format string is very likely to be extended in the |
5373 | | * future. |
5374 | | * |
5375 | | * The first character of the format string must not be '*' '?' '@' or |
5376 | | * 'r'; in essence, a new #GVariant must always be constructed by this |
5377 | | * function (and not merely passed through it unmodified). |
5378 | | * |
5379 | | * Note that the arguments must be of the correct width for their types |
5380 | | * specified in @format_string. This can be achieved by casting them. See |
5381 | | * the [GVariant varargs documentation][gvariant-varargs]. |
5382 | | * |
5383 | | * |[<!-- language="C" --> |
5384 | | * MyFlags some_flags = FLAG_ONE | FLAG_TWO; |
5385 | | * const gchar *some_strings[] = { "a", "b", "c", NULL }; |
5386 | | * GVariant *new_variant; |
5387 | | * |
5388 | | * new_variant = g_variant_new ("(t^as)", |
5389 | | * // This cast is required. |
5390 | | * (guint64) some_flags, |
5391 | | * some_strings); |
5392 | | * ]| |
5393 | | * |
5394 | | * Returns: a new floating #GVariant instance |
5395 | | * |
5396 | | * Since: 2.24 |
5397 | | **/ |
5398 | | GVariant * |
5399 | | g_variant_new (const gchar *format_string, |
5400 | | ...) |
5401 | 0 | { |
5402 | 0 | GVariant *value; |
5403 | 0 | va_list ap; |
5404 | |
|
5405 | 0 | g_return_val_if_fail (valid_format_string (format_string, TRUE, NULL) && |
5406 | 0 | format_string[0] != '?' && format_string[0] != '@' && |
5407 | 0 | format_string[0] != '*' && format_string[0] != 'r', |
5408 | 0 | NULL); |
5409 | | |
5410 | 0 | va_start (ap, format_string); |
5411 | 0 | value = g_variant_new_va (format_string, NULL, &ap); |
5412 | 0 | va_end (ap); |
5413 | |
|
5414 | 0 | return value; |
5415 | 0 | } |
5416 | | |
5417 | | /** |
5418 | | * g_variant_new_va: (skip) |
5419 | | * @format_string: a string that is prefixed with a format string |
5420 | | * @endptr: (nullable) (default NULL): location to store the end pointer, |
5421 | | * or %NULL |
5422 | | * @app: a pointer to a #va_list |
5423 | | * |
5424 | | * This function is intended to be used by libraries based on |
5425 | | * #GVariant that want to provide g_variant_new()-like functionality |
5426 | | * to their users. |
5427 | | * |
5428 | | * The API is more general than g_variant_new() to allow a wider range |
5429 | | * of possible uses. |
5430 | | * |
5431 | | * @format_string must still point to a valid format string, but it only |
5432 | | * needs to be nul-terminated if @endptr is %NULL. If @endptr is |
5433 | | * non-%NULL then it is updated to point to the first character past the |
5434 | | * end of the format string. |
5435 | | * |
5436 | | * @app is a pointer to a #va_list. The arguments, according to |
5437 | | * @format_string, are collected from this #va_list and the list is left |
5438 | | * pointing to the argument following the last. |
5439 | | * |
5440 | | * Note that the arguments in @app must be of the correct width for their |
5441 | | * types specified in @format_string when collected into the #va_list. |
5442 | | * See the [GVariant varargs documentation][gvariant-varargs]. |
5443 | | * |
5444 | | * These two generalisations allow mixing of multiple calls to |
5445 | | * g_variant_new_va() and g_variant_get_va() within a single actual |
5446 | | * varargs call by the user. |
5447 | | * |
5448 | | * The return value will be floating if it was a newly created GVariant |
5449 | | * instance (for example, if the format string was "(ii)"). In the case |
5450 | | * that the format_string was '*', '?', 'r', or a format starting with |
5451 | | * '@' then the collected #GVariant pointer will be returned unmodified, |
5452 | | * without adding any additional references. |
5453 | | * |
5454 | | * In order to behave correctly in all cases it is necessary for the |
5455 | | * calling function to g_variant_ref_sink() the return result before |
5456 | | * returning control to the user that originally provided the pointer. |
5457 | | * At this point, the caller will have their own full reference to the |
5458 | | * result. This can also be done by adding the result to a container, |
5459 | | * or by passing it to another g_variant_new() call. |
5460 | | * |
5461 | | * Returns: a new, usually floating, #GVariant |
5462 | | * |
5463 | | * Since: 2.24 |
5464 | | **/ |
5465 | | GVariant * |
5466 | | g_variant_new_va (const gchar *format_string, |
5467 | | const gchar **endptr, |
5468 | | va_list *app) |
5469 | 0 | { |
5470 | 0 | GVariant *value; |
5471 | |
|
5472 | 0 | g_return_val_if_fail (valid_format_string (format_string, !endptr, NULL), |
5473 | 0 | NULL); |
5474 | 0 | g_return_val_if_fail (app != NULL, NULL); |
5475 | | |
5476 | 0 | value = g_variant_valist_new (&format_string, app); |
5477 | |
|
5478 | 0 | if (endptr != NULL) |
5479 | 0 | *endptr = format_string; |
5480 | |
|
5481 | 0 | return value; |
5482 | 0 | } |
5483 | | |
5484 | | /** |
5485 | | * g_variant_get: (skip) |
5486 | | * @value: a #GVariant instance |
5487 | | * @format_string: a #GVariant format string |
5488 | | * @...: arguments, as per @format_string |
5489 | | * |
5490 | | * Deconstructs a #GVariant instance. |
5491 | | * |
5492 | | * Think of this function as an analogue to scanf(). |
5493 | | * |
5494 | | * The arguments that are expected by this function are entirely |
5495 | | * determined by @format_string. @format_string also restricts the |
5496 | | * permissible types of @value. It is an error to give a value with |
5497 | | * an incompatible type. See the section on |
5498 | | * [GVariant format strings][gvariant-format-strings]. |
5499 | | * Please note that the syntax of the format string is very likely to be |
5500 | | * extended in the future. |
5501 | | * |
5502 | | * @format_string determines the C types that are used for unpacking |
5503 | | * the values and also determines if the values are copied or borrowed, |
5504 | | * see the section on |
5505 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
5506 | | * |
5507 | | * Since: 2.24 |
5508 | | **/ |
5509 | | void |
5510 | | g_variant_get (GVariant *value, |
5511 | | const gchar *format_string, |
5512 | | ...) |
5513 | 0 | { |
5514 | 0 | va_list ap; |
5515 | |
|
5516 | 0 | g_return_if_fail (value != NULL); |
5517 | 0 | g_return_if_fail (valid_format_string (format_string, TRUE, value)); |
5518 | | |
5519 | | /* if any direct-pointer-access formats are in use, flatten first */ |
5520 | 0 | if (strchr (format_string, '&')) |
5521 | 0 | g_variant_get_data (value); |
5522 | |
|
5523 | 0 | va_start (ap, format_string); |
5524 | 0 | g_variant_get_va (value, format_string, NULL, &ap); |
5525 | 0 | va_end (ap); |
5526 | 0 | } |
5527 | | |
5528 | | /** |
5529 | | * g_variant_get_va: (skip) |
5530 | | * @value: a #GVariant |
5531 | | * @format_string: a string that is prefixed with a format string |
5532 | | * @endptr: (nullable) (default NULL): location to store the end pointer, |
5533 | | * or %NULL |
5534 | | * @app: a pointer to a #va_list |
5535 | | * |
5536 | | * This function is intended to be used by libraries based on #GVariant |
5537 | | * that want to provide g_variant_get()-like functionality to their |
5538 | | * users. |
5539 | | * |
5540 | | * The API is more general than g_variant_get() to allow a wider range |
5541 | | * of possible uses. |
5542 | | * |
5543 | | * @format_string must still point to a valid format string, but it only |
5544 | | * need to be nul-terminated if @endptr is %NULL. If @endptr is |
5545 | | * non-%NULL then it is updated to point to the first character past the |
5546 | | * end of the format string. |
5547 | | * |
5548 | | * @app is a pointer to a #va_list. The arguments, according to |
5549 | | * @format_string, are collected from this #va_list and the list is left |
5550 | | * pointing to the argument following the last. |
5551 | | * |
5552 | | * These two generalisations allow mixing of multiple calls to |
5553 | | * g_variant_new_va() and g_variant_get_va() within a single actual |
5554 | | * varargs call by the user. |
5555 | | * |
5556 | | * @format_string determines the C types that are used for unpacking |
5557 | | * the values and also determines if the values are copied or borrowed, |
5558 | | * see the section on |
5559 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
5560 | | * |
5561 | | * Since: 2.24 |
5562 | | **/ |
5563 | | void |
5564 | | g_variant_get_va (GVariant *value, |
5565 | | const gchar *format_string, |
5566 | | const gchar **endptr, |
5567 | | va_list *app) |
5568 | 0 | { |
5569 | 0 | g_return_if_fail (valid_format_string (format_string, !endptr, value)); |
5570 | 0 | g_return_if_fail (value != NULL); |
5571 | 0 | g_return_if_fail (app != NULL); |
5572 | | |
5573 | | /* if any direct-pointer-access formats are in use, flatten first */ |
5574 | 0 | if (strchr (format_string, '&')) |
5575 | 0 | g_variant_get_data (value); |
5576 | |
|
5577 | 0 | g_variant_valist_get (&format_string, value, FALSE, app); |
5578 | |
|
5579 | 0 | if (endptr != NULL) |
5580 | 0 | *endptr = format_string; |
5581 | 0 | } |
5582 | | |
5583 | | /* Varargs-enabled Utility Functions {{{1 */ |
5584 | | |
5585 | | /** |
5586 | | * g_variant_builder_add: (skip) |
5587 | | * @builder: a #GVariantBuilder |
5588 | | * @format_string: a #GVariant varargs format string |
5589 | | * @...: arguments, as per @format_string |
5590 | | * |
5591 | | * Adds to a #GVariantBuilder. |
5592 | | * |
5593 | | * This call is a convenience wrapper that is exactly equivalent to |
5594 | | * calling g_variant_new() followed by g_variant_builder_add_value(). |
5595 | | * |
5596 | | * Note that the arguments must be of the correct width for their types |
5597 | | * specified in @format_string. This can be achieved by casting them. See |
5598 | | * the [GVariant varargs documentation][gvariant-varargs]. |
5599 | | * |
5600 | | * This function might be used as follows: |
5601 | | * |
5602 | | * |[<!-- language="C" --> |
5603 | | * GVariant * |
5604 | | * make_pointless_dictionary (void) |
5605 | | * { |
5606 | | * GVariantBuilder builder; |
5607 | | * int i; |
5608 | | * |
5609 | | * g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
5610 | | * for (i = 0; i < 16; i++) |
5611 | | * { |
5612 | | * gchar buf[3]; |
5613 | | * |
5614 | | * sprintf (buf, "%d", i); |
5615 | | * g_variant_builder_add (&builder, "{is}", i, buf); |
5616 | | * } |
5617 | | * |
5618 | | * return g_variant_builder_end (&builder); |
5619 | | * } |
5620 | | * ]| |
5621 | | * |
5622 | | * Since: 2.24 |
5623 | | */ |
5624 | | void |
5625 | | g_variant_builder_add (GVariantBuilder *builder, |
5626 | | const gchar *format_string, |
5627 | | ...) |
5628 | 0 | { |
5629 | 0 | GVariant *variant; |
5630 | 0 | va_list ap; |
5631 | |
|
5632 | 0 | va_start (ap, format_string); |
5633 | 0 | variant = g_variant_new_va (format_string, NULL, &ap); |
5634 | 0 | va_end (ap); |
5635 | |
|
5636 | 0 | g_variant_builder_add_value (builder, variant); |
5637 | 0 | } |
5638 | | |
5639 | | /** |
5640 | | * g_variant_get_child: (skip) |
5641 | | * @value: a container #GVariant |
5642 | | * @index_: the index of the child to deconstruct |
5643 | | * @format_string: a #GVariant format string |
5644 | | * @...: arguments, as per @format_string |
5645 | | * |
5646 | | * Reads a child item out of a container #GVariant instance and |
5647 | | * deconstructs it according to @format_string. This call is |
5648 | | * essentially a combination of g_variant_get_child_value() and |
5649 | | * g_variant_get(). |
5650 | | * |
5651 | | * @format_string determines the C types that are used for unpacking |
5652 | | * the values and also determines if the values are copied or borrowed, |
5653 | | * see the section on |
5654 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
5655 | | * |
5656 | | * Since: 2.24 |
5657 | | **/ |
5658 | | void |
5659 | | g_variant_get_child (GVariant *value, |
5660 | | gsize index_, |
5661 | | const gchar *format_string, |
5662 | | ...) |
5663 | 0 | { |
5664 | 0 | GVariant *child; |
5665 | 0 | va_list ap; |
5666 | | |
5667 | | /* if any direct-pointer-access formats are in use, flatten first */ |
5668 | 0 | if (strchr (format_string, '&')) |
5669 | 0 | g_variant_get_data (value); |
5670 | |
|
5671 | 0 | child = g_variant_get_child_value (value, index_); |
5672 | 0 | g_return_if_fail (valid_format_string (format_string, TRUE, child)); |
5673 | | |
5674 | 0 | va_start (ap, format_string); |
5675 | 0 | g_variant_get_va (child, format_string, NULL, &ap); |
5676 | 0 | va_end (ap); |
5677 | |
|
5678 | 0 | g_variant_unref (child); |
5679 | 0 | } |
5680 | | |
5681 | | /** |
5682 | | * g_variant_iter_next: (skip) |
5683 | | * @iter: a #GVariantIter |
5684 | | * @format_string: a GVariant format string |
5685 | | * @...: the arguments to unpack the value into |
5686 | | * |
5687 | | * Gets the next item in the container and unpacks it into the variable |
5688 | | * argument list according to @format_string, returning %TRUE. |
5689 | | * |
5690 | | * If no more items remain then %FALSE is returned. |
5691 | | * |
5692 | | * All of the pointers given on the variable arguments list of this |
5693 | | * function are assumed to point at uninitialised memory. It is the |
5694 | | * responsibility of the caller to free all of the values returned by |
5695 | | * the unpacking process. |
5696 | | * |
5697 | | * Here is an example for memory management with g_variant_iter_next(): |
5698 | | * |[<!-- language="C" --> |
5699 | | * // Iterates a dictionary of type 'a{sv}' |
5700 | | * void |
5701 | | * iterate_dictionary (GVariant *dictionary) |
5702 | | * { |
5703 | | * GVariantIter iter; |
5704 | | * GVariant *value; |
5705 | | * gchar *key; |
5706 | | * |
5707 | | * g_variant_iter_init (&iter, dictionary); |
5708 | | * while (g_variant_iter_next (&iter, "{sv}", &key, &value)) |
5709 | | * { |
5710 | | * g_print ("Item '%s' has type '%s'\n", key, |
5711 | | * g_variant_get_type_string (value)); |
5712 | | * |
5713 | | * // must free data for ourselves |
5714 | | * g_variant_unref (value); |
5715 | | * g_free (key); |
5716 | | * } |
5717 | | * } |
5718 | | * ]| |
5719 | | * |
5720 | | * For a solution that is likely to be more convenient to C programmers |
5721 | | * when dealing with loops, see g_variant_iter_loop(). |
5722 | | * |
5723 | | * @format_string determines the C types that are used for unpacking |
5724 | | * the values and also determines if the values are copied or borrowed. |
5725 | | * |
5726 | | * See the section on |
5727 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
5728 | | * |
5729 | | * Returns: %TRUE if a value was unpacked, or %FALSE if there as no value |
5730 | | * |
5731 | | * Since: 2.24 |
5732 | | **/ |
5733 | | gboolean |
5734 | | g_variant_iter_next (GVariantIter *iter, |
5735 | | const gchar *format_string, |
5736 | | ...) |
5737 | 0 | { |
5738 | 0 | GVariant *value; |
5739 | |
|
5740 | 0 | value = g_variant_iter_next_value (iter); |
5741 | |
|
5742 | 0 | g_return_val_if_fail (valid_format_string (format_string, TRUE, value), |
5743 | 0 | FALSE); |
5744 | | |
5745 | 0 | if (value != NULL) |
5746 | 0 | { |
5747 | 0 | va_list ap; |
5748 | |
|
5749 | 0 | va_start (ap, format_string); |
5750 | 0 | g_variant_valist_get (&format_string, value, FALSE, &ap); |
5751 | 0 | va_end (ap); |
5752 | |
|
5753 | 0 | g_variant_unref (value); |
5754 | 0 | } |
5755 | |
|
5756 | 0 | return value != NULL; |
5757 | 0 | } |
5758 | | |
5759 | | /** |
5760 | | * g_variant_iter_loop: (skip) |
5761 | | * @iter: a #GVariantIter |
5762 | | * @format_string: a GVariant format string |
5763 | | * @...: the arguments to unpack the value into |
5764 | | * |
5765 | | * Gets the next item in the container and unpacks it into the variable |
5766 | | * argument list according to @format_string, returning %TRUE. |
5767 | | * |
5768 | | * If no more items remain then %FALSE is returned. |
5769 | | * |
5770 | | * On the first call to this function, the pointers appearing on the |
5771 | | * variable argument list are assumed to point at uninitialised memory. |
5772 | | * On the second and later calls, it is assumed that the same pointers |
5773 | | * will be given and that they will point to the memory as set by the |
5774 | | * previous call to this function. This allows the previous values to |
5775 | | * be freed, as appropriate. |
5776 | | * |
5777 | | * This function is intended to be used with a while loop as |
5778 | | * demonstrated in the following example. This function can only be |
5779 | | * used when iterating over an array. It is only valid to call this |
5780 | | * function with a string constant for the format string and the same |
5781 | | * string constant must be used each time. Mixing calls to this |
5782 | | * function and g_variant_iter_next() or g_variant_iter_next_value() on |
5783 | | * the same iterator causes undefined behavior. |
5784 | | * |
5785 | | * If you break out of a such a while loop using g_variant_iter_loop() then |
5786 | | * you must free or unreference all the unpacked values as you would with |
5787 | | * g_variant_get(). Failure to do so will cause a memory leak. |
5788 | | * |
5789 | | * Here is an example for memory management with g_variant_iter_loop(): |
5790 | | * |[<!-- language="C" --> |
5791 | | * // Iterates a dictionary of type 'a{sv}' |
5792 | | * void |
5793 | | * iterate_dictionary (GVariant *dictionary) |
5794 | | * { |
5795 | | * GVariantIter iter; |
5796 | | * GVariant *value; |
5797 | | * gchar *key; |
5798 | | * |
5799 | | * g_variant_iter_init (&iter, dictionary); |
5800 | | * while (g_variant_iter_loop (&iter, "{sv}", &key, &value)) |
5801 | | * { |
5802 | | * g_print ("Item '%s' has type '%s'\n", key, |
5803 | | * g_variant_get_type_string (value)); |
5804 | | * |
5805 | | * // no need to free 'key' and 'value' here |
5806 | | * // unless breaking out of this loop |
5807 | | * } |
5808 | | * } |
5809 | | * ]| |
5810 | | * |
5811 | | * For most cases you should use g_variant_iter_next(). |
5812 | | * |
5813 | | * This function is really only useful when unpacking into #GVariant or |
5814 | | * #GVariantIter in order to allow you to skip the call to |
5815 | | * g_variant_unref() or g_variant_iter_free(). |
5816 | | * |
5817 | | * For example, if you are only looping over simple integer and string |
5818 | | * types, g_variant_iter_next() is definitely preferred. For string |
5819 | | * types, use the '&' prefix to avoid allocating any memory at all (and |
5820 | | * thereby avoiding the need to free anything as well). |
5821 | | * |
5822 | | * @format_string determines the C types that are used for unpacking |
5823 | | * the values and also determines if the values are copied or borrowed. |
5824 | | * |
5825 | | * See the section on |
5826 | | * [GVariant format strings][gvariant-format-strings-pointers]. |
5827 | | * |
5828 | | * Returns: %TRUE if a value was unpacked, or %FALSE if there was no |
5829 | | * value |
5830 | | * |
5831 | | * Since: 2.24 |
5832 | | **/ |
5833 | | gboolean |
5834 | | g_variant_iter_loop (GVariantIter *iter, |
5835 | | const gchar *format_string, |
5836 | | ...) |
5837 | 0 | { |
5838 | 0 | gboolean first_time = GVSI(iter)->loop_format == NULL; |
5839 | 0 | GVariant *value; |
5840 | 0 | va_list ap; |
5841 | |
|
5842 | 0 | g_return_val_if_fail (first_time || |
5843 | 0 | format_string == GVSI(iter)->loop_format, |
5844 | 0 | FALSE); |
5845 | | |
5846 | 0 | if (first_time) |
5847 | 0 | { |
5848 | 0 | TYPE_CHECK (GVSI(iter)->value, G_VARIANT_TYPE_ARRAY, FALSE); |
5849 | 0 | GVSI(iter)->loop_format = format_string; |
5850 | |
|
5851 | 0 | if (strchr (format_string, '&')) |
5852 | 0 | g_variant_get_data (GVSI(iter)->value); |
5853 | 0 | } |
5854 | | |
5855 | 0 | value = g_variant_iter_next_value (iter); |
5856 | |
|
5857 | 0 | g_return_val_if_fail (!first_time || |
5858 | 0 | valid_format_string (format_string, TRUE, value), |
5859 | 0 | FALSE); |
5860 | | |
5861 | 0 | va_start (ap, format_string); |
5862 | 0 | g_variant_valist_get (&format_string, value, !first_time, &ap); |
5863 | 0 | va_end (ap); |
5864 | |
|
5865 | 0 | if (value != NULL) |
5866 | 0 | g_variant_unref (value); |
5867 | |
|
5868 | 0 | return value != NULL; |
5869 | 0 | } |
5870 | | |
5871 | | /* Serialized data {{{1 */ |
5872 | | static GVariant * |
5873 | | g_variant_deep_copy (GVariant *value, |
5874 | | gboolean byteswap) |
5875 | 0 | { |
5876 | 0 | switch (g_variant_classify (value)) |
5877 | 0 | { |
5878 | 0 | case G_VARIANT_CLASS_MAYBE: |
5879 | 0 | case G_VARIANT_CLASS_TUPLE: |
5880 | 0 | case G_VARIANT_CLASS_DICT_ENTRY: |
5881 | 0 | case G_VARIANT_CLASS_VARIANT: |
5882 | 0 | { |
5883 | 0 | GVariantBuilder builder; |
5884 | 0 | gsize i, n_children; |
5885 | |
|
5886 | 0 | g_variant_builder_init (&builder, g_variant_get_type (value)); |
5887 | |
|
5888 | 0 | for (i = 0, n_children = g_variant_n_children (value); i < n_children; i++) |
5889 | 0 | { |
5890 | 0 | GVariant *child = g_variant_get_child_value (value, i); |
5891 | 0 | g_variant_builder_add_value (&builder, g_variant_deep_copy (child, byteswap)); |
5892 | 0 | g_variant_unref (child); |
5893 | 0 | } |
5894 | |
|
5895 | 0 | return g_variant_builder_end (&builder); |
5896 | 0 | } |
5897 | | |
5898 | 0 | case G_VARIANT_CLASS_ARRAY: |
5899 | 0 | { |
5900 | 0 | GVariantBuilder builder; |
5901 | 0 | gsize i, n_children; |
5902 | 0 | GVariant *first_invalid_child_deep_copy = NULL; |
5903 | | |
5904 | | /* Arrays are in theory treated the same as maybes, tuples, dict entries |
5905 | | * and variants, and could be another case in the above block of code. |
5906 | | * |
5907 | | * However, they have the property that when dealing with non-normal |
5908 | | * data (which is the only time g_variant_deep_copy() is currently |
5909 | | * called) in a variable-sized array, the code above can easily end up |
5910 | | * creating many default child values in order to return an array which |
5911 | | * is of the right length and type, but without containing non-normal |
5912 | | * data. This can happen if the offset table for the array is malformed. |
5913 | | * |
5914 | | * In this case, the code above would end up allocating the same default |
5915 | | * value for each one of the child indexes beyond the first malformed |
5916 | | * entry in the offset table. This can end up being a lot of identical |
5917 | | * allocations of default values, particularly if the non-normal array |
5918 | | * is crafted maliciously. |
5919 | | * |
5920 | | * Avoid that problem by returning a new reference to the same default |
5921 | | * value for every child after the first invalid one. This results in |
5922 | | * returning an equivalent array, in normal form and trusted — but with |
5923 | | * significantly fewer memory allocations. |
5924 | | * |
5925 | | * See https://gitlab.gnome.org/GNOME/glib/-/issues/2540 */ |
5926 | |
|
5927 | 0 | g_variant_builder_init (&builder, g_variant_get_type (value)); |
5928 | |
|
5929 | 0 | for (i = 0, n_children = g_variant_n_children (value); i < n_children; i++) |
5930 | 0 | { |
5931 | | /* Try maybe_get_child_value() first; if it returns NULL, this child |
5932 | | * is non-normal. get_child_value() would have constructed and |
5933 | | * returned a default value in that case. */ |
5934 | 0 | GVariant *child = g_variant_maybe_get_child_value (value, i); |
5935 | |
|
5936 | 0 | if (child != NULL) |
5937 | 0 | { |
5938 | | /* Non-normal children may not always be contiguous, as they may |
5939 | | * be non-normal for reasons other than invalid offset table |
5940 | | * entries. As they are all the same type, they will all have |
5941 | | * the same default value though, so keep that around. */ |
5942 | 0 | g_variant_builder_add_value (&builder, g_variant_deep_copy (child, byteswap)); |
5943 | 0 | } |
5944 | 0 | else if (child == NULL && first_invalid_child_deep_copy != NULL) |
5945 | 0 | { |
5946 | 0 | g_variant_builder_add_value (&builder, first_invalid_child_deep_copy); |
5947 | 0 | } |
5948 | 0 | else if (child == NULL) |
5949 | 0 | { |
5950 | 0 | child = g_variant_get_child_value (value, i); |
5951 | 0 | first_invalid_child_deep_copy = g_variant_ref_sink (g_variant_deep_copy (child, byteswap)); |
5952 | 0 | g_variant_builder_add_value (&builder, first_invalid_child_deep_copy); |
5953 | 0 | } |
5954 | |
|
5955 | 0 | g_clear_pointer (&child, g_variant_unref); |
5956 | 0 | } |
5957 | |
|
5958 | 0 | g_clear_pointer (&first_invalid_child_deep_copy, g_variant_unref); |
5959 | |
|
5960 | 0 | return g_variant_builder_end (&builder); |
5961 | 0 | } |
5962 | | |
5963 | 0 | case G_VARIANT_CLASS_BOOLEAN: |
5964 | 0 | return g_variant_new_boolean (g_variant_get_boolean (value)); |
5965 | | |
5966 | 0 | case G_VARIANT_CLASS_BYTE: |
5967 | 0 | return g_variant_new_byte (g_variant_get_byte (value)); |
5968 | | |
5969 | 0 | case G_VARIANT_CLASS_INT16: |
5970 | 0 | if (byteswap) |
5971 | 0 | return g_variant_new_int16 (GUINT16_SWAP_LE_BE (g_variant_get_int16 (value))); |
5972 | 0 | else |
5973 | 0 | return g_variant_new_int16 (g_variant_get_int16 (value)); |
5974 | | |
5975 | 0 | case G_VARIANT_CLASS_UINT16: |
5976 | 0 | if (byteswap) |
5977 | 0 | return g_variant_new_uint16 (GUINT16_SWAP_LE_BE (g_variant_get_uint16 (value))); |
5978 | 0 | else |
5979 | 0 | return g_variant_new_uint16 (g_variant_get_uint16 (value)); |
5980 | | |
5981 | 0 | case G_VARIANT_CLASS_INT32: |
5982 | 0 | if (byteswap) |
5983 | 0 | return g_variant_new_int32 (GUINT32_SWAP_LE_BE (g_variant_get_int32 (value))); |
5984 | 0 | else |
5985 | 0 | return g_variant_new_int32 (g_variant_get_int32 (value)); |
5986 | | |
5987 | 0 | case G_VARIANT_CLASS_UINT32: |
5988 | 0 | if (byteswap) |
5989 | 0 | return g_variant_new_uint32 (GUINT32_SWAP_LE_BE (g_variant_get_uint32 (value))); |
5990 | 0 | else |
5991 | 0 | return g_variant_new_uint32 (g_variant_get_uint32 (value)); |
5992 | | |
5993 | 0 | case G_VARIANT_CLASS_INT64: |
5994 | 0 | if (byteswap) |
5995 | 0 | return g_variant_new_int64 (GUINT64_SWAP_LE_BE (g_variant_get_int64 (value))); |
5996 | 0 | else |
5997 | 0 | return g_variant_new_int64 (g_variant_get_int64 (value)); |
5998 | | |
5999 | 0 | case G_VARIANT_CLASS_UINT64: |
6000 | 0 | if (byteswap) |
6001 | 0 | return g_variant_new_uint64 (GUINT64_SWAP_LE_BE (g_variant_get_uint64 (value))); |
6002 | 0 | else |
6003 | 0 | return g_variant_new_uint64 (g_variant_get_uint64 (value)); |
6004 | | |
6005 | 0 | case G_VARIANT_CLASS_HANDLE: |
6006 | 0 | if (byteswap) |
6007 | 0 | return g_variant_new_handle (GUINT32_SWAP_LE_BE (g_variant_get_handle (value))); |
6008 | 0 | else |
6009 | 0 | return g_variant_new_handle (g_variant_get_handle (value)); |
6010 | | |
6011 | 0 | case G_VARIANT_CLASS_DOUBLE: |
6012 | 0 | if (byteswap) |
6013 | 0 | { |
6014 | | /* We have to convert the double to a uint64 here using a union, |
6015 | | * because a cast will round it numerically. */ |
6016 | 0 | union |
6017 | 0 | { |
6018 | 0 | guint64 u64; |
6019 | 0 | gdouble dbl; |
6020 | 0 | } u1, u2; |
6021 | 0 | u1.dbl = g_variant_get_double (value); |
6022 | 0 | u2.u64 = GUINT64_SWAP_LE_BE (u1.u64); |
6023 | 0 | return g_variant_new_double (u2.dbl); |
6024 | 0 | } |
6025 | 0 | else |
6026 | 0 | return g_variant_new_double (g_variant_get_double (value)); |
6027 | | |
6028 | 0 | case G_VARIANT_CLASS_STRING: |
6029 | 0 | return g_variant_new_string (g_variant_get_string (value, NULL)); |
6030 | | |
6031 | 0 | case G_VARIANT_CLASS_OBJECT_PATH: |
6032 | 0 | return g_variant_new_object_path (g_variant_get_string (value, NULL)); |
6033 | | |
6034 | 0 | case G_VARIANT_CLASS_SIGNATURE: |
6035 | 0 | return g_variant_new_signature (g_variant_get_string (value, NULL)); |
6036 | 0 | } |
6037 | | |
6038 | 0 | g_assert_not_reached (); |
6039 | 0 | } |
6040 | | |
6041 | | /** |
6042 | | * g_variant_get_normal_form: |
6043 | | * @value: a #GVariant |
6044 | | * |
6045 | | * Gets a #GVariant instance that has the same value as @value and is |
6046 | | * trusted to be in normal form. |
6047 | | * |
6048 | | * If @value is already trusted to be in normal form then a new |
6049 | | * reference to @value is returned. |
6050 | | * |
6051 | | * If @value is not already trusted, then it is scanned to check if it |
6052 | | * is in normal form. If it is found to be in normal form then it is |
6053 | | * marked as trusted and a new reference to it is returned. |
6054 | | * |
6055 | | * If @value is found not to be in normal form then a new trusted |
6056 | | * #GVariant is created with the same value as @value. The non-normal parts of |
6057 | | * @value will be replaced with default values which are guaranteed to be in |
6058 | | * normal form. |
6059 | | * |
6060 | | * It makes sense to call this function if you've received #GVariant |
6061 | | * data from untrusted sources and you want to ensure your serialized |
6062 | | * output is definitely in normal form. |
6063 | | * |
6064 | | * If @value is already in normal form, a new reference will be returned |
6065 | | * (which will be floating if @value is floating). If it is not in normal form, |
6066 | | * the newly created #GVariant will be returned with a single non-floating |
6067 | | * reference. Typically, g_variant_take_ref() should be called on the return |
6068 | | * value from this function to guarantee ownership of a single non-floating |
6069 | | * reference to it. |
6070 | | * |
6071 | | * Returns: (transfer full): a trusted #GVariant |
6072 | | * |
6073 | | * Since: 2.24 |
6074 | | **/ |
6075 | | GVariant * |
6076 | | g_variant_get_normal_form (GVariant *value) |
6077 | 0 | { |
6078 | 0 | GVariant *trusted; |
6079 | |
|
6080 | 0 | if (g_variant_is_normal_form (value)) |
6081 | 0 | return g_variant_ref (value); |
6082 | | |
6083 | 0 | trusted = g_variant_deep_copy (value, FALSE); |
6084 | 0 | g_assert (g_variant_is_trusted (trusted)); |
6085 | | |
6086 | 0 | return g_variant_ref_sink (trusted); |
6087 | 0 | } |
6088 | | |
6089 | | /** |
6090 | | * g_variant_byteswap: |
6091 | | * @value: a #GVariant |
6092 | | * |
6093 | | * Performs a byteswapping operation on the contents of @value. The |
6094 | | * result is that all multi-byte numeric data contained in @value is |
6095 | | * byteswapped. That includes 16, 32, and 64bit signed and unsigned |
6096 | | * integers as well as file handles and double precision floating point |
6097 | | * values. |
6098 | | * |
6099 | | * This function is an identity mapping on any value that does not |
6100 | | * contain multi-byte numeric data. That include strings, booleans, |
6101 | | * bytes and containers containing only these things (recursively). |
6102 | | * |
6103 | | * While this function can safely handle untrusted, non-normal data, it is |
6104 | | * recommended to check whether the input is in normal form beforehand, using |
6105 | | * g_variant_is_normal_form(), and to reject non-normal inputs if your |
6106 | | * application can be strict about what inputs it rejects. |
6107 | | * |
6108 | | * The returned value is always in normal form and is marked as trusted. |
6109 | | * A full, not floating, reference is returned. |
6110 | | * |
6111 | | * Returns: (transfer full): the byteswapped form of @value |
6112 | | * |
6113 | | * Since: 2.24 |
6114 | | **/ |
6115 | | GVariant * |
6116 | | g_variant_byteswap (GVariant *value) |
6117 | 0 | { |
6118 | 0 | GVariantTypeInfo *type_info; |
6119 | 0 | guint alignment; |
6120 | 0 | GVariant *new; |
6121 | |
|
6122 | 0 | type_info = g_variant_get_type_info (value); |
6123 | |
|
6124 | 0 | g_variant_type_info_query (type_info, &alignment, NULL); |
6125 | |
|
6126 | 0 | if (alignment && g_variant_is_normal_form (value)) |
6127 | 0 | { |
6128 | | /* (potentially) contains multi-byte numeric data, but is also already in |
6129 | | * normal form so we can use a faster byteswapping codepath on the |
6130 | | * serialised data */ |
6131 | 0 | GVariantSerialised serialised = { 0, }; |
6132 | 0 | GBytes *bytes; |
6133 | |
|
6134 | 0 | serialised.type_info = g_variant_get_type_info (value); |
6135 | 0 | serialised.size = g_variant_get_size (value); |
6136 | 0 | serialised.data = g_malloc (serialised.size); |
6137 | 0 | serialised.depth = g_variant_get_depth (value); |
6138 | 0 | serialised.ordered_offsets_up_to = G_MAXSIZE; /* operating on the normal form */ |
6139 | 0 | serialised.checked_offsets_up_to = G_MAXSIZE; |
6140 | 0 | g_variant_store (value, serialised.data); |
6141 | |
|
6142 | 0 | g_variant_serialised_byteswap (serialised); |
6143 | |
|
6144 | 0 | bytes = g_bytes_new_take (serialised.data, serialised.size); |
6145 | 0 | new = g_variant_ref_sink (g_variant_new_from_bytes (g_variant_get_type (value), bytes, TRUE)); |
6146 | 0 | g_bytes_unref (bytes); |
6147 | 0 | } |
6148 | 0 | else if (alignment) |
6149 | | /* (potentially) contains multi-byte numeric data */ |
6150 | 0 | new = g_variant_ref_sink (g_variant_deep_copy (value, TRUE)); |
6151 | 0 | else |
6152 | | /* contains no multi-byte data */ |
6153 | 0 | new = g_variant_get_normal_form (value); |
6154 | |
|
6155 | 0 | g_assert (g_variant_is_trusted (new)); |
6156 | | |
6157 | 0 | return g_steal_pointer (&new); |
6158 | 0 | } |
6159 | | |
6160 | | /** |
6161 | | * g_variant_new_from_data: |
6162 | | * @type: a definite #GVariantType |
6163 | | * @data: (array length=size) (element-type guint8): the serialized data |
6164 | | * @size: the size of @data |
6165 | | * @trusted: %TRUE if @data is definitely in normal form |
6166 | | * @notify: (scope async): function to call when @data is no longer needed |
6167 | | * @user_data: data for @notify |
6168 | | * |
6169 | | * Creates a new #GVariant instance from serialized data. |
6170 | | * |
6171 | | * @type is the type of #GVariant instance that will be constructed. |
6172 | | * The interpretation of @data depends on knowing the type. |
6173 | | * |
6174 | | * @data is not modified by this function and must remain valid with an |
6175 | | * unchanging value until such a time as @notify is called with |
6176 | | * @user_data. If the contents of @data change before that time then |
6177 | | * the result is undefined. |
6178 | | * |
6179 | | * If @data is trusted to be serialized data in normal form then |
6180 | | * @trusted should be %TRUE. This applies to serialized data created |
6181 | | * within this process or read from a trusted location on the disk (such |
6182 | | * as a file installed in /usr/lib alongside your application). You |
6183 | | * should set trusted to %FALSE if @data is read from the network, a |
6184 | | * file in the user's home directory, etc. |
6185 | | * |
6186 | | * If @data was not stored in this machine's native endianness, any multi-byte |
6187 | | * numeric values in the returned variant will also be in non-native |
6188 | | * endianness. g_variant_byteswap() can be used to recover the original values. |
6189 | | * |
6190 | | * @notify will be called with @user_data when @data is no longer |
6191 | | * needed. The exact time of this call is unspecified and might even be |
6192 | | * before this function returns. |
6193 | | * |
6194 | | * Note: @data must be backed by memory that is aligned appropriately for the |
6195 | | * @type being loaded. Otherwise this function will internally create a copy of |
6196 | | * the memory (since GLib 2.60) or (in older versions) fail and exit the |
6197 | | * process. |
6198 | | * |
6199 | | * Returns: (transfer none): a new floating #GVariant of type @type |
6200 | | * |
6201 | | * Since: 2.24 |
6202 | | **/ |
6203 | | GVariant * |
6204 | | g_variant_new_from_data (const GVariantType *type, |
6205 | | gconstpointer data, |
6206 | | gsize size, |
6207 | | gboolean trusted, |
6208 | | GDestroyNotify notify, |
6209 | | gpointer user_data) |
6210 | 0 | { |
6211 | 0 | GVariant *value; |
6212 | 0 | GBytes *bytes; |
6213 | |
|
6214 | 0 | g_return_val_if_fail (g_variant_type_is_definite (type), NULL); |
6215 | 0 | g_return_val_if_fail (data != NULL || size == 0, NULL); |
6216 | | |
6217 | 0 | if (notify) |
6218 | 0 | bytes = g_bytes_new_with_free_func (data, size, notify, user_data); |
6219 | 0 | else |
6220 | 0 | bytes = g_bytes_new_static (data, size); |
6221 | |
|
6222 | 0 | value = g_variant_new_from_bytes (type, bytes, trusted); |
6223 | 0 | g_bytes_unref (bytes); |
6224 | |
|
6225 | 0 | return value; |
6226 | 0 | } |
6227 | | |
6228 | | /* Epilogue {{{1 */ |
6229 | | /* vim:set foldmethod=marker: */ |