]> wimlib.net Git - wimlib/blobdiff - src/encoding.c
Improve tstr <=> UTF-16LE conversions
[wimlib] / src / encoding.c
index e588eabff6e60cd015999c6446ccf11e2344bf35..927cb5dad342128017133ad903530643c246b056 100644 (file)
@@ -558,40 +558,14 @@ cmp_utf16le_strings(const utf16lechar *s1, size_t n1,
        return (n1 < n2) ? -1 : 1;
 }
 
-/* Duplicates a string of system-dependent encoding into a UTF-16LE string and
- * returns the string and its length, in bytes, in the pointer arguments.  Frees
- * any existing string at the return location before overwriting it. */
-int
-get_utf16le_string(const tchar *name, utf16lechar **name_utf16le_ret,
-                  u16 *name_utf16le_nbytes_ret)
+/* Duplicate a UTF16-LE string which may not be null-terminated.  */
+utf16lechar *
+utf16le_dupz(const utf16lechar *ustr, size_t usize)
 {
-       utf16lechar *name_utf16le;
-       size_t name_utf16le_nbytes;
-       int ret;
-#if TCHAR_IS_UTF16LE
-       name_utf16le_nbytes = tstrlen(name) * sizeof(utf16lechar);
-       name_utf16le = MALLOC(name_utf16le_nbytes + sizeof(utf16lechar));
-       if (name_utf16le == NULL)
-               return WIMLIB_ERR_NOMEM;
-       memcpy(name_utf16le, name, name_utf16le_nbytes + sizeof(utf16lechar));
-       ret = 0;
-#else
-
-       ret = tstr_to_utf16le(name, tstrlen(name), &name_utf16le,
-                             &name_utf16le_nbytes);
-       if (ret == 0) {
-               if (name_utf16le_nbytes > 0xffff) {
-                       FREE(name_utf16le);
-                       ERROR("Multibyte string \"%"TS"\" is too long!", name);
-                       ret = WIMLIB_ERR_INVALID_UTF8_STRING;
-               }
-       }
-#endif
-       if (ret == 0) {
-               FREE(*name_utf16le_ret);
-               *name_utf16le_ret = name_utf16le;
-               *name_utf16le_nbytes_ret = name_utf16le_nbytes;
+       utf16lechar *dup = MALLOC(usize + sizeof(utf16lechar));
+       if (dup) {
+               memcpy(dup, ustr, usize);
+               dup[usize / sizeof(utf16lechar)] = 0;
        }
-       return ret;
+       return dup;
 }
-