-/* Like iconv, but keeps going even when the input is invalid
- If you're supplying inrepls, the source charset should be stateless;
- if you're supplying an outrepl, the target charset should be. */
-/* XXX: MC: I do not understand what it does yet */
-ssize_t mutt_iconv(iconv_t cd,
- const char **inbuf, ssize_t *inbytesleft,
- char **outbuf, ssize_t *outbytesleft,
- const char **inrepls, const char *outrepl)
-{
- ssize_t ret = 0, ret1;
- const char *ib = *inbuf;
- ssize_t ibl = *inbytesleft;
- char *ob = *outbuf;
- ssize_t obl = *outbytesleft;
-
- for (;;) {
- ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
- if (ret1 != -1)
- ret += ret1;
-
- if (ibl && obl && errno == EILSEQ) {
- if (inrepls) {
- /* Try replacing the input */
- const char **t;
-
- for (t = inrepls; *t; t++) {
- const char *ib1 = *t;
- ssize_t ibl1 = m_strlen(*t);
- char *ob1 = ob;
- ssize_t obl1 = obl;
-
- my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
- if (!ibl1) {
- ++ib, --ibl;
- ob = ob1, obl = obl1;
- ++ret;
- break;
- }
- }
- if (*t)
- continue;
- }
- /* Replace the output */
- if (!outrepl)
- outrepl = "?";
- my_iconv(cd, 0, 0, &ob, &obl);
- if (obl) {
- ssize_t n = m_strlen(outrepl);
-
- if (n > obl) {
- outrepl = "?";
- n = 1;
- }
- memcpy(ob, outrepl, n);
- ++ib, --ibl;
- ob += n, obl -= n;
- ++ret;
- my_iconv(cd, 0, 0, 0, 0); /* for good measure */
- continue;
- }
- }
- *inbuf = ib, *inbytesleft = ibl;
- *outbuf = ob, *outbytesleft = obl;
- return ret;
- }
-}
-