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 * @param str memory to hold the string
143 * @param max_length the maximum size of the string
144 * @returns #TRUE on success
147 _dbus_string_init (DBusString *str,
150 DBusRealString *real;
152 _dbus_assert (str != NULL);
153 _dbus_assert (max_length >= 0);
155 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
157 real = (DBusRealString*) str;
159 /* It's very important not to touch anything
160 * other than real->str if we're going to fail,
161 * since we also use this function to reset
162 * an existing string, e.g. in _dbus_string_steal_data()
165 #define INITIAL_ALLOC 2
167 real->str = dbus_malloc (INITIAL_ALLOC);
168 if (real->str == NULL)
171 real->allocated = INITIAL_ALLOC;
173 real->str[real->len] = '\0';
175 real->max_length = max_length;
176 real->constant = FALSE;
177 real->locked = FALSE;
178 real->invalid = FALSE;
180 ASSERT_8_BYTE_ALIGNED (str);
186 * Initializes a constant string. The value parameter is not copied
187 * (should be static), and the string may never be modified.
188 * It is safe but not necessary to call _dbus_string_free()
191 * @param str memory to use for the string
192 * @param value a string to be stored in str (not copied!!!)
195 _dbus_string_init_const (DBusString *str,
198 DBusRealString *real;
200 _dbus_assert (str != NULL);
201 _dbus_assert (value != NULL);
203 real = (DBusRealString*) str;
205 real->str = (char*) value;
206 real->len = strlen (real->str);
207 real->allocated = real->len;
208 real->max_length = real->len;
209 real->constant = TRUE;
210 real->invalid = FALSE;
212 /* We don't require const strings to be 8-byte aligned as the
213 * memory is coming from elsewhere.
218 * Frees a string created by _dbus_string_init().
220 * @param str memory where the string is stored.
223 _dbus_string_free (DBusString *str)
225 DBUS_LOCKED_STRING_PREAMBLE (str);
230 dbus_free (real->str);
232 real->invalid = TRUE;
236 * Locks a string such that any attempts to change the string
237 * will result in aborting the program. Also, if the string
238 * is wasting a lot of memory (allocation is larger than what
239 * the string is really using), _dbus_string_lock() will realloc
240 * the string's data to "compact" it.
242 * @param str the string to lock.
245 _dbus_string_lock (DBusString *str)
247 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
251 /* Try to realloc to avoid excess memory usage, since
252 * we know we won't change the string further
255 if (real->allocated > (real->len + MAX_WASTE))
260 new_allocated = real->len + 1;
262 new_str = dbus_realloc (real->str, new_allocated);
266 real->allocated = new_allocated;
267 ASSERT_8_BYTE_ALIGNED (str);
273 * Gets the raw character buffer from the string. The returned buffer
274 * will be nul-terminated, but note that strings may contain binary
275 * data so there may be extra nul characters prior to the termination.
276 * This function should be little-used, extend DBusString or add
277 * stuff to dbus-sysdeps.c instead. It's an error to use this
278 * function on a const string.
280 * @param str the string
281 * @param data_return place to store the returned data
284 _dbus_string_get_data (DBusString *str,
287 DBUS_STRING_PREAMBLE (str);
288 _dbus_assert (data_return != NULL);
290 *data_return = real->str;
294 * Gets the raw character buffer from a const string.
296 * @param str the string
297 * @param data_return location to store returned data
300 _dbus_string_get_const_data (const DBusString *str,
301 const char **data_return)
303 DBUS_CONST_STRING_PREAMBLE (str);
304 _dbus_assert (data_return != NULL);
306 *data_return = real->str;
310 * Gets a sub-portion of the raw character buffer from the
311 * string. The "len" field is required simply for error
312 * checking, to be sure you don't try to use more
313 * string than exists. The nul termination of the
314 * returned buffer remains at the end of the entire
315 * string, not at start + len.
317 * @param str the string
318 * @param data_return location to return the buffer
319 * @param start byte offset to return
320 * @param len length of segment to return
323 _dbus_string_get_data_len (DBusString *str,
328 DBUS_STRING_PREAMBLE (str);
329 _dbus_assert (data_return != NULL);
330 _dbus_assert (start >= 0);
331 _dbus_assert (len >= 0);
332 _dbus_assert ((start + len) <= real->len);
334 *data_return = real->str + start;
338 * const version of _dbus_string_get_data_len().
340 * @param str the string
341 * @param data_return location to return the buffer
342 * @param start byte offset to return
343 * @param len length of segment to return
346 _dbus_string_get_const_data_len (const DBusString *str,
347 const char **data_return,
351 DBUS_CONST_STRING_PREAMBLE (str);
352 _dbus_assert (data_return != NULL);
353 _dbus_assert (start >= 0);
354 _dbus_assert (len >= 0);
355 _dbus_assert ((start + len) <= real->len);
357 *data_return = real->str + start;
361 * Like _dbus_string_get_data(), but removes the
362 * gotten data from the original string. The caller
363 * must free the data returned. This function may
364 * fail due to lack of memory, and return #FALSE.
366 * @param str the string
367 * @param data_return location to return the buffer
368 * @returns #TRUE on success
371 _dbus_string_steal_data (DBusString *str,
374 DBUS_STRING_PREAMBLE (str);
375 _dbus_assert (data_return != NULL);
377 *data_return = real->str;
379 /* reset the string */
380 if (!_dbus_string_init (str, real->max_length))
382 /* hrm, put it back then */
383 real->str = *data_return;
392 * Like _dbus_string_get_data_len(), but removes the gotten data from
393 * the original string. The caller must free the data returned. This
394 * function may fail due to lack of memory, and return #FALSE.
395 * The returned string is nul-terminated and has length len.
397 * @param str the string
398 * @param data_return location to return the buffer
399 * @param start the start of segment to steal
400 * @param len the length of segment to steal
401 * @returns #TRUE on success
404 _dbus_string_steal_data_len (DBusString *str,
411 DBUS_STRING_PREAMBLE (str);
412 _dbus_assert (data_return != NULL);
413 _dbus_assert (start >= 0);
414 _dbus_assert (len >= 0);
415 _dbus_assert ((start + len) <= real->len);
417 if (!_dbus_string_init (&dest, real->max_length))
420 if (!_dbus_string_move_len (str, start, len, &dest, 0))
422 _dbus_string_free (&dest);
426 if (!_dbus_string_steal_data (&dest, data_return))
428 _dbus_string_free (&dest);
432 _dbus_string_free (&dest);
437 * Gets the length of a string (not including nul termination).
439 * @returns the length.
442 _dbus_string_get_length (const DBusString *str)
444 DBUS_CONST_STRING_PREAMBLE (str);
450 set_length (DBusRealString *real,
453 /* Note, we are setting the length without nul termination */
455 /* exceeding max length is the same as failure to allocate memory */
456 if (new_length > real->max_length)
459 while (new_length >= real->allocated)
464 new_allocated = 2 + real->allocated * 2;
465 if (new_allocated < real->allocated)
466 return FALSE; /* overflow */
468 new_str = dbus_realloc (real->str, new_allocated);
473 real->allocated = new_allocated;
475 ASSERT_8_BYTE_ALIGNED (real);
478 real->len = new_length;
479 real->str[real->len] = '\0';
485 * Makes a string longer by the given number of bytes. Checks whether
486 * adding additional_length to the current length would overflow an
487 * integer, and checks for exceeding a string's max length.
488 * The new bytes are not initialized, other than nul-terminating
489 * the end of the string. The uninitialized bytes may contain
490 * unexpected nul bytes or other junk.
492 * @param str a string
493 * @param additional_length length to add to the string.
494 * @returns #TRUE on success.
497 _dbus_string_lengthen (DBusString *str,
498 int additional_length)
500 DBUS_STRING_PREAMBLE (str);
501 _dbus_assert (additional_length >= 0);
503 if ((real->len + additional_length) < real->len)
504 return FALSE; /* overflow */
506 return set_length (real,
507 real->len + additional_length);
511 * Makes a string shorter by the given number of bytes.
513 * @param str a string
514 * @param length_to_remove length to remove from the string.
517 _dbus_string_shorten (DBusString *str,
518 int length_to_remove)
520 DBUS_STRING_PREAMBLE (str);
521 _dbus_assert (length_to_remove >= 0);
522 _dbus_assert (length_to_remove <= real->len);
525 real->len - length_to_remove);
529 * Sets the length of a string. Can be used to truncate or lengthen
530 * the string. If the string is lengthened, the function may fail and
531 * return #FALSE. Newly-added bytes are not initialized, as with
532 * _dbus_string_lengthen().
534 * @param str a string
535 * @param length new length of the string.
536 * @returns #FALSE on failure.
539 _dbus_string_set_length (DBusString *str,
542 DBUS_STRING_PREAMBLE (str);
543 _dbus_assert (length >= 0);
545 return set_length (real, length);
549 * Align the length of a string to a specific alignment (typically 4 or 8)
550 * by appending nul bytes to the string.
552 * @param str a string
553 * @param alignment the alignment
554 * @returns #FALSE if no memory
557 _dbus_string_align_length (DBusString *str,
562 DBUS_STRING_PREAMBLE (str);
563 _dbus_assert (alignment >= 1);
564 _dbus_assert (alignment <= 16); /* arbitrary */
566 new_len = _DBUS_ALIGN_VALUE (real->len, alignment);
568 delta = new_len - real->len;
569 _dbus_assert (delta >= 0);
574 if (!set_length (real, new_len))
577 memset (real->str + (new_len - delta),
584 append (DBusRealString *real,
591 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
594 memcpy (real->str + (real->len - buffer_len),
602 * Appends a nul-terminated C-style string to a DBusString.
604 * @param str the DBusString
605 * @param buffer the nul-terminated characters to append
606 * @returns #FALSE if not enough memory.
609 _dbus_string_append (DBusString *str,
614 DBUS_STRING_PREAMBLE (str);
615 _dbus_assert (buffer != NULL);
617 buffer_len = strlen (buffer);
619 return append (real, buffer, buffer_len);
623 * Appends block of bytes with the given length to a DBusString.
625 * @param str the DBusString
626 * @param buffer the bytes to append
627 * @param len the number of bytes to append
628 * @returns #FALSE if not enough memory.
631 _dbus_string_append_len (DBusString *str,
635 DBUS_STRING_PREAMBLE (str);
636 _dbus_assert (buffer != NULL);
637 _dbus_assert (len >= 0);
639 return append (real, buffer, len);
643 * Appends a single byte to the string, returning #FALSE
644 * if not enough memory.
646 * @param str the string
647 * @param byte the byte to append
648 * @returns #TRUE on success
651 _dbus_string_append_byte (DBusString *str,
654 DBUS_STRING_PREAMBLE (str);
656 if (!set_length (real, real->len + 1))
659 real->str[real->len-1] = byte;
665 * Appends a single Unicode character, encoding the character
668 * @param str the string
669 * @param ch the Unicode character
672 _dbus_string_append_unichar (DBusString *str,
680 DBUS_STRING_PREAMBLE (str);
682 /* this code is from GLib but is pretty standard I think */
696 else if (ch < 0x10000)
701 else if (ch < 0x200000)
706 else if (ch < 0x4000000)
717 if (!set_length (real, real->len + len))
720 out = real->str + (real->len - len);
722 for (i = len - 1; i > 0; --i)
724 out[i] = (ch & 0x3f) | 0x80;
733 delete (DBusRealString *real,
740 memmove (real->str + start, real->str + start + len, real->len - (start + len));
742 real->str[real->len] = '\0';
746 * Deletes a segment of a DBusString with length len starting at
747 * start. (Hint: to clear an entire string, setting length to 0
748 * with _dbus_string_set_length() is easier.)
750 * @param str the DBusString
751 * @param start where to start deleting
752 * @param len the number of bytes to delete
755 _dbus_string_delete (DBusString *str,
759 DBUS_STRING_PREAMBLE (str);
760 _dbus_assert (start >= 0);
761 _dbus_assert (len >= 0);
762 _dbus_assert ((start + len) <= real->len);
764 delete (real, start, len);
769 DBusRealString *dest,
775 if (!set_length (dest, dest->len + len))
778 memmove (dest->str + insert_at + len,
779 dest->str + insert_at,
780 dest->len - len - insert_at);
786 copy (DBusRealString *source,
789 DBusRealString *dest,
792 if (!open_gap (len, dest, insert_at))
795 memcpy (dest->str + insert_at,
803 * Checks assertions for two strings we're copying a segment between,
804 * and declares real_source/real_dest variables.
806 * @param source the source string
807 * @param start the starting offset
808 * @param dest the dest string
809 * @param insert_at where the copied segment is inserted
811 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
812 DBusRealString *real_source = (DBusRealString*) source; \
813 DBusRealString *real_dest = (DBusRealString*) dest; \
814 _dbus_assert ((source) != (dest)); \
815 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
816 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
817 _dbus_assert (!real_source->constant); \
818 _dbus_assert (!real_source->locked); \
819 _dbus_assert (!real_dest->constant); \
820 _dbus_assert (!real_dest->locked); \
821 _dbus_assert ((start) >= 0); \
822 _dbus_assert ((start) <= real_source->len); \
823 _dbus_assert ((insert_at) >= 0); \
824 _dbus_assert ((insert_at) <= real_dest->len)
827 * Moves the end of one string into another string. Both strings
828 * must be initialized, valid strings.
830 * @param source the source string
831 * @param start where to chop off the source string
832 * @param dest the destination string
833 * @param insert_at where to move the chopped-off part of source string
834 * @returns #FALSE if not enough memory
837 _dbus_string_move (DBusString *source,
842 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
844 if (!copy (real_source, start,
845 real_source->len - start,
850 delete (real_source, start,
851 real_source->len - start);
857 * Like _dbus_string_move(), but does not delete the section
858 * of the source string that's copied to the dest string.
860 * @param source the source string
861 * @param start where to start copying the source string
862 * @param dest the destination string
863 * @param insert_at where to place the copied part of source string
864 * @returns #FALSE if not enough memory
867 _dbus_string_copy (const DBusString *source,
872 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
874 return copy (real_source, start,
875 real_source->len - start,
881 * Like _dbus_string_move(), but can move a segment from
882 * the middle of the source string.
884 * @param source the source string
885 * @param start first byte of source string to move
886 * @param len length of segment to move
887 * @param dest the destination string
888 * @param insert_at where to move the bytes from the source string
889 * @returns #FALSE if not enough memory
892 _dbus_string_move_len (DBusString *source,
899 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
900 _dbus_assert (len >= 0);
901 _dbus_assert ((start + len) <= real_source->len);
903 if (!copy (real_source, start, len,
908 delete (real_source, start,
915 * Like _dbus_string_copy(), but can copy a segment from the middle of
918 * @param source the source string
919 * @param start where to start copying the source string
920 * @param len length of segment to copy
921 * @param dest the destination string
922 * @param insert_at where to place the copied segment of source string
923 * @returns #FALSE if not enough memory
926 _dbus_string_copy_len (const DBusString *source,
932 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
933 _dbus_assert (len >= 0);
934 _dbus_assert ((start + len) <= real_source->len);
936 return copy (real_source, start, len,
941 /* Unicode macros from GLib */
943 /** computes length and mask of a unicode character
944 * @param Char the char
945 * @param Mask the mask variable to assign to
946 * @param Len the length variable to assign to
948 #define UTF8_COMPUTE(Char, Mask, Len) \
954 else if ((Char & 0xe0) == 0xc0) \
959 else if ((Char & 0xf0) == 0xe0) \
964 else if ((Char & 0xf8) == 0xf0) \
969 else if ((Char & 0xfc) == 0xf8) \
974 else if ((Char & 0xfe) == 0xfc) \
983 * computes length of a unicode character in UTF-8
984 * @param Char the char
986 #define UTF8_LENGTH(Char) \
987 ((Char) < 0x80 ? 1 : \
988 ((Char) < 0x800 ? 2 : \
989 ((Char) < 0x10000 ? 3 : \
990 ((Char) < 0x200000 ? 4 : \
991 ((Char) < 0x4000000 ? 5 : 6)))))
994 * Gets a UTF-8 value.
996 * @param Result variable for extracted unicode char.
997 * @param Chars the bytes to decode
998 * @param Count counter variable
999 * @param Mask mask for this char
1000 * @param Len length for this char in bytes
1002 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1003 (Result) = (Chars)[0] & (Mask); \
1004 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1006 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1012 (Result) |= ((Chars)[(Count)] & 0x3f); \
1016 * Check whether a unicode char is in a valid range.
1018 * @param Char the character
1020 #define UNICODE_VALID(Char) \
1021 ((Char) < 0x110000 && \
1022 ((Char) < 0xD800 || (Char) >= 0xE000) && \
1023 (Char) != 0xFFFE && (Char) != 0xFFFF)
1026 * Gets a unicode character from a UTF-8 string. Does no validation;
1027 * you must verify that the string is valid UTF-8 in advance and must
1028 * pass in the start of a character.
1030 * @param str the string
1031 * @param start the start of the UTF-8 character.
1032 * @param ch_return location to return the character
1033 * @param end_return location to return the byte index of next character
1034 * @returns #TRUE on success, #FALSE otherwise.
1037 _dbus_string_get_unichar (const DBusString *str,
1039 dbus_unichar_t *ch_return,
1043 dbus_unichar_t result;
1046 DBUS_CONST_STRING_PREAMBLE (str);
1051 *end_return = real->len;
1054 p = real->str + start;
1057 UTF8_COMPUTE (c, mask, len);
1060 UTF8_GET (result, p, i, mask, len);
1062 if (result == (dbus_unichar_t)-1)
1066 *ch_return = result;
1068 *end_return = start + len;
1072 * Finds the given substring in the string,
1073 * returning #TRUE and filling in the byte index
1074 * where the substring was found, if it was found.
1075 * Returns #FALSE if the substring wasn't found.
1076 * Sets *start to the length of the string if the substring
1079 * @param str the string
1080 * @param start where to start looking
1081 * @param substr the substring
1082 * @param found return location for where it was found, or #NULL
1083 * @returns #TRUE if found
1086 _dbus_string_find (const DBusString *str,
1092 DBUS_CONST_STRING_PREAMBLE (str);
1093 _dbus_assert (substr != NULL);
1094 _dbus_assert (start <= real->len);
1096 /* we always "find" an empty string */
1097 if (*substr == '\0')
1105 while (i < real->len)
1107 if (real->str[i] == substr[0])
1111 while (j < real->len)
1113 if (substr[j - i] == '\0')
1115 else if (real->str[j] != substr[j - i])
1121 if (substr[j - i] == '\0')
1139 * Finds a blank (space or tab) in the string. Returns #TRUE
1140 * if found, #FALSE otherwise. If a blank is not found sets
1141 * *found to the length of the string.
1143 * @param str the string
1144 * @param start byte index to start looking
1145 * @param found place to store the location of the first blank
1146 * @returns #TRUE if a blank was found
1149 _dbus_string_find_blank (const DBusString *str,
1154 DBUS_CONST_STRING_PREAMBLE (str);
1155 _dbus_assert (start <= real->len);
1158 while (i < real->len)
1160 if (real->str[i] == ' ' ||
1161 real->str[i] == '\t')
1178 * Skips blanks from start, storing the first non-blank in *end
1180 * @param str the string
1181 * @param start where to start
1182 * @param end where to store the first non-blank byte index
1185 _dbus_string_skip_blank (const DBusString *str,
1190 DBUS_CONST_STRING_PREAMBLE (str);
1191 _dbus_assert (start <= real->len);
1194 while (i < real->len)
1196 if (!(real->str[i] == ' ' ||
1197 real->str[i] == '\t'))
1203 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1204 real->str[i] == '\t'));
1211 * Tests two DBusString for equality.
1213 * @param a first string
1214 * @param b second string
1215 * @returns #TRUE if equal
1218 _dbus_string_equal (const DBusString *a,
1219 const DBusString *b)
1221 const unsigned char *ap;
1222 const unsigned char *bp;
1223 const unsigned char *a_end;
1224 const DBusRealString *real_a = (const DBusRealString*) a;
1225 const DBusRealString *real_b = (const DBusRealString*) b;
1226 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1227 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1229 if (real_a->len != real_b->len)
1234 a_end = real_a->str + real_a->len;
1248 * Checks whether a string is equal to a C string.
1250 * @param a the string
1251 * @param c_str the C string
1252 * @returns #TRUE if equal
1255 _dbus_string_equal_c_str (const DBusString *a,
1258 const unsigned char *ap;
1259 const unsigned char *bp;
1260 const unsigned char *a_end;
1261 const DBusRealString *real_a = (const DBusRealString*) a;
1262 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1265 bp = (const unsigned char*) c_str;
1266 a_end = real_a->str + real_a->len;
1267 while (ap != a_end && *bp)
1276 if (*ap && *bp == '\0')
1278 else if (ap == a_end && *bp)
1284 static const signed char base64_table[] = {
1351 /** The minimum char that's a valid char in Base64-encoded text */
1352 #define UNBASE64_MIN_CHAR (43)
1353 /** The maximum char that's a valid char in Base64-encoded text */
1354 #define UNBASE64_MAX_CHAR (122)
1355 /** Must subtract this from a char's integer value before offsetting
1356 * into unbase64_table
1358 #define UNBASE64_TABLE_OFFSET UNBASE64_MIN_CHAR
1359 static const signed char unbase64_table[] = {
1443 * Encodes a string using Base64, as documented in RFC 2045.
1445 * @param source the string to encode
1446 * @param start byte index to start encoding
1447 * @param dest string where encoded data should be placed
1448 * @param insert_at where to place encoded data
1449 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
1452 _dbus_string_base64_encode (const DBusString *source,
1459 const unsigned char *s;
1461 const unsigned char *triplet_end;
1462 const unsigned char *final_end;
1463 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1464 _dbus_assert (source != dest);
1466 /* For each 24 bits (3 bytes) of input, we have 4 chars of
1469 source_len = real_source->len - start;
1470 dest_len = (source_len / 3) * 4;
1471 if (source_len % 3 != 0)
1474 if (source_len == 0)
1477 if (!open_gap (dest_len, real_dest, insert_at))
1480 d = real_dest->str + insert_at;
1481 s = real_source->str + start;
1482 final_end = real_source->str + (start + source_len);
1483 triplet_end = final_end - (source_len % 3);
1484 _dbus_assert (triplet_end <= final_end);
1485 _dbus_assert ((final_end - triplet_end) < 3);
1487 #define ENCODE_64(v) (base64_table[ (unsigned char) (v) ])
1488 #define SIX_BITS_MASK (0x3f)
1489 _dbus_assert (SIX_BITS_MASK < _DBUS_N_ELEMENTS (base64_table));
1491 while (s != triplet_end)
1493 unsigned int triplet;
1495 triplet = s[0] | (s[1] << 8) | (s[2] << 16);
1497 /* Encode each 6 bits */
1499 *d++ = ENCODE_64 (triplet >> 18);
1500 *d++ = ENCODE_64 ((triplet >> 12) & SIX_BITS_MASK);
1501 *d++ = ENCODE_64 ((triplet >> 6) & SIX_BITS_MASK);
1502 *d++ = ENCODE_64 (triplet & SIX_BITS_MASK);
1507 switch (final_end - triplet_end)
1511 unsigned int doublet;
1513 doublet = s[0] | (s[1] << 8);
1515 *d++ = ENCODE_64 (doublet >> 12);
1516 *d++ = ENCODE_64 ((doublet >> 6) & SIX_BITS_MASK);
1517 *d++ = ENCODE_64 (doublet & SIX_BITS_MASK);
1523 unsigned int singlet;
1527 *d++ = ENCODE_64 ((singlet >> 6) & SIX_BITS_MASK);
1528 *d++ = ENCODE_64 (singlet & SIX_BITS_MASK);
1537 _dbus_assert (d == (real_dest->str + (insert_at + dest_len)));
1544 * Decodes a string from Base64, as documented in RFC 2045.
1546 * @param source the string to decode
1547 * @param start byte index to start decode
1548 * @param dest string where decoded data should be placed
1549 * @param insert_at where to place decoded data
1550 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
1553 _dbus_string_base64_decode (const DBusString *source,
1562 unsigned int triplet = 0;
1565 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1566 _dbus_assert (source != dest);
1568 source_len = real_source->len - start;
1569 s = real_source->str + start;
1570 end = real_source->str + source_len;
1572 if (source_len == 0)
1575 if (!_dbus_string_init (&result, _DBUS_INT_MAX))
1582 /* The idea is to just skip anything that isn't
1583 * a base64 char - it's allowed to have whitespace,
1584 * newlines, etc. in here. We also ignore trailing
1585 * base64 chars, though that's suspicious.
1588 if (*s >= UNBASE64_MIN_CHAR &&
1589 *s <= UNBASE64_MAX_CHAR)
1593 /* '=' is padding, doesn't represent additional data
1594 * but does increment our count.
1603 val = unbase64_table[(*s) - UNBASE64_TABLE_OFFSET];
1608 triplet |= (unsigned int) val;
1613 if (sextet_count == 4)
1615 /* no pad = 3 bytes, 1 pad = 2 bytes, 2 pad = 1 byte */
1617 _dbus_string_append_byte (&result,
1621 _dbus_string_append_byte (&result,
1622 (triplet >> 8) & 0xff);
1625 _dbus_string_append_byte (&result,
1637 if (!_dbus_string_move (&result, 0, dest, insert_at))
1639 _dbus_string_free (&result);
1643 _dbus_string_free (&result);
1649 * Checks that the given range of the string
1650 * is valid ASCII. If the given range is not contained
1651 * in the string, returns #FALSE.
1653 * @param str the string
1654 * @param start first byte index to check
1655 * @param len number of bytes to check
1656 * @returns #TRUE if the byte range exists and is all valid ASCII
1659 _dbus_string_validate_ascii (const DBusString *str,
1663 const unsigned char *s;
1664 const unsigned char *end;
1665 DBUS_CONST_STRING_PREAMBLE (str);
1666 _dbus_assert (start >= 0);
1667 _dbus_assert (len >= 0);
1669 if ((start + len) > real->len)
1672 s = real->str + start;
1677 ((*s & ~0x7f) != 0))
1688 #ifdef DBUS_BUILD_TESTS
1689 #include "dbus-test.h"
1693 test_max_len (DBusString *str,
1698 if (!_dbus_string_set_length (str, max_len - 1))
1699 _dbus_assert_not_reached ("setting len to one less than max should have worked");
1702 if (!_dbus_string_set_length (str, max_len))
1703 _dbus_assert_not_reached ("setting len to max len should have worked");
1705 if (_dbus_string_set_length (str, max_len + 1))
1706 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
1708 if (!_dbus_string_set_length (str, 0))
1709 _dbus_assert_not_reached ("setting len to zero should have worked");
1713 test_base64_roundtrip (const unsigned char *data,
1721 len = strlen (data);
1723 if (!_dbus_string_init (&orig, _DBUS_INT_MAX))
1724 _dbus_assert_not_reached ("could not init string");
1726 if (!_dbus_string_init (&encoded, _DBUS_INT_MAX))
1727 _dbus_assert_not_reached ("could not init string");
1729 if (!_dbus_string_init (&decoded, _DBUS_INT_MAX))
1730 _dbus_assert_not_reached ("could not init string");
1732 if (!_dbus_string_append_len (&orig, data, len))
1733 _dbus_assert_not_reached ("couldn't append orig data");
1735 if (!_dbus_string_base64_encode (&orig, 0, &encoded, 0))
1736 _dbus_assert_not_reached ("could not encode");
1738 if (!_dbus_string_base64_decode (&encoded, 0, &decoded, 0))
1739 _dbus_assert_not_reached ("could not decode");
1741 if (!_dbus_string_equal (&orig, &decoded))
1745 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
1746 _dbus_string_get_length (&orig),
1747 _dbus_string_get_length (&encoded),
1748 _dbus_string_get_length (&decoded));
1749 printf ("Original: %s\n", data);
1750 _dbus_string_get_const_data (&decoded, &s);
1751 printf ("Decoded: %s\n", s);
1752 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
1755 _dbus_string_free (&orig);
1756 _dbus_string_free (&encoded);
1757 _dbus_string_free (&decoded);
1761 * @ingroup DBusStringInternals
1762 * Unit test for DBusString.
1764 * @todo Need to write tests for _dbus_string_copy() and
1765 * _dbus_string_move() moving to/from each of start/middle/end of a
1766 * string. Also need tests for _dbus_string_move_len ()
1768 * @returns #TRUE on success.
1771 _dbus_string_test (void)
1778 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 };
1783 while (i < _DBUS_N_ELEMENTS (lens))
1785 if (!_dbus_string_init (&str, lens[i]))
1786 _dbus_assert_not_reached ("failed to init string");
1788 test_max_len (&str, lens[i]);
1789 _dbus_string_free (&str);
1794 /* Test shortening and setting length */
1796 while (i < _DBUS_N_ELEMENTS (lens))
1800 if (!_dbus_string_init (&str, lens[i]))
1801 _dbus_assert_not_reached ("failed to init string");
1803 if (!_dbus_string_set_length (&str, lens[i]))
1804 _dbus_assert_not_reached ("failed to set string length");
1809 _dbus_assert (_dbus_string_get_length (&str) == j);
1812 _dbus_string_shorten (&str, 1);
1813 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
1818 _dbus_string_free (&str);
1823 /* Test appending data */
1824 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1825 _dbus_assert_not_reached ("failed to init string");
1830 if (!_dbus_string_append (&str, "a"))
1831 _dbus_assert_not_reached ("failed to append string to string\n");
1833 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
1835 if (!_dbus_string_append_byte (&str, 'b'))
1836 _dbus_assert_not_reached ("failed to append byte to string\n");
1838 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
1843 _dbus_string_free (&str);
1845 /* Check steal_data */
1847 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1848 _dbus_assert_not_reached ("failed to init string");
1850 if (!_dbus_string_append (&str, "Hello World"))
1851 _dbus_assert_not_reached ("could not append to string");
1853 i = _dbus_string_get_length (&str);
1855 if (!_dbus_string_steal_data (&str, &s))
1856 _dbus_assert_not_reached ("failed to steal data");
1858 _dbus_assert (_dbus_string_get_length (&str) == 0);
1859 _dbus_assert (((int)strlen (s)) == i);
1865 if (!_dbus_string_append (&str, "Hello World"))
1866 _dbus_assert_not_reached ("could not append to string");
1868 i = _dbus_string_get_length (&str);
1870 if (!_dbus_string_init (&other, _DBUS_INT_MAX))
1871 _dbus_assert_not_reached ("could not init string");
1873 if (!_dbus_string_move (&str, 0, &other, 0))
1874 _dbus_assert_not_reached ("could not move");
1876 _dbus_assert (_dbus_string_get_length (&str) == 0);
1877 _dbus_assert (_dbus_string_get_length (&other) == i);
1879 if (!_dbus_string_append (&str, "Hello World"))
1880 _dbus_assert_not_reached ("could not append to string");
1882 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
1883 _dbus_assert_not_reached ("could not move");
1885 _dbus_assert (_dbus_string_get_length (&str) == 0);
1886 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
1888 if (!_dbus_string_append (&str, "Hello World"))
1889 _dbus_assert_not_reached ("could not append to string");
1891 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
1892 _dbus_assert_not_reached ("could not move");
1894 _dbus_assert (_dbus_string_get_length (&str) == 0);
1895 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
1897 _dbus_string_free (&other);
1901 if (!_dbus_string_append (&str, "Hello World"))
1902 _dbus_assert_not_reached ("could not append to string");
1904 i = _dbus_string_get_length (&str);
1906 if (!_dbus_string_init (&other, _DBUS_INT_MAX))
1907 _dbus_assert_not_reached ("could not init string");
1909 if (!_dbus_string_copy (&str, 0, &other, 0))
1910 _dbus_assert_not_reached ("could not copy");
1912 _dbus_assert (_dbus_string_get_length (&str) == i);
1913 _dbus_assert (_dbus_string_get_length (&other) == i);
1915 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
1916 _dbus_assert_not_reached ("could not copy");
1918 _dbus_assert (_dbus_string_get_length (&str) == i);
1919 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
1920 _dbus_assert (_dbus_string_equal_c_str (&other,
1921 "Hello WorldHello World"));
1923 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
1924 _dbus_assert_not_reached ("could not copy");
1926 _dbus_assert (_dbus_string_get_length (&str) == i);
1927 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
1928 _dbus_assert (_dbus_string_equal_c_str (&other,
1929 "Hello WorldHello WorldHello World"));
1931 _dbus_string_free (&str);
1932 _dbus_string_free (&other);
1934 /* Check append/get unichar */
1936 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1937 _dbus_assert_not_reached ("failed to init string");
1940 if (!_dbus_string_append_unichar (&str, 0xfffc))
1941 _dbus_assert_not_reached ("failed to append unichar");
1943 _dbus_string_get_unichar (&str, 0, &ch, &i);
1945 _dbus_assert (ch == 0xfffc);
1946 _dbus_assert (i == _dbus_string_get_length (&str));
1948 _dbus_string_free (&str);
1950 /* Check append/parse int/double */
1952 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1953 _dbus_assert_not_reached ("failed to init string");
1955 if (!_dbus_string_append_int (&str, 27))
1956 _dbus_assert_not_reached ("failed to append int");
1958 i = _dbus_string_get_length (&str);
1960 if (!_dbus_string_parse_int (&str, 0, &v, &end))
1961 _dbus_assert_not_reached ("failed to parse int");
1963 _dbus_assert (v == 27);
1964 _dbus_assert (end == i);
1966 _dbus_string_free (&str);
1968 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1969 _dbus_assert_not_reached ("failed to init string");
1971 if (!_dbus_string_append_double (&str, 50.3))
1972 _dbus_assert_not_reached ("failed to append float");
1974 i = _dbus_string_get_length (&str);
1976 if (!_dbus_string_parse_double (&str, 0, &d, &end))
1977 _dbus_assert_not_reached ("failed to parse float");
1979 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
1980 _dbus_assert (end == i);
1982 _dbus_string_free (&str);
1985 if (!_dbus_string_init (&str, _DBUS_INT_MAX))
1986 _dbus_assert_not_reached ("failed to init string");
1988 if (!_dbus_string_append (&str, "Hello"))
1989 _dbus_assert_not_reached ("couldn't append to string");
1991 if (!_dbus_string_find (&str, 0, "He", &i))
1992 _dbus_assert_not_reached ("didn't find 'He'");
1993 _dbus_assert (i == 0);
1995 if (!_dbus_string_find (&str, 0, "ello", &i))
1996 _dbus_assert_not_reached ("didn't find 'ello'");
1997 _dbus_assert (i == 1);
1999 if (!_dbus_string_find (&str, 0, "lo", &i))
2000 _dbus_assert_not_reached ("didn't find 'lo'");
2001 _dbus_assert (i == 3);
2003 if (!_dbus_string_find (&str, 2, "lo", &i))
2004 _dbus_assert_not_reached ("didn't find 'lo'");
2005 _dbus_assert (i == 3);
2007 if (_dbus_string_find (&str, 4, "lo", &i))
2008 _dbus_assert_not_reached ("did find 'lo'");
2010 if (!_dbus_string_find (&str, 0, "l", &i))
2011 _dbus_assert_not_reached ("didn't find 'l'");
2012 _dbus_assert (i == 2);
2014 if (!_dbus_string_find (&str, 0, "H", &i))
2015 _dbus_assert_not_reached ("didn't find 'H'");
2016 _dbus_assert (i == 0);
2018 if (!_dbus_string_find (&str, 0, "", &i))
2019 _dbus_assert_not_reached ("didn't find ''");
2020 _dbus_assert (i == 0);
2022 if (_dbus_string_find (&str, 0, "Hello!", NULL))
2023 _dbus_assert_not_reached ("Did find 'Hello!'");
2025 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
2026 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
2028 if (_dbus_string_find (&str, 0, "ill", NULL))
2029 _dbus_assert_not_reached ("Did find 'ill'");
2031 if (_dbus_string_find (&str, 0, "q", NULL))
2032 _dbus_assert_not_reached ("Did find 'q'");
2034 _dbus_string_free (&str);
2037 test_base64_roundtrip ("Hello this is a string\n", -1);
2038 test_base64_roundtrip ("Hello this is a string\n1", -1);
2039 test_base64_roundtrip ("Hello this is a string\n12", -1);
2040 test_base64_roundtrip ("Hello this is a string\n123", -1);
2041 test_base64_roundtrip ("Hello this is a string\n1234", -1);
2042 test_base64_roundtrip ("Hello this is a string\n12345", -1);
2043 test_base64_roundtrip ("", 0);
2044 test_base64_roundtrip ("1", 1);
2045 test_base64_roundtrip ("12", 2);
2046 test_base64_roundtrip ("123", 3);
2047 test_base64_roundtrip ("1234", 4);
2048 test_base64_roundtrip ("12345", 5);
2049 test_base64_roundtrip ("", 1);
2050 test_base64_roundtrip ("1", 2);
2051 test_base64_roundtrip ("12", 3);
2052 test_base64_roundtrip ("123", 4);
2053 test_base64_roundtrip ("1234", 5);
2054 test_base64_roundtrip ("12345", 6);
2056 unsigned char buf[512];
2058 while (i < _DBUS_N_ELEMENTS (buf))
2064 while (i < _DBUS_N_ELEMENTS (buf))
2066 test_base64_roundtrip (buf, i);
2074 #endif /* DBUS_BUILD_TESTS */