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, 2005 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 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
31 #include "dbus-string-private.h"
32 #include "dbus-marshal-basic.h" /* probably should be removed by moving the usage of DBUS_TYPE
33 * into the marshaling-related files
35 /* for DBUS_VA_COPY */
36 #include "dbus-sysdeps.h"
39 * @defgroup DBusString string class
40 * @ingroup DBusInternals
41 * @brief DBusString data structure
43 * Types and functions related to DBusString. DBusString is intended
44 * to be a string class that makes it hard to mess up security issues
45 * (and just in general harder to write buggy code). It should be
46 * used (or extended and then used) rather than the libc stuff in
47 * string.h. The string class is a bit inconvenient at spots because
48 * it handles out-of-memory failures and tries to be extra-robust.
50 * A DBusString has a maximum length set at initialization time; this
51 * can be used to ensure that a buffer doesn't get too big. The
52 * _dbus_string_lengthen() method checks for overflow, and for max
53 * length being exceeded.
55 * Try to avoid conversion to a plain C string, i.e. add methods on
56 * the string object instead, only convert to C string when passing
57 * things out to the public API. In particular, no sprintf, strcpy,
58 * strcat, any of that should be used. The GString feature of
59 * accepting negative numbers for "length of string" is also absent,
60 * because it could keep us from detecting bogus huge lengths. i.e. if
61 * we passed in some bogus huge length it would be taken to mean
62 * "current length of string" instead of "broken crack"
64 * @todo #DBusString needs a lot of cleaning up; some of the
65 * API is no longer used, and the API is pretty inconsistent.
66 * In particular all the "append" APIs, especially those involving
67 * alignment but probably lots of them, are no longer used by the
68 * marshaling code which always does "inserts" now.
72 * @defgroup DBusStringInternals DBusString implementation details
73 * @ingroup DBusInternals
74 * @brief DBusString implementation details
76 * The guts of DBusString.
82 * This is the maximum max length (and thus also the maximum length)
85 #define MAX_MAX_LENGTH (_DBUS_INT_MAX - _DBUS_STRING_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 - _DBUS_STRING_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 - _DBUS_STRING_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 (_DBUS_STRING_ALLOCATION_PADDING + allocate_size);
203 if (real->str == NULL)
206 real->allocated = _DBUS_STRING_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 + _DBUS_STRING_ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
297 real->max_length = real->len + 1;
298 real->constant = TRUE;
300 real->invalid = FALSE;
301 real->align_offset = 0;
303 /* We don't require const strings to be 8-byte aligned as the
304 * memory is coming from elsewhere.
309 * Frees a string created by _dbus_string_init().
311 * @param str memory where the string is stored.
314 _dbus_string_free (DBusString *str)
316 DBusRealString *real = (DBusRealString*) str;
317 DBUS_GENERIC_STRING_PREAMBLE (real);
321 dbus_free (real->str - real->align_offset);
323 real->invalid = TRUE;
326 #ifdef DBUS_BUILD_TESTS
327 /* Not using this feature at the moment,
328 * so marked DBUS_BUILD_TESTS-only
331 * Locks a string such that any attempts to change the string will
332 * result in aborting the program. Also, if the string is wasting a
333 * lot of memory (allocation is sufficiently larger than what the
334 * string is really using), _dbus_string_lock() will realloc the
335 * string's data to "compact" it.
337 * @param str the string to lock.
340 _dbus_string_lock (DBusString *str)
342 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
346 /* Try to realloc to avoid excess memory usage, since
347 * we know we won't change the string further
350 if (real->allocated - MAX_WASTE > real->len)
355 new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
357 new_str = dbus_realloc (real->str - real->align_offset,
361 real->str = new_str + real->align_offset;
362 real->allocated = new_allocated;
363 fixup_alignment (real);
367 #endif /* DBUS_BUILD_TESTS */
370 reallocate_for_length (DBusRealString *real,
376 /* at least double our old allocation to avoid O(n), avoiding
379 if (real->allocated > (MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
380 new_allocated = MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
382 new_allocated = real->allocated * 2;
384 /* if you change the code just above here, run the tests without
385 * the following assert-only hack before you commit
387 /* This is keyed off asserts in addition to tests so when you
388 * disable asserts to profile, you don't get this destroyer
391 #ifdef DBUS_DISABLE_ASSERT
393 #ifdef DBUS_BUILD_TESTS
394 new_allocated = 0; /* ensure a realloc every time so that we go
395 * through all malloc failure codepaths
397 #endif /* DBUS_BUILD_TESTS */
398 #endif /* !DBUS_DISABLE_ASSERT */
400 /* But be sure we always alloc at least space for the new length */
401 new_allocated = MAX (new_allocated,
402 new_length + _DBUS_STRING_ALLOCATION_PADDING);
404 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
405 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
406 if (_DBUS_UNLIKELY (new_str == NULL))
409 real->str = new_str + real->align_offset;
410 real->allocated = new_allocated;
411 fixup_alignment (real);
417 set_length (DBusRealString *real,
420 /* Note, we are setting the length not including nul termination */
422 /* exceeding max length is the same as failure to allocate memory */
423 if (_DBUS_UNLIKELY (new_length > real->max_length))
425 else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
426 _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
430 real->len = new_length;
431 real->str[new_length] = '\0';
438 DBusRealString *dest,
444 if (len > dest->max_length - dest->len)
445 return FALSE; /* detected overflow of dest->len + len below */
447 if (!set_length (dest, dest->len + len))
450 memmove (dest->str + insert_at + len,
451 dest->str + insert_at,
452 dest->len - len - insert_at);
458 * Gets the raw character buffer from the string. The returned buffer
459 * will be nul-terminated, but note that strings may contain binary
460 * data so there may be extra nul characters prior to the termination.
461 * This function should be little-used, extend DBusString or add
462 * stuff to dbus-sysdeps.c instead. It's an error to use this
463 * function on a const string.
465 * @param str the string
469 _dbus_string_get_data (DBusString *str)
471 DBUS_STRING_PREAMBLE (str);
476 /* only do the function if we don't have the macro */
477 #ifndef _dbus_string_get_const_data
479 * Gets the raw character buffer from a const string.
481 * @param str the string
482 * @returns the string data
485 _dbus_string_get_const_data (const DBusString *str)
487 DBUS_CONST_STRING_PREAMBLE (str);
491 #endif /* _dbus_string_get_const_data */
494 * Gets a sub-portion of the raw character buffer from the
495 * string. The "len" field is required simply for error
496 * checking, to be sure you don't try to use more
497 * string than exists. The nul termination of the
498 * returned buffer remains at the end of the entire
499 * string, not at start + len.
501 * @param str the string
502 * @param start byte offset to return
503 * @param len length of segment to return
504 * @returns the string data
507 _dbus_string_get_data_len (DBusString *str,
511 DBUS_STRING_PREAMBLE (str);
512 _dbus_assert (start >= 0);
513 _dbus_assert (len >= 0);
514 _dbus_assert (start <= real->len);
515 _dbus_assert (len <= real->len - start);
517 return real->str + start;
520 /* only do the function if we don't have the macro */
521 #ifndef _dbus_string_get_const_data_len
523 * const version of _dbus_string_get_data_len().
525 * @param str the string
526 * @param start byte offset to return
527 * @param len length of segment to return
528 * @returns the string data
531 _dbus_string_get_const_data_len (const DBusString *str,
535 DBUS_CONST_STRING_PREAMBLE (str);
536 _dbus_assert (start >= 0);
537 _dbus_assert (len >= 0);
538 _dbus_assert (start <= real->len);
539 _dbus_assert (len <= real->len - start);
541 return real->str + start;
543 #endif /* _dbus_string_get_const_data_len */
545 /* only do the function if we don't have the macro */
546 #ifndef _dbus_string_set_byte
548 * Sets the value of the byte at the given position.
550 * @param str the string
551 * @param i the position
552 * @param byte the new value
555 _dbus_string_set_byte (DBusString *str,
559 DBUS_STRING_PREAMBLE (str);
560 _dbus_assert (i < real->len);
561 _dbus_assert (i >= 0);
565 #endif /* _dbus_string_set_byte */
567 /* only have the function if we didn't create a macro */
568 #ifndef _dbus_string_get_byte
570 * Gets the byte at the given position. It is
571 * allowed to ask for the nul byte at the end of
574 * @param str the string
575 * @param start the position
576 * @returns the byte at that position
579 _dbus_string_get_byte (const DBusString *str,
582 DBUS_CONST_STRING_PREAMBLE (str);
583 _dbus_assert (start <= real->len);
584 _dbus_assert (start >= 0);
586 return real->str[start];
588 #endif /* _dbus_string_get_byte */
591 * Inserts a number of bytes of a given value at the
594 * @param str the string
595 * @param i the position
596 * @param n_bytes number of bytes
597 * @param byte the value to insert
598 * @returns #TRUE on success
601 _dbus_string_insert_bytes (DBusString *str,
606 DBUS_STRING_PREAMBLE (str);
607 _dbus_assert (i <= real->len);
608 _dbus_assert (i >= 0);
609 _dbus_assert (n_bytes >= 0);
614 if (!open_gap (n_bytes, real, i))
617 memset (real->str + i, byte, n_bytes);
623 * Inserts a single byte at the given position.
625 * @param str the string
626 * @param i the position
627 * @param byte the value to insert
628 * @returns #TRUE on success
631 _dbus_string_insert_byte (DBusString *str,
635 DBUS_STRING_PREAMBLE (str);
636 _dbus_assert (i <= real->len);
637 _dbus_assert (i >= 0);
639 if (!open_gap (1, real, i))
648 * Like _dbus_string_get_data(), but removes the
649 * gotten data from the original string. The caller
650 * must free the data returned. This function may
651 * fail due to lack of memory, and return #FALSE.
653 * @param str the string
654 * @param data_return location to return the buffer
655 * @returns #TRUE on success
658 _dbus_string_steal_data (DBusString *str,
662 DBUS_STRING_PREAMBLE (str);
663 _dbus_assert (data_return != NULL);
665 undo_alignment (real);
667 *data_return = real->str;
669 old_max_length = real->max_length;
671 /* reset the string */
672 if (!_dbus_string_init (str))
674 /* hrm, put it back then */
675 real->str = *data_return;
677 fixup_alignment (real);
681 real->max_length = old_max_length;
687 * Like _dbus_string_get_data_len(), but removes the gotten data from
688 * the original string. The caller must free the data returned. This
689 * function may fail due to lack of memory, and return #FALSE.
690 * The returned string is nul-terminated and has length len.
692 * @todo this function is broken because on failure it
693 * may corrupt the source string.
695 * @param str the string
696 * @param data_return location to return the buffer
697 * @param start the start of segment to steal
698 * @param len the length of segment to steal
699 * @returns #TRUE on success
702 _dbus_string_steal_data_len (DBusString *str,
708 DBUS_STRING_PREAMBLE (str);
709 _dbus_assert (data_return != NULL);
710 _dbus_assert (start >= 0);
711 _dbus_assert (len >= 0);
712 _dbus_assert (start <= real->len);
713 _dbus_assert (len <= real->len - start);
715 if (!_dbus_string_init (&dest))
718 set_max_length (&dest, real->max_length);
720 if (!_dbus_string_move_len (str, start, len, &dest, 0))
722 _dbus_string_free (&dest);
726 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
727 if (!_dbus_string_steal_data (&dest, data_return))
729 _dbus_string_free (&dest);
733 _dbus_string_free (&dest);
739 * Copies the data from the string into a char*
741 * @param str the string
742 * @param data_return place to return the data
743 * @returns #TRUE on success, #FALSE on no memory
746 _dbus_string_copy_data (const DBusString *str,
749 DBUS_CONST_STRING_PREAMBLE (str);
750 _dbus_assert (data_return != NULL);
752 *data_return = dbus_malloc (real->len + 1);
753 if (*data_return == NULL)
756 memcpy (*data_return, real->str, real->len + 1);
762 * Copies a segment of the string into a char*
764 * @param str the string
765 * @param data_return place to return the data
766 * @param start start index
767 * @param len length to copy
768 * @returns #FALSE if no memory
771 _dbus_string_copy_data_len (const DBusString *str,
778 DBUS_CONST_STRING_PREAMBLE (str);
779 _dbus_assert (data_return != NULL);
780 _dbus_assert (start >= 0);
781 _dbus_assert (len >= 0);
782 _dbus_assert (start <= real->len);
783 _dbus_assert (len <= real->len - start);
785 if (!_dbus_string_init (&dest))
788 set_max_length (&dest, real->max_length);
790 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
792 _dbus_string_free (&dest);
796 if (!_dbus_string_steal_data (&dest, data_return))
798 _dbus_string_free (&dest);
802 _dbus_string_free (&dest);
807 * Copies the contents of a DBusString into a different
808 * buffer. The resulting buffer will be nul-terminated.
810 * @param str a string
811 * @param buffer a C buffer to copy data to
812 * @param len maximum length of C buffer
815 _dbus_string_copy_to_buffer (const DBusString *str,
820 DBUS_CONST_STRING_PREAMBLE (str);
822 _dbus_assert (avail_len >= 0);
824 copy_len = MIN (avail_len, real->len+1);
825 memcpy (buffer, real->str, copy_len);
826 if (avail_len > 0 && avail_len == copy_len)
827 buffer[avail_len-1] = '\0';
830 /* Only have the function if we don't have the macro */
831 #ifndef _dbus_string_get_length
833 * Gets the length of a string (not including nul termination).
835 * @returns the length.
838 _dbus_string_get_length (const DBusString *str)
840 DBUS_CONST_STRING_PREAMBLE (str);
844 #endif /* !_dbus_string_get_length */
847 * Makes a string longer by the given number of bytes. Checks whether
848 * adding additional_length to the current length would overflow an
849 * integer, and checks for exceeding a string's max length.
850 * The new bytes are not initialized, other than nul-terminating
851 * the end of the string. The uninitialized bytes may contain
852 * nul bytes or other junk.
854 * @param str a string
855 * @param additional_length length to add to the string.
856 * @returns #TRUE on success.
859 _dbus_string_lengthen (DBusString *str,
860 int additional_length)
862 DBUS_STRING_PREAMBLE (str);
863 _dbus_assert (additional_length >= 0);
865 if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
866 return FALSE; /* would overflow */
868 return set_length (real,
869 real->len + additional_length);
873 * Makes a string shorter by the given number of bytes.
875 * @param str a string
876 * @param length_to_remove length to remove from the string.
879 _dbus_string_shorten (DBusString *str,
880 int length_to_remove)
882 DBUS_STRING_PREAMBLE (str);
883 _dbus_assert (length_to_remove >= 0);
884 _dbus_assert (length_to_remove <= real->len);
887 real->len - length_to_remove);
891 * Sets the length of a string. Can be used to truncate or lengthen
892 * the string. If the string is lengthened, the function may fail and
893 * return #FALSE. Newly-added bytes are not initialized, as with
894 * _dbus_string_lengthen().
896 * @param str a string
897 * @param length new length of the string.
898 * @returns #FALSE on failure.
901 _dbus_string_set_length (DBusString *str,
904 DBUS_STRING_PREAMBLE (str);
905 _dbus_assert (length >= 0);
907 return set_length (real, length);
911 align_insert_point_then_open_gap (DBusString *str,
916 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
917 unsigned long gap_pos;
920 DBUS_STRING_PREAMBLE (str);
921 _dbus_assert (alignment >= 1);
922 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
924 insert_at = *insert_at_p;
926 _dbus_assert (insert_at <= real->len);
928 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
929 new_len = real->len + (gap_pos - insert_at) + gap_size;
931 if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length))
934 delta = new_len - real->len;
935 _dbus_assert (delta >= 0);
937 if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
939 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
943 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
947 /* nul the padding if we had to add any padding */
948 if (gap_size < delta)
950 memset (&real->str[insert_at], '\0',
951 gap_pos - insert_at);
954 *insert_at_p = gap_pos;
960 align_length_then_lengthen (DBusString *str,
962 int then_lengthen_by)
966 insert_at = _dbus_string_get_length (str);
968 return align_insert_point_then_open_gap (str,
970 alignment, then_lengthen_by);
974 * Align the length of a string to a specific alignment (typically 4 or 8)
975 * by appending nul bytes to the string.
977 * @param str a string
978 * @param alignment the alignment
979 * @returns #FALSE if no memory
982 _dbus_string_align_length (DBusString *str,
985 return align_length_then_lengthen (str, alignment, 0);
989 * Preallocate extra_bytes such that a future lengthening of the
990 * string by extra_bytes is guaranteed to succeed without an out of
993 * @param str a string
994 * @param extra_bytes bytes to alloc
995 * @returns #FALSE if no memory
998 _dbus_string_alloc_space (DBusString *str,
1001 if (!_dbus_string_lengthen (str, extra_bytes))
1003 _dbus_string_shorten (str, extra_bytes);
1009 append (DBusRealString *real,
1013 if (buffer_len == 0)
1016 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
1019 memcpy (real->str + (real->len - buffer_len),
1027 * Appends a nul-terminated C-style string to a DBusString.
1029 * @param str the DBusString
1030 * @param buffer the nul-terminated characters to append
1031 * @returns #FALSE if not enough memory.
1034 _dbus_string_append (DBusString *str,
1037 unsigned long buffer_len;
1039 DBUS_STRING_PREAMBLE (str);
1040 _dbus_assert (buffer != NULL);
1042 buffer_len = strlen (buffer);
1043 if (buffer_len > (unsigned long) real->max_length)
1046 return append (real, buffer, buffer_len);
1049 #define ASSIGN_4_OCTETS(p, octets) \
1050 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
1052 #ifdef DBUS_HAVE_INT64
1053 #define ASSIGN_8_OCTETS(p, octets) \
1054 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
1056 #define ASSIGN_8_OCTETS(p, octets) \
1070 _dbus_assert (b == p + 8); \
1072 #endif /* DBUS_HAVE_INT64 */
1075 * Appends 4 bytes aligned on a 4 byte boundary
1076 * with any alignment padding initialized to 0.
1078 * @param str the DBusString
1079 * @param octets 4 bytes to append
1080 * @returns #FALSE if not enough memory.
1083 _dbus_string_append_4_aligned (DBusString *str,
1084 const unsigned char octets[4])
1086 DBUS_STRING_PREAMBLE (str);
1088 if (!align_length_then_lengthen (str, 4, 4))
1091 ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
1097 * Appends 8 bytes aligned on an 8 byte boundary
1098 * with any alignment padding initialized to 0.
1100 * @param str the DBusString
1101 * @param octets 8 bytes to append
1102 * @returns #FALSE if not enough memory.
1105 _dbus_string_append_8_aligned (DBusString *str,
1106 const unsigned char octets[8])
1108 DBUS_STRING_PREAMBLE (str);
1110 if (!align_length_then_lengthen (str, 8, 8))
1113 ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
1119 * Inserts 4 bytes aligned on a 4 byte boundary
1120 * with any alignment padding initialized to 0.
1122 * @param str the DBusString
1123 * @param octets 4 bytes to insert
1124 * @returns #FALSE if not enough memory.
1127 _dbus_string_insert_4_aligned (DBusString *str,
1129 const unsigned char octets[4])
1131 DBUS_STRING_PREAMBLE (str);
1133 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1136 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1142 * Inserts 8 bytes aligned on an 8 byte boundary
1143 * with any alignment padding initialized to 0.
1145 * @param str the DBusString
1146 * @param octets 8 bytes to insert
1147 * @returns #FALSE if not enough memory.
1150 _dbus_string_insert_8_aligned (DBusString *str,
1152 const unsigned char octets[8])
1154 DBUS_STRING_PREAMBLE (str);
1156 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1159 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1161 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1168 * Inserts padding at *insert_at such to align it to the given
1169 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1170 * to the aligned position.
1172 * @param str the DBusString
1173 * @param insert_at location to be aligned
1174 * @param alignment alignment boundary (1, 4, or 8)
1175 * @returns #FALSE if not enough memory.
1178 _dbus_string_insert_alignment (DBusString *str,
1182 DBUS_STRING_PREAMBLE (str);
1184 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1187 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1193 * Appends a printf-style formatted string
1194 * to the #DBusString.
1196 * @param str the string
1197 * @param format printf format
1198 * @param args variable argument list
1199 * @returns #FALSE if no memory
1202 _dbus_string_append_printf_valist (DBusString *str,
1210 DBUS_STRING_PREAMBLE (str);
1212 DBUS_VA_COPY (args_copy, args);
1214 /* Measure the message length without terminating nul */
1215 len = vsnprintf (&c, 1, format, args);
1217 if (!_dbus_string_lengthen (str, len))
1219 /* don't leak the copy */
1224 vsprintf (real->str + (real->len - len),
1233 * Appends a printf-style formatted string
1234 * to the #DBusString.
1236 * @param str the string
1237 * @param format printf format
1238 * @returns #FALSE if no memory
1241 _dbus_string_append_printf (DBusString *str,
1248 va_start (args, format);
1249 retval = _dbus_string_append_printf_valist (str, format, args);
1256 * Appends block of bytes with the given length to a DBusString.
1258 * @param str the DBusString
1259 * @param buffer the bytes to append
1260 * @param len the number of bytes to append
1261 * @returns #FALSE if not enough memory.
1264 _dbus_string_append_len (DBusString *str,
1268 DBUS_STRING_PREAMBLE (str);
1269 _dbus_assert (buffer != NULL);
1270 _dbus_assert (len >= 0);
1272 return append (real, buffer, len);
1276 * Appends a single byte to the string, returning #FALSE
1277 * if not enough memory.
1279 * @param str the string
1280 * @param byte the byte to append
1281 * @returns #TRUE on success
1284 _dbus_string_append_byte (DBusString *str,
1287 DBUS_STRING_PREAMBLE (str);
1289 if (!set_length (real, real->len + 1))
1292 real->str[real->len-1] = byte;
1298 * Appends a single Unicode character, encoding the character
1301 * @param str the string
1302 * @param ch the Unicode character
1305 _dbus_string_append_unichar (DBusString *str,
1313 DBUS_STRING_PREAMBLE (str);
1315 /* this code is from GLib but is pretty standard I think */
1324 else if (ch < 0x800)
1329 else if (ch < 0x10000)
1334 else if (ch < 0x200000)
1339 else if (ch < 0x4000000)
1350 if (len > (real->max_length - real->len))
1351 return FALSE; /* real->len + len would overflow */
1353 if (!set_length (real, real->len + len))
1356 out = real->str + (real->len - len);
1358 for (i = len - 1; i > 0; --i)
1360 out[i] = (ch & 0x3f) | 0x80;
1363 out[0] = ch | first;
1369 delete (DBusRealString *real,
1376 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1378 real->str[real->len] = '\0';
1382 * Deletes a segment of a DBusString with length len starting at
1383 * start. (Hint: to clear an entire string, setting length to 0
1384 * with _dbus_string_set_length() is easier.)
1386 * @param str the DBusString
1387 * @param start where to start deleting
1388 * @param len the number of bytes to delete
1391 _dbus_string_delete (DBusString *str,
1395 DBUS_STRING_PREAMBLE (str);
1396 _dbus_assert (start >= 0);
1397 _dbus_assert (len >= 0);
1398 _dbus_assert (start <= real->len);
1399 _dbus_assert (len <= real->len - start);
1401 delete (real, start, len);
1405 copy (DBusRealString *source,
1408 DBusRealString *dest,
1414 if (!open_gap (len, dest, insert_at))
1417 memcpy (dest->str + insert_at,
1418 source->str + start,
1425 * Checks assertions for two strings we're copying a segment between,
1426 * and declares real_source/real_dest variables.
1428 * @param source the source string
1429 * @param start the starting offset
1430 * @param dest the dest string
1431 * @param insert_at where the copied segment is inserted
1433 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1434 DBusRealString *real_source = (DBusRealString*) source; \
1435 DBusRealString *real_dest = (DBusRealString*) dest; \
1436 _dbus_assert ((source) != (dest)); \
1437 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1438 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1439 _dbus_assert (!real_dest->constant); \
1440 _dbus_assert (!real_dest->locked); \
1441 _dbus_assert ((start) >= 0); \
1442 _dbus_assert ((start) <= real_source->len); \
1443 _dbus_assert ((insert_at) >= 0); \
1444 _dbus_assert ((insert_at) <= real_dest->len)
1447 * Moves the end of one string into another string. Both strings
1448 * must be initialized, valid strings.
1450 * @param source the source string
1451 * @param start where to chop off the source string
1452 * @param dest the destination string
1453 * @param insert_at where to move the chopped-off part of source string
1454 * @returns #FALSE if not enough memory
1457 _dbus_string_move (DBusString *source,
1462 DBusRealString *real_source = (DBusRealString*) source;
1463 _dbus_assert (start <= real_source->len);
1465 return _dbus_string_move_len (source, start,
1466 real_source->len - start,
1471 * Like _dbus_string_move(), but does not delete the section
1472 * of the source string that's copied to the dest string.
1474 * @param source the source string
1475 * @param start where to start copying the source string
1476 * @param dest the destination string
1477 * @param insert_at where to place the copied part of source string
1478 * @returns #FALSE if not enough memory
1481 _dbus_string_copy (const DBusString *source,
1486 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1488 return copy (real_source, start,
1489 real_source->len - start,
1495 * Like _dbus_string_move(), but can move a segment from
1496 * the middle of the source string.
1498 * @todo this doesn't do anything with max_length field.
1499 * we should probably just kill the max_length field though.
1501 * @param source the source string
1502 * @param start first byte of source string to move
1503 * @param len length of segment to move
1504 * @param dest the destination string
1505 * @param insert_at where to move the bytes from the source string
1506 * @returns #FALSE if not enough memory
1509 _dbus_string_move_len (DBusString *source,
1516 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1517 _dbus_assert (len >= 0);
1518 _dbus_assert ((start + len) <= real_source->len);
1525 else if (start == 0 &&
1526 len == real_source->len &&
1527 real_dest->len == 0)
1529 /* Short-circuit moving an entire existing string to an empty string
1530 * by just swapping the buffers.
1532 /* we assume ->constant doesn't matter as you can't have
1533 * a constant string involved in a move.
1535 #define ASSIGN_DATA(a, b) do { \
1536 (a)->str = (b)->str; \
1537 (a)->len = (b)->len; \
1538 (a)->allocated = (b)->allocated; \
1539 (a)->align_offset = (b)->align_offset; \
1544 ASSIGN_DATA (&tmp, real_source);
1545 ASSIGN_DATA (real_source, real_dest);
1546 ASSIGN_DATA (real_dest, &tmp);
1552 if (!copy (real_source, start, len,
1557 delete (real_source, start,
1565 * Like _dbus_string_copy(), but can copy a segment from the middle of
1566 * the source string.
1568 * @param source the source string
1569 * @param start where to start copying the source string
1570 * @param len length of segment to copy
1571 * @param dest the destination string
1572 * @param insert_at where to place the copied segment of source string
1573 * @returns #FALSE if not enough memory
1576 _dbus_string_copy_len (const DBusString *source,
1582 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1583 _dbus_assert (len >= 0);
1584 _dbus_assert (start <= real_source->len);
1585 _dbus_assert (len <= real_source->len - start);
1587 return copy (real_source, start, len,
1593 * Replaces a segment of dest string with a segment of source string.
1595 * @todo optimize the case where the two lengths are the same, and
1596 * avoid memmoving the data in the trailing part of the string twice.
1598 * @todo avoid inserting the source into dest, then deleting
1599 * the replaced chunk of dest (which creates a potentially large
1600 * intermediate string). Instead, extend the replaced chunk
1601 * of dest with padding to the same size as the source chunk,
1602 * then copy in the source bytes.
1604 * @param source the source string
1605 * @param start where to start copying the source string
1606 * @param len length of segment to copy
1607 * @param dest the destination string
1608 * @param replace_at start of segment of dest string to replace
1609 * @param replace_len length of segment of dest string to replace
1610 * @returns #FALSE if not enough memory
1614 _dbus_string_replace_len (const DBusString *source,
1621 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1622 _dbus_assert (len >= 0);
1623 _dbus_assert (start <= real_source->len);
1624 _dbus_assert (len <= real_source->len - start);
1625 _dbus_assert (replace_at >= 0);
1626 _dbus_assert (replace_at <= real_dest->len);
1627 _dbus_assert (replace_len <= real_dest->len - replace_at);
1629 if (!copy (real_source, start, len,
1630 real_dest, replace_at))
1633 delete (real_dest, replace_at + len, replace_len);
1638 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1639 * Pennington, and Tom Tromey are the authors and authorized relicense.
1642 /** computes length and mask of a unicode character
1643 * @param Char the char
1644 * @param Mask the mask variable to assign to
1645 * @param Len the length variable to assign to
1647 #define UTF8_COMPUTE(Char, Mask, Len) \
1653 else if ((Char & 0xe0) == 0xc0) \
1658 else if ((Char & 0xf0) == 0xe0) \
1663 else if ((Char & 0xf8) == 0xf0) \
1668 else if ((Char & 0xfc) == 0xf8) \
1673 else if ((Char & 0xfe) == 0xfc) \
1685 * computes length of a unicode character in UTF-8
1686 * @param Char the char
1688 #define UTF8_LENGTH(Char) \
1689 ((Char) < 0x80 ? 1 : \
1690 ((Char) < 0x800 ? 2 : \
1691 ((Char) < 0x10000 ? 3 : \
1692 ((Char) < 0x200000 ? 4 : \
1693 ((Char) < 0x4000000 ? 5 : 6)))))
1696 * Gets a UTF-8 value.
1698 * @param Result variable for extracted unicode char.
1699 * @param Chars the bytes to decode
1700 * @param Count counter variable
1701 * @param Mask mask for this char
1702 * @param Len length for this char in bytes
1704 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1705 (Result) = (Chars)[0] & (Mask); \
1706 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1708 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1714 (Result) |= ((Chars)[(Count)] & 0x3f); \
1718 * Check whether a unicode char is in a valid range.
1720 * @param Char the character
1722 #define UNICODE_VALID(Char) \
1723 ((Char) < 0x110000 && \
1724 (((Char) & 0xFFFFF800) != 0xD800) && \
1725 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1726 ((Char) & 0xFFFF) != 0xFFFF)
1729 * Gets a unicode character from a UTF-8 string. Does no validation;
1730 * you must verify that the string is valid UTF-8 in advance and must
1731 * pass in the start of a character.
1733 * @param str the string
1734 * @param start the start of the UTF-8 character.
1735 * @param ch_return location to return the character
1736 * @param end_return location to return the byte index of next character
1739 _dbus_string_get_unichar (const DBusString *str,
1741 dbus_unichar_t *ch_return,
1745 dbus_unichar_t result;
1748 DBUS_CONST_STRING_PREAMBLE (str);
1749 _dbus_assert (start >= 0);
1750 _dbus_assert (start <= real->len);
1755 *end_return = real->len;
1758 p = real->str + start;
1761 UTF8_COMPUTE (c, mask, len);
1764 UTF8_GET (result, p, i, mask, len);
1766 if (result == (dbus_unichar_t)-1)
1770 *ch_return = result;
1772 *end_return = start + len;
1776 * Finds the given substring in the string,
1777 * returning #TRUE and filling in the byte index
1778 * where the substring was found, if it was found.
1779 * Returns #FALSE if the substring wasn't found.
1780 * Sets *start to the length of the string if the substring
1783 * @param str the string
1784 * @param start where to start looking
1785 * @param substr the substring
1786 * @param found return location for where it was found, or #NULL
1787 * @returns #TRUE if found
1790 _dbus_string_find (const DBusString *str,
1795 return _dbus_string_find_to (str, start,
1796 ((const DBusRealString*)str)->len,
1801 * Finds the given substring in the string,
1802 * up to a certain position,
1803 * returning #TRUE and filling in the byte index
1804 * where the substring was found, if it was found.
1805 * Returns #FALSE if the substring wasn't found.
1806 * Sets *start to the length of the string if the substring
1809 * @param str the string
1810 * @param start where to start looking
1811 * @param end where to stop looking
1812 * @param substr the substring
1813 * @param found return location for where it was found, or #NULL
1814 * @returns #TRUE if found
1817 _dbus_string_find_to (const DBusString *str,
1824 DBUS_CONST_STRING_PREAMBLE (str);
1825 _dbus_assert (substr != NULL);
1826 _dbus_assert (start <= real->len);
1827 _dbus_assert (start >= 0);
1828 _dbus_assert (substr != NULL);
1829 _dbus_assert (end <= real->len);
1830 _dbus_assert (start <= end);
1832 /* we always "find" an empty string */
1833 if (*substr == '\0')
1843 if (real->str[i] == substr[0])
1849 if (substr[j - i] == '\0')
1851 else if (real->str[j] != substr[j - i])
1857 if (substr[j - i] == '\0')
1875 * Find the given byte scanning backward from the given start.
1876 * Sets *found to -1 if the byte is not found.
1878 * @param str the string
1879 * @param start the place to start scanning (will not find the byte at this point)
1880 * @param byte the byte to find
1881 * @param found return location for where it was found
1882 * @returns #TRUE if found
1885 _dbus_string_find_byte_backward (const DBusString *str,
1891 DBUS_CONST_STRING_PREAMBLE (str);
1892 _dbus_assert (start <= real->len);
1893 _dbus_assert (start >= 0);
1894 _dbus_assert (found != NULL);
1899 if (real->str[i] == byte)
1912 * Finds a blank (space or tab) in the string. Returns #TRUE
1913 * if found, #FALSE otherwise. If a blank is not found sets
1914 * *found to the length of the string.
1916 * @param str the string
1917 * @param start byte index to start looking
1918 * @param found place to store the location of the first blank
1919 * @returns #TRUE if a blank was found
1922 _dbus_string_find_blank (const DBusString *str,
1927 DBUS_CONST_STRING_PREAMBLE (str);
1928 _dbus_assert (start <= real->len);
1929 _dbus_assert (start >= 0);
1932 while (i < real->len)
1934 if (real->str[i] == ' ' ||
1935 real->str[i] == '\t')
1952 * Skips blanks from start, storing the first non-blank in *end
1953 * (blank is space or tab).
1955 * @param str the string
1956 * @param start where to start
1957 * @param end where to store the first non-blank byte index
1960 _dbus_string_skip_blank (const DBusString *str,
1965 DBUS_CONST_STRING_PREAMBLE (str);
1966 _dbus_assert (start <= real->len);
1967 _dbus_assert (start >= 0);
1970 while (i < real->len)
1972 if (!(real->str[i] == ' ' ||
1973 real->str[i] == '\t'))
1979 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1980 real->str[i] == '\t'));
1987 * Skips whitespace from start, storing the first non-whitespace in *end.
1988 * (whitespace is space, tab, newline, CR).
1990 * @param str the string
1991 * @param start where to start
1992 * @param end where to store the first non-whitespace byte index
1995 _dbus_string_skip_white (const DBusString *str,
2000 DBUS_CONST_STRING_PREAMBLE (str);
2001 _dbus_assert (start <= real->len);
2002 _dbus_assert (start >= 0);
2005 while (i < real->len)
2007 if (!(real->str[i] == ' ' ||
2008 real->str[i] == '\n' ||
2009 real->str[i] == '\r' ||
2010 real->str[i] == '\t'))
2016 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
2017 real->str[i] == '\t'));
2024 * Assigns a newline-terminated or \\r\\n-terminated line from the front
2025 * of the string to the given dest string. The dest string's previous
2026 * contents are deleted. If the source string contains no newline,
2027 * moves the entire source string to the dest string.
2029 * @todo owen correctly notes that this is a stupid function (it was
2030 * written purely for test code,
2031 * e.g. dbus-message-builder.c). Probably should be enforced as test
2032 * code only with #ifdef DBUS_BUILD_TESTS
2034 * @param source the source string
2035 * @param dest the destination string (contents are replaced)
2036 * @returns #FALSE if no memory, or source has length 0
2039 _dbus_string_pop_line (DBusString *source,
2043 dbus_bool_t have_newline;
2045 _dbus_string_set_length (dest, 0);
2048 if (_dbus_string_find (source, 0, "\n", &eol))
2050 have_newline = TRUE;
2051 eol += 1; /* include newline */
2055 eol = _dbus_string_get_length (source);
2056 have_newline = FALSE;
2060 return FALSE; /* eof */
2062 if (!_dbus_string_move_len (source, 0, eol,
2068 /* dump the newline and the \r if we have one */
2071 dbus_bool_t have_cr;
2073 _dbus_assert (_dbus_string_get_length (dest) > 0);
2075 if (_dbus_string_get_length (dest) > 1 &&
2076 _dbus_string_get_byte (dest,
2077 _dbus_string_get_length (dest) - 2) == '\r')
2082 _dbus_string_set_length (dest,
2083 _dbus_string_get_length (dest) -
2091 * Deletes up to and including the first blank space
2094 * @param str the string
2097 _dbus_string_delete_first_word (DBusString *str)
2101 if (_dbus_string_find_blank (str, 0, &i))
2102 _dbus_string_skip_blank (str, i, &i);
2104 _dbus_string_delete (str, 0, i);
2108 * Deletes any leading blanks in the string
2110 * @param str the string
2113 _dbus_string_delete_leading_blanks (DBusString *str)
2117 _dbus_string_skip_blank (str, 0, &i);
2120 _dbus_string_delete (str, 0, i);
2124 * Tests two DBusString for equality.
2126 * @todo memcmp is probably faster
2128 * @param a first string
2129 * @param b second string
2130 * @returns #TRUE if equal
2133 _dbus_string_equal (const DBusString *a,
2134 const DBusString *b)
2136 const unsigned char *ap;
2137 const unsigned char *bp;
2138 const unsigned char *a_end;
2139 const DBusRealString *real_a = (const DBusRealString*) a;
2140 const DBusRealString *real_b = (const DBusRealString*) b;
2141 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2142 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2144 if (real_a->len != real_b->len)
2149 a_end = real_a->str + real_a->len;
2163 * Tests two DBusString for equality up to the given length.
2164 * The strings may be shorter than the given length.
2166 * @todo write a unit test
2168 * @todo memcmp is probably faster
2170 * @param a first string
2171 * @param b second string
2172 * @param len the maximum length to look at
2173 * @returns #TRUE if equal for the given number of bytes
2176 _dbus_string_equal_len (const DBusString *a,
2177 const DBusString *b,
2180 const unsigned char *ap;
2181 const unsigned char *bp;
2182 const unsigned char *a_end;
2183 const DBusRealString *real_a = (const DBusRealString*) a;
2184 const DBusRealString *real_b = (const DBusRealString*) b;
2185 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2186 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2188 if (real_a->len != real_b->len &&
2189 (real_a->len < len || real_b->len < len))
2194 a_end = real_a->str + MIN (real_a->len, len);
2208 * Tests two sub-parts of two DBusString for equality. The specified
2209 * range of the first string must exist; the specified start position
2210 * of the second string must exist.
2212 * @todo write a unit test
2214 * @todo memcmp is probably faster
2216 * @param a first string
2217 * @param a_start where to start substring in first string
2218 * @param a_len length of substring in first string
2219 * @param b second string
2220 * @param b_start where to start substring in second string
2221 * @returns #TRUE if the two substrings are equal
2224 _dbus_string_equal_substring (const DBusString *a,
2227 const DBusString *b,
2230 const unsigned char *ap;
2231 const unsigned char *bp;
2232 const unsigned char *a_end;
2233 const DBusRealString *real_a = (const DBusRealString*) a;
2234 const DBusRealString *real_b = (const DBusRealString*) b;
2235 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2236 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2237 _dbus_assert (a_start >= 0);
2238 _dbus_assert (a_len >= 0);
2239 _dbus_assert (a_start <= real_a->len);
2240 _dbus_assert (a_len <= real_a->len - a_start);
2241 _dbus_assert (b_start >= 0);
2242 _dbus_assert (b_start <= real_b->len);
2244 if (a_len > real_b->len - b_start)
2247 ap = real_a->str + a_start;
2248 bp = real_b->str + b_start;
2259 _dbus_assert (bp <= (real_b->str + real_b->len));
2265 * Checks whether a string is equal to a C string.
2267 * @param a the string
2268 * @param c_str the C string
2269 * @returns #TRUE if equal
2272 _dbus_string_equal_c_str (const DBusString *a,
2275 const unsigned char *ap;
2276 const unsigned char *bp;
2277 const unsigned char *a_end;
2278 const DBusRealString *real_a = (const DBusRealString*) a;
2279 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2280 _dbus_assert (c_str != NULL);
2283 bp = (const unsigned char*) c_str;
2284 a_end = real_a->str + real_a->len;
2285 while (ap != a_end && *bp)
2294 if (ap != a_end || *bp)
2301 * Checks whether a string starts with the given C string.
2303 * @param a the string
2304 * @param c_str the C string
2305 * @returns #TRUE if string starts with it
2308 _dbus_string_starts_with_c_str (const DBusString *a,
2311 const unsigned char *ap;
2312 const unsigned char *bp;
2313 const unsigned char *a_end;
2314 const DBusRealString *real_a = (const DBusRealString*) a;
2315 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2316 _dbus_assert (c_str != NULL);
2319 bp = (const unsigned char*) c_str;
2320 a_end = real_a->str + real_a->len;
2321 while (ap != a_end && *bp)
2337 * Returns whether a string ends with the given suffix
2339 * @todo memcmp might make this faster.
2341 * @param a the string
2342 * @param c_str the C-style string
2343 * @returns #TRUE if the string ends with the suffix
2346 _dbus_string_ends_with_c_str (const DBusString *a,
2349 const unsigned char *ap;
2350 const unsigned char *bp;
2351 const unsigned char *a_end;
2352 unsigned long c_str_len;
2353 const DBusRealString *real_a = (const DBusRealString*) a;
2354 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2355 _dbus_assert (c_str != NULL);
2357 c_str_len = strlen (c_str);
2358 if (((unsigned long)real_a->len) < c_str_len)
2361 ap = real_a->str + (real_a->len - c_str_len);
2362 bp = (const unsigned char*) c_str;
2363 a_end = real_a->str + real_a->len;
2373 _dbus_assert (*ap == '\0');
2374 _dbus_assert (*bp == '\0');
2380 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2381 * encoded. (Each byte is two hex digits.)
2383 * @param source the string to encode
2384 * @param start byte index to start encoding
2385 * @param dest string where encoded data should be placed
2386 * @param insert_at where to place encoded data
2387 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2390 _dbus_string_hex_encode (const DBusString *source,
2396 const char hexdigits[16] = {
2397 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2398 'a', 'b', 'c', 'd', 'e', 'f'
2400 const unsigned char *p;
2401 const unsigned char *end;
2404 _dbus_assert (start <= _dbus_string_get_length (source));
2406 if (!_dbus_string_init (&result))
2411 p = (const unsigned char*) _dbus_string_get_const_data (source);
2412 end = p + _dbus_string_get_length (source);
2417 if (!_dbus_string_append_byte (&result,
2418 hexdigits[(*p >> 4)]))
2421 if (!_dbus_string_append_byte (&result,
2422 hexdigits[(*p & 0x0f)]))
2428 if (!_dbus_string_move (&result, 0, dest, insert_at))
2434 _dbus_string_free (&result);
2439 * Decodes a string from hex encoding.
2441 * @param source the string to decode
2442 * @param start byte index to start decode
2443 * @param end_return return location of the end of the hex data, or #NULL
2444 * @param dest string where decoded data should be placed
2445 * @param insert_at where to place decoded data
2446 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2449 _dbus_string_hex_decode (const DBusString *source,
2456 const unsigned char *p;
2457 const unsigned char *end;
2459 dbus_bool_t high_bits;
2461 _dbus_assert (start <= _dbus_string_get_length (source));
2463 if (!_dbus_string_init (&result))
2469 p = (const unsigned char*) _dbus_string_get_const_data (source);
2470 end = p + _dbus_string_get_length (source);
2539 if (!_dbus_string_append_byte (&result,
2548 len = _dbus_string_get_length (&result);
2550 b = _dbus_string_get_byte (&result, len - 1);
2554 _dbus_string_set_byte (&result, len - 1, b);
2557 high_bits = !high_bits;
2563 if (!_dbus_string_move (&result, 0, dest, insert_at))
2567 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2572 _dbus_string_free (&result);
2577 * Checks that the given range of the string is valid ASCII with no
2578 * nul bytes. If the given range is not entirely contained in the
2579 * string, returns #FALSE.
2581 * @todo this is inconsistent with most of DBusString in that
2582 * it allows a start,len range that extends past the string end.
2584 * @param str the string
2585 * @param start first byte index to check
2586 * @param len number of bytes to check
2587 * @returns #TRUE if the byte range exists and is all valid ASCII
2590 _dbus_string_validate_ascii (const DBusString *str,
2594 const unsigned char *s;
2595 const unsigned char *end;
2596 DBUS_CONST_STRING_PREAMBLE (str);
2597 _dbus_assert (start >= 0);
2598 _dbus_assert (start <= real->len);
2599 _dbus_assert (len >= 0);
2601 if (len > real->len - start)
2604 s = real->str + start;
2608 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2618 * Checks that the given range of the string is valid UTF-8. If the
2619 * given range is not entirely contained in the string, returns
2620 * #FALSE. If the string contains any nul bytes in the given range,
2621 * returns #FALSE. If the start and start+len are not on character
2622 * boundaries, returns #FALSE.
2624 * @todo this is inconsistent with most of DBusString in that
2625 * it allows a start,len range that extends past the string end.
2627 * @param str the string
2628 * @param start first byte index to check
2629 * @param len number of bytes to check
2630 * @returns #TRUE if the byte range exists and is all valid UTF-8
2633 _dbus_string_validate_utf8 (const DBusString *str,
2637 const unsigned char *p;
2638 const unsigned char *end;
2639 DBUS_CONST_STRING_PREAMBLE (str);
2640 _dbus_assert (start >= 0);
2641 _dbus_assert (start <= real->len);
2642 _dbus_assert (len >= 0);
2644 /* we are doing _DBUS_UNLIKELY() here which might be
2645 * dubious in a generic library like GLib, but in D-BUS
2646 * we know we're validating messages and that it would
2647 * only be evil/broken apps that would have invalid
2648 * UTF-8. Also, this function seems to be a performance
2649 * bottleneck in profiles.
2652 if (_DBUS_UNLIKELY (len > real->len - start))
2655 p = real->str + start;
2660 int i, mask, char_len;
2661 dbus_unichar_t result;
2663 /* nul bytes considered invalid */
2667 /* Special-case ASCII; this makes us go a lot faster in
2668 * D-BUS profiles where we are typically validating
2669 * function names and such. We have to know that
2670 * all following checks will pass for ASCII though,
2671 * comments follow ...
2679 UTF8_COMPUTE (*p, mask, char_len);
2681 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2684 /* check that the expected number of bytes exists in the remaining length */
2685 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2688 UTF8_GET (result, p, i, mask, char_len);
2690 /* Check for overlong UTF-8 */
2691 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2694 /* The UNICODE_VALID check below will catch this */
2695 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2699 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2702 /* UNICODE_VALID should have caught it */
2703 _dbus_assert (result != (dbus_unichar_t)-1);
2708 /* See that we covered the entire length if a length was
2711 if (_DBUS_UNLIKELY (p != end))
2718 * Checks that the given range of the string is all nul bytes. If the
2719 * given range is not entirely contained in the string, returns
2722 * @todo this is inconsistent with most of DBusString in that
2723 * it allows a start,len range that extends past the string end.
2725 * @param str the string
2726 * @param start first byte index to check
2727 * @param len number of bytes to check
2728 * @returns #TRUE if the byte range exists and is all nul bytes
2731 _dbus_string_validate_nul (const DBusString *str,
2735 const unsigned char *s;
2736 const unsigned char *end;
2737 DBUS_CONST_STRING_PREAMBLE (str);
2738 _dbus_assert (start >= 0);
2739 _dbus_assert (len >= 0);
2740 _dbus_assert (start <= real->len);
2742 if (len > real->len - start)
2745 s = real->str + start;
2749 if (_DBUS_UNLIKELY (*s != '\0'))
2758 * Clears all allocated bytes in the string to zero.
2760 * @param str the string
2763 _dbus_string_zero (DBusString *str)
2765 DBUS_STRING_PREAMBLE (str);
2767 memset (real->str - real->align_offset, '\0', real->allocated);
2771 #ifdef DBUS_BUILD_TESTS
2772 #include "dbus-test.h"
2776 * Parses a basic type defined by type contained in a DBusString. The
2777 * end_return parameter may be #NULL if you aren't interested in it. The
2778 * type is parsed and stored in value_return. Return parameters are not
2779 * initialized if the function returns #FALSE.
2781 * @param str the string
2782 * @param type the type of the basic type
2783 * @param start the byte index of the start of the type
2784 * @param value_return return location of the value or #NULL
2785 * @param end_return return location of the end of the type, or #NULL
2786 * @returns #TRUE on success
2789 _dbus_string_parse_basic_type (const DBusString *str,
2799 case DBUS_TYPE_BOOLEAN:
2801 int len = _dbus_string_get_length (str) - start;
2802 if (len >= 5 && _dbus_string_find_to (str, start, start + 5, "false", NULL))
2805 *(unsigned char *) value = TRUE;
2807 else if (len >= 4 && _dbus_string_find_to (str, start, start + 4, "true", NULL))
2810 *(unsigned char *) value = FALSE;
2813 _dbus_warn ("could not parse BOOLEAN\n");
2816 case DBUS_TYPE_BYTE:
2820 if (_dbus_string_get_byte (str, start) == '\'' &&
2821 _dbus_string_get_length (str) >= start + 4 &&
2822 _dbus_string_get_byte (str, start + 1) == '\\' &&
2823 _dbus_string_get_byte (str, start + 2) == '\'' &&
2824 _dbus_string_get_byte (str, start + 3) == '\'')
2829 else if (_dbus_string_get_byte (str, start) == '\'' &&
2830 _dbus_string_get_length (str) >= start + 3 &&
2831 _dbus_string_get_byte (str, start + 2) == '\'')
2833 val = _dbus_string_get_byte (str, start + 1);
2838 if (!_dbus_string_parse_int (str, start, &val, &end))
2839 _dbus_warn ("Failed to parse integer for BYTE\n");
2843 _dbus_warn ("A byte must be in range 0-255 not %ld\n", val);
2845 *(unsigned char *) value = val;
2848 case DBUS_TYPE_INT32:
2851 if (_dbus_string_parse_int (str, start, &val, &end))
2852 *(dbus_int32_t *)value = val;
2855 case DBUS_TYPE_UINT32:
2858 if (_dbus_string_parse_uint (str, start, &val, &end))
2859 *(dbus_uint32_t *)value = val;
2862 #ifdef DBUS_HAVE_INT64
2863 case DBUS_TYPE_INT64:
2864 case DBUS_TYPE_UINT64:
2865 /* use stroll oull */
2866 _dbus_assert_not_reached ("string -> [u]int64 not supported yet");
2868 #endif /* DBUS_HAVE_INT64 */
2869 case DBUS_TYPE_DOUBLE:
2870 _dbus_string_parse_double (str, start, value, &end);
2873 _dbus_assert_not_reached ("not a basic type");
2879 return end != start;
2883 test_max_len (DBusString *str,
2888 if (!_dbus_string_set_length (str, max_len - 1))
2889 _dbus_assert_not_reached ("setting len to one less than max should have worked");
2892 if (!_dbus_string_set_length (str, max_len))
2893 _dbus_assert_not_reached ("setting len to max len should have worked");
2895 if (_dbus_string_set_length (str, max_len + 1))
2896 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
2898 if (!_dbus_string_set_length (str, 0))
2899 _dbus_assert_not_reached ("setting len to zero should have worked");
2903 test_hex_roundtrip (const unsigned char *data,
2912 len = strlen (data);
2914 if (!_dbus_string_init (&orig))
2915 _dbus_assert_not_reached ("could not init string");
2917 if (!_dbus_string_init (&encoded))
2918 _dbus_assert_not_reached ("could not init string");
2920 if (!_dbus_string_init (&decoded))
2921 _dbus_assert_not_reached ("could not init string");
2923 if (!_dbus_string_append_len (&orig, data, len))
2924 _dbus_assert_not_reached ("couldn't append orig data");
2926 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
2927 _dbus_assert_not_reached ("could not encode");
2929 if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
2930 _dbus_assert_not_reached ("could not decode");
2932 _dbus_assert (_dbus_string_get_length (&encoded) == end);
2934 if (!_dbus_string_equal (&orig, &decoded))
2938 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
2939 _dbus_string_get_length (&orig),
2940 _dbus_string_get_length (&encoded),
2941 _dbus_string_get_length (&decoded));
2942 printf ("Original: %s\n", data);
2943 s = _dbus_string_get_const_data (&decoded);
2944 printf ("Decoded: %s\n", s);
2945 _dbus_assert_not_reached ("original string not the same as string decoded from hex");
2948 _dbus_string_free (&orig);
2949 _dbus_string_free (&encoded);
2950 _dbus_string_free (&decoded);
2953 typedef void (* TestRoundtripFunc) (const unsigned char *data,
2956 test_roundtrips (TestRoundtripFunc func)
2958 (* func) ("Hello this is a string\n", -1);
2959 (* func) ("Hello this is a string\n1", -1);
2960 (* func) ("Hello this is a string\n12", -1);
2961 (* func) ("Hello this is a string\n123", -1);
2962 (* func) ("Hello this is a string\n1234", -1);
2963 (* func) ("Hello this is a string\n12345", -1);
2967 (* func) ("123", 3);
2968 (* func) ("1234", 4);
2969 (* func) ("12345", 5);
2973 (* func) ("123", 4);
2974 (* func) ("1234", 5);
2975 (* func) ("12345", 6);
2977 unsigned char buf[512];
2981 while (i < _DBUS_N_ELEMENTS (buf))
2987 while (i < _DBUS_N_ELEMENTS (buf))
2997 * @ingroup DBusStringInternals
2998 * Unit test for DBusString.
3000 * @todo Need to write tests for _dbus_string_copy() and
3001 * _dbus_string_move() moving to/from each of start/middle/end of a
3002 * string. Also need tests for _dbus_string_move_len ()
3004 * @returns #TRUE on success.
3007 _dbus_string_test (void)
3014 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 };
3019 while (i < _DBUS_N_ELEMENTS (lens))
3021 if (!_dbus_string_init (&str))
3022 _dbus_assert_not_reached ("failed to init string");
3024 set_max_length (&str, lens[i]);
3026 test_max_len (&str, lens[i]);
3027 _dbus_string_free (&str);
3032 /* Test shortening and setting length */
3034 while (i < _DBUS_N_ELEMENTS (lens))
3038 if (!_dbus_string_init (&str))
3039 _dbus_assert_not_reached ("failed to init string");
3041 set_max_length (&str, lens[i]);
3043 if (!_dbus_string_set_length (&str, lens[i]))
3044 _dbus_assert_not_reached ("failed to set string length");
3049 _dbus_assert (_dbus_string_get_length (&str) == j);
3052 _dbus_string_shorten (&str, 1);
3053 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
3058 _dbus_string_free (&str);
3063 /* Test appending data */
3064 if (!_dbus_string_init (&str))
3065 _dbus_assert_not_reached ("failed to init string");
3070 if (!_dbus_string_append (&str, "a"))
3071 _dbus_assert_not_reached ("failed to append string to string\n");
3073 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
3075 if (!_dbus_string_append_byte (&str, 'b'))
3076 _dbus_assert_not_reached ("failed to append byte to string\n");
3078 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
3083 _dbus_string_free (&str);
3085 /* Check steal_data */
3087 if (!_dbus_string_init (&str))
3088 _dbus_assert_not_reached ("failed to init string");
3090 if (!_dbus_string_append (&str, "Hello World"))
3091 _dbus_assert_not_reached ("could not append to string");
3093 i = _dbus_string_get_length (&str);
3095 if (!_dbus_string_steal_data (&str, &s))
3096 _dbus_assert_not_reached ("failed to steal data");
3098 _dbus_assert (_dbus_string_get_length (&str) == 0);
3099 _dbus_assert (((int)strlen (s)) == i);
3105 if (!_dbus_string_append (&str, "Hello World"))
3106 _dbus_assert_not_reached ("could not append to string");
3108 i = _dbus_string_get_length (&str);
3110 if (!_dbus_string_init (&other))
3111 _dbus_assert_not_reached ("could not init string");
3113 if (!_dbus_string_move (&str, 0, &other, 0))
3114 _dbus_assert_not_reached ("could not move");
3116 _dbus_assert (_dbus_string_get_length (&str) == 0);
3117 _dbus_assert (_dbus_string_get_length (&other) == i);
3119 if (!_dbus_string_append (&str, "Hello World"))
3120 _dbus_assert_not_reached ("could not append to string");
3122 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
3123 _dbus_assert_not_reached ("could not move");
3125 _dbus_assert (_dbus_string_get_length (&str) == 0);
3126 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3128 if (!_dbus_string_append (&str, "Hello World"))
3129 _dbus_assert_not_reached ("could not append to string");
3131 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3132 _dbus_assert_not_reached ("could not move");
3134 _dbus_assert (_dbus_string_get_length (&str) == 0);
3135 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3137 _dbus_string_free (&other);
3141 if (!_dbus_string_append (&str, "Hello World"))
3142 _dbus_assert_not_reached ("could not append to string");
3144 i = _dbus_string_get_length (&str);
3146 if (!_dbus_string_init (&other))
3147 _dbus_assert_not_reached ("could not init string");
3149 if (!_dbus_string_copy (&str, 0, &other, 0))
3150 _dbus_assert_not_reached ("could not copy");
3152 _dbus_assert (_dbus_string_get_length (&str) == i);
3153 _dbus_assert (_dbus_string_get_length (&other) == i);
3155 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
3156 _dbus_assert_not_reached ("could not copy");
3158 _dbus_assert (_dbus_string_get_length (&str) == i);
3159 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3160 _dbus_assert (_dbus_string_equal_c_str (&other,
3161 "Hello WorldHello World"));
3163 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3164 _dbus_assert_not_reached ("could not copy");
3166 _dbus_assert (_dbus_string_get_length (&str) == i);
3167 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3168 _dbus_assert (_dbus_string_equal_c_str (&other,
3169 "Hello WorldHello WorldHello World"));
3171 _dbus_string_free (&str);
3172 _dbus_string_free (&other);
3176 if (!_dbus_string_init (&str))
3177 _dbus_assert_not_reached ("failed to init string");
3179 if (!_dbus_string_append (&str, "Hello World"))
3180 _dbus_assert_not_reached ("could not append to string");
3182 i = _dbus_string_get_length (&str);
3184 if (!_dbus_string_init (&other))
3185 _dbus_assert_not_reached ("could not init string");
3187 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3188 &other, 0, _dbus_string_get_length (&other)))
3189 _dbus_assert_not_reached ("could not replace");
3191 _dbus_assert (_dbus_string_get_length (&str) == i);
3192 _dbus_assert (_dbus_string_get_length (&other) == i);
3193 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
3195 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3197 _dbus_assert_not_reached ("could not replace center space");
3199 _dbus_assert (_dbus_string_get_length (&str) == i);
3200 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3201 _dbus_assert (_dbus_string_equal_c_str (&other,
3202 "HelloHello WorldWorld"));
3205 if (!_dbus_string_replace_len (&str, 1, 1,
3207 _dbus_string_get_length (&other) - 1,
3209 _dbus_assert_not_reached ("could not replace end character");
3211 _dbus_assert (_dbus_string_get_length (&str) == i);
3212 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3213 _dbus_assert (_dbus_string_equal_c_str (&other,
3214 "HelloHello WorldWorle"));
3216 _dbus_string_free (&str);
3217 _dbus_string_free (&other);
3219 /* Check append/get unichar */
3221 if (!_dbus_string_init (&str))
3222 _dbus_assert_not_reached ("failed to init string");
3225 if (!_dbus_string_append_unichar (&str, 0xfffc))
3226 _dbus_assert_not_reached ("failed to append unichar");
3228 _dbus_string_get_unichar (&str, 0, &ch, &i);
3230 _dbus_assert (ch == 0xfffc);
3231 _dbus_assert (i == _dbus_string_get_length (&str));
3233 _dbus_string_free (&str);
3235 /* Check insert/set/get byte */
3237 if (!_dbus_string_init (&str))
3238 _dbus_assert_not_reached ("failed to init string");
3240 if (!_dbus_string_append (&str, "Hello"))
3241 _dbus_assert_not_reached ("failed to append Hello");
3243 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
3244 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
3245 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
3246 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
3247 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
3249 _dbus_string_set_byte (&str, 1, 'q');
3250 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
3252 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
3253 _dbus_assert_not_reached ("can't insert byte");
3255 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
3256 _dbus_assert_not_reached ("can't insert byte");
3258 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
3259 _dbus_assert_not_reached ("can't insert byte");
3261 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
3262 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
3263 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
3264 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
3265 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
3266 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
3267 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
3268 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
3269 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
3270 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
3271 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
3273 _dbus_string_free (&str);
3275 /* Check append/parse int/double */
3277 if (!_dbus_string_init (&str))
3278 _dbus_assert_not_reached ("failed to init string");
3280 if (!_dbus_string_append_int (&str, 27))
3281 _dbus_assert_not_reached ("failed to append int");
3283 i = _dbus_string_get_length (&str);
3285 if (!_dbus_string_parse_int (&str, 0, &v, &end))
3286 _dbus_assert_not_reached ("failed to parse int");
3288 _dbus_assert (v == 27);
3289 _dbus_assert (end == i);
3291 _dbus_string_free (&str);
3293 if (!_dbus_string_init (&str))
3294 _dbus_assert_not_reached ("failed to init string");
3296 if (!_dbus_string_append_double (&str, 50.3))
3297 _dbus_assert_not_reached ("failed to append float");
3299 i = _dbus_string_get_length (&str);
3301 if (!_dbus_string_parse_double (&str, 0, &d, &end))
3302 _dbus_assert_not_reached ("failed to parse float");
3304 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
3305 _dbus_assert (end == i);
3307 _dbus_string_free (&str);
3310 if (!_dbus_string_init (&str))
3311 _dbus_assert_not_reached ("failed to init string");
3313 if (!_dbus_string_append (&str, "Hello"))
3314 _dbus_assert_not_reached ("couldn't append to string");
3316 if (!_dbus_string_find (&str, 0, "He", &i))
3317 _dbus_assert_not_reached ("didn't find 'He'");
3318 _dbus_assert (i == 0);
3320 if (!_dbus_string_find (&str, 0, "Hello", &i))
3321 _dbus_assert_not_reached ("didn't find 'Hello'");
3322 _dbus_assert (i == 0);
3324 if (!_dbus_string_find (&str, 0, "ello", &i))
3325 _dbus_assert_not_reached ("didn't find 'ello'");
3326 _dbus_assert (i == 1);
3328 if (!_dbus_string_find (&str, 0, "lo", &i))
3329 _dbus_assert_not_reached ("didn't find 'lo'");
3330 _dbus_assert (i == 3);
3332 if (!_dbus_string_find (&str, 2, "lo", &i))
3333 _dbus_assert_not_reached ("didn't find 'lo'");
3334 _dbus_assert (i == 3);
3336 if (_dbus_string_find (&str, 4, "lo", &i))
3337 _dbus_assert_not_reached ("did find 'lo'");
3339 if (!_dbus_string_find (&str, 0, "l", &i))
3340 _dbus_assert_not_reached ("didn't find 'l'");
3341 _dbus_assert (i == 2);
3343 if (!_dbus_string_find (&str, 0, "H", &i))
3344 _dbus_assert_not_reached ("didn't find 'H'");
3345 _dbus_assert (i == 0);
3347 if (!_dbus_string_find (&str, 0, "", &i))
3348 _dbus_assert_not_reached ("didn't find ''");
3349 _dbus_assert (i == 0);
3351 if (_dbus_string_find (&str, 0, "Hello!", NULL))
3352 _dbus_assert_not_reached ("Did find 'Hello!'");
3354 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
3355 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
3357 if (_dbus_string_find (&str, 0, "ill", NULL))
3358 _dbus_assert_not_reached ("Did find 'ill'");
3360 if (_dbus_string_find (&str, 0, "q", NULL))
3361 _dbus_assert_not_reached ("Did find 'q'");
3363 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
3364 _dbus_assert_not_reached ("Didn't find 'He'");
3366 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
3367 _dbus_assert_not_reached ("Did find 'Hello'");
3369 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
3370 _dbus_assert_not_reached ("Did not find 'H'");
3371 _dbus_assert (i == 0);
3373 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
3374 _dbus_assert_not_reached ("Did not find 'o'");
3375 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
3377 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
3378 _dbus_assert_not_reached ("Did find 'o'");
3379 _dbus_assert (i == -1);
3381 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
3382 _dbus_assert_not_reached ("Did find 'e'");
3383 _dbus_assert (i == -1);
3385 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
3386 _dbus_assert_not_reached ("Didn't find 'e'");
3387 _dbus_assert (i == 1);
3389 _dbus_string_free (&str);
3392 _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
3393 if (!_dbus_string_init (&other))
3394 _dbus_assert_not_reached ("could not init string");
3396 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
3397 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
3399 _dbus_assert (end == 8);
3401 _dbus_string_free (&other);
3403 test_roundtrips (test_hex_roundtrip);
3405 _dbus_string_free (&str);
3410 #endif /* DBUS_BUILD_TESTS */