merge the GVariant serialiser
[platform/upstream/glib.git] / glib / gvariant-serialiser.c
1 /*
2  * Copyright © 2007, 2008 Ryan Lortie
3  * Copyright © 2010 Codethink Limited
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Ryan Lortie <desrt@desrt.ca>
21  */
22
23 /* Prologue {{{1 */
24 #include "gvariant-serialiser.h"
25
26 #include <glib/gtestutils.h>
27 #include <glib/gstrfuncs.h>
28 #include <glib/gtypes.h>
29
30 #include <string.h>
31
32 #include "galias.h"
33
34 /* GVariantSerialiser
35  *
36  * After this prologue section, this file has roughly 2 parts.
37  *
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.
42  *
43  * Each section documents the format of that particular type of
44  * container and implements 5 functions for dealing with it:
45  *
46  *  n_children:
47  *    - determines (according to serialised data) how many child values
48  *      are inside a particular container value.
49  *
50  *  get_child:
51  *    - gets the type of and the serialised data corresponding to a
52  *      given child value within the container value.
53  *
54  *  needed_size:
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.
58  *
59  *  serialise:
60  *    - write the serialised data for a container of this type,
61  *      containing the given children, to a buffer.
62  *
63  *  is_normal:
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
69  *      the normal form.
70  *
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.
74  *
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.
79  *
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).
86  */
87
88 /* < private >
89  * GVariantSerialised:
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
93  *
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.
98  *
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.
102  *
103  * @type_info must be non-%NULL.
104  *
105  * @data must be properly aligned for the type described by @type_info.
106  *
107  * If @type_info describes a fixed-sized type then @size must always be
108  * equal to the fixed size of that type.
109  *
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.
117  */
118
119 /* < private >
120  * g_variant_serialised_check:
121  * @serialised: a #GVariantSerialised struct
122  *
123  * Checks @serialised for validity according to the invariants described
124  * above.
125  */
126 static void
127 g_variant_serialised_check (GVariantSerialised serialised)
128 {
129   gsize fixed_size;
130   guint alignment;
131
132   g_assert (serialised.type_info != NULL);
133   g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
134
135   if (fixed_size)
136     g_assert_cmpint (serialised.size, ==, fixed_size);
137   else
138     g_assert (serialised.size == 0 || serialised.data != NULL);
139
140   g_assert_cmpint (alignment & (gsize) serialised.data, ==, 0);
141 }
142
143 /* < private >
144  * GVariantSerialisedFiller:
145  * @serialised: a #GVariantSerialised instance to fill
146  * @data: data from the children array
147  *
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.
151  *
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.
157  *
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
162  * of the child.
163  *
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.
168  *
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
172  * @data.
173  *
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.
178  */
179
180 /* PART 1: Container types {{{1
181  *
182  * This section contains the serialiser implementation functions for
183  * each container type.
184  */
185
186 /* Maybe {{{2
187  *
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".
194  */
195
196 /* Fixed-sized Maybe {{{3
197  *
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.
205  *
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".
209  */
210
211 static gsize
212 gvs_fixed_sized_maybe_n_children (GVariantSerialised value)
213 {
214   gsize element_fixed_size;
215
216   g_variant_type_info_query_element (value.type_info, NULL,
217                                      &element_fixed_size);
218
219   return (element_fixed_size == value.size) ? 1 : 0;
220 }
221
222 static GVariantSerialised
223 gvs_fixed_sized_maybe_get_child (GVariantSerialised value,
224                                  gsize              index_)
225 {
226   /* the child has the same bounds as the
227    * container, so just update the type.
228    */
229   value.type_info = g_variant_type_info_element (value.type_info);
230   g_variant_type_info_ref (value.type_info);
231
232   return value;
233 }
234
235 static gsize
236 gvs_fixed_sized_maybe_needed_size (GVariantTypeInfo         *type_info,
237                                    GVariantSerialisedFiller  gvs_filler,
238                                    const gpointer           *children,
239                                    gsize                     n_children)
240 {
241   if (n_children)
242     {
243       gsize element_fixed_size;
244
245       g_variant_type_info_query_element (type_info, NULL,
246                                          &element_fixed_size);
247
248       return element_fixed_size;
249     }
250   else
251     return 0;
252 }
253
254 static void
255 gvs_fixed_sized_maybe_serialise (GVariantSerialised        value,
256                                  GVariantSerialisedFiller  gvs_filler,
257                                  const gpointer           *children,
258                                  gsize                     n_children)
259 {
260   if (n_children)
261     {
262       GVariantSerialised child = { NULL, value.data, value.size };
263
264       gvs_filler (&child, children[0]);
265     }
266 }
267
268 static gboolean
269 gvs_fixed_sized_maybe_is_normal (GVariantSerialised value)
270 {
271   if (value.size > 0)
272     {
273       gsize element_fixed_size;
274
275       g_variant_type_info_query_element (value.type_info,
276                                          NULL, &element_fixed_size);
277
278       if (value.size != element_fixed_size)
279         return FALSE;
280
281       /* proper element size: "Just".  recurse to the child. */
282       value.type_info = g_variant_type_info_element (value.type_info);
283
284       return g_variant_serialised_is_normal (value);
285     }
286
287   /* size of 0: "Nothing" */
288   return TRUE;
289 }
290
291 /* Variable-sized Maybe
292  *
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.
302  */
303
304 static gsize
305 gvs_variable_sized_maybe_n_children (GVariantSerialised value)
306 {
307   return (value.size > 0) ? 1 : 0;
308 }
309
310 static GVariantSerialised
311 gvs_variable_sized_maybe_get_child (GVariantSerialised value,
312                                     gsize              index_)
313 {
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);
317   value.size--;
318
319   /* if it's zero-sized then it may as well be NULL */
320   if (value.size == 0)
321     value.data = NULL;
322
323   return value;
324 }
325
326 static gsize
327 gvs_variable_sized_maybe_needed_size (GVariantTypeInfo         *type_info,
328                                       GVariantSerialisedFiller  gvs_filler,
329                                       const gpointer           *children,
330                                       gsize                     n_children)
331 {
332   if (n_children)
333     {
334       GVariantSerialised child = {  };
335
336       gvs_filler (&child, children[0]);
337
338       return child.size + 1;
339     }
340   else
341     return 0;
342 }
343
344 static void
345 gvs_variable_sized_maybe_serialise (GVariantSerialised        value,
346                                     GVariantSerialisedFiller  gvs_filler,
347                                     const gpointer           *children,
348                                     gsize                     n_children)
349 {
350   if (n_children)
351     {
352       GVariantSerialised child = { NULL, value.data, value.size - 1 };
353
354       /* write the data for the child.  */
355       gvs_filler (&child, children[0]);
356       value.data[child.size] = '\0';
357     }
358 }
359
360 static gboolean
361 gvs_variable_sized_maybe_is_normal (GVariantSerialised value)
362 {
363   if (value.size == 0)
364     return TRUE;
365
366   if (value.data[value.size - 1] != '\0')
367     return FALSE;
368
369   value.type_info = g_variant_type_info_element (value.type_info);
370   value.size--;
371
372   return g_variant_serialised_is_normal (value);
373 }
374
375 /* Arrays {{{2
376  *
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
382  * array".
383  */
384
385 /* Fixed-sized Array {{{3
386  *
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.
391  *
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.
395  */
396
397 static gsize
398 gvs_fixed_sized_array_n_children (GVariantSerialised value)
399 {
400   gsize element_fixed_size;
401
402   g_variant_type_info_query_element (value.type_info, NULL,
403                                      &element_fixed_size);
404
405   if (value.size % element_fixed_size == 0)
406     return value.size / element_fixed_size;
407
408   return 0;
409 }
410
411 static GVariantSerialised
412 gvs_fixed_sized_array_get_child (GVariantSerialised value,
413                                  gsize              index_)
414 {
415   GVariantSerialised child = {  };
416
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);
421
422   return child;
423 }
424
425 static gsize
426 gvs_fixed_sized_array_needed_size (GVariantTypeInfo         *type_info,
427                                    GVariantSerialisedFiller  gvs_filler,
428                                    const gpointer           *children,
429                                    gsize                     n_children)
430 {
431   gsize element_fixed_size;
432
433   g_variant_type_info_query_element (type_info, NULL, &element_fixed_size);
434
435   return element_fixed_size * n_children;
436 }
437
438 static void
439 gvs_fixed_sized_array_serialise (GVariantSerialised        value,
440                                  GVariantSerialisedFiller  gvs_filler,
441                                  const gpointer           *children,
442                                  gsize                     n_children)
443 {
444   GVariantSerialised child = {  };
445   gsize i;
446
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;
450
451   for (i = 0; i < n_children; i++)
452     {
453       gvs_filler (&child, children[i]);
454       child.data += child.size;
455     }
456 }
457
458 static gboolean
459 gvs_fixed_sized_array_is_normal (GVariantSerialised value)
460 {
461   GVariantSerialised child = {  };
462
463   child.type_info = g_variant_type_info_element (value.type_info);
464   g_variant_type_info_query (child.type_info, NULL, &child.size);
465
466   if (value.size % child.size != 0)
467     return FALSE;
468
469   for (child.data = value.data;
470        child.data < value.data + value.size;
471        child.data += child.size)
472     {
473       if (!g_variant_serialised_is_normal (child))
474         return FALSE;
475     }
476
477   return TRUE;
478 }
479
480 /* Variable-sized Array {{{3
481  *
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.
488  *
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.
493  *
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').
498  *
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.
507  *
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.
514  *
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.
524  */
525
526 static inline gsize
527 gvs_read_unaligned_le (guchar *bytes,
528                        guint   size)
529 {
530   union
531   {
532     guchar bytes[GLIB_SIZEOF_SIZE_T];
533     gsize integer;
534   } tmpvalue;
535
536   tmpvalue.integer = 0;
537   memcpy (&tmpvalue.bytes, bytes, size);
538
539   return GSIZE_FROM_LE (tmpvalue.integer);
540 }
541
542 static inline void
543 gvs_write_unaligned_le (guchar *bytes,
544                         gsize   value,
545                         guint   size)
546 {
547   union
548   {
549     guchar bytes[GLIB_SIZEOF_SIZE_T];
550     gsize integer;
551   } tmpvalue;
552
553   tmpvalue.integer = GSIZE_TO_LE (value);
554   memcpy (bytes, &tmpvalue.bytes, size);
555 }
556
557 static guint
558 gvs_get_offset_size (gsize size)
559 {
560   if (size > G_MAXUINT32)
561     return 8;
562
563   else if (size > G_MAXUINT16)
564     return 4;
565
566   else if (size > G_MAXUINT8)
567     return 2;
568
569   else if (size > 0)
570     return 1;
571
572   return 0;
573 }
574
575 static gsize
576 gvs_calculate_total_size (gsize body_size,
577                           gsize offsets)
578 {
579   if (body_size + 1 * offsets <= G_MAXUINT8)
580     return body_size + 1 * offsets;
581
582   if (body_size + 2 * offsets <= G_MAXUINT16)
583     return body_size + 2 * offsets;
584
585   if (body_size + 4 * offsets <= G_MAXUINT32)
586     return body_size + 4 * offsets;
587
588   return body_size + 8 * offsets;
589 }
590
591 static gsize
592 gvs_variable_sized_array_n_children (GVariantSerialised value)
593 {
594   gsize offsets_array_size;
595   gsize offset_size;
596   gsize last_end;
597
598   if (value.size == 0)
599     return 0;
600
601   offset_size = gvs_get_offset_size (value.size);
602
603   last_end = gvs_read_unaligned_le (value.data + value.size -
604                                     offset_size, offset_size);
605
606   if (last_end > value.size)
607     return 0;
608
609   offsets_array_size = value.size - last_end;
610
611   if (offsets_array_size % offset_size)
612     return 0;
613
614   return offsets_array_size / offset_size;
615 }
616
617 static GVariantSerialised
618 gvs_variable_sized_array_get_child (GVariantSerialised value,
619                                     gsize              index_)
620 {
621   GVariantSerialised child = {  };
622   gsize offset_size;
623   gsize last_end;
624   gsize start;
625   gsize end;
626
627   child.type_info = g_variant_type_info_element (value.type_info);
628   g_variant_type_info_ref (child.type_info);
629
630   offset_size = gvs_get_offset_size (value.size);
631
632   last_end = gvs_read_unaligned_le (value.data + value.size -
633                                     offset_size, offset_size);
634
635   if (index_ > 0)
636     {
637       guint alignment;
638
639       start = gvs_read_unaligned_le (value.data + last_end +
640                                      (offset_size * (index_ - 1)),
641                                      offset_size);
642
643       g_variant_type_info_query (child.type_info, &alignment, NULL);
644       start += (-start) & alignment;
645     }
646   else
647     start = 0;
648
649   end = gvs_read_unaligned_le (value.data + last_end +
650                                (offset_size * index_),
651                                offset_size);
652
653   if (start < end && end <= value.size)
654     {
655       child.data = value.data + start;
656       child.size = end - start;
657     }
658
659   return child;
660 }
661
662 static gsize
663 gvs_variable_sized_array_needed_size (GVariantTypeInfo         *type_info,
664                                       GVariantSerialisedFiller  gvs_filler,
665                                       const gpointer           *children,
666                                       gsize                     n_children)
667 {
668   guint alignment;
669   gsize offset;
670   gsize i;
671
672   g_variant_type_info_query (type_info, &alignment, NULL);
673   offset = 0;
674
675   for (i = 0; i < n_children; i++)
676     {
677       GVariantSerialised child = {  };
678
679       offset += (-offset) & alignment;
680       gvs_filler (&child, children[i]);
681       offset += child.size;
682     }
683
684   return gvs_calculate_total_size (offset, n_children);
685 }
686
687 static void
688 gvs_variable_sized_array_serialise (GVariantSerialised        value,
689                                     GVariantSerialisedFiller  gvs_filler,
690                                     const gpointer           *children,
691                                     gsize                     n_children)
692 {
693   guchar *offset_ptr;
694   gsize offset_size;
695   guint alignment;
696   gsize offset;
697   gsize i;
698
699   g_variant_type_info_query (value.type_info, &alignment, NULL);
700   offset_size = gvs_get_offset_size (value.size);
701   offset = 0;
702
703   offset_ptr = value.data + value.size - offset_size * n_children;
704
705   for (i = 0; i < n_children; i++)
706     {
707       GVariantSerialised child = {  };
708
709       while (offset & alignment)
710         value.data[offset++] = '\0';
711
712       child.data = value.data + offset;
713       gvs_filler (&child, children[i]);
714       offset += child.size;
715
716       gvs_write_unaligned_le (offset_ptr, offset, offset_size);
717       offset_ptr += offset_size;
718     }
719 }
720
721 static gboolean
722 gvs_variable_sized_array_is_normal (GVariantSerialised value)
723 {
724   GVariantSerialised child = {  };
725   gsize offsets_array_size;
726   guchar *offsets_array;
727   guint offset_size;
728   guint alignment;
729   gsize last_end;
730   gsize length;
731   gsize offset;
732   gsize i;
733
734   if (value.size == 0)
735     return TRUE;
736
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);
740
741   if (last_end > value.size)
742     return FALSE;
743
744   offsets_array_size = value.size - last_end;
745
746   if (offsets_array_size % offset_size)
747     return FALSE;
748
749   offsets_array = value.data + value.size - offsets_array_size;
750   length = offsets_array_size / offset_size;
751
752   if (length == 0)
753     return FALSE;
754
755   child.type_info = g_variant_type_info_element (value.type_info);
756   g_variant_type_info_query (child.type_info, &alignment, NULL);
757   offset = 0;
758
759   for (i = 0; i < length; i++)
760     {
761       gsize this_end;
762
763       this_end = gvs_read_unaligned_le (offsets_array + offset_size * i,
764                                         offset_size);
765
766       if (this_end < offset || this_end > last_end)
767         return FALSE;
768
769       while (offset & alignment)
770         {
771           if (!(offset < this_end && value.data[offset] == '\0'))
772             return FALSE;
773           offset++;
774         }
775
776       child.data = value.data + offset;
777       child.size = this_end - offset;
778
779       if (child.size == 0)
780         child.data = NULL;
781
782       if (!g_variant_serialised_is_normal (child))
783         return FALSE;
784
785       offset = this_end;
786     }
787
788   g_assert (offset == last_end);
789
790   return TRUE;
791 }
792
793 /* Tuples {{{2
794  *
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.
798  *
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.
807  *
808  * Tuple offsets are stored in reverse.  This design choice allows
809  * iterator-based deserialisers to be more efficient.
810  *
811  * Most of the "heavy lifting" here is handled by the GVariantTypeInfo
812  * for the tuple.  See the notes in gvarianttypeinfo.h.
813  */
814
815 static gsize
816 gvs_tuple_n_children (GVariantSerialised value)
817 {
818   return g_variant_type_info_n_members (value.type_info);
819 }
820
821 static GVariantSerialised
822 gvs_tuple_get_child (GVariantSerialised value,
823                      gsize              index_)
824 {
825   const GVariantMemberInfo *member_info;
826   GVariantSerialised child = {  };
827   gsize offset_size;
828   gsize start, end;
829
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);
833
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.
837    */
838   if G_UNLIKELY (value.data == NULL && value.size != 0)
839     {
840       g_variant_type_info_query (child.type_info, NULL, &child.size);
841
842       /* this can only happen in fixed-sized tuples,
843        * so the child must also be fixed sized.
844        */
845       g_assert (child.size != 0);
846       child.data = NULL;
847
848       return child;
849     }
850
851   if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
852     {
853       if (offset_size * (member_info->i + 2) > value.size)
854         return child;
855     }
856   else
857     {
858       if (offset_size * (member_info->i + 1) > value.size)
859         {
860           /* if the child is fixed size, return its size.
861            * if child is not fixed-sized, return size = 0.
862            */
863           g_variant_type_info_query (child.type_info, NULL, &child.size);
864
865           return child;
866         }
867     }
868
869   if (member_info->i + 1)
870     start = gvs_read_unaligned_le (value.data + value.size -
871                                    offset_size * (member_info->i + 1),
872                                    offset_size);
873   else
874     start = 0;
875
876   start += member_info->a;
877   start &= member_info->b;
878   start |= member_info->c;
879
880   if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_LAST)
881     end = value.size - offset_size * (member_info->i + 1);
882
883   else if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_FIXED)
884     {
885       gsize fixed_size;
886
887       g_variant_type_info_query (child.type_info, NULL, &fixed_size);
888       end = start + fixed_size;
889       child.size = fixed_size;
890     }
891
892   else /* G_VARIANT_MEMEBER_ENDING_OFFSET */
893     end = gvs_read_unaligned_le (value.data + value.size -
894                                  offset_size * (member_info->i + 2),
895                                  offset_size);
896
897   if (start < end && end <= value.size)
898     {
899       child.data = value.data + start;
900       child.size = end - start;
901     }
902
903   return child;
904 }
905
906 static gsize
907 gvs_tuple_needed_size (GVariantTypeInfo         *type_info,
908                        GVariantSerialisedFiller  gvs_filler,
909                        const gpointer           *children,
910                        gsize                     n_children)
911 {
912   const GVariantMemberInfo *member_info = NULL;
913   gsize fixed_size;
914   gsize offset;
915   gsize i;
916
917   g_variant_type_info_query (type_info, NULL, &fixed_size);
918
919   if (fixed_size)
920     return fixed_size;
921
922   offset = 0;
923
924   for (i = 0; i < n_children; i++)
925     {
926       guint alignment;
927
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;
932
933       if (fixed_size)
934         offset += fixed_size;
935       else
936         {
937           GVariantSerialised child = {  };
938
939           gvs_filler (&child, children[i]);
940           offset += child.size;
941         }
942     }
943
944   return gvs_calculate_total_size (offset, member_info->i + 1);
945 }
946
947 static void
948 gvs_tuple_serialise (GVariantSerialised        value,
949                      GVariantSerialisedFiller  gvs_filler,
950                      const gpointer           *children,
951                      gsize                     n_children)
952 {
953   gsize offset_size;
954   gsize offset;
955   gsize i;
956
957   offset_size = gvs_get_offset_size (value.size);
958   offset = 0;
959
960   for (i = 0; i < n_children; i++)
961     {
962       const GVariantMemberInfo *member_info;
963       GVariantSerialised child = {  };
964       guint alignment;
965
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);
968
969       while (offset & alignment)
970         value.data[offset++] = '\0';
971
972       child.data = value.data + offset;
973       gvs_filler (&child, children[i]);
974       offset += child.size;
975
976       if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
977         {
978           value.size -= offset_size;
979           gvs_write_unaligned_le (value.data + value.size,
980                                   offset, offset_size);
981         }
982     }
983
984   while (offset < value.size)
985     value.data[offset++] = '\0';
986 }
987
988 static gboolean
989 gvs_tuple_is_normal (GVariantSerialised value)
990 {
991   guint offset_size;
992   gsize offset_ptr;
993   gsize length;
994   gsize offset;
995   gsize i;
996
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;
1000   offset = 0;
1001
1002   for (i = 0; i < length; i++)
1003     {
1004       const GVariantMemberInfo *member_info;
1005       GVariantSerialised child;
1006       gsize fixed_size;
1007       guint alignment;
1008       gsize end;
1009
1010       member_info = g_variant_type_info_member_info (value.type_info, i);
1011       child.type_info = member_info->type_info;
1012
1013       g_variant_type_info_query (child.type_info, &alignment, &fixed_size);
1014
1015       while (offset & alignment)
1016         {
1017           if (offset > value.size || value.data[offset] != '\0')
1018             return FALSE;
1019           offset++;
1020         }
1021
1022       child.data = value.data + offset;
1023
1024       switch (member_info->ending_type)
1025         {
1026         case G_VARIANT_MEMBER_ENDING_FIXED:
1027           end = offset + fixed_size;
1028           break;
1029
1030         case G_VARIANT_MEMBER_ENDING_LAST:
1031           end = offset_ptr;
1032           break;
1033
1034         case G_VARIANT_MEMBER_ENDING_OFFSET:
1035           offset_ptr -= offset_size;
1036
1037           if (offset_ptr < offset)
1038             return FALSE;
1039
1040           end = gvs_read_unaligned_le (value.data + offset_ptr, offset_size);
1041           break;
1042         }
1043
1044       if (end < offset || end > offset_ptr)
1045         return FALSE;
1046
1047       child.size = end - offset;
1048
1049       if (child.size == 0)
1050         child.data = NULL;
1051
1052       if (!g_variant_serialised_is_normal (child))
1053         return FALSE;
1054
1055       offset = end;
1056     }
1057
1058   {
1059     gsize fixed_size;
1060     guint alignment;
1061
1062     g_variant_type_info_query (value.type_info, &alignment, &fixed_size);
1063
1064     if (fixed_size)
1065       {
1066         g_assert (fixed_size == value.size);
1067         g_assert (offset_ptr == value.size);
1068
1069         if (i == 0)
1070           {
1071             if (value.data[offset++] != '\0')
1072               return FALSE;
1073           }
1074         else
1075           {
1076             while (offset & alignment)
1077               if (value.data[offset++] != '\0')
1078                 return FALSE;
1079           }
1080
1081         g_assert (offset == value.size);
1082       }
1083   }
1084
1085   return offset_ptr == offset;
1086 }
1087
1088 /* Variants {{{2
1089  *
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
1092  * child.
1093  *
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
1097  * unit tuple: ().
1098  */
1099
1100 static inline gsize
1101 gvs_variant_n_children (GVariantSerialised value)
1102 {
1103   return 1;
1104 }
1105
1106 static inline GVariantSerialised
1107 gvs_variant_get_child (GVariantSerialised value,
1108                        gsize              index_)
1109 {
1110   GVariantSerialised child = {  };
1111
1112   /* NOTE: not O(1) and impossible for it to be... */
1113   if (value.size)
1114     {
1115
1116       /* find '\0' character */
1117       for (child.size = value.size - 1; child.size; child.size--)
1118         if (value.data[child.size] == '\0')
1119           break;
1120
1121       /* ensure we didn't just hit the start of the string */
1122       if (value.data[child.size] == '\0')
1123         {
1124           const gchar *type_string = (gchar *) &value.data[child.size + 1];
1125           const gchar *limit = (gchar *) &value.data[value.size];
1126           const gchar *end;
1127
1128           if (g_variant_type_string_scan (type_string, limit, &end) &&
1129               end == limit)
1130             {
1131               const GVariantType *type = (GVariantType *) type_string;
1132
1133               if (g_variant_type_is_definite (type))
1134                 {
1135                   gsize fixed_size;
1136
1137                   child.type_info = g_variant_type_info_get (type);
1138
1139                   if (child.size != 0)
1140                     /* only set to non-%NULL if size > 0 */
1141                     child.data = value.data;
1142
1143                   g_variant_type_info_query (child.type_info,
1144                                              NULL, &fixed_size);
1145
1146                   if (!fixed_size || fixed_size == child.size)
1147                     return child;
1148
1149                   g_variant_type_info_unref (child.type_info);
1150                 }
1151             }
1152         }
1153     }
1154
1155   child.type_info = g_variant_type_info_get (G_VARIANT_TYPE_UNIT);
1156   child.data = NULL;
1157   child.size = 1;
1158
1159   return child;
1160 }
1161
1162 static inline gsize
1163 gvs_variant_needed_size (GVariantTypeInfo         *type_info,
1164                          GVariantSerialisedFiller  gvs_filler,
1165                          const gpointer           *children,
1166                          gsize                     n_children)
1167 {
1168   GVariantSerialised child = {  };
1169   const gchar *type_string;
1170
1171   gvs_filler (&child, children[0]);
1172   type_string = g_variant_type_info_get_type_string (child.type_info);
1173
1174   return child.size + 1 + strlen (type_string);
1175 }
1176
1177 static inline void
1178 gvs_variant_serialise (GVariantSerialised        value,
1179                        GVariantSerialisedFiller  gvs_filler,
1180                        const gpointer           *children,
1181                        gsize                     n_children)
1182 {
1183   GVariantSerialised child = {  };
1184   const gchar *type_string;
1185
1186   child.data = value.data;
1187
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));
1192 }
1193
1194 static inline gboolean
1195 gvs_variant_is_normal (GVariantSerialised value)
1196 {
1197   GVariantSerialised child;
1198   gboolean normal;
1199
1200   child = gvs_variant_get_child (value, 0);
1201
1202   normal = (child.data != NULL || child.size == 0) &&
1203            g_variant_serialised_is_normal (child);
1204
1205   g_variant_type_info_unref (child.type_info);
1206
1207   return normal;
1208 }
1209
1210
1211
1212 /* PART 2: Serialiser API {{{1
1213  *
1214  * This is the implementation of the API of the serialiser as advertised
1215  * in gvariant-serialiser.h.
1216  */
1217
1218 /* Dispatch Utilities {{{2
1219  *
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).
1224  */
1225 #define DISPATCH_FIXED(type_info, before, after) \
1226   {                                                     \
1227     gsize fixed_size;                                   \
1228                                                         \
1229     g_variant_type_info_query_element (type_info, NULL, \
1230                                        &fixed_size);    \
1231                                                         \
1232     if (fixed_size)                                     \
1233       {                                                 \
1234         before ## fixed_sized ## after                  \
1235       }                                                 \
1236     else                                                \
1237       {                                                 \
1238         before ## variable_sized ## after               \
1239       }                                                 \
1240   }
1241
1242 #define DISPATCH_CASES(type_info, before, after) \
1243   switch (g_variant_type_info_get_type_char (type_info))        \
1244     {                                                           \
1245       case G_VARIANT_TYPE_INFO_CHAR_MAYBE:                      \
1246         DISPATCH_FIXED (type_info, before, _maybe ## after)     \
1247                                                                 \
1248       case G_VARIANT_TYPE_INFO_CHAR_ARRAY:                      \
1249         DISPATCH_FIXED (type_info, before, _array ## after)     \
1250                                                                 \
1251       case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY:                 \
1252       case G_VARIANT_TYPE_INFO_CHAR_TUPLE:                      \
1253         {                                                       \
1254           before ## tuple ## after                              \
1255         }                                                       \
1256                                                                 \
1257       case G_VARIANT_TYPE_INFO_CHAR_VARIANT:                    \
1258         {                                                       \
1259           before ## variant ## after                            \
1260         }                                                       \
1261     }
1262
1263 /* Serialiser entry points {{{2
1264  *
1265  * These are the functions that are called in order for the serialiser
1266  * to do its thing.
1267  */
1268
1269 /* < private >
1270  * g_variant_serialised_n_children:
1271  * @serialised: a #GVariantSerialised
1272  * @returns: the number of children
1273  *
1274  * For serialised data that represents a container value (maybes,
1275  * tuples, arrays, variants), determine how many child items are inside
1276  * that container.
1277  */
1278 gsize
1279 g_variant_serialised_n_children (GVariantSerialised serialised)
1280 {
1281   g_variant_serialised_check (serialised);
1282
1283   DISPATCH_CASES (serialised.type_info,
1284
1285                   return gvs_/**/,/**/_n_children (serialised);
1286
1287                  )
1288   g_assert_not_reached ();
1289 }
1290
1291 /* < private >
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
1296  *
1297  * Extracts a child from a serialised data representing a container
1298  * value.
1299  *
1300  * It is an error to call this function with an index out of bounds.
1301  *
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.
1305  *
1306  * In the case that .data == %NULL and .size == 0 then a zero-sized
1307  * item of a variable-sized type is being returned.
1308  *
1309  * .data is never non-%NULL if size is 0.
1310  */
1311 GVariantSerialised
1312 g_variant_serialised_get_child (GVariantSerialised serialised,
1313                                 gsize              index_)
1314 {
1315   GVariantSerialised child;
1316
1317   g_variant_serialised_check (serialised);
1318
1319   if G_LIKELY (index_ < g_variant_serialised_n_children (serialised))
1320     {
1321       DISPATCH_CASES (serialised.type_info,
1322
1323                       child = gvs_/**/,/**/_get_child (serialised, index_);
1324                       g_assert (child.size || child.data == NULL);
1325                       g_variant_serialised_check (child);
1326                       return child;
1327
1328                      )
1329       g_assert_not_reached ();
1330     }
1331
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));
1335 }
1336
1337 /* < private >
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
1343  *
1344  * Writes data in serialised form.
1345  *
1346  * The type_info field of @serialised must be filled in to type info for
1347  * the type that we are serialising.
1348  *
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().
1351  *
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
1354  * the size field).
1355  *
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.
1359  */
1360 void
1361 g_variant_serialiser_serialise (GVariantSerialised        serialised,
1362                                 GVariantSerialisedFiller  gvs_filler,
1363                                 const gpointer           *children,
1364                                 gsize                     n_children)
1365 {
1366   g_variant_serialised_check (serialised);
1367
1368   DISPATCH_CASES (serialised.type_info,
1369
1370                   gvs_/**/,/**/_serialise (serialised, gvs_filler,
1371                                            children, n_children);
1372                   return;
1373
1374                  )
1375   g_assert_not_reached ();
1376 }
1377
1378 /* < private >
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
1384  *
1385  * Determines how much memory would be needed to serialise this value.
1386  *
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.
1390  */
1391 gsize
1392 g_variant_serialiser_needed_size (GVariantTypeInfo         *type_info,
1393                                   GVariantSerialisedFiller  gvs_filler,
1394                                   const gpointer           *children,
1395                                   gsize                     n_children)
1396 {
1397   DISPATCH_CASES (type_info,
1398
1399                   return gvs_/**/,/**/_needed_size (type_info, gvs_filler,
1400                                                     children, n_children);
1401
1402                  )
1403   g_assert_not_reached ();
1404 }
1405
1406 /* Byteswapping {{{2 */
1407
1408 /* < private >
1409  * g_variant_serialised_byteswap:
1410  * @value: a #GVariantSerialised
1411  *
1412  * Byte-swap serialised data.  The result of this function is only
1413  * well-defined if the data is in normal form.
1414  */
1415 void
1416 g_variant_serialised_byteswap (GVariantSerialised serialised)
1417 {
1418   gsize fixed_size;
1419   guint alignment;
1420
1421   g_variant_serialised_check (serialised);
1422
1423   if (!serialised.data)
1424     return;
1425
1426   /* the types we potentially need to byteswap are
1427    * exactly those with alignment requirements.
1428    */
1429   g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
1430   if (!alignment)
1431     return;
1432
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.
1437    */
1438   if (alignment + 1 == fixed_size)
1439     {
1440       switch (fixed_size)
1441       {
1442         case 2:
1443           {
1444             guint16 *ptr = (guint16 *) serialised.data;
1445
1446             g_assert_cmpint (serialised.size, ==, 2);
1447             *ptr = GUINT16_SWAP_LE_BE (*ptr);
1448           }
1449           return;
1450
1451         case 4:
1452           {
1453             guint32 *ptr = (guint32 *) serialised.data;
1454
1455             g_assert_cmpint (serialised.size, ==, 4);
1456             *ptr = GUINT32_SWAP_LE_BE (*ptr);
1457           }
1458           return;
1459
1460         case 8:
1461           {
1462             guint64 *ptr = (guint64 *) serialised.data;
1463
1464             g_assert_cmpint (serialised.size, ==, 8);
1465             *ptr = GUINT64_SWAP_LE_BE (*ptr);
1466           }
1467           return;
1468
1469         default:
1470           g_assert_not_reached ();
1471       }
1472     }
1473
1474   /* else, we have a container that potentially contains
1475    * some children that need to be byteswapped.
1476    */
1477   else
1478     {
1479       gsize children, i;
1480
1481       children = g_variant_serialised_n_children (serialised);
1482       for (i = 0; i < children; i++)
1483         {
1484           GVariantSerialised child;
1485
1486           child = g_variant_serialised_get_child (serialised, i);
1487           g_variant_serialised_byteswap (child);
1488           g_variant_type_info_unref (child.type_info);
1489         }
1490     }
1491 }
1492
1493 /* Normal form checking {{{2 */
1494
1495 /* < private >
1496  * g_variant_serialised_is_normal:
1497  * @serialised: a #GVariantSerialised
1498  *
1499  * Determines, recursively if @serialised is in normal form.  There is
1500  * precisely one normal form of serialised data for each possible value.
1501  *
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
1505  * form.
1506  */
1507 gboolean
1508 g_variant_serialised_is_normal (GVariantSerialised serialised)
1509 {
1510   DISPATCH_CASES (serialised.type_info,
1511
1512                   return gvs_/**/,/**/_is_normal (serialised);
1513
1514                  )
1515
1516   /* some hard-coded terminal cases */
1517   switch (g_variant_type_info_get_type_char (serialised.type_info))
1518     {
1519     case 'b': /* boolean */
1520       return serialised.data[0] < 2;
1521
1522     case 's': /* string */
1523       return g_variant_serialiser_is_string (serialised.data,
1524                                              serialised.size);
1525
1526     case 'o':
1527       return g_variant_serialiser_is_object_path (serialised.data,
1528                                                   serialised.size);
1529
1530     case 'g':
1531       return g_variant_serialiser_is_signature (serialised.data,
1532                                                 serialised.size);
1533
1534     default:
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).
1538        */
1539       return TRUE;
1540     }
1541 }
1542
1543 /* Validity-checking functions {{{2
1544  *
1545  * Checks if strings, object paths and signature strings are valid.
1546  */
1547
1548 /* < private >
1549  * g_variant_serialiser_is_string:
1550  * @data: a possible string
1551  * @size: the size of @data
1552  *
1553  * Ensures that @data is a valid string with a nul terminator at the end
1554  * and no nul bytes embedded.
1555  */
1556 gboolean
1557 g_variant_serialiser_is_string (gconstpointer data,
1558                                 gsize         size)
1559 {
1560   const gchar *string = data;
1561
1562   if (size == 0)
1563     return FALSE;
1564
1565   if (string[size - 1] != '\0')
1566     return FALSE;
1567
1568   return strlen (string) == size - 1;
1569 }
1570
1571 /* < private >
1572  * g_variant_serialiser_is_object_path:
1573  * @data: a possible DBus object path
1574  * @size: the size of @data
1575  *
1576  * Performs the checks for being a valid string.
1577  *
1578  * Also, ensures that @data is a valid DBus object path, as per the DBus
1579  * specification.
1580  */
1581 gboolean
1582 g_variant_serialiser_is_object_path (gconstpointer data,
1583                                      gsize         size)
1584 {
1585   const gchar *string = data;
1586   gsize i;
1587
1588   if (!g_variant_serialiser_is_string (data, size))
1589     return FALSE;
1590
1591   /* The path must begin with an ASCII '/' (integer 47) character */
1592   if (string[0] != '/')
1593     return FALSE;
1594
1595   for (i = 1; string[i]; i++)
1596     /* Each element must only contain the ASCII characters
1597      * "[A-Z][a-z][0-9]_"
1598      */
1599     if (g_ascii_isalnum (string[i]) || string[i] == '_')
1600       ;
1601
1602     /* must consist of elements separated by slash characters. */
1603     else if (string[i] == '/')
1604       {
1605         /* No element may be the empty string. */
1606         /* Multiple '/' characters cannot occur in sequence. */
1607         if (string[i - 1] == '/')
1608           return FALSE;
1609       }
1610
1611     else
1612       return FALSE;
1613
1614   /* A trailing '/' character is not allowed unless the path is the
1615    * root path (a single '/' character).
1616    */
1617   if (i > 1 && string[i - 1] == '/')
1618     return FALSE;
1619
1620   return TRUE;
1621 }
1622
1623 /* < private >
1624  * g_variant_serialiser_is_signature:
1625  * @data: a possible DBus signature
1626  * @size: the size of @data
1627  *
1628  * Performs the checks for being a valid string.
1629  *
1630  * Also, ensures that @data is a valid DBus type signature, as per the
1631  * DBus specification.
1632  */
1633 gboolean
1634 g_variant_serialiser_is_signature (gconstpointer data,
1635                                    gsize         size)
1636 {
1637   const gchar *string = data;
1638   gsize first_invalid;
1639
1640   if (!g_variant_serialiser_is_string (data, size))
1641     return FALSE;
1642
1643   /* make sure no non-definite characters appear */
1644   first_invalid = strspn (string, "ybnqiuxthdvasog(){}");
1645   if (string[first_invalid])
1646     return FALSE;
1647
1648   /* make sure each type string is well-formed */
1649   while (*string)
1650     if (!g_variant_type_string_scan (string, NULL, &string))
1651       return FALSE;
1652
1653   return TRUE;
1654 }
1655
1656 /* vim:set foldmethod=marker: */