1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-string.c String utility class (internal to D-BUS implementation)
4 * Copyright (C) 2002, 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.0
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 #include "dbus-marshal.h"
31 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
32 #include "dbus-string-private.h"
33 #include "dbus-protocol.h"
34 /* for DBUS_VA_COPY */
35 #include "dbus-sysdeps.h"
38 * @defgroup DBusString string class
39 * @ingroup DBusInternals
40 * @brief DBusString data structure
42 * Types and functions related to DBusString. DBusString is intended
43 * to be a string class that makes it hard to mess up security issues
44 * (and just in general harder to write buggy code). It should be
45 * used (or extended and then used) rather than the libc stuff in
46 * string.h. The string class is a bit inconvenient at spots because
47 * it handles out-of-memory failures and tries to be extra-robust.
49 * A DBusString has a maximum length set at initialization time; this
50 * can be used to ensure that a buffer doesn't get too big. The
51 * _dbus_string_lengthen() method checks for overflow, and for max
52 * length being exceeded.
54 * Try to avoid conversion to a plain C string, i.e. add methods on
55 * the string object instead, only convert to C string when passing
56 * things out to the public API. In particular, no sprintf, strcpy,
57 * strcat, any of that should be used. The GString feature of
58 * accepting negative numbers for "length of string" is also absent,
59 * because it could keep us from detecting bogus huge lengths. i.e. if
60 * we passed in some bogus huge length it would be taken to mean
61 * "current length of string" instead of "broken crack"
65 * @defgroup DBusStringInternals DBusString implementation details
66 * @ingroup DBusInternals
67 * @brief DBusString implementation details
69 * The guts of DBusString.
75 * We allocate 1 byte for nul termination, plus 7 bytes for possible
76 * align_offset, so we always need 8 bytes on top of the string's
77 * length to be in the allocated block.
79 #define ALLOCATION_PADDING 8
82 * This is the maximum max length (and thus also the maximum length)
85 #define MAX_MAX_LENGTH (_DBUS_INT_MAX - ALLOCATION_PADDING)
88 * Checks a bunch of assertions about a string object
90 * @param real the DBusRealString
92 #define DBUS_GENERIC_STRING_PREAMBLE(real) _dbus_assert ((real) != NULL); _dbus_assert (!(real)->invalid); _dbus_assert ((real)->len >= 0); _dbus_assert ((real)->allocated >= 0); _dbus_assert ((real)->max_length >= 0); _dbus_assert ((real)->len <= ((real)->allocated - ALLOCATION_PADDING)); _dbus_assert ((real)->len <= (real)->max_length)
95 * Checks assertions about a string object that needs to be
96 * modifiable - may not be locked or const. Also declares
97 * the "real" variable pointing to DBusRealString.
98 * @param str the string
100 #define DBUS_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
101 DBUS_GENERIC_STRING_PREAMBLE (real); \
102 _dbus_assert (!(real)->constant); \
103 _dbus_assert (!(real)->locked)
106 * Checks assertions about a string object that may be locked but
107 * can't be const. i.e. a string object that we can free. Also
108 * declares the "real" variable pointing to DBusRealString.
110 * @param str the string
112 #define DBUS_LOCKED_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
113 DBUS_GENERIC_STRING_PREAMBLE (real); \
114 _dbus_assert (!(real)->constant)
117 * Checks assertions about a string that may be const or locked. Also
118 * declares the "real" variable pointing to DBusRealString.
119 * @param str the string.
121 #define DBUS_CONST_STRING_PREAMBLE(str) const DBusRealString *real = (DBusRealString*) str; \
122 DBUS_GENERIC_STRING_PREAMBLE (real)
127 * @addtogroup DBusString
132 fixup_alignment (DBusRealString *real)
136 unsigned int old_align_offset;
138 /* we have to have extra space in real->allocated for the align offset and nul byte */
139 _dbus_assert (real->len <= real->allocated - ALLOCATION_PADDING);
141 old_align_offset = real->align_offset;
142 real_block = real->str - old_align_offset;
144 aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
146 real->align_offset = aligned - real_block;
149 if (old_align_offset != real->align_offset)
151 /* Here comes the suck */
152 memmove (real_block + real->align_offset,
153 real_block + old_align_offset,
157 _dbus_assert (real->align_offset < 8);
158 _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
162 undo_alignment (DBusRealString *real)
164 if (real->align_offset != 0)
166 memmove (real->str - real->align_offset,
170 real->str = real->str - real->align_offset;
171 real->align_offset = 0;
176 * Initializes a string that can be up to the given allocation size
177 * before it has to realloc. The string starts life with zero length.
178 * The string must eventually be freed with _dbus_string_free().
180 * @param str memory to hold the string
181 * @param allocate_size amount to preallocate
182 * @returns #TRUE on success, #FALSE if no memory
185 _dbus_string_init_preallocated (DBusString *str,
188 DBusRealString *real;
190 _dbus_assert (str != NULL);
192 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
194 real = (DBusRealString*) str;
196 /* It's very important not to touch anything
197 * other than real->str if we're going to fail,
198 * since we also use this function to reset
199 * an existing string, e.g. in _dbus_string_steal_data()
202 real->str = dbus_malloc (ALLOCATION_PADDING + allocate_size);
203 if (real->str == NULL)
206 real->allocated = ALLOCATION_PADDING + allocate_size;
208 real->str[real->len] = '\0';
210 real->max_length = MAX_MAX_LENGTH;
211 real->constant = FALSE;
212 real->locked = FALSE;
213 real->invalid = FALSE;
214 real->align_offset = 0;
216 fixup_alignment (real);
222 * Initializes a string. The string starts life with zero length. The
223 * string must eventually be freed with _dbus_string_free().
225 * @param str memory to hold the string
226 * @returns #TRUE on success, #FALSE if no memory
229 _dbus_string_init (DBusString *str)
231 return _dbus_string_init_preallocated (str, 0);
234 /* The max length thing is sort of a historical artifact
235 * from a feature that turned out to be dumb; perhaps
236 * we should purge it entirely. The problem with
237 * the feature is that it looks like memory allocation
238 * failure, but is not a transient or resolvable failure.
241 set_max_length (DBusString *str,
244 DBusRealString *real;
246 real = (DBusRealString*) str;
248 real->max_length = max_length;
252 * Initializes a constant string. The value parameter is not copied
253 * (should be static), and the string may never be modified.
254 * It is safe but not necessary to call _dbus_string_free()
255 * on a const string. The string has a length limit of MAXINT - 8.
257 * @param str memory to use for the string
258 * @param value a string to be stored in str (not copied!!!)
261 _dbus_string_init_const (DBusString *str,
264 _dbus_assert (value != NULL);
266 _dbus_string_init_const_len (str, value,
271 * Initializes a constant string with a length. The value parameter is
272 * not copied (should be static), and the string may never be
273 * modified. It is safe but not necessary to call _dbus_string_free()
276 * @param str memory to use for the string
277 * @param value a string to be stored in str (not copied!!!)
278 * @param len the length to use
281 _dbus_string_init_const_len (DBusString *str,
285 DBusRealString *real;
287 _dbus_assert (str != NULL);
288 _dbus_assert (value != NULL);
289 _dbus_assert (len <= MAX_MAX_LENGTH);
290 _dbus_assert (len >= 0);
292 real = (DBusRealString*) str;
294 real->str = (char*) value;
296 real->allocated = real->len + ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
297 real->max_length = real->len + 1;
298 real->constant = TRUE;
299 real->invalid = FALSE;
301 /* We don't require const strings to be 8-byte aligned as the
302 * memory is coming from elsewhere.
307 * Frees a string created by _dbus_string_init().
309 * @param str memory where the string is stored.
312 _dbus_string_free (DBusString *str)
314 DBusRealString *real = (DBusRealString*) str;
315 DBUS_GENERIC_STRING_PREAMBLE (real);
319 dbus_free (real->str - real->align_offset);
321 real->invalid = TRUE;
324 #ifdef DBUS_BUILD_TESTS
325 /* Not using this feature at the moment,
326 * so marked DBUS_BUILD_TESTS-only
329 * Locks a string such that any attempts to change the string will
330 * result in aborting the program. Also, if the string is wasting a
331 * lot of memory (allocation is sufficiently larger than what the
332 * string is really using), _dbus_string_lock() will realloc the
333 * string's data to "compact" it.
335 * @param str the string to lock.
338 _dbus_string_lock (DBusString *str)
340 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
344 /* Try to realloc to avoid excess memory usage, since
345 * we know we won't change the string further
348 if (real->allocated - MAX_WASTE > real->len)
353 new_allocated = real->len + ALLOCATION_PADDING;
355 new_str = dbus_realloc (real->str - real->align_offset,
359 real->str = new_str + real->align_offset;
360 real->allocated = new_allocated;
361 fixup_alignment (real);
365 #endif /* DBUS_BUILD_TESTS */
368 reallocate_for_length (DBusRealString *real,
374 /* at least double our old allocation to avoid O(n), avoiding
377 if (real->allocated > (MAX_MAX_LENGTH + ALLOCATION_PADDING) / 2)
378 new_allocated = MAX_MAX_LENGTH + ALLOCATION_PADDING;
380 new_allocated = real->allocated * 2;
382 /* if you change the code just above here, run the tests without
383 * the following before you commit
385 #ifdef DBUS_BUILD_TESTS
386 new_allocated = 0; /* ensure a realloc every time so that we go
387 * through all malloc failure codepaths
391 /* But be sure we always alloc at least space for the new length */
392 new_allocated = MAX (new_allocated, new_length + ALLOCATION_PADDING);
394 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
395 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
399 real->str = new_str + real->align_offset;
400 real->allocated = new_allocated;
401 fixup_alignment (real);
407 set_length (DBusRealString *real,
410 /* Note, we are setting the length not including nul termination */
412 /* exceeding max length is the same as failure to allocate memory */
413 if (new_length > real->max_length)
415 else if (new_length > (real->allocated - ALLOCATION_PADDING) &&
416 !reallocate_for_length (real, new_length))
420 real->len = new_length;
421 real->str[real->len] = '\0';
428 DBusRealString *dest,
434 if (len > dest->max_length - dest->len)
435 return FALSE; /* detected overflow of dest->len + len below */
437 if (!set_length (dest, dest->len + len))
440 memmove (dest->str + insert_at + len,
441 dest->str + insert_at,
442 dest->len - len - insert_at);
448 * Gets the raw character buffer from the string. The returned buffer
449 * will be nul-terminated, but note that strings may contain binary
450 * data so there may be extra nul characters prior to the termination.
451 * This function should be little-used, extend DBusString or add
452 * stuff to dbus-sysdeps.c instead. It's an error to use this
453 * function on a const string.
455 * @param str the string
459 _dbus_string_get_data (DBusString *str)
461 DBUS_STRING_PREAMBLE (str);
467 * Gets the raw character buffer from a const string.
469 * @param str the string
470 * @returns the string data
473 _dbus_string_get_const_data (const DBusString *str)
475 DBUS_CONST_STRING_PREAMBLE (str);
481 * Gets a sub-portion of the raw character buffer from the
482 * string. The "len" field is required simply for error
483 * checking, to be sure you don't try to use more
484 * string than exists. The nul termination of the
485 * returned buffer remains at the end of the entire
486 * string, not at start + len.
488 * @param str the string
489 * @param start byte offset to return
490 * @param len length of segment to return
491 * @returns the string data
494 _dbus_string_get_data_len (DBusString *str,
498 DBUS_STRING_PREAMBLE (str);
499 _dbus_assert (start >= 0);
500 _dbus_assert (len >= 0);
501 _dbus_assert (start <= real->len);
502 _dbus_assert (len <= real->len - start);
504 return real->str + start;
508 * const version of _dbus_string_get_data_len().
510 * @param str the string
511 * @param start byte offset to return
512 * @param len length of segment to return
513 * @returns the string data
516 _dbus_string_get_const_data_len (const DBusString *str,
520 DBUS_CONST_STRING_PREAMBLE (str);
521 _dbus_assert (start >= 0);
522 _dbus_assert (len >= 0);
523 _dbus_assert (start <= real->len);
524 _dbus_assert (len <= real->len - start);
526 return real->str + start;
530 * Sets the value of the byte at the given position.
532 * @param str the string
533 * @param i the position
534 * @param byte the new value
537 _dbus_string_set_byte (DBusString *str,
541 DBUS_STRING_PREAMBLE (str);
542 _dbus_assert (i < real->len);
543 _dbus_assert (i >= 0);
549 * Gets the byte at the given position. It is
550 * allowed to ask for the nul byte at the end of
553 * @param str the string
554 * @param start the position
555 * @returns the byte at that position
558 _dbus_string_get_byte (const DBusString *str,
561 DBUS_CONST_STRING_PREAMBLE (str);
562 _dbus_assert (start <= real->len);
563 _dbus_assert (start >= 0);
565 return real->str[start];
569 * Inserts a number of bytes of a given value at the
572 * @param str the string
573 * @param i the position
574 * @param n_bytes number of bytes
575 * @param byte the value to insert
576 * @returns #TRUE on success
579 _dbus_string_insert_bytes (DBusString *str,
584 DBUS_STRING_PREAMBLE (str);
585 _dbus_assert (i <= real->len);
586 _dbus_assert (i >= 0);
587 _dbus_assert (n_bytes >= 0);
592 if (!open_gap (n_bytes, real, i))
595 memset (real->str + i, byte, n_bytes);
601 * Like _dbus_string_get_data(), but removes the
602 * gotten data from the original string. The caller
603 * must free the data returned. This function may
604 * fail due to lack of memory, and return #FALSE.
606 * @param str the string
607 * @param data_return location to return the buffer
608 * @returns #TRUE on success
611 _dbus_string_steal_data (DBusString *str,
615 DBUS_STRING_PREAMBLE (str);
616 _dbus_assert (data_return != NULL);
618 undo_alignment (real);
620 *data_return = real->str;
622 old_max_length = real->max_length;
624 /* reset the string */
625 if (!_dbus_string_init (str))
627 /* hrm, put it back then */
628 real->str = *data_return;
630 fixup_alignment (real);
634 real->max_length = old_max_length;
640 * Like _dbus_string_get_data_len(), but removes the gotten data from
641 * the original string. The caller must free the data returned. This
642 * function may fail due to lack of memory, and return #FALSE.
643 * The returned string is nul-terminated and has length len.
645 * @todo this function is broken because on failure it
646 * may corrupt the source string.
648 * @param str the string
649 * @param data_return location to return the buffer
650 * @param start the start of segment to steal
651 * @param len the length of segment to steal
652 * @returns #TRUE on success
655 _dbus_string_steal_data_len (DBusString *str,
661 DBUS_STRING_PREAMBLE (str);
662 _dbus_assert (data_return != NULL);
663 _dbus_assert (start >= 0);
664 _dbus_assert (len >= 0);
665 _dbus_assert (start <= real->len);
666 _dbus_assert (len <= real->len - start);
668 if (!_dbus_string_init (&dest))
671 set_max_length (&dest, real->max_length);
673 if (!_dbus_string_move_len (str, start, len, &dest, 0))
675 _dbus_string_free (&dest);
679 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
680 if (!_dbus_string_steal_data (&dest, data_return))
682 _dbus_string_free (&dest);
686 _dbus_string_free (&dest);
692 * Copies the data from the string into a char*
694 * @param str the string
695 * @param data_return place to return the data
696 * @returns #TRUE on success, #FALSE on no memory
699 _dbus_string_copy_data (const DBusString *str,
702 DBUS_CONST_STRING_PREAMBLE (str);
703 _dbus_assert (data_return != NULL);
705 *data_return = dbus_malloc (real->len + 1);
706 if (*data_return == NULL)
709 memcpy (*data_return, real->str, real->len + 1);
715 * Copies a segment of the string into a char*
717 * @param str the string
718 * @param data_return place to return the data
719 * @param start start index
720 * @param len length to copy
721 * @returns #FALSE if no memory
724 _dbus_string_copy_data_len (const DBusString *str,
731 DBUS_CONST_STRING_PREAMBLE (str);
732 _dbus_assert (data_return != NULL);
733 _dbus_assert (start >= 0);
734 _dbus_assert (len >= 0);
735 _dbus_assert (start <= real->len);
736 _dbus_assert (len <= real->len - start);
738 if (!_dbus_string_init (&dest))
741 set_max_length (&dest, real->max_length);
743 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
745 _dbus_string_free (&dest);
749 if (!_dbus_string_steal_data (&dest, data_return))
751 _dbus_string_free (&dest);
755 _dbus_string_free (&dest);
760 * Gets the length of a string (not including nul termination).
762 * @returns the length.
765 _dbus_string_get_length (const DBusString *str)
767 DBUS_CONST_STRING_PREAMBLE (str);
773 * Makes a string longer by the given number of bytes. Checks whether
774 * adding additional_length to the current length would overflow an
775 * integer, and checks for exceeding a string's max length.
776 * The new bytes are not initialized, other than nul-terminating
777 * the end of the string. The uninitialized bytes may contain
778 * nul bytes or other junk.
780 * @param str a string
781 * @param additional_length length to add to the string.
782 * @returns #TRUE on success.
785 _dbus_string_lengthen (DBusString *str,
786 int additional_length)
788 DBUS_STRING_PREAMBLE (str);
789 _dbus_assert (additional_length >= 0);
791 if (additional_length > real->max_length - real->len)
792 return FALSE; /* would overflow */
794 return set_length (real,
795 real->len + additional_length);
799 * Makes a string shorter by the given number of bytes.
801 * @param str a string
802 * @param length_to_remove length to remove from the string.
805 _dbus_string_shorten (DBusString *str,
806 int length_to_remove)
808 DBUS_STRING_PREAMBLE (str);
809 _dbus_assert (length_to_remove >= 0);
810 _dbus_assert (length_to_remove <= real->len);
813 real->len - length_to_remove);
817 * Sets the length of a string. Can be used to truncate or lengthen
818 * the string. If the string is lengthened, the function may fail and
819 * return #FALSE. Newly-added bytes are not initialized, as with
820 * _dbus_string_lengthen().
822 * @param str a string
823 * @param length new length of the string.
824 * @returns #FALSE on failure.
827 _dbus_string_set_length (DBusString *str,
830 DBUS_STRING_PREAMBLE (str);
831 _dbus_assert (length >= 0);
833 return set_length (real, length);
837 align_length_then_lengthen (DBusString *str,
839 int then_lengthen_by)
841 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
843 DBUS_STRING_PREAMBLE (str);
844 _dbus_assert (alignment >= 1);
845 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
847 new_len = _DBUS_ALIGN_VALUE (real->len, alignment);
848 if (new_len > (unsigned long) real->max_length - then_lengthen_by)
850 new_len += then_lengthen_by;
852 delta = new_len - real->len;
853 _dbus_assert (delta >= 0);
858 if (!set_length (real, new_len))
861 /* delta == padding + then_lengthen_by
862 * new_len == old_len + padding + then_lengthen_by
864 if (then_lengthen_by < delta)
868 while (i < (new_len - then_lengthen_by))
879 * Align the length of a string to a specific alignment (typically 4 or 8)
880 * by appending nul bytes to the string.
882 * @param str a string
883 * @param alignment the alignment
884 * @returns #FALSE if no memory
887 _dbus_string_align_length (DBusString *str,
890 return align_length_then_lengthen (str, alignment, 0);
894 append (DBusRealString *real,
901 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
904 memcpy (real->str + (real->len - buffer_len),
912 * Appends a nul-terminated C-style string to a DBusString.
914 * @param str the DBusString
915 * @param buffer the nul-terminated characters to append
916 * @returns #FALSE if not enough memory.
919 _dbus_string_append (DBusString *str,
922 unsigned long buffer_len;
924 DBUS_STRING_PREAMBLE (str);
925 _dbus_assert (buffer != NULL);
927 buffer_len = strlen (buffer);
928 if (buffer_len > (unsigned long) real->max_length)
931 return append (real, buffer, buffer_len);
935 * Appends 4 bytes aligned on a 4 byte boundary
936 * with any alignment padding initialized to 0.
938 * @param str the DBusString
939 * @param octets 4 bytes to append
940 * @returns #FALSE if not enough memory.
943 _dbus_string_append_4_aligned (DBusString *str,
944 const unsigned char octets[4])
947 DBUS_STRING_PREAMBLE (str);
949 if (!align_length_then_lengthen (str, 4, 4))
952 p = (dbus_uint32_t*) (real->str + (real->len - 4));
953 *p = *((dbus_uint32_t*)octets);
959 * Appends 8 bytes aligned on an 8 byte boundary
960 * with any alignment padding initialized to 0.
962 * @param str the DBusString
963 * @param octets 4 bytes to append
964 * @returns #FALSE if not enough memory.
967 _dbus_string_append_8_aligned (DBusString *str,
968 const unsigned char octets[8])
970 #ifdef DBUS_HAVE_INT64
972 DBUS_STRING_PREAMBLE (str);
974 if (!align_length_then_lengthen (str, 8, 8))
977 p = (dbus_uint64_t*) (real->str + (real->len - 8));
978 *p = *((dbus_uint64_t*)octets);
981 DBUS_STRING_PREAMBLE (str);
983 if (!align_length_then_lengthen (str, 8, 8))
986 p = real->str + (real->len - 8);
996 _dbus_assert (p == (real->str + real->len));
1003 * Appends a printf-style formatted string
1004 * to the #DBusString.
1006 * @param str the string
1007 * @param format printf format
1008 * @param args variable argument list
1009 * @returns #FALSE if no memory
1012 _dbus_string_append_printf_valist (DBusString *str,
1020 DBUS_STRING_PREAMBLE (str);
1022 DBUS_VA_COPY (args_copy, args);
1024 /* Measure the message length without terminating nul */
1025 len = vsnprintf (&c, 1, format, args);
1027 if (!_dbus_string_lengthen (str, len))
1029 /* don't leak the copy */
1034 vsprintf (real->str + (real->len - len),
1043 * Appends a printf-style formatted string
1044 * to the #DBusString.
1046 * @param str the string
1047 * @param format printf format
1048 * @returns #FALSE if no memory
1051 _dbus_string_append_printf (DBusString *str,
1058 va_start (args, format);
1059 retval = _dbus_string_append_printf_valist (str, format, args);
1066 * Appends block of bytes with the given length to a DBusString.
1068 * @param str the DBusString
1069 * @param buffer the bytes to append
1070 * @param len the number of bytes to append
1071 * @returns #FALSE if not enough memory.
1074 _dbus_string_append_len (DBusString *str,
1078 DBUS_STRING_PREAMBLE (str);
1079 _dbus_assert (buffer != NULL);
1080 _dbus_assert (len >= 0);
1082 return append (real, buffer, len);
1086 * Appends a single byte to the string, returning #FALSE
1087 * if not enough memory.
1089 * @param str the string
1090 * @param byte the byte to append
1091 * @returns #TRUE on success
1094 _dbus_string_append_byte (DBusString *str,
1097 DBUS_STRING_PREAMBLE (str);
1099 if (!set_length (real, real->len + 1))
1102 real->str[real->len-1] = byte;
1108 * Appends a single Unicode character, encoding the character
1111 * @param str the string
1112 * @param ch the Unicode character
1115 _dbus_string_append_unichar (DBusString *str,
1123 DBUS_STRING_PREAMBLE (str);
1125 /* this code is from GLib but is pretty standard I think */
1134 else if (ch < 0x800)
1139 else if (ch < 0x10000)
1144 else if (ch < 0x200000)
1149 else if (ch < 0x4000000)
1160 if (len > (real->max_length - real->len))
1161 return FALSE; /* real->len + len would overflow */
1163 if (!set_length (real, real->len + len))
1166 out = real->str + (real->len - len);
1168 for (i = len - 1; i > 0; --i)
1170 out[i] = (ch & 0x3f) | 0x80;
1173 out[0] = ch | first;
1179 delete (DBusRealString *real,
1186 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1188 real->str[real->len] = '\0';
1192 * Deletes a segment of a DBusString with length len starting at
1193 * start. (Hint: to clear an entire string, setting length to 0
1194 * with _dbus_string_set_length() is easier.)
1196 * @param str the DBusString
1197 * @param start where to start deleting
1198 * @param len the number of bytes to delete
1201 _dbus_string_delete (DBusString *str,
1205 DBUS_STRING_PREAMBLE (str);
1206 _dbus_assert (start >= 0);
1207 _dbus_assert (len >= 0);
1208 _dbus_assert (start <= real->len);
1209 _dbus_assert (len <= real->len - start);
1211 delete (real, start, len);
1215 copy (DBusRealString *source,
1218 DBusRealString *dest,
1224 if (!open_gap (len, dest, insert_at))
1227 memcpy (dest->str + insert_at,
1228 source->str + start,
1235 * Checks assertions for two strings we're copying a segment between,
1236 * and declares real_source/real_dest variables.
1238 * @param source the source string
1239 * @param start the starting offset
1240 * @param dest the dest string
1241 * @param insert_at where the copied segment is inserted
1243 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1244 DBusRealString *real_source = (DBusRealString*) source; \
1245 DBusRealString *real_dest = (DBusRealString*) dest; \
1246 _dbus_assert ((source) != (dest)); \
1247 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1248 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1249 _dbus_assert (!real_dest->constant); \
1250 _dbus_assert (!real_dest->locked); \
1251 _dbus_assert ((start) >= 0); \
1252 _dbus_assert ((start) <= real_source->len); \
1253 _dbus_assert ((insert_at) >= 0); \
1254 _dbus_assert ((insert_at) <= real_dest->len)
1257 * Moves the end of one string into another string. Both strings
1258 * must be initialized, valid strings.
1260 * @param source the source string
1261 * @param start where to chop off the source string
1262 * @param dest the destination string
1263 * @param insert_at where to move the chopped-off part of source string
1264 * @returns #FALSE if not enough memory
1267 _dbus_string_move (DBusString *source,
1272 DBusRealString *real_source = (DBusRealString*) source;
1273 _dbus_assert (start <= real_source->len);
1275 return _dbus_string_move_len (source, start,
1276 real_source->len - start,
1281 * Like _dbus_string_move(), but does not delete the section
1282 * of the source string that's copied to the dest string.
1284 * @param source the source string
1285 * @param start where to start copying the source string
1286 * @param dest the destination string
1287 * @param insert_at where to place the copied part of source string
1288 * @returns #FALSE if not enough memory
1291 _dbus_string_copy (const DBusString *source,
1296 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1298 return copy (real_source, start,
1299 real_source->len - start,
1305 * Like _dbus_string_move(), but can move a segment from
1306 * the middle of the source string.
1308 * @todo this doesn't do anything with max_length field.
1309 * we should probably just kill the max_length field though.
1311 * @param source the source string
1312 * @param start first byte of source string to move
1313 * @param len length of segment to move
1314 * @param dest the destination string
1315 * @param insert_at where to move the bytes from the source string
1316 * @returns #FALSE if not enough memory
1319 _dbus_string_move_len (DBusString *source,
1326 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1327 _dbus_assert (len >= 0);
1328 _dbus_assert ((start + len) <= real_source->len);
1335 else if (start == 0 &&
1336 len == real_source->len &&
1337 real_dest->len == 0)
1339 /* Short-circuit moving an entire existing string to an empty string
1340 * by just swapping the buffers.
1342 /* we assume ->constant doesn't matter as you can't have
1343 * a constant string involved in a move.
1345 #define ASSIGN_DATA(a, b) do { \
1346 (a)->str = (b)->str; \
1347 (a)->len = (b)->len; \
1348 (a)->allocated = (b)->allocated; \
1349 (a)->align_offset = (b)->align_offset; \
1354 ASSIGN_DATA (&tmp, real_source);
1355 ASSIGN_DATA (real_source, real_dest);
1356 ASSIGN_DATA (real_dest, &tmp);
1362 if (!copy (real_source, start, len,
1367 delete (real_source, start,
1375 * Like _dbus_string_copy(), but can copy a segment from the middle of
1376 * the source string.
1378 * @param source the source string
1379 * @param start where to start copying the source string
1380 * @param len length of segment to copy
1381 * @param dest the destination string
1382 * @param insert_at where to place the copied segment of source string
1383 * @returns #FALSE if not enough memory
1386 _dbus_string_copy_len (const DBusString *source,
1392 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1393 _dbus_assert (len >= 0);
1394 _dbus_assert (start <= real_source->len);
1395 _dbus_assert (len <= real_source->len - start);
1397 return copy (real_source, start, len,
1403 * Replaces a segment of dest string with a segment of source string.
1405 * @todo optimize the case where the two lengths are the same, and
1406 * avoid memmoving the data in the trailing part of the string twice.
1408 * @todo avoid inserting the source into dest, then deleting
1409 * the replaced chunk of dest (which creates a potentially large
1410 * intermediate string). Instead, extend the replaced chunk
1411 * of dest with padding to the same size as the source chunk,
1412 * then copy in the source bytes.
1414 * @param source the source string
1415 * @param start where to start copying the source string
1416 * @param len length of segment to copy
1417 * @param dest the destination string
1418 * @param replace_at start of segment of dest string to replace
1419 * @param replace_len length of segment of dest string to replace
1420 * @returns #FALSE if not enough memory
1424 _dbus_string_replace_len (const DBusString *source,
1431 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1432 _dbus_assert (len >= 0);
1433 _dbus_assert (start <= real_source->len);
1434 _dbus_assert (len <= real_source->len - start);
1435 _dbus_assert (replace_at >= 0);
1436 _dbus_assert (replace_at <= real_dest->len);
1437 _dbus_assert (replace_len <= real_dest->len - replace_at);
1439 if (!copy (real_source, start, len,
1440 real_dest, replace_at))
1443 delete (real_dest, replace_at + len, replace_len);
1448 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1449 * Pennington, and Tom Tromey are the authors and authorized relicense.
1452 /** computes length and mask of a unicode character
1453 * @param Char the char
1454 * @param Mask the mask variable to assign to
1455 * @param Len the length variable to assign to
1457 #define UTF8_COMPUTE(Char, Mask, Len) \
1463 else if ((Char & 0xe0) == 0xc0) \
1468 else if ((Char & 0xf0) == 0xe0) \
1473 else if ((Char & 0xf8) == 0xf0) \
1478 else if ((Char & 0xfc) == 0xf8) \
1483 else if ((Char & 0xfe) == 0xfc) \
1492 * computes length of a unicode character in UTF-8
1493 * @param Char the char
1495 #define UTF8_LENGTH(Char) \
1496 ((Char) < 0x80 ? 1 : \
1497 ((Char) < 0x800 ? 2 : \
1498 ((Char) < 0x10000 ? 3 : \
1499 ((Char) < 0x200000 ? 4 : \
1500 ((Char) < 0x4000000 ? 5 : 6)))))
1503 * Gets a UTF-8 value.
1505 * @param Result variable for extracted unicode char.
1506 * @param Chars the bytes to decode
1507 * @param Count counter variable
1508 * @param Mask mask for this char
1509 * @param Len length for this char in bytes
1511 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1512 (Result) = (Chars)[0] & (Mask); \
1513 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1515 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1521 (Result) |= ((Chars)[(Count)] & 0x3f); \
1525 * Check whether a unicode char is in a valid range.
1527 * @param Char the character
1529 #define UNICODE_VALID(Char) \
1530 ((Char) < 0x110000 && \
1531 (((Char) & 0xFFFFF800) != 0xD800) && \
1532 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1533 ((Char) & 0xFFFF) != 0xFFFF)
1536 * Gets a unicode character from a UTF-8 string. Does no validation;
1537 * you must verify that the string is valid UTF-8 in advance and must
1538 * pass in the start of a character.
1540 * @param str the string
1541 * @param start the start of the UTF-8 character.
1542 * @param ch_return location to return the character
1543 * @param end_return location to return the byte index of next character
1546 _dbus_string_get_unichar (const DBusString *str,
1548 dbus_unichar_t *ch_return,
1552 dbus_unichar_t result;
1555 DBUS_CONST_STRING_PREAMBLE (str);
1556 _dbus_assert (start >= 0);
1557 _dbus_assert (start <= real->len);
1562 *end_return = real->len;
1565 p = real->str + start;
1568 UTF8_COMPUTE (c, mask, len);
1571 UTF8_GET (result, p, i, mask, len);
1573 if (result == (dbus_unichar_t)-1)
1577 *ch_return = result;
1579 *end_return = start + len;
1583 * Finds the given substring in the string,
1584 * returning #TRUE and filling in the byte index
1585 * where the substring was found, if it was found.
1586 * Returns #FALSE if the substring wasn't found.
1587 * Sets *start to the length of the string if the substring
1590 * @param str the string
1591 * @param start where to start looking
1592 * @param substr the substring
1593 * @param found return location for where it was found, or #NULL
1594 * @returns #TRUE if found
1597 _dbus_string_find (const DBusString *str,
1602 return _dbus_string_find_to (str, start,
1603 ((const DBusRealString*)str)->len,
1608 * Finds the given substring in the string,
1609 * up to a certain position,
1610 * returning #TRUE and filling in the byte index
1611 * where the substring was found, if it was found.
1612 * Returns #FALSE if the substring wasn't found.
1613 * Sets *start to the length of the string if the substring
1616 * @param str the string
1617 * @param start where to start looking
1618 * @param end where to stop looking
1619 * @param substr the substring
1620 * @param found return location for where it was found, or #NULL
1621 * @returns #TRUE if found
1624 _dbus_string_find_to (const DBusString *str,
1631 DBUS_CONST_STRING_PREAMBLE (str);
1632 _dbus_assert (substr != NULL);
1633 _dbus_assert (start <= real->len);
1634 _dbus_assert (start >= 0);
1635 _dbus_assert (substr != NULL);
1636 _dbus_assert (end <= real->len);
1637 _dbus_assert (start <= end);
1639 /* we always "find" an empty string */
1640 if (*substr == '\0')
1650 if (real->str[i] == substr[0])
1656 if (substr[j - i] == '\0')
1658 else if (real->str[j] != substr[j - i])
1664 if (substr[j - i] == '\0')
1682 * Find the given byte scanning backward from the given start.
1683 * Sets *found to -1 if the byte is not found.
1685 * @param str the string
1686 * @param start the place to start scanning (will not find the byte at this point)
1687 * @param byte the byte to find
1688 * @param found return location for where it was found
1689 * @returns #TRUE if found
1692 _dbus_string_find_byte_backward (const DBusString *str,
1698 DBUS_CONST_STRING_PREAMBLE (str);
1699 _dbus_assert (start <= real->len);
1700 _dbus_assert (start >= 0);
1701 _dbus_assert (found != NULL);
1706 if (real->str[i] == byte)
1719 * Finds a blank (space or tab) in the string. Returns #TRUE
1720 * if found, #FALSE otherwise. If a blank is not found sets
1721 * *found to the length of the string.
1723 * @param str the string
1724 * @param start byte index to start looking
1725 * @param found place to store the location of the first blank
1726 * @returns #TRUE if a blank was found
1729 _dbus_string_find_blank (const DBusString *str,
1734 DBUS_CONST_STRING_PREAMBLE (str);
1735 _dbus_assert (start <= real->len);
1736 _dbus_assert (start >= 0);
1739 while (i < real->len)
1741 if (real->str[i] == ' ' ||
1742 real->str[i] == '\t')
1759 * Skips blanks from start, storing the first non-blank in *end
1760 * (blank is space or tab).
1762 * @param str the string
1763 * @param start where to start
1764 * @param end where to store the first non-blank byte index
1767 _dbus_string_skip_blank (const DBusString *str,
1772 DBUS_CONST_STRING_PREAMBLE (str);
1773 _dbus_assert (start <= real->len);
1774 _dbus_assert (start >= 0);
1777 while (i < real->len)
1779 if (!(real->str[i] == ' ' ||
1780 real->str[i] == '\t'))
1786 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1787 real->str[i] == '\t'));
1794 * Skips whitespace from start, storing the first non-whitespace in *end.
1795 * (whitespace is space, tab, newline, CR).
1797 * @param str the string
1798 * @param start where to start
1799 * @param end where to store the first non-whitespace byte index
1802 _dbus_string_skip_white (const DBusString *str,
1807 DBUS_CONST_STRING_PREAMBLE (str);
1808 _dbus_assert (start <= real->len);
1809 _dbus_assert (start >= 0);
1812 while (i < real->len)
1814 if (!(real->str[i] == ' ' ||
1815 real->str[i] == '\n' ||
1816 real->str[i] == '\r' ||
1817 real->str[i] == '\t'))
1823 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1824 real->str[i] == '\t'));
1831 * Assigns a newline-terminated or \\r\\n-terminated line from the front
1832 * of the string to the given dest string. The dest string's previous
1833 * contents are deleted. If the source string contains no newline,
1834 * moves the entire source string to the dest string.
1836 * @todo owen correctly notes that this is a stupid function (it was
1837 * written purely for test code,
1838 * e.g. dbus-message-builder.c). Probably should be enforced as test
1839 * code only with #ifdef DBUS_BUILD_TESTS
1841 * @param source the source string
1842 * @param dest the destination string (contents are replaced)
1843 * @returns #FALSE if no memory, or source has length 0
1846 _dbus_string_pop_line (DBusString *source,
1850 dbus_bool_t have_newline;
1852 _dbus_string_set_length (dest, 0);
1855 if (_dbus_string_find (source, 0, "\n", &eol))
1857 have_newline = TRUE;
1858 eol += 1; /* include newline */
1862 eol = _dbus_string_get_length (source);
1863 have_newline = FALSE;
1867 return FALSE; /* eof */
1869 if (!_dbus_string_move_len (source, 0, eol,
1875 /* dump the newline and the \r if we have one */
1878 dbus_bool_t have_cr;
1880 _dbus_assert (_dbus_string_get_length (dest) > 0);
1882 if (_dbus_string_get_length (dest) > 1 &&
1883 _dbus_string_get_byte (dest,
1884 _dbus_string_get_length (dest) - 2) == '\r')
1889 _dbus_string_set_length (dest,
1890 _dbus_string_get_length (dest) -
1898 * Deletes up to and including the first blank space
1901 * @param str the string
1904 _dbus_string_delete_first_word (DBusString *str)
1908 if (_dbus_string_find_blank (str, 0, &i))
1909 _dbus_string_skip_blank (str, i, &i);
1911 _dbus_string_delete (str, 0, i);
1915 * Deletes any leading blanks in the string
1917 * @param str the string
1920 _dbus_string_delete_leading_blanks (DBusString *str)
1924 _dbus_string_skip_blank (str, 0, &i);
1927 _dbus_string_delete (str, 0, i);
1931 * Tests two DBusString for equality.
1933 * @todo memcmp is probably faster
1935 * @param a first string
1936 * @param b second string
1937 * @returns #TRUE if equal
1940 _dbus_string_equal (const DBusString *a,
1941 const DBusString *b)
1943 const unsigned char *ap;
1944 const unsigned char *bp;
1945 const unsigned char *a_end;
1946 const DBusRealString *real_a = (const DBusRealString*) a;
1947 const DBusRealString *real_b = (const DBusRealString*) b;
1948 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1949 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1951 if (real_a->len != real_b->len)
1956 a_end = real_a->str + real_a->len;
1970 * Tests two DBusString for equality up to the given length.
1972 * @todo write a unit test
1974 * @todo memcmp is probably faster
1976 * @param a first string
1977 * @param b second string
1978 * @param len the lengh
1979 * @returns #TRUE if equal for the given number of bytes
1982 _dbus_string_equal_len (const DBusString *a,
1983 const DBusString *b,
1986 const unsigned char *ap;
1987 const unsigned char *bp;
1988 const unsigned char *a_end;
1989 const DBusRealString *real_a = (const DBusRealString*) a;
1990 const DBusRealString *real_b = (const DBusRealString*) b;
1991 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1992 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1994 if (real_a->len != real_b->len &&
1995 (real_a->len < len || real_b->len < len))
2000 a_end = real_a->str + MIN (real_a->len, len);
2014 * Checks whether a string is equal to a C string.
2016 * @param a the string
2017 * @param c_str the C string
2018 * @returns #TRUE if equal
2021 _dbus_string_equal_c_str (const DBusString *a,
2024 const unsigned char *ap;
2025 const unsigned char *bp;
2026 const unsigned char *a_end;
2027 const DBusRealString *real_a = (const DBusRealString*) a;
2028 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2029 _dbus_assert (c_str != NULL);
2032 bp = (const unsigned char*) c_str;
2033 a_end = real_a->str + real_a->len;
2034 while (ap != a_end && *bp)
2043 if (ap != a_end || *bp)
2050 * Checks whether a string starts with the given C string.
2052 * @param a the string
2053 * @param c_str the C string
2054 * @returns #TRUE if string starts with it
2057 _dbus_string_starts_with_c_str (const DBusString *a,
2060 const unsigned char *ap;
2061 const unsigned char *bp;
2062 const unsigned char *a_end;
2063 const DBusRealString *real_a = (const DBusRealString*) a;
2064 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2065 _dbus_assert (c_str != NULL);
2068 bp = (const unsigned char*) c_str;
2069 a_end = real_a->str + real_a->len;
2070 while (ap != a_end && *bp)
2086 * Returns whether a string ends with the given suffix
2088 * @todo memcmp might make this faster.
2090 * @param a the string
2091 * @param c_str the C-style string
2092 * @returns #TRUE if the string ends with the suffix
2095 _dbus_string_ends_with_c_str (const DBusString *a,
2098 const unsigned char *ap;
2099 const unsigned char *bp;
2100 const unsigned char *a_end;
2101 unsigned long c_str_len;
2102 const DBusRealString *real_a = (const DBusRealString*) a;
2103 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2104 _dbus_assert (c_str != NULL);
2106 c_str_len = strlen (c_str);
2107 if (((unsigned long)real_a->len) < c_str_len)
2110 ap = real_a->str + (real_a->len - c_str_len);
2111 bp = (const unsigned char*) c_str;
2112 a_end = real_a->str + real_a->len;
2122 _dbus_assert (*ap == '\0');
2123 _dbus_assert (*bp == '\0');
2129 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2130 * encoded. (Each byte is two hex digits.)
2132 * @param source the string to encode
2133 * @param start byte index to start encoding
2134 * @param dest string where encoded data should be placed
2135 * @param insert_at where to place encoded data
2136 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2139 _dbus_string_hex_encode (const DBusString *source,
2145 const char hexdigits[16] = {
2146 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2147 'a', 'b', 'c', 'd', 'e', 'f'
2149 const unsigned char *p;
2150 const unsigned char *end;
2153 _dbus_assert (start <= _dbus_string_get_length (source));
2155 if (!_dbus_string_init (&result))
2160 p = (const unsigned char*) _dbus_string_get_const_data (source);
2161 end = p + _dbus_string_get_length (source);
2166 if (!_dbus_string_append_byte (&result,
2167 hexdigits[(*p >> 4)]))
2170 if (!_dbus_string_append_byte (&result,
2171 hexdigits[(*p & 0x0f)]))
2177 if (!_dbus_string_move (&result, 0, dest, insert_at))
2183 _dbus_string_free (&result);
2188 * Decodes a string from hex encoding.
2190 * @param source the string to decode
2191 * @param start byte index to start decode
2192 * @param end_return return location of the end of the hex data, or #NULL
2193 * @param dest string where decoded data should be placed
2194 * @param insert_at where to place decoded data
2195 * @returns #TRUE if decoding was successful, #FALSE if no memory.
2198 _dbus_string_hex_decode (const DBusString *source,
2205 const unsigned char *p;
2206 const unsigned char *end;
2208 dbus_bool_t high_bits;
2210 _dbus_assert (start <= _dbus_string_get_length (source));
2212 if (!_dbus_string_init (&result))
2218 p = (const unsigned char*) _dbus_string_get_const_data (source);
2219 end = p + _dbus_string_get_length (source);
2288 if (!_dbus_string_append_byte (&result,
2297 len = _dbus_string_get_length (&result);
2299 b = _dbus_string_get_byte (&result, len - 1);
2303 _dbus_string_set_byte (&result, len - 1, b);
2306 high_bits = !high_bits;
2312 if (!_dbus_string_move (&result, 0, dest, insert_at))
2316 *end_return = p - (const unsigned char*) _dbus_string_get_const_data (source);
2321 _dbus_string_free (&result);
2326 * Checks that the given range of the string is valid ASCII with no
2327 * nul bytes. If the given range is not entirely contained in the
2328 * string, returns #FALSE.
2330 * @todo this is inconsistent with most of DBusString in that
2331 * it allows a start,len range that isn't in the string.
2333 * @param str the string
2334 * @param start first byte index to check
2335 * @param len number of bytes to check
2336 * @returns #TRUE if the byte range exists and is all valid ASCII
2339 _dbus_string_validate_ascii (const DBusString *str,
2343 const unsigned char *s;
2344 const unsigned char *end;
2345 DBUS_CONST_STRING_PREAMBLE (str);
2346 _dbus_assert (start >= 0);
2347 _dbus_assert (start <= real->len);
2348 _dbus_assert (len >= 0);
2350 if (len > real->len - start)
2353 s = real->str + start;
2357 if (_DBUS_UNLIKELY (*s == '\0' ||
2358 ((*s & ~0x7f) != 0)))
2368 * Checks that the given range of the string is valid UTF-8. If the
2369 * given range is not entirely contained in the string, returns
2370 * #FALSE. If the string contains any nul bytes in the given range,
2371 * returns #FALSE. If the start and start+len are not on character
2372 * boundaries, returns #FALSE.
2374 * @todo this is inconsistent with most of DBusString in that
2375 * it allows a start,len range that isn't in the string.
2377 * @param str the string
2378 * @param start first byte index to check
2379 * @param len number of bytes to check
2380 * @returns #TRUE if the byte range exists and is all valid UTF-8
2383 _dbus_string_validate_utf8 (const DBusString *str,
2387 const unsigned char *p;
2388 const unsigned char *end;
2389 DBUS_CONST_STRING_PREAMBLE (str);
2390 _dbus_assert (start >= 0);
2391 _dbus_assert (start <= real->len);
2392 _dbus_assert (len >= 0);
2394 /* we are doing _DBUS_UNLIKELY() here which might be
2395 * dubious in a generic library like GLib, but in D-BUS
2396 * we know we're validating messages and that it would
2397 * only be evil/broken apps that would have invalid
2398 * UTF-8. Also, this function seems to be a performance
2399 * bottleneck in profiles.
2402 if (_DBUS_UNLIKELY (len > real->len - start))
2405 p = real->str + start;
2410 int i, mask = 0, char_len;
2411 dbus_unichar_t result;
2412 unsigned char c = (unsigned char) *p;
2414 UTF8_COMPUTE (c, mask, char_len);
2416 if (_DBUS_UNLIKELY (char_len == -1))
2419 /* check that the expected number of bytes exists in the remaining length */
2420 if (_DBUS_UNLIKELY ((end - p) < char_len))
2423 UTF8_GET (result, p, i, mask, char_len);
2425 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* Check for overlong UTF-8 */
2428 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1))
2431 if (_DBUS_UNLIKELY (!UNICODE_VALID (result)))
2437 /* See that we covered the entire length if a length was
2440 if (_DBUS_UNLIKELY (p != end))
2447 * Checks that the given range of the string is all nul bytes. If the
2448 * given range is not entirely contained in the string, returns
2451 * @todo this is inconsistent with most of DBusString in that
2452 * it allows a start,len range that isn't in the string.
2454 * @param str the string
2455 * @param start first byte index to check
2456 * @param len number of bytes to check
2457 * @returns #TRUE if the byte range exists and is all nul bytes
2460 _dbus_string_validate_nul (const DBusString *str,
2464 const unsigned char *s;
2465 const unsigned char *end;
2466 DBUS_CONST_STRING_PREAMBLE (str);
2467 _dbus_assert (start >= 0);
2468 _dbus_assert (len >= 0);
2469 _dbus_assert (start <= real->len);
2471 if (len > real->len - start)
2474 s = real->str + start;
2478 if (_DBUS_UNLIKELY (*s != '\0'))
2487 * Checks that the given range of the string is a valid object path
2488 * name in the D-BUS protocol. This includes a length restriction,
2489 * etc., see the specification. It does not validate UTF-8, that has
2490 * to be done separately for now.
2492 * @todo this is inconsistent with most of DBusString in that
2493 * it allows a start,len range that isn't in the string.
2495 * @todo change spec to disallow more things, such as spaces in the
2498 * @param str the string
2499 * @param start first byte index to check
2500 * @param len number of bytes to check
2501 * @returns #TRUE if the byte range exists and is a valid name
2504 _dbus_string_validate_path (const DBusString *str,
2508 const unsigned char *s;
2509 const unsigned char *end;
2510 const unsigned char *last_slash;
2512 DBUS_CONST_STRING_PREAMBLE (str);
2513 _dbus_assert (start >= 0);
2514 _dbus_assert (len >= 0);
2515 _dbus_assert (start <= real->len);
2517 if (len > real->len - start)
2520 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2526 s = real->str + start;
2538 if ((s - last_slash) < 2)
2539 return FALSE; /* no empty path components allowed */
2547 if ((end - last_slash) < 2 &&
2549 return FALSE; /* trailing slash not allowed unless the string is "/" */
2554 #define VALID_INITIAL_NAME_CHARACTER(c) \
2555 ( ((c) >= 'A' && (c) <= 'Z') || \
2556 ((c) >= 'a' && (c) <= 'z') || \
2559 #define VALID_NAME_CHARACTER(c) \
2560 ( ((c) >= '0' && (c) <= '9') || \
2561 ((c) >= 'A' && (c) <= 'Z') || \
2562 ((c) >= 'a' && (c) <= 'z') || \
2566 * Checks that the given range of the string is a valid interface name
2567 * in the D-BUS protocol. This includes a length restriction and an
2568 * ASCII subset, see the specification.
2570 * @todo this is inconsistent with most of DBusString in that
2571 * it allows a start,len range that isn't in the string.
2573 * @param str the string
2574 * @param start first byte index to check
2575 * @param len number of bytes to check
2576 * @returns #TRUE if the byte range exists and is a valid name
2579 _dbus_string_validate_interface (const DBusString *str,
2583 const unsigned char *s;
2584 const unsigned char *end;
2585 const unsigned char *iface;
2586 const unsigned char *last_dot;
2588 DBUS_CONST_STRING_PREAMBLE (str);
2589 _dbus_assert (start >= 0);
2590 _dbus_assert (len >= 0);
2591 _dbus_assert (start <= real->len);
2593 if (len > real->len - start)
2596 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2603 iface = real->str + start;
2607 /* check special cases of first char so it doesn't have to be done
2608 * in the loop. Note we know len > 0
2610 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
2612 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
2621 if (_DBUS_UNLIKELY ((s + 1) == end))
2623 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
2626 ++s; /* we just validated the next char, so skip two */
2628 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
2636 if (_DBUS_UNLIKELY (last_dot == NULL))
2643 * Checks that the given range of the string is a valid member name
2644 * in the D-BUS protocol. This includes a length restriction, etc.,
2645 * see the specification.
2647 * @todo this is inconsistent with most of DBusString in that
2648 * it allows a start,len range that isn't in the string.
2650 * @param str the string
2651 * @param start first byte index to check
2652 * @param len number of bytes to check
2653 * @returns #TRUE if the byte range exists and is a valid name
2656 _dbus_string_validate_member (const DBusString *str,
2660 const unsigned char *s;
2661 const unsigned char *end;
2662 const unsigned char *member;
2664 DBUS_CONST_STRING_PREAMBLE (str);
2665 _dbus_assert (start >= 0);
2666 _dbus_assert (len >= 0);
2667 _dbus_assert (start <= real->len);
2669 if (len > real->len - start)
2672 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2678 member = real->str + start;
2682 /* check special cases of first char so it doesn't have to be done
2683 * in the loop. Note we know len > 0
2686 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
2693 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
2705 * Checks that the given range of the string is a valid error name
2706 * in the D-BUS protocol. This includes a length restriction, etc.,
2707 * see the specification.
2709 * @todo this is inconsistent with most of DBusString in that
2710 * it allows a start,len range that isn't in the string.
2712 * @param str the string
2713 * @param start first byte index to check
2714 * @param len number of bytes to check
2715 * @returns #TRUE if the byte range exists and is a valid name
2718 _dbus_string_validate_error_name (const DBusString *str,
2722 /* Same restrictions as interface name at the moment */
2723 return _dbus_string_validate_interface (str, start, len);
2726 /* This assumes the first char exists and is ':' */
2728 _dbus_string_validate_base_service (const DBusString *str,
2732 const unsigned char *s;
2733 const unsigned char *end;
2734 const unsigned char *service;
2736 DBUS_CONST_STRING_PREAMBLE (str);
2737 _dbus_assert (start >= 0);
2738 _dbus_assert (len >= 0);
2739 _dbus_assert (start <= real->len);
2741 if (len > real->len - start)
2744 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2747 _dbus_assert (len > 0);
2749 service = real->str + start;
2750 end = service + len;
2751 _dbus_assert (*service == ':');
2758 if (_DBUS_UNLIKELY ((s + 1) == end))
2760 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*(s + 1))))
2762 ++s; /* we just validated the next char, so skip two */
2764 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
2776 * Checks that the given range of the string is a valid service name
2777 * in the D-BUS protocol. This includes a length restriction, etc.,
2778 * see the specification.
2780 * @todo this is inconsistent with most of DBusString in that
2781 * it allows a start,len range that isn't in the string.
2783 * @param str the string
2784 * @param start first byte index to check
2785 * @param len number of bytes to check
2786 * @returns #TRUE if the byte range exists and is a valid name
2789 _dbus_string_validate_service (const DBusString *str,
2793 if (_DBUS_UNLIKELY (len == 0))
2795 if (_dbus_string_get_byte (str, start) == ':')
2796 return _dbus_string_validate_base_service (str, start, len);
2798 return _dbus_string_validate_interface (str, start, len);
2802 * Clears all allocated bytes in the string to zero.
2804 * @param str the string
2807 _dbus_string_zero (DBusString *str)
2809 DBUS_STRING_PREAMBLE (str);
2811 memset (real->str - real->align_offset, '\0', real->allocated);
2815 #ifdef DBUS_BUILD_TESTS
2816 #include "dbus-test.h"
2820 * Parses a basic type defined by type contained in a DBusString. The
2821 * end_return parameter may be #NULL if you aren't interested in it. The
2822 * type is parsed and stored in value_return. Return parameters are not
2823 * initialized if the function returns #FALSE.
2825 * @param str the string
2826 * @param type the type of the basic type
2827 * @param start the byte index of the start of the type
2828 * @param value_return return location of the value or #NULL
2829 * @param end_return return location of the end of the type, or #NULL
2830 * @returns #TRUE on success
2833 _dbus_string_parse_basic_type (const DBusString *str,
2843 case DBUS_TYPE_BOOLEAN:
2845 int len = _dbus_string_get_length (str) - start;
2846 if (len >= 5 && _dbus_string_find_to (str, start, start + 5, "false", NULL))
2849 *(unsigned char *) value = TRUE;
2851 else if (len >= 4 && _dbus_string_find_to (str, start, start + 4, "true", NULL))
2854 *(unsigned char *) value = FALSE;
2857 _dbus_warn ("could not parse BOOLEAN\n");
2860 case DBUS_TYPE_BYTE:
2864 if (_dbus_string_get_byte (str, start) == '\'' &&
2865 _dbus_string_get_length (str) >= start + 4 &&
2866 _dbus_string_get_byte (str, start + 1) == '\\' &&
2867 _dbus_string_get_byte (str, start + 2) == '\'' &&
2868 _dbus_string_get_byte (str, start + 3) == '\'')
2873 else if (_dbus_string_get_byte (str, start) == '\'' &&
2874 _dbus_string_get_length (str) >= start + 3 &&
2875 _dbus_string_get_byte (str, start + 2) == '\'')
2877 val = _dbus_string_get_byte (str, start + 1);
2882 if (!_dbus_string_parse_int (str, start, &val, &end))
2883 _dbus_warn ("Failed to parse integer for BYTE\n");
2887 _dbus_warn ("A byte must be in range 0-255 not %ld\n", val);
2889 *(unsigned char *) value = val;
2892 case DBUS_TYPE_INT32:
2895 if (_dbus_string_parse_int (str, start, &val, &end))
2896 *(dbus_int32_t *)value = val;
2899 case DBUS_TYPE_UINT32:
2902 if (_dbus_string_parse_uint (str, start, &val, &end))
2903 *(dbus_uint32_t *)value = val;
2906 #ifdef DBUS_HAVE_INT64
2907 case DBUS_TYPE_INT64:
2908 case DBUS_TYPE_UINT64:
2909 /* use stroll oull */
2910 _dbus_assert_not_reached ("string -> [u]int64 not supported yet");
2912 #endif /* DBUS_HAVE_INT64 */
2913 case DBUS_TYPE_DOUBLE:
2914 _dbus_string_parse_double (str, start, value, &end);
2917 _dbus_assert_not_reached ("not a basic type");
2923 return end != start;
2927 test_max_len (DBusString *str,
2932 if (!_dbus_string_set_length (str, max_len - 1))
2933 _dbus_assert_not_reached ("setting len to one less than max should have worked");
2936 if (!_dbus_string_set_length (str, max_len))
2937 _dbus_assert_not_reached ("setting len to max len should have worked");
2939 if (_dbus_string_set_length (str, max_len + 1))
2940 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
2942 if (!_dbus_string_set_length (str, 0))
2943 _dbus_assert_not_reached ("setting len to zero should have worked");
2947 test_hex_roundtrip (const unsigned char *data,
2956 len = strlen (data);
2958 if (!_dbus_string_init (&orig))
2959 _dbus_assert_not_reached ("could not init string");
2961 if (!_dbus_string_init (&encoded))
2962 _dbus_assert_not_reached ("could not init string");
2964 if (!_dbus_string_init (&decoded))
2965 _dbus_assert_not_reached ("could not init string");
2967 if (!_dbus_string_append_len (&orig, data, len))
2968 _dbus_assert_not_reached ("couldn't append orig data");
2970 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
2971 _dbus_assert_not_reached ("could not encode");
2973 if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
2974 _dbus_assert_not_reached ("could not decode");
2976 _dbus_assert (_dbus_string_get_length (&encoded) == end);
2978 if (!_dbus_string_equal (&orig, &decoded))
2982 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
2983 _dbus_string_get_length (&orig),
2984 _dbus_string_get_length (&encoded),
2985 _dbus_string_get_length (&decoded));
2986 printf ("Original: %s\n", data);
2987 s = _dbus_string_get_const_data (&decoded);
2988 printf ("Decoded: %s\n", s);
2989 _dbus_assert_not_reached ("original string not the same as string decoded from hex");
2992 _dbus_string_free (&orig);
2993 _dbus_string_free (&encoded);
2994 _dbus_string_free (&decoded);
2997 typedef void (* TestRoundtripFunc) (const unsigned char *data,
3000 test_roundtrips (TestRoundtripFunc func)
3002 (* func) ("Hello this is a string\n", -1);
3003 (* func) ("Hello this is a string\n1", -1);
3004 (* func) ("Hello this is a string\n12", -1);
3005 (* func) ("Hello this is a string\n123", -1);
3006 (* func) ("Hello this is a string\n1234", -1);
3007 (* func) ("Hello this is a string\n12345", -1);
3011 (* func) ("123", 3);
3012 (* func) ("1234", 4);
3013 (* func) ("12345", 5);
3017 (* func) ("123", 4);
3018 (* func) ("1234", 5);
3019 (* func) ("12345", 6);
3021 unsigned char buf[512];
3025 while (i < _DBUS_N_ELEMENTS (buf))
3031 while (i < _DBUS_N_ELEMENTS (buf))
3041 * @ingroup DBusStringInternals
3042 * Unit test for DBusString.
3044 * @todo Need to write tests for _dbus_string_copy() and
3045 * _dbus_string_move() moving to/from each of start/middle/end of a
3046 * string. Also need tests for _dbus_string_move_len ()
3048 * @returns #TRUE on success.
3051 _dbus_string_test (void)
3058 int lens[] = { 0, 1, 2, 3, 4, 5, 10, 16, 17, 18, 25, 31, 32, 33, 34, 35, 63, 64, 65, 66, 67, 68, 69, 70, 71, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136 };
3061 const char *valid_paths[] = {
3067 const char *invalid_paths[] = {
3083 const char *valid_interfaces[] = {
3084 "org.freedesktop.Foo",
3086 "Blah.Blah.Blah.Blah.Blah",
3089 "a0.b1.c2.d3.e4.f5.g6",
3092 const char *invalid_interfaces[] = {
3120 const char *valid_base_services[] = {
3129 ":abce.freedesktop.blah"
3131 const char *invalid_base_services[] = {
3145 const char *valid_members[] = {
3153 const char *invalid_members[] = {
3169 while (i < _DBUS_N_ELEMENTS (lens))
3171 if (!_dbus_string_init (&str))
3172 _dbus_assert_not_reached ("failed to init string");
3174 set_max_length (&str, lens[i]);
3176 test_max_len (&str, lens[i]);
3177 _dbus_string_free (&str);
3182 /* Test shortening and setting length */
3184 while (i < _DBUS_N_ELEMENTS (lens))
3188 if (!_dbus_string_init (&str))
3189 _dbus_assert_not_reached ("failed to init string");
3191 set_max_length (&str, lens[i]);
3193 if (!_dbus_string_set_length (&str, lens[i]))
3194 _dbus_assert_not_reached ("failed to set string length");
3199 _dbus_assert (_dbus_string_get_length (&str) == j);
3202 _dbus_string_shorten (&str, 1);
3203 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
3208 _dbus_string_free (&str);
3213 /* Test appending data */
3214 if (!_dbus_string_init (&str))
3215 _dbus_assert_not_reached ("failed to init string");
3220 if (!_dbus_string_append (&str, "a"))
3221 _dbus_assert_not_reached ("failed to append string to string\n");
3223 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
3225 if (!_dbus_string_append_byte (&str, 'b'))
3226 _dbus_assert_not_reached ("failed to append byte to string\n");
3228 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
3233 _dbus_string_free (&str);
3235 /* Check steal_data */
3237 if (!_dbus_string_init (&str))
3238 _dbus_assert_not_reached ("failed to init string");
3240 if (!_dbus_string_append (&str, "Hello World"))
3241 _dbus_assert_not_reached ("could not append to string");
3243 i = _dbus_string_get_length (&str);
3245 if (!_dbus_string_steal_data (&str, &s))
3246 _dbus_assert_not_reached ("failed to steal data");
3248 _dbus_assert (_dbus_string_get_length (&str) == 0);
3249 _dbus_assert (((int)strlen (s)) == i);
3255 if (!_dbus_string_append (&str, "Hello World"))
3256 _dbus_assert_not_reached ("could not append to string");
3258 i = _dbus_string_get_length (&str);
3260 if (!_dbus_string_init (&other))
3261 _dbus_assert_not_reached ("could not init string");
3263 if (!_dbus_string_move (&str, 0, &other, 0))
3264 _dbus_assert_not_reached ("could not move");
3266 _dbus_assert (_dbus_string_get_length (&str) == 0);
3267 _dbus_assert (_dbus_string_get_length (&other) == i);
3269 if (!_dbus_string_append (&str, "Hello World"))
3270 _dbus_assert_not_reached ("could not append to string");
3272 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
3273 _dbus_assert_not_reached ("could not move");
3275 _dbus_assert (_dbus_string_get_length (&str) == 0);
3276 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3278 if (!_dbus_string_append (&str, "Hello World"))
3279 _dbus_assert_not_reached ("could not append to string");
3281 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3282 _dbus_assert_not_reached ("could not move");
3284 _dbus_assert (_dbus_string_get_length (&str) == 0);
3285 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3287 _dbus_string_free (&other);
3291 if (!_dbus_string_append (&str, "Hello World"))
3292 _dbus_assert_not_reached ("could not append to string");
3294 i = _dbus_string_get_length (&str);
3296 if (!_dbus_string_init (&other))
3297 _dbus_assert_not_reached ("could not init string");
3299 if (!_dbus_string_copy (&str, 0, &other, 0))
3300 _dbus_assert_not_reached ("could not copy");
3302 _dbus_assert (_dbus_string_get_length (&str) == i);
3303 _dbus_assert (_dbus_string_get_length (&other) == i);
3305 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
3306 _dbus_assert_not_reached ("could not copy");
3308 _dbus_assert (_dbus_string_get_length (&str) == i);
3309 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3310 _dbus_assert (_dbus_string_equal_c_str (&other,
3311 "Hello WorldHello World"));
3313 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3314 _dbus_assert_not_reached ("could not copy");
3316 _dbus_assert (_dbus_string_get_length (&str) == i);
3317 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3318 _dbus_assert (_dbus_string_equal_c_str (&other,
3319 "Hello WorldHello WorldHello World"));
3321 _dbus_string_free (&str);
3322 _dbus_string_free (&other);
3326 if (!_dbus_string_init (&str))
3327 _dbus_assert_not_reached ("failed to init string");
3329 if (!_dbus_string_append (&str, "Hello World"))
3330 _dbus_assert_not_reached ("could not append to string");
3332 i = _dbus_string_get_length (&str);
3334 if (!_dbus_string_init (&other))
3335 _dbus_assert_not_reached ("could not init string");
3337 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3338 &other, 0, _dbus_string_get_length (&other)))
3339 _dbus_assert_not_reached ("could not replace");
3341 _dbus_assert (_dbus_string_get_length (&str) == i);
3342 _dbus_assert (_dbus_string_get_length (&other) == i);
3343 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
3345 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3347 _dbus_assert_not_reached ("could not replace center space");
3349 _dbus_assert (_dbus_string_get_length (&str) == i);
3350 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3351 _dbus_assert (_dbus_string_equal_c_str (&other,
3352 "HelloHello WorldWorld"));
3355 if (!_dbus_string_replace_len (&str, 1, 1,
3357 _dbus_string_get_length (&other) - 1,
3359 _dbus_assert_not_reached ("could not replace end character");
3361 _dbus_assert (_dbus_string_get_length (&str) == i);
3362 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3363 _dbus_assert (_dbus_string_equal_c_str (&other,
3364 "HelloHello WorldWorle"));
3366 _dbus_string_free (&str);
3367 _dbus_string_free (&other);
3369 /* Check append/get unichar */
3371 if (!_dbus_string_init (&str))
3372 _dbus_assert_not_reached ("failed to init string");
3375 if (!_dbus_string_append_unichar (&str, 0xfffc))
3376 _dbus_assert_not_reached ("failed to append unichar");
3378 _dbus_string_get_unichar (&str, 0, &ch, &i);
3380 _dbus_assert (ch == 0xfffc);
3381 _dbus_assert (i == _dbus_string_get_length (&str));
3383 _dbus_string_free (&str);
3385 /* Check insert/set/get byte */
3387 if (!_dbus_string_init (&str))
3388 _dbus_assert_not_reached ("failed to init string");
3390 if (!_dbus_string_append (&str, "Hello"))
3391 _dbus_assert_not_reached ("failed to append Hello");
3393 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
3394 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
3395 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
3396 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
3397 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
3399 _dbus_string_set_byte (&str, 1, 'q');
3400 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
3402 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
3403 _dbus_assert_not_reached ("can't insert byte");
3405 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
3406 _dbus_assert_not_reached ("can't insert byte");
3408 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
3409 _dbus_assert_not_reached ("can't insert byte");
3411 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
3412 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
3413 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
3414 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
3415 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
3416 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
3417 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
3418 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
3419 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
3420 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
3421 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
3423 _dbus_string_free (&str);
3425 /* Check append/parse int/double */
3427 if (!_dbus_string_init (&str))
3428 _dbus_assert_not_reached ("failed to init string");
3430 if (!_dbus_string_append_int (&str, 27))
3431 _dbus_assert_not_reached ("failed to append int");
3433 i = _dbus_string_get_length (&str);
3435 if (!_dbus_string_parse_int (&str, 0, &v, &end))
3436 _dbus_assert_not_reached ("failed to parse int");
3438 _dbus_assert (v == 27);
3439 _dbus_assert (end == i);
3441 _dbus_string_free (&str);
3443 if (!_dbus_string_init (&str))
3444 _dbus_assert_not_reached ("failed to init string");
3446 if (!_dbus_string_append_double (&str, 50.3))
3447 _dbus_assert_not_reached ("failed to append float");
3449 i = _dbus_string_get_length (&str);
3451 if (!_dbus_string_parse_double (&str, 0, &d, &end))
3452 _dbus_assert_not_reached ("failed to parse float");
3454 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
3455 _dbus_assert (end == i);
3457 _dbus_string_free (&str);
3460 if (!_dbus_string_init (&str))
3461 _dbus_assert_not_reached ("failed to init string");
3463 if (!_dbus_string_append (&str, "Hello"))
3464 _dbus_assert_not_reached ("couldn't append to string");
3466 if (!_dbus_string_find (&str, 0, "He", &i))
3467 _dbus_assert_not_reached ("didn't find 'He'");
3468 _dbus_assert (i == 0);
3470 if (!_dbus_string_find (&str, 0, "Hello", &i))
3471 _dbus_assert_not_reached ("didn't find 'Hello'");
3472 _dbus_assert (i == 0);
3474 if (!_dbus_string_find (&str, 0, "ello", &i))
3475 _dbus_assert_not_reached ("didn't find 'ello'");
3476 _dbus_assert (i == 1);
3478 if (!_dbus_string_find (&str, 0, "lo", &i))
3479 _dbus_assert_not_reached ("didn't find 'lo'");
3480 _dbus_assert (i == 3);
3482 if (!_dbus_string_find (&str, 2, "lo", &i))
3483 _dbus_assert_not_reached ("didn't find 'lo'");
3484 _dbus_assert (i == 3);
3486 if (_dbus_string_find (&str, 4, "lo", &i))
3487 _dbus_assert_not_reached ("did find 'lo'");
3489 if (!_dbus_string_find (&str, 0, "l", &i))
3490 _dbus_assert_not_reached ("didn't find 'l'");
3491 _dbus_assert (i == 2);
3493 if (!_dbus_string_find (&str, 0, "H", &i))
3494 _dbus_assert_not_reached ("didn't find 'H'");
3495 _dbus_assert (i == 0);
3497 if (!_dbus_string_find (&str, 0, "", &i))
3498 _dbus_assert_not_reached ("didn't find ''");
3499 _dbus_assert (i == 0);
3501 if (_dbus_string_find (&str, 0, "Hello!", NULL))
3502 _dbus_assert_not_reached ("Did find 'Hello!'");
3504 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
3505 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
3507 if (_dbus_string_find (&str, 0, "ill", NULL))
3508 _dbus_assert_not_reached ("Did find 'ill'");
3510 if (_dbus_string_find (&str, 0, "q", NULL))
3511 _dbus_assert_not_reached ("Did find 'q'");
3513 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
3514 _dbus_assert_not_reached ("Didn't find 'He'");
3516 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
3517 _dbus_assert_not_reached ("Did find 'Hello'");
3519 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
3520 _dbus_assert_not_reached ("Did not find 'H'");
3521 _dbus_assert (i == 0);
3523 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
3524 _dbus_assert_not_reached ("Did not find 'o'");
3525 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
3527 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
3528 _dbus_assert_not_reached ("Did find 'o'");
3529 _dbus_assert (i == -1);
3531 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
3532 _dbus_assert_not_reached ("Did find 'e'");
3533 _dbus_assert (i == -1);
3535 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
3536 _dbus_assert_not_reached ("Didn't find 'e'");
3537 _dbus_assert (i == 1);
3539 _dbus_string_free (&str);
3542 _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
3543 if (!_dbus_string_init (&other))
3544 _dbus_assert_not_reached ("could not init string");
3546 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
3547 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
3549 _dbus_assert (end == 8);
3551 _dbus_string_free (&other);
3553 test_roundtrips (test_hex_roundtrip);
3555 /* Path validation */
3557 while (i < (int) _DBUS_N_ELEMENTS (valid_paths))
3559 _dbus_string_init_const (&str, valid_paths[i]);
3561 if (!_dbus_string_validate_path (&str, 0,
3562 _dbus_string_get_length (&str)))
3564 _dbus_warn ("Path \"%s\" should have been valid\n", valid_paths[i]);
3565 _dbus_assert_not_reached ("invalid path");
3572 while (i < (int) _DBUS_N_ELEMENTS (invalid_paths))
3574 _dbus_string_init_const (&str, invalid_paths[i]);
3576 if (_dbus_string_validate_path (&str, 0,
3577 _dbus_string_get_length (&str)))
3579 _dbus_warn ("Path \"%s\" should have been invalid\n", invalid_paths[i]);
3580 _dbus_assert_not_reached ("valid path");
3586 /* Interface validation */
3588 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
3590 _dbus_string_init_const (&str, valid_interfaces[i]);
3592 if (!_dbus_string_validate_interface (&str, 0,
3593 _dbus_string_get_length (&str)))
3595 _dbus_warn ("Interface \"%s\" should have been valid\n", valid_interfaces[i]);
3596 _dbus_assert_not_reached ("invalid interface");
3603 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
3605 _dbus_string_init_const (&str, invalid_interfaces[i]);
3607 if (_dbus_string_validate_interface (&str, 0,
3608 _dbus_string_get_length (&str)))
3610 _dbus_warn ("Interface \"%s\" should have been invalid\n", invalid_interfaces[i]);
3611 _dbus_assert_not_reached ("valid interface");
3617 /* Service validation (check that valid interfaces are valid services,
3618 * and invalid interfaces are invalid services except if they start with ':')
3621 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
3623 _dbus_string_init_const (&str, valid_interfaces[i]);
3625 if (!_dbus_string_validate_service (&str, 0,
3626 _dbus_string_get_length (&str)))
3628 _dbus_warn ("Service \"%s\" should have been valid\n", valid_interfaces[i]);
3629 _dbus_assert_not_reached ("invalid service");
3636 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
3638 if (invalid_interfaces[i][0] != ':')
3640 _dbus_string_init_const (&str, invalid_interfaces[i]);
3642 if (_dbus_string_validate_service (&str, 0,
3643 _dbus_string_get_length (&str)))
3645 _dbus_warn ("Service \"%s\" should have been invalid\n", invalid_interfaces[i]);
3646 _dbus_assert_not_reached ("valid service");
3653 /* Base service validation */
3655 while (i < (int) _DBUS_N_ELEMENTS (valid_base_services))
3657 _dbus_string_init_const (&str, valid_base_services[i]);
3659 if (!_dbus_string_validate_service (&str, 0,
3660 _dbus_string_get_length (&str)))
3662 _dbus_warn ("Service \"%s\" should have been valid\n", valid_base_services[i]);
3663 _dbus_assert_not_reached ("invalid base service");
3670 while (i < (int) _DBUS_N_ELEMENTS (invalid_base_services))
3672 _dbus_string_init_const (&str, invalid_base_services[i]);
3674 if (_dbus_string_validate_service (&str, 0,
3675 _dbus_string_get_length (&str)))
3677 _dbus_warn ("Service \"%s\" should have been invalid\n", invalid_base_services[i]);
3678 _dbus_assert_not_reached ("valid base service");
3685 /* Error name validation (currently identical to interfaces)
3688 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
3690 _dbus_string_init_const (&str, valid_interfaces[i]);
3692 if (!_dbus_string_validate_error_name (&str, 0,
3693 _dbus_string_get_length (&str)))
3695 _dbus_warn ("Error name \"%s\" should have been valid\n", valid_interfaces[i]);
3696 _dbus_assert_not_reached ("invalid error name");
3703 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
3705 if (invalid_interfaces[i][0] != ':')
3707 _dbus_string_init_const (&str, invalid_interfaces[i]);
3709 if (_dbus_string_validate_error_name (&str, 0,
3710 _dbus_string_get_length (&str)))
3712 _dbus_warn ("Error name \"%s\" should have been invalid\n", invalid_interfaces[i]);
3713 _dbus_assert_not_reached ("valid error name");
3720 /* Member validation */
3722 while (i < (int) _DBUS_N_ELEMENTS (valid_members))
3724 _dbus_string_init_const (&str, valid_members[i]);
3726 if (!_dbus_string_validate_member (&str, 0,
3727 _dbus_string_get_length (&str)))
3729 _dbus_warn ("Member \"%s\" should have been valid\n", valid_members[i]);
3730 _dbus_assert_not_reached ("invalid member");
3737 while (i < (int) _DBUS_N_ELEMENTS (invalid_members))
3739 _dbus_string_init_const (&str, invalid_members[i]);
3741 if (_dbus_string_validate_member (&str, 0,
3742 _dbus_string_get_length (&str)))
3744 _dbus_warn ("Member \"%s\" should have been invalid\n", invalid_members[i]);
3745 _dbus_assert_not_reached ("valid member");
3751 /* Validate claimed length longer than real length */
3752 _dbus_string_init_const (&str, "abc.efg");
3753 if (_dbus_string_validate_service (&str, 0, 8))
3754 _dbus_assert_not_reached ("validated too-long string");
3755 if (_dbus_string_validate_interface (&str, 0, 8))
3756 _dbus_assert_not_reached ("validated too-long string");
3757 if (_dbus_string_validate_error_name (&str, 0, 8))
3758 _dbus_assert_not_reached ("validated too-long string");
3760 _dbus_string_init_const (&str, "abc");
3761 if (_dbus_string_validate_member (&str, 0, 4))
3762 _dbus_assert_not_reached ("validated too-long string");
3764 /* Validate string exceeding max name length */
3765 if (!_dbus_string_init (&str))
3766 _dbus_assert_not_reached ("no memory");
3768 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
3769 if (!_dbus_string_append (&str, "abc.def"))
3770 _dbus_assert_not_reached ("no memory");
3772 if (_dbus_string_validate_service (&str, 0, _dbus_string_get_length (&str)))
3773 _dbus_assert_not_reached ("validated overmax string");
3774 if (_dbus_string_validate_interface (&str, 0, _dbus_string_get_length (&str)))
3775 _dbus_assert_not_reached ("validated overmax string");
3776 if (_dbus_string_validate_error_name (&str, 0, _dbus_string_get_length (&str)))
3777 _dbus_assert_not_reached ("validated overmax string");
3779 /* overlong member */
3780 _dbus_string_set_length (&str, 0);
3781 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
3782 if (!_dbus_string_append (&str, "abc"))
3783 _dbus_assert_not_reached ("no memory");
3785 if (_dbus_string_validate_member (&str, 0, _dbus_string_get_length (&str)))
3786 _dbus_assert_not_reached ("validated overmax string");
3788 /* overlong base service */
3789 _dbus_string_set_length (&str, 0);
3790 _dbus_string_append (&str, ":");
3791 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
3792 if (!_dbus_string_append (&str, "abc"))
3793 _dbus_assert_not_reached ("no memory");
3795 if (_dbus_string_validate_service (&str, 0, _dbus_string_get_length (&str)))
3796 _dbus_assert_not_reached ("validated overmax string");
3798 _dbus_string_free (&str);
3803 #endif /* DBUS_BUILD_TESTS */