dbus-marshal-byteswap: Byte-swap Unix fd indexes if needed
[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         case DBUS_TYPE_UNIX_FD:
65           {
66             p = _DBUS_ALIGN_ADDRESS (p, 4);
67             *((dbus_uint32_t*)p) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)p));
68             p += 4;
69           }
70           break;
71           
72         case DBUS_TYPE_INT64:
73         case DBUS_TYPE_UINT64:
74         case DBUS_TYPE_DOUBLE:
75           {
76             p = _DBUS_ALIGN_ADDRESS (p, 8);
77             *((dbus_uint64_t*)p) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)p));
78             p += 8;
79           }
80           break;
81
82         case DBUS_TYPE_ARRAY:
83         case DBUS_TYPE_STRING:
84         case DBUS_TYPE_OBJECT_PATH:
85           {
86             dbus_uint32_t array_len;
87             
88             p = _DBUS_ALIGN_ADDRESS (p, 4);
89
90             array_len = _dbus_unpack_uint32 (old_byte_order, p);
91
92             *((dbus_uint32_t*)p) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)p));
93             p += 4;
94
95             if (current_type == DBUS_TYPE_ARRAY)
96               {
97                 int elem_type;
98                 int alignment;
99
100                 elem_type = _dbus_type_reader_get_element_type (reader);
101                 alignment = _dbus_type_get_alignment (elem_type);
102
103                 _dbus_assert ((array_len / alignment) < DBUS_MAXIMUM_ARRAY_LENGTH);
104
105                 p = _DBUS_ALIGN_ADDRESS (p, alignment);
106                 
107                 if (dbus_type_is_fixed (elem_type))
108                   {
109                     if (alignment > 1)
110                       _dbus_swap_array (p, array_len / alignment, alignment);
111                     p += array_len;
112                   }
113                 else
114                   {
115                     DBusTypeReader sub;
116                     const unsigned char *array_end;
117
118                     array_end = p + array_len;
119                     
120                     _dbus_type_reader_recurse (reader, &sub);
121
122                     while (p < array_end)
123                       {
124                         byteswap_body_helper (&sub,
125                                               FALSE,
126                                               old_byte_order,
127                                               new_byte_order,
128                                               p, &p);
129                       }
130                   }
131               }
132             else
133               {
134                 _dbus_assert (current_type == DBUS_TYPE_STRING ||
135                               current_type == DBUS_TYPE_OBJECT_PATH);
136                 
137                 p += (array_len + 1); /* + 1 for nul */
138               }
139           }
140           break;
141
142         case DBUS_TYPE_SIGNATURE:
143           {
144             dbus_uint32_t sig_len;
145
146             sig_len = *p;
147             
148             p += (sig_len + 2); /* +2 for len and nul */
149           }
150           break;
151
152         case DBUS_TYPE_VARIANT:
153           {
154             /* 1 byte sig len, sig typecodes, align to
155              * contained-type-boundary, values.
156              */
157             dbus_uint32_t sig_len;
158             DBusString sig;
159             DBusTypeReader sub;
160             int contained_alignment;
161
162             sig_len = *p;
163             ++p;
164
165             _dbus_string_init_const_len (&sig, (const char *) p, sig_len);
166
167             p += (sig_len + 1); /* 1 for nul */
168
169             contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (&sig, 0));
170             
171             p = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
172
173             _dbus_type_reader_init_types_only (&sub, &sig, 0);
174
175             byteswap_body_helper (&sub, FALSE, old_byte_order, new_byte_order, p, &p);
176           }
177           break;
178
179         case DBUS_TYPE_STRUCT:
180         case DBUS_TYPE_DICT_ENTRY:
181           {
182             DBusTypeReader sub;
183
184             p = _DBUS_ALIGN_ADDRESS (p, 8);
185             
186             _dbus_type_reader_recurse (reader, &sub);
187             
188             byteswap_body_helper (&sub, TRUE, old_byte_order, new_byte_order, p, &p);
189           }
190           break;
191
192         default:
193           _dbus_assert_not_reached ("invalid typecode in supposedly-validated signature");
194           break;
195         }
196
197       if (walk_reader_to_end)
198         _dbus_type_reader_next (reader);
199       else
200         break;
201     }
202
203   if (new_p)
204     *new_p = p;
205 }
206
207 /**
208  * Byteswaps the marshaled data in the given value_str.
209  *
210  * @param signature the types in the value_str
211  * @param signature_start where in signature is the signature
212  * @param old_byte_order the old byte order
213  * @param new_byte_order the new byte order
214  * @param value_str the string containing the body
215  * @param value_pos where the values start
216  */
217 void
218 _dbus_marshal_byteswap (const DBusString *signature,
219                         int               signature_start,
220                         int               old_byte_order,
221                         int               new_byte_order,
222                         DBusString       *value_str,
223                         int               value_pos)
224 {
225   DBusTypeReader reader;
226
227   _dbus_assert (value_pos >= 0);
228   _dbus_assert (value_pos <= _dbus_string_get_length (value_str));
229
230   if (old_byte_order == new_byte_order)
231     return;
232   
233   _dbus_type_reader_init_types_only (&reader,
234                                      signature, signature_start);
235
236   byteswap_body_helper (&reader, TRUE,
237                         old_byte_order, new_byte_order,
238                         _dbus_string_get_udata_len (value_str, value_pos, 0),
239                         NULL);
240 }
241
242 /** @} */
243
244 /* Tests in dbus-marshal-byteswap-util.c */