#include <string.h>
-#include "galias.h"
/* GVariantSerialiser
*
* values is permitted (eg: 0 to 255 is a valid byte). Special checks
* need to be performed for booleans (only 0 or 1 allowed), strings
* (properly nul-terminated) and object paths and signature strings
- * (meeting the DBus specification requirements).
+ * (meeting the D-Bus specification requirements).
*/
/* < private >
else
g_assert (serialised.size == 0 || serialised.data != NULL);
+ /* Depending on the native alignment requirements of the machine, the
+ * compiler will insert either 3 or 7 padding bytes after the char.
+ * This will result in the sizeof() the struct being 12 or 16.
+ * Subtract 9 to get 3 or 7 which is a nice bitmask to apply to get
+ * the alignment bits that we "care about" being zero: in the
+ * 4-aligned case, we care about 2 bits, and in the 8-aligned case, we
+ * care about 3 bits.
+ */
+ alignment &= sizeof (struct {
+ char a;
+ union {
+ guint64 x;
+ void *y;
+ gdouble z;
+ } b;
+ }
+ ) - 9;
+
+ /* Some OSes (FreeBSD is a known example) have a malloc() that returns
+ * unaligned memory if you request small sizes. 'malloc (1);', for
+ * example, has been seen to return pointers aligned to 6 mod 16.
+ *
+ * Check if this is a small allocation and return without enforcing
+ * the alignment assertion if this is the case.
+ */
+ if (serialised.size <= alignment)
+ return;
+
g_assert_cmpint (alignment & (gsize) serialised.data, ==, 0);
}
{
if (n_children)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
gvs_filler (&child, children[0]);
gvs_fixed_sized_array_get_child (GVariantSerialised value,
gsize index_)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
child.type_info = g_variant_type_info_element (value.type_info);
g_variant_type_info_query (child.type_info, NULL, &child.size);
const gpointer *children,
gsize n_children)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
gsize i;
child.type_info = g_variant_type_info_element (value.type_info);
static gboolean
gvs_fixed_sized_array_is_normal (GVariantSerialised value)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
child.type_info = g_variant_type_info_element (value.type_info);
g_variant_type_info_query (child.type_info, NULL, &child.size);
gvs_variable_sized_array_get_child (GVariantSerialised value,
gsize index_)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
gsize offset_size;
gsize last_end;
gsize start;
for (i = 0; i < n_children; i++)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
offset += (-offset) & alignment;
gvs_filler (&child, children[i]);
for (i = 0; i < n_children; i++)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
while (offset & alignment)
value.data[offset++] = '\0';
static gboolean
gvs_variable_sized_array_is_normal (GVariantSerialised value)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
gsize offsets_array_size;
guchar *offsets_array;
guint offset_size;
gsize index_)
{
const GVariantMemberInfo *member_info;
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
gsize offset_size;
gsize start, end;
offset += fixed_size;
else
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
gvs_filler (&child, children[i]);
offset += child.size;
for (i = 0; i < n_children; i++)
{
const GVariantMemberInfo *member_info;
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
guint alignment;
member_info = g_variant_type_info_member_info (value.type_info, i);
end = gvs_read_unaligned_le (value.data + offset_ptr, offset_size);
break;
+
+ default:
+ g_assert_not_reached ();
}
if (end < offset || end > offset_ptr)
gvs_variant_get_child (GVariantSerialised value,
gsize index_)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
/* NOTE: not O(1) and impossible for it to be... */
if (value.size)
const gpointer *children,
gsize n_children)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
const gchar *type_string;
gvs_filler (&child, children[0]);
const gpointer *children,
gsize n_children)
{
- GVariantSerialised child = { };
+ GVariantSerialised child = { 0, };
const gchar *type_string;
child.data = value.data;
)
+ if (serialised.data == NULL)
+ return FALSE;
+
/* some hard-coded terminal cases */
switch (g_variant_type_info_get_type_char (serialised.type_info))
{
g_variant_serialiser_is_string (gconstpointer data,
gsize size)
{
- const gchar *string = data;
+ const gchar *end;
- if (size == 0)
- return FALSE;
+ g_utf8_validate (data, size, &end);
- if (string[size - 1] != '\0')
- return FALSE;
-
- return strlen (string) == size - 1;
+ return data == end - (size - 1);
}
/* < private >
* g_variant_serialiser_is_object_path:
- * @data: a possible DBus object path
+ * @data: a possible D-Bus object path
* @size: the size of @data
*
* Performs the checks for being a valid string.
*
- * Also, ensures that @data is a valid DBus object path, as per the DBus
+ * Also, ensures that @data is a valid DBus object path, as per the D-Bus
* specification.
*/
gboolean
/* < private >
* g_variant_serialiser_is_signature:
- * @data: a possible DBus signature
+ * @data: a possible D-Bus signature
* @size: the size of @data
*
* Performs the checks for being a valid string.
*
- * Also, ensures that @data is a valid DBus type signature, as per the
- * DBus specification.
+ * Also, ensures that @data is a valid D-Bus type signature, as per the
+ * D-Bus specification.
*/
gboolean
g_variant_serialiser_is_signature (gconstpointer data,
}
/* Epilogue {{{1 */
-#define __G_VARIANT_SERIALISER_C__
-#include "galiasdef.c"
-
/* vim:set foldmethod=marker: */