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) { g_assert_not_reached ();
868 return FALSE; /* XXX free the array and type infos */ }
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);
881 gvs_variable_sized_array_needed_size (GVariantTypeInfo *type_info,
882 GVariantSerialisedFiller gvs_filler,
883 const gpointer *children,
890 g_variant_type_info_query (type_info, &alignment, NULL);
893 for (i = 0; i < n_children; i++)
895 GVariantSerialised child = { 0, };
897 offset += (-offset) & alignment;
898 gvs_filler (&child, children[i]);
899 offset += child.size;
902 return gvs_calculate_total_size (offset, n_children);
906 gvs_variable_sized_array_serialise (GVariantSerialised value,
907 GVariantSerialisedFiller gvs_filler,
908 const gpointer *children,
917 g_variant_type_info_query (value.type_info, &alignment, NULL);
918 offset_size = gvs_get_offset_size (value.size);
921 offset_ptr = value.data + value.size - offset_size * n_children;
923 for (i = 0; i < n_children; i++)
925 GVariantSerialised child = { 0, };
927 while (offset & alignment)
928 value.data[offset++] = '\0';
930 child.data = value.data + offset;
931 gvs_filler (&child, children[i]);
932 offset += child.size;
934 gvs_write_unaligned_le (offset_ptr, offset, offset_size);
935 offset_ptr += offset_size;
940 gvs_variable_sized_array_write_to_vectors (GVariantVectors *vectors,
941 GVariantTypeInfo *type_info,
943 const gpointer *children,
954 offset_key = g_variant_vectors_reserve_offsets (vectors, n_children, gvs_get_offset_size (size));
955 g_variant_type_info_query (type_info, &alignment, NULL);
958 for (i = 0; i < n_children; i++)
960 if ((-offset) & alignment)
961 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
963 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
965 g_variant_vectors_write_to_offsets (vectors, i, offset, offset_key);
968 g_variant_vectors_commit_offsets (vectors, offset_key);
972 gvs_variable_sized_array_is_normal (GVariantSerialised value)
974 GVariantSerialised child = { 0, };
975 gsize offsets_array_size;
976 guchar *offsets_array;
987 offset_size = gvs_get_offset_size (value.size);
988 last_end = gvs_read_unaligned_le (value.data + value.size -
989 offset_size, offset_size);
991 if (last_end > value.size)
994 offsets_array_size = value.size - last_end;
996 if (offsets_array_size % offset_size)
999 offsets_array = value.data + value.size - offsets_array_size;
1000 length = offsets_array_size / offset_size;
1005 child.type_info = g_variant_type_info_element (value.type_info);
1006 g_variant_type_info_query (child.type_info, &alignment, NULL);
1009 for (i = 0; i < length; i++)
1013 this_end = gvs_read_unaligned_le (offsets_array + offset_size * i,
1016 if (this_end < offset || this_end > last_end)
1019 while (offset & alignment)
1021 if (!(offset < this_end && value.data[offset] == '\0'))
1026 child.data = value.data + offset;
1027 child.size = this_end - offset;
1029 if (child.size == 0)
1032 if (!g_variant_serialised_is_normal (child))
1038 g_assert (offset == last_end);
1045 * Since tuples can contain a mix of variable- and fixed-sized items,
1046 * they are, in terms of serialisation, a hybrid of variable-sized and
1047 * fixed-sized arrays.
1049 * Offsets are only stored for variable-sized items. Also, since the
1050 * number of items in a tuple is known from its type, we are able to
1051 * know exactly how many offsets to expect in the serialised data (and
1052 * therefore how much space is taken up by the offset array). This
1053 * means that we know where the end of the serialised data for the last
1054 * item is -- we can just subtract the size of the offset array from the
1055 * total size of the tuple. For this reason, the last item in the tuple
1056 * doesn't need an offset stored.
1058 * Tuple offsets are stored in reverse. This design choice allows
1059 * iterator-based deserialisers to be more efficient.
1061 * Most of the "heavy lifting" here is handled by the GVariantTypeInfo
1062 * for the tuple. See the notes in gvarianttypeinfo.h.
1066 gvs_tuple_n_children (GVariantSerialised value)
1068 return g_variant_type_info_n_members (value.type_info);
1071 static GVariantSerialised
1072 gvs_tuple_get_child (GVariantSerialised value,
1075 const GVariantMemberInfo *member_info;
1076 GVariantSerialised child = { 0, };
1080 member_info = g_variant_type_info_member_info (value.type_info, index_);
1081 child.type_info = g_variant_type_info_ref (member_info->type_info);
1082 offset_size = gvs_get_offset_size (value.size);
1084 /* tuples are the only (potentially) fixed-sized containers, so the
1085 * only ones that have to deal with the possibility of having %NULL
1086 * data with a non-zero %size if errors occurred elsewhere.
1088 if G_UNLIKELY (value.data == NULL && value.size != 0)
1090 g_variant_type_info_query (child.type_info, NULL, &child.size);
1092 /* this can only happen in fixed-sized tuples,
1093 * so the child must also be fixed sized.
1095 g_assert (child.size != 0);
1101 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
1103 if (offset_size * (member_info->i + 2) > value.size)
1108 if (offset_size * (member_info->i + 1) > value.size)
1110 /* if the child is fixed size, return its size.
1111 * if child is not fixed-sized, return size = 0.
1113 g_variant_type_info_query (child.type_info, NULL, &child.size);
1119 if (member_info->i + 1)
1120 start = gvs_read_unaligned_le (value.data + value.size -
1121 offset_size * (member_info->i + 1),
1126 start += member_info->a;
1127 start &= member_info->b;
1128 start |= member_info->c;
1130 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_LAST)
1131 end = value.size - offset_size * (member_info->i + 1);
1133 else if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_FIXED)
1137 g_variant_type_info_query (child.type_info, NULL, &fixed_size);
1138 end = start + fixed_size;
1139 child.size = fixed_size;
1142 else /* G_VARIANT_MEMBER_ENDING_OFFSET */
1143 end = gvs_read_unaligned_le (value.data + value.size -
1144 offset_size * (member_info->i + 2),
1147 if (start < end && end <= value.size)
1149 child.data = value.data + start;
1150 child.size = end - start;
1157 gvs_tuple_unpack_all (GVariantTypeInfo *type_info,
1163 g_assert_not_reached (); /* FIXME */
1168 gvs_tuple_needed_size (GVariantTypeInfo *type_info,
1169 GVariantSerialisedFiller gvs_filler,
1170 const gpointer *children,
1173 const GVariantMemberInfo *member_info = NULL;
1178 g_variant_type_info_query (type_info, NULL, &fixed_size);
1185 for (i = 0; i < n_children; i++)
1189 member_info = g_variant_type_info_member_info (type_info, i);
1190 g_variant_type_info_query (member_info->type_info,
1191 &alignment, &fixed_size);
1192 offset += (-offset) & alignment;
1195 offset += fixed_size;
1198 GVariantSerialised child = { 0, };
1200 gvs_filler (&child, children[i]);
1201 offset += child.size;
1205 return gvs_calculate_total_size (offset, member_info->i + 1);
1209 gvs_tuple_serialise (GVariantSerialised value,
1210 GVariantSerialisedFiller gvs_filler,
1211 const gpointer *children,
1218 offset_size = gvs_get_offset_size (value.size);
1221 for (i = 0; i < n_children; i++)
1223 const GVariantMemberInfo *member_info;
1224 GVariantSerialised child = { 0, };
1227 member_info = g_variant_type_info_member_info (value.type_info, i);
1228 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1230 while (offset & alignment)
1231 value.data[offset++] = '\0';
1233 child.data = value.data + offset;
1234 gvs_filler (&child, children[i]);
1235 offset += child.size;
1237 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
1239 value.size -= offset_size;
1240 gvs_write_unaligned_le (value.data + value.size,
1241 offset, offset_size);
1245 while (offset < value.size)
1246 value.data[offset++] = '\0';
1251 gvs_tuple_write_to_vectors (GVariantVectors *vectors,
1252 GVariantTypeInfo *type_info,
1254 const gpointer *children,
1257 const GVariantMemberInfo *member_info = NULL;
1262 if (n_children == 0)
1264 g_variant_vectors_append_copy (vectors, "", 1);
1268 g_variant_type_info_query (type_info, NULL, &fixed_size);
1275 member_info = g_variant_type_info_member_info (type_info, n_children - 1);
1276 n_offsets = member_info->i + 1;
1280 gsize offset_key = 0;
1282 offset_key = g_variant_vectors_reserve_offsets (vectors, n_offsets, gvs_get_offset_size (size));
1284 for (i = 0; i < n_children; i++)
1288 member_info = g_variant_type_info_member_info (type_info, i);
1289 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1291 if ((-offset) & alignment)
1292 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
1294 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
1296 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
1297 g_variant_vectors_write_to_offsets (vectors, --n_offsets, offset, offset_key);
1300 g_variant_vectors_commit_offsets (vectors, offset_key);
1304 for (i = 0; i < n_children; i++)
1308 member_info = g_variant_type_info_member_info (type_info, i);
1309 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1311 if ((-offset) & alignment)
1312 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
1314 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
1320 for (i = 0; i < n_children; i++)
1324 member_info = g_variant_type_info_member_info (type_info, i);
1325 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1327 if ((-offset) & alignment)
1328 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
1330 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
1333 g_assert (fixed_size - offset < 8);
1334 g_variant_vectors_append_pad (vectors, fixed_size - offset);
1339 gvs_tuple_is_normal (GVariantSerialised value)
1347 /* as per the comment in gvs_tuple_get_child() */
1348 if G_UNLIKELY (value.data == NULL && value.size != 0)
1351 offset_size = gvs_get_offset_size (value.size);
1352 length = g_variant_type_info_n_members (value.type_info);
1353 offset_ptr = value.size;
1356 for (i = 0; i < length; i++)
1358 const GVariantMemberInfo *member_info;
1359 GVariantSerialised child;
1364 member_info = g_variant_type_info_member_info (value.type_info, i);
1365 child.type_info = member_info->type_info;
1367 g_variant_type_info_query (child.type_info, &alignment, &fixed_size);
1369 while (offset & alignment)
1371 if (offset > value.size || value.data[offset] != '\0')
1376 child.data = value.data + offset;
1378 switch (member_info->ending_type)
1380 case G_VARIANT_MEMBER_ENDING_FIXED:
1381 end = offset + fixed_size;
1384 case G_VARIANT_MEMBER_ENDING_LAST:
1388 case G_VARIANT_MEMBER_ENDING_OFFSET:
1389 offset_ptr -= offset_size;
1391 if (offset_ptr < offset)
1394 end = gvs_read_unaligned_le (value.data + offset_ptr, offset_size);
1398 g_assert_not_reached ();
1401 if (end < offset || end > offset_ptr)
1404 child.size = end - offset;
1406 if (child.size == 0)
1409 if (!g_variant_serialised_is_normal (child))
1419 g_variant_type_info_query (value.type_info, &alignment, &fixed_size);
1423 g_assert (fixed_size == value.size);
1424 g_assert (offset_ptr == value.size);
1428 if (value.data[offset++] != '\0')
1433 while (offset & alignment)
1434 if (value.data[offset++] != '\0')
1438 g_assert (offset == value.size);
1442 return offset_ptr == offset;
1447 * Variants are stored by storing the serialised data of the child,
1448 * followed by a '\0' character, followed by the type string of the
1451 * In the case that a value is presented that contains no '\0'
1452 * character, or doesn't have a single well-formed definite type string
1453 * following that character, the variant must be taken as containing the
1458 gvs_variant_n_children (GVariantSerialised value)
1463 static inline GVariantSerialised
1464 gvs_variant_get_child (GVariantSerialised value,
1467 GVariantSerialised child = { 0, };
1469 /* NOTE: not O(1) and impossible for it to be... */
1472 /* find '\0' character */
1473 for (child.size = value.size - 1; child.size; child.size--)
1474 if (value.data[child.size] == '\0')
1477 /* ensure we didn't just hit the start of the string */
1478 if (value.data[child.size] == '\0')
1480 const gchar *type_string = (gchar *) &value.data[child.size + 1];
1481 const gchar *limit = (gchar *) &value.data[value.size];
1484 if (g_variant_type_string_scan (type_string, limit, &end) &&
1487 const GVariantType *type = (GVariantType *) type_string;
1489 if (g_variant_type_is_definite (type))
1493 child.type_info = g_variant_type_info_get (type);
1495 if (child.size != 0)
1496 /* only set to non-%NULL if size > 0 */
1497 child.data = value.data;
1499 g_variant_type_info_query (child.type_info,
1502 if (!fixed_size || fixed_size == child.size)
1505 g_variant_type_info_unref (child.type_info);
1511 child.type_info = g_variant_type_info_get (G_VARIANT_TYPE_UNIT);
1519 gvs_variant_unpack_all (GVariantTypeInfo *type_info,
1525 g_assert_not_reached (); /* FIXME */
1530 gvs_variant_needed_size (GVariantTypeInfo *type_info,
1531 GVariantSerialisedFiller gvs_filler,
1532 const gpointer *children,
1535 GVariantSerialised child = { 0, };
1536 const gchar *type_string;
1538 gvs_filler (&child, children[0]);
1539 type_string = g_variant_type_info_get_type_string (child.type_info);
1541 return child.size + 1 + strlen (type_string);
1545 gvs_variant_serialise (GVariantSerialised value,
1546 GVariantSerialisedFiller gvs_filler,
1547 const gpointer *children,
1550 GVariantSerialised child = { 0, };
1551 const gchar *type_string;
1553 child.data = value.data;
1555 gvs_filler (&child, children[0]);
1556 type_string = g_variant_type_info_get_type_string (child.type_info);
1557 value.data[child.size] = '\0';
1558 memcpy (value.data + child.size + 1, type_string, strlen (type_string));
1562 gvs_variant_write_to_vectors (GVariantVectors *vectors,
1563 GVariantTypeInfo *type_info,
1565 const gpointer *children,
1568 GVariantTypeInfo *child_type_info;
1569 const gchar *type_string;
1571 g_variant_callback_write_to_vectors (vectors, children[0], &child_type_info);
1572 type_string = g_variant_type_info_get_type_string (child_type_info);
1574 g_variant_vectors_append_copy (vectors, "", 1);
1575 g_variant_vectors_append_copy (vectors, type_string, strlen (type_string));
1578 static inline gboolean
1579 gvs_variant_is_normal (GVariantSerialised value)
1581 GVariantSerialised child;
1584 child = gvs_variant_get_child (value, 0);
1586 normal = (child.data != NULL || child.size == 0) &&
1587 g_variant_serialised_is_normal (child);
1589 g_variant_type_info_unref (child.type_info);
1596 /* PART 2: Serialiser API {{{1
1598 * This is the implementation of the API of the serialiser as advertised
1599 * in gvariant-serialiser.h.
1602 /* Dispatch Utilities {{{2
1604 * These macros allow a given function (for example,
1605 * g_variant_serialiser_serialise) to be dispatched to the appropriate
1606 * type-specific function above (fixed/variable-sized maybe,
1607 * fixed/variable-sized array, tuple or variant).
1609 #define DISPATCH_FIXED(type_info, before, after) \
1613 g_variant_type_info_query_element (type_info, NULL, \
1618 before ## fixed_sized ## after \
1622 before ## variable_sized ## after \
1626 #define DISPATCH_CASES(type_info, before, after) \
1627 switch (g_variant_type_info_get_type_char (type_info)) \
1629 case G_VARIANT_TYPE_INFO_CHAR_MAYBE: \
1630 DISPATCH_FIXED (type_info, before, _maybe ## after) \
1632 case G_VARIANT_TYPE_INFO_CHAR_ARRAY: \
1633 DISPATCH_FIXED (type_info, before, _array ## after) \
1635 case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY: \
1636 case G_VARIANT_TYPE_INFO_CHAR_TUPLE: \
1638 before ## tuple ## after \
1641 case G_VARIANT_TYPE_INFO_CHAR_VARIANT: \
1643 before ## variant ## after \
1647 /* Serialiser entry points {{{2
1649 * These are the functions that are called in order for the serialiser
1654 * g_variant_serialised_n_children:
1655 * @serialised: a #GVariantSerialised
1657 * For serialised data that represents a container value (maybes,
1658 * tuples, arrays, variants), determine how many child items are inside
1661 * Returns: the number of children
1664 g_variant_serialised_n_children (GVariantSerialised serialised)
1666 g_variant_serialised_check (serialised);
1668 DISPATCH_CASES (serialised.type_info,
1670 return gvs_/**/,/**/_n_children (serialised);
1673 g_assert_not_reached ();
1677 * g_variant_serialised_get_child:
1678 * @serialised: a #GVariantSerialised
1679 * @index_: the index of the child to fetch
1681 * Extracts a child from a serialised data representing a container
1684 * It is an error to call this function with an index out of bounds.
1686 * If the result .data == %NULL and .size > 0 then there has been an
1687 * error extracting the requested fixed-sized value. This number of
1688 * zero bytes needs to be allocated instead.
1690 * In the case that .data == %NULL and .size == 0 then a zero-sized
1691 * item of a variable-sized type is being returned.
1693 * .data is never non-%NULL if size is 0.
1695 * Returns: a #GVariantSerialised for the child
1698 g_variant_serialised_get_child (GVariantSerialised serialised,
1701 GVariantSerialised child;
1703 g_variant_serialised_check (serialised);
1705 if G_LIKELY (index_ < g_variant_serialised_n_children (serialised))
1707 DISPATCH_CASES (serialised.type_info,
1709 child = gvs_/**/,/**/_get_child (serialised, index_);
1710 g_assert (child.size || child.data == NULL);
1711 g_variant_serialised_check (child);
1715 g_assert_not_reached ();
1718 g_error ("Attempt to access item %"G_GSIZE_FORMAT
1719 " in a container with only %"G_GSIZE_FORMAT" items",
1720 index_, g_variant_serialised_n_children (serialised));
1724 * g_variant_serialiser_serialise:
1725 * @serialised: a #GVariantSerialised, properly set up
1726 * @gvs_filler: the filler function
1727 * @children: an array of child items
1728 * @n_children: the size of @children
1730 * Writes data in serialised form.
1732 * The type_info field of @serialised must be filled in to type info for
1733 * the type that we are serialising.
1735 * The size field of @serialised must be filled in with the value
1736 * returned by a previous call to g_variant_serialiser_needed_size().
1738 * The data field of @serialised must be a pointer to a properly-aligned
1739 * memory region large enough to serialise into (ie: at least as big as
1742 * This function is only resonsible for serialising the top-level
1743 * container. @gvs_filler is called on each child of the container in
1744 * order for all of the data of that child to be filled in.
1747 g_variant_serialiser_serialise (GVariantSerialised serialised,
1748 GVariantSerialisedFiller gvs_filler,
1749 const gpointer *children,
1752 g_variant_serialised_check (serialised);
1754 DISPATCH_CASES (serialised.type_info,
1756 gvs_/**/,/**/_serialise (serialised, gvs_filler,
1757 children, n_children);
1761 g_assert_not_reached ();
1765 * g_variant_serialiser_needed_size:
1766 * @type_info: the type to serialise for
1767 * @gvs_filler: the filler function
1768 * @children: an array of child items
1769 * @n_children: the size of @children
1771 * Determines how much memory would be needed to serialise this value.
1773 * This function is only resonsible for performing calculations for the
1774 * top-level container. @gvs_filler is called on each child of the
1775 * container in order to determine its size.
1778 g_variant_serialiser_needed_size (GVariantTypeInfo *type_info,
1779 GVariantSerialisedFiller gvs_filler,
1780 const gpointer *children,
1783 DISPATCH_CASES (type_info,
1785 return gvs_/**/,/**/_needed_size (type_info, gvs_filler,
1786 children, n_children);
1788 g_assert_not_reached ();
1792 g_variant_serialiser_unpack_all (GVariantTypeInfo *type_info,
1798 DISPATCH_CASES (type_info,
1799 return gvs_/**/,/**/_unpack_all (type_info, end, end_size, total_size, results);
1802 /* We are here because type_info is not a container type */
1807 g_variant_serialiser_write_to_vectors (GVariantVectors *vectors,
1808 GVariantTypeInfo *type_info,
1810 const gpointer *children,
1813 DISPATCH_CASES (type_info,
1814 gvs_/**/,/**/_write_to_vectors (vectors, type_info, size, children, n_children);
1817 g_assert_not_reached ();
1820 /* Byteswapping {{{2 */
1823 * g_variant_serialised_byteswap:
1824 * @value: a #GVariantSerialised
1826 * Byte-swap serialised data. The result of this function is only
1827 * well-defined if the data is in normal form.
1830 g_variant_serialised_byteswap (GVariantSerialised serialised)
1835 g_variant_serialised_check (serialised);
1837 if (!serialised.data)
1840 /* the types we potentially need to byteswap are
1841 * exactly those with alignment requirements.
1843 g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
1847 /* if fixed size and alignment are equal then we are down
1848 * to the base integer type and we should swap it. the
1849 * only exception to this is if we have a tuple with a
1850 * single item, and then swapping it will be OK anyway.
1852 if (alignment + 1 == fixed_size)
1858 guint16 *ptr = (guint16 *) serialised.data;
1860 g_assert_cmpint (serialised.size, ==, 2);
1861 *ptr = GUINT16_SWAP_LE_BE (*ptr);
1867 guint32 *ptr = (guint32 *) serialised.data;
1869 g_assert_cmpint (serialised.size, ==, 4);
1870 *ptr = GUINT32_SWAP_LE_BE (*ptr);
1876 guint64 *ptr = (guint64 *) serialised.data;
1878 g_assert_cmpint (serialised.size, ==, 8);
1879 *ptr = GUINT64_SWAP_LE_BE (*ptr);
1884 g_assert_not_reached ();
1888 /* else, we have a container that potentially contains
1889 * some children that need to be byteswapped.
1895 children = g_variant_serialised_n_children (serialised);
1896 for (i = 0; i < children; i++)
1898 GVariantSerialised child;
1900 child = g_variant_serialised_get_child (serialised, i);
1901 g_variant_serialised_byteswap (child);
1902 g_variant_type_info_unref (child.type_info);
1907 /* Normal form checking {{{2 */
1910 * g_variant_serialised_is_normal:
1911 * @serialised: a #GVariantSerialised
1913 * Determines, recursively if @serialised is in normal form. There is
1914 * precisely one normal form of serialised data for each possible value.
1916 * It is possible that multiple byte sequences form the serialised data
1917 * for a given value if, for example, the padding bytes are filled in
1918 * with something other than zeros, but only one form is the normal
1922 g_variant_serialised_is_normal (GVariantSerialised serialised)
1924 DISPATCH_CASES (serialised.type_info,
1926 return gvs_/**/,/**/_is_normal (serialised);
1930 if (serialised.data == NULL)
1933 /* some hard-coded terminal cases */
1934 switch (g_variant_type_info_get_type_char (serialised.type_info))
1936 case 'b': /* boolean */
1937 return serialised.data[0] < 2;
1939 case 's': /* string */
1940 return g_variant_serialiser_is_string (serialised.data,
1944 return g_variant_serialiser_is_object_path (serialised.data,
1948 return g_variant_serialiser_is_signature (serialised.data,
1952 /* all of the other types are fixed-sized numerical types for
1953 * which all possible values are valid (including various NaN
1954 * representations for floating point values).
1960 /* Validity-checking functions {{{2
1962 * Checks if strings, object paths and signature strings are valid.
1966 * g_variant_serialiser_is_string:
1967 * @data: a possible string
1968 * @size: the size of @data
1970 * Ensures that @data is a valid string with a nul terminator at the end
1971 * and no nul bytes embedded.
1974 g_variant_serialiser_is_string (gconstpointer data,
1977 const gchar *expected_end;
1983 expected_end = ((gchar *) data) + size - 1;
1985 if (*expected_end != '\0')
1988 g_utf8_validate (data, size, &end);
1990 return end == expected_end;
1994 * g_variant_serialiser_is_object_path:
1995 * @data: a possible D-Bus object path
1996 * @size: the size of @data
1998 * Performs the checks for being a valid string.
2000 * Also, ensures that @data is a valid DBus object path, as per the D-Bus
2004 g_variant_serialiser_is_object_path (gconstpointer data,
2007 const gchar *string = data;
2010 if (!g_variant_serialiser_is_string (data, size))
2013 /* The path must begin with an ASCII '/' (integer 47) character */
2014 if (string[0] != '/')
2017 for (i = 1; string[i]; i++)
2018 /* Each element must only contain the ASCII characters
2019 * "[A-Z][a-z][0-9]_"
2021 if (g_ascii_isalnum (string[i]) || string[i] == '_')
2024 /* must consist of elements separated by slash characters. */
2025 else if (string[i] == '/')
2027 /* No element may be the empty string. */
2028 /* Multiple '/' characters cannot occur in sequence. */
2029 if (string[i - 1] == '/')
2036 /* A trailing '/' character is not allowed unless the path is the
2037 * root path (a single '/' character).
2039 if (i > 1 && string[i - 1] == '/')
2046 * g_variant_serialiser_is_signature:
2047 * @data: a possible D-Bus signature
2048 * @size: the size of @data
2050 * Performs the checks for being a valid string.
2052 * Also, ensures that @data is a valid D-Bus type signature, as per the
2053 * D-Bus specification.
2056 g_variant_serialiser_is_signature (gconstpointer data,
2059 const gchar *string = data;
2060 gsize first_invalid;
2062 if (!g_variant_serialiser_is_string (data, size))
2065 /* make sure no non-definite characters appear */
2066 first_invalid = strspn (string, "ybnqiuxthdvasog(){}");
2067 if (string[first_invalid])
2070 /* make sure each type string is well-formed */
2072 if (!g_variant_type_string_scan (string, NULL, &string))
2079 /* vim:set foldmethod=marker: */