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;
669 * Copies the data from the string into a char*
671 * @param str the string
672 * @param data_return place to return the data
673 * @returns #TRUE on success, #FALSE on no memory
676 _dbus_string_copy_data (const DBusString *str,
679 DBUS_CONST_STRING_PREAMBLE (str);
680 _dbus_assert (data_return != NULL);
682 *data_return = dbus_malloc (real->len + 1);
683 if (*data_return == NULL)
686 memcpy (*data_return, real->str, real->len + 1);
692 * Copies the contents of a DBusString into a different buffer. It is
693 * a bug if avail_len is too short to hold the string contents. nul
694 * termination is not copied, just the supplied bytes.
696 * @param str a string
697 * @param buffer a C buffer to copy data to
698 * @param avail_len maximum length of C buffer
701 _dbus_string_copy_to_buffer (const DBusString *str,
705 DBUS_CONST_STRING_PREAMBLE (str);
707 _dbus_assert (avail_len >= 0);
708 _dbus_assert (avail_len >= real->len);
710 memcpy (buffer, real->str, real->len);
714 * Copies the contents of a DBusString into a different buffer. It is
715 * a bug if avail_len is too short to hold the string contents plus a
718 * @param str a string
719 * @param buffer a C buffer to copy data to
720 * @param avail_len maximum length of C buffer
723 _dbus_string_copy_to_buffer_with_nul (const DBusString *str,
727 DBUS_CONST_STRING_PREAMBLE (str);
729 _dbus_assert (avail_len >= 0);
730 _dbus_assert (avail_len > real->len);
732 memcpy (buffer, real->str, real->len+1);
735 /* Only have the function if we don't have the macro */
736 #ifndef _dbus_string_get_length
738 * Gets the length of a string (not including nul termination).
740 * @returns the length.
743 _dbus_string_get_length (const DBusString *str)
745 DBUS_CONST_STRING_PREAMBLE (str);
749 #endif /* !_dbus_string_get_length */
752 * Makes a string longer by the given number of bytes. Checks whether
753 * adding additional_length to the current length would overflow an
754 * integer, and checks for exceeding a string's max length.
755 * The new bytes are not initialized, other than nul-terminating
756 * the end of the string. The uninitialized bytes may contain
757 * nul bytes or other junk.
759 * @param str a string
760 * @param additional_length length to add to the string.
761 * @returns #TRUE on success.
764 _dbus_string_lengthen (DBusString *str,
765 int additional_length)
767 DBUS_STRING_PREAMBLE (str);
768 _dbus_assert (additional_length >= 0);
770 if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
771 return FALSE; /* would overflow */
773 return set_length (real,
774 real->len + additional_length);
778 * Makes a string shorter by the given number of bytes.
780 * @param str a string
781 * @param length_to_remove length to remove from the string.
784 _dbus_string_shorten (DBusString *str,
785 int length_to_remove)
787 DBUS_STRING_PREAMBLE (str);
788 _dbus_assert (length_to_remove >= 0);
789 _dbus_assert (length_to_remove <= real->len);
792 real->len - length_to_remove);
796 * Sets the length of a string. Can be used to truncate or lengthen
797 * the string. If the string is lengthened, the function may fail and
798 * return #FALSE. Newly-added bytes are not initialized, as with
799 * _dbus_string_lengthen().
801 * @param str a string
802 * @param length new length of the string.
803 * @returns #FALSE on failure.
806 _dbus_string_set_length (DBusString *str,
809 DBUS_STRING_PREAMBLE (str);
810 _dbus_assert (length >= 0);
812 return set_length (real, length);
816 align_insert_point_then_open_gap (DBusString *str,
821 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
822 unsigned long gap_pos;
825 DBUS_STRING_PREAMBLE (str);
826 _dbus_assert (alignment >= 1);
827 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
829 insert_at = *insert_at_p;
831 _dbus_assert (insert_at <= real->len);
833 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
834 new_len = real->len + (gap_pos - insert_at) + gap_size;
836 if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length))
839 delta = new_len - real->len;
840 _dbus_assert (delta >= 0);
842 if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
844 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
848 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
852 /* nul the padding if we had to add any padding */
853 if (gap_size < delta)
855 memset (&real->str[insert_at], '\0',
856 gap_pos - insert_at);
859 *insert_at_p = gap_pos;
865 align_length_then_lengthen (DBusString *str,
867 int then_lengthen_by)
871 insert_at = _dbus_string_get_length (str);
873 return align_insert_point_then_open_gap (str,
875 alignment, then_lengthen_by);
879 * Align the length of a string to a specific alignment (typically 4 or 8)
880 * by appending nul bytes to the string.
882 * @param str a string
883 * @param alignment the alignment
884 * @returns #FALSE if no memory
887 _dbus_string_align_length (DBusString *str,
890 return align_length_then_lengthen (str, alignment, 0);
894 * Preallocate extra_bytes such that a future lengthening of the
895 * string by extra_bytes is guaranteed to succeed without an out of
898 * @param str a string
899 * @param extra_bytes bytes to alloc
900 * @returns #FALSE if no memory
903 _dbus_string_alloc_space (DBusString *str,
906 if (!_dbus_string_lengthen (str, extra_bytes))
908 _dbus_string_shorten (str, extra_bytes);
914 append (DBusRealString *real,
921 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
924 memcpy (real->str + (real->len - buffer_len),
932 * Appends a nul-terminated C-style string to a DBusString.
934 * @param str the DBusString
935 * @param buffer the nul-terminated characters to append
936 * @returns #FALSE if not enough memory.
939 _dbus_string_append (DBusString *str,
942 unsigned long buffer_len;
944 DBUS_STRING_PREAMBLE (str);
945 _dbus_assert (buffer != NULL);
947 buffer_len = strlen (buffer);
948 if (buffer_len > (unsigned long) real->max_length)
951 return append (real, buffer, buffer_len);
954 /** assign 2 bytes from one string to another */
955 #define ASSIGN_2_OCTETS(p, octets) \
956 *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
958 /** assign 4 bytes from one string to another */
959 #define ASSIGN_4_OCTETS(p, octets) \
960 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
962 #ifdef DBUS_HAVE_INT64
963 /** assign 8 bytes from one string to another */
964 #define ASSIGN_8_OCTETS(p, octets) \
965 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
967 /** assign 8 bytes from one string to another */
968 #define ASSIGN_8_OCTETS(p, octets) \
982 _dbus_assert (b == p + 8); \
984 #endif /* DBUS_HAVE_INT64 */
986 #ifdef DBUS_BUILD_TESTS
988 * Appends 4 bytes aligned on a 4 byte boundary
989 * with any alignment padding initialized to 0.
991 * @param str the DBusString
992 * @param octets 4 bytes to append
993 * @returns #FALSE if not enough memory.
996 _dbus_string_append_4_aligned (DBusString *str,
997 const unsigned char octets[4])
999 DBUS_STRING_PREAMBLE (str);
1001 if (!align_length_then_lengthen (str, 4, 4))
1004 ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
1008 #endif /* DBUS_BUILD_TESTS */
1010 #ifdef DBUS_BUILD_TESTS
1012 * Appends 8 bytes aligned on an 8 byte boundary
1013 * with any alignment padding initialized to 0.
1015 * @param str the DBusString
1016 * @param octets 8 bytes to append
1017 * @returns #FALSE if not enough memory.
1020 _dbus_string_append_8_aligned (DBusString *str,
1021 const unsigned char octets[8])
1023 DBUS_STRING_PREAMBLE (str);
1025 if (!align_length_then_lengthen (str, 8, 8))
1028 ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
1032 #endif /* DBUS_BUILD_TESTS */
1035 * Inserts 2 bytes aligned on a 2 byte boundary
1036 * with any alignment padding initialized to 0.
1038 * @param str the DBusString
1039 * @param insert_at where to insert
1040 * @param octets 2 bytes to insert
1041 * @returns #FALSE if not enough memory.
1044 _dbus_string_insert_2_aligned (DBusString *str,
1046 const unsigned char octets[4])
1048 DBUS_STRING_PREAMBLE (str);
1050 if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
1053 ASSIGN_2_OCTETS (real->str + insert_at, octets);
1059 * Inserts 4 bytes aligned on a 4 byte boundary
1060 * with any alignment padding initialized to 0.
1062 * @param str the DBusString
1063 * @param insert_at where to insert
1064 * @param octets 4 bytes to insert
1065 * @returns #FALSE if not enough memory.
1068 _dbus_string_insert_4_aligned (DBusString *str,
1070 const unsigned char octets[4])
1072 DBUS_STRING_PREAMBLE (str);
1074 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1077 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1083 * Inserts 8 bytes aligned on an 8 byte boundary
1084 * with any alignment padding initialized to 0.
1086 * @param str the DBusString
1087 * @param insert_at where to insert
1088 * @param octets 8 bytes to insert
1089 * @returns #FALSE if not enough memory.
1092 _dbus_string_insert_8_aligned (DBusString *str,
1094 const unsigned char octets[8])
1096 DBUS_STRING_PREAMBLE (str);
1098 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1101 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1103 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1110 * Inserts padding at *insert_at such to align it to the given
1111 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1112 * to the aligned position.
1114 * @param str the DBusString
1115 * @param insert_at location to be aligned
1116 * @param alignment alignment boundary (1, 2, 4, or 8)
1117 * @returns #FALSE if not enough memory.
1120 _dbus_string_insert_alignment (DBusString *str,
1124 DBUS_STRING_PREAMBLE (str);
1126 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1129 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1135 * Appends a printf-style formatted string
1136 * to the #DBusString.
1138 * @param str the string
1139 * @param format printf format
1140 * @param args variable argument list
1141 * @returns #FALSE if no memory
1144 _dbus_string_append_printf_valist (DBusString *str,
1151 DBUS_STRING_PREAMBLE (str);
1153 DBUS_VA_COPY (args_copy, args);
1155 /* Measure the message length without terminating nul */
1156 len = _dbus_printf_string_upper_bound (format, args);
1158 if (!_dbus_string_lengthen (str, len))
1160 /* don't leak the copy */
1165 vsprintf ((char*) (real->str + (real->len - len)),
1174 * Appends a printf-style formatted string
1175 * to the #DBusString.
1177 * @param str the string
1178 * @param format printf format
1179 * @returns #FALSE if no memory
1182 _dbus_string_append_printf (DBusString *str,
1189 va_start (args, format);
1190 retval = _dbus_string_append_printf_valist (str, format, args);
1197 * Appends block of bytes with the given length to a DBusString.
1199 * @param str the DBusString
1200 * @param buffer the bytes to append
1201 * @param len the number of bytes to append
1202 * @returns #FALSE if not enough memory.
1205 _dbus_string_append_len (DBusString *str,
1209 DBUS_STRING_PREAMBLE (str);
1210 _dbus_assert (buffer != NULL);
1211 _dbus_assert (len >= 0);
1213 return append (real, buffer, len);
1217 * Appends a single byte to the string, returning #FALSE
1218 * if not enough memory.
1220 * @param str the string
1221 * @param byte the byte to append
1222 * @returns #TRUE on success
1225 _dbus_string_append_byte (DBusString *str,
1228 DBUS_STRING_PREAMBLE (str);
1230 if (!set_length (real, real->len + 1))
1233 real->str[real->len-1] = byte;
1238 #ifdef DBUS_BUILD_TESTS
1240 * Appends a single Unicode character, encoding the character
1243 * @param str the string
1244 * @param ch the Unicode character
1247 _dbus_string_append_unichar (DBusString *str,
1255 DBUS_STRING_PREAMBLE (str);
1257 /* this code is from GLib but is pretty standard I think */
1266 else if (ch < 0x800)
1271 else if (ch < 0x10000)
1276 else if (ch < 0x200000)
1281 else if (ch < 0x4000000)
1292 if (len > (real->max_length - real->len))
1293 return FALSE; /* real->len + len would overflow */
1295 if (!set_length (real, real->len + len))
1298 out = real->str + (real->len - len);
1300 for (i = len - 1; i > 0; --i)
1302 out[i] = (ch & 0x3f) | 0x80;
1305 out[0] = ch | first;
1309 #endif /* DBUS_BUILD_TESTS */
1312 delete (DBusRealString *real,
1319 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1321 real->str[real->len] = '\0';
1325 * Deletes a segment of a DBusString with length len starting at
1326 * start. (Hint: to clear an entire string, setting length to 0
1327 * with _dbus_string_set_length() is easier.)
1329 * @param str the DBusString
1330 * @param start where to start deleting
1331 * @param len the number of bytes to delete
1334 _dbus_string_delete (DBusString *str,
1338 DBUS_STRING_PREAMBLE (str);
1339 _dbus_assert (start >= 0);
1340 _dbus_assert (len >= 0);
1341 _dbus_assert (start <= real->len);
1342 _dbus_assert (len <= real->len - start);
1344 delete (real, start, len);
1348 copy (DBusRealString *source,
1351 DBusRealString *dest,
1357 if (!open_gap (len, dest, insert_at))
1360 memmove (dest->str + insert_at,
1361 source->str + start,
1368 * Checks assertions for two strings we're copying a segment between,
1369 * and declares real_source/real_dest variables.
1371 * @param source the source string
1372 * @param start the starting offset
1373 * @param dest the dest string
1374 * @param insert_at where the copied segment is inserted
1376 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1377 DBusRealString *real_source = (DBusRealString*) source; \
1378 DBusRealString *real_dest = (DBusRealString*) dest; \
1379 _dbus_assert ((source) != (dest)); \
1380 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1381 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1382 _dbus_assert (!real_dest->constant); \
1383 _dbus_assert (!real_dest->locked); \
1384 _dbus_assert ((start) >= 0); \
1385 _dbus_assert ((start) <= real_source->len); \
1386 _dbus_assert ((insert_at) >= 0); \
1387 _dbus_assert ((insert_at) <= real_dest->len)
1390 * Moves the end of one string into another string. Both strings
1391 * must be initialized, valid strings.
1393 * @param source the source string
1394 * @param start where to chop off the source string
1395 * @param dest the destination string
1396 * @param insert_at where to move the chopped-off part of source string
1397 * @returns #FALSE if not enough memory
1400 _dbus_string_move (DBusString *source,
1405 DBusRealString *real_source = (DBusRealString*) source;
1406 _dbus_assert (start <= real_source->len);
1408 return _dbus_string_move_len (source, start,
1409 real_source->len - start,
1414 * Like _dbus_string_move(), but does not delete the section
1415 * of the source string that's copied to the dest string.
1417 * @param source the source string
1418 * @param start where to start copying the source string
1419 * @param dest the destination string
1420 * @param insert_at where to place the copied part of source string
1421 * @returns #FALSE if not enough memory
1424 _dbus_string_copy (const DBusString *source,
1429 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1431 return copy (real_source, start,
1432 real_source->len - start,
1438 * Like _dbus_string_move(), but can move a segment from
1439 * the middle of the source string.
1441 * @todo this doesn't do anything with max_length field.
1442 * we should probably just kill the max_length field though.
1444 * @param source the source string
1445 * @param start first byte of source string to move
1446 * @param len length of segment to move
1447 * @param dest the destination string
1448 * @param insert_at where to move the bytes from the source string
1449 * @returns #FALSE if not enough memory
1452 _dbus_string_move_len (DBusString *source,
1459 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1460 _dbus_assert (len >= 0);
1461 _dbus_assert ((start + len) <= real_source->len);
1468 else if (start == 0 &&
1469 len == real_source->len &&
1470 real_dest->len == 0)
1472 /* Short-circuit moving an entire existing string to an empty string
1473 * by just swapping the buffers.
1475 /* we assume ->constant doesn't matter as you can't have
1476 * a constant string involved in a move.
1478 #define ASSIGN_DATA(a, b) do { \
1479 (a)->str = (b)->str; \
1480 (a)->len = (b)->len; \
1481 (a)->allocated = (b)->allocated; \
1482 (a)->align_offset = (b)->align_offset; \
1487 ASSIGN_DATA (&tmp, real_source);
1488 ASSIGN_DATA (real_source, real_dest);
1489 ASSIGN_DATA (real_dest, &tmp);
1495 if (!copy (real_source, start, len,
1500 delete (real_source, start,
1508 * Like _dbus_string_copy(), but can copy a segment from the middle of
1509 * the source string.
1511 * @param source the source string
1512 * @param start where to start copying the source string
1513 * @param len length of segment to copy
1514 * @param dest the destination string
1515 * @param insert_at where to place the copied segment of source string
1516 * @returns #FALSE if not enough memory
1519 _dbus_string_copy_len (const DBusString *source,
1525 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1526 _dbus_assert (len >= 0);
1527 _dbus_assert (start <= real_source->len);
1528 _dbus_assert (len <= real_source->len - start);
1530 return copy (real_source, start, len,
1536 * Replaces a segment of dest string with a segment of source string.
1538 * @param source the source string
1539 * @param start where to start copying the source string
1540 * @param len length of segment to copy
1541 * @param dest the destination string
1542 * @param replace_at start of segment of dest string to replace
1543 * @param replace_len length of segment of dest string to replace
1544 * @returns #FALSE if not enough memory
1548 _dbus_string_replace_len (const DBusString *source,
1555 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1556 _dbus_assert (len >= 0);
1557 _dbus_assert (start <= real_source->len);
1558 _dbus_assert (len <= real_source->len - start);
1559 _dbus_assert (replace_at >= 0);
1560 _dbus_assert (replace_at <= real_dest->len);
1561 _dbus_assert (replace_len <= real_dest->len - replace_at);
1563 if (len == replace_len)
1565 memmove (real_dest->str + replace_at,
1566 real_source->str + start, len);
1568 else if (len < replace_len)
1570 memmove (real_dest->str + replace_at,
1571 real_source->str + start, len);
1572 delete (real_dest, replace_at + len,
1579 _dbus_assert (len > replace_len);
1581 diff = len - replace_len;
1583 /* First of all we check if destination string can be enlarged as
1584 * required, then we overwrite previous bytes
1587 if (!copy (real_source, start + replace_len, diff,
1588 real_dest, replace_at + replace_len))
1591 memmove (real_dest->str + replace_at,
1592 real_source->str + start, replace_len);
1599 * Looks for the first occurance of a byte, deletes that byte,
1600 * and moves everything after the byte to the beginning of a
1601 * separate string. Both strings must be initialized, valid
1604 * @param source the source string
1605 * @param byte the byte to remove and split the string at
1606 * @param tail the split off string
1607 * @returns #FALSE if not enough memory or if byte could not be found
1611 _dbus_string_split_on_byte (DBusString *source,
1616 char byte_string[2] = "";
1620 byte_string[0] = (char) byte;
1622 if (!_dbus_string_find (source, 0, byte_string, &byte_position))
1625 head_length = byte_position;
1626 tail_length = _dbus_string_get_length (source) - head_length - 1;
1628 if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
1632 /* remove the trailing delimiter byte from the head now.
1634 if (!_dbus_string_set_length (source, head_length))
1640 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1641 * Pennington, and Tom Tromey are the authors and authorized relicense.
1644 /** computes length and mask of a unicode character
1645 * @param Char the char
1646 * @param Mask the mask variable to assign to
1647 * @param Len the length variable to assign to
1649 #define UTF8_COMPUTE(Char, Mask, Len) \
1655 else if ((Char & 0xe0) == 0xc0) \
1660 else if ((Char & 0xf0) == 0xe0) \
1665 else if ((Char & 0xf8) == 0xf0) \
1670 else if ((Char & 0xfc) == 0xf8) \
1675 else if ((Char & 0xfe) == 0xfc) \
1687 * computes length of a unicode character in UTF-8
1688 * @param Char the char
1690 #define UTF8_LENGTH(Char) \
1691 ((Char) < 0x80 ? 1 : \
1692 ((Char) < 0x800 ? 2 : \
1693 ((Char) < 0x10000 ? 3 : \
1694 ((Char) < 0x200000 ? 4 : \
1695 ((Char) < 0x4000000 ? 5 : 6)))))
1698 * Gets a UTF-8 value.
1700 * @param Result variable for extracted unicode char.
1701 * @param Chars the bytes to decode
1702 * @param Count counter variable
1703 * @param Mask mask for this char
1704 * @param Len length for this char in bytes
1706 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1707 (Result) = (Chars)[0] & (Mask); \
1708 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1710 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1716 (Result) |= ((Chars)[(Count)] & 0x3f); \
1720 * Check whether a Unicode (5.2) char is in a valid range.
1722 * The first check comes from the Unicode guarantee to never encode
1723 * a point above 0x0010ffff, since UTF-16 couldn't represent it.
1725 * The second check covers surrogate pairs (category Cs).
1727 * The last two checks cover "Noncharacter": defined as:
1728 * "A code point that is permanently reserved for
1729 * internal use, and that should never be interchanged. In
1730 * Unicode 3.1, these consist of the values U+nFFFE and U+nFFFF
1731 * (where n is from 0 to 10_16) and the values U+FDD0..U+FDEF."
1733 * @param Char the character
1735 #define UNICODE_VALID(Char) \
1736 ((Char) < 0x110000 && \
1737 (((Char) & 0xFFFFF800) != 0xD800) && \
1738 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1739 ((Char) & 0xFFFE) != 0xFFFE)
1741 #ifdef DBUS_BUILD_TESTS
1743 * Gets a unicode character from a UTF-8 string. Does no validation;
1744 * you must verify that the string is valid UTF-8 in advance and must
1745 * pass in the start of a character.
1747 * @param str the string
1748 * @param start the start of the UTF-8 character.
1749 * @param ch_return location to return the character
1750 * @param end_return location to return the byte index of next character
1753 _dbus_string_get_unichar (const DBusString *str,
1755 dbus_unichar_t *ch_return,
1759 dbus_unichar_t result;
1762 DBUS_CONST_STRING_PREAMBLE (str);
1763 _dbus_assert (start >= 0);
1764 _dbus_assert (start <= real->len);
1769 *end_return = real->len;
1772 p = real->str + start;
1775 UTF8_COMPUTE (c, mask, len);
1778 UTF8_GET (result, p, i, mask, len);
1780 if (result == (dbus_unichar_t)-1)
1784 *ch_return = result;
1786 *end_return = start + len;
1788 #endif /* DBUS_BUILD_TESTS */
1791 * Finds the given substring in the string,
1792 * returning #TRUE and filling in the byte index
1793 * where the substring was found, if it was found.
1794 * Returns #FALSE if the substring wasn't found.
1795 * Sets *start to the length of the string if the substring
1798 * @param str the string
1799 * @param start where to start looking
1800 * @param substr the substring
1801 * @param found return location for where it was found, or #NULL
1802 * @returns #TRUE if found
1805 _dbus_string_find (const DBusString *str,
1810 return _dbus_string_find_to (str, start,
1811 ((const DBusRealString*)str)->len,
1816 * Finds end of line ("\r\n" or "\n") in the string,
1817 * returning #TRUE and filling in the byte index
1818 * where the eol string was found, if it was found.
1819 * Returns #FALSE if eol wasn't found.
1821 * @param str the string
1822 * @param start where to start looking
1823 * @param found return location for where eol was found or string length otherwise
1824 * @param found_len return length of found eol string or zero otherwise
1825 * @returns #TRUE if found
1828 _dbus_string_find_eol (const DBusString *str,
1835 DBUS_CONST_STRING_PREAMBLE (str);
1836 _dbus_assert (start <= real->len);
1837 _dbus_assert (start >= 0);
1840 while (i < real->len)
1842 if (real->str[i] == '\r')
1844 if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
1852 else /* only "\r" */
1861 else if (real->str[i] == '\n') /* only "\n" */
1882 * Finds the given substring in the string,
1883 * up to a certain position,
1884 * returning #TRUE and filling in the byte index
1885 * where the substring was found, if it was found.
1886 * Returns #FALSE if the substring wasn't found.
1887 * Sets *start to the length of the string if the substring
1890 * @param str the string
1891 * @param start where to start looking
1892 * @param end where to stop looking
1893 * @param substr the substring
1894 * @param found return location for where it was found, or #NULL
1895 * @returns #TRUE if found
1898 _dbus_string_find_to (const DBusString *str,
1905 DBUS_CONST_STRING_PREAMBLE (str);
1906 _dbus_assert (substr != NULL);
1907 _dbus_assert (start <= real->len);
1908 _dbus_assert (start >= 0);
1909 _dbus_assert (substr != NULL);
1910 _dbus_assert (end <= real->len);
1911 _dbus_assert (start <= end);
1913 /* we always "find" an empty string */
1914 if (*substr == '\0')
1924 if (real->str[i] == substr[0])
1930 if (substr[j - i] == '\0')
1932 else if (real->str[j] != substr[j - i])
1938 if (substr[j - i] == '\0')
1956 * Finds a blank (space or tab) in the string. Returns #TRUE
1957 * if found, #FALSE otherwise. If a blank is not found sets
1958 * *found to the length of the string.
1960 * @param str the string
1961 * @param start byte index to start looking
1962 * @param found place to store the location of the first blank
1963 * @returns #TRUE if a blank was found
1966 _dbus_string_find_blank (const DBusString *str,
1971 DBUS_CONST_STRING_PREAMBLE (str);
1972 _dbus_assert (start <= real->len);
1973 _dbus_assert (start >= 0);
1976 while (i < real->len)
1978 if (real->str[i] == ' ' ||
1979 real->str[i] == '\t')
1996 * Skips blanks from start, storing the first non-blank in *end
1997 * (blank is space or tab).
1999 * @param str the string
2000 * @param start where to start
2001 * @param end where to store the first non-blank byte index
2004 _dbus_string_skip_blank (const DBusString *str,
2009 DBUS_CONST_STRING_PREAMBLE (str);
2010 _dbus_assert (start <= real->len);
2011 _dbus_assert (start >= 0);
2014 while (i < real->len)
2016 if (!DBUS_IS_ASCII_BLANK (real->str[i]))
2022 _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
2030 * Skips whitespace from start, storing the first non-whitespace in *end.
2031 * (whitespace is space, tab, newline, CR).
2033 * @param str the string
2034 * @param start where to start
2035 * @param end where to store the first non-whitespace byte index
2038 _dbus_string_skip_white (const DBusString *str,
2043 DBUS_CONST_STRING_PREAMBLE (str);
2044 _dbus_assert (start <= real->len);
2045 _dbus_assert (start >= 0);
2048 while (i < real->len)
2050 if (!DBUS_IS_ASCII_WHITE (real->str[i]))
2056 _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
2063 * Skips whitespace from end, storing the start index of the trailing
2064 * whitespace in *start. (whitespace is space, tab, newline, CR).
2066 * @param str the string
2067 * @param end where to start scanning backward
2068 * @param start where to store the start of whitespace chars
2071 _dbus_string_skip_white_reverse (const DBusString *str,
2076 DBUS_CONST_STRING_PREAMBLE (str);
2077 _dbus_assert (end <= real->len);
2078 _dbus_assert (end >= 0);
2083 if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
2088 _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
2095 * Assigns a newline-terminated or \\r\\n-terminated line from the front
2096 * of the string to the given dest string. The dest string's previous
2097 * contents are deleted. If the source string contains no newline,
2098 * moves the entire source string to the dest string.
2100 * @todo owen correctly notes that this is a stupid function (it was
2101 * written purely for test code,
2102 * e.g. dbus-message-builder.c). Probably should be enforced as test
2103 * code only with ifdef DBUS_BUILD_TESTS
2105 * @param source the source string
2106 * @param dest the destination string (contents are replaced)
2107 * @returns #FALSE if no memory, or source has length 0
2110 _dbus_string_pop_line (DBusString *source,
2115 _dbus_string_set_length (dest, 0);
2119 if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
2121 _dbus_assert (eol == _dbus_string_get_length (source));
2124 /* If there's no newline and source has zero length, we're done */
2127 /* otherwise, the last line of the file has no eol characters */
2130 /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
2131 * since find_eol returned TRUE
2134 if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
2137 /* remove line ending */
2138 if (!_dbus_string_set_length (dest, eol))
2140 _dbus_assert_not_reached ("out of memory when shortening a string");
2147 #ifdef DBUS_BUILD_TESTS
2149 * Deletes up to and including the first blank space
2152 * @param str the string
2155 _dbus_string_delete_first_word (DBusString *str)
2159 if (_dbus_string_find_blank (str, 0, &i))
2160 _dbus_string_skip_blank (str, i, &i);
2162 _dbus_string_delete (str, 0, i);
2166 #ifdef DBUS_BUILD_TESTS
2168 * Deletes any leading blanks in the string
2170 * @param str the string
2173 _dbus_string_delete_leading_blanks (DBusString *str)
2177 _dbus_string_skip_blank (str, 0, &i);
2180 _dbus_string_delete (str, 0, i);
2185 * Deletes leading and trailing whitespace
2187 * @param str the string
2190 _dbus_string_chop_white(DBusString *str)
2194 _dbus_string_skip_white (str, 0, &i);
2197 _dbus_string_delete (str, 0, i);
2199 _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
2201 _dbus_string_set_length (str, i);
2205 * Tests two DBusString for equality.
2207 * @todo memcmp is probably faster
2209 * @param a first string
2210 * @param b second string
2211 * @returns #TRUE if equal
2214 _dbus_string_equal (const DBusString *a,
2215 const DBusString *b)
2217 const unsigned char *ap;
2218 const unsigned char *bp;
2219 const unsigned char *a_end;
2220 const DBusRealString *real_a = (const DBusRealString*) a;
2221 const DBusRealString *real_b = (const DBusRealString*) b;
2222 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2223 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2225 if (real_a->len != real_b->len)
2230 a_end = real_a->str + real_a->len;
2244 * Tests two DBusString for equality up to the given length.
2245 * The strings may be shorter than the given length.
2247 * @todo write a unit test
2249 * @todo memcmp is probably faster
2251 * @param a first string
2252 * @param b second string
2253 * @param len the maximum length to look at
2254 * @returns #TRUE if equal for the given number of bytes
2257 _dbus_string_equal_len (const DBusString *a,
2258 const DBusString *b,
2261 const unsigned char *ap;
2262 const unsigned char *bp;
2263 const unsigned char *a_end;
2264 const DBusRealString *real_a = (const DBusRealString*) a;
2265 const DBusRealString *real_b = (const DBusRealString*) b;
2266 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2267 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2269 if (real_a->len != real_b->len &&
2270 (real_a->len < len || real_b->len < len))
2275 a_end = real_a->str + MIN (real_a->len, len);
2289 * Tests two sub-parts of two DBusString for equality. The specified
2290 * range of the first string must exist; the specified start position
2291 * of the second string must exist.
2293 * @todo write a unit test
2295 * @todo memcmp is probably faster
2297 * @param a first string
2298 * @param a_start where to start substring in first string
2299 * @param a_len length of substring in first string
2300 * @param b second string
2301 * @param b_start where to start substring in second string
2302 * @returns #TRUE if the two substrings are equal
2305 _dbus_string_equal_substring (const DBusString *a,
2308 const DBusString *b,
2311 const unsigned char *ap;
2312 const unsigned char *bp;
2313 const unsigned char *a_end;
2314 const DBusRealString *real_a = (const DBusRealString*) a;
2315 const DBusRealString *real_b = (const DBusRealString*) b;
2316 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2317 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2318 _dbus_assert (a_start >= 0);
2319 _dbus_assert (a_len >= 0);
2320 _dbus_assert (a_start <= real_a->len);
2321 _dbus_assert (a_len <= real_a->len - a_start);
2322 _dbus_assert (b_start >= 0);
2323 _dbus_assert (b_start <= real_b->len);
2325 if (a_len > real_b->len - b_start)
2328 ap = real_a->str + a_start;
2329 bp = real_b->str + b_start;
2340 _dbus_assert (bp <= (real_b->str + real_b->len));
2346 * Checks whether a string is equal to a C string.
2348 * @param a the string
2349 * @param c_str the C string
2350 * @returns #TRUE if equal
2353 _dbus_string_equal_c_str (const DBusString *a,
2356 const unsigned char *ap;
2357 const unsigned char *bp;
2358 const unsigned char *a_end;
2359 const DBusRealString *real_a = (const DBusRealString*) a;
2360 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2361 _dbus_assert (c_str != NULL);
2364 bp = (const unsigned char*) c_str;
2365 a_end = real_a->str + real_a->len;
2366 while (ap != a_end && *bp)
2375 if (ap != a_end || *bp)
2381 #ifdef DBUS_BUILD_TESTS
2383 * Checks whether a string starts with the given C string.
2385 * @param a the string
2386 * @param c_str the C string
2387 * @returns #TRUE if string starts with it
2390 _dbus_string_starts_with_c_str (const DBusString *a,
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 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2398 _dbus_assert (c_str != NULL);
2401 bp = (const unsigned char*) c_str;
2402 a_end = real_a->str + real_a->len;
2403 while (ap != a_end && *bp)
2417 #endif /* DBUS_BUILD_TESTS */
2420 * Appends a two-character hex digit to a string, where the hex digit
2421 * has the value of the given byte.
2423 * @param str the string
2424 * @param byte the byte
2425 * @returns #FALSE if no memory
2428 _dbus_string_append_byte_as_hex (DBusString *str,
2431 const char hexdigits[16] = {
2432 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2433 'a', 'b', 'c', 'd', 'e', 'f'
2436 if (!_dbus_string_append_byte (str,
2437 hexdigits[(byte >> 4)]))
2440 if (!_dbus_string_append_byte (str,
2441 hexdigits[(byte & 0x0f)]))
2443 _dbus_string_set_length (str,
2444 _dbus_string_get_length (str) - 1);
2452 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2453 * encoded. (Each byte is two hex digits.)
2455 * @param source the string to encode
2456 * @param start byte index to start encoding
2457 * @param dest string where encoded data should be placed
2458 * @param insert_at where to place encoded data
2459 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2462 _dbus_string_hex_encode (const DBusString *source,
2468 const unsigned char *p;
2469 const unsigned char *end;
2472 _dbus_assert (start <= _dbus_string_get_length (source));
2474 if (!_dbus_string_init (&result))
2479 p = (const unsigned char*) _dbus_string_get_const_data (source);
2480 end = p + _dbus_string_get_length (source);
2485 if (!_dbus_string_append_byte_as_hex (&result, *p))
2491 if (!_dbus_string_move (&result, 0, dest, insert_at))
2497 _dbus_string_free (&result);
2502 * Decodes a string from hex encoding.
2504 * @param source the string to decode
2505 * @param start byte index to start decode
2506 * @param end_return return location of the end of the hex data, or #NULL
2507 * @param dest string where decoded data should be placed
2508 * @param insert_at where to place decoded data
2509 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2512 _dbus_string_hex_decode (const DBusString *source,
2519 const unsigned char *p;
2520 const unsigned char *end;
2522 dbus_bool_t high_bits;
2524 _dbus_assert (start <= _dbus_string_get_length (source));
2526 if (!_dbus_string_init (&result))
2532 p = (const unsigned char*) _dbus_string_get_const_data (source);
2533 end = p + _dbus_string_get_length (source);
2602 if (!_dbus_string_append_byte (&result,
2611 len = _dbus_string_get_length (&result);
2613 b = _dbus_string_get_byte (&result, len - 1);
2617 _dbus_string_set_byte (&result, len - 1, b);
2620 high_bits = !high_bits;
2626 if (!_dbus_string_move (&result, 0, dest, insert_at))
2630 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2635 _dbus_string_free (&result);
2640 * Checks that the given range of the string is valid ASCII with no
2641 * nul bytes. If the given range is not entirely contained in the
2642 * string, returns #FALSE.
2644 * @todo this is inconsistent with most of DBusString in that
2645 * it allows a start,len range that extends past the string end.
2647 * @param str the string
2648 * @param start first byte index to check
2649 * @param len number of bytes to check
2650 * @returns #TRUE if the byte range exists and is all valid ASCII
2653 _dbus_string_validate_ascii (const DBusString *str,
2657 const unsigned char *s;
2658 const unsigned char *end;
2659 DBUS_CONST_STRING_PREAMBLE (str);
2660 _dbus_assert (start >= 0);
2661 _dbus_assert (start <= real->len);
2662 _dbus_assert (len >= 0);
2664 if (len > real->len - start)
2667 s = real->str + start;
2671 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2681 * Converts the given range of the string to lower case.
2683 * @param str the string
2684 * @param start first byte index to convert
2685 * @param len number of bytes to convert
2688 _dbus_string_tolower_ascii (const DBusString *str,
2694 DBUS_STRING_PREAMBLE (str);
2695 _dbus_assert (start >= 0);
2696 _dbus_assert (start <= real->len);
2697 _dbus_assert (len >= 0);
2698 _dbus_assert (len <= real->len - start);
2700 s = real->str + start;
2705 if (*s >= 'A' && *s <= 'Z')
2712 * Converts the given range of the string to upper case.
2714 * @param str the string
2715 * @param start first byte index to convert
2716 * @param len number of bytes to convert
2719 _dbus_string_toupper_ascii (const DBusString *str,
2725 DBUS_STRING_PREAMBLE (str);
2726 _dbus_assert (start >= 0);
2727 _dbus_assert (start <= real->len);
2728 _dbus_assert (len >= 0);
2729 _dbus_assert (len <= real->len - start);
2731 s = real->str + start;
2736 if (*s >= 'a' && *s <= 'z')
2743 * Checks that the given range of the string is valid UTF-8. If the
2744 * given range is not entirely contained in the string, returns
2745 * #FALSE. If the string contains any nul bytes in the given range,
2746 * returns #FALSE. If the start and start+len are not on character
2747 * boundaries, returns #FALSE.
2749 * @todo this is inconsistent with most of DBusString in that
2750 * it allows a start,len range that extends past the string end.
2752 * @param str the string
2753 * @param start first byte index to check
2754 * @param len number of bytes to check
2755 * @returns #TRUE if the byte range exists and is all valid UTF-8
2758 _dbus_string_validate_utf8 (const DBusString *str,
2762 const unsigned char *p;
2763 const unsigned char *end;
2764 DBUS_CONST_STRING_PREAMBLE (str);
2765 _dbus_assert (start >= 0);
2766 _dbus_assert (start <= real->len);
2767 _dbus_assert (len >= 0);
2769 /* we are doing _DBUS_UNLIKELY() here which might be
2770 * dubious in a generic library like GLib, but in D-Bus
2771 * we know we're validating messages and that it would
2772 * only be evil/broken apps that would have invalid
2773 * UTF-8. Also, this function seems to be a performance
2774 * bottleneck in profiles.
2777 if (_DBUS_UNLIKELY (len > real->len - start))
2780 p = real->str + start;
2785 int i, mask, char_len;
2786 dbus_unichar_t result;
2788 /* nul bytes considered invalid */
2792 /* Special-case ASCII; this makes us go a lot faster in
2793 * D-Bus profiles where we are typically validating
2794 * function names and such. We have to know that
2795 * all following checks will pass for ASCII though,
2796 * comments follow ...
2804 UTF8_COMPUTE (*p, mask, char_len);
2806 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2809 /* check that the expected number of bytes exists in the remaining length */
2810 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2813 UTF8_GET (result, p, i, mask, char_len);
2815 /* Check for overlong UTF-8 */
2816 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2819 /* The UNICODE_VALID check below will catch this */
2820 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2824 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2827 /* UNICODE_VALID should have caught it */
2828 _dbus_assert (result != (dbus_unichar_t)-1);
2833 /* See that we covered the entire length if a length was
2836 if (_DBUS_UNLIKELY (p != end))
2843 * Checks that the given range of the string is all nul bytes. If the
2844 * given range is not entirely contained in the string, returns
2847 * @todo this is inconsistent with most of DBusString in that
2848 * it allows a start,len range that extends past the string end.
2850 * @param str the string
2851 * @param start first byte index to check
2852 * @param len number of bytes to check
2853 * @returns #TRUE if the byte range exists and is all nul bytes
2856 _dbus_string_validate_nul (const DBusString *str,
2860 const unsigned char *s;
2861 const unsigned char *end;
2862 DBUS_CONST_STRING_PREAMBLE (str);
2863 _dbus_assert (start >= 0);
2864 _dbus_assert (len >= 0);
2865 _dbus_assert (start <= real->len);
2867 if (len > real->len - start)
2870 s = real->str + start;
2874 if (_DBUS_UNLIKELY (*s != '\0'))
2883 * Clears all allocated bytes in the string to zero.
2885 * @param str the string
2888 _dbus_string_zero (DBusString *str)
2890 DBUS_STRING_PREAMBLE (str);
2892 memset (real->str - real->align_offset, '\0', real->allocated);
2896 /* tests are in dbus-string-util.c */