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 avail_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 /** assign 4 bytes from one string to another */
1050 #define ASSIGN_4_OCTETS(p, octets) \
1051 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
1053 #ifdef DBUS_HAVE_INT64
1054 /** assign 8 bytes from one string to another */
1055 #define ASSIGN_8_OCTETS(p, octets) \
1056 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
1058 /** assign 8 bytes from one string to another */
1059 #define ASSIGN_8_OCTETS(p, octets) \
1073 _dbus_assert (b == p + 8); \
1075 #endif /* DBUS_HAVE_INT64 */
1078 * Appends 4 bytes aligned on a 4 byte boundary
1079 * with any alignment padding initialized to 0.
1081 * @param str the DBusString
1082 * @param octets 4 bytes to append
1083 * @returns #FALSE if not enough memory.
1086 _dbus_string_append_4_aligned (DBusString *str,
1087 const unsigned char octets[4])
1089 DBUS_STRING_PREAMBLE (str);
1091 if (!align_length_then_lengthen (str, 4, 4))
1094 ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
1100 * Appends 8 bytes aligned on an 8 byte boundary
1101 * with any alignment padding initialized to 0.
1103 * @param str the DBusString
1104 * @param octets 8 bytes to append
1105 * @returns #FALSE if not enough memory.
1108 _dbus_string_append_8_aligned (DBusString *str,
1109 const unsigned char octets[8])
1111 DBUS_STRING_PREAMBLE (str);
1113 if (!align_length_then_lengthen (str, 8, 8))
1116 ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
1122 * Inserts 4 bytes aligned on a 4 byte boundary
1123 * with any alignment padding initialized to 0.
1125 * @param str the DBusString
1126 * @param insert_at where to insert
1127 * @param octets 4 bytes to insert
1128 * @returns #FALSE if not enough memory.
1131 _dbus_string_insert_4_aligned (DBusString *str,
1133 const unsigned char octets[4])
1135 DBUS_STRING_PREAMBLE (str);
1137 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1140 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1146 * Inserts 8 bytes aligned on an 8 byte boundary
1147 * with any alignment padding initialized to 0.
1149 * @param str the DBusString
1150 * @param insert_at where to insert
1151 * @param octets 8 bytes to insert
1152 * @returns #FALSE if not enough memory.
1155 _dbus_string_insert_8_aligned (DBusString *str,
1157 const unsigned char octets[8])
1159 DBUS_STRING_PREAMBLE (str);
1161 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1164 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1166 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1173 * Inserts padding at *insert_at such to align it to the given
1174 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1175 * to the aligned position.
1177 * @param str the DBusString
1178 * @param insert_at location to be aligned
1179 * @param alignment alignment boundary (1, 4, or 8)
1180 * @returns #FALSE if not enough memory.
1183 _dbus_string_insert_alignment (DBusString *str,
1187 DBUS_STRING_PREAMBLE (str);
1189 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1192 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1198 * Appends a printf-style formatted string
1199 * to the #DBusString.
1201 * @param str the string
1202 * @param format printf format
1203 * @param args variable argument list
1204 * @returns #FALSE if no memory
1207 _dbus_string_append_printf_valist (DBusString *str,
1215 DBUS_STRING_PREAMBLE (str);
1217 DBUS_VA_COPY (args_copy, args);
1219 /* Measure the message length without terminating nul */
1220 len = vsnprintf (&c, 1, format, args);
1222 if (!_dbus_string_lengthen (str, len))
1224 /* don't leak the copy */
1229 vsprintf (real->str + (real->len - len),
1238 * Appends a printf-style formatted string
1239 * to the #DBusString.
1241 * @param str the string
1242 * @param format printf format
1243 * @returns #FALSE if no memory
1246 _dbus_string_append_printf (DBusString *str,
1253 va_start (args, format);
1254 retval = _dbus_string_append_printf_valist (str, format, args);
1261 * Appends block of bytes with the given length to a DBusString.
1263 * @param str the DBusString
1264 * @param buffer the bytes to append
1265 * @param len the number of bytes to append
1266 * @returns #FALSE if not enough memory.
1269 _dbus_string_append_len (DBusString *str,
1273 DBUS_STRING_PREAMBLE (str);
1274 _dbus_assert (buffer != NULL);
1275 _dbus_assert (len >= 0);
1277 return append (real, buffer, len);
1281 * Appends a single byte to the string, returning #FALSE
1282 * if not enough memory.
1284 * @param str the string
1285 * @param byte the byte to append
1286 * @returns #TRUE on success
1289 _dbus_string_append_byte (DBusString *str,
1292 DBUS_STRING_PREAMBLE (str);
1294 if (!set_length (real, real->len + 1))
1297 real->str[real->len-1] = byte;
1303 * Appends a single Unicode character, encoding the character
1306 * @param str the string
1307 * @param ch the Unicode character
1310 _dbus_string_append_unichar (DBusString *str,
1318 DBUS_STRING_PREAMBLE (str);
1320 /* this code is from GLib but is pretty standard I think */
1329 else if (ch < 0x800)
1334 else if (ch < 0x10000)
1339 else if (ch < 0x200000)
1344 else if (ch < 0x4000000)
1355 if (len > (real->max_length - real->len))
1356 return FALSE; /* real->len + len would overflow */
1358 if (!set_length (real, real->len + len))
1361 out = real->str + (real->len - len);
1363 for (i = len - 1; i > 0; --i)
1365 out[i] = (ch & 0x3f) | 0x80;
1368 out[0] = ch | first;
1374 delete (DBusRealString *real,
1381 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1383 real->str[real->len] = '\0';
1387 * Deletes a segment of a DBusString with length len starting at
1388 * start. (Hint: to clear an entire string, setting length to 0
1389 * with _dbus_string_set_length() is easier.)
1391 * @param str the DBusString
1392 * @param start where to start deleting
1393 * @param len the number of bytes to delete
1396 _dbus_string_delete (DBusString *str,
1400 DBUS_STRING_PREAMBLE (str);
1401 _dbus_assert (start >= 0);
1402 _dbus_assert (len >= 0);
1403 _dbus_assert (start <= real->len);
1404 _dbus_assert (len <= real->len - start);
1406 delete (real, start, len);
1410 copy (DBusRealString *source,
1413 DBusRealString *dest,
1419 if (!open_gap (len, dest, insert_at))
1422 memcpy (dest->str + insert_at,
1423 source->str + start,
1430 * Checks assertions for two strings we're copying a segment between,
1431 * and declares real_source/real_dest variables.
1433 * @param source the source string
1434 * @param start the starting offset
1435 * @param dest the dest string
1436 * @param insert_at where the copied segment is inserted
1438 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1439 DBusRealString *real_source = (DBusRealString*) source; \
1440 DBusRealString *real_dest = (DBusRealString*) dest; \
1441 _dbus_assert ((source) != (dest)); \
1442 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1443 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1444 _dbus_assert (!real_dest->constant); \
1445 _dbus_assert (!real_dest->locked); \
1446 _dbus_assert ((start) >= 0); \
1447 _dbus_assert ((start) <= real_source->len); \
1448 _dbus_assert ((insert_at) >= 0); \
1449 _dbus_assert ((insert_at) <= real_dest->len)
1452 * Moves the end of one string into another string. Both strings
1453 * must be initialized, valid strings.
1455 * @param source the source string
1456 * @param start where to chop off the source string
1457 * @param dest the destination string
1458 * @param insert_at where to move the chopped-off part of source string
1459 * @returns #FALSE if not enough memory
1462 _dbus_string_move (DBusString *source,
1467 DBusRealString *real_source = (DBusRealString*) source;
1468 _dbus_assert (start <= real_source->len);
1470 return _dbus_string_move_len (source, start,
1471 real_source->len - start,
1476 * Like _dbus_string_move(), but does not delete the section
1477 * of the source string that's copied to the dest string.
1479 * @param source the source string
1480 * @param start where to start copying the source string
1481 * @param dest the destination string
1482 * @param insert_at where to place the copied part of source string
1483 * @returns #FALSE if not enough memory
1486 _dbus_string_copy (const DBusString *source,
1491 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1493 return copy (real_source, start,
1494 real_source->len - start,
1500 * Like _dbus_string_move(), but can move a segment from
1501 * the middle of the source string.
1503 * @todo this doesn't do anything with max_length field.
1504 * we should probably just kill the max_length field though.
1506 * @param source the source string
1507 * @param start first byte of source string to move
1508 * @param len length of segment to move
1509 * @param dest the destination string
1510 * @param insert_at where to move the bytes from the source string
1511 * @returns #FALSE if not enough memory
1514 _dbus_string_move_len (DBusString *source,
1521 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1522 _dbus_assert (len >= 0);
1523 _dbus_assert ((start + len) <= real_source->len);
1530 else if (start == 0 &&
1531 len == real_source->len &&
1532 real_dest->len == 0)
1534 /* Short-circuit moving an entire existing string to an empty string
1535 * by just swapping the buffers.
1537 /* we assume ->constant doesn't matter as you can't have
1538 * a constant string involved in a move.
1540 #define ASSIGN_DATA(a, b) do { \
1541 (a)->str = (b)->str; \
1542 (a)->len = (b)->len; \
1543 (a)->allocated = (b)->allocated; \
1544 (a)->align_offset = (b)->align_offset; \
1549 ASSIGN_DATA (&tmp, real_source);
1550 ASSIGN_DATA (real_source, real_dest);
1551 ASSIGN_DATA (real_dest, &tmp);
1557 if (!copy (real_source, start, len,
1562 delete (real_source, start,
1570 * Like _dbus_string_copy(), but can copy a segment from the middle of
1571 * the source string.
1573 * @param source the source string
1574 * @param start where to start copying the source string
1575 * @param len length of segment to copy
1576 * @param dest the destination string
1577 * @param insert_at where to place the copied segment of source string
1578 * @returns #FALSE if not enough memory
1581 _dbus_string_copy_len (const DBusString *source,
1587 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1588 _dbus_assert (len >= 0);
1589 _dbus_assert (start <= real_source->len);
1590 _dbus_assert (len <= real_source->len - start);
1592 return copy (real_source, start, len,
1598 * Replaces a segment of dest string with a segment of source string.
1600 * @todo optimize the case where the two lengths are the same, and
1601 * avoid memmoving the data in the trailing part of the string twice.
1603 * @todo avoid inserting the source into dest, then deleting
1604 * the replaced chunk of dest (which creates a potentially large
1605 * intermediate string). Instead, extend the replaced chunk
1606 * of dest with padding to the same size as the source chunk,
1607 * then copy in the source bytes.
1609 * @param source the source string
1610 * @param start where to start copying the source string
1611 * @param len length of segment to copy
1612 * @param dest the destination string
1613 * @param replace_at start of segment of dest string to replace
1614 * @param replace_len length of segment of dest string to replace
1615 * @returns #FALSE if not enough memory
1619 _dbus_string_replace_len (const DBusString *source,
1626 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1627 _dbus_assert (len >= 0);
1628 _dbus_assert (start <= real_source->len);
1629 _dbus_assert (len <= real_source->len - start);
1630 _dbus_assert (replace_at >= 0);
1631 _dbus_assert (replace_at <= real_dest->len);
1632 _dbus_assert (replace_len <= real_dest->len - replace_at);
1634 if (!copy (real_source, start, len,
1635 real_dest, replace_at))
1638 delete (real_dest, replace_at + len, replace_len);
1643 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1644 * Pennington, and Tom Tromey are the authors and authorized relicense.
1647 /** computes length and mask of a unicode character
1648 * @param Char the char
1649 * @param Mask the mask variable to assign to
1650 * @param Len the length variable to assign to
1652 #define UTF8_COMPUTE(Char, Mask, Len) \
1658 else if ((Char & 0xe0) == 0xc0) \
1663 else if ((Char & 0xf0) == 0xe0) \
1668 else if ((Char & 0xf8) == 0xf0) \
1673 else if ((Char & 0xfc) == 0xf8) \
1678 else if ((Char & 0xfe) == 0xfc) \
1690 * computes length of a unicode character in UTF-8
1691 * @param Char the char
1693 #define UTF8_LENGTH(Char) \
1694 ((Char) < 0x80 ? 1 : \
1695 ((Char) < 0x800 ? 2 : \
1696 ((Char) < 0x10000 ? 3 : \
1697 ((Char) < 0x200000 ? 4 : \
1698 ((Char) < 0x4000000 ? 5 : 6)))))
1701 * Gets a UTF-8 value.
1703 * @param Result variable for extracted unicode char.
1704 * @param Chars the bytes to decode
1705 * @param Count counter variable
1706 * @param Mask mask for this char
1707 * @param Len length for this char in bytes
1709 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1710 (Result) = (Chars)[0] & (Mask); \
1711 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1713 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1719 (Result) |= ((Chars)[(Count)] & 0x3f); \
1723 * Check whether a unicode char is in a valid range.
1725 * @param Char the character
1727 #define UNICODE_VALID(Char) \
1728 ((Char) < 0x110000 && \
1729 (((Char) & 0xFFFFF800) != 0xD800) && \
1730 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1731 ((Char) & 0xFFFF) != 0xFFFF)
1734 * Gets a unicode character from a UTF-8 string. Does no validation;
1735 * you must verify that the string is valid UTF-8 in advance and must
1736 * pass in the start of a character.
1738 * @param str the string
1739 * @param start the start of the UTF-8 character.
1740 * @param ch_return location to return the character
1741 * @param end_return location to return the byte index of next character
1744 _dbus_string_get_unichar (const DBusString *str,
1746 dbus_unichar_t *ch_return,
1750 dbus_unichar_t result;
1753 DBUS_CONST_STRING_PREAMBLE (str);
1754 _dbus_assert (start >= 0);
1755 _dbus_assert (start <= real->len);
1760 *end_return = real->len;
1763 p = real->str + start;
1766 UTF8_COMPUTE (c, mask, len);
1769 UTF8_GET (result, p, i, mask, len);
1771 if (result == (dbus_unichar_t)-1)
1775 *ch_return = result;
1777 *end_return = start + len;
1781 * Finds the given substring in the string,
1782 * returning #TRUE and filling in the byte index
1783 * where the substring was found, if it was found.
1784 * Returns #FALSE if the substring wasn't found.
1785 * Sets *start to the length of the string if the substring
1788 * @param str the string
1789 * @param start where to start looking
1790 * @param substr the substring
1791 * @param found return location for where it was found, or #NULL
1792 * @returns #TRUE if found
1795 _dbus_string_find (const DBusString *str,
1800 return _dbus_string_find_to (str, start,
1801 ((const DBusRealString*)str)->len,
1806 * Finds the given substring in the string,
1807 * up to a certain position,
1808 * returning #TRUE and filling in the byte index
1809 * where the substring was found, if it was found.
1810 * Returns #FALSE if the substring wasn't found.
1811 * Sets *start to the length of the string if the substring
1814 * @param str the string
1815 * @param start where to start looking
1816 * @param end where to stop looking
1817 * @param substr the substring
1818 * @param found return location for where it was found, or #NULL
1819 * @returns #TRUE if found
1822 _dbus_string_find_to (const DBusString *str,
1829 DBUS_CONST_STRING_PREAMBLE (str);
1830 _dbus_assert (substr != NULL);
1831 _dbus_assert (start <= real->len);
1832 _dbus_assert (start >= 0);
1833 _dbus_assert (substr != NULL);
1834 _dbus_assert (end <= real->len);
1835 _dbus_assert (start <= end);
1837 /* we always "find" an empty string */
1838 if (*substr == '\0')
1848 if (real->str[i] == substr[0])
1854 if (substr[j - i] == '\0')
1856 else if (real->str[j] != substr[j - i])
1862 if (substr[j - i] == '\0')
1880 * Find the given byte scanning backward from the given start.
1881 * Sets *found to -1 if the byte is not found.
1883 * @param str the string
1884 * @param start the place to start scanning (will not find the byte at this point)
1885 * @param byte the byte to find
1886 * @param found return location for where it was found
1887 * @returns #TRUE if found
1890 _dbus_string_find_byte_backward (const DBusString *str,
1896 DBUS_CONST_STRING_PREAMBLE (str);
1897 _dbus_assert (start <= real->len);
1898 _dbus_assert (start >= 0);
1899 _dbus_assert (found != NULL);
1904 if (real->str[i] == byte)
1917 * Finds a blank (space or tab) in the string. Returns #TRUE
1918 * if found, #FALSE otherwise. If a blank is not found sets
1919 * *found to the length of the string.
1921 * @param str the string
1922 * @param start byte index to start looking
1923 * @param found place to store the location of the first blank
1924 * @returns #TRUE if a blank was found
1927 _dbus_string_find_blank (const DBusString *str,
1932 DBUS_CONST_STRING_PREAMBLE (str);
1933 _dbus_assert (start <= real->len);
1934 _dbus_assert (start >= 0);
1937 while (i < real->len)
1939 if (real->str[i] == ' ' ||
1940 real->str[i] == '\t')
1957 * Skips blanks from start, storing the first non-blank in *end
1958 * (blank is space or tab).
1960 * @param str the string
1961 * @param start where to start
1962 * @param end where to store the first non-blank byte index
1965 _dbus_string_skip_blank (const DBusString *str,
1970 DBUS_CONST_STRING_PREAMBLE (str);
1971 _dbus_assert (start <= real->len);
1972 _dbus_assert (start >= 0);
1975 while (i < real->len)
1977 if (!(real->str[i] == ' ' ||
1978 real->str[i] == '\t'))
1984 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1985 real->str[i] == '\t'));
1992 * Skips whitespace from start, storing the first non-whitespace in *end.
1993 * (whitespace is space, tab, newline, CR).
1995 * @param str the string
1996 * @param start where to start
1997 * @param end where to store the first non-whitespace byte index
2000 _dbus_string_skip_white (const DBusString *str,
2005 DBUS_CONST_STRING_PREAMBLE (str);
2006 _dbus_assert (start <= real->len);
2007 _dbus_assert (start >= 0);
2010 while (i < real->len)
2012 if (!(real->str[i] == ' ' ||
2013 real->str[i] == '\n' ||
2014 real->str[i] == '\r' ||
2015 real->str[i] == '\t'))
2021 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
2022 real->str[i] == '\t'));
2029 * Assigns a newline-terminated or \\r\\n-terminated line from the front
2030 * of the string to the given dest string. The dest string's previous
2031 * contents are deleted. If the source string contains no newline,
2032 * moves the entire source string to the dest string.
2034 * @todo owen correctly notes that this is a stupid function (it was
2035 * written purely for test code,
2036 * e.g. dbus-message-builder.c). Probably should be enforced as test
2037 * code only with #ifdef DBUS_BUILD_TESTS
2039 * @param source the source string
2040 * @param dest the destination string (contents are replaced)
2041 * @returns #FALSE if no memory, or source has length 0
2044 _dbus_string_pop_line (DBusString *source,
2048 dbus_bool_t have_newline;
2050 _dbus_string_set_length (dest, 0);
2053 if (_dbus_string_find (source, 0, "\n", &eol))
2055 have_newline = TRUE;
2056 eol += 1; /* include newline */
2060 eol = _dbus_string_get_length (source);
2061 have_newline = FALSE;
2065 return FALSE; /* eof */
2067 if (!_dbus_string_move_len (source, 0, eol,
2073 /* dump the newline and the \r if we have one */
2076 dbus_bool_t have_cr;
2078 _dbus_assert (_dbus_string_get_length (dest) > 0);
2080 if (_dbus_string_get_length (dest) > 1 &&
2081 _dbus_string_get_byte (dest,
2082 _dbus_string_get_length (dest) - 2) == '\r')
2087 _dbus_string_set_length (dest,
2088 _dbus_string_get_length (dest) -
2096 * Deletes up to and including the first blank space
2099 * @param str the string
2102 _dbus_string_delete_first_word (DBusString *str)
2106 if (_dbus_string_find_blank (str, 0, &i))
2107 _dbus_string_skip_blank (str, i, &i);
2109 _dbus_string_delete (str, 0, i);
2113 * Deletes any leading blanks in the string
2115 * @param str the string
2118 _dbus_string_delete_leading_blanks (DBusString *str)
2122 _dbus_string_skip_blank (str, 0, &i);
2125 _dbus_string_delete (str, 0, i);
2129 * Tests two DBusString for equality.
2131 * @todo memcmp is probably faster
2133 * @param a first string
2134 * @param b second string
2135 * @returns #TRUE if equal
2138 _dbus_string_equal (const DBusString *a,
2139 const DBusString *b)
2141 const unsigned char *ap;
2142 const unsigned char *bp;
2143 const unsigned char *a_end;
2144 const DBusRealString *real_a = (const DBusRealString*) a;
2145 const DBusRealString *real_b = (const DBusRealString*) b;
2146 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2147 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2149 if (real_a->len != real_b->len)
2154 a_end = real_a->str + real_a->len;
2168 * Tests two DBusString for equality up to the given length.
2169 * The strings may be shorter than the given length.
2171 * @todo write a unit test
2173 * @todo memcmp is probably faster
2175 * @param a first string
2176 * @param b second string
2177 * @param len the maximum length to look at
2178 * @returns #TRUE if equal for the given number of bytes
2181 _dbus_string_equal_len (const DBusString *a,
2182 const DBusString *b,
2185 const unsigned char *ap;
2186 const unsigned char *bp;
2187 const unsigned char *a_end;
2188 const DBusRealString *real_a = (const DBusRealString*) a;
2189 const DBusRealString *real_b = (const DBusRealString*) b;
2190 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2191 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2193 if (real_a->len != real_b->len &&
2194 (real_a->len < len || real_b->len < len))
2199 a_end = real_a->str + MIN (real_a->len, len);
2213 * Tests two sub-parts of two DBusString for equality. The specified
2214 * range of the first string must exist; the specified start position
2215 * of the second string must exist.
2217 * @todo write a unit test
2219 * @todo memcmp is probably faster
2221 * @param a first string
2222 * @param a_start where to start substring in first string
2223 * @param a_len length of substring in first string
2224 * @param b second string
2225 * @param b_start where to start substring in second string
2226 * @returns #TRUE if the two substrings are equal
2229 _dbus_string_equal_substring (const DBusString *a,
2232 const DBusString *b,
2235 const unsigned char *ap;
2236 const unsigned char *bp;
2237 const unsigned char *a_end;
2238 const DBusRealString *real_a = (const DBusRealString*) a;
2239 const DBusRealString *real_b = (const DBusRealString*) b;
2240 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2241 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2242 _dbus_assert (a_start >= 0);
2243 _dbus_assert (a_len >= 0);
2244 _dbus_assert (a_start <= real_a->len);
2245 _dbus_assert (a_len <= real_a->len - a_start);
2246 _dbus_assert (b_start >= 0);
2247 _dbus_assert (b_start <= real_b->len);
2249 if (a_len > real_b->len - b_start)
2252 ap = real_a->str + a_start;
2253 bp = real_b->str + b_start;
2264 _dbus_assert (bp <= (real_b->str + real_b->len));
2270 * Checks whether a string is equal to a C string.
2272 * @param a the string
2273 * @param c_str the C string
2274 * @returns #TRUE if equal
2277 _dbus_string_equal_c_str (const DBusString *a,
2280 const unsigned char *ap;
2281 const unsigned char *bp;
2282 const unsigned char *a_end;
2283 const DBusRealString *real_a = (const DBusRealString*) a;
2284 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2285 _dbus_assert (c_str != NULL);
2288 bp = (const unsigned char*) c_str;
2289 a_end = real_a->str + real_a->len;
2290 while (ap != a_end && *bp)
2299 if (ap != a_end || *bp)
2306 * Checks whether a string starts with the given C string.
2308 * @param a the string
2309 * @param c_str the C string
2310 * @returns #TRUE if string starts with it
2313 _dbus_string_starts_with_c_str (const DBusString *a,
2316 const unsigned char *ap;
2317 const unsigned char *bp;
2318 const unsigned char *a_end;
2319 const DBusRealString *real_a = (const DBusRealString*) a;
2320 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2321 _dbus_assert (c_str != NULL);
2324 bp = (const unsigned char*) c_str;
2325 a_end = real_a->str + real_a->len;
2326 while (ap != a_end && *bp)
2342 * Returns whether a string ends with the given suffix
2344 * @todo memcmp might make this faster.
2346 * @param a the string
2347 * @param c_str the C-style string
2348 * @returns #TRUE if the string ends with the suffix
2351 _dbus_string_ends_with_c_str (const DBusString *a,
2354 const unsigned char *ap;
2355 const unsigned char *bp;
2356 const unsigned char *a_end;
2357 unsigned long c_str_len;
2358 const DBusRealString *real_a = (const DBusRealString*) a;
2359 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2360 _dbus_assert (c_str != NULL);
2362 c_str_len = strlen (c_str);
2363 if (((unsigned long)real_a->len) < c_str_len)
2366 ap = real_a->str + (real_a->len - c_str_len);
2367 bp = (const unsigned char*) c_str;
2368 a_end = real_a->str + real_a->len;
2378 _dbus_assert (*ap == '\0');
2379 _dbus_assert (*bp == '\0');
2385 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2386 * encoded. (Each byte is two hex digits.)
2388 * @param source the string to encode
2389 * @param start byte index to start encoding
2390 * @param dest string where encoded data should be placed
2391 * @param insert_at where to place encoded data
2392 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2395 _dbus_string_hex_encode (const DBusString *source,
2401 const char hexdigits[16] = {
2402 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2403 'a', 'b', 'c', 'd', 'e', 'f'
2405 const unsigned char *p;
2406 const unsigned char *end;
2409 _dbus_assert (start <= _dbus_string_get_length (source));
2411 if (!_dbus_string_init (&result))
2416 p = (const unsigned char*) _dbus_string_get_const_data (source);
2417 end = p + _dbus_string_get_length (source);
2422 if (!_dbus_string_append_byte (&result,
2423 hexdigits[(*p >> 4)]))
2426 if (!_dbus_string_append_byte (&result,
2427 hexdigits[(*p & 0x0f)]))
2433 if (!_dbus_string_move (&result, 0, dest, insert_at))
2439 _dbus_string_free (&result);
2444 * Decodes a string from hex encoding.
2446 * @param source the string to decode
2447 * @param start byte index to start decode
2448 * @param end_return return location of the end of the hex data, or #NULL
2449 * @param dest string where decoded data should be placed
2450 * @param insert_at where to place decoded data
2451 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2454 _dbus_string_hex_decode (const DBusString *source,
2461 const unsigned char *p;
2462 const unsigned char *end;
2464 dbus_bool_t high_bits;
2466 _dbus_assert (start <= _dbus_string_get_length (source));
2468 if (!_dbus_string_init (&result))
2474 p = (const unsigned char*) _dbus_string_get_const_data (source);
2475 end = p + _dbus_string_get_length (source);
2544 if (!_dbus_string_append_byte (&result,
2553 len = _dbus_string_get_length (&result);
2555 b = _dbus_string_get_byte (&result, len - 1);
2559 _dbus_string_set_byte (&result, len - 1, b);
2562 high_bits = !high_bits;
2568 if (!_dbus_string_move (&result, 0, dest, insert_at))
2572 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2577 _dbus_string_free (&result);
2582 * Checks that the given range of the string is valid ASCII with no
2583 * nul bytes. If the given range is not entirely contained in the
2584 * string, returns #FALSE.
2586 * @todo this is inconsistent with most of DBusString in that
2587 * it allows a start,len range that extends past the string end.
2589 * @param str the string
2590 * @param start first byte index to check
2591 * @param len number of bytes to check
2592 * @returns #TRUE if the byte range exists and is all valid ASCII
2595 _dbus_string_validate_ascii (const DBusString *str,
2599 const unsigned char *s;
2600 const unsigned char *end;
2601 DBUS_CONST_STRING_PREAMBLE (str);
2602 _dbus_assert (start >= 0);
2603 _dbus_assert (start <= real->len);
2604 _dbus_assert (len >= 0);
2606 if (len > real->len - start)
2609 s = real->str + start;
2613 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2623 * Checks that the given range of the string is valid UTF-8. If the
2624 * given range is not entirely contained in the string, returns
2625 * #FALSE. If the string contains any nul bytes in the given range,
2626 * returns #FALSE. If the start and start+len are not on character
2627 * boundaries, returns #FALSE.
2629 * @todo this is inconsistent with most of DBusString in that
2630 * it allows a start,len range that extends past the string end.
2632 * @param str the string
2633 * @param start first byte index to check
2634 * @param len number of bytes to check
2635 * @returns #TRUE if the byte range exists and is all valid UTF-8
2638 _dbus_string_validate_utf8 (const DBusString *str,
2642 const unsigned char *p;
2643 const unsigned char *end;
2644 DBUS_CONST_STRING_PREAMBLE (str);
2645 _dbus_assert (start >= 0);
2646 _dbus_assert (start <= real->len);
2647 _dbus_assert (len >= 0);
2649 /* we are doing _DBUS_UNLIKELY() here which might be
2650 * dubious in a generic library like GLib, but in D-BUS
2651 * we know we're validating messages and that it would
2652 * only be evil/broken apps that would have invalid
2653 * UTF-8. Also, this function seems to be a performance
2654 * bottleneck in profiles.
2657 if (_DBUS_UNLIKELY (len > real->len - start))
2660 p = real->str + start;
2665 int i, mask, char_len;
2666 dbus_unichar_t result;
2668 /* nul bytes considered invalid */
2672 /* Special-case ASCII; this makes us go a lot faster in
2673 * D-BUS profiles where we are typically validating
2674 * function names and such. We have to know that
2675 * all following checks will pass for ASCII though,
2676 * comments follow ...
2684 UTF8_COMPUTE (*p, mask, char_len);
2686 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2689 /* check that the expected number of bytes exists in the remaining length */
2690 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2693 UTF8_GET (result, p, i, mask, char_len);
2695 /* Check for overlong UTF-8 */
2696 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2699 /* The UNICODE_VALID check below will catch this */
2700 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2704 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2707 /* UNICODE_VALID should have caught it */
2708 _dbus_assert (result != (dbus_unichar_t)-1);
2713 /* See that we covered the entire length if a length was
2716 if (_DBUS_UNLIKELY (p != end))
2723 * Checks that the given range of the string is all nul bytes. If the
2724 * given range is not entirely contained in the string, returns
2727 * @todo this is inconsistent with most of DBusString in that
2728 * it allows a start,len range that extends past the string end.
2730 * @param str the string
2731 * @param start first byte index to check
2732 * @param len number of bytes to check
2733 * @returns #TRUE if the byte range exists and is all nul bytes
2736 _dbus_string_validate_nul (const DBusString *str,
2740 const unsigned char *s;
2741 const unsigned char *end;
2742 DBUS_CONST_STRING_PREAMBLE (str);
2743 _dbus_assert (start >= 0);
2744 _dbus_assert (len >= 0);
2745 _dbus_assert (start <= real->len);
2747 if (len > real->len - start)
2750 s = real->str + start;
2754 if (_DBUS_UNLIKELY (*s != '\0'))
2763 * Clears all allocated bytes in the string to zero.
2765 * @param str the string
2768 _dbus_string_zero (DBusString *str)
2770 DBUS_STRING_PREAMBLE (str);
2772 memset (real->str - real->align_offset, '\0', real->allocated);
2776 #ifdef DBUS_BUILD_TESTS
2777 #include "dbus-test.h"
2781 * Parses a basic type defined by type contained in a DBusString. The
2782 * end_return parameter may be #NULL if you aren't interested in it. The
2783 * type is parsed and stored in value_return. Return parameters are not
2784 * initialized if the function returns #FALSE.
2786 * @param str the string
2787 * @param type the type of the basic type
2788 * @param start the byte index of the start of the type
2789 * @param value_return return location of the value or #NULL
2790 * @param end_return return location of the end of the type, or #NULL
2791 * @returns #TRUE on success
2794 _dbus_string_parse_basic_type (const DBusString *str,
2804 case DBUS_TYPE_BOOLEAN:
2806 int len = _dbus_string_get_length (str) - start;
2807 if (len >= 5 && _dbus_string_find_to (str, start, start + 5, "false", NULL))
2810 *(unsigned char *) value = TRUE;
2812 else if (len >= 4 && _dbus_string_find_to (str, start, start + 4, "true", NULL))
2815 *(unsigned char *) value = FALSE;
2818 _dbus_warn ("could not parse BOOLEAN\n");
2821 case DBUS_TYPE_BYTE:
2825 if (_dbus_string_get_byte (str, start) == '\'' &&
2826 _dbus_string_get_length (str) >= start + 4 &&
2827 _dbus_string_get_byte (str, start + 1) == '\\' &&
2828 _dbus_string_get_byte (str, start + 2) == '\'' &&
2829 _dbus_string_get_byte (str, start + 3) == '\'')
2834 else if (_dbus_string_get_byte (str, start) == '\'' &&
2835 _dbus_string_get_length (str) >= start + 3 &&
2836 _dbus_string_get_byte (str, start + 2) == '\'')
2838 val = _dbus_string_get_byte (str, start + 1);
2843 if (!_dbus_string_parse_int (str, start, &val, &end))
2844 _dbus_warn ("Failed to parse integer for BYTE\n");
2848 _dbus_warn ("A byte must be in range 0-255 not %ld\n", val);
2850 *(unsigned char *) value = val;
2853 case DBUS_TYPE_INT32:
2856 if (_dbus_string_parse_int (str, start, &val, &end))
2857 *(dbus_int32_t *)value = val;
2860 case DBUS_TYPE_UINT32:
2863 if (_dbus_string_parse_uint (str, start, &val, &end))
2864 *(dbus_uint32_t *)value = val;
2867 #ifdef DBUS_HAVE_INT64
2868 case DBUS_TYPE_INT64:
2869 case DBUS_TYPE_UINT64:
2870 /* use stroll oull */
2871 _dbus_assert_not_reached ("string -> [u]int64 not supported yet");
2873 #endif /* DBUS_HAVE_INT64 */
2874 case DBUS_TYPE_DOUBLE:
2875 _dbus_string_parse_double (str, start, value, &end);
2878 _dbus_assert_not_reached ("not a basic type");
2884 return end != start;
2888 test_max_len (DBusString *str,
2893 if (!_dbus_string_set_length (str, max_len - 1))
2894 _dbus_assert_not_reached ("setting len to one less than max should have worked");
2897 if (!_dbus_string_set_length (str, max_len))
2898 _dbus_assert_not_reached ("setting len to max len should have worked");
2900 if (_dbus_string_set_length (str, max_len + 1))
2901 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
2903 if (!_dbus_string_set_length (str, 0))
2904 _dbus_assert_not_reached ("setting len to zero should have worked");
2908 test_hex_roundtrip (const unsigned char *data,
2917 len = strlen (data);
2919 if (!_dbus_string_init (&orig))
2920 _dbus_assert_not_reached ("could not init string");
2922 if (!_dbus_string_init (&encoded))
2923 _dbus_assert_not_reached ("could not init string");
2925 if (!_dbus_string_init (&decoded))
2926 _dbus_assert_not_reached ("could not init string");
2928 if (!_dbus_string_append_len (&orig, data, len))
2929 _dbus_assert_not_reached ("couldn't append orig data");
2931 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
2932 _dbus_assert_not_reached ("could not encode");
2934 if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
2935 _dbus_assert_not_reached ("could not decode");
2937 _dbus_assert (_dbus_string_get_length (&encoded) == end);
2939 if (!_dbus_string_equal (&orig, &decoded))
2943 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
2944 _dbus_string_get_length (&orig),
2945 _dbus_string_get_length (&encoded),
2946 _dbus_string_get_length (&decoded));
2947 printf ("Original: %s\n", data);
2948 s = _dbus_string_get_const_data (&decoded);
2949 printf ("Decoded: %s\n", s);
2950 _dbus_assert_not_reached ("original string not the same as string decoded from hex");
2953 _dbus_string_free (&orig);
2954 _dbus_string_free (&encoded);
2955 _dbus_string_free (&decoded);
2958 typedef void (* TestRoundtripFunc) (const unsigned char *data,
2961 test_roundtrips (TestRoundtripFunc func)
2963 (* func) ("Hello this is a string\n", -1);
2964 (* func) ("Hello this is a string\n1", -1);
2965 (* func) ("Hello this is a string\n12", -1);
2966 (* func) ("Hello this is a string\n123", -1);
2967 (* func) ("Hello this is a string\n1234", -1);
2968 (* func) ("Hello this is a string\n12345", -1);
2972 (* func) ("123", 3);
2973 (* func) ("1234", 4);
2974 (* func) ("12345", 5);
2978 (* func) ("123", 4);
2979 (* func) ("1234", 5);
2980 (* func) ("12345", 6);
2982 unsigned char buf[512];
2986 while (i < _DBUS_N_ELEMENTS (buf))
2992 while (i < _DBUS_N_ELEMENTS (buf))
3002 * @ingroup DBusStringInternals
3003 * Unit test for DBusString.
3005 * @todo Need to write tests for _dbus_string_copy() and
3006 * _dbus_string_move() moving to/from each of start/middle/end of a
3007 * string. Also need tests for _dbus_string_move_len ()
3009 * @returns #TRUE on success.
3012 _dbus_string_test (void)
3019 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 };
3024 while (i < _DBUS_N_ELEMENTS (lens))
3026 if (!_dbus_string_init (&str))
3027 _dbus_assert_not_reached ("failed to init string");
3029 set_max_length (&str, lens[i]);
3031 test_max_len (&str, lens[i]);
3032 _dbus_string_free (&str);
3037 /* Test shortening and setting length */
3039 while (i < _DBUS_N_ELEMENTS (lens))
3043 if (!_dbus_string_init (&str))
3044 _dbus_assert_not_reached ("failed to init string");
3046 set_max_length (&str, lens[i]);
3048 if (!_dbus_string_set_length (&str, lens[i]))
3049 _dbus_assert_not_reached ("failed to set string length");
3054 _dbus_assert (_dbus_string_get_length (&str) == j);
3057 _dbus_string_shorten (&str, 1);
3058 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
3063 _dbus_string_free (&str);
3068 /* Test appending data */
3069 if (!_dbus_string_init (&str))
3070 _dbus_assert_not_reached ("failed to init string");
3075 if (!_dbus_string_append (&str, "a"))
3076 _dbus_assert_not_reached ("failed to append string to string\n");
3078 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
3080 if (!_dbus_string_append_byte (&str, 'b'))
3081 _dbus_assert_not_reached ("failed to append byte to string\n");
3083 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
3088 _dbus_string_free (&str);
3090 /* Check steal_data */
3092 if (!_dbus_string_init (&str))
3093 _dbus_assert_not_reached ("failed to init string");
3095 if (!_dbus_string_append (&str, "Hello World"))
3096 _dbus_assert_not_reached ("could not append to string");
3098 i = _dbus_string_get_length (&str);
3100 if (!_dbus_string_steal_data (&str, &s))
3101 _dbus_assert_not_reached ("failed to steal data");
3103 _dbus_assert (_dbus_string_get_length (&str) == 0);
3104 _dbus_assert (((int)strlen (s)) == i);
3110 if (!_dbus_string_append (&str, "Hello World"))
3111 _dbus_assert_not_reached ("could not append to string");
3113 i = _dbus_string_get_length (&str);
3115 if (!_dbus_string_init (&other))
3116 _dbus_assert_not_reached ("could not init string");
3118 if (!_dbus_string_move (&str, 0, &other, 0))
3119 _dbus_assert_not_reached ("could not move");
3121 _dbus_assert (_dbus_string_get_length (&str) == 0);
3122 _dbus_assert (_dbus_string_get_length (&other) == i);
3124 if (!_dbus_string_append (&str, "Hello World"))
3125 _dbus_assert_not_reached ("could not append to string");
3127 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
3128 _dbus_assert_not_reached ("could not move");
3130 _dbus_assert (_dbus_string_get_length (&str) == 0);
3131 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3133 if (!_dbus_string_append (&str, "Hello World"))
3134 _dbus_assert_not_reached ("could not append to string");
3136 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3137 _dbus_assert_not_reached ("could not move");
3139 _dbus_assert (_dbus_string_get_length (&str) == 0);
3140 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3142 _dbus_string_free (&other);
3146 if (!_dbus_string_append (&str, "Hello World"))
3147 _dbus_assert_not_reached ("could not append to string");
3149 i = _dbus_string_get_length (&str);
3151 if (!_dbus_string_init (&other))
3152 _dbus_assert_not_reached ("could not init string");
3154 if (!_dbus_string_copy (&str, 0, &other, 0))
3155 _dbus_assert_not_reached ("could not copy");
3157 _dbus_assert (_dbus_string_get_length (&str) == i);
3158 _dbus_assert (_dbus_string_get_length (&other) == i);
3160 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
3161 _dbus_assert_not_reached ("could not copy");
3163 _dbus_assert (_dbus_string_get_length (&str) == i);
3164 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3165 _dbus_assert (_dbus_string_equal_c_str (&other,
3166 "Hello WorldHello World"));
3168 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3169 _dbus_assert_not_reached ("could not copy");
3171 _dbus_assert (_dbus_string_get_length (&str) == i);
3172 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3173 _dbus_assert (_dbus_string_equal_c_str (&other,
3174 "Hello WorldHello WorldHello World"));
3176 _dbus_string_free (&str);
3177 _dbus_string_free (&other);
3181 if (!_dbus_string_init (&str))
3182 _dbus_assert_not_reached ("failed to init string");
3184 if (!_dbus_string_append (&str, "Hello World"))
3185 _dbus_assert_not_reached ("could not append to string");
3187 i = _dbus_string_get_length (&str);
3189 if (!_dbus_string_init (&other))
3190 _dbus_assert_not_reached ("could not init string");
3192 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3193 &other, 0, _dbus_string_get_length (&other)))
3194 _dbus_assert_not_reached ("could not replace");
3196 _dbus_assert (_dbus_string_get_length (&str) == i);
3197 _dbus_assert (_dbus_string_get_length (&other) == i);
3198 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
3200 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3202 _dbus_assert_not_reached ("could not replace center space");
3204 _dbus_assert (_dbus_string_get_length (&str) == i);
3205 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3206 _dbus_assert (_dbus_string_equal_c_str (&other,
3207 "HelloHello WorldWorld"));
3210 if (!_dbus_string_replace_len (&str, 1, 1,
3212 _dbus_string_get_length (&other) - 1,
3214 _dbus_assert_not_reached ("could not replace end character");
3216 _dbus_assert (_dbus_string_get_length (&str) == i);
3217 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3218 _dbus_assert (_dbus_string_equal_c_str (&other,
3219 "HelloHello WorldWorle"));
3221 _dbus_string_free (&str);
3222 _dbus_string_free (&other);
3224 /* Check append/get unichar */
3226 if (!_dbus_string_init (&str))
3227 _dbus_assert_not_reached ("failed to init string");
3230 if (!_dbus_string_append_unichar (&str, 0xfffc))
3231 _dbus_assert_not_reached ("failed to append unichar");
3233 _dbus_string_get_unichar (&str, 0, &ch, &i);
3235 _dbus_assert (ch == 0xfffc);
3236 _dbus_assert (i == _dbus_string_get_length (&str));
3238 _dbus_string_free (&str);
3240 /* Check insert/set/get byte */
3242 if (!_dbus_string_init (&str))
3243 _dbus_assert_not_reached ("failed to init string");
3245 if (!_dbus_string_append (&str, "Hello"))
3246 _dbus_assert_not_reached ("failed to append Hello");
3248 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
3249 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
3250 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
3251 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
3252 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
3254 _dbus_string_set_byte (&str, 1, 'q');
3255 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
3257 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
3258 _dbus_assert_not_reached ("can't insert byte");
3260 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
3261 _dbus_assert_not_reached ("can't insert byte");
3263 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
3264 _dbus_assert_not_reached ("can't insert byte");
3266 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
3267 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
3268 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
3269 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
3270 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
3271 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
3272 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
3273 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
3274 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
3275 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
3276 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
3278 _dbus_string_free (&str);
3280 /* Check append/parse int/double */
3282 if (!_dbus_string_init (&str))
3283 _dbus_assert_not_reached ("failed to init string");
3285 if (!_dbus_string_append_int (&str, 27))
3286 _dbus_assert_not_reached ("failed to append int");
3288 i = _dbus_string_get_length (&str);
3290 if (!_dbus_string_parse_int (&str, 0, &v, &end))
3291 _dbus_assert_not_reached ("failed to parse int");
3293 _dbus_assert (v == 27);
3294 _dbus_assert (end == i);
3296 _dbus_string_free (&str);
3298 if (!_dbus_string_init (&str))
3299 _dbus_assert_not_reached ("failed to init string");
3301 if (!_dbus_string_append_double (&str, 50.3))
3302 _dbus_assert_not_reached ("failed to append float");
3304 i = _dbus_string_get_length (&str);
3306 if (!_dbus_string_parse_double (&str, 0, &d, &end))
3307 _dbus_assert_not_reached ("failed to parse float");
3309 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
3310 _dbus_assert (end == i);
3312 _dbus_string_free (&str);
3315 if (!_dbus_string_init (&str))
3316 _dbus_assert_not_reached ("failed to init string");
3318 if (!_dbus_string_append (&str, "Hello"))
3319 _dbus_assert_not_reached ("couldn't append to string");
3321 if (!_dbus_string_find (&str, 0, "He", &i))
3322 _dbus_assert_not_reached ("didn't find 'He'");
3323 _dbus_assert (i == 0);
3325 if (!_dbus_string_find (&str, 0, "Hello", &i))
3326 _dbus_assert_not_reached ("didn't find 'Hello'");
3327 _dbus_assert (i == 0);
3329 if (!_dbus_string_find (&str, 0, "ello", &i))
3330 _dbus_assert_not_reached ("didn't find 'ello'");
3331 _dbus_assert (i == 1);
3333 if (!_dbus_string_find (&str, 0, "lo", &i))
3334 _dbus_assert_not_reached ("didn't find 'lo'");
3335 _dbus_assert (i == 3);
3337 if (!_dbus_string_find (&str, 2, "lo", &i))
3338 _dbus_assert_not_reached ("didn't find 'lo'");
3339 _dbus_assert (i == 3);
3341 if (_dbus_string_find (&str, 4, "lo", &i))
3342 _dbus_assert_not_reached ("did find 'lo'");
3344 if (!_dbus_string_find (&str, 0, "l", &i))
3345 _dbus_assert_not_reached ("didn't find 'l'");
3346 _dbus_assert (i == 2);
3348 if (!_dbus_string_find (&str, 0, "H", &i))
3349 _dbus_assert_not_reached ("didn't find 'H'");
3350 _dbus_assert (i == 0);
3352 if (!_dbus_string_find (&str, 0, "", &i))
3353 _dbus_assert_not_reached ("didn't find ''");
3354 _dbus_assert (i == 0);
3356 if (_dbus_string_find (&str, 0, "Hello!", NULL))
3357 _dbus_assert_not_reached ("Did find 'Hello!'");
3359 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
3360 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
3362 if (_dbus_string_find (&str, 0, "ill", NULL))
3363 _dbus_assert_not_reached ("Did find 'ill'");
3365 if (_dbus_string_find (&str, 0, "q", NULL))
3366 _dbus_assert_not_reached ("Did find 'q'");
3368 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
3369 _dbus_assert_not_reached ("Didn't find 'He'");
3371 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
3372 _dbus_assert_not_reached ("Did find 'Hello'");
3374 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
3375 _dbus_assert_not_reached ("Did not find 'H'");
3376 _dbus_assert (i == 0);
3378 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
3379 _dbus_assert_not_reached ("Did not find 'o'");
3380 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
3382 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
3383 _dbus_assert_not_reached ("Did find 'o'");
3384 _dbus_assert (i == -1);
3386 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
3387 _dbus_assert_not_reached ("Did find 'e'");
3388 _dbus_assert (i == -1);
3390 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
3391 _dbus_assert_not_reached ("Didn't find 'e'");
3392 _dbus_assert (i == 1);
3394 _dbus_string_free (&str);
3397 _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
3398 if (!_dbus_string_init (&other))
3399 _dbus_assert_not_reached ("could not init string");
3401 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
3402 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
3404 _dbus_assert (end == 8);
3406 _dbus_string_free (&other);
3408 test_roundtrips (test_hex_roundtrip);
3410 _dbus_string_free (&str);
3415 #endif /* DBUS_BUILD_TESTS */