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
25 #include "dbus-internals.h"
26 #include "dbus-string.h"
27 /* we allow a system header here, for speed/convenience */
31 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
32 #include "dbus-string-private.h"
33 #include "dbus-marshal-basic.h" /* probably should be removed by moving the usage of DBUS_TYPE
34 * into the marshaling-related files
36 /* for DBUS_VA_COPY */
37 #include "dbus-sysdeps.h"
40 * @defgroup DBusString DBusString class
41 * @ingroup DBusInternals
42 * @brief DBusString data structure for safer string handling
44 * Types and functions related to DBusString. DBusString is intended
45 * to be a string class that makes it hard to mess up security issues
46 * (and just in general harder to write buggy code). It should be
47 * used (or extended and then used) rather than the libc stuff in
48 * string.h. The string class is a bit inconvenient at spots because
49 * it handles out-of-memory failures and tries to be extra-robust.
51 * A DBusString has a maximum length set at initialization time; this
52 * can be used to ensure that a buffer doesn't get too big. The
53 * _dbus_string_lengthen() method checks for overflow, and for max
54 * length being exceeded.
56 * Try to avoid conversion to a plain C string, i.e. add methods on
57 * the string object instead, only convert to C string when passing
58 * things out to the public API. In particular, no sprintf, strcpy,
59 * strcat, any of that should be used. The GString feature of
60 * accepting negative numbers for "length of string" is also absent,
61 * because it could keep us from detecting bogus huge lengths. i.e. if
62 * we passed in some bogus huge length it would be taken to mean
63 * "current length of string" instead of "broken crack"
65 * @todo #DBusString needs a lot of cleaning up; some of the
66 * API is no longer used, and the API is pretty inconsistent.
67 * In particular all the "append" APIs, especially those involving
68 * alignment but probably lots of them, are no longer used by the
69 * marshaling code which always does "inserts" now.
73 * @addtogroup DBusString
78 fixup_alignment (DBusRealString *real)
80 unsigned char *aligned;
81 unsigned char *real_block;
82 unsigned int old_align_offset;
84 /* we have to have extra space in real->allocated for the align offset and nul byte */
85 _dbus_assert (real->len <= real->allocated - _DBUS_STRING_ALLOCATION_PADDING);
87 old_align_offset = real->align_offset;
88 real_block = real->str - old_align_offset;
90 aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
92 real->align_offset = aligned - real_block;
95 if (old_align_offset != real->align_offset)
97 /* Here comes the suck */
98 memmove (real_block + real->align_offset,
99 real_block + old_align_offset,
103 _dbus_assert (real->align_offset < 8);
104 _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
108 undo_alignment (DBusRealString *real)
110 if (real->align_offset != 0)
112 memmove (real->str - real->align_offset,
116 real->str = real->str - real->align_offset;
117 real->align_offset = 0;
122 * Initializes a string that can be up to the given allocation size
123 * before it has to realloc. The string starts life with zero length.
124 * The string must eventually be freed with _dbus_string_free().
126 * @param str memory to hold the string
127 * @param allocate_size amount to preallocate
128 * @returns #TRUE on success, #FALSE if no memory
131 _dbus_string_init_preallocated (DBusString *str,
134 DBusRealString *real;
136 _dbus_assert (str != NULL);
138 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
140 real = (DBusRealString*) str;
142 /* It's very important not to touch anything
143 * other than real->str if we're going to fail,
144 * since we also use this function to reset
145 * an existing string, e.g. in _dbus_string_steal_data()
148 real->str = dbus_malloc (_DBUS_STRING_ALLOCATION_PADDING + allocate_size);
149 if (real->str == NULL)
152 real->allocated = _DBUS_STRING_ALLOCATION_PADDING + allocate_size;
154 real->str[real->len] = '\0';
156 real->max_length = _DBUS_STRING_MAX_MAX_LENGTH;
157 real->constant = FALSE;
158 real->locked = FALSE;
159 real->invalid = FALSE;
160 real->align_offset = 0;
162 fixup_alignment (real);
168 * Initializes a string. The string starts life with zero length. The
169 * string must eventually be freed with _dbus_string_free().
171 * @param str memory to hold the string
172 * @returns #TRUE on success, #FALSE if no memory
175 _dbus_string_init (DBusString *str)
177 return _dbus_string_init_preallocated (str, 0);
180 #ifdef DBUS_BUILD_TESTS
181 /* The max length thing is sort of a historical artifact
182 * from a feature that turned out to be dumb; perhaps
183 * we should purge it entirely. The problem with
184 * the feature is that it looks like memory allocation
185 * failure, but is not a transient or resolvable failure.
188 set_max_length (DBusString *str,
191 DBusRealString *real;
193 real = (DBusRealString*) str;
195 real->max_length = max_length;
197 #endif /* DBUS_BUILD_TESTS */
200 * Initializes a constant string. The value parameter is not copied
201 * (should be static), and the string may never be modified.
202 * It is safe but not necessary to call _dbus_string_free()
203 * on a const string. The string has a length limit of MAXINT - 8.
205 * @param str memory to use for the string
206 * @param value a string to be stored in str (not copied!!!)
209 _dbus_string_init_const (DBusString *str,
212 _dbus_assert (value != NULL);
214 _dbus_string_init_const_len (str, value,
219 * Initializes a constant string with a length. The value parameter is
220 * not copied (should be static), and the string may never be
221 * modified. It is safe but not necessary to call _dbus_string_free()
224 * @param str memory to use for the string
225 * @param value a string to be stored in str (not copied!!!)
226 * @param len the length to use
229 _dbus_string_init_const_len (DBusString *str,
233 DBusRealString *real;
235 _dbus_assert (str != NULL);
236 _dbus_assert (len == 0 || value != NULL);
237 _dbus_assert (len <= _DBUS_STRING_MAX_MAX_LENGTH);
238 _dbus_assert (len >= 0);
240 real = (DBusRealString*) str;
242 real->str = (unsigned char*) value;
244 real->allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
245 real->max_length = real->len + 1;
246 real->constant = TRUE;
248 real->invalid = FALSE;
249 real->align_offset = 0;
251 /* We don't require const strings to be 8-byte aligned as the
252 * memory is coming from elsewhere.
257 * Frees a string created by _dbus_string_init().
259 * @param str memory where the string is stored.
262 _dbus_string_free (DBusString *str)
264 DBusRealString *real = (DBusRealString*) str;
265 DBUS_GENERIC_STRING_PREAMBLE (real);
269 dbus_free (real->str - real->align_offset);
271 real->invalid = TRUE;
275 compact (DBusRealString *real,
278 unsigned char *new_str;
282 waste = real->allocated - (real->len + _DBUS_STRING_ALLOCATION_PADDING);
284 if (waste <= max_waste)
287 new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
289 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
290 if (_DBUS_UNLIKELY (new_str == NULL))
293 real->str = new_str + real->align_offset;
294 real->allocated = new_allocated;
295 fixup_alignment (real);
300 #ifdef DBUS_BUILD_TESTS
301 /* Not using this feature at the moment,
302 * so marked DBUS_BUILD_TESTS-only
305 * Locks a string such that any attempts to change the string will
306 * result in aborting the program. Also, if the string is wasting a
307 * lot of memory (allocation is sufficiently larger than what the
308 * string is really using), _dbus_string_lock() will realloc the
309 * string's data to "compact" it.
311 * @param str the string to lock.
314 _dbus_string_lock (DBusString *str)
316 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
320 /* Try to realloc to avoid excess memory usage, since
321 * we know we won't change the string further
324 compact (real, MAX_WASTE);
326 #endif /* DBUS_BUILD_TESTS */
329 reallocate_for_length (DBusRealString *real,
333 unsigned char *new_str;
335 /* at least double our old allocation to avoid O(n), avoiding
338 if (real->allocated > (_DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
339 new_allocated = _DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
341 new_allocated = real->allocated * 2;
343 /* if you change the code just above here, run the tests without
344 * the following assert-only hack before you commit
346 /* This is keyed off asserts in addition to tests so when you
347 * disable asserts to profile, you don't get this destroyer
350 #ifdef DBUS_DISABLE_ASSERT
352 #ifdef DBUS_BUILD_TESTS
353 new_allocated = 0; /* ensure a realloc every time so that we go
354 * through all malloc failure codepaths
356 #endif /* DBUS_BUILD_TESTS */
357 #endif /* !DBUS_DISABLE_ASSERT */
359 /* But be sure we always alloc at least space for the new length */
360 new_allocated = MAX (new_allocated,
361 new_length + _DBUS_STRING_ALLOCATION_PADDING);
363 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
364 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
365 if (_DBUS_UNLIKELY (new_str == NULL))
368 real->str = new_str + real->align_offset;
369 real->allocated = new_allocated;
370 fixup_alignment (real);
376 * Compacts the string to avoid wasted memory. Wasted memory is
377 * memory that is allocated but not actually required to store the
378 * current length of the string. The compact is only done if more
379 * than the given amount of memory is being wasted (otherwise the
380 * waste is ignored and the call does nothing).
382 * @param str the string
383 * @param max_waste the maximum amount of waste to ignore
384 * @returns #FALSE if the compact failed due to realloc failure
387 _dbus_string_compact (DBusString *str,
390 DBUS_STRING_PREAMBLE (str);
392 return compact (real, max_waste);
396 set_length (DBusRealString *real,
399 /* Note, we are setting the length not including nul termination */
401 /* exceeding max length is the same as failure to allocate memory */
402 if (_DBUS_UNLIKELY (new_length > real->max_length))
404 else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
405 _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
409 real->len = new_length;
410 real->str[new_length] = '\0';
417 DBusRealString *dest,
423 if (len > dest->max_length - dest->len)
424 return FALSE; /* detected overflow of dest->len + len below */
426 if (!set_length (dest, dest->len + len))
429 memmove (dest->str + insert_at + len,
430 dest->str + insert_at,
431 dest->len - len - insert_at);
436 #ifndef _dbus_string_get_data
438 * Gets the raw character buffer from the string. The returned buffer
439 * will be nul-terminated, but note that strings may contain binary
440 * data so there may be extra nul characters prior to the termination.
441 * This function should be little-used, extend DBusString or add
442 * stuff to dbus-sysdeps.c instead. It's an error to use this
443 * function on a const string.
445 * @param str the string
449 _dbus_string_get_data (DBusString *str)
451 DBUS_STRING_PREAMBLE (str);
453 return (char*) real->str;
455 #endif /* _dbus_string_get_data */
457 /* only do the function if we don't have the macro */
458 #ifndef _dbus_string_get_const_data
460 * Gets the raw character buffer from a const string.
462 * @param str the string
463 * @returns the string data
466 _dbus_string_get_const_data (const DBusString *str)
468 DBUS_CONST_STRING_PREAMBLE (str);
470 return (const char*) real->str;
472 #endif /* _dbus_string_get_const_data */
475 * Gets a sub-portion of the raw character buffer from the
476 * string. The "len" field is required simply for error
477 * checking, to be sure you don't try to use more
478 * string than exists. The nul termination of the
479 * returned buffer remains at the end of the entire
480 * string, not at start + len.
482 * @param str the string
483 * @param start byte offset to return
484 * @param len length of segment to return
485 * @returns the string data
488 _dbus_string_get_data_len (DBusString *str,
492 DBUS_STRING_PREAMBLE (str);
493 _dbus_assert (start >= 0);
494 _dbus_assert (len >= 0);
495 _dbus_assert (start <= real->len);
496 _dbus_assert (len <= real->len - start);
498 return (char*) real->str + start;
501 /* only do the function if we don't have the macro */
502 #ifndef _dbus_string_get_const_data_len
504 * const version of _dbus_string_get_data_len().
506 * @param str the string
507 * @param start byte offset to return
508 * @param len length of segment to return
509 * @returns the string data
512 _dbus_string_get_const_data_len (const DBusString *str,
516 DBUS_CONST_STRING_PREAMBLE (str);
517 _dbus_assert (start >= 0);
518 _dbus_assert (len >= 0);
519 _dbus_assert (start <= real->len);
520 _dbus_assert (len <= real->len - start);
522 return (const char*) real->str + start;
524 #endif /* _dbus_string_get_const_data_len */
526 /* only do the function if we don't have the macro */
527 #ifndef _dbus_string_set_byte
529 * Sets the value of the byte at the given position.
531 * @param str the string
532 * @param i the position
533 * @param byte the new value
536 _dbus_string_set_byte (DBusString *str,
540 DBUS_STRING_PREAMBLE (str);
541 _dbus_assert (i < real->len);
542 _dbus_assert (i >= 0);
546 #endif /* _dbus_string_set_byte */
548 /* only have the function if we didn't create a macro */
549 #ifndef _dbus_string_get_byte
551 * Gets the byte at the given position. It is
552 * allowed to ask for the nul byte at the end of
555 * @param str the string
556 * @param start the position
557 * @returns the byte at that position
560 _dbus_string_get_byte (const DBusString *str,
563 DBUS_CONST_STRING_PREAMBLE (str);
564 _dbus_assert (start <= real->len);
565 _dbus_assert (start >= 0);
567 return real->str[start];
569 #endif /* _dbus_string_get_byte */
572 * Inserts a number of bytes of a given value at the
575 * @param str the string
576 * @param i the position
577 * @param n_bytes number of bytes
578 * @param byte the value to insert
579 * @returns #TRUE on success
582 _dbus_string_insert_bytes (DBusString *str,
587 DBUS_STRING_PREAMBLE (str);
588 _dbus_assert (i <= real->len);
589 _dbus_assert (i >= 0);
590 _dbus_assert (n_bytes >= 0);
595 if (!open_gap (n_bytes, real, i))
598 memset (real->str + i, byte, n_bytes);
604 * Inserts a single byte at the given position.
606 * @param str the string
607 * @param i the position
608 * @param byte the value to insert
609 * @returns #TRUE on success
612 _dbus_string_insert_byte (DBusString *str,
616 DBUS_STRING_PREAMBLE (str);
617 _dbus_assert (i <= real->len);
618 _dbus_assert (i >= 0);
620 if (!open_gap (1, real, i))
629 * Like _dbus_string_get_data(), but removes the
630 * gotten data from the original string. The caller
631 * must free the data returned. This function may
632 * fail due to lack of memory, and return #FALSE.
634 * @param str the string
635 * @param data_return location to return the buffer
636 * @returns #TRUE on success
639 _dbus_string_steal_data (DBusString *str,
643 DBUS_STRING_PREAMBLE (str);
644 _dbus_assert (data_return != NULL);
646 undo_alignment (real);
648 *data_return = (char*) real->str;
650 old_max_length = real->max_length;
652 /* reset the string */
653 if (!_dbus_string_init (str))
655 /* hrm, put it back then */
656 real->str = (unsigned char*) *data_return;
658 fixup_alignment (real);
662 real->max_length = old_max_length;
667 #ifdef DBUS_BUILD_TESTS
669 * Like _dbus_string_get_data_len(), but removes the gotten data from
670 * the original string. The caller must free the data returned. This
671 * function may fail due to lack of memory, and return #FALSE.
672 * The returned string is nul-terminated and has length len.
674 * @todo this function is broken because on failure it
675 * may corrupt the source string.
677 * @param str the string
678 * @param data_return location to return the buffer
679 * @param start the start of segment to steal
680 * @param len the length of segment to steal
681 * @returns #TRUE on success
684 _dbus_string_steal_data_len (DBusString *str,
690 DBUS_STRING_PREAMBLE (str);
691 _dbus_assert (data_return != NULL);
692 _dbus_assert (start >= 0);
693 _dbus_assert (len >= 0);
694 _dbus_assert (start <= real->len);
695 _dbus_assert (len <= real->len - start);
697 if (!_dbus_string_init (&dest))
700 set_max_length (&dest, real->max_length);
702 if (!_dbus_string_move_len (str, start, len, &dest, 0))
704 _dbus_string_free (&dest);
708 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
709 if (!_dbus_string_steal_data (&dest, data_return))
711 _dbus_string_free (&dest);
715 _dbus_string_free (&dest);
718 #endif /* DBUS_BUILD_TESTS */
721 * Copies the data from the string into a char*
723 * @param str the string
724 * @param data_return place to return the data
725 * @returns #TRUE on success, #FALSE on no memory
728 _dbus_string_copy_data (const DBusString *str,
731 DBUS_CONST_STRING_PREAMBLE (str);
732 _dbus_assert (data_return != NULL);
734 *data_return = dbus_malloc (real->len + 1);
735 if (*data_return == NULL)
738 memcpy (*data_return, real->str, real->len + 1);
744 * Copies the contents of a DBusString into a different buffer. It is
745 * a bug if avail_len is too short to hold the string contents. nul
746 * termination is not copied, just the supplied bytes.
748 * @param str a string
749 * @param buffer a C buffer to copy data to
750 * @param avail_len maximum length of C buffer
753 _dbus_string_copy_to_buffer (const DBusString *str,
757 DBUS_CONST_STRING_PREAMBLE (str);
759 _dbus_assert (avail_len >= 0);
760 _dbus_assert (avail_len >= real->len);
762 memcpy (buffer, real->str, real->len);
766 * Copies the contents of a DBusString into a different buffer. It is
767 * a bug if avail_len is too short to hold the string contents plus a
770 * @param str a string
771 * @param buffer a C buffer to copy data to
772 * @param avail_len maximum length of C buffer
775 _dbus_string_copy_to_buffer_with_nul (const DBusString *str,
779 DBUS_CONST_STRING_PREAMBLE (str);
781 _dbus_assert (avail_len >= 0);
782 _dbus_assert (avail_len > real->len);
784 memcpy (buffer, real->str, real->len+1);
787 #ifdef DBUS_BUILD_TESTS
789 * Copies a segment of the string into a char*
791 * @param str the string
792 * @param data_return place to return the data
793 * @param start start index
794 * @param len length to copy
795 * @returns #FALSE if no memory
798 _dbus_string_copy_data_len (const DBusString *str,
805 DBUS_CONST_STRING_PREAMBLE (str);
806 _dbus_assert (data_return != NULL);
807 _dbus_assert (start >= 0);
808 _dbus_assert (len >= 0);
809 _dbus_assert (start <= real->len);
810 _dbus_assert (len <= real->len - start);
812 if (!_dbus_string_init (&dest))
815 set_max_length (&dest, real->max_length);
817 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
819 _dbus_string_free (&dest);
823 if (!_dbus_string_steal_data (&dest, data_return))
825 _dbus_string_free (&dest);
829 _dbus_string_free (&dest);
832 #endif /* DBUS_BUILD_TESTS */
834 /* Only have the function if we don't have the macro */
835 #ifndef _dbus_string_get_length
837 * Gets the length of a string (not including nul termination).
839 * @returns the length.
842 _dbus_string_get_length (const DBusString *str)
844 DBUS_CONST_STRING_PREAMBLE (str);
848 #endif /* !_dbus_string_get_length */
851 * Makes a string longer by the given number of bytes. Checks whether
852 * adding additional_length to the current length would overflow an
853 * integer, and checks for exceeding a string's max length.
854 * The new bytes are not initialized, other than nul-terminating
855 * the end of the string. The uninitialized bytes may contain
856 * nul bytes or other junk.
858 * @param str a string
859 * @param additional_length length to add to the string.
860 * @returns #TRUE on success.
863 _dbus_string_lengthen (DBusString *str,
864 int additional_length)
866 DBUS_STRING_PREAMBLE (str);
867 _dbus_assert (additional_length >= 0);
869 if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
870 return FALSE; /* would overflow */
872 return set_length (real,
873 real->len + additional_length);
877 * Makes a string shorter by the given number of bytes.
879 * @param str a string
880 * @param length_to_remove length to remove from the string.
883 _dbus_string_shorten (DBusString *str,
884 int length_to_remove)
886 DBUS_STRING_PREAMBLE (str);
887 _dbus_assert (length_to_remove >= 0);
888 _dbus_assert (length_to_remove <= real->len);
891 real->len - length_to_remove);
895 * Sets the length of a string. Can be used to truncate or lengthen
896 * the string. If the string is lengthened, the function may fail and
897 * return #FALSE. Newly-added bytes are not initialized, as with
898 * _dbus_string_lengthen().
900 * @param str a string
901 * @param length new length of the string.
902 * @returns #FALSE on failure.
905 _dbus_string_set_length (DBusString *str,
908 DBUS_STRING_PREAMBLE (str);
909 _dbus_assert (length >= 0);
911 return set_length (real, length);
915 align_insert_point_then_open_gap (DBusString *str,
920 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
921 unsigned long gap_pos;
924 DBUS_STRING_PREAMBLE (str);
925 _dbus_assert (alignment >= 1);
926 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
928 insert_at = *insert_at_p;
930 _dbus_assert (insert_at <= real->len);
932 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
933 new_len = real->len + (gap_pos - insert_at) + gap_size;
935 if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length))
938 delta = new_len - real->len;
939 _dbus_assert (delta >= 0);
941 if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
943 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
947 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
951 /* nul the padding if we had to add any padding */
952 if (gap_size < delta)
954 memset (&real->str[insert_at], '\0',
955 gap_pos - insert_at);
958 *insert_at_p = gap_pos;
964 align_length_then_lengthen (DBusString *str,
966 int then_lengthen_by)
970 insert_at = _dbus_string_get_length (str);
972 return align_insert_point_then_open_gap (str,
974 alignment, then_lengthen_by);
978 * Align the length of a string to a specific alignment (typically 4 or 8)
979 * by appending nul bytes to the string.
981 * @param str a string
982 * @param alignment the alignment
983 * @returns #FALSE if no memory
986 _dbus_string_align_length (DBusString *str,
989 return align_length_then_lengthen (str, alignment, 0);
993 * Preallocate extra_bytes such that a future lengthening of the
994 * string by extra_bytes is guaranteed to succeed without an out of
997 * @param str a string
998 * @param extra_bytes bytes to alloc
999 * @returns #FALSE if no memory
1002 _dbus_string_alloc_space (DBusString *str,
1005 if (!_dbus_string_lengthen (str, extra_bytes))
1007 _dbus_string_shorten (str, extra_bytes);
1013 append (DBusRealString *real,
1017 if (buffer_len == 0)
1020 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
1023 memcpy (real->str + (real->len - buffer_len),
1031 * Appends a nul-terminated C-style string to a DBusString.
1033 * @param str the DBusString
1034 * @param buffer the nul-terminated characters to append
1035 * @returns #FALSE if not enough memory.
1038 _dbus_string_append (DBusString *str,
1041 unsigned long buffer_len;
1043 DBUS_STRING_PREAMBLE (str);
1044 _dbus_assert (buffer != NULL);
1046 buffer_len = strlen (buffer);
1047 if (buffer_len > (unsigned long) real->max_length)
1050 return append (real, buffer, buffer_len);
1053 /** assign 2 bytes from one string to another */
1054 #define ASSIGN_2_OCTETS(p, octets) \
1055 *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
1057 /** assign 4 bytes from one string to another */
1058 #define ASSIGN_4_OCTETS(p, octets) \
1059 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
1061 #ifdef DBUS_HAVE_INT64
1062 /** assign 8 bytes from one string to another */
1063 #define ASSIGN_8_OCTETS(p, octets) \
1064 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
1066 /** assign 8 bytes from one string to another */
1067 #define ASSIGN_8_OCTETS(p, octets) \
1081 _dbus_assert (b == p + 8); \
1083 #endif /* DBUS_HAVE_INT64 */
1085 #ifdef DBUS_BUILD_TESTS
1087 * Appends 4 bytes aligned on a 4 byte boundary
1088 * with any alignment padding initialized to 0.
1090 * @param str the DBusString
1091 * @param octets 4 bytes to append
1092 * @returns #FALSE if not enough memory.
1095 _dbus_string_append_4_aligned (DBusString *str,
1096 const unsigned char octets[4])
1098 DBUS_STRING_PREAMBLE (str);
1100 if (!align_length_then_lengthen (str, 4, 4))
1103 ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
1107 #endif /* DBUS_BUILD_TESTS */
1109 #ifdef DBUS_BUILD_TESTS
1111 * Appends 8 bytes aligned on an 8 byte boundary
1112 * with any alignment padding initialized to 0.
1114 * @param str the DBusString
1115 * @param octets 8 bytes to append
1116 * @returns #FALSE if not enough memory.
1119 _dbus_string_append_8_aligned (DBusString *str,
1120 const unsigned char octets[8])
1122 DBUS_STRING_PREAMBLE (str);
1124 if (!align_length_then_lengthen (str, 8, 8))
1127 ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
1131 #endif /* DBUS_BUILD_TESTS */
1134 * Inserts 2 bytes aligned on a 2 byte boundary
1135 * with any alignment padding initialized to 0.
1137 * @param str the DBusString
1138 * @param insert_at where to insert
1139 * @param octets 2 bytes to insert
1140 * @returns #FALSE if not enough memory.
1143 _dbus_string_insert_2_aligned (DBusString *str,
1145 const unsigned char octets[4])
1147 DBUS_STRING_PREAMBLE (str);
1149 if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
1152 ASSIGN_2_OCTETS (real->str + insert_at, octets);
1158 * Inserts 4 bytes aligned on a 4 byte boundary
1159 * with any alignment padding initialized to 0.
1161 * @param str the DBusString
1162 * @param insert_at where to insert
1163 * @param octets 4 bytes to insert
1164 * @returns #FALSE if not enough memory.
1167 _dbus_string_insert_4_aligned (DBusString *str,
1169 const unsigned char octets[4])
1171 DBUS_STRING_PREAMBLE (str);
1173 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1176 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1182 * Inserts 8 bytes aligned on an 8 byte boundary
1183 * with any alignment padding initialized to 0.
1185 * @param str the DBusString
1186 * @param insert_at where to insert
1187 * @param octets 8 bytes to insert
1188 * @returns #FALSE if not enough memory.
1191 _dbus_string_insert_8_aligned (DBusString *str,
1193 const unsigned char octets[8])
1195 DBUS_STRING_PREAMBLE (str);
1197 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1200 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1202 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1209 * Inserts padding at *insert_at such to align it to the given
1210 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1211 * to the aligned position.
1213 * @param str the DBusString
1214 * @param insert_at location to be aligned
1215 * @param alignment alignment boundary (1, 2, 4, or 8)
1216 * @returns #FALSE if not enough memory.
1219 _dbus_string_insert_alignment (DBusString *str,
1223 DBUS_STRING_PREAMBLE (str);
1225 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1228 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1234 * Appends a printf-style formatted string
1235 * to the #DBusString.
1237 * @param str the string
1238 * @param format printf format
1239 * @param args variable argument list
1240 * @returns #FALSE if no memory
1243 _dbus_string_append_printf_valist (DBusString *str,
1250 DBUS_STRING_PREAMBLE (str);
1252 DBUS_VA_COPY (args_copy, args);
1254 /* Measure the message length without terminating nul */
1255 len = _dbus_printf_string_upper_bound (format, args);
1257 if (!_dbus_string_lengthen (str, len))
1259 /* don't leak the copy */
1264 vsprintf ((char*) (real->str + (real->len - len)),
1273 * Appends a printf-style formatted string
1274 * to the #DBusString.
1276 * @param str the string
1277 * @param format printf format
1278 * @returns #FALSE if no memory
1281 _dbus_string_append_printf (DBusString *str,
1288 va_start (args, format);
1289 retval = _dbus_string_append_printf_valist (str, format, args);
1296 * Appends block of bytes with the given length to a DBusString.
1298 * @param str the DBusString
1299 * @param buffer the bytes to append
1300 * @param len the number of bytes to append
1301 * @returns #FALSE if not enough memory.
1304 _dbus_string_append_len (DBusString *str,
1308 DBUS_STRING_PREAMBLE (str);
1309 _dbus_assert (buffer != NULL);
1310 _dbus_assert (len >= 0);
1312 return append (real, buffer, len);
1316 * Appends a single byte to the string, returning #FALSE
1317 * if not enough memory.
1319 * @param str the string
1320 * @param byte the byte to append
1321 * @returns #TRUE on success
1324 _dbus_string_append_byte (DBusString *str,
1327 DBUS_STRING_PREAMBLE (str);
1329 if (!set_length (real, real->len + 1))
1332 real->str[real->len-1] = byte;
1337 #ifdef DBUS_BUILD_TESTS
1339 * Appends a single Unicode character, encoding the character
1342 * @param str the string
1343 * @param ch the Unicode character
1346 _dbus_string_append_unichar (DBusString *str,
1354 DBUS_STRING_PREAMBLE (str);
1356 /* this code is from GLib but is pretty standard I think */
1365 else if (ch < 0x800)
1370 else if (ch < 0x10000)
1375 else if (ch < 0x200000)
1380 else if (ch < 0x4000000)
1391 if (len > (real->max_length - real->len))
1392 return FALSE; /* real->len + len would overflow */
1394 if (!set_length (real, real->len + len))
1397 out = real->str + (real->len - len);
1399 for (i = len - 1; i > 0; --i)
1401 out[i] = (ch & 0x3f) | 0x80;
1404 out[0] = ch | first;
1408 #endif /* DBUS_BUILD_TESTS */
1411 delete (DBusRealString *real,
1418 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1420 real->str[real->len] = '\0';
1424 * Deletes a segment of a DBusString with length len starting at
1425 * start. (Hint: to clear an entire string, setting length to 0
1426 * with _dbus_string_set_length() is easier.)
1428 * @param str the DBusString
1429 * @param start where to start deleting
1430 * @param len the number of bytes to delete
1433 _dbus_string_delete (DBusString *str,
1437 DBUS_STRING_PREAMBLE (str);
1438 _dbus_assert (start >= 0);
1439 _dbus_assert (len >= 0);
1440 _dbus_assert (start <= real->len);
1441 _dbus_assert (len <= real->len - start);
1443 delete (real, start, len);
1447 copy (DBusRealString *source,
1450 DBusRealString *dest,
1456 if (!open_gap (len, dest, insert_at))
1459 memmove (dest->str + insert_at,
1460 source->str + start,
1467 * Checks assertions for two strings we're copying a segment between,
1468 * and declares real_source/real_dest variables.
1470 * @param source the source string
1471 * @param start the starting offset
1472 * @param dest the dest string
1473 * @param insert_at where the copied segment is inserted
1475 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1476 DBusRealString *real_source = (DBusRealString*) source; \
1477 DBusRealString *real_dest = (DBusRealString*) dest; \
1478 _dbus_assert ((source) != (dest)); \
1479 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1480 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1481 _dbus_assert (!real_dest->constant); \
1482 _dbus_assert (!real_dest->locked); \
1483 _dbus_assert ((start) >= 0); \
1484 _dbus_assert ((start) <= real_source->len); \
1485 _dbus_assert ((insert_at) >= 0); \
1486 _dbus_assert ((insert_at) <= real_dest->len)
1489 * Moves the end of one string into another string. Both strings
1490 * must be initialized, valid strings.
1492 * @param source the source string
1493 * @param start where to chop off the source string
1494 * @param dest the destination string
1495 * @param insert_at where to move the chopped-off part of source string
1496 * @returns #FALSE if not enough memory
1499 _dbus_string_move (DBusString *source,
1504 DBusRealString *real_source = (DBusRealString*) source;
1505 _dbus_assert (start <= real_source->len);
1507 return _dbus_string_move_len (source, start,
1508 real_source->len - start,
1513 * Like _dbus_string_move(), but does not delete the section
1514 * of the source string that's copied to the dest string.
1516 * @param source the source string
1517 * @param start where to start copying the source string
1518 * @param dest the destination string
1519 * @param insert_at where to place the copied part of source string
1520 * @returns #FALSE if not enough memory
1523 _dbus_string_copy (const DBusString *source,
1528 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1530 return copy (real_source, start,
1531 real_source->len - start,
1537 * Like _dbus_string_move(), but can move a segment from
1538 * the middle of the source string.
1540 * @todo this doesn't do anything with max_length field.
1541 * we should probably just kill the max_length field though.
1543 * @param source the source string
1544 * @param start first byte of source string to move
1545 * @param len length of segment to move
1546 * @param dest the destination string
1547 * @param insert_at where to move the bytes from the source string
1548 * @returns #FALSE if not enough memory
1551 _dbus_string_move_len (DBusString *source,
1558 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1559 _dbus_assert (len >= 0);
1560 _dbus_assert ((start + len) <= real_source->len);
1567 else if (start == 0 &&
1568 len == real_source->len &&
1569 real_dest->len == 0)
1571 /* Short-circuit moving an entire existing string to an empty string
1572 * by just swapping the buffers.
1574 /* we assume ->constant doesn't matter as you can't have
1575 * a constant string involved in a move.
1577 #define ASSIGN_DATA(a, b) do { \
1578 (a)->str = (b)->str; \
1579 (a)->len = (b)->len; \
1580 (a)->allocated = (b)->allocated; \
1581 (a)->align_offset = (b)->align_offset; \
1586 ASSIGN_DATA (&tmp, real_source);
1587 ASSIGN_DATA (real_source, real_dest);
1588 ASSIGN_DATA (real_dest, &tmp);
1594 if (!copy (real_source, start, len,
1599 delete (real_source, start,
1607 * Like _dbus_string_copy(), but can copy a segment from the middle of
1608 * the source string.
1610 * @param source the source string
1611 * @param start where to start copying the source string
1612 * @param len length of segment to copy
1613 * @param dest the destination string
1614 * @param insert_at where to place the copied segment of source string
1615 * @returns #FALSE if not enough memory
1618 _dbus_string_copy_len (const DBusString *source,
1624 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1625 _dbus_assert (len >= 0);
1626 _dbus_assert (start <= real_source->len);
1627 _dbus_assert (len <= real_source->len - start);
1629 return copy (real_source, start, len,
1635 * Replaces a segment of dest string with a segment of source string.
1637 * @todo optimize the case where the two lengths are the same, and
1638 * avoid memmoving the data in the trailing part of the string twice.
1640 * @todo avoid inserting the source into dest, then deleting
1641 * the replaced chunk of dest (which creates a potentially large
1642 * intermediate string). Instead, extend the replaced chunk
1643 * of dest with padding to the same size as the source chunk,
1644 * then copy in the source bytes.
1646 * @param source the source string
1647 * @param start where to start copying the source string
1648 * @param len length of segment to copy
1649 * @param dest the destination string
1650 * @param replace_at start of segment of dest string to replace
1651 * @param replace_len length of segment of dest string to replace
1652 * @returns #FALSE if not enough memory
1656 _dbus_string_replace_len (const DBusString *source,
1663 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1664 _dbus_assert (len >= 0);
1665 _dbus_assert (start <= real_source->len);
1666 _dbus_assert (len <= real_source->len - start);
1667 _dbus_assert (replace_at >= 0);
1668 _dbus_assert (replace_at <= real_dest->len);
1669 _dbus_assert (replace_len <= real_dest->len - replace_at);
1671 if (!copy (real_source, start, len,
1672 real_dest, replace_at))
1675 delete (real_dest, replace_at + len, replace_len);
1681 * Looks for the first occurance of a byte, deletes that byte,
1682 * and moves everything after the byte to the beginning of a
1683 * separate string. Both strings must be initialized, valid
1686 * @param source the source string
1687 * @param byte the byte to remove and split the string at
1688 * @param tail the split off string
1689 * @returns #FALSE if not enough memory or if byte could not be found
1693 _dbus_string_split_on_byte (DBusString *source,
1698 char byte_string[2] = "";
1702 byte_string[0] = (char) byte;
1704 if (!_dbus_string_find (source, 0, byte_string, &byte_position))
1707 head_length = byte_position;
1708 tail_length = _dbus_string_get_length (source) - head_length - 1;
1710 if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
1714 /* remove the trailing delimiter byte from the head now.
1716 if (!_dbus_string_set_length (source, head_length))
1722 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1723 * Pennington, and Tom Tromey are the authors and authorized relicense.
1726 /** computes length and mask of a unicode character
1727 * @param Char the char
1728 * @param Mask the mask variable to assign to
1729 * @param Len the length variable to assign to
1731 #define UTF8_COMPUTE(Char, Mask, Len) \
1737 else if ((Char & 0xe0) == 0xc0) \
1742 else if ((Char & 0xf0) == 0xe0) \
1747 else if ((Char & 0xf8) == 0xf0) \
1752 else if ((Char & 0xfc) == 0xf8) \
1757 else if ((Char & 0xfe) == 0xfc) \
1769 * computes length of a unicode character in UTF-8
1770 * @param Char the char
1772 #define UTF8_LENGTH(Char) \
1773 ((Char) < 0x80 ? 1 : \
1774 ((Char) < 0x800 ? 2 : \
1775 ((Char) < 0x10000 ? 3 : \
1776 ((Char) < 0x200000 ? 4 : \
1777 ((Char) < 0x4000000 ? 5 : 6)))))
1780 * Gets a UTF-8 value.
1782 * @param Result variable for extracted unicode char.
1783 * @param Chars the bytes to decode
1784 * @param Count counter variable
1785 * @param Mask mask for this char
1786 * @param Len length for this char in bytes
1788 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1789 (Result) = (Chars)[0] & (Mask); \
1790 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1792 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1798 (Result) |= ((Chars)[(Count)] & 0x3f); \
1802 * Check whether a Unicode (5.2) char is in a valid range.
1804 * The first check comes from the Unicode guarantee to never encode
1805 * a point above 0x0010ffff, since UTF-16 couldn't represent it.
1807 * The second check covers surrogate pairs (category Cs).
1809 * The last two checks cover "Noncharacter": defined as:
1810 * "A code point that is permanently reserved for
1811 * internal use, and that should never be interchanged. In
1812 * Unicode 3.1, these consist of the values U+nFFFE and U+nFFFF
1813 * (where n is from 0 to 10_16) and the values U+FDD0..U+FDEF."
1815 * @param Char the character
1817 #define UNICODE_VALID(Char) \
1818 ((Char) < 0x110000 && \
1819 (((Char) & 0xFFFFF800) != 0xD800) && \
1820 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1821 ((Char) & 0xFFFE) != 0xFFFE)
1823 #ifdef DBUS_BUILD_TESTS
1825 * Gets a unicode character from a UTF-8 string. Does no validation;
1826 * you must verify that the string is valid UTF-8 in advance and must
1827 * pass in the start of a character.
1829 * @param str the string
1830 * @param start the start of the UTF-8 character.
1831 * @param ch_return location to return the character
1832 * @param end_return location to return the byte index of next character
1835 _dbus_string_get_unichar (const DBusString *str,
1837 dbus_unichar_t *ch_return,
1841 dbus_unichar_t result;
1844 DBUS_CONST_STRING_PREAMBLE (str);
1845 _dbus_assert (start >= 0);
1846 _dbus_assert (start <= real->len);
1851 *end_return = real->len;
1854 p = real->str + start;
1857 UTF8_COMPUTE (c, mask, len);
1860 UTF8_GET (result, p, i, mask, len);
1862 if (result == (dbus_unichar_t)-1)
1866 *ch_return = result;
1868 *end_return = start + len;
1870 #endif /* DBUS_BUILD_TESTS */
1873 * Finds the given substring in the string,
1874 * returning #TRUE and filling in the byte index
1875 * where the substring was found, if it was found.
1876 * Returns #FALSE if the substring wasn't found.
1877 * Sets *start to the length of the string if the substring
1880 * @param str the string
1881 * @param start where to start looking
1882 * @param substr the substring
1883 * @param found return location for where it was found, or #NULL
1884 * @returns #TRUE if found
1887 _dbus_string_find (const DBusString *str,
1892 return _dbus_string_find_to (str, start,
1893 ((const DBusRealString*)str)->len,
1898 * Finds end of line ("\r\n" or "\n") in the string,
1899 * returning #TRUE and filling in the byte index
1900 * where the eol string was found, if it was found.
1901 * Returns #FALSE if eol wasn't found.
1903 * @param str the string
1904 * @param start where to start looking
1905 * @param found return location for where eol was found or string length otherwise
1906 * @param found_len return length of found eol string or zero otherwise
1907 * @returns #TRUE if found
1910 _dbus_string_find_eol (const DBusString *str,
1917 DBUS_CONST_STRING_PREAMBLE (str);
1918 _dbus_assert (start <= real->len);
1919 _dbus_assert (start >= 0);
1922 while (i < real->len)
1924 if (real->str[i] == '\r')
1926 if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
1934 else /* only "\r" */
1943 else if (real->str[i] == '\n') /* only "\n" */
1964 * Finds the given substring in the string,
1965 * up to a certain position,
1966 * returning #TRUE and filling in the byte index
1967 * where the substring was found, if it was found.
1968 * Returns #FALSE if the substring wasn't found.
1969 * Sets *start to the length of the string if the substring
1972 * @param str the string
1973 * @param start where to start looking
1974 * @param end where to stop looking
1975 * @param substr the substring
1976 * @param found return location for where it was found, or #NULL
1977 * @returns #TRUE if found
1980 _dbus_string_find_to (const DBusString *str,
1987 DBUS_CONST_STRING_PREAMBLE (str);
1988 _dbus_assert (substr != NULL);
1989 _dbus_assert (start <= real->len);
1990 _dbus_assert (start >= 0);
1991 _dbus_assert (substr != NULL);
1992 _dbus_assert (end <= real->len);
1993 _dbus_assert (start <= end);
1995 /* we always "find" an empty string */
1996 if (*substr == '\0')
2006 if (real->str[i] == substr[0])
2012 if (substr[j - i] == '\0')
2014 else if (real->str[j] != substr[j - i])
2020 if (substr[j - i] == '\0')
2038 * Finds a blank (space or tab) in the string. Returns #TRUE
2039 * if found, #FALSE otherwise. If a blank is not found sets
2040 * *found to the length of the string.
2042 * @param str the string
2043 * @param start byte index to start looking
2044 * @param found place to store the location of the first blank
2045 * @returns #TRUE if a blank was found
2048 _dbus_string_find_blank (const DBusString *str,
2053 DBUS_CONST_STRING_PREAMBLE (str);
2054 _dbus_assert (start <= real->len);
2055 _dbus_assert (start >= 0);
2058 while (i < real->len)
2060 if (real->str[i] == ' ' ||
2061 real->str[i] == '\t')
2078 * Skips blanks from start, storing the first non-blank in *end
2079 * (blank is space or tab).
2081 * @param str the string
2082 * @param start where to start
2083 * @param end where to store the first non-blank byte index
2086 _dbus_string_skip_blank (const DBusString *str,
2091 DBUS_CONST_STRING_PREAMBLE (str);
2092 _dbus_assert (start <= real->len);
2093 _dbus_assert (start >= 0);
2096 while (i < real->len)
2098 if (!DBUS_IS_ASCII_BLANK (real->str[i]))
2104 _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
2112 * Skips whitespace from start, storing the first non-whitespace in *end.
2113 * (whitespace is space, tab, newline, CR).
2115 * @param str the string
2116 * @param start where to start
2117 * @param end where to store the first non-whitespace byte index
2120 _dbus_string_skip_white (const DBusString *str,
2125 DBUS_CONST_STRING_PREAMBLE (str);
2126 _dbus_assert (start <= real->len);
2127 _dbus_assert (start >= 0);
2130 while (i < real->len)
2132 if (!DBUS_IS_ASCII_WHITE (real->str[i]))
2138 _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
2145 * Skips whitespace from end, storing the start index of the trailing
2146 * whitespace in *start. (whitespace is space, tab, newline, CR).
2148 * @param str the string
2149 * @param end where to start scanning backward
2150 * @param start where to store the start of whitespace chars
2153 _dbus_string_skip_white_reverse (const DBusString *str,
2158 DBUS_CONST_STRING_PREAMBLE (str);
2159 _dbus_assert (end <= real->len);
2160 _dbus_assert (end >= 0);
2165 if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
2170 _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
2177 * Assigns a newline-terminated or \\r\\n-terminated line from the front
2178 * of the string to the given dest string. The dest string's previous
2179 * contents are deleted. If the source string contains no newline,
2180 * moves the entire source string to the dest string.
2182 * @todo owen correctly notes that this is a stupid function (it was
2183 * written purely for test code,
2184 * e.g. dbus-message-builder.c). Probably should be enforced as test
2185 * code only with ifdef DBUS_BUILD_TESTS
2187 * @param source the source string
2188 * @param dest the destination string (contents are replaced)
2189 * @returns #FALSE if no memory, or source has length 0
2192 _dbus_string_pop_line (DBusString *source,
2197 _dbus_string_set_length (dest, 0);
2201 if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
2203 _dbus_assert (eol == _dbus_string_get_length (source));
2206 /* If there's no newline and source has zero length, we're done */
2209 /* otherwise, the last line of the file has no eol characters */
2212 /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
2213 * since find_eol returned TRUE
2216 if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
2219 /* remove line ending */
2220 if (!_dbus_string_set_length (dest, eol))
2222 _dbus_assert_not_reached ("out of memory when shortening a string");
2229 #ifdef DBUS_BUILD_TESTS
2231 * Deletes up to and including the first blank space
2234 * @param str the string
2237 _dbus_string_delete_first_word (DBusString *str)
2241 if (_dbus_string_find_blank (str, 0, &i))
2242 _dbus_string_skip_blank (str, i, &i);
2244 _dbus_string_delete (str, 0, i);
2248 #ifdef DBUS_BUILD_TESTS
2250 * Deletes any leading blanks in the string
2252 * @param str the string
2255 _dbus_string_delete_leading_blanks (DBusString *str)
2259 _dbus_string_skip_blank (str, 0, &i);
2262 _dbus_string_delete (str, 0, i);
2267 * Deletes leading and trailing whitespace
2269 * @param str the string
2272 _dbus_string_chop_white(DBusString *str)
2276 _dbus_string_skip_white (str, 0, &i);
2279 _dbus_string_delete (str, 0, i);
2281 _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
2283 _dbus_string_set_length (str, i);
2287 * Tests two DBusString for equality.
2289 * @todo memcmp is probably faster
2291 * @param a first string
2292 * @param b second string
2293 * @returns #TRUE if equal
2296 _dbus_string_equal (const DBusString *a,
2297 const DBusString *b)
2299 const unsigned char *ap;
2300 const unsigned char *bp;
2301 const unsigned char *a_end;
2302 const DBusRealString *real_a = (const DBusRealString*) a;
2303 const DBusRealString *real_b = (const DBusRealString*) b;
2304 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2305 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2307 if (real_a->len != real_b->len)
2312 a_end = real_a->str + real_a->len;
2326 * Tests two DBusString for equality up to the given length.
2327 * The strings may be shorter than the given length.
2329 * @todo write a unit test
2331 * @todo memcmp is probably faster
2333 * @param a first string
2334 * @param b second string
2335 * @param len the maximum length to look at
2336 * @returns #TRUE if equal for the given number of bytes
2339 _dbus_string_equal_len (const DBusString *a,
2340 const DBusString *b,
2343 const unsigned char *ap;
2344 const unsigned char *bp;
2345 const unsigned char *a_end;
2346 const DBusRealString *real_a = (const DBusRealString*) a;
2347 const DBusRealString *real_b = (const DBusRealString*) b;
2348 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2349 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2351 if (real_a->len != real_b->len &&
2352 (real_a->len < len || real_b->len < len))
2357 a_end = real_a->str + MIN (real_a->len, len);
2371 * Tests two sub-parts of two DBusString for equality. The specified
2372 * range of the first string must exist; the specified start position
2373 * of the second string must exist.
2375 * @todo write a unit test
2377 * @todo memcmp is probably faster
2379 * @param a first string
2380 * @param a_start where to start substring in first string
2381 * @param a_len length of substring in first string
2382 * @param b second string
2383 * @param b_start where to start substring in second string
2384 * @returns #TRUE if the two substrings are equal
2387 _dbus_string_equal_substring (const DBusString *a,
2390 const DBusString *b,
2393 const unsigned char *ap;
2394 const unsigned char *bp;
2395 const unsigned char *a_end;
2396 const DBusRealString *real_a = (const DBusRealString*) a;
2397 const DBusRealString *real_b = (const DBusRealString*) b;
2398 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2399 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2400 _dbus_assert (a_start >= 0);
2401 _dbus_assert (a_len >= 0);
2402 _dbus_assert (a_start <= real_a->len);
2403 _dbus_assert (a_len <= real_a->len - a_start);
2404 _dbus_assert (b_start >= 0);
2405 _dbus_assert (b_start <= real_b->len);
2407 if (a_len > real_b->len - b_start)
2410 ap = real_a->str + a_start;
2411 bp = real_b->str + b_start;
2422 _dbus_assert (bp <= (real_b->str + real_b->len));
2428 * Checks whether a string is equal to a C string.
2430 * @param a the string
2431 * @param c_str the C string
2432 * @returns #TRUE if equal
2435 _dbus_string_equal_c_str (const DBusString *a,
2438 const unsigned char *ap;
2439 const unsigned char *bp;
2440 const unsigned char *a_end;
2441 const DBusRealString *real_a = (const DBusRealString*) a;
2442 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2443 _dbus_assert (c_str != NULL);
2446 bp = (const unsigned char*) c_str;
2447 a_end = real_a->str + real_a->len;
2448 while (ap != a_end && *bp)
2457 if (ap != a_end || *bp)
2463 #ifdef DBUS_BUILD_TESTS
2465 * Checks whether a string starts with the given C string.
2467 * @param a the string
2468 * @param c_str the C string
2469 * @returns #TRUE if string starts with it
2472 _dbus_string_starts_with_c_str (const DBusString *a,
2475 const unsigned char *ap;
2476 const unsigned char *bp;
2477 const unsigned char *a_end;
2478 const DBusRealString *real_a = (const DBusRealString*) a;
2479 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2480 _dbus_assert (c_str != NULL);
2483 bp = (const unsigned char*) c_str;
2484 a_end = real_a->str + real_a->len;
2485 while (ap != a_end && *bp)
2499 #endif /* DBUS_BUILD_TESTS */
2502 * Appends a two-character hex digit to a string, where the hex digit
2503 * has the value of the given byte.
2505 * @param str the string
2506 * @param byte the byte
2507 * @returns #FALSE if no memory
2510 _dbus_string_append_byte_as_hex (DBusString *str,
2513 const char hexdigits[16] = {
2514 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2515 'a', 'b', 'c', 'd', 'e', 'f'
2518 if (!_dbus_string_append_byte (str,
2519 hexdigits[(byte >> 4)]))
2522 if (!_dbus_string_append_byte (str,
2523 hexdigits[(byte & 0x0f)]))
2525 _dbus_string_set_length (str,
2526 _dbus_string_get_length (str) - 1);
2534 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2535 * encoded. (Each byte is two hex digits.)
2537 * @param source the string to encode
2538 * @param start byte index to start encoding
2539 * @param dest string where encoded data should be placed
2540 * @param insert_at where to place encoded data
2541 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2544 _dbus_string_hex_encode (const DBusString *source,
2550 const unsigned char *p;
2551 const unsigned char *end;
2554 _dbus_assert (start <= _dbus_string_get_length (source));
2556 if (!_dbus_string_init (&result))
2561 p = (const unsigned char*) _dbus_string_get_const_data (source);
2562 end = p + _dbus_string_get_length (source);
2567 if (!_dbus_string_append_byte_as_hex (&result, *p))
2573 if (!_dbus_string_move (&result, 0, dest, insert_at))
2579 _dbus_string_free (&result);
2584 * Decodes a string from hex encoding.
2586 * @param source the string to decode
2587 * @param start byte index to start decode
2588 * @param end_return return location of the end of the hex data, or #NULL
2589 * @param dest string where decoded data should be placed
2590 * @param insert_at where to place decoded data
2591 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2594 _dbus_string_hex_decode (const DBusString *source,
2601 const unsigned char *p;
2602 const unsigned char *end;
2604 dbus_bool_t high_bits;
2606 _dbus_assert (start <= _dbus_string_get_length (source));
2608 if (!_dbus_string_init (&result))
2614 p = (const unsigned char*) _dbus_string_get_const_data (source);
2615 end = p + _dbus_string_get_length (source);
2684 if (!_dbus_string_append_byte (&result,
2693 len = _dbus_string_get_length (&result);
2695 b = _dbus_string_get_byte (&result, len - 1);
2699 _dbus_string_set_byte (&result, len - 1, b);
2702 high_bits = !high_bits;
2708 if (!_dbus_string_move (&result, 0, dest, insert_at))
2712 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2717 _dbus_string_free (&result);
2722 * Checks that the given range of the string is valid ASCII with no
2723 * nul bytes. If the given range is not entirely contained in the
2724 * string, returns #FALSE.
2726 * @todo this is inconsistent with most of DBusString in that
2727 * it allows a start,len range that extends past the string end.
2729 * @param str the string
2730 * @param start first byte index to check
2731 * @param len number of bytes to check
2732 * @returns #TRUE if the byte range exists and is all valid ASCII
2735 _dbus_string_validate_ascii (const DBusString *str,
2739 const unsigned char *s;
2740 const unsigned char *end;
2741 DBUS_CONST_STRING_PREAMBLE (str);
2742 _dbus_assert (start >= 0);
2743 _dbus_assert (start <= real->len);
2744 _dbus_assert (len >= 0);
2746 if (len > real->len - start)
2749 s = real->str + start;
2753 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2763 * Converts the given range of the string to lower case.
2765 * @param str the string
2766 * @param start first byte index to convert
2767 * @param len number of bytes to convert
2770 _dbus_string_tolower_ascii (const DBusString *str,
2776 DBUS_STRING_PREAMBLE (str);
2777 _dbus_assert (start >= 0);
2778 _dbus_assert (start <= real->len);
2779 _dbus_assert (len >= 0);
2780 _dbus_assert (len <= real->len - start);
2782 s = real->str + start;
2787 if (*s >= 'A' && *s <= 'Z')
2794 * Converts the given range of the string to upper case.
2796 * @param str the string
2797 * @param start first byte index to convert
2798 * @param len number of bytes to convert
2801 _dbus_string_toupper_ascii (const DBusString *str,
2807 DBUS_STRING_PREAMBLE (str);
2808 _dbus_assert (start >= 0);
2809 _dbus_assert (start <= real->len);
2810 _dbus_assert (len >= 0);
2811 _dbus_assert (len <= real->len - start);
2813 s = real->str + start;
2818 if (*s >= 'a' && *s <= 'z')
2825 * Checks that the given range of the string is valid UTF-8. If the
2826 * given range is not entirely contained in the string, returns
2827 * #FALSE. If the string contains any nul bytes in the given range,
2828 * returns #FALSE. If the start and start+len are not on character
2829 * boundaries, returns #FALSE.
2831 * @todo this is inconsistent with most of DBusString in that
2832 * it allows a start,len range that extends past the string end.
2834 * @param str the string
2835 * @param start first byte index to check
2836 * @param len number of bytes to check
2837 * @returns #TRUE if the byte range exists and is all valid UTF-8
2840 _dbus_string_validate_utf8 (const DBusString *str,
2844 const unsigned char *p;
2845 const unsigned char *end;
2846 DBUS_CONST_STRING_PREAMBLE (str);
2847 _dbus_assert (start >= 0);
2848 _dbus_assert (start <= real->len);
2849 _dbus_assert (len >= 0);
2851 /* we are doing _DBUS_UNLIKELY() here which might be
2852 * dubious in a generic library like GLib, but in D-Bus
2853 * we know we're validating messages and that it would
2854 * only be evil/broken apps that would have invalid
2855 * UTF-8. Also, this function seems to be a performance
2856 * bottleneck in profiles.
2859 if (_DBUS_UNLIKELY (len > real->len - start))
2862 p = real->str + start;
2867 int i, mask, char_len;
2868 dbus_unichar_t result;
2870 /* nul bytes considered invalid */
2874 /* Special-case ASCII; this makes us go a lot faster in
2875 * D-Bus profiles where we are typically validating
2876 * function names and such. We have to know that
2877 * all following checks will pass for ASCII though,
2878 * comments follow ...
2886 UTF8_COMPUTE (*p, mask, char_len);
2888 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2891 /* check that the expected number of bytes exists in the remaining length */
2892 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2895 UTF8_GET (result, p, i, mask, char_len);
2897 /* Check for overlong UTF-8 */
2898 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2901 /* The UNICODE_VALID check below will catch this */
2902 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2906 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2909 /* UNICODE_VALID should have caught it */
2910 _dbus_assert (result != (dbus_unichar_t)-1);
2915 /* See that we covered the entire length if a length was
2918 if (_DBUS_UNLIKELY (p != end))
2925 * Checks that the given range of the string is all nul bytes. If the
2926 * given range is not entirely contained in the string, returns
2929 * @todo this is inconsistent with most of DBusString in that
2930 * it allows a start,len range that extends past the string end.
2932 * @param str the string
2933 * @param start first byte index to check
2934 * @param len number of bytes to check
2935 * @returns #TRUE if the byte range exists and is all nul bytes
2938 _dbus_string_validate_nul (const DBusString *str,
2942 const unsigned char *s;
2943 const unsigned char *end;
2944 DBUS_CONST_STRING_PREAMBLE (str);
2945 _dbus_assert (start >= 0);
2946 _dbus_assert (len >= 0);
2947 _dbus_assert (start <= real->len);
2949 if (len > real->len - start)
2952 s = real->str + start;
2956 if (_DBUS_UNLIKELY (*s != '\0'))
2965 * Clears all allocated bytes in the string to zero.
2967 * @param str the string
2970 _dbus_string_zero (DBusString *str)
2972 DBUS_STRING_PREAMBLE (str);
2974 memset (real->str - real->align_offset, '\0', real->allocated);
2978 /* tests are in dbus-string-util.c */