D-Bus  1.6.8
dbus-marshal-basic.h
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-marshal-basic.h  Marshalling routines for basic (primitive) types
00003  *
00004  * Copyright (C) 2002  CodeFactory AB
00005  * Copyright (C) 2004, 2005  Red Hat, Inc.
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  */
00024 
00025 #ifndef DBUS_MARSHAL_BASIC_H
00026 #define DBUS_MARSHAL_BASIC_H
00027 
00028 #ifdef HAVE_BYTESWAP_H
00029 #include <byteswap.h>
00030 #endif
00031 
00032 #include <dbus/dbus-protocol.h>
00033 #include <dbus/dbus-types.h>
00034 #include <dbus/dbus-arch-deps.h>
00035 #include <dbus/dbus-string.h>
00036 
00037 #ifdef WORDS_BIGENDIAN
00038 #define DBUS_COMPILER_BYTE_ORDER DBUS_BIG_ENDIAN
00039 #else
00040 #define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN
00041 #endif
00042 
00043 #ifdef HAVE_BYTESWAP_H
00044 #define DBUS_UINT16_SWAP_LE_BE_CONSTANT(val) bswap_16(val)
00045 #define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val) bswap_32(val)
00046 #else /* HAVE_BYTESWAP_H */
00047 
00048 #define DBUS_UINT16_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint16_t) (      \
00049     (dbus_uint16_t) ((dbus_uint16_t) (val) >> 8) |                      \
00050     (dbus_uint16_t) ((dbus_uint16_t) (val) << 8)))
00051 
00052 #define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint32_t) (      \
00053     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x000000ffU) << 24) |     \
00054     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x0000ff00U) <<  8) |     \
00055     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0x00ff0000U) >>  8) |     \
00056     (((dbus_uint32_t) (val) & (dbus_uint32_t) 0xff000000U) >> 24)))
00057 
00058 #endif /* HAVE_BYTESWAP_H */
00059 
00060 #ifdef DBUS_HAVE_INT64
00061 
00062 #ifdef HAVE_BYTESWAP_H
00063 #define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val) bswap_64(val)
00064 #else /* HAVE_BYTESWAP_H */
00065 
00066 #define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val)    ((dbus_uint64_t) (              \
00067       (((dbus_uint64_t) (val) &                                                 \
00068         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000000000ff)) << 56) |    \
00069       (((dbus_uint64_t) (val) &                                                 \
00070         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000000000ff00)) << 40) |    \
00071       (((dbus_uint64_t) (val) &                                                 \
00072         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000000000ff0000)) << 24) |    \
00073       (((dbus_uint64_t) (val) &                                                 \
00074         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000ff000000)) <<  8) |    \
00075       (((dbus_uint64_t) (val) &                                                 \
00076         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x000000ff00000000)) >>  8) |    \
00077       (((dbus_uint64_t) (val) &                                                 \
00078         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x0000ff0000000000)) >> 24) |    \
00079       (((dbus_uint64_t) (val) &                                                 \
00080         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00ff000000000000)) >> 40) |    \
00081       (((dbus_uint64_t) (val) &                                                 \
00082         (dbus_uint64_t) DBUS_UINT64_CONSTANT (0xff00000000000000)) >> 56)))
00083 #endif /* DBUS_HAVE_INT64 */
00084 
00085 #endif /* HAVE_BYTESWAP_H */
00086 
00087 #define DBUS_UINT16_SWAP_LE_BE(val) (DBUS_UINT16_SWAP_LE_BE_CONSTANT (val))
00088 #define DBUS_INT16_SWAP_LE_BE(val)  ((dbus_int16_t)DBUS_UINT16_SWAP_LE_BE_CONSTANT (val))
00089 
00090 #define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
00091 #define DBUS_INT32_SWAP_LE_BE(val)  ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
00092 
00093 #ifdef DBUS_HAVE_INT64
00094 #  define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
00095 #  define DBUS_INT64_SWAP_LE_BE(val)  ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
00096 #endif /* DBUS_HAVE_INT64 */
00097 
00098 #ifdef WORDS_BIGENDIAN
00099 
00100 #  define DBUS_INT16_TO_BE(val) ((dbus_int16_t) (val))
00101 #  define DBUS_UINT16_TO_BE(val)        ((dbus_uint16_t) (val))
00102 #  define DBUS_INT16_TO_LE(val) (DBUS_INT16_SWAP_LE_BE (val))
00103 #  define DBUS_UINT16_TO_LE(val)        (DBUS_UINT16_SWAP_LE_BE (val))
00104 #  define DBUS_INT32_TO_BE(val) ((dbus_int32_t) (val))
00105 #  define DBUS_UINT32_TO_BE(val)        ((dbus_uint32_t) (val))
00106 #  define DBUS_INT32_TO_LE(val) (DBUS_INT32_SWAP_LE_BE (val))
00107 #  define DBUS_UINT32_TO_LE(val)        (DBUS_UINT32_SWAP_LE_BE (val))
00108 #  ifdef DBUS_HAVE_INT64
00109 #    define DBUS_INT64_TO_BE(val)       ((dbus_int64_t) (val))
00110 #    define DBUS_UINT64_TO_BE(val)      ((dbus_uint64_t) (val))
00111 #    define DBUS_INT64_TO_LE(val)       (DBUS_INT64_SWAP_LE_BE (val))
00112 #    define DBUS_UINT64_TO_LE(val)      (DBUS_UINT64_SWAP_LE_BE (val))
00113 #  endif /* DBUS_HAVE_INT64 */
00114 
00115 #else /* WORDS_BIGENDIAN */
00116 
00117 #  define DBUS_INT16_TO_LE(val) ((dbus_int16_t) (val))
00118 #  define DBUS_UINT16_TO_LE(val)        ((dbus_uint16_t) (val))
00119 #  define DBUS_INT16_TO_BE(val) ((dbus_int16_t) DBUS_UINT16_SWAP_LE_BE (val))
00120 #  define DBUS_UINT16_TO_BE(val)        (DBUS_UINT16_SWAP_LE_BE (val))
00121 #  define DBUS_INT32_TO_LE(val) ((dbus_int32_t) (val))
00122 #  define DBUS_UINT32_TO_LE(val)        ((dbus_uint32_t) (val))
00123 #  define DBUS_INT32_TO_BE(val) ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
00124 #  define DBUS_UINT32_TO_BE(val)        (DBUS_UINT32_SWAP_LE_BE (val))
00125 #  ifdef DBUS_HAVE_INT64
00126 #    define DBUS_INT64_TO_LE(val)       ((dbus_int64_t) (val))
00127 #    define DBUS_UINT64_TO_LE(val)      ((dbus_uint64_t) (val))
00128 #    define DBUS_INT64_TO_BE(val)       ((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
00129 #    define DBUS_UINT64_TO_BE(val)      (DBUS_UINT64_SWAP_LE_BE (val))
00130 #  endif /* DBUS_HAVE_INT64 */
00131 #endif
00132 
00133 /* The transformation is symmetric, so the FROM just maps to the TO. */
00134 #define DBUS_INT16_FROM_LE(val)  (DBUS_INT16_TO_LE (val))
00135 #define DBUS_UINT16_FROM_LE(val) (DBUS_UINT16_TO_LE (val))
00136 #define DBUS_INT16_FROM_BE(val)  (DBUS_INT16_TO_BE (val))
00137 #define DBUS_UINT16_FROM_BE(val) (DBUS_UINT16_TO_BE (val))
00138 #define DBUS_INT32_FROM_LE(val)  (DBUS_INT32_TO_LE (val))
00139 #define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
00140 #define DBUS_INT32_FROM_BE(val)  (DBUS_INT32_TO_BE (val))
00141 #define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
00142 #ifdef DBUS_HAVE_INT64
00143 #  define DBUS_INT64_FROM_LE(val)        (DBUS_INT64_TO_LE (val))
00144 #  define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
00145 #  define DBUS_INT64_FROM_BE(val)        (DBUS_INT64_TO_BE (val))
00146 #  define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
00147 #endif /* DBUS_HAVE_INT64 */
00148 
00149 #ifdef DBUS_DISABLE_ASSERT
00150 #define _dbus_unpack_uint16(byte_order, data)           \
00151    (((byte_order) == DBUS_LITTLE_ENDIAN) ?              \
00152      DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)(data)) :    \
00153      DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)(data)))
00154 
00155 #define _dbus_unpack_uint32(byte_order, data)           \
00156    (((byte_order) == DBUS_LITTLE_ENDIAN) ?              \
00157      DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)(data)) :    \
00158      DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)(data)))
00159 #endif
00160 
00161 #ifndef _dbus_unpack_uint16
00162 dbus_uint16_t _dbus_unpack_uint16 (int                  byte_order,
00163                                    const unsigned char *data);
00164 #endif
00165 
00166 void          _dbus_pack_uint32   (dbus_uint32_t        value,
00167                                    int                  byte_order,
00168                                    unsigned char       *data);
00169 #ifndef _dbus_unpack_uint32
00170 dbus_uint32_t _dbus_unpack_uint32 (int                  byte_order,
00171                                    const unsigned char *data);
00172 #endif
00173 
00174 dbus_bool_t   _dbus_marshal_set_basic         (DBusString       *str,
00175                                                int               pos,
00176                                                int               type,
00177                                                const void       *value,
00178                                                int               byte_order,
00179                                                int              *old_end_pos,
00180                                                int              *new_end_pos);
00181 dbus_bool_t   _dbus_marshal_write_basic       (DBusString       *str,
00182                                                int               insert_at,
00183                                                int               type,
00184                                                const void       *value,
00185                                                int               byte_order,
00186                                                int              *pos_after);
00187 dbus_bool_t   _dbus_marshal_write_fixed_multi (DBusString       *str,
00188                                                int               insert_at,
00189                                                int               element_type,
00190                                                const void       *value,
00191                                                int               n_elements,
00192                                                int               byte_order,
00193                                                int              *pos_after);
00194 void          _dbus_marshal_read_basic        (const DBusString *str,
00195                                                int               pos,
00196                                                int               type,
00197                                                void             *value,
00198                                                int               byte_order,
00199                                                int              *new_pos);
00200 void          _dbus_marshal_read_fixed_multi  (const DBusString *str,
00201                                                int               pos,
00202                                                int               element_type,
00203                                                void             *value,
00204                                                int               n_elements,
00205                                                int               byte_order,
00206                                                int              *new_pos);
00207 void          _dbus_marshal_skip_basic        (const DBusString *str,
00208                                                int               type,
00209                                                int               byte_order,
00210                                                int              *pos);
00211 void          _dbus_marshal_skip_array        (const DBusString *str,
00212                                                int               element_type,
00213                                                int               byte_order,
00214                                                int              *pos);
00215 void          _dbus_marshal_set_uint32        (DBusString       *str,
00216                                                int               pos,
00217                                                dbus_uint32_t     value,
00218                                                int               byte_order);
00219 dbus_uint32_t _dbus_marshal_read_uint32       (const DBusString *str,
00220                                                int               pos,
00221                                                int               byte_order,
00222                                                int              *new_pos);
00223 int           _dbus_type_get_alignment        (int               typecode);
00224 int           _dbus_type_get_alignment        (int               typecode);
00225 const char*   _dbus_type_to_string            (int               typecode);
00226 
00227 int           _dbus_first_type_in_signature   (const DBusString *str,
00228                                                int               pos);
00229 
00230 int           _dbus_first_type_in_signature_c_str   (const char       *str,
00231                                                      int               pos);
00232 
00233 void _dbus_swap_array (unsigned char *data,
00234                        int            n_elements,
00235                        int            alignment);
00236 
00237 #endif /* DBUS_MARSHAL_BASIC_H */