1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-string.c String utility class (internal to D-BUS implementation)
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-string.h"
26 /* we allow a system header here, for speed/convenience */
30 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
31 #include "dbus-string-private.h"
32 #include "dbus-marshal-basic.h" /* probably should be removed by moving the usage of DBUS_TYPE
33 * into the marshaling-related files
35 /* for DBUS_VA_COPY */
36 #include "dbus-sysdeps.h"
39 * @defgroup DBusString string class
40 * @ingroup DBusInternals
41 * @brief DBusString data structure
43 * Types and functions related to DBusString. DBusString is intended
44 * to be a string class that makes it hard to mess up security issues
45 * (and just in general harder to write buggy code). It should be
46 * used (or extended and then used) rather than the libc stuff in
47 * string.h. The string class is a bit inconvenient at spots because
48 * it handles out-of-memory failures and tries to be extra-robust.
50 * A DBusString has a maximum length set at initialization time; this
51 * can be used to ensure that a buffer doesn't get too big. The
52 * _dbus_string_lengthen() method checks for overflow, and for max
53 * length being exceeded.
55 * Try to avoid conversion to a plain C string, i.e. add methods on
56 * the string object instead, only convert to C string when passing
57 * things out to the public API. In particular, no sprintf, strcpy,
58 * strcat, any of that should be used. The GString feature of
59 * accepting negative numbers for "length of string" is also absent,
60 * because it could keep us from detecting bogus huge lengths. i.e. if
61 * we passed in some bogus huge length it would be taken to mean
62 * "current length of string" instead of "broken crack"
64 * @todo #DBusString needs a lot of cleaning up; some of the
65 * API is no longer used, and the API is pretty inconsistent.
66 * In particular all the "append" APIs, especially those involving
67 * alignment but probably lots of them, are no longer used by the
68 * marshaling code which always does "inserts" now.
72 * @addtogroup DBusString
77 fixup_alignment (DBusRealString *real)
81 unsigned int old_align_offset;
83 /* we have to have extra space in real->allocated for the align offset and nul byte */
84 _dbus_assert (real->len <= real->allocated - _DBUS_STRING_ALLOCATION_PADDING);
86 old_align_offset = real->align_offset;
87 real_block = real->str - old_align_offset;
89 aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
91 real->align_offset = aligned - real_block;
94 if (old_align_offset != real->align_offset)
96 /* Here comes the suck */
97 memmove (real_block + real->align_offset,
98 real_block + old_align_offset,
102 _dbus_assert (real->align_offset < 8);
103 _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
107 undo_alignment (DBusRealString *real)
109 if (real->align_offset != 0)
111 memmove (real->str - real->align_offset,
115 real->str = real->str - real->align_offset;
116 real->align_offset = 0;
121 * Initializes a string that can be up to the given allocation size
122 * before it has to realloc. The string starts life with zero length.
123 * The string must eventually be freed with _dbus_string_free().
125 * @param str memory to hold the string
126 * @param allocate_size amount to preallocate
127 * @returns #TRUE on success, #FALSE if no memory
130 _dbus_string_init_preallocated (DBusString *str,
133 DBusRealString *real;
135 _dbus_assert (str != NULL);
137 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
139 real = (DBusRealString*) str;
141 /* It's very important not to touch anything
142 * other than real->str if we're going to fail,
143 * since we also use this function to reset
144 * an existing string, e.g. in _dbus_string_steal_data()
147 real->str = dbus_malloc (_DBUS_STRING_ALLOCATION_PADDING + allocate_size);
148 if (real->str == NULL)
151 real->allocated = _DBUS_STRING_ALLOCATION_PADDING + allocate_size;
153 real->str[real->len] = '\0';
155 real->max_length = _DBUS_STRING_MAX_MAX_LENGTH;
156 real->constant = FALSE;
157 real->locked = FALSE;
158 real->invalid = FALSE;
159 real->align_offset = 0;
161 fixup_alignment (real);
167 * Initializes a string. The string starts life with zero length. The
168 * string must eventually be freed with _dbus_string_free().
170 * @param str memory to hold the string
171 * @returns #TRUE on success, #FALSE if no memory
174 _dbus_string_init (DBusString *str)
176 return _dbus_string_init_preallocated (str, 0);
179 #ifdef DBUS_BUILD_TESTS
180 /* The max length thing is sort of a historical artifact
181 * from a feature that turned out to be dumb; perhaps
182 * we should purge it entirely. The problem with
183 * the feature is that it looks like memory allocation
184 * failure, but is not a transient or resolvable failure.
187 set_max_length (DBusString *str,
190 DBusRealString *real;
192 real = (DBusRealString*) str;
194 real->max_length = max_length;
196 #endif /* DBUS_BUILD_TESTS */
199 * Initializes a constant string. The value parameter is not copied
200 * (should be static), and the string may never be modified.
201 * It is safe but not necessary to call _dbus_string_free()
202 * on a const string. The string has a length limit of MAXINT - 8.
204 * @param str memory to use for the string
205 * @param value a string to be stored in str (not copied!!!)
208 _dbus_string_init_const (DBusString *str,
211 _dbus_assert (value != NULL);
213 _dbus_string_init_const_len (str, value,
218 * Initializes a constant string with a length. The value parameter is
219 * not copied (should be static), and the string may never be
220 * modified. It is safe but not necessary to call _dbus_string_free()
223 * @param str memory to use for the string
224 * @param value a string to be stored in str (not copied!!!)
225 * @param len the length to use
228 _dbus_string_init_const_len (DBusString *str,
232 DBusRealString *real;
234 _dbus_assert (str != NULL);
235 _dbus_assert (value != NULL);
236 _dbus_assert (len <= _DBUS_STRING_MAX_MAX_LENGTH);
237 _dbus_assert (len >= 0);
239 real = (DBusRealString*) str;
241 real->str = (char*) value;
243 real->allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
244 real->max_length = real->len + 1;
245 real->constant = TRUE;
247 real->invalid = FALSE;
248 real->align_offset = 0;
250 /* We don't require const strings to be 8-byte aligned as the
251 * memory is coming from elsewhere.
256 * Frees a string created by _dbus_string_init().
258 * @param str memory where the string is stored.
261 _dbus_string_free (DBusString *str)
263 DBusRealString *real = (DBusRealString*) str;
264 DBUS_GENERIC_STRING_PREAMBLE (real);
268 dbus_free (real->str - real->align_offset);
270 real->invalid = TRUE;
273 #ifdef DBUS_BUILD_TESTS
274 /* Not using this feature at the moment,
275 * so marked DBUS_BUILD_TESTS-only
278 * Locks a string such that any attempts to change the string will
279 * result in aborting the program. Also, if the string is wasting a
280 * lot of memory (allocation is sufficiently larger than what the
281 * string is really using), _dbus_string_lock() will realloc the
282 * string's data to "compact" it.
284 * @param str the string to lock.
287 _dbus_string_lock (DBusString *str)
289 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
293 /* Try to realloc to avoid excess memory usage, since
294 * we know we won't change the string further
297 if (real->allocated - MAX_WASTE > real->len)
302 new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
304 new_str = dbus_realloc (real->str - real->align_offset,
308 real->str = new_str + real->align_offset;
309 real->allocated = new_allocated;
310 fixup_alignment (real);
314 #endif /* DBUS_BUILD_TESTS */
317 reallocate_for_length (DBusRealString *real,
323 /* at least double our old allocation to avoid O(n), avoiding
326 if (real->allocated > (_DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING) / 2)
327 new_allocated = _DBUS_STRING_MAX_MAX_LENGTH + _DBUS_STRING_ALLOCATION_PADDING;
329 new_allocated = real->allocated * 2;
331 /* if you change the code just above here, run the tests without
332 * the following assert-only hack before you commit
334 /* This is keyed off asserts in addition to tests so when you
335 * disable asserts to profile, you don't get this destroyer
338 #ifdef DBUS_DISABLE_ASSERT
340 #ifdef DBUS_BUILD_TESTS
341 new_allocated = 0; /* ensure a realloc every time so that we go
342 * through all malloc failure codepaths
344 #endif /* DBUS_BUILD_TESTS */
345 #endif /* !DBUS_DISABLE_ASSERT */
347 /* But be sure we always alloc at least space for the new length */
348 new_allocated = MAX (new_allocated,
349 new_length + _DBUS_STRING_ALLOCATION_PADDING);
351 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
352 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
353 if (_DBUS_UNLIKELY (new_str == NULL))
356 real->str = new_str + real->align_offset;
357 real->allocated = new_allocated;
358 fixup_alignment (real);
364 set_length (DBusRealString *real,
367 /* Note, we are setting the length not including nul termination */
369 /* exceeding max length is the same as failure to allocate memory */
370 if (_DBUS_UNLIKELY (new_length > real->max_length))
372 else if (new_length > (real->allocated - _DBUS_STRING_ALLOCATION_PADDING) &&
373 _DBUS_UNLIKELY (!reallocate_for_length (real, new_length)))
377 real->len = new_length;
378 real->str[new_length] = '\0';
385 DBusRealString *dest,
391 if (len > dest->max_length - dest->len)
392 return FALSE; /* detected overflow of dest->len + len below */
394 if (!set_length (dest, dest->len + len))
397 memmove (dest->str + insert_at + len,
398 dest->str + insert_at,
399 dest->len - len - insert_at);
404 #ifndef _dbus_string_get_data
406 * Gets the raw character buffer from the string. The returned buffer
407 * will be nul-terminated, but note that strings may contain binary
408 * data so there may be extra nul characters prior to the termination.
409 * This function should be little-used, extend DBusString or add
410 * stuff to dbus-sysdeps.c instead. It's an error to use this
411 * function on a const string.
413 * @param str the string
417 _dbus_string_get_data (DBusString *str)
419 DBUS_STRING_PREAMBLE (str);
423 #endif /* _dbus_string_get_data */
425 /* only do the function if we don't have the macro */
426 #ifndef _dbus_string_get_const_data
428 * Gets the raw character buffer from a const string.
430 * @param str the string
431 * @returns the string data
434 _dbus_string_get_const_data (const DBusString *str)
436 DBUS_CONST_STRING_PREAMBLE (str);
440 #endif /* _dbus_string_get_const_data */
443 * Gets a sub-portion of the raw character buffer from the
444 * string. The "len" field is required simply for error
445 * checking, to be sure you don't try to use more
446 * string than exists. The nul termination of the
447 * returned buffer remains at the end of the entire
448 * string, not at start + len.
450 * @param str the string
451 * @param start byte offset to return
452 * @param len length of segment to return
453 * @returns the string data
456 _dbus_string_get_data_len (DBusString *str,
460 DBUS_STRING_PREAMBLE (str);
461 _dbus_assert (start >= 0);
462 _dbus_assert (len >= 0);
463 _dbus_assert (start <= real->len);
464 _dbus_assert (len <= real->len - start);
466 return real->str + start;
469 /* only do the function if we don't have the macro */
470 #ifndef _dbus_string_get_const_data_len
472 * const version of _dbus_string_get_data_len().
474 * @param str the string
475 * @param start byte offset to return
476 * @param len length of segment to return
477 * @returns the string data
480 _dbus_string_get_const_data_len (const DBusString *str,
484 DBUS_CONST_STRING_PREAMBLE (str);
485 _dbus_assert (start >= 0);
486 _dbus_assert (len >= 0);
487 _dbus_assert (start <= real->len);
488 _dbus_assert (len <= real->len - start);
490 return real->str + start;
492 #endif /* _dbus_string_get_const_data_len */
494 /* only do the function if we don't have the macro */
495 #ifndef _dbus_string_set_byte
497 * Sets the value of the byte at the given position.
499 * @param str the string
500 * @param i the position
501 * @param byte the new value
504 _dbus_string_set_byte (DBusString *str,
508 DBUS_STRING_PREAMBLE (str);
509 _dbus_assert (i < real->len);
510 _dbus_assert (i >= 0);
514 #endif /* _dbus_string_set_byte */
516 /* only have the function if we didn't create a macro */
517 #ifndef _dbus_string_get_byte
519 * Gets the byte at the given position. It is
520 * allowed to ask for the nul byte at the end of
523 * @param str the string
524 * @param start the position
525 * @returns the byte at that position
528 _dbus_string_get_byte (const DBusString *str,
531 DBUS_CONST_STRING_PREAMBLE (str);
532 _dbus_assert (start <= real->len);
533 _dbus_assert (start >= 0);
535 return real->str[start];
537 #endif /* _dbus_string_get_byte */
540 * Inserts a number of bytes of a given value at the
543 * @param str the string
544 * @param i the position
545 * @param n_bytes number of bytes
546 * @param byte the value to insert
547 * @returns #TRUE on success
550 _dbus_string_insert_bytes (DBusString *str,
555 DBUS_STRING_PREAMBLE (str);
556 _dbus_assert (i <= real->len);
557 _dbus_assert (i >= 0);
558 _dbus_assert (n_bytes >= 0);
563 if (!open_gap (n_bytes, real, i))
566 memset (real->str + i, byte, n_bytes);
572 * Inserts a single byte at the given position.
574 * @param str the string
575 * @param i the position
576 * @param byte the value to insert
577 * @returns #TRUE on success
580 _dbus_string_insert_byte (DBusString *str,
584 DBUS_STRING_PREAMBLE (str);
585 _dbus_assert (i <= real->len);
586 _dbus_assert (i >= 0);
588 if (!open_gap (1, real, i))
597 * Like _dbus_string_get_data(), but removes the
598 * gotten data from the original string. The caller
599 * must free the data returned. This function may
600 * fail due to lack of memory, and return #FALSE.
602 * @param str the string
603 * @param data_return location to return the buffer
604 * @returns #TRUE on success
607 _dbus_string_steal_data (DBusString *str,
611 DBUS_STRING_PREAMBLE (str);
612 _dbus_assert (data_return != NULL);
614 undo_alignment (real);
616 *data_return = real->str;
618 old_max_length = real->max_length;
620 /* reset the string */
621 if (!_dbus_string_init (str))
623 /* hrm, put it back then */
624 real->str = *data_return;
626 fixup_alignment (real);
630 real->max_length = old_max_length;
635 #ifdef DBUS_BUILD_TESTS
637 * Like _dbus_string_get_data_len(), but removes the gotten data from
638 * the original string. The caller must free the data returned. This
639 * function may fail due to lack of memory, and return #FALSE.
640 * The returned string is nul-terminated and has length len.
642 * @todo this function is broken because on failure it
643 * may corrupt the source string.
645 * @param str the string
646 * @param data_return location to return the buffer
647 * @param start the start of segment to steal
648 * @param len the length of segment to steal
649 * @returns #TRUE on success
652 _dbus_string_steal_data_len (DBusString *str,
658 DBUS_STRING_PREAMBLE (str);
659 _dbus_assert (data_return != NULL);
660 _dbus_assert (start >= 0);
661 _dbus_assert (len >= 0);
662 _dbus_assert (start <= real->len);
663 _dbus_assert (len <= real->len - start);
665 if (!_dbus_string_init (&dest))
668 set_max_length (&dest, real->max_length);
670 if (!_dbus_string_move_len (str, start, len, &dest, 0))
672 _dbus_string_free (&dest);
676 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
677 if (!_dbus_string_steal_data (&dest, data_return))
679 _dbus_string_free (&dest);
683 _dbus_string_free (&dest);
686 #endif /* DBUS_BUILD_TESTS */
689 * Copies the data from the string into a char*
691 * @param str the string
692 * @param data_return place to return the data
693 * @returns #TRUE on success, #FALSE on no memory
696 _dbus_string_copy_data (const DBusString *str,
699 DBUS_CONST_STRING_PREAMBLE (str);
700 _dbus_assert (data_return != NULL);
702 *data_return = dbus_malloc (real->len + 1);
703 if (*data_return == NULL)
706 memcpy (*data_return, real->str, real->len + 1);
711 #ifdef DBUS_BUILD_TESTS
713 * Copies a segment of the string into a char*
715 * @param str the string
716 * @param data_return place to return the data
717 * @param start start index
718 * @param len length to copy
719 * @returns #FALSE if no memory
722 _dbus_string_copy_data_len (const DBusString *str,
729 DBUS_CONST_STRING_PREAMBLE (str);
730 _dbus_assert (data_return != NULL);
731 _dbus_assert (start >= 0);
732 _dbus_assert (len >= 0);
733 _dbus_assert (start <= real->len);
734 _dbus_assert (len <= real->len - start);
736 if (!_dbus_string_init (&dest))
739 set_max_length (&dest, real->max_length);
741 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
743 _dbus_string_free (&dest);
747 if (!_dbus_string_steal_data (&dest, data_return))
749 _dbus_string_free (&dest);
753 _dbus_string_free (&dest);
756 #endif /* DBUS_BUILD_TESTS */
758 /* Only have the function if we don't have the macro */
759 #ifndef _dbus_string_get_length
761 * Gets the length of a string (not including nul termination).
763 * @returns the length.
766 _dbus_string_get_length (const DBusString *str)
768 DBUS_CONST_STRING_PREAMBLE (str);
772 #endif /* !_dbus_string_get_length */
775 * Makes a string longer by the given number of bytes. Checks whether
776 * adding additional_length to the current length would overflow an
777 * integer, and checks for exceeding a string's max length.
778 * The new bytes are not initialized, other than nul-terminating
779 * the end of the string. The uninitialized bytes may contain
780 * nul bytes or other junk.
782 * @param str a string
783 * @param additional_length length to add to the string.
784 * @returns #TRUE on success.
787 _dbus_string_lengthen (DBusString *str,
788 int additional_length)
790 DBUS_STRING_PREAMBLE (str);
791 _dbus_assert (additional_length >= 0);
793 if (_DBUS_UNLIKELY (additional_length > real->max_length - real->len))
794 return FALSE; /* would overflow */
796 return set_length (real,
797 real->len + additional_length);
801 * Makes a string shorter by the given number of bytes.
803 * @param str a string
804 * @param length_to_remove length to remove from the string.
807 _dbus_string_shorten (DBusString *str,
808 int length_to_remove)
810 DBUS_STRING_PREAMBLE (str);
811 _dbus_assert (length_to_remove >= 0);
812 _dbus_assert (length_to_remove <= real->len);
815 real->len - length_to_remove);
819 * Sets the length of a string. Can be used to truncate or lengthen
820 * the string. If the string is lengthened, the function may fail and
821 * return #FALSE. Newly-added bytes are not initialized, as with
822 * _dbus_string_lengthen().
824 * @param str a string
825 * @param length new length of the string.
826 * @returns #FALSE on failure.
829 _dbus_string_set_length (DBusString *str,
832 DBUS_STRING_PREAMBLE (str);
833 _dbus_assert (length >= 0);
835 return set_length (real, length);
839 align_insert_point_then_open_gap (DBusString *str,
844 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
845 unsigned long gap_pos;
848 DBUS_STRING_PREAMBLE (str);
849 _dbus_assert (alignment >= 1);
850 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
852 insert_at = *insert_at_p;
854 _dbus_assert (insert_at <= real->len);
856 gap_pos = _DBUS_ALIGN_VALUE (insert_at, alignment);
857 new_len = real->len + (gap_pos - insert_at) + gap_size;
859 if (_DBUS_UNLIKELY (new_len > (unsigned long) real->max_length))
862 delta = new_len - real->len;
863 _dbus_assert (delta >= 0);
865 if (delta == 0) /* only happens if gap_size == 0 and insert_at is aligned already */
867 _dbus_assert (((unsigned long) *insert_at_p) == gap_pos);
871 if (_DBUS_UNLIKELY (!open_gap (new_len - real->len,
875 /* nul the padding if we had to add any padding */
876 if (gap_size < delta)
878 memset (&real->str[insert_at], '\0',
879 gap_pos - insert_at);
882 *insert_at_p = gap_pos;
888 align_length_then_lengthen (DBusString *str,
890 int then_lengthen_by)
894 insert_at = _dbus_string_get_length (str);
896 return align_insert_point_then_open_gap (str,
898 alignment, then_lengthen_by);
902 * Align the length of a string to a specific alignment (typically 4 or 8)
903 * by appending nul bytes to the string.
905 * @param str a string
906 * @param alignment the alignment
907 * @returns #FALSE if no memory
910 _dbus_string_align_length (DBusString *str,
913 return align_length_then_lengthen (str, alignment, 0);
917 * Preallocate extra_bytes such that a future lengthening of the
918 * string by extra_bytes is guaranteed to succeed without an out of
921 * @param str a string
922 * @param extra_bytes bytes to alloc
923 * @returns #FALSE if no memory
926 _dbus_string_alloc_space (DBusString *str,
929 if (!_dbus_string_lengthen (str, extra_bytes))
931 _dbus_string_shorten (str, extra_bytes);
937 append (DBusRealString *real,
944 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
947 memcpy (real->str + (real->len - buffer_len),
955 * Appends a nul-terminated C-style string to a DBusString.
957 * @param str the DBusString
958 * @param buffer the nul-terminated characters to append
959 * @returns #FALSE if not enough memory.
962 _dbus_string_append (DBusString *str,
965 unsigned long buffer_len;
967 DBUS_STRING_PREAMBLE (str);
968 _dbus_assert (buffer != NULL);
970 buffer_len = strlen (buffer);
971 if (buffer_len > (unsigned long) real->max_length)
974 return append (real, buffer, buffer_len);
977 /** assign 2 bytes from one string to another */
978 #define ASSIGN_2_OCTETS(p, octets) \
979 *((dbus_uint16_t*)(p)) = *((dbus_uint16_t*)(octets));
981 /** assign 4 bytes from one string to another */
982 #define ASSIGN_4_OCTETS(p, octets) \
983 *((dbus_uint32_t*)(p)) = *((dbus_uint32_t*)(octets));
985 #ifdef DBUS_HAVE_INT64
986 /** assign 8 bytes from one string to another */
987 #define ASSIGN_8_OCTETS(p, octets) \
988 *((dbus_uint64_t*)(p)) = *((dbus_uint64_t*)(octets));
990 /** assign 8 bytes from one string to another */
991 #define ASSIGN_8_OCTETS(p, octets) \
1005 _dbus_assert (b == p + 8); \
1007 #endif /* DBUS_HAVE_INT64 */
1009 #ifdef DBUS_BUILD_TESTS
1011 * Appends 4 bytes aligned on a 4 byte boundary
1012 * with any alignment padding initialized to 0.
1014 * @param str the DBusString
1015 * @param octets 4 bytes to append
1016 * @returns #FALSE if not enough memory.
1019 _dbus_string_append_4_aligned (DBusString *str,
1020 const unsigned char octets[4])
1022 DBUS_STRING_PREAMBLE (str);
1024 if (!align_length_then_lengthen (str, 4, 4))
1027 ASSIGN_4_OCTETS (real->str + (real->len - 4), octets);
1031 #endif /* DBUS_BUILD_TESTS */
1033 #ifdef DBUS_BUILD_TESTS
1035 * Appends 8 bytes aligned on an 8 byte boundary
1036 * with any alignment padding initialized to 0.
1038 * @param str the DBusString
1039 * @param octets 8 bytes to append
1040 * @returns #FALSE if not enough memory.
1043 _dbus_string_append_8_aligned (DBusString *str,
1044 const unsigned char octets[8])
1046 DBUS_STRING_PREAMBLE (str);
1048 if (!align_length_then_lengthen (str, 8, 8))
1051 ASSIGN_8_OCTETS (real->str + (real->len - 8), octets);
1055 #endif /* DBUS_BUILD_TESTS */
1058 * Inserts 2 bytes aligned on a 2 byte boundary
1059 * with any alignment padding initialized to 0.
1061 * @param str the DBusString
1062 * @param insert_at where to insert
1063 * @param octets 2 bytes to insert
1064 * @returns #FALSE if not enough memory.
1067 _dbus_string_insert_2_aligned (DBusString *str,
1069 const unsigned char octets[4])
1071 DBUS_STRING_PREAMBLE (str);
1073 if (!align_insert_point_then_open_gap (str, &insert_at, 2, 2))
1076 ASSIGN_2_OCTETS (real->str + insert_at, octets);
1082 * Inserts 4 bytes aligned on a 4 byte boundary
1083 * with any alignment padding initialized to 0.
1085 * @param str the DBusString
1086 * @param insert_at where to insert
1087 * @param octets 4 bytes to insert
1088 * @returns #FALSE if not enough memory.
1091 _dbus_string_insert_4_aligned (DBusString *str,
1093 const unsigned char octets[4])
1095 DBUS_STRING_PREAMBLE (str);
1097 if (!align_insert_point_then_open_gap (str, &insert_at, 4, 4))
1100 ASSIGN_4_OCTETS (real->str + insert_at, octets);
1106 * Inserts 8 bytes aligned on an 8 byte boundary
1107 * with any alignment padding initialized to 0.
1109 * @param str the DBusString
1110 * @param insert_at where to insert
1111 * @param octets 8 bytes to insert
1112 * @returns #FALSE if not enough memory.
1115 _dbus_string_insert_8_aligned (DBusString *str,
1117 const unsigned char octets[8])
1119 DBUS_STRING_PREAMBLE (str);
1121 if (!align_insert_point_then_open_gap (str, &insert_at, 8, 8))
1124 _dbus_assert (_DBUS_ALIGN_VALUE (insert_at, 8) == (unsigned) insert_at);
1126 ASSIGN_8_OCTETS (real->str + insert_at, octets);
1133 * Inserts padding at *insert_at such to align it to the given
1134 * boundary. Initializes the padding to nul bytes. Sets *insert_at
1135 * to the aligned position.
1137 * @param str the DBusString
1138 * @param insert_at location to be aligned
1139 * @param alignment alignment boundary (1, 2, 4, or 8)
1140 * @returns #FALSE if not enough memory.
1143 _dbus_string_insert_alignment (DBusString *str,
1147 DBUS_STRING_PREAMBLE (str);
1149 if (!align_insert_point_then_open_gap (str, insert_at, alignment, 0))
1152 _dbus_assert (_DBUS_ALIGN_VALUE (*insert_at, alignment) == (unsigned) *insert_at);
1158 * Appends a printf-style formatted string
1159 * to the #DBusString.
1161 * @param str the string
1162 * @param format printf format
1163 * @param args variable argument list
1164 * @returns #FALSE if no memory
1167 _dbus_string_append_printf_valist (DBusString *str,
1175 DBUS_STRING_PREAMBLE (str);
1177 DBUS_VA_COPY (args_copy, args);
1179 /* Measure the message length without terminating nul */
1180 len = vsnprintf (&c, 1, format, args);
1182 if (!_dbus_string_lengthen (str, len))
1184 /* don't leak the copy */
1189 vsprintf (real->str + (real->len - len),
1198 * Appends a printf-style formatted string
1199 * to the #DBusString.
1201 * @param str the string
1202 * @param format printf format
1203 * @returns #FALSE if no memory
1206 _dbus_string_append_printf (DBusString *str,
1213 va_start (args, format);
1214 retval = _dbus_string_append_printf_valist (str, format, args);
1221 * Appends block of bytes with the given length to a DBusString.
1223 * @param str the DBusString
1224 * @param buffer the bytes to append
1225 * @param len the number of bytes to append
1226 * @returns #FALSE if not enough memory.
1229 _dbus_string_append_len (DBusString *str,
1233 DBUS_STRING_PREAMBLE (str);
1234 _dbus_assert (buffer != NULL);
1235 _dbus_assert (len >= 0);
1237 return append (real, buffer, len);
1241 * Appends a single byte to the string, returning #FALSE
1242 * if not enough memory.
1244 * @param str the string
1245 * @param byte the byte to append
1246 * @returns #TRUE on success
1249 _dbus_string_append_byte (DBusString *str,
1252 DBUS_STRING_PREAMBLE (str);
1254 if (!set_length (real, real->len + 1))
1257 real->str[real->len-1] = byte;
1262 #ifdef DBUS_BUILD_TESTS
1264 * Appends a single Unicode character, encoding the character
1267 * @param str the string
1268 * @param ch the Unicode character
1271 _dbus_string_append_unichar (DBusString *str,
1279 DBUS_STRING_PREAMBLE (str);
1281 /* this code is from GLib but is pretty standard I think */
1290 else if (ch < 0x800)
1295 else if (ch < 0x10000)
1300 else if (ch < 0x200000)
1305 else if (ch < 0x4000000)
1316 if (len > (real->max_length - real->len))
1317 return FALSE; /* real->len + len would overflow */
1319 if (!set_length (real, real->len + len))
1322 out = real->str + (real->len - len);
1324 for (i = len - 1; i > 0; --i)
1326 out[i] = (ch & 0x3f) | 0x80;
1329 out[0] = ch | first;
1333 #endif /* DBUS_BUILD_TESTS */
1336 delete (DBusRealString *real,
1343 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1345 real->str[real->len] = '\0';
1349 * Deletes a segment of a DBusString with length len starting at
1350 * start. (Hint: to clear an entire string, setting length to 0
1351 * with _dbus_string_set_length() is easier.)
1353 * @param str the DBusString
1354 * @param start where to start deleting
1355 * @param len the number of bytes to delete
1358 _dbus_string_delete (DBusString *str,
1362 DBUS_STRING_PREAMBLE (str);
1363 _dbus_assert (start >= 0);
1364 _dbus_assert (len >= 0);
1365 _dbus_assert (start <= real->len);
1366 _dbus_assert (len <= real->len - start);
1368 delete (real, start, len);
1372 copy (DBusRealString *source,
1375 DBusRealString *dest,
1381 if (!open_gap (len, dest, insert_at))
1384 memcpy (dest->str + insert_at,
1385 source->str + start,
1392 * Checks assertions for two strings we're copying a segment between,
1393 * and declares real_source/real_dest variables.
1395 * @param source the source string
1396 * @param start the starting offset
1397 * @param dest the dest string
1398 * @param insert_at where the copied segment is inserted
1400 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1401 DBusRealString *real_source = (DBusRealString*) source; \
1402 DBusRealString *real_dest = (DBusRealString*) dest; \
1403 _dbus_assert ((source) != (dest)); \
1404 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1405 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1406 _dbus_assert (!real_dest->constant); \
1407 _dbus_assert (!real_dest->locked); \
1408 _dbus_assert ((start) >= 0); \
1409 _dbus_assert ((start) <= real_source->len); \
1410 _dbus_assert ((insert_at) >= 0); \
1411 _dbus_assert ((insert_at) <= real_dest->len)
1414 * Moves the end of one string into another string. Both strings
1415 * must be initialized, valid strings.
1417 * @param source the source string
1418 * @param start where to chop off the source string
1419 * @param dest the destination string
1420 * @param insert_at where to move the chopped-off part of source string
1421 * @returns #FALSE if not enough memory
1424 _dbus_string_move (DBusString *source,
1429 DBusRealString *real_source = (DBusRealString*) source;
1430 _dbus_assert (start <= real_source->len);
1432 return _dbus_string_move_len (source, start,
1433 real_source->len - start,
1438 * Like _dbus_string_move(), but does not delete the section
1439 * of the source string that's copied to the dest string.
1441 * @param source the source string
1442 * @param start where to start copying the source string
1443 * @param dest the destination string
1444 * @param insert_at where to place the copied part of source string
1445 * @returns #FALSE if not enough memory
1448 _dbus_string_copy (const DBusString *source,
1453 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1455 return copy (real_source, start,
1456 real_source->len - start,
1462 * Like _dbus_string_move(), but can move a segment from
1463 * the middle of the source string.
1465 * @todo this doesn't do anything with max_length field.
1466 * we should probably just kill the max_length field though.
1468 * @param source the source string
1469 * @param start first byte of source string to move
1470 * @param len length of segment to move
1471 * @param dest the destination string
1472 * @param insert_at where to move the bytes from the source string
1473 * @returns #FALSE if not enough memory
1476 _dbus_string_move_len (DBusString *source,
1483 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1484 _dbus_assert (len >= 0);
1485 _dbus_assert ((start + len) <= real_source->len);
1492 else if (start == 0 &&
1493 len == real_source->len &&
1494 real_dest->len == 0)
1496 /* Short-circuit moving an entire existing string to an empty string
1497 * by just swapping the buffers.
1499 /* we assume ->constant doesn't matter as you can't have
1500 * a constant string involved in a move.
1502 #define ASSIGN_DATA(a, b) do { \
1503 (a)->str = (b)->str; \
1504 (a)->len = (b)->len; \
1505 (a)->allocated = (b)->allocated; \
1506 (a)->align_offset = (b)->align_offset; \
1511 ASSIGN_DATA (&tmp, real_source);
1512 ASSIGN_DATA (real_source, real_dest);
1513 ASSIGN_DATA (real_dest, &tmp);
1519 if (!copy (real_source, start, len,
1524 delete (real_source, start,
1532 * Like _dbus_string_copy(), but can copy a segment from the middle of
1533 * the source string.
1535 * @param source the source string
1536 * @param start where to start copying the source string
1537 * @param len length of segment to copy
1538 * @param dest the destination string
1539 * @param insert_at where to place the copied segment of source string
1540 * @returns #FALSE if not enough memory
1543 _dbus_string_copy_len (const DBusString *source,
1549 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1550 _dbus_assert (len >= 0);
1551 _dbus_assert (start <= real_source->len);
1552 _dbus_assert (len <= real_source->len - start);
1554 return copy (real_source, start, len,
1560 * Replaces a segment of dest string with a segment of source string.
1562 * @todo optimize the case where the two lengths are the same, and
1563 * avoid memmoving the data in the trailing part of the string twice.
1565 * @todo avoid inserting the source into dest, then deleting
1566 * the replaced chunk of dest (which creates a potentially large
1567 * intermediate string). Instead, extend the replaced chunk
1568 * of dest with padding to the same size as the source chunk,
1569 * then copy in the source bytes.
1571 * @param source the source string
1572 * @param start where to start copying the source string
1573 * @param len length of segment to copy
1574 * @param dest the destination string
1575 * @param replace_at start of segment of dest string to replace
1576 * @param replace_len length of segment of dest string to replace
1577 * @returns #FALSE if not enough memory
1581 _dbus_string_replace_len (const DBusString *source,
1588 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1589 _dbus_assert (len >= 0);
1590 _dbus_assert (start <= real_source->len);
1591 _dbus_assert (len <= real_source->len - start);
1592 _dbus_assert (replace_at >= 0);
1593 _dbus_assert (replace_at <= real_dest->len);
1594 _dbus_assert (replace_len <= real_dest->len - replace_at);
1596 if (!copy (real_source, start, len,
1597 real_dest, replace_at))
1600 delete (real_dest, replace_at + len, replace_len);
1605 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1606 * Pennington, and Tom Tromey are the authors and authorized relicense.
1609 /** computes length and mask of a unicode character
1610 * @param Char the char
1611 * @param Mask the mask variable to assign to
1612 * @param Len the length variable to assign to
1614 #define UTF8_COMPUTE(Char, Mask, Len) \
1620 else if ((Char & 0xe0) == 0xc0) \
1625 else if ((Char & 0xf0) == 0xe0) \
1630 else if ((Char & 0xf8) == 0xf0) \
1635 else if ((Char & 0xfc) == 0xf8) \
1640 else if ((Char & 0xfe) == 0xfc) \
1652 * computes length of a unicode character in UTF-8
1653 * @param Char the char
1655 #define UTF8_LENGTH(Char) \
1656 ((Char) < 0x80 ? 1 : \
1657 ((Char) < 0x800 ? 2 : \
1658 ((Char) < 0x10000 ? 3 : \
1659 ((Char) < 0x200000 ? 4 : \
1660 ((Char) < 0x4000000 ? 5 : 6)))))
1663 * Gets a UTF-8 value.
1665 * @param Result variable for extracted unicode char.
1666 * @param Chars the bytes to decode
1667 * @param Count counter variable
1668 * @param Mask mask for this char
1669 * @param Len length for this char in bytes
1671 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1672 (Result) = (Chars)[0] & (Mask); \
1673 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1675 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1681 (Result) |= ((Chars)[(Count)] & 0x3f); \
1685 * Check whether a unicode char is in a valid range.
1687 * @param Char the character
1689 #define UNICODE_VALID(Char) \
1690 ((Char) < 0x110000 && \
1691 (((Char) & 0xFFFFF800) != 0xD800) && \
1692 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1693 ((Char) & 0xFFFF) != 0xFFFF)
1695 #ifdef DBUS_BUILD_TESTS
1697 * Gets a unicode character from a UTF-8 string. Does no validation;
1698 * you must verify that the string is valid UTF-8 in advance and must
1699 * pass in the start of a character.
1701 * @param str the string
1702 * @param start the start of the UTF-8 character.
1703 * @param ch_return location to return the character
1704 * @param end_return location to return the byte index of next character
1707 _dbus_string_get_unichar (const DBusString *str,
1709 dbus_unichar_t *ch_return,
1713 dbus_unichar_t result;
1716 DBUS_CONST_STRING_PREAMBLE (str);
1717 _dbus_assert (start >= 0);
1718 _dbus_assert (start <= real->len);
1723 *end_return = real->len;
1726 p = real->str + start;
1729 UTF8_COMPUTE (c, mask, len);
1732 UTF8_GET (result, p, i, mask, len);
1734 if (result == (dbus_unichar_t)-1)
1738 *ch_return = result;
1740 *end_return = start + len;
1742 #endif /* DBUS_BUILD_TESTS */
1745 * Finds the given substring in the string,
1746 * returning #TRUE and filling in the byte index
1747 * where the substring was found, if it was found.
1748 * Returns #FALSE if the substring wasn't found.
1749 * Sets *start to the length of the string if the substring
1752 * @param str the string
1753 * @param start where to start looking
1754 * @param substr the substring
1755 * @param found return location for where it was found, or #NULL
1756 * @returns #TRUE if found
1759 _dbus_string_find (const DBusString *str,
1764 return _dbus_string_find_to (str, start,
1765 ((const DBusRealString*)str)->len,
1770 * Finds the given substring in the string,
1771 * up to a certain position,
1772 * returning #TRUE and filling in the byte index
1773 * where the substring was found, if it was found.
1774 * Returns #FALSE if the substring wasn't found.
1775 * Sets *start to the length of the string if the substring
1778 * @param str the string
1779 * @param start where to start looking
1780 * @param end where to stop looking
1781 * @param substr the substring
1782 * @param found return location for where it was found, or #NULL
1783 * @returns #TRUE if found
1786 _dbus_string_find_to (const DBusString *str,
1793 DBUS_CONST_STRING_PREAMBLE (str);
1794 _dbus_assert (substr != NULL);
1795 _dbus_assert (start <= real->len);
1796 _dbus_assert (start >= 0);
1797 _dbus_assert (substr != NULL);
1798 _dbus_assert (end <= real->len);
1799 _dbus_assert (start <= end);
1801 /* we always "find" an empty string */
1802 if (*substr == '\0')
1812 if (real->str[i] == substr[0])
1818 if (substr[j - i] == '\0')
1820 else if (real->str[j] != substr[j - i])
1826 if (substr[j - i] == '\0')
1844 * Finds a blank (space or tab) in the string. Returns #TRUE
1845 * if found, #FALSE otherwise. If a blank is not found sets
1846 * *found to the length of the string.
1848 * @param str the string
1849 * @param start byte index to start looking
1850 * @param found place to store the location of the first blank
1851 * @returns #TRUE if a blank was found
1854 _dbus_string_find_blank (const DBusString *str,
1859 DBUS_CONST_STRING_PREAMBLE (str);
1860 _dbus_assert (start <= real->len);
1861 _dbus_assert (start >= 0);
1864 while (i < real->len)
1866 if (real->str[i] == ' ' ||
1867 real->str[i] == '\t')
1884 * Skips blanks from start, storing the first non-blank in *end
1885 * (blank is space or tab).
1887 * @param str the string
1888 * @param start where to start
1889 * @param end where to store the first non-blank byte index
1892 _dbus_string_skip_blank (const DBusString *str,
1897 DBUS_CONST_STRING_PREAMBLE (str);
1898 _dbus_assert (start <= real->len);
1899 _dbus_assert (start >= 0);
1902 while (i < real->len)
1904 if (!(real->str[i] == ' ' ||
1905 real->str[i] == '\t'))
1911 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1912 real->str[i] == '\t'));
1919 * Assigns a newline-terminated or \\r\\n-terminated line from the front
1920 * of the string to the given dest string. The dest string's previous
1921 * contents are deleted. If the source string contains no newline,
1922 * moves the entire source string to the dest string.
1924 * @todo owen correctly notes that this is a stupid function (it was
1925 * written purely for test code,
1926 * e.g. dbus-message-builder.c). Probably should be enforced as test
1927 * code only with #ifdef DBUS_BUILD_TESTS
1929 * @param source the source string
1930 * @param dest the destination string (contents are replaced)
1931 * @returns #FALSE if no memory, or source has length 0
1934 _dbus_string_pop_line (DBusString *source,
1938 dbus_bool_t have_newline;
1940 _dbus_string_set_length (dest, 0);
1943 if (_dbus_string_find (source, 0, "\n", &eol))
1945 have_newline = TRUE;
1946 eol += 1; /* include newline */
1950 eol = _dbus_string_get_length (source);
1951 have_newline = FALSE;
1955 return FALSE; /* eof */
1957 if (!_dbus_string_move_len (source, 0, eol,
1963 /* dump the newline and the \r if we have one */
1966 dbus_bool_t have_cr;
1968 _dbus_assert (_dbus_string_get_length (dest) > 0);
1970 if (_dbus_string_get_length (dest) > 1 &&
1971 _dbus_string_get_byte (dest,
1972 _dbus_string_get_length (dest) - 2) == '\r')
1977 _dbus_string_set_length (dest,
1978 _dbus_string_get_length (dest) -
1985 #ifdef DBUS_BUILD_TESTS
1987 * Deletes up to and including the first blank space
1990 * @param str the string
1993 _dbus_string_delete_first_word (DBusString *str)
1997 if (_dbus_string_find_blank (str, 0, &i))
1998 _dbus_string_skip_blank (str, i, &i);
2000 _dbus_string_delete (str, 0, i);
2004 #ifdef DBUS_BUILD_TESTS
2006 * Deletes any leading blanks in the string
2008 * @param str the string
2011 _dbus_string_delete_leading_blanks (DBusString *str)
2015 _dbus_string_skip_blank (str, 0, &i);
2018 _dbus_string_delete (str, 0, i);
2023 * Tests two DBusString for equality.
2025 * @todo memcmp is probably faster
2027 * @param a first string
2028 * @param b second string
2029 * @returns #TRUE if equal
2032 _dbus_string_equal (const DBusString *a,
2033 const DBusString *b)
2035 const unsigned char *ap;
2036 const unsigned char *bp;
2037 const unsigned char *a_end;
2038 const DBusRealString *real_a = (const DBusRealString*) a;
2039 const DBusRealString *real_b = (const DBusRealString*) b;
2040 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2041 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2043 if (real_a->len != real_b->len)
2048 a_end = real_a->str + real_a->len;
2061 #ifdef DBUS_BUILD_TESTS
2063 * Tests two DBusString for equality up to the given length.
2064 * The strings may be shorter than the given length.
2066 * @todo write a unit test
2068 * @todo memcmp is probably faster
2070 * @param a first string
2071 * @param b second string
2072 * @param len the maximum length to look at
2073 * @returns #TRUE if equal for the given number of bytes
2076 _dbus_string_equal_len (const DBusString *a,
2077 const DBusString *b,
2080 const unsigned char *ap;
2081 const unsigned char *bp;
2082 const unsigned char *a_end;
2083 const DBusRealString *real_a = (const DBusRealString*) a;
2084 const DBusRealString *real_b = (const DBusRealString*) b;
2085 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2086 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2088 if (real_a->len != real_b->len &&
2089 (real_a->len < len || real_b->len < len))
2094 a_end = real_a->str + MIN (real_a->len, len);
2106 #endif /* DBUS_BUILD_TESTS */
2109 * Tests two sub-parts of two DBusString for equality. The specified
2110 * range of the first string must exist; the specified start position
2111 * of the second string must exist.
2113 * @todo write a unit test
2115 * @todo memcmp is probably faster
2117 * @param a first string
2118 * @param a_start where to start substring in first string
2119 * @param a_len length of substring in first string
2120 * @param b second string
2121 * @param b_start where to start substring in second string
2122 * @returns #TRUE if the two substrings are equal
2125 _dbus_string_equal_substring (const DBusString *a,
2128 const DBusString *b,
2131 const unsigned char *ap;
2132 const unsigned char *bp;
2133 const unsigned char *a_end;
2134 const DBusRealString *real_a = (const DBusRealString*) a;
2135 const DBusRealString *real_b = (const DBusRealString*) b;
2136 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2137 DBUS_GENERIC_STRING_PREAMBLE (real_b);
2138 _dbus_assert (a_start >= 0);
2139 _dbus_assert (a_len >= 0);
2140 _dbus_assert (a_start <= real_a->len);
2141 _dbus_assert (a_len <= real_a->len - a_start);
2142 _dbus_assert (b_start >= 0);
2143 _dbus_assert (b_start <= real_b->len);
2145 if (a_len > real_b->len - b_start)
2148 ap = real_a->str + a_start;
2149 bp = real_b->str + b_start;
2160 _dbus_assert (bp <= (real_b->str + real_b->len));
2166 * Checks whether a string is equal to a C string.
2168 * @param a the string
2169 * @param c_str the C string
2170 * @returns #TRUE if equal
2173 _dbus_string_equal_c_str (const DBusString *a,
2176 const unsigned char *ap;
2177 const unsigned char *bp;
2178 const unsigned char *a_end;
2179 const DBusRealString *real_a = (const DBusRealString*) a;
2180 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2181 _dbus_assert (c_str != NULL);
2184 bp = (const unsigned char*) c_str;
2185 a_end = real_a->str + real_a->len;
2186 while (ap != a_end && *bp)
2195 if (ap != a_end || *bp)
2201 #ifdef DBUS_BUILD_TESTS
2203 * Checks whether a string starts with the given C string.
2205 * @param a the string
2206 * @param c_str the C string
2207 * @returns #TRUE if string starts with it
2210 _dbus_string_starts_with_c_str (const DBusString *a,
2213 const unsigned char *ap;
2214 const unsigned char *bp;
2215 const unsigned char *a_end;
2216 const DBusRealString *real_a = (const DBusRealString*) a;
2217 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2218 _dbus_assert (c_str != NULL);
2221 bp = (const unsigned char*) c_str;
2222 a_end = real_a->str + real_a->len;
2223 while (ap != a_end && *bp)
2237 #endif /* DBUS_BUILD_TESTS */
2240 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2241 * encoded. (Each byte is two hex digits.)
2243 * @param source the string to encode
2244 * @param start byte index to start encoding
2245 * @param dest string where encoded data should be placed
2246 * @param insert_at where to place encoded data
2247 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2250 _dbus_string_hex_encode (const DBusString *source,
2256 const char hexdigits[16] = {
2257 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2258 'a', 'b', 'c', 'd', 'e', 'f'
2260 const unsigned char *p;
2261 const unsigned char *end;
2264 _dbus_assert (start <= _dbus_string_get_length (source));
2266 if (!_dbus_string_init (&result))
2271 p = (const unsigned char*) _dbus_string_get_const_data (source);
2272 end = p + _dbus_string_get_length (source);
2277 if (!_dbus_string_append_byte (&result,
2278 hexdigits[(*p >> 4)]))
2281 if (!_dbus_string_append_byte (&result,
2282 hexdigits[(*p & 0x0f)]))
2288 if (!_dbus_string_move (&result, 0, dest, insert_at))
2294 _dbus_string_free (&result);
2299 * Decodes a string from hex encoding.
2301 * @param source the string to decode
2302 * @param start byte index to start decode
2303 * @param end_return return location of the end of the hex data, or #NULL
2304 * @param dest string where decoded data should be placed
2305 * @param insert_at where to place decoded data
2306 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2309 _dbus_string_hex_decode (const DBusString *source,
2316 const unsigned char *p;
2317 const unsigned char *end;
2319 dbus_bool_t high_bits;
2321 _dbus_assert (start <= _dbus_string_get_length (source));
2323 if (!_dbus_string_init (&result))
2329 p = (const unsigned char*) _dbus_string_get_const_data (source);
2330 end = p + _dbus_string_get_length (source);
2399 if (!_dbus_string_append_byte (&result,
2408 len = _dbus_string_get_length (&result);
2410 b = _dbus_string_get_byte (&result, len - 1);
2414 _dbus_string_set_byte (&result, len - 1, b);
2417 high_bits = !high_bits;
2423 if (!_dbus_string_move (&result, 0, dest, insert_at))
2427 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2432 _dbus_string_free (&result);
2437 * Checks that the given range of the string is valid ASCII with no
2438 * nul bytes. If the given range is not entirely contained in the
2439 * string, returns #FALSE.
2441 * @todo this is inconsistent with most of DBusString in that
2442 * it allows a start,len range that extends past the string end.
2444 * @param str the string
2445 * @param start first byte index to check
2446 * @param len number of bytes to check
2447 * @returns #TRUE if the byte range exists and is all valid ASCII
2450 _dbus_string_validate_ascii (const DBusString *str,
2454 const unsigned char *s;
2455 const unsigned char *end;
2456 DBUS_CONST_STRING_PREAMBLE (str);
2457 _dbus_assert (start >= 0);
2458 _dbus_assert (start <= real->len);
2459 _dbus_assert (len >= 0);
2461 if (len > real->len - start)
2464 s = real->str + start;
2468 if (_DBUS_UNLIKELY (!_DBUS_ISASCII (*s)))
2478 * Checks that the given range of the string is valid UTF-8. If the
2479 * given range is not entirely contained in the string, returns
2480 * #FALSE. If the string contains any nul bytes in the given range,
2481 * returns #FALSE. If the start and start+len are not on character
2482 * boundaries, returns #FALSE.
2484 * @todo this is inconsistent with most of DBusString in that
2485 * it allows a start,len range that extends past the string end.
2487 * @param str the string
2488 * @param start first byte index to check
2489 * @param len number of bytes to check
2490 * @returns #TRUE if the byte range exists and is all valid UTF-8
2493 _dbus_string_validate_utf8 (const DBusString *str,
2497 const unsigned char *p;
2498 const unsigned char *end;
2499 DBUS_CONST_STRING_PREAMBLE (str);
2500 _dbus_assert (start >= 0);
2501 _dbus_assert (start <= real->len);
2502 _dbus_assert (len >= 0);
2504 /* we are doing _DBUS_UNLIKELY() here which might be
2505 * dubious in a generic library like GLib, but in D-BUS
2506 * we know we're validating messages and that it would
2507 * only be evil/broken apps that would have invalid
2508 * UTF-8. Also, this function seems to be a performance
2509 * bottleneck in profiles.
2512 if (_DBUS_UNLIKELY (len > real->len - start))
2515 p = real->str + start;
2520 int i, mask, char_len;
2521 dbus_unichar_t result;
2523 /* nul bytes considered invalid */
2527 /* Special-case ASCII; this makes us go a lot faster in
2528 * D-BUS profiles where we are typically validating
2529 * function names and such. We have to know that
2530 * all following checks will pass for ASCII though,
2531 * comments follow ...
2539 UTF8_COMPUTE (*p, mask, char_len);
2541 if (_DBUS_UNLIKELY (char_len == 0)) /* ASCII: char_len == 1 */
2544 /* check that the expected number of bytes exists in the remaining length */
2545 if (_DBUS_UNLIKELY ((end - p) < char_len)) /* ASCII: p < end and char_len == 1 */
2548 UTF8_GET (result, p, i, mask, char_len);
2550 /* Check for overlong UTF-8 */
2551 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* ASCII: UTF8_LENGTH == 1 */
2554 /* The UNICODE_VALID check below will catch this */
2555 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1)) /* ASCII: result = ascii value */
2559 if (_DBUS_UNLIKELY (!UNICODE_VALID (result))) /* ASCII: always valid */
2562 /* UNICODE_VALID should have caught it */
2563 _dbus_assert (result != (dbus_unichar_t)-1);
2568 /* See that we covered the entire length if a length was
2571 if (_DBUS_UNLIKELY (p != end))
2578 * Checks that the given range of the string is all nul bytes. If the
2579 * given range is not entirely contained in the string, returns
2582 * @todo this is inconsistent with most of DBusString in that
2583 * it allows a start,len range that extends past the string end.
2585 * @param str the string
2586 * @param start first byte index to check
2587 * @param len number of bytes to check
2588 * @returns #TRUE if the byte range exists and is all nul bytes
2591 _dbus_string_validate_nul (const DBusString *str,
2595 const unsigned char *s;
2596 const unsigned char *end;
2597 DBUS_CONST_STRING_PREAMBLE (str);
2598 _dbus_assert (start >= 0);
2599 _dbus_assert (len >= 0);
2600 _dbus_assert (start <= real->len);
2602 if (len > real->len - start)
2605 s = real->str + start;
2609 if (_DBUS_UNLIKELY (*s != '\0'))
2618 * Clears all allocated bytes in the string to zero.
2620 * @param str the string
2623 _dbus_string_zero (DBusString *str)
2625 DBUS_STRING_PREAMBLE (str);
2627 memset (real->str - real->align_offset, '\0', real->allocated);
2631 /* tests are in dbus-string-util.c */