/*
* Copyright (C) 2012, 2013 Eric Biggers
*
- * This file is part of wimlib, a library for working with WIM files.
+ * This file is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option) any
+ * later version.
*
- * wimlib is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version.
- *
- * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- * A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * This file is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
- * You should have received a copy of the GNU General Public License
- * along with wimlib; if not, see http://www.gnu.org/licenses/.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this file; if not, see http://www.gnu.org/licenses/.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
+#include <errno.h>
+#include <iconv.h>
+#include <pthread.h>
+#include <string.h>
+
#include "wimlib.h"
+#include "wimlib/alloca.h"
#include "wimlib/assert.h"
#include "wimlib/encoding.h"
#include "wimlib/endianness.h"
#include "wimlib/list.h"
#include "wimlib/util.h"
-#include <errno.h>
-#include <iconv.h>
-#include <pthread.h>
-#include <stdlib.h>
-#include <string.h>
-
-#ifdef HAVE_ALLOCA_H
-# include <alloca.h>
-#endif
bool wimlib_mbs_is_utf8 = !TCHAR_IS_UTF16LE;
bool buf_onheap; \
bufsize = (worst_case_len_expr) * sizeof(chartype2); \
/* Worst case length */ \
- if (bufsize <= STACK_MAX) { \
+ if (bufsize <= STACK_MAX) { \
buf = alloca(bufsize); \
buf_onheap = false; \
} else { \
return (n1 < n2) ? -1 : 1;
}
-/* Duplicate a UTF16-LE string which may not be null-terminated. */
+/* Like cmp_utf16le_strings(), but assumes the strings are null terminated. */
+int
+cmp_utf16le_strings_z(const utf16lechar *s1, const utf16lechar *s2,
+ bool ignore_case)
+{
+ if (ignore_case) {
+ for (;;) {
+ u16 c1 = upcase[le16_to_cpu(*s1)];
+ u16 c2 = upcase[le16_to_cpu(*s2)];
+ if (c1 != c2)
+ return (c1 < c2) ? -1 : 1;
+ if (c1 == 0)
+ return 0;
+ s1++, s2++;
+ }
+ } else {
+ while (*s1 && *s1 == *s2)
+ s1++, s2++;
+ if (*s1 == *s2)
+ return 0;
+ return (le16_to_cpu(*s1) < le16_to_cpu(*s2)) ? -1 : 1;
+ }
+}
+
+/* Duplicate a UTF-16LE string. The input string might not be null terminated
+ * and might be misaligned, but the returned string is guaranteed to be null
+ * terminated and properly aligned. */
utf16lechar *
-utf16le_dupz(const utf16lechar *ustr, size_t usize)
+utf16le_dupz(const void *ustr, size_t usize)
{
utf16lechar *dup = MALLOC(usize + sizeof(utf16lechar));
if (dup) {
}
return dup;
}
+
+/* Duplicate a null-terminated UTF-16LE string. */
+utf16lechar *
+utf16le_dup(const utf16lechar *ustr)
+{
+ const utf16lechar *p = ustr;
+ while (*p++)
+ ;
+ return memdup(ustr, (const u8 *)p - (const u8 *)ustr);
+}
+
+/* Return the length, in bytes, of a UTF-null terminated UTF-16 string,
+ * excluding the null terminator. */
+size_t
+utf16le_len_bytes(const utf16lechar *s)
+{
+ const utf16lechar *p = s;
+ while (*p)
+ p++;
+ return (p - s) * sizeof(utf16lechar);
+}
+
+/* Return the length, in UTF-16 coding units, of a UTF-null terminated UTF-16
+ * string, excluding the null terminator. */
+size_t
+utf16le_len_chars(const utf16lechar *s)
+{
+ return utf16le_len_bytes(s) / sizeof(utf16lechar);
+}