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
143 * to the app like out of memory, and the app might try
144 * to "recover" - but recovery in this case is impossible,
145 * as we can't ever "get more memory" - so should delete the
146 * max length feature I think.
148 * @todo we could make this init routine not alloc any memory and
149 * return void, would simplify a lot of code, however it might
150 * complexify things elsewhere because _dbus_string_get_data()
151 * etc. could suddenly fail as they'd need to alloc new memory.
153 * @param str memory to hold the string
154 * @param max_length the maximum size of the string
155 * @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 DBUS_LOCKED_STRING_PREAMBLE (str);
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 * Gets the raw character buffer from the string. The returned buffer
304 * will be nul-terminated, but note that strings may contain binary
305 * data so there may be extra nul characters prior to the termination.
306 * This function should be little-used, extend DBusString or add
307 * stuff to dbus-sysdeps.c instead. It's an error to use this
308 * function on a const string.
310 * @param str the string
311 * @param data_return place to store the returned data
314 _dbus_string_get_data (DBusString *str,
317 DBUS_STRING_PREAMBLE (str);
318 _dbus_assert (data_return != NULL);
320 *data_return = real->str;
324 * Gets the raw character buffer from a const string.
326 * @param str the string
327 * @param data_return location to store returned data
330 _dbus_string_get_const_data (const DBusString *str,
331 const char **data_return)
333 DBUS_CONST_STRING_PREAMBLE (str);
334 _dbus_assert (data_return != NULL);
336 *data_return = real->str;
340 * Gets a sub-portion of the raw character buffer from the
341 * string. The "len" field is required simply for error
342 * checking, to be sure you don't try to use more
343 * string than exists. The nul termination of the
344 * returned buffer remains at the end of the entire
345 * string, not at start + len.
347 * @param str the string
348 * @param data_return location to return the buffer
349 * @param start byte offset to return
350 * @param len length of segment to return
353 _dbus_string_get_data_len (DBusString *str,
358 DBUS_STRING_PREAMBLE (str);
359 _dbus_assert (data_return != NULL);
360 _dbus_assert (start >= 0);
361 _dbus_assert (len >= 0);
362 _dbus_assert ((start + len) <= real->len);
364 *data_return = real->str + start;
368 * const version of _dbus_string_get_data_len().
370 * @param str the string
371 * @param data_return location to return the buffer
372 * @param start byte offset to return
373 * @param len length of segment to return
376 _dbus_string_get_const_data_len (const DBusString *str,
377 const char **data_return,
381 DBUS_CONST_STRING_PREAMBLE (str);
382 _dbus_assert (data_return != NULL);
383 _dbus_assert (start >= 0);
384 _dbus_assert (len >= 0);
385 _dbus_assert ((start + len) <= real->len);
387 *data_return = real->str + start;
391 * Gets the byte at the given position.
393 * @param str the string
394 * @param start the position
395 * @returns the byte at that position
398 _dbus_string_get_byte (const DBusString *str,
401 DBUS_CONST_STRING_PREAMBLE (str);
402 _dbus_assert (start < real->len);
404 return real->str[start];
408 * Like _dbus_string_get_data(), but removes the
409 * gotten data from the original string. The caller
410 * must free the data returned. This function may
411 * fail due to lack of memory, and return #FALSE.
413 * @param str the string
414 * @param data_return location to return the buffer
415 * @returns #TRUE on success
418 _dbus_string_steal_data (DBusString *str,
421 DBUS_STRING_PREAMBLE (str);
422 _dbus_assert (data_return != NULL);
424 *data_return = real->str;
426 /* reset the string */
427 if (!_dbus_string_init (str, real->max_length))
429 /* hrm, put it back then */
430 real->str = *data_return;
439 * Like _dbus_string_get_data_len(), but removes the gotten data from
440 * the original string. The caller must free the data returned. This
441 * function may fail due to lack of memory, and return #FALSE.
442 * The returned string is nul-terminated and has length len.
444 * @param str the string
445 * @param data_return location to return the buffer
446 * @param start the start of segment to steal
447 * @param len the length of segment to steal
448 * @returns #TRUE on success
451 _dbus_string_steal_data_len (DBusString *str,
458 DBUS_STRING_PREAMBLE (str);
459 _dbus_assert (data_return != NULL);
460 _dbus_assert (start >= 0);
461 _dbus_assert (len >= 0);
462 _dbus_assert ((start + len) <= real->len);
464 if (!_dbus_string_init (&dest, real->max_length))
467 if (!_dbus_string_move_len (str, start, len, &dest, 0))
469 _dbus_string_free (&dest);
473 if (!_dbus_string_steal_data (&dest, data_return))
475 _dbus_string_free (&dest);
479 _dbus_string_free (&dest);
484 * Gets the length of a string (not including nul termination).
486 * @returns the length.
489 _dbus_string_get_length (const DBusString *str)
491 DBUS_CONST_STRING_PREAMBLE (str);
497 set_length (DBusRealString *real,
500 /* Note, we are setting the length without nul termination */
502 /* exceeding max length is the same as failure to allocate memory */
503 if (new_length > real->max_length)
506 while (new_length >= real->allocated)
511 new_allocated = 2 + real->allocated * 2;
512 if (new_allocated < real->allocated)
513 return FALSE; /* overflow */
515 new_str = dbus_realloc (real->str, new_allocated);
520 real->allocated = new_allocated;
522 ASSERT_8_BYTE_ALIGNED (real);
525 real->len = new_length;
526 real->str[real->len] = '\0';
532 * Makes a string longer by the given number of bytes. Checks whether
533 * adding additional_length to the current length would overflow an
534 * integer, and checks for exceeding a string's max length.
535 * The new bytes are not initialized, other than nul-terminating
536 * the end of the string. The uninitialized bytes may contain
537 * unexpected nul bytes or other junk.
539 * @param str a string
540 * @param additional_length length to add to the string.
541 * @returns #TRUE on success.
544 _dbus_string_lengthen (DBusString *str,
545 int additional_length)
547 DBUS_STRING_PREAMBLE (str);
548 _dbus_assert (additional_length >= 0);
550 if ((real->len + additional_length) < real->len)
551 return FALSE; /* overflow */
553 return set_length (real,
554 real->len + additional_length);
558 * Makes a string shorter by the given number of bytes.
560 * @param str a string
561 * @param length_to_remove length to remove from the string.
564 _dbus_string_shorten (DBusString *str,
565 int length_to_remove)
567 DBUS_STRING_PREAMBLE (str);
568 _dbus_assert (length_to_remove >= 0);
569 _dbus_assert (length_to_remove <= real->len);
572 real->len - length_to_remove);
576 * Sets the length of a string. Can be used to truncate or lengthen
577 * the string. If the string is lengthened, the function may fail and
578 * return #FALSE. Newly-added bytes are not initialized, as with
579 * _dbus_string_lengthen().
581 * @param str a string
582 * @param length new length of the string.
583 * @returns #FALSE on failure.
586 _dbus_string_set_length (DBusString *str,
589 DBUS_STRING_PREAMBLE (str);
590 _dbus_assert (length >= 0);
592 return set_length (real, length);
596 * Align the length of a string to a specific alignment (typically 4 or 8)
597 * by appending nul bytes to the string.
599 * @param str a string
600 * @param alignment the alignment
601 * @returns #FALSE if no memory
604 _dbus_string_align_length (DBusString *str,
609 DBUS_STRING_PREAMBLE (str);
610 _dbus_assert (alignment >= 1);
611 _dbus_assert (alignment <= 16); /* arbitrary */
613 new_len = _DBUS_ALIGN_VALUE (real->len, alignment);
615 delta = new_len - real->len;
616 _dbus_assert (delta >= 0);
621 if (!set_length (real, new_len))
624 memset (real->str + (new_len - delta),
631 append (DBusRealString *real,
638 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
641 memcpy (real->str + (real->len - buffer_len),
649 * Appends a nul-terminated C-style string to a DBusString.
651 * @param str the DBusString
652 * @param buffer the nul-terminated characters to append
653 * @returns #FALSE if not enough memory.
656 _dbus_string_append (DBusString *str,
661 DBUS_STRING_PREAMBLE (str);
662 _dbus_assert (buffer != NULL);
664 buffer_len = strlen (buffer);
666 return append (real, buffer, buffer_len);
670 * Appends block of bytes with the given length to a DBusString.
672 * @param str the DBusString
673 * @param buffer the bytes to append
674 * @param len the number of bytes to append
675 * @returns #FALSE if not enough memory.
678 _dbus_string_append_len (DBusString *str,
682 DBUS_STRING_PREAMBLE (str);
683 _dbus_assert (buffer != NULL);
684 _dbus_assert (len >= 0);
686 return append (real, buffer, len);
690 * Appends a single byte to the string, returning #FALSE
691 * if not enough memory.
693 * @param str the string
694 * @param byte the byte to append
695 * @returns #TRUE on success
698 _dbus_string_append_byte (DBusString *str,
701 DBUS_STRING_PREAMBLE (str);
703 if (!set_length (real, real->len + 1))
706 real->str[real->len-1] = byte;
712 * Appends a single Unicode character, encoding the character
715 * @param str the string
716 * @param ch the Unicode character
719 _dbus_string_append_unichar (DBusString *str,
727 DBUS_STRING_PREAMBLE (str);
729 /* this code is from GLib but is pretty standard I think */
743 else if (ch < 0x10000)
748 else if (ch < 0x200000)
753 else if (ch < 0x4000000)
764 if (!set_length (real, real->len + len))
767 out = real->str + (real->len - len);
769 for (i = len - 1; i > 0; --i)
771 out[i] = (ch & 0x3f) | 0x80;
780 delete (DBusRealString *real,
787 memmove (real->str + start, real->str + start + len, real->len - (start + len));
789 real->str[real->len] = '\0';
793 * Deletes a segment of a DBusString with length len starting at
794 * start. (Hint: to clear an entire string, setting length to 0
795 * with _dbus_string_set_length() is easier.)
797 * @param str the DBusString
798 * @param start where to start deleting
799 * @param len the number of bytes to delete
802 _dbus_string_delete (DBusString *str,
806 DBUS_STRING_PREAMBLE (str);
807 _dbus_assert (start >= 0);
808 _dbus_assert (len >= 0);
809 _dbus_assert ((start + len) <= real->len);
811 delete (real, start, len);
816 DBusRealString *dest,
822 if (!set_length (dest, dest->len + len))
825 memmove (dest->str + insert_at + len,
826 dest->str + insert_at,
827 dest->len - len - insert_at);
833 copy (DBusRealString *source,
836 DBusRealString *dest,
842 if (!open_gap (len, dest, insert_at))
845 memcpy (dest->str + insert_at,
853 * Checks assertions for two strings we're copying a segment between,
854 * and declares real_source/real_dest variables.
856 * @param source the source string
857 * @param start the starting offset
858 * @param dest the dest string
859 * @param insert_at where the copied segment is inserted
861 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
862 DBusRealString *real_source = (DBusRealString*) source; \
863 DBusRealString *real_dest = (DBusRealString*) dest; \
864 _dbus_assert ((source) != (dest)); \
865 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
866 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
867 _dbus_assert (!real_source->constant); \
868 _dbus_assert (!real_source->locked); \
869 _dbus_assert (!real_dest->constant); \
870 _dbus_assert (!real_dest->locked); \
871 _dbus_assert ((start) >= 0); \
872 _dbus_assert ((start) <= real_source->len); \
873 _dbus_assert ((insert_at) >= 0); \
874 _dbus_assert ((insert_at) <= real_dest->len)
877 * Moves the end of one string into another string. Both strings
878 * must be initialized, valid strings.
880 * @param source the source string
881 * @param start where to chop off the source string
882 * @param dest the destination string
883 * @param insert_at where to move the chopped-off part of source string
884 * @returns #FALSE if not enough memory
887 _dbus_string_move (DBusString *source,
892 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
894 if (!copy (real_source, start,
895 real_source->len - start,
900 delete (real_source, start,
901 real_source->len - start);
907 * Like _dbus_string_move(), but does not delete the section
908 * of the source string that's copied to the dest string.
910 * @param source the source string
911 * @param start where to start copying the source string
912 * @param dest the destination string
913 * @param insert_at where to place the copied part of source string
914 * @returns #FALSE if not enough memory
917 _dbus_string_copy (const DBusString *source,
922 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
924 return copy (real_source, start,
925 real_source->len - start,
931 * Like _dbus_string_move(), but can move a segment from
932 * the middle of the source string.
934 * @param source the source string
935 * @param start first byte of source string to move
936 * @param len length of segment to move
937 * @param dest the destination string
938 * @param insert_at where to move the bytes from the source string
939 * @returns #FALSE if not enough memory
942 _dbus_string_move_len (DBusString *source,
949 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
950 _dbus_assert (len >= 0);
951 _dbus_assert ((start + len) <= real_source->len);
953 if (!copy (real_source, start, len,
958 delete (real_source, start,
965 * Like _dbus_string_copy(), but can copy a segment from the middle of
968 * @param source the source string
969 * @param start where to start copying the source string
970 * @param len length of segment to copy
971 * @param dest the destination string
972 * @param insert_at where to place the copied segment of source string
973 * @returns #FALSE if not enough memory
976 _dbus_string_copy_len (const DBusString *source,
982 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
983 _dbus_assert (len >= 0);
984 _dbus_assert ((start + len) <= real_source->len);
986 return copy (real_source, start, len,
992 * Replaces a segment of dest string with a segment of source string.
994 * @todo optimize the case where the two lengths are the same, and
995 * avoid memmoving the data in the trailing part of the string twice.
997 * @param source the source string
998 * @param start where to start copying the source string
999 * @param len length of segment to copy
1000 * @param dest the destination string
1001 * @param replace_at start of segment of dest string to replace
1002 * @param replace_len length of segment of dest string to replace
1003 * @returns #FALSE if not enough memory
1007 _dbus_string_replace_len (const DBusString *source,
1014 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1015 _dbus_assert (len >= 0);
1016 _dbus_assert ((start + len) <= real_source->len);
1017 _dbus_assert (replace_at >= 0);
1018 _dbus_assert ((replace_at + replace_len) <= real_dest->len);
1020 if (!copy (real_source, start, len,
1021 real_dest, replace_at))
1024 delete (real_dest, replace_at + len, replace_len);
1029 /* Unicode macros from GLib */
1031 /** computes length and mask of a unicode character
1032 * @param Char the char
1033 * @param Mask the mask variable to assign to
1034 * @param Len the length variable to assign to
1036 #define UTF8_COMPUTE(Char, Mask, Len) \
1042 else if ((Char & 0xe0) == 0xc0) \
1047 else if ((Char & 0xf0) == 0xe0) \
1052 else if ((Char & 0xf8) == 0xf0) \
1057 else if ((Char & 0xfc) == 0xf8) \
1062 else if ((Char & 0xfe) == 0xfc) \
1071 * computes length of a unicode character in UTF-8
1072 * @param Char the char
1074 #define UTF8_LENGTH(Char) \
1075 ((Char) < 0x80 ? 1 : \
1076 ((Char) < 0x800 ? 2 : \
1077 ((Char) < 0x10000 ? 3 : \
1078 ((Char) < 0x200000 ? 4 : \
1079 ((Char) < 0x4000000 ? 5 : 6)))))
1082 * Gets a UTF-8 value.
1084 * @param Result variable for extracted unicode char.
1085 * @param Chars the bytes to decode
1086 * @param Count counter variable
1087 * @param Mask mask for this char
1088 * @param Len length for this char in bytes
1090 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1091 (Result) = (Chars)[0] & (Mask); \
1092 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1094 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1100 (Result) |= ((Chars)[(Count)] & 0x3f); \
1104 * Check whether a unicode char is in a valid range.
1106 * @param Char the character
1108 #define UNICODE_VALID(Char) \
1109 ((Char) < 0x110000 && \
1110 ((Char) < 0xD800 || (Char) >= 0xE000) && \
1111 (Char) != 0xFFFE && (Char) != 0xFFFF)
1114 * Gets a unicode character from a UTF-8 string. Does no validation;
1115 * you must verify that the string is valid UTF-8 in advance and must
1116 * pass in the start of a character.
1118 * @param str the string
1119 * @param start the start of the UTF-8 character.
1120 * @param ch_return location to return the character
1121 * @param end_return location to return the byte index of next character
1122 * @returns #TRUE on success, #FALSE otherwise.
1125 _dbus_string_get_unichar (const DBusString *str,
1127 dbus_unichar_t *ch_return,
1131 dbus_unichar_t result;
1134 DBUS_CONST_STRING_PREAMBLE (str);
1139 *end_return = real->len;
1142 p = real->str + start;
1145 UTF8_COMPUTE (c, mask, len);
1148 UTF8_GET (result, p, i, mask, len);
1150 if (result == (dbus_unichar_t)-1)
1154 *ch_return = result;
1156 *end_return = start + len;
1160 * Finds the given substring in the string,
1161 * returning #TRUE and filling in the byte index
1162 * where the substring was found, if it was found.
1163 * Returns #FALSE if the substring wasn't found.
1164 * Sets *start to the length of the string if the substring
1167 * @param str the string
1168 * @param start where to start looking
1169 * @param substr the substring
1170 * @param found return location for where it was found, or #NULL
1171 * @returns #TRUE if found
1174 _dbus_string_find (const DBusString *str,
1180 DBUS_CONST_STRING_PREAMBLE (str);
1181 _dbus_assert (substr != NULL);
1182 _dbus_assert (start <= real->len);
1184 /* we always "find" an empty string */
1185 if (*substr == '\0')
1193 while (i < real->len)
1195 if (real->str[i] == substr[0])
1199 while (j < real->len)
1201 if (substr[j - i] == '\0')
1203 else if (real->str[j] != substr[j - i])
1209 if (substr[j - i] == '\0')
1227 * Finds a blank (space or tab) in the string. Returns #TRUE
1228 * if found, #FALSE otherwise. If a blank is not found sets
1229 * *found to the length of the string.
1231 * @param str the string
1232 * @param start byte index to start looking
1233 * @param found place to store the location of the first blank
1234 * @returns #TRUE if a blank was found
1237 _dbus_string_find_blank (const DBusString *str,
1242 DBUS_CONST_STRING_PREAMBLE (str);
1243 _dbus_assert (start <= real->len);
1246 while (i < real->len)
1248 if (real->str[i] == ' ' ||
1249 real->str[i] == '\t')
1266 * Skips blanks from start, storing the first non-blank in *end
1268 * @param str the string
1269 * @param start where to start
1270 * @param end where to store the first non-blank byte index
1273 _dbus_string_skip_blank (const DBusString *str,
1278 DBUS_CONST_STRING_PREAMBLE (str);
1279 _dbus_assert (start <= real->len);
1282 while (i < real->len)
1284 if (!(real->str[i] == ' ' ||
1285 real->str[i] == '\t'))
1291 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1292 real->str[i] == '\t'));
1299 * Tests two DBusString for equality.
1301 * @param a first string
1302 * @param b second string
1303 * @returns #TRUE if equal
1306 _dbus_string_equal (const DBusString *a,
1307 const DBusString *b)
1309 const unsigned char *ap;
1310 const unsigned char *bp;
1311 const unsigned char *a_end;
1312 const DBusRealString *real_a = (const DBusRealString*) a;
1313 const DBusRealString *real_b = (const DBusRealString*) b;
1314 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1315 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1317 if (real_a->len != real_b->len)
1322 a_end = real_a->str + real_a->len;
1336 * Checks whether a string is equal to a C string.
1338 * @param a the string
1339 * @param c_str the C string
1340 * @returns #TRUE if equal
1343 _dbus_string_equal_c_str (const DBusString *a,
1346 const unsigned char *ap;
1347 const unsigned char *bp;
1348 const unsigned char *a_end;
1349 const DBusRealString *real_a = (const DBusRealString*) a;
1350 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1353 bp = (const unsigned char*) c_str;
1354 a_end = real_a->str + real_a->len;
1355 while (ap != a_end && *bp)
1364 if (*ap && *bp == '\0')
1366 else if (ap == a_end && *bp)
1372 static const signed char base64_table[] = {
1439 /** The minimum char that's a valid char in Base64-encoded text */
1440 #define UNBASE64_MIN_CHAR (43)
1441 /** The maximum char that's a valid char in Base64-encoded text */
1442 #define UNBASE64_MAX_CHAR (122)
1443 /** Must subtract this from a char's integer value before offsetting
1444 * into unbase64_table
1446 #define UNBASE64_TABLE_OFFSET UNBASE64_MIN_CHAR
1447 static const signed char unbase64_table[] = {
1531 * Encodes a string using Base64, as documented in RFC 2045.
1533 * @param source the string to encode
1534 * @param start byte index to start encoding
1535 * @param dest string where encoded data should be placed
1536 * @param insert_at where to place encoded data
1537 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
1540 _dbus_string_base64_encode (const DBusString *source,
1547 const unsigned char *s;
1549 const unsigned char *triplet_end;
1550 const unsigned char *final_end;
1551 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1552 _dbus_assert (source != dest);
1554 /* For each 24 bits (3 bytes) of input, we have 4 chars of
1557 source_len = real_source->len - start;
1558 dest_len = (source_len / 3) * 4;
1559 if (source_len % 3 != 0)
1562 if (source_len == 0)
1565 if (!open_gap (dest_len, real_dest, insert_at))
1568 d = real_dest->str + insert_at;
1569 s = real_source->str + start;
1570 final_end = real_source->str + (start + source_len);
1571 triplet_end = final_end - (source_len % 3);
1572 _dbus_assert (triplet_end <= final_end);
1573 _dbus_assert ((final_end - triplet_end) < 3);
1575 #define ENCODE_64(v) (base64_table[ (unsigned char) (v) ])
1576 #define SIX_BITS_MASK (0x3f)
1577 _dbus_assert (SIX_BITS_MASK < _DBUS_N_ELEMENTS (base64_table));
1579 while (s != triplet_end)
1581 unsigned int triplet;
1583 triplet = s[0] | (s[1] << 8) | (s[2] << 16);
1585 /* Encode each 6 bits */
1587 *d++ = ENCODE_64 (triplet >> 18);
1588 *d++ = ENCODE_64 ((triplet >> 12) & SIX_BITS_MASK);
1589 *d++ = ENCODE_64 ((triplet >> 6) & SIX_BITS_MASK);
1590 *d++ = ENCODE_64 (triplet & SIX_BITS_MASK);
1595 switch (final_end - triplet_end)
1599 unsigned int doublet;
1601 doublet = s[0] | (s[1] << 8);
1603 *d++ = ENCODE_64 (doublet >> 12);
1604 *d++ = ENCODE_64 ((doublet >> 6) & SIX_BITS_MASK);
1605 *d++ = ENCODE_64 (doublet & SIX_BITS_MASK);
1611 unsigned int singlet;
1615 *d++ = ENCODE_64 ((singlet >> 6) & SIX_BITS_MASK);
1616 *d++ = ENCODE_64 (singlet & SIX_BITS_MASK);
1625 _dbus_assert (d == (real_dest->str + (insert_at + dest_len)));
1632 * Decodes a string from Base64, as documented in RFC 2045.
1634 * @param source the string to decode
1635 * @param start byte index to start decode
1636 * @param dest string where decoded data should be placed
1637 * @param insert_at where to place decoded data
1638 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
1641 _dbus_string_base64_decode (const DBusString *source,
1650 unsigned int triplet = 0;
1653 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1654 _dbus_assert (source != dest);
1656 source_len = real_source->len - start;
1657 s = real_source->str + start;
1658 end = real_source->str + source_len;
1660 if (source_len == 0)
1663 if (!_dbus_string_init (&result, _DBUS_INT_MAX))
1670 /* The idea is to just skip anything that isn't
1671 * a base64 char - it's allowed to have whitespace,
1672 * newlines, etc. in here. We also ignore trailing
1673 * base64 chars, though that's suspicious.
1676 if (*s >= UNBASE64_MIN_CHAR &&
1677 *s <= UNBASE64_MAX_CHAR)
1681 /* '=' is padding, doesn't represent additional data
1682 * but does increment our count.
1691 val = unbase64_table[(*s) - UNBASE64_TABLE_OFFSET];
1696 triplet |= (unsigned int) val;
1701 if (sextet_count == 4)
1703 /* no pad = 3 bytes, 1 pad = 2 bytes, 2 pad = 1 byte */
1705 _dbus_string_append_byte (&result,
1709 _dbus_string_append_byte (&result,
1710 (triplet >> 8) & 0xff);
1713 _dbus_string_append_byte (&result,
1725 if (!_dbus_string_move (&result, 0, dest, insert_at))
1727 _dbus_string_free (&result);
1731 _dbus_string_free (&result);
1737 * Checks that the given range of the string
1738 * is valid ASCII. If the given range is not contained
1739 * in the string, returns #FALSE.
1741 * @param str the string
1742 * @param start first byte index to check
1743 * @param len number of bytes to check
1744 * @returns #TRUE if the byte range exists and is all valid ASCII
1747 _dbus_string_validate_ascii (const DBusString *str,
1751 const unsigned char *s;
1752 const unsigned char *end;
1753 DBUS_CONST_STRING_PREAMBLE (str);
1754 _dbus_assert (start >= 0);
1755 _dbus_assert (len >= 0);
1757 if ((start + len) > real->len)
1760 s = real->str + start;
1765 ((*s & ~0x7f) != 0))
1776 #ifdef DBUS_BUILD_TESTS
1777 #include "dbus-test.h"
1781 test_max_len (DBusString *str,
1786 if (!_dbus_string_set_length (str, max_len - 1))
1787 _dbus_assert_not_reached ("setting len to one less than max should have worked");
1790 if (!_dbus_string_set_length (str, max_len))
1791 _dbus_assert_not_reached ("setting len to max len should have worked");
1793 if (_dbus_string_set_length (str, max_len + 1))
1794 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
1796 if (!_dbus_string_set_length (str, 0))
1797 _dbus_assert_not_reached ("setting len to zero should have worked");
1801 test_base64_roundtrip (const unsigned char *data,
1809 len = strlen (data);
1811 if (!_dbus_string_init (&orig, _DBUS_INT_MAX))
1812 _dbus_assert_not_reached ("could not init string");
1814 if (!_dbus_string_init (&encoded, _DBUS_INT_MAX))
1815 _dbus_assert_not_reached ("could not init string");
1817 if (!_dbus_string_init (&decoded, _DBUS_INT_MAX))
1818 _dbus_assert_not_reached ("could not init string");
1820 if (!_dbus_string_append_len (&orig, data, len))
1821 _dbus_assert_not_reached ("couldn't append orig data");
1823 if (!_dbus_string_base64_encode (&orig, 0, &encoded, 0))
1824 _dbus_assert_not_reached ("could not encode");
1826 if (!_dbus_string_base64_decode (&encoded, 0, &decoded, 0))
1827 _dbus_assert_not_reached ("could not decode");
1829 if (!_dbus_string_equal (&orig, &decoded))
1833 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
1834 _dbus_string_get_length (&orig),
1835 _dbus_string_get_length (&encoded),
1836 _dbus_string_get_length (&decoded));
1837 printf ("Original: %s\n", data);
1838 _dbus_string_get_const_data (&decoded, &s);
1839 printf ("Decoded: %s\n", s);
1840 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
1843 _dbus_string_free (&orig);
1844 _dbus_string_free (&encoded);
1845 _dbus_string_free (&decoded);
1849 * @ingroup DBusStringInternals
1850 * Unit test for DBusString.
1852 * @todo Need to write tests for _dbus_string_copy() and
1853 * _dbus_string_move() moving to/from each of start/middle/end of a
1854 * string. Also need tests for _dbus_string_move_len ()
1856 * @returns #TRUE on success.
1859 _dbus_string_test (void)
1866 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 };
1871 while (i < _DBUS_N_ELEMENTS (lens))
1873 if (!_dbus_string_init (&str, lens[i]))
1874 _dbus_assert_not_reached ("failed to init string");
1876 test_max_len (&str, lens[i]);
1877 _dbus_string_free (&str);
1882 /* Test shortening and setting length */
1884 while (i < _DBUS_N_ELEMENTS (lens))
1888 if (!_dbus_string_init (&str, lens[i]))
1889 _dbus_assert_not_reached ("failed to init string");
1891 if (!_dbus_string_set_length (&str, lens[i]))
1892 _dbus_assert_not_reached ("failed to set string length");
1897 _dbus_assert (_dbus_string_get_length (&str) == j);
1900 _dbus_string_shorten (&str, 1);
1901 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
1906 _dbus_string_free (&str);
1911 /* Test appending data */
1912 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1913 _dbus_assert_not_reached ("failed to init string");
1918 if (!_dbus_string_append (&str, "a"))
1919 _dbus_assert_not_reached ("failed to append string to string\n");
1921 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
1923 if (!_dbus_string_append_byte (&str, 'b'))
1924 _dbus_assert_not_reached ("failed to append byte to string\n");
1926 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
1931 _dbus_string_free (&str);
1933 /* Check steal_data */
1935 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1936 _dbus_assert_not_reached ("failed to init string");
1938 if (!_dbus_string_append (&str, "Hello World"))
1939 _dbus_assert_not_reached ("could not append to string");
1941 i = _dbus_string_get_length (&str);
1943 if (!_dbus_string_steal_data (&str, &s))
1944 _dbus_assert_not_reached ("failed to steal data");
1946 _dbus_assert (_dbus_string_get_length (&str) == 0);
1947 _dbus_assert (((int)strlen (s)) == i);
1953 if (!_dbus_string_append (&str, "Hello World"))
1954 _dbus_assert_not_reached ("could not append to string");
1956 i = _dbus_string_get_length (&str);
1958 if (!_dbus_string_init (&other, _DBUS_INT_MAX))
1959 _dbus_assert_not_reached ("could not init string");
1961 if (!_dbus_string_move (&str, 0, &other, 0))
1962 _dbus_assert_not_reached ("could not move");
1964 _dbus_assert (_dbus_string_get_length (&str) == 0);
1965 _dbus_assert (_dbus_string_get_length (&other) == i);
1967 if (!_dbus_string_append (&str, "Hello World"))
1968 _dbus_assert_not_reached ("could not append to string");
1970 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
1971 _dbus_assert_not_reached ("could not move");
1973 _dbus_assert (_dbus_string_get_length (&str) == 0);
1974 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
1976 if (!_dbus_string_append (&str, "Hello World"))
1977 _dbus_assert_not_reached ("could not append to string");
1979 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
1980 _dbus_assert_not_reached ("could not move");
1982 _dbus_assert (_dbus_string_get_length (&str) == 0);
1983 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
1985 _dbus_string_free (&other);
1989 if (!_dbus_string_append (&str, "Hello World"))
1990 _dbus_assert_not_reached ("could not append to string");
1992 i = _dbus_string_get_length (&str);
1994 if (!_dbus_string_init (&other, _DBUS_INT_MAX))
1995 _dbus_assert_not_reached ("could not init string");
1997 if (!_dbus_string_copy (&str, 0, &other, 0))
1998 _dbus_assert_not_reached ("could not copy");
2000 _dbus_assert (_dbus_string_get_length (&str) == i);
2001 _dbus_assert (_dbus_string_get_length (&other) == i);
2003 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
2004 _dbus_assert_not_reached ("could not copy");
2006 _dbus_assert (_dbus_string_get_length (&str) == i);
2007 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
2008 _dbus_assert (_dbus_string_equal_c_str (&other,
2009 "Hello WorldHello World"));
2011 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
2012 _dbus_assert_not_reached ("could not copy");
2014 _dbus_assert (_dbus_string_get_length (&str) == i);
2015 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
2016 _dbus_assert (_dbus_string_equal_c_str (&other,
2017 "Hello WorldHello WorldHello World"));
2019 _dbus_string_free (&str);
2020 _dbus_string_free (&other);
2024 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2025 _dbus_assert_not_reached ("failed to init string");
2027 if (!_dbus_string_append (&str, "Hello World"))
2028 _dbus_assert_not_reached ("could not append to string");
2030 i = _dbus_string_get_length (&str);
2032 if (!_dbus_string_init (&other, _DBUS_INT_MAX))
2033 _dbus_assert_not_reached ("could not init string");
2035 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
2036 &other, 0, _dbus_string_get_length (&other)))
2037 _dbus_assert_not_reached ("could not replace");
2039 _dbus_assert (_dbus_string_get_length (&str) == i);
2040 _dbus_assert (_dbus_string_get_length (&other) == i);
2041 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
2043 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
2045 _dbus_assert_not_reached ("could not replace center space");
2047 _dbus_assert (_dbus_string_get_length (&str) == i);
2048 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
2049 _dbus_assert (_dbus_string_equal_c_str (&other,
2050 "HelloHello WorldWorld"));
2053 if (!_dbus_string_replace_len (&str, 1, 1,
2055 _dbus_string_get_length (&other) - 1,
2057 _dbus_assert_not_reached ("could not replace end character");
2059 _dbus_assert (_dbus_string_get_length (&str) == i);
2060 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
2061 _dbus_assert (_dbus_string_equal_c_str (&other,
2062 "HelloHello WorldWorle"));
2064 _dbus_string_free (&str);
2065 _dbus_string_free (&other);
2067 /* Check append/get unichar */
2069 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2070 _dbus_assert_not_reached ("failed to init string");
2073 if (!_dbus_string_append_unichar (&str, 0xfffc))
2074 _dbus_assert_not_reached ("failed to append unichar");
2076 _dbus_string_get_unichar (&str, 0, &ch, &i);
2078 _dbus_assert (ch == 0xfffc);
2079 _dbus_assert (i == _dbus_string_get_length (&str));
2081 _dbus_string_free (&str);
2083 /* Check append/parse int/double */
2085 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2086 _dbus_assert_not_reached ("failed to init string");
2088 if (!_dbus_string_append_int (&str, 27))
2089 _dbus_assert_not_reached ("failed to append int");
2091 i = _dbus_string_get_length (&str);
2093 if (!_dbus_string_parse_int (&str, 0, &v, &end))
2094 _dbus_assert_not_reached ("failed to parse int");
2096 _dbus_assert (v == 27);
2097 _dbus_assert (end == i);
2099 _dbus_string_free (&str);
2101 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2102 _dbus_assert_not_reached ("failed to init string");
2104 if (!_dbus_string_append_double (&str, 50.3))
2105 _dbus_assert_not_reached ("failed to append float");
2107 i = _dbus_string_get_length (&str);
2109 if (!_dbus_string_parse_double (&str, 0, &d, &end))
2110 _dbus_assert_not_reached ("failed to parse float");
2112 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
2113 _dbus_assert (end == i);
2115 _dbus_string_free (&str);
2118 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
2119 _dbus_assert_not_reached ("failed to init string");
2121 if (!_dbus_string_append (&str, "Hello"))
2122 _dbus_assert_not_reached ("couldn't append to string");
2124 if (!_dbus_string_find (&str, 0, "He", &i))
2125 _dbus_assert_not_reached ("didn't find 'He'");
2126 _dbus_assert (i == 0);
2128 if (!_dbus_string_find (&str, 0, "ello", &i))
2129 _dbus_assert_not_reached ("didn't find 'ello'");
2130 _dbus_assert (i == 1);
2132 if (!_dbus_string_find (&str, 0, "lo", &i))
2133 _dbus_assert_not_reached ("didn't find 'lo'");
2134 _dbus_assert (i == 3);
2136 if (!_dbus_string_find (&str, 2, "lo", &i))
2137 _dbus_assert_not_reached ("didn't find 'lo'");
2138 _dbus_assert (i == 3);
2140 if (_dbus_string_find (&str, 4, "lo", &i))
2141 _dbus_assert_not_reached ("did find 'lo'");
2143 if (!_dbus_string_find (&str, 0, "l", &i))
2144 _dbus_assert_not_reached ("didn't find 'l'");
2145 _dbus_assert (i == 2);
2147 if (!_dbus_string_find (&str, 0, "H", &i))
2148 _dbus_assert_not_reached ("didn't find 'H'");
2149 _dbus_assert (i == 0);
2151 if (!_dbus_string_find (&str, 0, "", &i))
2152 _dbus_assert_not_reached ("didn't find ''");
2153 _dbus_assert (i == 0);
2155 if (_dbus_string_find (&str, 0, "Hello!", NULL))
2156 _dbus_assert_not_reached ("Did find 'Hello!'");
2158 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
2159 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
2161 if (_dbus_string_find (&str, 0, "ill", NULL))
2162 _dbus_assert_not_reached ("Did find 'ill'");
2164 if (_dbus_string_find (&str, 0, "q", NULL))
2165 _dbus_assert_not_reached ("Did find 'q'");
2167 _dbus_string_free (&str);
2170 test_base64_roundtrip ("Hello this is a string\n", -1);
2171 test_base64_roundtrip ("Hello this is a string\n1", -1);
2172 test_base64_roundtrip ("Hello this is a string\n12", -1);
2173 test_base64_roundtrip ("Hello this is a string\n123", -1);
2174 test_base64_roundtrip ("Hello this is a string\n1234", -1);
2175 test_base64_roundtrip ("Hello this is a string\n12345", -1);
2176 test_base64_roundtrip ("", 0);
2177 test_base64_roundtrip ("1", 1);
2178 test_base64_roundtrip ("12", 2);
2179 test_base64_roundtrip ("123", 3);
2180 test_base64_roundtrip ("1234", 4);
2181 test_base64_roundtrip ("12345", 5);
2182 test_base64_roundtrip ("", 1);
2183 test_base64_roundtrip ("1", 2);
2184 test_base64_roundtrip ("12", 3);
2185 test_base64_roundtrip ("123", 4);
2186 test_base64_roundtrip ("1234", 5);
2187 test_base64_roundtrip ("12345", 6);
2189 unsigned char buf[512];
2191 while (i < _DBUS_N_ELEMENTS (buf))
2197 while (i < _DBUS_N_ELEMENTS (buf))
2199 test_base64_roundtrip (buf, i);
2207 #endif /* DBUS_BUILD_TESTS */