1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-string.c String utility class (internal to D-BUS implementation)
4 * Copyright (C) 2002 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-string.h"
26 /* we allow a system header here, for speed/convenience */
30 * @defgroup DBusString string class
31 * @ingroup DBusInternals
32 * @brief DBusString data structure
34 * Types and functions related to DBusString. DBusString is intended
35 * to be a string class that makes it hard to mess up security issues
36 * (and just in general harder to write buggy code). It should be
37 * used (or extended and then used) rather than the libc stuff in
38 * string.h. The string class is a bit inconvenient at spots because
39 * it handles out-of-memory failures and tries to be extra-robust.
41 * A DBusString has a maximum length set at initialization time; this
42 * can be used to ensure that a buffer doesn't get too big. The
43 * _dbus_string_lengthen() method checks for overflow, and for max
44 * length being exceeded.
46 * Try to avoid conversion to a plain C string, i.e. add methods on
47 * the string object instead, only convert to C string when passing
48 * things out to the public API. In particular, no sprintf, strcpy,
49 * strcat, any of that should be used. The GString feature of
50 * accepting negative numbers for "length of string" is also absent,
51 * because it could keep us from detecting bogus huge lengths. i.e. if
52 * we passed in some bogus huge length it would be taken to mean
53 * "current length of string" instead of "broken crack"
57 * @defgroup DBusStringInternals DBusString implementation details
58 * @ingroup DBusInternals
59 * @brief DBusString implementation details
61 * The guts of DBusString.
67 * @brief Internals of DBusString.
69 * DBusString internals. DBusString is an opaque objects, it must be
70 * used via accessor functions.
74 unsigned char *str; /**< String data, plus nul termination */
75 int len; /**< Length without nul */
76 int allocated; /**< Allocated size of data */
77 int max_length; /**< Max length of this string. */
78 unsigned int constant : 1; /**< String data is not owned by DBusString */
79 unsigned int locked : 1; /**< DBusString has been locked and can't be changed */
80 unsigned int invalid : 1; /**< DBusString is invalid (e.g. already freed) */
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); _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
127 /** Assert that the string's memory is 8-byte aligned.
129 * @todo Currently we just hope libc returns 8-byte aligned memory
130 * (which is true for GNU libc), but really we need to ensure it by
131 * allocating 8 extra bytes and keeping an "align_offset : 3" field
132 * in DBusString, or something along those lines.
134 #define ASSERT_8_BYTE_ALIGNED(s) \
135 _dbus_assert (_DBUS_ALIGN_ADDRESS (((const DBusRealString*)s)->str, 8) == ((const DBusRealString*)s)->str)
138 * Initializes a string. The maximum length may be _DBUS_INT_MAX for
139 * no maximum. The string starts life with zero length.
140 * The string must eventually be freed with _dbus_string_free().
142 * @todo the max length feature is useless, because it looks to the
143 * app like out of memory, and the app might try to "recover" - but
144 * recovery in this case is impossible, as we can't ever "get more
145 * memory" - so should delete the max length feature I think. Well, at
146 * least there's a strong caveat that it can only be used when
147 * out-of-memory is a permanent fatal error.
149 * @todo we could make this init routine not alloc any memory and
150 * return void, would simplify a lot of code, however it might
151 * complexify things elsewhere because _dbus_string_get_data()
152 * etc. could suddenly fail as they'd need to alloc new memory.
154 * @param str memory to hold the string
155 * @param max_length the maximum size of the string
156 * @returns #TRUE on success */
158 _dbus_string_init (DBusString *str,
161 DBusRealString *real;
163 _dbus_assert (str != NULL);
164 _dbus_assert (max_length >= 0);
166 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
168 real = (DBusRealString*) str;
170 /* It's very important not to touch anything
171 * other than real->str if we're going to fail,
172 * since we also use this function to reset
173 * an existing string, e.g. in _dbus_string_steal_data()
176 #define INITIAL_ALLOC 2
178 real->str = dbus_malloc (INITIAL_ALLOC);
179 if (real->str == NULL)
182 real->allocated = INITIAL_ALLOC;
184 real->str[real->len] = '\0';
186 real->max_length = max_length;
187 real->constant = FALSE;
188 real->locked = FALSE;
189 real->invalid = FALSE;
191 ASSERT_8_BYTE_ALIGNED (str);
197 * Initializes a constant string. The value parameter is not copied
198 * (should be static), and the string may never be modified.
199 * It is safe but not necessary to call _dbus_string_free()
202 * @param str memory to use for the string
203 * @param value a string to be stored in str (not copied!!!)
206 _dbus_string_init_const (DBusString *str,
209 _dbus_string_init_const_len (str, value,
214 * Initializes a constant string with a length. The value parameter is
215 * not copied (should be static), and the string may never be
216 * modified. It is safe but not necessary to call _dbus_string_free()
219 * @param str memory to use for the string
220 * @param value a string to be stored in str (not copied!!!)
221 * @param len the length to use
224 _dbus_string_init_const_len (DBusString *str,
228 DBusRealString *real;
230 _dbus_assert (str != NULL);
231 _dbus_assert (value != NULL);
233 real = (DBusRealString*) str;
235 real->str = (char*) value;
237 real->allocated = real->len;
238 real->max_length = real->len;
239 real->constant = TRUE;
240 real->invalid = FALSE;
242 /* We don't require const strings to be 8-byte aligned as the
243 * memory is coming from elsewhere.
248 * Frees a string created by _dbus_string_init().
250 * @param str memory where the string is stored.
253 _dbus_string_free (DBusString *str)
255 DBusRealString *real = (DBusRealString*) str;
256 DBUS_GENERIC_STRING_PREAMBLE (real);
260 dbus_free (real->str);
262 real->invalid = TRUE;
266 * Locks a string such that any attempts to change the string
267 * will result in aborting the program. Also, if the string
268 * is wasting a lot of memory (allocation is larger than what
269 * the string is really using), _dbus_string_lock() will realloc
270 * the string's data to "compact" it.
272 * @param str the string to lock.
275 _dbus_string_lock (DBusString *str)
277 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
281 /* Try to realloc to avoid excess memory usage, since
282 * we know we won't change the string further
285 if (real->allocated > (real->len + MAX_WASTE))
290 new_allocated = real->len + 1;
292 new_str = dbus_realloc (real->str, new_allocated);
296 real->allocated = new_allocated;
297 ASSERT_8_BYTE_ALIGNED (str);
303 set_length (DBusRealString *real,
306 /* Note, we are setting the length without nul termination */
308 /* exceeding max length is the same as failure to allocate memory */
309 if (new_length > real->max_length)
312 while (new_length >= real->allocated)
317 new_allocated = 2 + real->allocated * 2;
318 if (new_allocated < real->allocated)
319 return FALSE; /* overflow */
321 new_str = dbus_realloc (real->str, new_allocated);
326 real->allocated = new_allocated;
328 ASSERT_8_BYTE_ALIGNED (real);
331 real->len = new_length;
332 real->str[real->len] = '\0';
339 DBusRealString *dest,
345 if (!set_length (dest, dest->len + len))
348 memmove (dest->str + insert_at + len,
349 dest->str + insert_at,
350 dest->len - len - insert_at);
356 * Gets the raw character buffer from the string. The returned buffer
357 * will be nul-terminated, but note that strings may contain binary
358 * data so there may be extra nul characters prior to the termination.
359 * This function should be little-used, extend DBusString or add
360 * stuff to dbus-sysdeps.c instead. It's an error to use this
361 * function on a const string.
363 * @param str the string
364 * @param data_return place to store the returned data
367 _dbus_string_get_data (DBusString *str,
370 DBUS_STRING_PREAMBLE (str);
371 _dbus_assert (data_return != NULL);
373 *data_return = real->str;
377 * Gets the raw character buffer from a const string.
379 * @todo should return the const char* instead of using an out param;
380 * the temporary variable encourages a bug where you use const data
381 * after modifying the string and possibly causing a realloc.
383 * @param str the string
384 * @param data_return location to store returned data
387 _dbus_string_get_const_data (const DBusString *str,
388 const char **data_return)
390 DBUS_CONST_STRING_PREAMBLE (str);
391 _dbus_assert (data_return != NULL);
393 *data_return = real->str;
397 * Gets a sub-portion of the raw character buffer from the
398 * string. The "len" field is required simply for error
399 * checking, to be sure you don't try to use more
400 * string than exists. The nul termination of the
401 * returned buffer remains at the end of the entire
402 * string, not at start + len.
404 * @param str the string
405 * @param data_return location to return the buffer
406 * @param start byte offset to return
407 * @param len length of segment to return
410 _dbus_string_get_data_len (DBusString *str,
415 DBUS_STRING_PREAMBLE (str);
416 _dbus_assert (data_return != NULL);
417 _dbus_assert (start >= 0);
418 _dbus_assert (len >= 0);
419 _dbus_assert ((start + len) <= real->len);
421 *data_return = real->str + start;
425 * const version of _dbus_string_get_data_len().
427 * @todo should return the const char* instead of using an out param;
428 * the temporary variable encourages a bug where you use const data
429 * after modifying the string and possibly causing a realloc.
431 * @param str the string
432 * @param data_return location to return the buffer
433 * @param start byte offset to return
434 * @param len length of segment to return
437 _dbus_string_get_const_data_len (const DBusString *str,
438 const char **data_return,
442 DBUS_CONST_STRING_PREAMBLE (str);
443 _dbus_assert (data_return != NULL);
444 _dbus_assert (start >= 0);
445 _dbus_assert (len >= 0);
446 _dbus_assert ((start + len) <= real->len);
448 *data_return = real->str + start;
452 * Sets the value of the byte at the given position.
454 * @param str the string
455 * @param i the position
456 * @param byte the new value
459 _dbus_string_set_byte (DBusString *str,
463 DBUS_STRING_PREAMBLE (str);
464 _dbus_assert (i < real->len);
470 * Gets the byte at the given position.
472 * @param str the string
473 * @param start the position
474 * @returns the byte at that position
477 _dbus_string_get_byte (const DBusString *str,
480 DBUS_CONST_STRING_PREAMBLE (str);
481 _dbus_assert (start < real->len);
483 return real->str[start];
487 * Inserts the given byte at the given position.
489 * @param str the string
490 * @param i the position
491 * @param byte the value to insert
492 * @returns #TRUE on success
495 _dbus_string_insert_byte (DBusString *str,
499 DBUS_STRING_PREAMBLE (str);
500 _dbus_assert (i <= real->len);
502 if (!open_gap (1, real, i))
511 * Like _dbus_string_get_data(), but removes the
512 * gotten data from the original string. The caller
513 * must free the data returned. This function may
514 * fail due to lack of memory, and return #FALSE.
516 * @param str the string
517 * @param data_return location to return the buffer
518 * @returns #TRUE on success
521 _dbus_string_steal_data (DBusString *str,
524 DBUS_STRING_PREAMBLE (str);
525 _dbus_assert (data_return != NULL);
527 *data_return = real->str;
529 /* reset the string */
530 if (!_dbus_string_init (str, real->max_length))
532 /* hrm, put it back then */
533 real->str = *data_return;
542 * Like _dbus_string_get_data_len(), but removes the gotten data from
543 * the original string. The caller must free the data returned. This
544 * function may fail due to lack of memory, and return #FALSE.
545 * The returned string is nul-terminated and has length len.
547 * @param str the string
548 * @param data_return location to return the buffer
549 * @param start the start of segment to steal
550 * @param len the length of segment to steal
551 * @returns #TRUE on success
554 _dbus_string_steal_data_len (DBusString *str,
561 DBUS_STRING_PREAMBLE (str);
562 _dbus_assert (data_return != NULL);
563 _dbus_assert (start >= 0);
564 _dbus_assert (len >= 0);
565 _dbus_assert ((start + len) <= real->len);
567 if (!_dbus_string_init (&dest, real->max_length))
570 if (!_dbus_string_move_len (str, start, len, &dest, 0))
572 _dbus_string_free (&dest);
576 if (!_dbus_string_steal_data (&dest, data_return))
578 _dbus_string_free (&dest);
582 _dbus_string_free (&dest);
587 * Gets the length of a string (not including nul termination).
589 * @returns the length.
592 _dbus_string_get_length (const DBusString *str)
594 DBUS_CONST_STRING_PREAMBLE (str);
600 * Makes a string longer by the given number of bytes. Checks whether
601 * adding additional_length to the current length would overflow an
602 * integer, and checks for exceeding a string's max length.
603 * The new bytes are not initialized, other than nul-terminating
604 * the end of the string. The uninitialized bytes may contain
605 * unexpected nul bytes or other junk.
607 * @param str a string
608 * @param additional_length length to add to the string.
609 * @returns #TRUE on success.
612 _dbus_string_lengthen (DBusString *str,
613 int additional_length)
615 DBUS_STRING_PREAMBLE (str);
616 _dbus_assert (additional_length >= 0);
618 if ((real->len + additional_length) < real->len)
619 return FALSE; /* overflow */
621 return set_length (real,
622 real->len + additional_length);
626 * Makes a string shorter by the given number of bytes.
628 * @param str a string
629 * @param length_to_remove length to remove from the string.
632 _dbus_string_shorten (DBusString *str,
633 int length_to_remove)
635 DBUS_STRING_PREAMBLE (str);
636 _dbus_assert (length_to_remove >= 0);
637 _dbus_assert (length_to_remove <= real->len);
640 real->len - length_to_remove);
644 * Sets the length of a string. Can be used to truncate or lengthen
645 * the string. If the string is lengthened, the function may fail and
646 * return #FALSE. Newly-added bytes are not initialized, as with
647 * _dbus_string_lengthen().
649 * @param str a string
650 * @param length new length of the string.
651 * @returns #FALSE on failure.
654 _dbus_string_set_length (DBusString *str,
657 DBUS_STRING_PREAMBLE (str);
658 _dbus_assert (length >= 0);
660 return set_length (real, length);
664 * Align the length of a string to a specific alignment (typically 4 or 8)
665 * by appending nul bytes to the string.
667 * @param str a string
668 * @param alignment the alignment
669 * @returns #FALSE if no memory
672 _dbus_string_align_length (DBusString *str,
677 DBUS_STRING_PREAMBLE (str);
678 _dbus_assert (alignment >= 1);
679 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
681 new_len = _DBUS_ALIGN_VALUE (real->len, alignment);
683 delta = new_len - real->len;
684 _dbus_assert (delta >= 0);
689 if (!set_length (real, new_len))
692 memset (real->str + (new_len - delta),
699 append (DBusRealString *real,
706 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
709 memcpy (real->str + (real->len - buffer_len),
717 * Appends a nul-terminated C-style string to a DBusString.
719 * @param str the DBusString
720 * @param buffer the nul-terminated characters to append
721 * @returns #FALSE if not enough memory.
724 _dbus_string_append (DBusString *str,
729 DBUS_STRING_PREAMBLE (str);
730 _dbus_assert (buffer != NULL);
732 buffer_len = strlen (buffer);
734 return append (real, buffer, buffer_len);
738 * Appends block of bytes with the given length to a DBusString.
740 * @param str the DBusString
741 * @param buffer the bytes to append
742 * @param len the number of bytes to append
743 * @returns #FALSE if not enough memory.
746 _dbus_string_append_len (DBusString *str,
750 DBUS_STRING_PREAMBLE (str);
751 _dbus_assert (buffer != NULL);
752 _dbus_assert (len >= 0);
754 return append (real, buffer, len);
758 * Appends a single byte to the string, returning #FALSE
759 * if not enough memory.
761 * @param str the string
762 * @param byte the byte to append
763 * @returns #TRUE on success
766 _dbus_string_append_byte (DBusString *str,
769 DBUS_STRING_PREAMBLE (str);
771 if (!set_length (real, real->len + 1))
774 real->str[real->len-1] = byte;
780 * Appends a single Unicode character, encoding the character
783 * @param str the string
784 * @param ch the Unicode character
787 _dbus_string_append_unichar (DBusString *str,
795 DBUS_STRING_PREAMBLE (str);
797 /* this code is from GLib but is pretty standard I think */
811 else if (ch < 0x10000)
816 else if (ch < 0x200000)
821 else if (ch < 0x4000000)
832 if (!set_length (real, real->len + len))
835 out = real->str + (real->len - len);
837 for (i = len - 1; i > 0; --i)
839 out[i] = (ch & 0x3f) | 0x80;
848 delete (DBusRealString *real,
855 memmove (real->str + start, real->str + start + len, real->len - (start + len));
857 real->str[real->len] = '\0';
861 * Deletes a segment of a DBusString with length len starting at
862 * start. (Hint: to clear an entire string, setting length to 0
863 * with _dbus_string_set_length() is easier.)
865 * @param str the DBusString
866 * @param start where to start deleting
867 * @param len the number of bytes to delete
870 _dbus_string_delete (DBusString *str,
874 DBUS_STRING_PREAMBLE (str);
875 _dbus_assert (start >= 0);
876 _dbus_assert (len >= 0);
877 _dbus_assert ((start + len) <= real->len);
879 delete (real, start, len);
883 copy (DBusRealString *source,
886 DBusRealString *dest,
892 if (!open_gap (len, dest, insert_at))
895 memcpy (dest->str + insert_at,
903 * Checks assertions for two strings we're copying a segment between,
904 * and declares real_source/real_dest variables.
906 * @param source the source string
907 * @param start the starting offset
908 * @param dest the dest string
909 * @param insert_at where the copied segment is inserted
911 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
912 DBusRealString *real_source = (DBusRealString*) source; \
913 DBusRealString *real_dest = (DBusRealString*) dest; \
914 _dbus_assert ((source) != (dest)); \
915 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
916 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
917 _dbus_assert (!real_dest->constant); \
918 _dbus_assert (!real_dest->locked); \
919 _dbus_assert ((start) >= 0); \
920 _dbus_assert ((start) <= real_source->len); \
921 _dbus_assert ((insert_at) >= 0); \
922 _dbus_assert ((insert_at) <= real_dest->len)
925 * Moves the end of one string into another string. Both strings
926 * must be initialized, valid strings.
928 * @param source the source string
929 * @param start where to chop off the source string
930 * @param dest the destination string
931 * @param insert_at where to move the chopped-off part of source string
932 * @returns #FALSE if not enough memory
935 _dbus_string_move (DBusString *source,
940 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
942 if (!copy (real_source, start,
943 real_source->len - start,
948 delete (real_source, start,
949 real_source->len - start);
955 * Like _dbus_string_move(), but does not delete the section
956 * of the source string that's copied to the dest string.
958 * @param source the source string
959 * @param start where to start copying the source string
960 * @param dest the destination string
961 * @param insert_at where to place the copied part of source string
962 * @returns #FALSE if not enough memory
965 _dbus_string_copy (const DBusString *source,
970 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
972 return copy (real_source, start,
973 real_source->len - start,
979 * Like _dbus_string_move(), but can move a segment from
980 * the middle of the source string.
982 * @param source the source string
983 * @param start first byte of source string to move
984 * @param len length of segment to move
985 * @param dest the destination string
986 * @param insert_at where to move the bytes from the source string
987 * @returns #FALSE if not enough memory
990 _dbus_string_move_len (DBusString *source,
997 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
998 _dbus_assert (len >= 0);
999 _dbus_assert ((start + len) <= real_source->len);
1001 if (!copy (real_source, start, len,
1006 delete (real_source, start,
1013 * Like _dbus_string_copy(), but can copy a segment from the middle of
1014 * the source string.
1016 * @param source the source string
1017 * @param start where to start copying the source string
1018 * @param len length of segment to copy
1019 * @param dest the destination string
1020 * @param insert_at where to place the copied segment of source string
1021 * @returns #FALSE if not enough memory
1024 _dbus_string_copy_len (const DBusString *source,
1030 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1031 _dbus_assert (len >= 0);
1032 _dbus_assert ((start + len) <= real_source->len);
1034 return copy (real_source, start, len,
1040 * Replaces a segment of dest string with a segment of source string.
1042 * @todo optimize the case where the two lengths are the same, and
1043 * avoid memmoving the data in the trailing part of the string twice.
1045 * @param source the source string
1046 * @param start where to start copying the source string
1047 * @param len length of segment to copy
1048 * @param dest the destination string
1049 * @param replace_at start of segment of dest string to replace
1050 * @param replace_len length of segment of dest string to replace
1051 * @returns #FALSE if not enough memory
1055 _dbus_string_replace_len (const DBusString *source,
1062 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1063 _dbus_assert (len >= 0);
1064 _dbus_assert ((start + len) <= real_source->len);
1065 _dbus_assert (replace_at >= 0);
1066 _dbus_assert ((replace_at + replace_len) <= real_dest->len);
1068 if (!copy (real_source, start, len,
1069 real_dest, replace_at))
1072 delete (real_dest, replace_at + len, replace_len);
1077 /* Unicode macros from GLib */
1079 /** computes length and mask of a unicode character
1080 * @param Char the char
1081 * @param Mask the mask variable to assign to
1082 * @param Len the length variable to assign to
1084 #define UTF8_COMPUTE(Char, Mask, Len) \
1090 else if ((Char & 0xe0) == 0xc0) \
1095 else if ((Char & 0xf0) == 0xe0) \
1100 else if ((Char & 0xf8) == 0xf0) \
1105 else if ((Char & 0xfc) == 0xf8) \
1110 else if ((Char & 0xfe) == 0xfc) \
1119 * computes length of a unicode character in UTF-8
1120 * @param Char the char
1122 #define UTF8_LENGTH(Char) \
1123 ((Char) < 0x80 ? 1 : \
1124 ((Char) < 0x800 ? 2 : \
1125 ((Char) < 0x10000 ? 3 : \
1126 ((Char) < 0x200000 ? 4 : \
1127 ((Char) < 0x4000000 ? 5 : 6)))))
1130 * Gets a UTF-8 value.
1132 * @param Result variable for extracted unicode char.
1133 * @param Chars the bytes to decode
1134 * @param Count counter variable
1135 * @param Mask mask for this char
1136 * @param Len length for this char in bytes
1138 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1139 (Result) = (Chars)[0] & (Mask); \
1140 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1142 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1148 (Result) |= ((Chars)[(Count)] & 0x3f); \
1152 * Check whether a unicode char is in a valid range.
1154 * @param Char the character
1156 #define UNICODE_VALID(Char) \
1157 ((Char) < 0x110000 && \
1158 ((Char) < 0xD800 || (Char) >= 0xE000) && \
1159 (Char) != 0xFFFE && (Char) != 0xFFFF)
1162 * Gets a unicode character from a UTF-8 string. Does no validation;
1163 * you must verify that the string is valid UTF-8 in advance and must
1164 * pass in the start of a character.
1166 * @param str the string
1167 * @param start the start of the UTF-8 character.
1168 * @param ch_return location to return the character
1169 * @param end_return location to return the byte index of next character
1170 * @returns #TRUE on success, #FALSE otherwise.
1173 _dbus_string_get_unichar (const DBusString *str,
1175 dbus_unichar_t *ch_return,
1179 dbus_unichar_t result;
1182 DBUS_CONST_STRING_PREAMBLE (str);
1187 *end_return = real->len;
1190 p = real->str + start;
1193 UTF8_COMPUTE (c, mask, len);
1196 UTF8_GET (result, p, i, mask, len);
1198 if (result == (dbus_unichar_t)-1)
1202 *ch_return = result;
1204 *end_return = start + len;
1208 * Finds the given substring in the string,
1209 * returning #TRUE and filling in the byte index
1210 * where the substring was found, if it was found.
1211 * Returns #FALSE if the substring wasn't found.
1212 * Sets *start to the length of the string if the substring
1215 * @param str the string
1216 * @param start where to start looking
1217 * @param substr the substring
1218 * @param found return location for where it was found, or #NULL
1219 * @returns #TRUE if found
1222 _dbus_string_find (const DBusString *str,
1228 DBUS_CONST_STRING_PREAMBLE (str);
1229 _dbus_assert (substr != NULL);
1230 _dbus_assert (start <= real->len);
1232 /* we always "find" an empty string */
1233 if (*substr == '\0')
1241 while (i < real->len)
1243 if (real->str[i] == substr[0])
1247 while (j < real->len)
1249 if (substr[j - i] == '\0')
1251 else if (real->str[j] != substr[j - i])
1257 if (substr[j - i] == '\0')
1275 * Finds the given substring in the string,
1276 * up to a certain position,
1277 * returning #TRUE and filling in the byte index
1278 * where the substring was found, if it was found.
1279 * Returns #FALSE if the substring wasn't found.
1280 * Sets *start to the length of the string if the substring
1283 * @param str the string
1284 * @param start where to start looking
1285 * @param end where to stop looking
1286 * @param substr the substring
1287 * @param found return location for where it was found, or #NULL
1288 * @returns #TRUE if found
1291 _dbus_string_find_to (const DBusString *str,
1298 DBUS_CONST_STRING_PREAMBLE (str);
1299 _dbus_assert (substr != NULL);
1300 _dbus_assert (start <= real->len);
1301 _dbus_assert (end <= real->len);
1302 _dbus_assert (start < end);
1304 /* we always "find" an empty string */
1305 if (*substr == '\0')
1313 while (i < real->len && i < end)
1315 if (real->str[i] == substr[0])
1319 while (j < real->len && j < end)
1321 if (substr[j - i] == '\0')
1323 else if (real->str[j] != substr[j - i])
1329 if (substr[j - i] == '\0')
1347 * Finds a blank (space or tab) in the string. Returns #TRUE
1348 * if found, #FALSE otherwise. If a blank is not found sets
1349 * *found to the length of the string.
1351 * @param str the string
1352 * @param start byte index to start looking
1353 * @param found place to store the location of the first blank
1354 * @returns #TRUE if a blank was found
1357 _dbus_string_find_blank (const DBusString *str,
1362 DBUS_CONST_STRING_PREAMBLE (str);
1363 _dbus_assert (start <= real->len);
1366 while (i < real->len)
1368 if (real->str[i] == ' ' ||
1369 real->str[i] == '\t')
1386 * Skips blanks from start, storing the first non-blank in *end
1388 * @param str the string
1389 * @param start where to start
1390 * @param end where to store the first non-blank byte index
1393 _dbus_string_skip_blank (const DBusString *str,
1398 DBUS_CONST_STRING_PREAMBLE (str);
1399 _dbus_assert (start <= real->len);
1402 while (i < real->len)
1404 if (!(real->str[i] == ' ' ||
1405 real->str[i] == '\t'))
1411 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1412 real->str[i] == '\t'));
1419 * Assigns a newline-terminated or \r\n-terminated line from the front
1420 * of the string to the given dest string. The dest string's previous
1421 * contents are deleted. If the source string contains no newline,
1422 * moves the entire source string to the dest string.
1424 * @param source the source string
1425 * @param dest the destination string (contents are replaced)
1426 * @returns #FALSE if no memory, or source has length 0
1429 _dbus_string_pop_line (DBusString *source,
1433 dbus_bool_t have_newline;
1435 _dbus_string_set_length (dest, 0);
1438 if (_dbus_string_find (source, 0, "\n", &eol))
1440 have_newline = TRUE;
1441 eol += 1; /* include newline */
1445 eol = _dbus_string_get_length (source);
1446 have_newline = FALSE;
1450 return FALSE; /* eof */
1452 if (!_dbus_string_move_len (source, 0, eol,
1458 /* dump the newline and the \r if we have one */
1461 dbus_bool_t have_cr;
1463 _dbus_assert (_dbus_string_get_length (dest) > 0);
1465 if (_dbus_string_get_length (dest) > 1 &&
1466 _dbus_string_get_byte (dest,
1467 _dbus_string_get_length (dest) - 2) == '\r')
1472 _dbus_string_set_length (dest,
1473 _dbus_string_get_length (dest) -
1481 * Deletes up to and including the first blank space
1484 * @param str the string
1487 _dbus_string_delete_first_word (DBusString *str)
1492 if (_dbus_string_find_blank (str, 0, &i))
1493 _dbus_string_skip_blank (str, i, &i);
1495 _dbus_string_delete (str, 0, i);
1499 * Deletes any leading blanks in the string
1501 * @param str the string
1504 _dbus_string_delete_leading_blanks (DBusString *str)
1509 _dbus_string_skip_blank (str, 0, &i);
1512 _dbus_string_delete (str, 0, i);
1516 * Tests two DBusString for equality.
1518 * @param a first string
1519 * @param b second string
1520 * @returns #TRUE if equal
1523 _dbus_string_equal (const DBusString *a,
1524 const DBusString *b)
1526 const unsigned char *ap;
1527 const unsigned char *bp;
1528 const unsigned char *a_end;
1529 const DBusRealString *real_a = (const DBusRealString*) a;
1530 const DBusRealString *real_b = (const DBusRealString*) b;
1531 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1532 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1534 if (real_a->len != real_b->len)
1539 a_end = real_a->str + real_a->len;
1553 * Tests two DBusString for equality up to the given length.
1555 * @todo write a unit test
1557 * @param a first string
1558 * @param b second string
1559 * @param len the lengh
1560 * @returns #TRUE if equal for the given number of bytes
1563 _dbus_string_equal_len (const DBusString *a,
1564 const DBusString *b,
1567 const unsigned char *ap;
1568 const unsigned char *bp;
1569 const unsigned char *a_end;
1570 const DBusRealString *real_a = (const DBusRealString*) a;
1571 const DBusRealString *real_b = (const DBusRealString*) b;
1572 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1573 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1575 if (real_a->len != real_b->len &&
1576 (real_a->len < len || real_b->len < len))
1581 a_end = real_a->str + MIN (real_a->len, len);
1595 * Checks whether a string is equal to a C string.
1597 * @param a the string
1598 * @param c_str the C string
1599 * @returns #TRUE if equal
1602 _dbus_string_equal_c_str (const DBusString *a,
1605 const unsigned char *ap;
1606 const unsigned char *bp;
1607 const unsigned char *a_end;
1608 const DBusRealString *real_a = (const DBusRealString*) a;
1609 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1612 bp = (const unsigned char*) c_str;
1613 a_end = real_a->str + real_a->len;
1614 while (ap != a_end && *bp)
1623 if (*ap && *bp == '\0')
1625 else if (ap == a_end && *bp)
1632 * Checks whether a string starts with the given C string.
1634 * @param a the string
1635 * @param c_str the C string
1636 * @returns #TRUE if string starts with it
1639 _dbus_string_starts_with_c_str (const DBusString *a,
1642 const unsigned char *ap;
1643 const unsigned char *bp;
1644 const unsigned char *a_end;
1645 const DBusRealString *real_a = (const DBusRealString*) a;
1646 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1649 bp = (const unsigned char*) c_str;
1650 a_end = real_a->str + real_a->len;
1651 while (ap != a_end && *bp)
1667 * Returns whether a string ends with the given suffix
1669 * @param a the string
1670 * @param c_str the C-style string
1671 * @returns #TRUE if the string ends with the suffix
1674 _dbus_string_ends_with_c_str (const DBusString *a,
1677 const unsigned char *ap;
1678 const unsigned char *bp;
1679 const unsigned char *a_end;
1681 const DBusRealString *real_a = (const DBusRealString*) a;
1682 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1684 c_str_len = strlen (c_str);
1685 if (real_a->len < c_str_len)
1688 ap = real_a->str + (real_a->len - c_str_len);
1689 bp = (const unsigned char*) c_str;
1690 a_end = real_a->str + real_a->len;
1700 _dbus_assert (*ap == '\0');
1701 _dbus_assert (*bp == '\0');
1706 static const signed char base64_table[] = {
1773 /** The minimum char that's a valid char in Base64-encoded text */
1774 #define UNBASE64_MIN_CHAR (43)
1775 /** The maximum char that's a valid char in Base64-encoded text */
1776 #define UNBASE64_MAX_CHAR (122)
1777 /** Must subtract this from a char's integer value before offsetting
1778 * into unbase64_table
1780 #define UNBASE64_TABLE_OFFSET UNBASE64_MIN_CHAR
1781 static const signed char unbase64_table[] = {
1865 * Encodes a string using Base64, as documented in RFC 2045.
1867 * @param source the string to encode
1868 * @param start byte index to start encoding
1869 * @param dest string where encoded data should be placed
1870 * @param insert_at where to place encoded data
1871 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
1874 _dbus_string_base64_encode (const DBusString *source,
1881 const unsigned char *s;
1883 const unsigned char *triplet_end;
1884 const unsigned char *final_end;
1885 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1886 _dbus_assert (source != dest);
1888 /* For each 24 bits (3 bytes) of input, we have 4 chars of
1891 source_len = real_source->len - start;
1892 dest_len = (source_len / 3) * 4;
1893 if (source_len % 3 != 0)
1896 if (source_len == 0)
1899 if (!open_gap (dest_len, real_dest, insert_at))
1902 d = real_dest->str + insert_at;
1903 s = real_source->str + start;
1904 final_end = real_source->str + (start + source_len);
1905 triplet_end = final_end - (source_len % 3);
1906 _dbus_assert (triplet_end <= final_end);
1907 _dbus_assert ((final_end - triplet_end) < 3);
1909 #define ENCODE_64(v) (base64_table[ (unsigned char) (v) ])
1910 #define SIX_BITS_MASK (0x3f)
1911 _dbus_assert (SIX_BITS_MASK < _DBUS_N_ELEMENTS (base64_table));
1913 while (s != triplet_end)
1915 unsigned int triplet;
1917 triplet = s[2] | (s[1] << 8) | (s[0] << 16);
1919 /* Encode each 6 bits. */
1921 *d++ = ENCODE_64 (triplet >> 18);
1922 *d++ = ENCODE_64 ((triplet >> 12) & SIX_BITS_MASK);
1923 *d++ = ENCODE_64 ((triplet >> 6) & SIX_BITS_MASK);
1924 *d++ = ENCODE_64 (triplet & SIX_BITS_MASK);
1929 switch (final_end - triplet_end)
1933 unsigned int doublet;
1935 doublet = s[1] | (s[0] << 8);
1937 *d++ = ENCODE_64 (doublet >> 12);
1938 *d++ = ENCODE_64 ((doublet >> 6) & SIX_BITS_MASK);
1939 *d++ = ENCODE_64 (doublet & SIX_BITS_MASK);
1945 unsigned int singlet;
1949 *d++ = ENCODE_64 ((singlet >> 6) & SIX_BITS_MASK);
1950 *d++ = ENCODE_64 (singlet & SIX_BITS_MASK);
1959 _dbus_assert (d == (real_dest->str + (insert_at + dest_len)));
1966 * Decodes a string from Base64, as documented in RFC 2045.
1968 * @param source the string to decode
1969 * @param start byte index to start decode
1970 * @param dest string where decoded data should be placed
1971 * @param insert_at where to place decoded data
1972 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
1975 _dbus_string_base64_decode (const DBusString *source,
1984 unsigned int triplet = 0;
1987 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1988 _dbus_assert (source != dest);
1990 source_len = real_source->len - start;
1991 s = real_source->str + start;
1992 end = real_source->str + source_len;
1994 if (source_len == 0)
1997 if (!_dbus_string_init (&result, _DBUS_INT_MAX))
2004 /* The idea is to just skip anything that isn't
2005 * a base64 char - it's allowed to have whitespace,
2006 * newlines, etc. in here. We also ignore trailing
2007 * base64 chars, though that's suspicious.
2010 if (*s >= UNBASE64_MIN_CHAR &&
2011 *s <= UNBASE64_MAX_CHAR)
2015 /* '=' is padding, doesn't represent additional data
2016 * but does increment our count.
2025 val = unbase64_table[(*s) - UNBASE64_TABLE_OFFSET];
2030 triplet |= (unsigned int) val;
2035 if (sextet_count == 4)
2037 /* no pad = 3 bytes, 1 pad = 2 bytes, 2 pad = 1 byte */
2039 _dbus_string_append_byte (&result,
2043 _dbus_string_append_byte (&result,
2044 (triplet >> 8) & 0xff);
2046 _dbus_string_append_byte (&result,
2058 if (!_dbus_string_move (&result, 0, dest, insert_at))
2060 _dbus_string_free (&result);
2064 _dbus_string_free (&result);
2070 * Checks that the given range of the string
2071 * is valid ASCII. If the given range is not contained
2072 * in the string, returns #FALSE.
2074 * @param str the string
2075 * @param start first byte index to check
2076 * @param len number of bytes to check
2077 * @returns #TRUE if the byte range exists and is all valid ASCII
2080 _dbus_string_validate_ascii (const DBusString *str,
2084 const unsigned char *s;
2085 const unsigned char *end;
2086 DBUS_CONST_STRING_PREAMBLE (str);
2087 _dbus_assert (start >= 0);
2088 _dbus_assert (len >= 0);
2090 if ((start + len) > real->len)
2093 s = real->str + start;
2098 ((*s & ~0x7f) != 0))
2108 * Checks that the given range of the string
2109 * is valid UTF-8. If the given range is not contained
2110 * in the string, returns #FALSE. If the string
2111 * contains any nul bytes in the given range, returns
2114 * @todo right now just calls _dbus_string_validate_ascii()
2116 * @param str the string
2117 * @param start first byte index to check
2118 * @param len number of bytes to check
2119 * @returns #TRUE if the byte range exists and is all valid UTF-8
2122 _dbus_string_validate_utf8 (const DBusString *str,
2126 /* FIXME actually validate UTF-8 */
2127 return _dbus_string_validate_ascii (str, start, len);
2131 * Checks that the given range of the string
2132 * is all nul bytes. If the given range is
2133 * not contained in the string, returns #FALSE.
2135 * @param str the string
2136 * @param start first byte index to check
2137 * @param len number of bytes to check
2138 * @returns #TRUE if the byte range exists and is all nul bytes
2141 _dbus_string_validate_nul (const DBusString *str,
2145 const unsigned char *s;
2146 const unsigned char *end;
2147 DBUS_CONST_STRING_PREAMBLE (str);
2148 _dbus_assert (start >= 0);
2149 _dbus_assert (len >= 0);
2151 if ((start + len) > real->len)
2154 s = real->str + start;
2168 #ifdef DBUS_BUILD_TESTS
2169 #include "dbus-test.h"
2173 test_max_len (DBusString *str,
2178 if (!_dbus_string_set_length (str, max_len - 1))
2179 _dbus_assert_not_reached ("setting len to one less than max should have worked");
2182 if (!_dbus_string_set_length (str, max_len))
2183 _dbus_assert_not_reached ("setting len to max len should have worked");
2185 if (_dbus_string_set_length (str, max_len + 1))
2186 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
2188 if (!_dbus_string_set_length (str, 0))
2189 _dbus_assert_not_reached ("setting len to zero should have worked");
2193 test_base64_roundtrip (const unsigned char *data,
2201 len = strlen (data);
2203 if (!_dbus_string_init (&orig, _DBUS_INT_MAX))
2204 _dbus_assert_not_reached ("could not init string");
2206 if (!_dbus_string_init (&encoded, _DBUS_INT_MAX))
2207 _dbus_assert_not_reached ("could not init string");
2209 if (!_dbus_string_init (&decoded, _DBUS_INT_MAX))
2210 _dbus_assert_not_reached ("could not init string");
2212 if (!_dbus_string_append_len (&orig, data, len))
2213 _dbus_assert_not_reached ("couldn't append orig data");
2215 if (!_dbus_string_base64_encode (&orig, 0, &encoded, 0))
2216 _dbus_assert_not_reached ("could not encode");
2218 if (!_dbus_string_base64_decode (&encoded, 0, &decoded, 0))
2219 _dbus_assert_not_reached ("could not decode");
2221 if (!_dbus_string_equal (&orig, &decoded))
2225 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
2226 _dbus_string_get_length (&orig),
2227 _dbus_string_get_length (&encoded),
2228 _dbus_string_get_length (&decoded));
2229 printf ("Original: %s\n", data);
2230 _dbus_string_get_const_data (&decoded, &s);
2231 printf ("Decoded: %s\n", s);
2232 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
2235 _dbus_string_free (&orig);
2236 _dbus_string_free (&encoded);
2237 _dbus_string_free (&decoded);
2241 * @ingroup DBusStringInternals
2242 * Unit test for DBusString.
2244 * @todo Need to write tests for _dbus_string_copy() and
2245 * _dbus_string_move() moving to/from each of start/middle/end of a
2246 * string. Also need tests for _dbus_string_move_len ()
2248 * @returns #TRUE on success.
2251 _dbus_string_test (void)
2258 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 };
2263 while (i < _DBUS_N_ELEMENTS (lens))
2265 if (!_dbus_string_init (&str, lens[i]))
2266 _dbus_assert_not_reached ("failed to init string");
2268 test_max_len (&str, lens[i]);
2269 _dbus_string_free (&str);
2274 /* Test shortening and setting length */
2276 while (i < _DBUS_N_ELEMENTS (lens))
2280 if (!_dbus_string_init (&str, lens[i]))
2281 _dbus_assert_not_reached ("failed to init string");
2283 if (!_dbus_string_set_length (&str, lens[i]))
2284 _dbus_assert_not_reached ("failed to set string length");
2289 _dbus_assert (_dbus_string_get_length (&str) == j);
2292 _dbus_string_shorten (&str, 1);
2293 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
2298 _dbus_string_free (&str);
2303 /* Test appending data */
2304 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2305 _dbus_assert_not_reached ("failed to init string");
2310 if (!_dbus_string_append (&str, "a"))
2311 _dbus_assert_not_reached ("failed to append string to string\n");
2313 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
2315 if (!_dbus_string_append_byte (&str, 'b'))
2316 _dbus_assert_not_reached ("failed to append byte to string\n");
2318 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
2323 _dbus_string_free (&str);
2325 /* Check steal_data */
2327 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2328 _dbus_assert_not_reached ("failed to init string");
2330 if (!_dbus_string_append (&str, "Hello World"))
2331 _dbus_assert_not_reached ("could not append to string");
2333 i = _dbus_string_get_length (&str);
2335 if (!_dbus_string_steal_data (&str, &s))
2336 _dbus_assert_not_reached ("failed to steal data");
2338 _dbus_assert (_dbus_string_get_length (&str) == 0);
2339 _dbus_assert (((int)strlen (s)) == i);
2345 if (!_dbus_string_append (&str, "Hello World"))
2346 _dbus_assert_not_reached ("could not append to string");
2348 i = _dbus_string_get_length (&str);
2350 if (!_dbus_string_init (&other, _DBUS_INT_MAX))
2351 _dbus_assert_not_reached ("could not init string");
2353 if (!_dbus_string_move (&str, 0, &other, 0))
2354 _dbus_assert_not_reached ("could not move");
2356 _dbus_assert (_dbus_string_get_length (&str) == 0);
2357 _dbus_assert (_dbus_string_get_length (&other) == i);
2359 if (!_dbus_string_append (&str, "Hello World"))
2360 _dbus_assert_not_reached ("could not append to string");
2362 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
2363 _dbus_assert_not_reached ("could not move");
2365 _dbus_assert (_dbus_string_get_length (&str) == 0);
2366 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
2368 if (!_dbus_string_append (&str, "Hello World"))
2369 _dbus_assert_not_reached ("could not append to string");
2371 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
2372 _dbus_assert_not_reached ("could not move");
2374 _dbus_assert (_dbus_string_get_length (&str) == 0);
2375 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
2377 _dbus_string_free (&other);
2381 if (!_dbus_string_append (&str, "Hello World"))
2382 _dbus_assert_not_reached ("could not append to string");
2384 i = _dbus_string_get_length (&str);
2386 if (!_dbus_string_init (&other, _DBUS_INT_MAX))
2387 _dbus_assert_not_reached ("could not init string");
2389 if (!_dbus_string_copy (&str, 0, &other, 0))
2390 _dbus_assert_not_reached ("could not copy");
2392 _dbus_assert (_dbus_string_get_length (&str) == i);
2393 _dbus_assert (_dbus_string_get_length (&other) == i);
2395 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
2396 _dbus_assert_not_reached ("could not copy");
2398 _dbus_assert (_dbus_string_get_length (&str) == i);
2399 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
2400 _dbus_assert (_dbus_string_equal_c_str (&other,
2401 "Hello WorldHello World"));
2403 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
2404 _dbus_assert_not_reached ("could not copy");
2406 _dbus_assert (_dbus_string_get_length (&str) == i);
2407 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
2408 _dbus_assert (_dbus_string_equal_c_str (&other,
2409 "Hello WorldHello WorldHello World"));
2411 _dbus_string_free (&str);
2412 _dbus_string_free (&other);
2416 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2417 _dbus_assert_not_reached ("failed to init string");
2419 if (!_dbus_string_append (&str, "Hello World"))
2420 _dbus_assert_not_reached ("could not append to string");
2422 i = _dbus_string_get_length (&str);
2424 if (!_dbus_string_init (&other, _DBUS_INT_MAX))
2425 _dbus_assert_not_reached ("could not init string");
2427 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
2428 &other, 0, _dbus_string_get_length (&other)))
2429 _dbus_assert_not_reached ("could not replace");
2431 _dbus_assert (_dbus_string_get_length (&str) == i);
2432 _dbus_assert (_dbus_string_get_length (&other) == i);
2433 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
2435 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
2437 _dbus_assert_not_reached ("could not replace center space");
2439 _dbus_assert (_dbus_string_get_length (&str) == i);
2440 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
2441 _dbus_assert (_dbus_string_equal_c_str (&other,
2442 "HelloHello WorldWorld"));
2445 if (!_dbus_string_replace_len (&str, 1, 1,
2447 _dbus_string_get_length (&other) - 1,
2449 _dbus_assert_not_reached ("could not replace end character");
2451 _dbus_assert (_dbus_string_get_length (&str) == i);
2452 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
2453 _dbus_assert (_dbus_string_equal_c_str (&other,
2454 "HelloHello WorldWorle"));
2456 _dbus_string_free (&str);
2457 _dbus_string_free (&other);
2459 /* Check append/get unichar */
2461 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2462 _dbus_assert_not_reached ("failed to init string");
2465 if (!_dbus_string_append_unichar (&str, 0xfffc))
2466 _dbus_assert_not_reached ("failed to append unichar");
2468 _dbus_string_get_unichar (&str, 0, &ch, &i);
2470 _dbus_assert (ch == 0xfffc);
2471 _dbus_assert (i == _dbus_string_get_length (&str));
2473 _dbus_string_free (&str);
2475 /* Check insert/set/get byte */
2477 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2478 _dbus_assert_not_reached ("failed to init string");
2480 if (!_dbus_string_append (&str, "Hello"))
2481 _dbus_assert_not_reached ("failed to append Hello");
2483 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
2484 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
2485 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
2486 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
2487 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
2489 _dbus_string_set_byte (&str, 1, 'q');
2490 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
2492 if (!_dbus_string_insert_byte (&str, 0, 255))
2493 _dbus_assert_not_reached ("can't insert byte");
2495 if (!_dbus_string_insert_byte (&str, 2, 'Z'))
2496 _dbus_assert_not_reached ("can't insert byte");
2498 if (!_dbus_string_insert_byte (&str, _dbus_string_get_length (&str), 'W'))
2499 _dbus_assert_not_reached ("can't insert byte");
2501 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
2502 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
2503 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
2504 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'q');
2505 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'l');
2506 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'l');
2507 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'o');
2508 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'W');
2510 _dbus_string_free (&str);
2512 /* Check append/parse int/double */
2514 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2515 _dbus_assert_not_reached ("failed to init string");
2517 if (!_dbus_string_append_int (&str, 27))
2518 _dbus_assert_not_reached ("failed to append int");
2520 i = _dbus_string_get_length (&str);
2522 if (!_dbus_string_parse_int (&str, 0, &v, &end))
2523 _dbus_assert_not_reached ("failed to parse int");
2525 _dbus_assert (v == 27);
2526 _dbus_assert (end == i);
2528 _dbus_string_free (&str);
2530 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2531 _dbus_assert_not_reached ("failed to init string");
2533 if (!_dbus_string_append_double (&str, 50.3))
2534 _dbus_assert_not_reached ("failed to append float");
2536 i = _dbus_string_get_length (&str);
2538 if (!_dbus_string_parse_double (&str, 0, &d, &end))
2539 _dbus_assert_not_reached ("failed to parse float");
2541 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
2542 _dbus_assert (end == i);
2544 _dbus_string_free (&str);
2547 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2548 _dbus_assert_not_reached ("failed to init string");
2550 if (!_dbus_string_append (&str, "Hello"))
2551 _dbus_assert_not_reached ("couldn't append to string");
2553 if (!_dbus_string_find (&str, 0, "He", &i))
2554 _dbus_assert_not_reached ("didn't find 'He'");
2555 _dbus_assert (i == 0);
2557 if (!_dbus_string_find (&str, 0, "Hello", &i))
2558 _dbus_assert_not_reached ("didn't find 'Hello'");
2559 _dbus_assert (i == 0);
2561 if (!_dbus_string_find (&str, 0, "ello", &i))
2562 _dbus_assert_not_reached ("didn't find 'ello'");
2563 _dbus_assert (i == 1);
2565 if (!_dbus_string_find (&str, 0, "lo", &i))
2566 _dbus_assert_not_reached ("didn't find 'lo'");
2567 _dbus_assert (i == 3);
2569 if (!_dbus_string_find (&str, 2, "lo", &i))
2570 _dbus_assert_not_reached ("didn't find 'lo'");
2571 _dbus_assert (i == 3);
2573 if (_dbus_string_find (&str, 4, "lo", &i))
2574 _dbus_assert_not_reached ("did find 'lo'");
2576 if (!_dbus_string_find (&str, 0, "l", &i))
2577 _dbus_assert_not_reached ("didn't find 'l'");
2578 _dbus_assert (i == 2);
2580 if (!_dbus_string_find (&str, 0, "H", &i))
2581 _dbus_assert_not_reached ("didn't find 'H'");
2582 _dbus_assert (i == 0);
2584 if (!_dbus_string_find (&str, 0, "", &i))
2585 _dbus_assert_not_reached ("didn't find ''");
2586 _dbus_assert (i == 0);
2588 if (_dbus_string_find (&str, 0, "Hello!", NULL))
2589 _dbus_assert_not_reached ("Did find 'Hello!'");
2591 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
2592 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
2594 if (_dbus_string_find (&str, 0, "ill", NULL))
2595 _dbus_assert_not_reached ("Did find 'ill'");
2597 if (_dbus_string_find (&str, 0, "q", NULL))
2598 _dbus_assert_not_reached ("Did find 'q'");
2600 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
2601 _dbus_assert_not_reached ("Didn't find 'He'");
2603 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
2604 _dbus_assert_not_reached ("Did find 'Hello'");
2606 _dbus_string_free (&str);
2609 test_base64_roundtrip ("Hello this is a string\n", -1);
2610 test_base64_roundtrip ("Hello this is a string\n1", -1);
2611 test_base64_roundtrip ("Hello this is a string\n12", -1);
2612 test_base64_roundtrip ("Hello this is a string\n123", -1);
2613 test_base64_roundtrip ("Hello this is a string\n1234", -1);
2614 test_base64_roundtrip ("Hello this is a string\n12345", -1);
2615 test_base64_roundtrip ("", 0);
2616 test_base64_roundtrip ("1", 1);
2617 test_base64_roundtrip ("12", 2);
2618 test_base64_roundtrip ("123", 3);
2619 test_base64_roundtrip ("1234", 4);
2620 test_base64_roundtrip ("12345", 5);
2621 test_base64_roundtrip ("", 1);
2622 test_base64_roundtrip ("1", 2);
2623 test_base64_roundtrip ("12", 3);
2624 test_base64_roundtrip ("123", 4);
2625 test_base64_roundtrip ("1234", 5);
2626 test_base64_roundtrip ("12345", 6);
2628 unsigned char buf[512];
2630 while (i < _DBUS_N_ELEMENTS (buf))
2636 while (i < _DBUS_N_ELEMENTS (buf))
2638 test_base64_roundtrip (buf, i);
2646 #endif /* DBUS_BUILD_TESTS */