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