]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
2002-12-26 Anders Carlsson <andersca@codefactory.se>
authorAnders Carlsson <andersca@codefactory.se>
Wed, 25 Dec 2002 22:58:50 +0000 (22:58 +0000)
committerAnders Carlsson <andersca@codefactory.se>
Wed, 25 Dec 2002 22:58:50 +0000 (22:58 +0000)
* dbus/dbus-marshal.c: (_dbus_marshal_string),
(_dbus_demarshal_double), (_dbus_demarshal_int32),
(_dbus_demarshal_uint32), (_dbus_demarshal_string),
(_dbus_marshal_test):
* dbus/dbus-marshal.h:
Add string marshal functions and have the demarshal functions
return the new position.

ChangeLog
dbus/dbus-marshal.c
dbus/dbus-marshal.h

index c093e47f7a2dd13320d77f105b29f000e7a3ac4f..110de95ec6c982afdfa72a98aa166c1c745fec46 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2002-12-26  Anders Carlsson  <andersca@codefactory.se>
+
+       * dbus/dbus-marshal.c: (_dbus_marshal_string),
+       (_dbus_demarshal_double), (_dbus_demarshal_int32),
+       (_dbus_demarshal_uint32), (_dbus_demarshal_string),
+       (_dbus_marshal_test):
+       * dbus/dbus-marshal.h:
+       Add string marshal functions and have the demarshal functions
+       return the new position.
+       
 2002-12-25  Havoc Pennington  <hp@pobox.com>
 
        * doc/dbus-sasl-profile.txt: docs on the authentication protocol, 
index 0f9def5beaa19243216bb68275fda6b2c746a75b..bbcda141cc654e2d62eb70ddc1c94a9a54d8d555 100644 (file)
@@ -24,6 +24,7 @@
 #include "dbus-marshal.h"
 #include "dbus-internals.h"
 
+#include <string.h>
 
 /* This alignment thing is from ORBit2 */
 /* Align a value upward to a boundary, expressed as a number of bytes.
@@ -108,62 +109,126 @@ _dbus_marshal_uint32 (DBusString    *str,
   return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_uint32_t));
 }
 
+dbus_bool_t
+_dbus_marshal_string (DBusString    *str,
+                     int            byte_order,
+                     const char    *value)
+{
+  int len;
+  
+  if (!_dbus_string_set_length (str,
+                               DBUS_ALIGN_VALUE (_dbus_string_get_length (str),
+                                                 sizeof (dbus_uint32_t))))
+    return FALSE;
+
+  len = strlen (value);
+  
+  if (!_dbus_string_lengthen (str, len + 1))
+    return FALSE;
+
+  if (!_dbus_marshal_uint32 (str, byte_order, len))
+    return FALSE;
+      
+  return _dbus_string_append_len (str, value, len + 1);
+}
 
 
 double
 _dbus_demarshal_double (DBusString  *str,
                        int          byte_order,
-                       int          start)
+                       int          pos,
+                       int         *new_pos)
 {
   double retval;
   const char *buffer;
   
-  _dbus_string_get_const_data_len (str, &buffer, start, sizeof (double));
+  _dbus_string_get_const_data_len (str, &buffer, pos, sizeof (double));
 
   retval = *(double *)buffer;
   
   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
     swap_bytes ((unsigned char *)&retval, sizeof (double));
 
+  if (new_pos)
+    *new_pos = pos + sizeof (double);
+  
   return retval;  
 }
 
 dbus_int32_t
 _dbus_demarshal_int32  (DBusString *str,
                        int         byte_order,
-                       int         start)
+                       int         pos,
+                       int        *new_pos)
 {
   dbus_int32_t retval;
   const char *buffer;
 
-  _dbus_string_get_const_data_len (str, &buffer, start, sizeof (dbus_int32_t));
+  _dbus_string_get_const_data_len (str, &buffer, pos, sizeof (dbus_int32_t));
 
   retval = *(dbus_int32_t *)buffer;
 
   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
     swap_bytes ((unsigned char *)&retval, sizeof (dbus_int32_t));
 
+  if (new_pos)
+    *new_pos = pos + sizeof (dbus_int32_t);
+  
   return retval;  
 }
 
 dbus_uint32_t
 _dbus_demarshal_uint32  (DBusString *str,
                         int         byte_order,
-                        int         start)
+                        int         pos,
+                        int        *new_pos)
 {
   dbus_uint32_t retval;
   const char *buffer;
 
-  _dbus_string_get_const_data_len (str, &buffer, start, sizeof (dbus_uint32_t));
+  _dbus_string_get_const_data_len (str, &buffer, pos, sizeof (dbus_uint32_t));
 
   retval = *(dbus_uint32_t *)buffer;
 
   if (byte_order != DBUS_COMPILER_BYTE_ORDER)
     swap_bytes ((unsigned char *)&retval, sizeof (dbus_uint32_t));
 
+  if (new_pos)
+    *new_pos = pos + sizeof (dbus_uint32_t);
+  
   return retval;  
 }
 
+char *
+_dbus_demarshal_string (DBusString *str,
+                       int         byte_order,
+                       int         pos,
+                       int        *new_pos)
+{
+  int len;
+  char *retval;
+  const char *data;
+  
+  len = _dbus_demarshal_uint32 (str, byte_order, pos, &pos);
+
+  retval = dbus_malloc (len + 1);
+
+  if (!retval)
+    return NULL;
+
+  _dbus_string_get_const_data_len (str, &data, pos, len + 1);
+  
+  if (!data)
+    return NULL;
+
+  memcpy (retval, data, len + 1);
+
+  if (new_pos)
+    *new_pos = pos + len + 1;
+  
+  return retval;
+}
+
 /** @} */
 
 #ifdef DBUS_BUILD_TESTS
