D-Bus  1.6.18
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 
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 #ifdef DBUS_HAVE_INT64
78  *((dbus_uint64_t*)p) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)p));
79 #else
80  _dbus_swap_array (p, 1, 8);
81 #endif
82  p += 8;
83  }
84  break;
85 
86  case DBUS_TYPE_ARRAY:
87  case DBUS_TYPE_STRING:
89  {
90  dbus_uint32_t array_len;
91 
92  p = _DBUS_ALIGN_ADDRESS (p, 4);
93 
94  array_len = _dbus_unpack_uint32 (old_byte_order, p);
95 
96  *((dbus_uint32_t*)p) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)p));
97  p += 4;
98 
99  if (current_type == DBUS_TYPE_ARRAY)
100  {
101  int elem_type;
102  int alignment;
103 
104  elem_type = _dbus_type_reader_get_element_type (reader);
105  alignment = _dbus_type_get_alignment (elem_type);
106 
107  _dbus_assert ((array_len / alignment) < DBUS_MAXIMUM_ARRAY_LENGTH);
108 
109  p = _DBUS_ALIGN_ADDRESS (p, alignment);
110 
111  if (dbus_type_is_fixed (elem_type))
112  {
113  if (alignment > 1)
114  _dbus_swap_array (p, array_len / alignment, alignment);
115  p += array_len;
116  }
117  else
118  {
119  DBusTypeReader sub;
120  const unsigned char *array_end;
121 
122  array_end = p + array_len;
123 
124  _dbus_type_reader_recurse (reader, &sub);
125 
126  while (p < array_end)
127  {
128  byteswap_body_helper (&sub,
129  FALSE,
130  old_byte_order,
131  new_byte_order,
132  p, &p);
133  }
134  }
135  }
136  else
137  {
138  _dbus_assert (current_type == DBUS_TYPE_STRING ||
139  current_type == DBUS_TYPE_OBJECT_PATH);
140 
141  p += (array_len + 1); /* + 1 for nul */
142  }
143  }
144  break;
145 
146  case DBUS_TYPE_SIGNATURE:
147  {
148  dbus_uint32_t sig_len;
149 
150  sig_len = *p;
151 
152  p += (sig_len + 2); /* +2 for len and nul */
153  }
154  break;
155 
156  case DBUS_TYPE_VARIANT:
157  {
158  /* 1 byte sig len, sig typecodes, align to
159  * contained-type-boundary, values.
160  */
161  dbus_uint32_t sig_len;
162  DBusString sig;
163  DBusTypeReader sub;
164  int contained_alignment;
165 
166  sig_len = *p;
167  ++p;
168 
169  _dbus_string_init_const_len (&sig, p, sig_len);
170 
171  p += (sig_len + 1); /* 1 for nul */
172 
173  contained_alignment = _dbus_type_get_alignment (_dbus_first_type_in_signature (&sig, 0));
174 
175  p = _DBUS_ALIGN_ADDRESS (p, contained_alignment);
176 
177  _dbus_type_reader_init_types_only (&sub, &sig, 0);
178 
179  byteswap_body_helper (&sub, FALSE, old_byte_order, new_byte_order, p, &p);
180  }
181  break;
182 
183  case DBUS_TYPE_STRUCT:
185  {
186  DBusTypeReader sub;
187 
188  p = _DBUS_ALIGN_ADDRESS (p, 8);
189 
190  _dbus_type_reader_recurse (reader, &sub);
191 
192  byteswap_body_helper (&sub, TRUE, old_byte_order, new_byte_order, p, &p);
193  }
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 
221 void
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 
238  signature, signature_start);
239 
240  byteswap_body_helper (&reader, TRUE,
241  old_byte_order, new_byte_order,
242  _dbus_string_get_data_len (value_str, value_pos, 0),
243  NULL);
244 }
245 
248 /* Tests in dbus-marshal-byteswap-util.c */
dbus_bool_t dbus_type_is_fixed(int typecode)
Tells you whether values of this type can change length if you set them to some other value...
#define DBUS_TYPE_UINT16
Type code marking a 16-bit unsigned integer.
Definition: dbus-protocol.h:78
#define NULL
A null pointer, defined appropriately for C or C++.
int _dbus_first_type_in_signature(const DBusString *str, int pos)
Get the first type in the signature.
void _dbus_type_reader_recurse(DBusTypeReader *reader, DBusTypeReader *sub)
Initialize a new reader pointing to the first type and corresponding value that's a child of the curr...
#define DBUS_TYPE_STRUCT
STRUCT and DICT_ENTRY are sort of special since their codes can't appear in a type string...
#define DBUS_TYPE_DICT_ENTRY
Type code used to represent a dict entry; however, this type code does not appear in type signatures...
#define DBUS_TYPE_STRING
Type code marking a UTF-8 encoded, nul-terminated Unicode string.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define DBUS_TYPE_BYTE
Type code marking an 8-bit unsigned integer.
Definition: dbus-protocol.h:66
void _dbus_swap_array(unsigned char *data, int n_elements, int alignment)
Swaps the elements of an array to the opposite byte order.
char * _dbus_string_get_data_len(DBusString *str, int start, int len)
Gets a sub-portion of the raw character buffer from the string.
Definition: dbus-string.c:476
void _dbus_type_reader_init_types_only(DBusTypeReader *reader, const DBusString *type_str, int type_pos)
Like _dbus_type_reader_init() but the iteration is over the signature, not over values.
#define DBUS_TYPE_DOUBLE
Type code marking an 8-byte double in IEEE 754 format.
Definition: dbus-protocol.h:98
#define DBUS_TYPE_ARRAY
Type code marking a D-Bus array type.
#define DBUS_TYPE_INT64
Type code marking a 64-bit signed integer.
Definition: dbus-protocol.h:90
dbus_uint32_t _dbus_unpack_uint32(int byte_order, const unsigned char *data)
Unpacks a 32 bit unsigned integer from a data pointer.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
#define DBUS_TYPE_INT32
Type code marking a 32-bit signed integer.
Definition: dbus-protocol.h:82
dbus_bool_t _dbus_type_reader_next(DBusTypeReader *reader)
Skip to the next value on this "level".
#define DBUS_TYPE_SIGNATURE
Type code marking a D-Bus type signature.
#define DBUS_TYPE_UINT64
Type code marking a 64-bit unsigned integer.
Definition: dbus-protocol.h:94
#define DBUS_TYPE_VARIANT
Type code marking a D-Bus variant type.
#define DBUS_TYPE_UINT32
Type code marking a 32-bit unsigned integer.
Definition: dbus-protocol.h:86
The type reader is an iterator for reading values from a block of values.
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define DBUS_TYPE_OBJECT_PATH
Type code marking a D-Bus object path.
#define DBUS_TYPE_UNIX_FD
Type code marking a unix file descriptor.
#define DBUS_TYPE_INVALID
Type code that is never equal to a legitimate type code.
Definition: dbus-protocol.h:60
#define DBUS_MAXIMUM_ARRAY_LENGTH
Max length of a marshaled array in bytes (64M, 2^26) We use signed int for lengths so must be INT_MAX...
#define DBUS_TYPE_INT16
Type code marking a 16-bit signed integer.
Definition: dbus-protocol.h:74
#define DBUS_TYPE_BOOLEAN
Type code marking a boolean.
Definition: dbus-protocol.h:70
int _dbus_type_reader_get_element_type(const DBusTypeReader *reader)
Gets the type of an element of the array the reader is currently pointing to.
int _dbus_type_reader_get_current_type(const DBusTypeReader *reader)
Gets the type of the value the reader is currently pointing to; or for a types-only reader gets the t...
#define FALSE
Expands to "0".
int _dbus_type_get_alignment(int typecode)
Gets the alignment requirement for the given type; will be 1, 4, or 8.
void _dbus_string_init_const_len(DBusString *str, const char *value, int len)
Initializes a constant string with a length.
Definition: dbus-string.c:210
void _dbus_marshal_byteswap(const DBusString *signature, int signature_start, int old_byte_order, int new_byte_order, DBusString *value_str, int value_pos)
Byteswaps the marshaled data in the given value_str.