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 1.2
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"
36 * @defgroup DBusString string class
37 * @ingroup DBusInternals
38 * @brief DBusString data structure
40 * Types and functions related to DBusString. DBusString is intended
41 * to be a string class that makes it hard to mess up security issues
42 * (and just in general harder to write buggy code). It should be
43 * used (or extended and then used) rather than the libc stuff in
44 * string.h. The string class is a bit inconvenient at spots because
45 * it handles out-of-memory failures and tries to be extra-robust.
47 * A DBusString has a maximum length set at initialization time; this
48 * can be used to ensure that a buffer doesn't get too big. The
49 * _dbus_string_lengthen() method checks for overflow, and for max
50 * length being exceeded.
52 * Try to avoid conversion to a plain C string, i.e. add methods on
53 * the string object instead, only convert to C string when passing
54 * things out to the public API. In particular, no sprintf, strcpy,
55 * strcat, any of that should be used. The GString feature of
56 * accepting negative numbers for "length of string" is also absent,
57 * because it could keep us from detecting bogus huge lengths. i.e. if
58 * we passed in some bogus huge length it would be taken to mean
59 * "current length of string" instead of "broken crack"
63 * @defgroup DBusStringInternals DBusString implementation details
64 * @ingroup DBusInternals
65 * @brief DBusString implementation details
67 * The guts of DBusString.
73 * We allocate 1 byte for nul termination, plus 7 bytes for possible
74 * align_offset, so we always need 8 bytes on top of the string's
75 * length to be in the allocated block.
77 #define ALLOCATION_PADDING 8
80 * This is the maximum max length (and thus also the maximum length)
83 #define MAX_MAX_LENGTH (_DBUS_INT_MAX - ALLOCATION_PADDING)
86 * Checks a bunch of assertions about a string object
88 * @param real the DBusRealString
90 #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)
93 * Checks assertions about a string object that needs to be
94 * modifiable - may not be locked or const. Also declares
95 * the "real" variable pointing to DBusRealString.
96 * @param str the string
98 #define DBUS_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
99 DBUS_GENERIC_STRING_PREAMBLE (real); \
100 _dbus_assert (!(real)->constant); \
101 _dbus_assert (!(real)->locked)
104 * Checks assertions about a string object that may be locked but
105 * can't be const. i.e. a string object that we can free. Also
106 * declares the "real" variable pointing to DBusRealString.
108 * @param str the string
110 #define DBUS_LOCKED_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
111 DBUS_GENERIC_STRING_PREAMBLE (real); \
112 _dbus_assert (!(real)->constant)
115 * Checks assertions about a string that may be const or locked. Also
116 * declares the "real" variable pointing to DBusRealString.
117 * @param str the string.
119 #define DBUS_CONST_STRING_PREAMBLE(str) const DBusRealString *real = (DBusRealString*) str; \
120 DBUS_GENERIC_STRING_PREAMBLE (real)
125 * @addtogroup DBusString
130 fixup_alignment (DBusRealString *real)
134 unsigned int old_align_offset;
136 /* we have to have extra space in real->allocated for the align offset and nul byte */
137 _dbus_assert (real->len <= real->allocated - ALLOCATION_PADDING);
139 old_align_offset = real->align_offset;
140 real_block = real->str - old_align_offset;
142 aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
144 real->align_offset = aligned - real_block;
147 if (old_align_offset != real->align_offset)
149 /* Here comes the suck */
150 memmove (real_block + real->align_offset,
151 real_block + old_align_offset,
155 _dbus_assert (real->align_offset < 8);
156 _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
160 undo_alignment (DBusRealString *real)
162 if (real->align_offset != 0)
164 memmove (real->str - real->align_offset,
168 real->str = real->str - real->align_offset;
169 real->align_offset = 0;
174 * Initializes a string that can be up to the given allocation size
175 * before it has to realloc. The string starts life with zero length.
176 * The string must eventually be freed with _dbus_string_free().
178 * @param str memory to hold the string
179 * @param allocate_size amount to preallocate
180 * @returns #TRUE on success, #FALSE if no memory
183 _dbus_string_init_preallocated (DBusString *str,
186 DBusRealString *real;
188 _dbus_assert (str != NULL);
190 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
192 real = (DBusRealString*) str;
194 /* It's very important not to touch anything
195 * other than real->str if we're going to fail,
196 * since we also use this function to reset
197 * an existing string, e.g. in _dbus_string_steal_data()
200 real->str = dbus_malloc (ALLOCATION_PADDING + allocate_size);
201 if (real->str == NULL)
204 real->allocated = ALLOCATION_PADDING + allocate_size;
206 real->str[real->len] = '\0';
208 real->max_length = MAX_MAX_LENGTH;
209 real->constant = FALSE;
210 real->locked = FALSE;
211 real->invalid = FALSE;
212 real->align_offset = 0;
214 fixup_alignment (real);
220 * Initializes a string. The string starts life with zero length. The
221 * string must eventually be freed with _dbus_string_free().
223 * @param str memory to hold the string
224 * @returns #TRUE on success, #FALSE if no memory
227 _dbus_string_init (DBusString *str)
229 return _dbus_string_init_preallocated (str, 0);
232 /* The max length thing is sort of a historical artifact
233 * from a feature that turned out to be dumb; perhaps
234 * we should purge it entirely. The problem with
235 * the feature is that it looks like memory allocation
236 * failure, but is not a transient or resolvable failure.
239 set_max_length (DBusString *str,
242 DBusRealString *real;
244 real = (DBusRealString*) str;
246 real->max_length = max_length;
250 * Initializes a constant string. The value parameter is not copied
251 * (should be static), and the string may never be modified.
252 * It is safe but not necessary to call _dbus_string_free()
253 * on a const string. The string has a length limit of MAXINT - 8.
255 * @param str memory to use for the string
256 * @param value a string to be stored in str (not copied!!!)
259 _dbus_string_init_const (DBusString *str,
262 _dbus_assert (value != NULL);
264 _dbus_string_init_const_len (str, value,
269 * Initializes a constant string with a length. The value parameter is
270 * not copied (should be static), and the string may never be
271 * modified. It is safe but not necessary to call _dbus_string_free()
274 * @param str memory to use for the string
275 * @param value a string to be stored in str (not copied!!!)
276 * @param len the length to use
279 _dbus_string_init_const_len (DBusString *str,
283 DBusRealString *real;
285 _dbus_assert (str != NULL);
286 _dbus_assert (value != NULL);
287 _dbus_assert (len <= MAX_MAX_LENGTH);
288 _dbus_assert (len >= 0);
290 real = (DBusRealString*) str;
292 real->str = (char*) value;
294 real->allocated = real->len + ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
295 real->max_length = real->len + 1;
296 real->constant = TRUE;
297 real->invalid = FALSE;
299 /* We don't require const strings to be 8-byte aligned as the
300 * memory is coming from elsewhere.
305 * Frees a string created by _dbus_string_init().
307 * @param str memory where the string is stored.
310 _dbus_string_free (DBusString *str)
312 DBusRealString *real = (DBusRealString*) str;
313 DBUS_GENERIC_STRING_PREAMBLE (real);
317 dbus_free (real->str - real->align_offset);
319 real->invalid = TRUE;
322 #ifdef DBUS_BUILD_TESTS
323 /* Not using this feature at the moment,
324 * so marked DBUS_BUILD_TESTS-only
327 * Locks a string such that any attempts to change the string will
328 * result in aborting the program. Also, if the string is wasting a
329 * lot of memory (allocation is sufficiently larger than what the
330 * string is really using), _dbus_string_lock() will realloc the
331 * string's data to "compact" it.
333 * @param str the string to lock.
336 _dbus_string_lock (DBusString *str)
338 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
342 /* Try to realloc to avoid excess memory usage, since
343 * we know we won't change the string further
346 if (real->allocated - MAX_WASTE > real->len)
351 new_allocated = real->len + ALLOCATION_PADDING;
353 new_str = dbus_realloc (real->str - real->align_offset,
357 real->str = new_str + real->align_offset;
358 real->allocated = new_allocated;
359 fixup_alignment (real);
363 #endif /* DBUS_BUILD_TESTS */
366 reallocate_for_length (DBusRealString *real,
372 /* at least double our old allocation to avoid O(n), avoiding
375 if (real->allocated > (MAX_MAX_LENGTH + ALLOCATION_PADDING) / 2)
376 new_allocated = MAX_MAX_LENGTH + ALLOCATION_PADDING;
378 new_allocated = real->allocated * 2;
380 /* if you change the code just above here, run the tests without
381 * the following before you commit
383 #ifdef DBUS_BUILD_TESTS
384 new_allocated = 0; /* ensure a realloc every time so that we go
385 * through all malloc failure codepaths
389 /* But be sure we always alloc at least space for the new length */
390 new_allocated = MAX (new_allocated, new_length + ALLOCATION_PADDING);
392 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
393 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
397 real->str = new_str + real->align_offset;
398 real->allocated = new_allocated;
399 fixup_alignment (real);
405 set_length (DBusRealString *real,
408 /* Note, we are setting the length not including nul termination */
410 /* exceeding max length is the same as failure to allocate memory */
411 if (new_length > real->max_length)
413 else if (new_length > (real->allocated - ALLOCATION_PADDING) &&
414 !reallocate_for_length (real, new_length))
418 real->len = new_length;
419 real->str[real->len] = '\0';
426 DBusRealString *dest,
432 if (len > dest->max_length - dest->len)
433 return FALSE; /* detected overflow of dest->len + len below */
435 if (!set_length (dest, dest->len + len))
438 memmove (dest->str + insert_at + len,
439 dest->str + insert_at,
440 dest->len - len - insert_at);
446 * Gets the raw character buffer from the string. The returned buffer
447 * will be nul-terminated, but note that strings may contain binary
448 * data so there may be extra nul characters prior to the termination.
449 * This function should be little-used, extend DBusString or add
450 * stuff to dbus-sysdeps.c instead. It's an error to use this
451 * function on a const string.
453 * @param str the string
457 _dbus_string_get_data (DBusString *str)
459 DBUS_STRING_PREAMBLE (str);
465 * Gets the raw character buffer from a const string.
467 * @param str the string
468 * @returns the string data
471 _dbus_string_get_const_data (const DBusString *str)
473 DBUS_CONST_STRING_PREAMBLE (str);
479 * Gets a sub-portion of the raw character buffer from the
480 * string. The "len" field is required simply for error
481 * checking, to be sure you don't try to use more
482 * string than exists. The nul termination of the
483 * returned buffer remains at the end of the entire
484 * string, not at start + len.
486 * @param str the string
487 * @param start byte offset to return
488 * @param len length of segment to return
489 * @returns the string data
492 _dbus_string_get_data_len (DBusString *str,
496 DBUS_STRING_PREAMBLE (str);
497 _dbus_assert (start >= 0);
498 _dbus_assert (len >= 0);
499 _dbus_assert (start <= real->len);
500 _dbus_assert (len <= real->len - start);
502 return real->str + start;
506 * const version of _dbus_string_get_data_len().
508 * @param str the string
509 * @param start byte offset to return
510 * @param len length of segment to return
511 * @returns the string data
514 _dbus_string_get_const_data_len (const DBusString *str,
518 DBUS_CONST_STRING_PREAMBLE (str);
519 _dbus_assert (start >= 0);
520 _dbus_assert (len >= 0);
521 _dbus_assert (start <= real->len);
522 _dbus_assert (len <= real->len - start);
524 return real->str + start;
528 * Sets the value of the byte at the given position.
530 * @param str the string
531 * @param i the position
532 * @param byte the new value
535 _dbus_string_set_byte (DBusString *str,
539 DBUS_STRING_PREAMBLE (str);
540 _dbus_assert (i < real->len);
541 _dbus_assert (i >= 0);
547 * Gets the byte at the given position.
549 * @param str the string
550 * @param start the position
551 * @returns the byte at that position
554 _dbus_string_get_byte (const DBusString *str,
557 DBUS_CONST_STRING_PREAMBLE (str);
558 _dbus_assert (start < real->len);
559 _dbus_assert (start >= 0);
561 return real->str[start];
565 * Inserts the given byte at the given position.
567 * @param str the string
568 * @param i the position
569 * @param byte the value to insert
570 * @returns #TRUE on success
573 _dbus_string_insert_byte (DBusString *str,
577 DBUS_STRING_PREAMBLE (str);
578 _dbus_assert (i <= real->len);
579 _dbus_assert (i >= 0);
581 if (!open_gap (1, real, i))
590 * Like _dbus_string_get_data(), but removes the
591 * gotten data from the original string. The caller
592 * must free the data returned. This function may
593 * fail due to lack of memory, and return #FALSE.
595 * @param str the string
596 * @param data_return location to return the buffer
597 * @returns #TRUE on success
600 _dbus_string_steal_data (DBusString *str,
604 DBUS_STRING_PREAMBLE (str);
605 _dbus_assert (data_return != NULL);
607 undo_alignment (real);
609 *data_return = real->str;
611 old_max_length = real->max_length;
613 /* reset the string */
614 if (!_dbus_string_init (str))
616 /* hrm, put it back then */
617 real->str = *data_return;
619 fixup_alignment (real);
623 real->max_length = old_max_length;
629 * Like _dbus_string_get_data_len(), but removes the gotten data from
630 * the original string. The caller must free the data returned. This
631 * function may fail due to lack of memory, and return #FALSE.
632 * The returned string is nul-terminated and has length len.
634 * @todo this function is broken because on failure it
635 * may corrupt the source string.
637 * @param str the string
638 * @param data_return location to return the buffer
639 * @param start the start of segment to steal
640 * @param len the length of segment to steal
641 * @returns #TRUE on success
644 _dbus_string_steal_data_len (DBusString *str,
650 DBUS_STRING_PREAMBLE (str);
651 _dbus_assert (data_return != NULL);
652 _dbus_assert (start >= 0);
653 _dbus_assert (len >= 0);
654 _dbus_assert (start <= real->len);
655 _dbus_assert (len <= real->len - start);
657 if (!_dbus_string_init (&dest))
660 set_max_length (&dest, real->max_length);
662 if (!_dbus_string_move_len (str, start, len, &dest, 0))
664 _dbus_string_free (&dest);
668 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
669 if (!_dbus_string_steal_data (&dest, data_return))
671 _dbus_string_free (&dest);
675 _dbus_string_free (&dest);
681 * Copies the data from the string into a char*
683 * @param str the string
684 * @param data_return place to return the data
685 * @returns #TRUE on success, #FALSE on no memory
688 _dbus_string_copy_data (const DBusString *str,
691 DBUS_CONST_STRING_PREAMBLE (str);
692 _dbus_assert (data_return != NULL);
694 *data_return = dbus_malloc (real->len + 1);
695 if (*data_return == NULL)
698 memcpy (*data_return, real->str, real->len + 1);
704 * Copies a segment of the string into a char*
706 * @param str the string
707 * @param data_return place to return the data
708 * @param start start index
709 * @param len length to copy
710 * @returns #FALSE if no memory
713 _dbus_string_copy_data_len (const DBusString *str,
720 DBUS_CONST_STRING_PREAMBLE (str);
721 _dbus_assert (data_return != NULL);
722 _dbus_assert (start >= 0);
723 _dbus_assert (len >= 0);
724 _dbus_assert (start <= real->len);
725 _dbus_assert (len <= real->len - start);
727 if (!_dbus_string_init (&dest))
730 set_max_length (&dest, real->max_length);
732 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
734 _dbus_string_free (&dest);
738 if (!_dbus_string_steal_data (&dest, data_return))
740 _dbus_string_free (&dest);
744 _dbus_string_free (&dest);
749 * Gets the length of a string (not including nul termination).
751 * @returns the length.
754 _dbus_string_get_length (const DBusString *str)
756 DBUS_CONST_STRING_PREAMBLE (str);
762 * Makes a string longer by the given number of bytes. Checks whether
763 * adding additional_length to the current length would overflow an
764 * integer, and checks for exceeding a string's max length.
765 * The new bytes are not initialized, other than nul-terminating
766 * the end of the string. The uninitialized bytes may contain
767 * nul bytes or other junk.
769 * @param str a string
770 * @param additional_length length to add to the string.
771 * @returns #TRUE on success.
774 _dbus_string_lengthen (DBusString *str,
775 int additional_length)
777 DBUS_STRING_PREAMBLE (str);
778 _dbus_assert (additional_length >= 0);
780 if (additional_length > real->max_length - real->len)
781 return FALSE; /* would overflow */
783 return set_length (real,
784 real->len + additional_length);
788 * Makes a string shorter by the given number of bytes.
790 * @param str a string
791 * @param length_to_remove length to remove from the string.
794 _dbus_string_shorten (DBusString *str,
795 int length_to_remove)
797 DBUS_STRING_PREAMBLE (str);
798 _dbus_assert (length_to_remove >= 0);
799 _dbus_assert (length_to_remove <= real->len);
802 real->len - length_to_remove);
806 * Sets the length of a string. Can be used to truncate or lengthen
807 * the string. If the string is lengthened, the function may fail and
808 * return #FALSE. Newly-added bytes are not initialized, as with
809 * _dbus_string_lengthen().
811 * @param str a string
812 * @param length new length of the string.
813 * @returns #FALSE on failure.
816 _dbus_string_set_length (DBusString *str,
819 DBUS_STRING_PREAMBLE (str);
820 _dbus_assert (length >= 0);
822 return set_length (real, length);
826 align_length_then_lengthen (DBusString *str,
828 int then_lengthen_by)
830 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
832 DBUS_STRING_PREAMBLE (str);
833 _dbus_assert (alignment >= 1);
834 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
836 new_len = _DBUS_ALIGN_VALUE (real->len, alignment);
837 if (new_len > (unsigned long) real->max_length - then_lengthen_by)
839 new_len += then_lengthen_by;
841 delta = new_len - real->len;
842 _dbus_assert (delta >= 0);
847 if (!set_length (real, new_len))
850 /* delta == padding + then_lengthen_by
851 * new_len == old_len + padding + then_lengthen_by
853 if (then_lengthen_by < delta)
857 while (i < (new_len - then_lengthen_by))
868 * Align the length of a string to a specific alignment (typically 4 or 8)
869 * by appending nul bytes to the string.
871 * @param str a string
872 * @param alignment the alignment
873 * @returns #FALSE if no memory
876 _dbus_string_align_length (DBusString *str,
879 return align_length_then_lengthen (str, alignment, 0);
883 append (DBusRealString *real,
890 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
893 memcpy (real->str + (real->len - buffer_len),
901 * Appends a nul-terminated C-style string to a DBusString.
903 * @param str the DBusString
904 * @param buffer the nul-terminated characters to append
905 * @returns #FALSE if not enough memory.
908 _dbus_string_append (DBusString *str,
911 unsigned long buffer_len;
913 DBUS_STRING_PREAMBLE (str);
914 _dbus_assert (buffer != NULL);
916 buffer_len = strlen (buffer);
917 if (buffer_len > (unsigned long) real->max_length)
920 return append (real, buffer, buffer_len);
924 * Appends 4 bytes aligned on a 4 byte boundary
925 * with any alignment padding initialized to 0.
927 * @param str the DBusString
928 * @param octets 4 bytes to append
929 * @returns #FALSE if not enough memory.
932 _dbus_string_append_4_aligned (DBusString *str,
933 const unsigned char octets[4])
936 DBUS_STRING_PREAMBLE (str);
938 if (!align_length_then_lengthen (str, 4, 4))
941 p = (dbus_uint32_t*) (real->str + (real->len - 4));
942 *p = *((dbus_uint32_t*)octets);
948 * Appends 8 bytes aligned on an 8 byte boundary
949 * with any alignment padding initialized to 0.
951 * @param str the DBusString
952 * @param octets 4 bytes to append
953 * @returns #FALSE if not enough memory.
956 _dbus_string_append_8_aligned (DBusString *str,
957 const unsigned char octets[8])
959 #ifdef DBUS_HAVE_INT64
961 DBUS_STRING_PREAMBLE (str);
963 if (!align_length_then_lengthen (str, 8, 8))
966 p = (dbus_uint64_t*) (real->str + (real->len - 8));
967 *p = *((dbus_uint64_t*)octets);
970 DBUS_STRING_PREAMBLE (str);
972 if (!align_length_then_lengthen (str, 8, 8))
975 p = real->str + (real->len - 8);
985 _dbus_assert (p == (real->str + real->len));
992 * Appends a printf-style formatted string
993 * to the #DBusString.
995 * @param str the string
996 * @param format printf format
997 * @param args variable argument list
998 * @returns #FALSE if no memory
1001 _dbus_string_append_printf_valist (DBusString *str,
1007 DBUS_STRING_PREAMBLE (str);
1009 /* Measure the message length without terminating nul */
1010 len = vsnprintf (&c, 1, format, args);
1012 if (!_dbus_string_lengthen (str, len))
1015 vsprintf (real->str + (real->len - len),
1022 * Appends a printf-style formatted string
1023 * to the #DBusString.
1025 * @param str the string
1026 * @param format printf format
1027 * @returns #FALSE if no memory
1030 _dbus_string_append_printf (DBusString *str,
1037 va_start (args, format);
1038 retval = _dbus_string_append_printf_valist (str, format, args);
1045 * Appends block of bytes with the given length to a DBusString.
1047 * @param str the DBusString
1048 * @param buffer the bytes to append
1049 * @param len the number of bytes to append
1050 * @returns #FALSE if not enough memory.
1053 _dbus_string_append_len (DBusString *str,
1057 DBUS_STRING_PREAMBLE (str);
1058 _dbus_assert (buffer != NULL);
1059 _dbus_assert (len >= 0);
1061 return append (real, buffer, len);
1065 * Appends a single byte to the string, returning #FALSE
1066 * if not enough memory.
1068 * @param str the string
1069 * @param byte the byte to append
1070 * @returns #TRUE on success
1073 _dbus_string_append_byte (DBusString *str,
1076 DBUS_STRING_PREAMBLE (str);
1078 if (!set_length (real, real->len + 1))
1081 real->str[real->len-1] = byte;
1087 * Appends a single Unicode character, encoding the character
1090 * @param str the string
1091 * @param ch the Unicode character
1094 _dbus_string_append_unichar (DBusString *str,
1102 DBUS_STRING_PREAMBLE (str);
1104 /* this code is from GLib but is pretty standard I think */
1113 else if (ch < 0x800)
1118 else if (ch < 0x10000)
1123 else if (ch < 0x200000)
1128 else if (ch < 0x4000000)
1139 if (len > (real->max_length - real->len))
1140 return FALSE; /* real->len + len would overflow */
1142 if (!set_length (real, real->len + len))
1145 out = real->str + (real->len - len);
1147 for (i = len - 1; i > 0; --i)
1149 out[i] = (ch & 0x3f) | 0x80;
1152 out[0] = ch | first;
1158 delete (DBusRealString *real,
1165 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1167 real->str[real->len] = '\0';
1171 * Deletes a segment of a DBusString with length len starting at
1172 * start. (Hint: to clear an entire string, setting length to 0
1173 * with _dbus_string_set_length() is easier.)
1175 * @param str the DBusString
1176 * @param start where to start deleting
1177 * @param len the number of bytes to delete
1180 _dbus_string_delete (DBusString *str,
1184 DBUS_STRING_PREAMBLE (str);
1185 _dbus_assert (start >= 0);
1186 _dbus_assert (len >= 0);
1187 _dbus_assert (start <= real->len);
1188 _dbus_assert (len <= real->len - start);
1190 delete (real, start, len);
1194 copy (DBusRealString *source,
1197 DBusRealString *dest,
1203 if (!open_gap (len, dest, insert_at))
1206 memcpy (dest->str + insert_at,
1207 source->str + start,
1214 * Checks assertions for two strings we're copying a segment between,
1215 * and declares real_source/real_dest variables.
1217 * @param source the source string
1218 * @param start the starting offset
1219 * @param dest the dest string
1220 * @param insert_at where the copied segment is inserted
1222 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1223 DBusRealString *real_source = (DBusRealString*) source; \
1224 DBusRealString *real_dest = (DBusRealString*) dest; \
1225 _dbus_assert ((source) != (dest)); \
1226 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1227 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1228 _dbus_assert (!real_dest->constant); \
1229 _dbus_assert (!real_dest->locked); \
1230 _dbus_assert ((start) >= 0); \
1231 _dbus_assert ((start) <= real_source->len); \
1232 _dbus_assert ((insert_at) >= 0); \
1233 _dbus_assert ((insert_at) <= real_dest->len)
1236 * Moves the end of one string into another string. Both strings
1237 * must be initialized, valid strings.
1239 * @param source the source string
1240 * @param start where to chop off the source string
1241 * @param dest the destination string
1242 * @param insert_at where to move the chopped-off part of source string
1243 * @returns #FALSE if not enough memory
1246 _dbus_string_move (DBusString *source,
1251 DBusRealString *real_source = (DBusRealString*) source;
1252 _dbus_assert (start <= real_source->len);
1254 return _dbus_string_move_len (source, start,
1255 real_source->len - start,
1260 * Like _dbus_string_move(), but does not delete the section
1261 * of the source string that's copied to the dest string.
1263 * @param source the source string
1264 * @param start where to start copying the source string
1265 * @param dest the destination string
1266 * @param insert_at where to place the copied part of source string
1267 * @returns #FALSE if not enough memory
1270 _dbus_string_copy (const DBusString *source,
1275 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1277 return copy (real_source, start,
1278 real_source->len - start,
1284 * Like _dbus_string_move(), but can move a segment from
1285 * the middle of the source string.
1287 * @todo this doesn't do anything with max_length field.
1288 * we should probably just kill the max_length field though.
1290 * @param source the source string
1291 * @param start first byte of source string to move
1292 * @param len length of segment to move
1293 * @param dest the destination string
1294 * @param insert_at where to move the bytes from the source string
1295 * @returns #FALSE if not enough memory
1298 _dbus_string_move_len (DBusString *source,
1305 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1306 _dbus_assert (len >= 0);
1307 _dbus_assert ((start + len) <= real_source->len);
1314 else if (start == 0 &&
1315 len == real_source->len &&
1316 real_dest->len == 0)
1318 /* Short-circuit moving an entire existing string to an empty string
1319 * by just swapping the buffers.
1321 /* we assume ->constant doesn't matter as you can't have
1322 * a constant string involved in a move.
1324 #define ASSIGN_DATA(a, b) do { \
1325 (a)->str = (b)->str; \
1326 (a)->len = (b)->len; \
1327 (a)->allocated = (b)->allocated; \
1328 (a)->align_offset = (b)->align_offset; \
1333 ASSIGN_DATA (&tmp, real_source);
1334 ASSIGN_DATA (real_source, real_dest);
1335 ASSIGN_DATA (real_dest, &tmp);
1341 if (!copy (real_source, start, len,
1346 delete (real_source, start,
1354 * Like _dbus_string_copy(), but can copy a segment from the middle of
1355 * the source string.
1357 * @param source the source string
1358 * @param start where to start copying the source string
1359 * @param len length of segment to copy
1360 * @param dest the destination string
1361 * @param insert_at where to place the copied segment of source string
1362 * @returns #FALSE if not enough memory
1365 _dbus_string_copy_len (const DBusString *source,
1371 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1372 _dbus_assert (len >= 0);
1373 _dbus_assert (start <= real_source->len);
1374 _dbus_assert (len <= real_source->len - start);
1376 return copy (real_source, start, len,
1382 * Replaces a segment of dest string with a segment of source string.
1384 * @todo optimize the case where the two lengths are the same, and
1385 * avoid memmoving the data in the trailing part of the string twice.
1387 * @todo avoid inserting the source into dest, then deleting
1388 * the replaced chunk of dest (which creates a potentially large
1389 * intermediate string). Instead, extend the replaced chunk
1390 * of dest with padding to the same size as the source chunk,
1391 * then copy in the source bytes.
1393 * @param source the source string
1394 * @param start where to start copying the source string
1395 * @param len length of segment to copy
1396 * @param dest the destination string
1397 * @param replace_at start of segment of dest string to replace
1398 * @param replace_len length of segment of dest string to replace
1399 * @returns #FALSE if not enough memory
1403 _dbus_string_replace_len (const DBusString *source,
1410 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1411 _dbus_assert (len >= 0);
1412 _dbus_assert (start <= real_source->len);
1413 _dbus_assert (len <= real_source->len - start);
1414 _dbus_assert (replace_at >= 0);
1415 _dbus_assert (replace_at <= real_dest->len);
1416 _dbus_assert (replace_len <= real_dest->len - replace_at);
1418 if (!copy (real_source, start, len,
1419 real_dest, replace_at))
1422 delete (real_dest, replace_at + len, replace_len);
1427 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1428 * Pennington, and Tom Tromey are the authors and authorized relicense.
1431 /** computes length and mask of a unicode character
1432 * @param Char the char
1433 * @param Mask the mask variable to assign to
1434 * @param Len the length variable to assign to
1436 #define UTF8_COMPUTE(Char, Mask, Len) \
1442 else if ((Char & 0xe0) == 0xc0) \
1447 else if ((Char & 0xf0) == 0xe0) \
1452 else if ((Char & 0xf8) == 0xf0) \
1457 else if ((Char & 0xfc) == 0xf8) \
1462 else if ((Char & 0xfe) == 0xfc) \
1471 * computes length of a unicode character in UTF-8
1472 * @param Char the char
1474 #define UTF8_LENGTH(Char) \
1475 ((Char) < 0x80 ? 1 : \
1476 ((Char) < 0x800 ? 2 : \
1477 ((Char) < 0x10000 ? 3 : \
1478 ((Char) < 0x200000 ? 4 : \
1479 ((Char) < 0x4000000 ? 5 : 6)))))
1482 * Gets a UTF-8 value.
1484 * @param Result variable for extracted unicode char.
1485 * @param Chars the bytes to decode
1486 * @param Count counter variable
1487 * @param Mask mask for this char
1488 * @param Len length for this char in bytes
1490 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1491 (Result) = (Chars)[0] & (Mask); \
1492 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1494 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1500 (Result) |= ((Chars)[(Count)] & 0x3f); \
1504 * Check whether a unicode char is in a valid range.
1506 * @param Char the character
1508 #define UNICODE_VALID(Char) \
1509 ((Char) < 0x110000 && \
1510 (((Char) & 0xFFFFF800) != 0xD800) && \
1511 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1512 ((Char) & 0xFFFF) != 0xFFFF)
1515 * Gets a unicode character from a UTF-8 string. Does no validation;
1516 * you must verify that the string is valid UTF-8 in advance and must
1517 * pass in the start of a character.
1519 * @param str the string
1520 * @param start the start of the UTF-8 character.
1521 * @param ch_return location to return the character
1522 * @param end_return location to return the byte index of next character
1525 _dbus_string_get_unichar (const DBusString *str,
1527 dbus_unichar_t *ch_return,
1531 dbus_unichar_t result;
1534 DBUS_CONST_STRING_PREAMBLE (str);
1535 _dbus_assert (start >= 0);
1536 _dbus_assert (start <= real->len);
1541 *end_return = real->len;
1544 p = real->str + start;
1547 UTF8_COMPUTE (c, mask, len);
1550 UTF8_GET (result, p, i, mask, len);
1552 if (result == (dbus_unichar_t)-1)
1556 *ch_return = result;
1558 *end_return = start + len;
1562 * Finds the given substring in the string,
1563 * returning #TRUE and filling in the byte index
1564 * where the substring was found, if it was found.
1565 * Returns #FALSE if the substring wasn't found.
1566 * Sets *start to the length of the string if the substring
1569 * @param str the string
1570 * @param start where to start looking
1571 * @param substr the substring
1572 * @param found return location for where it was found, or #NULL
1573 * @returns #TRUE if found
1576 _dbus_string_find (const DBusString *str,
1581 return _dbus_string_find_to (str, start,
1582 ((const DBusRealString*)str)->len,
1587 * Finds the given substring in the string,
1588 * up to a certain position,
1589 * returning #TRUE and filling in the byte index
1590 * where the substring was found, if it was found.
1591 * Returns #FALSE if the substring wasn't found.
1592 * Sets *start to the length of the string if the substring
1595 * @param str the string
1596 * @param start where to start looking
1597 * @param end where to stop looking
1598 * @param substr the substring
1599 * @param found return location for where it was found, or #NULL
1600 * @returns #TRUE if found
1603 _dbus_string_find_to (const DBusString *str,
1610 DBUS_CONST_STRING_PREAMBLE (str);
1611 _dbus_assert (substr != NULL);
1612 _dbus_assert (start <= real->len);
1613 _dbus_assert (start >= 0);
1614 _dbus_assert (substr != NULL);
1615 _dbus_assert (end <= real->len);
1616 _dbus_assert (start <= end);
1618 /* we always "find" an empty string */
1619 if (*substr == '\0')
1629 if (real->str[i] == substr[0])
1635 if (substr[j - i] == '\0')
1637 else if (real->str[j] != substr[j - i])
1643 if (substr[j - i] == '\0')
1661 * Find the given byte scanning backward from the given start.
1662 * Sets *found to -1 if the byte is not found.
1664 * @param str the string
1665 * @param start the place to start scanning (will not find the byte at this point)
1666 * @param byte the byte to find
1667 * @param found return location for where it was found
1668 * @returns #TRUE if found
1671 _dbus_string_find_byte_backward (const DBusString *str,
1677 DBUS_CONST_STRING_PREAMBLE (str);
1678 _dbus_assert (start <= real->len);
1679 _dbus_assert (start >= 0);
1680 _dbus_assert (found != NULL);
1685 if (real->str[i] == byte)
1698 * Finds a blank (space or tab) in the string. Returns #TRUE
1699 * if found, #FALSE otherwise. If a blank is not found sets
1700 * *found to the length of the string.
1702 * @param str the string
1703 * @param start byte index to start looking
1704 * @param found place to store the location of the first blank
1705 * @returns #TRUE if a blank was found
1708 _dbus_string_find_blank (const DBusString *str,
1713 DBUS_CONST_STRING_PREAMBLE (str);
1714 _dbus_assert (start <= real->len);
1715 _dbus_assert (start >= 0);
1718 while (i < real->len)
1720 if (real->str[i] == ' ' ||
1721 real->str[i] == '\t')
1738 * Skips blanks from start, storing the first non-blank in *end
1739 * (blank is space or tab).
1741 * @param str the string
1742 * @param start where to start
1743 * @param end where to store the first non-blank byte index
1746 _dbus_string_skip_blank (const DBusString *str,
1751 DBUS_CONST_STRING_PREAMBLE (str);
1752 _dbus_assert (start <= real->len);
1753 _dbus_assert (start >= 0);
1756 while (i < real->len)
1758 if (!(real->str[i] == ' ' ||
1759 real->str[i] == '\t'))
1765 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1766 real->str[i] == '\t'));
1773 * Skips whitespace from start, storing the first non-whitespace in *end.
1774 * (whitespace is space, tab, newline, CR).
1776 * @param str the string
1777 * @param start where to start
1778 * @param end where to store the first non-whitespace byte index
1781 _dbus_string_skip_white (const DBusString *str,
1786 DBUS_CONST_STRING_PREAMBLE (str);
1787 _dbus_assert (start <= real->len);
1788 _dbus_assert (start >= 0);
1791 while (i < real->len)
1793 if (!(real->str[i] == ' ' ||
1794 real->str[i] == '\n' ||
1795 real->str[i] == '\r' ||
1796 real->str[i] == '\t'))
1802 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1803 real->str[i] == '\t'));
1810 * Assigns a newline-terminated or \r\n-terminated line from the front
1811 * of the string to the given dest string. The dest string's previous
1812 * contents are deleted. If the source string contains no newline,
1813 * moves the entire source string to the dest string.
1815 * @todo owen correctly notes that this is a stupid function (it was
1816 * written purely for test code,
1817 * e.g. dbus-message-builder.c). Probably should be enforced as test
1818 * code only with #ifdef DBUS_BUILD_TESTS
1820 * @param source the source string
1821 * @param dest the destination string (contents are replaced)
1822 * @returns #FALSE if no memory, or source has length 0
1825 _dbus_string_pop_line (DBusString *source,
1829 dbus_bool_t have_newline;
1831 _dbus_string_set_length (dest, 0);
1834 if (_dbus_string_find (source, 0, "\n", &eol))
1836 have_newline = TRUE;
1837 eol += 1; /* include newline */
1841 eol = _dbus_string_get_length (source);
1842 have_newline = FALSE;
1846 return FALSE; /* eof */
1848 if (!_dbus_string_move_len (source, 0, eol,
1854 /* dump the newline and the \r if we have one */
1857 dbus_bool_t have_cr;
1859 _dbus_assert (_dbus_string_get_length (dest) > 0);
1861 if (_dbus_string_get_length (dest) > 1 &&
1862 _dbus_string_get_byte (dest,
1863 _dbus_string_get_length (dest) - 2) == '\r')
1868 _dbus_string_set_length (dest,
1869 _dbus_string_get_length (dest) -
1877 * Deletes up to and including the first blank space
1880 * @param str the string
1883 _dbus_string_delete_first_word (DBusString *str)
1887 if (_dbus_string_find_blank (str, 0, &i))
1888 _dbus_string_skip_blank (str, i, &i);
1890 _dbus_string_delete (str, 0, i);
1894 * Deletes any leading blanks in the string
1896 * @param str the string
1899 _dbus_string_delete_leading_blanks (DBusString *str)
1903 _dbus_string_skip_blank (str, 0, &i);
1906 _dbus_string_delete (str, 0, i);
1910 * Tests two DBusString for equality.
1912 * @todo memcmp is probably faster
1914 * @param a first string
1915 * @param b second string
1916 * @returns #TRUE if equal
1919 _dbus_string_equal (const DBusString *a,
1920 const DBusString *b)
1922 const unsigned char *ap;
1923 const unsigned char *bp;
1924 const unsigned char *a_end;
1925 const DBusRealString *real_a = (const DBusRealString*) a;
1926 const DBusRealString *real_b = (const DBusRealString*) b;
1927 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1928 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1930 if (real_a->len != real_b->len)
1935 a_end = real_a->str + real_a->len;
1949 * Tests two DBusString for equality up to the given length.
1951 * @todo write a unit test
1953 * @todo memcmp is probably faster
1955 * @param a first string
1956 * @param b second string
1957 * @param len the lengh
1958 * @returns #TRUE if equal for the given number of bytes
1961 _dbus_string_equal_len (const DBusString *a,
1962 const DBusString *b,
1965 const unsigned char *ap;
1966 const unsigned char *bp;
1967 const unsigned char *a_end;
1968 const DBusRealString *real_a = (const DBusRealString*) a;
1969 const DBusRealString *real_b = (const DBusRealString*) b;
1970 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1971 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1973 if (real_a->len != real_b->len &&
1974 (real_a->len < len || real_b->len < len))
1979 a_end = real_a->str + MIN (real_a->len, len);
1993 * Checks whether a string is equal to a C string.
1995 * @param a the string
1996 * @param c_str the C string
1997 * @returns #TRUE if equal
2000 _dbus_string_equal_c_str (const DBusString *a,
2003 const unsigned char *ap;
2004 const unsigned char *bp;
2005 const unsigned char *a_end;
2006 const DBusRealString *real_a = (const DBusRealString*) a;
2007 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2008 _dbus_assert (c_str != NULL);
2011 bp = (const unsigned char*) c_str;
2012 a_end = real_a->str + real_a->len;
2013 while (ap != a_end && *bp)
2022 if (ap != a_end || *bp)
2029 * Checks whether a string starts with the given C string.
2031 * @param a the string
2032 * @param c_str the C string
2033 * @returns #TRUE if string starts with it
2036 _dbus_string_starts_with_c_str (const DBusString *a,
2039 const unsigned char *ap;
2040 const unsigned char *bp;
2041 const unsigned char *a_end;
2042 const DBusRealString *real_a = (const DBusRealString*) a;
2043 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2044 _dbus_assert (c_str != NULL);
2047 bp = (const unsigned char*) c_str;
2048 a_end = real_a->str + real_a->len;
2049 while (ap != a_end && *bp)
2065 * Returns whether a string ends with the given suffix
2067 * @todo memcmp might make this faster.
2069 * @param a the string
2070 * @param c_str the C-style string
2071 * @returns #TRUE if the string ends with the suffix
2074 _dbus_string_ends_with_c_str (const DBusString *a,
2077 const unsigned char *ap;
2078 const unsigned char *bp;
2079 const unsigned char *a_end;
2080 unsigned long c_str_len;
2081 const DBusRealString *real_a = (const DBusRealString*) a;
2082 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2083 _dbus_assert (c_str != NULL);
2085 c_str_len = strlen (c_str);
2086 if (((unsigned long)real_a->len) < c_str_len)
2089 ap = real_a->str + (real_a->len - c_str_len);
2090 bp = (const unsigned char*) c_str;
2091 a_end = real_a->str + real_a->len;
2101 _dbus_assert (*ap == '\0');
2102 _dbus_assert (*bp == '\0');
2107 static const signed char base64_table[] = {
2174 /** The minimum char that's a valid char in Base64-encoded text */
2175 #define UNBASE64_MIN_CHAR (43)
2176 /** The maximum char that's a valid char in Base64-encoded text */
2177 #define UNBASE64_MAX_CHAR (122)
2178 /** Must subtract this from a char's integer value before offsetting
2179 * into unbase64_table
2181 #define UNBASE64_TABLE_OFFSET UNBASE64_MIN_CHAR
2182 static const signed char unbase64_table[] = {
2266 * Encodes a string using Base64, as documented in RFC 2045.
2268 * @param source the string to encode
2269 * @param start byte index to start encoding
2270 * @param dest string where encoded data should be placed
2271 * @param insert_at where to place encoded data
2272 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2275 _dbus_string_base64_encode (const DBusString *source,
2281 unsigned int dest_len; /* unsigned for overflow checks below */
2282 const unsigned char *s;
2284 const unsigned char *triplet_end;
2285 const unsigned char *final_end;
2286 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
2287 _dbus_assert (source != dest);
2289 /* For each 24 bits (3 bytes) of input, we have 4 bytes of
2292 source_len = real_source->len - start;
2293 dest_len = (source_len / 3) * 4;
2294 if (source_len % 3 != 0)
2297 if (dest_len > (unsigned int) real_dest->max_length)
2300 if (source_len == 0)
2303 if (!open_gap (dest_len, real_dest, insert_at))
2306 d = real_dest->str + insert_at;
2307 s = real_source->str + start;
2308 final_end = real_source->str + (start + source_len);
2309 triplet_end = final_end - (source_len % 3);
2310 _dbus_assert (triplet_end <= final_end);
2311 _dbus_assert ((final_end - triplet_end) < 3);
2313 #define ENCODE_64(v) (base64_table[ (unsigned char) (v) ])
2314 #define SIX_BITS_MASK (0x3f)
2315 _dbus_assert (SIX_BITS_MASK < _DBUS_N_ELEMENTS (base64_table));
2317 while (s != triplet_end)
2319 unsigned int triplet;
2321 triplet = s[2] | (s[1] << 8) | (s[0] << 16);
2323 /* Encode each 6 bits. */
2325 *d++ = ENCODE_64 (triplet >> 18);
2326 *d++ = ENCODE_64 ((triplet >> 12) & SIX_BITS_MASK);
2327 *d++ = ENCODE_64 ((triplet >> 6) & SIX_BITS_MASK);
2328 *d++ = ENCODE_64 (triplet & SIX_BITS_MASK);
2333 switch (final_end - triplet_end)
2337 unsigned int doublet;
2339 doublet = s[1] | (s[0] << 8);
2341 *d++ = ENCODE_64 (doublet >> 12);
2342 *d++ = ENCODE_64 ((doublet >> 6) & SIX_BITS_MASK);
2343 *d++ = ENCODE_64 (doublet & SIX_BITS_MASK);
2349 unsigned int singlet;
2353 *d++ = ENCODE_64 ((singlet >> 6) & SIX_BITS_MASK);
2354 *d++ = ENCODE_64 (singlet & SIX_BITS_MASK);
2363 _dbus_assert (d == (real_dest->str + (insert_at + dest_len)));
2369 * Decodes a string from Base64, as documented in RFC 2045.
2371 * @todo sort out the AUDIT comment in here. The case it mentions
2372 * ("====" or "x===") is not allowed in correct base64, so need to
2373 * decide what to do with that kind of input. Probably ignore it
2374 * since we ignore any other junk seen.
2376 * @param source the string to decode
2377 * @param start byte index to start decode
2378 * @param dest string where decoded data should be placed
2379 * @param insert_at where to place decoded data
2380 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
2383 _dbus_string_base64_decode (const DBusString *source,
2392 unsigned int triplet = 0;
2395 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
2396 _dbus_assert (source != dest);
2398 source_len = real_source->len - start;
2399 s = real_source->str + start;
2400 end = real_source->str + source_len;
2402 if (source_len == 0)
2405 if (!_dbus_string_init (&result))
2412 /* The idea is to just skip anything that isn't
2413 * a base64 char - it's allowed to have whitespace,
2414 * newlines, etc. in here. We also ignore trailing
2415 * base64 chars, though that's suspicious.
2418 if (*s >= UNBASE64_MIN_CHAR &&
2419 *s <= UNBASE64_MAX_CHAR)
2423 /* '=' is padding, doesn't represent additional data
2424 * but does increment our count.
2433 val = unbase64_table[(*s) - UNBASE64_TABLE_OFFSET];
2438 triplet |= (unsigned int) val;
2443 if (sextet_count == 4)
2445 /* no pad = 3 bytes, 1 pad = 2 bytes, 2 pad = 1 byte */
2448 /* AUDIT: Comment doesn't mention 4 pad => 0,
2449 * 3 pad => 1 byte, though the code should
2450 * work fine if those are the required outputs.
2452 * I assume that the spec requires dropping
2453 * the top two bits of, say, ///= which is > 2
2454 * bytes worth of bits. (Or otherwise, you couldn't
2455 * actually represent 2 byte sequences.
2460 if (!_dbus_string_append_byte (&result,
2467 if (!_dbus_string_append_byte (&result,
2468 (triplet >> 8) & 0xff))
2472 if (!_dbus_string_append_byte (&result,
2485 if (!_dbus_string_move (&result, 0, dest, insert_at))
2487 _dbus_string_free (&result);
2491 _dbus_string_free (&result);
2496 _dbus_string_free (&result);
2502 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2503 * encoded. (Each byte is two hex digits.)
2505 * @param source the string to encode
2506 * @param start byte index to start encoding
2507 * @param dest string where encoded data should be placed
2508 * @param insert_at where to place encoded data
2509 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2512 _dbus_string_hex_encode (const DBusString *source,
2518 const char hexdigits[16] = {
2519 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2520 'a', 'b', 'c', 'd', 'e', 'f'
2522 const unsigned char *p;
2523 const unsigned char *end;
2526 _dbus_assert (start <= _dbus_string_get_length (source));
2528 if (!_dbus_string_init (&result))
2533 p = (const unsigned char*) _dbus_string_get_const_data (source);
2534 end = p + _dbus_string_get_length (source);
2539 if (!_dbus_string_append_byte (&result,
2540 hexdigits[(*p >> 4)]))
2543 if (!_dbus_string_append_byte (&result,
2544 hexdigits[(*p & 0x0f)]))
2550 if (!_dbus_string_move (&result, 0, dest, insert_at))
2556 _dbus_string_free (&result);
2561 * Decodes a string from hex encoding.
2563 * @param source the string to decode
2564 * @param start byte index to start decode
2565 * @param dest string where decoded data should be placed
2566 * @param insert_at where to place decoded data
2567 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
2570 _dbus_string_hex_decode (const DBusString *source,
2576 const unsigned char *p;
2577 const unsigned char *end;
2579 dbus_bool_t high_bits;
2581 _dbus_assert (start <= _dbus_string_get_length (source));
2583 if (!_dbus_string_init (&result))
2589 p = (const unsigned char*) _dbus_string_get_const_data (source);
2590 end = p + _dbus_string_get_length (source);
2655 _dbus_verbose ("invalid character '%c' in hex encoded text\n",
2662 if (!_dbus_string_append_byte (&result,
2671 len = _dbus_string_get_length (&result);
2673 b = _dbus_string_get_byte (&result, len - 1);
2677 _dbus_string_set_byte (&result, len - 1, b);
2680 high_bits = !high_bits;
2685 if (!_dbus_string_move (&result, 0, dest, insert_at))
2691 _dbus_string_free (&result);
2696 * Checks that the given range of the string is valid ASCII with no
2697 * nul bytes. If the given range is not entirely contained in the
2698 * string, returns #FALSE.
2700 * @todo this is inconsistent with most of DBusString in that
2701 * it allows a start,len range that isn't in the string.
2703 * @param str the string
2704 * @param start first byte index to check
2705 * @param len number of bytes to check
2706 * @returns #TRUE if the byte range exists and is all valid ASCII
2709 _dbus_string_validate_ascii (const DBusString *str,
2713 const unsigned char *s;
2714 const unsigned char *end;
2715 DBUS_CONST_STRING_PREAMBLE (str);
2716 _dbus_assert (start >= 0);
2717 _dbus_assert (start <= real->len);
2718 _dbus_assert (len >= 0);
2720 if (len > real->len - start)
2723 s = real->str + start;
2728 ((*s & ~0x7f) != 0))
2738 * Checks that the given range of the string is valid UTF-8. If the
2739 * given range is not entirely contained in the string, returns
2740 * #FALSE. If the string contains any nul bytes in the given range,
2741 * returns #FALSE. If the start and start+len are not on character
2742 * boundaries, returns #FALSE.
2744 * @todo this is inconsistent with most of DBusString in that
2745 * it allows a start,len range that isn't in the string.
2747 * @param str the string
2748 * @param start first byte index to check
2749 * @param len number of bytes to check
2750 * @returns #TRUE if the byte range exists and is all valid UTF-8
2753 _dbus_string_validate_utf8 (const DBusString *str,
2757 const unsigned char *p;
2758 const unsigned char *end;
2759 DBUS_CONST_STRING_PREAMBLE (str);
2760 _dbus_assert (start >= 0);
2761 _dbus_assert (start <= real->len);
2762 _dbus_assert (len >= 0);
2764 if (len > real->len - start)
2767 p = real->str + start;
2772 int i, mask = 0, char_len;
2773 dbus_unichar_t result;
2774 unsigned char c = (unsigned char) *p;
2776 UTF8_COMPUTE (c, mask, char_len);
2781 /* check that the expected number of bytes exists in the remaining length */
2782 if ((end - p) < char_len)
2785 UTF8_GET (result, p, i, mask, char_len);
2787 if (UTF8_LENGTH (result) != char_len) /* Check for overlong UTF-8 */
2790 if (result == (dbus_unichar_t)-1)
2793 if (!UNICODE_VALID (result))
2799 /* See that we covered the entire length if a length was
2809 * Checks that the given range of the string is all nul bytes. If the
2810 * given range is not entirely contained in the string, returns
2813 * @todo this is inconsistent with most of DBusString in that
2814 * it allows a start,len range that isn't in the string.
2816 * @param str the string
2817 * @param start first byte index to check
2818 * @param len number of bytes to check
2819 * @returns #TRUE if the byte range exists and is all nul bytes
2822 _dbus_string_validate_nul (const DBusString *str,
2826 const unsigned char *s;
2827 const unsigned char *end;
2828 DBUS_CONST_STRING_PREAMBLE (str);
2829 _dbus_assert (start >= 0);
2830 _dbus_assert (len >= 0);
2831 _dbus_assert (start <= real->len);
2833 if (len > real->len - start)
2836 s = real->str + start;
2849 * Checks that the given range of the string is a valid object path
2850 * name in the D-BUS protocol. This includes a length restriction,
2851 * etc., see the specification. It does not validate UTF-8, that has
2852 * to be done separately for now.
2854 * @todo this is inconsistent with most of DBusString in that
2855 * it allows a start,len range that isn't in the string.
2857 * @todo change spec to disallow more things, such as spaces in the
2860 * @param str the string
2861 * @param start first byte index to check
2862 * @param len number of bytes to check
2863 * @returns #TRUE if the byte range exists and is a valid name
2866 _dbus_string_validate_path (const DBusString *str,
2870 const unsigned char *s;
2871 const unsigned char *end;
2872 const unsigned char *last_slash;
2874 DBUS_CONST_STRING_PREAMBLE (str);
2875 _dbus_assert (start >= 0);
2876 _dbus_assert (len >= 0);
2877 _dbus_assert (start <= real->len);
2879 if (len > real->len - start)
2882 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2888 s = real->str + start;
2900 if ((s - last_slash) < 2)
2901 return FALSE; /* no empty path components allowed */
2909 if ((end - last_slash) < 2 &&
2911 return FALSE; /* trailing slash not allowed unless the string is "/" */
2917 * Checks that the given range of the string is a valid interface name
2918 * in the D-BUS protocol. This includes a length restriction, etc.,
2919 * see the specification. It does not validate UTF-8, that has to be
2920 * done separately for now.
2922 * @todo this is inconsistent with most of DBusString in that
2923 * it allows a start,len range that isn't in the string.
2925 * @todo change spec to disallow more things, such as spaces in the
2928 * @param str the string
2929 * @param start first byte index to check
2930 * @param len number of bytes to check
2931 * @returns #TRUE if the byte range exists and is a valid name
2934 _dbus_string_validate_interface (const DBusString *str,
2938 const unsigned char *s;
2939 const unsigned char *end;
2940 dbus_bool_t saw_dot;
2942 DBUS_CONST_STRING_PREAMBLE (str);
2943 _dbus_assert (start >= 0);
2944 _dbus_assert (len >= 0);
2945 _dbus_assert (start <= real->len);
2947 if (len > real->len - start)
2950 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2957 s = real->str + start;
2977 * Checks that the given range of the string is a valid member name
2978 * in the D-BUS protocol. This includes a length restriction, etc.,
2979 * see the specification. It does not validate UTF-8, that has to be
2980 * done separately for now.
2982 * @todo this is inconsistent with most of DBusString in that
2983 * it allows a start,len range that isn't in the string.
2985 * @todo change spec to disallow more things, such as spaces in the
2988 * @param str the string
2989 * @param start first byte index to check
2990 * @param len number of bytes to check
2991 * @returns #TRUE if the byte range exists and is a valid name
2994 _dbus_string_validate_member (const DBusString *str,
2998 const unsigned char *s;
2999 const unsigned char *end;
3000 dbus_bool_t saw_dot;
3002 DBUS_CONST_STRING_PREAMBLE (str);
3003 _dbus_assert (start >= 0);
3004 _dbus_assert (len >= 0);
3005 _dbus_assert (start <= real->len);
3007 if (len > real->len - start)
3010 if (len > DBUS_MAXIMUM_NAME_LENGTH)
3017 s = real->str + start;
3030 /* No dot allowed in member names */
3038 * Checks that the given range of the string is a valid error name
3039 * in the D-BUS protocol. This includes a length restriction, etc.,
3040 * see the specification. It does not validate UTF-8, that has to be
3041 * done separately for now.
3043 * @todo this is inconsistent with most of DBusString in that
3044 * it allows a start,len range that isn't in the string.
3046 * @param str the string
3047 * @param start first byte index to check
3048 * @param len number of bytes to check
3049 * @returns #TRUE if the byte range exists and is a valid name
3052 _dbus_string_validate_error_name (const DBusString *str,
3056 /* Same restrictions as interface name at the moment */
3057 return _dbus_string_validate_interface (str, start, len);
3061 * Checks that the given range of the string is a valid service name
3062 * in the D-BUS protocol. This includes a length restriction, etc.,
3063 * see the specification. It does not validate UTF-8, that has to be
3064 * done separately for now.
3066 * @todo this is inconsistent with most of DBusString in that
3067 * it allows a start,len range that isn't in the string.
3069 * @todo change spec to disallow more things, such as spaces in the
3072 * @param str the string
3073 * @param start first byte index to check
3074 * @param len number of bytes to check
3075 * @returns #TRUE if the byte range exists and is a valid name
3078 _dbus_string_validate_service (const DBusString *str,
3082 const unsigned char *s;
3083 const unsigned char *end;
3084 dbus_bool_t saw_dot;
3085 dbus_bool_t is_base_service;
3087 DBUS_CONST_STRING_PREAMBLE (str);
3088 _dbus_assert (start >= 0);
3089 _dbus_assert (len >= 0);
3090 _dbus_assert (start <= real->len);
3092 if (len > real->len - start)
3095 if (len > DBUS_MAXIMUM_NAME_LENGTH)
3101 is_base_service = _dbus_string_get_byte (str, start) == ':';
3102 if (is_base_service)
3103 return TRUE; /* can have any content */
3105 /* non-base-service must have the '.' indicating a namespace */
3108 s = real->str + start;
3125 * Clears all allocated bytes in the string to zero.
3127 * @param str the string
3130 _dbus_string_zero (DBusString *str)
3132 DBUS_STRING_PREAMBLE (str);
3134 memset (real->str - real->align_offset, '\0', real->allocated);
3138 #ifdef DBUS_BUILD_TESTS
3139 #include "dbus-test.h"
3143 test_max_len (DBusString *str,
3148 if (!_dbus_string_set_length (str, max_len - 1))
3149 _dbus_assert_not_reached ("setting len to one less than max should have worked");
3152 if (!_dbus_string_set_length (str, max_len))
3153 _dbus_assert_not_reached ("setting len to max len should have worked");
3155 if (_dbus_string_set_length (str, max_len + 1))
3156 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
3158 if (!_dbus_string_set_length (str, 0))
3159 _dbus_assert_not_reached ("setting len to zero should have worked");
3163 test_base64_roundtrip (const unsigned char *data,
3171 len = strlen (data);
3173 if (!_dbus_string_init (&orig))
3174 _dbus_assert_not_reached ("could not init string");
3176 if (!_dbus_string_init (&encoded))
3177 _dbus_assert_not_reached ("could not init string");
3179 if (!_dbus_string_init (&decoded))
3180 _dbus_assert_not_reached ("could not init string");
3182 if (!_dbus_string_append_len (&orig, data, len))
3183 _dbus_assert_not_reached ("couldn't append orig data");
3185 if (!_dbus_string_base64_encode (&orig, 0, &encoded, 0))
3186 _dbus_assert_not_reached ("could not encode");
3188 if (!_dbus_string_base64_decode (&encoded, 0, &decoded, 0))
3189 _dbus_assert_not_reached ("could not decode");
3191 if (!_dbus_string_equal (&orig, &decoded))
3195 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
3196 _dbus_string_get_length (&orig),
3197 _dbus_string_get_length (&encoded),
3198 _dbus_string_get_length (&decoded));
3199 printf ("Original: %s\n", data);
3200 s = _dbus_string_get_const_data (&decoded);
3201 printf ("Decoded: %s\n", s);
3202 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
3205 _dbus_string_free (&orig);
3206 _dbus_string_free (&encoded);
3207 _dbus_string_free (&decoded);
3211 test_hex_roundtrip (const unsigned char *data,
3219 len = strlen (data);
3221 if (!_dbus_string_init (&orig))
3222 _dbus_assert_not_reached ("could not init string");
3224 if (!_dbus_string_init (&encoded))
3225 _dbus_assert_not_reached ("could not init string");
3227 if (!_dbus_string_init (&decoded))
3228 _dbus_assert_not_reached ("could not init string");
3230 if (!_dbus_string_append_len (&orig, data, len))
3231 _dbus_assert_not_reached ("couldn't append orig data");
3233 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
3234 _dbus_assert_not_reached ("could not encode");
3236 if (!_dbus_string_hex_decode (&encoded, 0, &decoded, 0))
3237 _dbus_assert_not_reached ("could not decode");
3239 if (!_dbus_string_equal (&orig, &decoded))
3243 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
3244 _dbus_string_get_length (&orig),
3245 _dbus_string_get_length (&encoded),
3246 _dbus_string_get_length (&decoded));
3247 printf ("Original: %s\n", data);
3248 s = _dbus_string_get_const_data (&decoded);
3249 printf ("Decoded: %s\n", s);
3250 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
3253 _dbus_string_free (&orig);
3254 _dbus_string_free (&encoded);
3255 _dbus_string_free (&decoded);
3258 typedef void (* TestRoundtripFunc) (const unsigned char *data,
3261 test_roundtrips (TestRoundtripFunc func)
3263 (* func) ("Hello this is a string\n", -1);
3264 (* func) ("Hello this is a string\n1", -1);
3265 (* func) ("Hello this is a string\n12", -1);
3266 (* func) ("Hello this is a string\n123", -1);
3267 (* func) ("Hello this is a string\n1234", -1);
3268 (* func) ("Hello this is a string\n12345", -1);
3272 (* func) ("123", 3);
3273 (* func) ("1234", 4);
3274 (* func) ("12345", 5);
3278 (* func) ("123", 4);
3279 (* func) ("1234", 5);
3280 (* func) ("12345", 6);
3282 unsigned char buf[512];
3286 while (i < _DBUS_N_ELEMENTS (buf))
3292 while (i < _DBUS_N_ELEMENTS (buf))
3302 * @ingroup DBusStringInternals
3303 * Unit test for DBusString.
3305 * @todo Need to write tests for _dbus_string_copy() and
3306 * _dbus_string_move() moving to/from each of start/middle/end of a
3307 * string. Also need tests for _dbus_string_move_len ()
3309 * @returns #TRUE on success.
3312 _dbus_string_test (void)
3319 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 };
3322 const char *valid_paths[] = {
3328 const char *invalid_paths[] = {
3342 while (i < _DBUS_N_ELEMENTS (lens))
3344 if (!_dbus_string_init (&str))
3345 _dbus_assert_not_reached ("failed to init string");
3347 set_max_length (&str, lens[i]);
3349 test_max_len (&str, lens[i]);
3350 _dbus_string_free (&str);
3355 /* Test shortening and setting length */
3357 while (i < _DBUS_N_ELEMENTS (lens))
3361 if (!_dbus_string_init (&str))
3362 _dbus_assert_not_reached ("failed to init string");
3364 set_max_length (&str, lens[i]);
3366 if (!_dbus_string_set_length (&str, lens[i]))
3367 _dbus_assert_not_reached ("failed to set string length");
3372 _dbus_assert (_dbus_string_get_length (&str) == j);
3375 _dbus_string_shorten (&str, 1);
3376 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
3381 _dbus_string_free (&str);
3386 /* Test appending data */
3387 if (!_dbus_string_init (&str))
3388 _dbus_assert_not_reached ("failed to init string");
3393 if (!_dbus_string_append (&str, "a"))
3394 _dbus_assert_not_reached ("failed to append string to string\n");
3396 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
3398 if (!_dbus_string_append_byte (&str, 'b'))
3399 _dbus_assert_not_reached ("failed to append byte to string\n");
3401 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
3406 _dbus_string_free (&str);
3408 /* Check steal_data */
3410 if (!_dbus_string_init (&str))
3411 _dbus_assert_not_reached ("failed to init string");
3413 if (!_dbus_string_append (&str, "Hello World"))
3414 _dbus_assert_not_reached ("could not append to string");
3416 i = _dbus_string_get_length (&str);
3418 if (!_dbus_string_steal_data (&str, &s))
3419 _dbus_assert_not_reached ("failed to steal data");
3421 _dbus_assert (_dbus_string_get_length (&str) == 0);
3422 _dbus_assert (((int)strlen (s)) == i);
3428 if (!_dbus_string_append (&str, "Hello World"))
3429 _dbus_assert_not_reached ("could not append to string");
3431 i = _dbus_string_get_length (&str);
3433 if (!_dbus_string_init (&other))
3434 _dbus_assert_not_reached ("could not init string");
3436 if (!_dbus_string_move (&str, 0, &other, 0))
3437 _dbus_assert_not_reached ("could not move");
3439 _dbus_assert (_dbus_string_get_length (&str) == 0);
3440 _dbus_assert (_dbus_string_get_length (&other) == i);
3442 if (!_dbus_string_append (&str, "Hello World"))
3443 _dbus_assert_not_reached ("could not append to string");
3445 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
3446 _dbus_assert_not_reached ("could not move");
3448 _dbus_assert (_dbus_string_get_length (&str) == 0);
3449 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3451 if (!_dbus_string_append (&str, "Hello World"))
3452 _dbus_assert_not_reached ("could not append to string");
3454 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3455 _dbus_assert_not_reached ("could not move");
3457 _dbus_assert (_dbus_string_get_length (&str) == 0);
3458 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3460 _dbus_string_free (&other);
3464 if (!_dbus_string_append (&str, "Hello World"))
3465 _dbus_assert_not_reached ("could not append to string");
3467 i = _dbus_string_get_length (&str);
3469 if (!_dbus_string_init (&other))
3470 _dbus_assert_not_reached ("could not init string");
3472 if (!_dbus_string_copy (&str, 0, &other, 0))
3473 _dbus_assert_not_reached ("could not copy");
3475 _dbus_assert (_dbus_string_get_length (&str) == i);
3476 _dbus_assert (_dbus_string_get_length (&other) == i);
3478 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
3479 _dbus_assert_not_reached ("could not copy");
3481 _dbus_assert (_dbus_string_get_length (&str) == i);
3482 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3483 _dbus_assert (_dbus_string_equal_c_str (&other,
3484 "Hello WorldHello World"));
3486 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3487 _dbus_assert_not_reached ("could not copy");
3489 _dbus_assert (_dbus_string_get_length (&str) == i);
3490 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3491 _dbus_assert (_dbus_string_equal_c_str (&other,
3492 "Hello WorldHello WorldHello World"));
3494 _dbus_string_free (&str);
3495 _dbus_string_free (&other);
3499 if (!_dbus_string_init (&str))
3500 _dbus_assert_not_reached ("failed to init string");
3502 if (!_dbus_string_append (&str, "Hello World"))
3503 _dbus_assert_not_reached ("could not append to string");
3505 i = _dbus_string_get_length (&str);
3507 if (!_dbus_string_init (&other))
3508 _dbus_assert_not_reached ("could not init string");
3510 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3511 &other, 0, _dbus_string_get_length (&other)))
3512 _dbus_assert_not_reached ("could not replace");
3514 _dbus_assert (_dbus_string_get_length (&str) == i);
3515 _dbus_assert (_dbus_string_get_length (&other) == i);
3516 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
3518 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3520 _dbus_assert_not_reached ("could not replace center space");
3522 _dbus_assert (_dbus_string_get_length (&str) == i);
3523 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3524 _dbus_assert (_dbus_string_equal_c_str (&other,
3525 "HelloHello WorldWorld"));
3528 if (!_dbus_string_replace_len (&str, 1, 1,
3530 _dbus_string_get_length (&other) - 1,
3532 _dbus_assert_not_reached ("could not replace end character");
3534 _dbus_assert (_dbus_string_get_length (&str) == i);
3535 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3536 _dbus_assert (_dbus_string_equal_c_str (&other,
3537 "HelloHello WorldWorle"));
3539 _dbus_string_free (&str);
3540 _dbus_string_free (&other);
3542 /* Check append/get unichar */
3544 if (!_dbus_string_init (&str))
3545 _dbus_assert_not_reached ("failed to init string");
3548 if (!_dbus_string_append_unichar (&str, 0xfffc))
3549 _dbus_assert_not_reached ("failed to append unichar");
3551 _dbus_string_get_unichar (&str, 0, &ch, &i);
3553 _dbus_assert (ch == 0xfffc);
3554 _dbus_assert (i == _dbus_string_get_length (&str));
3556 _dbus_string_free (&str);
3558 /* Check insert/set/get byte */
3560 if (!_dbus_string_init (&str))
3561 _dbus_assert_not_reached ("failed to init string");
3563 if (!_dbus_string_append (&str, "Hello"))
3564 _dbus_assert_not_reached ("failed to append Hello");
3566 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
3567 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
3568 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
3569 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
3570 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
3572 _dbus_string_set_byte (&str, 1, 'q');
3573 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
3575 if (!_dbus_string_insert_byte (&str, 0, 255))
3576 _dbus_assert_not_reached ("can't insert byte");
3578 if (!_dbus_string_insert_byte (&str, 2, 'Z'))
3579 _dbus_assert_not_reached ("can't insert byte");
3581 if (!_dbus_string_insert_byte (&str, _dbus_string_get_length (&str), 'W'))
3582 _dbus_assert_not_reached ("can't insert byte");
3584 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
3585 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
3586 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
3587 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'q');
3588 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'l');
3589 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'l');
3590 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'o');
3591 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'W');
3593 _dbus_string_free (&str);
3595 /* Check append/parse int/double */
3597 if (!_dbus_string_init (&str))
3598 _dbus_assert_not_reached ("failed to init string");
3600 if (!_dbus_string_append_int (&str, 27))
3601 _dbus_assert_not_reached ("failed to append int");
3603 i = _dbus_string_get_length (&str);
3605 if (!_dbus_string_parse_int (&str, 0, &v, &end))
3606 _dbus_assert_not_reached ("failed to parse int");
3608 _dbus_assert (v == 27);
3609 _dbus_assert (end == i);
3611 _dbus_string_free (&str);
3613 if (!_dbus_string_init (&str))
3614 _dbus_assert_not_reached ("failed to init string");
3616 if (!_dbus_string_append_double (&str, 50.3))
3617 _dbus_assert_not_reached ("failed to append float");
3619 i = _dbus_string_get_length (&str);
3621 if (!_dbus_string_parse_double (&str, 0, &d, &end))
3622 _dbus_assert_not_reached ("failed to parse float");
3624 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
3625 _dbus_assert (end == i);
3627 _dbus_string_free (&str);
3630 if (!_dbus_string_init (&str))
3631 _dbus_assert_not_reached ("failed to init string");
3633 if (!_dbus_string_append (&str, "Hello"))
3634 _dbus_assert_not_reached ("couldn't append to string");
3636 if (!_dbus_string_find (&str, 0, "He", &i))
3637 _dbus_assert_not_reached ("didn't find 'He'");
3638 _dbus_assert (i == 0);
3640 if (!_dbus_string_find (&str, 0, "Hello", &i))
3641 _dbus_assert_not_reached ("didn't find 'Hello'");
3642 _dbus_assert (i == 0);
3644 if (!_dbus_string_find (&str, 0, "ello", &i))
3645 _dbus_assert_not_reached ("didn't find 'ello'");
3646 _dbus_assert (i == 1);
3648 if (!_dbus_string_find (&str, 0, "lo", &i))
3649 _dbus_assert_not_reached ("didn't find 'lo'");
3650 _dbus_assert (i == 3);
3652 if (!_dbus_string_find (&str, 2, "lo", &i))
3653 _dbus_assert_not_reached ("didn't find 'lo'");
3654 _dbus_assert (i == 3);
3656 if (_dbus_string_find (&str, 4, "lo", &i))
3657 _dbus_assert_not_reached ("did find 'lo'");
3659 if (!_dbus_string_find (&str, 0, "l", &i))
3660 _dbus_assert_not_reached ("didn't find 'l'");
3661 _dbus_assert (i == 2);
3663 if (!_dbus_string_find (&str, 0, "H", &i))
3664 _dbus_assert_not_reached ("didn't find 'H'");
3665 _dbus_assert (i == 0);
3667 if (!_dbus_string_find (&str, 0, "", &i))
3668 _dbus_assert_not_reached ("didn't find ''");
3669 _dbus_assert (i == 0);
3671 if (_dbus_string_find (&str, 0, "Hello!", NULL))
3672 _dbus_assert_not_reached ("Did find 'Hello!'");
3674 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
3675 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
3677 if (_dbus_string_find (&str, 0, "ill", NULL))
3678 _dbus_assert_not_reached ("Did find 'ill'");
3680 if (_dbus_string_find (&str, 0, "q", NULL))
3681 _dbus_assert_not_reached ("Did find 'q'");
3683 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
3684 _dbus_assert_not_reached ("Didn't find 'He'");
3686 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
3687 _dbus_assert_not_reached ("Did find 'Hello'");
3689 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
3690 _dbus_assert_not_reached ("Did not find 'H'");
3691 _dbus_assert (i == 0);
3693 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
3694 _dbus_assert_not_reached ("Did not find 'o'");
3695 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
3697 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
3698 _dbus_assert_not_reached ("Did find 'o'");
3699 _dbus_assert (i == -1);
3701 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
3702 _dbus_assert_not_reached ("Did find 'e'");
3703 _dbus_assert (i == -1);
3705 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
3706 _dbus_assert_not_reached ("Didn't find 'e'");
3707 _dbus_assert (i == 1);
3709 _dbus_string_free (&str);
3711 /* Base 64 and Hex encoding */
3712 test_roundtrips (test_base64_roundtrip);
3713 test_roundtrips (test_hex_roundtrip);
3715 /* Path validation */
3717 while (i < (int) _DBUS_N_ELEMENTS (valid_paths))
3719 _dbus_string_init_const (&str, valid_paths[i]);
3721 if (!_dbus_string_validate_path (&str, 0,
3722 _dbus_string_get_length (&str)))
3724 _dbus_warn ("Path \"%s\" should have been valid\n", valid_paths[i]);
3725 _dbus_assert_not_reached ("invalid path");
3732 while (i < (int) _DBUS_N_ELEMENTS (invalid_paths))
3734 _dbus_string_init_const (&str, invalid_paths[i]);
3736 if (_dbus_string_validate_path (&str, 0,
3737 _dbus_string_get_length (&str)))
3739 _dbus_warn ("Path \"%s\" should have been invalid\n", invalid_paths[i]);
3740 _dbus_assert_not_reached ("valid path");
3749 #endif /* DBUS_BUILD_TESTS */