D-Bus 1.4.1
|
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 #ifndef DBUS_HAVE_INT64 00150 00154 typedef struct 00155 { 00156 dbus_uint32_t first32; 00157 dbus_uint32_t second32; 00158 } DBus8ByteStruct; 00159 #endif /* DBUS_HAVE_INT64 */ 00160 00166 typedef union 00167 { 00168 dbus_int16_t i16; 00169 dbus_uint16_t u16; 00170 dbus_int32_t i32; 00171 dbus_uint32_t u32; 00172 #ifdef DBUS_HAVE_INT64 00173 dbus_int64_t i64; 00174 dbus_uint64_t u64; 00175 #else 00176 DBus8ByteStruct u64; 00177 #endif 00178 double dbl; 00179 unsigned char byt; 00180 char *str; 00181 } DBusBasicValue; 00182 00183 #ifdef DBUS_DISABLE_ASSERT 00184 #define _dbus_unpack_uint16(byte_order, data) \ 00185 (((byte_order) == DBUS_LITTLE_ENDIAN) ? \ 00186 DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)(data)) : \ 00187 DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)(data))) 00188 00189 #define _dbus_unpack_uint32(byte_order, data) \ 00190 (((byte_order) == DBUS_LITTLE_ENDIAN) ? \ 00191 DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)(data)) : \ 00192 DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)(data))) 00193 #endif 00194 00195 #ifndef _dbus_unpack_uint16 00196 dbus_uint16_t _dbus_unpack_uint16 (int byte_order, 00197 const unsigned char *data); 00198 #endif 00199 00200 void _dbus_pack_uint32 (dbus_uint32_t value, 00201 int byte_order, 00202 unsigned char *data); 00203 #ifndef _dbus_unpack_uint32 00204 dbus_uint32_t _dbus_unpack_uint32 (int byte_order, 00205 const unsigned char *data); 00206 #endif 00207 00208 dbus_bool_t _dbus_marshal_set_basic (DBusString *str, 00209 int pos, 00210 int type, 00211 const void *value, 00212 int byte_order, 00213 int *old_end_pos, 00214 int *new_end_pos); 00215 dbus_bool_t _dbus_marshal_write_basic (DBusString *str, 00216 int insert_at, 00217 int type, 00218 const void *value, 00219 int byte_order, 00220 int *pos_after); 00221 dbus_bool_t _dbus_marshal_write_fixed_multi (DBusString *str, 00222 int insert_at, 00223 int element_type, 00224 const void *value, 00225 int n_elements, 00226 int byte_order, 00227 int *pos_after); 00228 void _dbus_marshal_read_basic (const DBusString *str, 00229 int pos, 00230 int type, 00231 void *value, 00232 int byte_order, 00233 int *new_pos); 00234 void _dbus_marshal_read_fixed_multi (const DBusString *str, 00235 int pos, 00236 int element_type, 00237 void *value, 00238 int n_elements, 00239 int byte_order, 00240 int *new_pos); 00241 void _dbus_marshal_skip_basic (const DBusString *str, 00242 int type, 00243 int byte_order, 00244 int *pos); 00245 void _dbus_marshal_skip_array (const DBusString *str, 00246 int element_type, 00247 int byte_order, 00248 int *pos); 00249 void _dbus_marshal_set_uint32 (DBusString *str, 00250 int pos, 00251 dbus_uint32_t value, 00252 int byte_order); 00253 dbus_uint32_t _dbus_marshal_read_uint32 (const DBusString *str, 00254 int pos, 00255 int byte_order, 00256 int *new_pos); 00257 dbus_bool_t _dbus_type_is_valid (int typecode); 00258 int _dbus_type_get_alignment (int typecode); 00259 dbus_bool_t _dbus_type_is_fixed (int typecode); 00260 int _dbus_type_get_alignment (int typecode); 00261 const char* _dbus_type_to_string (int typecode); 00262 00263 int _dbus_first_type_in_signature (const DBusString *str, 00264 int pos); 00265 00266 int _dbus_first_type_in_signature_c_str (const char *str, 00267 int pos); 00268 00269 void _dbus_swap_array (unsigned char *data, 00270 int n_elements, 00271 int alignment); 00272 00273 #endif /* DBUS_MARSHAL_BASIC_H */