1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal-basic.c Marshalling routines for basic (primitive) types
4 * Copyright (C) 2002 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-marshal-basic.h"
31 * @defgroup DBusMarshal marshaling and unmarshaling
32 * @ingroup DBusInternals
33 * @brief functions to marshal/unmarshal data from the wire
35 * Types and functions related to converting primitive data types from
36 * wire format to native machine format, and vice versa.
38 * A signature is just a string with multiple types one after the other.
39 * for example a type is "i" or "(ii)", a signature is "i(ii)"
40 * where i is int and (ii) is struct { int; int; }
46 pack_4_octets (dbus_uint32_t value,
50 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
52 if ((byte_order) == DBUS_LITTLE_ENDIAN)
53 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_LE (value);
55 *((dbus_uint32_t*)(data)) = DBUS_UINT32_TO_BE (value);
59 pack_8_octets (DBusBasicValue value,
63 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
65 #ifdef DBUS_HAVE_INT64
66 if ((byte_order) == DBUS_LITTLE_ENDIAN)
67 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_LE (value.u64);
69 *((dbus_uint64_t*)(data)) = DBUS_UINT64_TO_BE (value.u64);
71 *(DBus8ByteStruct*)data = value.u64;
72 swap_8_octets ((DBusBasicValue*)data, byte_order);
77 * Packs a 32 bit unsigned integer into a data pointer.
79 * @param value the value
80 * @param byte_order the byte order to use
81 * @param data the data pointer
84 _dbus_pack_uint32 (dbus_uint32_t value,
88 pack_4_octets (value, byte_order, data);
92 * Packs a 32 bit signed integer into a data pointer.
94 * @param value the value
95 * @param byte_order the byte order to use
96 * @param data the data pointer
99 _dbus_pack_int32 (dbus_int32_t value,
103 pack_4_octets ((dbus_uint32_t) value, byte_order, data);
107 unpack_4_octets (int byte_order,
108 const unsigned char *data)
110 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 4) == data);
112 if (byte_order == DBUS_LITTLE_ENDIAN)
113 return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
115 return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
118 #ifndef DBUS_HAVE_INT64
121 swap_bytes (unsigned char *data,
124 unsigned char *p1 = data;
125 unsigned char *p2 = data + len - 1;
129 unsigned char tmp = *p1;
137 #endif /* !DBUS_HAVE_INT64 */
140 swap_8_octets (DBusBasicValue *value,
143 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
145 #ifdef DBUS_HAVE_INT64
146 value->u64 = DBUS_UINT64_SWAP_LE_BE (value->u64);
148 swap_bytes ((unsigned char *)value, 8);
153 static DBusBasicValue
154 unpack_8_octets (int byte_order,
155 const unsigned char *data)
159 _dbus_assert (_DBUS_ALIGN_ADDRESS (data, 8) == data);
160 _dbus_assert (sizeof (r) == 8);
162 #ifdef DBUS_HAVE_INT64
163 if (byte_order == DBUS_LITTLE_ENDIAN)
164 r.u64 = DBUS_UINT64_FROM_LE (*(dbus_uint64_t*)data);
166 r.u64 = DBUS_UINT64_FROM_BE (*(dbus_uint64_t*)data);
168 r.u64 = *(DBus8ByteStruct*)data;
169 swap_8_octets (&r, byte_order);
176 * Unpacks a 32 bit unsigned integer from a data pointer
178 * @param byte_order The byte order to use
179 * @param data the data pointer
180 * @returns the integer
183 _dbus_unpack_uint32 (int byte_order,
184 const unsigned char *data)
186 return unpack_4_octets (byte_order, data);
190 * Unpacks a 32 bit signed integer from a data pointer
192 * @param byte_order The byte order to use
193 * @param data the data pointer
194 * @returns the integer
197 _dbus_unpack_int32 (int byte_order,
198 const unsigned char *data)
200 return (dbus_int32_t) unpack_4_octets (byte_order, data);
204 set_4_octets (DBusString *str,
211 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
212 byte_order == DBUS_BIG_ENDIAN);
214 data = _dbus_string_get_data_len (str, offset, 4);
216 _dbus_pack_uint32 (value, byte_order, data);
220 set_8_octets (DBusString *str,
222 DBusBasicValue value,
227 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN ||
228 byte_order == DBUS_BIG_ENDIAN);
230 data = _dbus_string_get_data_len (str, offset, 8);
232 pack_8_octets (value, byte_order, data);
236 * Sets the 4 bytes at the given offset to a marshaled unsigned
237 * integer, replacing anything found there previously.
239 * @param str the string to write the marshalled int to
240 * @param pos the byte offset where int should be written
241 * @param value the value
242 * @param byte_order the byte order to use
246 _dbus_marshal_set_uint32 (DBusString *str,
251 set_4_octets (str, pos, value, byte_order);
255 * Sets the existing marshaled string at the given offset with
256 * a new marshaled string. The given offset must point to
257 * an existing string or the wrong length will be deleted
258 * and replaced with the new string.
260 * Note: no attempt is made by this function to re-align
261 * any data which has been already marshalled after this
262 * string. Use with caution.
264 * @param str the string to write the marshalled string to
265 * @param pos the position of the marshaled string length
266 * @param value the value
267 * @param byte_order the byte order to use
268 * @param old_end_pos place to store byte after the nul byte of the old value
269 * @param new_end_pos place to store byte after the nul byte of the new value
270 * @returns #TRUE on success, #FALSE if no memory
274 set_string (DBusString *str,
281 int old_len, new_len;
284 _dbus_string_init_const (&dstr, value);
286 old_len = _dbus_demarshal_uint32 (str, pos, byte_order, NULL);
288 new_len = _dbus_string_get_length (&dstr);
290 if (!_dbus_string_replace_len (&dstr, 0, new_len,
291 str, pos + 4, old_len))
294 _dbus_marshal_set_uint32 (str, pos, new_len, byte_order);
297 *old_end_pos = pos + 4 + old_len + 1;
299 *new_end_pos = pos + 4 + new_len + 1;
305 * Sets the existing marshaled signature at the given offset to a new
306 * marshaled signature. Same basic ideas as set_string().
308 * @param str the string to write the marshalled signature to
309 * @param pos the position of the marshaled signature length
310 * @param value the value
311 * @param byte_order the byte order to use
312 * @param old_end_pos place to store byte after the nul byte of the old value
313 * @param new_end_pos place to store byte after the nul byte of the new value
314 * @returns #TRUE on success, #FALSE if no memory
318 set_signature (DBusString *str,
325 int old_len, new_len;
328 _dbus_string_init_const (&dstr, value);
330 old_len = _dbus_string_get_byte (str, pos);
331 new_len = _dbus_string_get_length (&dstr);
333 if (!_dbus_string_replace_len (&dstr, 0, new_len,
334 str, pos + 1, old_len))
337 _dbus_string_set_byte (str, pos, new_len);
340 *old_end_pos = pos + 1 + old_len + 1;
342 *new_end_pos = pos + 1 + new_len + 1;
348 * Sets an existing basic type value to a new value.
349 * Arguments work the same way as _dbus_marshal_basic_type().
351 * @param str the string
352 * @param pos location of the current value
353 * @param type the type of the current and new values
354 * @param value the address of the new value
355 * @param byte_order byte order for marshaling
356 * @param old_end_pos location to store end position of the old value, or #NULL
357 * @param new_end_pos location to store end position of the new value, or #NULL
358 * @returns #FALSE if no memory
361 _dbus_marshal_set_basic_type (DBusString *str,
369 const DBusBasicValue *vp;
376 case DBUS_TYPE_BOOLEAN:
377 _dbus_string_set_byte (str, pos, vp->byt);
379 *old_end_pos = pos + 1;
381 *new_end_pos = pos + 1;
384 case DBUS_TYPE_INT32:
385 case DBUS_TYPE_UINT32:
386 set_4_octets (str, pos, vp->u32, byte_order);
388 *old_end_pos = pos + 4;
390 *new_end_pos = pos + 4;
393 case DBUS_TYPE_INT64:
394 case DBUS_TYPE_UINT64:
395 case DBUS_TYPE_DOUBLE:
397 set_8_octets (str, pos, *vp, byte_order);
399 *old_end_pos = pos + 8;
401 *new_end_pos = pos + 8;
405 case DBUS_TYPE_STRING:
406 case DBUS_TYPE_OBJECT_PATH:
407 return set_string (str, pos, vp->str, byte_order,
408 old_end_pos, new_end_pos);
410 case DBUS_TYPE_SIGNATURE:
411 return set_signature (str, pos, vp->str, byte_order,
412 old_end_pos, new_end_pos);
415 _dbus_assert_not_reached ("not a basic type");
422 demarshal_4_octets (const DBusString *str,
427 pos = _DBUS_ALIGN_VALUE (pos, 4);
432 return unpack_4_octets (byte_order,
433 _dbus_string_get_const_data (str) + pos);
437 * Convenience function to demarshal a 32 bit unsigned integer.
439 * @param str the string containing the data
440 * @param byte_order the byte order
441 * @param pos the position in the string
442 * @param new_pos the new position of the string
443 * @returns the demarshaled integer.
446 _dbus_demarshal_uint32 (const DBusString *str,
451 return demarshal_4_octets (str, pos, byte_order, new_pos);
455 * Demarshals a basic type. The "value" pointer is always
456 * the address of a variable of the basic type. So e.g.
457 * if the basic type is "double" then the pointer is
458 * a double*, and if it's "char*" then the pointer is
461 * A value of type #DBusBasicValue is guaranteed to be large enough to
462 * hold any of the types that may be returned, which is handy if you
463 * are trying to do things generically. For example you can pass
464 * a DBusBasicValue* in to this function, and then pass the same
465 * DBusBasicValue* in to _dbus_marshal_basic_type() in order to
466 * move a value from one place to another.
468 * @param str the string containing the data
469 * @param pos position in the string
470 * @param type type of value to demarshal
471 * @param value pointer to return value data
472 * @param byte_order the byte order
473 * @param new_pos pointer to update with new position, or #NULL
476 _dbus_demarshal_basic_type (const DBusString *str,
483 const char *str_data;
486 _dbus_assert (_dbus_type_is_basic (type));
488 str_data = _dbus_string_get_const_data (str);
494 case DBUS_TYPE_BOOLEAN:
495 vp->byt = _dbus_string_get_byte (str, pos);
498 case DBUS_TYPE_INT32:
499 case DBUS_TYPE_UINT32:
500 pos = _DBUS_ALIGN_VALUE (pos, 4);
501 vp->u32 = *(dbus_uint32_t *)(str_data + pos);
502 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
503 vp->u32 = DBUS_UINT32_SWAP_LE_BE (vp->u32);
506 case DBUS_TYPE_INT64:
507 case DBUS_TYPE_UINT64:
508 case DBUS_TYPE_DOUBLE:
509 pos = _DBUS_ALIGN_VALUE (pos, 8);
510 #ifdef DBUS_HAVE_INT64
511 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
512 vp->u64 = DBUS_UINT64_SWAP_LE_BE (*(dbus_uint64_t*)(str_data + pos));
514 vp->u64 = *(dbus_uint64_t*)(str_data + pos);
516 vp->u64 = *(DBus8ByteStruct*) (str_data + pos);
517 swap_8_octets (vp, byte_order);
521 case DBUS_TYPE_STRING:
522 case DBUS_TYPE_OBJECT_PATH:
526 len = _dbus_demarshal_uint32 (str, pos, byte_order, &pos);
528 vp->str = (char*) str_data + pos;
530 pos += len + 1; /* length plus nul */
533 case DBUS_TYPE_SIGNATURE:
537 len = _dbus_string_get_byte (str, pos);
540 vp->str = (char*) str_data + pos;
542 pos += len + 1; /* length plus nul */
546 _dbus_warn ("type %s not a basic type\n",
547 _dbus_type_to_string (type));
548 _dbus_assert_not_reached ("not a basic type");
557 marshal_4_octets (DBusString *str,
566 _dbus_assert (sizeof (value) == 4);
568 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
569 value = DBUS_UINT32_SWAP_LE_BE (value);
571 orig_len = _dbus_string_get_length (str);
573 retval = _dbus_string_insert_4_aligned (str, insert_at,
574 (const unsigned char *)&value);
578 *pos_after = insert_at + (_dbus_string_get_length (str) - orig_len);
579 _dbus_assert (*pos_after <= _dbus_string_get_length (str));
586 marshal_8_octets (DBusString *str,
588 DBusBasicValue value,
595 _dbus_assert (sizeof (value) == 8);
597 swap_8_octets (&value, byte_order);
599 orig_len = _dbus_string_get_length (str);
601 retval = _dbus_string_insert_8_aligned (str, insert_at,
602 (const unsigned char *)&value);
605 *pos_after = insert_at + _dbus_string_get_length (str) - orig_len;
613 MARSHAL_AS_SIGNATURE,
614 MARSHAL_AS_BYTE_ARRAY
618 marshal_len_followed_by_bytes (int marshal_as,
621 const unsigned char *value,
622 int data_len, /* doesn't include nul if any */
627 DBusString value_str;
630 _dbus_assert (byte_order == DBUS_LITTLE_ENDIAN || byte_order == DBUS_BIG_ENDIAN);
631 if (insert_at > _dbus_string_get_length (str))
632 _dbus_warn ("insert_at = %d string len = %d data_len = %d\n",
633 insert_at, _dbus_string_get_length (str), data_len);
635 if (marshal_as == MARSHAL_AS_BYTE_ARRAY)
636 value_len = data_len;
638 value_len = data_len + 1; /* value has a nul */
640 /* FIXME this is probably broken for byte arrays because
641 * DBusString wants strings to be nul-terminated?
642 * Maybe I planned on this when writing init_const_len though
644 _dbus_string_init_const_len (&value_str, value, value_len);
648 if (marshal_as == MARSHAL_AS_SIGNATURE)
650 if (!_dbus_string_insert_byte (str, pos, data_len))
657 if (!marshal_4_octets (str, pos, data_len,
662 if (!_dbus_string_copy_len (&value_str, 0, value_len,
668 _dbus_assert (_dbus_string_equal_substring (&value_str, 0, value_len,
670 _dbus_verbose_bytes_of_string (str, pos, value_len);
681 /* Delete what we've inserted */
682 _dbus_string_delete (str, insert_at, pos - insert_at);
688 marshal_string (DBusString *str,
694 return marshal_len_followed_by_bytes (MARSHAL_AS_STRING,
695 str, insert_at, value,
697 byte_order, pos_after);
701 marshal_signature (DBusString *str,
706 return marshal_len_followed_by_bytes (MARSHAL_AS_SIGNATURE,
707 str, insert_at, value,
709 DBUS_COMPILER_BYTE_ORDER, /* irrelevant */
714 * Marshals a basic type. The "value" pointer is always the
715 * address of a variable containing the basic type value.
716 * So for example for int32 it will be dbus_int32_t*, and
717 * for string it will be const char**. This is for symmetry
718 * with _dbus_demarshal_basic_type() and to have a simple
721 * @param str string to marshal to
722 * @param insert_at where to insert the value
723 * @param type type of value
724 * @param value pointer to a variable containing the value
725 * @param byte_order byte order
726 * @param pos_after #NULL or the position after the type
727 * @returns #TRUE on success
730 _dbus_marshal_basic_type (DBusString *str,
737 const DBusBasicValue *vp;
739 _dbus_assert (_dbus_type_is_basic (type));
746 case DBUS_TYPE_BOOLEAN:
747 if (!_dbus_string_insert_byte (str, insert_at, vp->byt))
750 *pos_after = insert_at + 1;
753 case DBUS_TYPE_INT32:
754 case DBUS_TYPE_UINT32:
755 return marshal_4_octets (str, insert_at, vp->u32,
756 byte_order, pos_after);
758 case DBUS_TYPE_INT64:
759 case DBUS_TYPE_UINT64:
760 case DBUS_TYPE_DOUBLE:
761 return marshal_8_octets (str, insert_at, *vp, byte_order, pos_after);
764 case DBUS_TYPE_STRING:
765 case DBUS_TYPE_OBJECT_PATH:
766 return marshal_string (str, insert_at, vp->str, byte_order, pos_after);
768 case DBUS_TYPE_SIGNATURE:
769 return marshal_signature (str, insert_at, vp->str, pos_after);
772 _dbus_assert_not_reached ("not a basic type");
779 marshal_1_octets_array (DBusString *str,
781 const unsigned char *value,
786 return marshal_len_followed_by_bytes (MARSHAL_AS_BYTE_ARRAY,
787 str, insert_at, value, len,
788 byte_order, pos_after);
792 marshal_4_octets_array (DBusString *str,
794 const dbus_uint32_t *value,
801 _dbus_assert_not_reached ("FIXME insert_at");
803 old_string_len = _dbus_string_get_length (str);
805 if (!marshal_4_octets (str, insert_at, len*4, byte_order, NULL))
808 array_start = _dbus_string_get_length (str);
810 if (!_dbus_string_append_len (str, (const unsigned char*) value,
814 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
816 const unsigned char *d;
817 const unsigned char *end;
819 d = _dbus_string_get_data (str) + array_start;
823 *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d));
831 /* Restore previous length */
832 _dbus_string_set_length (str, old_string_len);
838 marshal_8_octets_array (DBusString *str,
840 const DBusBasicValue *value,
847 _dbus_assert_not_reached ("FIXME insert_at");
849 old_string_len = _dbus_string_get_length (str);
851 /* The array length is the length in bytes of the array,
852 * *excluding* alignment padding.
854 if (!marshal_4_octets (str, insert_at, len*8, byte_order, NULL))
857 array_start = _dbus_string_get_length (str);
859 /* Note that we do alignment padding unconditionally
860 * even if the array is empty; this means that
861 * padding + len is always equal to the number of bytes
865 if (!_dbus_string_align_length (str, 8))
868 if (!_dbus_string_append_len (str, (const unsigned char*) value,
872 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
874 const unsigned char *d;
875 const unsigned char *end;
877 d = _dbus_string_get_data (str) + array_start;
881 #ifdef DBUS_HAVE_INT64
882 *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d));
884 swap_8_bytes ((DBusBasicValue*) d);
893 /* Restore previous length */
894 _dbus_string_set_length (str, old_string_len);
900 * Marshals a basic type array
902 * @param str string to marshal to
903 * @param insert_at where to insert the value
904 * @param element_type type of array elements
905 * @param value pointer to value
906 * @param len length of value data in elements
907 * @param byte_order byte order
908 * @param pos_after #NULL or the position after the type
909 * @returns #TRUE on success
912 _dbus_marshal_basic_type_array (DBusString *str,
920 /* FIXME use the insert_at arg and fill in pos_after */
922 switch (element_type)
924 case DBUS_TYPE_BOOLEAN:
925 /* FIXME: we canonicalize to 0 or 1 for the single boolean case
926 * should we here too ? */
928 return marshal_1_octets_array (str, insert_at, value, len, byte_order, pos_after);
930 case DBUS_TYPE_INT32:
931 case DBUS_TYPE_UINT32:
932 return marshal_4_octets_array (str, insert_at, value, len, byte_order);
934 case DBUS_TYPE_INT64:
935 case DBUS_TYPE_UINT64:
936 case DBUS_TYPE_DOUBLE:
937 return marshal_8_octets_array (str, insert_at, value, len, byte_order);
940 case DBUS_TYPE_STRING:
941 case DBUS_TYPE_OBJECT_PATH:
942 _dbus_assert_not_reached ("handle string arrays");
945 case DBUS_TYPE_SIGNATURE:
946 _dbus_assert_not_reached ("handle signature");
950 _dbus_assert_not_reached ("non basic type in array");
959 * Skips over a basic type, reporting the following position.
961 * @param str the string containing the data
962 * @param type type of value to demarshal
963 * @param byte_order the byte order
964 * @param pos pointer to position in the string,
965 * updated on return to new position
968 _dbus_marshal_skip_basic_type (const DBusString *str,
976 case DBUS_TYPE_BOOLEAN:
979 case DBUS_TYPE_INT32:
980 case DBUS_TYPE_UINT32:
981 *pos = _DBUS_ALIGN_VALUE (*pos, 4);
984 case DBUS_TYPE_INT64:
985 case DBUS_TYPE_UINT64:
986 case DBUS_TYPE_DOUBLE:
987 *pos = _DBUS_ALIGN_VALUE (*pos, 8);
990 case DBUS_TYPE_STRING:
991 case DBUS_TYPE_OBJECT_PATH:
995 len = _dbus_demarshal_uint32 (str, *pos, byte_order, pos);
997 *pos += len + 1; /* length plus nul */
1000 case DBUS_TYPE_SIGNATURE:
1004 len = _dbus_string_get_byte (str, *pos);
1006 *pos += len + 2; /* length byte plus length plus nul */
1010 _dbus_warn ("type %s not a basic type\n",
1011 _dbus_type_to_string (type));
1012 _dbus_assert_not_reached ("not a basic type");
1018 * Skips an array, returning the next position.
1020 * @param str the string containing the data
1021 * @param element_type the type of array elements
1022 * @param byte_order the byte order
1023 * @param pos pointer to position in the string,
1024 * updated on return to new position
1027 _dbus_marshal_skip_array (const DBusString *str,
1032 dbus_uint32_t array_len;
1036 i = _DBUS_ALIGN_VALUE (*pos, 4);
1038 _dbus_demarshal_basic_type (str,
1045 alignment = _dbus_type_get_alignment (element_type);
1047 i = _DBUS_ALIGN_VALUE (i, alignment);
1049 *pos = i + array_len;
1053 * Gets the alignment requirement for the given type;
1054 * will be 1, 4, or 8.
1056 * @param typecode the type
1057 * @returns alignment of 1, 4, or 8
1060 _dbus_type_get_alignment (int typecode)
1064 case DBUS_TYPE_BYTE:
1065 case DBUS_TYPE_BOOLEAN:
1066 case DBUS_TYPE_VARIANT:
1067 case DBUS_TYPE_SIGNATURE:
1069 case DBUS_TYPE_INT32:
1070 case DBUS_TYPE_UINT32:
1071 /* this stuff is 4 since it starts with a length */
1072 case DBUS_TYPE_STRING:
1073 case DBUS_TYPE_OBJECT_PATH:
1074 case DBUS_TYPE_ARRAY:
1076 case DBUS_TYPE_INT64:
1077 case DBUS_TYPE_UINT64:
1078 case DBUS_TYPE_DOUBLE:
1079 /* struct is 8 since it could contain an 8-aligned item
1080 * and it's simpler to just always align structs to 8;
1081 * we want the amount of padding in a struct of a given
1082 * type to be predictable, not location-dependent.
1084 case DBUS_TYPE_STRUCT:
1088 _dbus_assert_not_reached ("unknown typecode in _dbus_type_get_alignment()");
1095 * Return #TRUE if the typecode is a valid typecode.
1096 * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and
1097 * random unknown bytes aren't either. This function is safe with
1100 * @returns #TRUE if valid
1103 _dbus_type_is_valid (int typecode)
1107 case DBUS_TYPE_BYTE:
1108 case DBUS_TYPE_BOOLEAN:
1109 case DBUS_TYPE_INT32:
1110 case DBUS_TYPE_UINT32:
1111 case DBUS_TYPE_INT64:
1112 case DBUS_TYPE_UINT64:
1113 case DBUS_TYPE_DOUBLE:
1114 case DBUS_TYPE_STRING:
1115 case DBUS_TYPE_OBJECT_PATH:
1116 case DBUS_TYPE_SIGNATURE:
1117 case DBUS_TYPE_ARRAY:
1118 case DBUS_TYPE_STRUCT:
1119 case DBUS_TYPE_VARIANT:
1127 #define TYPE_IS_CONTAINER(typecode) \
1128 ((typecode) == DBUS_TYPE_STRUCT || \
1129 (typecode) == DBUS_TYPE_VARIANT || \
1130 (typecode) == DBUS_TYPE_ARRAY)
1133 * A "container type" can contain basic types, or nested
1134 * container types. #DBUS_TYPE_INVALID is not a container type.
1135 * This function will crash if passed a typecode that isn't
1136 * in dbus-protocol.h
1138 * @returns #TRUE if type is a container
1141 _dbus_type_is_container (int typecode)
1143 /* only reasonable (non-line-noise) typecodes are allowed */
1144 _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
1145 return TYPE_IS_CONTAINER (typecode);
1149 * A "basic type" is a somewhat arbitrary concept, but the intent
1150 * is to include those types that are fully-specified by a single
1151 * typecode, with no additional type information or nested
1152 * values. So all numbers and strings are basic types and
1153 * structs, arrays, and variants are not basic types.
1154 * #DBUS_TYPE_INVALID is not a basic type.
1156 * This function is defined to return #TRUE for exactly those
1157 * types that can be written with _dbus_marshal_basic_type()
1158 * and read with _dbus_demarshal_basic_type().
1160 * This function will crash if passed a typecode that isn't
1161 * in dbus-protocol.h
1163 * @returns #TRUE if type is basic
1166 _dbus_type_is_basic (int typecode)
1168 /* only reasonable (non-line-noise) typecodes are allowed */
1169 _dbus_assert (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID);
1171 /* everything that isn't invalid or a container */
1172 return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
1176 * Tells you whether values of this type can change length if you set
1177 * them to some other value. For this purpose, you assume that the
1178 * first byte of the old and new value would be in the same location,
1179 * so alignment padding is not a factor.
1181 * @returns #TRUE if the type can occupy different lengths
1184 _dbus_type_length_varies (int typecode)
1188 case DBUS_TYPE_BYTE:
1189 case DBUS_TYPE_BOOLEAN:
1190 case DBUS_TYPE_INT32:
1191 case DBUS_TYPE_UINT32:
1192 case DBUS_TYPE_INT64:
1193 case DBUS_TYPE_UINT64:
1194 case DBUS_TYPE_DOUBLE:
1202 * If in verbose mode, print a block of binary data.
1204 * @todo right now it prints even if not in verbose mode
1206 * @param data the data
1207 * @param len the length of the data
1208 * @param offset where to start counting for byte indexes
1211 _dbus_verbose_bytes (const unsigned char *data,
1216 const unsigned char *aligned;
1218 _dbus_assert (len >= 0);
1220 /* Print blanks on first row if appropriate */
1221 aligned = _DBUS_ALIGN_ADDRESS (data, 4);
1224 _dbus_assert (aligned <= data);
1226 if (aligned != data)
1228 _dbus_verbose ("%4d\t%p: ", - (data - aligned), aligned);
1229 while (aligned != data)
1231 _dbus_verbose (" ");
1236 /* now print the bytes */
1240 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1242 _dbus_verbose ("%4d\t%p: ",
1243 offset + i, &data[i]);
1246 if (data[i] >= 32 &&
1248 _dbus_verbose (" '%c' ", data[i]);
1250 _dbus_verbose ("0x%s%x ",
1251 data[i] <= 0xf ? "0" : "", data[i]);
1255 if (_DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
1258 _dbus_verbose ("BE: %d LE: %d",
1259 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
1260 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
1263 _DBUS_ALIGN_ADDRESS (&data[i], 8) == &data[i])
1265 _dbus_verbose (" dbl: %g",
1266 *(double*)&data[i-8]);
1269 _dbus_verbose ("\n");
1273 _dbus_verbose ("\n");
1277 * Dump the given part of the string to verbose log.
1279 * @param str the string
1280 * @param start the start of range to dump
1281 * @param len length of range
1284 _dbus_verbose_bytes_of_string (const DBusString *str,
1291 real_len = _dbus_string_get_length (str);
1293 _dbus_assert (start >= 0);
1295 if (start > real_len)
1297 _dbus_verbose (" [%d,%d) is not inside string of length %d\n",
1298 start, len, real_len);
1302 if ((start + len) > real_len)
1304 _dbus_verbose (" [%d,%d) extends outside string of length %d\n",
1305 start, len, real_len);
1306 len = real_len - start;
1309 d = _dbus_string_get_const_data_len (str, start, len);
1311 _dbus_verbose_bytes (d, len, start);
1316 #ifdef DBUS_BUILD_TESTS
1317 #include "dbus-test.h"
1320 #define MARSHAL_BASIC(typename, byte_order, literal) \
1322 v_##typename = literal; \
1323 if (!_dbus_marshal_basic_type (&str, pos, DBUS_TYPE_##typename, \
1325 byte_order, NULL)) \
1326 _dbus_assert_not_reached ("no memory"); \
1329 #define DEMARSHAL_BASIC(typename, byte_order) \
1331 _dbus_demarshal_basic_type (&str, pos, DBUS_TYPE_##typename, &v_##typename, \
1332 byte_order, &pos); \
1335 #define DEMARSHAL_BASIC_AND_CHECK(typename, byte_order, literal) \
1337 DEMARSHAL_BASIC (typename, byte_order); \
1338 if (literal != v_##typename) \
1340 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1341 _dbus_string_get_length (&str) - dump_pos); \
1342 _dbus_assert_not_reached ("demarshaled wrong value"); \
1346 #define MARSHAL_TEST(typename, byte_order, literal) \
1348 MARSHAL_BASIC (typename, byte_order, literal); \
1350 DEMARSHAL_BASIC_AND_CHECK (typename, byte_order, literal); \
1353 #define MARSHAL_TEST_STRCMP(typename, byte_order, literal) \
1355 MARSHAL_BASIC (typename, byte_order, literal); \
1357 DEMARSHAL_BASIC (typename, byte_order); \
1358 if (strcmp (literal, v_##typename) != 0) \
1360 _dbus_verbose_bytes_of_string (&str, dump_pos, \
1361 _dbus_string_get_length (&str) - dump_pos); \
1362 _dbus_warn ("literal '%s'\nvalue '%s'\n", literal, v_##typename); \
1363 _dbus_assert_not_reached ("demarshaled wrong value"); \
1368 _dbus_marshal_test (void)
1373 dbus_int32_t array1[3] = { 0x123, 0x456, 0x789 }, *array2;
1374 #ifdef DBUS_HAVE_INT64
1375 dbus_int64_t array3[3] = { DBUS_INT64_CONSTANT (0x123ffffffff),
1376 DBUS_INT64_CONSTANT (0x456ffffffff),
1377 DBUS_INT64_CONSTANT (0x789ffffffff) }, *array4;
1383 dbus_int32_t v_INT32;
1384 dbus_uint32_t v_UINT32;
1385 dbus_int64_t v_INT64;
1386 dbus_uint64_t v_UINT64;
1387 unsigned char v_BYTE;
1388 unsigned char v_BOOLEAN;
1389 const char *v_STRING;
1390 const char *v_SIGNATURE;
1391 const char *v_OBJECT_PATH;
1394 if (!_dbus_string_init (&str))
1395 _dbus_assert_not_reached ("failed to init string");
1399 /* Marshal doubles */
1400 MARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN, 3.14);
1401 DEMARSHAL_BASIC (DOUBLE, DBUS_BIG_ENDIAN);
1403 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1404 _dbus_assert_not_reached ("got wrong double value");
1406 MARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN, 3.14);
1407 DEMARSHAL_BASIC (DOUBLE, DBUS_LITTLE_ENDIAN);
1409 if (!_DBUS_DOUBLES_BITWISE_EQUAL (t_DOUBLE, v_DOUBLE))
1410 _dbus_assert_not_reached ("got wrong double value");
1412 /* Marshal signed integers */
1413 MARSHAL_TEST (INT32, DBUS_BIG_ENDIAN, -12345678);
1414 MARSHAL_TEST (INT32, DBUS_LITTLE_ENDIAN, -12345678);
1416 /* Marshal unsigned integers */
1417 MARSHAL_TEST (UINT32, DBUS_BIG_ENDIAN, 0x12345678);
1418 MARSHAL_TEST (UINT32, DBUS_LITTLE_ENDIAN, 0x12345678);
1420 #ifdef DBUS_HAVE_INT64
1421 /* Marshal signed integers */
1422 MARSHAL_TEST (INT64, DBUS_BIG_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1423 MARSHAL_TEST (INT64, DBUS_LITTLE_ENDIAN, DBUS_INT64_CONSTANT (-0x123456789abc7));
1425 /* Marshal unsigned integers */
1426 MARSHAL_TEST (UINT64, DBUS_BIG_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1427 MARSHAL_TEST (UINT64, DBUS_LITTLE_ENDIAN, DBUS_UINT64_CONSTANT (0x123456789abc7));
1428 #endif /* DBUS_HAVE_INT64 */
1431 MARSHAL_TEST (BYTE, DBUS_BIG_ENDIAN, 5);
1432 MARSHAL_TEST (BYTE, DBUS_LITTLE_ENDIAN, 5);
1434 /* Marshal all possible bools! */
1435 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, FALSE);
1436 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, FALSE);
1437 MARSHAL_TEST (BOOLEAN, DBUS_BIG_ENDIAN, TRUE);
1438 MARSHAL_TEST (BOOLEAN, DBUS_LITTLE_ENDIAN, TRUE);
1440 /* Marshal strings */
1441 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "");
1442 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "");
1443 MARSHAL_TEST_STRCMP (STRING, DBUS_BIG_ENDIAN, "This is the dbus test string");
1444 MARSHAL_TEST_STRCMP (STRING, DBUS_LITTLE_ENDIAN, "This is the dbus test string");
1447 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_BIG_ENDIAN, "/a/b/c");
1448 MARSHAL_TEST_STRCMP (OBJECT_PATH, DBUS_LITTLE_ENDIAN, "/a/b/c");
1451 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "");
1452 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "");
1453 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_BIG_ENDIAN, "a(ii)");
1454 MARSHAL_TEST_STRCMP (SIGNATURE, DBUS_LITTLE_ENDIAN, "a(ii)");
1459 * FIXME restore the set/pack tests
1462 #ifdef DBUS_HAVE_INT64
1463 /* set/pack 64-bit integers */
1464 _dbus_string_set_length (&str, 8);
1467 _dbus_marshal_set_int64 (&str, DBUS_LITTLE_ENDIAN,
1468 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1470 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1471 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1472 _dbus_string_get_const_data (&str)));
1475 _dbus_marshal_set_int64 (&str, DBUS_BIG_ENDIAN,
1476 0, DBUS_INT64_CONSTANT (-0x123456789abc7));
1478 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1479 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1480 _dbus_string_get_const_data (&str)));
1482 /* signed little pack */
1483 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1485 _dbus_string_get_data (&str));
1487 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1488 _dbus_unpack_int64 (DBUS_LITTLE_ENDIAN,
1489 _dbus_string_get_const_data (&str)));
1491 /* signed big pack */
1492 _dbus_pack_int64 (DBUS_INT64_CONSTANT (-0x123456789abc7),
1494 _dbus_string_get_data (&str));
1496 _dbus_assert (DBUS_INT64_CONSTANT (-0x123456789abc7) ==
1497 _dbus_unpack_int64 (DBUS_BIG_ENDIAN,
1498 _dbus_string_get_const_data (&str)));
1500 /* unsigned little */
1501 _dbus_marshal_set_uint64 (&str, DBUS_LITTLE_ENDIAN,
1502 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1504 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1505 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1506 _dbus_string_get_const_data (&str)));
1509 _dbus_marshal_set_uint64 (&str, DBUS_BIG_ENDIAN,
1510 0, DBUS_UINT64_CONSTANT (0x123456789abc7));
1512 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1513 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1514 _dbus_string_get_const_data (&str)));
1516 /* unsigned little pack */
1517 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1519 _dbus_string_get_data (&str));
1521 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1522 _dbus_unpack_uint64 (DBUS_LITTLE_ENDIAN,
1523 _dbus_string_get_const_data (&str)));
1525 /* unsigned big pack */
1526 _dbus_pack_uint64 (DBUS_UINT64_CONSTANT (0x123456789abc7),
1528 _dbus_string_get_data (&str));
1530 _dbus_assert (DBUS_UINT64_CONSTANT (0x123456789abc7) ==
1531 _dbus_unpack_uint64 (DBUS_BIG_ENDIAN,
1532 _dbus_string_get_const_data (&str)));
1533 #endif /* DBUS_HAVE_INT64 */
1535 /* set/pack 32-bit integers */
1536 _dbus_string_set_length (&str, 4);
1539 _dbus_marshal_set_int32 (&str, DBUS_LITTLE_ENDIAN,
1542 _dbus_assert (-0x123456 ==
1543 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1544 _dbus_string_get_const_data (&str)));
1547 _dbus_marshal_set_int32 (&str, DBUS_BIG_ENDIAN,
1550 _dbus_assert (-0x123456 ==
1551 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1552 _dbus_string_get_const_data (&str)));
1554 /* signed little pack */
1555 _dbus_pack_int32 (-0x123456,
1557 _dbus_string_get_data (&str));
1559 _dbus_assert (-0x123456 ==
1560 _dbus_unpack_int32 (DBUS_LITTLE_ENDIAN,
1561 _dbus_string_get_const_data (&str)));
1563 /* signed big pack */
1564 _dbus_pack_int32 (-0x123456,
1566 _dbus_string_get_data (&str));
1568 _dbus_assert (-0x123456 ==
1569 _dbus_unpack_int32 (DBUS_BIG_ENDIAN,
1570 _dbus_string_get_const_data (&str)));
1572 /* unsigned little */
1573 _dbus_marshal_set_uint32 (&str, DBUS_LITTLE_ENDIAN,
1576 _dbus_assert (0x123456 ==
1577 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1578 _dbus_string_get_const_data (&str)));
1581 _dbus_marshal_set_uint32 (&str, DBUS_BIG_ENDIAN,
1584 _dbus_assert (0x123456 ==
1585 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1586 _dbus_string_get_const_data (&str)));
1588 /* unsigned little pack */
1589 _dbus_pack_uint32 (0x123456,
1591 _dbus_string_get_data (&str));
1593 _dbus_assert (0x123456 ==
1594 _dbus_unpack_uint32 (DBUS_LITTLE_ENDIAN,
1595 _dbus_string_get_const_data (&str)));
1597 /* unsigned big pack */
1598 _dbus_pack_uint32 (0x123456,
1600 _dbus_string_get_data (&str));
1602 _dbus_assert (0x123456 ==
1603 _dbus_unpack_uint32 (DBUS_BIG_ENDIAN,
1604 _dbus_string_get_const_data (&str)));
1606 #endif /* set/pack tests for integers */
1608 /* Strings in-place set */
1609 byte_order = DBUS_LITTLE_ENDIAN;
1613 _dbus_string_set_length (&str, 0);
1615 /* reset pos for the macros */
1618 MARSHAL_TEST_STRCMP (STRING, byte_order, "Hello world");
1620 /* Set it to something longer */
1621 _dbus_string_init_const (&t, "Hello world foo");
1623 v_STRING = _dbus_string_get_const_data (&t);
1624 _dbus_marshal_set_basic_type (&str, 0, DBUS_TYPE_STRING,
1625 &v_STRING, byte_order, NULL, NULL);
1627 _dbus_demarshal_basic_type (&str, 0, DBUS_TYPE_STRING,
1628 &v_STRING, byte_order,
1630 _dbus_assert (strcmp (v_STRING, "Hello world foo") == 0);
1632 /* Set it to something shorter */
1633 _dbus_string_init_const (&t, "Hello");
1635 v_STRING = _dbus_string_get_const_data (&t);
1636 _dbus_marshal_set_basic_type (&str, 0, DBUS_TYPE_STRING,
1637 &v_STRING, byte_order, NULL, NULL);
1638 _dbus_demarshal_basic_type (&str, 0, DBUS_TYPE_STRING,
1639 &v_STRING, byte_order,
1641 _dbus_assert (strcmp (v_STRING, "Hello") == 0);
1643 /* Do the other byte order */
1644 if (byte_order == DBUS_LITTLE_ENDIAN)
1645 byte_order = DBUS_BIG_ENDIAN;
1651 _dbus_string_free (&str);
1656 #endif /* DBUS_BUILD_TESTS */