-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-string.c String utility class (internal to D-Bus implementation)
*
* Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
+ * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
*
* Licensed under the Academic Free License version 2.1
*
#include "dbus-sysdeps.h"
/**
- * @defgroup DBusString string class
+ * @defgroup DBusString DBusString class
* @ingroup DBusInternals
- * @brief DBusString data structure
+ * @brief DBusString data structure for safer string handling
*
* Types and functions related to DBusString. DBusString is intended
* to be a string class that makes it hard to mess up security issues
real->invalid = TRUE;
}
+static dbus_bool_t
+compact (DBusRealString *real,
+ int max_waste)
+{
+ unsigned char *new_str;
+ int new_allocated;
+ int waste;
+
+ waste = real->allocated - (real->len + _DBUS_STRING_ALLOCATION_PADDING);
+
+ if (waste <= max_waste)
+ return TRUE;
+
+ new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
+
+ new_str = dbus_realloc (real->str - real->align_offset, new_allocated);
+ if (_DBUS_UNLIKELY (new_str == NULL))
+ return FALSE;
+
+ real->str = new_str + real->align_offset;
+ real->allocated = new_allocated;
+ fixup_alignment (real);
+
+ return TRUE;
+}
+
#ifdef DBUS_BUILD_TESTS
/* Not using this feature at the moment,
* so marked DBUS_BUILD_TESTS-only
* we know we won't change the string further
*/
#define MAX_WASTE 48
- if (real->allocated - MAX_WASTE > real->len)
- {
- unsigned char *new_str;
- int new_allocated;
-
- new_allocated = real->len + _DBUS_STRING_ALLOCATION_PADDING;
-
- new_str = dbus_realloc (real->str - real->align_offset,
- new_allocated);
- if (new_str != NULL)
- {
- real->str = new_str + real->align_offset;
- real->allocated = new_allocated;
- fixup_alignment (real);
- }
- }
+ compact (real, MAX_WASTE);
}
#endif /* DBUS_BUILD_TESTS */
return TRUE;
}
+/**
+ * Compacts the string to avoid wasted memory. Wasted memory is
+ * memory that is allocated but not actually required to store the
+ * current length of the string. The compact is only done if more
+ * than the given amount of memory is being wasted (otherwise the
+ * waste is ignored and the call does nothing).
+ *
+ * @param str the string
+ * @param max_waste the maximum amount of waste to ignore
+ * @returns #FALSE if the compact failed due to realloc failure
+ */
+dbus_bool_t
+_dbus_string_compact (DBusString *str,
+ int max_waste)
+{
+ DBUS_STRING_PREAMBLE (str);
+
+ return compact (real, max_waste);
+}
+
static dbus_bool_t
set_length (DBusRealString *real,
int new_length)
}
/**
+ * Finds end of line ("\r\n" or "\n") in the string,
+ * returning #TRUE and filling in the byte index
+ * where the eol string was found, if it was found.
+ * Returns #FALSE if eol wasn't found.
+ *
+ * @param str the string
+ * @param start where to start looking
+ * @param found return location for where eol was found or string length otherwise
+ * @param found_len return length of found eol string or zero otherwise
+ * @returns #TRUE if found
+ */
+dbus_bool_t
+_dbus_string_find_eol (const DBusString *str,
+ int start,
+ int *found,
+ int *found_len)
+{
+ 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] == '\r')
+ {
+ if ((i+1) < real->len && real->str[i+1] == '\n') /* "\r\n" */
+ {
+ if (found)
+ *found = i;
+ if (found_len)
+ *found_len = 2;
+ return TRUE;
+ }
+ else /* only "\r" */
+ {
+ if (found)
+ *found = i;
+ if (found_len)
+ *found_len = 1;
+ return TRUE;
+ }
+ }
+ else if (real->str[i] == '\n') /* only "\n" */
+ {
+ if (found)
+ *found = i;
+ if (found_len)
+ *found_len = 1;
+ return TRUE;
+ }
+ ++i;
+ }
+
+ if (found)
+ *found = real->len;
+
+ if (found_len)
+ *found_len = 0;
+
+ return FALSE;
+}
+
+/**
* Finds the given substring in the string,
* up to a certain position,
* returning #TRUE and filling in the byte index
* @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
+ * code only with ifdef DBUS_BUILD_TESTS
*
* @param source the source string
* @param dest the destination string (contents are replaced)
_dbus_string_pop_line (DBusString *source,
DBusString *dest)
{
- int eol;
- dbus_bool_t have_newline;
+ int eol, eol_len;
_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_len = 0;
+ if (!_dbus_string_find_eol (source, 0, &eol, &eol_len))
{
- eol = _dbus_string_get_length (source);
- have_newline = FALSE;
+ _dbus_assert (eol == _dbus_string_get_length (source));
+ if (eol == 0)
+ {
+ /* If there's no newline and source has zero length, we're done */
+ return FALSE;
+ }
+ /* otherwise, the last line of the file has no eol characters */
}
- if (eol == 0)
- return FALSE; /* eof */
+ /* remember eol can be 0 if it's an empty line, but eol_len should not be zero also
+ * since find_eol returned TRUE
+ */
+
+ if (!_dbus_string_move_len (source, 0, eol + eol_len, dest, 0))
+ return FALSE;
- if (!_dbus_string_move_len (source, 0, eol,
- dest, 0))
+ /* remove line ending */
+ if (!_dbus_string_set_length (dest, eol))
{
+ _dbus_assert_not_reached ("out of memory when shortening a string");
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;
}