27695aafbeb9f7ef8a488bbca9e0089a41fdc481
[platform/upstream/dbus.git] / dbus / dbus-marshal-byteswap.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-marshal-byteswap.c  Swap a block of marshaled data
3  *
4  * Copyright (C) 2005 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include <config.h>
25 #include "dbus-marshal-byteswap.h"
26 #include "dbus-marshal-basic.h"
27 #include "dbus-signature.h"
28
29 /**
30  * @addtogroup DBusMarshal
31  * @{
32  */
33
34 static void
35 byteswap_body_helper (DBusTypeReader       *reader,
36                       dbus_bool_t           walk_reader_to_end,
37                       int                   old_byte_order,
38                       int                   new_byte_order,
39                       unsigned char        *p,
40                       unsigned char       **new_p)
41 {
42   int current_type;
43
44   while ((current_type = _dbus_type_reader_get_current_type (reader)) != DBUS_TYPE_INVALID)
45     {
46       switch (current_type)
47         {
48         case DBUS_TYPE_BYTE:
49           ++p;
50           break;
51
52         case DBUS_TYPE_INT16:
53         case DBUS_TYPE_UINT16:
54           {
55             p = _DBUS_ALIGN_ADDRESS (p, 2);
56             *((dbus_uint16_t*)p) = DBUS_UINT16_SWAP_LE_BE (*((dbus_uint16_t*)p));
57             p += 2;
58           }
59           break;
60           
61         case DBUS_TYPE_BOOLEAN:
62         case DBUS_TYPE_INT32:
63         case DBUS_TYPE_UINT32:
64           {
65             p = _DBUS_ALIGN_ADDRESS (p, 4);
66             *((dbus_uint32_t*)p) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)p));
67             p += 4;
68           }
69           break;
70           
71         case DBUS_TYPE_INT64:
72         case DBUS_TYPE_UINT64:
73         case DBUS_TYPE_DOUBLE:
74           {
75             p = _DBUS_ALIGN_ADDRESS (p, 8);
76             *((dbus_uint64_t*)p) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)p));
77             p += 8;
78           }
79           break;
80
81         case DBUS_TYPE_ARRAY:
82         case DBUS_TYPE_STRING:
83         case DBUS_TYPE_OBJECT_PATH:
84           {
85             dbus_uint32_t array_len;
86             
87             p = _DBUS_ALIGN_ADDRESS (p, 4);
88
89             array_len = _dbus_unpack_uint32 (old_byte_order, p);
90
91             *((dbus_uint32_t*)p) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)p));
92             p += 4;
93
94             if (current_type == DBUS_TYPE_ARRAY)
95               {
96                 int elem_type;
97                 int alignment;
98
99                 elem_type = _dbus_type_reader_get_element_type (reader);
100                 alignment = _dbus_type_get_alignment (elem_type);
101
102                 _dbus_assert ((array_len / alignment) < DBUS_MAXIMUM_ARRAY_LENGTH);
103
104                 p = _DBUS_ALIGN_ADDRESS (p, alignment);
105                 
106                 if (dbus_type_is_fixed (elem_type))
107                   {
108                     if (alignment > 1)
109                       _dbus_swap_array (p, array_len / alignment, alignment);
110                     p += array_len;
111                   }
112                 else
113                   {
114                     DBusTypeReader sub;
115                     const unsigned char *array_end;
116
117                     array_end = p + array_len;
118                     
119                     _dbus_type_reader_recurse (reader, &sub);
120
121                     while (p < array_end)
122                       {
123                         byteswap_body_helper (&sub,
124                                               FALSE,
125                                               old_byte_order,
126                                               new_byte_order,
127                                               p, &p);
128                       }
129                   }
130               }
131             else
132               {
133                 _dbus_assert (current_type == DBUS_TYPE_STRING ||
134                               current_type == DBUS_TYPE_OBJECT_PATH);
135                 
136                 p += (array_len + 1); /* + 1 for nul */
137               }
138           }
139           break;
140
141         case DBUS_TYPE_SIGNATURE:
142           {
143             dbus_uint32_t sig_len;
144
145             sig_len = *p;
146             
147             p += (sig_len + 2); /* +2 for len and nul */
148           }
149           break;
150
151         case DBUS_TYPE_VARIANT:
152           {
153             /* 1 byte sig len, sig typecodes, align to
154              * contained-type-boundary, values.
155              */
156             dbus_uint32_t sig_len;
157             DBusString sig;
158             DBusTypeReader sub;
159             int contained_alignment;
160
161             sig_len = *p;
162             ++p;
163
164             _dbus_string_init_const_len (&sig, (const char *) p, sig_len);
165
166             p += (sig_len + 1); /* 1 for nul */
167
168             contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (&sig, 0));
169             
170             p = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
171
172             _dbus_type_reader_init_types_only (&sub, &sig, 0);
173
174             byteswap_body_helper (&sub, FALSE, old_byte_order, new_byte_order, p, &p);
175           }
176           break;
177
178         case DBUS_TYPE_STRUCT:
179         case DBUS_TYPE_DICT_ENTRY:
180           {
181             DBusTypeReader sub;
182
183             p = _DBUS_ALIGN_ADDRESS (p, 8);
184             
185             _dbus_type_reader_recurse (reader, &sub);
186             
187             byteswap_body_helper (&sub, TRUE, old_byte_order, new_byte_order, p, &p);
188           }
189           break;
190
191         case DBUS_TYPE_UNIX_FD:
192           /* fds can only be passed on a local machine, so byte order must always match */
193           _dbus_assert_not_reached("attempted to byteswap unix fds which makes no sense");
194           break;
195
196         default:
197           _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
198           break;
199         }
200
201       if (walk_reader_to_end)
202         _dbus_type_reader_next (reader);
203       else
204         break;
205     }
206
207   if (new_p)
208     *new_p = p;
209 }
210
211 /**
212  * Byteswaps the marshaled data in the given value_str.
213  *
214  * @param signature the types in the value_str
215  * @param signature_start where in signature is the signature
216  * @param old_byte_order the old byte order
217  * @param new_byte_order the new byte order
218  * @param value_str the string containing the body
219  * @param value_pos where the values start
220  */
221 void
222 _dbus_marshal_byteswap (const DBusString *signature,
223                         int               signature_start,
224                         int               old_byte_order,
225                         int               new_byte_order,
226                         DBusString       *value_str,
227                         int               value_pos)
228 {
229   DBusTypeReader reader;
230
231   _dbus_assert (value_pos >= 0);
232   _dbus_assert (value_pos <= _dbus_string_get_length (value_str));
233
234   if (old_byte_order == new_byte_order)
235     return;
236   
237   _dbus_type_reader_init_types_only (&reader,
238                                      signature, signature_start);
239
240   byteswap_body_helper (&reader, TRUE,
241                         old_byte_order, new_byte_order,
242                         _dbus_string_get_udata_len (value_str, value_pos, 0),
243                         NULL);
244 }
245
246 /** @} */
247
248 /* Tests in dbus-marshal-byteswap-util.c */