- 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;
- }
+ 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;
+ }