/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-string.c String utility class (internal to D-BUS implementation)
*
- * Copyright (C) 2002 Red Hat, Inc.
+ * Copyright (C) 2002, 2003 Red Hat, Inc.
*
* Licensed under the Academic Free License version 1.2
*
#include "dbus-string.h"
/* we allow a system header here, for speed/convenience */
#include <string.h>
+#include "dbus-marshal.h"
+#define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
+#include "dbus-string-private.h"
/**
* @defgroup DBusString string class
*/
/**
- * @brief Internals of DBusString.
- *
- * DBusString internals. DBusString is an opaque objects, it must be
- * used via accessor functions.
+ * We allocate 1 byte for nul termination, plus 7 bytes for possible
+ * align_offset, so we always need 8 bytes on top of the string's
+ * length to be in the allocated block.
*/
-typedef struct
-{
- unsigned char *str; /**< String data, plus nul termination */
- int len; /**< Length without nul */
- int allocated; /**< Allocated size of data */
- int max_length; /**< Max length of this string. */
- unsigned int constant : 1; /**< String data is not owned by DBusString */
- unsigned int locked : 1; /**< DBusString has been locked and can't be changed */
- unsigned int invalid : 1; /**< DBusString is invalid (e.g. already freed) */
-} DBusRealString;
+#define ALLOCATION_PADDING 8
+
+/**
+ * This is the maximum max length (and thus also the maximum length)
+ * of a DBusString
+ */
+#define MAX_MAX_LENGTH (_DBUS_INT_MAX - ALLOCATION_PADDING)
/**
* Checks a bunch of assertions about a string object
*
* @param real the DBusRealString
*/
-#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)
+#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)
/**
* Checks assertions about a string object that needs to be
* @{
*/
-/** Assert that the string's memory is 8-byte aligned.
- *
- * @todo Currently we just hope libc returns 8-byte aligned memory
- * (which is true for GNU libc), but really we need to ensure it by
- * allocating 8 extra bytes and keeping an "align_offset : 3" field
- * in DBusString, or something along those lines.
- */
-#define ASSERT_8_BYTE_ALIGNED(s) \
- _dbus_assert (_DBUS_ALIGN_ADDRESS (((const DBusRealString*)s)->str, 8) == ((const DBusRealString*)s)->str)
+static void
+fixup_alignment (DBusRealString *real)
+{
+ char *aligned;
+ char *real_block;
+ unsigned int old_align_offset;
+
+ /* we have to have extra space in real->allocated for the align offset and nul byte */
+ _dbus_assert (real->len <= real->allocated - ALLOCATION_PADDING);
+
+ old_align_offset = real->align_offset;
+ real_block = real->str - old_align_offset;
+
+ aligned = _DBUS_ALIGN_ADDRESS (real_block, 8);
+
+ real->align_offset = aligned - real_block;
+ real->str = aligned;
+
+ if (old_align_offset != real->align_offset)
+ {
+ /* Here comes the suck */
+ memmove (real_block + real->align_offset,
+ real_block + old_align_offset,
+ real->len + 1);
+ }
+
+ _dbus_assert (real->align_offset < 8);
+ _dbus_assert (_DBUS_ALIGN_ADDRESS (real->str, 8) == real->str);
+}
+
+static void
+undo_alignment (DBusRealString *real)
+{
+ if (real->align_offset != 0)
+ {
+ memmove (real->str - real->align_offset,
+ real->str,
+ real->len + 1);
+
+ real->str = real->str - real->align_offset;
+ real->align_offset = 0;
+ }
+}
/**
- * Initializes a string. The maximum length may be _DBUS_INT_MAX for
- * no maximum. The string starts life with zero length.
- * The string must eventually be freed with _dbus_string_free().
- *
- * @todo the max length feature is useless, because it looks
- * to the app like out of memory, and the app might try
- * to "recover" - but recovery in this case is impossible,
- * as we can't ever "get more memory" - so should delete the
- * max length feature I think.
- *
- * @todo we could make this init routine not alloc any memory and
- * return void, would simplify a lot of code, however it might
- * complexify things elsewhere because _dbus_string_get_data()
- * etc. could suddenly fail as they'd need to alloc new memory.
+ * Initializes a string. The string starts life with zero length. The
+ * string must eventually be freed with _dbus_string_free().
*
* @param str memory to hold the string
- * @param max_length the maximum size of the string
- * @returns #TRUE on success
+ * @returns #TRUE on success, #FALSE if no memory
*/
dbus_bool_t
-_dbus_string_init (DBusString *str,
- int max_length)
+_dbus_string_init (DBusString *str)
{
DBusRealString *real;
_dbus_assert (str != NULL);
- _dbus_assert (max_length >= 0);
_dbus_assert (sizeof (DBusString) == sizeof (DBusRealString));
* an existing string, e.g. in _dbus_string_steal_data()
*/
-#define INITIAL_ALLOC 2
-
- real->str = dbus_malloc (INITIAL_ALLOC);
+ real->str = dbus_malloc (ALLOCATION_PADDING);
if (real->str == NULL)
return FALSE;
- real->allocated = INITIAL_ALLOC;
+ real->allocated = ALLOCATION_PADDING;
real->len = 0;
real->str[real->len] = '\0';
- real->max_length = max_length;
+ real->max_length = MAX_MAX_LENGTH;
real->constant = FALSE;
real->locked = FALSE;
real->invalid = FALSE;
-
- ASSERT_8_BYTE_ALIGNED (str);
+ real->align_offset = 0;
+
+ fixup_alignment (real);
return TRUE;
}
+/* The max length thing is sort of a historical artifact
+ * from a feature that turned out to be dumb; perhaps
+ * we should purge it entirely. The problem with
+ * the feature is that it looks like memory allocation
+ * failure, but is not a transient or resolvable failure.
+ */
+static void
+set_max_length (DBusString *str,
+ int max_length)
+{
+ DBusRealString *real;
+
+ real = (DBusRealString*) str;
+
+ real->max_length = max_length;
+}
+
/**
* Initializes a constant string. The value parameter is not copied
* (should be static), and the string may never be modified.
* It is safe but not necessary to call _dbus_string_free()
- * on a const string.
+ * on a const string. The string has a length limit of MAXINT - 8.
*
* @param str memory to use for the string
* @param value a string to be stored in str (not copied!!!)
_dbus_string_init_const (DBusString *str,
const char *value)
{
+ _dbus_assert (value != NULL);
+
_dbus_string_init_const_len (str, value,
strlen (value));
}
_dbus_assert (str != NULL);
_dbus_assert (value != NULL);
-
+ _dbus_assert (len <= MAX_MAX_LENGTH);
+ _dbus_assert (len >= 0);
+
real = (DBusRealString*) str;
real->str = (char*) value;
real->len = len;
- real->allocated = real->len;
- real->max_length = real->len;
+ real->allocated = real->len + ALLOCATION_PADDING; /* a lie, just to avoid special-case assertions... */
+ real->max_length = real->len + 1;
real->constant = TRUE;
real->invalid = FALSE;
void
_dbus_string_free (DBusString *str)
{
- DBUS_LOCKED_STRING_PREAMBLE (str);
+ DBusRealString *real = (DBusRealString*) str;
+ DBUS_GENERIC_STRING_PREAMBLE (real);
if (real->constant)
return;
-
- dbus_free (real->str);
+ dbus_free (real->str - real->align_offset);
real->invalid = TRUE;
}
/**
- * Locks a string such that any attempts to change the string
- * will result in aborting the program. Also, if the string
- * is wasting a lot of memory (allocation is larger than what
- * the string is really using), _dbus_string_lock() will realloc
- * the string's data to "compact" it.
+ * Locks a string such that any attempts to change the string will
+ * result in aborting the program. Also, if the string is wasting a
+ * lot of memory (allocation is sufficiently larger than what the
+ * string is really using), _dbus_string_lock() will realloc the
+ * string's data to "compact" it.
*
* @param str the string to lock.
*/
/* Try to realloc to avoid excess memory usage, since
* we know we won't change the string further
*/
-#define MAX_WASTE 24
- if (real->allocated > (real->len + MAX_WASTE))
+#define MAX_WASTE 48
+ if (real->allocated - MAX_WASTE > real->len)
{
char *new_str;
int new_allocated;
- new_allocated = real->len + 1;
+ new_allocated = real->len + ALLOCATION_PADDING;
- new_str = dbus_realloc (real->str, new_allocated);
+ new_str = dbus_realloc (real->str - real->align_offset,
+ new_allocated);
if (new_str != NULL)
{
- real->str = new_str;
+ real->str = new_str + real->align_offset;
real->allocated = new_allocated;
- ASSERT_8_BYTE_ALIGNED (str);
+ fixup_alignment (real);
}
}
}
+static dbus_bool_t
+set_length (DBusRealString *real,
+ int new_length)
+{
+ /* Note, we are setting the length without nul termination */
+
+ /* exceeding max length is the same as failure to allocate memory */
+ if (new_length > real->max_length)
+ return FALSE;
+
+ if (new_length > (real->allocated - ALLOCATION_PADDING))
+ {
+ int new_allocated;
+ char *new_str;
+
+ /* at least double our old allocation to avoid O(n), avoiding
+ * overflow
+ */
+ if (real->allocated > (MAX_MAX_LENGTH + ALLOCATION_PADDING) / 2)
+ new_allocated = MAX_MAX_LENGTH + ALLOCATION_PADDING;
+ else
+ new_allocated = real->allocated * 2;
+
+ /* But be sure we always alloc at least space for the new length */
+ new_allocated = MAX (real->allocated, new_length + ALLOCATION_PADDING);
+
+ new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
+ if (new_str == NULL)
+ return FALSE;
+
+ real->str = new_str + real->align_offset;
+ real->allocated = new_allocated;
+ fixup_alignment (real);
+ }
+
+ real->len = new_length;
+ real->str[real->len] = '\0';
+
+ return TRUE;
+}
+
+static dbus_bool_t
+open_gap (int len,
+ DBusRealString *dest,
+ int insert_at)
+{
+ if (len == 0)
+ return TRUE;
+
+ if (len > dest->max_length - dest->len)
+ return FALSE; /* detected overflow of dest->len + len below */
+
+ if (!set_length (dest, dest->len + len))
+ return FALSE;
+
+ memmove (dest->str + insert_at + len,
+ dest->str + insert_at,
+ dest->len - len - insert_at);
+
+ return TRUE;
+}
+
/**
* Gets the raw character buffer from the string. The returned buffer
* will be nul-terminated, but note that strings may contain binary
* function on a const string.
*
* @param str the string
- * @param data_return place to store the returned data
+ * @returns the data
*/
-void
-_dbus_string_get_data (DBusString *str,
- char **data_return)
+char*
+_dbus_string_get_data (DBusString *str)
{
DBUS_STRING_PREAMBLE (str);
- _dbus_assert (data_return != NULL);
- *data_return = real->str;
+ return real->str;
}
/**
- * Gets the raw character buffer from a const string.
+ * Gets the raw character buffer from a const string.
*
* @param str the string
- * @param data_return location to store returned data
+ * @returns the string data
*/
-void
-_dbus_string_get_const_data (const DBusString *str,
- const char **data_return)
+const char*
+_dbus_string_get_const_data (const DBusString *str)
{
DBUS_CONST_STRING_PREAMBLE (str);
- _dbus_assert (data_return != NULL);
- *data_return = real->str;
+ return real->str;
}
/**
* string, not at start + len.
*
* @param str the string
- * @param data_return location to return the buffer
* @param start byte offset to return
* @param len length of segment to return
+ * @returns the string data
*/
-void
+char*
_dbus_string_get_data_len (DBusString *str,
- char **data_return,
int start,
int len)
{
DBUS_STRING_PREAMBLE (str);
- _dbus_assert (data_return != NULL);
_dbus_assert (start >= 0);
_dbus_assert (len >= 0);
- _dbus_assert ((start + len) <= real->len);
+ _dbus_assert (start <= real->len);
+ _dbus_assert (len <= real->len - start);
- *data_return = real->str + start;
+ return real->str + start;
}
/**
* const version of _dbus_string_get_data_len().
*
+ * @todo should return the const char* instead of using an out param;
+ * the temporary variable encourages a bug where you use const data
+ * after modifying the string and possibly causing a realloc.
+ *
* @param str the string
- * @param data_return location to return the buffer
* @param start byte offset to return
* @param len length of segment to return
+ * @returns the string data
*/
-void
+const char*
_dbus_string_get_const_data_len (const DBusString *str,
- const char **data_return,
int start,
int len)
{
DBUS_CONST_STRING_PREAMBLE (str);
- _dbus_assert (data_return != NULL);
_dbus_assert (start >= 0);
_dbus_assert (len >= 0);
- _dbus_assert ((start + len) <= real->len);
+ _dbus_assert (start <= real->len);
+ _dbus_assert (len <= real->len - start);
- *data_return = real->str + start;
+ return real->str + start;
+}
+
+/**
+ * Sets the value of the byte at the given position.
+ *
+ * @param str the string
+ * @param i the position
+ * @param byte the new value
+ */
+void
+_dbus_string_set_byte (DBusString *str,
+ int i,
+ unsigned char byte)
+{
+ DBUS_STRING_PREAMBLE (str);
+ _dbus_assert (i < real->len);
+ _dbus_assert (i >= 0);
+
+ real->str[i] = byte;
}
/**
* @param start the position
* @returns the byte at that position
*/
-char
+unsigned char
_dbus_string_get_byte (const DBusString *str,
int start)
{
DBUS_CONST_STRING_PREAMBLE (str);
_dbus_assert (start < real->len);
-
+ _dbus_assert (start >= 0);
+
return real->str[start];
}
/**
+ * Inserts the given byte at the given position.
+ *
+ * @param str the string
+ * @param i the position
+ * @param byte the value to insert
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+_dbus_string_insert_byte (DBusString *str,
+ int i,
+ unsigned char byte)
+{
+ DBUS_STRING_PREAMBLE (str);
+ _dbus_assert (i <= real->len);
+ _dbus_assert (i >= 0);
+
+ if (!open_gap (1, real, i))
+ return FALSE;
+
+ real->str[i] = byte;
+
+ return TRUE;
+}
+
+/**
* Like _dbus_string_get_data(), but removes the
* gotten data from the original string. The caller
* must free the data returned. This function may
_dbus_string_steal_data (DBusString *str,
char **data_return)
{
+ int old_max_length;
DBUS_STRING_PREAMBLE (str);
_dbus_assert (data_return != NULL);
+
+ undo_alignment (real);
*data_return = real->str;
+ old_max_length = real->max_length;
+
/* reset the string */
- if (!_dbus_string_init (str, real->max_length))
+ if (!_dbus_string_init (str))
{
/* hrm, put it back then */
real->str = *data_return;
*data_return = NULL;
+ fixup_alignment (real);
return FALSE;
}
+ real->max_length = old_max_length;
+
return TRUE;
}
* function may fail due to lack of memory, and return #FALSE.
* The returned string is nul-terminated and has length len.
*
+ * @todo this function is broken because on failure it
+ * may corrupt the source string.
+ *
* @param str the string
* @param data_return location to return the buffer
* @param start the start of segment to steal
int len)
{
DBusString dest;
-
DBUS_STRING_PREAMBLE (str);
_dbus_assert (data_return != NULL);
_dbus_assert (start >= 0);
_dbus_assert (len >= 0);
- _dbus_assert ((start + len) <= real->len);
+ _dbus_assert (start <= real->len);
+ _dbus_assert (len <= real->len - start);
- if (!_dbus_string_init (&dest, real->max_length))
+ if (!_dbus_string_init (&dest))
return FALSE;
+ set_max_length (&dest, real->max_length);
+
if (!_dbus_string_move_len (str, start, len, &dest, 0))
{
_dbus_string_free (&dest);
return FALSE;
}
-
+
+ _dbus_warn ("Broken code in _dbus_string_steal_data_len(), see @todo, FIXME\n");
if (!_dbus_string_steal_data (&dest, data_return))
{
_dbus_string_free (&dest);
return TRUE;
}
+
/**
- * Gets the length of a string (not including nul termination).
+ * Copies the data from the string into a char*
*
- * @returns the length.
+ * @param str the string
+ * @param data_return place to return the data
+ * @returns #TRUE on success, #FALSE on no memory
*/
-int
-_dbus_string_get_length (const DBusString *str)
+dbus_bool_t
+_dbus_string_copy_data (const DBusString *str,
+ char **data_return)
{
DBUS_CONST_STRING_PREAMBLE (str);
+ _dbus_assert (data_return != NULL);
- return real->len;
+ *data_return = dbus_malloc (real->len + 1);
+ if (*data_return == NULL)
+ return FALSE;
+
+ memcpy (*data_return, real->str, real->len + 1);
+
+ return TRUE;
}
-static dbus_bool_t
-set_length (DBusRealString *real,
- int new_length)
+/**
+ * Copies a segment of the string into a char*
+ *
+ * @param str the string
+ * @param data_return place to return the data
+ * @param start start index
+ * @param len length to copy
+ * @returns #FALSE if no memory
+ */
+dbus_bool_t
+_dbus_string_copy_data_len (const DBusString *str,
+ char **data_return,
+ int start,
+ int len)
{
- /* Note, we are setting the length without nul termination */
+ DBusString dest;
- /* exceeding max length is the same as failure to allocate memory */
- if (new_length > real->max_length)
+ DBUS_CONST_STRING_PREAMBLE (str);
+ _dbus_assert (data_return != NULL);
+ _dbus_assert (start >= 0);
+ _dbus_assert (len >= 0);
+ _dbus_assert (start <= real->len);
+ _dbus_assert (len <= real->len - start);
+
+ if (!_dbus_string_init (&dest))
return FALSE;
-
- while (new_length >= real->allocated)
- {
- int new_allocated;
- char *new_str;
-
- new_allocated = 2 + real->allocated * 2;
- if (new_allocated < real->allocated)
- return FALSE; /* overflow */
-
- new_str = dbus_realloc (real->str, new_allocated);
- if (new_str == NULL)
- return FALSE;
- real->str = new_str;
- real->allocated = new_allocated;
+ set_max_length (&dest, real->max_length);
- ASSERT_8_BYTE_ALIGNED (real);
+ if (!_dbus_string_copy_len (str, start, len, &dest, 0))
+ {
+ _dbus_string_free (&dest);
+ return FALSE;
}
- real->len = new_length;
- real->str[real->len] = '\0';
+ if (!_dbus_string_steal_data (&dest, data_return))
+ {
+ _dbus_string_free (&dest);
+ return FALSE;
+ }
+ _dbus_string_free (&dest);
return TRUE;
}
/**
+ * Gets the length of a string (not including nul termination).
+ *
+ * @returns the length.
+ */
+int
+_dbus_string_get_length (const DBusString *str)
+{
+ DBUS_CONST_STRING_PREAMBLE (str);
+
+ return real->len;
+}
+
+/**
* Makes a string longer by the given number of bytes. Checks whether
* adding additional_length to the current length would overflow an
* integer, and checks for exceeding a string's max length.
* The new bytes are not initialized, other than nul-terminating
* the end of the string. The uninitialized bytes may contain
- * unexpected nul bytes or other junk.
+ * nul bytes or other junk.
*
* @param str a string
* @param additional_length length to add to the string.
{
DBUS_STRING_PREAMBLE (str);
_dbus_assert (additional_length >= 0);
-
- if ((real->len + additional_length) < real->len)
- return FALSE; /* overflow */
+
+ if (additional_length > real->max_length - real->len)
+ return FALSE; /* would overflow */
return set_length (real,
real->len + additional_length);
_dbus_string_align_length (DBusString *str,
int alignment)
{
- int new_len;
+ unsigned long new_len; /* ulong to avoid _DBUS_ALIGN_VALUE overflow */
int delta;
DBUS_STRING_PREAMBLE (str);
_dbus_assert (alignment >= 1);
- _dbus_assert (alignment <= 16); /* arbitrary */
+ _dbus_assert (alignment <= 8); /* it has to be a bug if > 8 */
new_len = _DBUS_ALIGN_VALUE (real->len, alignment);
-
+ if (new_len > (unsigned long) real->max_length)
+ return FALSE;
+
delta = new_len - real->len;
_dbus_assert (delta >= 0);
_dbus_string_append (DBusString *str,
const char *buffer)
{
- int buffer_len;
+ unsigned long buffer_len;
DBUS_STRING_PREAMBLE (str);
_dbus_assert (buffer != NULL);
buffer_len = strlen (buffer);
-
+ if (buffer_len > (unsigned long) real->max_length)
+ return FALSE;
+
return append (real, buffer, buffer_len);
}
len = 6;
}
+ if (len > (real->max_length - real->len))
+ return FALSE; /* real->len + len would overflow */
+
if (!set_length (real, real->len + len))
return FALSE;
DBUS_STRING_PREAMBLE (str);
_dbus_assert (start >= 0);
_dbus_assert (len >= 0);
- _dbus_assert ((start + len) <= real->len);
+ _dbus_assert (start <= real->len);
+ _dbus_assert (len <= real->len - start);
delete (real, start, len);
}
static dbus_bool_t
-open_gap (int len,
- DBusRealString *dest,
- int insert_at)
-{
- if (len == 0)
- return TRUE;
-
- if (!set_length (dest, dest->len + len))
- return FALSE;
-
- memmove (dest->str + insert_at + len,
- dest->str + insert_at,
- dest->len - len - insert_at);
-
- return TRUE;
-}
-
-static dbus_bool_t
copy (DBusRealString *source,
int start,
int len,
_dbus_assert ((source) != (dest)); \
DBUS_GENERIC_STRING_PREAMBLE (real_source); \
DBUS_GENERIC_STRING_PREAMBLE (real_dest); \
- _dbus_assert (!real_source->constant); \
- _dbus_assert (!real_source->locked); \
_dbus_assert (!real_dest->constant); \
_dbus_assert (!real_dest->locked); \
_dbus_assert ((start) >= 0); \
{
DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
_dbus_assert (len >= 0);
- _dbus_assert ((start + len) <= real_source->len);
+ _dbus_assert (start <= real_source->len);
+ _dbus_assert (len <= real_source->len - start);
return copy (real_source, start, len,
real_dest,
*
* @todo optimize the case where the two lengths are the same, and
* avoid memmoving the data in the trailing part of the string twice.
+ *
+ * @todo avoid inserting the source into dest, then deleting
+ * the replaced chunk of dest (which creates a potentially large
+ * intermediate string). Instead, extend the replaced chunk
+ * of dest with padding to the same size as the source chunk,
+ * then copy in the source bytes.
*
* @param source the source string
* @param start where to start copying the source string
{
DBUS_STRING_COPY_PREAMBLE (source, start, dest, replace_at);
_dbus_assert (len >= 0);
- _dbus_assert ((start + len) <= real_source->len);
+ _dbus_assert (start <= real_source->len);
+ _dbus_assert (len <= real_source->len - start);
_dbus_assert (replace_at >= 0);
- _dbus_assert ((replace_at + replace_len) <= real_dest->len);
+ _dbus_assert (replace_at <= real_dest->len);
+ _dbus_assert (replace_len <= real_dest->len - replace_at);
if (!copy (real_source, start, len,
real_dest, replace_at))
return TRUE;
}
-/* Unicode macros from GLib */
+/* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
+ * Pennington, and Tom Tromey are the authors and authorized relicense.
+ */
/** computes length and mask of a unicode character
* @param Char the char
*/
#define UNICODE_VALID(Char) \
((Char) < 0x110000 && \
- ((Char) < 0xD800 || (Char) >= 0xE000) && \
- (Char) != 0xFFFE && (Char) != 0xFFFF)
+ (((Char) & 0xFFFFF800) != 0xD800) && \
+ ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \
+ ((Char) & 0xFFFF) != 0xFFFF)
/**
* Gets a unicode character from a UTF-8 string. Does no validation;
* @param start the start of the UTF-8 character.
* @param ch_return location to return the character
* @param end_return location to return the byte index of next character
- * @returns #TRUE on success, #FALSE otherwise.
*/
void
_dbus_string_get_unichar (const DBusString *str,
unsigned char c;
unsigned char *p;
DBUS_CONST_STRING_PREAMBLE (str);
-
+ _dbus_assert (start >= 0);
+ _dbus_assert (start <= real->len);
+
if (ch_return)
*ch_return = 0;
if (end_return)
const char *substr,
int *found)
{
- int i;
- DBUS_CONST_STRING_PREAMBLE (str);
- _dbus_assert (substr != NULL);
- _dbus_assert (start <= real->len);
-
- /* we always "find" an empty string */
+ return _dbus_string_find_to (str, start,
+ ((const DBusRealString*)str)->len,
+ substr, found);
+}
+
+/**
+ * Finds the given substring in the string,
+ * up to a certain position,
+ * returning #TRUE and filling in the byte index
+ * where the substring was found, if it was found.
+ * Returns #FALSE if the substring wasn't found.
+ * Sets *start to the length of the string if the substring
+ * is not found.
+ *
+ * @param str the string
+ * @param start where to start looking
+ * @param end where to stop looking
+ * @param substr the substring
+ * @param found return location for where it was found, or #NULL
+ * @returns #TRUE if found
+ */
+dbus_bool_t
+_dbus_string_find_to (const DBusString *str,
+ int start,
+ int end,
+ const char *substr,
+ int *found)
+{
+ int i;
+ DBUS_CONST_STRING_PREAMBLE (str);
+ _dbus_assert (substr != NULL);
+ _dbus_assert (start <= real->len);
+ _dbus_assert (start >= 0);
+ _dbus_assert (substr != NULL);
+ _dbus_assert (end <= real->len);
+ _dbus_assert (start <= end);
+
+ /* we always "find" an empty string */
if (*substr == '\0')
{
if (found)
- *found = 0;
+ *found = start;
return TRUE;
}
-
+
i = start;
- while (i < real->len)
+ while (i < end)
{
if (real->str[i] == substr[0])
{
int j = i + 1;
- while (j < real->len)
+ while (j < end)
{
if (substr[j - i] == '\0')
break;
}
if (found)
- *found = real->len;
+ *found = end;
- return FALSE;
+ return FALSE;
}
/**
int i;
DBUS_CONST_STRING_PREAMBLE (str);
_dbus_assert (start <= real->len);
+ _dbus_assert (start >= 0);
i = start;
while (i < real->len)
/**
* Skips blanks from start, storing the first non-blank in *end
+ * (blank is space or tab).
*
* @param str the string
* @param start where to start
int i;
DBUS_CONST_STRING_PREAMBLE (str);
_dbus_assert (start <= real->len);
+ _dbus_assert (start >= 0);
+
+ i = start;
+ while (i < real->len)
+ {
+ if (!(real->str[i] == ' ' ||
+ real->str[i] == '\t'))
+ break;
+
+ ++i;
+ }
+
+ _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
+ real->str[i] == '\t'));
+
+ if (end)
+ *end = i;
+}
+
+/**
+ * Skips whitespace from start, storing the first non-whitespace in *end.
+ * (whitespace is space, tab, newline, CR).
+ *
+ * @param str the string
+ * @param start where to start
+ * @param end where to store the first non-whitespace byte index
+ */
+void
+_dbus_string_skip_white (const DBusString *str,
+ int start,
+ int *end)
+{
+ int i;
+ DBUS_CONST_STRING_PREAMBLE (str);
+ _dbus_assert (start <= real->len);
+ _dbus_assert (start >= 0);
i = start;
while (i < real->len)
{
if (!(real->str[i] == ' ' ||
+ real->str[i] == '\n' ||
+ real->str[i] == '\r' ||
real->str[i] == '\t'))
break;
}
/**
+ * Assigns a newline-terminated or \r\n-terminated line from the front
+ * of the string to the given dest string. The dest string's previous
+ * contents are deleted. If the source string contains no newline,
+ * moves the entire source string to the dest string.
+ *
+ * @todo owen correctly notes that this is a stupid function (it was
+ * written purely for test code,
+ * e.g. dbus-message-builder.c). Probably should be enforced as test
+ * code only with #ifdef DBUS_BUILD_TESTS
+ *
+ * @param source the source string
+ * @param dest the destination string (contents are replaced)
+ * @returns #FALSE if no memory, or source has length 0
+ */
+dbus_bool_t
+_dbus_string_pop_line (DBusString *source,
+ DBusString *dest)
+{
+ int eol;
+ dbus_bool_t have_newline;
+
+ _dbus_string_set_length (dest, 0);
+
+ eol = 0;
+ if (_dbus_string_find (source, 0, "\n", &eol))
+ {
+ have_newline = TRUE;
+ eol += 1; /* include newline */
+ }
+ else
+ {
+ eol = _dbus_string_get_length (source);
+ have_newline = FALSE;
+ }
+
+ if (eol == 0)
+ return FALSE; /* eof */
+
+ if (!_dbus_string_move_len (source, 0, eol,
+ dest, 0))
+ {
+ return FALSE;
+ }
+
+ /* dump the newline and the \r if we have one */
+ if (have_newline)
+ {
+ dbus_bool_t have_cr;
+
+ _dbus_assert (_dbus_string_get_length (dest) > 0);
+
+ if (_dbus_string_get_length (dest) > 1 &&
+ _dbus_string_get_byte (dest,
+ _dbus_string_get_length (dest) - 2) == '\r')
+ have_cr = TRUE;
+ else
+ have_cr = FALSE;
+
+ _dbus_string_set_length (dest,
+ _dbus_string_get_length (dest) -
+ (have_cr ? 2 : 1));
+ }
+
+ return TRUE;
+}
+
+/**
+ * Deletes up to and including the first blank space
+ * in the string.
+ *
+ * @param str the string
+ */
+void
+_dbus_string_delete_first_word (DBusString *str)
+{
+ int i;
+
+ if (_dbus_string_find_blank (str, 0, &i))
+ _dbus_string_skip_blank (str, i, &i);
+
+ _dbus_string_delete (str, 0, i);
+}
+
+/**
+ * Deletes any leading blanks in the string
+ *
+ * @param str the string
+ */
+void
+_dbus_string_delete_leading_blanks (DBusString *str)
+{
+ int i;
+
+ _dbus_string_skip_blank (str, 0, &i);
+
+ if (i > 0)
+ _dbus_string_delete (str, 0, i);
+}
+
+/**
* Tests two DBusString for equality.
*
+ * @todo memcmp is probably faster
+ *
* @param a first string
* @param b second string
* @returns #TRUE if equal
}
/**
+ * Tests two DBusString for equality up to the given length.
+ *
+ * @todo write a unit test
+ *
+ * @todo memcmp is probably faster
+ *
+ * @param a first string
+ * @param b second string
+ * @param len the lengh
+ * @returns #TRUE if equal for the given number of bytes
+ */
+dbus_bool_t
+_dbus_string_equal_len (const DBusString *a,
+ const DBusString *b,
+ int len)
+{
+ const unsigned char *ap;
+ const unsigned char *bp;
+ const unsigned char *a_end;
+ const DBusRealString *real_a = (const DBusRealString*) a;
+ const DBusRealString *real_b = (const DBusRealString*) b;
+ DBUS_GENERIC_STRING_PREAMBLE (real_a);
+ DBUS_GENERIC_STRING_PREAMBLE (real_b);
+
+ if (real_a->len != real_b->len &&
+ (real_a->len < len || real_b->len < len))
+ return FALSE;
+
+ ap = real_a->str;
+ bp = real_b->str;
+ a_end = real_a->str + MIN (real_a->len, len);
+ while (ap != a_end)
+ {
+ if (*ap != *bp)
+ return FALSE;
+
+ ++ap;
+ ++bp;
+ }
+
+ return TRUE;
+}
+
+/**
* Checks whether a string is equal to a C string.
*
* @param a the string
const unsigned char *a_end;
const DBusRealString *real_a = (const DBusRealString*) a;
DBUS_GENERIC_STRING_PREAMBLE (real_a);
+ _dbus_assert (c_str != NULL);
+
+ ap = real_a->str;
+ bp = (const unsigned char*) c_str;
+ a_end = real_a->str + real_a->len;
+ while (ap != a_end && *bp)
+ {
+ if (*ap != *bp)
+ return FALSE;
+
+ ++ap;
+ ++bp;
+ }
+
+ if (ap != a_end || *bp)
+ return FALSE;
+
+ return TRUE;
+}
+/**
+ * Checks whether a string starts with the given C string.
+ *
+ * @param a the string
+ * @param c_str the C string
+ * @returns #TRUE if string starts with it
+ */
+dbus_bool_t
+_dbus_string_starts_with_c_str (const DBusString *a,
+ const char *c_str)
+{
+ const unsigned char *ap;
+ const unsigned char *bp;
+ const unsigned char *a_end;
+ const DBusRealString *real_a = (const DBusRealString*) a;
+ DBUS_GENERIC_STRING_PREAMBLE (real_a);
+ _dbus_assert (c_str != NULL);
+
ap = real_a->str;
bp = (const unsigned char*) c_str;
a_end = real_a->str + real_a->len;
++bp;
}
- if (*ap && *bp == '\0')
+ if (*bp == '\0')
+ return TRUE;
+ else
return FALSE;
- else if (ap == a_end && *bp)
+}
+
+/**
+ * Returns whether a string ends with the given suffix
+ *
+ * @todo memcmp might make this faster.
+ *
+ * @param a the string
+ * @param c_str the C-style string
+ * @returns #TRUE if the string ends with the suffix
+ */
+dbus_bool_t
+_dbus_string_ends_with_c_str (const DBusString *a,
+ const char *c_str)
+{
+ const unsigned char *ap;
+ const unsigned char *bp;
+ const unsigned char *a_end;
+ unsigned long c_str_len;
+ const DBusRealString *real_a = (const DBusRealString*) a;
+ DBUS_GENERIC_STRING_PREAMBLE (real_a);
+ _dbus_assert (c_str != NULL);
+
+ c_str_len = strlen (c_str);
+ if (((unsigned long)real_a->len) < c_str_len)
return FALSE;
+ ap = real_a->str + (real_a->len - c_str_len);
+ bp = (const unsigned char*) c_str;
+ a_end = real_a->str + real_a->len;
+ while (ap != a_end)
+ {
+ if (*ap != *bp)
+ return FALSE;
+
+ ++ap;
+ ++bp;
+ }
+
+ _dbus_assert (*ap == '\0');
+ _dbus_assert (*bp == '\0');
+
return TRUE;
}
int insert_at)
{
int source_len;
- int dest_len;
+ unsigned int dest_len; /* unsigned for overflow checks below */
const unsigned char *s;
unsigned char *d;
const unsigned char *triplet_end;
DBUS_STRING_COPY_PREAMBLE (source, start, dest, insert_at);
_dbus_assert (source != dest);
- /* For each 24 bits (3 bytes) of input, we have 4 chars of
+ /* For each 24 bits (3 bytes) of input, we have 4 bytes of
* output.
*/
source_len = real_source->len - start;
if (source_len % 3 != 0)
dest_len += 4;
+ if (dest_len > (unsigned int) real_dest->max_length)
+ return FALSE;
+
if (source_len == 0)
return TRUE;
{
unsigned int triplet;
- triplet = s[0] | (s[1] << 8) | (s[2] << 16);
+ triplet = s[2] | (s[1] << 8) | (s[0] << 16);
+
+ /* Encode each 6 bits. */
- /* Encode each 6 bits */
-
*d++ = ENCODE_64 (triplet >> 18);
*d++ = ENCODE_64 ((triplet >> 12) & SIX_BITS_MASK);
*d++ = ENCODE_64 ((triplet >> 6) & SIX_BITS_MASK);
{
unsigned int doublet;
- doublet = s[0] | (s[1] << 8);
-
+ doublet = s[1] | (s[0] << 8);
+
*d++ = ENCODE_64 (doublet >> 12);
*d++ = ENCODE_64 ((doublet >> 6) & SIX_BITS_MASK);
*d++ = ENCODE_64 (doublet & SIX_BITS_MASK);
unsigned int singlet;
singlet = s[0];
-
+
*d++ = ENCODE_64 ((singlet >> 6) & SIX_BITS_MASK);
*d++ = ENCODE_64 (singlet & SIX_BITS_MASK);
*d++ = '=';
return TRUE;
}
-
/**
* Decodes a string from Base64, as documented in RFC 2045.
*
+ * @todo sort out the AUDIT comment in here. The case it mentions
+ * ("====" or "x===") is not allowed in correct base64, so need to
+ * decide what to do with that kind of input. Probably ignore it
+ * since we ignore any other junk seen.
+ *
* @param source the string to decode
* @param start byte index to start decode
* @param dest string where decoded data should be placed
if (source_len == 0)
return TRUE;
- if (!_dbus_string_init (&result, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&result))
return FALSE;
pad_count = 0;
if (sextet_count == 4)
{
/* no pad = 3 bytes, 1 pad = 2 bytes, 2 pad = 1 byte */
+
+
+ /* AUDIT: Comment doesn't mention 4 pad => 0,
+ * 3 pad => 1 byte, though the code should
+ * work fine if those are the required outputs.
+ *
+ * I assume that the spec requires dropping
+ * the top two bits of, say, ///= which is > 2
+ * bytes worth of bits. (Or otherwise, you couldn't
+ * actually represent 2 byte sequences.
+ */
- _dbus_string_append_byte (&result,
- triplet & 0xff);
+ if (pad_count < 1)
+ {
+ if (!_dbus_string_append_byte (&result,
+ triplet >> 16))
+ goto failed;
+ }
if (pad_count < 2)
- _dbus_string_append_byte (&result,
- (triplet >> 8) & 0xff);
-
- if (pad_count < 1)
- _dbus_string_append_byte (&result,
- triplet >> 16);
-
+ {
+ if (!_dbus_string_append_byte (&result,
+ (triplet >> 8) & 0xff))
+ goto failed;
+ }
+
+ if (!_dbus_string_append_byte (&result,
+ triplet & 0xff))
+ goto failed;
+
sextet_count = 0;
pad_count = 0;
triplet = 0;
_dbus_string_free (&result);
return TRUE;
+
+ failed:
+ _dbus_string_free (&result);
+
+ return FALSE;
+}
+
+/**
+ * Encodes a string in hex, the way MD5 and SHA-1 are usually
+ * encoded. (Each byte is two hex digits.)
+ *
+ * @param source the string to encode
+ * @param start byte index to start encoding
+ * @param dest string where encoded data should be placed
+ * @param insert_at where to place encoded data
+ * @returns #TRUE if encoding was successful, #FALSE if no memory etc.
+ */
+dbus_bool_t
+_dbus_string_hex_encode (const DBusString *source,
+ int start,
+ DBusString *dest,
+ int insert_at)
+{
+ DBusString result;
+ const char hexdigits[16] = {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ 'a', 'b', 'c', 'd', 'e', 'f'
+ };
+ const unsigned char *p;
+ const unsigned char *end;
+ dbus_bool_t retval;
+
+ _dbus_assert (start <= _dbus_string_get_length (source));
+
+ if (!_dbus_string_init (&result))
+ return FALSE;
+
+ retval = FALSE;
+
+ p = (const unsigned char*) _dbus_string_get_const_data (source);
+ end = p + _dbus_string_get_length (source);
+ p += start;
+
+ while (p != end)
+ {
+ if (!_dbus_string_append_byte (&result,
+ hexdigits[(*p >> 4)]))
+ goto out;
+
+ if (!_dbus_string_append_byte (&result,
+ hexdigits[(*p & 0x0f)]))
+ goto out;
+
+ ++p;
+ }
+
+ if (!_dbus_string_move (&result, 0, dest, insert_at))
+ goto out;
+
+ retval = TRUE;
+
+ out:
+ _dbus_string_free (&result);
+ return retval;
+}
+
+/**
+ * Decodes a string from hex encoding.
+ *
+ * @param source the string to decode
+ * @param start byte index to start decode
+ * @param dest string where decoded data should be placed
+ * @param insert_at where to place decoded data
+ * @returns #TRUE if decoding was successful, #FALSE if no memory etc.
+ */
+dbus_bool_t
+_dbus_string_hex_decode (const DBusString *source,
+ int start,
+ DBusString *dest,
+ int insert_at)
+{
+ DBusString result;
+ const unsigned char *p;
+ const unsigned char *end;
+ dbus_bool_t retval;
+ dbus_bool_t high_bits;
+
+ _dbus_assert (start <= _dbus_string_get_length (source));
+
+ if (!_dbus_string_init (&result))
+ return FALSE;
+
+ retval = FALSE;
+
+ high_bits = TRUE;
+ p = (const unsigned char*) _dbus_string_get_const_data (source);
+ end = p + _dbus_string_get_length (source);
+ p += start;
+
+ while (p != end)
+ {
+ unsigned int val;
+
+ switch (*p)
+ {
+ case '0':
+ val = 0;
+ break;
+ case '1':
+ val = 1;
+ break;
+ case '2':
+ val = 2;
+ break;
+ case '3':
+ val = 3;
+ break;
+ case '4':
+ val = 4;
+ break;
+ case '5':
+ val = 5;
+ break;
+ case '6':
+ val = 6;
+ break;
+ case '7':
+ val = 7;
+ break;
+ case '8':
+ val = 8;
+ break;
+ case '9':
+ val = 9;
+ break;
+ case 'a':
+ case 'A':
+ val = 10;
+ break;
+ case 'b':
+ case 'B':
+ val = 11;
+ break;
+ case 'c':
+ case 'C':
+ val = 12;
+ break;
+ case 'd':
+ case 'D':
+ val = 13;
+ break;
+ case 'e':
+ case 'E':
+ val = 14;
+ break;
+ case 'f':
+ case 'F':
+ val = 15;
+ break;
+ default:
+ val = 0;
+ _dbus_verbose ("invalid character '%c' in hex encoded text\n",
+ *p);
+ goto out;
+ }
+
+ if (high_bits)
+ {
+ if (!_dbus_string_append_byte (&result,
+ val << 4))
+ goto out;
+ }
+ else
+ {
+ int len;
+ unsigned char b;
+
+ len = _dbus_string_get_length (&result);
+
+ b = _dbus_string_get_byte (&result, len - 1);
+
+ b |= val;
+
+ _dbus_string_set_byte (&result, len - 1, b);
+ }
+
+ high_bits = !high_bits;
+
+ ++p;
+ }
+
+ if (!_dbus_string_move (&result, 0, dest, insert_at))
+ goto out;
+
+ retval = TRUE;
+
+ out:
+ _dbus_string_free (&result);
+ return retval;
}
/**
- * Checks that the given range of the string
- * is valid ASCII. If the given range is not contained
- * in the string, returns #FALSE.
+ * Checks that the given range of the string is valid ASCII with no
+ * nul bytes. If the given range is not entirely contained in the
+ * string, returns #FALSE.
*
+ * @todo this is inconsistent with most of DBusString in that
+ * it allows a start,len range that isn't in the string.
+ *
* @param str the string
* @param start first byte index to check
* @param len number of bytes to check
const unsigned char *end;
DBUS_CONST_STRING_PREAMBLE (str);
_dbus_assert (start >= 0);
+ _dbus_assert (start <= real->len);
_dbus_assert (len >= 0);
- if ((start + len) > real->len)
+ if (len > real->len - start)
return FALSE;
s = real->str + start;
return TRUE;
}
+/**
+ * Checks that the given range of the string is valid UTF-8. If the
+ * given range is not entirely contained in the string, returns
+ * #FALSE. If the string contains any nul bytes in the given range,
+ * returns #FALSE. If the start and start+len are not on character
+ * boundaries, returns #FALSE.
+ *
+ * @todo this is inconsistent with most of DBusString in that
+ * it allows a start,len range that isn't in the string.
+ *
+ * @param str the string
+ * @param start first byte index to check
+ * @param len number of bytes to check
+ * @returns #TRUE if the byte range exists and is all valid UTF-8
+ */
+dbus_bool_t
+_dbus_string_validate_utf8 (const DBusString *str,
+ int start,
+ int len)
+{
+ const unsigned char *p;
+ const unsigned char *end;
+ DBUS_CONST_STRING_PREAMBLE (str);
+ _dbus_assert (start >= 0);
+ _dbus_assert (start <= real->len);
+ _dbus_assert (len >= 0);
+
+ if (len > real->len - start)
+ return FALSE;
+
+ p = real->str + start;
+ end = p + len;
+
+ while (p < end)
+ {
+ int i, mask = 0, char_len;
+ dbus_unichar_t result;
+ unsigned char c = (unsigned char) *p;
+
+ UTF8_COMPUTE (c, mask, char_len);
+
+ if (char_len == -1)
+ break;
+
+ /* check that the expected number of bytes exists in the remaining length */
+ if ((end - p) < char_len)
+ break;
+
+ UTF8_GET (result, p, i, mask, char_len);
+
+ if (UTF8_LENGTH (result) != char_len) /* Check for overlong UTF-8 */
+ break;
+
+ if (result == (dbus_unichar_t)-1)
+ break;
+
+ if (!UNICODE_VALID (result))
+ break;
+
+ p += char_len;
+ }
+
+ /* See that we covered the entire length if a length was
+ * passed in
+ */
+ if (p != end)
+ return FALSE;
+ else
+ return TRUE;
+}
+
+/**
+ * Checks that the given range of the string is all nul bytes. If the
+ * given range is not entirely contained in the string, returns
+ * #FALSE.
+ *
+ * @todo this is inconsistent with most of DBusString in that
+ * it allows a start,len range that isn't in the string.
+ *
+ * @param str the string
+ * @param start first byte index to check
+ * @param len number of bytes to check
+ * @returns #TRUE if the byte range exists and is all nul bytes
+ */
+dbus_bool_t
+_dbus_string_validate_nul (const DBusString *str,
+ int start,
+ int len)
+{
+ const unsigned char *s;
+ const unsigned char *end;
+ DBUS_CONST_STRING_PREAMBLE (str);
+ _dbus_assert (start >= 0);
+ _dbus_assert (len >= 0);
+ _dbus_assert (start <= real->len);
+
+ if (len > real->len - start)
+ return FALSE;
+
+ s = real->str + start;
+ end = s + len;
+ while (s != end)
+ {
+ if (*s != '\0')
+ return FALSE;
+ ++s;
+ }
+
+ return TRUE;
+}
+
+/**
+ * Clears all allocated bytes in the string to zero.
+ *
+ * @param str the string
+ */
+void
+_dbus_string_zero (DBusString *str)
+{
+ DBUS_STRING_PREAMBLE (str);
+
+ memset (real->str, '\0', real->allocated);
+}
/** @} */
#ifdef DBUS_BUILD_TESTS
if (len < 0)
len = strlen (data);
- if (!_dbus_string_init (&orig, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&orig))
_dbus_assert_not_reached ("could not init string");
- if (!_dbus_string_init (&encoded, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&encoded))
_dbus_assert_not_reached ("could not init string");
- if (!_dbus_string_init (&decoded, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&decoded))
_dbus_assert_not_reached ("could not init string");
if (!_dbus_string_append_len (&orig, data, len))
_dbus_string_get_length (&encoded),
_dbus_string_get_length (&decoded));
printf ("Original: %s\n", data);
- _dbus_string_get_const_data (&decoded, &s);
+ s = _dbus_string_get_const_data (&decoded);
printf ("Decoded: %s\n", s);
_dbus_assert_not_reached ("original string not the same as string decoded from base64");
}
_dbus_string_free (&decoded);
}
+static void
+test_hex_roundtrip (const unsigned char *data,
+ int len)
+{
+ DBusString orig;
+ DBusString encoded;
+ DBusString decoded;
+
+ if (len < 0)
+ len = strlen (data);
+
+ if (!_dbus_string_init (&orig))
+ _dbus_assert_not_reached ("could not init string");
+
+ if (!_dbus_string_init (&encoded))
+ _dbus_assert_not_reached ("could not init string");
+
+ if (!_dbus_string_init (&decoded))
+ _dbus_assert_not_reached ("could not init string");
+
+ if (!_dbus_string_append_len (&orig, data, len))
+ _dbus_assert_not_reached ("couldn't append orig data");
+
+ if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
+ _dbus_assert_not_reached ("could not encode");
+
+ if (!_dbus_string_hex_decode (&encoded, 0, &decoded, 0))
+ _dbus_assert_not_reached ("could not decode");
+
+ if (!_dbus_string_equal (&orig, &decoded))
+ {
+ const char *s;
+
+ printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
+ _dbus_string_get_length (&orig),
+ _dbus_string_get_length (&encoded),
+ _dbus_string_get_length (&decoded));
+ printf ("Original: %s\n", data);
+ s = _dbus_string_get_const_data (&decoded);
+ printf ("Decoded: %s\n", s);
+ _dbus_assert_not_reached ("original string not the same as string decoded from base64");
+ }
+
+ _dbus_string_free (&orig);
+ _dbus_string_free (&encoded);
+ _dbus_string_free (&decoded);
+}
+
+typedef void (* TestRoundtripFunc) (const unsigned char *data,
+ int len);
+static void
+test_roundtrips (TestRoundtripFunc func)
+{
+ (* func) ("Hello this is a string\n", -1);
+ (* func) ("Hello this is a string\n1", -1);
+ (* func) ("Hello this is a string\n12", -1);
+ (* func) ("Hello this is a string\n123", -1);
+ (* func) ("Hello this is a string\n1234", -1);
+ (* func) ("Hello this is a string\n12345", -1);
+ (* func) ("", 0);
+ (* func) ("1", 1);
+ (* func) ("12", 2);
+ (* func) ("123", 3);
+ (* func) ("1234", 4);
+ (* func) ("12345", 5);
+ (* func) ("", 1);
+ (* func) ("1", 2);
+ (* func) ("12", 3);
+ (* func) ("123", 4);
+ (* func) ("1234", 5);
+ (* func) ("12345", 6);
+ {
+ unsigned char buf[512];
+ int i;
+
+ i = 0;
+ while (i < _DBUS_N_ELEMENTS (buf))
+ {
+ buf[i] = i;
+ ++i;
+ }
+ i = 0;
+ while (i < _DBUS_N_ELEMENTS (buf))
+ {
+ (* func) (buf, i);
+ ++i;
+ }
+ }
+}
+
+
/**
* @ingroup DBusStringInternals
* Unit test for DBusString.
i = 0;
while (i < _DBUS_N_ELEMENTS (lens))
{
- if (!_dbus_string_init (&str, lens[i]))
+ if (!_dbus_string_init (&str))
_dbus_assert_not_reached ("failed to init string");
+
+ set_max_length (&str, lens[i]);
test_max_len (&str, lens[i]);
_dbus_string_free (&str);
{
int j;
- if (!_dbus_string_init (&str, lens[i]))
+ if (!_dbus_string_init (&str))
_dbus_assert_not_reached ("failed to init string");
+
+ set_max_length (&str, lens[i]);
if (!_dbus_string_set_length (&str, lens[i]))
_dbus_assert_not_reached ("failed to set string length");
}
/* Test appending data */
- if (!_dbus_string_init (&str, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&str))
_dbus_assert_not_reached ("failed to init string");
i = 0;
/* Check steal_data */
- if (!_dbus_string_init (&str, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&str))
_dbus_assert_not_reached ("failed to init string");
if (!_dbus_string_append (&str, "Hello World"))
i = _dbus_string_get_length (&str);
- if (!_dbus_string_init (&other, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&other))
_dbus_assert_not_reached ("could not init string");
if (!_dbus_string_move (&str, 0, &other, 0))
i = _dbus_string_get_length (&str);
- if (!_dbus_string_init (&other, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&other))
_dbus_assert_not_reached ("could not init string");
if (!_dbus_string_copy (&str, 0, &other, 0))
/* Check replace */
- if (!_dbus_string_init (&str, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&str))
_dbus_assert_not_reached ("failed to init string");
if (!_dbus_string_append (&str, "Hello World"))
i = _dbus_string_get_length (&str);
- if (!_dbus_string_init (&other, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&other))
_dbus_assert_not_reached ("could not init string");
if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
/* Check append/get unichar */
- if (!_dbus_string_init (&str, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&str))
_dbus_assert_not_reached ("failed to init string");
ch = 0;
_dbus_assert (i == _dbus_string_get_length (&str));
_dbus_string_free (&str);
+
+ /* Check insert/set/get byte */
+
+ if (!_dbus_string_init (&str))
+ _dbus_assert_not_reached ("failed to init string");
+
+ if (!_dbus_string_append (&str, "Hello"))
+ _dbus_assert_not_reached ("failed to append Hello");
+
+ _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
+ _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
+ _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
+ _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
+ _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
+
+ _dbus_string_set_byte (&str, 1, 'q');
+ _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
+
+ if (!_dbus_string_insert_byte (&str, 0, 255))
+ _dbus_assert_not_reached ("can't insert byte");
+
+ if (!_dbus_string_insert_byte (&str, 2, 'Z'))
+ _dbus_assert_not_reached ("can't insert byte");
+
+ if (!_dbus_string_insert_byte (&str, _dbus_string_get_length (&str), 'W'))
+ _dbus_assert_not_reached ("can't insert byte");
+
+ _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
+ _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
+ _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
+ _dbus_assert (_dbus_string_get_byte (&str, 3) == 'q');
+ _dbus_assert (_dbus_string_get_byte (&str, 4) == 'l');
+ _dbus_assert (_dbus_string_get_byte (&str, 5) == 'l');
+ _dbus_assert (_dbus_string_get_byte (&str, 6) == 'o');
+ _dbus_assert (_dbus_string_get_byte (&str, 7) == 'W');
+
+ _dbus_string_free (&str);
/* Check append/parse int/double */
- if (!_dbus_string_init (&str, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&str))
_dbus_assert_not_reached ("failed to init string");
if (!_dbus_string_append_int (&str, 27))
_dbus_string_free (&str);
- if (!_dbus_string_init (&str, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&str))
_dbus_assert_not_reached ("failed to init string");
if (!_dbus_string_append_double (&str, 50.3))
_dbus_string_free (&str);
/* Test find */
- if (!_dbus_string_init (&str, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&str))
_dbus_assert_not_reached ("failed to init string");
if (!_dbus_string_append (&str, "Hello"))
_dbus_assert_not_reached ("didn't find 'He'");
_dbus_assert (i == 0);
+ if (!_dbus_string_find (&str, 0, "Hello", &i))
+ _dbus_assert_not_reached ("didn't find 'Hello'");
+ _dbus_assert (i == 0);
+
if (!_dbus_string_find (&str, 0, "ello", &i))
_dbus_assert_not_reached ("didn't find 'ello'");
_dbus_assert (i == 1);
if (_dbus_string_find (&str, 0, "q", NULL))
_dbus_assert_not_reached ("Did find 'q'");
+
+ if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
+ _dbus_assert_not_reached ("Didn't find 'He'");
+
+ if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
+ _dbus_assert_not_reached ("Did find 'Hello'");
_dbus_string_free (&str);
- /* Base 64 */
- test_base64_roundtrip ("Hello this is a string\n", -1);
- test_base64_roundtrip ("Hello this is a string\n1", -1);
- test_base64_roundtrip ("Hello this is a string\n12", -1);
- test_base64_roundtrip ("Hello this is a string\n123", -1);
- test_base64_roundtrip ("Hello this is a string\n1234", -1);
- test_base64_roundtrip ("Hello this is a string\n12345", -1);
- test_base64_roundtrip ("", 0);
- test_base64_roundtrip ("1", 1);
- test_base64_roundtrip ("12", 2);
- test_base64_roundtrip ("123", 3);
- test_base64_roundtrip ("1234", 4);
- test_base64_roundtrip ("12345", 5);
- test_base64_roundtrip ("", 1);
- test_base64_roundtrip ("1", 2);
- test_base64_roundtrip ("12", 3);
- test_base64_roundtrip ("123", 4);
- test_base64_roundtrip ("1234", 5);
- test_base64_roundtrip ("12345", 6);
- {
- unsigned char buf[512];
- i = 0;
- while (i < _DBUS_N_ELEMENTS (buf))
- {
- buf[i] = i;
- ++i;
- }
- i = 0;
- while (i < _DBUS_N_ELEMENTS (buf))
- {
- test_base64_roundtrip (buf, i);
- ++i;
- }
- }
+ /* Base 64 and Hex encoding */
+ test_roundtrips (test_base64_roundtrip);
+ test_roundtrips (test_hex_roundtrip);
return TRUE;
}