1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-string.c String utility class (internal to D-BUS implementation)
4 * Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-string.h"
26 /* we allow a system header here, for speed/convenience */
30 #include "dbus-marshal.h"
31 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
32 #include "dbus-string-private.h"
33 #include "dbus-protocol.h"
34 /* for DBUS_VA_COPY */
35 #include "dbus-sysdeps.h"
38 * @defgroup DBusString string class
39 * @ingroup DBusInternals
40 * @brief DBusString data structure
42 * Types and functions related to DBusString. DBusString is intended
43 * to be a string class that makes it hard to mess up security issues
44 * (and just in general harder to write buggy code). It should be
45 * used (or extended and then used) rather than the libc stuff in
46 * string.h. The string class is a bit inconvenient at spots because
47 * it handles out-of-memory failures and tries to be extra-robust.
49 * A DBusString has a maximum length set at initialization time; this
50 * can be used to ensure that a buffer doesn't get too big. The
51 * _dbus_string_lengthen() method checks for overflow, and for max
52 * length being exceeded.
54 * Try to avoid conversion to a plain C string, i.e. add methods on
55 * the string object instead, only convert to C string when passing
56 * things out to the public API. In particular, no sprintf, strcpy,
57 * strcat, any of that should be used. The GString feature of
58 * accepting negative numbers for "length of string" is also absent,
59 * because it could keep us from detecting bogus huge lengths. i.e. if
60 * we passed in some bogus huge length it would be taken to mean
61 * "current length of string" instead of "broken crack"
65 * @defgroup DBusStringInternals DBusString implementation details
66 * @ingroup DBusInternals
67 * @brief DBusString implementation details
69 * The guts of DBusString.
75 * We allocate 1 byte for nul termination, plus 7 bytes for possible
76 * align_offset, so we always need 8 bytes on top of the string's
77 * length to be in the allocated block.
79 #define ALLOCATION_PADDING 8
82 * This is the maximum max length (and thus also the maximum length)
85 #define MAX_MAX_LENGTH (_DBUS_INT_MAX - ALLOCATION_PADDING)
88 * Checks a bunch of assertions about a string object
90 * @param real the DBusRealString
92 #define DBUS_GENERIC_STRING_PREAMBLE(real) _dbus_assert ((real) != NULL); _dbus_assert (!(real)->invalid); _dbus_assert ((real)->len >= 0); _dbus_assert ((real)->allocated >= 0); _dbus_assert ((real)->max_length >= 0); _dbus_assert ((real)->len <= ((real)->allocated - ALLOCATION_PADDING)); _dbus_assert ((real)->len <= (real)->max_length)
95 * Checks assertions about a string object that needs to be
96 * modifiable - may not be locked or const. Also declares
97 * the "real" variable pointing to DBusRealString.
98 * @param str the string
100 #define DBUS_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
101 DBUS_GENERIC_STRING_PREAMBLE (real); \
102 _dbus_assert (!(real)->constant); \
103 _dbus_assert (!(real)->locked)
106 * Checks assertions about a string object that may be locked but
107 * can't be const. i.e. a string object that we can free. Also
108 * declares the "real" variable pointing to DBusRealString.
110 * @param str the string
112 #define DBUS_LOCKED_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
113 DBUS_GENERIC_STRING_PREAMBLE (real); \
114 _dbus_assert (!(real)->constant)
117 * Checks assertions about a string that may be const or locked. Also
118 * declares the "real" variable pointing to DBusRealString.
119 * @param str the string.
121 #define DBUS_CONST_STRING_PREAMBLE(str) const DBusRealString *real = (DBusRealString*) str; \
122 DBUS_GENERIC_STRING_PREAMBLE (real)
127 * @addtogroup DBusString
132 fixup_alignment (DBusRealString *real)
136 unsigned int old_align_offset;
138 /* we have to have extra space in real->allocated for the align offset and nul byte */
139 _dbus_assert (real->len <= real->allocated - ALLOCATION_PADDING);
141 old_align_offset = real->align_offset;
142 real_block = real->str - old_align_offset;
144 aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
146 real->align_offset = aligned - real_block;
149 if (old_align_offset != real->align_offset)
151 /* Here comes the suck */
152 memmove (real_block + real->align_offset,
153 real_block + old_align_offset,
157 _dbus_assert (real->align_offset < 8);
158 _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
162 undo_alignment (DBusRealString *real)
164 if (real->align_offset != 0)
166 memmove (real->str - real->align_offset,
170 real->str = real->str - real->align_offset;
171 real->align_offset = 0;
176 * Initializes a string that can be up to the given allocation size
177 * before it has to realloc. The string starts life with zero length.
178 * The string must eventually be freed with _dbus_string_free().
180 * @param str memory to hold the string
181 * @param allocate_size amount to preallocate
182 * @returns #TRUE on success, #FALSE if no memory
185 _dbus_string_init_preallocated (DBusString *str,
188 DBusRealString *real;
190 _dbus_assert (str != NULL);
192 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
194 real = (DBusRealString*) str;
196 /* It's very important not to touch anything
197 * other than real->str if we're going to fail,
198 * since we also use this function to reset
199 * an existing string, e.g. in _dbus_string_steal_data()
202 real->str = dbus_malloc (ALLOCATION_PADDING + allocate_size);
203 if (real->str == NULL)
206 real->allocated = ALLOCATION_PADDING + allocate_size;
208 real->str[real->len] = '\0';
210 real->max_length = MAX_MAX_LENGTH;
211 real->constant = FALSE;
212 real->locked = FALSE;
213 real->invalid = FALSE;
214 real->align_offset = 0;
216 fixup_alignment (real);
222 * Initializes a string. The string starts life with zero length. The
223 * string must eventually be freed with _dbus_string_free().
225 * @param str memory to hold the string
226 * @returns #TRUE on success, #FALSE if no memory
229 _dbus_string_init (DBusString *str)
231 return _dbus_string_init_preallocated (str, 0);
234 /* The max length thing is sort of a historical artifact
235 * from a feature that turned out to be dumb; perhaps
236 * we should purge it entirely. The problem with
237 * the feature is that it looks like memory allocation
238 * failure, but is not a transient or resolvable failure.
241 set_max_length (DBusString *str,
244 DBusRealString *real;
246 real = (DBusRealString*) str;
248 real->max_length = max_length;
252 * Initializes a constant string. The value parameter is not copied
253 * (should be static), and the string may never be modified.
254 * It is safe but not necessary to call _dbus_string_free()
255 * on a const string. The string has a length limit of MAXINT - 8.
257 * @param str memory to use for the string
258 * @param value a string to be stored in str (not copied!!!)
261 _dbus_string_init_const (DBusString *str,
264 _dbus_assert (value != NULL);
266 _dbus_string_init_const_len (str, value,
271 * Initializes a constant string with a length. The value parameter is
272 * not copied (should be static), and the string may never be
273 * modified. It is safe but not necessary to call _dbus_string_free()
276 * @param str memory to use for the string
277 * @param value a string to be stored in str (not copied!!!)
278 * @param len the length to use
281 _dbus_string_init_const_len (DBusString *str,
285 DBusRealString *real;
287 _dbus_assert (str != NULL);
288 _dbus_assert (value != NULL);
289 _dbus_assert (len <= MAX_MAX_LENGTH);
290 _dbus_assert (len >= 0);
292 real = (DBusRealString*) str;
294 real->str = (char*) value;
296 real->allocated = real->len + ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
297 real->max_length = real->len + 1;
298 real->constant = TRUE;
299 real->invalid = FALSE;
301 /* We don't require const strings to be 8-byte aligned as the
302 * memory is coming from elsewhere.
307 * Frees a string created by _dbus_string_init().
309 * @param str memory where the string is stored.
312 _dbus_string_free (DBusString *str)
314 DBusRealString *real = (DBusRealString*) str;
315 DBUS_GENERIC_STRING_PREAMBLE (real);
319 dbus_free (real->str - real->align_offset);
321 real->invalid = TRUE;
324 #ifdef DBUS_BUILD_TESTS
325 /* Not using this feature at the moment,
326 * so marked DBUS_BUILD_TESTS-only
329 * Locks a string such that any attempts to change the string will
330 * result in aborting the program. Also, if the string is wasting a
331 * lot of memory (allocation is sufficiently larger than what the
332 * string is really using), _dbus_string_lock() will realloc the
333 * string's data to "compact" it.
335 * @param str the string to lock.
338 _dbus_string_lock (DBusString *str)
340 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
344 /* Try to realloc to avoid excess memory usage, since
345 * we know we won't change the string further
348 if (real->allocated - MAX_WASTE > real->len)
353 new_allocated = real->len + ALLOCATION_PADDING;
355 new_str = dbus_realloc (real->str - real->align_offset,
359 real->str = new_str + real->align_offset;
360 real->allocated = new_allocated;
361 fixup_alignment (real);
365 #endif /* DBUS_BUILD_TESTS */
368 reallocate_for_length (DBusRealString *real,
374 /* at least double our old allocation to avoid O(n), avoiding
377 if (real->allocated > (MAX_MAX_LENGTH + ALLOCATION_PADDING) / 2)
378 new_allocated = MAX_MAX_LENGTH + ALLOCATION_PADDING;
380 new_allocated = real->allocated * 2;
382 /* if you change the code just above here, run the tests without
383 * the following assert-only hack before you commit
385 /* This is keyed off asserts in addition to tests so when you
386 * disable asserts to profile, you don't get this destroyer
389 #ifdef DBUS_DISABLE_ASSERT
391 #ifdef DBUS_BUILD_TESTS
392 new_allocated = 0; /* ensure a realloc every time so that we go
393 * through all malloc failure codepaths
395 #endif /* DBUS_BUILD_TESTS */
396 #endif /* !DBUS_DISABLE_ASSERT */
398 /* But be sure we always alloc at least space for the new length */
399 new_allocated = MAX (new_allocated,
400 new_length + ALLOCATION_PADDING);
402 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
403 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
404 if (_DBUS_UNLIKELY (new_str == NULL))
407 real->str = new_str + real->align_offset;
408 real->allocated = new_allocated;
409 fixup_alignment (real);
415 set_length (DBusRealString *real,
418 /* Note, we are setting the length not including nul termination */
420 /* exceeding max length is the same as failure to allocate memory */
421 if (_DBUS_UNLIKELY (new_length > real->max_length))
423 else if (new_length > (real->allocated - ALLOCATION_PADDING) &&
424 _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
428 real->len = new_length;
429 real->str[new_length] = '\0';
436 DBusRealString *dest,
442 if (len > dest->max_length - dest->len)
443 return FALSE; /* detected overflow of dest->len + len below */
445 if (!set_length (dest, dest->len + len))
448 memmove (dest->str + insert_at + len,
449 dest->str + insert_at,
450 dest->len - len - insert_at);
456 * Gets the raw character buffer from the string. The returned buffer
457 * will be nul-terminated, but note that strings may contain binary
458 * data so there may be extra nul characters prior to the termination.
459 * This function should be little-used, extend DBusString or add
460 * stuff to dbus-sysdeps.c instead. It's an error to use this
461 * function on a const string.
463 * @param str the string
467 _dbus_string_get_data (DBusString *str)
469 DBUS_STRING_PREAMBLE (str);
474 /* only do the function if we don't have the macro */
475 #ifndef _dbus_string_get_const_data
477 * Gets the raw character buffer from a const string.
479 * @param str the string
480 * @returns the string data
483 _dbus_string_get_const_data (const DBusString *str)
485 DBUS_CONST_STRING_PREAMBLE (str);
489 #endif /* _dbus_string_get_const_data */
492 * Gets a sub-portion of the raw character buffer from the
493 * string. The "len" field is required simply for error
494 * checking, to be sure you don't try to use more
495 * string than exists. The nul termination of the
496 * returned buffer remains at the end of the entire
497 * string, not at start + len.
499 * @param str the string
500 * @param start byte offset to return
501 * @param len length of segment to return
502 * @returns the string data
505 _dbus_string_get_data_len (DBusString *str,
509 DBUS_STRING_PREAMBLE (str);
510 _dbus_assert (start >= 0);
511 _dbus_assert (len >= 0);
512 _dbus_assert (start <= real->len);
513 _dbus_assert (len <= real->len - start);
515 return real->str + start;
519 * const version of _dbus_string_get_data_len().
521 * @param str the string
522 * @param start byte offset to return
523 * @param len length of segment to return
524 * @returns the string data
527 _dbus_string_get_const_data_len (const DBusString *str,
531 DBUS_CONST_STRING_PREAMBLE (str);
532 _dbus_assert (start >= 0);
533 _dbus_assert (len >= 0);
534 _dbus_assert (start <= real->len);
535 _dbus_assert (len <= real->len - start);
537 return real->str + start;
541 * Sets the value of the byte at the given position.
543 * @param str the string
544 * @param i the position
545 * @param byte the new value
548 _dbus_string_set_byte (DBusString *str,
552 DBUS_STRING_PREAMBLE (str);
553 _dbus_assert (i < real->len);
554 _dbus_assert (i >= 0);
559 /* only have the function if we didn't create a macro */
560 #ifndef _dbus_string_get_byte
562 * Gets the byte at the given position. It is
563 * allowed to ask for the nul byte at the end of
566 * @param str the string
567 * @param start the position
568 * @returns the byte at that position
571 _dbus_string_get_byte (const DBusString *str,
574 DBUS_CONST_STRING_PREAMBLE (str);
575 _dbus_assert (start <= real->len);
576 _dbus_assert (start >= 0);
578 return real->str[start];
580 #endif /* _dbus_string_get_byte */
583 * Inserts a number of bytes of a given value at the
586 * @param str the string
587 * @param i the position
588 * @param n_bytes number of bytes
589 * @param byte the value to insert
590 * @returns #TRUE on success
593 _dbus_string_insert_bytes (DBusString *str,
598 DBUS_STRING_PREAMBLE (str);
599 _dbus_assert (i <= real->len);
600 _dbus_assert (i >= 0);
601 _dbus_assert (n_bytes >= 0);
606 if (!open_gap (n_bytes, real, i))
609 memset (real->str + i, byte, n_bytes);
615 * Like _dbus_string_get_data(), but removes the
616 * gotten data from the original string. The caller
617 * must free the data returned. This function may
618 * fail due to lack of memory, and return #FALSE.
620 * @param str the string
621 * @param data_return location to return the buffer
622 * @returns #TRUE on success
625 _dbus_string_steal_data (DBusString *str,
629 DBUS_STRING_PREAMBLE (str);
630 _dbus_assert (data_return != NULL);
632 undo_alignment (real);
634 *data_return = real->str;
636 old_max_length = real->max_length;
638 /* reset the string */
639 if (!_dbus_string_init (str))
641 /* hrm, put it back then */
642 real->str = *data_return;
644 fixup_alignment (real);
648 real->max_length = old_max_length;
654 * Like _dbus_string_get_data_len(), but removes the gotten data from
655 * the original string. The caller must free the data returned. This
656 * function may fail due to lack of memory, and return #FALSE.
657 * The returned string is nul-terminated and has length len.
659 * @todo this function is broken because on failure it
660 * may corrupt the source string.
662 * @param str the string
663 * @param data_return location to return the buffer
664 * @param start the start of segment to steal
665 * @param len the length of segment to steal
666 * @returns #TRUE on success
669 _dbus_string_steal_data_len (DBusString *str,
675 DBUS_STRING_PREAMBLE (str);
676 _dbus_assert (data_return != NULL);
677 _dbus_assert (start >= 0);
678 _dbus_assert (len >= 0);
679 _dbus_assert (start <= real->len);
680 _dbus_assert (len <= real->len - start);
682 if (!_dbus_string_init (&dest))
685 set_max_length (&dest, real->max_length);
687 if (!_dbus_string_move_len (str, start, len, &dest, 0))
689 _dbus_string_free (&dest);
693 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
694 if (!_dbus_string_steal_data (&dest, data_return))
696 _dbus_string_free (&dest);
700 _dbus_string_free (&dest);
706 * Copies the data from the string into a char*
708 * @param str the string
709 * @param data_return place to return the data
710 * @returns #TRUE on success, #FALSE on no memory
713 _dbus_string_copy_data (const DBusString *str,
716 DBUS_CONST_STRING_PREAMBLE (str);
717 _dbus_assert (data_return != NULL);
719 *data_return = dbus_malloc (real->len + 1);
720 if (*data_return == NULL)
723 memcpy (*data_return, real->str, real->len + 1);
729 * Copies a segment of the string into a char*
731 * @param str the string
732 * @param data_return place to return the data
733 * @param start start index
734 * @param len length to copy
735 * @returns #FALSE if no memory
738 _dbus_string_copy_data_len (const DBusString *str,
745 DBUS_CONST_STRING_PREAMBLE (str);
746 _dbus_assert (data_return != NULL);
747 _dbus_assert (start >= 0);
748 _dbus_assert (len >= 0);
749 _dbus_assert (start <= real->len);
750 _dbus_assert (len <= real->len - start);
752 if (!_dbus_string_init (&dest))
755 set_max_length (&dest, real->max_length);
757 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
759 _dbus_string_free (&dest);
763 if (!_dbus_string_steal_data (&dest, data_return))
765 _dbus_string_free (&dest);
769 _dbus_string_free (&dest);
774 * Copies the contents of a DBusString into a different
775 * buffer. The resulting buffer will be nul-terminated.
777 * @param str a string
778 * @param buffer a C buffer to copy data to
779 * @param len maximum length of C buffer
782 _dbus_string_copy_to_buffer (const DBusString *str,
787 DBUS_CONST_STRING_PREAMBLE (str);
789 _dbus_assert (avail_len >= 0);
791 copy_len = MIN (avail_len, real->len+1);
792 memcpy (buffer, real->str, copy_len);
793 if (avail_len > 0 && avail_len == copy_len)
794 buffer[avail_len-1] = '\0';
797 /* Only have the function if we don't have the macro */
798 #ifndef _dbus_string_get_length
800 * Gets the length of a string (not including nul termination).
802 * @returns the length.
805 _dbus_string_get_length (const DBusString *str)
807 DBUS_CONST_STRING_PREAMBLE (str);
811 #endif /* !_dbus_string_get_length */
814 * Makes a string longer by the given number of bytes. Checks whether
815 * adding additional_length to the current length would overflow an
816 * integer, and checks for exceeding a string's max length.
817 * The new bytes are not initialized, other than nul-terminating
818 * the end of the string. The uninitialized bytes may contain
819 * nul bytes or other junk.
821 * @param str a string
822 * @param additional_length length to add to the string.
823 * @returns #TRUE on success.
826 _dbus_string_lengthen (DBusString *str,
827 int additional_length)
829 DBUS_STRING_PREAMBLE (str);
830 _dbus_assert (additional_length >= 0);
832 if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
833 return FALSE; /* would overflow */
835 return set_length (real,
836 real->len + additional_length);
840 * Makes a string shorter by the given number of bytes.
842 * @param str a string
843 * @param length_to_remove length to remove from the string.
846 _dbus_string_shorten (DBusString *str,
847 int length_to_remove)
849 DBUS_STRING_PREAMBLE (str);
850 _dbus_assert (length_to_remove >= 0);
851 _dbus_assert (length_to_remove <= real->len);
854 real->len - length_to_remove);
858 * Sets the length of a string. Can be used to truncate or lengthen
859 * the string. If the string is lengthened, the function may fail and
860 * return #FALSE. Newly-added bytes are not initialized, as with
861 * _dbus_string_lengthen().
863 * @param str a string
864 * @param length new length of the string.
865 * @returns #FALSE on failure.
868 _dbus_string_set_length (DBusString *str,
871 DBUS_STRING_PREAMBLE (str);
872 _dbus_assert (length >= 0);
874 return set_length (real, length);
878 align_length_then_lengthen (DBusString *str,
880 int then_lengthen_by)
882 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
884 DBUS_STRING_PREAMBLE (str);
885 _dbus_assert (alignment >= 1);
886 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
888 new_len = _DBUS_ALIGN_VALUE (real->len, alignment);
889 if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length - then_lengthen_by))
891 new_len += then_lengthen_by;
893 delta = new_len - real->len;
894 _dbus_assert (delta >= 0);
899 if (_DBUS_UNLIKELY (!set_length (real, new_len)))
902 /* delta == padding + then_lengthen_by
903 * new_len == old_len + padding + then_lengthen_by
904 * nul the padding if we had to add any padding
906 if (then_lengthen_by < delta)
908 memset (&real->str[new_len - delta], '\0',
909 delta - then_lengthen_by);
916 * Align the length of a string to a specific alignment (typically 4 or 8)
917 * by appending nul bytes to the string.
919 * @param str a string
920 * @param alignment the alignment
921 * @returns #FALSE if no memory
924 _dbus_string_align_length (DBusString *str,
927 return align_length_then_lengthen (str, alignment, 0);
931 append (DBusRealString *real,
938 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
941 memcpy (real->str + (real->len - buffer_len),
949 * Appends a nul-terminated C-style string to a DBusString.
951 * @param str the DBusString
952 * @param buffer the nul-terminated characters to append
953 * @returns #FALSE if not enough memory.
956 _dbus_string_append (DBusString *str,
959 unsigned long buffer_len;
961 DBUS_STRING_PREAMBLE (str);
962 _dbus_assert (buffer != NULL);
964 buffer_len = strlen (buffer);
965 if (buffer_len > (unsigned long) real->max_length)
968 return append (real, buffer, buffer_len);
972 * Appends 4 bytes aligned on a 4 byte boundary
973 * with any alignment padding initialized to 0.
975 * @param str the DBusString
976 * @param octets 4 bytes to append
977 * @returns #FALSE if not enough memory.
980 _dbus_string_append_4_aligned (DBusString *str,
981 const unsigned char octets[4])
984 DBUS_STRING_PREAMBLE (str);
986 if (!align_length_then_lengthen (str, 4, 4))
989 p = (dbus_uint32_t*) (real->str + (real->len - 4));
990 *p = *((dbus_uint32_t*)octets);
996 * Appends 8 bytes aligned on an 8 byte boundary
997 * with any alignment padding initialized to 0.
999 * @param str the DBusString
1000 * @param octets 4 bytes to append
1001 * @returns #FALSE if not enough memory.
1004 _dbus_string_append_8_aligned (DBusString *str,
1005 const unsigned char octets[8])
1007 #ifdef DBUS_HAVE_INT64
1009 DBUS_STRING_PREAMBLE (str);
1011 if (!align_length_then_lengthen (str, 8, 8))
1014 p = (dbus_uint64_t*) (real->str + (real->len - 8));
1015 *p = *((dbus_uint64_t*)octets);
1018 DBUS_STRING_PREAMBLE (str);
1020 if (!align_length_then_lengthen (str, 8, 8))
1023 p = real->str + (real->len - 8);
1033 _dbus_assert (p == (real->str + real->len));
1040 * Appends a printf-style formatted string
1041 * to the #DBusString.
1043 * @param str the string
1044 * @param format printf format
1045 * @param args variable argument list
1046 * @returns #FALSE if no memory
1049 _dbus_string_append_printf_valist (DBusString *str,
1057 DBUS_STRING_PREAMBLE (str);
1059 DBUS_VA_COPY (args_copy, args);
1061 /* Measure the message length without terminating nul */
1062 len = vsnprintf (&c, 1, format, args);
1064 if (!_dbus_string_lengthen (str, len))
1066 /* don't leak the copy */
1071 vsprintf (real->str + (real->len - len),
1080 * Appends a printf-style formatted string
1081 * to the #DBusString.
1083 * @param str the string
1084 * @param format printf format
1085 * @returns #FALSE if no memory
1088 _dbus_string_append_printf (DBusString *str,
1095 va_start (args, format);
1096 retval = _dbus_string_append_printf_valist (str, format, args);
1103 * Appends block of bytes with the given length to a DBusString.
1105 * @param str the DBusString
1106 * @param buffer the bytes to append
1107 * @param len the number of bytes to append
1108 * @returns #FALSE if not enough memory.
1111 _dbus_string_append_len (DBusString *str,
1115 DBUS_STRING_PREAMBLE (str);
1116 _dbus_assert (buffer != NULL);
1117 _dbus_assert (len >= 0);
1119 return append (real, buffer, len);
1123 * Appends a single byte to the string, returning #FALSE
1124 * if not enough memory.
1126 * @param str the string
1127 * @param byte the byte to append
1128 * @returns #TRUE on success
1131 _dbus_string_append_byte (DBusString *str,
1134 DBUS_STRING_PREAMBLE (str);
1136 if (!set_length (real, real->len + 1))
1139 real->str[real->len-1] = byte;
1145 * Appends a single Unicode character, encoding the character
1148 * @param str the string
1149 * @param ch the Unicode character
1152 _dbus_string_append_unichar (DBusString *str,
1160 DBUS_STRING_PREAMBLE (str);
1162 /* this code is from GLib but is pretty standard I think */
1171 else if (ch < 0x800)
1176 else if (ch < 0x10000)
1181 else if (ch < 0x200000)
1186 else if (ch < 0x4000000)
1197 if (len > (real->max_length - real->len))
1198 return FALSE; /* real->len + len would overflow */
1200 if (!set_length (real, real->len + len))
1203 out = real->str + (real->len - len);
1205 for (i = len - 1; i > 0; --i)
1207 out[i] = (ch & 0x3f) | 0x80;
1210 out[0] = ch | first;
1216 delete (DBusRealString *real,
1223 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1225 real->str[real->len] = '\0';
1229 * Deletes a segment of a DBusString with length len starting at
1230 * start. (Hint: to clear an entire string, setting length to 0
1231 * with _dbus_string_set_length() is easier.)
1233 * @param str the DBusString
1234 * @param start where to start deleting
1235 * @param len the number of bytes to delete
1238 _dbus_string_delete (DBusString *str,
1242 DBUS_STRING_PREAMBLE (str);
1243 _dbus_assert (start >= 0);
1244 _dbus_assert (len >= 0);
1245 _dbus_assert (start <= real->len);
1246 _dbus_assert (len <= real->len - start);
1248 delete (real, start, len);
1252 copy (DBusRealString *source,
1255 DBusRealString *dest,
1261 if (!open_gap (len, dest, insert_at))
1264 memcpy (dest->str + insert_at,
1265 source->str + start,
1272 * Checks assertions for two strings we're copying a segment between,
1273 * and declares real_source/real_dest variables.
1275 * @param source the source string
1276 * @param start the starting offset
1277 * @param dest the dest string
1278 * @param insert_at where the copied segment is inserted
1280 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1281 DBusRealString *real_source = (DBusRealString*) source; \
1282 DBusRealString *real_dest = (DBusRealString*) dest; \
1283 _dbus_assert ((source) != (dest)); \
1284 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1285 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1286 _dbus_assert (!real_dest->constant); \
1287 _dbus_assert (!real_dest->locked); \
1288 _dbus_assert ((start) >= 0); \
1289 _dbus_assert ((start) <= real_source->len); \
1290 _dbus_assert ((insert_at) >= 0); \
1291 _dbus_assert ((insert_at) <= real_dest->len)
1294 * Moves the end of one string into another string. Both strings
1295 * must be initialized, valid strings.
1297 * @param source the source string
1298 * @param start where to chop off the source string
1299 * @param dest the destination string
1300 * @param insert_at where to move the chopped-off part of source string
1301 * @returns #FALSE if not enough memory
1304 _dbus_string_move (DBusString *source,
1309 DBusRealString *real_source = (DBusRealString*) source;
1310 _dbus_assert (start <= real_source->len);
1312 return _dbus_string_move_len (source, start,
1313 real_source->len - start,
1318 * Like _dbus_string_move(), but does not delete the section
1319 * of the source string that's copied to the dest string.
1321 * @param source the source string
1322 * @param start where to start copying the source string
1323 * @param dest the destination string
1324 * @param insert_at where to place the copied part of source string
1325 * @returns #FALSE if not enough memory
1328 _dbus_string_copy (const DBusString *source,
1333 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1335 return copy (real_source, start,
1336 real_source->len - start,
1342 * Like _dbus_string_move(), but can move a segment from
1343 * the middle of the source string.
1345 * @todo this doesn't do anything with max_length field.
1346 * we should probably just kill the max_length field though.
1348 * @param source the source string
1349 * @param start first byte of source string to move
1350 * @param len length of segment to move
1351 * @param dest the destination string
1352 * @param insert_at where to move the bytes from the source string
1353 * @returns #FALSE if not enough memory
1356 _dbus_string_move_len (DBusString *source,
1363 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1364 _dbus_assert (len >= 0);
1365 _dbus_assert ((start + len) <= real_source->len);
1372 else if (start == 0 &&
1373 len == real_source->len &&
1374 real_dest->len == 0)
1376 /* Short-circuit moving an entire existing string to an empty string
1377 * by just swapping the buffers.
1379 /* we assume ->constant doesn't matter as you can't have
1380 * a constant string involved in a move.
1382 #define ASSIGN_DATA(a, b) do { \
1383 (a)->str = (b)->str; \
1384 (a)->len = (b)->len; \
1385 (a)->allocated = (b)->allocated; \
1386 (a)->align_offset = (b)->align_offset; \
1391 ASSIGN_DATA (&tmp, real_source);
1392 ASSIGN_DATA (real_source, real_dest);
1393 ASSIGN_DATA (real_dest, &tmp);
1399 if (!copy (real_source, start, len,
1404 delete (real_source, start,
1412 * Like _dbus_string_copy(), but can copy a segment from the middle of
1413 * the source string.
1415 * @param source the source string
1416 * @param start where to start copying the source string
1417 * @param len length of segment to copy
1418 * @param dest the destination string
1419 * @param insert_at where to place the copied segment of source string
1420 * @returns #FALSE if not enough memory
1423 _dbus_string_copy_len (const DBusString *source,
1429 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1430 _dbus_assert (len >= 0);
1431 _dbus_assert (start <= real_source->len);
1432 _dbus_assert (len <= real_source->len - start);
1434 return copy (real_source, start, len,
1440 * Replaces a segment of dest string with a segment of source string.
1442 * @todo optimize the case where the two lengths are the same, and
1443 * avoid memmoving the data in the trailing part of the string twice.
1445 * @todo avoid inserting the source into dest, then deleting
1446 * the replaced chunk of dest (which creates a potentially large
1447 * intermediate string). Instead, extend the replaced chunk
1448 * of dest with padding to the same size as the source chunk,
1449 * then copy in the source bytes.
1451 * @param source the source string
1452 * @param start where to start copying the source string
1453 * @param len length of segment to copy
1454 * @param dest the destination string
1455 * @param replace_at start of segment of dest string to replace
1456 * @param replace_len length of segment of dest string to replace
1457 * @returns #FALSE if not enough memory
1461 _dbus_string_replace_len (const DBusString *source,
1468 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1469 _dbus_assert (len >= 0);
1470 _dbus_assert (start <= real_source->len);
1471 _dbus_assert (len <= real_source->len - start);
1472 _dbus_assert (replace_at >= 0);
1473 _dbus_assert (replace_at <= real_dest->len);
1474 _dbus_assert (replace_len <= real_dest->len - replace_at);
1476 if (!copy (real_source, start, len,
1477 real_dest, replace_at))
1480 delete (real_dest, replace_at + len, replace_len);
1485 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1486 * Pennington, and Tom Tromey are the authors and authorized relicense.
1489 /** computes length and mask of a unicode character
1490 * @param Char the char
1491 * @param Mask the mask variable to assign to
1492 * @param Len the length variable to assign to
1494 #define UTF8_COMPUTE(Char, Mask, Len) \
1500 else if ((Char & 0xe0) == 0xc0) \
1505 else if ((Char & 0xf0) == 0xe0) \
1510 else if ((Char & 0xf8) == 0xf0) \
1515 else if ((Char & 0xfc) == 0xf8) \
1520 else if ((Char & 0xfe) == 0xfc) \
1532 * computes length of a unicode character in UTF-8
1533 * @param Char the char
1535 #define UTF8_LENGTH(Char) \
1536 ((Char) < 0x80 ? 1 : \
1537 ((Char) < 0x800 ? 2 : \
1538 ((Char) < 0x10000 ? 3 : \
1539 ((Char) < 0x200000 ? 4 : \
1540 ((Char) < 0x4000000 ? 5 : 6)))))
1543 * Gets a UTF-8 value.
1545 * @param Result variable for extracted unicode char.
1546 * @param Chars the bytes to decode
1547 * @param Count counter variable
1548 * @param Mask mask for this char
1549 * @param Len length for this char in bytes
1551 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1552 (Result) = (Chars)[0] & (Mask); \
1553 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1555 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1561 (Result) |= ((Chars)[(Count)] & 0x3f); \
1565 * Check whether a unicode char is in a valid range.
1567 * @param Char the character
1569 #define UNICODE_VALID(Char) \
1570 ((Char) < 0x110000 && \
1571 (((Char) & 0xFFFFF800) != 0xD800) && \
1572 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1573 ((Char) & 0xFFFF) != 0xFFFF)
1576 * Gets a unicode character from a UTF-8 string. Does no validation;
1577 * you must verify that the string is valid UTF-8 in advance and must
1578 * pass in the start of a character.
1580 * @param str the string
1581 * @param start the start of the UTF-8 character.
1582 * @param ch_return location to return the character
1583 * @param end_return location to return the byte index of next character
1586 _dbus_string_get_unichar (const DBusString *str,
1588 dbus_unichar_t *ch_return,
1592 dbus_unichar_t result;
1595 DBUS_CONST_STRING_PREAMBLE (str);
1596 _dbus_assert (start >= 0);
1597 _dbus_assert (start <= real->len);
1602 *end_return = real->len;
1605 p = real->str + start;
1608 UTF8_COMPUTE (c, mask, len);
1611 UTF8_GET (result, p, i, mask, len);
1613 if (result == (dbus_unichar_t)-1)
1617 *ch_return = result;
1619 *end_return = start + len;
1623 * Finds the given substring in the string,
1624 * returning #TRUE and filling in the byte index
1625 * where the substring was found, if it was found.
1626 * Returns #FALSE if the substring wasn't found.
1627 * Sets *start to the length of the string if the substring
1630 * @param str the string
1631 * @param start where to start looking
1632 * @param substr the substring
1633 * @param found return location for where it was found, or #NULL
1634 * @returns #TRUE if found
1637 _dbus_string_find (const DBusString *str,
1642 return _dbus_string_find_to (str, start,
1643 ((const DBusRealString*)str)->len,
1648 * Finds the given substring in the string,
1649 * up to a certain position,
1650 * returning #TRUE and filling in the byte index
1651 * where the substring was found, if it was found.
1652 * Returns #FALSE if the substring wasn't found.
1653 * Sets *start to the length of the string if the substring
1656 * @param str the string
1657 * @param start where to start looking
1658 * @param end where to stop looking
1659 * @param substr the substring
1660 * @param found return location for where it was found, or #NULL
1661 * @returns #TRUE if found
1664 _dbus_string_find_to (const DBusString *str,
1671 DBUS_CONST_STRING_PREAMBLE (str);
1672 _dbus_assert (substr != NULL);
1673 _dbus_assert (start <= real->len);
1674 _dbus_assert (start >= 0);
1675 _dbus_assert (substr != NULL);
1676 _dbus_assert (end <= real->len);
1677 _dbus_assert (start <= end);
1679 /* we always "find" an empty string */
1680 if (*substr == '\0')
1690 if (real->str[i] == substr[0])
1696 if (substr[j - i] == '\0')
1698 else if (real->str[j] != substr[j - i])
1704 if (substr[j - i] == '\0')
1722 * Find the given byte scanning backward from the given start.
1723 * Sets *found to -1 if the byte is not found.
1725 * @param str the string
1726 * @param start the place to start scanning (will not find the byte at this point)
1727 * @param byte the byte to find
1728 * @param found return location for where it was found
1729 * @returns #TRUE if found
1732 _dbus_string_find_byte_backward (const DBusString *str,
1738 DBUS_CONST_STRING_PREAMBLE (str);
1739 _dbus_assert (start <= real->len);
1740 _dbus_assert (start >= 0);
1741 _dbus_assert (found != NULL);
1746 if (real->str[i] == byte)
1759 * Finds a blank (space or tab) in the string. Returns #TRUE
1760 * if found, #FALSE otherwise. If a blank is not found sets
1761 * *found to the length of the string.
1763 * @param str the string
1764 * @param start byte index to start looking
1765 * @param found place to store the location of the first blank
1766 * @returns #TRUE if a blank was found
1769 _dbus_string_find_blank (const DBusString *str,
1774 DBUS_CONST_STRING_PREAMBLE (str);
1775 _dbus_assert (start <= real->len);
1776 _dbus_assert (start >= 0);
1779 while (i < real->len)
1781 if (real->str[i] == ' ' ||
1782 real->str[i] == '\t')
1799 * Skips blanks from start, storing the first non-blank in *end
1800 * (blank is space or tab).
1802 * @param str the string
1803 * @param start where to start
1804 * @param end where to store the first non-blank byte index
1807 _dbus_string_skip_blank (const DBusString *str,
1812 DBUS_CONST_STRING_PREAMBLE (str);
1813 _dbus_assert (start <= real->len);
1814 _dbus_assert (start >= 0);
1817 while (i < real->len)
1819 if (!(real->str[i] == ' ' ||
1820 real->str[i] == '\t'))
1826 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1827 real->str[i] == '\t'));
1834 * Skips whitespace from start, storing the first non-whitespace in *end.
1835 * (whitespace is space, tab, newline, CR).
1837 * @param str the string
1838 * @param start where to start
1839 * @param end where to store the first non-whitespace byte index
1842 _dbus_string_skip_white (const DBusString *str,
1847 DBUS_CONST_STRING_PREAMBLE (str);
1848 _dbus_assert (start <= real->len);
1849 _dbus_assert (start >= 0);
1852 while (i < real->len)
1854 if (!(real->str[i] == ' ' ||
1855 real->str[i] == '\n' ||
1856 real->str[i] == '\r' ||
1857 real->str[i] == '\t'))
1863 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1864 real->str[i] == '\t'));
1871 * Assigns a newline-terminated or \\r\\n-terminated line from the front
1872 * of the string to the given dest string. The dest string's previous
1873 * contents are deleted. If the source string contains no newline,
1874 * moves the entire source string to the dest string.
1876 * @todo owen correctly notes that this is a stupid function (it was
1877 * written purely for test code,
1878 * e.g. dbus-message-builder.c). Probably should be enforced as test
1879 * code only with #ifdef DBUS_BUILD_TESTS
1881 * @param source the source string
1882 * @param dest the destination string (contents are replaced)
1883 * @returns #FALSE if no memory, or source has length 0
1886 _dbus_string_pop_line (DBusString *source,
1890 dbus_bool_t have_newline;
1892 _dbus_string_set_length (dest, 0);
1895 if (_dbus_string_find (source, 0, "\n", &eol))
1897 have_newline = TRUE;
1898 eol += 1; /* include newline */
1902 eol = _dbus_string_get_length (source);
1903 have_newline = FALSE;
1907 return FALSE; /* eof */
1909 if (!_dbus_string_move_len (source, 0, eol,
1915 /* dump the newline and the \r if we have one */
1918 dbus_bool_t have_cr;
1920 _dbus_assert (_dbus_string_get_length (dest) > 0);
1922 if (_dbus_string_get_length (dest) > 1 &&
1923 _dbus_string_get_byte (dest,
1924 _dbus_string_get_length (dest) - 2) == '\r')
1929 _dbus_string_set_length (dest,
1930 _dbus_string_get_length (dest) -
1938 * Deletes up to and including the first blank space
1941 * @param str the string
1944 _dbus_string_delete_first_word (DBusString *str)
1948 if (_dbus_string_find_blank (str, 0, &i))
1949 _dbus_string_skip_blank (str, i, &i);
1951 _dbus_string_delete (str, 0, i);
1955 * Deletes any leading blanks in the string
1957 * @param str the string
1960 _dbus_string_delete_leading_blanks (DBusString *str)
1964 _dbus_string_skip_blank (str, 0, &i);
1967 _dbus_string_delete (str, 0, i);
1971 * Tests two DBusString for equality.
1973 * @todo memcmp is probably faster
1975 * @param a first string
1976 * @param b second string
1977 * @returns #TRUE if equal
1980 _dbus_string_equal (const DBusString *a,
1981 const DBusString *b)
1983 const unsigned char *ap;
1984 const unsigned char *bp;
1985 const unsigned char *a_end;
1986 const DBusRealString *real_a = (const DBusRealString*) a;
1987 const DBusRealString *real_b = (const DBusRealString*) b;
1988 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1989 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1991 if (real_a->len != real_b->len)
1996 a_end = real_a->str + real_a->len;
2010 * Tests two DBusString for equality up to the given length.
2012 * @todo write a unit test
2014 * @todo memcmp is probably faster
2016 * @param a first string
2017 * @param b second string
2018 * @param len the lengh
2019 * @returns #TRUE if equal for the given number of bytes
2022 _dbus_string_equal_len (const DBusString *a,
2023 const DBusString *b,
2026 const unsigned char *ap;
2027 const unsigned char *bp;
2028 const unsigned char *a_end;
2029 const DBusRealString *real_a = (const DBusRealString*) a;
2030 const DBusRealString *real_b = (const DBusRealString*) b;
2031 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2032 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2034 if (real_a->len != real_b->len &&
2035 (real_a->len < len || real_b->len < len))
2040 a_end = real_a->str + MIN (real_a->len, len);
2054 * Checks whether a string is equal to a C string.
2056 * @param a the string
2057 * @param c_str the C string
2058 * @returns #TRUE if equal
2061 _dbus_string_equal_c_str (const DBusString *a,
2064 const unsigned char *ap;
2065 const unsigned char *bp;
2066 const unsigned char *a_end;
2067 const DBusRealString *real_a = (const DBusRealString*) a;
2068 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2069 _dbus_assert (c_str != NULL);
2072 bp = (const unsigned char*) c_str;
2073 a_end = real_a->str + real_a->len;
2074 while (ap != a_end && *bp)
2083 if (ap != a_end || *bp)
2090 * Checks whether a string starts with the given C string.
2092 * @param a the string
2093 * @param c_str the C string
2094 * @returns #TRUE if string starts with it
2097 _dbus_string_starts_with_c_str (const DBusString *a,
2100 const unsigned char *ap;
2101 const unsigned char *bp;
2102 const unsigned char *a_end;
2103 const DBusRealString *real_a = (const DBusRealString*) a;
2104 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2105 _dbus_assert (c_str != NULL);
2108 bp = (const unsigned char*) c_str;
2109 a_end = real_a->str + real_a->len;
2110 while (ap != a_end && *bp)
2126 * Returns whether a string ends with the given suffix
2128 * @todo memcmp might make this faster.
2130 * @param a the string
2131 * @param c_str the C-style string
2132 * @returns #TRUE if the string ends with the suffix
2135 _dbus_string_ends_with_c_str (const DBusString *a,
2138 const unsigned char *ap;
2139 const unsigned char *bp;
2140 const unsigned char *a_end;
2141 unsigned long c_str_len;
2142 const DBusRealString *real_a = (const DBusRealString*) a;
2143 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2144 _dbus_assert (c_str != NULL);
2146 c_str_len = strlen (c_str);
2147 if (((unsigned long)real_a->len) < c_str_len)
2150 ap = real_a->str + (real_a->len - c_str_len);
2151 bp = (const unsigned char*) c_str;
2152 a_end = real_a->str + real_a->len;
2162 _dbus_assert (*ap == '\0');
2163 _dbus_assert (*bp == '\0');
2169 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2170 * encoded. (Each byte is two hex digits.)
2172 * @param source the string to encode
2173 * @param start byte index to start encoding
2174 * @param dest string where encoded data should be placed
2175 * @param insert_at where to place encoded data
2176 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2179 _dbus_string_hex_encode (const DBusString *source,
2185 const char hexdigits[16] = {
2186 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2187 'a', 'b', 'c', 'd', 'e', 'f'
2189 const unsigned char *p;
2190 const unsigned char *end;
2193 _dbus_assert (start <= _dbus_string_get_length (source));
2195 if (!_dbus_string_init (&result))
2200 p = (const unsigned char*) _dbus_string_get_const_data (source);
2201 end = p + _dbus_string_get_length (source);
2206 if (!_dbus_string_append_byte (&result,
2207 hexdigits[(*p >> 4)]))
2210 if (!_dbus_string_append_byte (&result,
2211 hexdigits[(*p & 0x0f)]))
2217 if (!_dbus_string_move (&result, 0, dest, insert_at))
2223 _dbus_string_free (&result);
2228 * Decodes a string from hex encoding.
2230 * @param source the string to decode
2231 * @param start byte index to start decode
2232 * @param end_return return location of the end of the hex data, or #NULL
2233 * @param dest string where decoded data should be placed
2234 * @param insert_at where to place decoded data
2235 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2238 _dbus_string_hex_decode (const DBusString *source,
2245 const unsigned char *p;
2246 const unsigned char *end;
2248 dbus_bool_t high_bits;
2250 _dbus_assert (start <= _dbus_string_get_length (source));
2252 if (!_dbus_string_init (&result))
2258 p = (const unsigned char*) _dbus_string_get_const_data (source);
2259 end = p + _dbus_string_get_length (source);
2328 if (!_dbus_string_append_byte (&result,
2337 len = _dbus_string_get_length (&result);
2339 b = _dbus_string_get_byte (&result, len - 1);
2343 _dbus_string_set_byte (&result, len - 1, b);
2346 high_bits = !high_bits;
2352 if (!_dbus_string_move (&result, 0, dest, insert_at))
2356 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2361 _dbus_string_free (&result);
2366 * Checks that the given range of the string is valid ASCII with no
2367 * nul bytes. If the given range is not entirely contained in the
2368 * string, returns #FALSE.
2370 * @todo this is inconsistent with most of DBusString in that
2371 * it allows a start,len range that extends past the string end.
2373 * @param str the string
2374 * @param start first byte index to check
2375 * @param len number of bytes to check
2376 * @returns #TRUE if the byte range exists and is all valid ASCII
2379 _dbus_string_validate_ascii (const DBusString *str,
2383 const unsigned char *s;
2384 const unsigned char *end;
2385 DBUS_CONST_STRING_PREAMBLE (str);
2386 _dbus_assert (start >= 0);
2387 _dbus_assert (start <= real->len);
2388 _dbus_assert (len >= 0);
2390 if (len > real->len - start)
2393 s = real->str + start;
2397 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2407 * Checks that the given range of the string is valid UTF-8. If the
2408 * given range is not entirely contained in the string, returns
2409 * #FALSE. If the string contains any nul bytes in the given range,
2410 * returns #FALSE. If the start and start+len are not on character
2411 * boundaries, returns #FALSE.
2413 * @todo this is inconsistent with most of DBusString in that
2414 * it allows a start,len range that extends past the string end.
2416 * @param str the string
2417 * @param start first byte index to check
2418 * @param len number of bytes to check
2419 * @returns #TRUE if the byte range exists and is all valid UTF-8
2422 _dbus_string_validate_utf8 (const DBusString *str,
2426 const unsigned char *p;
2427 const unsigned char *end;
2428 DBUS_CONST_STRING_PREAMBLE (str);
2429 _dbus_assert (start >= 0);
2430 _dbus_assert (start <= real->len);
2431 _dbus_assert (len >= 0);
2433 /* we are doing _DBUS_UNLIKELY() here which might be
2434 * dubious in a generic library like GLib, but in D-BUS
2435 * we know we're validating messages and that it would
2436 * only be evil/broken apps that would have invalid
2437 * UTF-8. Also, this function seems to be a performance
2438 * bottleneck in profiles.
2441 if (_DBUS_UNLIKELY (len > real->len - start))
2444 p = real->str + start;
2449 int i, mask, char_len;
2450 dbus_unichar_t result;
2452 /* nul bytes considered invalid */
2456 /* Special-case ASCII; this makes us go a lot faster in
2457 * D-BUS profiles where we are typically validating
2458 * function names and such. We have to know that
2459 * all following checks will pass for ASCII though,
2460 * comments follow ...
2468 UTF8_COMPUTE (*p, mask, char_len);
2470 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2473 /* check that the expected number of bytes exists in the remaining length */
2474 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2477 UTF8_GET (result, p, i, mask, char_len);
2479 /* Check for overlong UTF-8 */
2480 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2483 /* The UNICODE_VALID check below will catch this */
2484 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2488 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2491 /* UNICODE_VALID should have caught it */
2492 _dbus_assert (result != (dbus_unichar_t)-1);
2497 /* See that we covered the entire length if a length was
2500 if (_DBUS_UNLIKELY (p != end))
2507 * Checks that the given range of the string is all nul bytes. If the
2508 * given range is not entirely contained in the string, returns
2511 * @todo this is inconsistent with most of DBusString in that
2512 * it allows a start,len range that extends past the string end.
2514 * @param str the string
2515 * @param start first byte index to check
2516 * @param len number of bytes to check
2517 * @returns #TRUE if the byte range exists and is all nul bytes
2520 _dbus_string_validate_nul (const DBusString *str,
2524 const unsigned char *s;
2525 const unsigned char *end;
2526 DBUS_CONST_STRING_PREAMBLE (str);
2527 _dbus_assert (start >= 0);
2528 _dbus_assert (len >= 0);
2529 _dbus_assert (start <= real->len);
2531 if (len > real->len - start)
2534 s = real->str + start;
2538 if (_DBUS_UNLIKELY (*s != '\0'))
2547 * Checks that the given range of the string is a valid object path
2548 * name in the D-BUS protocol. This includes a length restriction,
2549 * etc., see the specification. It does not validate UTF-8, that has
2550 * to be done separately for now.
2552 * @todo this is inconsistent with most of DBusString in that
2553 * it allows a start,len range that extends past the string end.
2555 * @todo change spec to disallow more things, such as spaces in the
2558 * @param str the string
2559 * @param start first byte index to check
2560 * @param len number of bytes to check
2561 * @returns #TRUE if the byte range exists and is a valid name
2564 _dbus_string_validate_path (const DBusString *str,
2568 const unsigned char *s;
2569 const unsigned char *end;
2570 const unsigned char *last_slash;
2572 DBUS_CONST_STRING_PREAMBLE (str);
2573 _dbus_assert (start >= 0);
2574 _dbus_assert (len >= 0);
2575 _dbus_assert (start <= real->len);
2577 if (len > real->len - start)
2580 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2586 s = real->str + start;
2598 if ((s - last_slash) < 2)
2599 return FALSE; /* no empty path components allowed */
2607 if ((end - last_slash) < 2 &&
2609 return FALSE; /* trailing slash not allowed unless the string is "/" */
2615 * Determine wether the given charater is valid as the first charater
2618 #define VALID_INITIAL_NAME_CHARACTER(c) \
2619 ( ((c) >= 'A' && (c) <= 'Z') || \
2620 ((c) >= 'a' && (c) <= 'z') || \
2624 * Determine wether the given charater is valid as a second or later
2625 * character in a nam
2627 #define VALID_NAME_CHARACTER(c) \
2628 ( ((c) >= '0' && (c) <= '9') || \
2629 ((c) >= 'A' && (c) <= 'Z') || \
2630 ((c) >= 'a' && (c) <= 'z') || \
2634 * Checks that the given range of the string is a valid interface name
2635 * in the D-BUS protocol. This includes a length restriction and an
2636 * ASCII subset, see the specification.
2638 * @todo this is inconsistent with most of DBusString in that
2639 * it allows a start,len range that extends past the string end.
2641 * @param str the string
2642 * @param start first byte index to check
2643 * @param len number of bytes to check
2644 * @returns #TRUE if the byte range exists and is a valid name
2647 _dbus_string_validate_interface (const DBusString *str,
2651 const unsigned char *s;
2652 const unsigned char *end;
2653 const unsigned char *iface;
2654 const unsigned char *last_dot;
2656 DBUS_CONST_STRING_PREAMBLE (str);
2657 _dbus_assert (start >= 0);
2658 _dbus_assert (len >= 0);
2659 _dbus_assert (start <= real->len);
2661 if (len > real->len - start)
2664 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2671 iface = real->str + start;
2675 /* check special cases of first char so it doesn't have to be done
2676 * in the loop. Note we know len > 0
2678 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
2680 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
2689 if (_DBUS_UNLIKELY ((s + 1) == end))
2691 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
2694 ++s; /* we just validated the next char, so skip two */
2696 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
2704 if (_DBUS_UNLIKELY (last_dot == NULL))
2711 * Checks that the given range of the string is a valid member name
2712 * in the D-BUS protocol. This includes a length restriction, etc.,
2713 * see the specification.
2715 * @todo this is inconsistent with most of DBusString in that
2716 * it allows a start,len range that extends past the string end.
2718 * @param str the string
2719 * @param start first byte index to check
2720 * @param len number of bytes to check
2721 * @returns #TRUE if the byte range exists and is a valid name
2724 _dbus_string_validate_member (const DBusString *str,
2728 const unsigned char *s;
2729 const unsigned char *end;
2730 const unsigned char *member;
2732 DBUS_CONST_STRING_PREAMBLE (str);
2733 _dbus_assert (start >= 0);
2734 _dbus_assert (len >= 0);
2735 _dbus_assert (start <= real->len);
2737 if (len > real->len - start)
2740 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2746 member = real->str + start;
2750 /* check special cases of first char so it doesn't have to be done
2751 * in the loop. Note we know len > 0
2754 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
2761 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
2773 * Checks that the given range of the string is a valid error name
2774 * in the D-BUS protocol. This includes a length restriction, etc.,
2775 * see the specification.
2777 * @todo this is inconsistent with most of DBusString in that
2778 * it allows a start,len range that extends past the string end.
2780 * @param str the string
2781 * @param start first byte index to check
2782 * @param len number of bytes to check
2783 * @returns #TRUE if the byte range exists and is a valid name
2786 _dbus_string_validate_error_name (const DBusString *str,
2790 /* Same restrictions as interface name at the moment */
2791 return _dbus_string_validate_interface (str, start, len);
2794 /* This assumes the first char exists and is ':' */
2796 _dbus_string_validate_base_service (const DBusString *str,
2800 const unsigned char *s;
2801 const unsigned char *end;
2802 const unsigned char *service;
2804 DBUS_CONST_STRING_PREAMBLE (str);
2805 _dbus_assert (start >= 0);
2806 _dbus_assert (len >= 0);
2807 _dbus_assert (start <= real->len);
2809 if (len > real->len - start)
2812 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2815 _dbus_assert (len > 0);
2817 service = real->str + start;
2818 end = service + len;
2819 _dbus_assert (*service == ':');
2826 if (_DBUS_UNLIKELY ((s + 1) == end))
2828 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*(s + 1))))
2830 ++s; /* we just validated the next char, so skip two */
2832 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
2844 * Checks that the given range of the string is a valid service name
2845 * in the D-BUS protocol. This includes a length restriction, etc.,
2846 * see the specification.
2848 * @todo this is inconsistent with most of DBusString in that
2849 * it allows a start,len range that extends past the string end.
2851 * @param str the string
2852 * @param start first byte index to check
2853 * @param len number of bytes to check
2854 * @returns #TRUE if the byte range exists and is a valid name
2857 _dbus_string_validate_service (const DBusString *str,
2861 if (_DBUS_UNLIKELY (len == 0))
2863 if (_dbus_string_get_byte (str, start) == ':')
2864 return _dbus_string_validate_base_service (str, start, len);
2866 return _dbus_string_validate_interface (str, start, len);
2870 * Checks that the given range of the string is a valid message type
2871 * signature in the D-BUS protocol.
2873 * @todo this is inconsistent with most of DBusString in that
2874 * it allows a start,len range that extends past the string end.
2876 * @param str the string
2877 * @param start first byte index to check
2878 * @param len number of bytes to check
2879 * @returns #TRUE if the byte range exists and is a valid signature
2882 _dbus_string_validate_signature (const DBusString *str,
2886 const unsigned char *s;
2887 const unsigned char *end;
2888 DBUS_CONST_STRING_PREAMBLE (str);
2889 _dbus_assert (start >= 0);
2890 _dbus_assert (start <= real->len);
2891 _dbus_assert (len >= 0);
2893 if (len > real->len - start)
2896 s = real->str + start;
2903 case DBUS_TYPE_BYTE:
2904 case DBUS_TYPE_BOOLEAN:
2905 case DBUS_TYPE_INT32:
2906 case DBUS_TYPE_UINT32:
2907 case DBUS_TYPE_INT64:
2908 case DBUS_TYPE_UINT64:
2909 case DBUS_TYPE_DOUBLE:
2910 case DBUS_TYPE_STRING:
2911 case DBUS_TYPE_CUSTOM:
2912 case DBUS_TYPE_ARRAY:
2913 case DBUS_TYPE_DICT:
2914 case DBUS_TYPE_OBJECT_PATH:
2928 * Clears all allocated bytes in the string to zero.
2930 * @param str the string
2933 _dbus_string_zero (DBusString *str)
2935 DBUS_STRING_PREAMBLE (str);
2937 memset (real->str - real->align_offset, '\0', real->allocated);
2941 #ifdef DBUS_BUILD_TESTS
2942 #include "dbus-test.h"
2946 * Parses a basic type defined by type contained in a DBusString. The
2947 * end_return parameter may be #NULL if you aren't interested in it. The
2948 * type is parsed and stored in value_return. Return parameters are not
2949 * initialized if the function returns #FALSE.
2951 * @param str the string
2952 * @param type the type of the basic type
2953 * @param start the byte index of the start of the type
2954 * @param value_return return location of the value or #NULL
2955 * @param end_return return location of the end of the type, or #NULL
2956 * @returns #TRUE on success
2959 _dbus_string_parse_basic_type (const DBusString *str,
2969 case DBUS_TYPE_BOOLEAN:
2971 int len = _dbus_string_get_length (str) - start;
2972 if (len >= 5 && _dbus_string_find_to (str, start, start + 5, "false", NULL))
2975 *(unsigned char *) value = TRUE;
2977 else if (len >= 4 && _dbus_string_find_to (str, start, start + 4, "true", NULL))
2980 *(unsigned char *) value = FALSE;
2983 _dbus_warn ("could not parse BOOLEAN\n");
2986 case DBUS_TYPE_BYTE:
2990 if (_dbus_string_get_byte (str, start) == '\'' &&
2991 _dbus_string_get_length (str) >= start + 4 &&
2992 _dbus_string_get_byte (str, start + 1) == '\\' &&
2993 _dbus_string_get_byte (str, start + 2) == '\'' &&
2994 _dbus_string_get_byte (str, start + 3) == '\'')
2999 else if (_dbus_string_get_byte (str, start) == '\'' &&
3000 _dbus_string_get_length (str) >= start + 3 &&
3001 _dbus_string_get_byte (str, start + 2) == '\'')
3003 val = _dbus_string_get_byte (str, start + 1);
3008 if (!_dbus_string_parse_int (str, start, &val, &end))
3009 _dbus_warn ("Failed to parse integer for BYTE\n");
3013 _dbus_warn ("A byte must be in range 0-255 not %ld\n", val);
3015 *(unsigned char *) value = val;
3018 case DBUS_TYPE_INT32:
3021 if (_dbus_string_parse_int (str, start, &val, &end))
3022 *(dbus_int32_t *)value = val;
3025 case DBUS_TYPE_UINT32:
3028 if (_dbus_string_parse_uint (str, start, &val, &end))
3029 *(dbus_uint32_t *)value = val;
3032 #ifdef DBUS_HAVE_INT64
3033 case DBUS_TYPE_INT64:
3034 case DBUS_TYPE_UINT64:
3035 /* use stroll oull */
3036 _dbus_assert_not_reached ("string -> [u]int64 not supported yet");
3038 #endif /* DBUS_HAVE_INT64 */
3039 case DBUS_TYPE_DOUBLE:
3040 _dbus_string_parse_double (str, start, value, &end);
3043 _dbus_assert_not_reached ("not a basic type");
3049 return end != start;
3053 test_max_len (DBusString *str,
3058 if (!_dbus_string_set_length (str, max_len - 1))
3059 _dbus_assert_not_reached ("setting len to one less than max should have worked");
3062 if (!_dbus_string_set_length (str, max_len))
3063 _dbus_assert_not_reached ("setting len to max len should have worked");
3065 if (_dbus_string_set_length (str, max_len + 1))
3066 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
3068 if (!_dbus_string_set_length (str, 0))
3069 _dbus_assert_not_reached ("setting len to zero should have worked");
3073 test_hex_roundtrip (const unsigned char *data,
3082 len = strlen (data);
3084 if (!_dbus_string_init (&orig))
3085 _dbus_assert_not_reached ("could not init string");
3087 if (!_dbus_string_init (&encoded))
3088 _dbus_assert_not_reached ("could not init string");
3090 if (!_dbus_string_init (&decoded))
3091 _dbus_assert_not_reached ("could not init string");
3093 if (!_dbus_string_append_len (&orig, data, len))
3094 _dbus_assert_not_reached ("couldn't append orig data");
3096 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
3097 _dbus_assert_not_reached ("could not encode");
3099 if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
3100 _dbus_assert_not_reached ("could not decode");
3102 _dbus_assert (_dbus_string_get_length (&encoded) == end);
3104 if (!_dbus_string_equal (&orig, &decoded))
3108 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
3109 _dbus_string_get_length (&orig),
3110 _dbus_string_get_length (&encoded),
3111 _dbus_string_get_length (&decoded));
3112 printf ("Original: %s\n", data);
3113 s = _dbus_string_get_const_data (&decoded);
3114 printf ("Decoded: %s\n", s);
3115 _dbus_assert_not_reached ("original string not the same as string decoded from hex");
3118 _dbus_string_free (&orig);
3119 _dbus_string_free (&encoded);
3120 _dbus_string_free (&decoded);
3123 typedef void (* TestRoundtripFunc) (const unsigned char *data,
3126 test_roundtrips (TestRoundtripFunc func)
3128 (* func) ("Hello this is a string\n", -1);
3129 (* func) ("Hello this is a string\n1", -1);
3130 (* func) ("Hello this is a string\n12", -1);
3131 (* func) ("Hello this is a string\n123", -1);
3132 (* func) ("Hello this is a string\n1234", -1);
3133 (* func) ("Hello this is a string\n12345", -1);
3137 (* func) ("123", 3);
3138 (* func) ("1234", 4);
3139 (* func) ("12345", 5);
3143 (* func) ("123", 4);
3144 (* func) ("1234", 5);
3145 (* func) ("12345", 6);
3147 unsigned char buf[512];
3151 while (i < _DBUS_N_ELEMENTS (buf))
3157 while (i < _DBUS_N_ELEMENTS (buf))
3167 * @ingroup DBusStringInternals
3168 * Unit test for DBusString.
3170 * @todo Need to write tests for _dbus_string_copy() and
3171 * _dbus_string_move() moving to/from each of start/middle/end of a
3172 * string. Also need tests for _dbus_string_move_len ()
3174 * @returns #TRUE on success.
3177 _dbus_string_test (void)
3184 int lens[] = { 0, 1, 2, 3, 4, 5, 10, 16, 17, 18, 25, 31, 32, 33, 34, 35, 63, 64, 65, 66, 67, 68, 69, 70, 71, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136 };
3187 const char *valid_paths[] = {
3193 const char *invalid_paths[] = {
3209 const char *valid_interfaces[] = {
3210 "org.freedesktop.Foo",
3212 "Blah.Blah.Blah.Blah.Blah",
3215 "a0.b1.c2.d3.e4.f5.g6",
3218 const char *invalid_interfaces[] = {
3246 const char *valid_base_services[] = {
3255 ":abce.freedesktop.blah"
3257 const char *invalid_base_services[] = {
3271 const char *valid_members[] = {
3279 const char *invalid_members[] = {
3294 const char *valid_signatures[] = {
3301 const char *invalid_signatures[] = {
3303 "not a valid signature",
3310 while (i < _DBUS_N_ELEMENTS (lens))
3312 if (!_dbus_string_init (&str))
3313 _dbus_assert_not_reached ("failed to init string");
3315 set_max_length (&str, lens[i]);
3317 test_max_len (&str, lens[i]);
3318 _dbus_string_free (&str);
3323 /* Test shortening and setting length */
3325 while (i < _DBUS_N_ELEMENTS (lens))
3329 if (!_dbus_string_init (&str))
3330 _dbus_assert_not_reached ("failed to init string");
3332 set_max_length (&str, lens[i]);
3334 if (!_dbus_string_set_length (&str, lens[i]))
3335 _dbus_assert_not_reached ("failed to set string length");
3340 _dbus_assert (_dbus_string_get_length (&str) == j);
3343 _dbus_string_shorten (&str, 1);
3344 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
3349 _dbus_string_free (&str);
3354 /* Test appending data */
3355 if (!_dbus_string_init (&str))
3356 _dbus_assert_not_reached ("failed to init string");
3361 if (!_dbus_string_append (&str, "a"))
3362 _dbus_assert_not_reached ("failed to append string to string\n");
3364 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
3366 if (!_dbus_string_append_byte (&str, 'b'))
3367 _dbus_assert_not_reached ("failed to append byte to string\n");
3369 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
3374 _dbus_string_free (&str);
3376 /* Check steal_data */
3378 if (!_dbus_string_init (&str))
3379 _dbus_assert_not_reached ("failed to init string");
3381 if (!_dbus_string_append (&str, "Hello World"))
3382 _dbus_assert_not_reached ("could not append to string");
3384 i = _dbus_string_get_length (&str);
3386 if (!_dbus_string_steal_data (&str, &s))
3387 _dbus_assert_not_reached ("failed to steal data");
3389 _dbus_assert (_dbus_string_get_length (&str) == 0);
3390 _dbus_assert (((int)strlen (s)) == i);
3396 if (!_dbus_string_append (&str, "Hello World"))
3397 _dbus_assert_not_reached ("could not append to string");
3399 i = _dbus_string_get_length (&str);
3401 if (!_dbus_string_init (&other))
3402 _dbus_assert_not_reached ("could not init string");
3404 if (!_dbus_string_move (&str, 0, &other, 0))
3405 _dbus_assert_not_reached ("could not move");
3407 _dbus_assert (_dbus_string_get_length (&str) == 0);
3408 _dbus_assert (_dbus_string_get_length (&other) == i);
3410 if (!_dbus_string_append (&str, "Hello World"))
3411 _dbus_assert_not_reached ("could not append to string");
3413 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
3414 _dbus_assert_not_reached ("could not move");
3416 _dbus_assert (_dbus_string_get_length (&str) == 0);
3417 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3419 if (!_dbus_string_append (&str, "Hello World"))
3420 _dbus_assert_not_reached ("could not append to string");
3422 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3423 _dbus_assert_not_reached ("could not move");
3425 _dbus_assert (_dbus_string_get_length (&str) == 0);
3426 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3428 _dbus_string_free (&other);
3432 if (!_dbus_string_append (&str, "Hello World"))
3433 _dbus_assert_not_reached ("could not append to string");
3435 i = _dbus_string_get_length (&str);
3437 if (!_dbus_string_init (&other))
3438 _dbus_assert_not_reached ("could not init string");
3440 if (!_dbus_string_copy (&str, 0, &other, 0))
3441 _dbus_assert_not_reached ("could not copy");
3443 _dbus_assert (_dbus_string_get_length (&str) == i);
3444 _dbus_assert (_dbus_string_get_length (&other) == i);
3446 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
3447 _dbus_assert_not_reached ("could not copy");
3449 _dbus_assert (_dbus_string_get_length (&str) == i);
3450 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3451 _dbus_assert (_dbus_string_equal_c_str (&other,
3452 "Hello WorldHello World"));
3454 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3455 _dbus_assert_not_reached ("could not copy");
3457 _dbus_assert (_dbus_string_get_length (&str) == i);
3458 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3459 _dbus_assert (_dbus_string_equal_c_str (&other,
3460 "Hello WorldHello WorldHello World"));
3462 _dbus_string_free (&str);
3463 _dbus_string_free (&other);
3467 if (!_dbus_string_init (&str))
3468 _dbus_assert_not_reached ("failed to init string");
3470 if (!_dbus_string_append (&str, "Hello World"))
3471 _dbus_assert_not_reached ("could not append to string");
3473 i = _dbus_string_get_length (&str);
3475 if (!_dbus_string_init (&other))
3476 _dbus_assert_not_reached ("could not init string");
3478 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3479 &other, 0, _dbus_string_get_length (&other)))
3480 _dbus_assert_not_reached ("could not replace");
3482 _dbus_assert (_dbus_string_get_length (&str) == i);
3483 _dbus_assert (_dbus_string_get_length (&other) == i);
3484 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
3486 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3488 _dbus_assert_not_reached ("could not replace center space");
3490 _dbus_assert (_dbus_string_get_length (&str) == i);
3491 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3492 _dbus_assert (_dbus_string_equal_c_str (&other,
3493 "HelloHello WorldWorld"));
3496 if (!_dbus_string_replace_len (&str, 1, 1,
3498 _dbus_string_get_length (&other) - 1,
3500 _dbus_assert_not_reached ("could not replace end character");
3502 _dbus_assert (_dbus_string_get_length (&str) == i);
3503 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3504 _dbus_assert (_dbus_string_equal_c_str (&other,
3505 "HelloHello WorldWorle"));
3507 _dbus_string_free (&str);
3508 _dbus_string_free (&other);
3510 /* Check append/get unichar */
3512 if (!_dbus_string_init (&str))
3513 _dbus_assert_not_reached ("failed to init string");
3516 if (!_dbus_string_append_unichar (&str, 0xfffc))
3517 _dbus_assert_not_reached ("failed to append unichar");
3519 _dbus_string_get_unichar (&str, 0, &ch, &i);
3521 _dbus_assert (ch == 0xfffc);
3522 _dbus_assert (i == _dbus_string_get_length (&str));
3524 _dbus_string_free (&str);
3526 /* Check insert/set/get byte */
3528 if (!_dbus_string_init (&str))
3529 _dbus_assert_not_reached ("failed to init string");
3531 if (!_dbus_string_append (&str, "Hello"))
3532 _dbus_assert_not_reached ("failed to append Hello");
3534 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
3535 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
3536 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
3537 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
3538 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
3540 _dbus_string_set_byte (&str, 1, 'q');
3541 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
3543 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
3544 _dbus_assert_not_reached ("can't insert byte");
3546 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
3547 _dbus_assert_not_reached ("can't insert byte");
3549 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
3550 _dbus_assert_not_reached ("can't insert byte");
3552 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
3553 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
3554 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
3555 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
3556 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
3557 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
3558 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
3559 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
3560 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
3561 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
3562 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
3564 _dbus_string_free (&str);
3566 /* Check append/parse int/double */
3568 if (!_dbus_string_init (&str))
3569 _dbus_assert_not_reached ("failed to init string");
3571 if (!_dbus_string_append_int (&str, 27))
3572 _dbus_assert_not_reached ("failed to append int");
3574 i = _dbus_string_get_length (&str);
3576 if (!_dbus_string_parse_int (&str, 0, &v, &end))
3577 _dbus_assert_not_reached ("failed to parse int");
3579 _dbus_assert (v == 27);
3580 _dbus_assert (end == i);
3582 _dbus_string_free (&str);
3584 if (!_dbus_string_init (&str))
3585 _dbus_assert_not_reached ("failed to init string");
3587 if (!_dbus_string_append_double (&str, 50.3))
3588 _dbus_assert_not_reached ("failed to append float");
3590 i = _dbus_string_get_length (&str);
3592 if (!_dbus_string_parse_double (&str, 0, &d, &end))
3593 _dbus_assert_not_reached ("failed to parse float");
3595 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
3596 _dbus_assert (end == i);
3598 _dbus_string_free (&str);
3601 if (!_dbus_string_init (&str))
3602 _dbus_assert_not_reached ("failed to init string");
3604 if (!_dbus_string_append (&str, "Hello"))
3605 _dbus_assert_not_reached ("couldn't append to string");
3607 if (!_dbus_string_find (&str, 0, "He", &i))
3608 _dbus_assert_not_reached ("didn't find 'He'");
3609 _dbus_assert (i == 0);
3611 if (!_dbus_string_find (&str, 0, "Hello", &i))
3612 _dbus_assert_not_reached ("didn't find 'Hello'");
3613 _dbus_assert (i == 0);
3615 if (!_dbus_string_find (&str, 0, "ello", &i))
3616 _dbus_assert_not_reached ("didn't find 'ello'");
3617 _dbus_assert (i == 1);
3619 if (!_dbus_string_find (&str, 0, "lo", &i))
3620 _dbus_assert_not_reached ("didn't find 'lo'");
3621 _dbus_assert (i == 3);
3623 if (!_dbus_string_find (&str, 2, "lo", &i))
3624 _dbus_assert_not_reached ("didn't find 'lo'");
3625 _dbus_assert (i == 3);
3627 if (_dbus_string_find (&str, 4, "lo", &i))
3628 _dbus_assert_not_reached ("did find 'lo'");
3630 if (!_dbus_string_find (&str, 0, "l", &i))
3631 _dbus_assert_not_reached ("didn't find 'l'");
3632 _dbus_assert (i == 2);
3634 if (!_dbus_string_find (&str, 0, "H", &i))
3635 _dbus_assert_not_reached ("didn't find 'H'");
3636 _dbus_assert (i == 0);
3638 if (!_dbus_string_find (&str, 0, "", &i))
3639 _dbus_assert_not_reached ("didn't find ''");
3640 _dbus_assert (i == 0);
3642 if (_dbus_string_find (&str, 0, "Hello!", NULL))
3643 _dbus_assert_not_reached ("Did find 'Hello!'");
3645 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
3646 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
3648 if (_dbus_string_find (&str, 0, "ill", NULL))
3649 _dbus_assert_not_reached ("Did find 'ill'");
3651 if (_dbus_string_find (&str, 0, "q", NULL))
3652 _dbus_assert_not_reached ("Did find 'q'");
3654 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
3655 _dbus_assert_not_reached ("Didn't find 'He'");
3657 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
3658 _dbus_assert_not_reached ("Did find 'Hello'");
3660 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
3661 _dbus_assert_not_reached ("Did not find 'H'");
3662 _dbus_assert (i == 0);
3664 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
3665 _dbus_assert_not_reached ("Did not find 'o'");
3666 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
3668 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
3669 _dbus_assert_not_reached ("Did find 'o'");
3670 _dbus_assert (i == -1);
3672 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
3673 _dbus_assert_not_reached ("Did find 'e'");
3674 _dbus_assert (i == -1);
3676 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
3677 _dbus_assert_not_reached ("Didn't find 'e'");
3678 _dbus_assert (i == 1);
3680 _dbus_string_free (&str);
3683 _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
3684 if (!_dbus_string_init (&other))
3685 _dbus_assert_not_reached ("could not init string");
3687 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
3688 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
3690 _dbus_assert (end == 8);
3692 _dbus_string_free (&other);
3694 test_roundtrips (test_hex_roundtrip);
3696 /* Path validation */
3698 while (i < (int) _DBUS_N_ELEMENTS (valid_paths))
3700 _dbus_string_init_const (&str, valid_paths[i]);
3702 if (!_dbus_string_validate_path (&str, 0,
3703 _dbus_string_get_length (&str)))
3705 _dbus_warn ("Path \"%s\" should have been valid\n", valid_paths[i]);
3706 _dbus_assert_not_reached ("invalid path");
3713 while (i < (int) _DBUS_N_ELEMENTS (invalid_paths))
3715 _dbus_string_init_const (&str, invalid_paths[i]);
3717 if (_dbus_string_validate_path (&str, 0,
3718 _dbus_string_get_length (&str)))
3720 _dbus_warn ("Path \"%s\" should have been invalid\n", invalid_paths[i]);
3721 _dbus_assert_not_reached ("valid path");
3727 /* Interface validation */
3729 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
3731 _dbus_string_init_const (&str, valid_interfaces[i]);
3733 if (!_dbus_string_validate_interface (&str, 0,
3734 _dbus_string_get_length (&str)))
3736 _dbus_warn ("Interface \"%s\" should have been valid\n", valid_interfaces[i]);
3737 _dbus_assert_not_reached ("invalid interface");
3744 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
3746 _dbus_string_init_const (&str, invalid_interfaces[i]);
3748 if (_dbus_string_validate_interface (&str, 0,
3749 _dbus_string_get_length (&str)))
3751 _dbus_warn ("Interface \"%s\" should have been invalid\n", invalid_interfaces[i]);
3752 _dbus_assert_not_reached ("valid interface");
3758 /* Service validation (check that valid interfaces are valid services,
3759 * and invalid interfaces are invalid services except if they start with ':')
3762 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
3764 _dbus_string_init_const (&str, valid_interfaces[i]);
3766 if (!_dbus_string_validate_service (&str, 0,
3767 _dbus_string_get_length (&str)))
3769 _dbus_warn ("Service \"%s\" should have been valid\n", valid_interfaces[i]);
3770 _dbus_assert_not_reached ("invalid service");
3777 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
3779 if (invalid_interfaces[i][0] != ':')
3781 _dbus_string_init_const (&str, invalid_interfaces[i]);
3783 if (_dbus_string_validate_service (&str, 0,
3784 _dbus_string_get_length (&str)))
3786 _dbus_warn ("Service \"%s\" should have been invalid\n", invalid_interfaces[i]);
3787 _dbus_assert_not_reached ("valid service");
3794 /* Base service validation */
3796 while (i < (int) _DBUS_N_ELEMENTS (valid_base_services))
3798 _dbus_string_init_const (&str, valid_base_services[i]);
3800 if (!_dbus_string_validate_service (&str, 0,
3801 _dbus_string_get_length (&str)))
3803 _dbus_warn ("Service \"%s\" should have been valid\n", valid_base_services[i]);
3804 _dbus_assert_not_reached ("invalid base service");
3811 while (i < (int) _DBUS_N_ELEMENTS (invalid_base_services))
3813 _dbus_string_init_const (&str, invalid_base_services[i]);
3815 if (_dbus_string_validate_service (&str, 0,
3816 _dbus_string_get_length (&str)))
3818 _dbus_warn ("Service \"%s\" should have been invalid\n", invalid_base_services[i]);
3819 _dbus_assert_not_reached ("valid base service");
3826 /* Error name validation (currently identical to interfaces)
3829 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
3831 _dbus_string_init_const (&str, valid_interfaces[i]);
3833 if (!_dbus_string_validate_error_name (&str, 0,
3834 _dbus_string_get_length (&str)))
3836 _dbus_warn ("Error name \"%s\" should have been valid\n", valid_interfaces[i]);
3837 _dbus_assert_not_reached ("invalid error name");
3844 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
3846 if (invalid_interfaces[i][0] != ':')
3848 _dbus_string_init_const (&str, invalid_interfaces[i]);
3850 if (_dbus_string_validate_error_name (&str, 0,
3851 _dbus_string_get_length (&str)))
3853 _dbus_warn ("Error name \"%s\" should have been invalid\n", invalid_interfaces[i]);
3854 _dbus_assert_not_reached ("valid error name");
3861 /* Member validation */
3863 while (i < (int) _DBUS_N_ELEMENTS (valid_members))
3865 _dbus_string_init_const (&str, valid_members[i]);
3867 if (!_dbus_string_validate_member (&str, 0,
3868 _dbus_string_get_length (&str)))
3870 _dbus_warn ("Member \"%s\" should have been valid\n", valid_members[i]);
3871 _dbus_assert_not_reached ("invalid member");
3878 while (i < (int) _DBUS_N_ELEMENTS (invalid_members))
3880 _dbus_string_init_const (&str, invalid_members[i]);
3882 if (_dbus_string_validate_member (&str, 0,
3883 _dbus_string_get_length (&str)))
3885 _dbus_warn ("Member \"%s\" should have been invalid\n", invalid_members[i]);
3886 _dbus_assert_not_reached ("valid member");
3892 /* Signature validation */
3894 while (i < (int) _DBUS_N_ELEMENTS (valid_signatures))
3896 _dbus_string_init_const (&str, valid_signatures[i]);
3898 if (!_dbus_string_validate_signature (&str, 0,
3899 _dbus_string_get_length (&str)))
3901 _dbus_warn ("Signature \"%s\" should have been valid\n", valid_signatures[i]);
3902 _dbus_assert_not_reached ("invalid signature");
3909 while (i < (int) _DBUS_N_ELEMENTS (invalid_signatures))
3911 _dbus_string_init_const (&str, invalid_signatures[i]);
3913 if (_dbus_string_validate_signature (&str, 0,
3914 _dbus_string_get_length (&str)))
3916 _dbus_warn ("Signature \"%s\" should have been invalid\n", invalid_signatures[i]);
3917 _dbus_assert_not_reached ("valid signature");
3923 /* Validate claimed length longer than real length */
3924 _dbus_string_init_const (&str, "abc.efg");
3925 if (_dbus_string_validate_service (&str, 0, 8))
3926 _dbus_assert_not_reached ("validated too-long string");
3927 if (_dbus_string_validate_interface (&str, 0, 8))
3928 _dbus_assert_not_reached ("validated too-long string");
3929 if (_dbus_string_validate_error_name (&str, 0, 8))
3930 _dbus_assert_not_reached ("validated too-long string");
3932 _dbus_string_init_const (&str, "abc");
3933 if (_dbus_string_validate_member (&str, 0, 4))
3934 _dbus_assert_not_reached ("validated too-long string");
3936 _dbus_string_init_const (&str, "sss");
3937 if (_dbus_string_validate_signature (&str, 0, 4))
3938 _dbus_assert_not_reached ("validated too-long signature");
3940 /* Validate string exceeding max name length */
3941 if (!_dbus_string_init (&str))
3942 _dbus_assert_not_reached ("no memory");
3944 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
3945 if (!_dbus_string_append (&str, "abc.def"))
3946 _dbus_assert_not_reached ("no memory");
3948 if (_dbus_string_validate_service (&str, 0, _dbus_string_get_length (&str)))
3949 _dbus_assert_not_reached ("validated overmax string");
3950 if (_dbus_string_validate_interface (&str, 0, _dbus_string_get_length (&str)))
3951 _dbus_assert_not_reached ("validated overmax string");
3952 if (_dbus_string_validate_error_name (&str, 0, _dbus_string_get_length (&str)))
3953 _dbus_assert_not_reached ("validated overmax string");
3955 /* overlong member */
3956 _dbus_string_set_length (&str, 0);
3957 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
3958 if (!_dbus_string_append (&str, "abc"))
3959 _dbus_assert_not_reached ("no memory");
3961 if (_dbus_string_validate_member (&str, 0, _dbus_string_get_length (&str)))
3962 _dbus_assert_not_reached ("validated overmax string");
3964 /* overlong base service */
3965 _dbus_string_set_length (&str, 0);
3966 _dbus_string_append (&str, ":");
3967 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
3968 if (!_dbus_string_append (&str, "abc"))
3969 _dbus_assert_not_reached ("no memory");
3971 if (_dbus_string_validate_service (&str, 0, _dbus_string_get_length (&str)))
3972 _dbus_assert_not_reached ("validated overmax string");
3974 _dbus_string_free (&str);
3979 #endif /* DBUS_BUILD_TESTS */