2 * Copyright © 2007, 2008 Ryan Lortie
3 * Copyright © 2010 Codethink Limited
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gvariant-serialiser.h"
26 #include <glib/gtestutils.h>
27 #include <glib/gstrfuncs.h>
28 #include <glib/gtypes.h>
35 * After this prologue section, this file has roughly 2 parts.
37 * The first part is split up into sections according to various
38 * container types. Maybe, Array, Tuple, Variant. The Maybe and Array
39 * sections are subdivided for element types being fixed or
40 * variable-sized types.
42 * Each section documents the format of that particular type of
43 * container and implements 5 functions for dealing with it:
46 * - determines (according to serialised data) how many child values
47 * are inside a particular container value.
50 * - gets the type of and the serialised data corresponding to a
51 * given child value within the container value.
54 * - determines how much space would be required to serialise a
55 * container of this type, containing the given children so that
56 * buffers can be preallocated before serialising.
59 * - write the serialised data for a container of this type,
60 * containing the given children, to a buffer.
63 * - check the given data to ensure that it is in normal form. For a
64 * given set of child values, there is exactly one normal form for
65 * the serialised data of a container. Other forms are possible
66 * while maintaining the same children (for example, by inserting
67 * something other than zero bytes as padding) but only one form is
70 * The second part contains the main entry point for each of the above 5
71 * functions and logic to dispatch it to the handler for the appropriate
72 * container type code.
74 * The second part also contains a routine to byteswap serialised
75 * values. This code makes use of the n_children() and get_child()
76 * functions above to do its work so no extra support is needed on a
77 * per-container-type basis.
79 * There is also additional code for checking for normal form. All
80 * numeric types are always in normal form since the full range of
81 * values is permitted (eg: 0 to 255 is a valid byte). Special checks
82 * need to be performed for booleans (only 0 or 1 allowed), strings
83 * (properly nul-terminated) and object paths and signature strings
84 * (meeting the D-Bus specification requirements).
89 * @type_info: the #GVariantTypeInfo of this value
90 * @data: (allow-none): the serialised data of this value, or %NULL
91 * @size: the size of this value
93 * A structure representing a GVariant in serialised form. This
94 * structure is used with #GVariantSerialisedFiller functions and as the
95 * primary interface to the serialiser. See #GVariantSerialisedFiller
96 * for a description of its use there.
98 * When used with the serialiser API functions, the following invariants
99 * apply to all #GVariantTypeSerialised structures passed to and
100 * returned from the serialiser.
102 * @type_info must be non-%NULL.
104 * @data must be properly aligned for the type described by @type_info.
106 * If @type_info describes a fixed-sized type then @size must always be
107 * equal to the fixed size of that type.
109 * For fixed-sized types (and only fixed-sized types), @data may be
110 * %NULL even if @size is non-zero. This happens when a framing error
111 * occurs while attempting to extract a fixed-sized value out of a
112 * variable-sized container. There is no data to return for the
113 * fixed-sized type, yet @size must be non-zero. The effect of this
114 * combination should be as if @data were a pointer to an
115 * appropriately-sized zero-filled region.
119 * g_variant_serialised_check:
120 * @serialised: a #GVariantSerialised struct
122 * Checks @serialised for validity according to the invariants described
126 g_variant_serialised_check (GVariantSerialised serialised)
131 g_assert (serialised.type_info != NULL);
132 g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
135 g_assert_cmpint (serialised.size, ==, fixed_size);
137 g_assert (serialised.size == 0 || serialised.data != NULL);
139 /* Depending on the native alignment requirements of the machine, the
140 * compiler will insert either 3 or 7 padding bytes after the char.
141 * This will result in the sizeof() the struct being 12 or 16.
142 * Subtract 9 to get 3 or 7 which is a nice bitmask to apply to get
143 * the alignment bits that we "care about" being zero: in the
144 * 4-aligned case, we care about 2 bits, and in the 8-aligned case, we
147 alignment &= sizeof (struct {
157 /* Some OSes (FreeBSD is a known example) have a malloc() that returns
158 * unaligned memory if you request small sizes. 'malloc (1);', for
159 * example, has been seen to return pointers aligned to 6 mod 16.
161 * Check if this is a small allocation and return without enforcing
162 * the alignment assertion if this is the case.
164 if (serialised.size <= alignment)
167 g_assert_cmpint (alignment & (gsize) serialised.data, ==, 0);
171 * GVariantSerialisedFiller:
172 * @serialised: a #GVariantSerialised instance to fill
173 * @data: data from the children array
175 * This function is called back from g_variant_serialiser_needed_size()
176 * and g_variant_serialiser_serialise(). It fills in missing details
177 * from a partially-complete #GVariantSerialised.
179 * The @data parameter passed back to the function is one of the items
180 * that was passed to the serialiser in the @children array. It
181 * represents a single child item of the container that is being
182 * serialised. The information filled in to @serialised is the
183 * information for this child.
185 * If the @type_info field of @serialised is %NULL then the callback
186 * function must set it to the type information corresponding to the
187 * type of the child. No reference should be added. If it is non-%NULL
188 * then the callback should assert that it is equal to the actual type
191 * If the @size field is zero then the callback must fill it in with the
192 * required amount of space to store the serialised form of the child.
193 * If it is non-zero then the callback should assert that it is equal to
194 * the needed size of the child.
196 * If @data is non-%NULL then it points to a space that is properly
197 * aligned for and large enough to store the serialised data of the
198 * child. The callback must store the serialised form of the child at
201 * If the child value is another container then the callback will likely
202 * recurse back into the serialiser by calling
203 * g_variant_serialiser_needed_size() to determine @size and
204 * g_variant_serialiser_serialise() to write to @data.
207 /* PART 1: Container types {{{1
209 * This section contains the serialiser implementation functions for
210 * each container type.
215 * Maybe types are handled depending on if the element type of the maybe
216 * type is a fixed-sized or variable-sized type. Although all maybe
217 * types themselves are variable-sized types, herein, a maybe value with
218 * a fixed-sized element type is called a "fixed-sized maybe" for
219 * convenience and a maybe value with a variable-sized element type is
220 * called a "variable-sized maybe".
223 /* Fixed-sized Maybe {{{3
225 * The size of a maybe value with a fixed-sized element type is either 0
226 * or equal to the fixed size of its element type. The case where the
227 * size of the maybe value is zero corresponds to the "Nothing" case and
228 * the case where the size of the maybe value is equal to the fixed size
229 * of the element type corresponds to the "Just" case; in that case, the
230 * serialised data of the child value forms the entire serialised data
231 * of the maybe value.
233 * In the event that a fixed-sized maybe value is presented with a size
234 * that is not equal to the fixed size of the element type then the
235 * value must be taken to be "Nothing".
239 gvs_fixed_sized_maybe_n_children (GVariantSerialised value)
241 gsize element_fixed_size;
243 g_variant_type_info_query_element (value.type_info, NULL,
244 &element_fixed_size);
246 return (element_fixed_size == value.size) ? 1 : 0;
249 static GVariantSerialised
250 gvs_fixed_sized_maybe_get_child (GVariantSerialised value,
253 /* the child has the same bounds as the
254 * container, so just update the type.
256 value.type_info = g_variant_type_info_element (value.type_info);
257 g_variant_type_info_ref (value.type_info);
263 gvs_fixed_sized_maybe_unpack_all (GVariantTypeInfo *type_info,
271 GVariantUnpacked unpacked;
273 unpacked.type_info = g_variant_type_info_element (type_info);
274 g_variant_type_info_ref (unpacked.type_info);
276 unpacked.size = total_size;
278 g_array_append_val (results, unpacked);
285 gvs_fixed_sized_maybe_needed_size (GVariantTypeInfo *type_info,
286 GVariantSerialisedFiller gvs_filler,
287 const gpointer *children,
292 gsize element_fixed_size;
294 g_variant_type_info_query_element (type_info, NULL,
295 &element_fixed_size);
297 return element_fixed_size;
304 gvs_fixed_sized_maybe_serialise (GVariantSerialised value,
305 GVariantSerialisedFiller gvs_filler,
306 const gpointer *children,
311 GVariantSerialised child = { NULL, value.data, value.size };
313 gvs_filler (&child, children[0]);
318 gvs_fixed_sized_maybe_write_to_vectors (GVariantVectors *vectors,
319 GVariantTypeInfo *type_info,
321 const gpointer *children,
327 return g_variant_callback_write_to_vectors (vectors, children[0], NULL);
331 gvs_fixed_sized_maybe_is_normal (GVariantSerialised value)
335 gsize element_fixed_size;
337 g_variant_type_info_query_element (value.type_info,
338 NULL, &element_fixed_size);
340 if (value.size != element_fixed_size)
343 /* proper element size: "Just". recurse to the child. */
344 value.type_info = g_variant_type_info_element (value.type_info);
346 return g_variant_serialised_is_normal (value);
349 /* size of 0: "Nothing" */
353 /* Variable-sized Maybe
355 * The size of a maybe value with a variable-sized element type is
356 * either 0 or strictly greater than 0. The case where the size of the
357 * maybe value is zero corresponds to the "Nothing" case and the case
358 * where the size of the maybe value is greater than zero corresponds to
359 * the "Just" case; in that case, the serialised data of the child value
360 * forms the first part of the serialised data of the maybe value and is
361 * followed by a single zero byte. This zero byte is always appended,
362 * regardless of any zero bytes that may already be at the end of the
363 * serialised ata of the child value.
367 gvs_variable_sized_maybe_n_children (GVariantSerialised value)
369 return (value.size > 0) ? 1 : 0;
372 static GVariantSerialised
373 gvs_variable_sized_maybe_get_child (GVariantSerialised value,
376 /* remove the padding byte and update the type. */
377 value.type_info = g_variant_type_info_element (value.type_info);
378 g_variant_type_info_ref (value.type_info);
381 /* if it's zero-sized then it may as well be NULL */
389 gvs_variable_sized_maybe_unpack_all (GVariantTypeInfo *type_info,
397 GVariantUnpacked unpacked;
399 unpacked.type_info = g_variant_type_info_element (type_info);
400 g_variant_type_info_ref (unpacked.type_info);
402 unpacked.size = total_size - 1;
404 g_array_append_val (results, unpacked);
411 gvs_variable_sized_maybe_needed_size (GVariantTypeInfo *type_info,
412 GVariantSerialisedFiller gvs_filler,
413 const gpointer *children,
418 GVariantSerialised child = { 0, };
420 gvs_filler (&child, children[0]);
422 return child.size + 1;
429 gvs_variable_sized_maybe_serialise (GVariantSerialised value,
430 GVariantSerialisedFiller gvs_filler,
431 const gpointer *children,
436 GVariantSerialised child = { NULL, value.data, value.size - 1 };
438 /* write the data for the child. */
439 gvs_filler (&child, children[0]);
440 value.data[child.size] = '\0';
445 gvs_variable_sized_maybe_write_to_vectors (GVariantVectors *vectors,
446 GVariantTypeInfo *type_info,
448 const gpointer *children,
453 g_variant_callback_write_to_vectors (vectors, children[0], NULL);
454 g_variant_vectors_append_copy (vectors, "", 1);
459 gvs_variable_sized_maybe_is_normal (GVariantSerialised value)
464 if (value.data[value.size - 1] != '\0')
467 value.type_info = g_variant_type_info_element (value.type_info);
470 return g_variant_serialised_is_normal (value);
475 * Just as with maybe types, array types are handled depending on if the
476 * element type of the array type is a fixed-sized or variable-sized
477 * type. Similar to maybe types, for convenience, an array value with a
478 * fixed-sized element type is called a "fixed-sized array" and an array
479 * value with a variable-sized element type is called a "variable sized
483 /* Fixed-sized Array {{{3
485 * For fixed sized arrays, the serialised data is simply a concatenation
486 * of the serialised data of each element, in order. Since fixed-sized
487 * values always have a fixed size that is a multiple of their alignment
488 * requirement no extra padding is required.
490 * In the event that a fixed-sized array is presented with a size that
491 * is not an integer multiple of the element size then the value of the
492 * array must be taken as being empty.
496 gvs_fixed_sized_array_n_children (GVariantSerialised value)
498 gsize element_fixed_size;
500 g_variant_type_info_query_element (value.type_info, NULL,
501 &element_fixed_size);
503 if (value.size % element_fixed_size == 0)
504 return value.size / element_fixed_size;
509 static GVariantSerialised
510 gvs_fixed_sized_array_get_child (GVariantSerialised value,
513 GVariantSerialised child = { 0, };
515 child.type_info = g_variant_type_info_element (value.type_info);
516 g_variant_type_info_query (child.type_info, NULL, &child.size);
517 child.data = value.data + (child.size * index_);
518 g_variant_type_info_ref (child.type_info);
524 gvs_fixed_sized_array_unpack_all (GVariantTypeInfo *type_info,
530 GVariantTypeInfo *element;
531 gsize element_fixed_size;
534 element = g_variant_type_info_element (type_info);
535 g_variant_type_info_query (element, NULL, &element_fixed_size);
537 if (total_size % element_fixed_size)
540 n = total_size / element_fixed_size;
542 for (i = 0; i < n; i++)
544 GVariantUnpacked unpacked;
546 unpacked.type_info = g_variant_type_info_ref (element);
548 unpacked.size = element_fixed_size;
550 g_array_append_val (results, unpacked);
557 gvs_fixed_sized_array_needed_size (GVariantTypeInfo *type_info,
558 GVariantSerialisedFiller gvs_filler,
559 const gpointer *children,
562 gsize element_fixed_size;
564 g_variant_type_info_query_element (type_info, NULL, &element_fixed_size);
566 return element_fixed_size * n_children;
570 gvs_fixed_sized_array_serialise (GVariantSerialised value,
571 GVariantSerialisedFiller gvs_filler,
572 const gpointer *children,
575 GVariantSerialised child = { 0, };
578 child.type_info = g_variant_type_info_element (value.type_info);
579 g_variant_type_info_query (child.type_info, NULL, &child.size);
580 child.data = value.data;
582 for (i = 0; i < n_children; i++)
584 gvs_filler (&child, children[i]);
585 child.data += child.size;
590 gvs_fixed_sized_array_write_to_vectors (GVariantVectors *vectors,
591 GVariantTypeInfo *type_info,
593 const gpointer *children,
598 for (i = 0; i < n_children; i++)
599 g_variant_callback_write_to_vectors (vectors, children[i], NULL);
603 gvs_fixed_sized_array_is_normal (GVariantSerialised value)
605 GVariantSerialised child = { 0, };
607 child.type_info = g_variant_type_info_element (value.type_info);
608 g_variant_type_info_query (child.type_info, NULL, &child.size);
610 if (value.size % child.size != 0)
613 for (child.data = value.data;
614 child.data < value.data + value.size;
615 child.data += child.size)
617 if (!g_variant_serialised_is_normal (child))
624 /* Variable-sized Array {{{3
626 * Variable sized arrays, containing variable-sized elements, must be
627 * able to determine the boundaries between the elements. The items
628 * cannot simply be concatenated. Additionally, we are faced with the
629 * fact that non-fixed-sized values do not necessarily have a size that
630 * is a multiple of their alignment requirement, so we may need to
631 * insert zero-filled padding.
633 * While it is possible to find the start of an item by starting from
634 * the end of the item before it and padding for alignment, it is not
635 * generally possible to do the reverse operation. For this reason, we
636 * record the end point of each element in the array.
638 * GVariant works in terms of "offsets". An offset is a pointer to a
639 * boundary between two bytes. In 4 bytes of serialised data, there
640 * would be 5 possible offsets: one at the start ('0'), one between each
641 * pair of adjacent bytes ('1', '2', '3') and one at the end ('4').
643 * The numeric value of an offset is an unsigned integer given relative
644 * to the start of the serialised data of the array. Offsets are always
645 * stored in little endian byte order and are always only as big as they
646 * need to be. For example, in 255 bytes of serialised data, there are
647 * 256 offsets. All possibilities can be stored in an 8 bit unsigned
648 * integer. In 256 bytes of serialised data, however, there are 257
649 * possible offsets so 16 bit integers must be used. The size of an
650 * offset is always a power of 2.
652 * The offsets are stored at the end of the serialised data of the
653 * array. They are simply concatenated on without any particular
654 * alignment. The size of the offsets is included in the size of the
655 * serialised data for purposes of determining the size of the offsets.
656 * This presents a possibly ambiguity; in certain cases, a particular
657 * value of array could have two different serialised forms.
659 * Imagine an array containing a single string of 253 bytes in length
660 * (so, 254 bytes including the nul terminator). Now the offset must be
661 * written. If an 8 bit offset is written, it will bring the size of
662 * the array's serialised data to 255 -- which means that the use of an
663 * 8 bit offset was valid. If a 16 bit offset is used then the total
664 * size of the array will be 256 -- which means that the use of a 16 bit
665 * offset was valid. Although both of these will be accepted by the
666 * deserialiser, only the smaller of the two is considered to be in
667 * normal form and that is the one that the serialiser must produce.
670 /* bytes may be NULL if (size == 0). */
672 gvs_read_unaligned_le (const guchar *bytes,
677 guchar bytes[GLIB_SIZEOF_SIZE_T];
681 tmpvalue.integer = 0;
683 memcpy (&tmpvalue.bytes, bytes, size);
685 return GSIZE_FROM_LE (tmpvalue.integer);
689 gvs_write_unaligned_le (guchar *bytes,
695 guchar bytes[GLIB_SIZEOF_SIZE_T];
699 tmpvalue.integer = GSIZE_TO_LE (value);
700 memcpy (bytes, &tmpvalue.bytes, size);
704 gvs_get_offset_size (gsize size)
706 if (size > G_MAXUINT32)
709 else if (size > G_MAXUINT16)
712 else if (size > G_MAXUINT8)
722 gvs_calculate_total_size (gsize body_size,
725 if (body_size + 1 * offsets <= G_MAXUINT8)
726 return body_size + 1 * offsets;
728 if (body_size + 2 * offsets <= G_MAXUINT16)
729 return body_size + 2 * offsets;
731 if (body_size + 4 * offsets <= G_MAXUINT32)
732 return body_size + 4 * offsets;
734 return body_size + 8 * offsets;
738 gvs_variable_sized_array_n_children (GVariantSerialised value)
740 gsize offsets_array_size;
747 offset_size = gvs_get_offset_size (value.size);
749 last_end = gvs_read_unaligned_le (value.data + value.size -
750 offset_size, offset_size);
752 if (last_end > value.size)
755 offsets_array_size = value.size - last_end;
757 if (offsets_array_size % offset_size)
760 return offsets_array_size / offset_size;
763 static GVariantSerialised
764 gvs_variable_sized_array_get_child (GVariantSerialised value,
767 GVariantSerialised child = { 0, };
773 child.type_info = g_variant_type_info_element (value.type_info);
774 g_variant_type_info_ref (child.type_info);
776 offset_size = gvs_get_offset_size (value.size);
778 last_end = gvs_read_unaligned_le (value.data + value.size -
779 offset_size, offset_size);
785 start = gvs_read_unaligned_le (value.data + last_end +
786 (offset_size * (index_ - 1)),
789 g_variant_type_info_query (child.type_info, &alignment, NULL);
790 start += (-start) & alignment;
795 end = gvs_read_unaligned_le (value.data + last_end +
796 (offset_size * index_),
799 if (start < end && end <= value.size)
801 child.data = value.data + start;
802 child.size = end - start;
809 gvs_variable_sized_array_unpack_all (GVariantTypeInfo *type_info,
815 GVariantTypeInfo *element;
816 guint element_alignment;
817 const guchar *offsets;
819 gsize offsets_array_size;
827 element = g_variant_type_info_element (type_info);
828 g_variant_type_info_query (element, &element_alignment, NULL);
830 offset_size = gvs_get_offset_size (total_size);
832 if (offset_size > end_size)
835 last_end = gvs_read_unaligned_le (end - offset_size, offset_size);
837 if (last_end > total_size)
840 offsets_array_size = total_size - last_end;
842 if (offsets_array_size > end_size)
845 offsets = end - offsets_array_size;
847 if (offsets_array_size % offset_size)
850 n = offsets_array_size / offset_size;
857 for (i = 0; i < n; i++)
859 GVariantUnpacked unpacked;
863 start = prev_end + ((-prev_end) & element_alignment);
864 end = gvs_read_unaligned_le (offsets, offset_size);
865 offsets += offset_size;
867 if (start < prev_end || end < start)
870 unpacked.type_info = g_variant_type_info_ref (element);
871 unpacked.skip = start - prev_end;
872 unpacked.size = end - start;
874 g_array_append_val (results, unpacked);
883 gvs_variable_sized_array_needed_size (GVariantTypeInfo *type_info,
884 GVariantSerialisedFiller gvs_filler,
885 const gpointer *children,
892 g_variant_type_info_query (type_info, &alignment, NULL);
895 for (i = 0; i < n_children; i++)
897 GVariantSerialised child = { 0, };
899 offset += (-offset) & alignment;
900 gvs_filler (&child, children[i]);
901 offset += child.size;
904 return gvs_calculate_total_size (offset, n_children);
908 gvs_variable_sized_array_serialise (GVariantSerialised value,
909 GVariantSerialisedFiller gvs_filler,
910 const gpointer *children,
919 g_variant_type_info_query (value.type_info, &alignment, NULL);
920 offset_size = gvs_get_offset_size (value.size);
923 offset_ptr = value.data + value.size - offset_size * n_children;
925 for (i = 0; i < n_children; i++)
927 GVariantSerialised child = { 0, };
929 while (offset & alignment)
930 value.data[offset++] = '\0';
932 child.data = value.data + offset;
933 gvs_filler (&child, children[i]);
934 offset += child.size;
936 gvs_write_unaligned_le (offset_ptr, offset, offset_size);
937 offset_ptr += offset_size;
942 gvs_variable_sized_array_write_to_vectors (GVariantVectors *vectors,
943 GVariantTypeInfo *type_info,
945 const gpointer *children,
956 offset_key = g_variant_vectors_reserve_offsets (vectors, n_children, gvs_get_offset_size (size));
957 g_variant_type_info_query (type_info, &alignment, NULL);
960 for (i = 0; i < n_children; i++)
962 if ((-offset) & alignment)
963 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
965 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
967 g_variant_vectors_write_to_offsets (vectors, i, offset, offset_key);
970 g_variant_vectors_commit_offsets (vectors, offset_key);
974 gvs_variable_sized_array_is_normal (GVariantSerialised value)
976 GVariantSerialised child = { 0, };
977 gsize offsets_array_size;
978 guchar *offsets_array;
989 offset_size = gvs_get_offset_size (value.size);
990 last_end = gvs_read_unaligned_le (value.data + value.size -
991 offset_size, offset_size);
993 if (last_end > value.size)
996 offsets_array_size = value.size - last_end;
998 if (offsets_array_size % offset_size)
1001 offsets_array = value.data + value.size - offsets_array_size;
1002 length = offsets_array_size / offset_size;
1007 child.type_info = g_variant_type_info_element (value.type_info);
1008 g_variant_type_info_query (child.type_info, &alignment, NULL);
1011 for (i = 0; i < length; i++)
1015 this_end = gvs_read_unaligned_le (offsets_array + offset_size * i,
1018 if (this_end < offset || this_end > last_end)
1021 while (offset & alignment)
1023 if (!(offset < this_end && value.data[offset] == '\0'))
1028 child.data = value.data + offset;
1029 child.size = this_end - offset;
1031 if (child.size == 0)
1034 if (!g_variant_serialised_is_normal (child))
1040 g_assert (offset == last_end);
1047 * Since tuples can contain a mix of variable- and fixed-sized items,
1048 * they are, in terms of serialisation, a hybrid of variable-sized and
1049 * fixed-sized arrays.
1051 * Offsets are only stored for variable-sized items. Also, since the
1052 * number of items in a tuple is known from its type, we are able to
1053 * know exactly how many offsets to expect in the serialised data (and
1054 * therefore how much space is taken up by the offset array). This
1055 * means that we know where the end of the serialised data for the last
1056 * item is -- we can just subtract the size of the offset array from the
1057 * total size of the tuple. For this reason, the last item in the tuple
1058 * doesn't need an offset stored.
1060 * Tuple offsets are stored in reverse. This design choice allows
1061 * iterator-based deserialisers to be more efficient.
1063 * Most of the "heavy lifting" here is handled by the GVariantTypeInfo
1064 * for the tuple. See the notes in gvarianttypeinfo.h.
1068 gvs_tuple_n_children (GVariantSerialised value)
1070 return g_variant_type_info_n_members (value.type_info);
1073 static GVariantSerialised
1074 gvs_tuple_get_child (GVariantSerialised value,
1077 const GVariantMemberInfo *member_info;
1078 GVariantSerialised child = { 0, };
1082 member_info = g_variant_type_info_member_info (value.type_info, index_);
1083 child.type_info = g_variant_type_info_ref (member_info->type_info);
1084 offset_size = gvs_get_offset_size (value.size);
1086 /* tuples are the only (potentially) fixed-sized containers, so the
1087 * only ones that have to deal with the possibility of having %NULL
1088 * data with a non-zero %size if errors occurred elsewhere.
1090 if G_UNLIKELY (value.data == NULL && value.size != 0)
1092 g_variant_type_info_query (child.type_info, NULL, &child.size);
1094 /* this can only happen in fixed-sized tuples,
1095 * so the child must also be fixed sized.
1097 g_assert (child.size != 0);
1103 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
1105 if (offset_size * (member_info->i + 2) > value.size)
1110 if (offset_size * (member_info->i + 1) > value.size)
1112 /* if the child is fixed size, return its size.
1113 * if child is not fixed-sized, return size = 0.
1115 g_variant_type_info_query (child.type_info, NULL, &child.size);
1121 if (member_info->i + 1)
1122 start = gvs_read_unaligned_le (value.data + value.size -
1123 offset_size * (member_info->i + 1),
1128 start += member_info->a;
1129 start &= member_info->b;
1130 start |= member_info->c;
1132 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_LAST)
1133 end = value.size - offset_size * (member_info->i + 1);
1135 else if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_FIXED)
1139 g_variant_type_info_query (child.type_info, NULL, &fixed_size);
1140 end = start + fixed_size;
1141 child.size = fixed_size;
1144 else /* G_VARIANT_MEMBER_ENDING_OFFSET */
1145 end = gvs_read_unaligned_le (value.data + value.size -
1146 offset_size * (member_info->i + 2),
1149 if (start < end && end <= value.size)
1151 child.data = value.data + start;
1152 child.size = end - start;
1159 gvs_tuple_unpack_all (GVariantTypeInfo *type_info,
1160 const guchar *end_pointer,
1169 n = g_variant_type_info_n_members (type_info);
1171 /* An empty tuple (n = 0) is always encoded as a single byte, which
1172 * means that we should not be attempting to unpack it from multiple
1178 offset_size = gvs_get_offset_size (total_size);
1182 for (i = 0; i < n; i++)
1184 const GVariantMemberInfo *member_info;
1185 GVariantUnpacked unpacked;
1191 member_info = g_variant_type_info_member_info (type_info, i);
1192 g_variant_type_info_query (member_info->type_info, &alignment, &fixed_size);
1194 start = prev_end + ((-prev_end) & alignment);
1196 switch (member_info->ending_type)
1198 case G_VARIANT_MEMBER_ENDING_FIXED:
1199 end = start + fixed_size;
1202 case G_VARIANT_MEMBER_ENDING_LAST:
1206 case G_VARIANT_MEMBER_ENDING_OFFSET:
1207 if (end_size < offset_size)
1210 end_pointer -= offset_size;
1211 total_size -= offset_size;
1212 end_size -= offset_size;
1214 end = gvs_read_unaligned_le (end_pointer, offset_size);
1218 g_assert_not_reached ();
1221 if (start < prev_end || end < start)
1224 unpacked.type_info = g_variant_type_info_ref (member_info->type_info);
1225 unpacked.skip = start - prev_end;
1226 unpacked.size = end - start;
1228 g_array_append_val (results, unpacked);
1233 g_assert (prev_end == total_size);
1239 gvs_tuple_needed_size (GVariantTypeInfo *type_info,
1240 GVariantSerialisedFiller gvs_filler,
1241 const gpointer *children,
1244 const GVariantMemberInfo *member_info = NULL;
1249 g_variant_type_info_query (type_info, NULL, &fixed_size);
1256 for (i = 0; i < n_children; i++)
1260 member_info = g_variant_type_info_member_info (type_info, i);
1261 g_variant_type_info_query (member_info->type_info,
1262 &alignment, &fixed_size);
1263 offset += (-offset) & alignment;
1266 offset += fixed_size;
1269 GVariantSerialised child = { 0, };
1271 gvs_filler (&child, children[i]);
1272 offset += child.size;
1276 return gvs_calculate_total_size (offset, member_info->i + 1);
1280 gvs_tuple_serialise (GVariantSerialised value,
1281 GVariantSerialisedFiller gvs_filler,
1282 const gpointer *children,
1289 offset_size = gvs_get_offset_size (value.size);
1292 for (i = 0; i < n_children; i++)
1294 const GVariantMemberInfo *member_info;
1295 GVariantSerialised child = { 0, };
1298 member_info = g_variant_type_info_member_info (value.type_info, i);
1299 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1301 while (offset & alignment)
1302 value.data[offset++] = '\0';
1304 child.data = value.data + offset;
1305 gvs_filler (&child, children[i]);
1306 offset += child.size;
1308 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
1310 value.size -= offset_size;
1311 gvs_write_unaligned_le (value.data + value.size,
1312 offset, offset_size);
1316 while (offset < value.size)
1317 value.data[offset++] = '\0';
1322 gvs_tuple_write_to_vectors (GVariantVectors *vectors,
1323 GVariantTypeInfo *type_info,
1325 const gpointer *children,
1328 const GVariantMemberInfo *member_info = NULL;
1333 if (n_children == 0)
1335 g_variant_vectors_append_copy (vectors, "", 1);
1339 g_variant_type_info_query (type_info, NULL, &fixed_size);
1346 member_info = g_variant_type_info_member_info (type_info, n_children - 1);
1347 n_offsets = member_info->i + 1;
1351 gsize offset_key = 0;
1353 offset_key = g_variant_vectors_reserve_offsets (vectors, n_offsets, gvs_get_offset_size (size));
1355 for (i = 0; i < n_children; i++)
1359 member_info = g_variant_type_info_member_info (type_info, i);
1360 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1362 if ((-offset) & alignment)
1363 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
1365 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
1367 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
1368 g_variant_vectors_write_to_offsets (vectors, --n_offsets, offset, offset_key);
1371 g_variant_vectors_commit_offsets (vectors, offset_key);
1375 for (i = 0; i < n_children; i++)
1379 member_info = g_variant_type_info_member_info (type_info, i);
1380 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1382 if ((-offset) & alignment)
1383 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
1385 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
1391 for (i = 0; i < n_children; i++)
1395 member_info = g_variant_type_info_member_info (type_info, i);
1396 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1398 if ((-offset) & alignment)
1399 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
1401 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
1404 g_assert (fixed_size - offset < 8);
1405 g_variant_vectors_append_pad (vectors, fixed_size - offset);
1410 gvs_tuple_is_normal (GVariantSerialised value)
1418 /* as per the comment in gvs_tuple_get_child() */
1419 if G_UNLIKELY (value.data == NULL && value.size != 0)
1422 offset_size = gvs_get_offset_size (value.size);
1423 length = g_variant_type_info_n_members (value.type_info);
1424 offset_ptr = value.size;
1427 for (i = 0; i < length; i++)
1429 const GVariantMemberInfo *member_info;
1430 GVariantSerialised child;
1435 member_info = g_variant_type_info_member_info (value.type_info, i);
1436 child.type_info = member_info->type_info;
1438 g_variant_type_info_query (child.type_info, &alignment, &fixed_size);
1440 while (offset & alignment)
1442 if (offset > value.size || value.data[offset] != '\0')
1447 child.data = value.data + offset;
1449 switch (member_info->ending_type)
1451 case G_VARIANT_MEMBER_ENDING_FIXED:
1452 end = offset + fixed_size;
1455 case G_VARIANT_MEMBER_ENDING_LAST:
1459 case G_VARIANT_MEMBER_ENDING_OFFSET:
1460 offset_ptr -= offset_size;
1462 if (offset_ptr < offset)
1465 end = gvs_read_unaligned_le (value.data + offset_ptr, offset_size);
1469 g_assert_not_reached ();
1472 if (end < offset || end > offset_ptr)
1475 child.size = end - offset;
1477 if (child.size == 0)
1480 if (!g_variant_serialised_is_normal (child))
1490 g_variant_type_info_query (value.type_info, &alignment, &fixed_size);
1494 g_assert (fixed_size == value.size);
1495 g_assert (offset_ptr == value.size);
1499 if (value.data[offset++] != '\0')
1504 while (offset & alignment)
1505 if (value.data[offset++] != '\0')
1509 g_assert (offset == value.size);
1513 return offset_ptr == offset;
1518 * Variants are stored by storing the serialised data of the child,
1519 * followed by a '\0' character, followed by the type string of the
1522 * In the case that a value is presented that contains no '\0'
1523 * character, or doesn't have a single well-formed definite type string
1524 * following that character, the variant must be taken as containing the
1529 gvs_variant_n_children (GVariantSerialised value)
1534 static inline GVariantSerialised
1535 gvs_variant_get_child (GVariantSerialised value,
1538 GVariantSerialised child = { 0, };
1540 /* NOTE: not O(1) and impossible for it to be... */
1543 /* find '\0' character */
1544 for (child.size = value.size - 1; child.size; child.size--)
1545 if (value.data[child.size] == '\0')
1548 /* ensure we didn't just hit the start of the string */
1549 if (value.data[child.size] == '\0')
1551 const gchar *type_string = (gchar *) &value.data[child.size + 1];
1552 const gchar *limit = (gchar *) &value.data[value.size];
1555 if (g_variant_type_string_scan (type_string, limit, &end) &&
1558 const GVariantType *type = (GVariantType *) type_string;
1560 if (g_variant_type_is_definite (type))
1564 child.type_info = g_variant_type_info_get (type);
1566 if (child.size != 0)
1567 /* only set to non-%NULL if size > 0 */
1568 child.data = value.data;
1570 g_variant_type_info_query (child.type_info,
1573 if (!fixed_size || fixed_size == child.size)
1576 g_variant_type_info_unref (child.type_info);
1582 child.type_info = g_variant_type_info_get (G_VARIANT_TYPE_UNIT);
1589 static GVariantTypeInfo *
1590 gvs_variant_find_type (const guchar *end_pointer,
1597 for (i = 1; i <= end_size; i++)
1598 if (end_pointer[-i] == '\0')
1600 const gchar *type_string = (gchar *) end_pointer - i + 1;
1601 const gchar *limit = (gchar *) end_pointer;
1604 /* We may have a type string of length 'i'. Check for validity. */
1605 if (g_variant_type_string_scan (type_string, limit, &end) && end == limit)
1607 const GVariantType *type = (GVariantType *) type_string;
1609 if (g_variant_type_is_definite (type))
1611 GVariantTypeInfo *type_info;
1614 type_info = g_variant_type_info_get (type);
1616 g_variant_type_info_query (type_info, NULL, &fixed_size);
1618 if (!fixed_size || fixed_size == total_size - i)
1620 *child_size = total_size - i;
1625 g_variant_type_info_unref (type_info);
1629 /* No sense in trying other lengths if we already failed */
1637 gvs_variant_unpack_all (GVariantTypeInfo *type_info,
1638 const guchar *end_pointer,
1643 GVariantUnpacked unpacked;
1645 if ((unpacked.type_info = gvs_variant_find_type (end_pointer, end_size, total_size, &unpacked.size)))
1649 g_array_append_val (results, unpacked);
1658 gvs_variant_needed_size (GVariantTypeInfo *type_info,
1659 GVariantSerialisedFiller gvs_filler,
1660 const gpointer *children,
1663 GVariantSerialised child = { 0, };
1664 const gchar *type_string;
1666 gvs_filler (&child, children[0]);
1667 type_string = g_variant_type_info_get_type_string (child.type_info);
1669 return child.size + 1 + strlen (type_string);
1673 gvs_variant_serialise (GVariantSerialised value,
1674 GVariantSerialisedFiller gvs_filler,
1675 const gpointer *children,
1678 GVariantSerialised child = { 0, };
1679 const gchar *type_string;
1681 child.data = value.data;
1683 gvs_filler (&child, children[0]);
1684 type_string = g_variant_type_info_get_type_string (child.type_info);
1685 value.data[child.size] = '\0';
1686 memcpy (value.data + child.size + 1, type_string, strlen (type_string));
1690 gvs_variant_write_to_vectors (GVariantVectors *vectors,
1691 GVariantTypeInfo *type_info,
1693 const gpointer *children,
1696 GVariantTypeInfo *child_type_info;
1697 const gchar *type_string;
1699 g_variant_callback_write_to_vectors (vectors, children[0], &child_type_info);
1700 type_string = g_variant_type_info_get_type_string (child_type_info);
1702 g_variant_vectors_append_copy (vectors, "", 1);
1703 g_variant_vectors_append_copy (vectors, type_string, strlen (type_string));
1706 static inline gboolean
1707 gvs_variant_is_normal (GVariantSerialised value)
1709 GVariantSerialised child;
1712 child = gvs_variant_get_child (value, 0);
1714 normal = (child.data != NULL || child.size == 0) &&
1715 g_variant_serialised_is_normal (child);
1717 g_variant_type_info_unref (child.type_info);
1724 /* PART 2: Serialiser API {{{1
1726 * This is the implementation of the API of the serialiser as advertised
1727 * in gvariant-serialiser.h.
1730 /* Dispatch Utilities {{{2
1732 * These macros allow a given function (for example,
1733 * g_variant_serialiser_serialise) to be dispatched to the appropriate
1734 * type-specific function above (fixed/variable-sized maybe,
1735 * fixed/variable-sized array, tuple or variant).
1737 #define DISPATCH_FIXED(type_info, before, after) \
1741 g_variant_type_info_query_element (type_info, NULL, \
1746 before ## fixed_sized ## after \
1750 before ## variable_sized ## after \
1754 #define DISPATCH_CASES(type_info, before, after) \
1755 switch (g_variant_type_info_get_type_char (type_info)) \
1757 case G_VARIANT_TYPE_INFO_CHAR_MAYBE: \
1758 DISPATCH_FIXED (type_info, before, _maybe ## after) \
1760 case G_VARIANT_TYPE_INFO_CHAR_ARRAY: \
1761 DISPATCH_FIXED (type_info, before, _array ## after) \
1763 case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY: \
1764 case G_VARIANT_TYPE_INFO_CHAR_TUPLE: \
1766 before ## tuple ## after \
1769 case G_VARIANT_TYPE_INFO_CHAR_VARIANT: \
1771 before ## variant ## after \
1775 /* Serialiser entry points {{{2
1777 * These are the functions that are called in order for the serialiser
1782 * g_variant_serialised_n_children:
1783 * @serialised: a #GVariantSerialised
1785 * For serialised data that represents a container value (maybes,
1786 * tuples, arrays, variants), determine how many child items are inside
1789 * Returns: the number of children
1792 g_variant_serialised_n_children (GVariantSerialised serialised)
1794 g_variant_serialised_check (serialised);
1796 DISPATCH_CASES (serialised.type_info,
1798 return gvs_/**/,/**/_n_children (serialised);
1801 g_assert_not_reached ();
1805 * g_variant_serialised_get_child:
1806 * @serialised: a #GVariantSerialised
1807 * @index_: the index of the child to fetch
1809 * Extracts a child from a serialised data representing a container
1812 * It is an error to call this function with an index out of bounds.
1814 * If the result .data == %NULL and .size > 0 then there has been an
1815 * error extracting the requested fixed-sized value. This number of
1816 * zero bytes needs to be allocated instead.
1818 * In the case that .data == %NULL and .size == 0 then a zero-sized
1819 * item of a variable-sized type is being returned.
1821 * .data is never non-%NULL if size is 0.
1823 * Returns: a #GVariantSerialised for the child
1826 g_variant_serialised_get_child (GVariantSerialised serialised,
1829 GVariantSerialised child;
1831 g_variant_serialised_check (serialised);
1833 if G_LIKELY (index_ < g_variant_serialised_n_children (serialised))
1835 DISPATCH_CASES (serialised.type_info,
1837 child = gvs_/**/,/**/_get_child (serialised, index_);
1838 g_assert (child.size || child.data == NULL);
1839 g_variant_serialised_check (child);
1843 g_assert_not_reached ();
1846 g_error ("Attempt to access item %"G_GSIZE_FORMAT
1847 " in a container with only %"G_GSIZE_FORMAT" items",
1848 index_, g_variant_serialised_n_children (serialised));
1852 * g_variant_serialiser_serialise:
1853 * @serialised: a #GVariantSerialised, properly set up
1854 * @gvs_filler: the filler function
1855 * @children: an array of child items
1856 * @n_children: the size of @children
1858 * Writes data in serialised form.
1860 * The type_info field of @serialised must be filled in to type info for
1861 * the type that we are serialising.
1863 * The size field of @serialised must be filled in with the value
1864 * returned by a previous call to g_variant_serialiser_needed_size().
1866 * The data field of @serialised must be a pointer to a properly-aligned
1867 * memory region large enough to serialise into (ie: at least as big as
1870 * This function is only resonsible for serialising the top-level
1871 * container. @gvs_filler is called on each child of the container in
1872 * order for all of the data of that child to be filled in.
1875 g_variant_serialiser_serialise (GVariantSerialised serialised,
1876 GVariantSerialisedFiller gvs_filler,
1877 const gpointer *children,
1880 g_variant_serialised_check (serialised);
1882 DISPATCH_CASES (serialised.type_info,
1884 gvs_/**/,/**/_serialise (serialised, gvs_filler,
1885 children, n_children);
1889 g_assert_not_reached ();
1893 * g_variant_serialiser_needed_size:
1894 * @type_info: the type to serialise for
1895 * @gvs_filler: the filler function
1896 * @children: an array of child items
1897 * @n_children: the size of @children
1899 * Determines how much memory would be needed to serialise this value.
1901 * This function is only resonsible for performing calculations for the
1902 * top-level container. @gvs_filler is called on each child of the
1903 * container in order to determine its size.
1906 g_variant_serialiser_needed_size (GVariantTypeInfo *type_info,
1907 GVariantSerialisedFiller gvs_filler,
1908 const gpointer *children,
1911 DISPATCH_CASES (type_info,
1913 return gvs_/**/,/**/_needed_size (type_info, gvs_filler,
1914 children, n_children);
1916 g_assert_not_reached ();
1920 g_variant_serialiser_unpack_all (GVariantTypeInfo *type_info,
1926 DISPATCH_CASES (type_info,
1927 return gvs_/**/,/**/_unpack_all (type_info, end, end_size, total_size, results);
1930 /* We are here because type_info is not a container type */
1935 g_variant_serialiser_write_to_vectors (GVariantVectors *vectors,
1936 GVariantTypeInfo *type_info,
1938 const gpointer *children,
1941 DISPATCH_CASES (type_info,
1942 gvs_/**/,/**/_write_to_vectors (vectors, type_info, size, children, n_children);
1945 g_assert_not_reached ();
1948 /* Byteswapping {{{2 */
1951 * g_variant_serialised_byteswap:
1952 * @value: a #GVariantSerialised
1954 * Byte-swap serialised data. The result of this function is only
1955 * well-defined if the data is in normal form.
1958 g_variant_serialised_byteswap (GVariantSerialised serialised)
1963 g_variant_serialised_check (serialised);
1965 if (!serialised.data)
1968 /* the types we potentially need to byteswap are
1969 * exactly those with alignment requirements.
1971 g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
1975 /* if fixed size and alignment are equal then we are down
1976 * to the base integer type and we should swap it. the
1977 * only exception to this is if we have a tuple with a
1978 * single item, and then swapping it will be OK anyway.
1980 if (alignment + 1 == fixed_size)
1986 guint16 *ptr = (guint16 *) serialised.data;
1988 g_assert_cmpint (serialised.size, ==, 2);
1989 *ptr = GUINT16_SWAP_LE_BE (*ptr);
1995 guint32 *ptr = (guint32 *) serialised.data;
1997 g_assert_cmpint (serialised.size, ==, 4);
1998 *ptr = GUINT32_SWAP_LE_BE (*ptr);
2004 guint64 *ptr = (guint64 *) serialised.data;
2006 g_assert_cmpint (serialised.size, ==, 8);
2007 *ptr = GUINT64_SWAP_LE_BE (*ptr);
2012 g_assert_not_reached ();
2016 /* else, we have a container that potentially contains
2017 * some children that need to be byteswapped.
2023 children = g_variant_serialised_n_children (serialised);
2024 for (i = 0; i < children; i++)
2026 GVariantSerialised child;
2028 child = g_variant_serialised_get_child (serialised, i);
2029 g_variant_serialised_byteswap (child);
2030 g_variant_type_info_unref (child.type_info);
2035 /* Normal form checking {{{2 */
2038 * g_variant_serialised_is_normal:
2039 * @serialised: a #GVariantSerialised
2041 * Determines, recursively if @serialised is in normal form. There is
2042 * precisely one normal form of serialised data for each possible value.
2044 * It is possible that multiple byte sequences form the serialised data
2045 * for a given value if, for example, the padding bytes are filled in
2046 * with something other than zeros, but only one form is the normal
2050 g_variant_serialised_is_normal (GVariantSerialised serialised)
2052 DISPATCH_CASES (serialised.type_info,
2054 return gvs_/**/,/**/_is_normal (serialised);
2058 if (serialised.data == NULL)
2061 /* some hard-coded terminal cases */
2062 switch (g_variant_type_info_get_type_char (serialised.type_info))
2064 case 'b': /* boolean */
2065 return serialised.data[0] < 2;
2067 case 's': /* string */
2068 return g_variant_serialiser_is_string (serialised.data,
2072 return g_variant_serialiser_is_object_path (serialised.data,
2076 return g_variant_serialiser_is_signature (serialised.data,
2080 /* all of the other types are fixed-sized numerical types for
2081 * which all possible values are valid (including various NaN
2082 * representations for floating point values).
2088 /* Validity-checking functions {{{2
2090 * Checks if strings, object paths and signature strings are valid.
2094 * g_variant_serialiser_is_string:
2095 * @data: a possible string
2096 * @size: the size of @data
2098 * Ensures that @data is a valid string with a nul terminator at the end
2099 * and no nul bytes embedded.
2102 g_variant_serialiser_is_string (gconstpointer data,
2105 const gchar *expected_end;
2111 expected_end = ((gchar *) data) + size - 1;
2113 if (*expected_end != '\0')
2116 g_utf8_validate (data, size, &end);
2118 return end == expected_end;
2122 * g_variant_serialiser_is_object_path:
2123 * @data: a possible D-Bus object path
2124 * @size: the size of @data
2126 * Performs the checks for being a valid string.
2128 * Also, ensures that @data is a valid DBus object path, as per the D-Bus
2132 g_variant_serialiser_is_object_path (gconstpointer data,
2135 const gchar *string = data;
2138 if (!g_variant_serialiser_is_string (data, size))
2141 /* The path must begin with an ASCII '/' (integer 47) character */
2142 if (string[0] != '/')
2145 for (i = 1; string[i]; i++)
2146 /* Each element must only contain the ASCII characters
2147 * "[A-Z][a-z][0-9]_"
2149 if (g_ascii_isalnum (string[i]) || string[i] == '_')
2152 /* must consist of elements separated by slash characters. */
2153 else if (string[i] == '/')
2155 /* No element may be the empty string. */
2156 /* Multiple '/' characters cannot occur in sequence. */
2157 if (string[i - 1] == '/')
2164 /* A trailing '/' character is not allowed unless the path is the
2165 * root path (a single '/' character).
2167 if (i > 1 && string[i - 1] == '/')
2174 * g_variant_serialiser_is_signature:
2175 * @data: a possible D-Bus signature
2176 * @size: the size of @data
2178 * Performs the checks for being a valid string.
2180 * Also, ensures that @data is a valid D-Bus type signature, as per the
2181 * D-Bus specification.
2184 g_variant_serialiser_is_signature (gconstpointer data,
2187 const gchar *string = data;
2188 gsize first_invalid;
2190 if (!g_variant_serialiser_is_string (data, size))
2193 /* make sure no non-definite characters appear */
2194 first_invalid = strspn (string, "ybnqiuxthdvasog(){}");
2195 if (string[first_invalid])
2198 /* make sure each type string is well-formed */
2200 if (!g_variant_type_string_scan (string, NULL, &string))
2207 /* vim:set foldmethod=marker: */