1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal.c Marshalling routines
4 * Copyright (C) 2002 CodeFactory AB
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-marshal.h"
25 #include "dbus-internals.h"
28 /* This alignment thing is from ORBit2 */
29 /* Align a value upward to a boundary, expressed as a number of bytes.
30 E.g. align to an 8-byte boundary with argument of 8. */
33 * (this + boundary - 1)
38 #define DBUS_ALIGN_VALUE(this, boundary) \
39 (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
41 #define DBUS_ALIGN_ADDRESS(this, boundary) \
42 ((void*)DBUS_ALIGN_VALUE(this, boundary))
46 swap_bytes (unsigned char *data,
49 unsigned char *p1 = data;
50 unsigned char *p2 = data + len - 1;
54 unsigned char tmp = *p1;
64 _dbus_marshal_double (DBusString *str,
68 if (!_dbus_string_set_length (str,
69 DBUS_ALIGN_VALUE (_dbus_string_get_length (str),
73 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
74 swap_bytes ((unsigned char *)&value, sizeof (double));
76 return _dbus_string_append_len (str, (const char *)&value, sizeof (double));
80 _dbus_marshal_int32 (DBusString *str,
84 if (!_dbus_string_set_length (str,
85 DBUS_ALIGN_VALUE (_dbus_string_get_length (str),
86 sizeof (dbus_int32_t))))
89 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
90 swap_bytes ((unsigned char *)&value, sizeof (dbus_int32_t));
92 return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_int32_t));
96 _dbus_marshal_uint32 (DBusString *str,
100 if (!_dbus_string_set_length (str,
101 DBUS_ALIGN_VALUE (_dbus_string_get_length (str),
102 sizeof (dbus_uint32_t))))
105 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
106 swap_bytes ((unsigned char *)&value, sizeof (dbus_uint32_t));
108 return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_uint32_t));
114 _dbus_demarshal_double (DBusString *str,
121 _dbus_string_get_const_data_len (str, &buffer, start, sizeof (double));
123 retval = *(double *)buffer;
125 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
126 swap_bytes ((unsigned char *)&retval, sizeof (double));
132 _dbus_demarshal_int32 (DBusString *str,
139 _dbus_string_get_const_data_len (str, &buffer, start, sizeof (dbus_int32_t));
141 retval = *(dbus_int32_t *)buffer;
143 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
144 swap_bytes ((unsigned char *)&retval, sizeof (dbus_int32_t));
150 _dbus_demarshal_uint32 (DBusString *str,
154 dbus_uint32_t retval;
157 _dbus_string_get_const_data_len (str, &buffer, start, sizeof (dbus_uint32_t));
159 retval = *(dbus_uint32_t *)buffer;
161 if (byte_order != DBUS_COMPILER_BYTE_ORDER)
162 swap_bytes ((unsigned char *)&retval, sizeof (dbus_uint32_t));
169 #ifdef DBUS_BUILD_TESTS
170 #include "dbus-test.h"
174 _dbus_marshal_test (void)
179 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
180 _dbus_assert_not_reached ("failed to init string");
183 /* Marshal doubles */
184 if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
185 _dbus_assert_not_reached ("could not marshal double value");
186 _dbus_assert (_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos) == 3.14);
189 if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
190 _dbus_assert_not_reached ("could not marshal double value");
191 _dbus_assert (_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos) == 3.14);
194 /* Marshal signed integers */
195 if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
196 _dbus_assert_not_reached ("could not marshal signed integer value");
197 _dbus_assert (_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos) == -12345678);
200 if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
201 _dbus_assert_not_reached ("could not marshal signed integer value");
202 _dbus_assert (_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos) == -12345678);
205 /* Marshal unsigned integers */
206 if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
207 _dbus_assert_not_reached ("could not marshal signed integer value");
208 _dbus_assert (_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos) == 0x12345678);
211 _dbus_string_free (&str);
216 #endif /* DBUS_BUILD_TESTS */