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