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 */
28 #include "dbus-marshal.h"
29 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
30 #include "dbus-string-private.h"
31 #include "dbus-protocol.h"
34 * @defgroup DBusString string class
35 * @ingroup DBusInternals
36 * @brief DBusString data structure
38 * Types and functions related to DBusString. DBusString is intended
39 * to be a string class that makes it hard to mess up security issues
40 * (and just in general harder to write buggy code). It should be
41 * used (or extended and then used) rather than the libc stuff in
42 * string.h. The string class is a bit inconvenient at spots because
43 * it handles out-of-memory failures and tries to be extra-robust.
45 * A DBusString has a maximum length set at initialization time; this
46 * can be used to ensure that a buffer doesn't get too big. The
47 * _dbus_string_lengthen() method checks for overflow, and for max
48 * length being exceeded.
50 * Try to avoid conversion to a plain C string, i.e. add methods on
51 * the string object instead, only convert to C string when passing
52 * things out to the public API. In particular, no sprintf, strcpy,
53 * strcat, any of that should be used. The GString feature of
54 * accepting negative numbers for "length of string" is also absent,
55 * because it could keep us from detecting bogus huge lengths. i.e. if
56 * we passed in some bogus huge length it would be taken to mean
57 * "current length of string" instead of "broken crack"
61 * @defgroup DBusStringInternals DBusString implementation details
62 * @ingroup DBusInternals
63 * @brief DBusString implementation details
65 * The guts of DBusString.
71 * We allocate 1 byte for nul termination, plus 7 bytes for possible
72 * align_offset, so we always need 8 bytes on top of the string's
73 * length to be in the allocated block.
75 #define ALLOCATION_PADDING 8
78 * This is the maximum max length (and thus also the maximum length)
81 #define MAX_MAX_LENGTH (_DBUS_INT_MAX - ALLOCATION_PADDING)
84 * Checks a bunch of assertions about a string object
86 * @param real the DBusRealString
88 #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)
91 * Checks assertions about a string object that needs to be
92 * modifiable - may not be locked or const. Also declares
93 * the "real" variable pointing to DBusRealString.
94 * @param str the string
96 #define DBUS_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
97 DBUS_GENERIC_STRING_PREAMBLE (real); \
98 _dbus_assert (!(real)->constant); \
99 _dbus_assert (!(real)->locked)
102 * Checks assertions about a string object that may be locked but
103 * can't be const. i.e. a string object that we can free. Also
104 * declares the "real" variable pointing to DBusRealString.
106 * @param str the string
108 #define DBUS_LOCKED_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
109 DBUS_GENERIC_STRING_PREAMBLE (real); \
110 _dbus_assert (!(real)->constant)
113 * Checks assertions about a string that may be const or locked. Also
114 * declares the "real" variable pointing to DBusRealString.
115 * @param str the string.
117 #define DBUS_CONST_STRING_PREAMBLE(str) const DBusRealString *real = (DBusRealString*) str; \
118 DBUS_GENERIC_STRING_PREAMBLE (real)
123 * @addtogroup DBusString
128 fixup_alignment (DBusRealString *real)
132 unsigned int old_align_offset;
134 /* we have to have extra space in real->allocated for the align offset and nul byte */
135 _dbus_assert (real->len <= real->allocated - ALLOCATION_PADDING);
137 old_align_offset = real->align_offset;
138 real_block = real->str - old_align_offset;
140 aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
142 real->align_offset = aligned - real_block;
145 if (old_align_offset != real->align_offset)
147 /* Here comes the suck */
148 memmove (real_block + real->align_offset,
149 real_block + old_align_offset,
153 _dbus_assert (real->align_offset < 8);
154 _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
158 undo_alignment (DBusRealString *real)
160 if (real->align_offset != 0)
162 memmove (real->str - real->align_offset,
166 real->str = real->str - real->align_offset;
167 real->align_offset = 0;
172 * Initializes a string that can be up to the given allocation size
173 * before it has to realloc. The string starts life with zero length.
174 * The string must eventually be freed with _dbus_string_free().
176 * @param str memory to hold the string
177 * @param allocate_size amount to preallocate
178 * @returns #TRUE on success, #FALSE if no memory
181 _dbus_string_init_preallocated (DBusString *str,
184 DBusRealString *real;
186 _dbus_assert (str != NULL);
188 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
190 real = (DBusRealString*) str;
192 /* It's very important not to touch anything
193 * other than real->str if we're going to fail,
194 * since we also use this function to reset
195 * an existing string, e.g. in _dbus_string_steal_data()
198 real->str = dbus_malloc (ALLOCATION_PADDING + allocate_size);
199 if (real->str == NULL)
202 real->allocated = ALLOCATION_PADDING + allocate_size;
204 real->str[real->len] = '\0';
206 real->max_length = MAX_MAX_LENGTH;
207 real->constant = FALSE;
208 real->locked = FALSE;
209 real->invalid = FALSE;
210 real->align_offset = 0;
212 fixup_alignment (real);
218 * Initializes a string. The string starts life with zero length. The
219 * string must eventually be freed with _dbus_string_free().
221 * @param str memory to hold the string
222 * @returns #TRUE on success, #FALSE if no memory
225 _dbus_string_init (DBusString *str)
227 return _dbus_string_init_preallocated (str, 0);
230 /* The max length thing is sort of a historical artifact
231 * from a feature that turned out to be dumb; perhaps
232 * we should purge it entirely. The problem with
233 * the feature is that it looks like memory allocation
234 * failure, but is not a transient or resolvable failure.
237 set_max_length (DBusString *str,
240 DBusRealString *real;
242 real = (DBusRealString*) str;
244 real->max_length = max_length;
248 * Initializes a constant string. The value parameter is not copied
249 * (should be static), and the string may never be modified.
250 * It is safe but not necessary to call _dbus_string_free()
251 * on a const string. The string has a length limit of MAXINT - 8.
253 * @param str memory to use for the string
254 * @param value a string to be stored in str (not copied!!!)
257 _dbus_string_init_const (DBusString *str,
260 _dbus_assert (value != NULL);
262 _dbus_string_init_const_len (str, value,
267 * Initializes a constant string with a length. The value parameter is
268 * not copied (should be static), and the string may never be
269 * modified. It is safe but not necessary to call _dbus_string_free()
272 * @param str memory to use for the string
273 * @param value a string to be stored in str (not copied!!!)
274 * @param len the length to use
277 _dbus_string_init_const_len (DBusString *str,
281 DBusRealString *real;
283 _dbus_assert (str != NULL);
284 _dbus_assert (value != NULL);
285 _dbus_assert (len <= MAX_MAX_LENGTH);
286 _dbus_assert (len >= 0);
288 real = (DBusRealString*) str;
290 real->str = (char*) value;
292 real->allocated = real->len + ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
293 real->max_length = real->len + 1;
294 real->constant = TRUE;
295 real->invalid = FALSE;
297 /* We don't require const strings to be 8-byte aligned as the
298 * memory is coming from elsewhere.
303 * Frees a string created by _dbus_string_init().
305 * @param str memory where the string is stored.
308 _dbus_string_free (DBusString *str)
310 DBusRealString *real = (DBusRealString*) str;
311 DBUS_GENERIC_STRING_PREAMBLE (real);
315 dbus_free (real->str - real->align_offset);
317 real->invalid = TRUE;
320 #ifdef DBUS_BUILD_TESTS
321 /* Not using this feature at the moment,
322 * so marked DBUS_BUILD_TESTS-only
325 * Locks a string such that any attempts to change the string will
326 * result in aborting the program. Also, if the string is wasting a
327 * lot of memory (allocation is sufficiently larger than what the
328 * string is really using), _dbus_string_lock() will realloc the
329 * string's data to "compact" it.
331 * @param str the string to lock.
334 _dbus_string_lock (DBusString *str)
336 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
340 /* Try to realloc to avoid excess memory usage, since
341 * we know we won't change the string further
344 if (real->allocated - MAX_WASTE > real->len)
349 new_allocated = real->len + ALLOCATION_PADDING;
351 new_str = dbus_realloc (real->str - real->align_offset,
355 real->str = new_str + real->align_offset;
356 real->allocated = new_allocated;
357 fixup_alignment (real);
361 #endif /* DBUS_BUILD_TESTS */
364 reallocate_for_length (DBusRealString *real,
370 /* at least double our old allocation to avoid O(n), avoiding
373 if (real->allocated > (MAX_MAX_LENGTH + ALLOCATION_PADDING) / 2)
374 new_allocated = MAX_MAX_LENGTH + ALLOCATION_PADDING;
376 new_allocated = real->allocated * 2;
378 /* if you change the code just above here, run the tests without
379 * the following before you commit
381 #ifdef DBUS_BUILD_TESTS
382 new_allocated = 0; /* ensure a realloc every time so that we go
383 * through all malloc failure codepaths
387 /* But be sure we always alloc at least space for the new length */
388 new_allocated = MAX (new_allocated, new_length + ALLOCATION_PADDING);
390 _dbus_assert (new_allocated >= real->allocated); /* code relies on this */
391 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
395 real->str = new_str + real->align_offset;
396 real->allocated = new_allocated;
397 fixup_alignment (real);
403 set_length (DBusRealString *real,
406 /* Note, we are setting the length not including nul termination */
408 /* exceeding max length is the same as failure to allocate memory */
409 if (new_length > real->max_length)
411 else if (new_length > (real->allocated - ALLOCATION_PADDING) &&
412 !reallocate_for_length (real, new_length))
416 real->len = new_length;
417 real->str[real->len] = '\0';
424 DBusRealString *dest,
430 if (len > dest->max_length - dest->len)
431 return FALSE; /* detected overflow of dest->len + len below */
433 if (!set_length (dest, dest->len + len))
436 memmove (dest->str + insert_at + len,
437 dest->str + insert_at,
438 dest->len - len - insert_at);
444 * Gets the raw character buffer from the string. The returned buffer
445 * will be nul-terminated, but note that strings may contain binary
446 * data so there may be extra nul characters prior to the termination.
447 * This function should be little-used, extend DBusString or add
448 * stuff to dbus-sysdeps.c instead. It's an error to use this
449 * function on a const string.
451 * @param str the string
455 _dbus_string_get_data (DBusString *str)
457 DBUS_STRING_PREAMBLE (str);
463 * Gets the raw character buffer from a const string.
465 * @param str the string
466 * @returns the string data
469 _dbus_string_get_const_data (const DBusString *str)
471 DBUS_CONST_STRING_PREAMBLE (str);
477 * Gets a sub-portion of the raw character buffer from the
478 * string. The "len" field is required simply for error
479 * checking, to be sure you don't try to use more
480 * string than exists. The nul termination of the
481 * returned buffer remains at the end of the entire
482 * string, not at start + len.
484 * @param str the string
485 * @param start byte offset to return
486 * @param len length of segment to return
487 * @returns the string data
490 _dbus_string_get_data_len (DBusString *str,
494 DBUS_STRING_PREAMBLE (str);
495 _dbus_assert (start >= 0);
496 _dbus_assert (len >= 0);
497 _dbus_assert (start <= real->len);
498 _dbus_assert (len <= real->len - start);
500 return real->str + start;
504 * const version of _dbus_string_get_data_len().
506 * @param str the string
507 * @param start byte offset to return
508 * @param len length of segment to return
509 * @returns the string data
512 _dbus_string_get_const_data_len (const DBusString *str,
516 DBUS_CONST_STRING_PREAMBLE (str);
517 _dbus_assert (start >= 0);
518 _dbus_assert (len >= 0);
519 _dbus_assert (start <= real->len);
520 _dbus_assert (len <= real->len - start);
522 return real->str + start;
526 * Sets the value of the byte at the given position.
528 * @param str the string
529 * @param i the position
530 * @param byte the new value
533 _dbus_string_set_byte (DBusString *str,
537 DBUS_STRING_PREAMBLE (str);
538 _dbus_assert (i < real->len);
539 _dbus_assert (i >= 0);
545 * Gets the byte at the given position.
547 * @param str the string
548 * @param start the position
549 * @returns the byte at that position
552 _dbus_string_get_byte (const DBusString *str,
555 DBUS_CONST_STRING_PREAMBLE (str);
556 _dbus_assert (start < real->len);
557 _dbus_assert (start >= 0);
559 return real->str[start];
563 * Inserts the given byte at the given position.
565 * @param str the string
566 * @param i the position
567 * @param byte the value to insert
568 * @returns #TRUE on success
571 _dbus_string_insert_byte (DBusString *str,
575 DBUS_STRING_PREAMBLE (str);
576 _dbus_assert (i <= real->len);
577 _dbus_assert (i >= 0);
579 if (!open_gap (1, real, i))
588 * Like _dbus_string_get_data(), but removes the
589 * gotten data from the original string. The caller
590 * must free the data returned. This function may
591 * fail due to lack of memory, and return #FALSE.
593 * @param str the string
594 * @param data_return location to return the buffer
595 * @returns #TRUE on success
598 _dbus_string_steal_data (DBusString *str,
602 DBUS_STRING_PREAMBLE (str);
603 _dbus_assert (data_return != NULL);
605 undo_alignment (real);
607 *data_return = real->str;
609 old_max_length = real->max_length;
611 /* reset the string */
612 if (!_dbus_string_init (str))
614 /* hrm, put it back then */
615 real->str = *data_return;
617 fixup_alignment (real);
621 real->max_length = old_max_length;
627 * Like _dbus_string_get_data_len(), but removes the gotten data from
628 * the original string. The caller must free the data returned. This
629 * function may fail due to lack of memory, and return #FALSE.
630 * The returned string is nul-terminated and has length len.
632 * @todo this function is broken because on failure it
633 * may corrupt the source string.
635 * @param str the string
636 * @param data_return location to return the buffer
637 * @param start the start of segment to steal
638 * @param len the length of segment to steal
639 * @returns #TRUE on success
642 _dbus_string_steal_data_len (DBusString *str,
648 DBUS_STRING_PREAMBLE (str);
649 _dbus_assert (data_return != NULL);
650 _dbus_assert (start >= 0);
651 _dbus_assert (len >= 0);
652 _dbus_assert (start <= real->len);
653 _dbus_assert (len <= real->len - start);
655 if (!_dbus_string_init (&dest))
658 set_max_length (&dest, real->max_length);
660 if (!_dbus_string_move_len (str, start, len, &dest, 0))
662 _dbus_string_free (&dest);
666 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
667 if (!_dbus_string_steal_data (&dest, data_return))
669 _dbus_string_free (&dest);
673 _dbus_string_free (&dest);
679 * Copies the data from the string into a char*
681 * @param str the string
682 * @param data_return place to return the data
683 * @returns #TRUE on success, #FALSE on no memory
686 _dbus_string_copy_data (const DBusString *str,
689 DBUS_CONST_STRING_PREAMBLE (str);
690 _dbus_assert (data_return != NULL);
692 *data_return = dbus_malloc (real->len + 1);
693 if (*data_return == NULL)
696 memcpy (*data_return, real->str, real->len + 1);
702 * Copies a segment of the string into a char*
704 * @param str the string
705 * @param data_return place to return the data
706 * @param start start index
707 * @param len length to copy
708 * @returns #FALSE if no memory
711 _dbus_string_copy_data_len (const DBusString *str,
718 DBUS_CONST_STRING_PREAMBLE (str);
719 _dbus_assert (data_return != NULL);
720 _dbus_assert (start >= 0);
721 _dbus_assert (len >= 0);
722 _dbus_assert (start <= real->len);
723 _dbus_assert (len <= real->len - start);
725 if (!_dbus_string_init (&dest))
728 set_max_length (&dest, real->max_length);
730 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
732 _dbus_string_free (&dest);
736 if (!_dbus_string_steal_data (&dest, data_return))
738 _dbus_string_free (&dest);
742 _dbus_string_free (&dest);
747 * Gets the length of a string (not including nul termination).
749 * @returns the length.
752 _dbus_string_get_length (const DBusString *str)
754 DBUS_CONST_STRING_PREAMBLE (str);
760 * Makes a string longer by the given number of bytes. Checks whether
761 * adding additional_length to the current length would overflow an
762 * integer, and checks for exceeding a string's max length.
763 * The new bytes are not initialized, other than nul-terminating
764 * the end of the string. The uninitialized bytes may contain
765 * nul bytes or other junk.
767 * @param str a string
768 * @param additional_length length to add to the string.
769 * @returns #TRUE on success.
772 _dbus_string_lengthen (DBusString *str,
773 int additional_length)
775 DBUS_STRING_PREAMBLE (str);
776 _dbus_assert (additional_length >= 0);
778 if (additional_length > real->max_length - real->len)
779 return FALSE; /* would overflow */
781 return set_length (real,
782 real->len + additional_length);
786 * Makes a string shorter by the given number of bytes.
788 * @param str a string
789 * @param length_to_remove length to remove from the string.
792 _dbus_string_shorten (DBusString *str,
793 int length_to_remove)
795 DBUS_STRING_PREAMBLE (str);
796 _dbus_assert (length_to_remove >= 0);
797 _dbus_assert (length_to_remove <= real->len);
800 real->len - length_to_remove);
804 * Sets the length of a string. Can be used to truncate or lengthen
805 * the string. If the string is lengthened, the function may fail and
806 * return #FALSE. Newly-added bytes are not initialized, as with
807 * _dbus_string_lengthen().
809 * @param str a string
810 * @param length new length of the string.
811 * @returns #FALSE on failure.
814 _dbus_string_set_length (DBusString *str,
817 DBUS_STRING_PREAMBLE (str);
818 _dbus_assert (length >= 0);
820 return set_length (real, length);
824 align_length_then_lengthen (DBusString *str,
826 int then_lengthen_by)
828 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
830 DBUS_STRING_PREAMBLE (str);
831 _dbus_assert (alignment >= 1);
832 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
834 new_len = _DBUS_ALIGN_VALUE (real->len, alignment);
835 if (new_len > (unsigned long) real->max_length - then_lengthen_by)
837 new_len += then_lengthen_by;
839 delta = new_len - real->len;
840 _dbus_assert (delta >= 0);
845 if (!set_length (real, new_len))
848 /* delta == padding + then_lengthen_by
849 * new_len == old_len + padding + then_lengthen_by
851 if (then_lengthen_by < delta)
855 while (i < (new_len - then_lengthen_by))
866 * Align the length of a string to a specific alignment (typically 4 or 8)
867 * by appending nul bytes to the string.
869 * @param str a string
870 * @param alignment the alignment
871 * @returns #FALSE if no memory
874 _dbus_string_align_length (DBusString *str,
877 return align_length_then_lengthen (str, alignment, 0);
881 append (DBusRealString *real,
888 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
891 memcpy (real->str + (real->len - buffer_len),
899 * Appends a nul-terminated C-style string to a DBusString.
901 * @param str the DBusString
902 * @param buffer the nul-terminated characters to append
903 * @returns #FALSE if not enough memory.
906 _dbus_string_append (DBusString *str,
909 unsigned long buffer_len;
911 DBUS_STRING_PREAMBLE (str);
912 _dbus_assert (buffer != NULL);
914 buffer_len = strlen (buffer);
915 if (buffer_len > (unsigned long) real->max_length)
918 return append (real, buffer, buffer_len);
922 * Appends 4 bytes aligned on a 4 byte boundary
923 * with any alignment padding initialized to 0.
925 * @param str the DBusString
926 * @param octets 4 bytes to append
927 * @returns #FALSE if not enough memory.
930 _dbus_string_append_4_aligned (DBusString *str,
931 const unsigned char octets[4])
934 DBUS_STRING_PREAMBLE (str);
936 if (!align_length_then_lengthen (str, 4, 4))
939 p = (dbus_uint32_t*) (real->str + (real->len - 4));
940 *p = *((dbus_uint32_t*)octets);
946 * Appends 8 bytes aligned on an 8 byte boundary
947 * with any alignment padding initialized to 0.
949 * @param str the DBusString
950 * @param octets 4 bytes to append
951 * @returns #FALSE if not enough memory.
954 _dbus_string_append_8_aligned (DBusString *str,
955 const unsigned char octets[8])
957 #ifdef DBUS_HAVE_INT64
959 DBUS_STRING_PREAMBLE (str);
961 if (!align_length_then_lengthen (str, 8, 8))
964 p = (dbus_uint64_t*) (real->str + (real->len - 8));
965 *p = *((dbus_uint64_t*)octets);
968 DBUS_STRING_PREAMBLE (str);
970 if (!align_length_then_lengthen (str, 8, 8))
973 p = real->str + (real->len - 8);
983 _dbus_assert (p == (real->str + real->len));
990 * Appends block of bytes with the given length to a DBusString.
992 * @param str the DBusString
993 * @param buffer the bytes to append
994 * @param len the number of bytes to append
995 * @returns #FALSE if not enough memory.
998 _dbus_string_append_len (DBusString *str,
1002 DBUS_STRING_PREAMBLE (str);
1003 _dbus_assert (buffer != NULL);
1004 _dbus_assert (len >= 0);
1006 return append (real, buffer, len);
1010 * Appends a single byte to the string, returning #FALSE
1011 * if not enough memory.
1013 * @param str the string
1014 * @param byte the byte to append
1015 * @returns #TRUE on success
1018 _dbus_string_append_byte (DBusString *str,
1021 DBUS_STRING_PREAMBLE (str);
1023 if (!set_length (real, real->len + 1))
1026 real->str[real->len-1] = byte;
1032 * Appends a single Unicode character, encoding the character
1035 * @param str the string
1036 * @param ch the Unicode character
1039 _dbus_string_append_unichar (DBusString *str,
1047 DBUS_STRING_PREAMBLE (str);
1049 /* this code is from GLib but is pretty standard I think */
1058 else if (ch < 0x800)
1063 else if (ch < 0x10000)
1068 else if (ch < 0x200000)
1073 else if (ch < 0x4000000)
1084 if (len > (real->max_length - real->len))
1085 return FALSE; /* real->len + len would overflow */
1087 if (!set_length (real, real->len + len))
1090 out = real->str + (real->len - len);
1092 for (i = len - 1; i > 0; --i)
1094 out[i] = (ch & 0x3f) | 0x80;
1097 out[0] = ch | first;
1103 delete (DBusRealString *real,
1110 memmove (real->str + start, real->str + start + len, real->len - (start + len));
1112 real->str[real->len] = '\0';
1116 * Deletes a segment of a DBusString with length len starting at
1117 * start. (Hint: to clear an entire string, setting length to 0
1118 * with _dbus_string_set_length() is easier.)
1120 * @param str the DBusString
1121 * @param start where to start deleting
1122 * @param len the number of bytes to delete
1125 _dbus_string_delete (DBusString *str,
1129 DBUS_STRING_PREAMBLE (str);
1130 _dbus_assert (start >= 0);
1131 _dbus_assert (len >= 0);
1132 _dbus_assert (start <= real->len);
1133 _dbus_assert (len <= real->len - start);
1135 delete (real, start, len);
1139 copy (DBusRealString *source,
1142 DBusRealString *dest,
1148 if (!open_gap (len, dest, insert_at))
1151 memcpy (dest->str + insert_at,
1152 source->str + start,
1159 * Checks assertions for two strings we're copying a segment between,
1160 * and declares real_source/real_dest variables.
1162 * @param source the source string
1163 * @param start the starting offset
1164 * @param dest the dest string
1165 * @param insert_at where the copied segment is inserted
1167 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1168 DBusRealString *real_source = (DBusRealString*) source; \
1169 DBusRealString *real_dest = (DBusRealString*) dest; \
1170 _dbus_assert ((source) != (dest)); \
1171 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1172 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1173 _dbus_assert (!real_dest->constant); \
1174 _dbus_assert (!real_dest->locked); \
1175 _dbus_assert ((start) >= 0); \
1176 _dbus_assert ((start) <= real_source->len); \
1177 _dbus_assert ((insert_at) >= 0); \
1178 _dbus_assert ((insert_at) <= real_dest->len)
1181 * Moves the end of one string into another string. Both strings
1182 * must be initialized, valid strings.
1184 * @param source the source string
1185 * @param start where to chop off the source string
1186 * @param dest the destination string
1187 * @param insert_at where to move the chopped-off part of source string
1188 * @returns #FALSE if not enough memory
1191 _dbus_string_move (DBusString *source,
1196 DBusRealString *real_source = (DBusRealString*) source;
1197 _dbus_assert (start <= real_source->len);
1199 return _dbus_string_move_len (source, start,
1200 real_source->len - start,
1205 * Like _dbus_string_move(), but does not delete the section
1206 * of the source string that's copied to the dest string.
1208 * @param source the source string
1209 * @param start where to start copying the source string
1210 * @param dest the destination string
1211 * @param insert_at where to place the copied part of source string
1212 * @returns #FALSE if not enough memory
1215 _dbus_string_copy (const DBusString *source,
1220 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1222 return copy (real_source, start,
1223 real_source->len - start,
1229 * Like _dbus_string_move(), but can move a segment from
1230 * the middle of the source string.
1232 * @todo this doesn't do anything with max_length field.
1233 * we should probably just kill the max_length field though.
1235 * @param source the source string
1236 * @param start first byte of source string to move
1237 * @param len length of segment to move
1238 * @param dest the destination string
1239 * @param insert_at where to move the bytes from the source string
1240 * @returns #FALSE if not enough memory
1243 _dbus_string_move_len (DBusString *source,
1250 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1251 _dbus_assert (len >= 0);
1252 _dbus_assert ((start + len) <= real_source->len);
1259 else if (start == 0 &&
1260 len == real_source->len &&
1261 real_dest->len == 0)
1263 /* Short-circuit moving an entire existing string to an empty string
1264 * by just swapping the buffers.
1266 /* we assume ->constant doesn't matter as you can't have
1267 * a constant string involved in a move.
1269 #define ASSIGN_DATA(a, b) do { \
1270 (a)->str = (b)->str; \
1271 (a)->len = (b)->len; \
1272 (a)->allocated = (b)->allocated; \
1273 (a)->align_offset = (b)->align_offset; \
1278 ASSIGN_DATA (&tmp, real_source);
1279 ASSIGN_DATA (real_source, real_dest);
1280 ASSIGN_DATA (real_dest, &tmp);
1286 if (!copy (real_source, start, len,
1291 delete (real_source, start,
1299 * Like _dbus_string_copy(), but can copy a segment from the middle of
1300 * the source string.
1302 * @param source the source string
1303 * @param start where to start copying the source string
1304 * @param len length of segment to copy
1305 * @param dest the destination string
1306 * @param insert_at where to place the copied segment of source string
1307 * @returns #FALSE if not enough memory
1310 _dbus_string_copy_len (const DBusString *source,
1316 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1317 _dbus_assert (len >= 0);
1318 _dbus_assert (start <= real_source->len);
1319 _dbus_assert (len <= real_source->len - start);
1321 return copy (real_source, start, len,
1327 * Replaces a segment of dest string with a segment of source string.
1329 * @todo optimize the case where the two lengths are the same, and
1330 * avoid memmoving the data in the trailing part of the string twice.
1332 * @todo avoid inserting the source into dest, then deleting
1333 * the replaced chunk of dest (which creates a potentially large
1334 * intermediate string). Instead, extend the replaced chunk
1335 * of dest with padding to the same size as the source chunk,
1336 * then copy in the source bytes.
1338 * @param source the source string
1339 * @param start where to start copying the source string
1340 * @param len length of segment to copy
1341 * @param dest the destination string
1342 * @param replace_at start of segment of dest string to replace
1343 * @param replace_len length of segment of dest string to replace
1344 * @returns #FALSE if not enough memory
1348 _dbus_string_replace_len (const DBusString *source,
1355 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1356 _dbus_assert (len >= 0);
1357 _dbus_assert (start <= real_source->len);
1358 _dbus_assert (len <= real_source->len - start);
1359 _dbus_assert (replace_at >= 0);
1360 _dbus_assert (replace_at <= real_dest->len);
1361 _dbus_assert (replace_len <= real_dest->len - replace_at);
1363 if (!copy (real_source, start, len,
1364 real_dest, replace_at))
1367 delete (real_dest, replace_at + len, replace_len);
1372 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1373 * Pennington, and Tom Tromey are the authors and authorized relicense.
1376 /** computes length and mask of a unicode character
1377 * @param Char the char
1378 * @param Mask the mask variable to assign to
1379 * @param Len the length variable to assign to
1381 #define UTF8_COMPUTE(Char, Mask, Len) \
1387 else if ((Char & 0xe0) == 0xc0) \
1392 else if ((Char & 0xf0) == 0xe0) \
1397 else if ((Char & 0xf8) == 0xf0) \
1402 else if ((Char & 0xfc) == 0xf8) \
1407 else if ((Char & 0xfe) == 0xfc) \
1416 * computes length of a unicode character in UTF-8
1417 * @param Char the char
1419 #define UTF8_LENGTH(Char) \
1420 ((Char) < 0x80 ? 1 : \
1421 ((Char) < 0x800 ? 2 : \
1422 ((Char) < 0x10000 ? 3 : \
1423 ((Char) < 0x200000 ? 4 : \
1424 ((Char) < 0x4000000 ? 5 : 6)))))
1427 * Gets a UTF-8 value.
1429 * @param Result variable for extracted unicode char.
1430 * @param Chars the bytes to decode
1431 * @param Count counter variable
1432 * @param Mask mask for this char
1433 * @param Len length for this char in bytes
1435 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1436 (Result) = (Chars)[0] & (Mask); \
1437 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1439 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1445 (Result) |= ((Chars)[(Count)] & 0x3f); \
1449 * Check whether a unicode char is in a valid range.
1451 * @param Char the character
1453 #define UNICODE_VALID(Char) \
1454 ((Char) < 0x110000 && \
1455 (((Char) & 0xFFFFF800) != 0xD800) && \
1456 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1457 ((Char) & 0xFFFF) != 0xFFFF)
1460 * Gets a unicode character from a UTF-8 string. Does no validation;
1461 * you must verify that the string is valid UTF-8 in advance and must
1462 * pass in the start of a character.
1464 * @param str the string
1465 * @param start the start of the UTF-8 character.
1466 * @param ch_return location to return the character
1467 * @param end_return location to return the byte index of next character
1470 _dbus_string_get_unichar (const DBusString *str,
1472 dbus_unichar_t *ch_return,
1476 dbus_unichar_t result;
1479 DBUS_CONST_STRING_PREAMBLE (str);
1480 _dbus_assert (start >= 0);
1481 _dbus_assert (start <= real->len);
1486 *end_return = real->len;
1489 p = real->str + start;
1492 UTF8_COMPUTE (c, mask, len);
1495 UTF8_GET (result, p, i, mask, len);
1497 if (result == (dbus_unichar_t)-1)
1501 *ch_return = result;
1503 *end_return = start + len;
1507 * Finds the given substring in the string,
1508 * returning #TRUE and filling in the byte index
1509 * where the substring was found, if it was found.
1510 * Returns #FALSE if the substring wasn't found.
1511 * Sets *start to the length of the string if the substring
1514 * @param str the string
1515 * @param start where to start looking
1516 * @param substr the substring
1517 * @param found return location for where it was found, or #NULL
1518 * @returns #TRUE if found
1521 _dbus_string_find (const DBusString *str,
1526 return _dbus_string_find_to (str, start,
1527 ((const DBusRealString*)str)->len,
1532 * Finds the given substring in the string,
1533 * up to a certain position,
1534 * returning #TRUE and filling in the byte index
1535 * where the substring was found, if it was found.
1536 * Returns #FALSE if the substring wasn't found.
1537 * Sets *start to the length of the string if the substring
1540 * @param str the string
1541 * @param start where to start looking
1542 * @param end where to stop looking
1543 * @param substr the substring
1544 * @param found return location for where it was found, or #NULL
1545 * @returns #TRUE if found
1548 _dbus_string_find_to (const DBusString *str,
1555 DBUS_CONST_STRING_PREAMBLE (str);
1556 _dbus_assert (substr != NULL);
1557 _dbus_assert (start <= real->len);
1558 _dbus_assert (start >= 0);
1559 _dbus_assert (substr != NULL);
1560 _dbus_assert (end <= real->len);
1561 _dbus_assert (start <= end);
1563 /* we always "find" an empty string */
1564 if (*substr == '\0')
1574 if (real->str[i] == substr[0])
1580 if (substr[j - i] == '\0')
1582 else if (real->str[j] != substr[j - i])
1588 if (substr[j - i] == '\0')
1606 * Find the given byte scanning backward from the given start.
1607 * Sets *found to -1 if the byte is not found.
1609 * @param str the string
1610 * @param start the place to start scanning (will not find the byte at this point)
1611 * @param byte the byte to find
1612 * @param found return location for where it was found
1613 * @returns #TRUE if found
1616 _dbus_string_find_byte_backward (const DBusString *str,
1622 DBUS_CONST_STRING_PREAMBLE (str);
1623 _dbus_assert (start <= real->len);
1624 _dbus_assert (start >= 0);
1625 _dbus_assert (found != NULL);
1630 if (real->str[i] == byte)
1643 * Finds a blank (space or tab) in the string. Returns #TRUE
1644 * if found, #FALSE otherwise. If a blank is not found sets
1645 * *found to the length of the string.
1647 * @param str the string
1648 * @param start byte index to start looking
1649 * @param found place to store the location of the first blank
1650 * @returns #TRUE if a blank was found
1653 _dbus_string_find_blank (const DBusString *str,
1658 DBUS_CONST_STRING_PREAMBLE (str);
1659 _dbus_assert (start <= real->len);
1660 _dbus_assert (start >= 0);
1663 while (i < real->len)
1665 if (real->str[i] == ' ' ||
1666 real->str[i] == '\t')
1683 * Skips blanks from start, storing the first non-blank in *end
1684 * (blank is space or tab).
1686 * @param str the string
1687 * @param start where to start
1688 * @param end where to store the first non-blank byte index
1691 _dbus_string_skip_blank (const DBusString *str,
1696 DBUS_CONST_STRING_PREAMBLE (str);
1697 _dbus_assert (start <= real->len);
1698 _dbus_assert (start >= 0);
1701 while (i < real->len)
1703 if (!(real->str[i] == ' ' ||
1704 real->str[i] == '\t'))
1710 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1711 real->str[i] == '\t'));
1718 * Skips whitespace from start, storing the first non-whitespace in *end.
1719 * (whitespace is space, tab, newline, CR).
1721 * @param str the string
1722 * @param start where to start
1723 * @param end where to store the first non-whitespace byte index
1726 _dbus_string_skip_white (const DBusString *str,
1731 DBUS_CONST_STRING_PREAMBLE (str);
1732 _dbus_assert (start <= real->len);
1733 _dbus_assert (start >= 0);
1736 while (i < real->len)
1738 if (!(real->str[i] == ' ' ||
1739 real->str[i] == '\n' ||
1740 real->str[i] == '\r' ||
1741 real->str[i] == '\t'))
1747 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1748 real->str[i] == '\t'));
1755 * Assigns a newline-terminated or \r\n-terminated line from the front
1756 * of the string to the given dest string. The dest string's previous
1757 * contents are deleted. If the source string contains no newline,
1758 * moves the entire source string to the dest string.
1760 * @todo owen correctly notes that this is a stupid function (it was
1761 * written purely for test code,
1762 * e.g. dbus-message-builder.c). Probably should be enforced as test
1763 * code only with #ifdef DBUS_BUILD_TESTS
1765 * @param source the source string
1766 * @param dest the destination string (contents are replaced)
1767 * @returns #FALSE if no memory, or source has length 0
1770 _dbus_string_pop_line (DBusString *source,
1774 dbus_bool_t have_newline;
1776 _dbus_string_set_length (dest, 0);
1779 if (_dbus_string_find (source, 0, "\n", &eol))
1781 have_newline = TRUE;
1782 eol += 1; /* include newline */
1786 eol = _dbus_string_get_length (source);
1787 have_newline = FALSE;
1791 return FALSE; /* eof */
1793 if (!_dbus_string_move_len (source, 0, eol,
1799 /* dump the newline and the \r if we have one */
1802 dbus_bool_t have_cr;
1804 _dbus_assert (_dbus_string_get_length (dest) > 0);
1806 if (_dbus_string_get_length (dest) > 1 &&
1807 _dbus_string_get_byte (dest,
1808 _dbus_string_get_length (dest) - 2) == '\r')
1813 _dbus_string_set_length (dest,
1814 _dbus_string_get_length (dest) -
1822 * Deletes up to and including the first blank space
1825 * @param str the string
1828 _dbus_string_delete_first_word (DBusString *str)
1832 if (_dbus_string_find_blank (str, 0, &i))
1833 _dbus_string_skip_blank (str, i, &i);
1835 _dbus_string_delete (str, 0, i);
1839 * Deletes any leading blanks in the string
1841 * @param str the string
1844 _dbus_string_delete_leading_blanks (DBusString *str)
1848 _dbus_string_skip_blank (str, 0, &i);
1851 _dbus_string_delete (str, 0, i);
1855 * Tests two DBusString for equality.
1857 * @todo memcmp is probably faster
1859 * @param a first string
1860 * @param b second string
1861 * @returns #TRUE if equal
1864 _dbus_string_equal (const DBusString *a,
1865 const DBusString *b)
1867 const unsigned char *ap;
1868 const unsigned char *bp;
1869 const unsigned char *a_end;
1870 const DBusRealString *real_a = (const DBusRealString*) a;
1871 const DBusRealString *real_b = (const DBusRealString*) b;
1872 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1873 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1875 if (real_a->len != real_b->len)
1880 a_end = real_a->str + real_a->len;
1894 * Tests two DBusString for equality up to the given length.
1896 * @todo write a unit test
1898 * @todo memcmp is probably faster
1900 * @param a first string
1901 * @param b second string
1902 * @param len the lengh
1903 * @returns #TRUE if equal for the given number of bytes
1906 _dbus_string_equal_len (const DBusString *a,
1907 const DBusString *b,
1910 const unsigned char *ap;
1911 const unsigned char *bp;
1912 const unsigned char *a_end;
1913 const DBusRealString *real_a = (const DBusRealString*) a;
1914 const DBusRealString *real_b = (const DBusRealString*) b;
1915 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1916 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1918 if (real_a->len != real_b->len &&
1919 (real_a->len < len || real_b->len < len))
1924 a_end = real_a->str + MIN (real_a->len, len);
1938 * Checks whether a string is equal to a C string.
1940 * @param a the string
1941 * @param c_str the C string
1942 * @returns #TRUE if equal
1945 _dbus_string_equal_c_str (const DBusString *a,
1948 const unsigned char *ap;
1949 const unsigned char *bp;
1950 const unsigned char *a_end;
1951 const DBusRealString *real_a = (const DBusRealString*) a;
1952 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1953 _dbus_assert (c_str != NULL);
1956 bp = (const unsigned char*) c_str;
1957 a_end = real_a->str + real_a->len;
1958 while (ap != a_end && *bp)
1967 if (ap != a_end || *bp)
1974 * Checks whether a string starts with the given C string.
1976 * @param a the string
1977 * @param c_str the C string
1978 * @returns #TRUE if string starts with it
1981 _dbus_string_starts_with_c_str (const DBusString *a,
1984 const unsigned char *ap;
1985 const unsigned char *bp;
1986 const unsigned char *a_end;
1987 const DBusRealString *real_a = (const DBusRealString*) a;
1988 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1989 _dbus_assert (c_str != NULL);
1992 bp = (const unsigned char*) c_str;
1993 a_end = real_a->str + real_a->len;
1994 while (ap != a_end && *bp)
2010 * Returns whether a string ends with the given suffix
2012 * @todo memcmp might make this faster.
2014 * @param a the string
2015 * @param c_str the C-style string
2016 * @returns #TRUE if the string ends with the suffix
2019 _dbus_string_ends_with_c_str (const DBusString *a,
2022 const unsigned char *ap;
2023 const unsigned char *bp;
2024 const unsigned char *a_end;
2025 unsigned long c_str_len;
2026 const DBusRealString *real_a = (const DBusRealString*) a;
2027 DBUS_GENERIC_STRING_PREAMBLE (real_a);
2028 _dbus_assert (c_str != NULL);
2030 c_str_len = strlen (c_str);
2031 if (((unsigned long)real_a->len) < c_str_len)
2034 ap = real_a->str + (real_a->len - c_str_len);
2035 bp = (const unsigned char*) c_str;
2036 a_end = real_a->str + real_a->len;
2046 _dbus_assert (*ap == '\0');
2047 _dbus_assert (*bp == '\0');
2052 static const signed char base64_table[] = {
2119 /** The minimum char that's a valid char in Base64-encoded text */
2120 #define UNBASE64_MIN_CHAR (43)
2121 /** The maximum char that's a valid char in Base64-encoded text */
2122 #define UNBASE64_MAX_CHAR (122)
2123 /** Must subtract this from a char's integer value before offsetting
2124 * into unbase64_table
2126 #define UNBASE64_TABLE_OFFSET UNBASE64_MIN_CHAR
2127 static const signed char unbase64_table[] = {
2211 * Encodes a string using Base64, as documented in RFC 2045.
2213 * @param source the string to encode
2214 * @param start byte index to start encoding
2215 * @param dest string where encoded data should be placed
2216 * @param insert_at where to place encoded data
2217 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2220 _dbus_string_base64_encode (const DBusString *source,
2226 unsigned int dest_len; /* unsigned for overflow checks below */
2227 const unsigned char *s;
2229 const unsigned char *triplet_end;
2230 const unsigned char *final_end;
2231 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
2232 _dbus_assert (source != dest);
2234 /* For each 24 bits (3 bytes) of input, we have 4 bytes of
2237 source_len = real_source->len - start;
2238 dest_len = (source_len / 3) * 4;
2239 if (source_len % 3 != 0)
2242 if (dest_len > (unsigned int) real_dest->max_length)
2245 if (source_len == 0)
2248 if (!open_gap (dest_len, real_dest, insert_at))
2251 d = real_dest->str + insert_at;
2252 s = real_source->str + start;
2253 final_end = real_source->str + (start + source_len);
2254 triplet_end = final_end - (source_len % 3);
2255 _dbus_assert (triplet_end <= final_end);
2256 _dbus_assert ((final_end - triplet_end) < 3);
2258 #define ENCODE_64(v) (base64_table[ (unsigned char) (v) ])
2259 #define SIX_BITS_MASK (0x3f)
2260 _dbus_assert (SIX_BITS_MASK < _DBUS_N_ELEMENTS (base64_table));
2262 while (s != triplet_end)
2264 unsigned int triplet;
2266 triplet = s[2] | (s[1] << 8) | (s[0] << 16);
2268 /* Encode each 6 bits. */
2270 *d++ = ENCODE_64 (triplet >> 18);
2271 *d++ = ENCODE_64 ((triplet >> 12) & SIX_BITS_MASK);
2272 *d++ = ENCODE_64 ((triplet >> 6) & SIX_BITS_MASK);
2273 *d++ = ENCODE_64 (triplet & SIX_BITS_MASK);
2278 switch (final_end - triplet_end)
2282 unsigned int doublet;
2284 doublet = s[1] | (s[0] << 8);
2286 *d++ = ENCODE_64 (doublet >> 12);
2287 *d++ = ENCODE_64 ((doublet >> 6) & SIX_BITS_MASK);
2288 *d++ = ENCODE_64 (doublet & SIX_BITS_MASK);
2294 unsigned int singlet;
2298 *d++ = ENCODE_64 ((singlet >> 6) & SIX_BITS_MASK);
2299 *d++ = ENCODE_64 (singlet & SIX_BITS_MASK);
2308 _dbus_assert (d == (real_dest->str + (insert_at + dest_len)));
2314 * Decodes a string from Base64, as documented in RFC 2045.
2316 * @todo sort out the AUDIT comment in here. The case it mentions
2317 * ("====" or "x===") is not allowed in correct base64, so need to
2318 * decide what to do with that kind of input. Probably ignore it
2319 * since we ignore any other junk seen.
2321 * @param source the string to decode
2322 * @param start byte index to start decode
2323 * @param dest string where decoded data should be placed
2324 * @param insert_at where to place decoded data
2325 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
2328 _dbus_string_base64_decode (const DBusString *source,
2337 unsigned int triplet = 0;
2340 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
2341 _dbus_assert (source != dest);
2343 source_len = real_source->len - start;
2344 s = real_source->str + start;
2345 end = real_source->str + source_len;
2347 if (source_len == 0)
2350 if (!_dbus_string_init (&result))
2357 /* The idea is to just skip anything that isn't
2358 * a base64 char - it's allowed to have whitespace,
2359 * newlines, etc. in here. We also ignore trailing
2360 * base64 chars, though that's suspicious.
2363 if (*s >= UNBASE64_MIN_CHAR &&
2364 *s <= UNBASE64_MAX_CHAR)
2368 /* '=' is padding, doesn't represent additional data
2369 * but does increment our count.
2378 val = unbase64_table[(*s) - UNBASE64_TABLE_OFFSET];
2383 triplet |= (unsigned int) val;
2388 if (sextet_count == 4)
2390 /* no pad = 3 bytes, 1 pad = 2 bytes, 2 pad = 1 byte */
2393 /* AUDIT: Comment doesn't mention 4 pad => 0,
2394 * 3 pad => 1 byte, though the code should
2395 * work fine if those are the required outputs.
2397 * I assume that the spec requires dropping
2398 * the top two bits of, say, ///= which is > 2
2399 * bytes worth of bits. (Or otherwise, you couldn't
2400 * actually represent 2 byte sequences.
2405 if (!_dbus_string_append_byte (&result,
2412 if (!_dbus_string_append_byte (&result,
2413 (triplet >> 8) & 0xff))
2417 if (!_dbus_string_append_byte (&result,
2430 if (!_dbus_string_move (&result, 0, dest, insert_at))
2432 _dbus_string_free (&result);
2436 _dbus_string_free (&result);
2441 _dbus_string_free (&result);
2447 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2448 * encoded. (Each byte is two hex digits.)
2450 * @param source the string to encode
2451 * @param start byte index to start encoding
2452 * @param dest string where encoded data should be placed
2453 * @param insert_at where to place encoded data
2454 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2457 _dbus_string_hex_encode (const DBusString *source,
2463 const char hexdigits[16] = {
2464 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2465 'a', 'b', 'c', 'd', 'e', 'f'
2467 const unsigned char *p;
2468 const unsigned char *end;
2471 _dbus_assert (start <= _dbus_string_get_length (source));
2473 if (!_dbus_string_init (&result))
2478 p = (const unsigned char*) _dbus_string_get_const_data (source);
2479 end = p + _dbus_string_get_length (source);
2484 if (!_dbus_string_append_byte (&result,
2485 hexdigits[(*p >> 4)]))
2488 if (!_dbus_string_append_byte (&result,
2489 hexdigits[(*p & 0x0f)]))
2495 if (!_dbus_string_move (&result, 0, dest, insert_at))
2501 _dbus_string_free (&result);
2506 * Decodes a string from hex encoding.
2508 * @param source the string to decode
2509 * @param start byte index to start decode
2510 * @param dest string where decoded data should be placed
2511 * @param insert_at where to place decoded data
2512 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
2515 _dbus_string_hex_decode (const DBusString *source,
2521 const unsigned char *p;
2522 const unsigned char *end;
2524 dbus_bool_t high_bits;
2526 _dbus_assert (start <= _dbus_string_get_length (source));
2528 if (!_dbus_string_init (&result))
2534 p = (const unsigned char*) _dbus_string_get_const_data (source);
2535 end = p + _dbus_string_get_length (source);
2600 _dbus_verbose ("invalid character '%c' in hex encoded text\n",
2607 if (!_dbus_string_append_byte (&result,
2616 len = _dbus_string_get_length (&result);
2618 b = _dbus_string_get_byte (&result, len - 1);
2622 _dbus_string_set_byte (&result, len - 1, b);
2625 high_bits = !high_bits;
2630 if (!_dbus_string_move (&result, 0, dest, insert_at))
2636 _dbus_string_free (&result);
2641 * Checks that the given range of the string is valid ASCII with no
2642 * nul bytes. If the given range is not entirely contained in the
2643 * string, returns #FALSE.
2645 * @todo this is inconsistent with most of DBusString in that
2646 * it allows a start,len range that isn't in the string.
2648 * @param str the string
2649 * @param start first byte index to check
2650 * @param len number of bytes to check
2651 * @returns #TRUE if the byte range exists and is all valid ASCII
2654 _dbus_string_validate_ascii (const DBusString *str,
2658 const unsigned char *s;
2659 const unsigned char *end;
2660 DBUS_CONST_STRING_PREAMBLE (str);
2661 _dbus_assert (start >= 0);
2662 _dbus_assert (start <= real->len);
2663 _dbus_assert (len >= 0);
2665 if (len > real->len - start)
2668 s = real->str + start;
2673 ((*s & ~0x7f) != 0))
2683 * Checks that the given range of the string is valid UTF-8. If the
2684 * given range is not entirely contained in the string, returns
2685 * #FALSE. If the string contains any nul bytes in the given range,
2686 * returns #FALSE. If the start and start+len are not on character
2687 * boundaries, returns #FALSE.
2689 * @todo this is inconsistent with most of DBusString in that
2690 * it allows a start,len range that isn't in the string.
2692 * @param str the string
2693 * @param start first byte index to check
2694 * @param len number of bytes to check
2695 * @returns #TRUE if the byte range exists and is all valid UTF-8
2698 _dbus_string_validate_utf8 (const DBusString *str,
2702 const unsigned char *p;
2703 const unsigned char *end;
2704 DBUS_CONST_STRING_PREAMBLE (str);
2705 _dbus_assert (start >= 0);
2706 _dbus_assert (start <= real->len);
2707 _dbus_assert (len >= 0);
2709 if (len > real->len - start)
2712 p = real->str + start;
2717 int i, mask = 0, char_len;
2718 dbus_unichar_t result;
2719 unsigned char c = (unsigned char) *p;
2721 UTF8_COMPUTE (c, mask, char_len);
2726 /* check that the expected number of bytes exists in the remaining length */
2727 if ((end - p) < char_len)
2730 UTF8_GET (result, p, i, mask, char_len);
2732 if (UTF8_LENGTH (result) != char_len) /* Check for overlong UTF-8 */
2735 if (result == (dbus_unichar_t)-1)
2738 if (!UNICODE_VALID (result))
2744 /* See that we covered the entire length if a length was
2754 * Checks that the given range of the string is all nul bytes. If the
2755 * given range is not entirely contained in the string, returns
2758 * @todo this is inconsistent with most of DBusString in that
2759 * it allows a start,len range that isn't in the string.
2761 * @param str the string
2762 * @param start first byte index to check
2763 * @param len number of bytes to check
2764 * @returns #TRUE if the byte range exists and is all nul bytes
2767 _dbus_string_validate_nul (const DBusString *str,
2771 const unsigned char *s;
2772 const unsigned char *end;
2773 DBUS_CONST_STRING_PREAMBLE (str);
2774 _dbus_assert (start >= 0);
2775 _dbus_assert (len >= 0);
2776 _dbus_assert (start <= real->len);
2778 if (len > real->len - start)
2781 s = real->str + start;
2794 * Checks that the given range of the string is a valid message name
2795 * in the D-BUS protocol. This includes a length restriction, etc.,
2796 * see the specification. It does not validate UTF-8, that has to be
2797 * done separately for now.
2799 * @todo this is inconsistent with most of DBusString in that
2800 * it allows a start,len range that isn't in the string.
2802 * @param str the string
2803 * @param start first byte index to check
2804 * @param len number of bytes to check
2805 * @returns #TRUE if the byte range exists and is a valid name
2808 _dbus_string_validate_name (const DBusString *str,
2812 const unsigned char *s;
2813 const unsigned char *end;
2814 dbus_bool_t saw_dot;
2816 DBUS_CONST_STRING_PREAMBLE (str);
2817 _dbus_assert (start >= 0);
2818 _dbus_assert (len >= 0);
2819 _dbus_assert (start <= real->len);
2821 if (len > real->len - start)
2824 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2831 s = real->str + start;
2852 * Checks that the given range of the string is a valid service name
2853 * in the D-BUS protocol. This includes a length restriction, etc.,
2854 * see the specification. It does not validate UTF-8, that has to be
2855 * done separately for now.
2857 * @todo this is inconsistent with most of DBusString in that
2858 * it allows a start,len range that isn't in the string.
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_service (const DBusString *str,
2870 const unsigned char *s;
2871 const unsigned char *end;
2872 dbus_bool_t saw_dot;
2873 dbus_bool_t is_base_service;
2875 DBUS_CONST_STRING_PREAMBLE (str);
2876 _dbus_assert (start >= 0);
2877 _dbus_assert (len >= 0);
2878 _dbus_assert (start <= real->len);
2880 if (len > real->len - start)
2883 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2889 is_base_service = _dbus_string_get_byte (str, start) == ':';
2890 if (is_base_service)
2891 return TRUE; /* can have any content */
2893 /* non-base-service must have the '.' indicating a namespace */
2896 s = real->str + start;
2913 * Clears all allocated bytes in the string to zero.
2915 * @param str the string
2918 _dbus_string_zero (DBusString *str)
2920 DBUS_STRING_PREAMBLE (str);
2922 memset (real->str - real->align_offset, '\0', real->allocated);
2926 #ifdef DBUS_BUILD_TESTS
2927 #include "dbus-test.h"
2931 test_max_len (DBusString *str,
2936 if (!_dbus_string_set_length (str, max_len - 1))
2937 _dbus_assert_not_reached ("setting len to one less than max should have worked");
2940 if (!_dbus_string_set_length (str, max_len))
2941 _dbus_assert_not_reached ("setting len to max len should have worked");
2943 if (_dbus_string_set_length (str, max_len + 1))
2944 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
2946 if (!_dbus_string_set_length (str, 0))
2947 _dbus_assert_not_reached ("setting len to zero should have worked");
2951 test_base64_roundtrip (const unsigned char *data,
2959 len = strlen (data);
2961 if (!_dbus_string_init (&orig))
2962 _dbus_assert_not_reached ("could not init string");
2964 if (!_dbus_string_init (&encoded))
2965 _dbus_assert_not_reached ("could not init string");
2967 if (!_dbus_string_init (&decoded))
2968 _dbus_assert_not_reached ("could not init string");
2970 if (!_dbus_string_append_len (&orig, data, len))
2971 _dbus_assert_not_reached ("couldn't append orig data");
2973 if (!_dbus_string_base64_encode (&orig, 0, &encoded, 0))
2974 _dbus_assert_not_reached ("could not encode");
2976 if (!_dbus_string_base64_decode (&encoded, 0, &decoded, 0))
2977 _dbus_assert_not_reached ("could not decode");
2979 if (!_dbus_string_equal (&orig, &decoded))
2983 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
2984 _dbus_string_get_length (&orig),
2985 _dbus_string_get_length (&encoded),
2986 _dbus_string_get_length (&decoded));
2987 printf ("Original: %s\n", data);
2988 s = _dbus_string_get_const_data (&decoded);
2989 printf ("Decoded: %s\n", s);
2990 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
2993 _dbus_string_free (&orig);
2994 _dbus_string_free (&encoded);
2995 _dbus_string_free (&decoded);
2999 test_hex_roundtrip (const unsigned char *data,
3007 len = strlen (data);
3009 if (!_dbus_string_init (&orig))
3010 _dbus_assert_not_reached ("could not init string");
3012 if (!_dbus_string_init (&encoded))
3013 _dbus_assert_not_reached ("could not init string");
3015 if (!_dbus_string_init (&decoded))
3016 _dbus_assert_not_reached ("could not init string");
3018 if (!_dbus_string_append_len (&orig, data, len))
3019 _dbus_assert_not_reached ("couldn't append orig data");
3021 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
3022 _dbus_assert_not_reached ("could not encode");
3024 if (!_dbus_string_hex_decode (&encoded, 0, &decoded, 0))
3025 _dbus_assert_not_reached ("could not decode");
3027 if (!_dbus_string_equal (&orig, &decoded))
3031 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
3032 _dbus_string_get_length (&orig),
3033 _dbus_string_get_length (&encoded),
3034 _dbus_string_get_length (&decoded));
3035 printf ("Original: %s\n", data);
3036 s = _dbus_string_get_const_data (&decoded);
3037 printf ("Decoded: %s\n", s);
3038 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
3041 _dbus_string_free (&orig);
3042 _dbus_string_free (&encoded);
3043 _dbus_string_free (&decoded);
3046 typedef void (* TestRoundtripFunc) (const unsigned char *data,
3049 test_roundtrips (TestRoundtripFunc func)
3051 (* func) ("Hello this is a string\n", -1);
3052 (* func) ("Hello this is a string\n1", -1);
3053 (* func) ("Hello this is a string\n12", -1);
3054 (* func) ("Hello this is a string\n123", -1);
3055 (* func) ("Hello this is a string\n1234", -1);
3056 (* func) ("Hello this is a string\n12345", -1);
3060 (* func) ("123", 3);
3061 (* func) ("1234", 4);
3062 (* func) ("12345", 5);
3066 (* func) ("123", 4);
3067 (* func) ("1234", 5);
3068 (* func) ("12345", 6);
3070 unsigned char buf[512];
3074 while (i < _DBUS_N_ELEMENTS (buf))
3080 while (i < _DBUS_N_ELEMENTS (buf))
3090 * @ingroup DBusStringInternals
3091 * Unit test for DBusString.
3093 * @todo Need to write tests for _dbus_string_copy() and
3094 * _dbus_string_move() moving to/from each of start/middle/end of a
3095 * string. Also need tests for _dbus_string_move_len ()
3097 * @returns #TRUE on success.
3100 _dbus_string_test (void)
3107 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 };
3112 while (i < _DBUS_N_ELEMENTS (lens))
3114 if (!_dbus_string_init (&str))
3115 _dbus_assert_not_reached ("failed to init string");
3117 set_max_length (&str, lens[i]);
3119 test_max_len (&str, lens[i]);
3120 _dbus_string_free (&str);
3125 /* Test shortening and setting length */
3127 while (i < _DBUS_N_ELEMENTS (lens))
3131 if (!_dbus_string_init (&str))
3132 _dbus_assert_not_reached ("failed to init string");
3134 set_max_length (&str, lens[i]);
3136 if (!_dbus_string_set_length (&str, lens[i]))
3137 _dbus_assert_not_reached ("failed to set string length");
3142 _dbus_assert (_dbus_string_get_length (&str) == j);
3145 _dbus_string_shorten (&str, 1);
3146 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
3151 _dbus_string_free (&str);
3156 /* Test appending data */
3157 if (!_dbus_string_init (&str))
3158 _dbus_assert_not_reached ("failed to init string");
3163 if (!_dbus_string_append (&str, "a"))
3164 _dbus_assert_not_reached ("failed to append string to string\n");
3166 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
3168 if (!_dbus_string_append_byte (&str, 'b'))
3169 _dbus_assert_not_reached ("failed to append byte to string\n");
3171 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
3176 _dbus_string_free (&str);
3178 /* Check steal_data */
3180 if (!_dbus_string_init (&str))
3181 _dbus_assert_not_reached ("failed to init string");
3183 if (!_dbus_string_append (&str, "Hello World"))
3184 _dbus_assert_not_reached ("could not append to string");
3186 i = _dbus_string_get_length (&str);
3188 if (!_dbus_string_steal_data (&str, &s))
3189 _dbus_assert_not_reached ("failed to steal data");
3191 _dbus_assert (_dbus_string_get_length (&str) == 0);
3192 _dbus_assert (((int)strlen (s)) == i);
3198 if (!_dbus_string_append (&str, "Hello World"))
3199 _dbus_assert_not_reached ("could not append to string");
3201 i = _dbus_string_get_length (&str);
3203 if (!_dbus_string_init (&other))
3204 _dbus_assert_not_reached ("could not init string");
3206 if (!_dbus_string_move (&str, 0, &other, 0))
3207 _dbus_assert_not_reached ("could not move");
3209 _dbus_assert (_dbus_string_get_length (&str) == 0);
3210 _dbus_assert (_dbus_string_get_length (&other) == i);
3212 if (!_dbus_string_append (&str, "Hello World"))
3213 _dbus_assert_not_reached ("could not append to string");
3215 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
3216 _dbus_assert_not_reached ("could not move");
3218 _dbus_assert (_dbus_string_get_length (&str) == 0);
3219 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3221 if (!_dbus_string_append (&str, "Hello World"))
3222 _dbus_assert_not_reached ("could not append to string");
3224 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3225 _dbus_assert_not_reached ("could not move");
3227 _dbus_assert (_dbus_string_get_length (&str) == 0);
3228 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3230 _dbus_string_free (&other);
3234 if (!_dbus_string_append (&str, "Hello World"))
3235 _dbus_assert_not_reached ("could not append to string");
3237 i = _dbus_string_get_length (&str);
3239 if (!_dbus_string_init (&other))
3240 _dbus_assert_not_reached ("could not init string");
3242 if (!_dbus_string_copy (&str, 0, &other, 0))
3243 _dbus_assert_not_reached ("could not copy");
3245 _dbus_assert (_dbus_string_get_length (&str) == i);
3246 _dbus_assert (_dbus_string_get_length (&other) == i);
3248 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
3249 _dbus_assert_not_reached ("could not copy");
3251 _dbus_assert (_dbus_string_get_length (&str) == i);
3252 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3253 _dbus_assert (_dbus_string_equal_c_str (&other,
3254 "Hello WorldHello World"));
3256 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3257 _dbus_assert_not_reached ("could not copy");
3259 _dbus_assert (_dbus_string_get_length (&str) == i);
3260 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3261 _dbus_assert (_dbus_string_equal_c_str (&other,
3262 "Hello WorldHello WorldHello World"));
3264 _dbus_string_free (&str);
3265 _dbus_string_free (&other);
3269 if (!_dbus_string_init (&str))
3270 _dbus_assert_not_reached ("failed to init string");
3272 if (!_dbus_string_append (&str, "Hello World"))
3273 _dbus_assert_not_reached ("could not append to string");
3275 i = _dbus_string_get_length (&str);
3277 if (!_dbus_string_init (&other))
3278 _dbus_assert_not_reached ("could not init string");
3280 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3281 &other, 0, _dbus_string_get_length (&other)))
3282 _dbus_assert_not_reached ("could not replace");
3284 _dbus_assert (_dbus_string_get_length (&str) == i);
3285 _dbus_assert (_dbus_string_get_length (&other) == i);
3286 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
3288 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3290 _dbus_assert_not_reached ("could not replace center space");
3292 _dbus_assert (_dbus_string_get_length (&str) == i);
3293 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3294 _dbus_assert (_dbus_string_equal_c_str (&other,
3295 "HelloHello WorldWorld"));
3298 if (!_dbus_string_replace_len (&str, 1, 1,
3300 _dbus_string_get_length (&other) - 1,
3302 _dbus_assert_not_reached ("could not replace end character");
3304 _dbus_assert (_dbus_string_get_length (&str) == i);
3305 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3306 _dbus_assert (_dbus_string_equal_c_str (&other,
3307 "HelloHello WorldWorle"));
3309 _dbus_string_free (&str);
3310 _dbus_string_free (&other);
3312 /* Check append/get unichar */
3314 if (!_dbus_string_init (&str))
3315 _dbus_assert_not_reached ("failed to init string");
3318 if (!_dbus_string_append_unichar (&str, 0xfffc))
3319 _dbus_assert_not_reached ("failed to append unichar");
3321 _dbus_string_get_unichar (&str, 0, &ch, &i);
3323 _dbus_assert (ch == 0xfffc);
3324 _dbus_assert (i == _dbus_string_get_length (&str));
3326 _dbus_string_free (&str);
3328 /* Check insert/set/get byte */
3330 if (!_dbus_string_init (&str))
3331 _dbus_assert_not_reached ("failed to init string");
3333 if (!_dbus_string_append (&str, "Hello"))
3334 _dbus_assert_not_reached ("failed to append Hello");
3336 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
3337 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
3338 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
3339 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
3340 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
3342 _dbus_string_set_byte (&str, 1, 'q');
3343 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
3345 if (!_dbus_string_insert_byte (&str, 0, 255))
3346 _dbus_assert_not_reached ("can't insert byte");
3348 if (!_dbus_string_insert_byte (&str, 2, 'Z'))
3349 _dbus_assert_not_reached ("can't insert byte");
3351 if (!_dbus_string_insert_byte (&str, _dbus_string_get_length (&str), 'W'))
3352 _dbus_assert_not_reached ("can't insert byte");
3354 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
3355 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
3356 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
3357 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'q');
3358 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'l');
3359 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'l');
3360 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'o');
3361 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'W');
3363 _dbus_string_free (&str);
3365 /* Check append/parse int/double */
3367 if (!_dbus_string_init (&str))
3368 _dbus_assert_not_reached ("failed to init string");
3370 if (!_dbus_string_append_int (&str, 27))
3371 _dbus_assert_not_reached ("failed to append int");
3373 i = _dbus_string_get_length (&str);
3375 if (!_dbus_string_parse_int (&str, 0, &v, &end))
3376 _dbus_assert_not_reached ("failed to parse int");
3378 _dbus_assert (v == 27);
3379 _dbus_assert (end == i);
3381 _dbus_string_free (&str);
3383 if (!_dbus_string_init (&str))
3384 _dbus_assert_not_reached ("failed to init string");
3386 if (!_dbus_string_append_double (&str, 50.3))
3387 _dbus_assert_not_reached ("failed to append float");
3389 i = _dbus_string_get_length (&str);
3391 if (!_dbus_string_parse_double (&str, 0, &d, &end))
3392 _dbus_assert_not_reached ("failed to parse float");
3394 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
3395 _dbus_assert (end == i);
3397 _dbus_string_free (&str);
3400 if (!_dbus_string_init (&str))
3401 _dbus_assert_not_reached ("failed to init string");
3403 if (!_dbus_string_append (&str, "Hello"))
3404 _dbus_assert_not_reached ("couldn't append to string");
3406 if (!_dbus_string_find (&str, 0, "He", &i))
3407 _dbus_assert_not_reached ("didn't find 'He'");
3408 _dbus_assert (i == 0);
3410 if (!_dbus_string_find (&str, 0, "Hello", &i))
3411 _dbus_assert_not_reached ("didn't find 'Hello'");
3412 _dbus_assert (i == 0);
3414 if (!_dbus_string_find (&str, 0, "ello", &i))
3415 _dbus_assert_not_reached ("didn't find 'ello'");
3416 _dbus_assert (i == 1);
3418 if (!_dbus_string_find (&str, 0, "lo", &i))
3419 _dbus_assert_not_reached ("didn't find 'lo'");
3420 _dbus_assert (i == 3);
3422 if (!_dbus_string_find (&str, 2, "lo", &i))
3423 _dbus_assert_not_reached ("didn't find 'lo'");
3424 _dbus_assert (i == 3);
3426 if (_dbus_string_find (&str, 4, "lo", &i))
3427 _dbus_assert_not_reached ("did find 'lo'");
3429 if (!_dbus_string_find (&str, 0, "l", &i))
3430 _dbus_assert_not_reached ("didn't find 'l'");
3431 _dbus_assert (i == 2);
3433 if (!_dbus_string_find (&str, 0, "H", &i))
3434 _dbus_assert_not_reached ("didn't find 'H'");
3435 _dbus_assert (i == 0);
3437 if (!_dbus_string_find (&str, 0, "", &i))
3438 _dbus_assert_not_reached ("didn't find ''");
3439 _dbus_assert (i == 0);
3441 if (_dbus_string_find (&str, 0, "Hello!", NULL))
3442 _dbus_assert_not_reached ("Did find 'Hello!'");
3444 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
3445 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
3447 if (_dbus_string_find (&str, 0, "ill", NULL))
3448 _dbus_assert_not_reached ("Did find 'ill'");
3450 if (_dbus_string_find (&str, 0, "q", NULL))
3451 _dbus_assert_not_reached ("Did find 'q'");
3453 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
3454 _dbus_assert_not_reached ("Didn't find 'He'");
3456 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
3457 _dbus_assert_not_reached ("Did find 'Hello'");
3459 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
3460 _dbus_assert_not_reached ("Did not find 'H'");
3461 _dbus_assert (i == 0);
3463 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
3464 _dbus_assert_not_reached ("Did not find 'o'");
3465 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
3467 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
3468 _dbus_assert_not_reached ("Did find 'o'");
3469 _dbus_assert (i == -1);
3471 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
3472 _dbus_assert_not_reached ("Did find 'e'");
3473 _dbus_assert (i == -1);
3475 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
3476 _dbus_assert_not_reached ("Didn't find 'e'");
3477 _dbus_assert (i == 1);
3479 _dbus_string_free (&str);
3481 /* Base 64 and Hex encoding */
3482 test_roundtrips (test_base64_roundtrip);
3483 test_roundtrips (test_hex_roundtrip);
3488 #endif /* DBUS_BUILD_TESTS */