1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-string.c String utility class (internal to D-BUS implementation)
4 * Copyright (C) 2002, 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-string.h"
26 /* we allow a system header here, for speed/convenience */
28 #include "dbus-marshal.h"
29 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
30 #include "dbus-string-private.h"
31 #include "dbus-protocol.h"
34 * @defgroup DBusString string class
35 * @ingroup DBusInternals
36 * @brief DBusString data structure
38 * Types and functions related to DBusString. DBusString is intended
39 * to be a string class that makes it hard to mess up security issues
40 * (and just in general harder to write buggy code). It should be
41 * used (or extended and then used) rather than the libc stuff in
42 * string.h. The string class is a bit inconvenient at spots because
43 * it handles out-of-memory failures and tries to be extra-robust.
45 * A DBusString has a maximum length set at initialization time; this
46 * can be used to ensure that a buffer doesn't get too big. The
47 * _dbus_string_lengthen() method checks for overflow, and for max
48 * length being exceeded.
50 * Try to avoid conversion to a plain C string, i.e. add methods on
51 * the string object instead, only convert to C string when passing
52 * things out to the public API. In particular, no sprintf, strcpy,
53 * strcat, any of that should be used. The GString feature of
54 * accepting negative numbers for "length of string" is also absent,
55 * because it could keep us from detecting bogus huge lengths. i.e. if
56 * we passed in some bogus huge length it would be taken to mean
57 * "current length of string" instead of "broken crack"
61 * @defgroup DBusStringInternals DBusString implementation details
62 * @ingroup DBusInternals
63 * @brief DBusString implementation details
65 * The guts of DBusString.
71 * We allocate 1 byte for nul termination, plus 7 bytes for possible
72 * align_offset, so we always need 8 bytes on top of the string's
73 * length to be in the allocated block.
75 #define ALLOCATION_PADDING 8
78 * This is the maximum max length (and thus also the maximum length)
81 #define MAX_MAX_LENGTH (_DBUS_INT_MAX - ALLOCATION_PADDING)
84 * Checks a bunch of assertions about a string object
86 * @param real the DBusRealString
88 #define DBUS_GENERIC_STRING_PREAMBLE(real) _dbus_assert ((real) != NULL); _dbus_assert (!(real)->invalid); _dbus_assert ((real)->len >= 0); _dbus_assert ((real)->allocated >= 0); _dbus_assert ((real)->max_length >= 0); _dbus_assert ((real)->len <= ((real)->allocated - ALLOCATION_PADDING)); _dbus_assert ((real)->len <= (real)->max_length)
91 * Checks assertions about a string object that needs to be
92 * modifiable - may not be locked or const. Also declares
93 * the "real" variable pointing to DBusRealString.
94 * @param str the string
96 #define DBUS_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
97 DBUS_GENERIC_STRING_PREAMBLE (real); \
98 _dbus_assert (!(real)->constant); \
99 _dbus_assert (!(real)->locked)
102 * Checks assertions about a string object that may be locked but
103 * can't be const. i.e. a string object that we can free. Also
104 * declares the "real" variable pointing to DBusRealString.
106 * @param str the string
108 #define DBUS_LOCKED_STRING_PREAMBLE(str) DBusRealString *real = (DBusRealString*) str; \
109 DBUS_GENERIC_STRING_PREAMBLE (real); \
110 _dbus_assert (!(real)->constant)
113 * Checks assertions about a string that may be const or locked. Also
114 * declares the "real" variable pointing to DBusRealString.
115 * @param str the string.
117 #define DBUS_CONST_STRING_PREAMBLE(str) const DBusRealString *real = (DBusRealString*) str; \
118 DBUS_GENERIC_STRING_PREAMBLE (real)
123 * @addtogroup DBusString
128 fixup_alignment (DBusRealString *real)
132 unsigned int old_align_offset;
134 /* we have to have extra space in real->allocated for the align offset and nul byte */
135 _dbus_assert (real->len <= real->allocated - ALLOCATION_PADDING);
137 old_align_offset = real->align_offset;
138 real_block = real->str - old_align_offset;
140 aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
142 real->align_offset = aligned - real_block;
145 if (old_align_offset != real->align_offset)
147 /* Here comes the suck */
148 memmove (real_block + real->align_offset,
149 real_block + old_align_offset,
153 _dbus_assert (real->align_offset < 8);
154 _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
158 undo_alignment (DBusRealString *real)
160 if (real->align_offset != 0)
162 memmove (real->str - real->align_offset,
166 real->str = real->str - real->align_offset;
167 real->align_offset = 0;
172 * Initializes a string. The string starts life with zero length. The
173 * string must eventually be freed with _dbus_string_free().
175 * @param str memory to hold the string
176 * @returns #TRUE on success, #FALSE if no memory
179 _dbus_string_init (DBusString *str)
181 DBusRealString *real;
183 _dbus_assert (str != NULL);
185 _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
187 real = (DBusRealString*) str;
189 /* It's very important not to touch anything
190 * other than real->str if we're going to fail,
191 * since we also use this function to reset
192 * an existing string, e.g. in _dbus_string_steal_data()
195 real->str = dbus_malloc (ALLOCATION_PADDING);
196 if (real->str == NULL)
199 real->allocated = ALLOCATION_PADDING;
201 real->str[real->len] = '\0';
203 real->max_length = MAX_MAX_LENGTH;
204 real->constant = FALSE;
205 real->locked = FALSE;
206 real->invalid = FALSE;
207 real->align_offset = 0;
209 fixup_alignment (real);
214 /* The max length thing is sort of a historical artifact
215 * from a feature that turned out to be dumb; perhaps
216 * we should purge it entirely. The problem with
217 * the feature is that it looks like memory allocation
218 * failure, but is not a transient or resolvable failure.
221 set_max_length (DBusString *str,
224 DBusRealString *real;
226 real = (DBusRealString*) str;
228 real->max_length = max_length;
232 * Initializes a constant string. The value parameter is not copied
233 * (should be static), and the string may never be modified.
234 * It is safe but not necessary to call _dbus_string_free()
235 * on a const string. The string has a length limit of MAXINT - 8.
237 * @param str memory to use for the string
238 * @param value a string to be stored in str (not copied!!!)
241 _dbus_string_init_const (DBusString *str,
244 _dbus_assert (value != NULL);
246 _dbus_string_init_const_len (str, value,
251 * Initializes a constant string with a length. The value parameter is
252 * not copied (should be static), and the string may never be
253 * modified. It is safe but not necessary to call _dbus_string_free()
256 * @param str memory to use for the string
257 * @param value a string to be stored in str (not copied!!!)
258 * @param len the length to use
261 _dbus_string_init_const_len (DBusString *str,
265 DBusRealString *real;
267 _dbus_assert (str != NULL);
268 _dbus_assert (value != NULL);
269 _dbus_assert (len <= MAX_MAX_LENGTH);
270 _dbus_assert (len >= 0);
272 real = (DBusRealString*) str;
274 real->str = (char*) value;
276 real->allocated = real->len + ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
277 real->max_length = real->len + 1;
278 real->constant = TRUE;
279 real->invalid = FALSE;
281 /* We don't require const strings to be 8-byte aligned as the
282 * memory is coming from elsewhere.
287 * Frees a string created by _dbus_string_init().
289 * @param str memory where the string is stored.
292 _dbus_string_free (DBusString *str)
294 DBusRealString *real = (DBusRealString*) str;
295 DBUS_GENERIC_STRING_PREAMBLE (real);
299 dbus_free (real->str - real->align_offset);
301 real->invalid = TRUE;
304 #ifdef DBUS_BUILD_TESTS
305 /* Not using this feature at the moment,
306 * so marked DBUS_BUILD_TESTS-only
309 * Locks a string such that any attempts to change the string will
310 * result in aborting the program. Also, if the string is wasting a
311 * lot of memory (allocation is sufficiently larger than what the
312 * string is really using), _dbus_string_lock() will realloc the
313 * string's data to "compact" it.
315 * @param str the string to lock.
318 _dbus_string_lock (DBusString *str)
320 DBUS_LOCKED_STRING_PREAMBLE (str); /* can lock multiple times */
324 /* Try to realloc to avoid excess memory usage, since
325 * we know we won't change the string further
328 if (real->allocated - MAX_WASTE > real->len)
333 new_allocated = real->len + ALLOCATION_PADDING;
335 new_str = dbus_realloc (real->str - real->align_offset,
339 real->str = new_str + real->align_offset;
340 real->allocated = new_allocated;
341 fixup_alignment (real);
345 #endif /* DBUS_BUILD_TESTS */
348 set_length (DBusRealString *real,
351 /* Note, we are setting the length without nul termination */
353 /* exceeding max length is the same as failure to allocate memory */
354 if (new_length > real->max_length)
357 if (new_length > (real->allocated - ALLOCATION_PADDING))
362 /* at least double our old allocation to avoid O(n), avoiding
365 if (real->allocated > (MAX_MAX_LENGTH + ALLOCATION_PADDING) / 2)
366 new_allocated = MAX_MAX_LENGTH + ALLOCATION_PADDING;
368 new_allocated = real->allocated * 2;
370 /* if you change the code just above here, run the tests without
371 * the following before you commit
373 #ifdef DBUS_BUILD_TESTS
374 new_allocated = 0; /* ensure a realloc every time so that we go
375 * through all malloc failure codepaths
379 /* But be sure we always alloc at least space for the new length */
380 new_allocated = MAX (new_allocated, new_length + ALLOCATION_PADDING);
382 new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
386 real->str = new_str + real->align_offset;
387 real->allocated = new_allocated;
388 fixup_alignment (real);
391 real->len = new_length;
392 real->str[real->len] = '\0';
399 DBusRealString *dest,
405 if (len > dest->max_length - dest->len)
406 return FALSE; /* detected overflow of dest->len + len below */
408 if (!set_length (dest, dest->len + len))
411 memmove (dest->str + insert_at + len,
412 dest->str + insert_at,
413 dest->len - len - insert_at);
419 * Gets the raw character buffer from the string. The returned buffer
420 * will be nul-terminated, but note that strings may contain binary
421 * data so there may be extra nul characters prior to the termination.
422 * This function should be little-used, extend DBusString or add
423 * stuff to dbus-sysdeps.c instead. It's an error to use this
424 * function on a const string.
426 * @param str the string
430 _dbus_string_get_data (DBusString *str)
432 DBUS_STRING_PREAMBLE (str);
438 * Gets the raw character buffer from a const string.
440 * @param str the string
441 * @returns the string data
444 _dbus_string_get_const_data (const DBusString *str)
446 DBUS_CONST_STRING_PREAMBLE (str);
452 * Gets a sub-portion of the raw character buffer from the
453 * string. The "len" field is required simply for error
454 * checking, to be sure you don't try to use more
455 * string than exists. The nul termination of the
456 * returned buffer remains at the end of the entire
457 * string, not at start + len.
459 * @param str the string
460 * @param start byte offset to return
461 * @param len length of segment to return
462 * @returns the string data
465 _dbus_string_get_data_len (DBusString *str,
469 DBUS_STRING_PREAMBLE (str);
470 _dbus_assert (start >= 0);
471 _dbus_assert (len >= 0);
472 _dbus_assert (start <= real->len);
473 _dbus_assert (len <= real->len - start);
475 return real->str + start;
479 * const version of _dbus_string_get_data_len().
481 * @param str the string
482 * @param start byte offset to return
483 * @param len length of segment to return
484 * @returns the string data
487 _dbus_string_get_const_data_len (const DBusString *str,
491 DBUS_CONST_STRING_PREAMBLE (str);
492 _dbus_assert (start >= 0);
493 _dbus_assert (len >= 0);
494 _dbus_assert (start <= real->len);
495 _dbus_assert (len <= real->len - start);
497 return real->str + start;
501 * Sets the value of the byte at the given position.
503 * @param str the string
504 * @param i the position
505 * @param byte the new value
508 _dbus_string_set_byte (DBusString *str,
512 DBUS_STRING_PREAMBLE (str);
513 _dbus_assert (i < real->len);
514 _dbus_assert (i >= 0);
520 * Gets the byte at the given position.
522 * @param str the string
523 * @param start the position
524 * @returns the byte at that position
527 _dbus_string_get_byte (const DBusString *str,
530 DBUS_CONST_STRING_PREAMBLE (str);
531 _dbus_assert (start < real->len);
532 _dbus_assert (start >= 0);
534 return real->str[start];
538 * Inserts the given byte at the given position.
540 * @param str the string
541 * @param i the position
542 * @param byte the value to insert
543 * @returns #TRUE on success
546 _dbus_string_insert_byte (DBusString *str,
550 DBUS_STRING_PREAMBLE (str);
551 _dbus_assert (i <= real->len);
552 _dbus_assert (i >= 0);
554 if (!open_gap (1, real, i))
563 * Like _dbus_string_get_data(), but removes the
564 * gotten data from the original string. The caller
565 * must free the data returned. This function may
566 * fail due to lack of memory, and return #FALSE.
568 * @param str the string
569 * @param data_return location to return the buffer
570 * @returns #TRUE on success
573 _dbus_string_steal_data (DBusString *str,
577 DBUS_STRING_PREAMBLE (str);
578 _dbus_assert (data_return != NULL);
580 undo_alignment (real);
582 *data_return = real->str;
584 old_max_length = real->max_length;
586 /* reset the string */
587 if (!_dbus_string_init (str))
589 /* hrm, put it back then */
590 real->str = *data_return;
592 fixup_alignment (real);
596 real->max_length = old_max_length;
602 * Like _dbus_string_get_data_len(), but removes the gotten data from
603 * the original string. The caller must free the data returned. This
604 * function may fail due to lack of memory, and return #FALSE.
605 * The returned string is nul-terminated and has length len.
607 * @todo this function is broken because on failure it
608 * may corrupt the source string.
610 * @param str the string
611 * @param data_return location to return the buffer
612 * @param start the start of segment to steal
613 * @param len the length of segment to steal
614 * @returns #TRUE on success
617 _dbus_string_steal_data_len (DBusString *str,
623 DBUS_STRING_PREAMBLE (str);
624 _dbus_assert (data_return != NULL);
625 _dbus_assert (start >= 0);
626 _dbus_assert (len >= 0);
627 _dbus_assert (start <= real->len);
628 _dbus_assert (len <= real->len - start);
630 if (!_dbus_string_init (&dest))
633 set_max_length (&dest, real->max_length);
635 if (!_dbus_string_move_len (str, start, len, &dest, 0))
637 _dbus_string_free (&dest);
641 _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
642 if (!_dbus_string_steal_data (&dest, data_return))
644 _dbus_string_free (&dest);
648 _dbus_string_free (&dest);
654 * Copies the data from the string into a char*
656 * @param str the string
657 * @param data_return place to return the data
658 * @returns #TRUE on success, #FALSE on no memory
661 _dbus_string_copy_data (const DBusString *str,
664 DBUS_CONST_STRING_PREAMBLE (str);
665 _dbus_assert (data_return != NULL);
667 *data_return = dbus_malloc (real->len + 1);
668 if (*data_return == NULL)
671 memcpy (*data_return, real->str, real->len + 1);
677 * Copies a segment of the string into a char*
679 * @param str the string
680 * @param data_return place to return the data
681 * @param start start index
682 * @param len length to copy
683 * @returns #FALSE if no memory
686 _dbus_string_copy_data_len (const DBusString *str,
693 DBUS_CONST_STRING_PREAMBLE (str);
694 _dbus_assert (data_return != NULL);
695 _dbus_assert (start >= 0);
696 _dbus_assert (len >= 0);
697 _dbus_assert (start <= real->len);
698 _dbus_assert (len <= real->len - start);
700 if (!_dbus_string_init (&dest))
703 set_max_length (&dest, real->max_length);
705 if (!_dbus_string_copy_len (str, start, len, &dest, 0))
707 _dbus_string_free (&dest);
711 if (!_dbus_string_steal_data (&dest, data_return))
713 _dbus_string_free (&dest);
717 _dbus_string_free (&dest);
722 * Gets the length of a string (not including nul termination).
724 * @returns the length.
727 _dbus_string_get_length (const DBusString *str)
729 DBUS_CONST_STRING_PREAMBLE (str);
735 * Makes a string longer by the given number of bytes. Checks whether
736 * adding additional_length to the current length would overflow an
737 * integer, and checks for exceeding a string's max length.
738 * The new bytes are not initialized, other than nul-terminating
739 * the end of the string. The uninitialized bytes may contain
740 * nul bytes or other junk.
742 * @param str a string
743 * @param additional_length length to add to the string.
744 * @returns #TRUE on success.
747 _dbus_string_lengthen (DBusString *str,
748 int additional_length)
750 DBUS_STRING_PREAMBLE (str);
751 _dbus_assert (additional_length >= 0);
753 if (additional_length > real->max_length - real->len)
754 return FALSE; /* would overflow */
756 return set_length (real,
757 real->len + additional_length);
761 * Makes a string shorter by the given number of bytes.
763 * @param str a string
764 * @param length_to_remove length to remove from the string.
767 _dbus_string_shorten (DBusString *str,
768 int length_to_remove)
770 DBUS_STRING_PREAMBLE (str);
771 _dbus_assert (length_to_remove >= 0);
772 _dbus_assert (length_to_remove <= real->len);
775 real->len - length_to_remove);
779 * Sets the length of a string. Can be used to truncate or lengthen
780 * the string. If the string is lengthened, the function may fail and
781 * return #FALSE. Newly-added bytes are not initialized, as with
782 * _dbus_string_lengthen().
784 * @param str a string
785 * @param length new length of the string.
786 * @returns #FALSE on failure.
789 _dbus_string_set_length (DBusString *str,
792 DBUS_STRING_PREAMBLE (str);
793 _dbus_assert (length >= 0);
795 return set_length (real, length);
799 * Align the length of a string to a specific alignment (typically 4 or 8)
800 * by appending nul bytes to the string.
802 * @param str a string
803 * @param alignment the alignment
804 * @returns #FALSE if no memory
807 _dbus_string_align_length (DBusString *str,
810 unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
812 DBUS_STRING_PREAMBLE (str);
813 _dbus_assert (alignment >= 1);
814 _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
816 new_len = _DBUS_ALIGN_VALUE (real->len, alignment);
817 if (new_len > (unsigned long) real->max_length)
820 delta = new_len - real->len;
821 _dbus_assert (delta >= 0);
826 if (!set_length (real, new_len))
829 memset (real->str + (new_len - delta),
836 append (DBusRealString *real,
843 if (!_dbus_string_lengthen ((DBusString*)real, buffer_len))
846 memcpy (real->str + (real->len - buffer_len),
854 * Appends a nul-terminated C-style string to a DBusString.
856 * @param str the DBusString
857 * @param buffer the nul-terminated characters to append
858 * @returns #FALSE if not enough memory.
861 _dbus_string_append (DBusString *str,
864 unsigned long buffer_len;
866 DBUS_STRING_PREAMBLE (str);
867 _dbus_assert (buffer != NULL);
869 buffer_len = strlen (buffer);
870 if (buffer_len > (unsigned long) real->max_length)
873 return append (real, buffer, buffer_len);
877 * Appends block of bytes with the given length to a DBusString.
879 * @param str the DBusString
880 * @param buffer the bytes to append
881 * @param len the number of bytes to append
882 * @returns #FALSE if not enough memory.
885 _dbus_string_append_len (DBusString *str,
889 DBUS_STRING_PREAMBLE (str);
890 _dbus_assert (buffer != NULL);
891 _dbus_assert (len >= 0);
893 return append (real, buffer, len);
897 * Appends a single byte to the string, returning #FALSE
898 * if not enough memory.
900 * @param str the string
901 * @param byte the byte to append
902 * @returns #TRUE on success
905 _dbus_string_append_byte (DBusString *str,
908 DBUS_STRING_PREAMBLE (str);
910 if (!set_length (real, real->len + 1))
913 real->str[real->len-1] = byte;
919 * Appends a single Unicode character, encoding the character
922 * @param str the string
923 * @param ch the Unicode character
926 _dbus_string_append_unichar (DBusString *str,
934 DBUS_STRING_PREAMBLE (str);
936 /* this code is from GLib but is pretty standard I think */
950 else if (ch < 0x10000)
955 else if (ch < 0x200000)
960 else if (ch < 0x4000000)
971 if (len > (real->max_length - real->len))
972 return FALSE; /* real->len + len would overflow */
974 if (!set_length (real, real->len + len))
977 out = real->str + (real->len - len);
979 for (i = len - 1; i > 0; --i)
981 out[i] = (ch & 0x3f) | 0x80;
990 delete (DBusRealString *real,
997 memmove (real->str + start, real->str + start + len, real->len - (start + len));
999 real->str[real->len] = '\0';
1003 * Deletes a segment of a DBusString with length len starting at
1004 * start. (Hint: to clear an entire string, setting length to 0
1005 * with _dbus_string_set_length() is easier.)
1007 * @param str the DBusString
1008 * @param start where to start deleting
1009 * @param len the number of bytes to delete
1012 _dbus_string_delete (DBusString *str,
1016 DBUS_STRING_PREAMBLE (str);
1017 _dbus_assert (start >= 0);
1018 _dbus_assert (len >= 0);
1019 _dbus_assert (start <= real->len);
1020 _dbus_assert (len <= real->len - start);
1022 delete (real, start, len);
1026 copy (DBusRealString *source,
1029 DBusRealString *dest,
1035 if (!open_gap (len, dest, insert_at))
1038 memcpy (dest->str + insert_at,
1039 source->str + start,
1046 * Checks assertions for two strings we're copying a segment between,
1047 * and declares real_source/real_dest variables.
1049 * @param source the source string
1050 * @param start the starting offset
1051 * @param dest the dest string
1052 * @param insert_at where the copied segment is inserted
1054 #define DBUS_STRING_COPY_PREAMBLE(source, start, dest, insert_at) \
1055 DBusRealString *real_source = (DBusRealString*) source; \
1056 DBusRealString *real_dest = (DBusRealString*) dest; \
1057 _dbus_assert ((source) != (dest)); \
1058 DBUS_GENERIC_STRING_PREAMBLE (real_source); \
1059 DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
1060 _dbus_assert (!real_dest->constant); \
1061 _dbus_assert (!real_dest->locked); \
1062 _dbus_assert ((start) >= 0); \
1063 _dbus_assert ((start) <= real_source->len); \
1064 _dbus_assert ((insert_at) >= 0); \
1065 _dbus_assert ((insert_at) <= real_dest->len)
1068 * Moves the end of one string into another string. Both strings
1069 * must be initialized, valid strings.
1071 * @param source the source string
1072 * @param start where to chop off the source string
1073 * @param dest the destination string
1074 * @param insert_at where to move the chopped-off part of source string
1075 * @returns #FALSE if not enough memory
1078 _dbus_string_move (DBusString *source,
1083 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1085 if (!copy (real_source, start,
1086 real_source->len - start,
1091 delete (real_source, start,
1092 real_source->len - start);
1098 * Like _dbus_string_move(), but does not delete the section
1099 * of the source string that's copied to the dest string.
1101 * @param source the source string
1102 * @param start where to start copying the source string
1103 * @param dest the destination string
1104 * @param insert_at where to place the copied part of source string
1105 * @returns #FALSE if not enough memory
1108 _dbus_string_copy (const DBusString *source,
1113 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1115 return copy (real_source, start,
1116 real_source->len - start,
1122 * Like _dbus_string_move(), but can move a segment from
1123 * the middle of the source string.
1125 * @param source the source string
1126 * @param start first byte of source string to move
1127 * @param len length of segment to move
1128 * @param dest the destination string
1129 * @param insert_at where to move the bytes from the source string
1130 * @returns #FALSE if not enough memory
1133 _dbus_string_move_len (DBusString *source,
1140 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1141 _dbus_assert (len >= 0);
1142 _dbus_assert ((start + len) <= real_source->len);
1144 if (!copy (real_source, start, len,
1149 delete (real_source, start,
1156 * Like _dbus_string_copy(), but can copy a segment from the middle of
1157 * the source string.
1159 * @param source the source string
1160 * @param start where to start copying the source string
1161 * @param len length of segment to copy
1162 * @param dest the destination string
1163 * @param insert_at where to place the copied segment of source string
1164 * @returns #FALSE if not enough memory
1167 _dbus_string_copy_len (const DBusString *source,
1173 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
1174 _dbus_assert (len >= 0);
1175 _dbus_assert (start <= real_source->len);
1176 _dbus_assert (len <= real_source->len - start);
1178 return copy (real_source, start, len,
1184 * Replaces a segment of dest string with a segment of source string.
1186 * @todo optimize the case where the two lengths are the same, and
1187 * avoid memmoving the data in the trailing part of the string twice.
1189 * @todo avoid inserting the source into dest, then deleting
1190 * the replaced chunk of dest (which creates a potentially large
1191 * intermediate string). Instead, extend the replaced chunk
1192 * of dest with padding to the same size as the source chunk,
1193 * then copy in the source bytes.
1195 * @param source the source string
1196 * @param start where to start copying the source string
1197 * @param len length of segment to copy
1198 * @param dest the destination string
1199 * @param replace_at start of segment of dest string to replace
1200 * @param replace_len length of segment of dest string to replace
1201 * @returns #FALSE if not enough memory
1205 _dbus_string_replace_len (const DBusString *source,
1212 DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
1213 _dbus_assert (len >= 0);
1214 _dbus_assert (start <= real_source->len);
1215 _dbus_assert (len <= real_source->len - start);
1216 _dbus_assert (replace_at >= 0);
1217 _dbus_assert (replace_at <= real_dest->len);
1218 _dbus_assert (replace_len <= real_dest->len - replace_at);
1220 if (!copy (real_source, start, len,
1221 real_dest, replace_at))
1224 delete (real_dest, replace_at + len, replace_len);
1229 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
1230 * Pennington, and Tom Tromey are the authors and authorized relicense.
1233 /** computes length and mask of a unicode character
1234 * @param Char the char
1235 * @param Mask the mask variable to assign to
1236 * @param Len the length variable to assign to
1238 #define UTF8_COMPUTE(Char, Mask, Len) \
1244 else if ((Char & 0xe0) == 0xc0) \
1249 else if ((Char & 0xf0) == 0xe0) \
1254 else if ((Char & 0xf8) == 0xf0) \
1259 else if ((Char & 0xfc) == 0xf8) \
1264 else if ((Char & 0xfe) == 0xfc) \
1273 * computes length of a unicode character in UTF-8
1274 * @param Char the char
1276 #define UTF8_LENGTH(Char) \
1277 ((Char) < 0x80 ? 1 : \
1278 ((Char) < 0x800 ? 2 : \
1279 ((Char) < 0x10000 ? 3 : \
1280 ((Char) < 0x200000 ? 4 : \
1281 ((Char) < 0x4000000 ? 5 : 6)))))
1284 * Gets a UTF-8 value.
1286 * @param Result variable for extracted unicode char.
1287 * @param Chars the bytes to decode
1288 * @param Count counter variable
1289 * @param Mask mask for this char
1290 * @param Len length for this char in bytes
1292 #define UTF8_GET(Result, Chars, Count, Mask, Len) \
1293 (Result) = (Chars)[0] & (Mask); \
1294 for ((Count) = 1; (Count) < (Len); ++(Count)) \
1296 if (((Chars)[(Count)] & 0xc0) != 0x80) \
1302 (Result) |= ((Chars)[(Count)] & 0x3f); \
1306 * Check whether a unicode char is in a valid range.
1308 * @param Char the character
1310 #define UNICODE_VALID(Char) \
1311 ((Char) < 0x110000 && \
1312 (((Char) & 0xFFFFF800) != 0xD800) && \
1313 ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
1314 ((Char) & 0xFFFF) != 0xFFFF)
1317 * Gets a unicode character from a UTF-8 string. Does no validation;
1318 * you must verify that the string is valid UTF-8 in advance and must
1319 * pass in the start of a character.
1321 * @param str the string
1322 * @param start the start of the UTF-8 character.
1323 * @param ch_return location to return the character
1324 * @param end_return location to return the byte index of next character
1327 _dbus_string_get_unichar (const DBusString *str,
1329 dbus_unichar_t *ch_return,
1333 dbus_unichar_t result;
1336 DBUS_CONST_STRING_PREAMBLE (str);
1337 _dbus_assert (start >= 0);
1338 _dbus_assert (start <= real->len);
1343 *end_return = real->len;
1346 p = real->str + start;
1349 UTF8_COMPUTE (c, mask, len);
1352 UTF8_GET (result, p, i, mask, len);
1354 if (result == (dbus_unichar_t)-1)
1358 *ch_return = result;
1360 *end_return = start + len;
1364 * Finds the given substring in the string,
1365 * returning #TRUE and filling in the byte index
1366 * where the substring was found, if it was found.
1367 * Returns #FALSE if the substring wasn't found.
1368 * Sets *start to the length of the string if the substring
1371 * @param str the string
1372 * @param start where to start looking
1373 * @param substr the substring
1374 * @param found return location for where it was found, or #NULL
1375 * @returns #TRUE if found
1378 _dbus_string_find (const DBusString *str,
1383 return _dbus_string_find_to (str, start,
1384 ((const DBusRealString*)str)->len,
1389 * Finds the given substring in the string,
1390 * up to a certain position,
1391 * returning #TRUE and filling in the byte index
1392 * where the substring was found, if it was found.
1393 * Returns #FALSE if the substring wasn't found.
1394 * Sets *start to the length of the string if the substring
1397 * @param str the string
1398 * @param start where to start looking
1399 * @param end where to stop looking
1400 * @param substr the substring
1401 * @param found return location for where it was found, or #NULL
1402 * @returns #TRUE if found
1405 _dbus_string_find_to (const DBusString *str,
1412 DBUS_CONST_STRING_PREAMBLE (str);
1413 _dbus_assert (substr != NULL);
1414 _dbus_assert (start <= real->len);
1415 _dbus_assert (start >= 0);
1416 _dbus_assert (substr != NULL);
1417 _dbus_assert (end <= real->len);
1418 _dbus_assert (start <= end);
1420 /* we always "find" an empty string */
1421 if (*substr == '\0')
1431 if (real->str[i] == substr[0])
1437 if (substr[j - i] == '\0')
1439 else if (real->str[j] != substr[j - i])
1445 if (substr[j - i] == '\0')
1463 * Find the given byte scanning backward from the given start.
1464 * Sets *found to -1 if the byte is not found.
1466 * @param str the string
1467 * @param start the place to start scanning (will not find the byte at this point)
1468 * @param byte the byte to find
1469 * @param found return location for where it was found
1470 * @returns #TRUE if found
1473 _dbus_string_find_byte_backward (const DBusString *str,
1479 DBUS_CONST_STRING_PREAMBLE (str);
1480 _dbus_assert (start <= real->len);
1481 _dbus_assert (start >= 0);
1482 _dbus_assert (found != NULL);
1487 if (real->str[i] == byte)
1500 * Finds a blank (space or tab) in the string. Returns #TRUE
1501 * if found, #FALSE otherwise. If a blank is not found sets
1502 * *found to the length of the string.
1504 * @param str the string
1505 * @param start byte index to start looking
1506 * @param found place to store the location of the first blank
1507 * @returns #TRUE if a blank was found
1510 _dbus_string_find_blank (const DBusString *str,
1515 DBUS_CONST_STRING_PREAMBLE (str);
1516 _dbus_assert (start <= real->len);
1517 _dbus_assert (start >= 0);
1520 while (i < real->len)
1522 if (real->str[i] == ' ' ||
1523 real->str[i] == '\t')
1540 * Skips blanks from start, storing the first non-blank in *end
1541 * (blank is space or tab).
1543 * @param str the string
1544 * @param start where to start
1545 * @param end where to store the first non-blank byte index
1548 _dbus_string_skip_blank (const DBusString *str,
1553 DBUS_CONST_STRING_PREAMBLE (str);
1554 _dbus_assert (start <= real->len);
1555 _dbus_assert (start >= 0);
1558 while (i < real->len)
1560 if (!(real->str[i] == ' ' ||
1561 real->str[i] == '\t'))
1567 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1568 real->str[i] == '\t'));
1575 * Skips whitespace from start, storing the first non-whitespace in *end.
1576 * (whitespace is space, tab, newline, CR).
1578 * @param str the string
1579 * @param start where to start
1580 * @param end where to store the first non-whitespace byte index
1583 _dbus_string_skip_white (const DBusString *str,
1588 DBUS_CONST_STRING_PREAMBLE (str);
1589 _dbus_assert (start <= real->len);
1590 _dbus_assert (start >= 0);
1593 while (i < real->len)
1595 if (!(real->str[i] == ' ' ||
1596 real->str[i] == '\n' ||
1597 real->str[i] == '\r' ||
1598 real->str[i] == '\t'))
1604 _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
1605 real->str[i] == '\t'));
1612 * Assigns a newline-terminated or \r\n-terminated line from the front
1613 * of the string to the given dest string. The dest string's previous
1614 * contents are deleted. If the source string contains no newline,
1615 * moves the entire source string to the dest string.
1617 * @todo owen correctly notes that this is a stupid function (it was
1618 * written purely for test code,
1619 * e.g. dbus-message-builder.c). Probably should be enforced as test
1620 * code only with #ifdef DBUS_BUILD_TESTS
1622 * @param source the source string
1623 * @param dest the destination string (contents are replaced)
1624 * @returns #FALSE if no memory, or source has length 0
1627 _dbus_string_pop_line (DBusString *source,
1631 dbus_bool_t have_newline;
1633 _dbus_string_set_length (dest, 0);
1636 if (_dbus_string_find (source, 0, "\n", &eol))
1638 have_newline = TRUE;
1639 eol += 1; /* include newline */
1643 eol = _dbus_string_get_length (source);
1644 have_newline = FALSE;
1648 return FALSE; /* eof */
1650 if (!_dbus_string_move_len (source, 0, eol,
1656 /* dump the newline and the \r if we have one */
1659 dbus_bool_t have_cr;
1661 _dbus_assert (_dbus_string_get_length (dest) > 0);
1663 if (_dbus_string_get_length (dest) > 1 &&
1664 _dbus_string_get_byte (dest,
1665 _dbus_string_get_length (dest) - 2) == '\r')
1670 _dbus_string_set_length (dest,
1671 _dbus_string_get_length (dest) -
1679 * Deletes up to and including the first blank space
1682 * @param str the string
1685 _dbus_string_delete_first_word (DBusString *str)
1689 if (_dbus_string_find_blank (str, 0, &i))
1690 _dbus_string_skip_blank (str, i, &i);
1692 _dbus_string_delete (str, 0, i);
1696 * Deletes any leading blanks in the string
1698 * @param str the string
1701 _dbus_string_delete_leading_blanks (DBusString *str)
1705 _dbus_string_skip_blank (str, 0, &i);
1708 _dbus_string_delete (str, 0, i);
1712 * Tests two DBusString for equality.
1714 * @todo memcmp is probably faster
1716 * @param a first string
1717 * @param b second string
1718 * @returns #TRUE if equal
1721 _dbus_string_equal (const DBusString *a,
1722 const DBusString *b)
1724 const unsigned char *ap;
1725 const unsigned char *bp;
1726 const unsigned char *a_end;
1727 const DBusRealString *real_a = (const DBusRealString*) a;
1728 const DBusRealString *real_b = (const DBusRealString*) b;
1729 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1730 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1732 if (real_a->len != real_b->len)
1737 a_end = real_a->str + real_a->len;
1751 * Tests two DBusString for equality up to the given length.
1753 * @todo write a unit test
1755 * @todo memcmp is probably faster
1757 * @param a first string
1758 * @param b second string
1759 * @param len the lengh
1760 * @returns #TRUE if equal for the given number of bytes
1763 _dbus_string_equal_len (const DBusString *a,
1764 const DBusString *b,
1767 const unsigned char *ap;
1768 const unsigned char *bp;
1769 const unsigned char *a_end;
1770 const DBusRealString *real_a = (const DBusRealString*) a;
1771 const DBusRealString *real_b = (const DBusRealString*) b;
1772 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1773 DBUS_GENERIC_STRING_PREAMBLE (real_b);
1775 if (real_a->len != real_b->len &&
1776 (real_a->len < len || real_b->len < len))
1781 a_end = real_a->str + MIN (real_a->len, len);
1795 * Checks whether a string is equal to a C string.
1797 * @param a the string
1798 * @param c_str the C string
1799 * @returns #TRUE if equal
1802 _dbus_string_equal_c_str (const DBusString *a,
1805 const unsigned char *ap;
1806 const unsigned char *bp;
1807 const unsigned char *a_end;
1808 const DBusRealString *real_a = (const DBusRealString*) a;
1809 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1810 _dbus_assert (c_str != NULL);
1813 bp = (const unsigned char*) c_str;
1814 a_end = real_a->str + real_a->len;
1815 while (ap != a_end && *bp)
1824 if (ap != a_end || *bp)
1831 * Checks whether a string starts with the given C string.
1833 * @param a the string
1834 * @param c_str the C string
1835 * @returns #TRUE if string starts with it
1838 _dbus_string_starts_with_c_str (const DBusString *a,
1841 const unsigned char *ap;
1842 const unsigned char *bp;
1843 const unsigned char *a_end;
1844 const DBusRealString *real_a = (const DBusRealString*) a;
1845 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1846 _dbus_assert (c_str != NULL);
1849 bp = (const unsigned char*) c_str;
1850 a_end = real_a->str + real_a->len;
1851 while (ap != a_end && *bp)
1867 * Returns whether a string ends with the given suffix
1869 * @todo memcmp might make this faster.
1871 * @param a the string
1872 * @param c_str the C-style string
1873 * @returns #TRUE if the string ends with the suffix
1876 _dbus_string_ends_with_c_str (const DBusString *a,
1879 const unsigned char *ap;
1880 const unsigned char *bp;
1881 const unsigned char *a_end;
1882 unsigned long c_str_len;
1883 const DBusRealString *real_a = (const DBusRealString*) a;
1884 DBUS_GENERIC_STRING_PREAMBLE (real_a);
1885 _dbus_assert (c_str != NULL);
1887 c_str_len = strlen (c_str);
1888 if (((unsigned long)real_a->len) < c_str_len)
1891 ap = real_a->str + (real_a->len - c_str_len);
1892 bp = (const unsigned char*) c_str;
1893 a_end = real_a->str + real_a->len;
1903 _dbus_assert (*ap == '\0');
1904 _dbus_assert (*bp == '\0');
1909 static const signed char base64_table[] = {
1976 /** The minimum char that's a valid char in Base64-encoded text */
1977 #define UNBASE64_MIN_CHAR (43)
1978 /** The maximum char that's a valid char in Base64-encoded text */
1979 #define UNBASE64_MAX_CHAR (122)
1980 /** Must subtract this from a char's integer value before offsetting
1981 * into unbase64_table
1983 #define UNBASE64_TABLE_OFFSET UNBASE64_MIN_CHAR
1984 static const signed char unbase64_table[] = {
2068 * Encodes a string using Base64, as documented in RFC 2045.
2070 * @param source the string to encode
2071 * @param start byte index to start encoding
2072 * @param dest string where encoded data should be placed
2073 * @param insert_at where to place encoded data
2074 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2077 _dbus_string_base64_encode (const DBusString *source,
2083 unsigned int dest_len; /* unsigned for overflow checks below */
2084 const unsigned char *s;
2086 const unsigned char *triplet_end;
2087 const unsigned char *final_end;
2088 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
2089 _dbus_assert (source != dest);
2091 /* For each 24 bits (3 bytes) of input, we have 4 bytes of
2094 source_len = real_source->len - start;
2095 dest_len = (source_len / 3) * 4;
2096 if (source_len % 3 != 0)
2099 if (dest_len > (unsigned int) real_dest->max_length)
2102 if (source_len == 0)
2105 if (!open_gap (dest_len, real_dest, insert_at))
2108 d = real_dest->str + insert_at;
2109 s = real_source->str + start;
2110 final_end = real_source->str + (start + source_len);
2111 triplet_end = final_end - (source_len % 3);
2112 _dbus_assert (triplet_end <= final_end);
2113 _dbus_assert ((final_end - triplet_end) < 3);
2115 #define ENCODE_64(v) (base64_table[ (unsigned char) (v) ])
2116 #define SIX_BITS_MASK (0x3f)
2117 _dbus_assert (SIX_BITS_MASK < _DBUS_N_ELEMENTS (base64_table));
2119 while (s != triplet_end)
2121 unsigned int triplet;
2123 triplet = s[2] | (s[1] << 8) | (s[0] << 16);
2125 /* Encode each 6 bits. */
2127 *d++ = ENCODE_64 (triplet >> 18);
2128 *d++ = ENCODE_64 ((triplet >> 12) & SIX_BITS_MASK);
2129 *d++ = ENCODE_64 ((triplet >> 6) & SIX_BITS_MASK);
2130 *d++ = ENCODE_64 (triplet & SIX_BITS_MASK);
2135 switch (final_end - triplet_end)
2139 unsigned int doublet;
2141 doublet = s[1] | (s[0] << 8);
2143 *d++ = ENCODE_64 (doublet >> 12);
2144 *d++ = ENCODE_64 ((doublet >> 6) & SIX_BITS_MASK);
2145 *d++ = ENCODE_64 (doublet & SIX_BITS_MASK);
2151 unsigned int singlet;
2155 *d++ = ENCODE_64 ((singlet >> 6) & SIX_BITS_MASK);
2156 *d++ = ENCODE_64 (singlet & SIX_BITS_MASK);
2165 _dbus_assert (d == (real_dest->str + (insert_at + dest_len)));
2171 * Decodes a string from Base64, as documented in RFC 2045.
2173 * @todo sort out the AUDIT comment in here. The case it mentions
2174 * ("====" or "x===") is not allowed in correct base64, so need to
2175 * decide what to do with that kind of input. Probably ignore it
2176 * since we ignore any other junk seen.
2178 * @param source the string to decode
2179 * @param start byte index to start decode
2180 * @param dest string where decoded data should be placed
2181 * @param insert_at where to place decoded data
2182 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
2185 _dbus_string_base64_decode (const DBusString *source,
2194 unsigned int triplet = 0;
2197 DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
2198 _dbus_assert (source != dest);
2200 source_len = real_source->len - start;
2201 s = real_source->str + start;
2202 end = real_source->str + source_len;
2204 if (source_len == 0)
2207 if (!_dbus_string_init (&result))
2214 /* The idea is to just skip anything that isn't
2215 * a base64 char - it's allowed to have whitespace,
2216 * newlines, etc. in here. We also ignore trailing
2217 * base64 chars, though that's suspicious.
2220 if (*s >= UNBASE64_MIN_CHAR &&
2221 *s <= UNBASE64_MAX_CHAR)
2225 /* '=' is padding, doesn't represent additional data
2226 * but does increment our count.
2235 val = unbase64_table[(*s) - UNBASE64_TABLE_OFFSET];
2240 triplet |= (unsigned int) val;
2245 if (sextet_count == 4)
2247 /* no pad = 3 bytes, 1 pad = 2 bytes, 2 pad = 1 byte */
2250 /* AUDIT: Comment doesn't mention 4 pad => 0,
2251 * 3 pad => 1 byte, though the code should
2252 * work fine if those are the required outputs.
2254 * I assume that the spec requires dropping
2255 * the top two bits of, say, ///= which is > 2
2256 * bytes worth of bits. (Or otherwise, you couldn't
2257 * actually represent 2 byte sequences.
2262 if (!_dbus_string_append_byte (&result,
2269 if (!_dbus_string_append_byte (&result,
2270 (triplet >> 8) & 0xff))
2274 if (!_dbus_string_append_byte (&result,
2287 if (!_dbus_string_move (&result, 0, dest, insert_at))
2289 _dbus_string_free (&result);
2293 _dbus_string_free (&result);
2298 _dbus_string_free (&result);
2304 * Encodes a string in hex, the way MD5 and SHA-1 are usually
2305 * encoded. (Each byte is two hex digits.)
2307 * @param source the string to encode
2308 * @param start byte index to start encoding
2309 * @param dest string where encoded data should be placed
2310 * @param insert_at where to place encoded data
2311 * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
2314 _dbus_string_hex_encode (const DBusString *source,
2320 const char hexdigits[16] = {
2321 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2322 'a', 'b', 'c', 'd', 'e', 'f'
2324 const unsigned char *p;
2325 const unsigned char *end;
2328 _dbus_assert (start <= _dbus_string_get_length (source));
2330 if (!_dbus_string_init (&result))
2335 p = (const unsigned char*) _dbus_string_get_const_data (source);
2336 end = p + _dbus_string_get_length (source);
2341 if (!_dbus_string_append_byte (&result,
2342 hexdigits[(*p >> 4)]))
2345 if (!_dbus_string_append_byte (&result,
2346 hexdigits[(*p & 0x0f)]))
2352 if (!_dbus_string_move (&result, 0, dest, insert_at))
2358 _dbus_string_free (&result);
2363 * Decodes a string from hex encoding.
2365 * @param source the string to decode
2366 * @param start byte index to start decode
2367 * @param dest string where decoded data should be placed
2368 * @param insert_at where to place decoded data
2369 * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
2372 _dbus_string_hex_decode (const DBusString *source,
2378 const unsigned char *p;
2379 const unsigned char *end;
2381 dbus_bool_t high_bits;
2383 _dbus_assert (start <= _dbus_string_get_length (source));
2385 if (!_dbus_string_init (&result))
2391 p = (const unsigned char*) _dbus_string_get_const_data (source);
2392 end = p + _dbus_string_get_length (source);
2457 _dbus_verbose ("invalid character '%c' in hex encoded text\n",
2464 if (!_dbus_string_append_byte (&result,
2473 len = _dbus_string_get_length (&result);
2475 b = _dbus_string_get_byte (&result, len - 1);
2479 _dbus_string_set_byte (&result, len - 1, b);
2482 high_bits = !high_bits;
2487 if (!_dbus_string_move (&result, 0, dest, insert_at))
2493 _dbus_string_free (&result);
2498 * Checks that the given range of the string is valid ASCII with no
2499 * nul bytes. If the given range is not entirely contained in the
2500 * string, returns #FALSE.
2502 * @todo this is inconsistent with most of DBusString in that
2503 * it allows a start,len range that isn't in the string.
2505 * @param str the string
2506 * @param start first byte index to check
2507 * @param len number of bytes to check
2508 * @returns #TRUE if the byte range exists and is all valid ASCII
2511 _dbus_string_validate_ascii (const DBusString *str,
2515 const unsigned char *s;
2516 const unsigned char *end;
2517 DBUS_CONST_STRING_PREAMBLE (str);
2518 _dbus_assert (start >= 0);
2519 _dbus_assert (start <= real->len);
2520 _dbus_assert (len >= 0);
2522 if (len > real->len - start)
2525 s = real->str + start;
2530 ((*s & ~0x7f) != 0))
2540 * Checks that the given range of the string is valid UTF-8. If the
2541 * given range is not entirely contained in the string, returns
2542 * #FALSE. If the string contains any nul bytes in the given range,
2543 * returns #FALSE. If the start and start+len are not on character
2544 * boundaries, returns #FALSE.
2546 * @todo this is inconsistent with most of DBusString in that
2547 * it allows a start,len range that isn't in the string.
2549 * @param str the string
2550 * @param start first byte index to check
2551 * @param len number of bytes to check
2552 * @returns #TRUE if the byte range exists and is all valid UTF-8
2555 _dbus_string_validate_utf8 (const DBusString *str,
2559 const unsigned char *p;
2560 const unsigned char *end;
2561 DBUS_CONST_STRING_PREAMBLE (str);
2562 _dbus_assert (start >= 0);
2563 _dbus_assert (start <= real->len);
2564 _dbus_assert (len >= 0);
2566 if (len > real->len - start)
2569 p = real->str + start;
2574 int i, mask = 0, char_len;
2575 dbus_unichar_t result;
2576 unsigned char c = (unsigned char) *p;
2578 UTF8_COMPUTE (c, mask, char_len);
2583 /* check that the expected number of bytes exists in the remaining length */
2584 if ((end - p) < char_len)
2587 UTF8_GET (result, p, i, mask, char_len);
2589 if (UTF8_LENGTH (result) != char_len) /* Check for overlong UTF-8 */
2592 if (result == (dbus_unichar_t)-1)
2595 if (!UNICODE_VALID (result))
2601 /* See that we covered the entire length if a length was
2611 * Checks that the given range of the string is all nul bytes. If the
2612 * given range is not entirely contained in the string, returns
2615 * @todo this is inconsistent with most of DBusString in that
2616 * it allows a start,len range that isn't in the string.
2618 * @param str the string
2619 * @param start first byte index to check
2620 * @param len number of bytes to check
2621 * @returns #TRUE if the byte range exists and is all nul bytes
2624 _dbus_string_validate_nul (const DBusString *str,
2628 const unsigned char *s;
2629 const unsigned char *end;
2630 DBUS_CONST_STRING_PREAMBLE (str);
2631 _dbus_assert (start >= 0);
2632 _dbus_assert (len >= 0);
2633 _dbus_assert (start <= real->len);
2635 if (len > real->len - start)
2638 s = real->str + start;
2651 * Checks that the given range of the string is a valid message name
2652 * in the D-BUS protocol. This includes a length restriction, etc.,
2653 * see the specification. It does not validate UTF-8, that has to be
2654 * done separately for now.
2656 * @todo this is inconsistent with most of DBusString in that
2657 * it allows a start,len range that isn't in the string.
2659 * @param str the string
2660 * @param start first byte index to check
2661 * @param len number of bytes to check
2662 * @returns #TRUE if the byte range exists and is a valid name
2665 _dbus_string_validate_name (const DBusString *str,
2669 const unsigned char *s;
2670 const unsigned char *end;
2671 dbus_bool_t saw_dot;
2673 DBUS_CONST_STRING_PREAMBLE (str);
2674 _dbus_assert (start >= 0);
2675 _dbus_assert (len >= 0);
2676 _dbus_assert (start <= real->len);
2678 if (len > real->len - start)
2681 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2688 s = real->str + start;
2709 * Checks that the given range of the string is a valid service name
2710 * in the D-BUS protocol. This includes a length restriction, etc.,
2711 * see the specification. It does not validate UTF-8, that has to be
2712 * done separately for now.
2714 * @todo this is inconsistent with most of DBusString in that
2715 * it allows a start,len range that isn't in the string.
2717 * @param str the string
2718 * @param start first byte index to check
2719 * @param len number of bytes to check
2720 * @returns #TRUE if the byte range exists and is a valid name
2723 _dbus_string_validate_service (const DBusString *str,
2727 const unsigned char *s;
2728 const unsigned char *end;
2729 dbus_bool_t saw_dot;
2730 dbus_bool_t is_base_service;
2732 DBUS_CONST_STRING_PREAMBLE (str);
2733 _dbus_assert (start >= 0);
2734 _dbus_assert (len >= 0);
2735 _dbus_assert (start <= real->len);
2737 if (len > real->len - start)
2740 if (len > DBUS_MAXIMUM_NAME_LENGTH)
2746 is_base_service = _dbus_string_get_byte (str, start) == ':';
2747 if (is_base_service)
2748 return TRUE; /* can have any content */
2750 /* non-base-service must have the '.' indicating a namespace */
2753 s = real->str + start;
2770 * Clears all allocated bytes in the string to zero.
2772 * @param str the string
2775 _dbus_string_zero (DBusString *str)
2777 DBUS_STRING_PREAMBLE (str);
2779 memset (real->str - real->align_offset, '\0', real->allocated);
2783 #ifdef DBUS_BUILD_TESTS
2784 #include "dbus-test.h"
2788 test_max_len (DBusString *str,
2793 if (!_dbus_string_set_length (str, max_len - 1))
2794 _dbus_assert_not_reached ("setting len to one less than max should have worked");
2797 if (!_dbus_string_set_length (str, max_len))
2798 _dbus_assert_not_reached ("setting len to max len should have worked");
2800 if (_dbus_string_set_length (str, max_len + 1))
2801 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
2803 if (!_dbus_string_set_length (str, 0))
2804 _dbus_assert_not_reached ("setting len to zero should have worked");
2808 test_base64_roundtrip (const unsigned char *data,
2816 len = strlen (data);
2818 if (!_dbus_string_init (&orig))
2819 _dbus_assert_not_reached ("could not init string");
2821 if (!_dbus_string_init (&encoded))
2822 _dbus_assert_not_reached ("could not init string");
2824 if (!_dbus_string_init (&decoded))
2825 _dbus_assert_not_reached ("could not init string");
2827 if (!_dbus_string_append_len (&orig, data, len))
2828 _dbus_assert_not_reached ("couldn't append orig data");
2830 if (!_dbus_string_base64_encode (&orig, 0, &encoded, 0))
2831 _dbus_assert_not_reached ("could not encode");
2833 if (!_dbus_string_base64_decode (&encoded, 0, &decoded, 0))
2834 _dbus_assert_not_reached ("could not decode");
2836 if (!_dbus_string_equal (&orig, &decoded))
2840 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
2841 _dbus_string_get_length (&orig),
2842 _dbus_string_get_length (&encoded),
2843 _dbus_string_get_length (&decoded));
2844 printf ("Original: %s\n", data);
2845 s = _dbus_string_get_const_data (&decoded);
2846 printf ("Decoded: %s\n", s);
2847 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
2850 _dbus_string_free (&orig);
2851 _dbus_string_free (&encoded);
2852 _dbus_string_free (&decoded);
2856 test_hex_roundtrip (const unsigned char *data,
2864 len = strlen (data);
2866 if (!_dbus_string_init (&orig))
2867 _dbus_assert_not_reached ("could not init string");
2869 if (!_dbus_string_init (&encoded))
2870 _dbus_assert_not_reached ("could not init string");
2872 if (!_dbus_string_init (&decoded))
2873 _dbus_assert_not_reached ("could not init string");
2875 if (!_dbus_string_append_len (&orig, data, len))
2876 _dbus_assert_not_reached ("couldn't append orig data");
2878 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
2879 _dbus_assert_not_reached ("could not encode");
2881 if (!_dbus_string_hex_decode (&encoded, 0, &decoded, 0))
2882 _dbus_assert_not_reached ("could not decode");
2884 if (!_dbus_string_equal (&orig, &decoded))
2888 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
2889 _dbus_string_get_length (&orig),
2890 _dbus_string_get_length (&encoded),
2891 _dbus_string_get_length (&decoded));
2892 printf ("Original: %s\n", data);
2893 s = _dbus_string_get_const_data (&decoded);
2894 printf ("Decoded: %s\n", s);
2895 _dbus_assert_not_reached ("original string not the same as string decoded from base64");
2898 _dbus_string_free (&orig);
2899 _dbus_string_free (&encoded);
2900 _dbus_string_free (&decoded);
2903 typedef void (* TestRoundtripFunc) (const unsigned char *data,
2906 test_roundtrips (TestRoundtripFunc func)
2908 (* func) ("Hello this is a string\n", -1);
2909 (* func) ("Hello this is a string\n1", -1);
2910 (* func) ("Hello this is a string\n12", -1);
2911 (* func) ("Hello this is a string\n123", -1);
2912 (* func) ("Hello this is a string\n1234", -1);
2913 (* func) ("Hello this is a string\n12345", -1);
2917 (* func) ("123", 3);
2918 (* func) ("1234", 4);
2919 (* func) ("12345", 5);
2923 (* func) ("123", 4);
2924 (* func) ("1234", 5);
2925 (* func) ("12345", 6);
2927 unsigned char buf[512];
2931 while (i < _DBUS_N_ELEMENTS (buf))
2937 while (i < _DBUS_N_ELEMENTS (buf))
2947 * @ingroup DBusStringInternals
2948 * Unit test for DBusString.
2950 * @todo Need to write tests for _dbus_string_copy() and
2951 * _dbus_string_move() moving to/from each of start/middle/end of a
2952 * string. Also need tests for _dbus_string_move_len ()
2954 * @returns #TRUE on success.
2957 _dbus_string_test (void)
2964 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 };
2969 while (i < _DBUS_N_ELEMENTS (lens))
2971 if (!_dbus_string_init (&str))
2972 _dbus_assert_not_reached ("failed to init string");
2974 set_max_length (&str, lens[i]);
2976 test_max_len (&str, lens[i]);
2977 _dbus_string_free (&str);
2982 /* Test shortening and setting length */
2984 while (i < _DBUS_N_ELEMENTS (lens))
2988 if (!_dbus_string_init (&str))
2989 _dbus_assert_not_reached ("failed to init string");
2991 set_max_length (&str, lens[i]);
2993 if (!_dbus_string_set_length (&str, lens[i]))
2994 _dbus_assert_not_reached ("failed to set string length");
2999 _dbus_assert (_dbus_string_get_length (&str) == j);
3002 _dbus_string_shorten (&str, 1);
3003 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
3008 _dbus_string_free (&str);
3013 /* Test appending data */
3014 if (!_dbus_string_init (&str))
3015 _dbus_assert_not_reached ("failed to init string");
3020 if (!_dbus_string_append (&str, "a"))
3021 _dbus_assert_not_reached ("failed to append string to string\n");
3023 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
3025 if (!_dbus_string_append_byte (&str, 'b'))
3026 _dbus_assert_not_reached ("failed to append byte to string\n");
3028 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
3033 _dbus_string_free (&str);
3035 /* Check steal_data */
3037 if (!_dbus_string_init (&str))
3038 _dbus_assert_not_reached ("failed to init string");
3040 if (!_dbus_string_append (&str, "Hello World"))
3041 _dbus_assert_not_reached ("could not append to string");
3043 i = _dbus_string_get_length (&str);
3045 if (!_dbus_string_steal_data (&str, &s))
3046 _dbus_assert_not_reached ("failed to steal data");
3048 _dbus_assert (_dbus_string_get_length (&str) == 0);
3049 _dbus_assert (((int)strlen (s)) == i);
3055 if (!_dbus_string_append (&str, "Hello World"))
3056 _dbus_assert_not_reached ("could not append to string");
3058 i = _dbus_string_get_length (&str);
3060 if (!_dbus_string_init (&other))
3061 _dbus_assert_not_reached ("could not init string");
3063 if (!_dbus_string_move (&str, 0, &other, 0))
3064 _dbus_assert_not_reached ("could not move");
3066 _dbus_assert (_dbus_string_get_length (&str) == 0);
3067 _dbus_assert (_dbus_string_get_length (&other) == i);
3069 if (!_dbus_string_append (&str, "Hello World"))
3070 _dbus_assert_not_reached ("could not append to string");
3072 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
3073 _dbus_assert_not_reached ("could not move");
3075 _dbus_assert (_dbus_string_get_length (&str) == 0);
3076 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3078 if (!_dbus_string_append (&str, "Hello World"))
3079 _dbus_assert_not_reached ("could not append to string");
3081 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3082 _dbus_assert_not_reached ("could not move");
3084 _dbus_assert (_dbus_string_get_length (&str) == 0);
3085 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3087 _dbus_string_free (&other);
3091 if (!_dbus_string_append (&str, "Hello World"))
3092 _dbus_assert_not_reached ("could not append to string");
3094 i = _dbus_string_get_length (&str);
3096 if (!_dbus_string_init (&other))
3097 _dbus_assert_not_reached ("could not init string");
3099 if (!_dbus_string_copy (&str, 0, &other, 0))
3100 _dbus_assert_not_reached ("could not copy");
3102 _dbus_assert (_dbus_string_get_length (&str) == i);
3103 _dbus_assert (_dbus_string_get_length (&other) == i);
3105 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
3106 _dbus_assert_not_reached ("could not copy");
3108 _dbus_assert (_dbus_string_get_length (&str) == i);
3109 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
3110 _dbus_assert (_dbus_string_equal_c_str (&other,
3111 "Hello WorldHello World"));
3113 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
3114 _dbus_assert_not_reached ("could not copy");
3116 _dbus_assert (_dbus_string_get_length (&str) == i);
3117 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
3118 _dbus_assert (_dbus_string_equal_c_str (&other,
3119 "Hello WorldHello WorldHello World"));
3121 _dbus_string_free (&str);
3122 _dbus_string_free (&other);
3126 if (!_dbus_string_init (&str))
3127 _dbus_assert_not_reached ("failed to init string");
3129 if (!_dbus_string_append (&str, "Hello World"))
3130 _dbus_assert_not_reached ("could not append to string");
3132 i = _dbus_string_get_length (&str);
3134 if (!_dbus_string_init (&other))
3135 _dbus_assert_not_reached ("could not init string");
3137 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3138 &other, 0, _dbus_string_get_length (&other)))
3139 _dbus_assert_not_reached ("could not replace");
3141 _dbus_assert (_dbus_string_get_length (&str) == i);
3142 _dbus_assert (_dbus_string_get_length (&other) == i);
3143 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
3145 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
3147 _dbus_assert_not_reached ("could not replace center space");
3149 _dbus_assert (_dbus_string_get_length (&str) == i);
3150 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3151 _dbus_assert (_dbus_string_equal_c_str (&other,
3152 "HelloHello WorldWorld"));
3155 if (!_dbus_string_replace_len (&str, 1, 1,
3157 _dbus_string_get_length (&other) - 1,
3159 _dbus_assert_not_reached ("could not replace end character");
3161 _dbus_assert (_dbus_string_get_length (&str) == i);
3162 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
3163 _dbus_assert (_dbus_string_equal_c_str (&other,
3164 "HelloHello WorldWorle"));
3166 _dbus_string_free (&str);
3167 _dbus_string_free (&other);
3169 /* Check append/get unichar */
3171 if (!_dbus_string_init (&str))
3172 _dbus_assert_not_reached ("failed to init string");
3175 if (!_dbus_string_append_unichar (&str, 0xfffc))
3176 _dbus_assert_not_reached ("failed to append unichar");
3178 _dbus_string_get_unichar (&str, 0, &ch, &i);
3180 _dbus_assert (ch == 0xfffc);
3181 _dbus_assert (i == _dbus_string_get_length (&str));
3183 _dbus_string_free (&str);
3185 /* Check insert/set/get byte */
3187 if (!_dbus_string_init (&str))
3188 _dbus_assert_not_reached ("failed to init string");
3190 if (!_dbus_string_append (&str, "Hello"))
3191 _dbus_assert_not_reached ("failed to append Hello");
3193 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
3194 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
3195 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
3196 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
3197 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
3199 _dbus_string_set_byte (&str, 1, 'q');
3200 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
3202 if (!_dbus_string_insert_byte (&str, 0, 255))
3203 _dbus_assert_not_reached ("can't insert byte");
3205 if (!_dbus_string_insert_byte (&str, 2, 'Z'))
3206 _dbus_assert_not_reached ("can't insert byte");
3208 if (!_dbus_string_insert_byte (&str, _dbus_string_get_length (&str), 'W'))
3209 _dbus_assert_not_reached ("can't insert byte");
3211 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
3212 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
3213 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
3214 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'q');
3215 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'l');
3216 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'l');
3217 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'o');
3218 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'W');
3220 _dbus_string_free (&str);
3222 /* Check append/parse int/double */
3224 if (!_dbus_string_init (&str))
3225 _dbus_assert_not_reached ("failed to init string");
3227 if (!_dbus_string_append_int (&str, 27))
3228 _dbus_assert_not_reached ("failed to append int");
3230 i = _dbus_string_get_length (&str);
3232 if (!_dbus_string_parse_int (&str, 0, &v, &end))
3233 _dbus_assert_not_reached ("failed to parse int");
3235 _dbus_assert (v == 27);
3236 _dbus_assert (end == i);
3238 _dbus_string_free (&str);
3240 if (!_dbus_string_init (&str))
3241 _dbus_assert_not_reached ("failed to init string");
3243 if (!_dbus_string_append_double (&str, 50.3))
3244 _dbus_assert_not_reached ("failed to append float");
3246 i = _dbus_string_get_length (&str);
3248 if (!_dbus_string_parse_double (&str, 0, &d, &end))
3249 _dbus_assert_not_reached ("failed to parse float");
3251 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
3252 _dbus_assert (end == i);
3254 _dbus_string_free (&str);
3257 if (!_dbus_string_init (&str))
3258 _dbus_assert_not_reached ("failed to init string");
3260 if (!_dbus_string_append (&str, "Hello"))
3261 _dbus_assert_not_reached ("couldn't append to string");
3263 if (!_dbus_string_find (&str, 0, "He", &i))
3264 _dbus_assert_not_reached ("didn't find 'He'");
3265 _dbus_assert (i == 0);
3267 if (!_dbus_string_find (&str, 0, "Hello", &i))
3268 _dbus_assert_not_reached ("didn't find 'Hello'");
3269 _dbus_assert (i == 0);
3271 if (!_dbus_string_find (&str, 0, "ello", &i))
3272 _dbus_assert_not_reached ("didn't find 'ello'");
3273 _dbus_assert (i == 1);
3275 if (!_dbus_string_find (&str, 0, "lo", &i))
3276 _dbus_assert_not_reached ("didn't find 'lo'");
3277 _dbus_assert (i == 3);
3279 if (!_dbus_string_find (&str, 2, "lo", &i))
3280 _dbus_assert_not_reached ("didn't find 'lo'");
3281 _dbus_assert (i == 3);
3283 if (_dbus_string_find (&str, 4, "lo", &i))
3284 _dbus_assert_not_reached ("did find 'lo'");
3286 if (!_dbus_string_find (&str, 0, "l", &i))
3287 _dbus_assert_not_reached ("didn't find 'l'");
3288 _dbus_assert (i == 2);
3290 if (!_dbus_string_find (&str, 0, "H", &i))
3291 _dbus_assert_not_reached ("didn't find 'H'");
3292 _dbus_assert (i == 0);
3294 if (!_dbus_string_find (&str, 0, "", &i))
3295 _dbus_assert_not_reached ("didn't find ''");
3296 _dbus_assert (i == 0);
3298 if (_dbus_string_find (&str, 0, "Hello!", NULL))
3299 _dbus_assert_not_reached ("Did find 'Hello!'");
3301 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
3302 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
3304 if (_dbus_string_find (&str, 0, "ill", NULL))
3305 _dbus_assert_not_reached ("Did find 'ill'");
3307 if (_dbus_string_find (&str, 0, "q", NULL))
3308 _dbus_assert_not_reached ("Did find 'q'");
3310 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
3311 _dbus_assert_not_reached ("Didn't find 'He'");
3313 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
3314 _dbus_assert_not_reached ("Did find 'Hello'");
3316 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
3317 _dbus_assert_not_reached ("Did not find 'H'");
3318 _dbus_assert (i == 0);
3320 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
3321 _dbus_assert_not_reached ("Did not find 'o'");
3322 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
3324 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
3325 _dbus_assert_not_reached ("Did find 'o'");
3326 _dbus_assert (i == -1);
3328 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
3329 _dbus_assert_not_reached ("Did find 'e'");
3330 _dbus_assert (i == -1);
3332 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
3333 _dbus_assert_not_reached ("Didn't find 'e'");
3334 _dbus_assert (i == 1);
3336 _dbus_string_free (&str);
3338 /* Base 64 and Hex encoding */
3339 test_roundtrips (test_base64_roundtrip);
3340 test_roundtrips (test_hex_roundtrip);
3345 #endif /* DBUS_BUILD_TESTS */