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