2002-12-27 Anders Carlsson <andersca@codefactory.se>
[platform/upstream/dbus.git] / dbus / dbus-marshal.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-marshal.c  Marshalling routines
3  *
4  * Copyright (C) 2002  CodeFactory AB
5  *
6  * Licensed under the Academic Free License version 1.2
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.h"
25 #include "dbus-internals.h"
26
27 #include <string.h>
28
29 #define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint32_t) ( \
30     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x000000ffU) << 24) |  \
31     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x0000ff00U) <<  8) |  \
32     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x00ff0000U) >>  8) |  \
33     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0xff000000U) >> 24)))
34
35 #define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
36
37 #ifdef WORDS_BIGENDIAN
38 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) (val))
39 #define DBUS_UINT32_TO_BE(val)  ((dbus_uint32_t) (val))
40 #define DBUS_INT32_TO_LE(val)   ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
41 #define DBUS_UINT32_TO_LE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
42 #else
43 #define DBUS_INT32_TO_LE(val)   ((dbus_int32_t) (val))
44 #define DBUS_UINT32_TO_LE(val)  ((dbus_uint32_t) (val))
45 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
46 #define DBUS_UINT32_TO_BE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
47 #endif
48
49 /* The transformation is symmetric, so the FROM just maps to the TO. */
50 #define DBUS_INT32_FROM_LE(val)  (DBUS_INT32_TO_LE (val))
51 #define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
52 #define DBUS_INT32_FROM_BE(val)  (DBUS_INT32_TO_BE (val))
53 #define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
54
55
56 /* This alignment thing is from ORBit2 */
57 /* Align a value upward to a boundary, expressed as a number of bytes.
58  * E.g. align to an 8-byte boundary with argument of 8.
59  */
60
61 /*
62  *   (this + boundary - 1)
63  *          &
64  *    ~(boundary - 1)
65  */
66
67 #define DBUS_ALIGN_VALUE(this, boundary) \
68   (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
69
70 #define DBUS_ALIGN_ADDRESS(this, boundary) \
71   ((void*)DBUS_ALIGN_VALUE(this, boundary))
72
73 /* from ORBit */
74 static void
75 swap_bytes (unsigned char *data,
76             unsigned int   len)
77 {
78   unsigned char *p1 = data;
79   unsigned char *p2 = data + len - 1;
80
81   while (p1 < p2)
82     {
83       unsigned char tmp = *p1;
84       *p1 = *p2;
85       *p2 = tmp;
86
87       --p2;
88       ++p1;
89     }
90 }
91
92 static dbus_uint32_t
93 unpack_uint32 (int                  byte_order,
94                const unsigned char *data)
95 {
96   _dbus_assert (DBUS_ALIGN_ADDRESS (data, 4) == data);
97   
98   if (byte_order == DBUS_LITTLE_ENDIAN)
99     return DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)data);
100   else
101     return DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)data);
102 }             
103
104 static dbus_int32_t
105 unpack_int32 (int                  byte_order,
106               const unsigned char *data)
107 {
108   _dbus_assert (DBUS_ALIGN_ADDRESS (data, 4) == data);
109   
110   if (byte_order == DBUS_LITTLE_ENDIAN)
111     return DBUS_INT32_FROM_LE (*(dbus_int32_t*)data);
112   else
113     return DBUS_INT32_FROM_BE (*(dbus_int32_t*)data);
114 }
115
116 /**
117  * @defgroup DBusMarshal marshaling and unmarshaling
118  * @ingroup  DBusInternals
119  * @brief functions to marshal/unmarshal data from the wire
120  *
121  * Types and functions related to converting primitive data types from
122  * wire format to native machine format, and vice versa.
123  *
124  * @{
125  */
126
127 dbus_bool_t
128 _dbus_marshal_double (DBusString *str,
129                       int         byte_order,
130                       double      value)
131 {
132   if (!_dbus_string_set_length (str,
133                                 DBUS_ALIGN_VALUE (_dbus_string_get_length (str),
134                                                   sizeof (double))))
135     return FALSE;
136   
137   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
138     swap_bytes ((unsigned char *)&value, sizeof (double));
139
140   return _dbus_string_append_len (str, (const char *)&value, sizeof (double));
141 }
142
143 dbus_bool_t
144 _dbus_marshal_int32  (DBusString   *str,
145                       int           byte_order,
146                       dbus_int32_t  value)
147 {
148   if (!_dbus_string_set_length (str,
149                                 DBUS_ALIGN_VALUE (_dbus_string_get_length (str),
150                                                   sizeof (dbus_int32_t))))
151     return FALSE;
152   
153   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
154     swap_bytes ((unsigned char *)&value, sizeof (dbus_int32_t));
155
156   return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_int32_t));
157 }
158
159 dbus_bool_t
160 _dbus_marshal_uint32 (DBusString    *str,
161                       int            byte_order,
162                       dbus_uint32_t  value)
163 {
164   if (!_dbus_string_set_length (str,
165                                 DBUS_ALIGN_VALUE (_dbus_string_get_length (str),
166                                                   sizeof (dbus_uint32_t))))
167     return FALSE;
168
169   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
170     swap_bytes ((unsigned char *)&value, sizeof (dbus_uint32_t));
171
172   return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_uint32_t));
173 }
174
175 dbus_bool_t
176 _dbus_marshal_string (DBusString    *str,
177                       int            byte_order,
178                       const char    *value)
179 {
180   int len;
181
182   if (!_dbus_string_set_length (str,
183                                 DBUS_ALIGN_VALUE (_dbus_string_get_length (str),
184                                                   sizeof (dbus_uint32_t))))
185     return FALSE;
186
187   len = strlen (value);
188
189   if (!_dbus_marshal_uint32 (str, byte_order, len))
190     return FALSE;
191
192   return _dbus_string_append_len (str, value, len + 1);
193 }
194
195
196 double
197 _dbus_demarshal_double (DBusString  *str,
198                         int          byte_order,
199                         int          pos,
200                         int         *new_pos)
201 {
202   double retval;
203   const char *buffer;
204
205   pos = DBUS_ALIGN_VALUE (pos, sizeof (double));
206   
207   _dbus_string_get_const_data_len (str, &buffer, pos, sizeof (double));
208
209   retval = *(double *)buffer;
210   
211   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
212     swap_bytes ((unsigned char *)&retval, sizeof (double));
213
214   if (new_pos)
215     *new_pos = pos + sizeof (double);
216   
217   return retval;  
218 }
219
220 dbus_int32_t
221 _dbus_demarshal_int32  (DBusString *str,
222                         int         byte_order,
223                         int         pos,
224                         int        *new_pos)
225 {
226   const char *buffer;
227
228   pos = DBUS_ALIGN_VALUE (pos, sizeof (dbus_int32_t));
229   
230   _dbus_string_get_const_data_len (str, &buffer, pos, sizeof (dbus_int32_t));
231
232   if (new_pos)
233     *new_pos = pos + sizeof (dbus_int32_t);
234
235   return unpack_int32 (byte_order, buffer);
236 }
237
238 dbus_uint32_t
239 _dbus_demarshal_uint32  (DBusString *str,
240                          int         byte_order,
241                          int         pos,
242                          int        *new_pos)
243 {
244   const char *buffer;
245
246   pos = DBUS_ALIGN_VALUE (pos, sizeof (dbus_uint32_t));
247   
248   _dbus_string_get_const_data_len (str, &buffer, pos, sizeof (dbus_uint32_t));
249
250   if (new_pos)
251     *new_pos = pos + sizeof (dbus_uint32_t);
252
253   return unpack_uint32 (byte_order, buffer);
254 }
255
256 char *
257 _dbus_demarshal_string (DBusString *str,
258                         int         byte_order,
259                         int         pos,
260                         int        *new_pos)
261 {
262   int len;
263   char *retval;
264   const char *data;
265
266   len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
267
268   retval = dbus_malloc (len + 1);
269
270   if (!retval)
271     return NULL;
272
273   _dbus_string_get_const_data_len (str, &data, pos, 3);
274
275   if (!data)
276     return NULL;
277
278   memcpy (retval, data, len + 1);
279
280   if (new_pos)
281     *new_pos = pos + len + 1;
282   
283   return retval;
284 }
285
286 /**
287  * If in verbose mode, print a block of binary data.
288  *
289  * @param data the data
290  * @param len the length of the data
291  */
292 void
293 _dbus_verbose_bytes (const unsigned char *data,
294                      int                  len)
295 {
296   int i;
297   const unsigned char *aligned;
298
299   /* Print blanks on first row if appropriate */
300   aligned = DBUS_ALIGN_ADDRESS (data, 4);
301   if (aligned > data)
302     aligned -= 4;
303   _dbus_assert (aligned <= data);
304
305   if (aligned != data)
306     {
307       _dbus_verbose ("%5d\t%p: ", - (data - aligned), aligned); 
308       while (aligned != data)
309         {
310           _dbus_verbose ("    ");
311           ++aligned;
312         }
313     }
314
315   /* now print the bytes */
316   i = 0;
317   while (i < len)
318     {
319       if (DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
320         {
321           _dbus_verbose ("%5d\t%p: ",
322                    i, &data[i]);
323         }
324       
325       if (data[i] >= 32 &&
326           data[i] <= 126)
327         _dbus_verbose (" '%c' ", data[i]);
328       else
329         _dbus_verbose ("0x%s%x ",
330                  data[i] <= 0xf ? "0" : "", data[i]);
331
332       ++i;
333
334       if (DBUS_ALIGN_ADDRESS (&data[i], 4) == &data[i])
335         {
336           if (i > 3)
337             _dbus_verbose ("big: %d little: %d",
338                      unpack_uint32 (DBUS_BIG_ENDIAN, &data[i-4]),
339                      unpack_uint32 (DBUS_LITTLE_ENDIAN, &data[i-4]));
340           
341           _dbus_verbose ("\n");
342         }
343     }
344
345   _dbus_verbose ("\n");
346 }
347
348 /**
349  * Dump the given part of the string to verbose log.
350  *
351  * @param str the string
352  * @param start the start of range to dump
353  * @param len length of range
354  */
355 void
356 _dbus_verbose_bytes_of_string (const DBusString    *str,
357                                int                  start,
358                                int                  len)
359 {
360   const char *d;
361
362   _dbus_string_get_const_data_len (str, &d, start, len);
363
364   _dbus_verbose_bytes (d, len);
365 }
366
367 /** @} */
368
369 #ifdef DBUS_BUILD_TESTS
370 #include "dbus-test.h"
371 #include <stdio.h>
372
373 dbus_bool_t
374 _dbus_marshal_test (void)
375 {
376   DBusString str;
377   char *tmp1, *tmp2;
378   int pos = 0;
379   
380   if (!_dbus_string_init (&str, _DBUS_INT_MAX))
381     _dbus_assert_not_reached ("failed to init string");
382
383
384   /* Marshal doubles */
385   if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
386     _dbus_assert_not_reached ("could not marshal double value");
387   _dbus_assert (_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos, &pos) == 3.14);
388
389   
390   if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
391     _dbus_assert_not_reached ("could not marshal double value");
392   _dbus_assert (_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 3.14);
393   
394   /* Marshal signed integers */
395   if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
396     _dbus_assert_not_reached ("could not marshal signed integer value");
397   _dbus_assert (_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == -12345678);
398
399   if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
400     _dbus_assert_not_reached ("could not marshal signed integer value");
401   _dbus_assert (_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == -12345678);
402   
403   /* Marshal unsigned integers */
404   if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
405     _dbus_assert_not_reached ("could not marshal signed integer value");
406   _dbus_assert (_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 0x12345678);
407
408   /* Marshal strings */
409   tmp1 = "This is the dbus test string";
410   if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
411     _dbus_assert_not_reached ("could not marshal string");
412   tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
413   _dbus_assert (strcmp (tmp1, tmp2) == 0);
414   dbus_free (tmp2);
415
416   tmp1 = "This is the dbus test string";
417   if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
418     _dbus_assert_not_reached ("could not marshal string");
419   tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
420   _dbus_assert (strcmp (tmp1, tmp2) == 0);
421   dbus_free (tmp2);
422   
423   _dbus_string_free (&str);
424   
425   return TRUE;
426 }
427
428 #endif /* DBUS_BUILD_TESTS */