-#else
- ERROR("Cannot set custom memory allocator functions:");
- ERROR("wimlib was compiled with the --without-custom-memory-allocator "
- "flag");
- return WIMLIB_ERR_UNSUPPORTED;
-#endif
-}
-
-
-
-static iconv_t cd_utf16_to_utf8 = (iconv_t)(-1);
-
-/* Converts a string in the UTF-16 encoding to a newly allocated string in the
- * UTF-8 encoding. */
-char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
- size_t *utf8_len_ret)
-{
- if (cd_utf16_to_utf8 == (iconv_t)(-1)) {
- cd_utf16_to_utf8 = iconv_open("UTF-8", "UTF-16LE");
- if (cd_utf16_to_utf8 == (iconv_t)-1) {
- ERROR_WITH_ERRNO("Failed to get conversion descriptor "
- "for converting UTF-16LE to UTF-8");
- return NULL;
- }
- }
- size_t utf16_bytes_left = utf16_len;
- size_t utf8_bytes_left = utf16_len;
-
- char *utf8_str = MALLOC(utf8_bytes_left);
- if (!utf8_str)
- return NULL;
-
- char *orig_utf8_str = utf8_str;
-
- size_t num_chars_converted = iconv(cd_utf16_to_utf8, (char**)&utf16_str,
- &utf16_bytes_left, &utf8_str, &utf8_bytes_left);
-
- if (num_chars_converted == (size_t)(-1)) {
- ERROR_WITH_ERRNO("Failed to convert UTF-16LE string to UTF-8 "
- "string");
- FREE(orig_utf8_str);
- return NULL;
- }
-
- size_t utf8_len = utf16_len - utf8_bytes_left;
-
- *utf8_len_ret = utf8_len;
- orig_utf8_str[utf8_len] = '\0';
- return orig_utf8_str;
-}
-
-static iconv_t cd_utf8_to_utf16 = (iconv_t)(-1);
-
-/* Converts a string in the UTF-8 encoding to a newly allocated string in the
- * UTF-16 encoding. */
-char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
- size_t *utf16_len_ret)
-{
- if (cd_utf8_to_utf16 == (iconv_t)(-1)) {
- cd_utf8_to_utf16 = iconv_open("UTF-16LE", "UTF-8");
- if (cd_utf8_to_utf16 == (iconv_t)-1) {
- ERROR_WITH_ERRNO("Failed to get conversion descriptor "
- "for converting UTF-8 to UTF-16LE");
- return NULL;
- }
- }
-
- size_t utf8_bytes_left = utf8_len;
- size_t utf16_capacity = utf8_len * 4;
- size_t utf16_bytes_left = utf16_capacity;
-
- char *utf16_str = MALLOC(utf16_capacity + 2);
- if (!utf16_str)
- return NULL;
-
- char *orig_utf16_str = utf16_str;
-
- size_t num_chars_converted = iconv(cd_utf8_to_utf16, (char**)&utf8_str,
- &utf8_bytes_left, &utf16_str, &utf16_bytes_left);
-
- if (num_chars_converted == (size_t)(-1)) {
- ERROR_WITH_ERRNO("Failed to convert UTF-8 string to UTF-16LE "
- "string");
- FREE(orig_utf16_str);
- return NULL;
- }
-
- size_t utf16_len = utf16_capacity - utf16_bytes_left;
-
- *utf16_len_ret = utf16_len;
- orig_utf16_str[utf16_len] = '\0';
- orig_utf16_str[utf16_len + 1] = '\0';
- return orig_utf16_str;