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_needed_size (GVariantTypeInfo *type_info,
264 GVariantSerialisedFiller gvs_filler,
265 const gpointer *children,
270 gsize element_fixed_size;
272 g_variant_type_info_query_element (type_info, NULL,
273 &element_fixed_size);
275 return element_fixed_size;
282 gvs_fixed_sized_maybe_serialise (GVariantSerialised value,
283 GVariantSerialisedFiller gvs_filler,
284 const gpointer *children,
289 GVariantSerialised child = { NULL, value.data, value.size };
291 gvs_filler (&child, children[0]);
296 gvs_fixed_sized_maybe_write_to_vectors (GVariantVectors *vectors,
297 GVariantTypeInfo *type_info,
299 const gpointer *children,
305 return g_variant_callback_write_to_vectors (vectors, children[0], NULL);
309 gvs_fixed_sized_maybe_is_normal (GVariantSerialised value)
313 gsize element_fixed_size;
315 g_variant_type_info_query_element (value.type_info,
316 NULL, &element_fixed_size);
318 if (value.size != element_fixed_size)
321 /* proper element size: "Just". recurse to the child. */
322 value.type_info = g_variant_type_info_element (value.type_info);
324 return g_variant_serialised_is_normal (value);
327 /* size of 0: "Nothing" */
331 /* Variable-sized Maybe
333 * The size of a maybe value with a variable-sized element type is
334 * either 0 or strictly greater than 0. The case where the size of the
335 * maybe value is zero corresponds to the "Nothing" case and the case
336 * where the size of the maybe value is greater than zero corresponds to
337 * the "Just" case; in that case, the serialised data of the child value
338 * forms the first part of the serialised data of the maybe value and is
339 * followed by a single zero byte. This zero byte is always appended,
340 * regardless of any zero bytes that may already be at the end of the
341 * serialised ata of the child value.
345 gvs_variable_sized_maybe_n_children (GVariantSerialised value)
347 return (value.size > 0) ? 1 : 0;
350 static GVariantSerialised
351 gvs_variable_sized_maybe_get_child (GVariantSerialised value,
354 /* remove the padding byte and update the type. */
355 value.type_info = g_variant_type_info_element (value.type_info);
356 g_variant_type_info_ref (value.type_info);
359 /* if it's zero-sized then it may as well be NULL */
367 gvs_variable_sized_maybe_needed_size (GVariantTypeInfo *type_info,
368 GVariantSerialisedFiller gvs_filler,
369 const gpointer *children,
374 GVariantSerialised child = { 0, };
376 gvs_filler (&child, children[0]);
378 return child.size + 1;
385 gvs_variable_sized_maybe_serialise (GVariantSerialised value,
386 GVariantSerialisedFiller gvs_filler,
387 const gpointer *children,
392 GVariantSerialised child = { NULL, value.data, value.size - 1 };
394 /* write the data for the child. */
395 gvs_filler (&child, children[0]);
396 value.data[child.size] = '\0';
401 gvs_variable_sized_maybe_write_to_vectors (GVariantVectors *vectors,
402 GVariantTypeInfo *type_info,
404 const gpointer *children,
409 g_variant_callback_write_to_vectors (vectors, children[0], NULL);
410 g_variant_vectors_append_copy (vectors, "", 1);
415 gvs_variable_sized_maybe_is_normal (GVariantSerialised value)
420 if (value.data[value.size - 1] != '\0')
423 value.type_info = g_variant_type_info_element (value.type_info);
426 return g_variant_serialised_is_normal (value);
431 * Just as with maybe types, array types are handled depending on if the
432 * element type of the array type is a fixed-sized or variable-sized
433 * type. Similar to maybe types, for convenience, an array value with a
434 * fixed-sized element type is called a "fixed-sized array" and an array
435 * value with a variable-sized element type is called a "variable sized
439 /* Fixed-sized Array {{{3
441 * For fixed sized arrays, the serialised data is simply a concatenation
442 * of the serialised data of each element, in order. Since fixed-sized
443 * values always have a fixed size that is a multiple of their alignment
444 * requirement no extra padding is required.
446 * In the event that a fixed-sized array is presented with a size that
447 * is not an integer multiple of the element size then the value of the
448 * array must be taken as being empty.
452 gvs_fixed_sized_array_n_children (GVariantSerialised value)
454 gsize element_fixed_size;
456 g_variant_type_info_query_element (value.type_info, NULL,
457 &element_fixed_size);
459 if (value.size % element_fixed_size == 0)
460 return value.size / element_fixed_size;
465 static GVariantSerialised
466 gvs_fixed_sized_array_get_child (GVariantSerialised value,
469 GVariantSerialised child = { 0, };
471 child.type_info = g_variant_type_info_element (value.type_info);
472 g_variant_type_info_query (child.type_info, NULL, &child.size);
473 child.data = value.data + (child.size * index_);
474 g_variant_type_info_ref (child.type_info);
480 gvs_fixed_sized_array_needed_size (GVariantTypeInfo *type_info,
481 GVariantSerialisedFiller gvs_filler,
482 const gpointer *children,
485 gsize element_fixed_size;
487 g_variant_type_info_query_element (type_info, NULL, &element_fixed_size);
489 return element_fixed_size * n_children;
493 gvs_fixed_sized_array_serialise (GVariantSerialised value,
494 GVariantSerialisedFiller gvs_filler,
495 const gpointer *children,
498 GVariantSerialised child = { 0, };
501 child.type_info = g_variant_type_info_element (value.type_info);
502 g_variant_type_info_query (child.type_info, NULL, &child.size);
503 child.data = value.data;
505 for (i = 0; i < n_children; i++)
507 gvs_filler (&child, children[i]);
508 child.data += child.size;
513 gvs_fixed_sized_array_write_to_vectors (GVariantVectors *vectors,
514 GVariantTypeInfo *type_info,
516 const gpointer *children,
521 for (i = 0; i < n_children; i++)
522 g_variant_callback_write_to_vectors (vectors, children[i], NULL);
526 gvs_fixed_sized_array_is_normal (GVariantSerialised value)
528 GVariantSerialised child = { 0, };
530 child.type_info = g_variant_type_info_element (value.type_info);
531 g_variant_type_info_query (child.type_info, NULL, &child.size);
533 if (value.size % child.size != 0)
536 for (child.data = value.data;
537 child.data < value.data + value.size;
538 child.data += child.size)
540 if (!g_variant_serialised_is_normal (child))
547 /* Variable-sized Array {{{3
549 * Variable sized arrays, containing variable-sized elements, must be
550 * able to determine the boundaries between the elements. The items
551 * cannot simply be concatenated. Additionally, we are faced with the
552 * fact that non-fixed-sized values do not necessarily have a size that
553 * is a multiple of their alignment requirement, so we may need to
554 * insert zero-filled padding.
556 * While it is possible to find the start of an item by starting from
557 * the end of the item before it and padding for alignment, it is not
558 * generally possible to do the reverse operation. For this reason, we
559 * record the end point of each element in the array.
561 * GVariant works in terms of "offsets". An offset is a pointer to a
562 * boundary between two bytes. In 4 bytes of serialised data, there
563 * would be 5 possible offsets: one at the start ('0'), one between each
564 * pair of adjacent bytes ('1', '2', '3') and one at the end ('4').
566 * The numeric value of an offset is an unsigned integer given relative
567 * to the start of the serialised data of the array. Offsets are always
568 * stored in little endian byte order and are always only as big as they
569 * need to be. For example, in 255 bytes of serialised data, there are
570 * 256 offsets. All possibilities can be stored in an 8 bit unsigned
571 * integer. In 256 bytes of serialised data, however, there are 257
572 * possible offsets so 16 bit integers must be used. The size of an
573 * offset is always a power of 2.
575 * The offsets are stored at the end of the serialised data of the
576 * array. They are simply concatenated on without any particular
577 * alignment. The size of the offsets is included in the size of the
578 * serialised data for purposes of determining the size of the offsets.
579 * This presents a possibly ambiguity; in certain cases, a particular
580 * value of array could have two different serialised forms.
582 * Imagine an array containing a single string of 253 bytes in length
583 * (so, 254 bytes including the nul terminator). Now the offset must be
584 * written. If an 8 bit offset is written, it will bring the size of
585 * the array's serialised data to 255 -- which means that the use of an
586 * 8 bit offset was valid. If a 16 bit offset is used then the total
587 * size of the array will be 256 -- which means that the use of a 16 bit
588 * offset was valid. Although both of these will be accepted by the
589 * deserialiser, only the smaller of the two is considered to be in
590 * normal form and that is the one that the serialiser must produce.
593 /* bytes may be NULL if (size == 0). */
595 gvs_read_unaligned_le (guchar *bytes,
600 guchar bytes[GLIB_SIZEOF_SIZE_T];
604 tmpvalue.integer = 0;
606 memcpy (&tmpvalue.bytes, bytes, size);
608 return GSIZE_FROM_LE (tmpvalue.integer);
612 gvs_write_unaligned_le (guchar *bytes,
618 guchar bytes[GLIB_SIZEOF_SIZE_T];
622 tmpvalue.integer = GSIZE_TO_LE (value);
623 memcpy (bytes, &tmpvalue.bytes, size);
627 gvs_get_offset_size (gsize size)
629 if (size > G_MAXUINT32)
632 else if (size > G_MAXUINT16)
635 else if (size > G_MAXUINT8)
645 gvs_calculate_total_size (gsize body_size,
648 if (body_size + 1 * offsets <= G_MAXUINT8)
649 return body_size + 1 * offsets;
651 if (body_size + 2 * offsets <= G_MAXUINT16)
652 return body_size + 2 * offsets;
654 if (body_size + 4 * offsets <= G_MAXUINT32)
655 return body_size + 4 * offsets;
657 return body_size + 8 * offsets;
661 gvs_variable_sized_array_n_children (GVariantSerialised value)
663 gsize offsets_array_size;
670 offset_size = gvs_get_offset_size (value.size);
672 last_end = gvs_read_unaligned_le (value.data + value.size -
673 offset_size, offset_size);
675 if (last_end > value.size)
678 offsets_array_size = value.size - last_end;
680 if (offsets_array_size % offset_size)
683 return offsets_array_size / offset_size;
686 static GVariantSerialised
687 gvs_variable_sized_array_get_child (GVariantSerialised value,
690 GVariantSerialised child = { 0, };
696 child.type_info = g_variant_type_info_element (value.type_info);
697 g_variant_type_info_ref (child.type_info);
699 offset_size = gvs_get_offset_size (value.size);
701 last_end = gvs_read_unaligned_le (value.data + value.size -
702 offset_size, offset_size);
708 start = gvs_read_unaligned_le (value.data + last_end +
709 (offset_size * (index_ - 1)),
712 g_variant_type_info_query (child.type_info, &alignment, NULL);
713 start += (-start) & alignment;
718 end = gvs_read_unaligned_le (value.data + last_end +
719 (offset_size * index_),
722 if (start < end && end <= value.size)
724 child.data = value.data + start;
725 child.size = end - start;
732 gvs_variable_sized_array_needed_size (GVariantTypeInfo *type_info,
733 GVariantSerialisedFiller gvs_filler,
734 const gpointer *children,
741 g_variant_type_info_query (type_info, &alignment, NULL);
744 for (i = 0; i < n_children; i++)
746 GVariantSerialised child = { 0, };
748 offset += (-offset) & alignment;
749 gvs_filler (&child, children[i]);
750 offset += child.size;
753 return gvs_calculate_total_size (offset, n_children);
757 gvs_variable_sized_array_serialise (GVariantSerialised value,
758 GVariantSerialisedFiller gvs_filler,
759 const gpointer *children,
768 g_variant_type_info_query (value.type_info, &alignment, NULL);
769 offset_size = gvs_get_offset_size (value.size);
772 offset_ptr = value.data + value.size - offset_size * n_children;
774 for (i = 0; i < n_children; i++)
776 GVariantSerialised child = { 0, };
778 while (offset & alignment)
779 value.data[offset++] = '\0';
781 child.data = value.data + offset;
782 gvs_filler (&child, children[i]);
783 offset += child.size;
785 gvs_write_unaligned_le (offset_ptr, offset, offset_size);
786 offset_ptr += offset_size;
791 gvs_variable_sized_array_write_to_vectors (GVariantVectors *vectors,
792 GVariantTypeInfo *type_info,
794 const gpointer *children,
805 offset_key = g_variant_vectors_reserve_offsets (vectors, n_children, gvs_get_offset_size (size));
806 g_variant_type_info_query (type_info, &alignment, NULL);
809 for (i = 0; i < n_children; i++)
811 if ((-offset) & alignment)
812 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
814 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
816 g_variant_vectors_write_to_offsets (vectors, i, offset, offset_key);
819 g_variant_vectors_commit_offsets (vectors, offset_key);
823 gvs_variable_sized_array_is_normal (GVariantSerialised value)
825 GVariantSerialised child = { 0, };
826 gsize offsets_array_size;
827 guchar *offsets_array;
838 offset_size = gvs_get_offset_size (value.size);
839 last_end = gvs_read_unaligned_le (value.data + value.size -
840 offset_size, offset_size);
842 if (last_end > value.size)
845 offsets_array_size = value.size - last_end;
847 if (offsets_array_size % offset_size)
850 offsets_array = value.data + value.size - offsets_array_size;
851 length = offsets_array_size / offset_size;
856 child.type_info = g_variant_type_info_element (value.type_info);
857 g_variant_type_info_query (child.type_info, &alignment, NULL);
860 for (i = 0; i < length; i++)
864 this_end = gvs_read_unaligned_le (offsets_array + offset_size * i,
867 if (this_end < offset || this_end > last_end)
870 while (offset & alignment)
872 if (!(offset < this_end && value.data[offset] == '\0'))
877 child.data = value.data + offset;
878 child.size = this_end - offset;
883 if (!g_variant_serialised_is_normal (child))
889 g_assert (offset == last_end);
896 * Since tuples can contain a mix of variable- and fixed-sized items,
897 * they are, in terms of serialisation, a hybrid of variable-sized and
898 * fixed-sized arrays.
900 * Offsets are only stored for variable-sized items. Also, since the
901 * number of items in a tuple is known from its type, we are able to
902 * know exactly how many offsets to expect in the serialised data (and
903 * therefore how much space is taken up by the offset array). This
904 * means that we know where the end of the serialised data for the last
905 * item is -- we can just subtract the size of the offset array from the
906 * total size of the tuple. For this reason, the last item in the tuple
907 * doesn't need an offset stored.
909 * Tuple offsets are stored in reverse. This design choice allows
910 * iterator-based deserialisers to be more efficient.
912 * Most of the "heavy lifting" here is handled by the GVariantTypeInfo
913 * for the tuple. See the notes in gvarianttypeinfo.h.
917 gvs_tuple_n_children (GVariantSerialised value)
919 return g_variant_type_info_n_members (value.type_info);
922 static GVariantSerialised
923 gvs_tuple_get_child (GVariantSerialised value,
926 const GVariantMemberInfo *member_info;
927 GVariantSerialised child = { 0, };
931 member_info = g_variant_type_info_member_info (value.type_info, index_);
932 child.type_info = g_variant_type_info_ref (member_info->type_info);
933 offset_size = gvs_get_offset_size (value.size);
935 /* tuples are the only (potentially) fixed-sized containers, so the
936 * only ones that have to deal with the possibility of having %NULL
937 * data with a non-zero %size if errors occurred elsewhere.
939 if G_UNLIKELY (value.data == NULL && value.size != 0)
941 g_variant_type_info_query (child.type_info, NULL, &child.size);
943 /* this can only happen in fixed-sized tuples,
944 * so the child must also be fixed sized.
946 g_assert (child.size != 0);
952 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
954 if (offset_size * (member_info->i + 2) > value.size)
959 if (offset_size * (member_info->i + 1) > value.size)
961 /* if the child is fixed size, return its size.
962 * if child is not fixed-sized, return size = 0.
964 g_variant_type_info_query (child.type_info, NULL, &child.size);
970 if (member_info->i + 1)
971 start = gvs_read_unaligned_le (value.data + value.size -
972 offset_size * (member_info->i + 1),
977 start += member_info->a;
978 start &= member_info->b;
979 start |= member_info->c;
981 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_LAST)
982 end = value.size - offset_size * (member_info->i + 1);
984 else if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_FIXED)
988 g_variant_type_info_query (child.type_info, NULL, &fixed_size);
989 end = start + fixed_size;
990 child.size = fixed_size;
993 else /* G_VARIANT_MEMBER_ENDING_OFFSET */
994 end = gvs_read_unaligned_le (value.data + value.size -
995 offset_size * (member_info->i + 2),
998 if (start < end && end <= value.size)
1000 child.data = value.data + start;
1001 child.size = end - start;
1008 gvs_tuple_needed_size (GVariantTypeInfo *type_info,
1009 GVariantSerialisedFiller gvs_filler,
1010 const gpointer *children,
1013 const GVariantMemberInfo *member_info = NULL;
1018 g_variant_type_info_query (type_info, NULL, &fixed_size);
1025 for (i = 0; i < n_children; i++)
1029 member_info = g_variant_type_info_member_info (type_info, i);
1030 g_variant_type_info_query (member_info->type_info,
1031 &alignment, &fixed_size);
1032 offset += (-offset) & alignment;
1035 offset += fixed_size;
1038 GVariantSerialised child = { 0, };
1040 gvs_filler (&child, children[i]);
1041 offset += child.size;
1045 return gvs_calculate_total_size (offset, member_info->i + 1);
1049 gvs_tuple_serialise (GVariantSerialised value,
1050 GVariantSerialisedFiller gvs_filler,
1051 const gpointer *children,
1058 offset_size = gvs_get_offset_size (value.size);
1061 for (i = 0; i < n_children; i++)
1063 const GVariantMemberInfo *member_info;
1064 GVariantSerialised child = { 0, };
1067 member_info = g_variant_type_info_member_info (value.type_info, i);
1068 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1070 while (offset & alignment)
1071 value.data[offset++] = '\0';
1073 child.data = value.data + offset;
1074 gvs_filler (&child, children[i]);
1075 offset += child.size;
1077 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
1079 value.size -= offset_size;
1080 gvs_write_unaligned_le (value.data + value.size,
1081 offset, offset_size);
1085 while (offset < value.size)
1086 value.data[offset++] = '\0';
1091 gvs_tuple_write_to_vectors (GVariantVectors *vectors,
1092 GVariantTypeInfo *type_info,
1094 const gpointer *children,
1097 const GVariantMemberInfo *member_info = NULL;
1102 if (n_children == 0)
1104 g_variant_vectors_append_copy (vectors, "", 1);
1108 g_variant_type_info_query (type_info, NULL, &fixed_size);
1115 member_info = g_variant_type_info_member_info (type_info, n_children - 1);
1116 n_offsets = member_info->i + 1;
1120 gsize offset_key = 0;
1122 offset_key = g_variant_vectors_reserve_offsets (vectors, n_offsets, gvs_get_offset_size (size));
1124 for (i = 0; i < n_children; i++)
1128 member_info = g_variant_type_info_member_info (type_info, i);
1129 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1131 if ((-offset) & alignment)
1132 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
1134 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
1136 if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
1137 g_variant_vectors_write_to_offsets (vectors, --n_offsets, offset, offset_key);
1140 g_variant_vectors_commit_offsets (vectors, offset_key);
1144 for (i = 0; i < n_children; i++)
1148 member_info = g_variant_type_info_member_info (type_info, i);
1149 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1151 if ((-offset) & alignment)
1152 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
1154 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
1160 for (i = 0; i < n_children; i++)
1164 member_info = g_variant_type_info_member_info (type_info, i);
1165 g_variant_type_info_query (member_info->type_info, &alignment, NULL);
1167 if ((-offset) & alignment)
1168 offset += g_variant_vectors_append_pad (vectors, (-offset) & alignment);
1170 offset += g_variant_callback_write_to_vectors (vectors, children[i], NULL);
1173 g_assert (fixed_size - offset < 8);
1174 g_variant_vectors_append_pad (vectors, fixed_size - offset);
1179 gvs_tuple_is_normal (GVariantSerialised value)
1187 /* as per the comment in gvs_tuple_get_child() */
1188 if G_UNLIKELY (value.data == NULL && value.size != 0)
1191 offset_size = gvs_get_offset_size (value.size);
1192 length = g_variant_type_info_n_members (value.type_info);
1193 offset_ptr = value.size;
1196 for (i = 0; i < length; i++)
1198 const GVariantMemberInfo *member_info;
1199 GVariantSerialised child;
1204 member_info = g_variant_type_info_member_info (value.type_info, i);
1205 child.type_info = member_info->type_info;
1207 g_variant_type_info_query (child.type_info, &alignment, &fixed_size);
1209 while (offset & alignment)
1211 if (offset > value.size || value.data[offset] != '\0')
1216 child.data = value.data + offset;
1218 switch (member_info->ending_type)
1220 case G_VARIANT_MEMBER_ENDING_FIXED:
1221 end = offset + fixed_size;
1224 case G_VARIANT_MEMBER_ENDING_LAST:
1228 case G_VARIANT_MEMBER_ENDING_OFFSET:
1229 offset_ptr -= offset_size;
1231 if (offset_ptr < offset)
1234 end = gvs_read_unaligned_le (value.data + offset_ptr, offset_size);
1238 g_assert_not_reached ();
1241 if (end < offset || end > offset_ptr)
1244 child.size = end - offset;
1246 if (child.size == 0)
1249 if (!g_variant_serialised_is_normal (child))
1259 g_variant_type_info_query (value.type_info, &alignment, &fixed_size);
1263 g_assert (fixed_size == value.size);
1264 g_assert (offset_ptr == value.size);
1268 if (value.data[offset++] != '\0')
1273 while (offset & alignment)
1274 if (value.data[offset++] != '\0')
1278 g_assert (offset == value.size);
1282 return offset_ptr == offset;
1287 * Variants are stored by storing the serialised data of the child,
1288 * followed by a '\0' character, followed by the type string of the
1291 * In the case that a value is presented that contains no '\0'
1292 * character, or doesn't have a single well-formed definite type string
1293 * following that character, the variant must be taken as containing the
1298 gvs_variant_n_children (GVariantSerialised value)
1303 static inline GVariantSerialised
1304 gvs_variant_get_child (GVariantSerialised value,
1307 GVariantSerialised child = { 0, };
1309 /* NOTE: not O(1) and impossible for it to be... */
1312 /* find '\0' character */
1313 for (child.size = value.size - 1; child.size; child.size--)
1314 if (value.data[child.size] == '\0')
1317 /* ensure we didn't just hit the start of the string */
1318 if (value.data[child.size] == '\0')
1320 const gchar *type_string = (gchar *) &value.data[child.size + 1];
1321 const gchar *limit = (gchar *) &value.data[value.size];
1324 if (g_variant_type_string_scan (type_string, limit, &end) &&
1327 const GVariantType *type = (GVariantType *) type_string;
1329 if (g_variant_type_is_definite (type))
1333 child.type_info = g_variant_type_info_get (type);
1335 if (child.size != 0)
1336 /* only set to non-%NULL if size > 0 */
1337 child.data = value.data;
1339 g_variant_type_info_query (child.type_info,
1342 if (!fixed_size || fixed_size == child.size)
1345 g_variant_type_info_unref (child.type_info);
1351 child.type_info = g_variant_type_info_get (G_VARIANT_TYPE_UNIT);
1359 gvs_variant_needed_size (GVariantTypeInfo *type_info,
1360 GVariantSerialisedFiller gvs_filler,
1361 const gpointer *children,
1364 GVariantSerialised child = { 0, };
1365 const gchar *type_string;
1367 gvs_filler (&child, children[0]);
1368 type_string = g_variant_type_info_get_type_string (child.type_info);
1370 return child.size + 1 + strlen (type_string);
1374 gvs_variant_serialise (GVariantSerialised value,
1375 GVariantSerialisedFiller gvs_filler,
1376 const gpointer *children,
1379 GVariantSerialised child = { 0, };
1380 const gchar *type_string;
1382 child.data = value.data;
1384 gvs_filler (&child, children[0]);
1385 type_string = g_variant_type_info_get_type_string (child.type_info);
1386 value.data[child.size] = '\0';
1387 memcpy (value.data + child.size + 1, type_string, strlen (type_string));
1391 gvs_variant_write_to_vectors (GVariantVectors *vectors,
1392 GVariantTypeInfo *type_info,
1394 const gpointer *children,
1397 GVariantTypeInfo *child_type_info;
1398 const gchar *type_string;
1400 g_variant_callback_write_to_vectors (vectors, children[0], &child_type_info);
1401 type_string = g_variant_type_info_get_type_string (child_type_info);
1403 g_variant_vectors_append_copy (vectors, "", 1);
1404 g_variant_vectors_append_copy (vectors, type_string, strlen (type_string));
1407 static inline gboolean
1408 gvs_variant_is_normal (GVariantSerialised value)
1410 GVariantSerialised child;
1413 child = gvs_variant_get_child (value, 0);
1415 normal = (child.data != NULL || child.size == 0) &&
1416 g_variant_serialised_is_normal (child);
1418 g_variant_type_info_unref (child.type_info);
1425 /* PART 2: Serialiser API {{{1
1427 * This is the implementation of the API of the serialiser as advertised
1428 * in gvariant-serialiser.h.
1431 /* Dispatch Utilities {{{2
1433 * These macros allow a given function (for example,
1434 * g_variant_serialiser_serialise) to be dispatched to the appropriate
1435 * type-specific function above (fixed/variable-sized maybe,
1436 * fixed/variable-sized array, tuple or variant).
1438 #define DISPATCH_FIXED(type_info, before, after) \
1442 g_variant_type_info_query_element (type_info, NULL, \
1447 before ## fixed_sized ## after \
1451 before ## variable_sized ## after \
1455 #define DISPATCH_CASES(type_info, before, after) \
1456 switch (g_variant_type_info_get_type_char (type_info)) \
1458 case G_VARIANT_TYPE_INFO_CHAR_MAYBE: \
1459 DISPATCH_FIXED (type_info, before, _maybe ## after) \
1461 case G_VARIANT_TYPE_INFO_CHAR_ARRAY: \
1462 DISPATCH_FIXED (type_info, before, _array ## after) \
1464 case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY: \
1465 case G_VARIANT_TYPE_INFO_CHAR_TUPLE: \
1467 before ## tuple ## after \
1470 case G_VARIANT_TYPE_INFO_CHAR_VARIANT: \
1472 before ## variant ## after \
1476 /* Serialiser entry points {{{2
1478 * These are the functions that are called in order for the serialiser
1483 * g_variant_serialised_n_children:
1484 * @serialised: a #GVariantSerialised
1486 * For serialised data that represents a container value (maybes,
1487 * tuples, arrays, variants), determine how many child items are inside
1490 * Returns: the number of children
1493 g_variant_serialised_n_children (GVariantSerialised serialised)
1495 g_variant_serialised_check (serialised);
1497 DISPATCH_CASES (serialised.type_info,
1499 return gvs_/**/,/**/_n_children (serialised);
1502 g_assert_not_reached ();
1506 * g_variant_serialised_get_child:
1507 * @serialised: a #GVariantSerialised
1508 * @index_: the index of the child to fetch
1510 * Extracts a child from a serialised data representing a container
1513 * It is an error to call this function with an index out of bounds.
1515 * If the result .data == %NULL and .size > 0 then there has been an
1516 * error extracting the requested fixed-sized value. This number of
1517 * zero bytes needs to be allocated instead.
1519 * In the case that .data == %NULL and .size == 0 then a zero-sized
1520 * item of a variable-sized type is being returned.
1522 * .data is never non-%NULL if size is 0.
1524 * Returns: a #GVariantSerialised for the child
1527 g_variant_serialised_get_child (GVariantSerialised serialised,
1530 GVariantSerialised child;
1532 g_variant_serialised_check (serialised);
1534 if G_LIKELY (index_ < g_variant_serialised_n_children (serialised))
1536 DISPATCH_CASES (serialised.type_info,
1538 child = gvs_/**/,/**/_get_child (serialised, index_);
1539 g_assert (child.size || child.data == NULL);
1540 g_variant_serialised_check (child);
1544 g_assert_not_reached ();
1547 g_error ("Attempt to access item %"G_GSIZE_FORMAT
1548 " in a container with only %"G_GSIZE_FORMAT" items",
1549 index_, g_variant_serialised_n_children (serialised));
1553 * g_variant_serialiser_serialise:
1554 * @serialised: a #GVariantSerialised, properly set up
1555 * @gvs_filler: the filler function
1556 * @children: an array of child items
1557 * @n_children: the size of @children
1559 * Writes data in serialised form.
1561 * The type_info field of @serialised must be filled in to type info for
1562 * the type that we are serialising.
1564 * The size field of @serialised must be filled in with the value
1565 * returned by a previous call to g_variant_serialiser_needed_size().
1567 * The data field of @serialised must be a pointer to a properly-aligned
1568 * memory region large enough to serialise into (ie: at least as big as
1571 * This function is only resonsible for serialising the top-level
1572 * container. @gvs_filler is called on each child of the container in
1573 * order for all of the data of that child to be filled in.
1576 g_variant_serialiser_serialise (GVariantSerialised serialised,
1577 GVariantSerialisedFiller gvs_filler,
1578 const gpointer *children,
1581 g_variant_serialised_check (serialised);
1583 DISPATCH_CASES (serialised.type_info,
1585 gvs_/**/,/**/_serialise (serialised, gvs_filler,
1586 children, n_children);
1590 g_assert_not_reached ();
1594 * g_variant_serialiser_needed_size:
1595 * @type_info: the type to serialise for
1596 * @gvs_filler: the filler function
1597 * @children: an array of child items
1598 * @n_children: the size of @children
1600 * Determines how much memory would be needed to serialise this value.
1602 * This function is only resonsible for performing calculations for the
1603 * top-level container. @gvs_filler is called on each child of the
1604 * container in order to determine its size.
1607 g_variant_serialiser_needed_size (GVariantTypeInfo *type_info,
1608 GVariantSerialisedFiller gvs_filler,
1609 const gpointer *children,
1612 DISPATCH_CASES (type_info,
1614 return gvs_/**/,/**/_needed_size (type_info, gvs_filler,
1615 children, n_children);
1617 g_assert_not_reached ();
1622 g_variant_serialiser_write_to_vectors (GVariantVectors *vectors,
1623 GVariantTypeInfo *type_info,
1625 const gpointer *children,
1628 DISPATCH_CASES (type_info,
1629 gvs_/**/,/**/_write_to_vectors (vectors, type_info, size, children, n_children);
1632 g_assert_not_reached ();
1635 /* Byteswapping {{{2 */
1638 * g_variant_serialised_byteswap:
1639 * @value: a #GVariantSerialised
1641 * Byte-swap serialised data. The result of this function is only
1642 * well-defined if the data is in normal form.
1645 g_variant_serialised_byteswap (GVariantSerialised serialised)
1650 g_variant_serialised_check (serialised);
1652 if (!serialised.data)
1655 /* the types we potentially need to byteswap are
1656 * exactly those with alignment requirements.
1658 g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
1662 /* if fixed size and alignment are equal then we are down
1663 * to the base integer type and we should swap it. the
1664 * only exception to this is if we have a tuple with a
1665 * single item, and then swapping it will be OK anyway.
1667 if (alignment + 1 == fixed_size)
1673 guint16 *ptr = (guint16 *) serialised.data;
1675 g_assert_cmpint (serialised.size, ==, 2);
1676 *ptr = GUINT16_SWAP_LE_BE (*ptr);
1682 guint32 *ptr = (guint32 *) serialised.data;
1684 g_assert_cmpint (serialised.size, ==, 4);
1685 *ptr = GUINT32_SWAP_LE_BE (*ptr);
1691 guint64 *ptr = (guint64 *) serialised.data;
1693 g_assert_cmpint (serialised.size, ==, 8);
1694 *ptr = GUINT64_SWAP_LE_BE (*ptr);
1699 g_assert_not_reached ();
1703 /* else, we have a container that potentially contains
1704 * some children that need to be byteswapped.
1710 children = g_variant_serialised_n_children (serialised);
1711 for (i = 0; i < children; i++)
1713 GVariantSerialised child;
1715 child = g_variant_serialised_get_child (serialised, i);
1716 g_variant_serialised_byteswap (child);
1717 g_variant_type_info_unref (child.type_info);
1722 /* Normal form checking {{{2 */
1725 * g_variant_serialised_is_normal:
1726 * @serialised: a #GVariantSerialised
1728 * Determines, recursively if @serialised is in normal form. There is
1729 * precisely one normal form of serialised data for each possible value.
1731 * It is possible that multiple byte sequences form the serialised data
1732 * for a given value if, for example, the padding bytes are filled in
1733 * with something other than zeros, but only one form is the normal
1737 g_variant_serialised_is_normal (GVariantSerialised serialised)
1739 DISPATCH_CASES (serialised.type_info,
1741 return gvs_/**/,/**/_is_normal (serialised);
1745 if (serialised.data == NULL)
1748 /* some hard-coded terminal cases */
1749 switch (g_variant_type_info_get_type_char (serialised.type_info))
1751 case 'b': /* boolean */
1752 return serialised.data[0] < 2;
1754 case 's': /* string */
1755 return g_variant_serialiser_is_string (serialised.data,
1759 return g_variant_serialiser_is_object_path (serialised.data,
1763 return g_variant_serialiser_is_signature (serialised.data,
1767 /* all of the other types are fixed-sized numerical types for
1768 * which all possible values are valid (including various NaN
1769 * representations for floating point values).
1775 /* Validity-checking functions {{{2
1777 * Checks if strings, object paths and signature strings are valid.
1781 * g_variant_serialiser_is_string:
1782 * @data: a possible string
1783 * @size: the size of @data
1785 * Ensures that @data is a valid string with a nul terminator at the end
1786 * and no nul bytes embedded.
1789 g_variant_serialiser_is_string (gconstpointer data,
1792 const gchar *expected_end;
1798 expected_end = ((gchar *) data) + size - 1;
1800 if (*expected_end != '\0')
1803 g_utf8_validate (data, size, &end);
1805 return end == expected_end;
1809 * g_variant_serialiser_is_object_path:
1810 * @data: a possible D-Bus object path
1811 * @size: the size of @data
1813 * Performs the checks for being a valid string.
1815 * Also, ensures that @data is a valid DBus object path, as per the D-Bus
1819 g_variant_serialiser_is_object_path (gconstpointer data,
1822 const gchar *string = data;
1825 if (!g_variant_serialiser_is_string (data, size))
1828 /* The path must begin with an ASCII '/' (integer 47) character */
1829 if (string[0] != '/')
1832 for (i = 1; string[i]; i++)
1833 /* Each element must only contain the ASCII characters
1834 * "[A-Z][a-z][0-9]_"
1836 if (g_ascii_isalnum (string[i]) || string[i] == '_')
1839 /* must consist of elements separated by slash characters. */
1840 else if (string[i] == '/')
1842 /* No element may be the empty string. */
1843 /* Multiple '/' characters cannot occur in sequence. */
1844 if (string[i - 1] == '/')
1851 /* A trailing '/' character is not allowed unless the path is the
1852 * root path (a single '/' character).
1854 if (i > 1 && string[i - 1] == '/')
1861 * g_variant_serialiser_is_signature:
1862 * @data: a possible D-Bus signature
1863 * @size: the size of @data
1865 * Performs the checks for being a valid string.
1867 * Also, ensures that @data is a valid D-Bus type signature, as per the
1868 * D-Bus specification.
1871 g_variant_serialiser_is_signature (gconstpointer data,
1874 const gchar *string = data;
1875 gsize first_invalid;
1877 if (!g_variant_serialiser_is_string (data, size))
1880 /* make sure no non-definite characters appear */
1881 first_invalid = strspn (string, "ybnqiuxthdvasog(){}");
1882 if (string[first_invalid])
1885 /* make sure each type string is well-formed */
1887 if (!g_variant_type_string_scan (string, NULL, &string))
1894 /* vim:set foldmethod=marker: */