dbus-asv-util.c
/* dbus-asv-util.c - utility functions for a{sv}
*
* Copyright © 2011-2012 Nokia Corporation
* Copyright © 2012-2013 Collabora Ltd.
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <config.h>
#include <dbus/dbus.h>
#include "dbus/dbus-asv-util.h"
DBusMessage *
_dbus_asv_new_method_return (DBusMessage *message,
DBusMessageIter *iter,
DBusMessageIter *arr_iter)
{
DBusMessage *reply = dbus_message_new_method_return (message);
if (reply == NULL)
return NULL;
dbus_message_iter_init_append (reply, iter);
if (!dbus_message_iter_open_container (iter, DBUS_TYPE_ARRAY, "{sv}",
arr_iter))
{
dbus_message_unref (reply);
return NULL;
}
return reply;
}
/*
* Open a new entry in an a{sv} (map from string to variant).
*
* This must be paired with a call to either _dbus_asv_close_entry()
* or _dbus_asv_abandon_entry().
*
* If this function fails, the a{sv} must be abandoned, for instance
* with _dbus_asv_abandon().
*
* @param arr_iter the iterator which is appending to the array
* @param entry_iter will be initialized to append to the dict-entry
* @param key a UTF-8 key for the map
* @param type the type of the variant value, e.g. DBUS_TYPE_STRING_AS_STRING
* @param var_iter will be initialized to append (i.e. write) to the variant
* @returns #TRUE on success, or #FALSE if not enough memory
*/
dbus_bool_t
_dbus_asv_open_entry (DBusMessageIter *arr_iter,
DBusMessageIter *entry_iter,
const char *key,
const char *type,
DBusMessageIter *var_iter)
{
if (!dbus_message_iter_open_container (arr_iter, DBUS_TYPE_DICT_ENTRY,
NULL, entry_iter))
return FALSE;
if (!dbus_message_iter_append_basic (entry_iter, DBUS_TYPE_STRING, &key))
{
dbus_message_iter_abandon_container (arr_iter, entry_iter);
return FALSE;
}
if (!dbus_message_iter_open_container (entry_iter, DBUS_TYPE_VARIANT,
type, var_iter))
{
dbus_message_iter_abandon_container (arr_iter, entry_iter);
return FALSE;
}
return TRUE;
}
/*
* Closes an a{sv} entry after successfully appending the value.
*
* If this function fails, the a{sv} must be abandoned, for instance
* with _dbus_asv_abandon().
*
* @param arr_iter the iterator which is appending to the array
* @param entry_iter the iterator appending to the dict-entry, will be closed
* @param var_iter the iterator appending to the variant, will be closed
* @returns #TRUE on success, or #FALSE if not enough memory
*/
dbus_bool_t
_dbus_asv_close_entry (DBusMessageIter *arr_iter,
DBusMessageIter *entry_iter,
DBusMessageIter *var_iter)
{
if (!dbus_message_iter_close_container (entry_iter, var_iter))
{
dbus_message_iter_abandon_container (arr_iter, entry_iter);
return FALSE;
}
if (!dbus_message_iter_close_container (arr_iter, entry_iter))
return FALSE;
return TRUE;
}
dbus_bool_t
_dbus_asv_close (DBusMessageIter *iter,
DBusMessageIter *arr_iter)
{
return dbus_message_iter_close_container (iter, arr_iter);
}
/*
* Closes an a{sv} entry after unsuccessfully appending a value.
* You must also abandon the a{sv} itself (for instance with
* _dbus_asv_abandon()), and abandon whatever larger data structure
* the a{sv} was embedded in.
*
* @param iter the iterator which is appending to the message or other data structure containing the a{sv}
* @param arr_iter the iterator appending to the array, will be closed
* @returns #TRUE on success, or #FALSE if not enough memory
*/
void
_dbus_asv_abandon_entry (DBusMessageIter *arr_iter,
DBusMessageIter *entry_iter,
DBusMessageIter *var_iter)
{
dbus_message_iter_abandon_container (entry_iter, var_iter);
dbus_message_iter_abandon_container (arr_iter, entry_iter);
}
void
_dbus_asv_abandon (DBusMessageIter *iter,
DBusMessageIter *arr_iter)
{
dbus_message_iter_abandon_container (iter, arr_iter);
}
dbus_bool_t
_dbus_asv_add_uint32 (DBusMessageIter *arr_iter,
const char *key,
dbus_uint32_t value)
{
DBusMessageIter entry_iter, var_iter;
if (!_dbus_asv_open_entry (arr_iter, &entry_iter, key,
DBUS_TYPE_UINT32_AS_STRING, &var_iter))
return FALSE;
if (!dbus_message_iter_append_basic (&var_iter, DBUS_TYPE_UINT32,
&value))
{
_dbus_asv_abandon_entry (arr_iter, &entry_iter, &var_iter);
return FALSE;
}
if (!_dbus_asv_close_entry (arr_iter, &entry_iter, &var_iter))
return FALSE;
return TRUE;
}
dbus_bool_t
_dbus_asv_add_string (DBusMessageIter *arr_iter,
const char *key,
const char *value)
{
DBusMessageIter entry_iter, var_iter;
if (!_dbus_asv_open_entry (arr_iter, &entry_iter, key,
DBUS_TYPE_STRING_AS_STRING, &var_iter))
return FALSE;
if (!dbus_message_iter_append_basic (&var_iter, DBUS_TYPE_STRING,
&value))
{
_dbus_asv_abandon_entry (arr_iter, &entry_iter, &var_iter);
return FALSE;
}
if (!_dbus_asv_close_entry (arr_iter, &entry_iter, &var_iter))
return FALSE;
return TRUE;
}
dbus_bool_t
_dbus_asv_add_object_path (DBusMessageIter *arr_iter,
const char *key,
const char *value)
{
DBusMessageIter entry_iter, var_iter;
if (!_dbus_asv_open_entry (arr_iter, &entry_iter, key,
DBUS_TYPE_OBJECT_PATH_AS_STRING, &var_iter))
return FALSE;
if (!dbus_message_iter_append_basic (&var_iter, DBUS_TYPE_OBJECT_PATH,
&value))
{
_dbus_asv_abandon_entry (arr_iter, &entry_iter, &var_iter);
return FALSE;
}
if (!_dbus_asv_close_entry (arr_iter, &entry_iter, &var_iter))
return FALSE;
return TRUE;
}
dbus_bool_t
_dbus_asv_add_fixed_array (DBusMessageIter *arr_iter,
const char *key,
char element_type,
const void *value,
int n_elements)
{
const char type[] = { DBUS_TYPE_ARRAY, element_type, 0 };
DBusMessageIter entry_iter;
DBusMessageIter var_iter;
DBusMessageIter array_iter;
_dbus_assert (dbus_type_is_fixed (element_type) && element_type != DBUS_TYPE_UNIX_FD);
if (!_dbus_asv_open_entry (arr_iter, &entry_iter, key, type, &var_iter))
return FALSE;
if (!dbus_message_iter_open_container (&var_iter, DBUS_TYPE_ARRAY, type + 1,
&array_iter))
{
_dbus_asv_abandon_entry (arr_iter, &entry_iter, &var_iter);
return FALSE;
}
if (!dbus_message_iter_append_fixed_array (&array_iter, element_type,
&value, n_elements))
{
dbus_message_iter_abandon_container (&var_iter, &array_iter);
_dbus_asv_abandon_entry (arr_iter, &entry_iter, &var_iter);
return FALSE;
}
if (!dbus_message_iter_close_container (&var_iter, &array_iter))
{
_dbus_asv_abandon_entry (arr_iter, &entry_iter, &var_iter);
return FALSE;
}
if (!_dbus_asv_close_entry (arr_iter, &entry_iter, &var_iter))
return FALSE;
return TRUE;
}
dbus_bool_t
_dbus_asv_add_byte_array (DBusMessageIter *arr_iter,
const char *key,
const void *value,
int n_elements)
{
return _dbus_asv_add_fixed_array (arr_iter, key, DBUS_TYPE_BYTE, value,
n_elements);
}
dbus_bool_t
_dbus_asv_add_unix_fd (DBusMessageIter *arr_iter,
const char *key,
int value)
{
DBusMessageIter entry_iter, var_iter;
if (!_dbus_asv_open_entry (arr_iter, &entry_iter, key,
DBUS_TYPE_UNIX_FD_AS_STRING, &var_iter))
return FALSE;
if (!dbus_message_iter_append_basic (&var_iter, DBUS_TYPE_UNIX_FD,
&value))
{
_dbus_asv_abandon_entry (arr_iter, &entry_iter, &var_iter);
return FALSE;
}
if (!_dbus_asv_close_entry (arr_iter, &entry_iter, &var_iter))
return FALSE;
return TRUE;
}
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
dbus_message_iter_append_basic
dbus_bool_t dbus_message_iter_append_basic(DBusMessageIter *iter, int type, const void *value)
Appends a basic-typed value to the message.
Definition dbus-message.c:2771
dbus_message_iter_append_fixed_array
dbus_bool_t dbus_message_iter_append_fixed_array(DBusMessageIter *iter, int element_type, const void *value, int n_elements)
Appends a block of fixed-length values to an array.
Definition dbus-message.c:2922
dbus_message_iter_abandon_container
void dbus_message_iter_abandon_container(DBusMessageIter *iter, DBusMessageIter *sub)
Abandons creation of a contained-typed value and frees resources created by dbus_message_iter_open_co...
Definition dbus-message.c:3123
dbus_message_iter_open_container
dbus_bool_t dbus_message_iter_open_container(DBusMessageIter *iter, int type, const char *contained_signature, DBusMessageIter *sub)
Appends a container-typed value to the message.
Definition dbus-message.c:2986
dbus_message_new_method_return
DBusMessage * dbus_message_new_method_return(DBusMessage *method_call)
Constructs a message that is a reply to a method call.
Definition dbus-message.c:1418
dbus_message_unref
void dbus_message_unref(DBusMessage *message)
Decrements the reference count of a DBusMessage, freeing the message if the count reaches 0.
Definition dbus-message.c:1735
dbus_message_iter_close_container
dbus_bool_t dbus_message_iter_close_container(DBusMessageIter *iter, DBusMessageIter *sub)
Closes a container-typed value appended to the message; may write out more information to the message...
Definition dbus-message.c:3089
dbus_message_iter_init_append
void dbus_message_iter_init_append(DBusMessage *message, DBusMessageIter *iter)
Initializes a DBusMessageIter for appending arguments to the end of a message.
Definition dbus-message.c:2533
DBUS_TYPE_UNIX_FD_AS_STRING
#define DBUS_TYPE_UNIX_FD_AS_STRING
DBUS_TYPE_UNIX_FD as a string literal instead of a int literal
Definition dbus-protocol.h:118
DBUS_TYPE_OBJECT_PATH
#define DBUS_TYPE_OBJECT_PATH
Type code marking a D-Bus object path.
Definition dbus-protocol.h:108
DBUS_TYPE_BYTE
#define DBUS_TYPE_BYTE
Type code marking an 8-bit unsigned integer.
Definition dbus-protocol.h:68
DBUS_TYPE_VARIANT
#define DBUS_TYPE_VARIANT
Type code marking a D-Bus variant type.
Definition dbus-protocol.h:126
DBUS_TYPE_UNIX_FD
#define DBUS_TYPE_UNIX_FD
Type code marking a unix file descriptor.
Definition dbus-protocol.h:116
DBUS_TYPE_STRING
#define DBUS_TYPE_STRING
Type code marking a UTF-8 encoded, nul-terminated Unicode string.
Definition dbus-protocol.h:104
DBUS_TYPE_OBJECT_PATH_AS_STRING
#define DBUS_TYPE_OBJECT_PATH_AS_STRING
DBUS_TYPE_OBJECT_PATH as a string literal instead of a int literal
Definition dbus-protocol.h:110
DBUS_TYPE_ARRAY
#define DBUS_TYPE_ARRAY
Type code marking a D-Bus array type.
Definition dbus-protocol.h:122
DBUS_TYPE_STRING_AS_STRING
#define DBUS_TYPE_STRING_AS_STRING
DBUS_TYPE_STRING as a string literal instead of a int literal
Definition dbus-protocol.h:106
DBUS_TYPE_DICT_ENTRY
#define DBUS_TYPE_DICT_ENTRY
Type code used to represent a dict entry; however, this type code does not appear in type signatures,...
Definition dbus-protocol.h:145
DBUS_TYPE_UINT32_AS_STRING
#define DBUS_TYPE_UINT32_AS_STRING
DBUS_TYPE_UINT32 as a string literal instead of a int literal
Definition dbus-protocol.h:90
DBUS_TYPE_UINT32
#define DBUS_TYPE_UINT32
Type code marking a 32-bit unsigned integer.
Definition dbus-protocol.h:88
dbus_type_is_fixed
dbus_bool_t dbus_type_is_fixed(int typecode)
Tells you whether values of this type can change length if you set them to some other value.
Definition dbus-signature.c:355
dbus_uint32_t
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
Definition dbus-arch-deps.h:45
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusMessageIter
DBusMessageIter struct; contains no public fields.
Definition dbus-message.h:64
DBusMessage
Internals of DBusMessage.
Definition dbus-message-private.h:102