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);
249 dbus_free (real->str - real->align_offset);
251 real->invalid = TRUE;
255 compact (DBusRealString *real,
258 unsigned char *new_str;
262 waste = real->allocated - (real->len + _DBUS_STRING_ALLOCATION_PADDING);
264 if (waste <= max_waste)
267 new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
269 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
270 if (_DBUS_UNLIKELY (new_str == NULL))
273 real->str = new_str + real->align_offset;
274 real->allocated = new_allocated;
275 fixup_alignment (real);
280 #ifdef DBUS_BUILD_TESTS
281 /* Not using this feature at the moment,
282 * so marked DBUS_BUILD_TESTS-only
285 * Locks a string such that any attempts to change the string will
286 * result in aborting the program. Also, if the string is wasting a
287 * lot of memory (allocation is sufficiently larger than what the
288 * string is really using), _dbus_string_lock() will realloc the
289 * string's data to "compact" it.
291 * @param str the string to lock.
294 _dbus_string_lock (DBusString *str)
296 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
300 /* Try to realloc to avoid excess memory usage, since
301 * we know we won't change the string further
304 compact (real, MAX_WASTE);
306 #endif /* DBUS_BUILD_TESTS */
309 reallocate_for_length (DBusRealString *real,
313 unsigned char *new_str;
315 /* at least double our old allocation to avoid O(n), avoiding
318 if (real->allocated > (_DBUS_STRING_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
319 new_allocated = _DBUS_STRING_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
321 new_allocated = real->allocated * 2;
323 /* if you change the code just above here, run the tests without
324 * the following assert-only hack before you commit
326 /* This is keyed off asserts in addition to tests so when you
327 * disable asserts to profile, you don't get this destroyer
330 #ifdef DBUS_DISABLE_ASSERT
332 #ifdef DBUS_BUILD_TESTS
333 new_allocated = 0; /* ensure a realloc every time so that we go
334 * through all malloc failure codepaths
336 #endif /* DBUS_BUILD_TESTS */
337 #endif /* !DBUS_DISABLE_ASSERT */
339 /* But be sure we always alloc at least space for the new length */
340 new_allocated = MAX (new_allocated,
341 new_length + _DBUS_STRING_ALLOCATION_PADDING);
343 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
344 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
345 if (_DBUS_UNLIKELY (new_str == NULL))
348 real->str = new_str + real->align_offset;
349 real->allocated = new_allocated;
350 fixup_alignment (real);
356 * Compacts the string to avoid wasted memory. Wasted memory is
357 * memory that is allocated but not actually required to store the
358 * current length of the string. The compact is only done if more
359 * than the given amount of memory is being wasted (otherwise the
360 * waste is ignored and the call does nothing).
362 * @param str the string
363 * @param max_waste the maximum amount of waste to ignore
364 * @returns #FALSE if the compact failed due to realloc failure
367 _dbus_string_compact (DBusString *str,
370 DBUS_STRING_PREAMBLE (str);
372 return compact (real, max_waste);
376 set_length (DBusRealString *real,
379 /* Note, we are setting the length not including nul termination */
381 /* exceeding max length is the same as failure to allocate memory */
382 if (_DBUS_UNLIKELY (new_length > _DBUS_STRING_MAX_LENGTH))
384 else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
385 _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
389 real->len = new_length;
390 real->str[new_length] = '\0';
397 DBusRealString *dest,
403 if (len > _DBUS_STRING_MAX_LENGTH - dest->len)
404 return FALSE; /* detected overflow of dest->len + len below */
406 if (!set_length (dest, dest->len + len))
409 memmove (dest->str + insert_at + len,
410 dest->str + insert_at,
411 dest->len - len - insert_at);
416 #ifndef _dbus_string_get_data
418 * Gets the raw character buffer from the string. The returned buffer
419 * will be nul-terminated, but note that strings may contain binary
420 * data so there may be extra nul characters prior to the termination.
421 * This function should be little-used, extend DBusString or add
422 * stuff to dbus-sysdeps.c instead. It's an error to use this
423 * function on a const string.
425 * @param str the string
429 _dbus_string_get_data (DBusString *str)
431 DBUS_STRING_PREAMBLE (str);
433 return (char*) real->str;
435 #endif /* _dbus_string_get_data */
437 /* only do the function if we don't have the macro */
438 #ifndef _dbus_string_get_const_data
440 * Gets the raw character buffer from a const string.
442 * @param str the string
443 * @returns the string data
446 _dbus_string_get_const_data (const DBusString *str)
448 DBUS_CONST_STRING_PREAMBLE (str);
450 return (const char*) real->str;
452 #endif /* _dbus_string_get_const_data */
455 * Gets a sub-portion of the raw character buffer from the
456 * string. The "len" field is required simply for error
457 * checking, to be sure you don't try to use more
458 * string than exists. The nul termination of the
459 * returned buffer remains at the end of the entire
460 * string, not at start + len.
462 * @param str the string
463 * @param start byte offset to return
464 * @param len length of segment to return
465 * @returns the string data
468 _dbus_string_get_data_len (DBusString *str,
472 DBUS_STRING_PREAMBLE (str);
473 _dbus_assert (start >= 0);
474 _dbus_assert (len >= 0);
475 _dbus_assert (start <= real->len);
476 _dbus_assert (len <= real->len - start);
478 return (char*) real->str + start;
481 /* only do the function if we don't have the macro */
482 #ifndef _dbus_string_get_const_data_len
484 * const version of _dbus_string_get_data_len().
486 * @param str the string
487 * @param start byte offset to return
488 * @param len length of segment to return
489 * @returns the string data
492 _dbus_string_get_const_data_len (const DBusString *str,
496 DBUS_CONST_STRING_PREAMBLE (str);
497 _dbus_assert (start >= 0);
498 _dbus_assert (len >= 0);
499 _dbus_assert (start <= real->len);
500 _dbus_assert (len <= real->len - start);
502 return (const char*) real->str + start;
504 #endif /* _dbus_string_get_const_data_len */
506 /* only do the function if we don't have the macro */
507 #ifndef _dbus_string_set_byte
509 * Sets the value of the byte at the given position.
511 * @param str the string
512 * @param i the position
513 * @param byte the new value
516 _dbus_string_set_byte (DBusString *str,
520 DBUS_STRING_PREAMBLE (str);
521 _dbus_assert (i < real->len);
522 _dbus_assert (i >= 0);
526 #endif /* _dbus_string_set_byte */
528 /* only have the function if we didn't create a macro */
529 #ifndef _dbus_string_get_byte
531 * Gets the byte at the given position. It is
532 * allowed to ask for the nul byte at the end of
535 * @param str the string
536 * @param start the position
537 * @returns the byte at that position
540 _dbus_string_get_byte (const DBusString *str,
543 DBUS_CONST_STRING_PREAMBLE (str);
544 _dbus_assert (start <= real->len);
545 _dbus_assert (start >= 0);
547 return real->str[start];
549 #endif /* _dbus_string_get_byte */
552 * Inserts a number of bytes of a given value at the
555 * @param str the string
556 * @param i the position
557 * @param n_bytes number of bytes
558 * @param byte the value to insert
559 * @returns #TRUE on success
562 _dbus_string_insert_bytes (DBusString *str,
567 DBUS_STRING_PREAMBLE (str);
568 _dbus_assert (i <= real->len);
569 _dbus_assert (i >= 0);
570 _dbus_assert (n_bytes >= 0);
575 if (!open_gap (n_bytes, real, i))
578 memset (real->str + i, byte, n_bytes);
584 * Inserts a single byte at the given position.
586 * @param str the string
587 * @param i the position
588 * @param byte the value to insert
589 * @returns #TRUE on success
592 _dbus_string_insert_byte (DBusString *str,
596 DBUS_STRING_PREAMBLE (str);
597 _dbus_assert (i <= real->len);
598 _dbus_assert (i >= 0);
600 if (!open_gap (1, real, i))
609 * Like _dbus_string_get_data(), but removes the
610 * gotten data from the original string. The caller
611 * must free the data returned. This function may
612 * fail due to lack of memory, and return #FALSE.
614 * @param str the string
615 * @param data_return location to return the buffer
616 * @returns #TRUE on success
619 _dbus_string_steal_data (DBusString *str,
622 DBUS_STRING_PREAMBLE (str);
623 _dbus_assert (data_return != NULL);
625 undo_alignment (real);
627 *data_return = (char*) real->str;
629 /* reset the string */
630 if (!_dbus_string_init (str))
632 /* hrm, put it back then */
633 real->str = (unsigned char*) *data_return;
635 fixup_alignment (real);
643 * Copies the data from the string into a char*
645 * @param str the string
646 * @param data_return place to return the data
647 * @returns #TRUE on success, #FALSE on no memory
650 _dbus_string_copy_data (const DBusString *str,
653 DBUS_CONST_STRING_PREAMBLE (str);
654 _dbus_assert (data_return != NULL);
656 *data_return = dbus_malloc (real->len + 1);
657 if (*data_return == NULL)
660 memcpy (*data_return, real->str, real->len + 1);
666 * Copies the contents of a DBusString into a different buffer. It is
667 * a bug if avail_len is too short to hold the string contents. nul
668 * termination is not copied, just the supplied bytes.
670 * @param str a string
671 * @param buffer a C buffer to copy data to
672 * @param avail_len maximum length of C buffer
675 _dbus_string_copy_to_buffer (const DBusString *str,
679 DBUS_CONST_STRING_PREAMBLE (str);
681 _dbus_assert (avail_len >= 0);
682 _dbus_assert (avail_len >= real->len);
684 memcpy (buffer, real->str, real->len);
688 * Copies the contents of a DBusString into a different buffer. It is
689 * a bug if avail_len is too short to hold the string contents plus a
692 * @param str a string
693 * @param buffer a C buffer to copy data to
694 * @param avail_len maximum length of C buffer
697 _dbus_string_copy_to_buffer_with_nul (const DBusString *str,
701 DBUS_CONST_STRING_PREAMBLE (str);
703 _dbus_assert (avail_len >= 0);
704 _dbus_assert (avail_len > real->len);
706 memcpy (buffer, real->str, real->len+1);
709 /* Only have the function if we don't have the macro */
710 #ifndef _dbus_string_get_length
712 * Gets the length of a string (not including nul termination).
714 * @returns the length.
717 _dbus_string_get_length (const DBusString *str)
719 DBUS_CONST_STRING_PREAMBLE (str);
723 #endif /* !_dbus_string_get_length */
726 * Makes a string longer by the given number of bytes. Checks whether
727 * adding additional_length to the current length would overflow an
728 * integer, and checks for exceeding a string's max length.
729 * The new bytes are not initialized, other than nul-terminating
730 * the end of the string. The uninitialized bytes may contain
731 * nul bytes or other junk.
733 * @param str a string
734 * @param additional_length length to add to the string.
735 * @returns #TRUE on success.
738 _dbus_string_lengthen (DBusString *str,
739 int additional_length)
741 DBUS_STRING_PREAMBLE (str);
742 _dbus_assert (additional_length >= 0);
744 if (_DBUS_UNLIKELY (additional_length > _DBUS_STRING_MAX_LENGTH - real->len))
745 return FALSE; /* would overflow */
747 return set_length (real,
748 real->len + additional_length);
752 * Makes a string shorter by the given number of bytes.
754 * @param str a string
755 * @param length_to_remove length to remove from the string.
758 _dbus_string_shorten (DBusString *str,
759 int length_to_remove)
761 DBUS_STRING_PREAMBLE (str);
762 _dbus_assert (length_to_remove >= 0);
763 _dbus_assert (length_to_remove <= real->len);
766 real->len - length_to_remove);
770 * Sets the length of a string. Can be used to truncate or lengthen
771 * the string. If the string is lengthened, the function may fail and
772 * return #FALSE. Newly-added bytes are not initialized, as with
773 * _dbus_string_lengthen().
775 * @param str a string
776 * @param length new length of the string.
777 * @returns #FALSE on failure.
780 _dbus_string_set_length (DBusString *str,
783 DBUS_STRING_PREAMBLE (str);
784 _dbus_assert (length >= 0);
786 return set_length (real, length);
790 align_insert_point_then_open_gap (DBusString *str,
795 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
796 unsigned long gap_pos;
799 DBUS_STRING_PREAMBLE (str);
800 _dbus_assert (alignment >= 1);
801 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
803 insert_at = *insert_at_p;
805 _dbus_assert (insert_at <= real->len);
807 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
808 new_len = real->len + (gap_pos - insert_at) + gap_size;
810 if (_DBUS_UNLIKELY (new_len > (unsigned long) _DBUS_STRING_MAX_LENGTH))
813 delta = new_len - real->len;
814 _dbus_assert (delta >= 0);
816 if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
818 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
822 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
826 /* nul the padding if we had to add any padding */
827 if (gap_size < delta)
829 memset (&real->str[insert_at], '\0',
830 gap_pos - insert_at);
833 *insert_at_p = gap_pos;
839 align_length_then_lengthen (DBusString *str,
841 int then_lengthen_by)
845 insert_at = _dbus_string_get_length (str);
847 return align_insert_point_then_open_gap (str,
849 alignment, then_lengthen_by);
853 * Align the length of a string to a specific alignment (typically 4 or 8)
854 * by appending nul bytes to the string.
856 * @param str a string
857 * @param alignment the alignment
858 * @returns #FALSE if no memory
861 _dbus_string_align_length (DBusString *str,
864 return align_length_then_lengthen (str, alignment, 0);
868 * Preallocate extra_bytes such that a future lengthening of the
869 * string by extra_bytes is guaranteed to succeed without an out of
872 * @param str a string
873 * @param extra_bytes bytes to alloc
874 * @returns #FALSE if no memory
877 _dbus_string_alloc_space (DBusString *str,
880 if (!_dbus_string_lengthen (str, extra_bytes))
882 _dbus_string_shorten (str, extra_bytes);
888 append (DBusRealString *real,
895 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
898 memcpy (real->str + (real->len - buffer_len),
906 * Appends a nul-terminated C-style string to a DBusString.
908 * @param str the DBusString
909 * @param buffer the nul-terminated characters to append
910 * @returns #FALSE if not enough memory.
913 _dbus_string_append (DBusString *str,
916 unsigned long buffer_len;
918 DBUS_STRING_PREAMBLE (str);
919 _dbus_assert (buffer != NULL);
921 buffer_len = strlen (buffer);
922 if (buffer_len > (unsigned long) _DBUS_STRING_MAX_LENGTH)
925 return append (real, buffer, buffer_len);
928 /** assign 2 bytes from one string to another */
929 #define ASSIGN_2_OCTETS(p, octets) \
930 *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
932 /** assign 4 bytes from one string to another */
933 #define ASSIGN_4_OCTETS(p, octets) \
934 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
936 #ifdef DBUS_HAVE_INT64
937 /** assign 8 bytes from one string to another */
938 #define ASSIGN_8_OCTETS(p, octets) \
939 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
941 /** assign 8 bytes from one string to another */
942 #define ASSIGN_8_OCTETS(p, octets) \
956 _dbus_assert (b == p + 8); \
958 #endif /* DBUS_HAVE_INT64 */
960 #ifdef DBUS_BUILD_TESTS
962 * Appends 4 bytes aligned on a 4 byte boundary
963 * with any alignment padding initialized to 0.
965 * @param str the DBusString
966 * @param octets 4 bytes to append
967 * @returns #FALSE if not enough memory.
970 _dbus_string_append_4_aligned (DBusString *str,
971 const unsigned char octets[4])
973 DBUS_STRING_PREAMBLE (str);
975 if (!align_length_then_lengthen (str, 4, 4))
978 ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
982 #endif /* DBUS_BUILD_TESTS */
984 #ifdef DBUS_BUILD_TESTS
986 * Appends 8 bytes aligned on an 8 byte boundary
987 * with any alignment padding initialized to 0.
989 * @param str the DBusString
990 * @param octets 8 bytes to append
991 * @returns #FALSE if not enough memory.
994 _dbus_string_append_8_aligned (DBusString *str,
995 const unsigned char octets[8])
997 DBUS_STRING_PREAMBLE (str);
999 if (!align_length_then_lengthen (str, 8, 8))
1002 ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
1006 #endif /* DBUS_BUILD_TESTS */
1009 * Inserts 2 bytes aligned on a 2 byte boundary
1010 * with any alignment padding initialized to 0.
1012 * @param str the DBusString
1013 * @param insert_at where to insert
1014 * @param octets 2 bytes to insert
1015 * @returns #FALSE if not enough memory.
1018 _dbus_string_insert_2_aligned (DBusString *str,
1020 const unsigned char octets[4])
1022 DBUS_STRING_PREAMBLE (str);
1024 if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
1027 ASSIGN_2_OCTETS (real->str + insert_at, octets);
1033 * Inserts 4 bytes aligned on a 4 byte boundary
1034 * with any alignment padding initialized to 0.
1036 * @param str the DBusString
1037 * @param insert_at where to insert
1038 * @param octets 4 bytes to insert
1039 * @returns #FALSE if not enough memory.
1042 _dbus_string_insert_4_aligned (DBusString *str,
1044 const unsigned char octets[4])
1046 DBUS_STRING_PREAMBLE (str);
1048 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1051 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1057 * Inserts 8 bytes aligned on an 8 byte boundary
1058 * with any alignment padding initialized to 0.
1060 * @param str the DBusString
1061 * @param insert_at where to insert
1062 * @param octets 8 bytes to insert
1063 * @returns #FALSE if not enough memory.
1066 _dbus_string_insert_8_aligned (DBusString *str,
1068 const unsigned char octets[8])
1070 DBUS_STRING_PREAMBLE (str);
1072 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1075 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1077 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1084 * Inserts padding at *insert_at such to align it to the given
1085 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1086 * to the aligned position.
1088 * @param str the DBusString
1089 * @param insert_at location to be aligned
1090 * @param alignment alignment boundary (1, 2, 4, or 8)
1091 * @returns #FALSE if not enough memory.
1094 _dbus_string_insert_alignment (DBusString *str,
1098 DBUS_STRING_PREAMBLE (str);
1100 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1103 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1109 * Appends a printf-style formatted string
1110 * to the #DBusString.
1112 * @param str the string
1113 * @param format printf format
1114 * @param args variable argument list
1115 * @returns #FALSE if no memory
1118 _dbus_string_append_printf_valist (DBusString *str,
1125 DBUS_STRING_PREAMBLE (str);
1127 DBUS_VA_COPY (args_copy, args);
1129 /* Measure the message length without terminating nul */
1130 len = _dbus_printf_string_upper_bound (format, args);
1135 if (!_dbus_string_lengthen (str, len))
1137 /* don't leak the copy */
1142 vsprintf ((char*) (real->str + (real->len - len)),
1151 * Appends a printf-style formatted string
1152 * to the #DBusString.
1154 * @param str the string
1155 * @param format printf format
1156 * @returns #FALSE if no memory
1159 _dbus_string_append_printf (DBusString *str,
1166 va_start (args, format);
1167 retval = _dbus_string_append_printf_valist (str, format, args);
1174 * Appends block of bytes with the given length to a DBusString.
1176 * @param str the DBusString
1177 * @param buffer the bytes to append
1178 * @param len the number of bytes to append
1179 * @returns #FALSE if not enough memory.
1182 _dbus_string_append_len (DBusString *str,
1186 DBUS_STRING_PREAMBLE (str);
1187 _dbus_assert (buffer != NULL);
1188 _dbus_assert (len >= 0);
1190 return append (real, buffer, len);
1194 * Appends a single byte to the string, returning #FALSE
1195 * if not enough memory.
1197 * @param str the string
1198 * @param byte the byte to append
1199 * @returns #TRUE on success
1202 _dbus_string_append_byte (DBusString *str,
1205 DBUS_STRING_PREAMBLE (str);
1207 if (!set_length (real, real->len + 1))
1210 real->str[real->len-1] = byte;
1215 #ifdef DBUS_BUILD_TESTS
1217 * Appends a single Unicode character, encoding the character
1220 * @param str the string
1221 * @param ch the Unicode character
1224 _dbus_string_append_unichar (DBusString *str,
1232 DBUS_STRING_PREAMBLE (str);
1234 /* this code is from GLib but is pretty standard I think */
1243 else if (ch < 0x800)
1248 else if (ch < 0x10000)
1253 else if (ch < 0x200000)
1258 else if (ch < 0x4000000)
1269 if (len > (_DBUS_STRING_MAX_LENGTH - real->len))
1270 return FALSE; /* real->len + len would overflow */
1272 if (!set_length (real, real->len + len))
1275 out = real->str + (real->len - len);
1277 for (i = len - 1; i > 0; --i)
1279 out[i] = (ch & 0x3f) | 0x80;
1282 out[0] = ch | first;
1286 #endif /* DBUS_BUILD_TESTS */
1289 delete (DBusRealString *real,
1296 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1298 real->str[real->len] = '\0';
1302 * Deletes a segment of a DBusString with length len starting at
1303 * start. (Hint: to clear an entire string, setting length to 0
1304 * with _dbus_string_set_length() is easier.)
1306 * @param str the DBusString
1307 * @param start where to start deleting
1308 * @param len the number of bytes to delete
1311 _dbus_string_delete (DBusString *str,
1315 DBUS_STRING_PREAMBLE (str);
1316 _dbus_assert (start >= 0);
1317 _dbus_assert (len >= 0);
1318 _dbus_assert (start <= real->len);
1319 _dbus_assert (len <= real->len - start);
1321 delete (real, start, len);
1325 copy (DBusRealString *source,
1328 DBusRealString *dest,
1334 if (!open_gap (len, dest, insert_at))
1337 memmove (dest->str + insert_at,
1338 source->str + start,
1345 * Checks assertions for two strings we're copying a segment between,
1346 * and declares real_source/real_dest variables.
1348 * @param source the source string
1349 * @param start the starting offset
1350 * @param dest the dest string
1351 * @param insert_at where the copied segment is inserted
1353 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1354 DBusRealString *real_source = (DBusRealString*) source; \
1355 DBusRealString *real_dest = (DBusRealString*) dest; \
1356 _dbus_assert ((source) != (dest)); \
1357 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1358 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1359 _dbus_assert (!real_dest->constant); \
1360 _dbus_assert (!real_dest->locked); \
1361 _dbus_assert ((start) >= 0); \
1362 _dbus_assert ((start) <= real_source->len); \
1363 _dbus_assert ((insert_at) >= 0); \
1364 _dbus_assert ((insert_at) <= real_dest->len)
1367 * Moves the end of one string into another string. Both strings
1368 * must be initialized, valid strings.
1370 * @param source the source string
1371 * @param start where to chop off the source string
1372 * @param dest the destination string
1373 * @param insert_at where to move the chopped-off part of source string
1374 * @returns #FALSE if not enough memory
1377 _dbus_string_move (DBusString *source,
1382 DBusRealString *real_source = (DBusRealString*) source;
1383 _dbus_assert (start <= real_source->len);
1385 return _dbus_string_move_len (source, start,
1386 real_source->len - start,
1391 * Like _dbus_string_move(), but does not delete the section
1392 * of the source string that's copied to the dest string.
1394 * @param source the source string
1395 * @param start where to start copying the source string
1396 * @param dest the destination string
1397 * @param insert_at where to place the copied part of source string
1398 * @returns #FALSE if not enough memory
1401 _dbus_string_copy (const DBusString *source,
1406 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1408 return copy (real_source, start,
1409 real_source->len - start,
1415 * Like _dbus_string_move(), but can move a segment from
1416 * the middle of the source string.
1418 * @param source the source string
1419 * @param start first byte of source string to move
1420 * @param len length of segment to move
1421 * @param dest the destination string
1422 * @param insert_at where to move the bytes from the source string
1423 * @returns #FALSE if not enough memory
1426 _dbus_string_move_len (DBusString *source,
1433 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1434 _dbus_assert (len >= 0);
1435 _dbus_assert ((start + len) <= real_source->len);
1442 else if (start == 0 &&
1443 len == real_source->len &&
1444 real_dest->len == 0)
1446 /* Short-circuit moving an entire existing string to an empty string
1447 * by just swapping the buffers.
1449 /* we assume ->constant doesn't matter as you can't have
1450 * a constant string involved in a move.
1452 #define ASSIGN_DATA(a, b) do { \
1453 (a)->str = (b)->str; \
1454 (a)->len = (b)->len; \
1455 (a)->allocated = (b)->allocated; \
1456 (a)->align_offset = (b)->align_offset; \
1461 ASSIGN_DATA (&tmp, real_source);
1462 ASSIGN_DATA (real_source, real_dest);
1463 ASSIGN_DATA (real_dest, &tmp);
1469 if (!copy (real_source, start, len,
1474 delete (real_source, start,
1482 * Like _dbus_string_copy(), but can copy a segment from the middle of
1483 * the source string.
1485 * @param source the source string
1486 * @param start where to start copying the source string
1487 * @param len length of segment to copy
1488 * @param dest the destination string
1489 * @param insert_at where to place the copied segment of source string
1490 * @returns #FALSE if not enough memory
1493 _dbus_string_copy_len (const DBusString *source,
1499 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1500 _dbus_assert (len >= 0);
1501 _dbus_assert (start <= real_source->len);
1502 _dbus_assert (len <= real_source->len - start);
1504 return copy (real_source, start, len,
1510 * Replaces a segment of dest string with a segment of source string.
1512 * @param source the source string
1513 * @param start where to start copying the source string
1514 * @param len length of segment to copy
1515 * @param dest the destination string
1516 * @param replace_at start of segment of dest string to replace
1517 * @param replace_len length of segment of dest string to replace
1518 * @returns #FALSE if not enough memory
1522 _dbus_string_replace_len (const DBusString *source,
1529 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1530 _dbus_assert (len >= 0);
1531 _dbus_assert (start <= real_source->len);
1532 _dbus_assert (len <= real_source->len - start);
1533 _dbus_assert (replace_at >= 0);
1534 _dbus_assert (replace_at <= real_dest->len);
1535 _dbus_assert (replace_len <= real_dest->len - replace_at);
1537 if (len == replace_len)
1539 memmove (real_dest->str + replace_at,
1540 real_source->str + start, len);
1542 else if (len < replace_len)
1544 memmove (real_dest->str + replace_at,
1545 real_source->str + start, len);
1546 delete (real_dest, replace_at + len,
1553 _dbus_assert (len > replace_len);
1555 diff = len - replace_len;
1557 /* First of all we check if destination string can be enlarged as
1558 * required, then we overwrite previous bytes
1561 if (!copy (real_source, start + replace_len, diff,
1562 real_dest, replace_at + replace_len))
1565 memmove (real_dest->str + replace_at,
1566 real_source->str + start, replace_len);
1573 * Looks for the first occurance of a byte, deletes that byte,
1574 * and moves everything after the byte to the beginning of a
1575 * separate string. Both strings must be initialized, valid
1578 * @param source the source string
1579 * @param byte the byte to remove and split the string at
1580 * @param tail the split off string
1581 * @returns #FALSE if not enough memory or if byte could not be found
1585 _dbus_string_split_on_byte (DBusString *source,
1590 char byte_string[2] = "";
1594 byte_string[0] = (char) byte;
1596 if (!_dbus_string_find (source, 0, byte_string, &byte_position))
1599 head_length = byte_position;
1600 tail_length = _dbus_string_get_length (source) - head_length - 1;
1602 if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
1606 /* remove the trailing delimiter byte from the head now.
1608 if (!_dbus_string_set_length (source, head_length))
1614 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1615 * Pennington, and Tom Tromey are the authors and authorized relicense.
1618 /** computes length and mask of a unicode character
1619 * @param Char the char
1620 * @param Mask the mask variable to assign to
1621 * @param Len the length variable to assign to
1623 #define UTF8_COMPUTE(Char, Mask, Len) \
1629 else if ((Char & 0xe0) == 0xc0) \
1634 else if ((Char & 0xf0) == 0xe0) \
1639 else if ((Char & 0xf8) == 0xf0) \
1644 else if ((Char & 0xfc) == 0xf8) \
1649 else if ((Char & 0xfe) == 0xfc) \
1661 * computes length of a unicode character in UTF-8
1662 * @param Char the char
1664 #define UTF8_LENGTH(Char) \
1665 ((Char) < 0x80 ? 1 : \
1666 ((Char) < 0x800 ? 2 : \
1667 ((Char) < 0x10000 ? 3 : \
1668 ((Char) < 0x200000 ? 4 : \
1669 ((Char) < 0x4000000 ? 5 : 6)))))
1672 * Gets a UTF-8 value.
1674 * @param Result variable for extracted unicode char.
1675 * @param Chars the bytes to decode
1676 * @param Count counter variable
1677 * @param Mask mask for this char
1678 * @param Len length for this char in bytes
1680 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1681 (Result) = (Chars)[0] & (Mask); \
1682 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1684 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1690 (Result) |= ((Chars)[(Count)] & 0x3f); \
1694 * Check whether a Unicode (5.2) char is in a valid range.
1696 * The first check comes from the Unicode guarantee to never encode
1697 * a point above 0x0010ffff, since UTF-16 couldn't represent it.
1699 * The second check covers surrogate pairs (category Cs).
1701 * The last two checks cover "Noncharacter": defined as:
1702 * "A code point that is permanently reserved for
1703 * internal use, and that should never be interchanged. In
1704 * Unicode 3.1, these consist of the values U+nFFFE and U+nFFFF
1705 * (where n is from 0 to 10_16) and the values U+FDD0..U+FDEF."
1707 * @param Char the character
1709 #define UNICODE_VALID(Char) \
1710 ((Char) < 0x110000 && \
1711 (((Char) & 0xFFFFF800) != 0xD800) && \
1712 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1713 ((Char) & 0xFFFE) != 0xFFFE)
1715 #ifdef DBUS_BUILD_TESTS
1717 * Gets a unicode character from a UTF-8 string. Does no validation;
1718 * you must verify that the string is valid UTF-8 in advance and must
1719 * pass in the start of a character.
1721 * @param str the string
1722 * @param start the start of the UTF-8 character.
1723 * @param ch_return location to return the character
1724 * @param end_return location to return the byte index of next character
1727 _dbus_string_get_unichar (const DBusString *str,
1729 dbus_unichar_t *ch_return,
1733 dbus_unichar_t result;
1736 DBUS_CONST_STRING_PREAMBLE (str);
1737 _dbus_assert (start >= 0);
1738 _dbus_assert (start <= real->len);
1743 *end_return = real->len;
1746 p = real->str + start;
1749 UTF8_COMPUTE (c, mask, len);
1752 UTF8_GET (result, p, i, mask, len);
1754 if (result == (dbus_unichar_t)-1)
1758 *ch_return = result;
1760 *end_return = start + len;
1762 #endif /* DBUS_BUILD_TESTS */
1765 * Finds the given substring in the string,
1766 * returning #TRUE and filling in the byte index
1767 * where the substring was found, if it was found.
1768 * Returns #FALSE if the substring wasn't found.
1769 * Sets *start to the length of the string if the substring
1772 * @param str the string
1773 * @param start where to start looking
1774 * @param substr the substring
1775 * @param found return location for where it was found, or #NULL
1776 * @returns #TRUE if found
1779 _dbus_string_find (const DBusString *str,
1784 return _dbus_string_find_to (str, start,
1785 ((const DBusRealString*)str)->len,
1790 * Finds end of line ("\r\n" or "\n") in the string,
1791 * returning #TRUE and filling in the byte index
1792 * where the eol string was found, if it was found.
1793 * Returns #FALSE if eol wasn't found.
1795 * @param str the string
1796 * @param start where to start looking
1797 * @param found return location for where eol was found or string length otherwise
1798 * @param found_len return length of found eol string or zero otherwise
1799 * @returns #TRUE if found
1802 _dbus_string_find_eol (const DBusString *str,
1809 DBUS_CONST_STRING_PREAMBLE (str);
1810 _dbus_assert (start <= real->len);
1811 _dbus_assert (start >= 0);
1814 while (i < real->len)
1816 if (real->str[i] == '\r')
1818 if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
1826 else /* only "\r" */
1835 else if (real->str[i] == '\n') /* only "\n" */
1856 * Finds the given substring in the string,
1857 * up to a certain position,
1858 * returning #TRUE and filling in the byte index
1859 * where the substring was found, if it was found.
1860 * Returns #FALSE if the substring wasn't found.
1861 * Sets *start to the length of the string if the substring
1864 * @param str the string
1865 * @param start where to start looking
1866 * @param end where to stop looking
1867 * @param substr the substring
1868 * @param found return location for where it was found, or #NULL
1869 * @returns #TRUE if found
1872 _dbus_string_find_to (const DBusString *str,
1879 DBUS_CONST_STRING_PREAMBLE (str);
1880 _dbus_assert (substr != NULL);
1881 _dbus_assert (start <= real->len);
1882 _dbus_assert (start >= 0);
1883 _dbus_assert (substr != NULL);
1884 _dbus_assert (end <= real->len);
1885 _dbus_assert (start <= end);
1887 /* we always "find" an empty string */
1888 if (*substr == '\0')
1898 if (real->str[i] == substr[0])
1904 if (substr[j - i] == '\0')
1906 else if (real->str[j] != substr[j - i])
1912 if (substr[j - i] == '\0')
1930 * Finds a blank (space or tab) in the string. Returns #TRUE
1931 * if found, #FALSE otherwise. If a blank is not found sets
1932 * *found to the length of the string.
1934 * @param str the string
1935 * @param start byte index to start looking
1936 * @param found place to store the location of the first blank
1937 * @returns #TRUE if a blank was found
1940 _dbus_string_find_blank (const DBusString *str,
1945 DBUS_CONST_STRING_PREAMBLE (str);
1946 _dbus_assert (start <= real->len);
1947 _dbus_assert (start >= 0);
1950 while (i < real->len)
1952 if (real->str[i] == ' ' ||
1953 real->str[i] == '\t')
1970 * Skips blanks from start, storing the first non-blank in *end
1971 * (blank is space or tab).
1973 * @param str the string
1974 * @param start where to start
1975 * @param end where to store the first non-blank byte index
1978 _dbus_string_skip_blank (const DBusString *str,
1983 DBUS_CONST_STRING_PREAMBLE (str);
1984 _dbus_assert (start <= real->len);
1985 _dbus_assert (start >= 0);
1988 while (i < real->len)
1990 if (!DBUS_IS_ASCII_BLANK (real->str[i]))
1996 _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
2004 * Skips whitespace from start, storing the first non-whitespace in *end.
2005 * (whitespace is space, tab, newline, CR).
2007 * @param str the string
2008 * @param start where to start
2009 * @param end where to store the first non-whitespace byte index
2012 _dbus_string_skip_white (const DBusString *str,
2017 DBUS_CONST_STRING_PREAMBLE (str);
2018 _dbus_assert (start <= real->len);
2019 _dbus_assert (start >= 0);
2022 while (i < real->len)
2024 if (!DBUS_IS_ASCII_WHITE (real->str[i]))
2030 _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
2037 * Skips whitespace from end, storing the start index of the trailing
2038 * whitespace in *start. (whitespace is space, tab, newline, CR).
2040 * @param str the string
2041 * @param end where to start scanning backward
2042 * @param start where to store the start of whitespace chars
2045 _dbus_string_skip_white_reverse (const DBusString *str,
2050 DBUS_CONST_STRING_PREAMBLE (str);
2051 _dbus_assert (end <= real->len);
2052 _dbus_assert (end >= 0);
2057 if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
2062 _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
2069 * Assigns a newline-terminated or \\r\\n-terminated line from the front
2070 * of the string to the given dest string. The dest string's previous
2071 * contents are deleted. If the source string contains no newline,
2072 * moves the entire source string to the dest string.
2074 * @todo owen correctly notes that this is a stupid function (it was
2075 * written purely for test code,
2076 * e.g. dbus-message-builder.c). Probably should be enforced as test
2077 * code only with ifdef DBUS_BUILD_TESTS
2079 * @param source the source string
2080 * @param dest the destination string (contents are replaced)
2081 * @returns #FALSE if no memory, or source has length 0
2084 _dbus_string_pop_line (DBusString *source,
2089 _dbus_string_set_length (dest, 0);
2093 if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
2095 _dbus_assert (eol == _dbus_string_get_length (source));
2098 /* If there's no newline and source has zero length, we're done */
2101 /* otherwise, the last line of the file has no eol characters */
2104 /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
2105 * since find_eol returned TRUE
2108 if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
2111 /* remove line ending */
2112 if (!_dbus_string_set_length (dest, eol))
2114 _dbus_assert_not_reached ("out of memory when shortening a string");
2121 #ifdef DBUS_BUILD_TESTS
2123 * Deletes up to and including the first blank space
2126 * @param str the string
2129 _dbus_string_delete_first_word (DBusString *str)
2133 if (_dbus_string_find_blank (str, 0, &i))
2134 _dbus_string_skip_blank (str, i, &i);
2136 _dbus_string_delete (str, 0, i);
2140 #ifdef DBUS_BUILD_TESTS
2142 * Deletes any leading blanks in the string
2144 * @param str the string
2147 _dbus_string_delete_leading_blanks (DBusString *str)
2151 _dbus_string_skip_blank (str, 0, &i);
2154 _dbus_string_delete (str, 0, i);
2159 * Deletes leading and trailing whitespace
2161 * @param str the string
2164 _dbus_string_chop_white(DBusString *str)
2168 _dbus_string_skip_white (str, 0, &i);
2171 _dbus_string_delete (str, 0, i);
2173 _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
2175 _dbus_string_set_length (str, i);
2179 * Tests two DBusString for equality.
2181 * @todo memcmp is probably faster
2183 * @param a first string
2184 * @param b second string
2185 * @returns #TRUE if equal
2188 _dbus_string_equal (const DBusString *a,
2189 const DBusString *b)
2191 const unsigned char *ap;
2192 const unsigned char *bp;
2193 const unsigned char *a_end;
2194 const DBusRealString *real_a = (const DBusRealString*) a;
2195 const DBusRealString *real_b = (const DBusRealString*) b;
2196 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2197 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2199 if (real_a->len != real_b->len)
2204 a_end = real_a->str + real_a->len;
2218 * Tests two DBusString for equality up to the given length.
2219 * The strings may be shorter than the given length.
2221 * @todo write a unit test
2223 * @todo memcmp is probably faster
2225 * @param a first string
2226 * @param b second string
2227 * @param len the maximum length to look at
2228 * @returns #TRUE if equal for the given number of bytes
2231 _dbus_string_equal_len (const DBusString *a,
2232 const DBusString *b,
2235 const unsigned char *ap;
2236 const unsigned char *bp;
2237 const unsigned char *a_end;
2238 const DBusRealString *real_a = (const DBusRealString*) a;
2239 const DBusRealString *real_b = (const DBusRealString*) b;
2240 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2241 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2243 if (real_a->len != real_b->len &&
2244 (real_a->len < len || real_b->len < len))
2249 a_end = real_a->str + MIN (real_a->len, len);
2263 * Tests two sub-parts of two DBusString for equality. The specified
2264 * range of the first string must exist; the specified start position
2265 * of the second string must exist.
2267 * @todo write a unit test
2269 * @todo memcmp is probably faster
2271 * @param a first string
2272 * @param a_start where to start substring in first string
2273 * @param a_len length of substring in first string
2274 * @param b second string
2275 * @param b_start where to start substring in second string
2276 * @returns #TRUE if the two substrings are equal
2279 _dbus_string_equal_substring (const DBusString *a,
2282 const DBusString *b,
2285 const unsigned char *ap;
2286 const unsigned char *bp;
2287 const unsigned char *a_end;
2288 const DBusRealString *real_a = (const DBusRealString*) a;
2289 const DBusRealString *real_b = (const DBusRealString*) b;
2290 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2291 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2292 _dbus_assert (a_start >= 0);
2293 _dbus_assert (a_len >= 0);
2294 _dbus_assert (a_start <= real_a->len);
2295 _dbus_assert (a_len <= real_a->len - a_start);
2296 _dbus_assert (b_start >= 0);
2297 _dbus_assert (b_start <= real_b->len);
2299 if (a_len > real_b->len - b_start)
2302 ap = real_a->str + a_start;
2303 bp = real_b->str + b_start;
2314 _dbus_assert (bp <= (real_b->str + real_b->len));
2320 * Checks whether a string is equal to a C string.
2322 * @param a the string
2323 * @param c_str the C string
2324 * @returns #TRUE if equal
2327 _dbus_string_equal_c_str (const DBusString *a,
2330 const unsigned char *ap;
2331 const unsigned char *bp;
2332 const unsigned char *a_end;
2333 const DBusRealString *real_a = (const DBusRealString*) a;
2334 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2335 _dbus_assert (c_str != NULL);
2338 bp = (const unsigned char*) c_str;
2339 a_end = real_a->str + real_a->len;
2340 while (ap != a_end && *bp)
2349 if (ap != a_end || *bp)
2355 #ifdef DBUS_BUILD_TESTS
2357 * Checks whether a string starts with the given C string.
2359 * @param a the string
2360 * @param c_str the C string
2361 * @returns #TRUE if string starts with it
2364 _dbus_string_starts_with_c_str (const DBusString *a,
2367 const unsigned char *ap;
2368 const unsigned char *bp;
2369 const unsigned char *a_end;
2370 const DBusRealString *real_a = (const DBusRealString*) a;
2371 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2372 _dbus_assert (c_str != NULL);
2375 bp = (const unsigned char*) c_str;
2376 a_end = real_a->str + real_a->len;
2377 while (ap != a_end && *bp)
2391 #endif /* DBUS_BUILD_TESTS */
2394 * Appends a two-character hex digit to a string, where the hex digit
2395 * has the value of the given byte.
2397 * @param str the string
2398 * @param byte the byte
2399 * @returns #FALSE if no memory
2402 _dbus_string_append_byte_as_hex (DBusString *str,
2405 const char hexdigits[16] = {
2406 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2407 'a', 'b', 'c', 'd', 'e', 'f'
2410 if (!_dbus_string_append_byte (str,
2411 hexdigits[(byte >> 4)]))
2414 if (!_dbus_string_append_byte (str,
2415 hexdigits[(byte & 0x0f)]))
2417 _dbus_string_set_length (str,
2418 _dbus_string_get_length (str) - 1);
2426 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2427 * encoded. (Each byte is two hex digits.)
2429 * @param source the string to encode
2430 * @param start byte index to start encoding
2431 * @param dest string where encoded data should be placed
2432 * @param insert_at where to place encoded data
2433 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2436 _dbus_string_hex_encode (const DBusString *source,
2442 const unsigned char *p;
2443 const unsigned char *end;
2446 _dbus_assert (start <= _dbus_string_get_length (source));
2448 if (!_dbus_string_init (&result))
2453 p = (const unsigned char*) _dbus_string_get_const_data (source);
2454 end = p + _dbus_string_get_length (source);
2459 if (!_dbus_string_append_byte_as_hex (&result, *p))
2465 if (!_dbus_string_move (&result, 0, dest, insert_at))
2471 _dbus_string_free (&result);
2476 * Decodes a string from hex encoding.
2478 * @param source the string to decode
2479 * @param start byte index to start decode
2480 * @param end_return return location of the end of the hex data, or #NULL
2481 * @param dest string where decoded data should be placed
2482 * @param insert_at where to place decoded data
2483 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2486 _dbus_string_hex_decode (const DBusString *source,
2493 const unsigned char *p;
2494 const unsigned char *end;
2496 dbus_bool_t high_bits;
2498 _dbus_assert (start <= _dbus_string_get_length (source));
2500 if (!_dbus_string_init (&result))
2506 p = (const unsigned char*) _dbus_string_get_const_data (source);
2507 end = p + _dbus_string_get_length (source);
2576 if (!_dbus_string_append_byte (&result,
2585 len = _dbus_string_get_length (&result);
2587 b = _dbus_string_get_byte (&result, len - 1);
2591 _dbus_string_set_byte (&result, len - 1, b);
2594 high_bits = !high_bits;
2600 if (!_dbus_string_move (&result, 0, dest, insert_at))
2604 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2609 _dbus_string_free (&result);
2614 * Checks that the given range of the string is valid ASCII with no
2615 * nul bytes. If the given range is not entirely contained in the
2616 * string, returns #FALSE.
2618 * @todo this is inconsistent with most of DBusString in that
2619 * it allows a start,len range that extends past the string end.
2621 * @param str the string
2622 * @param start first byte index to check
2623 * @param len number of bytes to check
2624 * @returns #TRUE if the byte range exists and is all valid ASCII
2627 _dbus_string_validate_ascii (const DBusString *str,
2631 const unsigned char *s;
2632 const unsigned char *end;
2633 DBUS_CONST_STRING_PREAMBLE (str);
2634 _dbus_assert (start >= 0);
2635 _dbus_assert (start <= real->len);
2636 _dbus_assert (len >= 0);
2638 if (len > real->len - start)
2641 s = real->str + start;
2645 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2655 * Converts the given range of the string to lower case.
2657 * @param str the string
2658 * @param start first byte index to convert
2659 * @param len number of bytes to convert
2662 _dbus_string_tolower_ascii (const DBusString *str,
2668 DBUS_STRING_PREAMBLE (str);
2669 _dbus_assert (start >= 0);
2670 _dbus_assert (start <= real->len);
2671 _dbus_assert (len >= 0);
2672 _dbus_assert (len <= real->len - start);
2674 s = real->str + start;
2679 if (*s >= 'A' && *s <= 'Z')
2686 * Converts the given range of the string to upper case.
2688 * @param str the string
2689 * @param start first byte index to convert
2690 * @param len number of bytes to convert
2693 _dbus_string_toupper_ascii (const DBusString *str,
2699 DBUS_STRING_PREAMBLE (str);
2700 _dbus_assert (start >= 0);
2701 _dbus_assert (start <= real->len);
2702 _dbus_assert (len >= 0);
2703 _dbus_assert (len <= real->len - start);
2705 s = real->str + start;
2710 if (*s >= 'a' && *s <= 'z')
2717 * Checks that the given range of the string is valid UTF-8. If the
2718 * given range is not entirely contained in the string, returns
2719 * #FALSE. If the string contains any nul bytes in the given range,
2720 * returns #FALSE. If the start and start+len are not on character
2721 * boundaries, returns #FALSE.
2723 * @todo this is inconsistent with most of DBusString in that
2724 * it allows a start,len range that extends past the string end.
2726 * @param str the string
2727 * @param start first byte index to check
2728 * @param len number of bytes to check
2729 * @returns #TRUE if the byte range exists and is all valid UTF-8
2732 _dbus_string_validate_utf8 (const DBusString *str,
2736 const unsigned char *p;
2737 const unsigned char *end;
2738 DBUS_CONST_STRING_PREAMBLE (str);
2739 _dbus_assert (start >= 0);
2740 _dbus_assert (start <= real->len);
2741 _dbus_assert (len >= 0);
2743 /* we are doing _DBUS_UNLIKELY() here which might be
2744 * dubious in a generic library like GLib, but in D-Bus
2745 * we know we're validating messages and that it would
2746 * only be evil/broken apps that would have invalid
2747 * UTF-8. Also, this function seems to be a performance
2748 * bottleneck in profiles.
2751 if (_DBUS_UNLIKELY (len > real->len - start))
2754 p = real->str + start;
2759 int i, mask, char_len;
2760 dbus_unichar_t result;
2762 /* nul bytes considered invalid */
2766 /* Special-case ASCII; this makes us go a lot faster in
2767 * D-Bus profiles where we are typically validating
2768 * function names and such. We have to know that
2769 * all following checks will pass for ASCII though,
2770 * comments follow ...
2778 UTF8_COMPUTE (*p, mask, char_len);
2780 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2783 /* check that the expected number of bytes exists in the remaining length */
2784 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2787 UTF8_GET (result, p, i, mask, char_len);
2789 /* Check for overlong UTF-8 */
2790 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2793 /* The UNICODE_VALID check below will catch this */
2794 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2798 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2801 /* UNICODE_VALID should have caught it */
2802 _dbus_assert (result != (dbus_unichar_t)-1);
2807 /* See that we covered the entire length if a length was
2810 if (_DBUS_UNLIKELY (p != end))
2817 * Checks that the given range of the string is all nul bytes. If the
2818 * given range is not entirely contained in the string, returns
2821 * @todo this is inconsistent with most of DBusString in that
2822 * it allows a start,len range that extends past the string end.
2824 * @param str the string
2825 * @param start first byte index to check
2826 * @param len number of bytes to check
2827 * @returns #TRUE if the byte range exists and is all nul bytes
2830 _dbus_string_validate_nul (const DBusString *str,
2834 const unsigned char *s;
2835 const unsigned char *end;
2836 DBUS_CONST_STRING_PREAMBLE (str);
2837 _dbus_assert (start >= 0);
2838 _dbus_assert (len >= 0);
2839 _dbus_assert (start <= real->len);
2841 if (len > real->len - start)
2844 s = real->str + start;
2848 if (_DBUS_UNLIKELY (*s != '\0'))
2857 * Clears all allocated bytes in the string to zero.
2859 * @param str the string
2862 _dbus_string_zero (DBusString *str)
2864 DBUS_STRING_PREAMBLE (str);
2866 memset (real->str - real->align_offset, '\0', real->allocated);
2870 /* tests are in dbus-string-util.c */