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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * 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>
36 * After this prologue section, this file has roughly 2 parts.
38 * The first part is split up into sections according to various
39 * container types. Maybe, Array, Tuple, Variant. The Maybe and Array
40 * sections are subdivided for element types being fixed or
41 * variable-sized types.
43 * Each section documents the format of that particular type of
44 * container and implements 5 functions for dealing with it:
47 * - determines (according to serialised data) how many child values
48 * are inside a particular container value.
51 * - gets the type of and the serialised data corresponding to a
52 * given child value within the container value.
55 * - determines how much space would be required to serialise a
56 * container of this type, containing the given children so that
57 * buffers can be preallocated before serialising.
60 * - write the serialised data for a container of this type,
61 * containing the given children, to a buffer.
64 * - check the given data to ensure that it is in normal form. For a
65 * given set of child values, there is exactly one normal form for
66 * the serialised data of a container. Other forms are possible
67 * while maintaining the same children (for example, by inserting
68 * something other than zero bytes as padding) but only one form is
71 * The second part contains the main entry point for each of the above 5
72 * functions and logic to dispatch it to the handler for the appropriate
73 * container type code.
75 * The second part also contains a routine to byteswap serialised
76 * values. This code makes use of the n_children() and get_child()
77 * functions above to do its work so no extra support is needed on a
78 * per-container-type basis.
80 * There is also additional code for checking for normal form. All
81 * numeric types are always in normal form since the full range of
82 * values is permitted (eg: 0 to 255 is a valid byte). Special checks
83 * need to be performed for booleans (only 0 or 1 allowed), strings
84 * (properly nul-terminated) and object paths and signature strings
85 * (meeting the DBus specification requirements).
90 * @type_info: the #GVariantTypeInfo of this value
91 * @data: the serialised data of this value, or %NULL
92 * @size: the size of this value
94 * A structure representing a GVariant in serialised form. This
95 * structure is used with #GVariantSerialisedFiller functions and as the
96 * primary interface to the serialiser. See #GVariantSerialisedFiller
97 * for a description of its use there.
99 * When used with the serialiser API functions, the following invariants
100 * apply to all #GVariantTypeSerialised structures passed to and
101 * returned from the serialiser.
103 * @type_info must be non-%NULL.
105 * @data must be properly aligned for the type described by @type_info.
107 * If @type_info describes a fixed-sized type then @size must always be
108 * equal to the fixed size of that type.
110 * For fixed-sized types (and only fixed-sized types), @data may be
111 * %NULL even if @size is non-zero. This happens when a framing error
112 * occurs while attempting to extract a fixed-sized value out of a
113 * variable-sized container. There is no data to return for the
114 * fixed-sized type, yet @size must be non-zero. The effect of this
115 * combination should be as if @data were a pointer to an
116 * appropriately-sized zero-filled region.
120 * g_variant_serialised_check:
121 * @serialised: a #GVariantSerialised struct
123 * Checks @serialised for validity according to the invariants described
127 g_variant_serialised_check (GVariantSerialised serialised)
132 g_assert (serialised.type_info != NULL);
133 g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
136 g_assert_cmpint (serialised.size, ==, fixed_size);
138 g_assert (serialised.size == 0 || serialised.data != NULL);
140 g_assert_cmpint (alignment & (gsize) serialised.data, ==, 0);
144 * GVariantSerialisedFiller:
145 * @serialised: a #GVariantSerialised instance to fill
146 * @data: data from the children array
148 * This function is called back from g_variant_serialiser_needed_size()
149 * and g_variant_serialiser_serialise(). It fills in missing details
150 * from a partially-complete #GVariantSerialised.
152 * The @data parameter passed back to the function is one of the items
153 * that was passed to the serialiser in the @children array. It
154 * represents a single child item of the container that is being
155 * serialised. The information filled in to @serialised is the
156 * information for this child.
158 * If the @type_info field of @serialised is %NULL then the callback
159 * function must set it to the type information corresponding to the
160 * type of the child. No reference should be added. If it is non-%NULL
161 * then the callback should assert that it is equal to the actual type
164 * If the @size field is zero then the callback must fill it in with the
165 * required amount of space to store the serialised form of the child.
166 * If it is non-zero then the callback should assert that it is equal to
167 * the needed size of the child.
169 * If @data is non-%NULL then it points to a space that is properly
170 * aligned for and large enough to store the serialised data of the
171 * child. The callback must store the serialised form of the child at
174 * If the child value is another container then the callback will likely
175 * recurse back into the serialiser by calling
176 * g_variant_serialiser_needed_size() to determine @size and
177 * g_variant_serialiser_serialise() to write to @data.
180 /* PART 1: Container types {{{1
182 * This section contains the serialiser implementation functions for
183 * each container type.
188 * Maybe types are handled depending on if the element type of the maybe
189 * type is a fixed-sized or variable-sized type. Although all maybe
190 * types themselves are variable-sized types, herein, a maybe value with
191 * a fixed-sized element type is called a "fixed-sized maybe" for
192 * convenience and a maybe value with a variable-sized element type is
193 * called a "variable-sized maybe".
196 /* Fixed-sized Maybe {{{3
198 * The size of a maybe value with a fixed-sized element type is either 0
199 * or equal to the fixed size of its element type. The case where the
200 * size of the maybe value is zero corresponds to the "Nothing" case and
201 * the case where the size of the maybe value is equal to the fixed size
202 * of the element type corresponds to the "Just" case; in that case, the
203 * serialised data of the child value forms the entire serialised data
204 * of the maybe value.
206 * In the event that a fixed-sized maybe value is presented with a size
207 * that is not equal to the fixed size of the element type then the
208 * value must be taken to be "Nothing".
212 gvs_fixed_sized_maybe_n_children (GVariantSerialised value)
214 gsize element_fixed_size;
216 g_variant_type_info_query_element (value.type_info, NULL,
217 &element_fixed_size);
219 return (element_fixed_size == value.size) ? 1 : 0;
222 static GVariantSerialised
223 gvs_fixed_sized_maybe_get_child (GVariantSerialised value,
226 /* the child has the same bounds as the
227 * container, so just update the type.
229 value.type_info = g_variant_type_info_element (value.type_info);
230 g_variant_type_info_ref (value.type_info);
236 gvs_fixed_sized_maybe_needed_size (GVariantTypeInfo *type_info,
237 GVariantSerialisedFiller gvs_filler,
238 const gpointer *children,
243 gsize element_fixed_size;
245 g_variant_type_info_query_element (type_info, NULL,
246 &element_fixed_size);
248 return element_fixed_size;
255 gvs_fixed_sized_maybe_serialise (GVariantSerialised value,
256 GVariantSerialisedFiller gvs_filler,
257 const gpointer *children,
262 GVariantSerialised child = { NULL, value.data, value.size };
264 gvs_filler (&child, children[0]);
269 gvs_fixed_sized_maybe_is_normal (GVariantSerialised value)
273 gsize element_fixed_size;
275 g_variant_type_info_query_element (value.type_info,
276 NULL, &element_fixed_size);
278 if (value.size != element_fixed_size)
281 /* proper element size: "Just". recurse to the child. */
282 value.type_info = g_variant_type_info_element (value.type_info);
284 return g_variant_serialised_is_normal (value);
287 /* size of 0: "Nothing" */
291 /* Variable-sized Maybe
293 * The size of a maybe value with a variable-sized element type is
294 * either 0 or strictly greater than 0. The case where the size of the
295 * maybe value is zero corresponds to the "Nothing" case and the case
296 * where the size of the maybe value is greater than zero corresponds to
297 * the "Just" case; in that case, the serialised data of the child value
298 * forms the first part of the serialised data of the maybe value and is
299 * followed by a single zero byte. This zero byte is always appended,
300 * regardless of any zero bytes that may already be at the end of the
301 * serialised ata of the child value.
305 gvs_variable_sized_maybe_n_children (GVariantSerialised value)
307 return (value.size > 0) ? 1 : 0;
310 static GVariantSerialised
311 gvs_variable_sized_maybe_get_child (GVariantSerialised value,
314 /* remove the padding byte and update the type. */
315 value.type_info = g_variant_type_info_element (value.type_info);
316 g_variant_type_info_ref (value.type_info);
319 /* if it's zero-sized then it may as well be NULL */
327 gvs_variable_sized_maybe_needed_size (GVariantTypeInfo *type_info,
328 GVariantSerialisedFiller gvs_filler,
329 const gpointer *children,
334 GVariantSerialised child = { };
336 gvs_filler (&child, children[0]);
338 return child.size + 1;
345 gvs_variable_sized_maybe_serialise (GVariantSerialised value,
346 GVariantSerialisedFiller gvs_filler,
347 const gpointer *children,
352 GVariantSerialised child = { NULL, value.data, value.size - 1 };
354 /* write the data for the child. */
355 gvs_filler (&child, children[0]);
356 value.data[child.size] = '\0';
361 gvs_variable_sized_maybe_is_normal (GVariantSerialised value)
366 if (value.data[value.size - 1] != '\0')
369 value.type_info = g_variant_type_info_element (value.type_info);
372 return g_variant_serialised_is_normal (value);
377 * Just as with maybe types, array types are handled depending on if the
378 * element type of the array type is a fixed-sized or variable-sized
379 * type. Similar to maybe types, for convenience, an array value with a
380 * fixed-sized element type is called a "fixed-sized array" and an array
381 * value with a variable-sized element type is called a "variable sized
385 /* Fixed-sized Array {{{3
387 * For fixed sized arrays, the serialised data is simply a concatenation
388 * of the serialised data of each element, in order. Since fixed-sized
389 * values always have a fixed size that is a multiple of their alignment
390 * requirement no extra padding is required.
392 * In the event that a fixed-sized array is presented with a size that
393 * is not an integer multiple of the element size then the value of the
394 * array must be taken as being empty.
398 gvs_fixed_sized_array_n_children (GVariantSerialised value)
400 gsize element_fixed_size;
402 g_variant_type_info_query_element (value.type_info, NULL,
403 &element_fixed_size);
405 if (value.size % element_fixed_size == 0)
406 return value.size / element_fixed_size;
411 static GVariantSerialised
412 gvs_fixed_sized_array_get_child (GVariantSerialised value,
415 GVariantSerialised child = { };
417 child.type_info = g_variant_type_info_element (value.type_info);
418 g_variant_type_info_query (child.type_info, NULL, &child.size);
419 child.data = value.data + (child.size * index_);
420 g_variant_type_info_ref (child.type_info);
426 gvs_fixed_sized_array_needed_size (GVariantTypeInfo *type_info,
427 GVariantSerialisedFiller gvs_filler,
428 const gpointer *children,
431 gsize element_fixed_size;
433 g_variant_type_info_query_element (type_info, NULL, &element_fixed_size);
435 return element_fixed_size * n_children;
439 gvs_fixed_sized_array_serialise (GVariantSerialised value,
440 GVariantSerialisedFiller gvs_filler,
441 const gpointer *children,
444 GVariantSerialised child = { };
447 child.type_info = g_variant_type_info_element (value.type_info);
448 g_variant_type_info_query (child.type_info, NULL, &child.size);
449 child.data = value.data;
451 for (i = 0; i < n_children; i++)
453 gvs_filler (&child, children[i]);
454 child.data += child.size;
459 gvs_fixed_sized_array_is_normal (GVariantSerialised value)
461 GVariantSerialised child = { };
463 child.type_info = g_variant_type_info_element (value.type_info);
464 g_variant_type_info_query (child.type_info, NULL, &child.size);
466 if (value.size % child.size != 0)
469 for (child.data = value.data;
470 child.data < value.data + value.size;
471 child.data += child.size)
473 if (!g_variant_serialised_is_normal (child))
480 /* Variable-sized Array {{{3
482 * Variable sized arrays, containing variable-sized elements, must be
483 * able to determine the boundaries between the elements. The items
484 * cannot simply be concatenated. Additionally, we are faced with the
485 * fact that non-fixed-sized values do not neccessarily have a size that
486 * is a multiple of their alignment requirement, so we may need to
487 * insert zero-filled padding.
489 * While it is possible to find the start of an item by starting from
490 * the end of the item before it and padding for alignment, it is not
491 * generally possible to do the reverse operation. For this reason, we
492 * record the end point of each element in the array.
494 * GVariant works in terms of "offsets". An offset is a pointer to a
495 * boundary between two bytes. In 4 bytes of serialised data, there
496 * would be 5 possible offsets: one at the start ('0'), one between each
497 * pair of adjacent bytes ('1', '2', '3') and one at the end ('4').
499 * The numeric value of an offset is an unsigned integer given relative
500 * to the start of the serialised data of the array. Offsets are always
501 * stored in little endian byte order and are always only as big as they
502 * need to be. For example, in 255 bytes of serialised data, there are
503 * 256 offsets. All possibilities can be stored in an 8 bit unsigned
504 * integer. In 256 bytes of serialised data, however, there are 257
505 * possible offsets so 16 bit integers must be used. The size of an
506 * offset is always a power of 2.
508 * The offsets are stored at the end of the serialised data of the
509 * array. They are simply concatenated on without any particular
510 * alignment. The size of the offsets is included in the size of the
511 * serialised data for purposes of determining the size of the offsets.
512 * This presents a possibly ambiguity; in certain cases, a particular
513 * value of array could have two different serialised forms.
515 * Imagine an array containing a single string of 253 bytes in length
516 * (so, 254 bytes including the nul terminator). Now the offset must be
517 * written. If an 8 bit offset is written, it will bring the size of
518 * the array's serialised data to 255 -- which means that the use of an
519 * 8 bit offset was valid. If a 16 bit offset is used then the total
520 * size of the array will be 256 -- which means that the use of a 16 bit
521 * offset was valid. Although both of these will be accepted by the
522 * deserialiser, only the smaller of the two is considered to be in
523 * normal form and that is the one that the serialiser must produce.
527 gvs_read_unaligned_le (guchar *bytes,
532 guchar bytes[GLIB_SIZEOF_SIZE_T];
536 tmpvalue.integer = 0;
537 memcpy (&tmpvalue.bytes, bytes, size);
539 return GSIZE_FROM_LE (tmpvalue.integer);
543 gvs_write_unaligned_le (guchar *bytes,
549 guchar bytes[GLIB_SIZEOF_SIZE_T];
553 tmpvalue.integer = GSIZE_TO_LE (value);
554 memcpy (bytes, &tmpvalue.bytes, size);
558 gvs_get_offset_size (gsize size)
560 if (size > G_MAXUINT32)
563 else if (size > G_MAXUINT16)
566 else if (size > G_MAXUINT8)
576 gvs_calculate_total_size (gsize body_size,
579 if (body_size + 1 * offsets <= G_MAXUINT8)
580 return body_size + 1 * offsets;
582 if (body_size + 2 * offsets <= G_MAXUINT16)
583 return body_size + 2 * offsets;
585 if (body_size + 4 * offsets <= G_MAXUINT32)
586 return body_size + 4 * offsets;
588 return body_size + 8 * offsets;
592 gvs_variable_sized_array_n_children (GVariantSerialised value)
594 gsize offsets_array_size;
601 offset_size = gvs_get_offset_size (value.size);
603 last_end = gvs_read_unaligned_le (value.data + value.size -
604 offset_size, offset_size);
606 if (last_end > value.size)
609 offsets_array_size = value.size - last_end;
611 if (offsets_array_size % offset_size)
614 return offsets_array_size / offset_size;
617 static GVariantSerialised
618 gvs_variable_sized_array_get_child (GVariantSerialised value,
621 GVariantSerialised child = { };
627 child.type_info = g_variant_type_info_element (value.type_info);
628 g_variant_type_info_ref (child.type_info);
630 offset_size = gvs_get_offset_size (value.size);
632 last_end = gvs_read_unaligned_le (value.data + value.size -
633 offset_size, offset_size);
639 start = gvs_read_unaligned_le (value.data + last_end +
640 (offset_size * (index_ - 1)),
643 g_variant_type_info_query (child.type_info, &alignment, NULL);
644 start += (-start) & alignment;
649 end = gvs_read_unaligned_le (value.data + last_end +
650 (offset_size * index_),
653 if (start < end && end <= value.size)
655 child.data = value.data + start;
656 child.size = end - start;
663 gvs_variable_sized_array_needed_size (GVariantTypeInfo *type_info,
664 GVariantSerialisedFiller gvs_filler,
665 const gpointer *children,
672 g_variant_type_info_query (type_info, &alignment, NULL);
675 for (i = 0; i < n_children; i++)
677 GVariantSerialised child = { };
679 offset += (-offset) & alignment;
680 gvs_filler (&child, children[i]);
681 offset += child.size;
684 return gvs_calculate_total_size (offset, n_children);
688 gvs_variable_sized_array_serialise (GVariantSerialised value,
689 GVariantSerialisedFiller gvs_filler,
690 const gpointer *children,
699 g_variant_type_info_query (value.type_info, &alignment, NULL);
700 offset_size = gvs_get_offset_size (value.size);
703 offset_ptr = value.data + value.size - offset_size * n_children;
705 for (i = 0; i < n_children; i++)
707 GVariantSerialised child = { };
709 while (offset & alignment)
710 value.data[offset++] = '\0';
712 child.data = value.data + offset;
713 gvs_filler (&child, children[i]);
714 offset += child.size;
716 gvs_write_unaligned_le (offset_ptr, offset, offset_size);
717 offset_ptr += offset_size;
722 gvs_variable_sized_array_is_normal (GVariantSerialised value)
724 GVariantSerialised child = { };
725 gsize offsets_array_size;
726 guchar *offsets_array;
737 offset_size = gvs_get_offset_size (value.size);
738 last_end = gvs_read_unaligned_le (value.data + value.size -
739 offset_size, offset_size);
741 if (last_end > value.size)
744 offsets_array_size = value.size - last_end;
746 if (offsets_array_size % offset_size)
749 offsets_array = value.data + value.size - offsets_array_size;
750 length = offsets_array_size / offset_size;
755 child.type_info = g_variant_type_info_element (value.type_info);
756 g_variant_type_info_query (child.type_info, &alignment, NULL);
759 for (i = 0; i < length; i++)
763 this_end = gvs_read_unaligned_le (offsets_array + offset_size * i,
766 if (this_end < offset || this_end > last_end)
769 while (offset & alignment)
771 if (!(offset < this_end && value.data[offset] == '\0'))
776 child.data = value.data + offset;
777 child.size = this_end - offset;
782 if (!g_variant_serialised_is_normal (child))
788 g_assert (offset == last_end);
795 * Since tuples can contain a mix of variable- and fixed-sized items,
796 * they are, in terms of serialisation, a hybrid of variable-sized and
797 * fixed-sized arrays.
799 * Offsets are only stored for variable-sized items. Also, since the
800 * number of items in a tuple is known from its type, we are able to
801 * know exactly how many offsets to expect in the serialised data (and
802 * therefore how much space is taken up by the offset array). This
803 * means that we know where the end of the serialised data for the last
804 * item is -- we can just subtract the size of the offset array from the
805 * total size of the tuple. For this reason, the last item in the tuple
806 * doesn't need an offset stored.
808 * Tuple offsets are stored in reverse. This design choice allows
809 * iterator-based deserialisers to be more efficient.
811 * Most of the "heavy lifting" here is handled by the GVariantTypeInfo
812 * for the tuple. See the notes in gvarianttypeinfo.h.
816 gvs_tuple_n_children (GVariantSerialised value)
818 return g_variant_type_info_n_members (value.type_info);
821 static GVariantSerialised
822 gvs_tuple_get_child (GVariantSerialised value,
825 const GVariantMemberInfo *member_info;
826 GVariantSerialised child = { };
830 member_info = g_variant_type_info_member_info (value.type_info, index_);
831 child.type_info = g_variant_type_info_ref (member_info->type_info);
832 offset_size = gvs_get_offset_size (value.size);
834 /* tuples are the only (potentially) fixed-sized containers, so the
835 * only ones that have to deal with the possibility of having %NULL
836 * data with a non-zero %size if errors occured elsewhere.
838 if G_UNLIKELY (value.data == NULL && value.size != 0)
840 g_variant_type_info_query (child.type_info, NULL, &child.size);
842 /* this can only happen in fixed-sized tuples,
843 * so the child must also be fixed sized.
845 g_assert (child.size != 0);
851 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
853 if (offset_size * (member_info->i + 2) > value.size)
858 if (offset_size * (member_info->i + 1) > value.size)
860 /* if the child is fixed size, return its size.
861 * if child is not fixed-sized, return size = 0.
863 g_variant_type_info_query (child.type_info, NULL, &child.size);
869 if (member_info->i + 1)
870 start = gvs_read_unaligned_le (value.data + value.size -
871 offset_size * (member_info->i + 1),
876 start += member_info->a;
877 start &= member_info->b;
878 start |= member_info->c;
880 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_LAST)
881 end = value.size - offset_size * (member_info->i + 1);
883 else if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_FIXED)
887 g_variant_type_info_query (child.type_info, NULL, &fixed_size);
888 end = start + fixed_size;
889 child.size = fixed_size;
892 else /* G_VARIANT_MEMEBER_ENDING_OFFSET */
893 end = gvs_read_unaligned_le (value.data + value.size -
894 offset_size * (member_info->i + 2),
897 if (start < end && end <= value.size)
899 child.data = value.data + start;
900 child.size = end - start;
907 gvs_tuple_needed_size (GVariantTypeInfo *type_info,
908 GVariantSerialisedFiller gvs_filler,
909 const gpointer *children,
912 const GVariantMemberInfo *member_info = NULL;
917 g_variant_type_info_query (type_info, NULL, &fixed_size);
924 for (i = 0; i < n_children; i++)
928 member_info = g_variant_type_info_member_info (type_info, i);
929 g_variant_type_info_query (member_info->type_info,
930 &alignment, &fixed_size);
931 offset += (-offset) & alignment;
934 offset += fixed_size;
937 GVariantSerialised child = { };
939 gvs_filler (&child, children[i]);
940 offset += child.size;
944 return gvs_calculate_total_size (offset, member_info->i + 1);
948 gvs_tuple_serialise (GVariantSerialised value,
949 GVariantSerialisedFiller gvs_filler,
950 const gpointer *children,
957 offset_size = gvs_get_offset_size (value.size);
960 for (i = 0; i < n_children; i++)
962 const GVariantMemberInfo *member_info;
963 GVariantSerialised child = { };
966 member_info = g_variant_type_info_member_info (value.type_info, i);
967 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
969 while (offset & alignment)
970 value.data[offset++] = '\0';
972 child.data = value.data + offset;
973 gvs_filler (&child, children[i]);
974 offset += child.size;
976 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
978 value.size -= offset_size;
979 gvs_write_unaligned_le (value.data + value.size,
980 offset, offset_size);
984 while (offset < value.size)
985 value.data[offset++] = '\0';
989 gvs_tuple_is_normal (GVariantSerialised value)
997 offset_size = gvs_get_offset_size (value.size);
998 length = g_variant_type_info_n_members (value.type_info);
999 offset_ptr = value.size;
1002 for (i = 0; i < length; i++)
1004 const GVariantMemberInfo *member_info;
1005 GVariantSerialised child;
1010 member_info = g_variant_type_info_member_info (value.type_info, i);
1011 child.type_info = member_info->type_info;
1013 g_variant_type_info_query (child.type_info, &alignment, &fixed_size);
1015 while (offset & alignment)
1017 if (offset > value.size || value.data[offset] != '\0')
1022 child.data = value.data + offset;
1024 switch (member_info->ending_type)
1026 case G_VARIANT_MEMBER_ENDING_FIXED:
1027 end = offset + fixed_size;
1030 case G_VARIANT_MEMBER_ENDING_LAST:
1034 case G_VARIANT_MEMBER_ENDING_OFFSET:
1035 offset_ptr -= offset_size;
1037 if (offset_ptr < offset)
1040 end = gvs_read_unaligned_le (value.data + offset_ptr, offset_size);
1044 if (end < offset || end > offset_ptr)
1047 child.size = end - offset;
1049 if (child.size == 0)
1052 if (!g_variant_serialised_is_normal (child))
1062 g_variant_type_info_query (value.type_info, &alignment, &fixed_size);
1066 g_assert (fixed_size == value.size);
1067 g_assert (offset_ptr == value.size);
1071 if (value.data[offset++] != '\0')
1076 while (offset & alignment)
1077 if (value.data[offset++] != '\0')
1081 g_assert (offset == value.size);
1085 return offset_ptr == offset;
1090 * Variants are stored by storing the serialised data of the child,
1091 * followed by a '\0' character, followed by the type string of the
1094 * In the case that a value is presented that contains no '\0'
1095 * character, or doesn't have a single well-formed definite type string
1096 * following that character, the variant must be taken as containing the
1101 gvs_variant_n_children (GVariantSerialised value)
1106 static inline GVariantSerialised
1107 gvs_variant_get_child (GVariantSerialised value,
1110 GVariantSerialised child = { };
1112 /* NOTE: not O(1) and impossible for it to be... */
1116 /* find '\0' character */
1117 for (child.size = value.size - 1; child.size; child.size--)
1118 if (value.data[child.size] == '\0')
1121 /* ensure we didn't just hit the start of the string */
1122 if (value.data[child.size] == '\0')
1124 const gchar *type_string = (gchar *) &value.data[child.size + 1];
1125 const gchar *limit = (gchar *) &value.data[value.size];
1128 if (g_variant_type_string_scan (type_string, limit, &end) &&
1131 const GVariantType *type = (GVariantType *) type_string;
1133 if (g_variant_type_is_definite (type))
1137 child.type_info = g_variant_type_info_get (type);
1139 if (child.size != 0)
1140 /* only set to non-%NULL if size > 0 */
1141 child.data = value.data;
1143 g_variant_type_info_query (child.type_info,
1146 if (!fixed_size || fixed_size == child.size)
1149 g_variant_type_info_unref (child.type_info);
1155 child.type_info = g_variant_type_info_get (G_VARIANT_TYPE_UNIT);
1163 gvs_variant_needed_size (GVariantTypeInfo *type_info,
1164 GVariantSerialisedFiller gvs_filler,
1165 const gpointer *children,
1168 GVariantSerialised child = { };
1169 const gchar *type_string;
1171 gvs_filler (&child, children[0]);
1172 type_string = g_variant_type_info_get_type_string (child.type_info);
1174 return child.size + 1 + strlen (type_string);
1178 gvs_variant_serialise (GVariantSerialised value,
1179 GVariantSerialisedFiller gvs_filler,
1180 const gpointer *children,
1183 GVariantSerialised child = { };
1184 const gchar *type_string;
1186 child.data = value.data;
1188 gvs_filler (&child, children[0]);
1189 type_string = g_variant_type_info_get_type_string (child.type_info);
1190 value.data[child.size] = '\0';
1191 memcpy (value.data + child.size + 1, type_string, strlen (type_string));
1194 static inline gboolean
1195 gvs_variant_is_normal (GVariantSerialised value)
1197 GVariantSerialised child;
1200 child = gvs_variant_get_child (value, 0);
1202 normal = (child.data != NULL || child.size == 0) &&
1203 g_variant_serialised_is_normal (child);
1205 g_variant_type_info_unref (child.type_info);
1212 /* PART 2: Serialiser API {{{1
1214 * This is the implementation of the API of the serialiser as advertised
1215 * in gvariant-serialiser.h.
1218 /* Dispatch Utilities {{{2
1220 * These macros allow a given function (for example,
1221 * g_variant_serialiser_serialise) to be dispatched to the appropriate
1222 * type-specific function above (fixed/variable-sized maybe,
1223 * fixed/variable-sized array, tuple or variant).
1225 #define DISPATCH_FIXED(type_info, before, after) \
1229 g_variant_type_info_query_element (type_info, NULL, \
1234 before ## fixed_sized ## after \
1238 before ## variable_sized ## after \
1242 #define DISPATCH_CASES(type_info, before, after) \
1243 switch (g_variant_type_info_get_type_char (type_info)) \
1245 case G_VARIANT_TYPE_INFO_CHAR_MAYBE: \
1246 DISPATCH_FIXED (type_info, before, _maybe ## after) \
1248 case G_VARIANT_TYPE_INFO_CHAR_ARRAY: \
1249 DISPATCH_FIXED (type_info, before, _array ## after) \
1251 case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY: \
1252 case G_VARIANT_TYPE_INFO_CHAR_TUPLE: \
1254 before ## tuple ## after \
1257 case G_VARIANT_TYPE_INFO_CHAR_VARIANT: \
1259 before ## variant ## after \
1263 /* Serialiser entry points {{{2
1265 * These are the functions that are called in order for the serialiser
1270 * g_variant_serialised_n_children:
1271 * @serialised: a #GVariantSerialised
1272 * @returns: the number of children
1274 * For serialised data that represents a container value (maybes,
1275 * tuples, arrays, variants), determine how many child items are inside
1279 g_variant_serialised_n_children (GVariantSerialised serialised)
1281 g_variant_serialised_check (serialised);
1283 DISPATCH_CASES (serialised.type_info,
1285 return gvs_/**/,/**/_n_children (serialised);
1288 g_assert_not_reached ();
1292 * g_variant_serialised_get_child:
1293 * @serialised: a #GVariantSerialised
1294 * @index_: the index of the child to fetch
1295 * @returns: a #GVariantSerialised for the child
1297 * Extracts a child from a serialised data representing a container
1300 * It is an error to call this function with an index out of bounds.
1302 * If the result .data == %NULL and .size > 0 then there has been an
1303 * error extracting the requested fixed-sized value. This number of
1304 * zero bytes needs to be allocated instead.
1306 * In the case that .data == %NULL and .size == 0 then a zero-sized
1307 * item of a variable-sized type is being returned.
1309 * .data is never non-%NULL if size is 0.
1312 g_variant_serialised_get_child (GVariantSerialised serialised,
1315 GVariantSerialised child;
1317 g_variant_serialised_check (serialised);
1319 if G_LIKELY (index_ < g_variant_serialised_n_children (serialised))
1321 DISPATCH_CASES (serialised.type_info,
1323 child = gvs_/**/,/**/_get_child (serialised, index_);
1324 g_assert (child.size || child.data == NULL);
1325 g_variant_serialised_check (child);
1329 g_assert_not_reached ();
1332 g_error ("Attempt to access item %"G_GSIZE_FORMAT
1333 " in a container with only %"G_GSIZE_FORMAT" items",
1334 index_, g_variant_serialised_n_children (serialised));
1338 * g_variant_serialiser_serialise:
1339 * @serialised: a #GVariantSerialised, properly set up
1340 * @gvs_filler: the filler function
1341 * @children: an array of child items
1342 * @n_children: the size of @children
1344 * Writes data in serialised form.
1346 * The type_info field of @serialised must be filled in to type info for
1347 * the type that we are serialising.
1349 * The size field of @serialised must be filled in with the value
1350 * returned by a previous call to g_variant_serialiser_needed_size().
1352 * The data field of @serialised must be a pointer to a properly-aligned
1353 * memory region large enough to serialise into (ie: at least as big as
1356 * This function is only resonsible for serialising the top-level
1357 * container. @gvs_filler is called on each child of the container in
1358 * order for all of the data of that child to be filled in.
1361 g_variant_serialiser_serialise (GVariantSerialised serialised,
1362 GVariantSerialisedFiller gvs_filler,
1363 const gpointer *children,
1366 g_variant_serialised_check (serialised);
1368 DISPATCH_CASES (serialised.type_info,
1370 gvs_/**/,/**/_serialise (serialised, gvs_filler,
1371 children, n_children);
1375 g_assert_not_reached ();
1379 * g_variant_serialiser_needed_size:
1380 * @type_info: the type to serialise for
1381 * @gvs_filler: the filler function
1382 * @children: an array of child items
1383 * @n_children: the size of @children
1385 * Determines how much memory would be needed to serialise this value.
1387 * This function is only resonsible for performing calculations for the
1388 * top-level container. @gvs_filler is called on each child of the
1389 * container in order to determine its size.
1392 g_variant_serialiser_needed_size (GVariantTypeInfo *type_info,
1393 GVariantSerialisedFiller gvs_filler,
1394 const gpointer *children,
1397 DISPATCH_CASES (type_info,
1399 return gvs_/**/,/**/_needed_size (type_info, gvs_filler,
1400 children, n_children);
1403 g_assert_not_reached ();
1406 /* Byteswapping {{{2 */
1409 * g_variant_serialised_byteswap:
1410 * @value: a #GVariantSerialised
1412 * Byte-swap serialised data. The result of this function is only
1413 * well-defined if the data is in normal form.
1416 g_variant_serialised_byteswap (GVariantSerialised serialised)
1421 g_variant_serialised_check (serialised);
1423 if (!serialised.data)
1426 /* the types we potentially need to byteswap are
1427 * exactly those with alignment requirements.
1429 g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
1433 /* if fixed size and alignment are equal then we are down
1434 * to the base integer type and we should swap it. the
1435 * only exception to this is if we have a tuple with a
1436 * single item, and then swapping it will be OK anyway.
1438 if (alignment + 1 == fixed_size)
1444 guint16 *ptr = (guint16 *) serialised.data;
1446 g_assert_cmpint (serialised.size, ==, 2);
1447 *ptr = GUINT16_SWAP_LE_BE (*ptr);
1453 guint32 *ptr = (guint32 *) serialised.data;
1455 g_assert_cmpint (serialised.size, ==, 4);
1456 *ptr = GUINT32_SWAP_LE_BE (*ptr);
1462 guint64 *ptr = (guint64 *) serialised.data;
1464 g_assert_cmpint (serialised.size, ==, 8);
1465 *ptr = GUINT64_SWAP_LE_BE (*ptr);
1470 g_assert_not_reached ();
1474 /* else, we have a container that potentially contains
1475 * some children that need to be byteswapped.
1481 children = g_variant_serialised_n_children (serialised);
1482 for (i = 0; i < children; i++)
1484 GVariantSerialised child;
1486 child = g_variant_serialised_get_child (serialised, i);
1487 g_variant_serialised_byteswap (child);
1488 g_variant_type_info_unref (child.type_info);
1493 /* Normal form checking {{{2 */
1496 * g_variant_serialised_is_normal:
1497 * @serialised: a #GVariantSerialised
1499 * Determines, recursively if @serialised is in normal form. There is
1500 * precisely one normal form of serialised data for each possible value.
1502 * It is possible that multiple byte sequences form the serialised data
1503 * for a given value if, for example, the padding bytes are filled in
1504 * with something other than zeros, but only one form is the normal
1508 g_variant_serialised_is_normal (GVariantSerialised serialised)
1510 DISPATCH_CASES (serialised.type_info,
1512 return gvs_/**/,/**/_is_normal (serialised);
1516 /* some hard-coded terminal cases */
1517 switch (g_variant_type_info_get_type_char (serialised.type_info))
1519 case 'b': /* boolean */
1520 return serialised.data[0] < 2;
1522 case 's': /* string */
1523 return g_variant_serialiser_is_string (serialised.data,
1527 return g_variant_serialiser_is_object_path (serialised.data,
1531 return g_variant_serialiser_is_signature (serialised.data,
1535 /* all of the other types are fixed-sized numerical types for
1536 * which all possible values are valid (including various NaN
1537 * representations for floating point values).
1543 /* Validity-checking functions {{{2
1545 * Checks if strings, object paths and signature strings are valid.
1549 * g_variant_serialiser_is_string:
1550 * @data: a possible string
1551 * @size: the size of @data
1553 * Ensures that @data is a valid string with a nul terminator at the end
1554 * and no nul bytes embedded.
1557 g_variant_serialiser_is_string (gconstpointer data,
1560 const gchar *string = data;
1565 if (string[size - 1] != '\0')
1568 return strlen (string) == size - 1;
1572 * g_variant_serialiser_is_object_path:
1573 * @data: a possible DBus object path
1574 * @size: the size of @data
1576 * Performs the checks for being a valid string.
1578 * Also, ensures that @data is a valid DBus object path, as per the DBus
1582 g_variant_serialiser_is_object_path (gconstpointer data,
1585 const gchar *string = data;
1588 if (!g_variant_serialiser_is_string (data, size))
1591 /* The path must begin with an ASCII '/' (integer 47) character */
1592 if (string[0] != '/')
1595 for (i = 1; string[i]; i++)
1596 /* Each element must only contain the ASCII characters
1597 * "[A-Z][a-z][0-9]_"
1599 if (g_ascii_isalnum (string[i]) || string[i] == '_')
1602 /* must consist of elements separated by slash characters. */
1603 else if (string[i] == '/')
1605 /* No element may be the empty string. */
1606 /* Multiple '/' characters cannot occur in sequence. */
1607 if (string[i - 1] == '/')
1614 /* A trailing '/' character is not allowed unless the path is the
1615 * root path (a single '/' character).
1617 if (i > 1 && string[i - 1] == '/')
1624 * g_variant_serialiser_is_signature:
1625 * @data: a possible DBus signature
1626 * @size: the size of @data
1628 * Performs the checks for being a valid string.
1630 * Also, ensures that @data is a valid DBus type signature, as per the
1631 * DBus specification.
1634 g_variant_serialiser_is_signature (gconstpointer data,
1637 const gchar *string = data;
1638 gsize first_invalid;
1640 if (!g_variant_serialiser_is_string (data, size))
1643 /* make sure no non-definite characters appear */
1644 first_invalid = strspn (string, "ybnqiuxthdvasog(){}");
1645 if (string[first_invalid])
1648 /* make sure each type string is well-formed */
1650 if (!g_variant_type_string_scan (string, NULL, &string))
1656 /* vim:set foldmethod=marker: */