# 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. The input string might 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 *
}
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);
+}