• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-marshal-basic.h  Marshalling routines for basic (primitive) types
3  *
4  * Copyright (C) 2002  CodeFactory AB
5  * Copyright (C) 2004, 2005  Red Hat, Inc.
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24 
25 #ifndef DBUS_MARSHAL_BASIC_H
26 #define DBUS_MARSHAL_BASIC_H
27 
28 #ifdef HAVE_BYTESWAP_H
29 #include <byteswap.h>
30 #endif
31 
32 #include <dbus/dbus-protocol.h>
33 #include <dbus/dbus-types.h>
34 #include <dbus/dbus-arch-deps.h>
35 #include <dbus/dbus-string.h>
36 
37 #ifdef WORDS_BIGENDIAN
38 #define DBUS_COMPILER_BYTE_ORDER DBUS_BIG_ENDIAN
39 #else
40 #define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN
41 #endif
42 
43 #ifdef HAVE_BYTESWAP_H
44 #define DBUS_UINT16_SWAP_LE_BE_CONSTANT(val) bswap_16(val)
45 #define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val) bswap_32(val)
46 #else /* HAVE_BYTESWAP_H */
47 
48 #define DBUS_UINT16_SWAP_LE_BE_CONSTANT(val)	((dbus_uint16_t) (      \
49     (dbus_uint16_t) ((dbus_uint16_t) (val) >> 8) |                      \
50     (dbus_uint16_t) ((dbus_uint16_t) (val) << 8)))
51 
52 #define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val)	((dbus_uint32_t) (      \
53     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x000000ffU) << 24) |     \
54     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x0000ff00U) <<  8) |     \
55     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x00ff0000U) >>  8) |     \
56     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0xff000000U) >> 24)))
57 
58 #endif /* HAVE_BYTESWAP_H */
59 
60 #ifdef DBUS_HAVE_INT64
61 
62 #ifdef HAVE_BYTESWAP_H
63 #define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val) bswap_64(val)
64 #else /* HAVE_BYTESWAP_H */
65 
66 #define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val)	((dbus_uint64_t) (              \
67       (((dbus_uint64_t) (val) &                                                 \
68 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000000000ff)) << 56) |    \
69       (((dbus_uint64_t) (val) &                                                 \
70 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000000000ff00)) << 40) |    \
71       (((dbus_uint64_t) (val) &                                                 \
72 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000000000ff0000)) << 24) |    \
73       (((dbus_uint64_t) (val) &                                                 \
74 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000ff000000)) <<  8) |    \
75       (((dbus_uint64_t) (val) &                                                 \
76 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000ff00000000)) >>  8) |    \
77       (((dbus_uint64_t) (val) &                                                 \
78 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000ff0000000000)) >> 24) |    \
79       (((dbus_uint64_t) (val) &                                                 \
80 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00ff000000000000)) >> 40) |    \
81       (((dbus_uint64_t) (val) &                                                 \
82 	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0xff00000000000000)) >> 56)))
83 #endif /* DBUS_HAVE_INT64 */
84 
85 #endif /* HAVE_BYTESWAP_H */
86 
87 #define DBUS_UINT16_SWAP_LE_BE(val) (DBUS_UINT16_SWAP_LE_BE_CONSTANT (val))
88 #define DBUS_INT16_SWAP_LE_BE(val)  ((dbus_int16_t)DBUS_UINT16_SWAP_LE_BE_CONSTANT (val))
89 
90 #define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
91 #define DBUS_INT32_SWAP_LE_BE(val)  ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
92 
93 #ifdef DBUS_HAVE_INT64
94 #  define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
95 #  define DBUS_INT64_SWAP_LE_BE(val)  ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
96 #endif /* DBUS_HAVE_INT64 */
97 
98 #ifdef WORDS_BIGENDIAN
99 
100 #  define DBUS_INT16_TO_BE(val)	((dbus_int16_t) (val))
101 #  define DBUS_UINT16_TO_BE(val)	((dbus_uint16_t) (val))
102 #  define DBUS_INT16_TO_LE(val)	(DBUS_INT16_SWAP_LE_BE (val))
103 #  define DBUS_UINT16_TO_LE(val)	(DBUS_UINT16_SWAP_LE_BE (val))
104 #  define DBUS_INT32_TO_BE(val)	((dbus_int32_t) (val))
105 #  define DBUS_UINT32_TO_BE(val)	((dbus_uint32_t) (val))
106 #  define DBUS_INT32_TO_LE(val)	(DBUS_INT32_SWAP_LE_BE (val))
107 #  define DBUS_UINT32_TO_LE(val)	(DBUS_UINT32_SWAP_LE_BE (val))
108 #  ifdef DBUS_HAVE_INT64
109 #    define DBUS_INT64_TO_BE(val)	((dbus_int64_t) (val))
110 #    define DBUS_UINT64_TO_BE(val)	((dbus_uint64_t) (val))
111 #    define DBUS_INT64_TO_LE(val)	(DBUS_INT64_SWAP_LE_BE (val))
112 #    define DBUS_UINT64_TO_LE(val)	(DBUS_UINT64_SWAP_LE_BE (val))
113 #  endif /* DBUS_HAVE_INT64 */
114 
115 #else /* WORDS_BIGENDIAN */
116 
117 #  define DBUS_INT16_TO_LE(val)	((dbus_int16_t) (val))
118 #  define DBUS_UINT16_TO_LE(val)	((dbus_uint16_t) (val))
119 #  define DBUS_INT16_TO_BE(val)	((dbus_int16_t) DBUS_UINT16_SWAP_LE_BE (val))
120 #  define DBUS_UINT16_TO_BE(val)	(DBUS_UINT16_SWAP_LE_BE (val))
121 #  define DBUS_INT32_TO_LE(val)	((dbus_int32_t) (val))
122 #  define DBUS_UINT32_TO_LE(val)	((dbus_uint32_t) (val))
123 #  define DBUS_INT32_TO_BE(val)	((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
124 #  define DBUS_UINT32_TO_BE(val)	(DBUS_UINT32_SWAP_LE_BE (val))
125 #  ifdef DBUS_HAVE_INT64
126 #    define DBUS_INT64_TO_LE(val)	((dbus_int64_t) (val))
127 #    define DBUS_UINT64_TO_LE(val)	((dbus_uint64_t) (val))
128 #    define DBUS_INT64_TO_BE(val)	((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
129 #    define DBUS_UINT64_TO_BE(val)	(DBUS_UINT64_SWAP_LE_BE (val))
130 #  endif /* DBUS_HAVE_INT64 */
131 #endif
132 
133 /* The transformation is symmetric, so the FROM just maps to the TO. */
134 #define DBUS_INT16_FROM_LE(val)	 (DBUS_INT16_TO_LE (val))
135 #define DBUS_UINT16_FROM_LE(val) (DBUS_UINT16_TO_LE (val))
136 #define DBUS_INT16_FROM_BE(val)	 (DBUS_INT16_TO_BE (val))
137 #define DBUS_UINT16_FROM_BE(val) (DBUS_UINT16_TO_BE (val))
138 #define DBUS_INT32_FROM_LE(val)	 (DBUS_INT32_TO_LE (val))
139 #define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
140 #define DBUS_INT32_FROM_BE(val)	 (DBUS_INT32_TO_BE (val))
141 #define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
142 #ifdef DBUS_HAVE_INT64
143 #  define DBUS_INT64_FROM_LE(val)	 (DBUS_INT64_TO_LE (val))
144 #  define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
145 #  define DBUS_INT64_FROM_BE(val)	 (DBUS_INT64_TO_BE (val))
146 #  define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
147 #endif /* DBUS_HAVE_INT64 */
148 
149 #ifdef DBUS_DISABLE_ASSERT
150 #define _dbus_unpack_uint16(byte_order, data)           \
151    (((byte_order) == DBUS_LITTLE_ENDIAN) ?              \
152      DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)(data)) :    \
153      DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)(data)))
154 
155 #define _dbus_unpack_uint32(byte_order, data)           \
156    (((byte_order) == DBUS_LITTLE_ENDIAN) ?              \
157      DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)(data)) :    \
158      DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)(data)))
159 #endif
160 
161 #ifndef _dbus_unpack_uint16
162 dbus_uint16_t _dbus_unpack_uint16 (int                  byte_order,
163                                    const unsigned char *data);
164 #endif
165 
166 void          _dbus_pack_uint32   (dbus_uint32_t        value,
167                                    int                  byte_order,
168                                    unsigned char       *data);
169 #ifndef _dbus_unpack_uint32
170 dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
171                                    const unsigned char *data);
172 #endif
173 
174 dbus_bool_t   _dbus_marshal_set_basic         (DBusString       *str,
175                                                int               pos,
176                                                int               type,
177                                                const void       *value,
178                                                int               byte_order,
179                                                int              *old_end_pos,
180                                                int              *new_end_pos);
181 dbus_bool_t   _dbus_marshal_write_basic       (DBusString       *str,
182                                                int               insert_at,
183                                                int               type,
184                                                const void       *value,
185                                                int               byte_order,
186                                                int              *pos_after);
187 dbus_bool_t   _dbus_marshal_write_fixed_multi (DBusString       *str,
188                                                int               insert_at,
189                                                int               element_type,
190                                                const void       *value,
191                                                int               n_elements,
192                                                int               byte_order,
193                                                int              *pos_after);
194 void          _dbus_marshal_read_basic        (const DBusString *str,
195                                                int               pos,
196                                                int               type,
197                                                void             *value,
198                                                int               byte_order,
199                                                int              *new_pos);
200 void          _dbus_marshal_read_fixed_multi  (const DBusString *str,
201                                                int               pos,
202                                                int               element_type,
203                                                void             *value,
204                                                int               n_elements,
205                                                int               byte_order,
206                                                int              *new_pos);
207 void          _dbus_marshal_skip_basic        (const DBusString *str,
208                                                int               type,
209                                                int               byte_order,
210                                                int              *pos);
211 void          _dbus_marshal_skip_array        (const DBusString *str,
212                                                int               element_type,
213                                                int               byte_order,
214                                                int              *pos);
215 void          _dbus_marshal_set_uint32        (DBusString       *str,
216                                                int               pos,
217                                                dbus_uint32_t     value,
218                                                int               byte_order);
219 dbus_uint32_t _dbus_marshal_read_uint32       (const DBusString *str,
220                                                int               pos,
221                                                int               byte_order,
222                                                int              *new_pos);
223 int           _dbus_type_get_alignment        (int               typecode);
224 int           _dbus_type_get_alignment        (int               typecode);
225 const char*   _dbus_type_to_string            (int               typecode);
226 
227 int           _dbus_first_type_in_signature   (const DBusString *str,
228                                                int               pos);
229 
230 int           _dbus_first_type_in_signature_c_str   (const char       *str,
231 						     int               pos);
232 
233 void _dbus_swap_array (unsigned char *data,
234                        int            n_elements,
235                        int            alignment);
236 
237 #endif /* DBUS_MARSHAL_BASIC_H */
238