-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-marshal-basic.h Marshalling routines for basic (primitive) types
*
* Copyright (C) 2002 CodeFactory AB
- * Copyright (C) 2004 Red Hat, Inc.
+ * Copyright (C) 2004, 2005 Red Hat, Inc.
*
* Licensed under the Academic Free License version 2.1
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
-#ifndef DBUS_MARSHAL_H
-#define DBUS_MARSHAL_H
+#ifndef DBUS_MARSHAL_BASIC_H
+#define DBUS_MARSHAL_BASIC_H
+
+#ifdef HAVE_BYTESWAP_H
+#include <byteswap.h>
+#endif
-#include <config.h>
#include <dbus/dbus-protocol.h>
#include <dbus/dbus-types.h>
#include <dbus/dbus-arch-deps.h>
#include <dbus/dbus-string.h>
-#ifndef PACKAGE
-#error "config.h not included here"
-#endif
-
-/****************************************************** Remove later */
-#undef DBUS_TYPE_INVALID
-#undef DBUS_TYPE_NIL
-#undef DBUS_TYPE_CUSTOM
-#undef DBUS_TYPE_BYTE
-#undef DBUS_TYPE_INT32
-#undef DBUS_TYPE_UINT32
-#undef DBUS_TYPE_INT64
-#undef DBUS_TYPE_UINT64
-#undef DBUS_TYPE_DOUBLE
-#undef DBUS_TYPE_STRING
-#undef DBUS_TYPE_OBJECT_PATH
-#undef DBUS_TYPE_ARRAY
-#undef DBUS_TYPE_DICT
-#undef DBUS_TYPE_VARIANT
-#undef DBUS_TYPE_STRUCT
-#undef DBUS_NUMBER_OF_TYPES
-
-
-/* Never a legitimate type */
-#define DBUS_TYPE_INVALID ((int) '\0')
-#define DBUS_TYPE_INVALID_AS_STRING "\0"
-
-/* Primitive types */
-#define DBUS_TYPE_BYTE ((int) 'y')
-#define DBUS_TYPE_BYTE_AS_STRING "y"
-#define DBUS_TYPE_BOOLEAN ((int) 'b')
-#define DBUS_TYPE_BOOLEAN_AS_STRING "b"
-#define DBUS_TYPE_INT32 ((int) 'i')
-#define DBUS_TYPE_INT32_AS_STRING "i"
-
-#define DBUS_TYPE_UINT32 ((int) 'u')
-#define DBUS_TYPE_UINT32_AS_STRING "u"
-#define DBUS_TYPE_INT64 ((int) 'x')
-#define DBUS_TYPE_INT64_AS_STRING "x"
-#define DBUS_TYPE_UINT64 ((int) 't')
-#define DBUS_TYPE_UINT64_AS_STRING "t"
-
-#define DBUS_TYPE_DOUBLE ((int) 'd')
-#define DBUS_TYPE_DOUBLE_AS_STRING "d"
-#define DBUS_TYPE_STRING ((int) 's')
-#define DBUS_TYPE_STRING_AS_STRING "s"
-#define DBUS_TYPE_OBJECT_PATH ((int) 'o')
-#define DBUS_TYPE_OBJECT_PATH_AS_STRING "o"
-#define DBUS_TYPE_SIGNATURE ((int) 'g')
-#define DBUS_TYPE_SIGNATURE_AS_STRING "g"
-
-/* Compound types */
-#define DBUS_TYPE_ARRAY ((int) 'a')
-#define DBUS_TYPE_ARRAY_AS_STRING "a"
-#define DBUS_TYPE_VARIANT ((int) 'v')
-#define DBUS_TYPE_VARIANT_AS_STRING "v"
-
-/* STRUCT is sort of special since its code can't appear in a type string,
- * instead DBUS_STRUCT_BEGIN_CHAR has to appear
- */
-#define DBUS_TYPE_STRUCT ((int) 'r')
-#define DBUS_TYPE_STRUCT_AS_STRING "r"
-
-/* Does not count INVALID */
-#define DBUS_NUMBER_OF_TYPES (13)
-
-/* characters other than typecodes that appear in type signatures */
-#define DBUS_STRUCT_BEGIN_CHAR ((int) '(')
-#define DBUS_STRUCT_BEGIN_CHAR_AS_STRING "("
-#define DBUS_STRUCT_END_CHAR ((int) ')')
-#define DBUS_STRUCT_END_CHAR_AS_STRING ")"
-
-#define DBUS_MAXIMUM_SIGNATURE_LENGTH 255
-
-static const char *
-_hack_dbus_type_to_string (int type)
-{
- switch (type)
- {
- case DBUS_TYPE_INVALID:
- return "invalid";
- case DBUS_TYPE_BOOLEAN:
- return "boolean";
- case DBUS_TYPE_INT32:
- return "int32";
- case DBUS_TYPE_UINT32:
- return "uint32";
- case DBUS_TYPE_DOUBLE:
- return "double";
- case DBUS_TYPE_STRING:
- return "string";
- case DBUS_TYPE_OBJECT_PATH:
- return "object_path";
- case DBUS_TYPE_SIGNATURE:
- return "signature";
- case DBUS_TYPE_STRUCT:
- return "struct";
- case DBUS_TYPE_ARRAY:
- return "array";
- case DBUS_TYPE_VARIANT:
- return "variant";
- case DBUS_STRUCT_BEGIN_CHAR:
- return "begin_struct";
- case DBUS_STRUCT_END_CHAR:
- return "end_struct";
- default:
- return "unknown";
- }
-}
-
-#define _dbus_type_to_string(t) _hack_dbus_type_to_string(t)
-
-/****************************************************** Remove later */
-
#ifdef WORDS_BIGENDIAN
#define DBUS_COMPILER_BYTE_ORDER DBUS_BIG_ENDIAN
#else
#define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN
#endif
+#ifdef HAVE_BYTESWAP_H
+#define DBUS_UINT16_SWAP_LE_BE_CONSTANT(val) bswap_16(val)
+#define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val) bswap_32(val)
+#else /* HAVE_BYTESWAP_H */
+
+#define DBUS_UINT16_SWAP_LE_BE_CONSTANT(val) ((dbus_uint16_t) ( \
+ (dbus_uint16_t) ((dbus_uint16_t) (val) >> 8) | \
+ (dbus_uint16_t) ((dbus_uint16_t) (val) << 8)))
+
#define DBUS_UINT32_SWAP_LE_BE_CONSTANT(val) ((dbus_uint32_t) ( \
(((dbus_uint32_t) (val) & (dbus_uint32_t) 0x000000ffU) << 24) | \
(((dbus_uint32_t) (val) & (dbus_uint32_t) 0x0000ff00U) << 8) | \
(((dbus_uint32_t) (val) & (dbus_uint32_t) 0x00ff0000U) >> 8) | \
(((dbus_uint32_t) (val) & (dbus_uint32_t) 0xff000000U) >> 24)))
+#endif /* HAVE_BYTESWAP_H */
+
#ifdef DBUS_HAVE_INT64
+#ifdef HAVE_BYTESWAP_H
+#define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val) bswap_64(val)
+#else /* HAVE_BYTESWAP_H */
+
#define DBUS_UINT64_SWAP_LE_BE_CONSTANT(val) ((dbus_uint64_t) ( \
(((dbus_uint64_t) (val) & \
(dbus_uint64_t) DBUS_UINT64_CONSTANT (0x00000000000000ff)) << 56) | \
(dbus_uint64_t) DBUS_UINT64_CONSTANT (0xff00000000000000)) >> 56)))
#endif /* DBUS_HAVE_INT64 */
+#endif /* HAVE_BYTESWAP_H */
+
+#define DBUS_UINT16_SWAP_LE_BE(val) (DBUS_UINT16_SWAP_LE_BE_CONSTANT (val))
+#define DBUS_INT16_SWAP_LE_BE(val) ((dbus_int16_t)DBUS_UINT16_SWAP_LE_BE_CONSTANT (val))
+
#define DBUS_UINT32_SWAP_LE_BE(val) (DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
#define DBUS_INT32_SWAP_LE_BE(val) ((dbus_int32_t)DBUS_UINT32_SWAP_LE_BE_CONSTANT (val))
#ifdef DBUS_HAVE_INT64
-#define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
-#define DBUS_INT64_SWAP_LE_BE(val) ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
+# define DBUS_UINT64_SWAP_LE_BE(val) (DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
+# define DBUS_INT64_SWAP_LE_BE(val) ((dbus_int64_t)DBUS_UINT64_SWAP_LE_BE_CONSTANT (val))
#endif /* DBUS_HAVE_INT64 */
#ifdef WORDS_BIGENDIAN
-#define DBUS_INT32_TO_BE(val) ((dbus_int32_t) (val))
-#define DBUS_UINT32_TO_BE(val) ((dbus_uint32_t) (val))
-#define DBUS_INT32_TO_LE(val) (DBUS_INT32_SWAP_LE_BE (val))
-#define DBUS_UINT32_TO_LE(val) (DBUS_UINT32_SWAP_LE_BE (val))
+
+# define DBUS_INT16_TO_BE(val) ((dbus_int16_t) (val))
+# define DBUS_UINT16_TO_BE(val) ((dbus_uint16_t) (val))
+# define DBUS_INT16_TO_LE(val) (DBUS_INT16_SWAP_LE_BE (val))
+# define DBUS_UINT16_TO_LE(val) (DBUS_UINT16_SWAP_LE_BE (val))
+# define DBUS_INT32_TO_BE(val) ((dbus_int32_t) (val))
+# define DBUS_UINT32_TO_BE(val) ((dbus_uint32_t) (val))
+# define DBUS_INT32_TO_LE(val) (DBUS_INT32_SWAP_LE_BE (val))
+# define DBUS_UINT32_TO_LE(val) (DBUS_UINT32_SWAP_LE_BE (val))
# ifdef DBUS_HAVE_INT64
-#define DBUS_INT64_TO_BE(val) ((dbus_int64_t) (val))
-#define DBUS_UINT64_TO_BE(val) ((dbus_uint64_t) (val))
-#define DBUS_INT64_TO_LE(val) (DBUS_INT64_SWAP_LE_BE (val))
-#define DBUS_UINT64_TO_LE(val) (DBUS_UINT64_SWAP_LE_BE (val))
+# define DBUS_INT64_TO_BE(val) ((dbus_int64_t) (val))
+# define DBUS_UINT64_TO_BE(val) ((dbus_uint64_t) (val))
+# define DBUS_INT64_TO_LE(val) (DBUS_INT64_SWAP_LE_BE (val))
+# define DBUS_UINT64_TO_LE(val) (DBUS_UINT64_SWAP_LE_BE (val))
# endif /* DBUS_HAVE_INT64 */
-#else
-#define DBUS_INT32_TO_LE(val) ((dbus_int32_t) (val))
-#define DBUS_UINT32_TO_LE(val) ((dbus_uint32_t) (val))
-#define DBUS_INT32_TO_BE(val) ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
-#define DBUS_UINT32_TO_BE(val) (DBUS_UINT32_SWAP_LE_BE (val))
+
+#else /* WORDS_BIGENDIAN */
+
+# define DBUS_INT16_TO_LE(val) ((dbus_int16_t) (val))
+# define DBUS_UINT16_TO_LE(val) ((dbus_uint16_t) (val))
+# define DBUS_INT16_TO_BE(val) ((dbus_int16_t) DBUS_UINT16_SWAP_LE_BE (val))
+# define DBUS_UINT16_TO_BE(val) (DBUS_UINT16_SWAP_LE_BE (val))
+# define DBUS_INT32_TO_LE(val) ((dbus_int32_t) (val))
+# define DBUS_UINT32_TO_LE(val) ((dbus_uint32_t) (val))
+# define DBUS_INT32_TO_BE(val) ((dbus_int32_t) DBUS_UINT32_SWAP_LE_BE (val))
+# define DBUS_UINT32_TO_BE(val) (DBUS_UINT32_SWAP_LE_BE (val))
# ifdef DBUS_HAVE_INT64
-#define DBUS_INT64_TO_LE(val) ((dbus_int64_t) (val))
-#define DBUS_UINT64_TO_LE(val) ((dbus_uint64_t) (val))
-#define DBUS_INT64_TO_BE(val) ((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
-#define DBUS_UINT64_TO_BE(val) (DBUS_UINT64_SWAP_LE_BE (val))
+# define DBUS_INT64_TO_LE(val) ((dbus_int64_t) (val))
+# define DBUS_UINT64_TO_LE(val) ((dbus_uint64_t) (val))
+# define DBUS_INT64_TO_BE(val) ((dbus_int64_t) DBUS_UINT64_SWAP_LE_BE (val))
+# define DBUS_UINT64_TO_BE(val) (DBUS_UINT64_SWAP_LE_BE (val))
# endif /* DBUS_HAVE_INT64 */
#endif
/* The transformation is symmetric, so the FROM just maps to the TO. */
+#define DBUS_INT16_FROM_LE(val) (DBUS_INT16_TO_LE (val))
+#define DBUS_UINT16_FROM_LE(val) (DBUS_UINT16_TO_LE (val))
+#define DBUS_INT16_FROM_BE(val) (DBUS_INT16_TO_BE (val))
+#define DBUS_UINT16_FROM_BE(val) (DBUS_UINT16_TO_BE (val))
#define DBUS_INT32_FROM_LE(val) (DBUS_INT32_TO_LE (val))
#define DBUS_UINT32_FROM_LE(val) (DBUS_UINT32_TO_LE (val))
#define DBUS_INT32_FROM_BE(val) (DBUS_INT32_TO_BE (val))
#define DBUS_UINT32_FROM_BE(val) (DBUS_UINT32_TO_BE (val))
#ifdef DBUS_HAVE_INT64
-#define DBUS_INT64_FROM_LE(val) (DBUS_INT64_TO_LE (val))
-#define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
-#define DBUS_INT64_FROM_BE(val) (DBUS_INT64_TO_BE (val))
-#define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
+# define DBUS_INT64_FROM_LE(val) (DBUS_INT64_TO_LE (val))
+# define DBUS_UINT64_FROM_LE(val) (DBUS_UINT64_TO_LE (val))
+# define DBUS_INT64_FROM_BE(val) (DBUS_INT64_TO_BE (val))
+# define DBUS_UINT64_FROM_BE(val) (DBUS_UINT64_TO_BE (val))
#endif /* DBUS_HAVE_INT64 */
-void _dbus_pack_int32 (dbus_int32_t value,
- int byte_order,
- unsigned char *data);
-dbus_int32_t _dbus_unpack_int32 (int byte_order,
+#ifdef DBUS_DISABLE_ASSERT
+#define _dbus_unpack_uint16(byte_order, data) \
+ (((byte_order) == DBUS_LITTLE_ENDIAN) ? \
+ DBUS_UINT16_FROM_LE (*(dbus_uint16_t*)(data)) : \
+ DBUS_UINT16_FROM_BE (*(dbus_uint16_t*)(data)))
+
+#define _dbus_unpack_uint32(byte_order, data) \
+ (((byte_order) == DBUS_LITTLE_ENDIAN) ? \
+ DBUS_UINT32_FROM_LE (*(dbus_uint32_t*)(data)) : \
+ DBUS_UINT32_FROM_BE (*(dbus_uint32_t*)(data)))
+#endif
+
+#ifndef _dbus_unpack_uint16
+dbus_uint16_t _dbus_unpack_uint16 (int byte_order,
const unsigned char *data);
+#endif
+
void _dbus_pack_uint32 (dbus_uint32_t value,
int byte_order,
unsigned char *data);
+#ifndef _dbus_unpack_uint32
dbus_uint32_t _dbus_unpack_uint32 (int byte_order,
const unsigned char *data);
+#endif
-#ifdef DBUS_HAVE_INT64
-void _dbus_pack_int64 (dbus_int64_t value,
- int byte_order,
- unsigned char *data);
-dbus_int64_t _dbus_unpack_int64 (int byte_order,
- const unsigned char *data);
-void _dbus_pack_uint64 (dbus_uint64_t value,
- int byte_order,
- unsigned char *data);
-dbus_uint64_t _dbus_unpack_uint64 (int byte_order,
- const unsigned char *data);
-#endif /* DBUS_HAVE_INT64 */
-
-void _dbus_marshal_set_int32 (DBusString *str,
- int byte_order,
- int offset,
- dbus_int32_t value);
-void _dbus_marshal_set_uint32 (DBusString *str,
- int byte_order,
- int offset,
- dbus_uint32_t value);
-#ifdef DBUS_HAVE_INT64
-void _dbus_marshal_set_int64 (DBusString *str,
- int byte_order,
- int offset,
- dbus_int64_t value);
-void _dbus_marshal_set_uint64 (DBusString *str,
- int byte_order,
- int offset,
- dbus_uint64_t value);
-#endif /* DBUS_HAVE_INT64 */
-
-dbus_bool_t _dbus_marshal_set_string (DBusString *str,
- int byte_order,
- int offset,
- const DBusString *value,
- int len);
-dbus_bool_t _dbus_marshal_basic_type (DBusString *str,
- int insert_at,
- char type,
- const void *value,
- int byte_order,
- int *pos_after);
-dbus_bool_t _dbus_marshal_basic_type_array (DBusString *str,
- int insert_at,
- char element_type,
- const void *value,
- int len,
- int byte_order,
- int *pos_after);
-dbus_uint32_t _dbus_demarshal_uint32 (const DBusString *str,
- int byte_order,
- int pos,
- int *new_pos);
-void _dbus_demarshal_basic_type (const DBusString *str,
- int type,
- void *value,
- int byte_order,
- int pos,
- int *new_pos);
-void _dbus_marshal_skip_basic_type (const DBusString *str,
- int type,
- int byte_order,
- int *pos);
-void _dbus_marshal_skip_array (const DBusString *str,
- int byte_order,
- int element_type,
- int *pos);
-dbus_bool_t _dbus_type_is_valid (int typecode);
-int _dbus_type_get_alignment (int typecode);
-
-#endif /* DBUS_MARSHAL_H */
+dbus_bool_t _dbus_marshal_set_basic (DBusString *str,
+ int pos,
+ int type,
+ const void *value,
+ int byte_order,
+ int *old_end_pos,
+ int *new_end_pos);
+dbus_bool_t _dbus_marshal_write_basic (DBusString *str,
+ int insert_at,
+ int type,
+ const void *value,
+ int byte_order,
+ int *pos_after);
+dbus_bool_t _dbus_marshal_write_fixed_multi (DBusString *str,
+ int insert_at,
+ int element_type,
+ const void *value,
+ int n_elements,
+ int byte_order,
+ int *pos_after);
+void _dbus_marshal_read_basic (const DBusString *str,
+ int pos,
+ int type,
+ void *value,
+ int byte_order,
+ int *new_pos);
+void _dbus_marshal_read_fixed_multi (const DBusString *str,
+ int pos,
+ int element_type,
+ void *value,
+ int n_elements,
+ int byte_order,
+ int *new_pos);
+void _dbus_marshal_skip_basic (const DBusString *str,
+ int type,
+ int byte_order,
+ int *pos);
+void _dbus_marshal_skip_array (const DBusString *str,
+ int element_type,
+ int byte_order,
+ int *pos);
+void _dbus_marshal_set_uint32 (DBusString *str,
+ int pos,
+ dbus_uint32_t value,
+ int byte_order);
+dbus_uint32_t _dbus_marshal_read_uint32 (const DBusString *str,
+ int pos,
+ int byte_order,
+ int *new_pos);
+int _dbus_type_get_alignment (int typecode);
+int _dbus_type_get_alignment (int typecode);
+const char* _dbus_type_to_string (int typecode);
+
+int _dbus_first_type_in_signature (const DBusString *str,
+ int pos);
+
+int _dbus_first_type_in_signature_c_str (const char *str,
+ int pos);
+
+void _dbus_swap_array (unsigned char *data,
+ int n_elements,
+ int alignment);
+
+#endif /* DBUS_MARSHAL_BASIC_H */