#include #include #include #include #include #include "internal.h" #include "rcciconv.h" static void rccIConvCopySymbol(char **in_buf, int *in_left, char **out_buf, int *out_left) { if ((out_left>0)&&(in_left>0)) { (**out_buf)=(**in_buf); (*out_buf)++; (*in_buf)++; (*in_left)--; (*out_left)--; } } static int rccIConvUTFBytes(unsigned char c) { int j; if (c<128) return 1; for (j=6;j>=0;j--) if ((c&(1<icnv = iconv_open(to, from); return icnv; } void rccIConvClose(rcc_iconv icnv) { if (icnv) { if (icnv->icnv != (iconv_t)-1) iconv_close(icnv->icnv); free(icnv); } } size_t rccIConvRecode(rcc_iconv icnv, char *outbuf, size_t outsize, const char *buf, size_t size) { char *in_buf, *out_buf, *res, err; int in_left, out_left, olen; int ub, utf_mode=0; int errors=0; if ((!buf)||(!outbuf)||(!outsize)||(!icnv)||(icnv->icnv == (iconv_t)-1)) return (size_t)-1; if (iconv(icnv->icnv, NULL, NULL, NULL, NULL) == -1) return (size_t)-1; size = STRNLEN(buf,size); loop_restart: errors = 0; in_buf = (char*)buf; /*DS*/ in_left = size; out_buf = outbuf; out_left = outsize; loop: err=iconv(icnv->icnv, &in_buf, &in_left, &out_buf, &out_left); if (err<0) { if (errno==E2BIG) { *(int*)(outbuf+(RCC_MAX_STRING_CHARS-sizeof(int)))=0; } else if (errno==EILSEQ) { if (errors++0;ub--) rccIConvCopySymbol(&in_buf, &in_left, &out_buf, &out_left); if (in_left>0) goto loop; } else if (!utf_mode) { utf_mode = 1; goto loop_restart; } else { return (size_t)-1; } } else { return (size_t)-1; } } outbuf[outsize - out_left] = 0; return outsize - out_left; } size_t rccIConv(rcc_context ctx, iconv_t icnv, const char *buf, size_t len) { char *in_buf, *out_buf, *res, err; int in_left, out_left, olen; int ub, utf_mode=0; int errors=0; if ((!buf)||(!ctx)||(icnv == (iconv_t)-1)) return (size_t)-1; len = STRNLEN(buf,len); if (iconv(icnv, NULL, NULL, NULL, NULL) == -1) return (size_t)-1; loop_restart: errors = 0; in_buf = (char*)buf; /*DS*/ in_left = len; out_buf = ctx->tmpbuffer; out_left = RCC_MAX_STRING_CHARS; loop: err=iconv(icnv, &in_buf, &in_left, &out_buf, &out_left); if (err<0) { if (errno==E2BIG) { *(int*)(ctx->tmpbuffer+(RCC_MAX_STRING_CHARS-sizeof(int)))=0; } else if (errno==EILSEQ) { if (errors++0;ub--) rccIConvCopySymbol(&in_buf, &in_left, &out_buf, &out_left); if (in_left>0) goto loop; } else if (!utf_mode) { utf_mode = 1; goto loop_restart; } else { return (size_t)-1; } } else { return (size_t)-1; } } ctx->tmpbuffer[RCC_MAX_STRING_CHARS - out_left] = 0; return RCC_MAX_STRING_CHARS - out_left; }