D-Bus  1.13.7
dbus-signature.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-signature.c Routines for reading recursive type signatures
3  *
4  * Copyright (C) 2005 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 
26 #include "dbus-signature.h"
27 #include "dbus-marshal-recursive.h"
28 #include "dbus-marshal-basic.h"
29 #include "dbus-internals.h"
30 #include "dbus-test.h"
31 #include <dbus/dbus-test-tap.h>
32 
36 typedef struct
37 {
38  const char *pos;
39  unsigned int finished : 1;
40  unsigned int in_array : 1;
42 
44 #define TYPE_IS_CONTAINER(typecode) \
45  ((typecode) == DBUS_TYPE_STRUCT || \
46  (typecode) == DBUS_TYPE_DICT_ENTRY || \
47  (typecode) == DBUS_TYPE_VARIANT || \
48  (typecode) == DBUS_TYPE_ARRAY)
49 
50 
67 void
69  const char *signature)
70 {
71  DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
72 
73  real_iter->pos = signature;
74  real_iter->finished = FALSE;
75  real_iter->in_array = FALSE;
76 }
77 
92 int
94 {
95  DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
96 
97  return _dbus_first_type_in_signature_c_str (real_iter->pos, 0);
98 }
99 
112 char *
114 {
115  DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
116  DBusString str;
117  char *ret;
118  int pos;
119 
120  if (!_dbus_string_init (&str))
121  return NULL;
122 
123  pos = 0;
124  _dbus_type_signature_next (real_iter->pos, &pos);
125 
126  if (!_dbus_string_append_len (&str, real_iter->pos, pos))
127  return NULL;
128  if (!_dbus_string_steal_data (&str, &ret))
129  ret = NULL;
130  _dbus_string_free (&str);
131 
132  return ret;
133 }
134 
146 int
148 {
149  DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
150 
151  _dbus_return_val_if_fail (dbus_signature_iter_get_current_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
152 
153  return _dbus_first_type_in_signature_c_str (real_iter->pos, 1);
154 }
155 
166 {
167  DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
168 
169  if (real_iter->finished)
170  return FALSE;
171  else
172  {
173  int pos;
174 
175  if (real_iter->in_array)
176  {
177  real_iter->finished = TRUE;
178  return FALSE;
179  }
180 
181  pos = 0;
182  _dbus_type_signature_next (real_iter->pos, &pos);
183  real_iter->pos += pos;
184 
185  if (*real_iter->pos == DBUS_STRUCT_END_CHAR
186  || *real_iter->pos == DBUS_DICT_ENTRY_END_CHAR)
187  {
188  real_iter->finished = TRUE;
189  return FALSE;
190  }
191 
192  return *real_iter->pos != DBUS_TYPE_INVALID;
193  }
194 }
195 
207 void
209  DBusSignatureIter *subiter)
210 {
211  DBusSignatureRealIter *real_iter = (DBusSignatureRealIter *) iter;
212  DBusSignatureRealIter *real_sub_iter = (DBusSignatureRealIter *) subiter;
213 
214  _dbus_return_if_fail (dbus_type_is_container (dbus_signature_iter_get_current_type (iter)));
215 
216  *real_sub_iter = *real_iter;
217  real_sub_iter->in_array = FALSE;
218  real_sub_iter->pos++;
219 
221  real_sub_iter->in_array = TRUE;
222 }
223 
234 dbus_signature_validate (const char *signature,
235  DBusError *error)
236 
237 {
238  DBusString str;
239  DBusValidity reason;
240 
241  _dbus_string_init_const (&str, signature);
242  reason = _dbus_validate_signature_with_reason (&str, 0, _dbus_string_get_length (&str));
243 
244  if (reason == DBUS_VALID)
245  return TRUE;
246  else
247  {
249  _dbus_validity_to_error_message (reason));
250  return FALSE;
251  }
252 }
253 
266 dbus_signature_validate_single (const char *signature,
267  DBusError *error)
268 {
269  DBusSignatureIter iter;
270 
271  if (!dbus_signature_validate (signature, error))
272  return FALSE;
273 
274  dbus_signature_iter_init (&iter, signature);
276  goto lose;
277  if (!dbus_signature_iter_next (&iter))
278  return TRUE;
279  lose:
280  dbus_set_error (error, DBUS_ERROR_INVALID_SIGNATURE, "Exactly one complete type required in signature");
281  return FALSE;
282 }
283 
297 {
298  /* only reasonable (non-line-noise) typecodes are allowed */
299  _dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
300  FALSE);
301  return TYPE_IS_CONTAINER (typecode);
302 }
303 
320 dbus_type_is_basic (int typecode)
321 {
322  /* only reasonable (non-line-noise) typecodes are allowed */
323  _dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
324  FALSE);
325 
326  /* everything that isn't invalid or a container */
327  return !(typecode == DBUS_TYPE_INVALID || TYPE_IS_CONTAINER (typecode));
328 }
329 
351 dbus_type_is_fixed (int typecode)
352 {
353  /* only reasonable (non-line-noise) typecodes are allowed */
354  _dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID,
355  FALSE);
356 
357  switch (typecode)
358  {
359  case DBUS_TYPE_BYTE:
360  case DBUS_TYPE_BOOLEAN:
361  case DBUS_TYPE_INT16:
362  case DBUS_TYPE_UINT16:
363  case DBUS_TYPE_INT32:
364  case DBUS_TYPE_UINT32:
365  case DBUS_TYPE_INT64:
366  case DBUS_TYPE_UINT64:
367  case DBUS_TYPE_DOUBLE:
368  case DBUS_TYPE_UNIX_FD:
369  return TRUE;
370  default:
371  return FALSE;
372  }
373 }
374 
385 dbus_type_is_valid (int typecode)
386 {
387  switch (typecode)
388  {
389  case DBUS_TYPE_BYTE:
390  case DBUS_TYPE_BOOLEAN:
391  case DBUS_TYPE_INT16:
392  case DBUS_TYPE_UINT16:
393  case DBUS_TYPE_INT32:
394  case DBUS_TYPE_UINT32:
395  case DBUS_TYPE_INT64:
396  case DBUS_TYPE_UINT64:
397  case DBUS_TYPE_DOUBLE:
398  case DBUS_TYPE_STRING:
400  case DBUS_TYPE_SIGNATURE:
401  case DBUS_TYPE_ARRAY:
402  case DBUS_TYPE_STRUCT:
404  case DBUS_TYPE_VARIANT:
405  case DBUS_TYPE_UNIX_FD:
406  return TRUE;
407 
408  default:
409  return FALSE;
410  }
411 }
412  /* end of DBusSignature group */
414 
415 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
416 
424 _dbus_signature_test (void)
425 {
426  DBusSignatureIter iter;
427  DBusSignatureIter subiter;
428  DBusSignatureIter subsubiter;
429  DBusSignatureIter subsubsubiter;
430  const char *sig;
431  dbus_bool_t boolres;
432 
433  _DBUS_STATIC_ASSERT (sizeof (DBusSignatureIter) >= sizeof (DBusSignatureRealIter));
434 
435  sig = "";
438  dbus_signature_iter_init (&iter, sig);
440 
444  dbus_signature_iter_init (&iter, sig);
446 
449  dbus_signature_iter_init (&iter, sig);
451  boolres = dbus_signature_iter_next (&iter);
452  _dbus_assert (boolres);
454 
463  dbus_signature_iter_init (&iter, sig);
465  boolres = dbus_signature_iter_next (&iter);
466  _dbus_assert (boolres);
468  dbus_signature_iter_recurse (&iter, &subiter);
470  boolres = dbus_signature_iter_next (&subiter);
471  _dbus_assert (boolres);
473  boolres = dbus_signature_iter_next (&subiter);
474  _dbus_assert (boolres);
476  boolres = dbus_signature_iter_next (&subiter);
477  _dbus_assert (boolres);
479 
483  DBUS_TYPE_BYTE_AS_STRING
488  DBUS_TYPE_BYTE_AS_STRING
489  DBUS_STRUCT_END_CHAR_AS_STRING
492  dbus_signature_iter_init (&iter, sig);
494  boolres = dbus_signature_iter_next (&iter);
495  _dbus_assert (boolres);
497  dbus_signature_iter_recurse (&iter, &subiter);
499  boolres = dbus_signature_iter_next (&subiter);
500  _dbus_assert (boolres);
502  boolres = dbus_signature_iter_next (&subiter);
503  _dbus_assert (boolres);
506 
507  dbus_signature_iter_recurse (&subiter, &subsubiter);
510 
511  dbus_signature_iter_recurse (&subsubiter, &subsubsubiter);
513  boolres = dbus_signature_iter_next (&subiter);
514  _dbus_assert (boolres);
516  dbus_signature_iter_recurse (&subiter, &subsubiter);
518 
527  dbus_signature_iter_init (&iter, sig);
530 
531  dbus_signature_iter_recurse (&iter, &subiter);
532  dbus_signature_iter_recurse (&subiter, &subsubiter);
534  boolres = dbus_signature_iter_next (&subsubiter);
535  _dbus_assert (boolres);
537  boolres = dbus_signature_iter_next (&subsubiter);
538  _dbus_assert (!boolres);
539 
540  boolres = dbus_signature_iter_next (&iter);
541  _dbus_assert (boolres);
543  boolres = dbus_signature_iter_next (&iter);
544  _dbus_assert (!boolres);
545 
548 
551 
555 
556  sig = DBUS_TYPE_ARRAY_AS_STRING
559 
562 
565 
569 
571  DBUS_TYPE_INT32_AS_STRING
574 
575  sig = DBUS_STRUCT_END_CHAR_AS_STRING
578 
579  sig = DBUS_STRUCT_BEGIN_CHAR_AS_STRING
582  return TRUE;
583 #if 0
584  oom:
585  _dbus_test_fatal ("out of memory");
586  return FALSE;
587 #endif
588 }
589 
590 #endif
591 
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...
#define DBUS_TYPE_UINT16
Type code marking a 16-bit unsigned integer.
Definition: dbus-protocol.h:78
#define NULL
A null pointer, defined appropriately for C or C++.
#define DBUS_STRUCT_BEGIN_CHAR_AS_STRING
DBUS_STRUCT_BEGIN_CHAR as a string literal instead of a int literal
#define DBUS_TYPE_STRUCT
STRUCT and DICT_ENTRY are sort of special since their codes can&#39;t appear in a type string...
#define DBUS_TYPE_DICT_ENTRY
Type code used to represent a dict entry; however, this type code does not appear in type signatures...
#define DBUS_TYPE_STRING
Type code marking a UTF-8 encoded, nul-terminated Unicode string.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void _dbus_type_signature_next(const char *type_str, int *type_pos)
Skips to the next "complete" type inside a type signature.
#define DBUS_TYPE_ARRAY_AS_STRING
DBUS_TYPE_ARRAY as a string literal instead of a int literal
#define DBUS_TYPE_BYTE
Type code marking an 8-bit unsigned integer.
Definition: dbus-protocol.h:66
DBusSignatureIter struct; contains no public fields.
dbus_bool_t dbus_signature_iter_next(DBusSignatureIter *iter)
Skip to the next value on this "level".
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
#define DBUS_DICT_ENTRY_END_CHAR_AS_STRING
DBUS_DICT_ENTRY_END_CHAR as a string literal instead of a int literal
DBusValidity
This is primarily used in unit testing, so we can verify that each invalid message is invalid for the...
#define DBUS_STRUCT_END_CHAR
Code marking the end of a struct type in a type signature.
#define DBUS_TYPE_DOUBLE
Type code marking an 8-byte double in IEEE 754 format.
Definition: dbus-protocol.h:98
#define DBUS_TYPE_ARRAY
Type code marking a D-Bus array type.
#define DBUS_TYPE_INT64
Type code marking a 64-bit signed integer.
Definition: dbus-protocol.h:90
Implementation details of DBusSignatureIter, all fields are private.
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.
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...
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
void dbus_signature_iter_init(DBusSignatureIter *iter, const char *signature)
Initializes a DBusSignatureIter for reading a type signature.
#define DBUS_TYPE_INT32_AS_STRING
DBUS_TYPE_INT32 as a string literal instead of a int literal
Definition: dbus-protocol.h:84
#define DBUS_TYPE_VARIANT_AS_STRING
DBUS_TYPE_VARIANT as a string literal instead of a int literal
void dbus_signature_iter_recurse(const DBusSignatureIter *iter, DBusSignatureIter *subiter)
Initialize a new iterator pointing to the first type in the current container.
#define DBUS_TYPE_UINT16_AS_STRING
DBUS_TYPE_UINT16 as a string literal instead of a int literal
Definition: dbus-protocol.h:80
#define DBUS_TYPE_INT32
Type code marking a 32-bit signed integer.
Definition: dbus-protocol.h:82
unsigned int finished
true if we are at the end iter
#define DBUS_TYPE_DOUBLE_AS_STRING
DBUS_TYPE_DOUBLE as a string literal instead of a int literal
Object representing an exception.
Definition: dbus-errors.h:48
#define DBUS_TYPE_BYTE_AS_STRING
DBUS_TYPE_BYTE as a string literal instead of a int literal
Definition: dbus-protocol.h:68
#define DBUS_TYPE_SIGNATURE
Type code marking a D-Bus type signature.
#define DBUS_TYPE_UINT64
Type code marking a 64-bit unsigned integer.
Definition: dbus-protocol.h:94
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:354
#define DBUS_TYPE_VARIANT
Type code marking a D-Bus variant type.
#define DBUS_TYPE_UINT32
Type code marking a 32-bit unsigned integer.
Definition: dbus-protocol.h:86
the data is valid
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:264
#define TRUE
Expands to "1".
#define DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
DBUS_DICT_ENTRY_BEGIN_CHAR as a string literal instead of a int literal
#define DBUS_TYPE_OBJECT_PATH
Type code marking a D-Bus object path.
#define DBUS_TYPE_UNIX_FD
Type code marking a unix file descriptor.
#define DBUS_TYPE_INT16_AS_STRING
DBUS_TYPE_INT16 as a string literal instead of a int literal
Definition: dbus-protocol.h:76
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...
#define DBUS_TYPE_INVALID
Type code that is never equal to a legitimate type code.
Definition: dbus-protocol.h:60
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.
#define DBUS_ERROR_INVALID_SIGNATURE
A type signature is not valid.
#define DBUS_TYPE_INT16
Type code marking a 16-bit signed integer.
Definition: dbus-protocol.h:74
#define DBUS_TYPE_BOOLEAN
Type code marking a boolean.
Definition: dbus-protocol.h:70
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:1154
#define DBUS_STRUCT_END_CHAR_AS_STRING
DBUS_STRUCT_END_CHAR a string literal instead of a int literal
dbus_bool_t dbus_signature_validate(const char *signature, DBusError *error)
Check a type signature for validity.
#define DBUS_TYPE_STRING_AS_STRING
DBUS_TYPE_STRING as a string literal instead of a int literal
#define FALSE
Expands to "0".
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. ...
#define DBUS_DICT_ENTRY_END_CHAR
Code marking the end of a dict entry type in a type signature.
#define DBUS_TYPE_BOOLEAN_AS_STRING
DBUS_TYPE_BOOLEAN as a string literal instead of a int literal
Definition: dbus-protocol.h:72
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:658
int dbus_signature_iter_get_current_type(const DBusSignatureIter *iter)
Returns the current type pointed to by the iterator.
const char * pos
current position in the signature string
unsigned int in_array
true if we are a subiterator pointing to an array&#39;s element type
#define DBUS_TYPE_DICT_ENTRY_AS_STRING
DBUS_TYPE_DICT_ENTRY as a string literal instead of a int literal
#define DBUS_TYPE_UINT32_AS_STRING
DBUS_TYPE_UINT32 as a string literal instead of a int literal
Definition: dbus-protocol.h:88
dbus_bool_t dbus_type_is_valid(int typecode)
Return TRUE if the argument is a valid typecode.
char * dbus_signature_iter_get_signature(const DBusSignatureIter *iter)
Returns the signature of the single complete type starting at the given iterator. ...
dbus_bool_t dbus_type_is_container(int typecode)
A "container type" can contain basic types, or nested container types.