2005-01-17 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-marshal-basic.h
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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 #ifndef PACKAGE
35 #error "config.h not included here"
36 #endif
37
38 #ifdef WORDS_BIGENDIAN
39 #define DBUS_COMPILER_BYTE_ORDER DBUS_BIG_ENDIAN
40 #else
41 #define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN
42 #endif
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_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
72 #define DBUS_INT32_SWAP_LE_BE(val)  ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
73
74 #ifdef DBUS_HAVE_INT64
75 #define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
76 #define DBUS_INT64_SWAP_LE_BE(val)  ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
77 #endif /* DBUS_HAVE_INT64 */
78
79 #ifdef WORDS_BIGENDIAN
80 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) (val))
81 #define DBUS_UINT32_TO_BE(val)  ((dbus_uint32_t) (val))
82 #define DBUS_INT32_TO_LE(val)   (DBUS_INT32_SWAP_LE_BE (val))
83 #define DBUS_UINT32_TO_LE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
84 #  ifdef DBUS_HAVE_INT64
85 #define DBUS_INT64_TO_BE(val)   ((dbus_int64_t) (val))
86 #define DBUS_UINT64_TO_BE(val)  ((dbus_uint64_t) (val))
87 #define DBUS_INT64_TO_LE(val)   (DBUS_INT64_SWAP_LE_BE (val))
88 #define DBUS_UINT64_TO_LE(val)  (DBUS_UINT64_SWAP_LE_BE (val))
89 #  endif /* DBUS_HAVE_INT64 */
90 #else
91 #define DBUS_INT32_TO_LE(val)   ((dbus_int32_t) (val))
92 #define DBUS_UINT32_TO_LE(val)  ((dbus_uint32_t) (val))
93 #define DBUS_INT32_TO_BE(val)   ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
94 #define DBUS_UINT32_TO_BE(val)  (DBUS_UINT32_SWAP_LE_BE (val))
95 #  ifdef DBUS_HAVE_INT64
96 #define DBUS_INT64_TO_LE(val)   ((dbus_int64_t) (val))
97 #define DBUS_UINT64_TO_LE(val)  ((dbus_uint64_t) (val))
98 #define DBUS_INT64_TO_BE(val)   ((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
99 #define DBUS_UINT64_TO_BE(val)  (DBUS_UINT64_SWAP_LE_BE (val))
100 #  endif /* DBUS_HAVE_INT64 */
101 #endif
102
103 /* The transformation is symmetric, so the FROM just maps to the TO. */
104 #define DBUS_INT32_FROM_LE(val)  (DBUS_INT32_TO_LE (val))
105 #define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
106 #define DBUS_INT32_FROM_BE(val)  (DBUS_INT32_TO_BE (val))
107 #define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
108 #ifdef DBUS_HAVE_INT64
109 #define DBUS_INT64_FROM_LE(val)  (DBUS_INT64_TO_LE (val))
110 #define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
111 #define DBUS_INT64_FROM_BE(val)  (DBUS_INT64_TO_BE (val))
112 #define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
113 #endif /* DBUS_HAVE_INT64 */
114
115 #ifndef DBUS_HAVE_INT64
116 /**
117  * An 8-byte struct you could use to access int64 without having
118  * int64 support
119  */
120 typedef struct
121 {
122   dbus_uint32_t first32;  /**< first 32 bits in the 8 bytes (beware endian issues) */
123   dbus_uint32_t second32; /**< second 32 bits in the 8 bytes (beware endian issues) */
124 } DBus8ByteStruct;
125 #endif /* DBUS_HAVE_INT64 */
126
127 /**
128  * A simple 8-byte value union that lets you access 8 bytes as if they
129  * were various types; useful when dealing with basic types via
130  * void pointers and varargs.
131  */
132 typedef union
133 {
134   dbus_int32_t  i32;   /**< as int32 */
135   dbus_uint32_t u32;   /**< as int32 */
136 #ifdef DBUS_HAVE_INT64
137   dbus_int64_t  i64;   /**< as int32 */
138   dbus_uint64_t u64;   /**< as int32 */
139 #else
140   DBus8ByteStruct u64; /**< as 8-byte-struct */
141 #endif
142   double dbl;          /**< as double */
143   unsigned char byt;   /**< as byte */
144   char *str;           /**< as char* */
145 } DBusBasicValue;
146
147 #ifdef DBUS_DISABLE_ASSERT
148 #define _dbus_unpack_uint32(byte_order, data)           \
149    (((byte_order) == DBUS_LITTLE_ENDIAN) ?              \
150      DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)(data)) :    \
151      DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)(data)))
152 #endif
153
154 void          _dbus_pack_uint32   (dbus_uint32_t        value,
155                                    int                  byte_order,
156                                    unsigned char       *data);
157 #ifndef _dbus_unpack_uint32
158 dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
159                                    const unsigned char *data);
160 #endif
161
162 dbus_bool_t   _dbus_marshal_set_basic         (DBusString       *str,
163                                                int               pos,
164                                                int               type,
165                                                const void       *value,
166                                                int               byte_order,
167                                                int              *old_end_pos,
168                                                int              *new_end_pos);
169 dbus_bool_t   _dbus_marshal_write_basic       (DBusString       *str,
170                                                int               insert_at,
171                                                int               type,
172                                                const void       *value,
173                                                int               byte_order,
174                                                int              *pos_after);
175 dbus_bool_t   _dbus_marshal_write_fixed_multi (DBusString       *str,
176                                                int               insert_at,
177                                                int               element_type,
178                                                const void       *value,
179                                                int               n_elements,
180                                                int               byte_order,
181                                                int              *pos_after);
182 void          _dbus_marshal_read_basic        (const DBusString *str,
183                                                int               pos,
184                                                int               type,
185                                                void             *value,
186                                                int               byte_order,
187                                                int              *new_pos);
188 void          _dbus_marshal_read_fixed_multi  (const DBusString *str,
189                                                int               pos,
190                                                int               element_type,
191                                                void             *value,
192                                                int               n_elements,
193                                                int               byte_order,
194                                                int              *new_pos);
195 void          _dbus_marshal_skip_basic        (const DBusString *str,
196                                                int               type,
197                                                int               byte_order,
198                                                int              *pos);
199 void          _dbus_marshal_skip_array        (const DBusString *str,
200                                                int               element_type,
201                                                int               byte_order,
202                                                int              *pos);
203 void          _dbus_marshal_set_uint32        (DBusString       *str,
204                                                int               pos,
205                                                dbus_uint32_t     value,
206                                                int               byte_order);
207 dbus_uint32_t _dbus_marshal_read_uint32       (const DBusString *str,
208                                                int               pos,
209                                                int               byte_order,
210                                                int              *new_pos);
211 dbus_bool_t   _dbus_type_is_valid             (int               typecode);
212 int           _dbus_type_get_alignment        (int               typecode);
213 dbus_bool_t   _dbus_type_is_basic             (int               typecode);
214 dbus_bool_t   _dbus_type_is_container         (int               typecode);
215 dbus_bool_t   _dbus_type_is_fixed             (int               typecode);
216 const char*   _dbus_type_to_string            (int               typecode);
217
218 int           _dbus_first_type_in_signature   (const DBusString *str,
219                                                int               pos);
220
221 #endif /* DBUS_MARSHAL_BASIC_H */