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 /** assign 8 bytes from one string to another */
942 #define ASSIGN_8_OCTETS(p, octets) \
943 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
946 * Inserts 2 bytes aligned on a 2 byte boundary
947 * with any alignment padding initialized to 0.
949 * @param str the DBusString
950 * @param insert_at where to insert
951 * @param octets 2 bytes to insert
952 * @returns #FALSE if not enough memory.
955 _dbus_string_insert_2_aligned (DBusString *str,
957 const unsigned char octets[2])
959 DBUS_STRING_PREAMBLE (str);
961 if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
964 ASSIGN_2_OCTETS (real->str + insert_at, octets);
970 * Inserts 4 bytes aligned on a 4 byte boundary
971 * with any alignment padding initialized to 0.
973 * @param str the DBusString
974 * @param insert_at where to insert
975 * @param octets 4 bytes to insert
976 * @returns #FALSE if not enough memory.
979 _dbus_string_insert_4_aligned (DBusString *str,
981 const unsigned char octets[4])
983 DBUS_STRING_PREAMBLE (str);
985 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
988 ASSIGN_4_OCTETS (real->str + insert_at, octets);
994 * Inserts 8 bytes aligned on an 8 byte boundary
995 * with any alignment padding initialized to 0.
997 * @param str the DBusString
998 * @param insert_at where to insert
999 * @param octets 8 bytes to insert
1000 * @returns #FALSE if not enough memory.
1003 _dbus_string_insert_8_aligned (DBusString *str,
1005 const unsigned char octets[8])
1007 DBUS_STRING_PREAMBLE (str);
1009 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1012 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1014 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1021 * Inserts padding at *insert_at such to align it to the given
1022 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1023 * to the aligned position.
1025 * @param str the DBusString
1026 * @param insert_at location to be aligned
1027 * @param alignment alignment boundary (1, 2, 4, or 8)
1028 * @returns #FALSE if not enough memory.
1031 _dbus_string_insert_alignment (DBusString *str,
1035 DBUS_STRING_PREAMBLE (str);
1037 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1040 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1046 * Appends a printf-style formatted string
1047 * to the #DBusString.
1049 * @param str the string
1050 * @param format printf format
1051 * @param args variable argument list
1052 * @returns #FALSE if no memory
1055 _dbus_string_append_printf_valist (DBusString *str,
1062 DBUS_STRING_PREAMBLE (str);
1064 DBUS_VA_COPY (args_copy, args);
1066 /* Measure the message length without terminating nul */
1067 len = _dbus_printf_string_upper_bound (format, args);
1072 if (!_dbus_string_lengthen (str, len))
1074 /* don't leak the copy */
1079 vsprintf ((char*) (real->str + (real->len - len)),
1088 * Appends a printf-style formatted string
1089 * to the #DBusString.
1091 * @param str the string
1092 * @param format printf format
1093 * @returns #FALSE if no memory
1096 _dbus_string_append_printf (DBusString *str,
1103 va_start (args, format);
1104 retval = _dbus_string_append_printf_valist (str, format, args);
1111 * Appends block of bytes with the given length to a DBusString.
1113 * @param str the DBusString
1114 * @param buffer the bytes to append
1115 * @param len the number of bytes to append
1116 * @returns #FALSE if not enough memory.
1119 _dbus_string_append_len (DBusString *str,
1123 DBUS_STRING_PREAMBLE (str);
1124 _dbus_assert (buffer != NULL);
1125 _dbus_assert (len >= 0);
1127 return append (real, buffer, len);
1131 * Appends a single byte to the string, returning #FALSE
1132 * if not enough memory.
1134 * @param str the string
1135 * @param byte the byte to append
1136 * @returns #TRUE on success
1139 _dbus_string_append_byte (DBusString *str,
1142 DBUS_STRING_PREAMBLE (str);
1144 if (!set_length (real, real->len + 1))
1147 real->str[real->len-1] = byte;
1153 delete (DBusRealString *real,
1160 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1162 real->str[real->len] = '\0';
1166 * Deletes a segment of a DBusString with length len starting at
1167 * start. (Hint: to clear an entire string, setting length to 0
1168 * with _dbus_string_set_length() is easier.)
1170 * @param str the DBusString
1171 * @param start where to start deleting
1172 * @param len the number of bytes to delete
1175 _dbus_string_delete (DBusString *str,
1179 DBUS_STRING_PREAMBLE (str);
1180 _dbus_assert (start >= 0);
1181 _dbus_assert (len >= 0);
1182 _dbus_assert (start <= real->len);
1183 _dbus_assert (len <= real->len - start);
1185 delete (real, start, len);
1189 copy (DBusRealString *source,
1192 DBusRealString *dest,
1198 if (!open_gap (len, dest, insert_at))
1201 memmove (dest->str + insert_at,
1202 source->str + start,
1209 * Checks assertions for two strings we're copying a segment between,
1210 * and declares real_source/real_dest variables.
1212 * @param source the source string
1213 * @param start the starting offset
1214 * @param dest the dest string
1215 * @param insert_at where the copied segment is inserted
1217 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1218 DBusRealString *real_source = (DBusRealString*) source; \
1219 DBusRealString *real_dest = (DBusRealString*) dest; \
1220 _dbus_assert ((source) != (dest)); \
1221 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1222 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1223 _dbus_assert (!real_dest->constant); \
1224 _dbus_assert (!real_dest->locked); \
1225 _dbus_assert ((start) >= 0); \
1226 _dbus_assert ((start) <= real_source->len); \
1227 _dbus_assert ((insert_at) >= 0); \
1228 _dbus_assert ((insert_at) <= real_dest->len)
1231 * Moves the end of one string into another string. Both strings
1232 * must be initialized, valid strings.
1234 * @param source the source string
1235 * @param start where to chop off the source string
1236 * @param dest the destination string
1237 * @param insert_at where to move the chopped-off part of source string
1238 * @returns #FALSE if not enough memory
1241 _dbus_string_move (DBusString *source,
1246 DBusRealString *real_source = (DBusRealString*) source;
1247 _dbus_assert (start <= real_source->len);
1249 return _dbus_string_move_len (source, start,
1250 real_source->len - start,
1255 * Like _dbus_string_move(), but does not delete the section
1256 * of the source string that's copied to the dest string.
1258 * @param source the source string
1259 * @param start where to start copying the source string
1260 * @param dest the destination string
1261 * @param insert_at where to place the copied part of source string
1262 * @returns #FALSE if not enough memory
1265 _dbus_string_copy (const DBusString *source,
1270 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1272 return copy (real_source, start,
1273 real_source->len - start,
1279 * Like _dbus_string_move(), but can move a segment from
1280 * the middle of the source string.
1282 * @param source the source string
1283 * @param start first byte of source string to move
1284 * @param len length of segment to move
1285 * @param dest the destination string
1286 * @param insert_at where to move the bytes from the source string
1287 * @returns #FALSE if not enough memory
1290 _dbus_string_move_len (DBusString *source,
1297 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1298 _dbus_assert (len >= 0);
1299 _dbus_assert ((start + len) <= real_source->len);
1306 else if (start == 0 &&
1307 len == real_source->len &&
1308 real_dest->len == 0)
1310 /* Short-circuit moving an entire existing string to an empty string
1311 * by just swapping the buffers.
1313 /* we assume ->constant doesn't matter as you can't have
1314 * a constant string involved in a move.
1316 #define ASSIGN_DATA(a, b) do { \
1317 (a)->str = (b)->str; \
1318 (a)->len = (b)->len; \
1319 (a)->allocated = (b)->allocated; \
1320 (a)->align_offset = (b)->align_offset; \
1325 ASSIGN_DATA (&tmp, real_source);
1326 ASSIGN_DATA (real_source, real_dest);
1327 ASSIGN_DATA (real_dest, &tmp);
1333 if (!copy (real_source, start, len,
1338 delete (real_source, start,
1346 * Like _dbus_string_copy(), but can copy a segment from the middle of
1347 * the source string.
1349 * @param source the source string
1350 * @param start where to start copying the source string
1351 * @param len length of segment to copy
1352 * @param dest the destination string
1353 * @param insert_at where to place the copied segment of source string
1354 * @returns #FALSE if not enough memory
1357 _dbus_string_copy_len (const DBusString *source,
1363 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1364 _dbus_assert (len >= 0);
1365 _dbus_assert (start <= real_source->len);
1366 _dbus_assert (len <= real_source->len - start);
1368 return copy (real_source, start, len,
1374 * Replaces a segment of dest string with a segment of source string.
1376 * @param source the source string
1377 * @param start where to start copying the source string
1378 * @param len length of segment to copy
1379 * @param dest the destination string
1380 * @param replace_at start of segment of dest string to replace
1381 * @param replace_len length of segment of dest string to replace
1382 * @returns #FALSE if not enough memory
1386 _dbus_string_replace_len (const DBusString *source,
1393 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1394 _dbus_assert (len >= 0);
1395 _dbus_assert (start <= real_source->len);
1396 _dbus_assert (len <= real_source->len - start);
1397 _dbus_assert (replace_at >= 0);
1398 _dbus_assert (replace_at <= real_dest->len);
1399 _dbus_assert (replace_len <= real_dest->len - replace_at);
1401 if (len == replace_len)
1403 memmove (real_dest->str + replace_at,
1404 real_source->str + start, len);
1406 else if (len < replace_len)
1408 memmove (real_dest->str + replace_at,
1409 real_source->str + start, len);
1410 delete (real_dest, replace_at + len,
1417 _dbus_assert (len > replace_len);
1419 diff = len - replace_len;
1421 /* First of all we check if destination string can be enlarged as
1422 * required, then we overwrite previous bytes
1425 if (!copy (real_source, start + replace_len, diff,
1426 real_dest, replace_at + replace_len))
1429 memmove (real_dest->str + replace_at,
1430 real_source->str + start, replace_len);
1437 * Looks for the first occurance of a byte, deletes that byte,
1438 * and moves everything after the byte to the beginning of a
1439 * separate string. Both strings must be initialized, valid
1442 * @param source the source string
1443 * @param byte the byte to remove and split the string at
1444 * @param tail the split off string
1445 * @returns #FALSE if not enough memory or if byte could not be found
1449 _dbus_string_split_on_byte (DBusString *source,
1454 char byte_string[2] = "";
1458 byte_string[0] = (char) byte;
1460 if (!_dbus_string_find (source, 0, byte_string, &byte_position))
1463 head_length = byte_position;
1464 tail_length = _dbus_string_get_length (source) - head_length - 1;
1466 if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
1470 /* remove the trailing delimiter byte from the head now.
1472 if (!_dbus_string_set_length (source, head_length))
1478 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1479 * Pennington, and Tom Tromey are the authors and authorized relicense.
1482 /** computes length and mask of a unicode character
1483 * @param Char the char
1484 * @param Mask the mask variable to assign to
1485 * @param Len the length variable to assign to
1487 #define UTF8_COMPUTE(Char, Mask, Len) \
1493 else if ((Char & 0xe0) == 0xc0) \
1498 else if ((Char & 0xf0) == 0xe0) \
1503 else if ((Char & 0xf8) == 0xf0) \
1508 else if ((Char & 0xfc) == 0xf8) \
1513 else if ((Char & 0xfe) == 0xfc) \
1525 * computes length of a unicode character in UTF-8
1526 * @param Char the char
1528 #define UTF8_LENGTH(Char) \
1529 ((Char) < 0x80 ? 1 : \
1530 ((Char) < 0x800 ? 2 : \
1531 ((Char) < 0x10000 ? 3 : \
1532 ((Char) < 0x200000 ? 4 : \
1533 ((Char) < 0x4000000 ? 5 : 6)))))
1536 * Gets a UTF-8 value.
1538 * @param Result variable for extracted unicode char.
1539 * @param Chars the bytes to decode
1540 * @param Count counter variable
1541 * @param Mask mask for this char
1542 * @param Len length for this char in bytes
1544 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1545 (Result) = (Chars)[0] & (Mask); \
1546 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1548 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1554 (Result) |= ((Chars)[(Count)] & 0x3f); \
1558 * Check whether a Unicode (5.2) char is in a valid range.
1560 * The first check comes from the Unicode guarantee to never encode
1561 * a point above 0x0010ffff, since UTF-16 couldn't represent it.
1563 * The second check covers surrogate pairs (category Cs).
1565 * @param Char the character
1567 #define UNICODE_VALID(Char) \
1568 ((Char) < 0x110000 && \
1569 (((Char) & 0xFFFFF800) != 0xD800))
1572 * Finds the given substring in the string,
1573 * returning #TRUE and filling in the byte index
1574 * where the substring was found, if it was found.
1575 * Returns #FALSE if the substring wasn't found.
1576 * Sets *start to the length of the string if the substring
1579 * @param str the string
1580 * @param start where to start looking
1581 * @param substr the substring
1582 * @param found return location for where it was found, or #NULL
1583 * @returns #TRUE if found
1586 _dbus_string_find (const DBusString *str,
1591 return _dbus_string_find_to (str, start,
1592 ((const DBusRealString*)str)->len,
1597 * Finds end of line ("\r\n" or "\n") in the string,
1598 * returning #TRUE and filling in the byte index
1599 * where the eol string was found, if it was found.
1600 * Returns #FALSE if eol wasn't found.
1602 * @param str the string
1603 * @param start where to start looking
1604 * @param found return location for where eol was found or string length otherwise
1605 * @param found_len return length of found eol string or zero otherwise
1606 * @returns #TRUE if found
1609 _dbus_string_find_eol (const DBusString *str,
1616 DBUS_CONST_STRING_PREAMBLE (str);
1617 _dbus_assert (start <= real->len);
1618 _dbus_assert (start >= 0);
1621 while (i < real->len)
1623 if (real->str[i] == '\r')
1625 if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
1633 else /* only "\r" */
1642 else if (real->str[i] == '\n') /* only "\n" */
1663 * Finds the given substring in the string,
1664 * up to a certain position,
1665 * returning #TRUE and filling in the byte index
1666 * where the substring was found, if it was found.
1667 * Returns #FALSE if the substring wasn't found.
1668 * Sets *start to the length of the string if the substring
1671 * @param str the string
1672 * @param start where to start looking
1673 * @param end where to stop looking
1674 * @param substr the substring
1675 * @param found return location for where it was found, or #NULL
1676 * @returns #TRUE if found
1679 _dbus_string_find_to (const DBusString *str,
1686 DBUS_CONST_STRING_PREAMBLE (str);
1687 _dbus_assert (substr != NULL);
1688 _dbus_assert (start <= real->len);
1689 _dbus_assert (start >= 0);
1690 _dbus_assert (substr != NULL);
1691 _dbus_assert (end <= real->len);
1692 _dbus_assert (start <= end);
1694 /* we always "find" an empty string */
1695 if (*substr == '\0')
1705 if (real->str[i] == substr[0])
1711 if (substr[j - i] == '\0')
1713 else if (real->str[j] != substr[j - i])
1719 if (substr[j - i] == '\0')
1737 * Finds a blank (space or tab) in the string. Returns #TRUE
1738 * if found, #FALSE otherwise. If a blank is not found sets
1739 * *found to the length of the string.
1741 * @param str the string
1742 * @param start byte index to start looking
1743 * @param found place to store the location of the first blank
1744 * @returns #TRUE if a blank was found
1747 _dbus_string_find_blank (const DBusString *str,
1752 DBUS_CONST_STRING_PREAMBLE (str);
1753 _dbus_assert (start <= real->len);
1754 _dbus_assert (start >= 0);
1757 while (i < real->len)
1759 if (real->str[i] == ' ' ||
1760 real->str[i] == '\t')
1777 * Skips blanks from start, storing the first non-blank in *end
1778 * (blank is space or tab).
1780 * @param str the string
1781 * @param start where to start
1782 * @param end where to store the first non-blank byte index
1785 _dbus_string_skip_blank (const DBusString *str,
1790 DBUS_CONST_STRING_PREAMBLE (str);
1791 _dbus_assert (start <= real->len);
1792 _dbus_assert (start >= 0);
1795 while (i < real->len)
1797 if (!DBUS_IS_ASCII_BLANK (real->str[i]))
1803 _dbus_assert (i == real->len || !DBUS_IS_ASCII_WHITE (real->str[i]));
1811 * Skips whitespace from start, storing the first non-whitespace in *end.
1812 * (whitespace is space, tab, newline, CR).
1814 * @param str the string
1815 * @param start where to start
1816 * @param end where to store the first non-whitespace byte index
1819 _dbus_string_skip_white (const DBusString *str,
1824 DBUS_CONST_STRING_PREAMBLE (str);
1825 _dbus_assert (start <= real->len);
1826 _dbus_assert (start >= 0);
1829 while (i < real->len)
1831 if (!DBUS_IS_ASCII_WHITE (real->str[i]))
1837 _dbus_assert (i == real->len || !(DBUS_IS_ASCII_WHITE (real->str[i])));
1844 * Skips whitespace from end, storing the start index of the trailing
1845 * whitespace in *start. (whitespace is space, tab, newline, CR).
1847 * @param str the string
1848 * @param end where to start scanning backward
1849 * @param start where to store the start of whitespace chars
1852 _dbus_string_skip_white_reverse (const DBusString *str,
1857 DBUS_CONST_STRING_PREAMBLE (str);
1858 _dbus_assert (end <= real->len);
1859 _dbus_assert (end >= 0);
1864 if (!DBUS_IS_ASCII_WHITE (real->str[i-1]))
1869 _dbus_assert (i >= 0 && (i == 0 || !(DBUS_IS_ASCII_WHITE (real->str[i-1]))));
1876 * Assigns a newline-terminated or \\r\\n-terminated line from the front
1877 * of the string to the given dest string. The dest string's previous
1878 * contents are deleted. If the source string contains no newline,
1879 * moves the entire source string to the dest string.
1881 * @todo owen correctly notes that this is a stupid function (it was
1882 * written purely for test code,
1883 * e.g. dbus-message-builder.c). Probably should be enforced as test
1884 * code only with ifdef DBUS_ENABLE_EMBEDDED_TESTS
1886 * @param source the source string
1887 * @param dest the destination string (contents are replaced)
1888 * @returns #FALSE if no memory, or source has length 0
1891 _dbus_string_pop_line (DBusString *source,
1896 _dbus_string_set_length (dest, 0);
1900 if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
1902 _dbus_assert (eol == _dbus_string_get_length (source));
1905 /* If there's no newline and source has zero length, we're done */
1908 /* otherwise, the last line of the file has no eol characters */
1911 /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
1912 * since find_eol returned TRUE
1915 if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
1918 /* remove line ending */
1919 if (!_dbus_string_set_length (dest, eol))
1921 _dbus_assert_not_reached ("out of memory when shortening a string");
1928 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1930 * Deletes up to and including the first blank space
1933 * @param str the string
1936 _dbus_string_delete_first_word (DBusString *str)
1940 if (_dbus_string_find_blank (str, 0, &i))
1941 _dbus_string_skip_blank (str, i, &i);
1943 _dbus_string_delete (str, 0, i);
1947 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1949 * Deletes any leading blanks in the string
1951 * @param str the string
1954 _dbus_string_delete_leading_blanks (DBusString *str)
1958 _dbus_string_skip_blank (str, 0, &i);
1961 _dbus_string_delete (str, 0, i);
1966 * Deletes leading and trailing whitespace
1968 * @param str the string
1971 _dbus_string_chop_white(DBusString *str)
1975 _dbus_string_skip_white (str, 0, &i);
1978 _dbus_string_delete (str, 0, i);
1980 _dbus_string_skip_white_reverse (str, _dbus_string_get_length (str), &i);
1982 _dbus_string_set_length (str, i);
1986 * Tests two DBusString for equality.
1988 * @todo memcmp is probably faster
1990 * @param a first string
1991 * @param b second string
1992 * @returns #TRUE if equal
1995 _dbus_string_equal (const DBusString *a,
1996 const DBusString *b)
1998 const unsigned char *ap;
1999 const unsigned char *bp;
2000 const unsigned char *a_end;
2001 const DBusRealString *real_a = (const DBusRealString*) a;
2002 const DBusRealString *real_b = (const DBusRealString*) b;
2003 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2004 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2006 if (real_a->len != real_b->len)
2011 a_end = real_a->str + real_a->len;
2025 * Tests two DBusString for equality up to the given length.
2026 * The strings may be shorter than the given length.
2028 * @todo write a unit test
2030 * @todo memcmp is probably faster
2032 * @param a first string
2033 * @param b second string
2034 * @param len the maximum length to look at
2035 * @returns #TRUE if equal for the given number of bytes
2038 _dbus_string_equal_len (const DBusString *a,
2039 const DBusString *b,
2042 const unsigned char *ap;
2043 const unsigned char *bp;
2044 const unsigned char *a_end;
2045 const DBusRealString *real_a = (const DBusRealString*) a;
2046 const DBusRealString *real_b = (const DBusRealString*) b;
2047 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2048 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2050 if (real_a->len != real_b->len &&
2051 (real_a->len < len || real_b->len < len))
2056 a_end = real_a->str + MIN (real_a->len, len);
2070 * Tests two sub-parts of two DBusString for equality. The specified
2071 * range of the first string must exist; the specified start position
2072 * of the second string must exist.
2074 * @todo write a unit test
2076 * @todo memcmp is probably faster
2078 * @param a first string
2079 * @param a_start where to start substring in first string
2080 * @param a_len length of substring in first string
2081 * @param b second string
2082 * @param b_start where to start substring in second string
2083 * @returns #TRUE if the two substrings are equal
2086 _dbus_string_equal_substring (const DBusString *a,
2089 const DBusString *b,
2092 const unsigned char *ap;
2093 const unsigned char *bp;
2094 const unsigned char *a_end;
2095 const DBusRealString *real_a = (const DBusRealString*) a;
2096 const DBusRealString *real_b = (const DBusRealString*) b;
2097 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2098 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2099 _dbus_assert (a_start >= 0);
2100 _dbus_assert (a_len >= 0);
2101 _dbus_assert (a_start <= real_a->len);
2102 _dbus_assert (a_len <= real_a->len - a_start);
2103 _dbus_assert (b_start >= 0);
2104 _dbus_assert (b_start <= real_b->len);
2106 if (a_len > real_b->len - b_start)
2109 ap = real_a->str + a_start;
2110 bp = real_b->str + b_start;
2121 _dbus_assert (bp <= (real_b->str + real_b->len));
2127 * Checks whether a string is equal to a C string.
2129 * @param a the string
2130 * @param c_str the C string
2131 * @returns #TRUE if equal
2134 _dbus_string_equal_c_str (const DBusString *a,
2137 const unsigned char *ap;
2138 const unsigned char *bp;
2139 const unsigned char *a_end;
2140 const DBusRealString *real_a = (const DBusRealString*) a;
2141 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2142 _dbus_assert (c_str != NULL);
2145 bp = (const unsigned char*) c_str;
2146 a_end = real_a->str + real_a->len;
2147 while (ap != a_end && *bp)
2156 if (ap != a_end || *bp)
2163 * Checks whether a string starts with the given C string.
2165 * @param a the string
2166 * @param c_str the C string
2167 * @returns #TRUE if string starts with it
2170 _dbus_string_starts_with_c_str (const DBusString *a,
2173 const unsigned char *ap;
2174 const unsigned char *bp;
2175 const unsigned char *a_end;
2176 const DBusRealString *real_a = (const DBusRealString*) a;
2177 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2178 _dbus_assert (c_str != NULL);
2181 bp = (const unsigned char*) c_str;
2182 a_end = real_a->str + real_a->len;
2183 while (ap != a_end && *bp)
2199 * Appends a two-character hex digit to a string, where the hex digit
2200 * has the value of the given byte.
2202 * @param str the string
2203 * @param byte the byte
2204 * @returns #FALSE if no memory
2207 _dbus_string_append_byte_as_hex (DBusString *str,
2210 const char hexdigits[16] = {
2211 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2212 'a', 'b', 'c', 'd', 'e', 'f'
2215 if (!_dbus_string_append_byte (str,
2216 hexdigits[(byte >> 4)]))
2219 if (!_dbus_string_append_byte (str,
2220 hexdigits[(byte & 0x0f)]))
2222 _dbus_string_set_length (str,
2223 _dbus_string_get_length (str) - 1);
2231 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2232 * encoded. (Each byte is two hex digits.)
2234 * @param source the string to encode
2235 * @param start byte index to start encoding
2236 * @param dest string where encoded data should be placed
2237 * @param insert_at where to place encoded data
2238 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2241 _dbus_string_hex_encode (const DBusString *source,
2247 const unsigned char *p;
2248 const unsigned char *end;
2251 _dbus_assert (start <= _dbus_string_get_length (source));
2253 if (!_dbus_string_init (&result))
2258 p = (const unsigned char*) _dbus_string_get_const_data (source);
2259 end = p + _dbus_string_get_length (source);
2264 if (!_dbus_string_append_byte_as_hex (&result, *p))
2270 if (!_dbus_string_move (&result, 0, dest, insert_at))
2276 _dbus_string_free (&result);
2281 * Decodes a string from hex encoding.
2283 * @param source the string to decode
2284 * @param start byte index to start decode
2285 * @param end_return return location of the end of the hex data, or #NULL
2286 * @param dest string where decoded data should be placed
2287 * @param insert_at where to place decoded data
2288 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2291 _dbus_string_hex_decode (const DBusString *source,
2298 const unsigned char *p;
2299 const unsigned char *end;
2301 dbus_bool_t high_bits;
2303 _dbus_assert (start <= _dbus_string_get_length (source));
2305 if (!_dbus_string_init (&result))
2311 p = (const unsigned char*) _dbus_string_get_const_data (source);
2312 end = p + _dbus_string_get_length (source);
2381 if (!_dbus_string_append_byte (&result,
2390 len = _dbus_string_get_length (&result);
2392 b = _dbus_string_get_byte (&result, len - 1);
2396 _dbus_string_set_byte (&result, len - 1, b);
2399 high_bits = !high_bits;
2405 if (!_dbus_string_move (&result, 0, dest, insert_at))
2409 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2414 _dbus_string_free (&result);
2419 * Checks that the given range of the string is valid ASCII with no
2420 * nul bytes. If the given range is not entirely contained in the
2421 * string, returns #FALSE.
2423 * @todo this is inconsistent with most of DBusString in that
2424 * it allows a start,len range that extends past the string end.
2426 * @param str the string
2427 * @param start first byte index to check
2428 * @param len number of bytes to check
2429 * @returns #TRUE if the byte range exists and is all valid ASCII
2432 _dbus_string_validate_ascii (const DBusString *str,
2436 const unsigned char *s;
2437 const unsigned char *end;
2438 DBUS_CONST_STRING_PREAMBLE (str);
2439 _dbus_assert (start >= 0);
2440 _dbus_assert (start <= real->len);
2441 _dbus_assert (len >= 0);
2443 if (len > real->len - start)
2446 s = real->str + start;
2450 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2460 * Converts the given range of the string to lower case.
2462 * @param str the string
2463 * @param start first byte index to convert
2464 * @param len number of bytes to convert
2467 _dbus_string_tolower_ascii (const DBusString *str,
2473 DBUS_STRING_PREAMBLE (str);
2474 _dbus_assert (start >= 0);
2475 _dbus_assert (start <= real->len);
2476 _dbus_assert (len >= 0);
2477 _dbus_assert (len <= real->len - start);
2479 s = real->str + start;
2484 if (*s >= 'A' && *s <= 'Z')
2491 * Converts the given range of the string to upper case.
2493 * @param str the string
2494 * @param start first byte index to convert
2495 * @param len number of bytes to convert
2498 _dbus_string_toupper_ascii (const DBusString *str,
2504 DBUS_STRING_PREAMBLE (str);
2505 _dbus_assert (start >= 0);
2506 _dbus_assert (start <= real->len);
2507 _dbus_assert (len >= 0);
2508 _dbus_assert (len <= real->len - start);
2510 s = real->str + start;
2515 if (*s >= 'a' && *s <= 'z')
2522 * Checks that the given range of the string is valid UTF-8. If the
2523 * given range is not entirely contained in the string, returns
2524 * #FALSE. If the string contains any nul bytes in the given range,
2525 * returns #FALSE. If the start and start+len are not on character
2526 * boundaries, returns #FALSE.
2528 * @todo this is inconsistent with most of DBusString in that
2529 * it allows a start,len range that extends past the string end.
2531 * @param str the string
2532 * @param start first byte index to check
2533 * @param len number of bytes to check
2534 * @returns #TRUE if the byte range exists and is all valid UTF-8
2537 _dbus_string_validate_utf8 (const DBusString *str,
2541 const unsigned char *p;
2542 const unsigned char *end;
2543 DBUS_CONST_STRING_PREAMBLE (str);
2544 _dbus_assert (start >= 0);
2545 _dbus_assert (start <= real->len);
2546 _dbus_assert (len >= 0);
2548 /* we are doing _DBUS_UNLIKELY() here which might be
2549 * dubious in a generic library like GLib, but in D-Bus
2550 * we know we're validating messages and that it would
2551 * only be evil/broken apps that would have invalid
2552 * UTF-8. Also, this function seems to be a performance
2553 * bottleneck in profiles.
2556 if (_DBUS_UNLIKELY (len > real->len - start))
2559 p = real->str + start;
2564 int i, mask, char_len;
2565 dbus_unichar_t result;
2567 /* nul bytes considered invalid */
2571 /* Special-case ASCII; this makes us go a lot faster in
2572 * D-Bus profiles where we are typically validating
2573 * function names and such. We have to know that
2574 * all following checks will pass for ASCII though,
2575 * comments follow ...
2583 UTF8_COMPUTE (*p, mask, char_len);
2585 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2588 /* check that the expected number of bytes exists in the remaining length */
2589 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2592 UTF8_GET (result, p, i, mask, char_len);
2594 /* Check for overlong UTF-8 */
2595 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2598 /* The UNICODE_VALID check below will catch this */
2599 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2603 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2606 /* UNICODE_VALID should have caught it */
2607 _dbus_assert (result != (dbus_unichar_t)-1);
2612 /* See that we covered the entire length if a length was
2615 if (_DBUS_UNLIKELY (p != end))
2622 * Checks that the given range of the string is all nul bytes. If the
2623 * given range is not entirely contained in the string, returns
2626 * @todo this is inconsistent with most of DBusString in that
2627 * it allows a start,len range that extends past the string end.
2629 * @param str the string
2630 * @param start first byte index to check
2631 * @param len number of bytes to check
2632 * @returns #TRUE if the byte range exists and is all nul bytes
2635 _dbus_string_validate_nul (const DBusString *str,
2639 const unsigned char *s;
2640 const unsigned char *end;
2641 DBUS_CONST_STRING_PREAMBLE (str);
2642 _dbus_assert (start >= 0);
2643 _dbus_assert (len >= 0);
2644 _dbus_assert (start <= real->len);
2646 if (len > real->len - start)
2649 s = real->str + start;
2653 if (_DBUS_UNLIKELY (*s != '\0'))
2662 * Clears all allocated bytes in the string to zero.
2664 * @param str the string
2667 _dbus_string_zero (DBusString *str)
2669 DBUS_STRING_PREAMBLE (str);
2671 memset (real->str - real->align_offset, '\0', real->allocated);
2675 /* tests are in dbus-string-util.c */