dbus-marshal-basic.h revision ec901d786f6de6e6f870279e2d955f491619c559
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 *
23 */
24
25#ifndef DBUS_MARSHAL_BASIC_H
26#define DBUS_MARSHAL_BASIC_H
27
28#include <config.h>
29
30#ifdef HAVE_BYTESWAP_H
31#include <byteswap.h>
32#endif
33
34#include <dbus/dbus-protocol.h>
35#include <dbus/dbus-types.h>
36#include <dbus/dbus-arch-deps.h>
37#include <dbus/dbus-string.h>
38
39#ifdef WORDS_BIGENDIAN
40#define DBUS_COMPILER_BYTE_ORDER DBUS_BIG_ENDIAN
41#else
42#define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN
43#endif
44
45#ifdef HAVE_BYTESWAP_H
46#define DBUS_UINT16_SWAP_LE_BE_CONSTANT(val) bswap_16(val)
47#define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val) bswap_32(val)
48#else /* HAVE_BYTESWAP_H */
49
50#define DBUS_UINT16_SWAP_LE_BE_CONSTANT(val)	((dbus_uint16_t) (      \
51    (dbus_uint16_t) ((dbus_uint16_t) (val) >> 8) |                      \
52    (dbus_uint16_t) ((dbus_uint16_t) (val) << 8)))
53
54#define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val)	((dbus_uint32_t) (      \
55    (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x000000ffU) << 24) |     \
56    (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x0000ff00U) <<  8) |     \
57    (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x00ff0000U) >>  8) |     \
58    (((dbus_uint32_t) (val) & (dbus_uint32_t) 0xff000000U) >> 24)))
59
60#endif /* HAVE_BYTESWAP_H */
61
62#ifdef DBUS_HAVE_INT64
63
64#ifdef HAVE_BYTESWAP_H
65#define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val) bswap_64(val)
66#else /* HAVE_BYTESWAP_H */
67
68#define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val)	((dbus_uint64_t) (              \
69      (((dbus_uint64_t) (val) &                                                 \
70	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000000000ff)) << 56) |    \
71      (((dbus_uint64_t) (val) &                                                 \
72	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000000000ff00)) << 40) |    \
73      (((dbus_uint64_t) (val) &                                                 \
74	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000000000ff0000)) << 24) |    \
75      (((dbus_uint64_t) (val) &                                                 \
76	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000ff000000)) <<  8) |    \
77      (((dbus_uint64_t) (val) &                                                 \
78	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000ff00000000)) >>  8) |    \
79      (((dbus_uint64_t) (val) &                                                 \
80	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000ff0000000000)) >> 24) |    \
81      (((dbus_uint64_t) (val) &                                                 \
82	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00ff000000000000)) >> 40) |    \
83      (((dbus_uint64_t) (val) &                                                 \
84	(dbus_uint64_t) DBUS_UINT64_CONSTANT (0xff00000000000000)) >> 56)))
85#endif /* DBUS_HAVE_INT64 */
86
87#endif /* HAVE_BYTESWAP_H */
88
89#define DBUS_UINT16_SWAP_LE_BE(val) (DBUS_UINT16_SWAP_LE_BE_CONSTANT (val))
90#define DBUS_INT16_SWAP_LE_BE(val)  ((dbus_int16_t)DBUS_UINT16_SWAP_LE_BE_CONSTANT (val))
91
92#define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
93#define DBUS_INT32_SWAP_LE_BE(val)  ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
94
95#ifdef DBUS_HAVE_INT64
96#  define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
97#  define DBUS_INT64_SWAP_LE_BE(val)  ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
98#endif /* DBUS_HAVE_INT64 */
99
100#ifdef WORDS_BIGENDIAN
101
102#  define DBUS_INT16_TO_BE(val)	((dbus_int16_t) (val))
103#  define DBUS_UINT16_TO_BE(val)	((dbus_uint16_t) (val))
104#  define DBUS_INT16_TO_LE(val)	(DBUS_INT16_SWAP_LE_BE (val))
105#  define DBUS_UINT16_TO_LE(val)	(DBUS_UINT16_SWAP_LE_BE (val))
106#  define DBUS_INT32_TO_BE(val)	((dbus_int32_t) (val))
107#  define DBUS_UINT32_TO_BE(val)	((dbus_uint32_t) (val))
108#  define DBUS_INT32_TO_LE(val)	(DBUS_INT32_SWAP_LE_BE (val))
109#  define DBUS_UINT32_TO_LE(val)	(DBUS_UINT32_SWAP_LE_BE (val))
110#  ifdef DBUS_HAVE_INT64
111#    define DBUS_INT64_TO_BE(val)	((dbus_int64_t) (val))
112#    define DBUS_UINT64_TO_BE(val)	((dbus_uint64_t) (val))
113#    define DBUS_INT64_TO_LE(val)	(DBUS_INT64_SWAP_LE_BE (val))
114#    define DBUS_UINT64_TO_LE(val)	(DBUS_UINT64_SWAP_LE_BE (val))
115#  endif /* DBUS_HAVE_INT64 */
116
117#else /* WORDS_BIGENDIAN */
118
119#  define DBUS_INT16_TO_LE(val)	((dbus_int16_t) (val))
120#  define DBUS_UINT16_TO_LE(val)	((dbus_uint16_t) (val))
121#  define DBUS_INT16_TO_BE(val)	((dbus_int16_t) DBUS_UINT16_SWAP_LE_BE (val))
122#  define DBUS_UINT16_TO_BE(val)	(DBUS_UINT16_SWAP_LE_BE (val))
123#  define DBUS_INT32_TO_LE(val)	((dbus_int32_t) (val))
124#  define DBUS_UINT32_TO_LE(val)	((dbus_uint32_t) (val))
125#  define DBUS_INT32_TO_BE(val)	((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
126#  define DBUS_UINT32_TO_BE(val)	(DBUS_UINT32_SWAP_LE_BE (val))
127#  ifdef DBUS_HAVE_INT64
128#    define DBUS_INT64_TO_LE(val)	((dbus_int64_t) (val))
129#    define DBUS_UINT64_TO_LE(val)	((dbus_uint64_t) (val))
130#    define DBUS_INT64_TO_BE(val)	((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
131#    define DBUS_UINT64_TO_BE(val)	(DBUS_UINT64_SWAP_LE_BE (val))
132#  endif /* DBUS_HAVE_INT64 */
133#endif
134
135/* The transformation is symmetric, so the FROM just maps to the TO. */
136#define DBUS_INT16_FROM_LE(val)	 (DBUS_INT16_TO_LE (val))
137#define DBUS_UINT16_FROM_LE(val) (DBUS_UINT16_TO_LE (val))
138#define DBUS_INT16_FROM_BE(val)	 (DBUS_INT16_TO_BE (val))
139#define DBUS_UINT16_FROM_BE(val) (DBUS_UINT16_TO_BE (val))
140#define DBUS_INT32_FROM_LE(val)	 (DBUS_INT32_TO_LE (val))
141#define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
142#define DBUS_INT32_FROM_BE(val)	 (DBUS_INT32_TO_BE (val))
143#define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
144#ifdef DBUS_HAVE_INT64
145#  define DBUS_INT64_FROM_LE(val)	 (DBUS_INT64_TO_LE (val))
146#  define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
147#  define DBUS_INT64_FROM_BE(val)	 (DBUS_INT64_TO_BE (val))
148#  define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
149#endif /* DBUS_HAVE_INT64 */
150
151#ifndef DBUS_HAVE_INT64
152/**
153 * An 8-byte struct you could use to access int64 without having
154 * int64 support
155 */
156typedef struct
157{
158  dbus_uint32_t first32;  /**< first 32 bits in the 8 bytes (beware endian issues) */
159  dbus_uint32_t second32; /**< second 32 bits in the 8 bytes (beware endian issues) */
160} DBus8ByteStruct;
161#endif /* DBUS_HAVE_INT64 */
162
163/**
164 * A simple 8-byte value union that lets you access 8 bytes as if they
165 * were various types; useful when dealing with basic types via
166 * void pointers and varargs.
167 */
168typedef union
169{
170  dbus_int16_t  i16;   /**< as int16 */
171  dbus_uint16_t u16;   /**< as int16 */
172  dbus_int32_t  i32;   /**< as int32 */
173  dbus_uint32_t u32;   /**< as int32 */
174#ifdef DBUS_HAVE_INT64
175  dbus_int64_t  i64;   /**< as int64 */
176  dbus_uint64_t u64;   /**< as int64 */
177#else
178  DBus8ByteStruct u64; /**< as 8-byte-struct */
179#endif
180  double dbl;          /**< as double */
181  unsigned char byt;   /**< as byte */
182  char *str;           /**< as char* */
183} DBusBasicValue;
184
185#ifdef DBUS_DISABLE_ASSERT
186#define _dbus_unpack_uint16(byte_order, data)           \
187   (((byte_order) == DBUS_LITTLE_ENDIAN) ?              \
188     DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)(data)) :    \
189     DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)(data)))
190
191#define _dbus_unpack_uint32(byte_order, data)           \
192   (((byte_order) == DBUS_LITTLE_ENDIAN) ?              \
193     DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)(data)) :    \
194     DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)(data)))
195#endif
196
197#ifndef _dbus_unpack_uint16
198dbus_uint16_t _dbus_unpack_uint16 (int                  byte_order,
199                                   const unsigned char *data);
200#endif
201
202void          _dbus_pack_uint32   (dbus_uint32_t        value,
203                                   int                  byte_order,
204                                   unsigned char       *data);
205#ifndef _dbus_unpack_uint32
206dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
207                                   const unsigned char *data);
208#endif
209
210dbus_bool_t   _dbus_marshal_set_basic         (DBusString       *str,
211                                               int               pos,
212                                               int               type,
213                                               const void       *value,
214                                               int               byte_order,
215                                               int              *old_end_pos,
216                                               int              *new_end_pos);
217dbus_bool_t   _dbus_marshal_write_basic       (DBusString       *str,
218                                               int               insert_at,
219                                               int               type,
220                                               const void       *value,
221                                               int               byte_order,
222                                               int              *pos_after);
223dbus_bool_t   _dbus_marshal_write_fixed_multi (DBusString       *str,
224                                               int               insert_at,
225                                               int               element_type,
226                                               const void       *value,
227                                               int               n_elements,
228                                               int               byte_order,
229                                               int              *pos_after);
230void          _dbus_marshal_read_basic        (const DBusString *str,
231                                               int               pos,
232                                               int               type,
233                                               void             *value,
234                                               int               byte_order,
235                                               int              *new_pos);
236void          _dbus_marshal_read_fixed_multi  (const DBusString *str,
237                                               int               pos,
238                                               int               element_type,
239                                               void             *value,
240                                               int               n_elements,
241                                               int               byte_order,
242                                               int              *new_pos);
243void          _dbus_marshal_skip_basic        (const DBusString *str,
244                                               int               type,
245                                               int               byte_order,
246                                               int              *pos);
247void          _dbus_marshal_skip_array        (const DBusString *str,
248                                               int               element_type,
249                                               int               byte_order,
250                                               int              *pos);
251void          _dbus_marshal_set_uint32        (DBusString       *str,
252                                               int               pos,
253                                               dbus_uint32_t     value,
254                                               int               byte_order);
255dbus_uint32_t _dbus_marshal_read_uint32       (const DBusString *str,
256                                               int               pos,
257                                               int               byte_order,
258                                               int              *new_pos);
259dbus_bool_t   _dbus_type_is_valid             (int               typecode);
260int           _dbus_type_get_alignment        (int               typecode);
261dbus_bool_t   _dbus_type_is_fixed             (int               typecode);
262int           _dbus_type_get_alignment        (int               typecode);
263const char*   _dbus_type_to_string            (int               typecode);
264
265int           _dbus_first_type_in_signature   (const DBusString *str,
266                                               int               pos);
267
268int           _dbus_first_type_in_signature_c_str   (const char       *str,
269						     int               pos);
270
271void _dbus_swap_array (unsigned char *data,
272                       int            n_elements,
273                       int            alignment);
274
275#endif /* DBUS_MARSHAL_BASIC_H */
276