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->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);
181 * Initializes a constant string. The value parameter is not copied
182 * (should be static), and the string may never be modified.
183 * It is safe but not necessary to call _dbus_string_free()
184 * on a const string. The string has a length limit of MAXINT - 8.
186 * @param str memory to use for the string
187 * @param value a string to be stored in str (not copied!!!)
190 _dbus_string_init_const (DBusString *str,
193 _dbus_assert (value != NULL);
195 _dbus_string_init_const_len (str, value,
200 * Initializes a constant string with a length. The value parameter is
201 * not copied (should be static), and the string may never be
202 * modified. It is safe but not necessary to call _dbus_string_free()
205 * @param str memory to use for the string
206 * @param value a string to be stored in str (not copied!!!)
207 * @param len the length to use
210 _dbus_string_init_const_len (DBusString *str,
214 DBusRealString *real;
216 _dbus_assert (str != NULL);
217 _dbus_assert (len == 0 || value != NULL);
218 _dbus_assert (len <= _DBUS_STRING_MAX_LENGTH);
219 _dbus_assert (len >= 0);
221 real = (DBusRealString*) str;
223 real->str = (unsigned char*) value;
225 real->allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
226 real->constant = TRUE;
228 real->invalid = FALSE;
229 real->align_offset = 0;
231 /* We don't require const strings to be 8-byte aligned as the
232 * memory is coming from elsewhere.
237 * Frees a string created by _dbus_string_init().
239 * @param str memory where the string is stored.
242 _dbus_string_free (DBusString *str)
244 DBusRealString *real = (DBusRealString*) str;
245 DBUS_GENERIC_STRING_PREAMBLE (real);
250 /* so it's safe if @p str returned by a failed
251 * _dbus_string_init call
252 * Bug: https://bugs.freedesktop.org/show_bug.cgi?id=65959
254 if (real->str == NULL)
257 dbus_free (real->str - real->align_offset);
259 real->invalid = TRUE;
263 compact (DBusRealString *real,
266 unsigned char *new_str;
270 waste = real->allocated - (real->len + _DBUS_STRING_ALLOCATION_PADDING);
272 if (waste <= max_waste)
275 new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
277 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
278 if (_DBUS_UNLIKELY (new_str == NULL))
281 real->str = new_str + real->align_offset;
282 real->allocated = new_allocated;
283 fixup_alignment (real);
288 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
289 /* Not using this feature at the moment,
290 * so marked DBUS_ENABLE_EMBEDDED_TESTS-only
293 * Locks a string such that any attempts to change the string will
294 * result in aborting the program. Also, if the string is wasting a
295 * lot of memory (allocation is sufficiently larger than what the
296 * string is really using), _dbus_string_lock() will realloc the
297 * string's data to "compact" it.
299 * @param str the string to lock.
302 _dbus_string_lock (DBusString *str)
304 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
308 /* Try to realloc to avoid excess memory usage, since
309 * we know we won't change the string further
312 compact (real, MAX_WASTE);
314 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
317 reallocate_for_length (DBusRealString *real,
321 unsigned char *new_str;
323 /* at least double our old allocation to avoid O(n), avoiding
326 if (real->allocated > (_DBUS_STRING_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
327 new_allocated = _DBUS_STRING_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
329 new_allocated = real->allocated * 2;
331 /* if you change the code just above here, run the tests without
332 * the following assert-only hack before you commit
334 /* This is keyed off asserts in addition to tests so when you
335 * disable asserts to profile, you don't get this destroyer
338 #ifdef DBUS_DISABLE_ASSERT
340 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
341 new_allocated = 0; /* ensure a realloc every time so that we go
342 * through all malloc failure codepaths
344 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
345 #endif /* !DBUS_DISABLE_ASSERT */
347 /* But be sure we always alloc at least space for the new length */
348 new_allocated = MAX (new_allocated,
349 new_length + _DBUS_STRING_ALLOCATION_PADDING);
351 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
352 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
353 if (_DBUS_UNLIKELY (new_str == NULL))
356 real->str = new_str + real->align_offset;
357 real->allocated = new_allocated;
358 fixup_alignment (real);
364 * Compacts the string to avoid wasted memory. Wasted memory is
365 * memory that is allocated but not actually required to store the
366 * current length of the string. The compact is only done if more
367 * than the given amount of memory is being wasted (otherwise the
368 * waste is ignored and the call does nothing).
370 * @param str the string
371 * @param max_waste the maximum amount of waste to ignore
372 * @returns #FALSE if the compact failed due to realloc failure
375 _dbus_string_compact (DBusString *str,
378 DBUS_STRING_PREAMBLE (str);
380 return compact (real, max_waste);
384 set_length (DBusRealString *real,
387 /* Note, we are setting the length not including nul termination */
389 /* exceeding max length is the same as failure to allocate memory */
390 if (_DBUS_UNLIKELY (new_length > _DBUS_STRING_MAX_LENGTH))
392 else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
393 _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
397 real->len = new_length;
398 real->str[new_length] = '\0';
405 DBusRealString *dest,
411 if (len > _DBUS_STRING_MAX_LENGTH - dest->len)
412 return FALSE; /* detected overflow of dest->len + len below */
414 if (!set_length (dest, dest->len + len))
417 memmove (dest->str + insert_at + len,
418 dest->str + insert_at,
419 dest->len - len - insert_at);
424 #ifndef _dbus_string_get_data
426 * Gets the raw character buffer from the string. The returned buffer
427 * will be nul-terminated, but note that strings may contain binary
428 * data so there may be extra nul characters prior to the termination.
429 * This function should be little-used, extend DBusString or add
430 * stuff to dbus-sysdeps.c instead. It's an error to use this
431 * function on a const string.
433 * @param str the string
437 _dbus_string_get_data (DBusString *str)
439 DBUS_STRING_PREAMBLE (str);
441 return (char*) real->str;
443 #endif /* _dbus_string_get_data */
445 /* only do the function if we don't have the macro */
446 #ifndef _dbus_string_get_const_data
448 * Gets the raw character buffer from a const string.
450 * @param str the string
451 * @returns the string data
454 _dbus_string_get_const_data (const DBusString *str)
456 DBUS_CONST_STRING_PREAMBLE (str);
458 return (const char*) real->str;
460 #endif /* _dbus_string_get_const_data */
463 * Gets a sub-portion of the raw character buffer from the
464 * string. The "len" field is required simply for error
465 * checking, to be sure you don't try to use more
466 * string than exists. The nul termination of the
467 * returned buffer remains at the end of the entire
468 * string, not at start + len.
470 * @param str the string
471 * @param start byte offset to return
472 * @param len length of segment to return
473 * @returns the string data
476 _dbus_string_get_data_len (DBusString *str,
480 DBUS_STRING_PREAMBLE (str);
481 _dbus_assert (start >= 0);
482 _dbus_assert (len >= 0);
483 _dbus_assert (start <= real->len);
484 _dbus_assert (len <= real->len - start);
486 return (char*) real->str + start;
489 /* only do the function if we don't have the macro */
490 #ifndef _dbus_string_get_const_data_len
492 * const version of _dbus_string_get_data_len().
494 * @param str the string
495 * @param start byte offset to return
496 * @param len length of segment to return
497 * @returns the string data
500 _dbus_string_get_const_data_len (const DBusString *str,
504 DBUS_CONST_STRING_PREAMBLE (str);
505 _dbus_assert (start >= 0);
506 _dbus_assert (len >= 0);
507 _dbus_assert (start <= real->len);
508 _dbus_assert (len <= real->len - start);
510 return (const char*) real->str + start;
512 #endif /* _dbus_string_get_const_data_len */
514 /* only do the function if we don't have the macro */
515 #ifndef _dbus_string_set_byte
517 * Sets the value of the byte at the given position.
519 * @param str the string
520 * @param i the position
521 * @param byte the new value
524 _dbus_string_set_byte (DBusString *str,
528 DBUS_STRING_PREAMBLE (str);
529 _dbus_assert (i < real->len);
530 _dbus_assert (i >= 0);
534 #endif /* _dbus_string_set_byte */
536 /* only have the function if we didn't create a macro */
537 #ifndef _dbus_string_get_byte
539 * Gets the byte at the given position. It is
540 * allowed to ask for the nul byte at the end of
543 * @param str the string
544 * @param start the position
545 * @returns the byte at that position
548 _dbus_string_get_byte (const DBusString *str,
551 DBUS_CONST_STRING_PREAMBLE (str);
552 _dbus_assert (start <= real->len);
553 _dbus_assert (start >= 0);
555 return real->str[start];
557 #endif /* _dbus_string_get_byte */
560 * Inserts a number of bytes of a given value at the
563 * @param str the string
564 * @param i the position
565 * @param n_bytes number of bytes
566 * @param byte the value to insert
567 * @returns #TRUE on success
570 _dbus_string_insert_bytes (DBusString *str,
575 DBUS_STRING_PREAMBLE (str);
576 _dbus_assert (i <= real->len);
577 _dbus_assert (i >= 0);
578 _dbus_assert (n_bytes >= 0);
583 if (!open_gap (n_bytes, real, i))
586 memset (real->str + i, byte, n_bytes);
592 * Inserts a single byte at the given position.
594 * @param str the string
595 * @param i the position
596 * @param byte the value to insert
597 * @returns #TRUE on success
600 _dbus_string_insert_byte (DBusString *str,
604 DBUS_STRING_PREAMBLE (str);
605 _dbus_assert (i <= real->len);
606 _dbus_assert (i >= 0);
608 if (!open_gap (1, real, i))
617 * Like _dbus_string_get_data(), but removes the
618 * gotten data from the original string. The caller
619 * must free the data returned. This function may
620 * fail due to lack of memory, and return #FALSE.
622 * @param str the string
623 * @param data_return location to return the buffer
624 * @returns #TRUE on success
627 _dbus_string_steal_data (DBusString *str,
630 DBUS_STRING_PREAMBLE (str);
631 _dbus_assert (data_return != NULL);
633 undo_alignment (real);
635 *data_return = (char*) real->str;
637 /* reset the string */
638 if (!_dbus_string_init (str))
640 /* hrm, put it back then */
641 real->str = (unsigned char*) *data_return;
643 fixup_alignment (real);
651 * Copies the data from the string into a char*
653 * @param str the string
654 * @param data_return place to return the data
655 * @returns #TRUE on success, #FALSE on no memory
658 _dbus_string_copy_data (const DBusString *str,
661 DBUS_CONST_STRING_PREAMBLE (str);
662 _dbus_assert (data_return != NULL);
664 *data_return = dbus_malloc (real->len + 1);
665 if (*data_return == NULL)
668 memcpy (*data_return, real->str, real->len + 1);
674 * Copies the contents of a DBusString into a different buffer. It is
675 * a bug if avail_len is too short to hold the string contents. nul
676 * termination is not copied, just the supplied bytes.
678 * @param str a string
679 * @param buffer a C buffer to copy data to
680 * @param avail_len maximum length of C buffer
683 _dbus_string_copy_to_buffer (const DBusString *str,
687 DBUS_CONST_STRING_PREAMBLE (str);
689 _dbus_assert (avail_len >= 0);
690 _dbus_assert (avail_len >= real->len);
692 memcpy (buffer, real->str, real->len);
696 * Copies the contents of a DBusString into a different buffer. It is
697 * a bug if avail_len is too short to hold the string contents plus a
700 * @param str a string
701 * @param buffer a C buffer to copy data to
702 * @param avail_len maximum length of C buffer
705 _dbus_string_copy_to_buffer_with_nul (const DBusString *str,
709 DBUS_CONST_STRING_PREAMBLE (str);
711 _dbus_assert (avail_len >= 0);
712 _dbus_assert (avail_len > real->len);
714 memcpy (buffer, real->str, real->len+1);
717 /* Only have the function if we don't have the macro */
718 #ifndef _dbus_string_get_length
720 * Gets the length of a string (not including nul termination).
722 * @returns the length.
725 _dbus_string_get_length (const DBusString *str)
727 DBUS_CONST_STRING_PREAMBLE (str);
731 #endif /* !_dbus_string_get_length */
734 * Makes a string longer by the given number of bytes. Checks whether
735 * adding additional_length to the current length would overflow an
736 * integer, and checks for exceeding a string's max length.
737 * The new bytes are not initialized, other than nul-terminating
738 * the end of the string. The uninitialized bytes may contain
739 * nul bytes or other junk.
741 * @param str a string
742 * @param additional_length length to add to the string.
743 * @returns #TRUE on success.
746 _dbus_string_lengthen (DBusString *str,
747 int additional_length)
749 DBUS_STRING_PREAMBLE (str);
750 _dbus_assert (additional_length >= 0);
752 if (_DBUS_UNLIKELY (additional_length > _DBUS_STRING_MAX_LENGTH - real->len))
753 return FALSE; /* would overflow */
755 return set_length (real,
756 real->len + additional_length);
760 * Makes a string shorter by the given number of bytes.
762 * @param str a string
763 * @param length_to_remove length to remove from the string.
766 _dbus_string_shorten (DBusString *str,
767 int length_to_remove)
769 DBUS_STRING_PREAMBLE (str);
770 _dbus_assert (length_to_remove >= 0);
771 _dbus_assert (length_to_remove <= real->len);
774 real->len - length_to_remove);
778 * Sets the length of a string. Can be used to truncate or lengthen
779 * the string. If the string is lengthened, the function may fail and
780 * return #FALSE. Newly-added bytes are not initialized, as with
781 * _dbus_string_lengthen().
783 * @param str a string
784 * @param length new length of the string.
785 * @returns #FALSE on failure.
788 _dbus_string_set_length (DBusString *str,
791 DBUS_STRING_PREAMBLE (str);
792 _dbus_assert (length >= 0);
794 return set_length (real, length);
798 align_insert_point_then_open_gap (DBusString *str,
803 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
804 unsigned long gap_pos;
807 DBUS_STRING_PREAMBLE (str);
808 _dbus_assert (alignment >= 1);
809 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
811 insert_at = *insert_at_p;
813 _dbus_assert (insert_at <= real->len);
815 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
816 new_len = real->len + (gap_pos - insert_at) + gap_size;
818 if (_DBUS_UNLIKELY (new_len > (unsigned long) _DBUS_STRING_MAX_LENGTH))
821 delta = new_len - real->len;
822 _dbus_assert (delta >= 0);
824 if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
826 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
830 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
834 /* nul the padding if we had to add any padding */
835 if (gap_size < delta)
837 memset (&real->str[insert_at], '\0',
838 gap_pos - insert_at);
841 *insert_at_p = gap_pos;
847 align_length_then_lengthen (DBusString *str,
849 int then_lengthen_by)
853 insert_at = _dbus_string_get_length (str);
855 return align_insert_point_then_open_gap (str,
857 alignment, then_lengthen_by);
861 * Align the length of a string to a specific alignment (typically 4 or 8)
862 * by appending nul bytes to the string.
864 * @param str a string
865 * @param alignment the alignment
866 * @returns #FALSE if no memory
869 _dbus_string_align_length (DBusString *str,
872 return align_length_then_lengthen (str, alignment, 0);
876 * Preallocate extra_bytes such that a future lengthening of the
877 * string by extra_bytes is guaranteed to succeed without an out of
880 * @param str a string
881 * @param extra_bytes bytes to alloc
882 * @returns #FALSE if no memory
885 _dbus_string_alloc_space (DBusString *str,
888 if (!_dbus_string_lengthen (str, extra_bytes))
890 _dbus_string_shorten (str, extra_bytes);
896 append (DBusRealString *real,
903 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
906 memcpy (real->str + (real->len - buffer_len),
914 * Appends a nul-terminated C-style string to a DBusString.
916 * @param str the DBusString
917 * @param buffer the nul-terminated characters to append
918 * @returns #FALSE if not enough memory.
921 _dbus_string_append (DBusString *str,
924 unsigned long buffer_len;
926 DBUS_STRING_PREAMBLE (str);
927 _dbus_assert (buffer != NULL);
929 buffer_len = strlen (buffer);
930 if (buffer_len > (unsigned long) _DBUS_STRING_MAX_LENGTH)
933 return append (real, buffer, buffer_len);
936 /** assign 2 bytes from one string to another */
937 #define ASSIGN_2_OCTETS(p, octets) \
938 *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
940 /** assign 4 bytes from one string to another */
941 #define ASSIGN_4_OCTETS(p, octets) \
942 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
944 #ifdef DBUS_HAVE_INT64
945 /** assign 8 bytes from one string to another */
946 #define ASSIGN_8_OCTETS(p, octets) \
947 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
949 /** assign 8 bytes from one string to another */
950 #define ASSIGN_8_OCTETS(p, octets) \
964 _dbus_assert (b == p + 8); \
966 #endif /* DBUS_HAVE_INT64 */
969 * Inserts 2 bytes aligned on a 2 byte boundary
970 * with any alignment padding initialized to 0.
972 * @param str the DBusString
973 * @param insert_at where to insert
974 * @param octets 2 bytes to insert
975 * @returns #FALSE if not enough memory.
978 _dbus_string_insert_2_aligned (DBusString *str,
980 const unsigned char octets[2])
982 DBUS_STRING_PREAMBLE (str);
984 if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
987 ASSIGN_2_OCTETS (real->str + insert_at, octets);
993 * Inserts 4 bytes aligned on a 4 byte boundary
994 * with any alignment padding initialized to 0.
996 * @param str the DBusString
997 * @param insert_at where to insert
998 * @param octets 4 bytes to insert
999 * @returns #FALSE if not enough memory.
1002 _dbus_string_insert_4_aligned (DBusString *str,
1004 const unsigned char octets[4])
1006 DBUS_STRING_PREAMBLE (str);
1008 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1011 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1017 * Inserts 8 bytes aligned on an 8 byte boundary
1018 * with any alignment padding initialized to 0.
1020 * @param str the DBusString
1021 * @param insert_at where to insert
1022 * @param octets 8 bytes to insert
1023 * @returns #FALSE if not enough memory.
1026 _dbus_string_insert_8_aligned (DBusString *str,
1028 const unsigned char octets[8])
1030 DBUS_STRING_PREAMBLE (str);
1032 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1035 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1037 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1044 * Inserts padding at *insert_at such to align it to the given
1045 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1046 * to the aligned position.
1048 * @param str the DBusString
1049 * @param insert_at location to be aligned
1050 * @param alignment alignment boundary (1, 2, 4, or 8)
1051 * @returns #FALSE if not enough memory.
1054 _dbus_string_insert_alignment (DBusString *str,
1058 DBUS_STRING_PREAMBLE (str);
1060 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1063 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1069 * Appends a printf-style formatted string
1070 * to the #DBusString.
1072 * @param str the string
1073 * @param format printf format
1074 * @param args variable argument list
1075 * @returns #FALSE if no memory
1078 _dbus_string_append_printf_valist (DBusString *str,
1085 DBUS_STRING_PREAMBLE (str);
1087 DBUS_VA_COPY (args_copy, args);
1089 /* Measure the message length without terminating nul */
1090 len = _dbus_printf_string_upper_bound (format, args);
1095 if (!_dbus_string_lengthen (str, len))
1097 /* don't leak the copy */
1102 vsprintf ((char*) (real->str + (real->len - len)),
1111 * Appends a printf-style formatted string
1112 * to the #DBusString.
1114 * @param str the string
1115 * @param format printf format
1116 * @returns #FALSE if no memory
1119 _dbus_string_append_printf (DBusString *str,
1126 va_start (args, format);
1127 retval = _dbus_string_append_printf_valist (str, format, args);
1134 * Appends block of bytes with the given length to a DBusString.
1136 * @param str the DBusString
1137 * @param buffer the bytes to append
1138 * @param len the number of bytes to append
1139 * @returns #FALSE if not enough memory.
1142 _dbus_string_append_len (DBusString *str,
1146 DBUS_STRING_PREAMBLE (str);
1147 _dbus_assert (buffer != NULL);
1148 _dbus_assert (len >= 0);
1150 return append (real, buffer, len);
1154 * Appends a single byte to the string, returning #FALSE
1155 * if not enough memory.
1157 * @param str the string
1158 * @param byte the byte to append
1159 * @returns #TRUE on success
1162 _dbus_string_append_byte (DBusString *str,
1165 DBUS_STRING_PREAMBLE (str);
1167 if (!set_length (real, real->len + 1))
1170 real->str[real->len-1] = byte;
1176 delete (DBusRealString *real,
1183 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1185 real->str[real->len] = '\0';
1189 * Deletes a segment of a DBusString with length len starting at
1190 * start. (Hint: to clear an entire string, setting length to 0
1191 * with _dbus_string_set_length() is easier.)
1193 * @param str the DBusString
1194 * @param start where to start deleting
1195 * @param len the number of bytes to delete
1198 _dbus_string_delete (DBusString *str,
1202 DBUS_STRING_PREAMBLE (str);
1203 _dbus_assert (start >= 0);
1204 _dbus_assert (len >= 0);
1205 _dbus_assert (start <= real->len);
1206 _dbus_assert (len <= real->len - start);
1208 delete (real, start, len);
1212 copy (DBusRealString *source,
1215 DBusRealString *dest,
1221 if (!open_gap (len, dest, insert_at))
1224 memmove (dest->str + insert_at,
1225 source->str + start,
1232 * Checks assertions for two strings we're copying a segment between,
1233 * and declares real_source/real_dest variables.
1235 * @param source the source string
1236 * @param start the starting offset
1237 * @param dest the dest string
1238 * @param insert_at where the copied segment is inserted
1240 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1241 DBusRealString *real_source = (DBusRealString*) source; \
1242 DBusRealString *real_dest = (DBusRealString*) dest; \
1243 _dbus_assert ((source) != (dest)); \
1244 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1245 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1246 _dbus_assert (!real_dest->constant); \
1247 _dbus_assert (!real_dest->locked); \
1248 _dbus_assert ((start) >= 0); \
1249 _dbus_assert ((start) <= real_source->len); \
1250 _dbus_assert ((insert_at) >= 0); \
1251 _dbus_assert ((insert_at) <= real_dest->len)
1254 * Moves the end of one string into another string. Both strings
1255 * must be initialized, valid strings.
1257 * @param source the source string
1258 * @param start where to chop off the source string
1259 * @param dest the destination string
1260 * @param insert_at where to move the chopped-off part of source string
1261 * @returns #FALSE if not enough memory
1264 _dbus_string_move (DBusString *source,
1269 DBusRealString *real_source = (DBusRealString*) source;
1270 _dbus_assert (start <= real_source->len);
1272 return _dbus_string_move_len (source, start,
1273 real_source->len - start,
1278 * Like _dbus_string_move(), but does not delete the section
1279 * of the source string that's copied to the dest string.
1281 * @param source the source string
1282 * @param start where to start copying the source string
1283 * @param dest the destination string
1284 * @param insert_at where to place the copied part of source string
1285 * @returns #FALSE if not enough memory
1288 _dbus_string_copy (const DBusString *source,
1293 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1295 return copy (real_source, start,
1296 real_source->len - start,
1302 * Like _dbus_string_move(), but can move a segment from
1303 * the middle of the source string.
1305 * @param source the source string
1306 * @param start first byte of source string to move
1307 * @param len length of segment to move
1308 * @param dest the destination string
1309 * @param insert_at where to move the bytes from the source string
1310 * @returns #FALSE if not enough memory
1313 _dbus_string_move_len (DBusString *source,
1320 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1321 _dbus_assert (len >= 0);
1322 _dbus_assert ((start + len) <= real_source->len);
1329 else if (start == 0 &&
1330 len == real_source->len &&
1331 real_dest->len == 0)
1333 /* Short-circuit moving an entire existing string to an empty string
1334 * by just swapping the buffers.
1336 /* we assume ->constant doesn't matter as you can't have
1337 * a constant string involved in a move.
1339 #define ASSIGN_DATA(a, b) do { \
1340 (a)->str = (b)->str; \
1341 (a)->len = (b)->len; \
1342 (a)->allocated = (b)->allocated; \
1343 (a)->align_offset = (b)->align_offset; \
1348 ASSIGN_DATA (&tmp, real_source);
1349 ASSIGN_DATA (real_source, real_dest);
1350 ASSIGN_DATA (real_dest, &tmp);
1356 if (!copy (real_source, start, len,
1361 delete (real_source, start,
1369 * Like _dbus_string_copy(), but can copy a segment from the middle of
1370 * the source string.
1372 * @param source the source string
1373 * @param start where to start copying the source string
1374 * @param len length of segment to copy
1375 * @param dest the destination string
1376 * @param insert_at where to place the copied segment of source string
1377 * @returns #FALSE if not enough memory
1380 _dbus_string_copy_len (const DBusString *source,
1386 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1387 _dbus_assert (len >= 0);
1388 _dbus_assert (start <= real_source->len);
1389 _dbus_assert (len <= real_source->len - start);
1391 return copy (real_source, start, len,
1397 * Replaces a segment of dest string with a segment of source string.
1399 * @param source the source string
1400 * @param start where to start copying the source string
1401 * @param len length of segment to copy
1402 * @param dest the destination string
1403 * @param replace_at start of segment of dest string to replace
1404 * @param replace_len length of segment of dest string to replace
1405 * @returns #FALSE if not enough memory
1409 _dbus_string_replace_len (const DBusString *source,
1416 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1417 _dbus_assert (len >= 0);
1418 _dbus_assert (start <= real_source->len);
1419 _dbus_assert (len <= real_source->len - start);
1420 _dbus_assert (replace_at >= 0);
1421 _dbus_assert (replace_at <= real_dest->len);
1422 _dbus_assert (replace_len <= real_dest->len - replace_at);
1424 if (len == replace_len)
1426 memmove (real_dest->str + replace_at,
1427 real_source->str + start, len);
1429 else if (len < replace_len)
1431 memmove (real_dest->str + replace_at,
1432 real_source->str + start, len);
1433 delete (real_dest, replace_at + len,
1440 _dbus_assert (len > replace_len);
1442 diff = len - replace_len;
1444 /* First of all we check if destination string can be enlarged as
1445 * required, then we overwrite previous bytes
1448 if (!copy (real_source, start + replace_len, diff,
1449 real_dest, replace_at + replace_len))
1452 memmove (real_dest->str + replace_at,
1453 real_source->str + start, replace_len);
1460 * Looks for the first occurance of a byte, deletes that byte,
1461 * and moves everything after the byte to the beginning of a
1462 * separate string. Both strings must be initialized, valid
1465 * @param source the source string
1466 * @param byte the byte to remove and split the string at
1467 * @param tail the split off string
1468 * @returns #FALSE if not enough memory or if byte could not be found
1472 _dbus_string_split_on_byte (DBusString *source,
1477 char byte_string[2] = "";
1481 byte_string[0] = (char) byte;
1483 if (!_dbus_string_find (source, 0, byte_string, &byte_position))
1486 head_length = byte_position;
1487 tail_length = _dbus_string_get_length (source) - head_length - 1;
1489 if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
1493 /* remove the trailing delimiter byte from the head now.
1495 if (!_dbus_string_set_length (source, head_length))
1501 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1502 * Pennington, and Tom Tromey are the authors and authorized relicense.
1505 /** computes length and mask of a unicode character
1506 * @param Char the char
1507 * @param Mask the mask variable to assign to
1508 * @param Len the length variable to assign to
1510 #define UTF8_COMPUTE(Char, Mask, Len) \
1516 else if ((Char & 0xe0) == 0xc0) \
1521 else if ((Char & 0xf0) == 0xe0) \
1526 else if ((Char & 0xf8) == 0xf0) \
1531 else if ((Char & 0xfc) == 0xf8) \
1536 else if ((Char & 0xfe) == 0xfc) \
1548 * computes length of a unicode character in UTF-8
1549 * @param Char the char
1551 #define UTF8_LENGTH(Char) \
1552 ((Char) < 0x80 ? 1 : \
1553 ((Char) < 0x800 ? 2 : \
1554 ((Char) < 0x10000 ? 3 : \
1555 ((Char) < 0x200000 ? 4 : \
1556 ((Char) < 0x4000000 ? 5 : 6)))))
1559 * Gets a UTF-8 value.
1561 * @param Result variable for extracted unicode char.
1562 * @param Chars the bytes to decode
1563 * @param Count counter variable
1564 * @param Mask mask for this char
1565 * @param Len length for this char in bytes
1567 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1568 (Result) = (Chars)[0] & (Mask); \
1569 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1571 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1577 (Result) |= ((Chars)[(Count)] & 0x3f); \
1581 * Check whether a Unicode (5.2) char is in a valid range.
1583 * The first check comes from the Unicode guarantee to never encode
1584 * a point above 0x0010ffff, since UTF-16 couldn't represent it.
1586 * The second check covers surrogate pairs (category Cs).
1588 * @param Char the character
1590 #define UNICODE_VALID(Char) \
1591 ((Char) < 0x110000 && \
1592 (((Char) & 0xFFFFF800) != 0xD800))
1595 * Finds the given substring in the string,
1596 * returning #TRUE and filling in the byte index
1597 * where the substring was found, if it was found.
1598 * Returns #FALSE if the substring wasn't found.
1599 * Sets *start to the length of the string if the substring
1602 * @param str the string
1603 * @param start where to start looking
1604 * @param substr the substring
1605 * @param found return location for where it was found, or #NULL
1606 * @returns #TRUE if found
1609 _dbus_string_find (const DBusString *str,
1614 return _dbus_string_find_to (str, start,
1615 ((const DBusRealString*)str)->len,
1620 * Finds end of line ("\r\n" or "\n") in the string,
1621 * returning #TRUE and filling in the byte index
1622 * where the eol string was found, if it was found.
1623 * Returns #FALSE if eol wasn't found.
1625 * @param str the string
1626 * @param start where to start looking
1627 * @param found return location for where eol was found or string length otherwise
1628 * @param found_len return length of found eol string or zero otherwise
1629 * @returns #TRUE if found
1632 _dbus_string_find_eol (const DBusString *str,
1639 DBUS_CONST_STRING_PREAMBLE (str);
1640 _dbus_assert (start <= real->len);
1641 _dbus_assert (start >= 0);
1644 while (i < real->len)
1646 if (real->str[i] == '\r')
1648 if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
1656 else /* only "\r" */
1665 else if (real->str[i] == '\n') /* only "\n" */
1686 * Finds the given substring in the string,
1687 * up to a certain position,
1688 * returning #TRUE and filling in the byte index
1689 * where the substring was found, if it was found.
1690 * Returns #FALSE if the substring wasn't found.
1691 * Sets *start to the length of the string if the substring
1694 * @param str the string
1695 * @param start where to start looking
1696 * @param end where to stop looking
1697 * @param substr the substring
1698 * @param found return location for where it was found, or #NULL
1699 * @returns #TRUE if found
1702 _dbus_string_find_to (const DBusString *str,
1709 DBUS_CONST_STRING_PREAMBLE (str);
1710 _dbus_assert (substr != NULL);
1711 _dbus_assert (start <= real->len);
1712 _dbus_assert (start >= 0);
1713 _dbus_assert (substr != NULL);
1714 _dbus_assert (end <= real->len);
1715 _dbus_assert (start <= end);
1717 /* we always "find" an empty string */
1718 if (*substr == '\0')
1728 if (real->str[i] == substr[0])
1734 if (substr[j - i] == '\0')
1736 else if (real->str[j] != substr[j - i])
1742 if (substr[j - i] == '\0')
1760 * Finds a blank (space or tab) in the string. Returns #TRUE
1761 * if found, #FALSE otherwise. If a blank is not found sets
1762 * *found to the length of the string.
1764 * @param str the string
1765 * @param start byte index to start looking
1766 * @param found place to store the location of the first blank
1767 * @returns #TRUE if a blank was found
1770 _dbus_string_find_blank (const DBusString *str,
1775 DBUS_CONST_STRING_PREAMBLE (str);
1776 _dbus_assert (start <= real->len);
1777 _dbus_assert (start >= 0);
1780 while (i < real->len)
1782 if (real->str[i] == ' ' ||
1783 real->str[i] == '\t')
1800 * Skips blanks from start, storing the first non-blank in *end
1801 * (blank is space or tab).
1803 * @param str the string
1804 * @param start where to start
1805 * @param end where to store the first non-blank byte index
1808 _dbus_string_skip_blank (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 (!DBUS_IS_ASCII_BLANK (real->str[i]))
1826 _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
1834 * Skips whitespace from start, storing the first non-whitespace in *end.
1835 * (whitespace is space, tab, newline, CR).
1837 * @param str the string
1838 * @param start where to start
1839 * @param end where to store the first non-whitespace byte index
1842 _dbus_string_skip_white (const DBusString *str,
1847 DBUS_CONST_STRING_PREAMBLE (str);
1848 _dbus_assert (start <= real->len);
1849 _dbus_assert (start >= 0);
1852 while (i < real->len)
1854 if (!DBUS_IS_ASCII_WHITE (real->str[i]))
1860 _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
1867 * Skips whitespace from end, storing the start index of the trailing
1868 * whitespace in *start. (whitespace is space, tab, newline, CR).
1870 * @param str the string
1871 * @param end where to start scanning backward
1872 * @param start where to store the start of whitespace chars
1875 _dbus_string_skip_white_reverse (const DBusString *str,
1880 DBUS_CONST_STRING_PREAMBLE (str);
1881 _dbus_assert (end <= real->len);
1882 _dbus_assert (end >= 0);
1887 if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
1892 _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
1899 * Assigns a newline-terminated or \\r\\n-terminated line from the front
1900 * of the string to the given dest string. The dest string's previous
1901 * contents are deleted. If the source string contains no newline,
1902 * moves the entire source string to the dest string.
1904 * @todo owen correctly notes that this is a stupid function (it was
1905 * written purely for test code,
1906 * e.g. dbus-message-builder.c). Probably should be enforced as test
1907 * code only with ifdef DBUS_ENABLE_EMBEDDED_TESTS
1909 * @param source the source string
1910 * @param dest the destination string (contents are replaced)
1911 * @returns #FALSE if no memory, or source has length 0
1914 _dbus_string_pop_line (DBusString *source,
1919 _dbus_string_set_length (dest, 0);
1923 if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
1925 _dbus_assert (eol == _dbus_string_get_length (source));
1928 /* If there's no newline and source has zero length, we're done */
1931 /* otherwise, the last line of the file has no eol characters */
1934 /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
1935 * since find_eol returned TRUE
1938 if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
1941 /* remove line ending */
1942 if (!_dbus_string_set_length (dest, eol))
1944 _dbus_assert_not_reached ("out of memory when shortening a string");
1951 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1953 * Deletes up to and including the first blank space
1956 * @param str the string
1959 _dbus_string_delete_first_word (DBusString *str)
1963 if (_dbus_string_find_blank (str, 0, &i))
1964 _dbus_string_skip_blank (str, i, &i);
1966 _dbus_string_delete (str, 0, i);
1970 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1972 * Deletes any leading blanks in the string
1974 * @param str the string
1977 _dbus_string_delete_leading_blanks (DBusString *str)
1981 _dbus_string_skip_blank (str, 0, &i);
1984 _dbus_string_delete (str, 0, i);
1989 * Deletes leading and trailing whitespace
1991 * @param str the string
1994 _dbus_string_chop_white(DBusString *str)
1998 _dbus_string_skip_white (str, 0, &i);
2001 _dbus_string_delete (str, 0, i);
2003 _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
2005 _dbus_string_set_length (str, i);
2009 * Tests two DBusString for equality.
2011 * @todo memcmp is probably faster
2013 * @param a first string
2014 * @param b second string
2015 * @returns #TRUE if equal
2018 _dbus_string_equal (const DBusString *a,
2019 const DBusString *b)
2021 const unsigned char *ap;
2022 const unsigned char *bp;
2023 const unsigned char *a_end;
2024 const DBusRealString *real_a = (const DBusRealString*) a;
2025 const DBusRealString *real_b = (const DBusRealString*) b;
2026 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2027 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2029 if (real_a->len != real_b->len)
2034 a_end = real_a->str + real_a->len;
2048 * Tests two DBusString for equality up to the given length.
2049 * The strings may be shorter than the given length.
2051 * @todo write a unit test
2053 * @todo memcmp is probably faster
2055 * @param a first string
2056 * @param b second string
2057 * @param len the maximum length to look at
2058 * @returns #TRUE if equal for the given number of bytes
2061 _dbus_string_equal_len (const DBusString *a,
2062 const DBusString *b,
2065 const unsigned char *ap;
2066 const unsigned char *bp;
2067 const unsigned char *a_end;
2068 const DBusRealString *real_a = (const DBusRealString*) a;
2069 const DBusRealString *real_b = (const DBusRealString*) b;
2070 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2071 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2073 if (real_a->len != real_b->len &&
2074 (real_a->len < len || real_b->len < len))
2079 a_end = real_a->str + MIN (real_a->len, len);
2093 * Tests two sub-parts of two DBusString for equality. The specified
2094 * range of the first string must exist; the specified start position
2095 * of the second string must exist.
2097 * @todo write a unit test
2099 * @todo memcmp is probably faster
2101 * @param a first string
2102 * @param a_start where to start substring in first string
2103 * @param a_len length of substring in first string
2104 * @param b second string
2105 * @param b_start where to start substring in second string
2106 * @returns #TRUE if the two substrings are equal
2109 _dbus_string_equal_substring (const DBusString *a,
2112 const DBusString *b,
2115 const unsigned char *ap;
2116 const unsigned char *bp;
2117 const unsigned char *a_end;
2118 const DBusRealString *real_a = (const DBusRealString*) a;
2119 const DBusRealString *real_b = (const DBusRealString*) b;
2120 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2121 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2122 _dbus_assert (a_start >= 0);
2123 _dbus_assert (a_len >= 0);
2124 _dbus_assert (a_start <= real_a->len);
2125 _dbus_assert (a_len <= real_a->len - a_start);
2126 _dbus_assert (b_start >= 0);
2127 _dbus_assert (b_start <= real_b->len);
2129 if (a_len > real_b->len - b_start)
2132 ap = real_a->str + a_start;
2133 bp = real_b->str + b_start;
2144 _dbus_assert (bp <= (real_b->str + real_b->len));
2150 * Checks whether a string is equal to a C string.
2152 * @param a the string
2153 * @param c_str the C string
2154 * @returns #TRUE if equal
2157 _dbus_string_equal_c_str (const DBusString *a,
2160 const unsigned char *ap;
2161 const unsigned char *bp;
2162 const unsigned char *a_end;
2163 const DBusRealString *real_a = (const DBusRealString*) a;
2164 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2165 _dbus_assert (c_str != NULL);
2168 bp = (const unsigned char*) c_str;
2169 a_end = real_a->str + real_a->len;
2170 while (ap != a_end && *bp)
2179 if (ap != a_end || *bp)
2186 * Checks whether a string starts with the given C string.
2188 * @param a the string
2189 * @param c_str the C string
2190 * @returns #TRUE if string starts with it
2193 _dbus_string_starts_with_c_str (const DBusString *a,
2196 const unsigned char *ap;
2197 const unsigned char *bp;
2198 const unsigned char *a_end;
2199 const DBusRealString *real_a = (const DBusRealString*) a;
2200 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2201 _dbus_assert (c_str != NULL);
2204 bp = (const unsigned char*) c_str;
2205 a_end = real_a->str + real_a->len;
2206 while (ap != a_end && *bp)
2222 * Appends a two-character hex digit to a string, where the hex digit
2223 * has the value of the given byte.
2225 * @param str the string
2226 * @param byte the byte
2227 * @returns #FALSE if no memory
2230 _dbus_string_append_byte_as_hex (DBusString *str,
2233 const char hexdigits[16] = {
2234 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2235 'a', 'b', 'c', 'd', 'e', 'f'
2238 if (!_dbus_string_append_byte (str,
2239 hexdigits[(byte >> 4)]))
2242 if (!_dbus_string_append_byte (str,
2243 hexdigits[(byte & 0x0f)]))
2245 _dbus_string_set_length (str,
2246 _dbus_string_get_length (str) - 1);
2254 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2255 * encoded. (Each byte is two hex digits.)
2257 * @param source the string to encode
2258 * @param start byte index to start encoding
2259 * @param dest string where encoded data should be placed
2260 * @param insert_at where to place encoded data
2261 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2264 _dbus_string_hex_encode (const DBusString *source,
2270 const unsigned char *p;
2271 const unsigned char *end;
2274 _dbus_assert (start <= _dbus_string_get_length (source));
2276 if (!_dbus_string_init (&result))
2281 p = (const unsigned char*) _dbus_string_get_const_data (source);
2282 end = p + _dbus_string_get_length (source);
2287 if (!_dbus_string_append_byte_as_hex (&result, *p))
2293 if (!_dbus_string_move (&result, 0, dest, insert_at))
2299 _dbus_string_free (&result);
2304 * Decodes a string from hex encoding.
2306 * @param source the string to decode
2307 * @param start byte index to start decode
2308 * @param end_return return location of the end of the hex data, or #NULL
2309 * @param dest string where decoded data should be placed
2310 * @param insert_at where to place decoded data
2311 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2314 _dbus_string_hex_decode (const DBusString *source,
2321 const unsigned char *p;
2322 const unsigned char *end;
2324 dbus_bool_t high_bits;
2326 _dbus_assert (start <= _dbus_string_get_length (source));
2328 if (!_dbus_string_init (&result))
2334 p = (const unsigned char*) _dbus_string_get_const_data (source);
2335 end = p + _dbus_string_get_length (source);
2404 if (!_dbus_string_append_byte (&result,
2413 len = _dbus_string_get_length (&result);
2415 b = _dbus_string_get_byte (&result, len - 1);
2419 _dbus_string_set_byte (&result, len - 1, b);
2422 high_bits = !high_bits;
2428 if (!_dbus_string_move (&result, 0, dest, insert_at))
2432 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2437 _dbus_string_free (&result);
2442 * Checks that the given range of the string is valid ASCII with no
2443 * nul bytes. If the given range is not entirely contained in the
2444 * string, returns #FALSE.
2446 * @todo this is inconsistent with most of DBusString in that
2447 * it allows a start,len range that extends past the string end.
2449 * @param str the string
2450 * @param start first byte index to check
2451 * @param len number of bytes to check
2452 * @returns #TRUE if the byte range exists and is all valid ASCII
2455 _dbus_string_validate_ascii (const DBusString *str,
2459 const unsigned char *s;
2460 const unsigned char *end;
2461 DBUS_CONST_STRING_PREAMBLE (str);
2462 _dbus_assert (start >= 0);
2463 _dbus_assert (start <= real->len);
2464 _dbus_assert (len >= 0);
2466 if (len > real->len - start)
2469 s = real->str + start;
2473 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2483 * Converts the given range of the string to lower case.
2485 * @param str the string
2486 * @param start first byte index to convert
2487 * @param len number of bytes to convert
2490 _dbus_string_tolower_ascii (const DBusString *str,
2496 DBUS_STRING_PREAMBLE (str);
2497 _dbus_assert (start >= 0);
2498 _dbus_assert (start <= real->len);
2499 _dbus_assert (len >= 0);
2500 _dbus_assert (len <= real->len - start);
2502 s = real->str + start;
2507 if (*s >= 'A' && *s <= 'Z')
2514 * Converts the given range of the string to upper case.
2516 * @param str the string
2517 * @param start first byte index to convert
2518 * @param len number of bytes to convert
2521 _dbus_string_toupper_ascii (const DBusString *str,
2527 DBUS_STRING_PREAMBLE (str);
2528 _dbus_assert (start >= 0);
2529 _dbus_assert (start <= real->len);
2530 _dbus_assert (len >= 0);
2531 _dbus_assert (len <= real->len - start);
2533 s = real->str + start;
2538 if (*s >= 'a' && *s <= 'z')
2545 * Checks that the given range of the string is valid UTF-8. If the
2546 * given range is not entirely contained in the string, returns
2547 * #FALSE. If the string contains any nul bytes in the given range,
2548 * returns #FALSE. If the start and start+len are not on character
2549 * boundaries, returns #FALSE.
2551 * @todo this is inconsistent with most of DBusString in that
2552 * it allows a start,len range that extends past the string end.
2554 * @param str the string
2555 * @param start first byte index to check
2556 * @param len number of bytes to check
2557 * @returns #TRUE if the byte range exists and is all valid UTF-8
2560 _dbus_string_validate_utf8 (const DBusString *str,
2564 const unsigned char *p;
2565 const unsigned char *end;
2566 DBUS_CONST_STRING_PREAMBLE (str);
2567 _dbus_assert (start >= 0);
2568 _dbus_assert (start <= real->len);
2569 _dbus_assert (len >= 0);
2571 /* we are doing _DBUS_UNLIKELY() here which might be
2572 * dubious in a generic library like GLib, but in D-Bus
2573 * we know we're validating messages and that it would
2574 * only be evil/broken apps that would have invalid
2575 * UTF-8. Also, this function seems to be a performance
2576 * bottleneck in profiles.
2579 if (_DBUS_UNLIKELY (len > real->len - start))
2582 p = real->str + start;
2587 int i, mask, char_len;
2588 dbus_unichar_t result;
2590 /* nul bytes considered invalid */
2594 /* Special-case ASCII; this makes us go a lot faster in
2595 * D-Bus profiles where we are typically validating
2596 * function names and such. We have to know that
2597 * all following checks will pass for ASCII though,
2598 * comments follow ...
2606 UTF8_COMPUTE (*p, mask, char_len);
2608 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2611 /* check that the expected number of bytes exists in the remaining length */
2612 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2615 UTF8_GET (result, p, i, mask, char_len);
2617 /* Check for overlong UTF-8 */
2618 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2621 /* The UNICODE_VALID check below will catch this */
2622 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2626 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2629 /* UNICODE_VALID should have caught it */
2630 _dbus_assert (result != (dbus_unichar_t)-1);
2635 /* See that we covered the entire length if a length was
2638 if (_DBUS_UNLIKELY (p != end))
2645 * Checks that the given range of the string is all nul bytes. If the
2646 * given range is not entirely contained in the string, returns
2649 * @todo this is inconsistent with most of DBusString in that
2650 * it allows a start,len range that extends past the string end.
2652 * @param str the string
2653 * @param start first byte index to check
2654 * @param len number of bytes to check
2655 * @returns #TRUE if the byte range exists and is all nul bytes
2658 _dbus_string_validate_nul (const DBusString *str,
2662 const unsigned char *s;
2663 const unsigned char *end;
2664 DBUS_CONST_STRING_PREAMBLE (str);
2665 _dbus_assert (start >= 0);
2666 _dbus_assert (len >= 0);
2667 _dbus_assert (start <= real->len);
2669 if (len > real->len - start)
2672 s = real->str + start;
2676 if (_DBUS_UNLIKELY (*s != '\0'))
2685 * Clears all allocated bytes in the string to zero.
2687 * @param str the string
2690 _dbus_string_zero (DBusString *str)
2692 DBUS_STRING_PREAMBLE (str);
2694 memset (real->str - real->align_offset, '\0', real->allocated);
2698 /* tests are in dbus-string-util.c */