GVariant variable arguments, tests, cleanups
[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 "config.h"
25
26 #include "gvariant-serialiser.h"
27
28 #include <glib/gtestutils.h>
29 #include <glib/gstrfuncs.h>
30 #include <glib/gtypes.h>
31
32 #include <string.h>
33
34 #include "galias.h"
35
36 /* GVariantSerialiser
37  *
38  * After this prologue section, this file has roughly 2 parts.
39  *
40  * The first part is split up into sections according to various
41  * container types.  Maybe, Array, Tuple, Variant.  The Maybe and Array
42  * sections are subdivided for element types being fixed or
43  * variable-sized types.
44  *
45  * Each section documents the format of that particular type of
46  * container and implements 5 functions for dealing with it:
47  *
48  *  n_children:
49  *    - determines (according to serialised data) how many child values
50  *      are inside a particular container value.
51  *
52  *  get_child:
53  *    - gets the type of and the serialised data corresponding to a
54  *      given child value within the container value.
55  *
56  *  needed_size:
57  *    - determines how much space would be required to serialise a
58  *      container of this type, containing the given children so that
59  *      buffers can be preallocated before serialising.
60  *
61  *  serialise:
62  *    - write the serialised data for a container of this type,
63  *      containing the given children, to a buffer.
64  *
65  *  is_normal:
66  *    - check the given data to ensure that it is in normal form.  For a
67  *      given set of child values, there is exactly one normal form for
68  *      the serialised data of a container.  Other forms are possible
69  *      while maintaining the same children (for example, by inserting
70  *      something other than zero bytes as padding) but only one form is
71  *      the normal form.
72  *
73  * The second part contains the main entry point for each of the above 5
74  * functions and logic to dispatch it to the handler for the appropriate
75  * container type code.
76  *
77  * The second part also contains a routine to byteswap serialised
78  * values.  This code makes use of the n_children() and get_child()
79  * functions above to do its work so no extra support is needed on a
80  * per-container-type basis.
81  *
82  * There is also additional code for checking for normal form.  All
83  * numeric types are always in normal form since the full range of
84  * values is permitted (eg: 0 to 255 is a valid byte).  Special checks
85  * need to be performed for booleans (only 0 or 1 allowed), strings
86  * (properly nul-terminated) and object paths and signature strings
87  * (meeting the DBus specification requirements).
88  */
89
90 /* < private >
91  * GVariantSerialised:
92  * @type_info: the #GVariantTypeInfo of this value
93  * @data: the serialised data of this value, or %NULL
94  * @size: the size of this value
95  *
96  * A structure representing a GVariant in serialised form.  This
97  * structure is used with #GVariantSerialisedFiller functions and as the
98  * primary interface to the serialiser.  See #GVariantSerialisedFiller
99  * for a description of its use there.
100  *
101  * When used with the serialiser API functions, the following invariants
102  * apply to all #GVariantTypeSerialised structures passed to and
103  * returned from the serialiser.
104  *
105  * @type_info must be non-%NULL.
106  *
107  * @data must be properly aligned for the type described by @type_info.
108  *
109  * If @type_info describes a fixed-sized type then @size must always be
110  * equal to the fixed size of that type.
111  *
112  * For fixed-sized types (and only fixed-sized types), @data may be
113  * %NULL even if @size is non-zero.  This happens when a framing error
114  * occurs while attempting to extract a fixed-sized value out of a
115  * variable-sized container.  There is no data to return for the
116  * fixed-sized type, yet @size must be non-zero.  The effect of this
117  * combination should be as if @data were a pointer to an
118  * appropriately-sized zero-filled region.
119  */
120
121 /* < private >
122  * g_variant_serialised_check:
123  * @serialised: a #GVariantSerialised struct
124  *
125  * Checks @serialised for validity according to the invariants described
126  * above.
127  */
128 static void
129 g_variant_serialised_check (GVariantSerialised serialised)
130 {
131   gsize fixed_size;
132   guint alignment;
133
134   g_assert (serialised.type_info != NULL);
135   g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
136
137   if (fixed_size)
138     g_assert_cmpint (serialised.size, ==, fixed_size);
139   else
140     g_assert (serialised.size == 0 || serialised.data != NULL);
141
142   g_assert_cmpint (alignment & (gsize) serialised.data, ==, 0);
143 }
144
145 /* < private >
146  * GVariantSerialisedFiller:
147  * @serialised: a #GVariantSerialised instance to fill
148  * @data: data from the children array
149  *
150  * This function is called back from g_variant_serialiser_needed_size()
151  * and g_variant_serialiser_serialise().  It fills in missing details
152  * from a partially-complete #GVariantSerialised.
153  *
154  * The @data parameter passed back to the function is one of the items
155  * that was passed to the serialiser in the @children array.  It
156  * represents a single child item of the container that is being
157  * serialised.  The information filled in to @serialised is the
158  * information for this child.
159  *
160  * If the @type_info field of @serialised is %NULL then the callback
161  * function must set it to the type information corresponding to the
162  * type of the child.  No reference should be added.  If it is non-%NULL
163  * then the callback should assert that it is equal to the actual type
164  * of the child.
165  *
166  * If the @size field is zero then the callback must fill it in with the
167  * required amount of space to store the serialised form of the child.
168  * If it is non-zero then the callback should assert that it is equal to
169  * the needed size of the child.
170  *
171  * If @data is non-%NULL then it points to a space that is properly
172  * aligned for and large enough to store the serialised data of the
173  * child.  The callback must store the serialised form of the child at
174  * @data.
175  *
176  * If the child value is another container then the callback will likely
177  * recurse back into the serialiser by calling
178  * g_variant_serialiser_needed_size() to determine @size and
179  * g_variant_serialiser_serialise() to write to @data.
180  */
181
182 /* PART 1: Container types {{{1
183  *
184  * This section contains the serialiser implementation functions for
185  * each container type.
186  */
187
188 /* Maybe {{{2
189  *
190  * Maybe types are handled depending on if the element type of the maybe
191  * type is a fixed-sized or variable-sized type.  Although all maybe
192  * types themselves are variable-sized types, herein, a maybe value with
193  * a fixed-sized element type is called a "fixed-sized maybe" for
194  * convenience and a maybe value with a variable-sized element type is
195  * called a "variable-sized maybe".
196  */
197
198 /* Fixed-sized Maybe {{{3
199  *
200  * The size of a maybe value with a fixed-sized element type is either 0
201  * or equal to the fixed size of its element type.  The case where the
202  * size of the maybe value is zero corresponds to the "Nothing" case and
203  * the case where the size of the maybe value is equal to the fixed size
204  * of the element type corresponds to the "Just" case; in that case, the
205  * serialised data of the child value forms the entire serialised data
206  * of the maybe value.
207  *
208  * In the event that a fixed-sized maybe value is presented with a size
209  * that is not equal to the fixed size of the element type then the
210  * value must be taken to be "Nothing".
211  */
212
213 static gsize
214 gvs_fixed_sized_maybe_n_children (GVariantSerialised value)
215 {
216   gsize element_fixed_size;
217
218   g_variant_type_info_query_element (value.type_info, NULL,
219                                      &element_fixed_size);
220
221   return (element_fixed_size == value.size) ? 1 : 0;
222 }
223
224 static GVariantSerialised
225 gvs_fixed_sized_maybe_get_child (GVariantSerialised value,
226                                  gsize              index_)
227 {
228   /* the child has the same bounds as the
229    * container, so just update the type.
230    */
231   value.type_info = g_variant_type_info_element (value.type_info);
232   g_variant_type_info_ref (value.type_info);
233
234   return value;
235 }
236
237 static gsize
238 gvs_fixed_sized_maybe_needed_size (GVariantTypeInfo         *type_info,
239                                    GVariantSerialisedFiller  gvs_filler,
240                                    const gpointer           *children,
241                                    gsize                     n_children)
242 {
243   if (n_children)
244     {
245       gsize element_fixed_size;
246
247       g_variant_type_info_query_element (type_info, NULL,
248                                          &element_fixed_size);
249
250       return element_fixed_size;
251     }
252   else
253     return 0;
254 }
255
256 static void
257 gvs_fixed_sized_maybe_serialise (GVariantSerialised        value,
258                                  GVariantSerialisedFiller  gvs_filler,
259                                  const gpointer           *children,
260                                  gsize                     n_children)
261 {
262   if (n_children)
263     {
264       GVariantSerialised child = { NULL, value.data, value.size };
265
266       gvs_filler (&child, children[0]);
267     }
268 }
269
270 static gboolean
271 gvs_fixed_sized_maybe_is_normal (GVariantSerialised value)
272 {
273   if (value.size > 0)
274     {
275       gsize element_fixed_size;
276
277       g_variant_type_info_query_element (value.type_info,
278                                          NULL, &element_fixed_size);
279
280       if (value.size != element_fixed_size)
281         return FALSE;
282
283       /* proper element size: "Just".  recurse to the child. */
284       value.type_info = g_variant_type_info_element (value.type_info);
285
286       return g_variant_serialised_is_normal (value);
287     }
288
289   /* size of 0: "Nothing" */
290   return TRUE;
291 }
292
293 /* Variable-sized Maybe
294  *
295  * The size of a maybe value with a variable-sized element type is
296  * either 0 or strictly greater than 0.  The case where the size of the
297  * maybe value is zero corresponds to the "Nothing" case and the case
298  * where the size of the maybe value is greater than zero corresponds to
299  * the "Just" case; in that case, the serialised data of the child value
300  * forms the first part of the serialised data of the maybe value and is
301  * followed by a single zero byte.  This zero byte is always appended,
302  * regardless of any zero bytes that may already be at the end of the
303  * serialised ata of the child value.
304  */
305
306 static gsize
307 gvs_variable_sized_maybe_n_children (GVariantSerialised value)
308 {
309   return (value.size > 0) ? 1 : 0;
310 }
311
312 static GVariantSerialised
313 gvs_variable_sized_maybe_get_child (GVariantSerialised value,
314                                     gsize              index_)
315 {
316   /* remove the padding byte and update the type. */
317   value.type_info = g_variant_type_info_element (value.type_info);
318   g_variant_type_info_ref (value.type_info);
319   value.size--;
320
321   /* if it's zero-sized then it may as well be NULL */
322   if (value.size == 0)
323     value.data = NULL;
324
325   return value;
326 }
327
328 static gsize
329 gvs_variable_sized_maybe_needed_size (GVariantTypeInfo         *type_info,
330                                       GVariantSerialisedFiller  gvs_filler,
331                                       const gpointer           *children,
332                                       gsize                     n_children)
333 {
334   if (n_children)
335     {
336       GVariantSerialised child = {  };
337
338       gvs_filler (&child, children[0]);
339
340       return child.size + 1;
341     }
342   else
343     return 0;
344 }
345
346 static void
347 gvs_variable_sized_maybe_serialise (GVariantSerialised        value,
348                                     GVariantSerialisedFiller  gvs_filler,
349                                     const gpointer           *children,
350                                     gsize                     n_children)
351 {
352   if (n_children)
353     {
354       GVariantSerialised child = { NULL, value.data, value.size - 1 };
355
356       /* write the data for the child.  */
357       gvs_filler (&child, children[0]);
358       value.data[child.size] = '\0';
359     }
360 }
361
362 static gboolean
363 gvs_variable_sized_maybe_is_normal (GVariantSerialised value)
364 {
365   if (value.size == 0)
366     return TRUE;
367
368   if (value.data[value.size - 1] != '\0')
369     return FALSE;
370
371   value.type_info = g_variant_type_info_element (value.type_info);
372   value.size--;
373
374   return g_variant_serialised_is_normal (value);
375 }
376
377 /* Arrays {{{2
378  *
379  * Just as with maybe types, array types are handled depending on if the
380  * element type of the array type is a fixed-sized or variable-sized
381  * type.  Similar to maybe types, for convenience, an array value with a
382  * fixed-sized element type is called a "fixed-sized array" and an array
383  * value with a variable-sized element type is called a "variable sized
384  * array".
385  */
386
387 /* Fixed-sized Array {{{3
388  *
389  * For fixed sized arrays, the serialised data is simply a concatenation
390  * of the serialised data of each element, in order.  Since fixed-sized
391  * values always have a fixed size that is a multiple of their alignment
392  * requirement no extra padding is required.
393  *
394  * In the event that a fixed-sized array is presented with a size that
395  * is not an integer multiple of the element size then the value of the
396  * array must be taken as being empty.
397  */
398
399 static gsize
400 gvs_fixed_sized_array_n_children (GVariantSerialised value)
401 {
402   gsize element_fixed_size;
403
404   g_variant_type_info_query_element (value.type_info, NULL,
405                                      &element_fixed_size);
406
407   if (value.size % element_fixed_size == 0)
408     return value.size / element_fixed_size;
409
410   return 0;
411 }
412
413 static GVariantSerialised
414 gvs_fixed_sized_array_get_child (GVariantSerialised value,
415                                  gsize              index_)
416 {
417   GVariantSerialised child = {  };
418
419   child.type_info = g_variant_type_info_element (value.type_info);
420   g_variant_type_info_query (child.type_info, NULL, &child.size);
421   child.data = value.data + (child.size * index_);
422   g_variant_type_info_ref (child.type_info);
423
424   return child;
425 }
426
427 static gsize
428 gvs_fixed_sized_array_needed_size (GVariantTypeInfo         *type_info,
429                                    GVariantSerialisedFiller  gvs_filler,
430                                    const gpointer           *children,
431                                    gsize                     n_children)
432 {
433   gsize element_fixed_size;
434
435   g_variant_type_info_query_element (type_info, NULL, &element_fixed_size);
436
437   return element_fixed_size * n_children;
438 }
439
440 static void
441 gvs_fixed_sized_array_serialise (GVariantSerialised        value,
442                                  GVariantSerialisedFiller  gvs_filler,
443                                  const gpointer           *children,
444                                  gsize                     n_children)
445 {
446   GVariantSerialised child = {  };
447   gsize i;
448
449   child.type_info = g_variant_type_info_element (value.type_info);
450   g_variant_type_info_query (child.type_info, NULL, &child.size);
451   child.data = value.data;
452
453   for (i = 0; i < n_children; i++)
454     {
455       gvs_filler (&child, children[i]);
456       child.data += child.size;
457     }
458 }
459
460 static gboolean
461 gvs_fixed_sized_array_is_normal (GVariantSerialised value)
462 {
463   GVariantSerialised child = {  };
464
465   child.type_info = g_variant_type_info_element (value.type_info);
466   g_variant_type_info_query (child.type_info, NULL, &child.size);
467
468   if (value.size % child.size != 0)
469     return FALSE;
470
471   for (child.data = value.data;
472        child.data < value.data + value.size;
473        child.data += child.size)
474     {
475       if (!g_variant_serialised_is_normal (child))
476         return FALSE;
477     }
478
479   return TRUE;
480 }
481
482 /* Variable-sized Array {{{3
483  *
484  * Variable sized arrays, containing variable-sized elements, must be
485  * able to determine the boundaries between the elements.  The items
486  * cannot simply be concatenated.  Additionally, we are faced with the
487  * fact that non-fixed-sized values do not neccessarily have a size that
488  * is a multiple of their alignment requirement, so we may need to
489  * insert zero-filled padding.
490  *
491  * While it is possible to find the start of an item by starting from
492  * the end of the item before it and padding for alignment, it is not
493  * generally possible to do the reverse operation.  For this reason, we
494  * record the end point of each element in the array.
495  *
496  * GVariant works in terms of "offsets".  An offset is a pointer to a
497  * boundary between two bytes.  In 4 bytes of serialised data, there
498  * would be 5 possible offsets: one at the start ('0'), one between each
499  * pair of adjacent bytes ('1', '2', '3') and one at the end ('4').
500  *
501  * The numeric value of an offset is an unsigned integer given relative
502  * to the start of the serialised data of the array.  Offsets are always
503  * stored in little endian byte order and are always only as big as they
504  * need to be.  For example, in 255 bytes of serialised data, there are
505  * 256 offsets.  All possibilities can be stored in an 8 bit unsigned
506  * integer.  In 256 bytes of serialised data, however, there are 257
507  * possible offsets so 16 bit integers must be used.  The size of an
508  * offset is always a power of 2.
509  *
510  * The offsets are stored at the end of the serialised data of the
511  * array.  They are simply concatenated on without any particular
512  * alignment.  The size of the offsets is included in the size of the
513  * serialised data for purposes of determining the size of the offsets.
514  * This presents a possibly ambiguity; in certain cases, a particular
515  * value of array could have two different serialised forms.
516  *
517  * Imagine an array containing a single string of 253 bytes in length
518  * (so, 254 bytes including the nul terminator).  Now the offset must be
519  * written.  If an 8 bit offset is written, it will bring the size of
520  * the array's serialised data to 255 -- which means that the use of an
521  * 8 bit offset was valid.  If a 16 bit offset is used then the total
522  * size of the array will be 256 -- which means that the use of a 16 bit
523  * offset was valid.  Although both of these will be accepted by the
524  * deserialiser, only the smaller of the two is considered to be in
525  * normal form and that is the one that the serialiser must produce.
526  */
527
528 static inline gsize
529 gvs_read_unaligned_le (guchar *bytes,
530                        guint   size)
531 {
532   union
533   {
534     guchar bytes[GLIB_SIZEOF_SIZE_T];
535     gsize integer;
536   } tmpvalue;
537
538   tmpvalue.integer = 0;
539   memcpy (&tmpvalue.bytes, bytes, size);
540
541   return GSIZE_FROM_LE (tmpvalue.integer);
542 }
543
544 static inline void
545 gvs_write_unaligned_le (guchar *bytes,
546                         gsize   value,
547                         guint   size)
548 {
549   union
550   {
551     guchar bytes[GLIB_SIZEOF_SIZE_T];
552     gsize integer;
553   } tmpvalue;
554
555   tmpvalue.integer = GSIZE_TO_LE (value);
556   memcpy (bytes, &tmpvalue.bytes, size);
557 }
558
559 static guint
560 gvs_get_offset_size (gsize size)
561 {
562   if (size > G_MAXUINT32)
563     return 8;
564
565   else if (size > G_MAXUINT16)
566     return 4;
567
568   else if (size > G_MAXUINT8)
569     return 2;
570
571   else if (size > 0)
572     return 1;
573
574   return 0;
575 }
576
577 static gsize
578 gvs_calculate_total_size (gsize body_size,
579                           gsize offsets)
580 {
581   if (body_size + 1 * offsets <= G_MAXUINT8)
582     return body_size + 1 * offsets;
583
584   if (body_size + 2 * offsets <= G_MAXUINT16)
585     return body_size + 2 * offsets;
586
587   if (body_size + 4 * offsets <= G_MAXUINT32)
588     return body_size + 4 * offsets;
589
590   return body_size + 8 * offsets;
591 }
592
593 static gsize
594 gvs_variable_sized_array_n_children (GVariantSerialised value)
595 {
596   gsize offsets_array_size;
597   gsize offset_size;
598   gsize last_end;
599
600   if (value.size == 0)
601     return 0;
602
603   offset_size = gvs_get_offset_size (value.size);
604
605   last_end = gvs_read_unaligned_le (value.data + value.size -
606                                     offset_size, offset_size);
607
608   if (last_end > value.size)
609     return 0;
610
611   offsets_array_size = value.size - last_end;
612
613   if (offsets_array_size % offset_size)
614     return 0;
615
616   return offsets_array_size / offset_size;
617 }
618
619 static GVariantSerialised
620 gvs_variable_sized_array_get_child (GVariantSerialised value,
621                                     gsize              index_)
622 {
623   GVariantSerialised child = {  };
624   gsize offset_size;
625   gsize last_end;
626   gsize start;
627   gsize end;
628
629   child.type_info = g_variant_type_info_element (value.type_info);
630   g_variant_type_info_ref (child.type_info);
631
632   offset_size = gvs_get_offset_size (value.size);
633
634   last_end = gvs_read_unaligned_le (value.data + value.size -
635                                     offset_size, offset_size);
636
637   if (index_ > 0)
638     {
639       guint alignment;
640
641       start = gvs_read_unaligned_le (value.data + last_end +
642                                      (offset_size * (index_ - 1)),
643                                      offset_size);
644
645       g_variant_type_info_query (child.type_info, &alignment, NULL);
646       start += (-start) & alignment;
647     }
648   else
649     start = 0;
650
651   end = gvs_read_unaligned_le (value.data + last_end +
652                                (offset_size * index_),
653                                offset_size);
654
655   if (start < end && end <= value.size)
656     {
657       child.data = value.data + start;
658       child.size = end - start;
659     }
660
661   return child;
662 }
663
664 static gsize
665 gvs_variable_sized_array_needed_size (GVariantTypeInfo         *type_info,
666                                       GVariantSerialisedFiller  gvs_filler,
667                                       const gpointer           *children,
668                                       gsize                     n_children)
669 {
670   guint alignment;
671   gsize offset;
672   gsize i;
673
674   g_variant_type_info_query (type_info, &alignment, NULL);
675   offset = 0;
676
677   for (i = 0; i < n_children; i++)
678     {
679       GVariantSerialised child = {  };
680
681       offset += (-offset) & alignment;
682       gvs_filler (&child, children[i]);
683       offset += child.size;
684     }
685
686   return gvs_calculate_total_size (offset, n_children);
687 }
688
689 static void
690 gvs_variable_sized_array_serialise (GVariantSerialised        value,
691                                     GVariantSerialisedFiller  gvs_filler,
692                                     const gpointer           *children,
693                                     gsize                     n_children)
694 {
695   guchar *offset_ptr;
696   gsize offset_size;
697   guint alignment;
698   gsize offset;
699   gsize i;
700
701   g_variant_type_info_query (value.type_info, &alignment, NULL);
702   offset_size = gvs_get_offset_size (value.size);
703   offset = 0;
704
705   offset_ptr = value.data + value.size - offset_size * n_children;
706
707   for (i = 0; i < n_children; i++)
708     {
709       GVariantSerialised child = {  };
710
711       while (offset & alignment)
712         value.data[offset++] = '\0';
713
714       child.data = value.data + offset;
715       gvs_filler (&child, children[i]);
716       offset += child.size;
717
718       gvs_write_unaligned_le (offset_ptr, offset, offset_size);
719       offset_ptr += offset_size;
720     }
721 }
722
723 static gboolean
724 gvs_variable_sized_array_is_normal (GVariantSerialised value)
725 {
726   GVariantSerialised child = {  };
727   gsize offsets_array_size;
728   guchar *offsets_array;
729   guint offset_size;
730   guint alignment;
731   gsize last_end;
732   gsize length;
733   gsize offset;
734   gsize i;
735
736   if (value.size == 0)
737     return TRUE;
738
739   offset_size = gvs_get_offset_size (value.size);
740   last_end = gvs_read_unaligned_le (value.data + value.size -
741                                     offset_size, offset_size);
742
743   if (last_end > value.size)
744     return FALSE;
745
746   offsets_array_size = value.size - last_end;
747
748   if (offsets_array_size % offset_size)
749     return FALSE;
750
751   offsets_array = value.data + value.size - offsets_array_size;
752   length = offsets_array_size / offset_size;
753
754   if (length == 0)
755     return FALSE;
756
757   child.type_info = g_variant_type_info_element (value.type_info);
758   g_variant_type_info_query (child.type_info, &alignment, NULL);
759   offset = 0;
760
761   for (i = 0; i < length; i++)
762     {
763       gsize this_end;
764
765       this_end = gvs_read_unaligned_le (offsets_array + offset_size * i,
766                                         offset_size);
767
768       if (this_end < offset || this_end > last_end)
769         return FALSE;
770
771       while (offset & alignment)
772         {
773           if (!(offset < this_end && value.data[offset] == '\0'))
774             return FALSE;
775           offset++;
776         }
777
778       child.data = value.data + offset;
779       child.size = this_end - offset;
780
781       if (child.size == 0)
782         child.data = NULL;
783
784       if (!g_variant_serialised_is_normal (child))
785         return FALSE;
786
787       offset = this_end;
788     }
789
790   g_assert (offset == last_end);
791
792   return TRUE;
793 }
794
795 /* Tuples {{{2
796  *
797  * Since tuples can contain a mix of variable- and fixed-sized items,
798  * they are, in terms of serialisation, a hybrid of variable-sized and
799  * fixed-sized arrays.
800  *
801  * Offsets are only stored for variable-sized items.  Also, since the
802  * number of items in a tuple is known from its type, we are able to
803  * know exactly how many offsets to expect in the serialised data (and
804  * therefore how much space is taken up by the offset array).  This
805  * means that we know where the end of the serialised data for the last
806  * item is -- we can just subtract the size of the offset array from the
807  * total size of the tuple.  For this reason, the last item in the tuple
808  * doesn't need an offset stored.
809  *
810  * Tuple offsets are stored in reverse.  This design choice allows
811  * iterator-based deserialisers to be more efficient.
812  *
813  * Most of the "heavy lifting" here is handled by the GVariantTypeInfo
814  * for the tuple.  See the notes in gvarianttypeinfo.h.
815  */
816
817 static gsize
818 gvs_tuple_n_children (GVariantSerialised value)
819 {
820   return g_variant_type_info_n_members (value.type_info);
821 }
822
823 static GVariantSerialised
824 gvs_tuple_get_child (GVariantSerialised value,
825                      gsize              index_)
826 {
827   const GVariantMemberInfo *member_info;
828   GVariantSerialised child = {  };
829   gsize offset_size;
830   gsize start, end;
831
832   member_info = g_variant_type_info_member_info (value.type_info, index_);
833   child.type_info = g_variant_type_info_ref (member_info->type_info);
834   offset_size = gvs_get_offset_size (value.size);
835
836   /* tuples are the only (potentially) fixed-sized containers, so the
837    * only ones that have to deal with the possibility of having %NULL
838    * data with a non-zero %size if errors occured elsewhere.
839    */
840   if G_UNLIKELY (value.data == NULL && value.size != 0)
841     {
842       g_variant_type_info_query (child.type_info, NULL, &child.size);
843
844       /* this can only happen in fixed-sized tuples,
845        * so the child must also be fixed sized.
846        */
847       g_assert (child.size != 0);
848       child.data = NULL;
849
850       return child;
851     }
852
853   if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
854     {
855       if (offset_size * (member_info->i + 2) > value.size)
856         return child;
857     }
858   else
859     {
860       if (offset_size * (member_info->i + 1) > value.size)
861         {
862           /* if the child is fixed size, return its size.
863            * if child is not fixed-sized, return size = 0.
864            */
865           g_variant_type_info_query (child.type_info, NULL, &child.size);
866
867           return child;
868         }
869     }
870
871   if (member_info->i + 1)
872     start = gvs_read_unaligned_le (value.data + value.size -
873                                    offset_size * (member_info->i + 1),
874                                    offset_size);
875   else
876     start = 0;
877
878   start += member_info->a;
879   start &= member_info->b;
880   start |= member_info->c;
881
882   if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_LAST)
883     end = value.size - offset_size * (member_info->i + 1);
884
885   else if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_FIXED)
886     {
887       gsize fixed_size;
888
889       g_variant_type_info_query (child.type_info, NULL, &fixed_size);
890       end = start + fixed_size;
891       child.size = fixed_size;
892     }
893
894   else /* G_VARIANT_MEMEBER_ENDING_OFFSET */
895     end = gvs_read_unaligned_le (value.data + value.size -
896                                  offset_size * (member_info->i + 2),
897                                  offset_size);
898
899   if (start < end && end <= value.size)
900     {
901       child.data = value.data + start;
902       child.size = end - start;
903     }
904
905   return child;
906 }
907
908 static gsize
909 gvs_tuple_needed_size (GVariantTypeInfo         *type_info,
910                        GVariantSerialisedFiller  gvs_filler,
911                        const gpointer           *children,
912                        gsize                     n_children)
913 {
914   const GVariantMemberInfo *member_info = NULL;
915   gsize fixed_size;
916   gsize offset;
917   gsize i;
918
919   g_variant_type_info_query (type_info, NULL, &fixed_size);
920
921   if (fixed_size)
922     return fixed_size;
923
924   offset = 0;
925
926   for (i = 0; i < n_children; i++)
927     {
928       guint alignment;
929
930       member_info = g_variant_type_info_member_info (type_info, i);
931       g_variant_type_info_query (member_info->type_info,
932                                  &alignment, &fixed_size);
933       offset += (-offset) & alignment;
934
935       if (fixed_size)
936         offset += fixed_size;
937       else
938         {
939           GVariantSerialised child = {  };
940
941           gvs_filler (&child, children[i]);
942           offset += child.size;
943         }
944     }
945
946   return gvs_calculate_total_size (offset, member_info->i + 1);
947 }
948
949 static void
950 gvs_tuple_serialise (GVariantSerialised        value,
951                      GVariantSerialisedFiller  gvs_filler,
952                      const gpointer           *children,
953                      gsize                     n_children)
954 {
955   gsize offset_size;
956   gsize offset;
957   gsize i;
958
959   offset_size = gvs_get_offset_size (value.size);
960   offset = 0;
961
962   for (i = 0; i < n_children; i++)
963     {
964       const GVariantMemberInfo *member_info;
965       GVariantSerialised child = {  };
966       guint alignment;
967
968       member_info = g_variant_type_info_member_info (value.type_info, i);
969       g_variant_type_info_query (member_info->type_info, &alignment, NULL);
970
971       while (offset & alignment)
972         value.data[offset++] = '\0';
973
974       child.data = value.data + offset;
975       gvs_filler (&child, children[i]);
976       offset += child.size;
977
978       if (member_info->ending_type == G_VARIANT_MEMBER_ENDING_OFFSET)
979         {
980           value.size -= offset_size;
981           gvs_write_unaligned_le (value.data + value.size,
982                                   offset, offset_size);
983         }
984     }
985
986   while (offset < value.size)
987     value.data[offset++] = '\0';
988 }
989
990 static gboolean
991 gvs_tuple_is_normal (GVariantSerialised value)
992 {
993   guint offset_size;
994   gsize offset_ptr;
995   gsize length;
996   gsize offset;
997   gsize i;
998
999   offset_size = gvs_get_offset_size (value.size);
1000   length = g_variant_type_info_n_members (value.type_info);
1001   offset_ptr = value.size;
1002   offset = 0;
1003
1004   for (i = 0; i < length; i++)
1005     {
1006       const GVariantMemberInfo *member_info;
1007       GVariantSerialised child;
1008       gsize fixed_size;
1009       guint alignment;
1010       gsize end;
1011
1012       member_info = g_variant_type_info_member_info (value.type_info, i);
1013       child.type_info = member_info->type_info;
1014
1015       g_variant_type_info_query (child.type_info, &alignment, &fixed_size);
1016
1017       while (offset & alignment)
1018         {
1019           if (offset > value.size || value.data[offset] != '\0')
1020             return FALSE;
1021           offset++;
1022         }
1023
1024       child.data = value.data + offset;
1025
1026       switch (member_info->ending_type)
1027         {
1028         case G_VARIANT_MEMBER_ENDING_FIXED:
1029           end = offset + fixed_size;
1030           break;
1031
1032         case G_VARIANT_MEMBER_ENDING_LAST:
1033           end = offset_ptr;
1034           break;
1035
1036         case G_VARIANT_MEMBER_ENDING_OFFSET:
1037           offset_ptr -= offset_size;
1038
1039           if (offset_ptr < offset)
1040             return FALSE;
1041
1042           end = gvs_read_unaligned_le (value.data + offset_ptr, offset_size);
1043           break;
1044         }
1045
1046       if (end < offset || end > offset_ptr)
1047         return FALSE;
1048
1049       child.size = end - offset;
1050
1051       if (child.size == 0)
1052         child.data = NULL;
1053
1054       if (!g_variant_serialised_is_normal (child))
1055         return FALSE;
1056
1057       offset = end;
1058     }
1059
1060   {
1061     gsize fixed_size;
1062     guint alignment;
1063
1064     g_variant_type_info_query (value.type_info, &alignment, &fixed_size);
1065
1066     if (fixed_size)
1067       {
1068         g_assert (fixed_size == value.size);
1069         g_assert (offset_ptr == value.size);
1070
1071         if (i == 0)
1072           {
1073             if (value.data[offset++] != '\0')
1074               return FALSE;
1075           }
1076         else
1077           {
1078             while (offset & alignment)
1079               if (value.data[offset++] != '\0')
1080                 return FALSE;
1081           }
1082
1083         g_assert (offset == value.size);
1084       }
1085   }
1086
1087   return offset_ptr == offset;
1088 }
1089
1090 /* Variants {{{2
1091  *
1092  * Variants are stored by storing the serialised data of the child,
1093  * followed by a '\0' character, followed by the type string of the
1094  * child.
1095  *
1096  * In the case that a value is presented that contains no '\0'
1097  * character, or doesn't have a single well-formed definite type string
1098  * following that character, the variant must be taken as containing the
1099  * unit tuple: ().
1100  */
1101
1102 static inline gsize
1103 gvs_variant_n_children (GVariantSerialised value)
1104 {
1105   return 1;
1106 }
1107
1108 static inline GVariantSerialised
1109 gvs_variant_get_child (GVariantSerialised value,
1110                        gsize              index_)
1111 {
1112   GVariantSerialised child = {  };
1113
1114   /* NOTE: not O(1) and impossible for it to be... */
1115   if (value.size)
1116     {
1117       /* find '\0' character */
1118       for (child.size = value.size - 1; child.size; child.size--)
1119         if (value.data[child.size] == '\0')
1120           break;
1121
1122       /* ensure we didn't just hit the start of the string */
1123       if (value.data[child.size] == '\0')
1124         {
1125           const gchar *type_string = (gchar *) &value.data[child.size + 1];
1126           const gchar *limit = (gchar *) &value.data[value.size];
1127           const gchar *end;
1128
1129           if (g_variant_type_string_scan (type_string, limit, &end) &&
1130               end == limit)
1131             {
1132               const GVariantType *type = (GVariantType *) type_string;
1133
1134               if (g_variant_type_is_definite (type))
1135                 {
1136                   gsize fixed_size;
1137
1138                   child.type_info = g_variant_type_info_get (type);
1139
1140                   if (child.size != 0)
1141                     /* only set to non-%NULL if size > 0 */
1142                     child.data = value.data;
1143
1144                   g_variant_type_info_query (child.type_info,
1145                                              NULL, &fixed_size);
1146
1147                   if (!fixed_size || fixed_size == child.size)
1148                     return child;
1149
1150                   g_variant_type_info_unref (child.type_info);
1151                 }
1152             }
1153         }
1154     }
1155
1156   child.type_info = g_variant_type_info_get (G_VARIANT_TYPE_UNIT);
1157   child.data = NULL;
1158   child.size = 1;
1159
1160   return child;
1161 }
1162
1163 static inline gsize
1164 gvs_variant_needed_size (GVariantTypeInfo         *type_info,
1165                          GVariantSerialisedFiller  gvs_filler,
1166                          const gpointer           *children,
1167                          gsize                     n_children)
1168 {
1169   GVariantSerialised child = {  };
1170   const gchar *type_string;
1171
1172   gvs_filler (&child, children[0]);
1173   type_string = g_variant_type_info_get_type_string (child.type_info);
1174
1175   return child.size + 1 + strlen (type_string);
1176 }
1177
1178 static inline void
1179 gvs_variant_serialise (GVariantSerialised        value,
1180                        GVariantSerialisedFiller  gvs_filler,
1181                        const gpointer           *children,
1182                        gsize                     n_children)
1183 {
1184   GVariantSerialised child = {  };
1185   const gchar *type_string;
1186
1187   child.data = value.data;
1188
1189   gvs_filler (&child, children[0]);
1190   type_string = g_variant_type_info_get_type_string (child.type_info);
1191   value.data[child.size] = '\0';
1192   memcpy (value.data + child.size + 1, type_string, strlen (type_string));
1193 }
1194
1195 static inline gboolean
1196 gvs_variant_is_normal (GVariantSerialised value)
1197 {
1198   GVariantSerialised child;
1199   gboolean normal;
1200
1201   child = gvs_variant_get_child (value, 0);
1202
1203   normal = (child.data != NULL || child.size == 0) &&
1204            g_variant_serialised_is_normal (child);
1205
1206   g_variant_type_info_unref (child.type_info);
1207
1208   return normal;
1209 }
1210
1211
1212
1213 /* PART 2: Serialiser API {{{1
1214  *
1215  * This is the implementation of the API of the serialiser as advertised
1216  * in gvariant-serialiser.h.
1217  */
1218
1219 /* Dispatch Utilities {{{2
1220  *
1221  * These macros allow a given function (for example,
1222  * g_variant_serialiser_serialise) to be dispatched to the appropriate
1223  * type-specific function above (fixed/variable-sized maybe,
1224  * fixed/variable-sized array, tuple or variant).
1225  */
1226 #define DISPATCH_FIXED(type_info, before, after) \
1227   {                                                     \
1228     gsize fixed_size;                                   \
1229                                                         \
1230     g_variant_type_info_query_element (type_info, NULL, \
1231                                        &fixed_size);    \
1232                                                         \
1233     if (fixed_size)                                     \
1234       {                                                 \
1235         before ## fixed_sized ## after                  \
1236       }                                                 \
1237     else                                                \
1238       {                                                 \
1239         before ## variable_sized ## after               \
1240       }                                                 \
1241   }
1242
1243 #define DISPATCH_CASES(type_info, before, after) \
1244   switch (g_variant_type_info_get_type_char (type_info))        \
1245     {                                                           \
1246       case G_VARIANT_TYPE_INFO_CHAR_MAYBE:                      \
1247         DISPATCH_FIXED (type_info, before, _maybe ## after)     \
1248                                                                 \
1249       case G_VARIANT_TYPE_INFO_CHAR_ARRAY:                      \
1250         DISPATCH_FIXED (type_info, before, _array ## after)     \
1251                                                                 \
1252       case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY:                 \
1253       case G_VARIANT_TYPE_INFO_CHAR_TUPLE:                      \
1254         {                                                       \
1255           before ## tuple ## after                              \
1256         }                                                       \
1257                                                                 \
1258       case G_VARIANT_TYPE_INFO_CHAR_VARIANT:                    \
1259         {                                                       \
1260           before ## variant ## after                            \
1261         }                                                       \
1262     }
1263
1264 /* Serialiser entry points {{{2
1265  *
1266  * These are the functions that are called in order for the serialiser
1267  * to do its thing.
1268  */
1269
1270 /* < private >
1271  * g_variant_serialised_n_children:
1272  * @serialised: a #GVariantSerialised
1273  * @returns: the number of children
1274  *
1275  * For serialised data that represents a container value (maybes,
1276  * tuples, arrays, variants), determine how many child items are inside
1277  * that container.
1278  */
1279 gsize
1280 g_variant_serialised_n_children (GVariantSerialised serialised)
1281 {
1282   g_variant_serialised_check (serialised);
1283
1284   DISPATCH_CASES (serialised.type_info,
1285
1286                   return gvs_/**/,/**/_n_children (serialised);
1287
1288                  )
1289   g_assert_not_reached ();
1290 }
1291
1292 /* < private >
1293  * g_variant_serialised_get_child:
1294  * @serialised: a #GVariantSerialised
1295  * @index_: the index of the child to fetch
1296  * @returns: a #GVariantSerialised for the child
1297  *
1298  * Extracts a child from a serialised data representing a container
1299  * value.
1300  *
1301  * It is an error to call this function with an index out of bounds.
1302  *
1303  * If the result .data == %NULL and .size > 0 then there has been an
1304  * error extracting the requested fixed-sized value.  This number of
1305  * zero bytes needs to be allocated instead.
1306  *
1307  * In the case that .data == %NULL and .size == 0 then a zero-sized
1308  * item of a variable-sized type is being returned.
1309  *
1310  * .data is never non-%NULL if size is 0.
1311  */
1312 GVariantSerialised
1313 g_variant_serialised_get_child (GVariantSerialised serialised,
1314                                 gsize              index_)
1315 {
1316   GVariantSerialised child;
1317
1318   g_variant_serialised_check (serialised);
1319
1320   if G_LIKELY (index_ < g_variant_serialised_n_children (serialised))
1321     {
1322       DISPATCH_CASES (serialised.type_info,
1323
1324                       child = gvs_/**/,/**/_get_child (serialised, index_);
1325                       g_assert (child.size || child.data == NULL);
1326                       g_variant_serialised_check (child);
1327                       return child;
1328
1329                      )
1330       g_assert_not_reached ();
1331     }
1332
1333   g_error ("Attempt to access item %"G_GSIZE_FORMAT
1334            " in a container with only %"G_GSIZE_FORMAT" items",
1335            index_, g_variant_serialised_n_children (serialised));
1336 }
1337
1338 /* < private >
1339  * g_variant_serialiser_serialise:
1340  * @serialised: a #GVariantSerialised, properly set up
1341  * @gvs_filler: the filler function
1342  * @children: an array of child items
1343  * @n_children: the size of @children
1344  *
1345  * Writes data in serialised form.
1346  *
1347  * The type_info field of @serialised must be filled in to type info for
1348  * the type that we are serialising.
1349  *
1350  * The size field of @serialised must be filled in with the value
1351  * returned by a previous call to g_variant_serialiser_needed_size().
1352  *
1353  * The data field of @serialised must be a pointer to a properly-aligned
1354  * memory region large enough to serialise into (ie: at least as big as
1355  * the size field).
1356  *
1357  * This function is only resonsible for serialising the top-level
1358  * container.  @gvs_filler is called on each child of the container in
1359  * order for all of the data of that child to be filled in.
1360  */
1361 void
1362 g_variant_serialiser_serialise (GVariantSerialised        serialised,
1363                                 GVariantSerialisedFiller  gvs_filler,
1364                                 const gpointer           *children,
1365                                 gsize                     n_children)
1366 {
1367   g_variant_serialised_check (serialised);
1368
1369   DISPATCH_CASES (serialised.type_info,
1370
1371                   gvs_/**/,/**/_serialise (serialised, gvs_filler,
1372                                            children, n_children);
1373                   return;
1374
1375                  )
1376   g_assert_not_reached ();
1377 }
1378
1379 /* < private >
1380  * g_variant_serialiser_needed_size:
1381  * @type_info: the type to serialise for
1382  * @gvs_filler: the filler function
1383  * @children: an array of child items
1384  * @n_children: the size of @children
1385  *
1386  * Determines how much memory would be needed to serialise this value.
1387  *
1388  * This function is only resonsible for performing calculations for the
1389  * top-level container.  @gvs_filler is called on each child of the
1390  * container in order to determine its size.
1391  */
1392 gsize
1393 g_variant_serialiser_needed_size (GVariantTypeInfo         *type_info,
1394                                   GVariantSerialisedFiller  gvs_filler,
1395                                   const gpointer           *children,
1396                                   gsize                     n_children)
1397 {
1398   DISPATCH_CASES (type_info,
1399
1400                   return gvs_/**/,/**/_needed_size (type_info, gvs_filler,
1401                                                     children, n_children);
1402
1403                  )
1404   g_assert_not_reached ();
1405 }
1406
1407 /* Byteswapping {{{2 */
1408
1409 /* < private >
1410  * g_variant_serialised_byteswap:
1411  * @value: a #GVariantSerialised
1412  *
1413  * Byte-swap serialised data.  The result of this function is only
1414  * well-defined if the data is in normal form.
1415  */
1416 void
1417 g_variant_serialised_byteswap (GVariantSerialised serialised)
1418 {
1419   gsize fixed_size;
1420   guint alignment;
1421
1422   g_variant_serialised_check (serialised);
1423
1424   if (!serialised.data)
1425     return;
1426
1427   /* the types we potentially need to byteswap are
1428    * exactly those with alignment requirements.
1429    */
1430   g_variant_type_info_query (serialised.type_info, &alignment, &fixed_size);
1431   if (!alignment)
1432     return;
1433
1434   /* if fixed size and alignment are equal then we are down
1435    * to the base integer type and we should swap it.  the
1436    * only exception to this is if we have a tuple with a
1437    * single item, and then swapping it will be OK anyway.
1438    */
1439   if (alignment + 1 == fixed_size)
1440     {
1441       switch (fixed_size)
1442       {
1443         case 2:
1444           {
1445             guint16 *ptr = (guint16 *) serialised.data;
1446
1447             g_assert_cmpint (serialised.size, ==, 2);
1448             *ptr = GUINT16_SWAP_LE_BE (*ptr);
1449           }
1450           return;
1451
1452         case 4:
1453           {
1454             guint32 *ptr = (guint32 *) serialised.data;
1455
1456             g_assert_cmpint (serialised.size, ==, 4);
1457             *ptr = GUINT32_SWAP_LE_BE (*ptr);
1458           }
1459           return;
1460
1461         case 8:
1462           {
1463             guint64 *ptr = (guint64 *) serialised.data;
1464
1465             g_assert_cmpint (serialised.size, ==, 8);
1466             *ptr = GUINT64_SWAP_LE_BE (*ptr);
1467           }
1468           return;
1469
1470         default:
1471           g_assert_not_reached ();
1472       }
1473     }
1474
1475   /* else, we have a container that potentially contains
1476    * some children that need to be byteswapped.
1477    */
1478   else
1479     {
1480       gsize children, i;
1481
1482       children = g_variant_serialised_n_children (serialised);
1483       for (i = 0; i < children; i++)
1484         {
1485           GVariantSerialised child;
1486
1487           child = g_variant_serialised_get_child (serialised, i);
1488           g_variant_serialised_byteswap (child);
1489           g_variant_type_info_unref (child.type_info);
1490         }
1491     }
1492 }
1493
1494 /* Normal form checking {{{2 */
1495
1496 /* < private >
1497  * g_variant_serialised_is_normal:
1498  * @serialised: a #GVariantSerialised
1499  *
1500  * Determines, recursively if @serialised is in normal form.  There is
1501  * precisely one normal form of serialised data for each possible value.
1502  *
1503  * It is possible that multiple byte sequences form the serialised data
1504  * for a given value if, for example, the padding bytes are filled in
1505  * with something other than zeros, but only one form is the normal
1506  * form.
1507  */
1508 gboolean
1509 g_variant_serialised_is_normal (GVariantSerialised serialised)
1510 {
1511   DISPATCH_CASES (serialised.type_info,
1512
1513                   return gvs_/**/,/**/_is_normal (serialised);
1514
1515                  )
1516
1517   /* some hard-coded terminal cases */
1518   switch (g_variant_type_info_get_type_char (serialised.type_info))
1519     {
1520     case 'b': /* boolean */
1521       return serialised.data[0] < 2;
1522
1523     case 's': /* string */
1524       return g_variant_serialiser_is_string (serialised.data,
1525                                              serialised.size);
1526
1527     case 'o':
1528       return g_variant_serialiser_is_object_path (serialised.data,
1529                                                   serialised.size);
1530
1531     case 'g':
1532       return g_variant_serialiser_is_signature (serialised.data,
1533                                                 serialised.size);
1534
1535     default:
1536       /* all of the other types are fixed-sized numerical types for
1537        * which all possible values are valid (including various NaN
1538        * representations for floating point values).
1539        */
1540       return TRUE;
1541     }
1542 }
1543
1544 /* Validity-checking functions {{{2
1545  *
1546  * Checks if strings, object paths and signature strings are valid.
1547  */
1548
1549 /* < private >
1550  * g_variant_serialiser_is_string:
1551  * @data: a possible string
1552  * @size: the size of @data
1553  *
1554  * Ensures that @data is a valid string with a nul terminator at the end
1555  * and no nul bytes embedded.
1556  */
1557 gboolean
1558 g_variant_serialiser_is_string (gconstpointer data,
1559                                 gsize         size)
1560 {
1561   const gchar *string = data;
1562
1563   if (size == 0)
1564     return FALSE;
1565
1566   if (string[size - 1] != '\0')
1567     return FALSE;
1568
1569   return strlen (string) == size - 1;
1570 }
1571
1572 /* < private >
1573  * g_variant_serialiser_is_object_path:
1574  * @data: a possible DBus object path
1575  * @size: the size of @data
1576  *
1577  * Performs the checks for being a valid string.
1578  *
1579  * Also, ensures that @data is a valid DBus object path, as per the DBus
1580  * specification.
1581  */
1582 gboolean
1583 g_variant_serialiser_is_object_path (gconstpointer data,
1584                                      gsize         size)
1585 {
1586   const gchar *string = data;
1587   gsize i;
1588
1589   if (!g_variant_serialiser_is_string (data, size))
1590     return FALSE;
1591
1592   /* The path must begin with an ASCII '/' (integer 47) character */
1593   if (string[0] != '/')
1594     return FALSE;
1595
1596   for (i = 1; string[i]; i++)
1597     /* Each element must only contain the ASCII characters
1598      * "[A-Z][a-z][0-9]_"
1599      */
1600     if (g_ascii_isalnum (string[i]) || string[i] == '_')
1601       ;
1602
1603     /* must consist of elements separated by slash characters. */
1604     else if (string[i] == '/')
1605       {
1606         /* No element may be the empty string. */
1607         /* Multiple '/' characters cannot occur in sequence. */
1608         if (string[i - 1] == '/')
1609           return FALSE;
1610       }
1611
1612     else
1613       return FALSE;
1614
1615   /* A trailing '/' character is not allowed unless the path is the
1616    * root path (a single '/' character).
1617    */
1618   if (i > 1 && string[i - 1] == '/')
1619     return FALSE;
1620
1621   return TRUE;
1622 }
1623
1624 /* < private >
1625  * g_variant_serialiser_is_signature:
1626  * @data: a possible DBus signature
1627  * @size: the size of @data
1628  *
1629  * Performs the checks for being a valid string.
1630  *
1631  * Also, ensures that @data is a valid DBus type signature, as per the
1632  * DBus specification.
1633  */
1634 gboolean
1635 g_variant_serialiser_is_signature (gconstpointer data,
1636                                    gsize         size)
1637 {
1638   const gchar *string = data;
1639   gsize first_invalid;
1640
1641   if (!g_variant_serialiser_is_string (data, size))
1642     return FALSE;
1643
1644   /* make sure no non-definite characters appear */
1645   first_invalid = strspn (string, "ybnqiuxthdvasog(){}");
1646   if (string[first_invalid])
1647     return FALSE;
1648
1649   /* make sure each type string is well-formed */
1650   while (*string)
1651     if (!g_variant_type_string_scan (string, NULL, &string))
1652       return FALSE;
1653
1654   return TRUE;
1655 }
1656
1657 /* Epilogue {{{1 */
1658 #define __G_VARIANT_SERIALISER_C__
1659 #include "galiasdef.c"
1660
1661 /* vim:set foldmethod=marker: */