13 #define ASSERT(x,...) { if( !(x) ) DIE(( __VA_ARGS__)); } 23 if (vb->
blen >= len) {
28 vb->
buf = realloc(vb->
buf, len);
68 size_t inbytesleft = iblen;
69 size_t icresult = (size_t)-1;
70 size_t outbytesleft = 0;
78 outbytesleft = dest->
blen - dest->
dlen;
79 outbuf = dest->
b + dest->
dlen;
82 dest->
dlen = outbuf - dest->
b;
83 if (inbytesleft)
pst_vbgrow(dest, 2*inbytesleft);
84 }
while ((
size_t)-1 == icresult && E2BIG == myerrno);
86 if (icresult == (
size_t)-1) {
87 DEBUG_WARN((
"iconv failure: %s\n", strerror(myerrno)));
116 for (i = 0; i < length; i += 2) {
117 if (str[i] == 0 && str[i + 1] == 0) {
123 DEBUG_WARN((
"utf16 string is not zero terminated\n"));
126 return (len == -1) ? 0 : 1;
139 else DIE((
"malloc() failure"));
157 if (vb->
dlen + len < vb->blen * 1.5)
158 len = vb->
blen * 1.5;
160 if (!nb)
DIE((
"malloc() failure"));
162 memcpy(nb, vb->
b, vb->
dlen);
168 if (vb->
b != vb->
buf)
183 memcpy(vb->
b, b, len);
197 memcpy(vb->
b + vb->
dlen, b, len);
205 i16to8 = iconv_open(
"utf-8",
"utf-16le");
206 if (
i16to8 == (iconv_t)-1) {
207 DEBUG_WARN((
"Couldn't open iconv descriptor for utf-16le to utf-8.\n"));
215 size_t inbytesleft = iblen;
216 size_t icresult = (size_t)-1;
217 size_t outbytesleft = 0;
229 outbytesleft = dest->
blen - dest->
dlen;
230 outbuf = dest->
b + dest->
dlen;
231 icresult = iconv(
i16to8, (
ICONV_CONST char**)&inbuf, &inbytesleft, &outbuf, &outbytesleft);
233 dest->
dlen = outbuf - dest->
b;
234 if (inbytesleft)
pst_vbgrow(dest, inbytesleft);
235 }
while ((
size_t)-1 == icresult && E2BIG == myerrno);
237 if (icresult == (
size_t)-1) {
238 DEBUG_WARN((
"iconv failure: %s\n", strerror(myerrno)));
242 return (icresult) ? (size_t)-1 : 0;
pst_vbuf * pst_vballoc(size_t len)
void pst_vbappend(pst_vbuf *vb, void *b, size_t len)
append len bytes of b to vb, resize if necessary
size_t pst_vb_utf16to8(pst_vbuf *dest, const char *inbuf, int iblen)
void * pst_malloc(size_t size)
static void open_targets(const char *charset)
size_t pst_vb_8bit2utf8(pst_vbuf *dest, const char *inbuf, int iblen, const char *charset)
static iconv_t i8totarget
size_t pst_vb_utf8to8bit(pst_vbuf *dest, const char *inbuf, int iblen, const char *charset)
void pst_vbgrow(pst_vbuf *vb, size_t len)
out: vbavail(vb) >= len, data are preserved
static int target_open_from
void pst_vbset(pst_vbuf *vb, void *b, size_t len)
set vbuf b size=len, resize if necessary, relen = how much to over-allocate
static size_t pst_vbavail(pst_vbuf *vb)
static int utf16_is_terminated(const char *str, int length)
static void pst_vbresize(pst_vbuf *vb, size_t len)
DESTRUCTIVELY grow or shrink buffer.
static size_t sbcs_conversion(pst_vbuf *dest, const char *inbuf, int iblen, iconv_t conversion)
static const char * target_charset
static int target_open_to
static void pst_unicode_close()