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 * Inserts a single byte at the given position.
617 * @param str the string
618 * @param i the position
619 * @param byte the value to insert
620 * @returns #TRUE on success
623 _dbus_string_insert_byte (DBusString *str,
627 DBUS_STRING_PREAMBLE (str);
628 _dbus_assert (i <= real->len);
629 _dbus_assert (i >= 0);
631 if (!open_gap (1, real, i))
640 * Like _dbus_string_get_data(), but removes the
641 * gotten data from the original string. The caller
642 * must free the data returned. This function may
643 * fail due to lack of memory, and return #FALSE.
645 * @param str the string
646 * @param data_return location to return the buffer
647 * @returns #TRUE on success
650 _dbus_string_steal_data (DBusString *str,
654 DBUS_STRING_PREAMBLE (str);
655 _dbus_assert (data_return != NULL);
657 undo_alignment (real);
659 *data_return = real->str;
661 old_max_length = real->max_length;
663 /* reset the string */
664 if (!_dbus_string_init (str))
666 /* hrm, put it back then */
667 real->str = *data_return;
669 fixup_alignment (real);
673 real->max_length = old_max_length;
679 * Like _dbus_string_get_data_len(), but removes the gotten data from
680 * the original string. The caller must free the data returned. This
681 * function may fail due to lack of memory, and return #FALSE.
682 * The returned string is nul-terminated and has length len.
684 * @todo this function is broken because on failure it
685 * may corrupt the source string.
687 * @param str the string
688 * @param data_return location to return the buffer
689 * @param start the start of segment to steal
690 * @param len the length of segment to steal
691 * @returns #TRUE on success
694 _dbus_string_steal_data_len (DBusString *str,
700 DBUS_STRING_PREAMBLE (str);
701 _dbus_assert (data_return != NULL);
702 _dbus_assert (start >= 0);
703 _dbus_assert (len >= 0);
704 _dbus_assert (start <= real->len);
705 _dbus_assert (len <= real->len - start);
707 if (!_dbus_string_init (&dest))
710 set_max_length (&dest, real->max_length);
712 if (!_dbus_string_move_len (str, start, len, &dest, 0))
714 _dbus_string_free (&dest);
718 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
719 if (!_dbus_string_steal_data (&dest, data_return))
721 _dbus_string_free (&dest);
725 _dbus_string_free (&dest);
731 * Copies the data from the string into a char*
733 * @param str the string
734 * @param data_return place to return the data
735 * @returns #TRUE on success, #FALSE on no memory
738 _dbus_string_copy_data (const DBusString *str,
741 DBUS_CONST_STRING_PREAMBLE (str);
742 _dbus_assert (data_return != NULL);
744 *data_return = dbus_malloc (real->len + 1);
745 if (*data_return == NULL)
748 memcpy (*data_return, real->str, real->len + 1);
754 * Copies a segment of the string into a char*
756 * @param str the string
757 * @param data_return place to return the data
758 * @param start start index
759 * @param len length to copy
760 * @returns #FALSE if no memory
763 _dbus_string_copy_data_len (const DBusString *str,
770 DBUS_CONST_STRING_PREAMBLE (str);
771 _dbus_assert (data_return != NULL);
772 _dbus_assert (start >= 0);
773 _dbus_assert (len >= 0);
774 _dbus_assert (start <= real->len);
775 _dbus_assert (len <= real->len - start);
777 if (!_dbus_string_init (&dest))
780 set_max_length (&dest, real->max_length);
782 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
784 _dbus_string_free (&dest);
788 if (!_dbus_string_steal_data (&dest, data_return))
790 _dbus_string_free (&dest);
794 _dbus_string_free (&dest);
799 * Copies the contents of a DBusString into a different
800 * buffer. The resulting buffer will be nul-terminated.
802 * @param str a string
803 * @param buffer a C buffer to copy data to
804 * @param len maximum length of C buffer
807 _dbus_string_copy_to_buffer (const DBusString *str,
812 DBUS_CONST_STRING_PREAMBLE (str);
814 _dbus_assert (avail_len >= 0);
816 copy_len = MIN (avail_len, real->len+1);
817 memcpy (buffer, real->str, copy_len);
818 if (avail_len > 0 && avail_len == copy_len)
819 buffer[avail_len-1] = '\0';
822 /* Only have the function if we don't have the macro */
823 #ifndef _dbus_string_get_length
825 * Gets the length of a string (not including nul termination).
827 * @returns the length.
830 _dbus_string_get_length (const DBusString *str)
832 DBUS_CONST_STRING_PREAMBLE (str);
836 #endif /* !_dbus_string_get_length */
839 * Makes a string longer by the given number of bytes. Checks whether
840 * adding additional_length to the current length would overflow an
841 * integer, and checks for exceeding a string's max length.
842 * The new bytes are not initialized, other than nul-terminating
843 * the end of the string. The uninitialized bytes may contain
844 * nul bytes or other junk.
846 * @param str a string
847 * @param additional_length length to add to the string.
848 * @returns #TRUE on success.
851 _dbus_string_lengthen (DBusString *str,
852 int additional_length)
854 DBUS_STRING_PREAMBLE (str);
855 _dbus_assert (additional_length >= 0);
857 if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
858 return FALSE; /* would overflow */
860 return set_length (real,
861 real->len + additional_length);
865 * Makes a string shorter by the given number of bytes.
867 * @param str a string
868 * @param length_to_remove length to remove from the string.
871 _dbus_string_shorten (DBusString *str,
872 int length_to_remove)
874 DBUS_STRING_PREAMBLE (str);
875 _dbus_assert (length_to_remove >= 0);
876 _dbus_assert (length_to_remove <= real->len);
879 real->len - length_to_remove);
883 * Sets the length of a string. Can be used to truncate or lengthen
884 * the string. If the string is lengthened, the function may fail and
885 * return #FALSE. Newly-added bytes are not initialized, as with
886 * _dbus_string_lengthen().
888 * @param str a string
889 * @param length new length of the string.
890 * @returns #FALSE on failure.
893 _dbus_string_set_length (DBusString *str,
896 DBUS_STRING_PREAMBLE (str);
897 _dbus_assert (length >= 0);
899 return set_length (real, length);
903 align_insert_point_then_open_gap (DBusString *str,
908 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
909 unsigned long gap_pos;
912 DBUS_STRING_PREAMBLE (str);
913 _dbus_assert (alignment >= 1);
914 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
916 insert_at = *insert_at_p;
918 _dbus_assert (insert_at <= real->len);
920 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
921 new_len = real->len + (gap_pos - insert_at) + gap_size;
923 if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length))
926 delta = new_len - real->len;
927 _dbus_assert (delta >= 0);
929 if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
931 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
935 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
939 /* nul the padding if we had to add any padding */
940 if (gap_size < delta)
942 memset (&real->str[insert_at], '\0',
943 gap_pos - insert_at);
946 *insert_at_p = gap_pos;
952 align_length_then_lengthen (DBusString *str,
954 int then_lengthen_by)
958 insert_at = _dbus_string_get_length (str);
960 return align_insert_point_then_open_gap (str,
962 alignment, then_lengthen_by);
966 * Align the length of a string to a specific alignment (typically 4 or 8)
967 * by appending nul bytes to the string.
969 * @param str a string
970 * @param alignment the alignment
971 * @returns #FALSE if no memory
974 _dbus_string_align_length (DBusString *str,
977 return align_length_then_lengthen (str, alignment, 0);
981 * Preallocate extra_bytes such that a future lengthening of the
982 * string by extra_bytes is guaranteed to succeed without an out of
985 * @param str a string
986 * @param extra_bytes bytes to alloc
987 * @returns #FALSE if no memory
990 _dbus_string_alloc_space (DBusString *str,
993 if (!_dbus_string_lengthen (str, extra_bytes))
995 _dbus_string_shorten (str, extra_bytes);
1001 append (DBusRealString *real,
1005 if (buffer_len == 0)
1008 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
1011 memcpy (real->str + (real->len - buffer_len),
1019 * Appends a nul-terminated C-style string to a DBusString.
1021 * @param str the DBusString
1022 * @param buffer the nul-terminated characters to append
1023 * @returns #FALSE if not enough memory.
1026 _dbus_string_append (DBusString *str,
1029 unsigned long buffer_len;
1031 DBUS_STRING_PREAMBLE (str);
1032 _dbus_assert (buffer != NULL);
1034 buffer_len = strlen (buffer);
1035 if (buffer_len > (unsigned long) real->max_length)
1038 return append (real, buffer, buffer_len);
1041 #define ASSIGN_4_OCTETS(p, octets) \
1042 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
1044 #ifdef DBUS_HAVE_INT64
1045 #define ASSIGN_8_OCTETS(p, octets) \
1046 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
1048 #define ASSIGN_8_OCTETS(p, octets) \
1062 _dbus_assert (b == p + 8); \
1064 #endif /* DBUS_HAVE_INT64 */
1067 * Appends 4 bytes aligned on a 4 byte boundary
1068 * with any alignment padding initialized to 0.
1070 * @param str the DBusString
1071 * @param octets 4 bytes to append
1072 * @returns #FALSE if not enough memory.
1075 _dbus_string_append_4_aligned (DBusString *str,
1076 const unsigned char octets[4])
1078 DBUS_STRING_PREAMBLE (str);
1080 if (!align_length_then_lengthen (str, 4, 4))
1083 ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
1089 * Appends 8 bytes aligned on an 8 byte boundary
1090 * with any alignment padding initialized to 0.
1092 * @param str the DBusString
1093 * @param octets 8 bytes to append
1094 * @returns #FALSE if not enough memory.
1097 _dbus_string_append_8_aligned (DBusString *str,
1098 const unsigned char octets[8])
1100 DBUS_STRING_PREAMBLE (str);
1102 if (!align_length_then_lengthen (str, 8, 8))
1105 ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
1111 * Inserts 4 bytes aligned on a 4 byte boundary
1112 * with any alignment padding initialized to 0.
1114 * @param str the DBusString
1115 * @param octets 4 bytes to insert
1116 * @returns #FALSE if not enough memory.
1119 _dbus_string_insert_4_aligned (DBusString *str,
1121 const unsigned char octets[4])
1123 DBUS_STRING_PREAMBLE (str);
1125 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1128 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1134 * Inserts 8 bytes aligned on an 8 byte boundary
1135 * with any alignment padding initialized to 0.
1137 * @param str the DBusString
1138 * @param octets 8 bytes to insert
1139 * @returns #FALSE if not enough memory.
1142 _dbus_string_insert_8_aligned (DBusString *str,
1144 const unsigned char octets[8])
1146 DBUS_STRING_PREAMBLE (str);
1148 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1151 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1153 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1159 * Appends a printf-style formatted string
1160 * to the #DBusString.
1162 * @param str the string
1163 * @param format printf format
1164 * @param args variable argument list
1165 * @returns #FALSE if no memory
1168 _dbus_string_append_printf_valist (DBusString *str,
1176 DBUS_STRING_PREAMBLE (str);
1178 DBUS_VA_COPY (args_copy, args);
1180 /* Measure the message length without terminating nul */
1181 len = vsnprintf (&c, 1, format, args);
1183 if (!_dbus_string_lengthen (str, len))
1185 /* don't leak the copy */
1190 vsprintf (real->str + (real->len - len),
1199 * Appends a printf-style formatted string
1200 * to the #DBusString.
1202 * @param str the string
1203 * @param format printf format
1204 * @returns #FALSE if no memory
1207 _dbus_string_append_printf (DBusString *str,
1214 va_start (args, format);
1215 retval = _dbus_string_append_printf_valist (str, format, args);
1222 * Appends block of bytes with the given length to a DBusString.
1224 * @param str the DBusString
1225 * @param buffer the bytes to append
1226 * @param len the number of bytes to append
1227 * @returns #FALSE if not enough memory.
1230 _dbus_string_append_len (DBusString *str,
1234 DBUS_STRING_PREAMBLE (str);
1235 _dbus_assert (buffer != NULL);
1236 _dbus_assert (len >= 0);
1238 return append (real, buffer, len);
1242 * Appends a single byte to the string, returning #FALSE
1243 * if not enough memory.
1245 * @param str the string
1246 * @param byte the byte to append
1247 * @returns #TRUE on success
1250 _dbus_string_append_byte (DBusString *str,
1253 DBUS_STRING_PREAMBLE (str);
1255 if (!set_length (real, real->len + 1))
1258 real->str[real->len-1] = byte;
1264 * Appends a single Unicode character, encoding the character
1267 * @param str the string
1268 * @param ch the Unicode character
1271 _dbus_string_append_unichar (DBusString *str,
1279 DBUS_STRING_PREAMBLE (str);
1281 /* this code is from GLib but is pretty standard I think */
1290 else if (ch < 0x800)
1295 else if (ch < 0x10000)
1300 else if (ch < 0x200000)
1305 else if (ch < 0x4000000)
1316 if (len > (real->max_length - real->len))
1317 return FALSE; /* real->len + len would overflow */
1319 if (!set_length (real, real->len + len))
1322 out = real->str + (real->len - len);
1324 for (i = len - 1; i > 0; --i)
1326 out[i] = (ch & 0x3f) | 0x80;
1329 out[0] = ch | first;
1335 delete (DBusRealString *real,
1342 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1344 real->str[real->len] = '\0';
1348 * Deletes a segment of a DBusString with length len starting at
1349 * start. (Hint: to clear an entire string, setting length to 0
1350 * with _dbus_string_set_length() is easier.)
1352 * @param str the DBusString
1353 * @param start where to start deleting
1354 * @param len the number of bytes to delete
1357 _dbus_string_delete (DBusString *str,
1361 DBUS_STRING_PREAMBLE (str);
1362 _dbus_assert (start >= 0);
1363 _dbus_assert (len >= 0);
1364 _dbus_assert (start <= real->len);
1365 _dbus_assert (len <= real->len - start);
1367 delete (real, start, len);
1371 copy (DBusRealString *source,
1374 DBusRealString *dest,
1380 if (!open_gap (len, dest, insert_at))
1383 memcpy (dest->str + insert_at,
1384 source->str + start,
1391 * Checks assertions for two strings we're copying a segment between,
1392 * and declares real_source/real_dest variables.
1394 * @param source the source string
1395 * @param start the starting offset
1396 * @param dest the dest string
1397 * @param insert_at where the copied segment is inserted
1399 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1400 DBusRealString *real_source = (DBusRealString*) source; \
1401 DBusRealString *real_dest = (DBusRealString*) dest; \
1402 _dbus_assert ((source) != (dest)); \
1403 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1404 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1405 _dbus_assert (!real_dest->constant); \
1406 _dbus_assert (!real_dest->locked); \
1407 _dbus_assert ((start) >= 0); \
1408 _dbus_assert ((start) <= real_source->len); \
1409 _dbus_assert ((insert_at) >= 0); \
1410 _dbus_assert ((insert_at) <= real_dest->len)
1413 * Moves the end of one string into another string. Both strings
1414 * must be initialized, valid strings.
1416 * @param source the source string
1417 * @param start where to chop off the source string
1418 * @param dest the destination string
1419 * @param insert_at where to move the chopped-off part of source string
1420 * @returns #FALSE if not enough memory
1423 _dbus_string_move (DBusString *source,
1428 DBusRealString *real_source = (DBusRealString*) source;
1429 _dbus_assert (start <= real_source->len);
1431 return _dbus_string_move_len (source, start,
1432 real_source->len - start,
1437 * Like _dbus_string_move(), but does not delete the section
1438 * of the source string that's copied to the dest string.
1440 * @param source the source string
1441 * @param start where to start copying the source string
1442 * @param dest the destination string
1443 * @param insert_at where to place the copied part of source string
1444 * @returns #FALSE if not enough memory
1447 _dbus_string_copy (const DBusString *source,
1452 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1454 return copy (real_source, start,
1455 real_source->len - start,
1461 * Like _dbus_string_move(), but can move a segment from
1462 * the middle of the source string.
1464 * @todo this doesn't do anything with max_length field.
1465 * we should probably just kill the max_length field though.
1467 * @param source the source string
1468 * @param start first byte of source string to move
1469 * @param len length of segment to move
1470 * @param dest the destination string
1471 * @param insert_at where to move the bytes from the source string
1472 * @returns #FALSE if not enough memory
1475 _dbus_string_move_len (DBusString *source,
1482 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1483 _dbus_assert (len >= 0);
1484 _dbus_assert ((start + len) <= real_source->len);
1491 else if (start == 0 &&
1492 len == real_source->len &&
1493 real_dest->len == 0)
1495 /* Short-circuit moving an entire existing string to an empty string
1496 * by just swapping the buffers.
1498 /* we assume ->constant doesn't matter as you can't have
1499 * a constant string involved in a move.
1501 #define ASSIGN_DATA(a, b) do { \
1502 (a)->str = (b)->str; \
1503 (a)->len = (b)->len; \
1504 (a)->allocated = (b)->allocated; \
1505 (a)->align_offset = (b)->align_offset; \
1510 ASSIGN_DATA (&tmp, real_source);
1511 ASSIGN_DATA (real_source, real_dest);
1512 ASSIGN_DATA (real_dest, &tmp);
1518 if (!copy (real_source, start, len,
1523 delete (real_source, start,
1531 * Like _dbus_string_copy(), but can copy a segment from the middle of
1532 * the source string.
1534 * @param source the source string
1535 * @param start where to start copying the source string
1536 * @param len length of segment to copy
1537 * @param dest the destination string
1538 * @param insert_at where to place the copied segment of source string
1539 * @returns #FALSE if not enough memory
1542 _dbus_string_copy_len (const DBusString *source,
1548 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1549 _dbus_assert (len >= 0);
1550 _dbus_assert (start <= real_source->len);
1551 _dbus_assert (len <= real_source->len - start);
1553 return copy (real_source, start, len,
1559 * Replaces a segment of dest string with a segment of source string.
1561 * @todo optimize the case where the two lengths are the same, and
1562 * avoid memmoving the data in the trailing part of the string twice.
1564 * @todo avoid inserting the source into dest, then deleting
1565 * the replaced chunk of dest (which creates a potentially large
1566 * intermediate string). Instead, extend the replaced chunk
1567 * of dest with padding to the same size as the source chunk,
1568 * then copy in the source bytes.
1570 * @param source the source string
1571 * @param start where to start copying the source string
1572 * @param len length of segment to copy
1573 * @param dest the destination string
1574 * @param replace_at start of segment of dest string to replace
1575 * @param replace_len length of segment of dest string to replace
1576 * @returns #FALSE if not enough memory
1580 _dbus_string_replace_len (const DBusString *source,
1587 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1588 _dbus_assert (len >= 0);
1589 _dbus_assert (start <= real_source->len);
1590 _dbus_assert (len <= real_source->len - start);
1591 _dbus_assert (replace_at >= 0);
1592 _dbus_assert (replace_at <= real_dest->len);
1593 _dbus_assert (replace_len <= real_dest->len - replace_at);
1595 if (!copy (real_source, start, len,
1596 real_dest, replace_at))
1599 delete (real_dest, replace_at + len, replace_len);
1604 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1605 * Pennington, and Tom Tromey are the authors and authorized relicense.
1608 /** computes length and mask of a unicode character
1609 * @param Char the char
1610 * @param Mask the mask variable to assign to
1611 * @param Len the length variable to assign to
1613 #define UTF8_COMPUTE(Char, Mask, Len) \
1619 else if ((Char & 0xe0) == 0xc0) \
1624 else if ((Char & 0xf0) == 0xe0) \
1629 else if ((Char & 0xf8) == 0xf0) \
1634 else if ((Char & 0xfc) == 0xf8) \
1639 else if ((Char & 0xfe) == 0xfc) \
1651 * computes length of a unicode character in UTF-8
1652 * @param Char the char
1654 #define UTF8_LENGTH(Char) \
1655 ((Char) < 0x80 ? 1 : \
1656 ((Char) < 0x800 ? 2 : \
1657 ((Char) < 0x10000 ? 3 : \
1658 ((Char) < 0x200000 ? 4 : \
1659 ((Char) < 0x4000000 ? 5 : 6)))))
1662 * Gets a UTF-8 value.
1664 * @param Result variable for extracted unicode char.
1665 * @param Chars the bytes to decode
1666 * @param Count counter variable
1667 * @param Mask mask for this char
1668 * @param Len length for this char in bytes
1670 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1671 (Result) = (Chars)[0] & (Mask); \
1672 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1674 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1680 (Result) |= ((Chars)[(Count)] & 0x3f); \
1684 * Check whether a unicode char is in a valid range.
1686 * @param Char the character
1688 #define UNICODE_VALID(Char) \
1689 ((Char) < 0x110000 && \
1690 (((Char) & 0xFFFFF800) != 0xD800) && \
1691 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1692 ((Char) & 0xFFFF) != 0xFFFF)
1695 * Gets a unicode character from a UTF-8 string. Does no validation;
1696 * you must verify that the string is valid UTF-8 in advance and must
1697 * pass in the start of a character.
1699 * @param str the string
1700 * @param start the start of the UTF-8 character.
1701 * @param ch_return location to return the character
1702 * @param end_return location to return the byte index of next character
1705 _dbus_string_get_unichar (const DBusString *str,
1707 dbus_unichar_t *ch_return,
1711 dbus_unichar_t result;
1714 DBUS_CONST_STRING_PREAMBLE (str);
1715 _dbus_assert (start >= 0);
1716 _dbus_assert (start <= real->len);
1721 *end_return = real->len;
1724 p = real->str + start;
1727 UTF8_COMPUTE (c, mask, len);
1730 UTF8_GET (result, p, i, mask, len);
1732 if (result == (dbus_unichar_t)-1)
1736 *ch_return = result;
1738 *end_return = start + len;
1742 * Finds the given substring in the string,
1743 * returning #TRUE and filling in the byte index
1744 * where the substring was found, if it was found.
1745 * Returns #FALSE if the substring wasn't found.
1746 * Sets *start to the length of the string if the substring
1749 * @param str the string
1750 * @param start where to start looking
1751 * @param substr the substring
1752 * @param found return location for where it was found, or #NULL
1753 * @returns #TRUE if found
1756 _dbus_string_find (const DBusString *str,
1761 return _dbus_string_find_to (str, start,
1762 ((const DBusRealString*)str)->len,
1767 * Finds the given substring in the string,
1768 * up to a certain position,
1769 * returning #TRUE and filling in the byte index
1770 * where the substring was found, if it was found.
1771 * Returns #FALSE if the substring wasn't found.
1772 * Sets *start to the length of the string if the substring
1775 * @param str the string
1776 * @param start where to start looking
1777 * @param end where to stop looking
1778 * @param substr the substring
1779 * @param found return location for where it was found, or #NULL
1780 * @returns #TRUE if found
1783 _dbus_string_find_to (const DBusString *str,
1790 DBUS_CONST_STRING_PREAMBLE (str);
1791 _dbus_assert (substr != NULL);
1792 _dbus_assert (start <= real->len);
1793 _dbus_assert (start >= 0);
1794 _dbus_assert (substr != NULL);
1795 _dbus_assert (end <= real->len);
1796 _dbus_assert (start <= end);
1798 /* we always "find" an empty string */
1799 if (*substr == '\0')
1809 if (real->str[i] == substr[0])
1815 if (substr[j - i] == '\0')
1817 else if (real->str[j] != substr[j - i])
1823 if (substr[j - i] == '\0')
1841 * Find the given byte scanning backward from the given start.
1842 * Sets *found to -1 if the byte is not found.
1844 * @param str the string
1845 * @param start the place to start scanning (will not find the byte at this point)
1846 * @param byte the byte to find
1847 * @param found return location for where it was found
1848 * @returns #TRUE if found
1851 _dbus_string_find_byte_backward (const DBusString *str,
1857 DBUS_CONST_STRING_PREAMBLE (str);
1858 _dbus_assert (start <= real->len);
1859 _dbus_assert (start >= 0);
1860 _dbus_assert (found != NULL);
1865 if (real->str[i] == byte)
1878 * Finds a blank (space or tab) in the string. Returns #TRUE
1879 * if found, #FALSE otherwise. If a blank is not found sets
1880 * *found to the length of the string.
1882 * @param str the string
1883 * @param start byte index to start looking
1884 * @param found place to store the location of the first blank
1885 * @returns #TRUE if a blank was found
1888 _dbus_string_find_blank (const DBusString *str,
1893 DBUS_CONST_STRING_PREAMBLE (str);
1894 _dbus_assert (start <= real->len);
1895 _dbus_assert (start >= 0);
1898 while (i < real->len)
1900 if (real->str[i] == ' ' ||
1901 real->str[i] == '\t')
1918 * Skips blanks from start, storing the first non-blank in *end
1919 * (blank is space or tab).
1921 * @param str the string
1922 * @param start where to start
1923 * @param end where to store the first non-blank byte index
1926 _dbus_string_skip_blank (const DBusString *str,
1931 DBUS_CONST_STRING_PREAMBLE (str);
1932 _dbus_assert (start <= real->len);
1933 _dbus_assert (start >= 0);
1936 while (i < real->len)
1938 if (!(real->str[i] == ' ' ||
1939 real->str[i] == '\t'))
1945 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1946 real->str[i] == '\t'));
1953 * Skips whitespace from start, storing the first non-whitespace in *end.
1954 * (whitespace is space, tab, newline, CR).
1956 * @param str the string
1957 * @param start where to start
1958 * @param end where to store the first non-whitespace byte index
1961 _dbus_string_skip_white (const DBusString *str,
1966 DBUS_CONST_STRING_PREAMBLE (str);
1967 _dbus_assert (start <= real->len);
1968 _dbus_assert (start >= 0);
1971 while (i < real->len)
1973 if (!(real->str[i] == ' ' ||
1974 real->str[i] == '\n' ||
1975 real->str[i] == '\r' ||
1976 real->str[i] == '\t'))
1982 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1983 real->str[i] == '\t'));
1990 * Assigns a newline-terminated or \\r\\n-terminated line from the front
1991 * of the string to the given dest string. The dest string's previous
1992 * contents are deleted. If the source string contains no newline,
1993 * moves the entire source string to the dest string.
1995 * @todo owen correctly notes that this is a stupid function (it was
1996 * written purely for test code,
1997 * e.g. dbus-message-builder.c). Probably should be enforced as test
1998 * code only with #ifdef DBUS_BUILD_TESTS
2000 * @param source the source string
2001 * @param dest the destination string (contents are replaced)
2002 * @returns #FALSE if no memory, or source has length 0
2005 _dbus_string_pop_line (DBusString *source,
2009 dbus_bool_t have_newline;
2011 _dbus_string_set_length (dest, 0);
2014 if (_dbus_string_find (source, 0, "\n", &eol))
2016 have_newline = TRUE;
2017 eol += 1; /* include newline */
2021 eol = _dbus_string_get_length (source);
2022 have_newline = FALSE;
2026 return FALSE; /* eof */
2028 if (!_dbus_string_move_len (source, 0, eol,
2034 /* dump the newline and the \r if we have one */
2037 dbus_bool_t have_cr;
2039 _dbus_assert (_dbus_string_get_length (dest) > 0);
2041 if (_dbus_string_get_length (dest) > 1 &&
2042 _dbus_string_get_byte (dest,
2043 _dbus_string_get_length (dest) - 2) == '\r')
2048 _dbus_string_set_length (dest,
2049 _dbus_string_get_length (dest) -
2057 * Deletes up to and including the first blank space
2060 * @param str the string
2063 _dbus_string_delete_first_word (DBusString *str)
2067 if (_dbus_string_find_blank (str, 0, &i))
2068 _dbus_string_skip_blank (str, i, &i);
2070 _dbus_string_delete (str, 0, i);
2074 * Deletes any leading blanks in the string
2076 * @param str the string
2079 _dbus_string_delete_leading_blanks (DBusString *str)
2083 _dbus_string_skip_blank (str, 0, &i);
2086 _dbus_string_delete (str, 0, i);
2090 * Tests two DBusString for equality.
2092 * @todo memcmp is probably faster
2094 * @param a first string
2095 * @param b second string
2096 * @returns #TRUE if equal
2099 _dbus_string_equal (const DBusString *a,
2100 const DBusString *b)
2102 const unsigned char *ap;
2103 const unsigned char *bp;
2104 const unsigned char *a_end;
2105 const DBusRealString *real_a = (const DBusRealString*) a;
2106 const DBusRealString *real_b = (const DBusRealString*) b;
2107 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2108 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2110 if (real_a->len != real_b->len)
2115 a_end = real_a->str + real_a->len;
2129 * Tests two DBusString for equality up to the given length.
2130 * The strings may be shorter than the given length.
2132 * @todo write a unit test
2134 * @todo memcmp is probably faster
2136 * @param a first string
2137 * @param b second string
2138 * @param len the maximum length to look at
2139 * @returns #TRUE if equal for the given number of bytes
2142 _dbus_string_equal_len (const DBusString *a,
2143 const DBusString *b,
2146 const unsigned char *ap;
2147 const unsigned char *bp;
2148 const unsigned char *a_end;
2149 const DBusRealString *real_a = (const DBusRealString*) a;
2150 const DBusRealString *real_b = (const DBusRealString*) b;
2151 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2152 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2154 if (real_a->len != real_b->len &&
2155 (real_a->len < len || real_b->len < len))
2160 a_end = real_a->str + MIN (real_a->len, len);
2174 * Tests two sub-parts of two DBusString for equality. The specified
2175 * range of the first string must exist; the specified start position
2176 * of the second string must exist.
2178 * @todo write a unit test
2180 * @todo memcmp is probably faster
2182 * @param a first string
2183 * @param a_start where to start substring in first string
2184 * @param a_len length of substring in first string
2185 * @param b second string
2186 * @param b_start where to start substring in second string
2187 * @returns #TRUE if the two substrings are equal
2190 _dbus_string_equal_substring (const DBusString *a,
2193 const DBusString *b,
2196 const unsigned char *ap;
2197 const unsigned char *bp;
2198 const unsigned char *a_end;
2199 const DBusRealString *real_a = (const DBusRealString*) a;
2200 const DBusRealString *real_b = (const DBusRealString*) b;
2201 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2202 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2203 _dbus_assert (a_start >= 0);
2204 _dbus_assert (a_len >= 0);
2205 _dbus_assert (a_start <= real_a->len);
2206 _dbus_assert (a_len <= real_a->len - a_start);
2207 _dbus_assert (b_start >= 0);
2208 _dbus_assert (b_start <= real_b->len);
2210 if (a_len > real_b->len - b_start)
2213 ap = real_a->str + a_start;
2214 bp = real_b->str + b_start;
2225 _dbus_assert (bp <= (real_b->str + real_b->len));
2231 * Checks whether a string is equal to a C string.
2233 * @param a the string
2234 * @param c_str the C string
2235 * @returns #TRUE if equal
2238 _dbus_string_equal_c_str (const DBusString *a,
2241 const unsigned char *ap;
2242 const unsigned char *bp;
2243 const unsigned char *a_end;
2244 const DBusRealString *real_a = (const DBusRealString*) a;
2245 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2246 _dbus_assert (c_str != NULL);
2249 bp = (const unsigned char*) c_str;
2250 a_end = real_a->str + real_a->len;
2251 while (ap != a_end && *bp)
2260 if (ap != a_end || *bp)
2267 * Checks whether a string starts with the given C string.
2269 * @param a the string
2270 * @param c_str the C string
2271 * @returns #TRUE if string starts with it
2274 _dbus_string_starts_with_c_str (const DBusString *a,
2277 const unsigned char *ap;
2278 const unsigned char *bp;
2279 const unsigned char *a_end;
2280 const DBusRealString *real_a = (const DBusRealString*) a;
2281 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2282 _dbus_assert (c_str != NULL);
2285 bp = (const unsigned char*) c_str;
2286 a_end = real_a->str + real_a->len;
2287 while (ap != a_end && *bp)
2303 * Returns whether a string ends with the given suffix
2305 * @todo memcmp might make this faster.
2307 * @param a the string
2308 * @param c_str the C-style string
2309 * @returns #TRUE if the string ends with the suffix
2312 _dbus_string_ends_with_c_str (const DBusString *a,
2315 const unsigned char *ap;
2316 const unsigned char *bp;
2317 const unsigned char *a_end;
2318 unsigned long c_str_len;
2319 const DBusRealString *real_a = (const DBusRealString*) a;
2320 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2321 _dbus_assert (c_str != NULL);
2323 c_str_len = strlen (c_str);
2324 if (((unsigned long)real_a->len) < c_str_len)
2327 ap = real_a->str + (real_a->len - c_str_len);
2328 bp = (const unsigned char*) c_str;
2329 a_end = real_a->str + real_a->len;
2339 _dbus_assert (*ap == '\0');
2340 _dbus_assert (*bp == '\0');
2346 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2347 * encoded. (Each byte is two hex digits.)
2349 * @param source the string to encode
2350 * @param start byte index to start encoding
2351 * @param dest string where encoded data should be placed
2352 * @param insert_at where to place encoded data
2353 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2356 _dbus_string_hex_encode (const DBusString *source,
2362 const char hexdigits[16] = {
2363 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2364 'a', 'b', 'c', 'd', 'e', 'f'
2366 const unsigned char *p;
2367 const unsigned char *end;
2370 _dbus_assert (start <= _dbus_string_get_length (source));
2372 if (!_dbus_string_init (&result))
2377 p = (const unsigned char*) _dbus_string_get_const_data (source);
2378 end = p + _dbus_string_get_length (source);
2383 if (!_dbus_string_append_byte (&result,
2384 hexdigits[(*p >> 4)]))
2387 if (!_dbus_string_append_byte (&result,
2388 hexdigits[(*p & 0x0f)]))
2394 if (!_dbus_string_move (&result, 0, dest, insert_at))
2400 _dbus_string_free (&result);
2405 * Decodes a string from hex encoding.
2407 * @param source the string to decode
2408 * @param start byte index to start decode
2409 * @param end_return return location of the end of the hex data, or #NULL
2410 * @param dest string where decoded data should be placed
2411 * @param insert_at where to place decoded data
2412 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2415 _dbus_string_hex_decode (const DBusString *source,
2422 const unsigned char *p;
2423 const unsigned char *end;
2425 dbus_bool_t high_bits;
2427 _dbus_assert (start <= _dbus_string_get_length (source));
2429 if (!_dbus_string_init (&result))
2435 p = (const unsigned char*) _dbus_string_get_const_data (source);
2436 end = p + _dbus_string_get_length (source);
2505 if (!_dbus_string_append_byte (&result,
2514 len = _dbus_string_get_length (&result);
2516 b = _dbus_string_get_byte (&result, len - 1);
2520 _dbus_string_set_byte (&result, len - 1, b);
2523 high_bits = !high_bits;
2529 if (!_dbus_string_move (&result, 0, dest, insert_at))
2533 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2538 _dbus_string_free (&result);
2543 * Checks that the given range of the string is valid ASCII with no
2544 * nul bytes. If the given range is not entirely contained in the
2545 * string, returns #FALSE.
2547 * @todo this is inconsistent with most of DBusString in that
2548 * it allows a start,len range that extends past the string end.
2550 * @param str the string
2551 * @param start first byte index to check
2552 * @param len number of bytes to check
2553 * @returns #TRUE if the byte range exists and is all valid ASCII
2556 _dbus_string_validate_ascii (const DBusString *str,
2560 const unsigned char *s;
2561 const unsigned char *end;
2562 DBUS_CONST_STRING_PREAMBLE (str);
2563 _dbus_assert (start >= 0);
2564 _dbus_assert (start <= real->len);
2565 _dbus_assert (len >= 0);
2567 if (len > real->len - start)
2570 s = real->str + start;
2574 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2584 * Checks that the given range of the string is valid UTF-8. If the
2585 * given range is not entirely contained in the string, returns
2586 * #FALSE. If the string contains any nul bytes in the given range,
2587 * returns #FALSE. If the start and start+len are not on character
2588 * boundaries, returns #FALSE.
2590 * @todo this is inconsistent with most of DBusString in that
2591 * it allows a start,len range that extends past the string end.
2593 * @param str the string
2594 * @param start first byte index to check
2595 * @param len number of bytes to check
2596 * @returns #TRUE if the byte range exists and is all valid UTF-8
2599 _dbus_string_validate_utf8 (const DBusString *str,
2603 const unsigned char *p;
2604 const unsigned char *end;
2605 DBUS_CONST_STRING_PREAMBLE (str);
2606 _dbus_assert (start >= 0);
2607 _dbus_assert (start <= real->len);
2608 _dbus_assert (len >= 0);
2610 /* we are doing _DBUS_UNLIKELY() here which might be
2611 * dubious in a generic library like GLib, but in D-BUS
2612 * we know we're validating messages and that it would
2613 * only be evil/broken apps that would have invalid
2614 * UTF-8. Also, this function seems to be a performance
2615 * bottleneck in profiles.
2618 if (_DBUS_UNLIKELY (len > real->len - start))
2621 p = real->str + start;
2626 int i, mask, char_len;
2627 dbus_unichar_t result;
2629 /* nul bytes considered invalid */
2633 /* Special-case ASCII; this makes us go a lot faster in
2634 * D-BUS profiles where we are typically validating
2635 * function names and such. We have to know that
2636 * all following checks will pass for ASCII though,
2637 * comments follow ...
2645 UTF8_COMPUTE (*p, mask, char_len);
2647 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2650 /* check that the expected number of bytes exists in the remaining length */
2651 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2654 UTF8_GET (result, p, i, mask, char_len);
2656 /* Check for overlong UTF-8 */
2657 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2660 /* The UNICODE_VALID check below will catch this */
2661 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2665 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2668 /* UNICODE_VALID should have caught it */
2669 _dbus_assert (result != (dbus_unichar_t)-1);
2674 /* See that we covered the entire length if a length was
2677 if (_DBUS_UNLIKELY (p != end))
2684 * Checks that the given range of the string is all nul bytes. If the
2685 * given range is not entirely contained in the string, returns
2688 * @todo this is inconsistent with most of DBusString in that
2689 * it allows a start,len range that extends past the string end.
2691 * @param str the string
2692 * @param start first byte index to check
2693 * @param len number of bytes to check
2694 * @returns #TRUE if the byte range exists and is all nul bytes
2697 _dbus_string_validate_nul (const DBusString *str,
2701 const unsigned char *s;
2702 const unsigned char *end;
2703 DBUS_CONST_STRING_PREAMBLE (str);
2704 _dbus_assert (start >= 0);
2705 _dbus_assert (len >= 0);
2706 _dbus_assert (start <= real->len);
2708 if (len > real->len - start)
2711 s = real->str + start;
2715 if (_DBUS_UNLIKELY (*s != '\0'))
2724 * Checks that the given range of the string is a valid object path
2725 * name in the D-BUS protocol. This includes a length restriction,
2726 * etc., see the specification. It does not validate UTF-8, that has
2727 * to be done separately for now.
2729 * @todo this is inconsistent with most of DBusString in that
2730 * it allows a start,len range that extends past the string end.
2732 * @todo change spec to disallow more things, such as spaces in the
2735 * @param str the string
2736 * @param start first byte index to check
2737 * @param len number of bytes to check
2738 * @returns #TRUE if the byte range exists and is a valid name
2741 _dbus_string_validate_path (const DBusString *str,
2745 const unsigned char *s;
2746 const unsigned char *end;
2747 const unsigned char *last_slash;
2749 DBUS_CONST_STRING_PREAMBLE (str);
2750 _dbus_assert (start >= 0);
2751 _dbus_assert (len >= 0);
2752 _dbus_assert (start <= real->len);
2754 if (len > real->len - start)
2757 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2763 s = real->str + start;
2775 if ((s - last_slash) < 2)
2776 return FALSE; /* no empty path components allowed */
2784 if ((end - last_slash) < 2 &&
2786 return FALSE; /* trailing slash not allowed unless the string is "/" */
2792 * Determine wether the given charater is valid as the first charater
2795 #define VALID_INITIAL_NAME_CHARACTER(c) \
2796 ( ((c) >= 'A' && (c) <= 'Z') || \
2797 ((c) >= 'a' && (c) <= 'z') || \
2801 * Determine wether the given charater is valid as a second or later
2802 * character in a nam
2804 #define VALID_NAME_CHARACTER(c) \
2805 ( ((c) >= '0' && (c) <= '9') || \
2806 ((c) >= 'A' && (c) <= 'Z') || \
2807 ((c) >= 'a' && (c) <= 'z') || \
2811 * Checks that the given range of the string is a valid interface name
2812 * in the D-BUS protocol. This includes a length restriction and an
2813 * ASCII subset, see the specification.
2815 * @todo this is inconsistent with most of DBusString in that
2816 * it allows a start,len range that extends past the string end.
2818 * @param str the string
2819 * @param start first byte index to check
2820 * @param len number of bytes to check
2821 * @returns #TRUE if the byte range exists and is a valid name
2824 _dbus_string_validate_interface (const DBusString *str,
2828 const unsigned char *s;
2829 const unsigned char *end;
2830 const unsigned char *iface;
2831 const unsigned char *last_dot;
2833 DBUS_CONST_STRING_PREAMBLE (str);
2834 _dbus_assert (start >= 0);
2835 _dbus_assert (len >= 0);
2836 _dbus_assert (start <= real->len);
2838 if (len > real->len - start)
2841 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2848 iface = real->str + start;
2852 /* check special cases of first char so it doesn't have to be done
2853 * in the loop. Note we know len > 0
2855 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
2857 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
2866 if (_DBUS_UNLIKELY ((s + 1) == end))
2868 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
2871 ++s; /* we just validated the next char, so skip two */
2873 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
2881 if (_DBUS_UNLIKELY (last_dot == NULL))
2888 * Checks that the given range of the string is a valid member name
2889 * in the D-BUS protocol. This includes a length restriction, etc.,
2890 * see the specification.
2892 * @todo this is inconsistent with most of DBusString in that
2893 * it allows a start,len range that extends past the string end.
2895 * @param str the string
2896 * @param start first byte index to check
2897 * @param len number of bytes to check
2898 * @returns #TRUE if the byte range exists and is a valid name
2901 _dbus_string_validate_member (const DBusString *str,
2905 const unsigned char *s;
2906 const unsigned char *end;
2907 const unsigned char *member;
2909 DBUS_CONST_STRING_PREAMBLE (str);
2910 _dbus_assert (start >= 0);
2911 _dbus_assert (len >= 0);
2912 _dbus_assert (start <= real->len);
2914 if (len > real->len - start)
2917 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2923 member = real->str + start;
2927 /* check special cases of first char so it doesn't have to be done
2928 * in the loop. Note we know len > 0
2931 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
2938 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
2950 * Checks that the given range of the string is a valid error name
2951 * in the D-BUS protocol. This includes a length restriction, etc.,
2952 * see the specification.
2954 * @todo this is inconsistent with most of DBusString in that
2955 * it allows a start,len range that extends past the string end.
2957 * @param str the string
2958 * @param start first byte index to check
2959 * @param len number of bytes to check
2960 * @returns #TRUE if the byte range exists and is a valid name
2963 _dbus_string_validate_error_name (const DBusString *str,
2967 /* Same restrictions as interface name at the moment */
2968 return _dbus_string_validate_interface (str, start, len);
2971 /* This assumes the first char exists and is ':' */
2973 _dbus_string_validate_base_service (const DBusString *str,
2977 const unsigned char *s;
2978 const unsigned char *end;
2979 const unsigned char *service;
2981 DBUS_CONST_STRING_PREAMBLE (str);
2982 _dbus_assert (start >= 0);
2983 _dbus_assert (len >= 0);
2984 _dbus_assert (start <= real->len);
2986 if (len > real->len - start)
2989 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2992 _dbus_assert (len > 0);
2994 service = real->str + start;
2995 end = service + len;
2996 _dbus_assert (*service == ':');
3003 if (_DBUS_UNLIKELY ((s + 1) == end))
3005 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*(s + 1))))
3007 ++s; /* we just validated the next char, so skip two */
3009 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
3021 * Checks that the given range of the string is a valid service name
3022 * in the D-BUS protocol. This includes a length restriction, etc.,
3023 * see the specification.
3025 * @todo this is inconsistent with most of DBusString in that
3026 * it allows a start,len range that extends past the string end.
3028 * @param str the string
3029 * @param start first byte index to check
3030 * @param len number of bytes to check
3031 * @returns #TRUE if the byte range exists and is a valid name
3034 _dbus_string_validate_service (const DBusString *str,
3038 if (_DBUS_UNLIKELY (len == 0))
3040 if (_dbus_string_get_byte (str, start) == ':')
3041 return _dbus_string_validate_base_service (str, start, len);
3043 return _dbus_string_validate_interface (str, start, len);
3047 * Checks that the given range of the string is a valid message type
3048 * signature in the D-BUS protocol.
3050 * @todo this is inconsistent with most of DBusString in that
3051 * it allows a start,len range that extends past the string end.
3053 * @param str the string
3054 * @param start first byte index to check
3055 * @param len number of bytes to check
3056 * @returns #TRUE if the byte range exists and is a valid signature
3059 _dbus_string_validate_signature (const DBusString *str,
3063 const unsigned char *s;
3064 const unsigned char *end;
3065 DBUS_CONST_STRING_PREAMBLE (str);
3066 _dbus_assert (start >= 0);
3067 _dbus_assert (start <= real->len);
3068 _dbus_assert (len >= 0);
3070 if (len > real->len - start)
3073 s = real->str + start;
3080 case DBUS_TYPE_BYTE:
3081 case DBUS_TYPE_BOOLEAN:
3082 case DBUS_TYPE_INT32:
3083 case DBUS_TYPE_UINT32:
3084 case DBUS_TYPE_INT64:
3085 case DBUS_TYPE_UINT64:
3086 case DBUS_TYPE_DOUBLE:
3087 case DBUS_TYPE_STRING:
3088 case DBUS_TYPE_CUSTOM:
3089 case DBUS_TYPE_ARRAY:
3090 case DBUS_TYPE_DICT:
3091 case DBUS_TYPE_OBJECT_PATH:
3105 * Clears all allocated bytes in the string to zero.
3107 * @param str the string
3110 _dbus_string_zero (DBusString *str)
3112 DBUS_STRING_PREAMBLE (str);
3114 memset (real->str - real->align_offset, '\0', real->allocated);
3118 #ifdef DBUS_BUILD_TESTS
3119 #include "dbus-test.h"
3123 * Parses a basic type defined by type contained in a DBusString. The
3124 * end_return parameter may be #NULL if you aren't interested in it. The
3125 * type is parsed and stored in value_return. Return parameters are not
3126 * initialized if the function returns #FALSE.
3128 * @param str the string
3129 * @param type the type of the basic type
3130 * @param start the byte index of the start of the type
3131 * @param value_return return location of the value or #NULL
3132 * @param end_return return location of the end of the type, or #NULL
3133 * @returns #TRUE on success
3136 _dbus_string_parse_basic_type (const DBusString *str,
3146 case DBUS_TYPE_BOOLEAN:
3148 int len = _dbus_string_get_length (str) - start;
3149 if (len >= 5 && _dbus_string_find_to (str, start, start + 5, "false", NULL))
3152 *(unsigned char *) value = TRUE;
3154 else if (len >= 4 && _dbus_string_find_to (str, start, start + 4, "true", NULL))
3157 *(unsigned char *) value = FALSE;
3160 _dbus_warn ("could not parse BOOLEAN\n");
3163 case DBUS_TYPE_BYTE:
3167 if (_dbus_string_get_byte (str, start) == '\'' &&
3168 _dbus_string_get_length (str) >= start + 4 &&
3169 _dbus_string_get_byte (str, start + 1) == '\\' &&
3170 _dbus_string_get_byte (str, start + 2) == '\'' &&
3171 _dbus_string_get_byte (str, start + 3) == '\'')
3176 else if (_dbus_string_get_byte (str, start) == '\'' &&
3177 _dbus_string_get_length (str) >= start + 3 &&
3178 _dbus_string_get_byte (str, start + 2) == '\'')
3180 val = _dbus_string_get_byte (str, start + 1);
3185 if (!_dbus_string_parse_int (str, start, &val, &end))
3186 _dbus_warn ("Failed to parse integer for BYTE\n");
3190 _dbus_warn ("A byte must be in range 0-255 not %ld\n", val);
3192 *(unsigned char *) value = val;
3195 case DBUS_TYPE_INT32:
3198 if (_dbus_string_parse_int (str, start, &val, &end))
3199 *(dbus_int32_t *)value = val;
3202 case DBUS_TYPE_UINT32:
3205 if (_dbus_string_parse_uint (str, start, &val, &end))
3206 *(dbus_uint32_t *)value = val;
3209 #ifdef DBUS_HAVE_INT64
3210 case DBUS_TYPE_INT64:
3211 case DBUS_TYPE_UINT64:
3212 /* use stroll oull */
3213 _dbus_assert_not_reached ("string -> [u]int64 not supported yet");
3215 #endif /* DBUS_HAVE_INT64 */
3216 case DBUS_TYPE_DOUBLE:
3217 _dbus_string_parse_double (str, start, value, &end);
3220 _dbus_assert_not_reached ("not a basic type");
3226 return end != start;
3230 test_max_len (DBusString *str,
3235 if (!_dbus_string_set_length (str, max_len - 1))
3236 _dbus_assert_not_reached ("setting len to one less than max should have worked");
3239 if (!_dbus_string_set_length (str, max_len))
3240 _dbus_assert_not_reached ("setting len to max len should have worked");
3242 if (_dbus_string_set_length (str, max_len + 1))
3243 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
3245 if (!_dbus_string_set_length (str, 0))
3246 _dbus_assert_not_reached ("setting len to zero should have worked");
3250 test_hex_roundtrip (const unsigned char *data,
3259 len = strlen (data);
3261 if (!_dbus_string_init (&orig))
3262 _dbus_assert_not_reached ("could not init string");
3264 if (!_dbus_string_init (&encoded))
3265 _dbus_assert_not_reached ("could not init string");
3267 if (!_dbus_string_init (&decoded))
3268 _dbus_assert_not_reached ("could not init string");
3270 if (!_dbus_string_append_len (&orig, data, len))
3271 _dbus_assert_not_reached ("couldn't append orig data");
3273 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
3274 _dbus_assert_not_reached ("could not encode");
3276 if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
3277 _dbus_assert_not_reached ("could not decode");
3279 _dbus_assert (_dbus_string_get_length (&encoded) == end);
3281 if (!_dbus_string_equal (&orig, &decoded))
3285 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
3286 _dbus_string_get_length (&orig),
3287 _dbus_string_get_length (&encoded),
3288 _dbus_string_get_length (&decoded));
3289 printf ("Original: %s\n", data);
3290 s = _dbus_string_get_const_data (&decoded);
3291 printf ("Decoded: %s\n", s);
3292 _dbus_assert_not_reached ("original string not the same as string decoded from hex");
3295 _dbus_string_free (&orig);
3296 _dbus_string_free (&encoded);
3297 _dbus_string_free (&decoded);
3300 typedef void (* TestRoundtripFunc) (const unsigned char *data,
3303 test_roundtrips (TestRoundtripFunc func)
3305 (* func) ("Hello this is a string\n", -1);
3306 (* func) ("Hello this is a string\n1", -1);
3307 (* func) ("Hello this is a string\n12", -1);
3308 (* func) ("Hello this is a string\n123", -1);
3309 (* func) ("Hello this is a string\n1234", -1);
3310 (* func) ("Hello this is a string\n12345", -1);
3314 (* func) ("123", 3);
3315 (* func) ("1234", 4);
3316 (* func) ("12345", 5);
3320 (* func) ("123", 4);
3321 (* func) ("1234", 5);
3322 (* func) ("12345", 6);
3324 unsigned char buf[512];
3328 while (i < _DBUS_N_ELEMENTS (buf))
3334 while (i < _DBUS_N_ELEMENTS (buf))
3344 * @ingroup DBusStringInternals
3345 * Unit test for DBusString.
3347 * @todo Need to write tests for _dbus_string_copy() and
3348 * _dbus_string_move() moving to/from each of start/middle/end of a
3349 * string. Also need tests for _dbus_string_move_len ()
3351 * @returns #TRUE on success.
3354 _dbus_string_test (void)
3361 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 };
3364 const char *valid_paths[] = {
3370 const char *invalid_paths[] = {
3386 const char *valid_interfaces[] = {
3387 "org.freedesktop.Foo",
3389 "Blah.Blah.Blah.Blah.Blah",
3392 "a0.b1.c2.d3.e4.f5.g6",
3395 const char *invalid_interfaces[] = {
3423 const char *valid_base_services[] = {
3432 ":abce.freedesktop.blah"
3434 const char *invalid_base_services[] = {
3448 const char *valid_members[] = {
3456 const char *invalid_members[] = {
3471 const char *valid_signatures[] = {
3478 const char *invalid_signatures[] = {
3480 "not a valid signature",
3487 while (i < _DBUS_N_ELEMENTS (lens))
3489 if (!_dbus_string_init (&str))
3490 _dbus_assert_not_reached ("failed to init string");
3492 set_max_length (&str, lens[i]);
3494 test_max_len (&str, lens[i]);
3495 _dbus_string_free (&str);
3500 /* Test shortening and setting length */
3502 while (i < _DBUS_N_ELEMENTS (lens))
3506 if (!_dbus_string_init (&str))
3507 _dbus_assert_not_reached ("failed to init string");
3509 set_max_length (&str, lens[i]);
3511 if (!_dbus_string_set_length (&str, lens[i]))
3512 _dbus_assert_not_reached ("failed to set string length");
3517 _dbus_assert (_dbus_string_get_length (&str) == j);
3520 _dbus_string_shorten (&str, 1);
3521 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
3526 _dbus_string_free (&str);
3531 /* Test appending data */
3532 if (!_dbus_string_init (&str))
3533 _dbus_assert_not_reached ("failed to init string");
3538 if (!_dbus_string_append (&str, "a"))
3539 _dbus_assert_not_reached ("failed to append string to string\n");
3541 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
3543 if (!_dbus_string_append_byte (&str, 'b'))
3544 _dbus_assert_not_reached ("failed to append byte to string\n");
3546 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
3551 _dbus_string_free (&str);
3553 /* Check steal_data */
3555 if (!_dbus_string_init (&str))
3556 _dbus_assert_not_reached ("failed to init string");
3558 if (!_dbus_string_append (&str, "Hello World"))
3559 _dbus_assert_not_reached ("could not append to string");
3561 i = _dbus_string_get_length (&str);
3563 if (!_dbus_string_steal_data (&str, &s))
3564 _dbus_assert_not_reached ("failed to steal data");
3566 _dbus_assert (_dbus_string_get_length (&str) == 0);
3567 _dbus_assert (((int)strlen (s)) == i);
3573 if (!_dbus_string_append (&str, "Hello World"))
3574 _dbus_assert_not_reached ("could not append to string");
3576 i = _dbus_string_get_length (&str);
3578 if (!_dbus_string_init (&other))
3579 _dbus_assert_not_reached ("could not init string");
3581 if (!_dbus_string_move (&str, 0, &other, 0))
3582 _dbus_assert_not_reached ("could not move");
3584 _dbus_assert (_dbus_string_get_length (&str) == 0);
3585 _dbus_assert (_dbus_string_get_length (&other) == i);
3587 if (!_dbus_string_append (&str, "Hello World"))
3588 _dbus_assert_not_reached ("could not append to string");
3590 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
3591 _dbus_assert_not_reached ("could not move");
3593 _dbus_assert (_dbus_string_get_length (&str) == 0);
3594 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3596 if (!_dbus_string_append (&str, "Hello World"))
3597 _dbus_assert_not_reached ("could not append to string");
3599 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3600 _dbus_assert_not_reached ("could not move");
3602 _dbus_assert (_dbus_string_get_length (&str) == 0);
3603 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3605 _dbus_string_free (&other);
3609 if (!_dbus_string_append (&str, "Hello World"))
3610 _dbus_assert_not_reached ("could not append to string");
3612 i = _dbus_string_get_length (&str);
3614 if (!_dbus_string_init (&other))
3615 _dbus_assert_not_reached ("could not init string");
3617 if (!_dbus_string_copy (&str, 0, &other, 0))
3618 _dbus_assert_not_reached ("could not copy");
3620 _dbus_assert (_dbus_string_get_length (&str) == i);
3621 _dbus_assert (_dbus_string_get_length (&other) == i);
3623 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
3624 _dbus_assert_not_reached ("could not copy");
3626 _dbus_assert (_dbus_string_get_length (&str) == i);
3627 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3628 _dbus_assert (_dbus_string_equal_c_str (&other,
3629 "Hello WorldHello World"));
3631 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3632 _dbus_assert_not_reached ("could not copy");
3634 _dbus_assert (_dbus_string_get_length (&str) == i);
3635 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3636 _dbus_assert (_dbus_string_equal_c_str (&other,
3637 "Hello WorldHello WorldHello World"));
3639 _dbus_string_free (&str);
3640 _dbus_string_free (&other);
3644 if (!_dbus_string_init (&str))
3645 _dbus_assert_not_reached ("failed to init string");
3647 if (!_dbus_string_append (&str, "Hello World"))
3648 _dbus_assert_not_reached ("could not append to string");
3650 i = _dbus_string_get_length (&str);
3652 if (!_dbus_string_init (&other))
3653 _dbus_assert_not_reached ("could not init string");
3655 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3656 &other, 0, _dbus_string_get_length (&other)))
3657 _dbus_assert_not_reached ("could not replace");
3659 _dbus_assert (_dbus_string_get_length (&str) == i);
3660 _dbus_assert (_dbus_string_get_length (&other) == i);
3661 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
3663 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3665 _dbus_assert_not_reached ("could not replace center space");
3667 _dbus_assert (_dbus_string_get_length (&str) == i);
3668 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3669 _dbus_assert (_dbus_string_equal_c_str (&other,
3670 "HelloHello WorldWorld"));
3673 if (!_dbus_string_replace_len (&str, 1, 1,
3675 _dbus_string_get_length (&other) - 1,
3677 _dbus_assert_not_reached ("could not replace end character");
3679 _dbus_assert (_dbus_string_get_length (&str) == i);
3680 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3681 _dbus_assert (_dbus_string_equal_c_str (&other,
3682 "HelloHello WorldWorle"));
3684 _dbus_string_free (&str);
3685 _dbus_string_free (&other);
3687 /* Check append/get unichar */
3689 if (!_dbus_string_init (&str))
3690 _dbus_assert_not_reached ("failed to init string");
3693 if (!_dbus_string_append_unichar (&str, 0xfffc))
3694 _dbus_assert_not_reached ("failed to append unichar");
3696 _dbus_string_get_unichar (&str, 0, &ch, &i);
3698 _dbus_assert (ch == 0xfffc);
3699 _dbus_assert (i == _dbus_string_get_length (&str));
3701 _dbus_string_free (&str);
3703 /* Check insert/set/get byte */
3705 if (!_dbus_string_init (&str))
3706 _dbus_assert_not_reached ("failed to init string");
3708 if (!_dbus_string_append (&str, "Hello"))
3709 _dbus_assert_not_reached ("failed to append Hello");
3711 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
3712 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
3713 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
3714 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
3715 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
3717 _dbus_string_set_byte (&str, 1, 'q');
3718 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
3720 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
3721 _dbus_assert_not_reached ("can't insert byte");
3723 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
3724 _dbus_assert_not_reached ("can't insert byte");
3726 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
3727 _dbus_assert_not_reached ("can't insert byte");
3729 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
3730 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
3731 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
3732 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
3733 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
3734 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
3735 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
3736 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
3737 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
3738 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
3739 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
3741 _dbus_string_free (&str);
3743 /* Check append/parse int/double */
3745 if (!_dbus_string_init (&str))
3746 _dbus_assert_not_reached ("failed to init string");
3748 if (!_dbus_string_append_int (&str, 27))
3749 _dbus_assert_not_reached ("failed to append int");
3751 i = _dbus_string_get_length (&str);
3753 if (!_dbus_string_parse_int (&str, 0, &v, &end))
3754 _dbus_assert_not_reached ("failed to parse int");
3756 _dbus_assert (v == 27);
3757 _dbus_assert (end == i);
3759 _dbus_string_free (&str);
3761 if (!_dbus_string_init (&str))
3762 _dbus_assert_not_reached ("failed to init string");
3764 if (!_dbus_string_append_double (&str, 50.3))
3765 _dbus_assert_not_reached ("failed to append float");
3767 i = _dbus_string_get_length (&str);
3769 if (!_dbus_string_parse_double (&str, 0, &d, &end))
3770 _dbus_assert_not_reached ("failed to parse float");
3772 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
3773 _dbus_assert (end == i);
3775 _dbus_string_free (&str);
3778 if (!_dbus_string_init (&str))
3779 _dbus_assert_not_reached ("failed to init string");
3781 if (!_dbus_string_append (&str, "Hello"))
3782 _dbus_assert_not_reached ("couldn't append to string");
3784 if (!_dbus_string_find (&str, 0, "He", &i))
3785 _dbus_assert_not_reached ("didn't find 'He'");
3786 _dbus_assert (i == 0);
3788 if (!_dbus_string_find (&str, 0, "Hello", &i))
3789 _dbus_assert_not_reached ("didn't find 'Hello'");
3790 _dbus_assert (i == 0);
3792 if (!_dbus_string_find (&str, 0, "ello", &i))
3793 _dbus_assert_not_reached ("didn't find 'ello'");
3794 _dbus_assert (i == 1);
3796 if (!_dbus_string_find (&str, 0, "lo", &i))
3797 _dbus_assert_not_reached ("didn't find 'lo'");
3798 _dbus_assert (i == 3);
3800 if (!_dbus_string_find (&str, 2, "lo", &i))
3801 _dbus_assert_not_reached ("didn't find 'lo'");
3802 _dbus_assert (i == 3);
3804 if (_dbus_string_find (&str, 4, "lo", &i))
3805 _dbus_assert_not_reached ("did find 'lo'");
3807 if (!_dbus_string_find (&str, 0, "l", &i))
3808 _dbus_assert_not_reached ("didn't find 'l'");
3809 _dbus_assert (i == 2);
3811 if (!_dbus_string_find (&str, 0, "H", &i))
3812 _dbus_assert_not_reached ("didn't find 'H'");
3813 _dbus_assert (i == 0);
3815 if (!_dbus_string_find (&str, 0, "", &i))
3816 _dbus_assert_not_reached ("didn't find ''");
3817 _dbus_assert (i == 0);
3819 if (_dbus_string_find (&str, 0, "Hello!", NULL))
3820 _dbus_assert_not_reached ("Did find 'Hello!'");
3822 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
3823 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
3825 if (_dbus_string_find (&str, 0, "ill", NULL))
3826 _dbus_assert_not_reached ("Did find 'ill'");
3828 if (_dbus_string_find (&str, 0, "q", NULL))
3829 _dbus_assert_not_reached ("Did find 'q'");
3831 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
3832 _dbus_assert_not_reached ("Didn't find 'He'");
3834 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
3835 _dbus_assert_not_reached ("Did find 'Hello'");
3837 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
3838 _dbus_assert_not_reached ("Did not find 'H'");
3839 _dbus_assert (i == 0);
3841 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
3842 _dbus_assert_not_reached ("Did not find 'o'");
3843 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
3845 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
3846 _dbus_assert_not_reached ("Did find 'o'");
3847 _dbus_assert (i == -1);
3849 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
3850 _dbus_assert_not_reached ("Did find 'e'");
3851 _dbus_assert (i == -1);
3853 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
3854 _dbus_assert_not_reached ("Didn't find 'e'");
3855 _dbus_assert (i == 1);
3857 _dbus_string_free (&str);
3860 _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
3861 if (!_dbus_string_init (&other))
3862 _dbus_assert_not_reached ("could not init string");
3864 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
3865 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
3867 _dbus_assert (end == 8);
3869 _dbus_string_free (&other);
3871 test_roundtrips (test_hex_roundtrip);
3873 /* Path validation */
3875 while (i < (int) _DBUS_N_ELEMENTS (valid_paths))
3877 _dbus_string_init_const (&str, valid_paths[i]);
3879 if (!_dbus_string_validate_path (&str, 0,
3880 _dbus_string_get_length (&str)))
3882 _dbus_warn ("Path \"%s\" should have been valid\n", valid_paths[i]);
3883 _dbus_assert_not_reached ("invalid path");
3890 while (i < (int) _DBUS_N_ELEMENTS (invalid_paths))
3892 _dbus_string_init_const (&str, invalid_paths[i]);
3894 if (_dbus_string_validate_path (&str, 0,
3895 _dbus_string_get_length (&str)))
3897 _dbus_warn ("Path \"%s\" should have been invalid\n", invalid_paths[i]);
3898 _dbus_assert_not_reached ("valid path");
3904 /* Interface validation */
3906 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
3908 _dbus_string_init_const (&str, valid_interfaces[i]);
3910 if (!_dbus_string_validate_interface (&str, 0,
3911 _dbus_string_get_length (&str)))
3913 _dbus_warn ("Interface \"%s\" should have been valid\n", valid_interfaces[i]);
3914 _dbus_assert_not_reached ("invalid interface");
3921 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
3923 _dbus_string_init_const (&str, invalid_interfaces[i]);
3925 if (_dbus_string_validate_interface (&str, 0,
3926 _dbus_string_get_length (&str)))
3928 _dbus_warn ("Interface \"%s\" should have been invalid\n", invalid_interfaces[i]);
3929 _dbus_assert_not_reached ("valid interface");
3935 /* Service validation (check that valid interfaces are valid services,
3936 * and invalid interfaces are invalid services except if they start with ':')
3939 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
3941 _dbus_string_init_const (&str, valid_interfaces[i]);
3943 if (!_dbus_string_validate_service (&str, 0,
3944 _dbus_string_get_length (&str)))
3946 _dbus_warn ("Service \"%s\" should have been valid\n", valid_interfaces[i]);
3947 _dbus_assert_not_reached ("invalid service");
3954 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
3956 if (invalid_interfaces[i][0] != ':')
3958 _dbus_string_init_const (&str, invalid_interfaces[i]);
3960 if (_dbus_string_validate_service (&str, 0,
3961 _dbus_string_get_length (&str)))
3963 _dbus_warn ("Service \"%s\" should have been invalid\n", invalid_interfaces[i]);
3964 _dbus_assert_not_reached ("valid service");
3971 /* Base service validation */
3973 while (i < (int) _DBUS_N_ELEMENTS (valid_base_services))
3975 _dbus_string_init_const (&str, valid_base_services[i]);
3977 if (!_dbus_string_validate_service (&str, 0,
3978 _dbus_string_get_length (&str)))
3980 _dbus_warn ("Service \"%s\" should have been valid\n", valid_base_services[i]);
3981 _dbus_assert_not_reached ("invalid base service");
3988 while (i < (int) _DBUS_N_ELEMENTS (invalid_base_services))
3990 _dbus_string_init_const (&str, invalid_base_services[i]);
3992 if (_dbus_string_validate_service (&str, 0,
3993 _dbus_string_get_length (&str)))
3995 _dbus_warn ("Service \"%s\" should have been invalid\n", invalid_base_services[i]);
3996 _dbus_assert_not_reached ("valid base service");
4003 /* Error name validation (currently identical to interfaces)
4006 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
4008 _dbus_string_init_const (&str, valid_interfaces[i]);
4010 if (!_dbus_string_validate_error_name (&str, 0,
4011 _dbus_string_get_length (&str)))
4013 _dbus_warn ("Error name \"%s\" should have been valid\n", valid_interfaces[i]);
4014 _dbus_assert_not_reached ("invalid error name");
4021 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
4023 if (invalid_interfaces[i][0] != ':')
4025 _dbus_string_init_const (&str, invalid_interfaces[i]);
4027 if (_dbus_string_validate_error_name (&str, 0,
4028 _dbus_string_get_length (&str)))
4030 _dbus_warn ("Error name \"%s\" should have been invalid\n", invalid_interfaces[i]);
4031 _dbus_assert_not_reached ("valid error name");
4038 /* Member validation */
4040 while (i < (int) _DBUS_N_ELEMENTS (valid_members))
4042 _dbus_string_init_const (&str, valid_members[i]);
4044 if (!_dbus_string_validate_member (&str, 0,
4045 _dbus_string_get_length (&str)))
4047 _dbus_warn ("Member \"%s\" should have been valid\n", valid_members[i]);
4048 _dbus_assert_not_reached ("invalid member");
4055 while (i < (int) _DBUS_N_ELEMENTS (invalid_members))
4057 _dbus_string_init_const (&str, invalid_members[i]);
4059 if (_dbus_string_validate_member (&str, 0,
4060 _dbus_string_get_length (&str)))
4062 _dbus_warn ("Member \"%s\" should have been invalid\n", invalid_members[i]);
4063 _dbus_assert_not_reached ("valid member");
4069 /* Signature validation */
4071 while (i < (int) _DBUS_N_ELEMENTS (valid_signatures))
4073 _dbus_string_init_const (&str, valid_signatures[i]);
4075 if (!_dbus_string_validate_signature (&str, 0,
4076 _dbus_string_get_length (&str)))
4078 _dbus_warn ("Signature \"%s\" should have been valid\n", valid_signatures[i]);
4079 _dbus_assert_not_reached ("invalid signature");
4086 while (i < (int) _DBUS_N_ELEMENTS (invalid_signatures))
4088 _dbus_string_init_const (&str, invalid_signatures[i]);
4090 if (_dbus_string_validate_signature (&str, 0,
4091 _dbus_string_get_length (&str)))
4093 _dbus_warn ("Signature \"%s\" should have been invalid\n", invalid_signatures[i]);
4094 _dbus_assert_not_reached ("valid signature");
4100 /* Validate claimed length longer than real length */
4101 _dbus_string_init_const (&str, "abc.efg");
4102 if (_dbus_string_validate_service (&str, 0, 8))
4103 _dbus_assert_not_reached ("validated too-long string");
4104 if (_dbus_string_validate_interface (&str, 0, 8))
4105 _dbus_assert_not_reached ("validated too-long string");
4106 if (_dbus_string_validate_error_name (&str, 0, 8))
4107 _dbus_assert_not_reached ("validated too-long string");
4109 _dbus_string_init_const (&str, "abc");
4110 if (_dbus_string_validate_member (&str, 0, 4))
4111 _dbus_assert_not_reached ("validated too-long string");
4113 _dbus_string_init_const (&str, "sss");
4114 if (_dbus_string_validate_signature (&str, 0, 4))
4115 _dbus_assert_not_reached ("validated too-long signature");
4117 /* Validate string exceeding max name length */
4118 if (!_dbus_string_init (&str))
4119 _dbus_assert_not_reached ("no memory");
4121 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
4122 if (!_dbus_string_append (&str, "abc.def"))
4123 _dbus_assert_not_reached ("no memory");
4125 if (_dbus_string_validate_service (&str, 0, _dbus_string_get_length (&str)))
4126 _dbus_assert_not_reached ("validated overmax string");
4127 if (_dbus_string_validate_interface (&str, 0, _dbus_string_get_length (&str)))
4128 _dbus_assert_not_reached ("validated overmax string");
4129 if (_dbus_string_validate_error_name (&str, 0, _dbus_string_get_length (&str)))
4130 _dbus_assert_not_reached ("validated overmax string");
4132 /* overlong member */
4133 _dbus_string_set_length (&str, 0);
4134 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
4135 if (!_dbus_string_append (&str, "abc"))
4136 _dbus_assert_not_reached ("no memory");
4138 if (_dbus_string_validate_member (&str, 0, _dbus_string_get_length (&str)))
4139 _dbus_assert_not_reached ("validated overmax string");
4141 /* overlong base service */
4142 _dbus_string_set_length (&str, 0);
4143 _dbus_string_append (&str, ":");
4144 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
4145 if (!_dbus_string_append (&str, "abc"))
4146 _dbus_assert_not_reached ("no memory");
4148 if (_dbus_string_validate_service (&str, 0, _dbus_string_get_length (&str)))
4149 _dbus_assert_not_reached ("validated overmax string");
4151 _dbus_string_free (&str);
4156 #endif /* DBUS_BUILD_TESTS */