1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-string.c String utility class (internal to D-Bus implementation)
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
5 * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "dbus-internals.h"
27 #include "dbus-string.h"
28 /* we allow a system header here, for speed/convenience */
32 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
33 #include "dbus-string-private.h"
34 #include "dbus-marshal-basic.h" /* probably should be removed by moving the usage of DBUS_TYPE
35 * into the marshaling-related files
37 /* for DBUS_VA_COPY */
38 #include "dbus-sysdeps.h"
41 * @defgroup DBusString DBusString class
42 * @ingroup DBusInternals
43 * @brief DBusString data structure for safer string handling
45 * Types and functions related to DBusString. DBusString is intended
46 * to be a string class that makes it hard to mess up security issues
47 * (and just in general harder to write buggy code). It should be
48 * used (or extended and then used) rather than the libc stuff in
49 * string.h. The string class is a bit inconvenient at spots because
50 * it handles out-of-memory failures and tries to be extra-robust.
52 * A DBusString has a maximum length set at initialization time; this
53 * can be used to ensure that a buffer doesn't get too big. The
54 * _dbus_string_lengthen() method checks for overflow, and for max
55 * length being exceeded.
57 * Try to avoid conversion to a plain C string, i.e. add methods on
58 * the string object instead, only convert to C string when passing
59 * things out to the public API. In particular, no sprintf, strcpy,
60 * strcat, any of that should be used. The GString feature of
61 * accepting negative numbers for "length of string" is also absent,
62 * because it could keep us from detecting bogus huge lengths. i.e. if
63 * we passed in some bogus huge length it would be taken to mean
64 * "current length of string" instead of "broken crack"
66 * @todo #DBusString needs a lot of cleaning up; some of the
67 * API is no longer used, and the API is pretty inconsistent.
68 * In particular all the "append" APIs, especially those involving
69 * alignment but probably lots of them, are no longer used by the
70 * marshaling code which always does "inserts" now.
74 * @addtogroup DBusString
79 fixup_alignment (DBusRealString *real)
81 unsigned char *aligned;
82 unsigned char *real_block;
83 unsigned int old_align_offset;
85 /* we have to have extra space in real->allocated for the align offset and nul byte */
86 _dbus_assert (real->len <= real->allocated - _DBUS_STRING_ALLOCATION_PADDING);
88 old_align_offset = real->align_offset;
89 real_block = real->str - old_align_offset;
91 aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
93 real->align_offset = aligned - real_block;
96 if (old_align_offset != real->align_offset)
98 /* Here comes the suck */
99 memmove (real_block + real->align_offset,
100 real_block + old_align_offset,
104 _dbus_assert (real->align_offset < 8);
105 _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
109 undo_alignment (DBusRealString *real)
111 if (real->align_offset != 0)
113 memmove (real->str - real->align_offset,
117 real->str = real->str - real->align_offset;
118 real->align_offset = 0;
123 * Initializes a string that can be up to the given allocation size
124 * before it has to realloc. The string starts life with zero length.
125 * The string must eventually be freed with _dbus_string_free().
127 * @param str memory to hold the string
128 * @param allocate_size amount to preallocate
129 * @returns #TRUE on success, #FALSE if no memory
132 _dbus_string_init_preallocated (DBusString *str,
135 DBusRealString *real;
137 _dbus_assert (str != NULL);
139 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
141 real = (DBusRealString*) str;
143 /* It's very important not to touch anything
144 * other than real->str if we're going to fail,
145 * since we also use this function to reset
146 * an existing string, e.g. in _dbus_string_steal_data()
149 real->str = dbus_malloc (_DBUS_STRING_ALLOCATION_PADDING + allocate_size);
150 if (real->str == NULL)
153 real->allocated = _DBUS_STRING_ALLOCATION_PADDING + allocate_size;
155 real->str[real->len] = '\0';
157 real->max_length = _DBUS_STRING_MAX_MAX_LENGTH;
158 real->constant = FALSE;
159 real->locked = FALSE;
160 real->invalid = FALSE;
161 real->align_offset = 0;
163 fixup_alignment (real);
169 * Initializes a string. The string starts life with zero length. The
170 * string must eventually be freed with _dbus_string_free().
172 * @param str memory to hold the string
173 * @returns #TRUE on success, #FALSE if no memory
176 _dbus_string_init (DBusString *str)
178 return _dbus_string_init_preallocated (str, 0);
181 #ifdef DBUS_BUILD_TESTS
182 /* The max length thing is sort of a historical artifact
183 * from a feature that turned out to be dumb; perhaps
184 * we should purge it entirely. The problem with
185 * the feature is that it looks like memory allocation
186 * failure, but is not a transient or resolvable failure.
189 set_max_length (DBusString *str,
192 DBusRealString *real;
194 real = (DBusRealString*) str;
196 real->max_length = max_length;
198 #endif /* DBUS_BUILD_TESTS */
201 * Initializes a constant string. The value parameter is not copied
202 * (should be static), and the string may never be modified.
203 * It is safe but not necessary to call _dbus_string_free()
204 * on a const string. The string has a length limit of MAXINT - 8.
206 * @param str memory to use for the string
207 * @param value a string to be stored in str (not copied!!!)
210 _dbus_string_init_const (DBusString *str,
213 _dbus_assert (value != NULL);
215 _dbus_string_init_const_len (str, value,
220 * Initializes a constant string with a length. The value parameter is
221 * not copied (should be static), and the string may never be
222 * modified. It is safe but not necessary to call _dbus_string_free()
225 * @param str memory to use for the string
226 * @param value a string to be stored in str (not copied!!!)
227 * @param len the length to use
230 _dbus_string_init_const_len (DBusString *str,
234 DBusRealString *real;
236 _dbus_assert (str != NULL);
237 _dbus_assert (len == 0 || value != NULL);
238 _dbus_assert (len <= _DBUS_STRING_MAX_MAX_LENGTH);
239 _dbus_assert (len >= 0);
241 real = (DBusRealString*) str;
243 real->str = (unsigned char*) value;
245 real->allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
246 real->max_length = real->len + 1;
247 real->constant = TRUE;
249 real->invalid = FALSE;
250 real->align_offset = 0;
252 /* We don't require const strings to be 8-byte aligned as the
253 * memory is coming from elsewhere.
258 * Frees a string created by _dbus_string_init().
260 * @param str memory where the string is stored.
263 _dbus_string_free (DBusString *str)
265 DBusRealString *real = (DBusRealString*) str;
266 DBUS_GENERIC_STRING_PREAMBLE (real);
270 dbus_free (real->str - real->align_offset);
272 real->invalid = TRUE;
276 compact (DBusRealString *real,
279 unsigned char *new_str;
283 waste = real->allocated - (real->len + _DBUS_STRING_ALLOCATION_PADDING);
285 if (waste <= max_waste)
288 new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
290 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
291 if (_DBUS_UNLIKELY (new_str == NULL))
294 real->str = new_str + real->align_offset;
295 real->allocated = new_allocated;
296 fixup_alignment (real);
301 #ifdef DBUS_BUILD_TESTS
302 /* Not using this feature at the moment,
303 * so marked DBUS_BUILD_TESTS-only
306 * Locks a string such that any attempts to change the string will
307 * result in aborting the program. Also, if the string is wasting a
308 * lot of memory (allocation is sufficiently larger than what the
309 * string is really using), _dbus_string_lock() will realloc the
310 * string's data to "compact" it.
312 * @param str the string to lock.
315 _dbus_string_lock (DBusString *str)
317 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
321 /* Try to realloc to avoid excess memory usage, since
322 * we know we won't change the string further
325 compact (real, MAX_WASTE);
327 #endif /* DBUS_BUILD_TESTS */
330 reallocate_for_length (DBusRealString *real,
334 unsigned char *new_str;
336 /* at least double our old allocation to avoid O(n), avoiding
339 if (real->allocated > (_DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
340 new_allocated = _DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
342 new_allocated = real->allocated * 2;
344 /* if you change the code just above here, run the tests without
345 * the following assert-only hack before you commit
347 /* This is keyed off asserts in addition to tests so when you
348 * disable asserts to profile, you don't get this destroyer
351 #ifdef DBUS_DISABLE_ASSERT
353 #ifdef DBUS_BUILD_TESTS
354 new_allocated = 0; /* ensure a realloc every time so that we go
355 * through all malloc failure codepaths
357 #endif /* DBUS_BUILD_TESTS */
358 #endif /* !DBUS_DISABLE_ASSERT */
360 /* But be sure we always alloc at least space for the new length */
361 new_allocated = MAX (new_allocated,
362 new_length + _DBUS_STRING_ALLOCATION_PADDING);
364 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
365 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
366 if (_DBUS_UNLIKELY (new_str == NULL))
369 real->str = new_str + real->align_offset;
370 real->allocated = new_allocated;
371 fixup_alignment (real);
377 * Compacts the string to avoid wasted memory. Wasted memory is
378 * memory that is allocated but not actually required to store the
379 * current length of the string. The compact is only done if more
380 * than the given amount of memory is being wasted (otherwise the
381 * waste is ignored and the call does nothing).
383 * @param str the string
384 * @param max_waste the maximum amount of waste to ignore
385 * @returns #FALSE if the compact failed due to realloc failure
388 _dbus_string_compact (DBusString *str,
391 DBUS_STRING_PREAMBLE (str);
393 return compact (real, max_waste);
397 set_length (DBusRealString *real,
400 /* Note, we are setting the length not including nul termination */
402 /* exceeding max length is the same as failure to allocate memory */
403 if (_DBUS_UNLIKELY (new_length > real->max_length))
405 else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
406 _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
410 real->len = new_length;
411 real->str[new_length] = '\0';
418 DBusRealString *dest,
424 if (len > dest->max_length - dest->len)
425 return FALSE; /* detected overflow of dest->len + len below */
427 if (!set_length (dest, dest->len + len))
430 memmove (dest->str + insert_at + len,
431 dest->str + insert_at,
432 dest->len - len - insert_at);
437 #ifndef _dbus_string_get_data
439 * Gets the raw character buffer from the string. The returned buffer
440 * will be nul-terminated, but note that strings may contain binary
441 * data so there may be extra nul characters prior to the termination.
442 * This function should be little-used, extend DBusString or add
443 * stuff to dbus-sysdeps.c instead. It's an error to use this
444 * function on a const string.
446 * @param str the string
450 _dbus_string_get_data (DBusString *str)
452 DBUS_STRING_PREAMBLE (str);
454 return (char*) real->str;
456 #endif /* _dbus_string_get_data */
458 /* only do the function if we don't have the macro */
459 #ifndef _dbus_string_get_const_data
461 * Gets the raw character buffer from a const string.
463 * @param str the string
464 * @returns the string data
467 _dbus_string_get_const_data (const DBusString *str)
469 DBUS_CONST_STRING_PREAMBLE (str);
471 return (const char*) real->str;
473 #endif /* _dbus_string_get_const_data */
476 * Gets a sub-portion of the raw character buffer from the
477 * string. The "len" field is required simply for error
478 * checking, to be sure you don't try to use more
479 * string than exists. The nul termination of the
480 * returned buffer remains at the end of the entire
481 * string, not at start + len.
483 * @param str the string
484 * @param start byte offset to return
485 * @param len length of segment to return
486 * @returns the string data
489 _dbus_string_get_data_len (DBusString *str,
493 DBUS_STRING_PREAMBLE (str);
494 _dbus_assert (start >= 0);
495 _dbus_assert (len >= 0);
496 _dbus_assert (start <= real->len);
497 _dbus_assert (len <= real->len - start);
499 return (char*) real->str + start;
502 /* only do the function if we don't have the macro */
503 #ifndef _dbus_string_get_const_data_len
505 * const version of _dbus_string_get_data_len().
507 * @param str the string
508 * @param start byte offset to return
509 * @param len length of segment to return
510 * @returns the string data
513 _dbus_string_get_const_data_len (const DBusString *str,
517 DBUS_CONST_STRING_PREAMBLE (str);
518 _dbus_assert (start >= 0);
519 _dbus_assert (len >= 0);
520 _dbus_assert (start <= real->len);
521 _dbus_assert (len <= real->len - start);
523 return (const char*) real->str + start;
525 #endif /* _dbus_string_get_const_data_len */
527 /* only do the function if we don't have the macro */
528 #ifndef _dbus_string_set_byte
530 * Sets the value of the byte at the given position.
532 * @param str the string
533 * @param i the position
534 * @param byte the new value
537 _dbus_string_set_byte (DBusString *str,
541 DBUS_STRING_PREAMBLE (str);
542 _dbus_assert (i < real->len);
543 _dbus_assert (i >= 0);
547 #endif /* _dbus_string_set_byte */
549 /* only have the function if we didn't create a macro */
550 #ifndef _dbus_string_get_byte
552 * Gets the byte at the given position. It is
553 * allowed to ask for the nul byte at the end of
556 * @param str the string
557 * @param start the position
558 * @returns the byte at that position
561 _dbus_string_get_byte (const DBusString *str,
564 DBUS_CONST_STRING_PREAMBLE (str);
565 _dbus_assert (start <= real->len);
566 _dbus_assert (start >= 0);
568 return real->str[start];
570 #endif /* _dbus_string_get_byte */
573 * Inserts a number of bytes of a given value at the
576 * @param str the string
577 * @param i the position
578 * @param n_bytes number of bytes
579 * @param byte the value to insert
580 * @returns #TRUE on success
583 _dbus_string_insert_bytes (DBusString *str,
588 DBUS_STRING_PREAMBLE (str);
589 _dbus_assert (i <= real->len);
590 _dbus_assert (i >= 0);
591 _dbus_assert (n_bytes >= 0);
596 if (!open_gap (n_bytes, real, i))
599 memset (real->str + i, byte, n_bytes);
605 * Inserts a single byte at the given position.
607 * @param str the string
608 * @param i the position
609 * @param byte the value to insert
610 * @returns #TRUE on success
613 _dbus_string_insert_byte (DBusString *str,
617 DBUS_STRING_PREAMBLE (str);
618 _dbus_assert (i <= real->len);
619 _dbus_assert (i >= 0);
621 if (!open_gap (1, real, i))
630 * Like _dbus_string_get_data(), but removes the
631 * gotten data from the original string. The caller
632 * must free the data returned. This function may
633 * fail due to lack of memory, and return #FALSE.
635 * @param str the string
636 * @param data_return location to return the buffer
637 * @returns #TRUE on success
640 _dbus_string_steal_data (DBusString *str,
644 DBUS_STRING_PREAMBLE (str);
645 _dbus_assert (data_return != NULL);
647 undo_alignment (real);
649 *data_return = (char*) real->str;
651 old_max_length = real->max_length;
653 /* reset the string */
654 if (!_dbus_string_init (str))
656 /* hrm, put it back then */
657 real->str = (unsigned char*) *data_return;
659 fixup_alignment (real);
663 real->max_length = old_max_length;
668 #ifdef DBUS_BUILD_TESTS
670 * Like _dbus_string_get_data_len(), but removes the gotten data from
671 * the original string. The caller must free the data returned. This
672 * function may fail due to lack of memory, and return #FALSE.
673 * The returned string is nul-terminated and has length len.
675 * @todo this function is broken because on failure it
676 * may corrupt the source string.
678 * @param str the string
679 * @param data_return location to return the buffer
680 * @param start the start of segment to steal
681 * @param len the length of segment to steal
682 * @returns #TRUE on success
685 _dbus_string_steal_data_len (DBusString *str,
691 DBUS_STRING_PREAMBLE (str);
692 _dbus_assert (data_return != NULL);
693 _dbus_assert (start >= 0);
694 _dbus_assert (len >= 0);
695 _dbus_assert (start <= real->len);
696 _dbus_assert (len <= real->len - start);
698 if (!_dbus_string_init (&dest))
701 set_max_length (&dest, real->max_length);
703 if (!_dbus_string_move_len (str, start, len, &dest, 0))
705 _dbus_string_free (&dest);
709 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
710 if (!_dbus_string_steal_data (&dest, data_return))
712 _dbus_string_free (&dest);
716 _dbus_string_free (&dest);
719 #endif /* DBUS_BUILD_TESTS */
722 * Copies the data from the string into a char*
724 * @param str the string
725 * @param data_return place to return the data
726 * @returns #TRUE on success, #FALSE on no memory
729 _dbus_string_copy_data (const DBusString *str,
732 DBUS_CONST_STRING_PREAMBLE (str);
733 _dbus_assert (data_return != NULL);
735 *data_return = dbus_malloc (real->len + 1);
736 if (*data_return == NULL)
739 memcpy (*data_return, real->str, real->len + 1);
745 * Copies the contents of a DBusString into a different buffer. It is
746 * a bug if avail_len is too short to hold the string contents. nul
747 * termination is not copied, just the supplied bytes.
749 * @param str a string
750 * @param buffer a C buffer to copy data to
751 * @param avail_len maximum length of C buffer
754 _dbus_string_copy_to_buffer (const DBusString *str,
758 DBUS_CONST_STRING_PREAMBLE (str);
760 _dbus_assert (avail_len >= 0);
761 _dbus_assert (avail_len >= real->len);
763 memcpy (buffer, real->str, real->len);
767 * Copies the contents of a DBusString into a different buffer. It is
768 * a bug if avail_len is too short to hold the string contents plus a
771 * @param str a string
772 * @param buffer a C buffer to copy data to
773 * @param avail_len maximum length of C buffer
776 _dbus_string_copy_to_buffer_with_nul (const DBusString *str,
780 DBUS_CONST_STRING_PREAMBLE (str);
782 _dbus_assert (avail_len >= 0);
783 _dbus_assert (avail_len > real->len);
785 memcpy (buffer, real->str, real->len+1);
788 #ifdef DBUS_BUILD_TESTS
790 * Copies a segment of the string into a char*
792 * @param str the string
793 * @param data_return place to return the data
794 * @param start start index
795 * @param len length to copy
796 * @returns #FALSE if no memory
799 _dbus_string_copy_data_len (const DBusString *str,
806 DBUS_CONST_STRING_PREAMBLE (str);
807 _dbus_assert (data_return != NULL);
808 _dbus_assert (start >= 0);
809 _dbus_assert (len >= 0);
810 _dbus_assert (start <= real->len);
811 _dbus_assert (len <= real->len - start);
813 if (!_dbus_string_init (&dest))
816 set_max_length (&dest, real->max_length);
818 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
820 _dbus_string_free (&dest);
824 if (!_dbus_string_steal_data (&dest, data_return))
826 _dbus_string_free (&dest);
830 _dbus_string_free (&dest);
833 #endif /* DBUS_BUILD_TESTS */
835 /* Only have the function if we don't have the macro */
836 #ifndef _dbus_string_get_length
838 * Gets the length of a string (not including nul termination).
840 * @returns the length.
843 _dbus_string_get_length (const DBusString *str)
845 DBUS_CONST_STRING_PREAMBLE (str);
849 #endif /* !_dbus_string_get_length */
852 * Makes a string longer by the given number of bytes. Checks whether
853 * adding additional_length to the current length would overflow an
854 * integer, and checks for exceeding a string's max length.
855 * The new bytes are not initialized, other than nul-terminating
856 * the end of the string. The uninitialized bytes may contain
857 * nul bytes or other junk.
859 * @param str a string
860 * @param additional_length length to add to the string.
861 * @returns #TRUE on success.
864 _dbus_string_lengthen (DBusString *str,
865 int additional_length)
867 DBUS_STRING_PREAMBLE (str);
868 _dbus_assert (additional_length >= 0);
870 if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
871 return FALSE; /* would overflow */
873 return set_length (real,
874 real->len + additional_length);
878 * Makes a string shorter by the given number of bytes.
880 * @param str a string
881 * @param length_to_remove length to remove from the string.
884 _dbus_string_shorten (DBusString *str,
885 int length_to_remove)
887 DBUS_STRING_PREAMBLE (str);
888 _dbus_assert (length_to_remove >= 0);
889 _dbus_assert (length_to_remove <= real->len);
892 real->len - length_to_remove);
896 * Sets the length of a string. Can be used to truncate or lengthen
897 * the string. If the string is lengthened, the function may fail and
898 * return #FALSE. Newly-added bytes are not initialized, as with
899 * _dbus_string_lengthen().
901 * @param str a string
902 * @param length new length of the string.
903 * @returns #FALSE on failure.
906 _dbus_string_set_length (DBusString *str,
909 DBUS_STRING_PREAMBLE (str);
910 _dbus_assert (length >= 0);
912 return set_length (real, length);
916 align_insert_point_then_open_gap (DBusString *str,
921 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
922 unsigned long gap_pos;
925 DBUS_STRING_PREAMBLE (str);
926 _dbus_assert (alignment >= 1);
927 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
929 insert_at = *insert_at_p;
931 _dbus_assert (insert_at <= real->len);
933 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
934 new_len = real->len + (gap_pos - insert_at) + gap_size;
936 if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length))
939 delta = new_len - real->len;
940 _dbus_assert (delta >= 0);
942 if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
944 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
948 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
952 /* nul the padding if we had to add any padding */
953 if (gap_size < delta)
955 memset (&real->str[insert_at], '\0',
956 gap_pos - insert_at);
959 *insert_at_p = gap_pos;
965 align_length_then_lengthen (DBusString *str,
967 int then_lengthen_by)
971 insert_at = _dbus_string_get_length (str);
973 return align_insert_point_then_open_gap (str,
975 alignment, then_lengthen_by);
979 * Align the length of a string to a specific alignment (typically 4 or 8)
980 * by appending nul bytes to the string.
982 * @param str a string
983 * @param alignment the alignment
984 * @returns #FALSE if no memory
987 _dbus_string_align_length (DBusString *str,
990 return align_length_then_lengthen (str, alignment, 0);
994 * Preallocate extra_bytes such that a future lengthening of the
995 * string by extra_bytes is guaranteed to succeed without an out of
998 * @param str a string
999 * @param extra_bytes bytes to alloc
1000 * @returns #FALSE if no memory
1003 _dbus_string_alloc_space (DBusString *str,
1006 if (!_dbus_string_lengthen (str, extra_bytes))
1008 _dbus_string_shorten (str, extra_bytes);
1014 append (DBusRealString *real,
1018 if (buffer_len == 0)
1021 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
1024 memcpy (real->str + (real->len - buffer_len),
1032 * Appends a nul-terminated C-style string to a DBusString.
1034 * @param str the DBusString
1035 * @param buffer the nul-terminated characters to append
1036 * @returns #FALSE if not enough memory.
1039 _dbus_string_append (DBusString *str,
1042 unsigned long buffer_len;
1044 DBUS_STRING_PREAMBLE (str);
1045 _dbus_assert (buffer != NULL);
1047 buffer_len = strlen (buffer);
1048 if (buffer_len > (unsigned long) real->max_length)
1051 return append (real, buffer, buffer_len);
1054 /** assign 2 bytes from one string to another */
1055 #define ASSIGN_2_OCTETS(p, octets) \
1056 *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
1058 /** assign 4 bytes from one string to another */
1059 #define ASSIGN_4_OCTETS(p, octets) \
1060 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
1062 #ifdef DBUS_HAVE_INT64
1063 /** assign 8 bytes from one string to another */
1064 #define ASSIGN_8_OCTETS(p, octets) \
1065 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
1067 /** assign 8 bytes from one string to another */
1068 #define ASSIGN_8_OCTETS(p, octets) \
1082 _dbus_assert (b == p + 8); \
1084 #endif /* DBUS_HAVE_INT64 */
1086 #ifdef DBUS_BUILD_TESTS
1088 * Appends 4 bytes aligned on a 4 byte boundary
1089 * with any alignment padding initialized to 0.
1091 * @param str the DBusString
1092 * @param octets 4 bytes to append
1093 * @returns #FALSE if not enough memory.
1096 _dbus_string_append_4_aligned (DBusString *str,
1097 const unsigned char octets[4])
1099 DBUS_STRING_PREAMBLE (str);
1101 if (!align_length_then_lengthen (str, 4, 4))
1104 ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
1108 #endif /* DBUS_BUILD_TESTS */
1110 #ifdef DBUS_BUILD_TESTS
1112 * Appends 8 bytes aligned on an 8 byte boundary
1113 * with any alignment padding initialized to 0.
1115 * @param str the DBusString
1116 * @param octets 8 bytes to append
1117 * @returns #FALSE if not enough memory.
1120 _dbus_string_append_8_aligned (DBusString *str,
1121 const unsigned char octets[8])
1123 DBUS_STRING_PREAMBLE (str);
1125 if (!align_length_then_lengthen (str, 8, 8))
1128 ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
1132 #endif /* DBUS_BUILD_TESTS */
1135 * Inserts 2 bytes aligned on a 2 byte boundary
1136 * with any alignment padding initialized to 0.
1138 * @param str the DBusString
1139 * @param insert_at where to insert
1140 * @param octets 2 bytes to insert
1141 * @returns #FALSE if not enough memory.
1144 _dbus_string_insert_2_aligned (DBusString *str,
1146 const unsigned char octets[4])
1148 DBUS_STRING_PREAMBLE (str);
1150 if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
1153 ASSIGN_2_OCTETS (real->str + insert_at, octets);
1159 * Inserts 4 bytes aligned on a 4 byte boundary
1160 * with any alignment padding initialized to 0.
1162 * @param str the DBusString
1163 * @param insert_at where to insert
1164 * @param octets 4 bytes to insert
1165 * @returns #FALSE if not enough memory.
1168 _dbus_string_insert_4_aligned (DBusString *str,
1170 const unsigned char octets[4])
1172 DBUS_STRING_PREAMBLE (str);
1174 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1177 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1183 * Inserts 8 bytes aligned on an 8 byte boundary
1184 * with any alignment padding initialized to 0.
1186 * @param str the DBusString
1187 * @param insert_at where to insert
1188 * @param octets 8 bytes to insert
1189 * @returns #FALSE if not enough memory.
1192 _dbus_string_insert_8_aligned (DBusString *str,
1194 const unsigned char octets[8])
1196 DBUS_STRING_PREAMBLE (str);
1198 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1201 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1203 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1210 * Inserts padding at *insert_at such to align it to the given
1211 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1212 * to the aligned position.
1214 * @param str the DBusString
1215 * @param insert_at location to be aligned
1216 * @param alignment alignment boundary (1, 2, 4, or 8)
1217 * @returns #FALSE if not enough memory.
1220 _dbus_string_insert_alignment (DBusString *str,
1224 DBUS_STRING_PREAMBLE (str);
1226 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1229 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1235 * Appends a printf-style formatted string
1236 * to the #DBusString.
1238 * @param str the string
1239 * @param format printf format
1240 * @param args variable argument list
1241 * @returns #FALSE if no memory
1244 _dbus_string_append_printf_valist (DBusString *str,
1251 DBUS_STRING_PREAMBLE (str);
1253 DBUS_VA_COPY (args_copy, args);
1255 /* Measure the message length without terminating nul */
1256 len = _dbus_printf_string_upper_bound (format, args);
1258 if (!_dbus_string_lengthen (str, len))
1260 /* don't leak the copy */
1265 vsprintf ((char*) (real->str + (real->len - len)),
1274 * Appends a printf-style formatted string
1275 * to the #DBusString.
1277 * @param str the string
1278 * @param format printf format
1279 * @returns #FALSE if no memory
1282 _dbus_string_append_printf (DBusString *str,
1289 va_start (args, format);
1290 retval = _dbus_string_append_printf_valist (str, format, args);
1297 * Appends block of bytes with the given length to a DBusString.
1299 * @param str the DBusString
1300 * @param buffer the bytes to append
1301 * @param len the number of bytes to append
1302 * @returns #FALSE if not enough memory.
1305 _dbus_string_append_len (DBusString *str,
1309 DBUS_STRING_PREAMBLE (str);
1310 _dbus_assert (buffer != NULL);
1311 _dbus_assert (len >= 0);
1313 return append (real, buffer, len);
1317 * Appends a single byte to the string, returning #FALSE
1318 * if not enough memory.
1320 * @param str the string
1321 * @param byte the byte to append
1322 * @returns #TRUE on success
1325 _dbus_string_append_byte (DBusString *str,
1328 DBUS_STRING_PREAMBLE (str);
1330 if (!set_length (real, real->len + 1))
1333 real->str[real->len-1] = byte;
1338 #ifdef DBUS_BUILD_TESTS
1340 * Appends a single Unicode character, encoding the character
1343 * @param str the string
1344 * @param ch the Unicode character
1347 _dbus_string_append_unichar (DBusString *str,
1355 DBUS_STRING_PREAMBLE (str);
1357 /* this code is from GLib but is pretty standard I think */
1366 else if (ch < 0x800)
1371 else if (ch < 0x10000)
1376 else if (ch < 0x200000)
1381 else if (ch < 0x4000000)
1392 if (len > (real->max_length - real->len))
1393 return FALSE; /* real->len + len would overflow */
1395 if (!set_length (real, real->len + len))
1398 out = real->str + (real->len - len);
1400 for (i = len - 1; i > 0; --i)
1402 out[i] = (ch & 0x3f) | 0x80;
1405 out[0] = ch | first;
1409 #endif /* DBUS_BUILD_TESTS */
1412 delete (DBusRealString *real,
1419 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1421 real->str[real->len] = '\0';
1425 * Deletes a segment of a DBusString with length len starting at
1426 * start. (Hint: to clear an entire string, setting length to 0
1427 * with _dbus_string_set_length() is easier.)
1429 * @param str the DBusString
1430 * @param start where to start deleting
1431 * @param len the number of bytes to delete
1434 _dbus_string_delete (DBusString *str,
1438 DBUS_STRING_PREAMBLE (str);
1439 _dbus_assert (start >= 0);
1440 _dbus_assert (len >= 0);
1441 _dbus_assert (start <= real->len);
1442 _dbus_assert (len <= real->len - start);
1444 delete (real, start, len);
1448 copy (DBusRealString *source,
1451 DBusRealString *dest,
1457 if (!open_gap (len, dest, insert_at))
1460 memmove (dest->str + insert_at,
1461 source->str + start,
1468 * Checks assertions for two strings we're copying a segment between,
1469 * and declares real_source/real_dest variables.
1471 * @param source the source string
1472 * @param start the starting offset
1473 * @param dest the dest string
1474 * @param insert_at where the copied segment is inserted
1476 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1477 DBusRealString *real_source = (DBusRealString*) source; \
1478 DBusRealString *real_dest = (DBusRealString*) dest; \
1479 _dbus_assert ((source) != (dest)); \
1480 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1481 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1482 _dbus_assert (!real_dest->constant); \
1483 _dbus_assert (!real_dest->locked); \
1484 _dbus_assert ((start) >= 0); \
1485 _dbus_assert ((start) <= real_source->len); \
1486 _dbus_assert ((insert_at) >= 0); \
1487 _dbus_assert ((insert_at) <= real_dest->len)
1490 * Moves the end of one string into another string. Both strings
1491 * must be initialized, valid strings.
1493 * @param source the source string
1494 * @param start where to chop off the source string
1495 * @param dest the destination string
1496 * @param insert_at where to move the chopped-off part of source string
1497 * @returns #FALSE if not enough memory
1500 _dbus_string_move (DBusString *source,
1505 DBusRealString *real_source = (DBusRealString*) source;
1506 _dbus_assert (start <= real_source->len);
1508 return _dbus_string_move_len (source, start,
1509 real_source->len - start,
1514 * Like _dbus_string_move(), but does not delete the section
1515 * of the source string that's copied to the dest string.
1517 * @param source the source string
1518 * @param start where to start copying the source string
1519 * @param dest the destination string
1520 * @param insert_at where to place the copied part of source string
1521 * @returns #FALSE if not enough memory
1524 _dbus_string_copy (const DBusString *source,
1529 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1531 return copy (real_source, start,
1532 real_source->len - start,
1538 * Like _dbus_string_move(), but can move a segment from
1539 * the middle of the source string.
1541 * @todo this doesn't do anything with max_length field.
1542 * we should probably just kill the max_length field though.
1544 * @param source the source string
1545 * @param start first byte of source string to move
1546 * @param len length of segment to move
1547 * @param dest the destination string
1548 * @param insert_at where to move the bytes from the source string
1549 * @returns #FALSE if not enough memory
1552 _dbus_string_move_len (DBusString *source,
1559 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1560 _dbus_assert (len >= 0);
1561 _dbus_assert ((start + len) <= real_source->len);
1568 else if (start == 0 &&
1569 len == real_source->len &&
1570 real_dest->len == 0)
1572 /* Short-circuit moving an entire existing string to an empty string
1573 * by just swapping the buffers.
1575 /* we assume ->constant doesn't matter as you can't have
1576 * a constant string involved in a move.
1578 #define ASSIGN_DATA(a, b) do { \
1579 (a)->str = (b)->str; \
1580 (a)->len = (b)->len; \
1581 (a)->allocated = (b)->allocated; \
1582 (a)->align_offset = (b)->align_offset; \
1587 ASSIGN_DATA (&tmp, real_source);
1588 ASSIGN_DATA (real_source, real_dest);
1589 ASSIGN_DATA (real_dest, &tmp);
1595 if (!copy (real_source, start, len,
1600 delete (real_source, start,
1608 * Like _dbus_string_copy(), but can copy a segment from the middle of
1609 * the source string.
1611 * @param source the source string
1612 * @param start where to start copying the source string
1613 * @param len length of segment to copy
1614 * @param dest the destination string
1615 * @param insert_at where to place the copied segment of source string
1616 * @returns #FALSE if not enough memory
1619 _dbus_string_copy_len (const DBusString *source,
1625 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1626 _dbus_assert (len >= 0);
1627 _dbus_assert (start <= real_source->len);
1628 _dbus_assert (len <= real_source->len - start);
1630 return copy (real_source, start, len,
1636 * Replaces a segment of dest string with a segment of source string.
1638 * @todo optimize the case where the two lengths are the same, and
1639 * avoid memmoving the data in the trailing part of the string twice.
1641 * @todo avoid inserting the source into dest, then deleting
1642 * the replaced chunk of dest (which creates a potentially large
1643 * intermediate string). Instead, extend the replaced chunk
1644 * of dest with padding to the same size as the source chunk,
1645 * then copy in the source bytes.
1647 * @param source the source string
1648 * @param start where to start copying the source string
1649 * @param len length of segment to copy
1650 * @param dest the destination string
1651 * @param replace_at start of segment of dest string to replace
1652 * @param replace_len length of segment of dest string to replace
1653 * @returns #FALSE if not enough memory
1657 _dbus_string_replace_len (const DBusString *source,
1664 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1665 _dbus_assert (len >= 0);
1666 _dbus_assert (start <= real_source->len);
1667 _dbus_assert (len <= real_source->len - start);
1668 _dbus_assert (replace_at >= 0);
1669 _dbus_assert (replace_at <= real_dest->len);
1670 _dbus_assert (replace_len <= real_dest->len - replace_at);
1672 if (!copy (real_source, start, len,
1673 real_dest, replace_at))
1676 delete (real_dest, replace_at + len, replace_len);
1682 * Looks for the first occurance of a byte, deletes that byte,
1683 * and moves everything after the byte to the beginning of a
1684 * separate string. Both strings must be initialized, valid
1687 * @param source the source string
1688 * @param byte the byte to remove and split the string at
1689 * @param tail the split off string
1690 * @returns #FALSE if not enough memory or if byte could not be found
1694 _dbus_string_split_on_byte (DBusString *source,
1699 char byte_string[2] = "";
1703 byte_string[0] = (char) byte;
1705 if (!_dbus_string_find (source, 0, byte_string, &byte_position))
1708 head_length = byte_position;
1709 tail_length = _dbus_string_get_length (source) - head_length - 1;
1711 if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
1715 /* remove the trailing delimiter byte from the head now.
1717 if (!_dbus_string_set_length (source, head_length))
1723 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1724 * Pennington, and Tom Tromey are the authors and authorized relicense.
1727 /** computes length and mask of a unicode character
1728 * @param Char the char
1729 * @param Mask the mask variable to assign to
1730 * @param Len the length variable to assign to
1732 #define UTF8_COMPUTE(Char, Mask, Len) \
1738 else if ((Char & 0xe0) == 0xc0) \
1743 else if ((Char & 0xf0) == 0xe0) \
1748 else if ((Char & 0xf8) == 0xf0) \
1753 else if ((Char & 0xfc) == 0xf8) \
1758 else if ((Char & 0xfe) == 0xfc) \
1770 * computes length of a unicode character in UTF-8
1771 * @param Char the char
1773 #define UTF8_LENGTH(Char) \
1774 ((Char) < 0x80 ? 1 : \
1775 ((Char) < 0x800 ? 2 : \
1776 ((Char) < 0x10000 ? 3 : \
1777 ((Char) < 0x200000 ? 4 : \
1778 ((Char) < 0x4000000 ? 5 : 6)))))
1781 * Gets a UTF-8 value.
1783 * @param Result variable for extracted unicode char.
1784 * @param Chars the bytes to decode
1785 * @param Count counter variable
1786 * @param Mask mask for this char
1787 * @param Len length for this char in bytes
1789 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1790 (Result) = (Chars)[0] & (Mask); \
1791 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1793 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1799 (Result) |= ((Chars)[(Count)] & 0x3f); \
1803 * Check whether a Unicode (5.2) char is in a valid range.
1805 * The first check comes from the Unicode guarantee to never encode
1806 * a point above 0x0010ffff, since UTF-16 couldn't represent it.
1808 * The second check covers surrogate pairs (category Cs).
1810 * The last two checks cover "Noncharacter": defined as:
1811 * "A code point that is permanently reserved for
1812 * internal use, and that should never be interchanged. In
1813 * Unicode 3.1, these consist of the values U+nFFFE and U+nFFFF
1814 * (where n is from 0 to 10_16) and the values U+FDD0..U+FDEF."
1816 * @param Char the character
1818 #define UNICODE_VALID(Char) \
1819 ((Char) < 0x110000 && \
1820 (((Char) & 0xFFFFF800) != 0xD800) && \
1821 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1822 ((Char) & 0xFFFE) != 0xFFFE)
1824 #ifdef DBUS_BUILD_TESTS
1826 * Gets a unicode character from a UTF-8 string. Does no validation;
1827 * you must verify that the string is valid UTF-8 in advance and must
1828 * pass in the start of a character.
1830 * @param str the string
1831 * @param start the start of the UTF-8 character.
1832 * @param ch_return location to return the character
1833 * @param end_return location to return the byte index of next character
1836 _dbus_string_get_unichar (const DBusString *str,
1838 dbus_unichar_t *ch_return,
1842 dbus_unichar_t result;
1845 DBUS_CONST_STRING_PREAMBLE (str);
1846 _dbus_assert (start >= 0);
1847 _dbus_assert (start <= real->len);
1852 *end_return = real->len;
1855 p = real->str + start;
1858 UTF8_COMPUTE (c, mask, len);
1861 UTF8_GET (result, p, i, mask, len);
1863 if (result == (dbus_unichar_t)-1)
1867 *ch_return = result;
1869 *end_return = start + len;
1871 #endif /* DBUS_BUILD_TESTS */
1874 * Finds the given substring in the string,
1875 * returning #TRUE and filling in the byte index
1876 * where the substring was found, if it was found.
1877 * Returns #FALSE if the substring wasn't found.
1878 * Sets *start to the length of the string if the substring
1881 * @param str the string
1882 * @param start where to start looking
1883 * @param substr the substring
1884 * @param found return location for where it was found, or #NULL
1885 * @returns #TRUE if found
1888 _dbus_string_find (const DBusString *str,
1893 return _dbus_string_find_to (str, start,
1894 ((const DBusRealString*)str)->len,
1899 * Finds end of line ("\r\n" or "\n") in the string,
1900 * returning #TRUE and filling in the byte index
1901 * where the eol string was found, if it was found.
1902 * Returns #FALSE if eol wasn't found.
1904 * @param str the string
1905 * @param start where to start looking
1906 * @param found return location for where eol was found or string length otherwise
1907 * @param found_len return length of found eol string or zero otherwise
1908 * @returns #TRUE if found
1911 _dbus_string_find_eol (const DBusString *str,
1918 DBUS_CONST_STRING_PREAMBLE (str);
1919 _dbus_assert (start <= real->len);
1920 _dbus_assert (start >= 0);
1923 while (i < real->len)
1925 if (real->str[i] == '\r')
1927 if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
1935 else /* only "\r" */
1944 else if (real->str[i] == '\n') /* only "\n" */
1965 * Finds the given substring in the string,
1966 * up to a certain position,
1967 * returning #TRUE and filling in the byte index
1968 * where the substring was found, if it was found.
1969 * Returns #FALSE if the substring wasn't found.
1970 * Sets *start to the length of the string if the substring
1973 * @param str the string
1974 * @param start where to start looking
1975 * @param end where to stop looking
1976 * @param substr the substring
1977 * @param found return location for where it was found, or #NULL
1978 * @returns #TRUE if found
1981 _dbus_string_find_to (const DBusString *str,
1988 DBUS_CONST_STRING_PREAMBLE (str);
1989 _dbus_assert (substr != NULL);
1990 _dbus_assert (start <= real->len);
1991 _dbus_assert (start >= 0);
1992 _dbus_assert (substr != NULL);
1993 _dbus_assert (end <= real->len);
1994 _dbus_assert (start <= end);
1996 /* we always "find" an empty string */
1997 if (*substr == '\0')
2007 if (real->str[i] == substr[0])
2013 if (substr[j - i] == '\0')
2015 else if (real->str[j] != substr[j - i])
2021 if (substr[j - i] == '\0')
2039 * Finds a blank (space or tab) in the string. Returns #TRUE
2040 * if found, #FALSE otherwise. If a blank is not found sets
2041 * *found to the length of the string.
2043 * @param str the string
2044 * @param start byte index to start looking
2045 * @param found place to store the location of the first blank
2046 * @returns #TRUE if a blank was found
2049 _dbus_string_find_blank (const DBusString *str,
2054 DBUS_CONST_STRING_PREAMBLE (str);
2055 _dbus_assert (start <= real->len);
2056 _dbus_assert (start >= 0);
2059 while (i < real->len)
2061 if (real->str[i] == ' ' ||
2062 real->str[i] == '\t')
2079 * Skips blanks from start, storing the first non-blank in *end
2080 * (blank is space or tab).
2082 * @param str the string
2083 * @param start where to start
2084 * @param end where to store the first non-blank byte index
2087 _dbus_string_skip_blank (const DBusString *str,
2092 DBUS_CONST_STRING_PREAMBLE (str);
2093 _dbus_assert (start <= real->len);
2094 _dbus_assert (start >= 0);
2097 while (i < real->len)
2099 if (!DBUS_IS_ASCII_BLANK (real->str[i]))
2105 _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
2113 * Skips whitespace from start, storing the first non-whitespace in *end.
2114 * (whitespace is space, tab, newline, CR).
2116 * @param str the string
2117 * @param start where to start
2118 * @param end where to store the first non-whitespace byte index
2121 _dbus_string_skip_white (const DBusString *str,
2126 DBUS_CONST_STRING_PREAMBLE (str);
2127 _dbus_assert (start <= real->len);
2128 _dbus_assert (start >= 0);
2131 while (i < real->len)
2133 if (!DBUS_IS_ASCII_WHITE (real->str[i]))
2139 _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
2146 * Skips whitespace from end, storing the start index of the trailing
2147 * whitespace in *start. (whitespace is space, tab, newline, CR).
2149 * @param str the string
2150 * @param end where to start scanning backward
2151 * @param start where to store the start of whitespace chars
2154 _dbus_string_skip_white_reverse (const DBusString *str,
2159 DBUS_CONST_STRING_PREAMBLE (str);
2160 _dbus_assert (end <= real->len);
2161 _dbus_assert (end >= 0);
2166 if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
2171 _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
2178 * Assigns a newline-terminated or \\r\\n-terminated line from the front
2179 * of the string to the given dest string. The dest string's previous
2180 * contents are deleted. If the source string contains no newline,
2181 * moves the entire source string to the dest string.
2183 * @todo owen correctly notes that this is a stupid function (it was
2184 * written purely for test code,
2185 * e.g. dbus-message-builder.c). Probably should be enforced as test
2186 * code only with ifdef DBUS_BUILD_TESTS
2188 * @param source the source string
2189 * @param dest the destination string (contents are replaced)
2190 * @returns #FALSE if no memory, or source has length 0
2193 _dbus_string_pop_line (DBusString *source,
2198 _dbus_string_set_length (dest, 0);
2202 if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
2204 _dbus_assert (eol == _dbus_string_get_length (source));
2207 /* If there's no newline and source has zero length, we're done */
2210 /* otherwise, the last line of the file has no eol characters */
2213 /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
2214 * since find_eol returned TRUE
2217 if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
2220 /* remove line ending */
2221 if (!_dbus_string_set_length (dest, eol))
2223 _dbus_assert_not_reached ("out of memory when shortening a string");
2230 #ifdef DBUS_BUILD_TESTS
2232 * Deletes up to and including the first blank space
2235 * @param str the string
2238 _dbus_string_delete_first_word (DBusString *str)
2242 if (_dbus_string_find_blank (str, 0, &i))
2243 _dbus_string_skip_blank (str, i, &i);
2245 _dbus_string_delete (str, 0, i);
2249 #ifdef DBUS_BUILD_TESTS
2251 * Deletes any leading blanks in the string
2253 * @param str the string
2256 _dbus_string_delete_leading_blanks (DBusString *str)
2260 _dbus_string_skip_blank (str, 0, &i);
2263 _dbus_string_delete (str, 0, i);
2268 * Deletes leading and trailing whitespace
2270 * @param str the string
2273 _dbus_string_chop_white(DBusString *str)
2277 _dbus_string_skip_white (str, 0, &i);
2280 _dbus_string_delete (str, 0, i);
2282 _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
2284 _dbus_string_set_length (str, i);
2288 * Tests two DBusString for equality.
2290 * @todo memcmp is probably faster
2292 * @param a first string
2293 * @param b second string
2294 * @returns #TRUE if equal
2297 _dbus_string_equal (const DBusString *a,
2298 const DBusString *b)
2300 const unsigned char *ap;
2301 const unsigned char *bp;
2302 const unsigned char *a_end;
2303 const DBusRealString *real_a = (const DBusRealString*) a;
2304 const DBusRealString *real_b = (const DBusRealString*) b;
2305 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2306 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2308 if (real_a->len != real_b->len)
2313 a_end = real_a->str + real_a->len;
2327 * Tests two DBusString for equality up to the given length.
2328 * The strings may be shorter than the given length.
2330 * @todo write a unit test
2332 * @todo memcmp is probably faster
2334 * @param a first string
2335 * @param b second string
2336 * @param len the maximum length to look at
2337 * @returns #TRUE if equal for the given number of bytes
2340 _dbus_string_equal_len (const DBusString *a,
2341 const DBusString *b,
2344 const unsigned char *ap;
2345 const unsigned char *bp;
2346 const unsigned char *a_end;
2347 const DBusRealString *real_a = (const DBusRealString*) a;
2348 const DBusRealString *real_b = (const DBusRealString*) b;
2349 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2350 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2352 if (real_a->len != real_b->len &&
2353 (real_a->len < len || real_b->len < len))
2358 a_end = real_a->str + MIN (real_a->len, len);
2372 * Tests two sub-parts of two DBusString for equality. The specified
2373 * range of the first string must exist; the specified start position
2374 * of the second string must exist.
2376 * @todo write a unit test
2378 * @todo memcmp is probably faster
2380 * @param a first string
2381 * @param a_start where to start substring in first string
2382 * @param a_len length of substring in first string
2383 * @param b second string
2384 * @param b_start where to start substring in second string
2385 * @returns #TRUE if the two substrings are equal
2388 _dbus_string_equal_substring (const DBusString *a,
2391 const DBusString *b,
2394 const unsigned char *ap;
2395 const unsigned char *bp;
2396 const unsigned char *a_end;
2397 const DBusRealString *real_a = (const DBusRealString*) a;
2398 const DBusRealString *real_b = (const DBusRealString*) b;
2399 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2400 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2401 _dbus_assert (a_start >= 0);
2402 _dbus_assert (a_len >= 0);
2403 _dbus_assert (a_start <= real_a->len);
2404 _dbus_assert (a_len <= real_a->len - a_start);
2405 _dbus_assert (b_start >= 0);
2406 _dbus_assert (b_start <= real_b->len);
2408 if (a_len > real_b->len - b_start)
2411 ap = real_a->str + a_start;
2412 bp = real_b->str + b_start;
2423 _dbus_assert (bp <= (real_b->str + real_b->len));
2429 * Checks whether a string is equal to a C string.
2431 * @param a the string
2432 * @param c_str the C string
2433 * @returns #TRUE if equal
2436 _dbus_string_equal_c_str (const DBusString *a,
2439 const unsigned char *ap;
2440 const unsigned char *bp;
2441 const unsigned char *a_end;
2442 const DBusRealString *real_a = (const DBusRealString*) a;
2443 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2444 _dbus_assert (c_str != NULL);
2447 bp = (const unsigned char*) c_str;
2448 a_end = real_a->str + real_a->len;
2449 while (ap != a_end && *bp)
2458 if (ap != a_end || *bp)
2464 #ifdef DBUS_BUILD_TESTS
2466 * Checks whether a string starts with the given C string.
2468 * @param a the string
2469 * @param c_str the C string
2470 * @returns #TRUE if string starts with it
2473 _dbus_string_starts_with_c_str (const DBusString *a,
2476 const unsigned char *ap;
2477 const unsigned char *bp;
2478 const unsigned char *a_end;
2479 const DBusRealString *real_a = (const DBusRealString*) a;
2480 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2481 _dbus_assert (c_str != NULL);
2484 bp = (const unsigned char*) c_str;
2485 a_end = real_a->str + real_a->len;
2486 while (ap != a_end && *bp)
2500 #endif /* DBUS_BUILD_TESTS */
2503 * Appends a two-character hex digit to a string, where the hex digit
2504 * has the value of the given byte.
2506 * @param str the string
2507 * @param byte the byte
2508 * @returns #FALSE if no memory
2511 _dbus_string_append_byte_as_hex (DBusString *str,
2514 const char hexdigits[16] = {
2515 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2516 'a', 'b', 'c', 'd', 'e', 'f'
2519 if (!_dbus_string_append_byte (str,
2520 hexdigits[(byte >> 4)]))
2523 if (!_dbus_string_append_byte (str,
2524 hexdigits[(byte & 0x0f)]))
2526 _dbus_string_set_length (str,
2527 _dbus_string_get_length (str) - 1);
2535 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2536 * encoded. (Each byte is two hex digits.)
2538 * @param source the string to encode
2539 * @param start byte index to start encoding
2540 * @param dest string where encoded data should be placed
2541 * @param insert_at where to place encoded data
2542 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2545 _dbus_string_hex_encode (const DBusString *source,
2551 const unsigned char *p;
2552 const unsigned char *end;
2555 _dbus_assert (start <= _dbus_string_get_length (source));
2557 if (!_dbus_string_init (&result))
2562 p = (const unsigned char*) _dbus_string_get_const_data (source);
2563 end = p + _dbus_string_get_length (source);
2568 if (!_dbus_string_append_byte_as_hex (&result, *p))
2574 if (!_dbus_string_move (&result, 0, dest, insert_at))
2580 _dbus_string_free (&result);
2585 * Decodes a string from hex encoding.
2587 * @param source the string to decode
2588 * @param start byte index to start decode
2589 * @param end_return return location of the end of the hex data, or #NULL
2590 * @param dest string where decoded data should be placed
2591 * @param insert_at where to place decoded data
2592 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2595 _dbus_string_hex_decode (const DBusString *source,
2602 const unsigned char *p;
2603 const unsigned char *end;
2605 dbus_bool_t high_bits;
2607 _dbus_assert (start <= _dbus_string_get_length (source));
2609 if (!_dbus_string_init (&result))
2615 p = (const unsigned char*) _dbus_string_get_const_data (source);
2616 end = p + _dbus_string_get_length (source);
2685 if (!_dbus_string_append_byte (&result,
2694 len = _dbus_string_get_length (&result);
2696 b = _dbus_string_get_byte (&result, len - 1);
2700 _dbus_string_set_byte (&result, len - 1, b);
2703 high_bits = !high_bits;
2709 if (!_dbus_string_move (&result, 0, dest, insert_at))
2713 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2718 _dbus_string_free (&result);
2723 * Checks that the given range of the string is valid ASCII with no
2724 * nul bytes. If the given range is not entirely contained in the
2725 * string, returns #FALSE.
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 valid ASCII
2736 _dbus_string_validate_ascii (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 (start <= real->len);
2745 _dbus_assert (len >= 0);
2747 if (len > real->len - start)
2750 s = real->str + start;
2754 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2764 * Converts the given range of the string to lower case.
2766 * @param str the string
2767 * @param start first byte index to convert
2768 * @param len number of bytes to convert
2771 _dbus_string_tolower_ascii (const DBusString *str,
2777 DBUS_STRING_PREAMBLE (str);
2778 _dbus_assert (start >= 0);
2779 _dbus_assert (start <= real->len);
2780 _dbus_assert (len >= 0);
2781 _dbus_assert (len <= real->len - start);
2783 s = real->str + start;
2788 if (*s >= 'A' && *s <= 'Z')
2795 * Converts the given range of the string to upper case.
2797 * @param str the string
2798 * @param start first byte index to convert
2799 * @param len number of bytes to convert
2802 _dbus_string_toupper_ascii (const DBusString *str,
2808 DBUS_STRING_PREAMBLE (str);
2809 _dbus_assert (start >= 0);
2810 _dbus_assert (start <= real->len);
2811 _dbus_assert (len >= 0);
2812 _dbus_assert (len <= real->len - start);
2814 s = real->str + start;
2819 if (*s >= 'a' && *s <= 'z')
2826 * Checks that the given range of the string is valid UTF-8. If the
2827 * given range is not entirely contained in the string, returns
2828 * #FALSE. If the string contains any nul bytes in the given range,
2829 * returns #FALSE. If the start and start+len are not on character
2830 * boundaries, returns #FALSE.
2832 * @todo this is inconsistent with most of DBusString in that
2833 * it allows a start,len range that extends past the string end.
2835 * @param str the string
2836 * @param start first byte index to check
2837 * @param len number of bytes to check
2838 * @returns #TRUE if the byte range exists and is all valid UTF-8
2841 _dbus_string_validate_utf8 (const DBusString *str,
2845 const unsigned char *p;
2846 const unsigned char *end;
2847 DBUS_CONST_STRING_PREAMBLE (str);
2848 _dbus_assert (start >= 0);
2849 _dbus_assert (start <= real->len);
2850 _dbus_assert (len >= 0);
2852 /* we are doing _DBUS_UNLIKELY() here which might be
2853 * dubious in a generic library like GLib, but in D-Bus
2854 * we know we're validating messages and that it would
2855 * only be evil/broken apps that would have invalid
2856 * UTF-8. Also, this function seems to be a performance
2857 * bottleneck in profiles.
2860 if (_DBUS_UNLIKELY (len > real->len - start))
2863 p = real->str + start;
2868 int i, mask, char_len;
2869 dbus_unichar_t result;
2871 /* nul bytes considered invalid */
2875 /* Special-case ASCII; this makes us go a lot faster in
2876 * D-Bus profiles where we are typically validating
2877 * function names and such. We have to know that
2878 * all following checks will pass for ASCII though,
2879 * comments follow ...
2887 UTF8_COMPUTE (*p, mask, char_len);
2889 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2892 /* check that the expected number of bytes exists in the remaining length */
2893 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2896 UTF8_GET (result, p, i, mask, char_len);
2898 /* Check for overlong UTF-8 */
2899 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2902 /* The UNICODE_VALID check below will catch this */
2903 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2907 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2910 /* UNICODE_VALID should have caught it */
2911 _dbus_assert (result != (dbus_unichar_t)-1);
2916 /* See that we covered the entire length if a length was
2919 if (_DBUS_UNLIKELY (p != end))
2926 * Checks that the given range of the string is all nul bytes. If the
2927 * given range is not entirely contained in the string, returns
2930 * @todo this is inconsistent with most of DBusString in that
2931 * it allows a start,len range that extends past the string end.
2933 * @param str the string
2934 * @param start first byte index to check
2935 * @param len number of bytes to check
2936 * @returns #TRUE if the byte range exists and is all nul bytes
2939 _dbus_string_validate_nul (const DBusString *str,
2943 const unsigned char *s;
2944 const unsigned char *end;
2945 DBUS_CONST_STRING_PREAMBLE (str);
2946 _dbus_assert (start >= 0);
2947 _dbus_assert (len >= 0);
2948 _dbus_assert (start <= real->len);
2950 if (len > real->len - start)
2953 s = real->str + start;
2957 if (_DBUS_UNLIKELY (*s != '\0'))
2966 * Clears all allocated bytes in the string to zero.
2968 * @param str the string
2971 _dbus_string_zero (DBusString *str)
2973 DBUS_STRING_PREAMBLE (str);
2975 memset (real->str - real->align_offset, '\0', real->allocated);
2979 /* tests are in dbus-string-util.c */