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>
26 #include "gvariant-serialiser.h"
28 #include <glib/gtestutils.h>
29 #include <glib/gstrfuncs.h>
30 #include <glib/gtypes.h>
38 * After this prologue section, this file has roughly 2 parts.
40 * The first part is split up into sections according to various
41 * container types. Maybe, Array, Tuple, Variant. The Maybe and Array
42 * sections are subdivided for element types being fixed or
43 * variable-sized types.
45 * Each section documents the format of that particular type of
46 * container and implements 5 functions for dealing with it:
49 * - determines (according to serialised data) how many child values
50 * are inside a particular container value.
53 * - gets the type of and the serialised data corresponding to a
54 * given child value within the container value.
57 * - determines how much space would be required to serialise a
58 * container of this type, containing the given children so that
59 * buffers can be preallocated before serialising.
62 * - write the serialised data for a container of this type,
63 * containing the given children, to a buffer.
66 * - check the given data to ensure that it is in normal form. For a
67 * given set of child values, there is exactly one normal form for
68 * the serialised data of a container. Other forms are possible
69 * while maintaining the same children (for example, by inserting
70 * something other than zero bytes as padding) but only one form is
73 * The second part contains the main entry point for each of the above 5
74 * functions and logic to dispatch it to the handler for the appropriate
75 * container type code.
77 * The second part also contains a routine to byteswap serialised
78 * values. This code makes use of the n_children() and get_child()
79 * functions above to do its work so no extra support is needed on a
80 * per-container-type basis.
82 * There is also additional code for checking for normal form. All
83 * numeric types are always in normal form since the full range of
84 * values is permitted (eg: 0 to 255 is a valid byte). Special checks
85 * need to be performed for booleans (only 0 or 1 allowed), strings
86 * (properly nul-terminated) and object paths and signature strings
87 * (meeting the DBus specification requirements).
92 * @type_info: the #GVariantTypeInfo of this value
93 * @data: the serialised data of this value, or %NULL
94 * @size: the size of this value
96 * A structure representing a GVariant in serialised form. This
97 * structure is used with #GVariantSerialisedFiller functions and as the
98 * primary interface to the serialiser. See #GVariantSerialisedFiller
99 * for a description of its use there.
101 * When used with the serialiser API functions, the following invariants
102 * apply to all #GVariantTypeSerialised structures passed to and
103 * returned from the serialiser.
105 * @type_info must be non-%NULL.
107 * @data must be properly aligned for the type described by @type_info.
109 * If @type_info describes a fixed-sized type then @size must always be
110 * equal to the fixed size of that type.
112 * For fixed-sized types (and only fixed-sized types), @data may be
113 * %NULL even if @size is non-zero. This happens when a framing error
114 * occurs while attempting to extract a fixed-sized value out of a
115 * variable-sized container. There is no data to return for the
116 * fixed-sized type, yet @size must be non-zero. The effect of this
117 * combination should be as if @data were a pointer to an
118 * appropriately-sized zero-filled region.
122 * g_variant_serialised_check:
123 * @serialised: a #GVariantSerialised struct
125 * Checks @serialised for validity according to the invariants described
129 g_variant_serialised_check (GVariantSerialised serialised)
134 g_assert (serialised.type_info != NULL);
135 g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
138 g_assert_cmpint (serialised.size, ==, fixed_size);
140 g_assert (serialised.size == 0 || serialised.data != NULL);
142 /* Depending on the native alignment requirements of the machine, the
143 * compiler will insert either 3 or 7 padding bytes after the char.
144 * This will result in the sizeof() the struct being 12 or 16.
145 * Subtract 9 to get 3 or 7 which is a nice bitmask to apply to get
146 * the alignment bits that we "care about" being zero: in the
147 * 4-aligned case, we care about 2 bits, and in the 8-aligned case, we
150 alignment &= sizeof (struct {
160 /* Some OSes (FreeBSD is a known example) have a malloc() that returns
161 * unaligned memory if you request small sizes. 'malloc (1);', for
162 * example, has been seen to return pointers aligned to 6 mod 16.
164 * Check if this is a small allocation and return without enforcing
165 * the alignment assertion if this is the case.
167 if (serialised.size <= alignment)
170 g_assert_cmpint (alignment & (gsize) serialised.data, ==, 0);
174 * GVariantSerialisedFiller:
175 * @serialised: a #GVariantSerialised instance to fill
176 * @data: data from the children array
178 * This function is called back from g_variant_serialiser_needed_size()
179 * and g_variant_serialiser_serialise(). It fills in missing details
180 * from a partially-complete #GVariantSerialised.
182 * The @data parameter passed back to the function is one of the items
183 * that was passed to the serialiser in the @children array. It
184 * represents a single child item of the container that is being
185 * serialised. The information filled in to @serialised is the
186 * information for this child.
188 * If the @type_info field of @serialised is %NULL then the callback
189 * function must set it to the type information corresponding to the
190 * type of the child. No reference should be added. If it is non-%NULL
191 * then the callback should assert that it is equal to the actual type
194 * If the @size field is zero then the callback must fill it in with the
195 * required amount of space to store the serialised form of the child.
196 * If it is non-zero then the callback should assert that it is equal to
197 * the needed size of the child.
199 * If @data is non-%NULL then it points to a space that is properly
200 * aligned for and large enough to store the serialised data of the
201 * child. The callback must store the serialised form of the child at
204 * If the child value is another container then the callback will likely
205 * recurse back into the serialiser by calling
206 * g_variant_serialiser_needed_size() to determine @size and
207 * g_variant_serialiser_serialise() to write to @data.
210 /* PART 1: Container types {{{1
212 * This section contains the serialiser implementation functions for
213 * each container type.
218 * Maybe types are handled depending on if the element type of the maybe
219 * type is a fixed-sized or variable-sized type. Although all maybe
220 * types themselves are variable-sized types, herein, a maybe value with
221 * a fixed-sized element type is called a "fixed-sized maybe" for
222 * convenience and a maybe value with a variable-sized element type is
223 * called a "variable-sized maybe".
226 /* Fixed-sized Maybe {{{3
228 * The size of a maybe value with a fixed-sized element type is either 0
229 * or equal to the fixed size of its element type. The case where the
230 * size of the maybe value is zero corresponds to the "Nothing" case and
231 * the case where the size of the maybe value is equal to the fixed size
232 * of the element type corresponds to the "Just" case; in that case, the
233 * serialised data of the child value forms the entire serialised data
234 * of the maybe value.
236 * In the event that a fixed-sized maybe value is presented with a size
237 * that is not equal to the fixed size of the element type then the
238 * value must be taken to be "Nothing".
242 gvs_fixed_sized_maybe_n_children (GVariantSerialised value)
244 gsize element_fixed_size;
246 g_variant_type_info_query_element (value.type_info, NULL,
247 &element_fixed_size);
249 return (element_fixed_size == value.size) ? 1 : 0;
252 static GVariantSerialised
253 gvs_fixed_sized_maybe_get_child (GVariantSerialised value,
256 /* the child has the same bounds as the
257 * container, so just update the type.
259 value.type_info = g_variant_type_info_element (value.type_info);
260 g_variant_type_info_ref (value.type_info);
266 gvs_fixed_sized_maybe_needed_size (GVariantTypeInfo *type_info,
267 GVariantSerialisedFiller gvs_filler,
268 const gpointer *children,
273 gsize element_fixed_size;
275 g_variant_type_info_query_element (type_info, NULL,
276 &element_fixed_size);
278 return element_fixed_size;
285 gvs_fixed_sized_maybe_serialise (GVariantSerialised value,
286 GVariantSerialisedFiller gvs_filler,
287 const gpointer *children,
292 GVariantSerialised child = { NULL, value.data, value.size };
294 gvs_filler (&child, children[0]);
299 gvs_fixed_sized_maybe_is_normal (GVariantSerialised value)
303 gsize element_fixed_size;
305 g_variant_type_info_query_element (value.type_info,
306 NULL, &element_fixed_size);
308 if (value.size != element_fixed_size)
311 /* proper element size: "Just". recurse to the child. */
312 value.type_info = g_variant_type_info_element (value.type_info);
314 return g_variant_serialised_is_normal (value);
317 /* size of 0: "Nothing" */
321 /* Variable-sized Maybe
323 * The size of a maybe value with a variable-sized element type is
324 * either 0 or strictly greater than 0. The case where the size of the
325 * maybe value is zero corresponds to the "Nothing" case and the case
326 * where the size of the maybe value is greater than zero corresponds to
327 * the "Just" case; in that case, the serialised data of the child value
328 * forms the first part of the serialised data of the maybe value and is
329 * followed by a single zero byte. This zero byte is always appended,
330 * regardless of any zero bytes that may already be at the end of the
331 * serialised ata of the child value.
335 gvs_variable_sized_maybe_n_children (GVariantSerialised value)
337 return (value.size > 0) ? 1 : 0;
340 static GVariantSerialised
341 gvs_variable_sized_maybe_get_child (GVariantSerialised value,
344 /* remove the padding byte and update the type. */
345 value.type_info = g_variant_type_info_element (value.type_info);
346 g_variant_type_info_ref (value.type_info);
349 /* if it's zero-sized then it may as well be NULL */
357 gvs_variable_sized_maybe_needed_size (GVariantTypeInfo *type_info,
358 GVariantSerialisedFiller gvs_filler,
359 const gpointer *children,
364 GVariantSerialised child = { 0, };
366 gvs_filler (&child, children[0]);
368 return child.size + 1;
375 gvs_variable_sized_maybe_serialise (GVariantSerialised value,
376 GVariantSerialisedFiller gvs_filler,
377 const gpointer *children,
382 GVariantSerialised child = { NULL, value.data, value.size - 1 };
384 /* write the data for the child. */
385 gvs_filler (&child, children[0]);
386 value.data[child.size] = '\0';
391 gvs_variable_sized_maybe_is_normal (GVariantSerialised value)
396 if (value.data[value.size - 1] != '\0')
399 value.type_info = g_variant_type_info_element (value.type_info);
402 return g_variant_serialised_is_normal (value);
407 * Just as with maybe types, array types are handled depending on if the
408 * element type of the array type is a fixed-sized or variable-sized
409 * type. Similar to maybe types, for convenience, an array value with a
410 * fixed-sized element type is called a "fixed-sized array" and an array
411 * value with a variable-sized element type is called a "variable sized
415 /* Fixed-sized Array {{{3
417 * For fixed sized arrays, the serialised data is simply a concatenation
418 * of the serialised data of each element, in order. Since fixed-sized
419 * values always have a fixed size that is a multiple of their alignment
420 * requirement no extra padding is required.
422 * In the event that a fixed-sized array is presented with a size that
423 * is not an integer multiple of the element size then the value of the
424 * array must be taken as being empty.
428 gvs_fixed_sized_array_n_children (GVariantSerialised value)
430 gsize element_fixed_size;
432 g_variant_type_info_query_element (value.type_info, NULL,
433 &element_fixed_size);
435 if (value.size % element_fixed_size == 0)
436 return value.size / element_fixed_size;
441 static GVariantSerialised
442 gvs_fixed_sized_array_get_child (GVariantSerialised value,
445 GVariantSerialised child = { 0, };
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 + (child.size * index_);
450 g_variant_type_info_ref (child.type_info);
456 gvs_fixed_sized_array_needed_size (GVariantTypeInfo *type_info,
457 GVariantSerialisedFiller gvs_filler,
458 const gpointer *children,
461 gsize element_fixed_size;
463 g_variant_type_info_query_element (type_info, NULL, &element_fixed_size);
465 return element_fixed_size * n_children;
469 gvs_fixed_sized_array_serialise (GVariantSerialised value,
470 GVariantSerialisedFiller gvs_filler,
471 const gpointer *children,
474 GVariantSerialised child = { 0, };
477 child.type_info = g_variant_type_info_element (value.type_info);
478 g_variant_type_info_query (child.type_info, NULL, &child.size);
479 child.data = value.data;
481 for (i = 0; i < n_children; i++)
483 gvs_filler (&child, children[i]);
484 child.data += child.size;
489 gvs_fixed_sized_array_is_normal (GVariantSerialised value)
491 GVariantSerialised child = { 0, };
493 child.type_info = g_variant_type_info_element (value.type_info);
494 g_variant_type_info_query (child.type_info, NULL, &child.size);
496 if (value.size % child.size != 0)
499 for (child.data = value.data;
500 child.data < value.data + value.size;
501 child.data += child.size)
503 if (!g_variant_serialised_is_normal (child))
510 /* Variable-sized Array {{{3
512 * Variable sized arrays, containing variable-sized elements, must be
513 * able to determine the boundaries between the elements. The items
514 * cannot simply be concatenated. Additionally, we are faced with the
515 * fact that non-fixed-sized values do not neccessarily have a size that
516 * is a multiple of their alignment requirement, so we may need to
517 * insert zero-filled padding.
519 * While it is possible to find the start of an item by starting from
520 * the end of the item before it and padding for alignment, it is not
521 * generally possible to do the reverse operation. For this reason, we
522 * record the end point of each element in the array.
524 * GVariant works in terms of "offsets". An offset is a pointer to a
525 * boundary between two bytes. In 4 bytes of serialised data, there
526 * would be 5 possible offsets: one at the start ('0'), one between each
527 * pair of adjacent bytes ('1', '2', '3') and one at the end ('4').
529 * The numeric value of an offset is an unsigned integer given relative
530 * to the start of the serialised data of the array. Offsets are always
531 * stored in little endian byte order and are always only as big as they
532 * need to be. For example, in 255 bytes of serialised data, there are
533 * 256 offsets. All possibilities can be stored in an 8 bit unsigned
534 * integer. In 256 bytes of serialised data, however, there are 257
535 * possible offsets so 16 bit integers must be used. The size of an
536 * offset is always a power of 2.
538 * The offsets are stored at the end of the serialised data of the
539 * array. They are simply concatenated on without any particular
540 * alignment. The size of the offsets is included in the size of the
541 * serialised data for purposes of determining the size of the offsets.
542 * This presents a possibly ambiguity; in certain cases, a particular
543 * value of array could have two different serialised forms.
545 * Imagine an array containing a single string of 253 bytes in length
546 * (so, 254 bytes including the nul terminator). Now the offset must be
547 * written. If an 8 bit offset is written, it will bring the size of
548 * the array's serialised data to 255 -- which means that the use of an
549 * 8 bit offset was valid. If a 16 bit offset is used then the total
550 * size of the array will be 256 -- which means that the use of a 16 bit
551 * offset was valid. Although both of these will be accepted by the
552 * deserialiser, only the smaller of the two is considered to be in
553 * normal form and that is the one that the serialiser must produce.
557 gvs_read_unaligned_le (guchar *bytes,
562 guchar bytes[GLIB_SIZEOF_SIZE_T];
566 tmpvalue.integer = 0;
567 memcpy (&tmpvalue.bytes, bytes, size);
569 return GSIZE_FROM_LE (tmpvalue.integer);
573 gvs_write_unaligned_le (guchar *bytes,
579 guchar bytes[GLIB_SIZEOF_SIZE_T];
583 tmpvalue.integer = GSIZE_TO_LE (value);
584 memcpy (bytes, &tmpvalue.bytes, size);
588 gvs_get_offset_size (gsize size)
590 if (size > G_MAXUINT32)
593 else if (size > G_MAXUINT16)
596 else if (size > G_MAXUINT8)
606 gvs_calculate_total_size (gsize body_size,
609 if (body_size + 1 * offsets <= G_MAXUINT8)
610 return body_size + 1 * offsets;
612 if (body_size + 2 * offsets <= G_MAXUINT16)
613 return body_size + 2 * offsets;
615 if (body_size + 4 * offsets <= G_MAXUINT32)
616 return body_size + 4 * offsets;
618 return body_size + 8 * offsets;
622 gvs_variable_sized_array_n_children (GVariantSerialised value)
624 gsize offsets_array_size;
631 offset_size = gvs_get_offset_size (value.size);
633 last_end = gvs_read_unaligned_le (value.data + value.size -
634 offset_size, offset_size);
636 if (last_end > value.size)
639 offsets_array_size = value.size - last_end;
641 if (offsets_array_size % offset_size)
644 return offsets_array_size / offset_size;
647 static GVariantSerialised
648 gvs_variable_sized_array_get_child (GVariantSerialised value,
651 GVariantSerialised child = { 0, };
657 child.type_info = g_variant_type_info_element (value.type_info);
658 g_variant_type_info_ref (child.type_info);
660 offset_size = gvs_get_offset_size (value.size);
662 last_end = gvs_read_unaligned_le (value.data + value.size -
663 offset_size, offset_size);
669 start = gvs_read_unaligned_le (value.data + last_end +
670 (offset_size * (index_ - 1)),
673 g_variant_type_info_query (child.type_info, &alignment, NULL);
674 start += (-start) & alignment;
679 end = gvs_read_unaligned_le (value.data + last_end +
680 (offset_size * index_),
683 if (start < end && end <= value.size)
685 child.data = value.data + start;
686 child.size = end - start;
693 gvs_variable_sized_array_needed_size (GVariantTypeInfo *type_info,
694 GVariantSerialisedFiller gvs_filler,
695 const gpointer *children,
702 g_variant_type_info_query (type_info, &alignment, NULL);
705 for (i = 0; i < n_children; i++)
707 GVariantSerialised child = { 0, };
709 offset += (-offset) & alignment;
710 gvs_filler (&child, children[i]);
711 offset += child.size;
714 return gvs_calculate_total_size (offset, n_children);
718 gvs_variable_sized_array_serialise (GVariantSerialised value,
719 GVariantSerialisedFiller gvs_filler,
720 const gpointer *children,
729 g_variant_type_info_query (value.type_info, &alignment, NULL);
730 offset_size = gvs_get_offset_size (value.size);
733 offset_ptr = value.data + value.size - offset_size * n_children;
735 for (i = 0; i < n_children; i++)
737 GVariantSerialised child = { 0, };
739 while (offset & alignment)
740 value.data[offset++] = '\0';
742 child.data = value.data + offset;
743 gvs_filler (&child, children[i]);
744 offset += child.size;
746 gvs_write_unaligned_le (offset_ptr, offset, offset_size);
747 offset_ptr += offset_size;
752 gvs_variable_sized_array_is_normal (GVariantSerialised value)
754 GVariantSerialised child = { 0, };
755 gsize offsets_array_size;
756 guchar *offsets_array;
767 offset_size = gvs_get_offset_size (value.size);
768 last_end = gvs_read_unaligned_le (value.data + value.size -
769 offset_size, offset_size);
771 if (last_end > value.size)
774 offsets_array_size = value.size - last_end;
776 if (offsets_array_size % offset_size)
779 offsets_array = value.data + value.size - offsets_array_size;
780 length = offsets_array_size / offset_size;
785 child.type_info = g_variant_type_info_element (value.type_info);
786 g_variant_type_info_query (child.type_info, &alignment, NULL);
789 for (i = 0; i < length; i++)
793 this_end = gvs_read_unaligned_le (offsets_array + offset_size * i,
796 if (this_end < offset || this_end > last_end)
799 while (offset & alignment)
801 if (!(offset < this_end && value.data[offset] == '\0'))
806 child.data = value.data + offset;
807 child.size = this_end - offset;
812 if (!g_variant_serialised_is_normal (child))
818 g_assert (offset == last_end);
825 * Since tuples can contain a mix of variable- and fixed-sized items,
826 * they are, in terms of serialisation, a hybrid of variable-sized and
827 * fixed-sized arrays.
829 * Offsets are only stored for variable-sized items. Also, since the
830 * number of items in a tuple is known from its type, we are able to
831 * know exactly how many offsets to expect in the serialised data (and
832 * therefore how much space is taken up by the offset array). This
833 * means that we know where the end of the serialised data for the last
834 * item is -- we can just subtract the size of the offset array from the
835 * total size of the tuple. For this reason, the last item in the tuple
836 * doesn't need an offset stored.
838 * Tuple offsets are stored in reverse. This design choice allows
839 * iterator-based deserialisers to be more efficient.
841 * Most of the "heavy lifting" here is handled by the GVariantTypeInfo
842 * for the tuple. See the notes in gvarianttypeinfo.h.
846 gvs_tuple_n_children (GVariantSerialised value)
848 return g_variant_type_info_n_members (value.type_info);
851 static GVariantSerialised
852 gvs_tuple_get_child (GVariantSerialised value,
855 const GVariantMemberInfo *member_info;
856 GVariantSerialised child = { 0, };
860 member_info = g_variant_type_info_member_info (value.type_info, index_);
861 child.type_info = g_variant_type_info_ref (member_info->type_info);
862 offset_size = gvs_get_offset_size (value.size);
864 /* tuples are the only (potentially) fixed-sized containers, so the
865 * only ones that have to deal with the possibility of having %NULL
866 * data with a non-zero %size if errors occured elsewhere.
868 if G_UNLIKELY (value.data == NULL && value.size != 0)
870 g_variant_type_info_query (child.type_info, NULL, &child.size);
872 /* this can only happen in fixed-sized tuples,
873 * so the child must also be fixed sized.
875 g_assert (child.size != 0);
881 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
883 if (offset_size * (member_info->i + 2) > value.size)
888 if (offset_size * (member_info->i + 1) > value.size)
890 /* if the child is fixed size, return its size.
891 * if child is not fixed-sized, return size = 0.
893 g_variant_type_info_query (child.type_info, NULL, &child.size);
899 if (member_info->i + 1)
900 start = gvs_read_unaligned_le (value.data + value.size -
901 offset_size * (member_info->i + 1),
906 start += member_info->a;
907 start &= member_info->b;
908 start |= member_info->c;
910 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_LAST)
911 end = value.size - offset_size * (member_info->i + 1);
913 else if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_FIXED)
917 g_variant_type_info_query (child.type_info, NULL, &fixed_size);
918 end = start + fixed_size;
919 child.size = fixed_size;
922 else /* G_VARIANT_MEMEBER_ENDING_OFFSET */
923 end = gvs_read_unaligned_le (value.data + value.size -
924 offset_size * (member_info->i + 2),
927 if (start < end && end <= value.size)
929 child.data = value.data + start;
930 child.size = end - start;
937 gvs_tuple_needed_size (GVariantTypeInfo *type_info,
938 GVariantSerialisedFiller gvs_filler,
939 const gpointer *children,
942 const GVariantMemberInfo *member_info = NULL;
947 g_variant_type_info_query (type_info, NULL, &fixed_size);
954 for (i = 0; i < n_children; i++)
958 member_info = g_variant_type_info_member_info (type_info, i);
959 g_variant_type_info_query (member_info->type_info,
960 &alignment, &fixed_size);
961 offset += (-offset) & alignment;
964 offset += fixed_size;
967 GVariantSerialised child = { 0, };
969 gvs_filler (&child, children[i]);
970 offset += child.size;
974 return gvs_calculate_total_size (offset, member_info->i + 1);
978 gvs_tuple_serialise (GVariantSerialised value,
979 GVariantSerialisedFiller gvs_filler,
980 const gpointer *children,
987 offset_size = gvs_get_offset_size (value.size);
990 for (i = 0; i < n_children; i++)
992 const GVariantMemberInfo *member_info;
993 GVariantSerialised child = { 0, };
996 member_info = g_variant_type_info_member_info (value.type_info, i);
997 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
999 while (offset & alignment)
1000 value.data[offset++] = '\0';
1002 child.data = value.data + offset;
1003 gvs_filler (&child, children[i]);
1004 offset += child.size;
1006 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
1008 value.size -= offset_size;
1009 gvs_write_unaligned_le (value.data + value.size,
1010 offset, offset_size);
1014 while (offset < value.size)
1015 value.data[offset++] = '\0';
1019 gvs_tuple_is_normal (GVariantSerialised value)
1027 offset_size = gvs_get_offset_size (value.size);
1028 length = g_variant_type_info_n_members (value.type_info);
1029 offset_ptr = value.size;
1032 for (i = 0; i < length; i++)
1034 const GVariantMemberInfo *member_info;
1035 GVariantSerialised child;
1040 member_info = g_variant_type_info_member_info (value.type_info, i);
1041 child.type_info = member_info->type_info;
1043 g_variant_type_info_query (child.type_info, &alignment, &fixed_size);
1045 while (offset & alignment)
1047 if (offset > value.size || value.data[offset] != '\0')
1052 child.data = value.data + offset;
1054 switch (member_info->ending_type)
1056 case G_VARIANT_MEMBER_ENDING_FIXED:
1057 end = offset + fixed_size;
1060 case G_VARIANT_MEMBER_ENDING_LAST:
1064 case G_VARIANT_MEMBER_ENDING_OFFSET:
1065 offset_ptr -= offset_size;
1067 if (offset_ptr < offset)
1070 end = gvs_read_unaligned_le (value.data + offset_ptr, offset_size);
1074 g_assert_not_reached ();
1077 if (end < offset || end > offset_ptr)
1080 child.size = end - offset;
1082 if (child.size == 0)
1085 if (!g_variant_serialised_is_normal (child))
1095 g_variant_type_info_query (value.type_info, &alignment, &fixed_size);
1099 g_assert (fixed_size == value.size);
1100 g_assert (offset_ptr == value.size);
1104 if (value.data[offset++] != '\0')
1109 while (offset & alignment)
1110 if (value.data[offset++] != '\0')
1114 g_assert (offset == value.size);
1118 return offset_ptr == offset;
1123 * Variants are stored by storing the serialised data of the child,
1124 * followed by a '\0' character, followed by the type string of the
1127 * In the case that a value is presented that contains no '\0'
1128 * character, or doesn't have a single well-formed definite type string
1129 * following that character, the variant must be taken as containing the
1134 gvs_variant_n_children (GVariantSerialised value)
1139 static inline GVariantSerialised
1140 gvs_variant_get_child (GVariantSerialised value,
1143 GVariantSerialised child = { 0, };
1145 /* NOTE: not O(1) and impossible for it to be... */
1148 /* find '\0' character */
1149 for (child.size = value.size - 1; child.size; child.size--)
1150 if (value.data[child.size] == '\0')
1153 /* ensure we didn't just hit the start of the string */
1154 if (value.data[child.size] == '\0')
1156 const gchar *type_string = (gchar *) &value.data[child.size + 1];
1157 const gchar *limit = (gchar *) &value.data[value.size];
1160 if (g_variant_type_string_scan (type_string, limit, &end) &&
1163 const GVariantType *type = (GVariantType *) type_string;
1165 if (g_variant_type_is_definite (type))
1169 child.type_info = g_variant_type_info_get (type);
1171 if (child.size != 0)
1172 /* only set to non-%NULL if size > 0 */
1173 child.data = value.data;
1175 g_variant_type_info_query (child.type_info,
1178 if (!fixed_size || fixed_size == child.size)
1181 g_variant_type_info_unref (child.type_info);
1187 child.type_info = g_variant_type_info_get (G_VARIANT_TYPE_UNIT);
1195 gvs_variant_needed_size (GVariantTypeInfo *type_info,
1196 GVariantSerialisedFiller gvs_filler,
1197 const gpointer *children,
1200 GVariantSerialised child = { 0, };
1201 const gchar *type_string;
1203 gvs_filler (&child, children[0]);
1204 type_string = g_variant_type_info_get_type_string (child.type_info);
1206 return child.size + 1 + strlen (type_string);
1210 gvs_variant_serialise (GVariantSerialised value,
1211 GVariantSerialisedFiller gvs_filler,
1212 const gpointer *children,
1215 GVariantSerialised child = { 0, };
1216 const gchar *type_string;
1218 child.data = value.data;
1220 gvs_filler (&child, children[0]);
1221 type_string = g_variant_type_info_get_type_string (child.type_info);
1222 value.data[child.size] = '\0';
1223 memcpy (value.data + child.size + 1, type_string, strlen (type_string));
1226 static inline gboolean
1227 gvs_variant_is_normal (GVariantSerialised value)
1229 GVariantSerialised child;
1232 child = gvs_variant_get_child (value, 0);
1234 normal = (child.data != NULL || child.size == 0) &&
1235 g_variant_serialised_is_normal (child);
1237 g_variant_type_info_unref (child.type_info);
1244 /* PART 2: Serialiser API {{{1
1246 * This is the implementation of the API of the serialiser as advertised
1247 * in gvariant-serialiser.h.
1250 /* Dispatch Utilities {{{2
1252 * These macros allow a given function (for example,
1253 * g_variant_serialiser_serialise) to be dispatched to the appropriate
1254 * type-specific function above (fixed/variable-sized maybe,
1255 * fixed/variable-sized array, tuple or variant).
1257 #define DISPATCH_FIXED(type_info, before, after) \
1261 g_variant_type_info_query_element (type_info, NULL, \
1266 before ## fixed_sized ## after \
1270 before ## variable_sized ## after \
1274 #define DISPATCH_CASES(type_info, before, after) \
1275 switch (g_variant_type_info_get_type_char (type_info)) \
1277 case G_VARIANT_TYPE_INFO_CHAR_MAYBE: \
1278 DISPATCH_FIXED (type_info, before, _maybe ## after) \
1280 case G_VARIANT_TYPE_INFO_CHAR_ARRAY: \
1281 DISPATCH_FIXED (type_info, before, _array ## after) \
1283 case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY: \
1284 case G_VARIANT_TYPE_INFO_CHAR_TUPLE: \
1286 before ## tuple ## after \
1289 case G_VARIANT_TYPE_INFO_CHAR_VARIANT: \
1291 before ## variant ## after \
1295 /* Serialiser entry points {{{2
1297 * These are the functions that are called in order for the serialiser
1302 * g_variant_serialised_n_children:
1303 * @serialised: a #GVariantSerialised
1304 * @returns: the number of children
1306 * For serialised data that represents a container value (maybes,
1307 * tuples, arrays, variants), determine how many child items are inside
1311 g_variant_serialised_n_children (GVariantSerialised serialised)
1313 g_variant_serialised_check (serialised);
1315 DISPATCH_CASES (serialised.type_info,
1317 return gvs_/**/,/**/_n_children (serialised);
1320 g_assert_not_reached ();
1324 * g_variant_serialised_get_child:
1325 * @serialised: a #GVariantSerialised
1326 * @index_: the index of the child to fetch
1327 * @returns: a #GVariantSerialised for the child
1329 * Extracts a child from a serialised data representing a container
1332 * It is an error to call this function with an index out of bounds.
1334 * If the result .data == %NULL and .size > 0 then there has been an
1335 * error extracting the requested fixed-sized value. This number of
1336 * zero bytes needs to be allocated instead.
1338 * In the case that .data == %NULL and .size == 0 then a zero-sized
1339 * item of a variable-sized type is being returned.
1341 * .data is never non-%NULL if size is 0.
1344 g_variant_serialised_get_child (GVariantSerialised serialised,
1347 GVariantSerialised child;
1349 g_variant_serialised_check (serialised);
1351 if G_LIKELY (index_ < g_variant_serialised_n_children (serialised))
1353 DISPATCH_CASES (serialised.type_info,
1355 child = gvs_/**/,/**/_get_child (serialised, index_);
1356 g_assert (child.size || child.data == NULL);
1357 g_variant_serialised_check (child);
1361 g_assert_not_reached ();
1364 g_error ("Attempt to access item %"G_GSIZE_FORMAT
1365 " in a container with only %"G_GSIZE_FORMAT" items",
1366 index_, g_variant_serialised_n_children (serialised));
1370 * g_variant_serialiser_serialise:
1371 * @serialised: a #GVariantSerialised, properly set up
1372 * @gvs_filler: the filler function
1373 * @children: an array of child items
1374 * @n_children: the size of @children
1376 * Writes data in serialised form.
1378 * The type_info field of @serialised must be filled in to type info for
1379 * the type that we are serialising.
1381 * The size field of @serialised must be filled in with the value
1382 * returned by a previous call to g_variant_serialiser_needed_size().
1384 * The data field of @serialised must be a pointer to a properly-aligned
1385 * memory region large enough to serialise into (ie: at least as big as
1388 * This function is only resonsible for serialising the top-level
1389 * container. @gvs_filler is called on each child of the container in
1390 * order for all of the data of that child to be filled in.
1393 g_variant_serialiser_serialise (GVariantSerialised serialised,
1394 GVariantSerialisedFiller gvs_filler,
1395 const gpointer *children,
1398 g_variant_serialised_check (serialised);
1400 DISPATCH_CASES (serialised.type_info,
1402 gvs_/**/,/**/_serialise (serialised, gvs_filler,
1403 children, n_children);
1407 g_assert_not_reached ();
1411 * g_variant_serialiser_needed_size:
1412 * @type_info: the type to serialise for
1413 * @gvs_filler: the filler function
1414 * @children: an array of child items
1415 * @n_children: the size of @children
1417 * Determines how much memory would be needed to serialise this value.
1419 * This function is only resonsible for performing calculations for the
1420 * top-level container. @gvs_filler is called on each child of the
1421 * container in order to determine its size.
1424 g_variant_serialiser_needed_size (GVariantTypeInfo *type_info,
1425 GVariantSerialisedFiller gvs_filler,
1426 const gpointer *children,
1429 DISPATCH_CASES (type_info,
1431 return gvs_/**/,/**/_needed_size (type_info, gvs_filler,
1432 children, n_children);
1435 g_assert_not_reached ();
1438 /* Byteswapping {{{2 */
1441 * g_variant_serialised_byteswap:
1442 * @value: a #GVariantSerialised
1444 * Byte-swap serialised data. The result of this function is only
1445 * well-defined if the data is in normal form.
1448 g_variant_serialised_byteswap (GVariantSerialised serialised)
1453 g_variant_serialised_check (serialised);
1455 if (!serialised.data)
1458 /* the types we potentially need to byteswap are
1459 * exactly those with alignment requirements.
1461 g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
1465 /* if fixed size and alignment are equal then we are down
1466 * to the base integer type and we should swap it. the
1467 * only exception to this is if we have a tuple with a
1468 * single item, and then swapping it will be OK anyway.
1470 if (alignment + 1 == fixed_size)
1476 guint16 *ptr = (guint16 *) serialised.data;
1478 g_assert_cmpint (serialised.size, ==, 2);
1479 *ptr = GUINT16_SWAP_LE_BE (*ptr);
1485 guint32 *ptr = (guint32 *) serialised.data;
1487 g_assert_cmpint (serialised.size, ==, 4);
1488 *ptr = GUINT32_SWAP_LE_BE (*ptr);
1494 guint64 *ptr = (guint64 *) serialised.data;
1496 g_assert_cmpint (serialised.size, ==, 8);
1497 *ptr = GUINT64_SWAP_LE_BE (*ptr);
1502 g_assert_not_reached ();
1506 /* else, we have a container that potentially contains
1507 * some children that need to be byteswapped.
1513 children = g_variant_serialised_n_children (serialised);
1514 for (i = 0; i < children; i++)
1516 GVariantSerialised child;
1518 child = g_variant_serialised_get_child (serialised, i);
1519 g_variant_serialised_byteswap (child);
1520 g_variant_type_info_unref (child.type_info);
1525 /* Normal form checking {{{2 */
1528 * g_variant_serialised_is_normal:
1529 * @serialised: a #GVariantSerialised
1531 * Determines, recursively if @serialised is in normal form. There is
1532 * precisely one normal form of serialised data for each possible value.
1534 * It is possible that multiple byte sequences form the serialised data
1535 * for a given value if, for example, the padding bytes are filled in
1536 * with something other than zeros, but only one form is the normal
1540 g_variant_serialised_is_normal (GVariantSerialised serialised)
1542 DISPATCH_CASES (serialised.type_info,
1544 return gvs_/**/,/**/_is_normal (serialised);
1548 /* some hard-coded terminal cases */
1549 switch (g_variant_type_info_get_type_char (serialised.type_info))
1551 case 'b': /* boolean */
1552 return serialised.data[0] < 2;
1554 case 's': /* string */
1555 return g_variant_serialiser_is_string (serialised.data,
1559 return g_variant_serialiser_is_object_path (serialised.data,
1563 return g_variant_serialiser_is_signature (serialised.data,
1567 /* all of the other types are fixed-sized numerical types for
1568 * which all possible values are valid (including various NaN
1569 * representations for floating point values).
1575 /* Validity-checking functions {{{2
1577 * Checks if strings, object paths and signature strings are valid.
1581 * g_variant_serialiser_is_string:
1582 * @data: a possible string
1583 * @size: the size of @data
1585 * Ensures that @data is a valid string with a nul terminator at the end
1586 * and no nul bytes embedded.
1589 g_variant_serialiser_is_string (gconstpointer data,
1594 g_utf8_validate (data, size, &end);
1596 return data == end - (size - 1);
1600 * g_variant_serialiser_is_object_path:
1601 * @data: a possible DBus object path
1602 * @size: the size of @data
1604 * Performs the checks for being a valid string.
1606 * Also, ensures that @data is a valid DBus object path, as per the DBus
1610 g_variant_serialiser_is_object_path (gconstpointer data,
1613 const gchar *string = data;
1616 if (!g_variant_serialiser_is_string (data, size))
1619 /* The path must begin with an ASCII '/' (integer 47) character */
1620 if (string[0] != '/')
1623 for (i = 1; string[i]; i++)
1624 /* Each element must only contain the ASCII characters
1625 * "[A-Z][a-z][0-9]_"
1627 if (g_ascii_isalnum (string[i]) || string[i] == '_')
1630 /* must consist of elements separated by slash characters. */
1631 else if (string[i] == '/')
1633 /* No element may be the empty string. */
1634 /* Multiple '/' characters cannot occur in sequence. */
1635 if (string[i - 1] == '/')
1642 /* A trailing '/' character is not allowed unless the path is the
1643 * root path (a single '/' character).
1645 if (i > 1 && string[i - 1] == '/')
1652 * g_variant_serialiser_is_signature:
1653 * @data: a possible DBus signature
1654 * @size: the size of @data
1656 * Performs the checks for being a valid string.
1658 * Also, ensures that @data is a valid DBus type signature, as per the
1659 * DBus specification.
1662 g_variant_serialiser_is_signature (gconstpointer data,
1665 const gchar *string = data;
1666 gsize first_invalid;
1668 if (!g_variant_serialiser_is_string (data, size))
1671 /* make sure no non-definite characters appear */
1672 first_invalid = strspn (string, "ybnqiuxthdvasog(){}");
1673 if (string[first_invalid])
1676 /* make sure each type string is well-formed */
1678 if (!g_variant_type_string_scan (string, NULL, &string))
1685 #define __G_VARIANT_SERIALISER_C__
1686 #include "galiasdef.c"
1688 /* vim:set foldmethod=marker: */