dbus-signature.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-signature.c Routines for reading recursive type signatures
*
* Copyright (C) 2005 Red Hat, Inc.
*
* 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-signature.h"
#include "dbus-marshal-recursive.h"
#include "dbus-marshal-basic.h"
#include "dbus-internals.h"
#include "dbus-test.h"
#include <dbus/dbus-test-tap.h>
typedef struct
{
const char *pos;
unsigned int finished : 1;
unsigned int in_array : 1;
} DBusSignatureRealIter;
_DBUS_STATIC_ASSERT (sizeof (DBusSignatureIter) >= sizeof (DBusSignatureRealIter));
#define TYPE_IS_CONTAINER(typecode) \
((typecode) == DBUS_TYPE_STRUCT || \
(typecode) == DBUS_TYPE_DICT_ENTRY || \
(typecode) == DBUS_TYPE_VARIANT || \
(typecode) == DBUS_TYPE_ARRAY)
void
dbus_signature_iter_init (DBusSignatureIter *iter,
const char *signature)
{
DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
real_iter->pos = signature;
real_iter->finished = FALSE;
real_iter->in_array = FALSE;
}
int
dbus_signature_iter_get_current_type (const DBusSignatureIter *iter)
{
DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
return _dbus_first_type_in_signature_c_str (real_iter->pos, 0);
}
char *
dbus_signature_iter_get_signature (const DBusSignatureIter *iter)
{
DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
DBusString str;
char *ret;
int pos;
if (!_dbus_string_init (&str))
return NULL;
pos = 0;
_dbus_type_signature_next (real_iter->pos, &pos);
if (!_dbus_string_append_len (&str, real_iter->pos, pos))
return NULL;
if (!_dbus_string_steal_data (&str, &ret))
ret = NULL;
_dbus_string_free (&str);
return ret;
}
int
dbus_signature_iter_get_element_type (const DBusSignatureIter *iter)
{
DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
_dbus_return_val_if_fail (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
return _dbus_first_type_in_signature_c_str (real_iter->pos, 1);
}
dbus_bool_t
dbus_signature_iter_next (DBusSignatureIter *iter)
{
DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
if (real_iter->finished)
return FALSE;
else
{
int pos;
if (real_iter->in_array)
{
real_iter->finished = TRUE;
return FALSE;
}
pos = 0;
_dbus_type_signature_next (real_iter->pos, &pos);
real_iter->pos += pos;
if (*real_iter->pos == DBUS_STRUCT_END_CHAR
|| *real_iter->pos == DBUS_DICT_ENTRY_END_CHAR)
{
real_iter->finished = TRUE;
return FALSE;
}
return *real_iter->pos != DBUS_TYPE_INVALID;
}
}
void
dbus_signature_iter_recurse (const DBusSignatureIter *iter,
DBusSignatureIter *subiter)
{
DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
DBusSignatureRealIter *real_sub_iter = (DBusSignatureRealIter *) subiter;
_dbus_return_if_fail (dbus_type_is_container (dbus_signature_iter_get_current_type (iter)));
*real_sub_iter = *real_iter;
real_sub_iter->in_array = FALSE;
real_sub_iter->pos++;
if (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY)
real_sub_iter->in_array = TRUE;
}
dbus_bool_t
dbus_signature_validate (const char *signature,
DBusError *error)
{
DBusString str;
DBusValidity reason;
_dbus_string_init_const (&str, signature);
reason = _dbus_validate_signature_with_reason (&str, 0, _dbus_string_get_length (&str));
if (reason == DBUS_VALID)
return TRUE;
else
{
dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "%s",
_dbus_validity_to_error_message (reason));
return FALSE;
}
}
dbus_bool_t
dbus_signature_validate_single (const char *signature,
DBusError *error)
{
DBusSignatureIter iter;
if (!dbus_signature_validate (signature, error))
return FALSE;
dbus_signature_iter_init (&iter, signature);
if (dbus_signature_iter_get_current_type (&iter) == DBUS_TYPE_INVALID)
goto lose;
if (!dbus_signature_iter_next (&iter))
return TRUE;
lose:
dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "Exactly one complete type required in signature");
return FALSE;
}
dbus_bool_t
dbus_type_is_container (int typecode)
{
/* only reasonable (non-line-noise) typecodes are allowed */
_dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
FALSE);
return TYPE_IS_CONTAINER (typecode);
}
dbus_bool_t
dbus_type_is_basic (int typecode)
{
/* only reasonable (non-line-noise) typecodes are allowed */
_dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
FALSE);
/* everything that isn't invalid or a container */
return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
}
dbus_bool_t
dbus_type_is_fixed (int typecode)
{
/* only reasonable (non-line-noise) typecodes are allowed */
_dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
FALSE);
switch (typecode)
{
case DBUS_TYPE_BYTE:
case DBUS_TYPE_BOOLEAN:
case DBUS_TYPE_INT16:
case DBUS_TYPE_UINT16:
case DBUS_TYPE_INT32:
case DBUS_TYPE_UINT32:
case DBUS_TYPE_INT64:
case DBUS_TYPE_UINT64:
case DBUS_TYPE_DOUBLE:
case DBUS_TYPE_UNIX_FD:
return TRUE;
default:
return FALSE;
}
}
dbus_bool_t
dbus_type_is_valid (int typecode)
{
switch (typecode)
{
case DBUS_TYPE_BYTE:
case DBUS_TYPE_BOOLEAN:
case DBUS_TYPE_INT16:
case DBUS_TYPE_UINT16:
case DBUS_TYPE_INT32:
case DBUS_TYPE_UINT32:
case DBUS_TYPE_INT64:
case DBUS_TYPE_UINT64:
case DBUS_TYPE_DOUBLE:
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
case DBUS_TYPE_ARRAY:
case DBUS_TYPE_STRUCT:
case DBUS_TYPE_DICT_ENTRY:
case DBUS_TYPE_VARIANT:
case DBUS_TYPE_UNIX_FD:
return TRUE;
default:
return FALSE;
}
}
/* end of DBusSignature group */
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition dbus-errors.c:356
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
DBusValidity
DBusValidity
This is primarily used in unit testing, so we can verify that each invalid message is invalid for the...
Definition dbus-marshal-validate.h:54
_dbus_validate_signature_with_reason
DBusValidity _dbus_validate_signature_with_reason(const DBusString *type_str, int type_pos, int len)
Verifies that the range of type_str from type_pos to type_end is a valid signature.
Definition dbus-marshal-validate.c:53
_dbus_first_type_in_signature_c_str
int _dbus_first_type_in_signature_c_str(const char *str, int pos)
Similar to _dbus_first_type_in_signature, but operates on a C string buffer.
Definition dbus-marshal-basic.c:1499
_dbus_type_signature_next
void _dbus_type_signature_next(const char *type_str, int *type_pos)
Skips to the next "complete" type inside a type signature.
Definition dbus-marshal-recursive.c:340
DBUS_VALID
@ DBUS_VALID
the data is valid
Definition dbus-marshal-validate.h:60
DBUS_TYPE_SIGNATURE
#define DBUS_TYPE_SIGNATURE
Type code marking a D-Bus type signature.
Definition dbus-protocol.h:112
DBUS_DICT_ENTRY_END_CHAR
#define DBUS_DICT_ENTRY_END_CHAR
Code marking the end of a dict entry type in a type signature.
Definition dbus-protocol.h:170
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_INT16
#define DBUS_TYPE_INT16
Type code marking a 16-bit signed integer.
Definition dbus-protocol.h:76
DBUS_TYPE_VARIANT
#define DBUS_TYPE_VARIANT
Type code marking a D-Bus variant type.
Definition dbus-protocol.h:126
DBUS_TYPE_INT32
#define DBUS_TYPE_INT32
Type code marking a 32-bit signed integer.
Definition dbus-protocol.h:84
DBUS_TYPE_UNIX_FD
#define DBUS_TYPE_UNIX_FD
Type code marking a unix file descriptor.
Definition dbus-protocol.h:116
DBUS_TYPE_BOOLEAN
#define DBUS_TYPE_BOOLEAN
Type code marking a boolean.
Definition dbus-protocol.h:72
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_ARRAY
#define DBUS_TYPE_ARRAY
Type code marking a D-Bus array type.
Definition dbus-protocol.h:122
DBUS_TYPE_INVALID
#define DBUS_TYPE_INVALID
Type code that is never equal to a legitimate type code.
Definition dbus-protocol.h:62
DBUS_TYPE_INT64
#define DBUS_TYPE_INT64
Type code marking a 64-bit signed integer.
Definition dbus-protocol.h:92
DBUS_TYPE_DOUBLE
#define DBUS_TYPE_DOUBLE
Type code marking an 8-byte double in IEEE 754 format.
Definition dbus-protocol.h:100
DBUS_ERROR_INVALID_SIGNATURE
#define DBUS_ERROR_INVALID_SIGNATURE
A type signature is not valid.
Definition dbus-protocol.h:448
DBUS_TYPE_UINT64
#define DBUS_TYPE_UINT64
Type code marking a 64-bit unsigned integer.
Definition dbus-protocol.h:96
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_UINT16
#define DBUS_TYPE_UINT16
Type code marking a 16-bit unsigned integer.
Definition dbus-protocol.h:80
DBUS_TYPE_STRUCT
#define DBUS_TYPE_STRUCT
STRUCT and DICT_ENTRY are sort of special since their codes can't appear in a type string,...
Definition dbus-protocol.h:138
DBUS_STRUCT_END_CHAR
#define DBUS_STRUCT_END_CHAR
Code marking the end of a struct type in a type signature.
Definition dbus-protocol.h:162
DBUS_TYPE_UINT32
#define DBUS_TYPE_UINT32
Type code marking a 32-bit unsigned integer.
Definition dbus-protocol.h:88
dbus_signature_iter_recurse
void dbus_signature_iter_recurse(const DBusSignatureIter *iter, DBusSignatureIter *subiter)
Initialize a new iterator pointing to the first type in the current container.
Definition dbus-signature.c:212
dbus_signature_validate
dbus_bool_t dbus_signature_validate(const char *signature, DBusError *error)
Check a type signature for validity.
Definition dbus-signature.c:238
dbus_type_is_basic
dbus_bool_t dbus_type_is_basic(int typecode)
A "basic type" is a somewhat arbitrary concept, but the intent is to include those types that are ful...
Definition dbus-signature.c:324
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_type_is_valid
dbus_bool_t dbus_type_is_valid(int typecode)
Return TRUE if the argument is a valid typecode.
Definition dbus-signature.c:389
dbus_signature_iter_get_signature
char * dbus_signature_iter_get_signature(const DBusSignatureIter *iter)
Returns the signature of the single complete type starting at the given iterator.
Definition dbus-signature.c:117
dbus_signature_iter_next
dbus_bool_t dbus_signature_iter_next(DBusSignatureIter *iter)
Skip to the next value on this "level".
Definition dbus-signature.c:169
dbus_type_is_container
dbus_bool_t dbus_type_is_container(int typecode)
A "container type" can contain basic types, or nested container types.
Definition dbus-signature.c:300
dbus_signature_iter_init
void dbus_signature_iter_init(DBusSignatureIter *iter, const char *signature)
Initializes a DBusSignatureIter for reading a type signature.
Definition dbus-signature.c:72
dbus_signature_validate_single
dbus_bool_t dbus_signature_validate_single(const char *signature, DBusError *error)
Check that a type signature is both valid and contains exactly one complete type.
Definition dbus-signature.c:270
dbus_signature_iter_get_current_type
int dbus_signature_iter_get_current_type(const DBusSignatureIter *iter)
Returns the current type pointed to by the iterator.
Definition dbus-signature.c:97
dbus_signature_iter_get_element_type
int dbus_signature_iter_get_element_type(const DBusSignatureIter *iter)
Convenience function for returning the element type of an array; This function allows you to avoid in...
Definition dbus-signature.c:151
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition dbus-string.c:182
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition dbus-string.c:197
_dbus_string_steal_data
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition dbus-string.c:686
_dbus_string_append_len
dbus_bool_t _dbus_string_append_len(DBusString *str, const char *buffer, int len)
Appends block of bytes with the given length to a DBusString.
Definition dbus-string.c:1170
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
Definition dbus-string.c:278
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusError
Object representing an exception.
Definition dbus-errors.h:51
DBusSignatureIter
DBusSignatureIter struct; contains no public fields.
Definition dbus-signature.h:47
DBusSignatureRealIter
Implementation details of DBusSignatureIter, all fields are private.
Definition dbus-signature.c:39
DBusSignatureRealIter::finished
unsigned int finished
true if we are at the end iter
Definition dbus-signature.c:41
DBusSignatureRealIter::in_array
unsigned int in_array
true if we are a subiterator pointing to an array's element type
Definition dbus-signature.c:42
DBusSignatureRealIter::pos
const char * pos
current position in the signature string
Definition dbus-signature.c:40
DBusString
Definition dbus-string.h:47