+/* Prevent printing an error message if a character conversion error occurs
+ * while printing an error message. (This variable is not per-thread but it
+ * doesn't matter too much since it's just the error messages.) */
+static bool error_message_being_printed = false;
+
+#define DEFINE_CHAR_CONVERSION_FUNCTIONS(varname1, longname1, chartype1,\
+ varname2, longname2, chartype2,\
+ earlyreturn_on_utf8_locale, \
+ earlyreturn_expr, \
+ worst_case_len_expr, \
+ err_return, \
+ err_msg, \
+ modifier) \
+static ICONV_LIST(iconv_##varname1##_to_##varname2, \
+ longname1, longname2); \
+ \
+modifier int \
+varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\
+ size_t *out_nbytes_ret) \
+{ \
+ iconv_t *cd = get_iconv(&iconv_##varname1##_to_##varname2); \
+ if (cd == NULL) \
+ return WIMLIB_ERR_ICONV_NOT_AVAILABLE; \
+ \
+ /* Worst case length */ \
+ chartype2 buf[worst_case_len_expr]; \
+ char *inbuf = (char*)in; \
+ size_t inbytesleft = in_nbytes; \
+ char *outbuf = (char*)buf; \
+ size_t outbytesleft = sizeof(buf); \
+ size_t len; \
+ int ret; \
+ \
+ len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); \
+ if (len == (size_t)-1) { \
+ if (!error_message_being_printed) { \
+ error_message_being_printed = true; \
+ err_msg; \
+ error_message_being_printed = false; \
+ } \
+ ret = err_return; \
+ } else { \
+ *out_nbytes_ret = sizeof(buf) - outbytesleft; \
+ ret = 0; \
+ } \
+ put_iconv(cd); \
+ return ret; \
+} \
+ \
+modifier int \
+varname1##_to_##varname2##_buf(const chartype1 *in, size_t in_nbytes, \
+ chartype2 *out) \
+{ \
+ iconv_t *cd = get_iconv(&iconv_##varname1##_to_##varname2); \
+ if (cd == NULL) \
+ return WIMLIB_ERR_ICONV_NOT_AVAILABLE; \
+ \
+ char *inbuf = (char*)in; \
+ size_t inbytesleft = in_nbytes; \
+ char *outbuf = (char*)out; \
+ const size_t LARGE_NUMBER = 1000000000; \
+ size_t outbytesleft = LARGE_NUMBER; \
+ size_t len; \
+ int ret; \
+ \
+ len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); \
+ if (len == (size_t)-1) { \
+ if (!error_message_being_printed) { \
+ error_message_being_printed = true; \
+ err_msg; \
+ error_message_being_printed = false; \
+ } \
+ ret = err_return; \
+ } else { \
+ out[(LARGE_NUMBER-outbytesleft)/sizeof(chartype2)] = 0; \
+ ret = 0; \
+ } \
+ put_iconv(cd); \
+ return ret; \
+} \
+ \
+modifier int \
+varname1##_to_##varname2(const chartype1 *in, size_t in_nbytes, \
+ chartype2 **out_ret, \
+ size_t *out_nbytes_ret) \
+{ \
+ int ret; \
+ chartype2 *out; \
+ size_t out_nbytes; \
+ \
+ if (earlyreturn_on_utf8_locale && wimlib_mbs_is_utf8) { \
+ earlyreturn_expr; \
+ /* Out same as in */ \
+ out = MALLOC(in_nbytes + sizeof(chartype2)); \
+ if (!out) \
+ return WIMLIB_ERR_NOMEM; \
+ memcpy(out, in, in_nbytes); \
+ out[in_nbytes / sizeof(chartype2)] = 0; \
+ *out_ret = out; \
+ *out_nbytes_ret = in_nbytes; \
+ return 0; \
+ } \
+ \
+ ret = varname1##_to_##varname2##_nbytes(in, in_nbytes, \
+ &out_nbytes); \
+ if (ret) \
+ return ret; \
+ \
+ out = MALLOC(out_nbytes + sizeof(chartype2)); \
+ if (!out) \
+ return WIMLIB_ERR_NOMEM; \
+ \
+ ret = varname1##_to_##varname2##_buf(in, in_nbytes, out); \
+ if (ret) { \
+ FREE(out); \
+ } else { \
+ *out_ret = out; \
+ *out_nbytes_ret = out_nbytes; \
+ } \
+ return ret; \