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-string.h"
25 /* we allow a system header here, for speed/convenience */
29 * @defgroup DBusString string class
30 * @ingroup DBusInternals
31 * @brief DBusString data structure
33 * Types and functions related to DBusString. DBusString is intended
34 * to be a string class that makes it hard to mess up security issues
35 * (and just in general harder to write buggy code). It should be
36 * used (or extended and then used) rather than the libc stuff in
37 * string.h. The string class is a bit inconvenient at spots because
38 * it handles out-of-memory failures and tries to be extra-robust.
40 * A DBusString has a maximum length set at initialization time; this
41 * can be used to ensure that a buffer doesn't get too big. The
42 * _dbus_string_lengthen() method checks for overflow, and for max
43 * length being exceeded.
45 * Try to avoid conversion to a plain C string, i.e. add methods on
46 * the string object instead, only convert to C string when passing
47 * things out to the public API. In particular, no sprintf, strcpy,
48 * strcat, any of that should be used. The GString feature of
49 * accepting negative numbers for "length of string" is also absent,
50 * because it could keep us from detecting bogus huge lengths. i.e. if
51 * we passed in some bogus huge length it would be taken to mean
52 * "current length of string" instead of "broken crack"
56 * @defgroup DBusStringInternals DBusString implementation details
57 * @ingroup DBusInternals
58 * @brief DBusString implementation details
60 * The guts of DBusString.
66 * @brief Internals of DBusString.
68 * DBusString internals. DBusString is an opaque objects, it must be
69 * used via accessor functions.
73 unsigned char *str; /**< String data, plus nul termination */
74 int len; /**< Length without nul */
75 int allocated; /**< Allocated size of data */
76 int max_length; /**< Max length of this string. */
77 unsigned int constant : 1; /**< String data is not owned by DBusString */
78 unsigned int locked : 1; /**< DBusString has been locked and can't be changed */
79 unsigned int invalid : 1; /**< DBusString is invalid (e.g. already freed) */
83 * Checks a bunch of assertions about a string object
85 * @param real the DBusRealString
87 #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)
90 * Checks assertions about a string object that needs to be
91 * modifiable - may not be locked or const. Also declares
92 * the "real" variable pointing to DBusRealString.
93 * @param str the string
95 #define DBUS_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
96 DBUS_GENERIC_STRING_PREAMBLE (real); \
97 _dbus_assert (!(real)->constant); \
98 _dbus_assert (!(real)->locked)
101 * Checks assertions about a string object that may be locked but
102 * can't be const. i.e. a string object that we can free. Also
103 * declares the "real" variable pointing to DBusRealString.
105 * @param str the string
107 #define DBUS_LOCKED_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
108 DBUS_GENERIC_STRING_PREAMBLE (real); \
109 _dbus_assert (!(real)->constant)
112 * Checks assertions about a string that may be const or locked. Also
113 * declares the "real" variable pointing to DBusRealString.
114 * @param str the string.
116 #define DBUS_CONST_STRING_PREAMBLE(str) const DBusRealString *real = (DBusRealString*) str; \
117 DBUS_GENERIC_STRING_PREAMBLE (real)
122 * @addtogroup DBusString
127 * Initializes a string. The maximum length may be _DBUS_INT_MAX for
128 * no maximum. The string starts life with zero length.
129 * The string must eventually be freed with _dbus_string_free().
131 * @param str memory to hold the string
132 * @param max_length the maximum size of the string
133 * @returns #TRUE on success
136 _dbus_string_init (DBusString *str,
139 DBusRealString *real;
141 _dbus_assert (str != NULL);
142 _dbus_assert (max_length >= 0);
144 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
146 real = (DBusRealString*) str;
148 /* It's very important not to touch anything
149 * other than real->str if we're going to fail,
150 * since we also use this function to reset
151 * an existing string, e.g. in _dbus_string_steal_data()
154 #define INITIAL_ALLOC 2
156 real->str = dbus_malloc (INITIAL_ALLOC);
157 if (real->str == NULL)
160 real->allocated = INITIAL_ALLOC;
162 real->str[real->len] = '\0';
164 real->max_length = max_length;
165 real->constant = FALSE;
166 real->locked = FALSE;
167 real->invalid = FALSE;
173 * Initializes a constant string. The value parameter is not copied
174 * (should be static), and the string may never be modified.
175 * It is safe but not necessary to call _dbus_string_free()
178 * @param str memory to use for the string
179 * @param value a string to be stored in str (not copied!!!)
182 _dbus_string_init_const (DBusString *str,
185 DBusRealString *real;
187 _dbus_assert (str != NULL);
188 _dbus_assert (value != NULL);
190 real = (DBusRealString*) str;
192 real->str = (char*) value;
193 real->len = strlen (real->str);
194 real->allocated = real->len;
195 real->max_length = real->len;
196 real->constant = TRUE;
197 real->invalid = FALSE;
201 * Frees a string created by _dbus_string_init().
203 * @param str memory where the string is stored.
206 _dbus_string_free (DBusString *str)
208 DBUS_LOCKED_STRING_PREAMBLE (str);
213 dbus_free (real->str);
215 real->invalid = TRUE;
219 * Locks a string such that any attempts to change the string
220 * will result in aborting the program. Also, if the string
221 * is wasting a lot of memory (allocation is larger than what
222 * the string is really using), _dbus_string_lock() will realloc
223 * the string's data to "compact" it.
225 * @param str the string to lock.
228 _dbus_string_lock (DBusString *str)
230 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
234 /* Try to realloc to avoid excess memory usage, since
235 * we know we won't change the string further
238 if (real->allocated > (real->len + MAX_WASTE))
243 new_allocated = real->len + 1;
245 new_str = dbus_realloc (real->str, new_allocated);
249 real->allocated = new_allocated;
255 * Gets the raw character buffer from the string. The returned buffer
256 * will be nul-terminated, but note that strings may contain binary
257 * data so there may be extra nul characters prior to the termination.
258 * This function should be little-used, extend DBusString or add
259 * stuff to dbus-sysdeps.c instead. It's an error to use this
260 * function on a const string.
262 * @param str the string
263 * @param data_return place to store the returned data
266 _dbus_string_get_data (DBusString *str,
269 DBUS_STRING_PREAMBLE (str);
270 _dbus_assert (data_return != NULL);
272 *data_return = real->str;
276 * Gets the raw character buffer from a const string.
278 * @param str the string
279 * @param data_return location to store returned data
282 _dbus_string_get_const_data (const DBusString *str,
283 const char **data_return)
285 DBUS_CONST_STRING_PREAMBLE (str);
286 _dbus_assert (data_return != NULL);
288 *data_return = real->str;
292 * Gets a sub-portion of the raw character buffer from the
293 * string. The "len" field is required simply for error
294 * checking, to be sure you don't try to use more
295 * string than exists. The nul termination of the
296 * returned buffer remains at the end of the entire
297 * string, not at start + len.
299 * @param str the string
300 * @param data_return location to return the buffer
301 * @param start byte offset to return
302 * @param len length of segment to return
305 _dbus_string_get_data_len (DBusString *str,
310 DBUS_STRING_PREAMBLE (str);
311 _dbus_assert (data_return != NULL);
312 _dbus_assert (start >= 0);
313 _dbus_assert (len >= 0);
314 _dbus_assert ((start + len) <= real->len);
316 *data_return = real->str + start;
320 * const version of _dbus_string_get_data_len().
322 * @param str the string
323 * @param data_return location to return the buffer
324 * @param start byte offset to return
325 * @param len length of segment to return
328 _dbus_string_get_const_data_len (const DBusString *str,
329 const char **data_return,
333 DBUS_CONST_STRING_PREAMBLE (str);
334 _dbus_assert (data_return != NULL);
335 _dbus_assert (start >= 0);
336 _dbus_assert (len >= 0);
337 _dbus_assert ((start + len) <= real->len);
339 *data_return = real->str + start;
343 * Like _dbus_string_get_data(), but removes the
344 * gotten data from the original string. The caller
345 * must free the data returned. This function may
346 * fail due to lack of memory, and return #FALSE.
348 * @param str the string
349 * @param data_return location to return the buffer
350 * @returns #TRUE on success
353 _dbus_string_steal_data (DBusString *str,
356 DBUS_STRING_PREAMBLE (str);
357 _dbus_assert (data_return != NULL);
359 *data_return = real->str;
361 /* reset the string */
362 if (!_dbus_string_init (str, real->max_length))
364 /* hrm, put it back then */
365 real->str = *data_return;
374 * Like _dbus_string_get_data_len(), but removes the gotten data from
375 * the original string. The caller must free the data returned. This
376 * function may fail due to lack of memory, and return #FALSE.
377 * The returned string is nul-terminated and has length len.
379 * @param str the string
380 * @param data_return location to return the buffer
381 * @param start the start of segment to steal
382 * @param len the length of segment to steal
383 * @returns #TRUE on success
386 _dbus_string_steal_data_len (DBusString *str,
393 DBUS_STRING_PREAMBLE (str);
394 _dbus_assert (data_return != NULL);
395 _dbus_assert (start >= 0);
396 _dbus_assert (len >= 0);
397 _dbus_assert ((start + len) <= real->len);
399 if (!_dbus_string_init (&dest, real->max_length))
402 if (!_dbus_string_move_len (str, start, len, &dest, 0))
404 _dbus_string_free (&dest);
408 if (!_dbus_string_steal_data (&dest, data_return))
410 _dbus_string_free (&dest);
414 _dbus_string_free (&dest);
419 * Gets the length of a string (not including nul termination).
421 * @returns the length.
424 _dbus_string_get_length (const DBusString *str)
426 DBUS_CONST_STRING_PREAMBLE (str);
432 set_length (DBusRealString *real,
435 /* Note, we are setting the length without nul termination */
437 /* exceeding max length is the same as failure to allocate memory */
438 if (new_length > real->max_length)
441 while (new_length >= real->allocated)
446 new_allocated = 2 + real->allocated * 2;
447 if (new_allocated < real->allocated)
448 return FALSE; /* overflow */
450 new_str = dbus_realloc (real->str, new_allocated);
455 real->allocated = new_allocated;
458 real->len = new_length;
459 real->str[real->len] = '\0';
465 * Makes a string longer by the given number of bytes. Checks whether
466 * adding additional_length to the current length would overflow an
467 * integer, and checks for exceeding a string's max length.
468 * The new bytes are not initialized, other than nul-terminating
469 * the end of the string. The uninitialized bytes may contain
470 * unexpected nul bytes or other junk.
472 * @param str a string
473 * @param additional_length length to add to the string.
474 * @returns #TRUE on success.
477 _dbus_string_lengthen (DBusString *str,
478 int additional_length)
480 DBUS_STRING_PREAMBLE (str);
481 _dbus_assert (additional_length >= 0);
483 if ((real->len + additional_length) < real->len)
484 return FALSE; /* overflow */
486 return set_length (real,
487 real->len + additional_length);
491 * Makes a string shorter by the given number of bytes.
493 * @param str a string
494 * @param length_to_remove length to remove from the string.
497 _dbus_string_shorten (DBusString *str,
498 int length_to_remove)
500 DBUS_STRING_PREAMBLE (str);
501 _dbus_assert (length_to_remove >= 0);
502 _dbus_assert (length_to_remove <= real->len);
505 real->len - length_to_remove);
509 * Sets the length of a string. Can be used to truncate or lengthen
510 * the string. If the string is lengthened, the function may fail and
511 * return #FALSE. Newly-added bytes are not initialized, as with
512 * _dbus_string_lengthen().
514 * @param str a string
515 * @param length new length of the string.
516 * @returns #FALSE on failure.
519 _dbus_string_set_length (DBusString *str,
522 DBUS_STRING_PREAMBLE (str);
523 _dbus_assert (length >= 0);
525 return set_length (real, length);
529 append (DBusRealString *real,
536 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
539 memcpy (real->str + (real->len - buffer_len),
547 * Appends a nul-terminated C-style string to a DBusString.
549 * @param str the DBusString
550 * @param buffer the nul-terminated characters to append
551 * @returns #FALSE if not enough memory.
554 _dbus_string_append (DBusString *str,
559 DBUS_STRING_PREAMBLE (str);
560 _dbus_assert (buffer != NULL);
562 buffer_len = strlen (buffer);
564 return append (real, buffer, buffer_len);
568 * Appends block of bytes with the given length to a DBusString.
570 * @param str the DBusString
571 * @param buffer the bytes to append
572 * @param len the number of bytes to append
573 * @returns #FALSE if not enough memory.
576 _dbus_string_append_len (DBusString *str,
580 DBUS_STRING_PREAMBLE (str);
581 _dbus_assert (buffer != NULL);
582 _dbus_assert (len >= 0);
584 return append (real, buffer, len);
588 * Appends a single byte to the string, returning #FALSE
589 * if not enough memory.
591 * @param str the string
592 * @param byte the byte to append
593 * @returns #TRUE on success
596 _dbus_string_append_byte (DBusString *str,
599 DBUS_STRING_PREAMBLE (str);
601 if (!set_length (real, real->len + 1))
604 real->str[real->len-1] = byte;
610 * Appends a single Unicode character, encoding the character
613 * @param str the string
614 * @param ch the Unicode character
617 _dbus_string_append_unichar (DBusString *str,
625 DBUS_STRING_PREAMBLE (str);
627 /* this code is from GLib but is pretty standard I think */
641 else if (ch < 0x10000)
646 else if (ch < 0x200000)
651 else if (ch < 0x4000000)
662 if (!set_length (real, real->len + len))
665 out = real->str + (real->len - len);
667 for (i = len - 1; i > 0; --i)
669 out[i] = (ch & 0x3f) | 0x80;
678 delete (DBusRealString *real,
685 memmove (real->str + start, real->str + start + len, real->len - (start + len));
687 real->str[real->len] = '\0';
691 * Deletes a segment of a DBusString with length len starting at
692 * start. (Hint: to clear an entire string, setting length to 0
693 * with _dbus_string_set_length() is easier.)
695 * @param str the DBusString
696 * @param start where to start deleting
697 * @param len the number of bytes to delete
700 _dbus_string_delete (DBusString *str,
704 DBUS_STRING_PREAMBLE (str);
705 _dbus_assert (start >= 0);
706 _dbus_assert (len >= 0);
707 _dbus_assert ((start + len) <= real->len);
709 delete (real, start, len);
713 copy (DBusRealString *source,
716 DBusRealString *dest,
722 if (!set_length (dest, dest->len + len))
725 memmove (dest->str + insert_at + len,
726 dest->str + insert_at,
729 memcpy (dest->str + insert_at,
737 * Checks assertions for two strings we're copying a segment between,
738 * and declares real_source/real_dest variables.
740 * @param source the source string
741 * @param start the starting offset
742 * @param dest the dest string
743 * @param insert_at where the copied segment is inserted
745 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
746 DBusRealString *real_source = (DBusRealString*) source; \
747 DBusRealString *real_dest = (DBusRealString*) dest; \
748 _dbus_assert ((source) != (dest)); \
749 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
750 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
751 _dbus_assert (!real_source->constant); \
752 _dbus_assert (!real_source->locked); \
753 _dbus_assert (!real_dest->constant); \
754 _dbus_assert (!real_dest->locked); \
755 _dbus_assert ((start) >= 0); \
756 _dbus_assert ((start) <= real_source->len); \
757 _dbus_assert ((insert_at) >= 0); \
758 _dbus_assert ((insert_at) <= real_dest->len)
761 * Moves the end of one string into another string. Both strings
762 * must be initialized, valid strings.
764 * @param source the source string
765 * @param start where to chop off the source string
766 * @param dest the destination string
767 * @param insert_at where to move the chopped-off part of source string
768 * @returns #FALSE if not enough memory
771 _dbus_string_move (DBusString *source,
776 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
778 if (!copy (real_source, start,
779 real_source->len - start,
784 delete (real_source, start,
785 real_source->len - start);
791 * Like _dbus_string_move(), but does not delete the section
792 * of the source string that's copied to the dest string.
794 * @param source the source string
795 * @param start where to start copying the source string
796 * @param dest the destination string
797 * @param insert_at where to place the copied part of source string
798 * @returns #FALSE if not enough memory
801 _dbus_string_copy (const DBusString *source,
806 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
808 return copy (real_source, start,
809 real_source->len - start,
815 * Like _dbus_string_move(), but can move a segment from
816 * the middle of the source string.
818 * @param source the source string
819 * @param start first byte of source string to move
820 * @param len length of segment to move
821 * @param dest the destination string
822 * @param insert_at where to move the bytes from the source string
823 * @returns #FALSE if not enough memory
826 _dbus_string_move_len (DBusString *source,
833 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
834 _dbus_assert (len >= 0);
835 _dbus_assert ((start + len) <= real_source->len);
837 if (!copy (real_source, start, len,
842 delete (real_source, start,
843 real_source->len - start);
849 * Like _dbus_string_copy(), but can copy a segment from the middle of
852 * @param source the source string
853 * @param start where to start copying the source string
854 * @param len length of segment to copy
855 * @param dest the destination string
856 * @param insert_at where to place the copied segment of source string
857 * @returns #FALSE if not enough memory
860 _dbus_string_copy_len (const DBusString *source,
866 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
867 _dbus_assert (len >= 0);
868 _dbus_assert ((start + len) <= real_source->len);
870 return copy (real_source, start, len,
875 /* Unicode macros from GLib */
877 /** computes length and mask of a unicode character
878 * @param Char the char
879 * @param Mask the mask variable to assign to
880 * @param Len the length variable to assign to
882 #define UTF8_COMPUTE(Char, Mask, Len) \
888 else if ((Char & 0xe0) == 0xc0) \
893 else if ((Char & 0xf0) == 0xe0) \
898 else if ((Char & 0xf8) == 0xf0) \
903 else if ((Char & 0xfc) == 0xf8) \
908 else if ((Char & 0xfe) == 0xfc) \
917 * computes length of a unicode character in UTF-8
918 * @param Char the char
920 #define UTF8_LENGTH(Char) \
921 ((Char) < 0x80 ? 1 : \
922 ((Char) < 0x800 ? 2 : \
923 ((Char) < 0x10000 ? 3 : \
924 ((Char) < 0x200000 ? 4 : \
925 ((Char) < 0x4000000 ? 5 : 6)))))
928 * Gets a UTF-8 value.
930 * @param Result variable for extracted unicode char.
931 * @param Chars the bytes to decode
932 * @param Count counter variable
933 * @param Mask mask for this char
934 * @param Len length for this char in bytes
936 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
937 (Result) = (Chars)[0] & (Mask); \
938 for ((Count) = 1; (Count) < (Len); ++(Count)) \
940 if (((Chars)[(Count)] & 0xc0) != 0x80) \
946 (Result) |= ((Chars)[(Count)] & 0x3f); \
950 * Check whether a unicode char is in a valid range.
952 * @param Char the character
954 #define UNICODE_VALID(Char) \
955 ((Char) < 0x110000 && \
956 ((Char) < 0xD800 || (Char) >= 0xE000) && \
957 (Char) != 0xFFFE && (Char) != 0xFFFF)
960 * Gets a unicode character from a UTF-8 string. Does no validation;
961 * you must verify that the string is valid UTF-8 in advance and must
962 * pass in the start of a character.
964 * @param str the string
965 * @param start the start of the UTF-8 character.
966 * @param ch_return location to return the character
967 * @param end_return location to return the byte index of next character
968 * @returns #TRUE on success, #FALSE otherwise.
971 _dbus_string_get_unichar (const DBusString *str,
973 dbus_unichar_t *ch_return,
977 dbus_unichar_t result;
980 DBUS_CONST_STRING_PREAMBLE (str);
985 *end_return = real->len;
988 p = real->str + start;
991 UTF8_COMPUTE (c, mask, len);
994 UTF8_GET (result, p, i, mask, len);
996 if (result == (dbus_unichar_t)-1)
1000 *ch_return = result;
1002 *end_return = start + len;
1007 #ifdef DBUS_BUILD_TESTS
1008 #include "dbus-test.h"
1012 test_max_len (DBusString *str,
1017 if (!_dbus_string_set_length (str, max_len - 1))
1018 _dbus_assert_not_reached ("setting len to one less than max should have worked");
1021 if (!_dbus_string_set_length (str, max_len))
1022 _dbus_assert_not_reached ("setting len to max len should have worked");
1024 if (_dbus_string_set_length (str, max_len + 1))
1025 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
1027 if (!_dbus_string_set_length (str, 0))
1028 _dbus_assert_not_reached ("setting len to zero should have worked");
1032 * @ingroup DBusStringInternals
1033 * Unit test for DBusString.
1035 * @todo Need to write tests for _dbus_string_copy() and
1036 * _dbus_string_move() moving to/from each of start/middle/end of a
1039 * @returns #TRUE on success.
1042 _dbus_string_test (void)
1049 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 };
1054 while (i < _DBUS_N_ELEMENTS (lens))
1056 if (!_dbus_string_init (&str, lens[i]))
1057 _dbus_assert_not_reached ("failed to init string");
1059 test_max_len (&str, lens[i]);
1060 _dbus_string_free (&str);
1065 /* Test shortening and setting length */
1067 while (i < _DBUS_N_ELEMENTS (lens))
1071 if (!_dbus_string_init (&str, lens[i]))
1072 _dbus_assert_not_reached ("failed to init string");
1074 if (!_dbus_string_set_length (&str, lens[i]))
1075 _dbus_assert_not_reached ("failed to set string length");
1080 _dbus_assert (_dbus_string_get_length (&str) == j);
1083 _dbus_string_shorten (&str, 1);
1084 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
1089 _dbus_string_free (&str);
1094 /* Test appending data */
1095 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1096 _dbus_assert_not_reached ("failed to init string");
1101 if (!_dbus_string_append (&str, "a"))
1102 _dbus_assert_not_reached ("failed to append string to string\n");
1104 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
1106 if (!_dbus_string_append_byte (&str, 'b'))
1107 _dbus_assert_not_reached ("failed to append byte to string\n");
1109 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
1114 _dbus_string_free (&str);
1116 /* Check steal_data */
1118 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1119 _dbus_assert_not_reached ("failed to init string");
1121 if (!_dbus_string_append (&str, "Hello World"))
1122 _dbus_assert_not_reached ("could not append to string");
1124 i = _dbus_string_get_length (&str);
1126 if (!_dbus_string_steal_data (&str, &s))
1127 _dbus_assert_not_reached ("failed to steal data");
1129 _dbus_assert (_dbus_string_get_length (&str) == 0);
1130 _dbus_assert (((int)strlen (s)) == i);
1136 if (!_dbus_string_append (&str, "Hello World"))
1137 _dbus_assert_not_reached ("could not append to string");
1139 i = _dbus_string_get_length (&str);
1141 if (!_dbus_string_init (&other, _DBUS_INT_MAX))
1142 _dbus_assert_not_reached ("could not init string");
1144 if (!_dbus_string_move (&str, 0, &other, 0))
1145 _dbus_assert_not_reached ("could not move");
1147 _dbus_assert (_dbus_string_get_length (&str) == 0);
1148 _dbus_assert (_dbus_string_get_length (&other) == i);
1150 _dbus_string_free (&other);
1154 if (!_dbus_string_append (&str, "Hello World"))
1155 _dbus_assert_not_reached ("could not append to string");
1157 i = _dbus_string_get_length (&str);
1159 if (!_dbus_string_init (&other, _DBUS_INT_MAX))
1160 _dbus_assert_not_reached ("could not init string");
1162 if (!_dbus_string_copy (&str, 0, &other, 0))
1163 _dbus_assert_not_reached ("could not copy");
1165 _dbus_assert (_dbus_string_get_length (&str) == i);
1166 _dbus_assert (_dbus_string_get_length (&other) == i);
1168 _dbus_string_free (&str);
1169 _dbus_string_free (&other);
1171 /* Check append/get unichar */
1173 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1174 _dbus_assert_not_reached ("failed to init string");
1177 if (!_dbus_string_append_unichar (&str, 0xfffc))
1178 _dbus_assert_not_reached ("failed to append unichar");
1180 _dbus_string_get_unichar (&str, 0, &ch, &i);
1182 _dbus_assert (ch == 0xfffc);
1183 _dbus_assert (i == _dbus_string_get_length (&str));
1185 _dbus_string_free (&str);
1187 /* Check append/parse int/double */
1189 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1190 _dbus_assert_not_reached ("failed to init string");
1192 if (!_dbus_string_append_int (&str, 27))
1193 _dbus_assert_not_reached ("failed to append int");
1195 i = _dbus_string_get_length (&str);
1197 if (!_dbus_string_parse_int (&str, 0, &v, &end))
1198 _dbus_assert_not_reached ("failed to parse int");
1200 _dbus_assert (v == 27);
1201 _dbus_assert (end == i);
1203 _dbus_string_set_length (&str, 0);
1205 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1206 _dbus_assert_not_reached ("failed to init string");
1208 if (!_dbus_string_append_double (&str, 50.3))
1209 _dbus_assert_not_reached ("failed to append float");
1211 i = _dbus_string_get_length (&str);
1213 if (!_dbus_string_parse_double (&str, 0, &d, &end))
1214 _dbus_assert_not_reached ("failed to parse float");
1216 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
1217 _dbus_assert (end == i);
1219 _dbus_string_free (&str);
1224 #endif /* DBUS_BUILD_TESTS */