@@ -183,33 +248,31 @@ _dbus_marshal_test (void)
   /* Marshal doubles */
   if (!_dbus_marshal_double (&str, DBUS_BIG_ENDIAN, 3.14))
     _dbus_assert_not_reached ("could not marshal double value");
-  _dbus_assert (_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos) == 3.14);
-  pos += 8;
+  _dbus_assert (_dbus_demarshal_double (&str, DBUS_BIG_ENDIAN, pos, &pos) == 3.14);
+
   
   if (!_dbus_marshal_double (&str, DBUS_LITTLE_ENDIAN, 3.14))
     _dbus_assert_not_reached ("could not marshal double value");
-  _dbus_assert (_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos) == 3.14);
-  pos += 8;
+  _dbus_assert (_dbus_demarshal_double (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 3.14);
   
   /* Marshal signed integers */
   if (!_dbus_marshal_int32 (&str, DBUS_BIG_ENDIAN, -12345678))
     _dbus_assert_not_reached ("could not marshal signed integer value");
-  _dbus_assert (_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos) == -12345678);
-  pos += 4;
+  _dbus_assert (_dbus_demarshal_int32 (&str, DBUS_BIG_ENDIAN, pos, &pos) == -12345678);
 
   if (!_dbus_marshal_int32 (&str, DBUS_LITTLE_ENDIAN, -12345678))
     _dbus_assert_not_reached ("could not marshal signed integer value");
-  _dbus_assert (_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos) == -12345678);
-  pos += 4;
+  _dbus_assert (_dbus_demarshal_int32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == -12345678);
   
   /* Marshal unsigned integers */
   if (!_dbus_marshal_uint32 (&str, DBUS_LITTLE_ENDIAN, 0x12345678))
     _dbus_assert_not_reached ("could not marshal signed integer value");
-  _dbus_assert (_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos) == 0x12345678);
-  pos += 4;
+  _dbus_assert (_dbus_demarshal_uint32 (&str, DBUS_LITTLE_ENDIAN, pos, &pos) == 0x12345678);
   
   _dbus_string_free (&str);
 
+  /* FIXME. Add string marshal tests */
+  
   return TRUE;
 }
 
index 00a5f5020e36d0ca1936b3ca90948a0f498485a7..92d45c649d1a4ae1f496a46c82261919a5384cfe 100644 (file)
@@ -43,18 +43,27 @@ dbus_bool_t _dbus_marshal_int32  (DBusString    *str,
 dbus_bool_t _dbus_marshal_uint32 (DBusString    *str,
                                  int            byte_order,
                                  dbus_uint32_t  value);
+dbus_bool_t _dbus_marshal_string (DBusString    *str,
+                                 int            byte_order,
+                                 const char    *value);
+
 
 double        _dbus_demarshal_double (DBusString *str,
                                      int         byte_order,
-                                     int         start);
+                                     int         pos,
+                                     int        *new_pos);
 dbus_int32_t  _dbus_demarshal_int32  (DBusString *str,
                                      int         byte_order,
-                                     int         start);
+                                     int         pos,
+                                     int        *new_pos);
 dbus_uint32_t _dbus_demarshal_uint32 (DBusString *str,
                                      int         byte_order,
-                                     int         start);
-
-
+                                     int         pos,
+                                     int        *new_pos);
+char *        _dbus_demarshal_string (DBusString *str,
+                                     int         byte_order,
+                                     int         pos,
+                                     int        *new_pos);