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 #if defined (DBUS_ENABLE_EMBEDDED_TESTS) && !defined (DBUS_DISABLE_ASSERT)
339 new_allocated = 0; /* ensure a realloc every time so that we go
340 * through all malloc failure codepaths
344 /* But be sure we always alloc at least space for the new length */
345 new_allocated = MAX (new_allocated,
346 new_length + _DBUS_STRING_ALLOCATION_PADDING);
348 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
349 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
350 if (_DBUS_UNLIKELY (new_str == NULL))
353 real->str = new_str + real->align_offset;
354 real->allocated = new_allocated;
355 fixup_alignment (real);
361 * Compacts the string to avoid wasted memory. Wasted memory is
362 * memory that is allocated but not actually required to store the
363 * current length of the string. The compact is only done if more
364 * than the given amount of memory is being wasted (otherwise the
365 * waste is ignored and the call does nothing).
367 * @param str the string
368 * @param max_waste the maximum amount of waste to ignore
369 * @returns #FALSE if the compact failed due to realloc failure
372 _dbus_string_compact (DBusString *str,
375 DBUS_STRING_PREAMBLE (str);
377 return compact (real, max_waste);
381 set_length (DBusRealString *real,
384 /* Note, we are setting the length not including nul termination */
386 /* exceeding max length is the same as failure to allocate memory */
387 if (_DBUS_UNLIKELY (new_length > _DBUS_STRING_MAX_LENGTH))
389 else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
390 _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
394 real->len = new_length;
395 real->str[new_length] = '\0';
402 DBusRealString *dest,
408 if (len > _DBUS_STRING_MAX_LENGTH - dest->len)
409 return FALSE; /* detected overflow of dest->len + len below */
411 if (!set_length (dest, dest->len + len))
414 memmove (dest->str + insert_at + len,
415 dest->str + insert_at,
416 dest->len - len - insert_at);
421 #ifndef _dbus_string_get_data
423 * Gets the raw character buffer from the string. The returned buffer
424 * will be nul-terminated, but note that strings may contain binary
425 * data so there may be extra nul characters prior to the termination.
426 * This function should be little-used, extend DBusString or add
427 * stuff to dbus-sysdeps.c instead. It's an error to use this
428 * function on a const string.
430 * @param str the string
434 _dbus_string_get_data (DBusString *str)
436 DBUS_STRING_PREAMBLE (str);
438 return (char*) real->str;
440 #endif /* _dbus_string_get_data */
442 /* only do the function if we don't have the macro */
443 #ifndef _dbus_string_get_const_data
445 * Gets the raw character buffer from a const string.
447 * @param str the string
448 * @returns the string data
451 _dbus_string_get_const_data (const DBusString *str)
453 DBUS_CONST_STRING_PREAMBLE (str);
455 return (const char*) real->str;
457 #endif /* _dbus_string_get_const_data */
460 * Gets a sub-portion of the raw character buffer from the
461 * string. The "len" field is required simply for error
462 * checking, to be sure you don't try to use more
463 * string than exists. The nul termination of the
464 * returned buffer remains at the end of the entire
465 * string, not at start + len.
467 * @param str the string
468 * @param start byte offset to return
469 * @param len length of segment to return
470 * @returns the string data
473 _dbus_string_get_data_len (DBusString *str,
477 DBUS_STRING_PREAMBLE (str);
478 _dbus_assert (start >= 0);
479 _dbus_assert (len >= 0);
480 _dbus_assert (start <= real->len);
481 _dbus_assert (len <= real->len - start);
483 return (char*) real->str + start;
486 /* only do the function if we don't have the macro */
487 #ifndef _dbus_string_get_const_data_len
489 * const version of _dbus_string_get_data_len().
491 * @param str the string
492 * @param start byte offset to return
493 * @param len length of segment to return
494 * @returns the string data
497 _dbus_string_get_const_data_len (const DBusString *str,
501 DBUS_CONST_STRING_PREAMBLE (str);
502 _dbus_assert (start >= 0);
503 _dbus_assert (len >= 0);
504 _dbus_assert (start <= real->len);
505 _dbus_assert (len <= real->len - start);
507 return (const char*) real->str + start;
509 #endif /* _dbus_string_get_const_data_len */
511 /* only do the function if we don't have the macro */
512 #ifndef _dbus_string_set_byte
514 * Sets the value of the byte at the given position.
516 * @param str the string
517 * @param i the position
518 * @param byte the new value
521 _dbus_string_set_byte (DBusString *str,
525 DBUS_STRING_PREAMBLE (str);
526 _dbus_assert (i < real->len);
527 _dbus_assert (i >= 0);
531 #endif /* _dbus_string_set_byte */
533 /* only have the function if we didn't create a macro */
534 #ifndef _dbus_string_get_byte
536 * Gets the byte at the given position. It is
537 * allowed to ask for the nul byte at the end of
540 * @param str the string
541 * @param start the position
542 * @returns the byte at that position
545 _dbus_string_get_byte (const DBusString *str,
548 DBUS_CONST_STRING_PREAMBLE (str);
549 _dbus_assert (start <= real->len);
550 _dbus_assert (start >= 0);
552 return real->str[start];
554 #endif /* _dbus_string_get_byte */
557 * Inserts a number of bytes of a given value at the
560 * @param str the string
561 * @param i the position
562 * @param n_bytes number of bytes
563 * @param byte the value to insert
564 * @returns #TRUE on success
567 _dbus_string_insert_bytes (DBusString *str,
572 DBUS_STRING_PREAMBLE (str);
573 _dbus_assert (i <= real->len);
574 _dbus_assert (i >= 0);
575 _dbus_assert (n_bytes >= 0);
580 if (!open_gap (n_bytes, real, i))
583 memset (real->str + i, byte, n_bytes);
589 * Inserts a single byte at the given position.
591 * @param str the string
592 * @param i the position
593 * @param byte the value to insert
594 * @returns #TRUE on success
597 _dbus_string_insert_byte (DBusString *str,
601 DBUS_STRING_PREAMBLE (str);
602 _dbus_assert (i <= real->len);
603 _dbus_assert (i >= 0);
605 if (!open_gap (1, real, i))
614 * Like _dbus_string_get_data(), but removes the
615 * gotten data from the original string. The caller
616 * must free the data returned. This function may
617 * fail due to lack of memory, and return #FALSE.
619 * @param str the string
620 * @param data_return location to return the buffer
621 * @returns #TRUE on success
624 _dbus_string_steal_data (DBusString *str,
627 DBUS_STRING_PREAMBLE (str);
628 _dbus_assert (data_return != NULL);
630 undo_alignment (real);
632 *data_return = (char*) real->str;
634 /* reset the string */
635 if (!_dbus_string_init (str))
637 /* hrm, put it back then */
638 real->str = (unsigned char*) *data_return;
640 fixup_alignment (real);
648 * Copies the data from the string into a char*
650 * @param str the string
651 * @param data_return place to return the data
652 * @returns #TRUE on success, #FALSE on no memory
655 _dbus_string_copy_data (const DBusString *str,
658 DBUS_CONST_STRING_PREAMBLE (str);
659 _dbus_assert (data_return != NULL);
661 *data_return = dbus_malloc (real->len + 1);
662 if (*data_return == NULL)
665 memcpy (*data_return, real->str, real->len + 1);
671 * Copies the contents of a DBusString into a different buffer. It is
672 * a bug if avail_len is too short to hold the string contents. nul
673 * termination is not copied, just the supplied bytes.
675 * @param str a string
676 * @param buffer a C buffer to copy data to
677 * @param avail_len maximum length of C buffer
680 _dbus_string_copy_to_buffer (const DBusString *str,
684 DBUS_CONST_STRING_PREAMBLE (str);
686 _dbus_assert (avail_len >= 0);
687 _dbus_assert (avail_len >= real->len);
689 memcpy (buffer, real->str, real->len);
693 * Copies the contents of a DBusString into a different buffer. It is
694 * a bug if avail_len is too short to hold the string contents plus a
697 * @param str a string
698 * @param buffer a C buffer to copy data to
699 * @param avail_len maximum length of C buffer
702 _dbus_string_copy_to_buffer_with_nul (const DBusString *str,
706 DBUS_CONST_STRING_PREAMBLE (str);
708 _dbus_assert (avail_len >= 0);
709 _dbus_assert (avail_len > real->len);
711 memcpy (buffer, real->str, real->len+1);
714 /* Only have the function if we don't have the macro */
715 #ifndef _dbus_string_get_length
717 * Gets the length of a string (not including nul termination).
719 * @returns the length.
722 _dbus_string_get_length (const DBusString *str)
724 DBUS_CONST_STRING_PREAMBLE (str);
728 #endif /* !_dbus_string_get_length */
731 * Makes a string longer by the given number of bytes. Checks whether
732 * adding additional_length to the current length would overflow an
733 * integer, and checks for exceeding a string's max length.
734 * The new bytes are not initialized, other than nul-terminating
735 * the end of the string. The uninitialized bytes may contain
736 * nul bytes or other junk.
738 * @param str a string
739 * @param additional_length length to add to the string.
740 * @returns #TRUE on success.
743 _dbus_string_lengthen (DBusString *str,
744 int additional_length)
746 DBUS_STRING_PREAMBLE (str);
747 _dbus_assert (additional_length >= 0);
749 if (_DBUS_UNLIKELY (additional_length > _DBUS_STRING_MAX_LENGTH - real->len))
750 return FALSE; /* would overflow */
752 return set_length (real,
753 real->len + additional_length);
757 * Makes a string shorter by the given number of bytes.
759 * @param str a string
760 * @param length_to_remove length to remove from the string.
763 _dbus_string_shorten (DBusString *str,
764 int length_to_remove)
766 DBUS_STRING_PREAMBLE (str);
767 _dbus_assert (length_to_remove >= 0);
768 _dbus_assert (length_to_remove <= real->len);
771 real->len - length_to_remove);
775 * Sets the length of a string. Can be used to truncate or lengthen
776 * the string. If the string is lengthened, the function may fail and
777 * return #FALSE. Newly-added bytes are not initialized, as with
778 * _dbus_string_lengthen().
780 * @param str a string
781 * @param length new length of the string.
782 * @returns #FALSE on failure.
785 _dbus_string_set_length (DBusString *str,
788 DBUS_STRING_PREAMBLE (str);
789 _dbus_assert (length >= 0);
791 return set_length (real, length);
795 align_insert_point_then_open_gap (DBusString *str,
800 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
801 unsigned long gap_pos;
804 DBUS_STRING_PREAMBLE (str);
805 _dbus_assert (alignment >= 1);
806 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
808 insert_at = *insert_at_p;
810 _dbus_assert (insert_at <= real->len);
812 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
813 new_len = real->len + (gap_pos - insert_at) + gap_size;
815 if (_DBUS_UNLIKELY (new_len > (unsigned long) _DBUS_STRING_MAX_LENGTH))
818 delta = new_len - real->len;
819 _dbus_assert (delta >= 0);
821 if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
823 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
827 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
831 /* nul the padding if we had to add any padding */
832 if (gap_size < delta)
834 memset (&real->str[insert_at], '\0',
835 gap_pos - insert_at);
838 *insert_at_p = gap_pos;
844 align_length_then_lengthen (DBusString *str,
846 int then_lengthen_by)
850 insert_at = _dbus_string_get_length (str);
852 return align_insert_point_then_open_gap (str,
854 alignment, then_lengthen_by);
858 * Align the length of a string to a specific alignment (typically 4 or 8)
859 * by appending nul bytes to the string.
861 * @param str a string
862 * @param alignment the alignment
863 * @returns #FALSE if no memory
866 _dbus_string_align_length (DBusString *str,
869 return align_length_then_lengthen (str, alignment, 0);
873 * Preallocate extra_bytes such that a future lengthening of the
874 * string by extra_bytes is guaranteed to succeed without an out of
877 * @param str a string
878 * @param extra_bytes bytes to alloc
879 * @returns #FALSE if no memory
882 _dbus_string_alloc_space (DBusString *str,
885 if (!_dbus_string_lengthen (str, extra_bytes))
887 _dbus_string_shorten (str, extra_bytes);
893 append (DBusRealString *real,
900 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
903 memcpy (real->str + (real->len - buffer_len),
911 * Appends a nul-terminated C-style string to a DBusString.
913 * @param str the DBusString
914 * @param buffer the nul-terminated characters to append
915 * @returns #FALSE if not enough memory.
918 _dbus_string_append (DBusString *str,
921 unsigned long buffer_len;
923 DBUS_STRING_PREAMBLE (str);
924 _dbus_assert (buffer != NULL);
926 buffer_len = strlen (buffer);
927 if (buffer_len > (unsigned long) _DBUS_STRING_MAX_LENGTH)
930 return append (real, buffer, buffer_len);
933 /** assign 2 bytes from one string to another */
934 #define ASSIGN_2_OCTETS(p, octets) \
935 *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
937 /** assign 4 bytes from one string to another */
938 #define ASSIGN_4_OCTETS(p, octets) \
939 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
941 #ifdef DBUS_HAVE_INT64
942 /** assign 8 bytes from one string to another */
943 #define ASSIGN_8_OCTETS(p, octets) \
944 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
946 /** assign 8 bytes from one string to another */
947 #define ASSIGN_8_OCTETS(p, octets) \
961 _dbus_assert (b == p + 8); \
963 #endif /* DBUS_HAVE_INT64 */
966 * Inserts 2 bytes aligned on a 2 byte boundary
967 * with any alignment padding initialized to 0.
969 * @param str the DBusString
970 * @param insert_at where to insert
971 * @param octets 2 bytes to insert
972 * @returns #FALSE if not enough memory.
975 _dbus_string_insert_2_aligned (DBusString *str,
977 const unsigned char octets[2])
979 DBUS_STRING_PREAMBLE (str);
981 if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
984 ASSIGN_2_OCTETS (real->str + insert_at, octets);
990 * Inserts 4 bytes aligned on a 4 byte boundary
991 * with any alignment padding initialized to 0.
993 * @param str the DBusString
994 * @param insert_at where to insert
995 * @param octets 4 bytes to insert
996 * @returns #FALSE if not enough memory.
999 _dbus_string_insert_4_aligned (DBusString *str,
1001 const unsigned char octets[4])
1003 DBUS_STRING_PREAMBLE (str);
1005 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1008 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1014 * Inserts 8 bytes aligned on an 8 byte boundary
1015 * with any alignment padding initialized to 0.
1017 * @param str the DBusString
1018 * @param insert_at where to insert
1019 * @param octets 8 bytes to insert
1020 * @returns #FALSE if not enough memory.
1023 _dbus_string_insert_8_aligned (DBusString *str,
1025 const unsigned char octets[8])
1027 DBUS_STRING_PREAMBLE (str);
1029 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1032 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1034 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1041 * Inserts padding at *insert_at such to align it to the given
1042 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1043 * to the aligned position.
1045 * @param str the DBusString
1046 * @param insert_at location to be aligned
1047 * @param alignment alignment boundary (1, 2, 4, or 8)
1048 * @returns #FALSE if not enough memory.
1051 _dbus_string_insert_alignment (DBusString *str,
1055 DBUS_STRING_PREAMBLE (str);
1057 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1060 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1066 * Appends a printf-style formatted string
1067 * to the #DBusString.
1069 * @param str the string
1070 * @param format printf format
1071 * @param args variable argument list
1072 * @returns #FALSE if no memory
1075 _dbus_string_append_printf_valist (DBusString *str,
1082 DBUS_STRING_PREAMBLE (str);
1084 DBUS_VA_COPY (args_copy, args);
1086 /* Measure the message length without terminating nul */
1087 len = _dbus_printf_string_upper_bound (format, args);
1092 if (!_dbus_string_lengthen (str, len))
1094 /* don't leak the copy */
1099 vsprintf ((char*) (real->str + (real->len - len)),
1108 * Appends a printf-style formatted string
1109 * to the #DBusString.
1111 * @param str the string
1112 * @param format printf format
1113 * @returns #FALSE if no memory
1116 _dbus_string_append_printf (DBusString *str,
1123 va_start (args, format);
1124 retval = _dbus_string_append_printf_valist (str, format, args);
1131 * Appends block of bytes with the given length to a DBusString.
1133 * @param str the DBusString
1134 * @param buffer the bytes to append
1135 * @param len the number of bytes to append
1136 * @returns #FALSE if not enough memory.
1139 _dbus_string_append_len (DBusString *str,
1143 DBUS_STRING_PREAMBLE (str);
1144 _dbus_assert (buffer != NULL);
1145 _dbus_assert (len >= 0);
1147 return append (real, buffer, len);
1151 * Appends a single byte to the string, returning #FALSE
1152 * if not enough memory.
1154 * @param str the string
1155 * @param byte the byte to append
1156 * @returns #TRUE on success
1159 _dbus_string_append_byte (DBusString *str,
1162 DBUS_STRING_PREAMBLE (str);
1164 if (!set_length (real, real->len + 1))
1167 real->str[real->len-1] = byte;
1173 delete (DBusRealString *real,
1180 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1182 real->str[real->len] = '\0';
1186 * Deletes a segment of a DBusString with length len starting at
1187 * start. (Hint: to clear an entire string, setting length to 0
1188 * with _dbus_string_set_length() is easier.)
1190 * @param str the DBusString
1191 * @param start where to start deleting
1192 * @param len the number of bytes to delete
1195 _dbus_string_delete (DBusString *str,
1199 DBUS_STRING_PREAMBLE (str);
1200 _dbus_assert (start >= 0);
1201 _dbus_assert (len >= 0);
1202 _dbus_assert (start <= real->len);
1203 _dbus_assert (len <= real->len - start);
1205 delete (real, start, len);
1209 copy (DBusRealString *source,
1212 DBusRealString *dest,
1218 if (!open_gap (len, dest, insert_at))
1221 memmove (dest->str + insert_at,
1222 source->str + start,
1229 * Checks assertions for two strings we're copying a segment between,
1230 * and declares real_source/real_dest variables.
1232 * @param source the source string
1233 * @param start the starting offset
1234 * @param dest the dest string
1235 * @param insert_at where the copied segment is inserted
1237 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1238 DBusRealString *real_source = (DBusRealString*) source; \
1239 DBusRealString *real_dest = (DBusRealString*) dest; \
1240 _dbus_assert ((source) != (dest)); \
1241 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1242 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1243 _dbus_assert (!real_dest->constant); \
1244 _dbus_assert (!real_dest->locked); \
1245 _dbus_assert ((start) >= 0); \
1246 _dbus_assert ((start) <= real_source->len); \
1247 _dbus_assert ((insert_at) >= 0); \
1248 _dbus_assert ((insert_at) <= real_dest->len)
1251 * Moves the end of one string into another string. Both strings
1252 * must be initialized, valid strings.
1254 * @param source the source string
1255 * @param start where to chop off the source string
1256 * @param dest the destination string
1257 * @param insert_at where to move the chopped-off part of source string
1258 * @returns #FALSE if not enough memory
1261 _dbus_string_move (DBusString *source,
1266 DBusRealString *real_source = (DBusRealString*) source;
1267 _dbus_assert (start <= real_source->len);
1269 return _dbus_string_move_len (source, start,
1270 real_source->len - start,
1275 * Like _dbus_string_move(), but does not delete the section
1276 * of the source string that's copied to the dest string.
1278 * @param source the source string
1279 * @param start where to start copying the source string
1280 * @param dest the destination string
1281 * @param insert_at where to place the copied part of source string
1282 * @returns #FALSE if not enough memory
1285 _dbus_string_copy (const DBusString *source,
1290 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1292 return copy (real_source, start,
1293 real_source->len - start,
1299 * Like _dbus_string_move(), but can move a segment from
1300 * the middle of the source string.
1302 * @param source the source string
1303 * @param start first byte of source string to move
1304 * @param len length of segment to move
1305 * @param dest the destination string
1306 * @param insert_at where to move the bytes from the source string
1307 * @returns #FALSE if not enough memory
1310 _dbus_string_move_len (DBusString *source,
1317 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1318 _dbus_assert (len >= 0);
1319 _dbus_assert ((start + len) <= real_source->len);
1326 else if (start == 0 &&
1327 len == real_source->len &&
1328 real_dest->len == 0)
1330 /* Short-circuit moving an entire existing string to an empty string
1331 * by just swapping the buffers.
1333 /* we assume ->constant doesn't matter as you can't have
1334 * a constant string involved in a move.
1336 #define ASSIGN_DATA(a, b) do { \
1337 (a)->str = (b)->str; \
1338 (a)->len = (b)->len; \
1339 (a)->allocated = (b)->allocated; \
1340 (a)->align_offset = (b)->align_offset; \
1345 ASSIGN_DATA (&tmp, real_source);
1346 ASSIGN_DATA (real_source, real_dest);
1347 ASSIGN_DATA (real_dest, &tmp);
1353 if (!copy (real_source, start, len,
1358 delete (real_source, start,
1366 * Like _dbus_string_copy(), but can copy a segment from the middle of
1367 * the source string.
1369 * @param source the source string
1370 * @param start where to start copying the source string
1371 * @param len length of segment to copy
1372 * @param dest the destination string
1373 * @param insert_at where to place the copied segment of source string
1374 * @returns #FALSE if not enough memory
1377 _dbus_string_copy_len (const DBusString *source,
1383 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1384 _dbus_assert (len >= 0);
1385 _dbus_assert (start <= real_source->len);
1386 _dbus_assert (len <= real_source->len - start);
1388 return copy (real_source, start, len,
1394 * Replaces a segment of dest string with a segment of source string.
1396 * @param source the source string
1397 * @param start where to start copying the source string
1398 * @param len length of segment to copy
1399 * @param dest the destination string
1400 * @param replace_at start of segment of dest string to replace
1401 * @param replace_len length of segment of dest string to replace
1402 * @returns #FALSE if not enough memory
1406 _dbus_string_replace_len (const DBusString *source,
1413 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1414 _dbus_assert (len >= 0);
1415 _dbus_assert (start <= real_source->len);
1416 _dbus_assert (len <= real_source->len - start);
1417 _dbus_assert (replace_at >= 0);
1418 _dbus_assert (replace_at <= real_dest->len);
1419 _dbus_assert (replace_len <= real_dest->len - replace_at);
1421 if (len == replace_len)
1423 memmove (real_dest->str + replace_at,
1424 real_source->str + start, len);
1426 else if (len < replace_len)
1428 memmove (real_dest->str + replace_at,
1429 real_source->str + start, len);
1430 delete (real_dest, replace_at + len,
1437 _dbus_assert (len > replace_len);
1439 diff = len - replace_len;
1441 /* First of all we check if destination string can be enlarged as
1442 * required, then we overwrite previous bytes
1445 if (!copy (real_source, start + replace_len, diff,
1446 real_dest, replace_at + replace_len))
1449 memmove (real_dest->str + replace_at,
1450 real_source->str + start, replace_len);
1457 * Looks for the first occurance of a byte, deletes that byte,
1458 * and moves everything after the byte to the beginning of a
1459 * separate string. Both strings must be initialized, valid
1462 * @param source the source string
1463 * @param byte the byte to remove and split the string at
1464 * @param tail the split off string
1465 * @returns #FALSE if not enough memory or if byte could not be found
1469 _dbus_string_split_on_byte (DBusString *source,
1474 char byte_string[2] = "";
1478 byte_string[0] = (char) byte;
1480 if (!_dbus_string_find (source, 0, byte_string, &byte_position))
1483 head_length = byte_position;
1484 tail_length = _dbus_string_get_length (source) - head_length - 1;
1486 if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
1490 /* remove the trailing delimiter byte from the head now.
1492 if (!_dbus_string_set_length (source, head_length))
1498 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1499 * Pennington, and Tom Tromey are the authors and authorized relicense.
1502 /** computes length and mask of a unicode character
1503 * @param Char the char
1504 * @param Mask the mask variable to assign to
1505 * @param Len the length variable to assign to
1507 #define UTF8_COMPUTE(Char, Mask, Len) \
1513 else if ((Char & 0xe0) == 0xc0) \
1518 else if ((Char & 0xf0) == 0xe0) \
1523 else if ((Char & 0xf8) == 0xf0) \
1528 else if ((Char & 0xfc) == 0xf8) \
1533 else if ((Char & 0xfe) == 0xfc) \
1545 * computes length of a unicode character in UTF-8
1546 * @param Char the char
1548 #define UTF8_LENGTH(Char) \
1549 ((Char) < 0x80 ? 1 : \
1550 ((Char) < 0x800 ? 2 : \
1551 ((Char) < 0x10000 ? 3 : \
1552 ((Char) < 0x200000 ? 4 : \
1553 ((Char) < 0x4000000 ? 5 : 6)))))
1556 * Gets a UTF-8 value.
1558 * @param Result variable for extracted unicode char.
1559 * @param Chars the bytes to decode
1560 * @param Count counter variable
1561 * @param Mask mask for this char
1562 * @param Len length for this char in bytes
1564 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1565 (Result) = (Chars)[0] & (Mask); \
1566 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1568 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1574 (Result) |= ((Chars)[(Count)] & 0x3f); \
1578 * Check whether a Unicode (5.2) char is in a valid range.
1580 * The first check comes from the Unicode guarantee to never encode
1581 * a point above 0x0010ffff, since UTF-16 couldn't represent it.
1583 * The second check covers surrogate pairs (category Cs).
1585 * @param Char the character
1587 #define UNICODE_VALID(Char) \
1588 ((Char) < 0x110000 && \
1589 (((Char) & 0xFFFFF800) != 0xD800))
1592 * Finds the given substring in the string,
1593 * returning #TRUE and filling in the byte index
1594 * where the substring was found, if it was found.
1595 * Returns #FALSE if the substring wasn't found.
1596 * Sets *start to the length of the string if the substring
1599 * @param str the string
1600 * @param start where to start looking
1601 * @param substr the substring
1602 * @param found return location for where it was found, or #NULL
1603 * @returns #TRUE if found
1606 _dbus_string_find (const DBusString *str,
1611 return _dbus_string_find_to (str, start,
1612 ((const DBusRealString*)str)->len,
1617 * Finds end of line ("\r\n" or "\n") in the string,
1618 * returning #TRUE and filling in the byte index
1619 * where the eol string was found, if it was found.
1620 * Returns #FALSE if eol wasn't found.
1622 * @param str the string
1623 * @param start where to start looking
1624 * @param found return location for where eol was found or string length otherwise
1625 * @param found_len return length of found eol string or zero otherwise
1626 * @returns #TRUE if found
1629 _dbus_string_find_eol (const DBusString *str,
1636 DBUS_CONST_STRING_PREAMBLE (str);
1637 _dbus_assert (start <= real->len);
1638 _dbus_assert (start >= 0);
1641 while (i < real->len)
1643 if (real->str[i] == '\r')
1645 if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
1653 else /* only "\r" */
1662 else if (real->str[i] == '\n') /* only "\n" */
1683 * Finds the given substring in the string,
1684 * up to a certain position,
1685 * returning #TRUE and filling in the byte index
1686 * where the substring was found, if it was found.
1687 * Returns #FALSE if the substring wasn't found.
1688 * Sets *start to the length of the string if the substring
1691 * @param str the string
1692 * @param start where to start looking
1693 * @param end where to stop looking
1694 * @param substr the substring
1695 * @param found return location for where it was found, or #NULL
1696 * @returns #TRUE if found
1699 _dbus_string_find_to (const DBusString *str,
1706 DBUS_CONST_STRING_PREAMBLE (str);
1707 _dbus_assert (substr != NULL);
1708 _dbus_assert (start <= real->len);
1709 _dbus_assert (start >= 0);
1710 _dbus_assert (substr != NULL);
1711 _dbus_assert (end <= real->len);
1712 _dbus_assert (start <= end);
1714 /* we always "find" an empty string */
1715 if (*substr == '\0')
1725 if (real->str[i] == substr[0])
1731 if (substr[j - i] == '\0')
1733 else if (real->str[j] != substr[j - i])
1739 if (substr[j - i] == '\0')
1757 * Finds a blank (space or tab) in the string. Returns #TRUE
1758 * if found, #FALSE otherwise. If a blank is not found sets
1759 * *found to the length of the string.
1761 * @param str the string
1762 * @param start byte index to start looking
1763 * @param found place to store the location of the first blank
1764 * @returns #TRUE if a blank was found
1767 _dbus_string_find_blank (const DBusString *str,
1772 DBUS_CONST_STRING_PREAMBLE (str);
1773 _dbus_assert (start <= real->len);
1774 _dbus_assert (start >= 0);
1777 while (i < real->len)
1779 if (real->str[i] == ' ' ||
1780 real->str[i] == '\t')
1797 * Skips blanks from start, storing the first non-blank in *end
1798 * (blank is space or tab).
1800 * @param str the string
1801 * @param start where to start
1802 * @param end where to store the first non-blank byte index
1805 _dbus_string_skip_blank (const DBusString *str,
1810 DBUS_CONST_STRING_PREAMBLE (str);
1811 _dbus_assert (start <= real->len);
1812 _dbus_assert (start >= 0);
1815 while (i < real->len)
1817 if (!DBUS_IS_ASCII_BLANK (real->str[i]))
1823 _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
1831 * Skips whitespace from start, storing the first non-whitespace in *end.
1832 * (whitespace is space, tab, newline, CR).
1834 * @param str the string
1835 * @param start where to start
1836 * @param end where to store the first non-whitespace byte index
1839 _dbus_string_skip_white (const DBusString *str,
1844 DBUS_CONST_STRING_PREAMBLE (str);
1845 _dbus_assert (start <= real->len);
1846 _dbus_assert (start >= 0);
1849 while (i < real->len)
1851 if (!DBUS_IS_ASCII_WHITE (real->str[i]))
1857 _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
1864 * Skips whitespace from end, storing the start index of the trailing
1865 * whitespace in *start. (whitespace is space, tab, newline, CR).
1867 * @param str the string
1868 * @param end where to start scanning backward
1869 * @param start where to store the start of whitespace chars
1872 _dbus_string_skip_white_reverse (const DBusString *str,
1877 DBUS_CONST_STRING_PREAMBLE (str);
1878 _dbus_assert (end <= real->len);
1879 _dbus_assert (end >= 0);
1884 if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
1889 _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
1896 * Assigns a newline-terminated or \\r\\n-terminated line from the front
1897 * of the string to the given dest string. The dest string's previous
1898 * contents are deleted. If the source string contains no newline,
1899 * moves the entire source string to the dest string.
1901 * @todo owen correctly notes that this is a stupid function (it was
1902 * written purely for test code,
1903 * e.g. dbus-message-builder.c). Probably should be enforced as test
1904 * code only with ifdef DBUS_ENABLE_EMBEDDED_TESTS
1906 * @param source the source string
1907 * @param dest the destination string (contents are replaced)
1908 * @returns #FALSE if no memory, or source has length 0
1911 _dbus_string_pop_line (DBusString *source,
1916 _dbus_string_set_length (dest, 0);
1920 if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
1922 _dbus_assert (eol == _dbus_string_get_length (source));
1925 /* If there's no newline and source has zero length, we're done */
1928 /* otherwise, the last line of the file has no eol characters */
1931 /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
1932 * since find_eol returned TRUE
1935 if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
1938 /* remove line ending */
1939 if (!_dbus_string_set_length (dest, eol))
1941 _dbus_assert_not_reached ("out of memory when shortening a string");
1948 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1950 * Deletes up to and including the first blank space
1953 * @param str the string
1956 _dbus_string_delete_first_word (DBusString *str)
1960 if (_dbus_string_find_blank (str, 0, &i))
1961 _dbus_string_skip_blank (str, i, &i);
1963 _dbus_string_delete (str, 0, i);
1967 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1969 * Deletes any leading blanks in the string
1971 * @param str the string
1974 _dbus_string_delete_leading_blanks (DBusString *str)
1978 _dbus_string_skip_blank (str, 0, &i);
1981 _dbus_string_delete (str, 0, i);
1986 * Deletes leading and trailing whitespace
1988 * @param str the string
1991 _dbus_string_chop_white(DBusString *str)
1995 _dbus_string_skip_white (str, 0, &i);
1998 _dbus_string_delete (str, 0, i);
2000 _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
2002 _dbus_string_set_length (str, i);
2006 * Tests two DBusString for equality.
2008 * @todo memcmp is probably faster
2010 * @param a first string
2011 * @param b second string
2012 * @returns #TRUE if equal
2015 _dbus_string_equal (const DBusString *a,
2016 const DBusString *b)
2018 const unsigned char *ap;
2019 const unsigned char *bp;
2020 const unsigned char *a_end;
2021 const DBusRealString *real_a = (const DBusRealString*) a;
2022 const DBusRealString *real_b = (const DBusRealString*) b;
2023 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2024 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2026 if (real_a->len != real_b->len)
2031 a_end = real_a->str + real_a->len;
2045 * Tests two DBusString for equality up to the given length.
2046 * The strings may be shorter than the given length.
2048 * @todo write a unit test
2050 * @todo memcmp is probably faster
2052 * @param a first string
2053 * @param b second string
2054 * @param len the maximum length to look at
2055 * @returns #TRUE if equal for the given number of bytes
2058 _dbus_string_equal_len (const DBusString *a,
2059 const DBusString *b,
2062 const unsigned char *ap;
2063 const unsigned char *bp;
2064 const unsigned char *a_end;
2065 const DBusRealString *real_a = (const DBusRealString*) a;
2066 const DBusRealString *real_b = (const DBusRealString*) b;
2067 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2068 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2070 if (real_a->len != real_b->len &&
2071 (real_a->len < len || real_b->len < len))
2076 a_end = real_a->str + MIN (real_a->len, len);
2090 * Tests two sub-parts of two DBusString for equality. The specified
2091 * range of the first string must exist; the specified start position
2092 * of the second string must exist.
2094 * @todo write a unit test
2096 * @todo memcmp is probably faster
2098 * @param a first string
2099 * @param a_start where to start substring in first string
2100 * @param a_len length of substring in first string
2101 * @param b second string
2102 * @param b_start where to start substring in second string
2103 * @returns #TRUE if the two substrings are equal
2106 _dbus_string_equal_substring (const DBusString *a,
2109 const DBusString *b,
2112 const unsigned char *ap;
2113 const unsigned char *bp;
2114 const unsigned char *a_end;
2115 const DBusRealString *real_a = (const DBusRealString*) a;
2116 const DBusRealString *real_b = (const DBusRealString*) b;
2117 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2118 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2119 _dbus_assert (a_start >= 0);
2120 _dbus_assert (a_len >= 0);
2121 _dbus_assert (a_start <= real_a->len);
2122 _dbus_assert (a_len <= real_a->len - a_start);
2123 _dbus_assert (b_start >= 0);
2124 _dbus_assert (b_start <= real_b->len);
2126 if (a_len > real_b->len - b_start)
2129 ap = real_a->str + a_start;
2130 bp = real_b->str + b_start;
2141 _dbus_assert (bp <= (real_b->str + real_b->len));
2147 * Checks whether a string is equal to a C string.
2149 * @param a the string
2150 * @param c_str the C string
2151 * @returns #TRUE if equal
2154 _dbus_string_equal_c_str (const DBusString *a,
2157 const unsigned char *ap;
2158 const unsigned char *bp;
2159 const unsigned char *a_end;
2160 const DBusRealString *real_a = (const DBusRealString*) a;
2161 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2162 _dbus_assert (c_str != NULL);
2165 bp = (const unsigned char*) c_str;
2166 a_end = real_a->str + real_a->len;
2167 while (ap != a_end && *bp)
2176 if (ap != a_end || *bp)
2183 * Checks whether a string starts with the given C string.
2185 * @param a the string
2186 * @param c_str the C string
2187 * @returns #TRUE if string starts with it
2190 _dbus_string_starts_with_c_str (const DBusString *a,
2193 const unsigned char *ap;
2194 const unsigned char *bp;
2195 const unsigned char *a_end;
2196 const DBusRealString *real_a = (const DBusRealString*) a;
2197 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2198 _dbus_assert (c_str != NULL);
2201 bp = (const unsigned char*) c_str;
2202 a_end = real_a->str + real_a->len;
2203 while (ap != a_end && *bp)
2219 * Appends a two-character hex digit to a string, where the hex digit
2220 * has the value of the given byte.
2222 * @param str the string
2223 * @param byte the byte
2224 * @returns #FALSE if no memory
2227 _dbus_string_append_byte_as_hex (DBusString *str,
2230 const char hexdigits[16] = {
2231 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2232 'a', 'b', 'c', 'd', 'e', 'f'
2235 if (!_dbus_string_append_byte (str,
2236 hexdigits[(byte >> 4)]))
2239 if (!_dbus_string_append_byte (str,
2240 hexdigits[(byte & 0x0f)]))
2242 _dbus_string_set_length (str,
2243 _dbus_string_get_length (str) - 1);
2251 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2252 * encoded. (Each byte is two hex digits.)
2254 * @param source the string to encode
2255 * @param start byte index to start encoding
2256 * @param dest string where encoded data should be placed
2257 * @param insert_at where to place encoded data
2258 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2261 _dbus_string_hex_encode (const DBusString *source,
2267 const unsigned char *p;
2268 const unsigned char *end;
2271 _dbus_assert (start <= _dbus_string_get_length (source));
2273 if (!_dbus_string_init (&result))
2278 p = (const unsigned char*) _dbus_string_get_const_data (source);
2279 end = p + _dbus_string_get_length (source);
2284 if (!_dbus_string_append_byte_as_hex (&result, *p))
2290 if (!_dbus_string_move (&result, 0, dest, insert_at))
2296 _dbus_string_free (&result);
2301 * Decodes a string from hex encoding.
2303 * @param source the string to decode
2304 * @param start byte index to start decode
2305 * @param end_return return location of the end of the hex data, or #NULL
2306 * @param dest string where decoded data should be placed
2307 * @param insert_at where to place decoded data
2308 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2311 _dbus_string_hex_decode (const DBusString *source,
2318 const unsigned char *p;
2319 const unsigned char *end;
2321 dbus_bool_t high_bits;
2323 _dbus_assert (start <= _dbus_string_get_length (source));
2325 if (!_dbus_string_init (&result))
2331 p = (const unsigned char*) _dbus_string_get_const_data (source);
2332 end = p + _dbus_string_get_length (source);
2401 if (!_dbus_string_append_byte (&result,
2410 len = _dbus_string_get_length (&result);
2412 b = _dbus_string_get_byte (&result, len - 1);
2416 _dbus_string_set_byte (&result, len - 1, b);
2419 high_bits = !high_bits;
2425 if (!_dbus_string_move (&result, 0, dest, insert_at))
2429 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2434 _dbus_string_free (&result);
2439 * Checks that the given range of the string is valid ASCII with no
2440 * nul bytes. If the given range is not entirely contained in the
2441 * string, returns #FALSE.
2443 * @todo this is inconsistent with most of DBusString in that
2444 * it allows a start,len range that extends past the string end.
2446 * @param str the string
2447 * @param start first byte index to check
2448 * @param len number of bytes to check
2449 * @returns #TRUE if the byte range exists and is all valid ASCII
2452 _dbus_string_validate_ascii (const DBusString *str,
2456 const unsigned char *s;
2457 const unsigned char *end;
2458 DBUS_CONST_STRING_PREAMBLE (str);
2459 _dbus_assert (start >= 0);
2460 _dbus_assert (start <= real->len);
2461 _dbus_assert (len >= 0);
2463 if (len > real->len - start)
2466 s = real->str + start;
2470 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2480 * Converts the given range of the string to lower case.
2482 * @param str the string
2483 * @param start first byte index to convert
2484 * @param len number of bytes to convert
2487 _dbus_string_tolower_ascii (const DBusString *str,
2493 DBUS_STRING_PREAMBLE (str);
2494 _dbus_assert (start >= 0);
2495 _dbus_assert (start <= real->len);
2496 _dbus_assert (len >= 0);
2497 _dbus_assert (len <= real->len - start);
2499 s = real->str + start;
2504 if (*s >= 'A' && *s <= 'Z')
2511 * Converts the given range of the string to upper case.
2513 * @param str the string
2514 * @param start first byte index to convert
2515 * @param len number of bytes to convert
2518 _dbus_string_toupper_ascii (const DBusString *str,
2524 DBUS_STRING_PREAMBLE (str);
2525 _dbus_assert (start >= 0);
2526 _dbus_assert (start <= real->len);
2527 _dbus_assert (len >= 0);
2528 _dbus_assert (len <= real->len - start);
2530 s = real->str + start;
2535 if (*s >= 'a' && *s <= 'z')
2542 * Checks that the given range of the string is valid UTF-8. If the
2543 * given range is not entirely contained in the string, returns
2544 * #FALSE. If the string contains any nul bytes in the given range,
2545 * returns #FALSE. If the start and start+len are not on character
2546 * boundaries, returns #FALSE.
2548 * @todo this is inconsistent with most of DBusString in that
2549 * it allows a start,len range that extends past the string end.
2551 * @param str the string
2552 * @param start first byte index to check
2553 * @param len number of bytes to check
2554 * @returns #TRUE if the byte range exists and is all valid UTF-8
2557 _dbus_string_validate_utf8 (const DBusString *str,
2561 const unsigned char *p;
2562 const unsigned char *end;
2563 DBUS_CONST_STRING_PREAMBLE (str);
2564 _dbus_assert (start >= 0);
2565 _dbus_assert (start <= real->len);
2566 _dbus_assert (len >= 0);
2568 /* we are doing _DBUS_UNLIKELY() here which might be
2569 * dubious in a generic library like GLib, but in D-Bus
2570 * we know we're validating messages and that it would
2571 * only be evil/broken apps that would have invalid
2572 * UTF-8. Also, this function seems to be a performance
2573 * bottleneck in profiles.
2576 if (_DBUS_UNLIKELY (len > real->len - start))
2579 p = real->str + start;
2584 int i, mask, char_len;
2585 dbus_unichar_t result;
2587 /* nul bytes considered invalid */
2591 /* Special-case ASCII; this makes us go a lot faster in
2592 * D-Bus profiles where we are typically validating
2593 * function names and such. We have to know that
2594 * all following checks will pass for ASCII though,
2595 * comments follow ...
2603 UTF8_COMPUTE (*p, mask, char_len);
2605 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2608 /* check that the expected number of bytes exists in the remaining length */
2609 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2612 UTF8_GET (result, p, i, mask, char_len);
2614 /* Check for overlong UTF-8 */
2615 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2618 /* The UNICODE_VALID check below will catch this */
2619 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2623 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2626 /* UNICODE_VALID should have caught it */
2627 _dbus_assert (result != (dbus_unichar_t)-1);
2632 /* See that we covered the entire length if a length was
2635 if (_DBUS_UNLIKELY (p != end))
2642 * Checks that the given range of the string is all nul bytes. If the
2643 * given range is not entirely contained in the string, returns
2646 * @todo this is inconsistent with most of DBusString in that
2647 * it allows a start,len range that extends past the string end.
2649 * @param str the string
2650 * @param start first byte index to check
2651 * @param len number of bytes to check
2652 * @returns #TRUE if the byte range exists and is all nul bytes
2655 _dbus_string_validate_nul (const DBusString *str,
2659 const unsigned char *s;
2660 const unsigned char *end;
2661 DBUS_CONST_STRING_PREAMBLE (str);
2662 _dbus_assert (start >= 0);
2663 _dbus_assert (len >= 0);
2664 _dbus_assert (start <= real->len);
2666 if (len > real->len - start)
2669 s = real->str + start;
2673 if (_DBUS_UNLIKELY (*s != '\0'))
2682 * Clears all allocated bytes in the string to zero.
2684 * @param str the string
2687 _dbus_string_zero (DBusString *str)
2689 DBUS_STRING_PREAMBLE (str);
2691 memset (real->str - real->align_offset, '\0', real->allocated);
2695 /* tests are in dbus-string-util.c */