+static int
+recode_substring_pool__ (const char *to, const char *from,
+ struct substring text, char fallbackchar,
+ struct pool *pool, struct substring *out)
+{
+ size_t bufsize;
+ iconv_t conv ;
+
+ if (to == NULL)
+ to = default_encoding;
+
+ if (from == NULL)
+ from = default_encoding;
+
+ conv = create_iconv (to, from);
+
+ if ( (iconv_t) -1 == conv )
+ {
+ if (fallbackchar)
+ {
+ out->string = pool_malloc (pool, text.length + 1);
+ out->length = text.length;
+ memcpy (out->string, text.string, text.length);
+ out->string[out->length] = '\0';
+ return 0;
+ }
+ else
+ return EPROTO;
+ }
+
+ for (bufsize = text.length + 1; bufsize > text.length; bufsize *= 2)
+ {
+ char *output = pool_malloc (pool, bufsize);
+ ssize_t retval;
+
+ retval = try_recode (conv, fallbackchar, text.string, text.length,
+ output, bufsize);
+ if (retval >= 0)
+ {
+ *out = ss_buffer (output, retval);
+ return 0;
+ }
+ pool_free (pool, output);
+
+ if (retval != -E2BIG)
+ return -retval;
+ }
+
+ NOT_REACHED ();
+}
+