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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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;
274 #ifdef DBUS_BUILD_TESTS
275 /* Not using this feature at the moment,
276 * so marked DBUS_BUILD_TESTS-only
279 * Locks a string such that any attempts to change the string will
280 * result in aborting the program. Also, if the string is wasting a
281 * lot of memory (allocation is sufficiently larger than what the
282 * string is really using), _dbus_string_lock() will realloc the
283 * string's data to "compact" it.
285 * @param str the string to lock.
288 _dbus_string_lock (DBusString *str)
290 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
294 /* Try to realloc to avoid excess memory usage, since
295 * we know we won't change the string further
298 if (real->allocated - MAX_WASTE > real->len)
300 unsigned char *new_str;
303 new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
305 new_str = dbus_realloc (real->str - real->align_offset,
309 real->str = new_str + real->align_offset;
310 real->allocated = new_allocated;
311 fixup_alignment (real);
315 #endif /* DBUS_BUILD_TESTS */
318 reallocate_for_length (DBusRealString *real,
322 unsigned char *new_str;
324 /* at least double our old allocation to avoid O(n), avoiding
327 if (real->allocated > (_DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
328 new_allocated = _DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
330 new_allocated = real->allocated * 2;
332 /* if you change the code just above here, run the tests without
333 * the following assert-only hack before you commit
335 /* This is keyed off asserts in addition to tests so when you
336 * disable asserts to profile, you don't get this destroyer
339 #ifdef DBUS_DISABLE_ASSERT
341 #ifdef DBUS_BUILD_TESTS
342 new_allocated = 0; /* ensure a realloc every time so that we go
343 * through all malloc failure codepaths
345 #endif /* DBUS_BUILD_TESTS */
346 #endif /* !DBUS_DISABLE_ASSERT */
348 /* But be sure we always alloc at least space for the new length */
349 new_allocated = MAX (new_allocated,
350 new_length + _DBUS_STRING_ALLOCATION_PADDING);
352 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
353 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
354 if (_DBUS_UNLIKELY (new_str == NULL))
357 real->str = new_str + real->align_offset;
358 real->allocated = new_allocated;
359 fixup_alignment (real);
365 set_length (DBusRealString *real,
368 /* Note, we are setting the length not including nul termination */
370 /* exceeding max length is the same as failure to allocate memory */
371 if (_DBUS_UNLIKELY (new_length > real->max_length))
373 else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
374 _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
378 real->len = new_length;
379 real->str[new_length] = '\0';
386 DBusRealString *dest,
392 if (len > dest->max_length - dest->len)
393 return FALSE; /* detected overflow of dest->len + len below */
395 if (!set_length (dest, dest->len + len))
398 memmove (dest->str + insert_at + len,
399 dest->str + insert_at,
400 dest->len - len - insert_at);
405 #ifndef _dbus_string_get_data
407 * Gets the raw character buffer from the string. The returned buffer
408 * will be nul-terminated, but note that strings may contain binary
409 * data so there may be extra nul characters prior to the termination.
410 * This function should be little-used, extend DBusString or add
411 * stuff to dbus-sysdeps.c instead. It's an error to use this
412 * function on a const string.
414 * @param str the string
418 _dbus_string_get_data (DBusString *str)
420 DBUS_STRING_PREAMBLE (str);
422 return (char*) real->str;
424 #endif /* _dbus_string_get_data */
426 /* only do the function if we don't have the macro */
427 #ifndef _dbus_string_get_const_data
429 * Gets the raw character buffer from a const string.
431 * @param str the string
432 * @returns the string data
435 _dbus_string_get_const_data (const DBusString *str)
437 DBUS_CONST_STRING_PREAMBLE (str);
439 return (const char*) real->str;
441 #endif /* _dbus_string_get_const_data */
444 * Gets a sub-portion of the raw character buffer from the
445 * string. The "len" field is required simply for error
446 * checking, to be sure you don't try to use more
447 * string than exists. The nul termination of the
448 * returned buffer remains at the end of the entire
449 * string, not at start + len.
451 * @param str the string
452 * @param start byte offset to return
453 * @param len length of segment to return
454 * @returns the string data
457 _dbus_string_get_data_len (DBusString *str,
461 DBUS_STRING_PREAMBLE (str);
462 _dbus_assert (start >= 0);
463 _dbus_assert (len >= 0);
464 _dbus_assert (start <= real->len);
465 _dbus_assert (len <= real->len - start);
467 return (char*) real->str + start;
470 /* only do the function if we don't have the macro */
471 #ifndef _dbus_string_get_const_data_len
473 * const version of _dbus_string_get_data_len().
475 * @param str the string
476 * @param start byte offset to return
477 * @param len length of segment to return
478 * @returns the string data
481 _dbus_string_get_const_data_len (const DBusString *str,
485 DBUS_CONST_STRING_PREAMBLE (str);
486 _dbus_assert (start >= 0);
487 _dbus_assert (len >= 0);
488 _dbus_assert (start <= real->len);
489 _dbus_assert (len <= real->len - start);
491 return (const char*) real->str + start;
493 #endif /* _dbus_string_get_const_data_len */
495 /* only do the function if we don't have the macro */
496 #ifndef _dbus_string_set_byte
498 * Sets the value of the byte at the given position.
500 * @param str the string
501 * @param i the position
502 * @param byte the new value
505 _dbus_string_set_byte (DBusString *str,
509 DBUS_STRING_PREAMBLE (str);
510 _dbus_assert (i < real->len);
511 _dbus_assert (i >= 0);
515 #endif /* _dbus_string_set_byte */
517 /* only have the function if we didn't create a macro */
518 #ifndef _dbus_string_get_byte
520 * Gets the byte at the given position. It is
521 * allowed to ask for the nul byte at the end of
524 * @param str the string
525 * @param start the position
526 * @returns the byte at that position
529 _dbus_string_get_byte (const DBusString *str,
532 DBUS_CONST_STRING_PREAMBLE (str);
533 _dbus_assert (start <= real->len);
534 _dbus_assert (start >= 0);
536 return real->str[start];
538 #endif /* _dbus_string_get_byte */
541 * Inserts a number of bytes of a given value at the
544 * @param str the string
545 * @param i the position
546 * @param n_bytes number of bytes
547 * @param byte the value to insert
548 * @returns #TRUE on success
551 _dbus_string_insert_bytes (DBusString *str,
556 DBUS_STRING_PREAMBLE (str);
557 _dbus_assert (i <= real->len);
558 _dbus_assert (i >= 0);
559 _dbus_assert (n_bytes >= 0);
564 if (!open_gap (n_bytes, real, i))
567 memset (real->str + i, byte, n_bytes);
573 * Inserts a single byte at the given position.
575 * @param str the string
576 * @param i the position
577 * @param byte the value to insert
578 * @returns #TRUE on success
581 _dbus_string_insert_byte (DBusString *str,
585 DBUS_STRING_PREAMBLE (str);
586 _dbus_assert (i <= real->len);
587 _dbus_assert (i >= 0);
589 if (!open_gap (1, real, i))
598 * Like _dbus_string_get_data(), but removes the
599 * gotten data from the original string. The caller
600 * must free the data returned. This function may
601 * fail due to lack of memory, and return #FALSE.
603 * @param str the string
604 * @param data_return location to return the buffer
605 * @returns #TRUE on success
608 _dbus_string_steal_data (DBusString *str,
612 DBUS_STRING_PREAMBLE (str);
613 _dbus_assert (data_return != NULL);
615 undo_alignment (real);
617 *data_return = (char*) real->str;
619 old_max_length = real->max_length;
621 /* reset the string */
622 if (!_dbus_string_init (str))
624 /* hrm, put it back then */
625 real->str = (unsigned char*) *data_return;
627 fixup_alignment (real);
631 real->max_length = old_max_length;
636 #ifdef DBUS_BUILD_TESTS
638 * Like _dbus_string_get_data_len(), but removes the gotten data from
639 * the original string. The caller must free the data returned. This
640 * function may fail due to lack of memory, and return #FALSE.
641 * The returned string is nul-terminated and has length len.
643 * @todo this function is broken because on failure it
644 * may corrupt the source string.
646 * @param str the string
647 * @param data_return location to return the buffer
648 * @param start the start of segment to steal
649 * @param len the length of segment to steal
650 * @returns #TRUE on success
653 _dbus_string_steal_data_len (DBusString *str,
659 DBUS_STRING_PREAMBLE (str);
660 _dbus_assert (data_return != NULL);
661 _dbus_assert (start >= 0);
662 _dbus_assert (len >= 0);
663 _dbus_assert (start <= real->len);
664 _dbus_assert (len <= real->len - start);
666 if (!_dbus_string_init (&dest))
669 set_max_length (&dest, real->max_length);
671 if (!_dbus_string_move_len (str, start, len, &dest, 0))
673 _dbus_string_free (&dest);
677 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
678 if (!_dbus_string_steal_data (&dest, data_return))
680 _dbus_string_free (&dest);
684 _dbus_string_free (&dest);
687 #endif /* DBUS_BUILD_TESTS */
690 * Copies the data from the string into a char*
692 * @param str the string
693 * @param data_return place to return the data
694 * @returns #TRUE on success, #FALSE on no memory
697 _dbus_string_copy_data (const DBusString *str,
700 DBUS_CONST_STRING_PREAMBLE (str);
701 _dbus_assert (data_return != NULL);
703 *data_return = dbus_malloc (real->len + 1);
704 if (*data_return == NULL)
707 memcpy (*data_return, real->str, real->len + 1);
713 * Copies the contents of a DBusString into a different
714 * buffer. The resulting buffer will be nul-terminated.
716 * @param str a string
717 * @param buffer a C buffer to copy data to
718 * @param avail_len maximum length of C buffer
721 _dbus_string_copy_to_buffer (const DBusString *str,
726 DBUS_CONST_STRING_PREAMBLE (str);
728 _dbus_assert (avail_len >= 0);
730 copy_len = MIN (avail_len, real->len+1);
731 memcpy (buffer, real->str, copy_len);
732 if (avail_len > 0 && avail_len == copy_len)
733 buffer[avail_len-1] = '\0';
736 #ifdef DBUS_BUILD_TESTS
738 * Copies a segment of the string into a char*
740 * @param str the string
741 * @param data_return place to return the data
742 * @param start start index
743 * @param len length to copy
744 * @returns #FALSE if no memory
747 _dbus_string_copy_data_len (const DBusString *str,
754 DBUS_CONST_STRING_PREAMBLE (str);
755 _dbus_assert (data_return != NULL);
756 _dbus_assert (start >= 0);
757 _dbus_assert (len >= 0);
758 _dbus_assert (start <= real->len);
759 _dbus_assert (len <= real->len - start);
761 if (!_dbus_string_init (&dest))
764 set_max_length (&dest, real->max_length);
766 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
768 _dbus_string_free (&dest);
772 if (!_dbus_string_steal_data (&dest, data_return))
774 _dbus_string_free (&dest);
778 _dbus_string_free (&dest);
781 #endif /* DBUS_BUILD_TESTS */
783 /* Only have the function if we don't have the macro */
784 #ifndef _dbus_string_get_length
786 * Gets the length of a string (not including nul termination).
788 * @returns the length.
791 _dbus_string_get_length (const DBusString *str)
793 DBUS_CONST_STRING_PREAMBLE (str);
797 #endif /* !_dbus_string_get_length */
800 * Makes a string longer by the given number of bytes. Checks whether
801 * adding additional_length to the current length would overflow an
802 * integer, and checks for exceeding a string's max length.
803 * The new bytes are not initialized, other than nul-terminating
804 * the end of the string. The uninitialized bytes may contain
805 * nul bytes or other junk.
807 * @param str a string
808 * @param additional_length length to add to the string.
809 * @returns #TRUE on success.
812 _dbus_string_lengthen (DBusString *str,
813 int additional_length)
815 DBUS_STRING_PREAMBLE (str);
816 _dbus_assert (additional_length >= 0);
818 if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
819 return FALSE; /* would overflow */
821 return set_length (real,
822 real->len + additional_length);
826 * Makes a string shorter by the given number of bytes.
828 * @param str a string
829 * @param length_to_remove length to remove from the string.
832 _dbus_string_shorten (DBusString *str,
833 int length_to_remove)
835 DBUS_STRING_PREAMBLE (str);
836 _dbus_assert (length_to_remove >= 0);
837 _dbus_assert (length_to_remove <= real->len);
840 real->len - length_to_remove);
844 * Sets the length of a string. Can be used to truncate or lengthen
845 * the string. If the string is lengthened, the function may fail and
846 * return #FALSE. Newly-added bytes are not initialized, as with
847 * _dbus_string_lengthen().
849 * @param str a string
850 * @param length new length of the string.
851 * @returns #FALSE on failure.
854 _dbus_string_set_length (DBusString *str,
857 DBUS_STRING_PREAMBLE (str);
858 _dbus_assert (length >= 0);
860 return set_length (real, length);
864 align_insert_point_then_open_gap (DBusString *str,
869 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
870 unsigned long gap_pos;
873 DBUS_STRING_PREAMBLE (str);
874 _dbus_assert (alignment >= 1);
875 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
877 insert_at = *insert_at_p;
879 _dbus_assert (insert_at <= real->len);
881 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
882 new_len = real->len + (gap_pos - insert_at) + gap_size;
884 if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length))
887 delta = new_len - real->len;
888 _dbus_assert (delta >= 0);
890 if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
892 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
896 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
900 /* nul the padding if we had to add any padding */
901 if (gap_size < delta)
903 memset (&real->str[insert_at], '\0',
904 gap_pos - insert_at);
907 *insert_at_p = gap_pos;
913 align_length_then_lengthen (DBusString *str,
915 int then_lengthen_by)
919 insert_at = _dbus_string_get_length (str);
921 return align_insert_point_then_open_gap (str,
923 alignment, then_lengthen_by);
927 * Align the length of a string to a specific alignment (typically 4 or 8)
928 * by appending nul bytes to the string.
930 * @param str a string
931 * @param alignment the alignment
932 * @returns #FALSE if no memory
935 _dbus_string_align_length (DBusString *str,
938 return align_length_then_lengthen (str, alignment, 0);
942 * Preallocate extra_bytes such that a future lengthening of the
943 * string by extra_bytes is guaranteed to succeed without an out of
946 * @param str a string
947 * @param extra_bytes bytes to alloc
948 * @returns #FALSE if no memory
951 _dbus_string_alloc_space (DBusString *str,
954 if (!_dbus_string_lengthen (str, extra_bytes))
956 _dbus_string_shorten (str, extra_bytes);
962 append (DBusRealString *real,
969 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
972 memcpy (real->str + (real->len - buffer_len),
980 * Appends a nul-terminated C-style string to a DBusString.
982 * @param str the DBusString
983 * @param buffer the nul-terminated characters to append
984 * @returns #FALSE if not enough memory.
987 _dbus_string_append (DBusString *str,
990 unsigned long buffer_len;
992 DBUS_STRING_PREAMBLE (str);
993 _dbus_assert (buffer != NULL);
995 buffer_len = strlen (buffer);
996 if (buffer_len > (unsigned long) real->max_length)
999 return append (real, buffer, buffer_len);
1002 /** assign 2 bytes from one string to another */
1003 #define ASSIGN_2_OCTETS(p, octets) \
1004 *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
1006 /** assign 4 bytes from one string to another */
1007 #define ASSIGN_4_OCTETS(p, octets) \
1008 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
1010 #ifdef DBUS_HAVE_INT64
1011 /** assign 8 bytes from one string to another */
1012 #define ASSIGN_8_OCTETS(p, octets) \
1013 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
1015 /** assign 8 bytes from one string to another */
1016 #define ASSIGN_8_OCTETS(p, octets) \
1030 _dbus_assert (b == p + 8); \
1032 #endif /* DBUS_HAVE_INT64 */
1034 #ifdef DBUS_BUILD_TESTS
1036 * Appends 4 bytes aligned on a 4 byte boundary
1037 * with any alignment padding initialized to 0.
1039 * @param str the DBusString
1040 * @param octets 4 bytes to append
1041 * @returns #FALSE if not enough memory.
1044 _dbus_string_append_4_aligned (DBusString *str,
1045 const unsigned char octets[4])
1047 DBUS_STRING_PREAMBLE (str);
1049 if (!align_length_then_lengthen (str, 4, 4))
1052 ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
1056 #endif /* DBUS_BUILD_TESTS */
1058 #ifdef DBUS_BUILD_TESTS
1060 * Appends 8 bytes aligned on an 8 byte boundary
1061 * with any alignment padding initialized to 0.
1063 * @param str the DBusString
1064 * @param octets 8 bytes to append
1065 * @returns #FALSE if not enough memory.
1068 _dbus_string_append_8_aligned (DBusString *str,
1069 const unsigned char octets[8])
1071 DBUS_STRING_PREAMBLE (str);
1073 if (!align_length_then_lengthen (str, 8, 8))
1076 ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
1080 #endif /* DBUS_BUILD_TESTS */
1083 * Inserts 2 bytes aligned on a 2 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 2 bytes to insert
1089 * @returns #FALSE if not enough memory.
1092 _dbus_string_insert_2_aligned (DBusString *str,
1094 const unsigned char octets[4])
1096 DBUS_STRING_PREAMBLE (str);
1098 if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
1101 ASSIGN_2_OCTETS (real->str + insert_at, octets);
1107 * Inserts 4 bytes aligned on a 4 byte boundary
1108 * with any alignment padding initialized to 0.
1110 * @param str the DBusString
1111 * @param insert_at where to insert
1112 * @param octets 4 bytes to insert
1113 * @returns #FALSE if not enough memory.
1116 _dbus_string_insert_4_aligned (DBusString *str,
1118 const unsigned char octets[4])
1120 DBUS_STRING_PREAMBLE (str);
1122 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1125 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1131 * Inserts 8 bytes aligned on an 8 byte boundary
1132 * with any alignment padding initialized to 0.
1134 * @param str the DBusString
1135 * @param insert_at where to insert
1136 * @param octets 8 bytes to insert
1137 * @returns #FALSE if not enough memory.
1140 _dbus_string_insert_8_aligned (DBusString *str,
1142 const unsigned char octets[8])
1144 DBUS_STRING_PREAMBLE (str);
1146 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1149 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1151 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1158 * Inserts padding at *insert_at such to align it to the given
1159 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1160 * to the aligned position.
1162 * @param str the DBusString
1163 * @param insert_at location to be aligned
1164 * @param alignment alignment boundary (1, 2, 4, or 8)
1165 * @returns #FALSE if not enough memory.
1168 _dbus_string_insert_alignment (DBusString *str,
1172 DBUS_STRING_PREAMBLE (str);
1174 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1177 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1183 * Appends a printf-style formatted string
1184 * to the #DBusString.
1186 * @param str the string
1187 * @param format printf format
1188 * @param args variable argument list
1189 * @returns #FALSE if no memory
1192 _dbus_string_append_printf_valist (DBusString *str,
1199 DBUS_STRING_PREAMBLE (str);
1201 DBUS_VA_COPY (args_copy, args);
1203 /* Measure the message length without terminating nul */
1204 len = _dbus_printf_string_upper_bound (format, args);
1206 if (!_dbus_string_lengthen (str, len))
1208 /* don't leak the copy */
1213 vsprintf ((char*) (real->str + (real->len - len)),
1222 * Appends a printf-style formatted string
1223 * to the #DBusString.
1225 * @param str the string
1226 * @param format printf format
1227 * @returns #FALSE if no memory
1230 _dbus_string_append_printf (DBusString *str,
1237 va_start (args, format);
1238 retval = _dbus_string_append_printf_valist (str, format, args);
1245 * Appends block of bytes with the given length to a DBusString.
1247 * @param str the DBusString
1248 * @param buffer the bytes to append
1249 * @param len the number of bytes to append
1250 * @returns #FALSE if not enough memory.
1253 _dbus_string_append_len (DBusString *str,
1257 DBUS_STRING_PREAMBLE (str);
1258 _dbus_assert (buffer != NULL);
1259 _dbus_assert (len >= 0);
1261 return append (real, buffer, len);
1265 * Appends a single byte to the string, returning #FALSE
1266 * if not enough memory.
1268 * @param str the string
1269 * @param byte the byte to append
1270 * @returns #TRUE on success
1273 _dbus_string_append_byte (DBusString *str,
1276 DBUS_STRING_PREAMBLE (str);
1278 if (!set_length (real, real->len + 1))
1281 real->str[real->len-1] = byte;
1286 #ifdef DBUS_BUILD_TESTS
1288 * Appends a single Unicode character, encoding the character
1291 * @param str the string
1292 * @param ch the Unicode character
1295 _dbus_string_append_unichar (DBusString *str,
1303 DBUS_STRING_PREAMBLE (str);
1305 /* this code is from GLib but is pretty standard I think */
1314 else if (ch < 0x800)
1319 else if (ch < 0x10000)
1324 else if (ch < 0x200000)
1329 else if (ch < 0x4000000)
1340 if (len > (real->max_length - real->len))
1341 return FALSE; /* real->len + len would overflow */
1343 if (!set_length (real, real->len + len))
1346 out = real->str + (real->len - len);
1348 for (i = len - 1; i > 0; --i)
1350 out[i] = (ch & 0x3f) | 0x80;
1353 out[0] = ch | first;
1357 #endif /* DBUS_BUILD_TESTS */
1360 delete (DBusRealString *real,
1367 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1369 real->str[real->len] = '\0';
1373 * Deletes a segment of a DBusString with length len starting at
1374 * start. (Hint: to clear an entire string, setting length to 0
1375 * with _dbus_string_set_length() is easier.)
1377 * @param str the DBusString
1378 * @param start where to start deleting
1379 * @param len the number of bytes to delete
1382 _dbus_string_delete (DBusString *str,
1386 DBUS_STRING_PREAMBLE (str);
1387 _dbus_assert (start >= 0);
1388 _dbus_assert (len >= 0);
1389 _dbus_assert (start <= real->len);
1390 _dbus_assert (len <= real->len - start);
1392 delete (real, start, len);
1396 copy (DBusRealString *source,
1399 DBusRealString *dest,
1405 if (!open_gap (len, dest, insert_at))
1408 memmove (dest->str + insert_at,
1409 source->str + start,
1416 * Checks assertions for two strings we're copying a segment between,
1417 * and declares real_source/real_dest variables.
1419 * @param source the source string
1420 * @param start the starting offset
1421 * @param dest the dest string
1422 * @param insert_at where the copied segment is inserted
1424 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1425 DBusRealString *real_source = (DBusRealString*) source; \
1426 DBusRealString *real_dest = (DBusRealString*) dest; \
1427 _dbus_assert ((source) != (dest)); \
1428 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1429 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1430 _dbus_assert (!real_dest->constant); \
1431 _dbus_assert (!real_dest->locked); \
1432 _dbus_assert ((start) >= 0); \
1433 _dbus_assert ((start) <= real_source->len); \
1434 _dbus_assert ((insert_at) >= 0); \
1435 _dbus_assert ((insert_at) <= real_dest->len)
1438 * Moves the end of one string into another string. Both strings
1439 * must be initialized, valid strings.
1441 * @param source the source string
1442 * @param start where to chop off the source string
1443 * @param dest the destination string
1444 * @param insert_at where to move the chopped-off part of source string
1445 * @returns #FALSE if not enough memory
1448 _dbus_string_move (DBusString *source,
1453 DBusRealString *real_source = (DBusRealString*) source;
1454 _dbus_assert (start <= real_source->len);
1456 return _dbus_string_move_len (source, start,
1457 real_source->len - start,
1462 * Like _dbus_string_move(), but does not delete the section
1463 * of the source string that's copied to the dest string.
1465 * @param source the source string
1466 * @param start where to start copying the source string
1467 * @param dest the destination string
1468 * @param insert_at where to place the copied part of source string
1469 * @returns #FALSE if not enough memory
1472 _dbus_string_copy (const DBusString *source,
1477 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1479 return copy (real_source, start,
1480 real_source->len - start,
1486 * Like _dbus_string_move(), but can move a segment from
1487 * the middle of the source string.
1489 * @todo this doesn't do anything with max_length field.
1490 * we should probably just kill the max_length field though.
1492 * @param source the source string
1493 * @param start first byte of source string to move
1494 * @param len length of segment to move
1495 * @param dest the destination string
1496 * @param insert_at where to move the bytes from the source string
1497 * @returns #FALSE if not enough memory
1500 _dbus_string_move_len (DBusString *source,
1507 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1508 _dbus_assert (len >= 0);
1509 _dbus_assert ((start + len) <= real_source->len);
1516 else if (start == 0 &&
1517 len == real_source->len &&
1518 real_dest->len == 0)
1520 /* Short-circuit moving an entire existing string to an empty string
1521 * by just swapping the buffers.
1523 /* we assume ->constant doesn't matter as you can't have
1524 * a constant string involved in a move.
1526 #define ASSIGN_DATA(a, b) do { \
1527 (a)->str = (b)->str; \
1528 (a)->len = (b)->len; \
1529 (a)->allocated = (b)->allocated; \
1530 (a)->align_offset = (b)->align_offset; \
1535 ASSIGN_DATA (&tmp, real_source);
1536 ASSIGN_DATA (real_source, real_dest);
1537 ASSIGN_DATA (real_dest, &tmp);
1543 if (!copy (real_source, start, len,
1548 delete (real_source, start,
1556 * Like _dbus_string_copy(), but can copy a segment from the middle of
1557 * the source string.
1559 * @param source the source string
1560 * @param start where to start copying the source string
1561 * @param len length of segment to copy
1562 * @param dest the destination string
1563 * @param insert_at where to place the copied segment of source string
1564 * @returns #FALSE if not enough memory
1567 _dbus_string_copy_len (const DBusString *source,
1573 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1574 _dbus_assert (len >= 0);
1575 _dbus_assert (start <= real_source->len);
1576 _dbus_assert (len <= real_source->len - start);
1578 return copy (real_source, start, len,
1584 * Replaces a segment of dest string with a segment of source string.
1586 * @todo optimize the case where the two lengths are the same, and
1587 * avoid memmoving the data in the trailing part of the string twice.
1589 * @todo avoid inserting the source into dest, then deleting
1590 * the replaced chunk of dest (which creates a potentially large
1591 * intermediate string). Instead, extend the replaced chunk
1592 * of dest with padding to the same size as the source chunk,
1593 * then copy in the source bytes.
1595 * @param source the source string
1596 * @param start where to start copying the source string
1597 * @param len length of segment to copy
1598 * @param dest the destination string
1599 * @param replace_at start of segment of dest string to replace
1600 * @param replace_len length of segment of dest string to replace
1601 * @returns #FALSE if not enough memory
1605 _dbus_string_replace_len (const DBusString *source,
1612 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1613 _dbus_assert (len >= 0);
1614 _dbus_assert (start <= real_source->len);
1615 _dbus_assert (len <= real_source->len - start);
1616 _dbus_assert (replace_at >= 0);
1617 _dbus_assert (replace_at <= real_dest->len);
1618 _dbus_assert (replace_len <= real_dest->len - replace_at);
1620 if (!copy (real_source, start, len,
1621 real_dest, replace_at))
1624 delete (real_dest, replace_at + len, replace_len);
1629 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1630 * Pennington, and Tom Tromey are the authors and authorized relicense.
1633 /** computes length and mask of a unicode character
1634 * @param Char the char
1635 * @param Mask the mask variable to assign to
1636 * @param Len the length variable to assign to
1638 #define UTF8_COMPUTE(Char, Mask, Len) \
1644 else if ((Char & 0xe0) == 0xc0) \
1649 else if ((Char & 0xf0) == 0xe0) \
1654 else if ((Char & 0xf8) == 0xf0) \
1659 else if ((Char & 0xfc) == 0xf8) \
1664 else if ((Char & 0xfe) == 0xfc) \
1676 * computes length of a unicode character in UTF-8
1677 * @param Char the char
1679 #define UTF8_LENGTH(Char) \
1680 ((Char) < 0x80 ? 1 : \
1681 ((Char) < 0x800 ? 2 : \
1682 ((Char) < 0x10000 ? 3 : \
1683 ((Char) < 0x200000 ? 4 : \
1684 ((Char) < 0x4000000 ? 5 : 6)))))
1687 * Gets a UTF-8 value.
1689 * @param Result variable for extracted unicode char.
1690 * @param Chars the bytes to decode
1691 * @param Count counter variable
1692 * @param Mask mask for this char
1693 * @param Len length for this char in bytes
1695 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1696 (Result) = (Chars)[0] & (Mask); \
1697 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1699 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1705 (Result) |= ((Chars)[(Count)] & 0x3f); \
1709 * Check whether a unicode char is in a valid range.
1711 * @param Char the character
1713 #define UNICODE_VALID(Char) \
1714 ((Char) < 0x110000 && \
1715 (((Char) & 0xFFFFF800) != 0xD800) && \
1716 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1717 ((Char) & 0xFFFF) != 0xFFFF)
1719 #ifdef DBUS_BUILD_TESTS
1721 * Gets a unicode character from a UTF-8 string. Does no validation;
1722 * you must verify that the string is valid UTF-8 in advance and must
1723 * pass in the start of a character.
1725 * @param str the string
1726 * @param start the start of the UTF-8 character.
1727 * @param ch_return location to return the character
1728 * @param end_return location to return the byte index of next character
1731 _dbus_string_get_unichar (const DBusString *str,
1733 dbus_unichar_t *ch_return,
1737 dbus_unichar_t result;
1740 DBUS_CONST_STRING_PREAMBLE (str);
1741 _dbus_assert (start >= 0);
1742 _dbus_assert (start <= real->len);
1747 *end_return = real->len;
1750 p = real->str + start;
1753 UTF8_COMPUTE (c, mask, len);
1756 UTF8_GET (result, p, i, mask, len);
1758 if (result == (dbus_unichar_t)-1)
1762 *ch_return = result;
1764 *end_return = start + len;
1766 #endif /* DBUS_BUILD_TESTS */
1769 * Finds the given substring in the string,
1770 * returning #TRUE and filling in the byte index
1771 * where the substring was found, if it was found.
1772 * Returns #FALSE if the substring wasn't found.
1773 * Sets *start to the length of the string if the substring
1776 * @param str the string
1777 * @param start where to start looking
1778 * @param substr the substring
1779 * @param found return location for where it was found, or #NULL
1780 * @returns #TRUE if found
1783 _dbus_string_find (const DBusString *str,
1788 return _dbus_string_find_to (str, start,
1789 ((const DBusRealString*)str)->len,
1794 * Finds end of line ("\r\n" or "\n") in the string,
1795 * returning #TRUE and filling in the byte index
1796 * where the eol string was found, if it was found.
1797 * Returns #FALSE if eol wasn't found.
1799 * @param str the string
1800 * @param start where to start looking
1801 * @param found return location for where eol was found or string length otherwise
1802 * @param found_len return length of found eol string or zero otherwise
1803 * @returns #TRUE if found
1806 _dbus_string_find_eol (const DBusString *str,
1813 DBUS_CONST_STRING_PREAMBLE (str);
1814 _dbus_assert (start <= real->len);
1815 _dbus_assert (start >= 0);
1818 while (i < real->len)
1820 if (real->str[i] == '\r')
1822 if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
1830 else /* only "\r" */
1839 else if (real->str[i] == '\n') /* only "\n" */
1860 * Finds the given substring in the string,
1861 * up to a certain position,
1862 * returning #TRUE and filling in the byte index
1863 * where the substring was found, if it was found.
1864 * Returns #FALSE if the substring wasn't found.
1865 * Sets *start to the length of the string if the substring
1868 * @param str the string
1869 * @param start where to start looking
1870 * @param end where to stop looking
1871 * @param substr the substring
1872 * @param found return location for where it was found, or #NULL
1873 * @returns #TRUE if found
1876 _dbus_string_find_to (const DBusString *str,
1883 DBUS_CONST_STRING_PREAMBLE (str);
1884 _dbus_assert (substr != NULL);
1885 _dbus_assert (start <= real->len);
1886 _dbus_assert (start >= 0);
1887 _dbus_assert (substr != NULL);
1888 _dbus_assert (end <= real->len);
1889 _dbus_assert (start <= end);
1891 /* we always "find" an empty string */
1892 if (*substr == '\0')
1902 if (real->str[i] == substr[0])
1908 if (substr[j - i] == '\0')
1910 else if (real->str[j] != substr[j - i])
1916 if (substr[j - i] == '\0')
1934 * Finds a blank (space or tab) in the string. Returns #TRUE
1935 * if found, #FALSE otherwise. If a blank is not found sets
1936 * *found to the length of the string.
1938 * @param str the string
1939 * @param start byte index to start looking
1940 * @param found place to store the location of the first blank
1941 * @returns #TRUE if a blank was found
1944 _dbus_string_find_blank (const DBusString *str,
1949 DBUS_CONST_STRING_PREAMBLE (str);
1950 _dbus_assert (start <= real->len);
1951 _dbus_assert (start >= 0);
1954 while (i < real->len)
1956 if (real->str[i] == ' ' ||
1957 real->str[i] == '\t')
1974 * Skips blanks from start, storing the first non-blank in *end
1975 * (blank is space or tab).
1977 * @param str the string
1978 * @param start where to start
1979 * @param end where to store the first non-blank byte index
1982 _dbus_string_skip_blank (const DBusString *str,
1987 DBUS_CONST_STRING_PREAMBLE (str);
1988 _dbus_assert (start <= real->len);
1989 _dbus_assert (start >= 0);
1992 while (i < real->len)
1994 if (!DBUS_IS_ASCII_BLANK (real->str[i]))
2000 _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
2008 * Skips whitespace from start, storing the first non-whitespace in *end.
2009 * (whitespace is space, tab, newline, CR).
2011 * @param str the string
2012 * @param start where to start
2013 * @param end where to store the first non-whitespace byte index
2016 _dbus_string_skip_white (const DBusString *str,
2021 DBUS_CONST_STRING_PREAMBLE (str);
2022 _dbus_assert (start <= real->len);
2023 _dbus_assert (start >= 0);
2026 while (i < real->len)
2028 if (!DBUS_IS_ASCII_WHITE (real->str[i]))
2034 _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
2041 * Skips whitespace from end, storing the start index of the trailing
2042 * whitespace in *start. (whitespace is space, tab, newline, CR).
2044 * @param str the string
2045 * @param end where to start scanning backward
2046 * @param start where to store the start of whitespace chars
2049 _dbus_string_skip_white_reverse (const DBusString *str,
2054 DBUS_CONST_STRING_PREAMBLE (str);
2055 _dbus_assert (end <= real->len);
2056 _dbus_assert (end >= 0);
2061 if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
2066 _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
2073 * Assigns a newline-terminated or \\r\\n-terminated line from the front
2074 * of the string to the given dest string. The dest string's previous
2075 * contents are deleted. If the source string contains no newline,
2076 * moves the entire source string to the dest string.
2078 * @todo owen correctly notes that this is a stupid function (it was
2079 * written purely for test code,
2080 * e.g. dbus-message-builder.c). Probably should be enforced as test
2081 * code only with ifdef DBUS_BUILD_TESTS
2083 * @param source the source string
2084 * @param dest the destination string (contents are replaced)
2085 * @returns #FALSE if no memory, or source has length 0
2088 _dbus_string_pop_line (DBusString *source,
2093 _dbus_string_set_length (dest, 0);
2097 if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
2099 _dbus_assert (eol == _dbus_string_get_length (source));
2102 /* If there's no newline and source has zero length, we're done */
2105 /* otherwise, the last line of the file has no eol characters */
2108 /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
2109 * since find_eol returned TRUE
2112 if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
2115 /* remove line ending */
2116 if (!_dbus_string_set_length (dest, eol))
2118 _dbus_assert_not_reached ("out of memory when shortening a string");
2125 #ifdef DBUS_BUILD_TESTS
2127 * Deletes up to and including the first blank space
2130 * @param str the string
2133 _dbus_string_delete_first_word (DBusString *str)
2137 if (_dbus_string_find_blank (str, 0, &i))
2138 _dbus_string_skip_blank (str, i, &i);
2140 _dbus_string_delete (str, 0, i);
2144 #ifdef DBUS_BUILD_TESTS
2146 * Deletes any leading blanks in the string
2148 * @param str the string
2151 _dbus_string_delete_leading_blanks (DBusString *str)
2155 _dbus_string_skip_blank (str, 0, &i);
2158 _dbus_string_delete (str, 0, i);
2163 * Deletes leading and trailing whitespace
2165 * @param str the string
2168 _dbus_string_chop_white(DBusString *str)
2172 _dbus_string_skip_white (str, 0, &i);
2175 _dbus_string_delete (str, 0, i);
2177 _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
2179 _dbus_string_set_length (str, i);
2183 * Tests two DBusString for equality.
2185 * @todo memcmp is probably faster
2187 * @param a first string
2188 * @param b second string
2189 * @returns #TRUE if equal
2192 _dbus_string_equal (const DBusString *a,
2193 const DBusString *b)
2195 const unsigned char *ap;
2196 const unsigned char *bp;
2197 const unsigned char *a_end;
2198 const DBusRealString *real_a = (const DBusRealString*) a;
2199 const DBusRealString *real_b = (const DBusRealString*) b;
2200 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2201 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2203 if (real_a->len != real_b->len)
2208 a_end = real_a->str + real_a->len;
2221 #ifdef DBUS_BUILD_TESTS
2223 * Tests two DBusString for equality up to the given length.
2224 * The strings may be shorter than the given length.
2226 * @todo write a unit test
2228 * @todo memcmp is probably faster
2230 * @param a first string
2231 * @param b second string
2232 * @param len the maximum length to look at
2233 * @returns #TRUE if equal for the given number of bytes
2236 _dbus_string_equal_len (const DBusString *a,
2237 const DBusString *b,
2240 const unsigned char *ap;
2241 const unsigned char *bp;
2242 const unsigned char *a_end;
2243 const DBusRealString *real_a = (const DBusRealString*) a;
2244 const DBusRealString *real_b = (const DBusRealString*) b;
2245 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2246 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2248 if (real_a->len != real_b->len &&
2249 (real_a->len < len || real_b->len < len))
2254 a_end = real_a->str + MIN (real_a->len, len);
2266 #endif /* DBUS_BUILD_TESTS */
2269 * Tests two sub-parts of two DBusString for equality. The specified
2270 * range of the first string must exist; the specified start position
2271 * of the second string must exist.
2273 * @todo write a unit test
2275 * @todo memcmp is probably faster
2277 * @param a first string
2278 * @param a_start where to start substring in first string
2279 * @param a_len length of substring in first string
2280 * @param b second string
2281 * @param b_start where to start substring in second string
2282 * @returns #TRUE if the two substrings are equal
2285 _dbus_string_equal_substring (const DBusString *a,
2288 const DBusString *b,
2291 const unsigned char *ap;
2292 const unsigned char *bp;
2293 const unsigned char *a_end;
2294 const DBusRealString *real_a = (const DBusRealString*) a;
2295 const DBusRealString *real_b = (const DBusRealString*) b;
2296 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2297 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2298 _dbus_assert (a_start >= 0);
2299 _dbus_assert (a_len >= 0);
2300 _dbus_assert (a_start <= real_a->len);
2301 _dbus_assert (a_len <= real_a->len - a_start);
2302 _dbus_assert (b_start >= 0);
2303 _dbus_assert (b_start <= real_b->len);
2305 if (a_len > real_b->len - b_start)
2308 ap = real_a->str + a_start;
2309 bp = real_b->str + b_start;
2320 _dbus_assert (bp <= (real_b->str + real_b->len));
2326 * Checks whether a string is equal to a C string.
2328 * @param a the string
2329 * @param c_str the C string
2330 * @returns #TRUE if equal
2333 _dbus_string_equal_c_str (const DBusString *a,
2336 const unsigned char *ap;
2337 const unsigned char *bp;
2338 const unsigned char *a_end;
2339 const DBusRealString *real_a = (const DBusRealString*) a;
2340 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2341 _dbus_assert (c_str != NULL);
2344 bp = (const unsigned char*) c_str;
2345 a_end = real_a->str + real_a->len;
2346 while (ap != a_end && *bp)
2355 if (ap != a_end || *bp)
2361 #ifdef DBUS_BUILD_TESTS
2363 * Checks whether a string starts with the given C string.
2365 * @param a the string
2366 * @param c_str the C string
2367 * @returns #TRUE if string starts with it
2370 _dbus_string_starts_with_c_str (const DBusString *a,
2373 const unsigned char *ap;
2374 const unsigned char *bp;
2375 const unsigned char *a_end;
2376 const DBusRealString *real_a = (const DBusRealString*) a;
2377 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2378 _dbus_assert (c_str != NULL);
2381 bp = (const unsigned char*) c_str;
2382 a_end = real_a->str + real_a->len;
2383 while (ap != a_end && *bp)
2397 #endif /* DBUS_BUILD_TESTS */
2400 * Appends a two-character hex digit to a string, where the hex digit
2401 * has the value of the given byte.
2403 * @param str the string
2404 * @param byte the byte
2405 * @returns #FALSE if no memory
2408 _dbus_string_append_byte_as_hex (DBusString *str,
2411 const char hexdigits[16] = {
2412 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2413 'a', 'b', 'c', 'd', 'e', 'f'
2416 if (!_dbus_string_append_byte (str,
2417 hexdigits[(byte >> 4)]))
2420 if (!_dbus_string_append_byte (str,
2421 hexdigits[(byte & 0x0f)]))
2423 _dbus_string_set_length (str,
2424 _dbus_string_get_length (str) - 1);
2432 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2433 * encoded. (Each byte is two hex digits.)
2435 * @param source the string to encode
2436 * @param start byte index to start encoding
2437 * @param dest string where encoded data should be placed
2438 * @param insert_at where to place encoded data
2439 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2442 _dbus_string_hex_encode (const DBusString *source,
2448 const unsigned char *p;
2449 const unsigned char *end;
2452 _dbus_assert (start <= _dbus_string_get_length (source));
2454 if (!_dbus_string_init (&result))
2459 p = (const unsigned char*) _dbus_string_get_const_data (source);
2460 end = p + _dbus_string_get_length (source);
2465 if (!_dbus_string_append_byte_as_hex (&result, *p))
2471 if (!_dbus_string_move (&result, 0, dest, insert_at))
2477 _dbus_string_free (&result);
2482 * Decodes a string from hex encoding.
2484 * @param source the string to decode
2485 * @param start byte index to start decode
2486 * @param end_return return location of the end of the hex data, or #NULL
2487 * @param dest string where decoded data should be placed
2488 * @param insert_at where to place decoded data
2489 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2492 _dbus_string_hex_decode (const DBusString *source,
2499 const unsigned char *p;
2500 const unsigned char *end;
2502 dbus_bool_t high_bits;
2504 _dbus_assert (start <= _dbus_string_get_length (source));
2506 if (!_dbus_string_init (&result))
2512 p = (const unsigned char*) _dbus_string_get_const_data (source);
2513 end = p + _dbus_string_get_length (source);
2582 if (!_dbus_string_append_byte (&result,
2591 len = _dbus_string_get_length (&result);
2593 b = _dbus_string_get_byte (&result, len - 1);
2597 _dbus_string_set_byte (&result, len - 1, b);
2600 high_bits = !high_bits;
2606 if (!_dbus_string_move (&result, 0, dest, insert_at))
2610 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2615 _dbus_string_free (&result);
2620 * Checks that the given range of the string is valid ASCII with no
2621 * nul bytes. If the given range is not entirely contained in the
2622 * string, returns #FALSE.
2624 * @todo this is inconsistent with most of DBusString in that
2625 * it allows a start,len range that extends past the string end.
2627 * @param str the string
2628 * @param start first byte index to check
2629 * @param len number of bytes to check
2630 * @returns #TRUE if the byte range exists and is all valid ASCII
2633 _dbus_string_validate_ascii (const DBusString *str,
2637 const unsigned char *s;
2638 const unsigned char *end;
2639 DBUS_CONST_STRING_PREAMBLE (str);
2640 _dbus_assert (start >= 0);
2641 _dbus_assert (start <= real->len);
2642 _dbus_assert (len >= 0);
2644 if (len > real->len - start)
2647 s = real->str + start;
2651 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2661 * Checks that the given range of the string is valid UTF-8. If the
2662 * given range is not entirely contained in the string, returns
2663 * #FALSE. If the string contains any nul bytes in the given range,
2664 * returns #FALSE. If the start and start+len are not on character
2665 * boundaries, returns #FALSE.
2667 * @todo this is inconsistent with most of DBusString in that
2668 * it allows a start,len range that extends past the string end.
2670 * @param str the string
2671 * @param start first byte index to check
2672 * @param len number of bytes to check
2673 * @returns #TRUE if the byte range exists and is all valid UTF-8
2676 _dbus_string_validate_utf8 (const DBusString *str,
2680 const unsigned char *p;
2681 const unsigned char *end;
2682 DBUS_CONST_STRING_PREAMBLE (str);
2683 _dbus_assert (start >= 0);
2684 _dbus_assert (start <= real->len);
2685 _dbus_assert (len >= 0);
2687 /* we are doing _DBUS_UNLIKELY() here which might be
2688 * dubious in a generic library like GLib, but in D-Bus
2689 * we know we're validating messages and that it would
2690 * only be evil/broken apps that would have invalid
2691 * UTF-8. Also, this function seems to be a performance
2692 * bottleneck in profiles.
2695 if (_DBUS_UNLIKELY (len > real->len - start))
2698 p = real->str + start;
2703 int i, mask, char_len;
2704 dbus_unichar_t result;
2706 /* nul bytes considered invalid */
2710 /* Special-case ASCII; this makes us go a lot faster in
2711 * D-Bus profiles where we are typically validating
2712 * function names and such. We have to know that
2713 * all following checks will pass for ASCII though,
2714 * comments follow ...
2722 UTF8_COMPUTE (*p, mask, char_len);
2724 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2727 /* check that the expected number of bytes exists in the remaining length */
2728 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2731 UTF8_GET (result, p, i, mask, char_len);
2733 /* Check for overlong UTF-8 */
2734 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2737 /* The UNICODE_VALID check below will catch this */
2738 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2742 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2745 /* UNICODE_VALID should have caught it */
2746 _dbus_assert (result != (dbus_unichar_t)-1);
2751 /* See that we covered the entire length if a length was
2754 if (_DBUS_UNLIKELY (p != end))
2761 * Checks that the given range of the string is all nul bytes. If the
2762 * given range is not entirely contained in the string, returns
2765 * @todo this is inconsistent with most of DBusString in that
2766 * it allows a start,len range that extends past the string end.
2768 * @param str the string
2769 * @param start first byte index to check
2770 * @param len number of bytes to check
2771 * @returns #TRUE if the byte range exists and is all nul bytes
2774 _dbus_string_validate_nul (const DBusString *str,
2778 const unsigned char *s;
2779 const unsigned char *end;
2780 DBUS_CONST_STRING_PREAMBLE (str);
2781 _dbus_assert (start >= 0);
2782 _dbus_assert (len >= 0);
2783 _dbus_assert (start <= real->len);
2785 if (len > real->len - start)
2788 s = real->str + start;
2792 if (_DBUS_UNLIKELY (*s != '\0'))
2801 * Clears all allocated bytes in the string to zero.
2803 * @param str the string
2806 _dbus_string_zero (DBusString *str)
2808 DBUS_STRING_PREAMBLE (str);
2810 memset (real->str - real->align_offset, '\0', real->allocated);
2814 /* tests are in dbus-string-util.c */