Consistently include <config.h> in all C source files and never in header files.
[platform/upstream/dbus.git] / dbus / dbus-string.c
index 000b4f6..e2eb93b 100644 (file)
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
+#include <config.h>
 #include "dbus-internals.h"
 #include "dbus-string.h"
 /* we allow a system header here, for speed/convenience */
@@ -741,8 +742,9 @@ _dbus_string_copy_data (const DBusString  *str,
 }
 
 /**
- * Copies the contents of a DBusString into a different
- * buffer. The resulting buffer will be nul-terminated.
+ * Copies the contents of a DBusString into a different buffer. It is
+ * a bug if avail_len is too short to hold the string contents. nul
+ * termination is not copied, just the supplied bytes.
  * 
  * @param str a string
  * @param buffer a C buffer to copy data to
@@ -753,15 +755,34 @@ _dbus_string_copy_to_buffer (const DBusString  *str,
                             char              *buffer,
                             int                avail_len)
 {
-  int copy_len;
   DBUS_CONST_STRING_PREAMBLE (str);
 
   _dbus_assert (avail_len >= 0);
+  _dbus_assert (avail_len >= real->len);
+  
+  memcpy (buffer, real->str, real->len);
+}
 
-  copy_len = MIN (avail_len, real->len+1);
-  memcpy (buffer, real->str, copy_len);
-  if (avail_len > 0 && avail_len == copy_len)
-    buffer[avail_len-1] = '\0';
+/**
+ * Copies the contents of a DBusString into a different buffer. It is
+ * a bug if avail_len is too short to hold the string contents plus a
+ * nul byte. 
+ * 
+ * @param str a string
+ * @param buffer a C buffer to copy data to
+ * @param avail_len maximum length of C buffer
+ */
+void
+_dbus_string_copy_to_buffer_with_nul (const DBusString  *str,
+                                      char              *buffer,
+                                      int                avail_len)
+{
+  DBUS_CONST_STRING_PREAMBLE (str);
+
+  _dbus_assert (avail_len >= 0);
+  _dbus_assert (avail_len > real->len);
+  
+  memcpy (buffer, real->str, real->len+1);
 }
 
 #ifdef DBUS_BUILD_TESTS
@@ -1657,6 +1678,48 @@ _dbus_string_replace_len (const DBusString *source,
   return TRUE;
 }
 
+/**
+ * Looks for the first occurance of a byte, deletes that byte,
+ * and moves everything after the byte to the beginning of a
+ * separate string.  Both strings must be initialized, valid
+ * strings.
+ *
+ * @param source the source string
+ * @param byte the byte to remove and split the string at
+ * @param tail the split off string
+ * @returns #FALSE if not enough memory or if byte could not be found
+ *
+ */
+dbus_bool_t
+_dbus_string_split_on_byte (DBusString        *source,
+                            unsigned char      byte,
+                            DBusString        *tail)
+{
+  int byte_position;
+  char byte_string[2] = "";
+  int head_length;
+  int tail_length;
+
+  byte_string[0] = (char) byte;
+
+  if (!_dbus_string_find (source, 0, byte_string, &byte_position))
+    return FALSE;
+
+  head_length = byte_position;
+  tail_length = _dbus_string_get_length (source) - head_length - 1;
+
+  if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
+                              tail, 0))
+    return FALSE;
+
+  /* remove the trailing delimiter byte from the head now.
+   */
+  if (!_dbus_string_set_length (source, head_length))
+    return FALSE;
+
+  return TRUE;
+}
+
 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
  * Pennington, and Tom Tromey are the authors and authorized relicense.
  */
@@ -1737,7 +1800,18 @@ _dbus_string_replace_len (const DBusString *source,
     }
 
 /**
- * Check whether a unicode char is in a valid range.
+ * Check whether a Unicode (5.2) char is in a valid range.
+ *
+ * The first check comes from the Unicode guarantee to never encode
+ * a point above 0x0010ffff, since UTF-16 couldn't represent it.
+ *
+ * The second check covers surrogate pairs (category Cs).
+ *
+ * The last two checks cover "Noncharacter": defined as:
+ *   "A code point that is permanently reserved for
+ *    internal use, and that should never be interchanged. In
+ *    Unicode 3.1, these consist of the values U+nFFFE and U+nFFFF
+ *    (where n is from 0 to 10_16) and the values U+FDD0..U+FDEF."
  *
  * @param Char the character
  */
@@ -1745,7 +1819,7 @@ _dbus_string_replace_len (const DBusString *source,
     ((Char) < 0x110000 &&                     \
      (((Char) & 0xFFFFF800) != 0xD800) &&     \
      ((Char) < 0xFDD0 || (Char) > 0xFDEF) &&  \
-     ((Char) & 0xFFFF) != 0xFFFF)
+     ((Char) & 0xFFFE) != 0xFFFE)
 
 #ifdef DBUS_BUILD_TESTS
 /**
@@ -2249,7 +2323,6 @@ _dbus_string_equal (const DBusString *a,
   return TRUE;
 }
 
-#ifdef DBUS_BUILD_TESTS
 /**
  * Tests two DBusString for equality up to the given length.
  * The strings may be shorter than the given length.
@@ -2294,7 +2367,6 @@ _dbus_string_equal_len (const DBusString *a,
 
   return TRUE;
 }
-#endif /* DBUS_BUILD_TESTS */
 
 /**
  * Tests two sub-parts of two DBusString for equality.  The specified
@@ -2689,6 +2761,68 @@ _dbus_string_validate_ascii (const DBusString *str,
 }
 
 /**
+ * Converts the given range of the string to lower case.
+ *
+ * @param str the string
+ * @param start first byte index to convert
+ * @param len number of bytes to convert
+ */
+void
+_dbus_string_tolower_ascii (const DBusString *str,
+                            int               start,
+                            int               len)
+{
+  unsigned char *s;
+  unsigned char *end;
+  DBUS_STRING_PREAMBLE (str);
+  _dbus_assert (start >= 0);
+  _dbus_assert (start <= real->len);
+  _dbus_assert (len >= 0);
+  _dbus_assert (len <= real->len - start);
+
+  s = real->str + start;
+  end = s + len;
+
+  while (s != end)
+    {
+      if (*s >= 'A' && *s <= 'Z')
+          *s += 'a' - 'A';
+      ++s;
+    }
+}
+
+/**
+ * Converts the given range of the string to upper case.
+ *
+ * @param str the string
+ * @param start first byte index to convert
+ * @param len number of bytes to convert
+ */
+void
+_dbus_string_toupper_ascii (const DBusString *str,
+                            int               start,
+                            int               len)
+{
+  unsigned char *s;
+  unsigned char *end;
+  DBUS_STRING_PREAMBLE (str);
+  _dbus_assert (start >= 0);
+  _dbus_assert (start <= real->len);
+  _dbus_assert (len >= 0);
+  _dbus_assert (len <= real->len - start);
+
+  s = real->str + start;
+  end = s + len;
+
+  while (s != end)
+    {
+      if (*s >= 'a' && *s <= 'z')
+          *s += 'A' - 'a';
+      ++s;
+    }
+}
+
+/**
  * 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,