1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-marshal-byteswap.c Swap a block of marshaled data
4 * Copyright (C) 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-marshal-byteswap.h"
26 #include "dbus-marshal-basic.h"
27 #include "dbus-signature.h"
30 * @addtogroup DBusMarshal
35 byteswap_body_helper (DBusTypeReader *reader,
36 dbus_bool_t walk_reader_to_end,
40 unsigned char **new_p)
44 while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
53 case DBUS_TYPE_UINT16:
55 p = _DBUS_ALIGN_ADDRESS (p, 2);
56 *((dbus_uint16_t*)p) = DBUS_UINT16_SWAP_LE_BE (*((dbus_uint16_t*)p));
61 case DBUS_TYPE_BOOLEAN:
63 case DBUS_TYPE_UINT32:
65 p = _DBUS_ALIGN_ADDRESS (p, 4);
66 *((dbus_uint32_t*)p) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)p));
72 case DBUS_TYPE_UINT64:
73 case DBUS_TYPE_DOUBLE:
75 p = _DBUS_ALIGN_ADDRESS (p, 8);
76 #ifdef DBUS_HAVE_INT64
77 *((dbus_uint64_t*)p) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)p));
79 _dbus_swap_array (p, 1, 8);
86 case DBUS_TYPE_STRING:
87 case DBUS_TYPE_OBJECT_PATH:
89 dbus_uint32_t array_len;
91 p = _DBUS_ALIGN_ADDRESS (p, 4);
93 array_len = _dbus_unpack_uint32 (old_byte_order, p);
95 *((dbus_uint32_t*)p) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)p));
98 if (current_type == DBUS_TYPE_ARRAY)
103 elem_type = _dbus_type_reader_get_element_type (reader);
104 alignment = _dbus_type_get_alignment (elem_type);
106 _dbus_assert ((array_len / alignment) < DBUS_MAXIMUM_ARRAY_LENGTH);
108 p = _DBUS_ALIGN_ADDRESS (p, alignment);
110 if (dbus_type_is_fixed (elem_type))
113 _dbus_swap_array (p, array_len / alignment, alignment);
119 const unsigned char *array_end;
121 array_end = p + array_len;
123 _dbus_type_reader_recurse (reader, &sub);
125 while (p < array_end)
127 byteswap_body_helper (&sub,
137 _dbus_assert (current_type == DBUS_TYPE_STRING ||
138 current_type == DBUS_TYPE_OBJECT_PATH);
140 p += (array_len + 1); /* + 1 for nul */
145 case DBUS_TYPE_SIGNATURE:
147 dbus_uint32_t sig_len;
151 p += (sig_len + 2); /* +2 for len and nul */
155 case DBUS_TYPE_VARIANT:
157 /* 1 byte sig len, sig typecodes, align to
158 * contained-type-boundary, values.
160 dbus_uint32_t sig_len;
163 int contained_alignment;
168 _dbus_string_init_const_len (&sig, p, sig_len);
170 p += (sig_len + 1); /* 1 for nul */
172 contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (&sig, 0));
174 p = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
176 _dbus_type_reader_init_types_only (&sub, &sig, 0);
178 byteswap_body_helper (&sub, FALSE, old_byte_order, new_byte_order, p, &p);
182 case DBUS_TYPE_STRUCT:
183 case DBUS_TYPE_DICT_ENTRY:
187 p = _DBUS_ALIGN_ADDRESS (p, 8);
189 _dbus_type_reader_recurse (reader, &sub);
191 byteswap_body_helper (&sub, TRUE, old_byte_order, new_byte_order, p, &p);
195 case DBUS_TYPE_UNIX_FD:
196 /* fds can only be passed on a local machine, so byte order must always match */
197 _dbus_assert_not_reached("attempted to byteswap unix fds which makes no sense");
201 _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
205 if (walk_reader_to_end)
206 _dbus_type_reader_next (reader);
216 * Byteswaps the marshaled data in the given value_str.
218 * @param signature the types in the value_str
219 * @param signature_start where in signature is the signature
220 * @param old_byte_order the old byte order
221 * @param new_byte_order the new byte order
222 * @param value_str the string containing the body
223 * @param value_pos where the values start
226 _dbus_marshal_byteswap (const DBusString *signature,
230 DBusString *value_str,
233 DBusTypeReader reader;
235 _dbus_assert (value_pos >= 0);
236 _dbus_assert (value_pos <= _dbus_string_get_length (value_str));
238 if (old_byte_order == new_byte_order)
241 _dbus_type_reader_init_types_only (&reader,
242 signature, signature_start);
244 byteswap_body_helper (&reader, TRUE,
245 old_byte_order, new_byte_order,
246 _dbus_string_get_data_len (value_str, value_pos, 0),
252 /* Tests in dbus-marshal-byteswap-util.c */