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');
2128 static const signed char base64_table[] = {
2195 /** The minimum char that's a valid char in Base64-encoded text */
2196 #define UNBASE64_MIN_CHAR (43)
2197 /** The maximum char that's a valid char in Base64-encoded text */
2198 #define UNBASE64_MAX_CHAR (122)
2199 /** Must subtract this from a char's integer value before offsetting
2200 * into unbase64_table
2202 #define UNBASE64_TABLE_OFFSET UNBASE64_MIN_CHAR
2203 static const signed char unbase64_table[] = {
2287 * Encodes a string using Base64, as documented in RFC 2045.
2289 * @param source the string to encode
2290 * @param start byte index to start encoding
2291 * @param dest string where encoded data should be placed
2292 * @param insert_at where to place encoded data
2293 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2296 _dbus_string_base64_encode (const DBusString *source,
2302 unsigned int dest_len; /* unsigned for overflow checks below */
2303 const unsigned char *s;
2305 const unsigned char *triplet_end;
2306 const unsigned char *final_end;
2307 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
2308 _dbus_assert (source != dest);
2310 /* For each 24 bits (3 bytes) of input, we have 4 bytes of
2313 source_len = real_source->len - start;
2314 dest_len = (source_len / 3) * 4;
2315 if (source_len % 3 != 0)
2318 if (dest_len > (unsigned int) real_dest->max_length)
2321 if (source_len == 0)
2324 if (!open_gap (dest_len, real_dest, insert_at))
2327 d = real_dest->str + insert_at;
2328 s = real_source->str + start;
2329 final_end = real_source->str + (start + source_len);
2330 triplet_end = final_end - (source_len % 3);
2331 _dbus_assert (triplet_end <= final_end);
2332 _dbus_assert ((final_end - triplet_end) < 3);
2334 #define ENCODE_64(v) (base64_table[ (unsigned char) (v) ])
2335 #define SIX_BITS_MASK (0x3f)
2336 _dbus_assert (SIX_BITS_MASK < _DBUS_N_ELEMENTS (base64_table));
2338 while (s != triplet_end)
2340 unsigned int triplet;
2342 triplet = s[2] | (s[1] << 8) | (s[0] << 16);
2344 /* Encode each 6 bits. */
2346 *d++ = ENCODE_64 (triplet >> 18);
2347 *d++ = ENCODE_64 ((triplet >> 12) & SIX_BITS_MASK);
2348 *d++ = ENCODE_64 ((triplet >> 6) & SIX_BITS_MASK);
2349 *d++ = ENCODE_64 (triplet & SIX_BITS_MASK);
2354 switch (final_end - triplet_end)
2358 unsigned int doublet;
2360 doublet = s[1] | (s[0] << 8);
2362 *d++ = ENCODE_64 (doublet >> 12);
2363 *d++ = ENCODE_64 ((doublet >> 6) & SIX_BITS_MASK);
2364 *d++ = ENCODE_64 (doublet & SIX_BITS_MASK);
2370 unsigned int singlet;
2374 *d++ = ENCODE_64 ((singlet >> 6) & SIX_BITS_MASK);
2375 *d++ = ENCODE_64 (singlet & SIX_BITS_MASK);
2384 _dbus_assert (d == (real_dest->str + (insert_at + dest_len)));
2390 * Decodes a string from Base64, as documented in RFC 2045.
2392 * @todo sort out the AUDIT comment in here. The case it mentions
2393 * ("====" or "x===") is not allowed in correct base64, so need to
2394 * decide what to do with that kind of input. Probably ignore it
2395 * since we ignore any other junk seen.
2397 * @param source the string to decode
2398 * @param start byte index to start decode
2399 * @param dest string where decoded data should be placed
2400 * @param insert_at where to place decoded data
2401 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
2404 _dbus_string_base64_decode (const DBusString *source,
2413 unsigned int triplet = 0;
2416 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
2417 _dbus_assert (source != dest);
2419 source_len = real_source->len - start;
2420 s = real_source->str + start;
2421 end = real_source->str + source_len;
2423 if (source_len == 0)
2426 if (!_dbus_string_init (&result))
2433 /* The idea is to just skip anything that isn't
2434 * a base64 char - it's allowed to have whitespace,
2435 * newlines, etc. in here. We also ignore trailing
2436 * base64 chars, though that's suspicious.
2439 if (*s >= UNBASE64_MIN_CHAR &&
2440 *s <= UNBASE64_MAX_CHAR)
2444 /* '=' is padding, doesn't represent additional data
2445 * but does increment our count.
2454 val = unbase64_table[(*s) - UNBASE64_TABLE_OFFSET];
2459 triplet |= (unsigned int) val;
2464 if (sextet_count == 4)
2466 /* no pad = 3 bytes, 1 pad = 2 bytes, 2 pad = 1 byte */
2469 /* AUDIT: Comment doesn't mention 4 pad => 0,
2470 * 3 pad => 1 byte, though the code should
2471 * work fine if those are the required outputs.
2473 * I assume that the spec requires dropping
2474 * the top two bits of, say, ///= which is > 2
2475 * bytes worth of bits. (Or otherwise, you couldn't
2476 * actually represent 2 byte sequences.
2481 if (!_dbus_string_append_byte (&result,
2488 if (!_dbus_string_append_byte (&result,
2489 (triplet >> 8) & 0xff))
2493 if (!_dbus_string_append_byte (&result,
2506 if (!_dbus_string_move (&result, 0, dest, insert_at))
2508 _dbus_string_free (&result);
2512 _dbus_string_free (&result);
2517 _dbus_string_free (&result);
2523 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2524 * encoded. (Each byte is two hex digits.)
2526 * @param source the string to encode
2527 * @param start byte index to start encoding
2528 * @param dest string where encoded data should be placed
2529 * @param insert_at where to place encoded data
2530 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2533 _dbus_string_hex_encode (const DBusString *source,
2539 const char hexdigits[16] = {
2540 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2541 'a', 'b', 'c', 'd', 'e', 'f'
2543 const unsigned char *p;
2544 const unsigned char *end;
2547 _dbus_assert (start <= _dbus_string_get_length (source));
2549 if (!_dbus_string_init (&result))
2554 p = (const unsigned char*) _dbus_string_get_const_data (source);
2555 end = p + _dbus_string_get_length (source);
2560 if (!_dbus_string_append_byte (&result,
2561 hexdigits[(*p >> 4)]))
2564 if (!_dbus_string_append_byte (&result,
2565 hexdigits[(*p & 0x0f)]))
2571 if (!_dbus_string_move (&result, 0, dest, insert_at))
2577 _dbus_string_free (&result);
2582 * Decodes a string from hex encoding.
2584 * @param source the string to decode
2585 * @param start byte index to start decode
2586 * @param dest string where decoded data should be placed
2587 * @param insert_at where to place decoded data
2588 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
2591 _dbus_string_hex_decode (const DBusString *source,
2597 const unsigned char *p;
2598 const unsigned char *end;
2600 dbus_bool_t high_bits;
2602 _dbus_assert (start <= _dbus_string_get_length (source));
2604 if (!_dbus_string_init (&result))
2610 p = (const unsigned char*) _dbus_string_get_const_data (source);
2611 end = p + _dbus_string_get_length (source);
2676 _dbus_verbose ("invalid character '%c' in hex encoded text\n",
2683 if (!_dbus_string_append_byte (&result,
2692 len = _dbus_string_get_length (&result);
2694 b = _dbus_string_get_byte (&result, len - 1);
2698 _dbus_string_set_byte (&result, len - 1, b);
2701 high_bits = !high_bits;
2706 if (!_dbus_string_move (&result, 0, dest, insert_at))
2712 _dbus_string_free (&result);
2717 * Checks that the given range of the string is valid ASCII with no
2718 * nul bytes. If the given range is not entirely contained in the
2719 * string, returns #FALSE.
2721 * @todo this is inconsistent with most of DBusString in that
2722 * it allows a start,len range that isn't in the string.
2724 * @param str the string
2725 * @param start first byte index to check
2726 * @param len number of bytes to check
2727 * @returns #TRUE if the byte range exists and is all valid ASCII
2730 _dbus_string_validate_ascii (const DBusString *str,
2734 const unsigned char *s;
2735 const unsigned char *end;
2736 DBUS_CONST_STRING_PREAMBLE (str);
2737 _dbus_assert (start >= 0);
2738 _dbus_assert (start <= real->len);
2739 _dbus_assert (len >= 0);
2741 if (len > real->len - start)
2744 s = real->str + start;
2748 if (_DBUS_UNLIKELY (*s == '\0' ||
2749 ((*s & ~0x7f) != 0)))
2759 * Checks that the given range of the string is valid UTF-8. If the
2760 * given range is not entirely contained in the string, returns
2761 * #FALSE. If the string contains any nul bytes in the given range,
2762 * returns #FALSE. If the start and start+len are not on character
2763 * boundaries, returns #FALSE.
2765 * @todo this is inconsistent with most of DBusString in that
2766 * it allows a start,len range that isn't in the string.
2768 * @param str the string
2769 * @param start first byte index to check
2770 * @param len number of bytes to check
2771 * @returns #TRUE if the byte range exists and is all valid UTF-8
2774 _dbus_string_validate_utf8 (const DBusString *str,
2778 const unsigned char *p;
2779 const unsigned char *end;
2780 DBUS_CONST_STRING_PREAMBLE (str);
2781 _dbus_assert (start >= 0);
2782 _dbus_assert (start <= real->len);
2783 _dbus_assert (len >= 0);
2785 /* we are doing _DBUS_UNLIKELY() here which might be
2786 * dubious in a generic library like GLib, but in D-BUS
2787 * we know we're validating messages and that it would
2788 * only be evil/broken apps that would have invalid
2789 * UTF-8. Also, this function seems to be a performance
2790 * bottleneck in profiles.
2793 if (_DBUS_UNLIKELY (len > real->len - start))
2796 p = real->str + start;
2801 int i, mask = 0, char_len;
2802 dbus_unichar_t result;
2803 unsigned char c = (unsigned char) *p;
2805 UTF8_COMPUTE (c, mask, char_len);
2807 if (_DBUS_UNLIKELY (char_len == -1))
2810 /* check that the expected number of bytes exists in the remaining length */
2811 if (_DBUS_UNLIKELY ((end - p) < char_len))
2814 UTF8_GET (result, p, i, mask, char_len);
2816 if (_DBUS_UNLIKELY (UTF8_LENGTH (result) != char_len)) /* Check for overlong UTF-8 */
2819 if (_DBUS_UNLIKELY (result == (dbus_unichar_t)-1))
2822 if (_DBUS_UNLIKELY (!UNICODE_VALID (result)))
2828 /* See that we covered the entire length if a length was
2831 if (_DBUS_UNLIKELY (p != end))
2838 * Checks that the given range of the string is all nul bytes. If the
2839 * given range is not entirely contained in the string, returns
2842 * @todo this is inconsistent with most of DBusString in that
2843 * it allows a start,len range that isn't in the string.
2845 * @param str the string
2846 * @param start first byte index to check
2847 * @param len number of bytes to check
2848 * @returns #TRUE if the byte range exists and is all nul bytes
2851 _dbus_string_validate_nul (const DBusString *str,
2855 const unsigned char *s;
2856 const unsigned char *end;
2857 DBUS_CONST_STRING_PREAMBLE (str);
2858 _dbus_assert (start >= 0);
2859 _dbus_assert (len >= 0);
2860 _dbus_assert (start <= real->len);
2862 if (len > real->len - start)
2865 s = real->str + start;
2869 if (_DBUS_UNLIKELY (*s != '\0'))
2878 * Checks that the given range of the string is a valid object path
2879 * name in the D-BUS protocol. This includes a length restriction,
2880 * etc., see the specification. It does not validate UTF-8, that has
2881 * to be done separately for now.
2883 * @todo this is inconsistent with most of DBusString in that
2884 * it allows a start,len range that isn't in the string.
2886 * @todo change spec to disallow more things, such as spaces in the
2889 * @param str the string
2890 * @param start first byte index to check
2891 * @param len number of bytes to check
2892 * @returns #TRUE if the byte range exists and is a valid name
2895 _dbus_string_validate_path (const DBusString *str,
2899 const unsigned char *s;
2900 const unsigned char *end;
2901 const unsigned char *last_slash;
2903 DBUS_CONST_STRING_PREAMBLE (str);
2904 _dbus_assert (start >= 0);
2905 _dbus_assert (len >= 0);
2906 _dbus_assert (start <= real->len);
2908 if (len > real->len - start)
2911 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2917 s = real->str + start;
2929 if ((s - last_slash) < 2)
2930 return FALSE; /* no empty path components allowed */
2938 if ((end - last_slash) < 2 &&
2940 return FALSE; /* trailing slash not allowed unless the string is "/" */
2945 #define VALID_INITIAL_NAME_CHARACTER(c) \
2946 ( ((c) >= 'A' && (c) <= 'Z') || \
2947 ((c) >= 'a' && (c) <= 'z') || \
2950 #define VALID_NAME_CHARACTER(c) \
2951 ( ((c) >= '0' && (c) <= '9') || \
2952 ((c) >= 'A' && (c) <= 'Z') || \
2953 ((c) >= 'a' && (c) <= 'z') || \
2957 * Checks that the given range of the string is a valid interface name
2958 * in the D-BUS protocol. This includes a length restriction and an
2959 * ASCII subset, see the specification.
2961 * @todo this is inconsistent with most of DBusString in that
2962 * it allows a start,len range that isn't in the string.
2964 * @param str the string
2965 * @param start first byte index to check
2966 * @param len number of bytes to check
2967 * @returns #TRUE if the byte range exists and is a valid name
2970 _dbus_string_validate_interface (const DBusString *str,
2974 const unsigned char *s;
2975 const unsigned char *end;
2976 const unsigned char *iface;
2977 const unsigned char *last_dot;
2979 DBUS_CONST_STRING_PREAMBLE (str);
2980 _dbus_assert (start >= 0);
2981 _dbus_assert (len >= 0);
2982 _dbus_assert (start <= real->len);
2984 if (len > real->len - start)
2987 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2994 iface = real->str + start;
2998 /* check special cases of first char so it doesn't have to be done
2999 * in the loop. Note we know len > 0
3001 if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
3003 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
3012 if (_DBUS_UNLIKELY ((s + 1) == end))
3014 else if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*(s + 1))))
3017 ++s; /* we just validated the next char, so skip two */
3019 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
3027 if (_DBUS_UNLIKELY (last_dot == NULL))
3034 * Checks that the given range of the string is a valid member name
3035 * in the D-BUS protocol. This includes a length restriction, etc.,
3036 * see the specification.
3038 * @todo this is inconsistent with most of DBusString in that
3039 * it allows a start,len range that isn't in the string.
3041 * @param str the string
3042 * @param start first byte index to check
3043 * @param len number of bytes to check
3044 * @returns #TRUE if the byte range exists and is a valid name
3047 _dbus_string_validate_member (const DBusString *str,
3051 const unsigned char *s;
3052 const unsigned char *end;
3053 const unsigned char *member;
3055 DBUS_CONST_STRING_PREAMBLE (str);
3056 _dbus_assert (start >= 0);
3057 _dbus_assert (len >= 0);
3058 _dbus_assert (start <= real->len);
3060 if (len > real->len - start)
3063 if (len > DBUS_MAXIMUM_NAME_LENGTH)
3069 member = real->str + start;
3073 /* check special cases of first char so it doesn't have to be done
3074 * in the loop. Note we know len > 0
3077 if (_DBUS_UNLIKELY (!VALID_INITIAL_NAME_CHARACTER (*s)))
3084 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
3096 * Checks that the given range of the string is a valid error name
3097 * in the D-BUS protocol. This includes a length restriction, etc.,
3098 * see the specification.
3100 * @todo this is inconsistent with most of DBusString in that
3101 * it allows a start,len range that isn't in the string.
3103 * @param str the string
3104 * @param start first byte index to check
3105 * @param len number of bytes to check
3106 * @returns #TRUE if the byte range exists and is a valid name
3109 _dbus_string_validate_error_name (const DBusString *str,
3113 /* Same restrictions as interface name at the moment */
3114 return _dbus_string_validate_interface (str, start, len);
3117 /* This assumes the first char exists and is ':' */
3119 _dbus_string_validate_base_service (const DBusString *str,
3123 const unsigned char *s;
3124 const unsigned char *end;
3125 const unsigned char *service;
3127 DBUS_CONST_STRING_PREAMBLE (str);
3128 _dbus_assert (start >= 0);
3129 _dbus_assert (len >= 0);
3130 _dbus_assert (start <= real->len);
3132 if (len > real->len - start)
3135 if (len > DBUS_MAXIMUM_NAME_LENGTH)
3138 _dbus_assert (len > 0);
3140 service = real->str + start;
3141 end = service + len;
3142 _dbus_assert (*service == ':');
3149 if (_DBUS_UNLIKELY ((s + 1) == end))
3151 if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*(s + 1))))
3153 ++s; /* we just validated the next char, so skip two */
3155 else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
3167 * Checks that the given range of the string is a valid service name
3168 * in the D-BUS protocol. This includes a length restriction, etc.,
3169 * see the specification.
3171 * @todo this is inconsistent with most of DBusString in that
3172 * it allows a start,len range that isn't in the string.
3174 * @param str the string
3175 * @param start first byte index to check
3176 * @param len number of bytes to check
3177 * @returns #TRUE if the byte range exists and is a valid name
3180 _dbus_string_validate_service (const DBusString *str,
3184 if (_DBUS_UNLIKELY (len == 0))
3186 if (_dbus_string_get_byte (str, start) == ':')
3187 return _dbus_string_validate_base_service (str, start, len);
3189 return _dbus_string_validate_interface (str, start, len);
3193 * Clears all allocated bytes in the string to zero.
3195 * @param str the string
3198 _dbus_string_zero (DBusString *str)
3200 DBUS_STRING_PREAMBLE (str);
3202 memset (real->str - real->align_offset, '\0', real->allocated);
3206 #ifdef DBUS_BUILD_TESTS
3207 #include "dbus-test.h"
3211 test_max_len (DBusString *str,
3216 if (!_dbus_string_set_length (str, max_len - 1))
3217 _dbus_assert_not_reached ("setting len to one less than max should have worked");
3220 if (!_dbus_string_set_length (str, max_len))
3221 _dbus_assert_not_reached ("setting len to max len should have worked");
3223 if (_dbus_string_set_length (str, max_len + 1))
3224 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
3226 if (!_dbus_string_set_length (str, 0))
3227 _dbus_assert_not_reached ("setting len to zero should have worked");
3231 test_base64_roundtrip (const unsigned char *data,
3239 len = strlen (data);
3241 if (!_dbus_string_init (&orig))
3242 _dbus_assert_not_reached ("could not init string");
3244 if (!_dbus_string_init (&encoded))
3245 _dbus_assert_not_reached ("could not init string");
3247 if (!_dbus_string_init (&decoded))
3248 _dbus_assert_not_reached ("could not init string");
3250 if (!_dbus_string_append_len (&orig, data, len))
3251 _dbus_assert_not_reached ("couldn't append orig data");
3253 if (!_dbus_string_base64_encode (&orig, 0, &encoded, 0))
3254 _dbus_assert_not_reached ("could not encode");
3256 if (!_dbus_string_base64_decode (&encoded, 0, &decoded, 0))
3257 _dbus_assert_not_reached ("could not decode");
3259 if (!_dbus_string_equal (&orig, &decoded))
3263 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
3264 _dbus_string_get_length (&orig),
3265 _dbus_string_get_length (&encoded),
3266 _dbus_string_get_length (&decoded));
3267 printf ("Original: %s\n", data);
3268 s = _dbus_string_get_const_data (&decoded);
3269 printf ("Decoded: %s\n", s);
3270 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
3273 _dbus_string_free (&orig);
3274 _dbus_string_free (&encoded);
3275 _dbus_string_free (&decoded);
3279 test_hex_roundtrip (const unsigned char *data,
3287 len = strlen (data);
3289 if (!_dbus_string_init (&orig))
3290 _dbus_assert_not_reached ("could not init string");
3292 if (!_dbus_string_init (&encoded))
3293 _dbus_assert_not_reached ("could not init string");
3295 if (!_dbus_string_init (&decoded))
3296 _dbus_assert_not_reached ("could not init string");
3298 if (!_dbus_string_append_len (&orig, data, len))
3299 _dbus_assert_not_reached ("couldn't append orig data");
3301 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
3302 _dbus_assert_not_reached ("could not encode");
3304 if (!_dbus_string_hex_decode (&encoded, 0, &decoded, 0))
3305 _dbus_assert_not_reached ("could not decode");
3307 if (!_dbus_string_equal (&orig, &decoded))
3311 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
3312 _dbus_string_get_length (&orig),
3313 _dbus_string_get_length (&encoded),
3314 _dbus_string_get_length (&decoded));
3315 printf ("Original: %s\n", data);
3316 s = _dbus_string_get_const_data (&decoded);
3317 printf ("Decoded: %s\n", s);
3318 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
3321 _dbus_string_free (&orig);
3322 _dbus_string_free (&encoded);
3323 _dbus_string_free (&decoded);
3326 typedef void (* TestRoundtripFunc) (const unsigned char *data,
3329 test_roundtrips (TestRoundtripFunc func)
3331 (* func) ("Hello this is a string\n", -1);
3332 (* func) ("Hello this is a string\n1", -1);
3333 (* func) ("Hello this is a string\n12", -1);
3334 (* func) ("Hello this is a string\n123", -1);
3335 (* func) ("Hello this is a string\n1234", -1);
3336 (* func) ("Hello this is a string\n12345", -1);
3340 (* func) ("123", 3);
3341 (* func) ("1234", 4);
3342 (* func) ("12345", 5);
3346 (* func) ("123", 4);
3347 (* func) ("1234", 5);
3348 (* func) ("12345", 6);
3350 unsigned char buf[512];
3354 while (i < _DBUS_N_ELEMENTS (buf))
3360 while (i < _DBUS_N_ELEMENTS (buf))
3370 * @ingroup DBusStringInternals
3371 * Unit test for DBusString.
3373 * @todo Need to write tests for _dbus_string_copy() and
3374 * _dbus_string_move() moving to/from each of start/middle/end of a
3375 * string. Also need tests for _dbus_string_move_len ()
3377 * @returns #TRUE on success.
3380 _dbus_string_test (void)
3387 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 };
3390 const char *valid_paths[] = {
3396 const char *invalid_paths[] = {
3412 const char *valid_interfaces[] = {
3413 "org.freedesktop.Foo",
3415 "Blah.Blah.Blah.Blah.Blah",
3418 "a0.b1.c2.d3.e4.f5.g6",
3421 const char *invalid_interfaces[] = {
3449 const char *valid_base_services[] = {
3458 ":abce.freedesktop.blah"
3460 const char *invalid_base_services[] = {
3474 const char *valid_members[] = {
3482 const char *invalid_members[] = {
3498 while (i < _DBUS_N_ELEMENTS (lens))
3500 if (!_dbus_string_init (&str))
3501 _dbus_assert_not_reached ("failed to init string");
3503 set_max_length (&str, lens[i]);
3505 test_max_len (&str, lens[i]);
3506 _dbus_string_free (&str);
3511 /* Test shortening and setting length */
3513 while (i < _DBUS_N_ELEMENTS (lens))
3517 if (!_dbus_string_init (&str))
3518 _dbus_assert_not_reached ("failed to init string");
3520 set_max_length (&str, lens[i]);
3522 if (!_dbus_string_set_length (&str, lens[i]))
3523 _dbus_assert_not_reached ("failed to set string length");
3528 _dbus_assert (_dbus_string_get_length (&str) == j);
3531 _dbus_string_shorten (&str, 1);
3532 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
3537 _dbus_string_free (&str);
3542 /* Test appending data */
3543 if (!_dbus_string_init (&str))
3544 _dbus_assert_not_reached ("failed to init string");
3549 if (!_dbus_string_append (&str, "a"))
3550 _dbus_assert_not_reached ("failed to append string to string\n");
3552 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
3554 if (!_dbus_string_append_byte (&str, 'b'))
3555 _dbus_assert_not_reached ("failed to append byte to string\n");
3557 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
3562 _dbus_string_free (&str);
3564 /* Check steal_data */
3566 if (!_dbus_string_init (&str))
3567 _dbus_assert_not_reached ("failed to init string");
3569 if (!_dbus_string_append (&str, "Hello World"))
3570 _dbus_assert_not_reached ("could not append to string");
3572 i = _dbus_string_get_length (&str);
3574 if (!_dbus_string_steal_data (&str, &s))
3575 _dbus_assert_not_reached ("failed to steal data");
3577 _dbus_assert (_dbus_string_get_length (&str) == 0);
3578 _dbus_assert (((int)strlen (s)) == i);
3584 if (!_dbus_string_append (&str, "Hello World"))
3585 _dbus_assert_not_reached ("could not append to string");
3587 i = _dbus_string_get_length (&str);
3589 if (!_dbus_string_init (&other))
3590 _dbus_assert_not_reached ("could not init string");
3592 if (!_dbus_string_move (&str, 0, &other, 0))
3593 _dbus_assert_not_reached ("could not move");
3595 _dbus_assert (_dbus_string_get_length (&str) == 0);
3596 _dbus_assert (_dbus_string_get_length (&other) == i);
3598 if (!_dbus_string_append (&str, "Hello World"))
3599 _dbus_assert_not_reached ("could not append to string");
3601 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
3602 _dbus_assert_not_reached ("could not move");
3604 _dbus_assert (_dbus_string_get_length (&str) == 0);
3605 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3607 if (!_dbus_string_append (&str, "Hello World"))
3608 _dbus_assert_not_reached ("could not append to string");
3610 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3611 _dbus_assert_not_reached ("could not move");
3613 _dbus_assert (_dbus_string_get_length (&str) == 0);
3614 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3616 _dbus_string_free (&other);
3620 if (!_dbus_string_append (&str, "Hello World"))
3621 _dbus_assert_not_reached ("could not append to string");
3623 i = _dbus_string_get_length (&str);
3625 if (!_dbus_string_init (&other))
3626 _dbus_assert_not_reached ("could not init string");
3628 if (!_dbus_string_copy (&str, 0, &other, 0))
3629 _dbus_assert_not_reached ("could not copy");
3631 _dbus_assert (_dbus_string_get_length (&str) == i);
3632 _dbus_assert (_dbus_string_get_length (&other) == i);
3634 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
3635 _dbus_assert_not_reached ("could not copy");
3637 _dbus_assert (_dbus_string_get_length (&str) == i);
3638 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3639 _dbus_assert (_dbus_string_equal_c_str (&other,
3640 "Hello WorldHello World"));
3642 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3643 _dbus_assert_not_reached ("could not copy");
3645 _dbus_assert (_dbus_string_get_length (&str) == i);
3646 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3647 _dbus_assert (_dbus_string_equal_c_str (&other,
3648 "Hello WorldHello WorldHello World"));
3650 _dbus_string_free (&str);
3651 _dbus_string_free (&other);
3655 if (!_dbus_string_init (&str))
3656 _dbus_assert_not_reached ("failed to init string");
3658 if (!_dbus_string_append (&str, "Hello World"))
3659 _dbus_assert_not_reached ("could not append to string");
3661 i = _dbus_string_get_length (&str);
3663 if (!_dbus_string_init (&other))
3664 _dbus_assert_not_reached ("could not init string");
3666 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3667 &other, 0, _dbus_string_get_length (&other)))
3668 _dbus_assert_not_reached ("could not replace");
3670 _dbus_assert (_dbus_string_get_length (&str) == i);
3671 _dbus_assert (_dbus_string_get_length (&other) == i);
3672 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
3674 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3676 _dbus_assert_not_reached ("could not replace center space");
3678 _dbus_assert (_dbus_string_get_length (&str) == i);
3679 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3680 _dbus_assert (_dbus_string_equal_c_str (&other,
3681 "HelloHello WorldWorld"));
3684 if (!_dbus_string_replace_len (&str, 1, 1,
3686 _dbus_string_get_length (&other) - 1,
3688 _dbus_assert_not_reached ("could not replace end character");
3690 _dbus_assert (_dbus_string_get_length (&str) == i);
3691 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3692 _dbus_assert (_dbus_string_equal_c_str (&other,
3693 "HelloHello WorldWorle"));
3695 _dbus_string_free (&str);
3696 _dbus_string_free (&other);
3698 /* Check append/get unichar */
3700 if (!_dbus_string_init (&str))
3701 _dbus_assert_not_reached ("failed to init string");
3704 if (!_dbus_string_append_unichar (&str, 0xfffc))
3705 _dbus_assert_not_reached ("failed to append unichar");
3707 _dbus_string_get_unichar (&str, 0, &ch, &i);
3709 _dbus_assert (ch == 0xfffc);
3710 _dbus_assert (i == _dbus_string_get_length (&str));
3712 _dbus_string_free (&str);
3714 /* Check insert/set/get byte */
3716 if (!_dbus_string_init (&str))
3717 _dbus_assert_not_reached ("failed to init string");
3719 if (!_dbus_string_append (&str, "Hello"))
3720 _dbus_assert_not_reached ("failed to append Hello");
3722 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
3723 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
3724 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
3725 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
3726 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
3728 _dbus_string_set_byte (&str, 1, 'q');
3729 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
3731 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
3732 _dbus_assert_not_reached ("can't insert byte");
3734 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
3735 _dbus_assert_not_reached ("can't insert byte");
3737 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
3738 _dbus_assert_not_reached ("can't insert byte");
3740 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
3741 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
3742 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
3743 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
3744 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
3745 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
3746 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
3747 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
3748 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
3749 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
3750 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
3752 _dbus_string_free (&str);
3754 /* Check append/parse int/double */
3756 if (!_dbus_string_init (&str))
3757 _dbus_assert_not_reached ("failed to init string");
3759 if (!_dbus_string_append_int (&str, 27))
3760 _dbus_assert_not_reached ("failed to append int");
3762 i = _dbus_string_get_length (&str);
3764 if (!_dbus_string_parse_int (&str, 0, &v, &end))
3765 _dbus_assert_not_reached ("failed to parse int");
3767 _dbus_assert (v == 27);
3768 _dbus_assert (end == i);
3770 _dbus_string_free (&str);
3772 if (!_dbus_string_init (&str))
3773 _dbus_assert_not_reached ("failed to init string");
3775 if (!_dbus_string_append_double (&str, 50.3))
3776 _dbus_assert_not_reached ("failed to append float");
3778 i = _dbus_string_get_length (&str);
3780 if (!_dbus_string_parse_double (&str, 0, &d, &end))
3781 _dbus_assert_not_reached ("failed to parse float");
3783 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
3784 _dbus_assert (end == i);
3786 _dbus_string_free (&str);
3789 if (!_dbus_string_init (&str))
3790 _dbus_assert_not_reached ("failed to init string");
3792 if (!_dbus_string_append (&str, "Hello"))
3793 _dbus_assert_not_reached ("couldn't append to string");
3795 if (!_dbus_string_find (&str, 0, "He", &i))
3796 _dbus_assert_not_reached ("didn't find 'He'");
3797 _dbus_assert (i == 0);
3799 if (!_dbus_string_find (&str, 0, "Hello", &i))
3800 _dbus_assert_not_reached ("didn't find 'Hello'");
3801 _dbus_assert (i == 0);
3803 if (!_dbus_string_find (&str, 0, "ello", &i))
3804 _dbus_assert_not_reached ("didn't find 'ello'");
3805 _dbus_assert (i == 1);
3807 if (!_dbus_string_find (&str, 0, "lo", &i))
3808 _dbus_assert_not_reached ("didn't find 'lo'");
3809 _dbus_assert (i == 3);
3811 if (!_dbus_string_find (&str, 2, "lo", &i))
3812 _dbus_assert_not_reached ("didn't find 'lo'");
3813 _dbus_assert (i == 3);
3815 if (_dbus_string_find (&str, 4, "lo", &i))
3816 _dbus_assert_not_reached ("did find 'lo'");
3818 if (!_dbus_string_find (&str, 0, "l", &i))
3819 _dbus_assert_not_reached ("didn't find 'l'");
3820 _dbus_assert (i == 2);
3822 if (!_dbus_string_find (&str, 0, "H", &i))
3823 _dbus_assert_not_reached ("didn't find 'H'");
3824 _dbus_assert (i == 0);
3826 if (!_dbus_string_find (&str, 0, "", &i))
3827 _dbus_assert_not_reached ("didn't find ''");
3828 _dbus_assert (i == 0);
3830 if (_dbus_string_find (&str, 0, "Hello!", NULL))
3831 _dbus_assert_not_reached ("Did find 'Hello!'");
3833 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
3834 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
3836 if (_dbus_string_find (&str, 0, "ill", NULL))
3837 _dbus_assert_not_reached ("Did find 'ill'");
3839 if (_dbus_string_find (&str, 0, "q", NULL))
3840 _dbus_assert_not_reached ("Did find 'q'");
3842 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
3843 _dbus_assert_not_reached ("Didn't find 'He'");
3845 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
3846 _dbus_assert_not_reached ("Did find 'Hello'");
3848 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
3849 _dbus_assert_not_reached ("Did not find 'H'");
3850 _dbus_assert (i == 0);
3852 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
3853 _dbus_assert_not_reached ("Did not find 'o'");
3854 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
3856 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
3857 _dbus_assert_not_reached ("Did find 'o'");
3858 _dbus_assert (i == -1);
3860 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
3861 _dbus_assert_not_reached ("Did find 'e'");
3862 _dbus_assert (i == -1);
3864 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
3865 _dbus_assert_not_reached ("Didn't find 'e'");
3866 _dbus_assert (i == 1);
3868 _dbus_string_free (&str);
3870 /* Base 64 and Hex encoding */
3871 test_roundtrips (test_base64_roundtrip);
3872 test_roundtrips (test_hex_roundtrip);
3874 /* Path validation */
3876 while (i < (int) _DBUS_N_ELEMENTS (valid_paths))
3878 _dbus_string_init_const (&str, valid_paths[i]);
3880 if (!_dbus_string_validate_path (&str, 0,
3881 _dbus_string_get_length (&str)))
3883 _dbus_warn ("Path \"%s\" should have been valid\n", valid_paths[i]);
3884 _dbus_assert_not_reached ("invalid path");
3891 while (i < (int) _DBUS_N_ELEMENTS (invalid_paths))
3893 _dbus_string_init_const (&str, invalid_paths[i]);
3895 if (_dbus_string_validate_path (&str, 0,
3896 _dbus_string_get_length (&str)))
3898 _dbus_warn ("Path \"%s\" should have been invalid\n", invalid_paths[i]);
3899 _dbus_assert_not_reached ("valid path");
3905 /* Interface validation */
3907 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
3909 _dbus_string_init_const (&str, valid_interfaces[i]);
3911 if (!_dbus_string_validate_interface (&str, 0,
3912 _dbus_string_get_length (&str)))
3914 _dbus_warn ("Interface \"%s\" should have been valid\n", valid_interfaces[i]);
3915 _dbus_assert_not_reached ("invalid interface");
3922 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
3924 _dbus_string_init_const (&str, invalid_interfaces[i]);
3926 if (_dbus_string_validate_interface (&str, 0,
3927 _dbus_string_get_length (&str)))
3929 _dbus_warn ("Interface \"%s\" should have been invalid\n", invalid_interfaces[i]);
3930 _dbus_assert_not_reached ("valid interface");
3936 /* Service validation (check that valid interfaces are valid services,
3937 * and invalid interfaces are invalid services except if they start with ':')
3940 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
3942 _dbus_string_init_const (&str, valid_interfaces[i]);
3944 if (!_dbus_string_validate_service (&str, 0,
3945 _dbus_string_get_length (&str)))
3947 _dbus_warn ("Service \"%s\" should have been valid\n", valid_interfaces[i]);
3948 _dbus_assert_not_reached ("invalid service");
3955 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
3957 if (invalid_interfaces[i][0] != ':')
3959 _dbus_string_init_const (&str, invalid_interfaces[i]);
3961 if (_dbus_string_validate_service (&str, 0,
3962 _dbus_string_get_length (&str)))
3964 _dbus_warn ("Service \"%s\" should have been invalid\n", invalid_interfaces[i]);
3965 _dbus_assert_not_reached ("valid service");
3972 /* Base service validation */
3974 while (i < (int) _DBUS_N_ELEMENTS (valid_base_services))
3976 _dbus_string_init_const (&str, valid_base_services[i]);
3978 if (!_dbus_string_validate_service (&str, 0,
3979 _dbus_string_get_length (&str)))
3981 _dbus_warn ("Service \"%s\" should have been valid\n", valid_base_services[i]);
3982 _dbus_assert_not_reached ("invalid base service");
3989 while (i < (int) _DBUS_N_ELEMENTS (invalid_base_services))
3991 _dbus_string_init_const (&str, invalid_base_services[i]);
3993 if (_dbus_string_validate_service (&str, 0,
3994 _dbus_string_get_length (&str)))
3996 _dbus_warn ("Service \"%s\" should have been invalid\n", invalid_base_services[i]);
3997 _dbus_assert_not_reached ("valid base service");
4004 /* Error name validation (currently identical to interfaces)
4007 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
4009 _dbus_string_init_const (&str, valid_interfaces[i]);
4011 if (!_dbus_string_validate_error_name (&str, 0,
4012 _dbus_string_get_length (&str)))
4014 _dbus_warn ("Error name \"%s\" should have been valid\n", valid_interfaces[i]);
4015 _dbus_assert_not_reached ("invalid error name");
4022 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
4024 if (invalid_interfaces[i][0] != ':')
4026 _dbus_string_init_const (&str, invalid_interfaces[i]);
4028 if (_dbus_string_validate_error_name (&str, 0,
4029 _dbus_string_get_length (&str)))
4031 _dbus_warn ("Error name \"%s\" should have been invalid\n", invalid_interfaces[i]);
4032 _dbus_assert_not_reached ("valid error name");
4039 /* Member validation */
4041 while (i < (int) _DBUS_N_ELEMENTS (valid_members))
4043 _dbus_string_init_const (&str, valid_members[i]);
4045 if (!_dbus_string_validate_member (&str, 0,
4046 _dbus_string_get_length (&str)))
4048 _dbus_warn ("Member \"%s\" should have been valid\n", valid_members[i]);
4049 _dbus_assert_not_reached ("invalid member");
4056 while (i < (int) _DBUS_N_ELEMENTS (invalid_members))
4058 _dbus_string_init_const (&str, invalid_members[i]);
4060 if (_dbus_string_validate_member (&str, 0,
4061 _dbus_string_get_length (&str)))
4063 _dbus_warn ("Member \"%s\" should have been invalid\n", invalid_members[i]);
4064 _dbus_assert_not_reached ("valid member");
4070 /* Validate claimed length longer than real length */
4071 _dbus_string_init_const (&str, "abc.efg");
4072 if (_dbus_string_validate_service (&str, 0, 8))
4073 _dbus_assert_not_reached ("validated too-long string");
4074 if (_dbus_string_validate_interface (&str, 0, 8))
4075 _dbus_assert_not_reached ("validated too-long string");
4076 if (_dbus_string_validate_error_name (&str, 0, 8))
4077 _dbus_assert_not_reached ("validated too-long string");
4079 _dbus_string_init_const (&str, "abc");
4080 if (_dbus_string_validate_member (&str, 0, 4))
4081 _dbus_assert_not_reached ("validated too-long string");
4083 /* Validate string exceeding max name length */
4084 if (!_dbus_string_init (&str))
4085 _dbus_assert_not_reached ("no memory");
4087 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
4088 if (!_dbus_string_append (&str, "abc.def"))
4089 _dbus_assert_not_reached ("no memory");
4091 if (_dbus_string_validate_service (&str, 0, _dbus_string_get_length (&str)))
4092 _dbus_assert_not_reached ("validated overmax string");
4093 if (_dbus_string_validate_interface (&str, 0, _dbus_string_get_length (&str)))
4094 _dbus_assert_not_reached ("validated overmax string");
4095 if (_dbus_string_validate_error_name (&str, 0, _dbus_string_get_length (&str)))
4096 _dbus_assert_not_reached ("validated overmax string");
4098 /* overlong member */
4099 _dbus_string_set_length (&str, 0);
4100 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
4101 if (!_dbus_string_append (&str, "abc"))
4102 _dbus_assert_not_reached ("no memory");
4104 if (_dbus_string_validate_member (&str, 0, _dbus_string_get_length (&str)))
4105 _dbus_assert_not_reached ("validated overmax string");
4107 /* overlong base service */
4108 _dbus_string_set_length (&str, 0);
4109 _dbus_string_append (&str, ":");
4110 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
4111 if (!_dbus_string_append (&str, "abc"))
4112 _dbus_assert_not_reached ("no memory");
4114 if (_dbus_string_validate_service (&str, 0, _dbus_string_get_length (&str)))
4115 _dbus_assert_not_reached ("validated overmax string");
4117 _dbus_string_free (&str);
4122 #endif /* DBUS_BUILD_TESTS */