From e537e421ff4f092621fcd9f6b51526a017ad020c Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sun, 16 Mar 2003 13:32:10 +0000 Subject: 2003-03-16 Anders Carlsson * dbus/dbus-connection.c: (dbus_connection_send_with_reply_and_block): Decrease connection->n_incoming when removing an entry from the list. * dbus/dbus-dict.c: (dbus_dict_entry_free), (dbus_dict_set_boolean_array), (dbus_dict_set_int32_array), (dbus_dict_set_uint32_array), (dbus_dict_set_double_array), (dbus_dict_set_byte_array), (dbus_dict_set_string_array), (dbus_dict_get_boolean_array), (dbus_dict_get_double_array), (dbus_dict_get_byte_array): Handle NULL arrays and strings. Also add support for byte arrays. * dbus/dbus-marshal.c: (_dbus_marshal_byte_array), (_dbus_marshal_dict), (_dbus_demarshal_byte_array), (_dbus_demarshal_int32_array), (_dbus_demarshal_uint32_array), (_dbus_demarshal_double_array), (_dbus_demarshal_string_array), (_dbus_demarshal_dict), (demarshal_and_validate_len), (_dbus_marshal_validate_arg), (_dbus_marshal_test): * dbus/dbus-marshal.h: Add support for marshalling and demarshalling empty arrays and strings. * dbus/dbus-message.c: (dbus_message_append_args_valist), (dbus_message_append_string_array), (dbus_message_iter_get_boolean), (dbus_message_iter_get_boolean_array), (dbus_message_iter_get_int32_array), (dbus_message_iter_get_uint32_array), (dbus_message_iter_get_double_array), (dbus_message_iter_get_byte_array), (dbus_message_iter_get_string_array), (dbus_message_iter_get_dict), (check_message_handling): Add support for getting empty arrays and dicts. * dbus/dbus-string.c: (_dbus_string_validate_utf8): Don't do any validation at all for now, that's better than just checking for ASCII. * test/data/valid-messages/emptiness.message: New test message with lots of empty arrays. --- dbus/dbus-dict.c | 128 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 102 insertions(+), 26 deletions(-) (limited to 'dbus/dbus-dict.c') diff --git a/dbus/dbus-dict.c b/dbus/dbus-dict.c index 2edc71fd..bf1d6c92 100644 --- a/dbus/dbus-dict.c +++ b/dbus/dbus-dict.c @@ -55,7 +55,7 @@ typedef struct struct { unsigned char *value; int len; - } boolean_array; + } byte_array; struct { dbus_int32_t *value; int len; @@ -92,8 +92,9 @@ dbus_dict_entry_free (DBusDictEntry *entry) case DBUS_TYPE_STRING: dbus_free (entry->v.string_value); break; + case DBUS_TYPE_BYTE_ARRAY: case DBUS_TYPE_BOOLEAN_ARRAY: - dbus_free (entry->v.boolean_array.value); + dbus_free (entry->v.byte_array.value); break; case DBUS_TYPE_INT32_ARRAY: dbus_free (entry->v.int32_array.value); @@ -473,18 +474,23 @@ dbus_dict_set_boolean_array (DBusDict *dict, DBusDictEntry *entry; unsigned char *tmp; - tmp = dbus_malloc (len); - - if (!tmp) - return FALSE; + if (len == 0) + tmp = NULL; + else + { + tmp = dbus_malloc (len); + + if (!tmp) + return FALSE; - memcpy (tmp, value, len); + memcpy (tmp, value, len); + } if (insert_entry (dict, key, &entry)) { entry->type = DBUS_TYPE_BOOLEAN_ARRAY; - entry->v.boolean_array.value = tmp; - entry->v.boolean_array.len = len; + entry->v.byte_array.value = tmp; + entry->v.byte_array.len = len; return TRUE; } @@ -511,11 +517,16 @@ dbus_dict_set_int32_array (DBusDict *dict, DBusDictEntry *entry; dbus_int32_t *tmp; - tmp = dbus_new (dbus_int32_t, len); - - if (!tmp) - return FALSE; - + if (len == 0) + tmp = NULL; + else + { + tmp = dbus_new (dbus_int32_t, len); + + if (!tmp) + return FALSE; + } + memcpy (tmp, value, len * sizeof (dbus_int32_t)); if (insert_entry (dict, key, &entry)) @@ -550,12 +561,17 @@ dbus_dict_set_uint32_array (DBusDict *dict, DBusDictEntry *entry; dbus_uint32_t *tmp; - tmp = dbus_new (dbus_uint32_t, len); - - if (!tmp) - return FALSE; - - memcpy (tmp, value, len * sizeof (dbus_uint32_t)); + if (len == 0) + tmp = NULL; + else + { + tmp = dbus_new (dbus_uint32_t, len); + + if (!tmp) + return FALSE; + + memcpy (tmp, value, len * sizeof (dbus_uint32_t)); + } if (insert_entry (dict, key, &entry)) { @@ -588,12 +604,17 @@ dbus_dict_set_double_array (DBusDict *dict, DBusDictEntry *entry; double *tmp; - tmp = dbus_new (double, len); + if (len == 0) + tmp = NULL; + else + { + tmp = dbus_new (double, len); - if (!tmp) - return FALSE; + if (!tmp) + return FALSE; - memcpy (tmp, value, len * sizeof (double)); + memcpy (tmp, value, len * sizeof (double)); + } if (insert_entry (dict, key, &entry)) { @@ -607,6 +628,40 @@ dbus_dict_set_double_array (DBusDict *dict, return FALSE; } +dbus_bool_t +dbus_dict_set_byte_array (DBusDict *dict, + const char *key, + unsigned const char *value, + int len) +{ + DBusDictEntry *entry; + unsigned char *tmp; + + if (len == 0) + tmp = NULL; + else + { + tmp = dbus_malloc (len); + + if (!tmp) + return FALSE; + + memcpy (tmp, value, len); + } + + if (insert_entry (dict, key, &entry)) + { + entry->type = DBUS_TYPE_BYTE_ARRAY; + entry->v.byte_array.value = tmp; + entry->v.byte_array.len = len; + + return TRUE; + } + else + return FALSE; +} + + /** * Adds a string array to the dict. If a value with the same key * already exists, then it will be replaced by the new value. @@ -631,6 +686,8 @@ dbus_dict_set_string_array (DBusDict *dict, if (!tmp) return FALSE; + tmp[len] = NULL; + for (i = 0; i < len; i++) { tmp[i] = _dbus_strdup (value[i]); @@ -806,8 +863,8 @@ dbus_dict_get_boolean_array (DBusDict *dict, if (!entry || entry->type != DBUS_TYPE_BOOLEAN_ARRAY) return FALSE; - *value = entry->v.boolean_array.value; - *len = entry->v.boolean_array.len; + *value = entry->v.byte_array.value; + *len = entry->v.byte_array.len; return TRUE; } @@ -899,6 +956,25 @@ dbus_dict_get_double_array (DBusDict *dict, return TRUE; } +dbus_bool_t +dbus_dict_get_byte_array (DBusDict *dict, + const char *key, + unsigned const char **value, + int *len) +{ + DBusDictEntry *entry; + + entry = _dbus_hash_table_lookup_string (dict->table, key); + + if (!entry || entry->type != DBUS_TYPE_BYTE_ARRAY) + return FALSE; + + *value = entry->v.byte_array.value; + *len = entry->v.byte_array.len; + + return TRUE; +} + /** * Gets a string array from a dict using a key. * -- cgit