Mercurial > libpst
comparison src/vbuf.c @ 164:ab384fed78c5
Compensate for iconv conversion to utf-7 that produces strings that are not null terminated.
Don't produce empty attachment files in separate mode.
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 16 Mar 2009 18:31:39 -0700 |
parents | cda7c812ec01 |
children | 6954d315aaa8 |
comparison
equal
deleted
inserted
replaced
163:03fbb0269f3c | 164:ab384fed78c5 |
---|---|
98 { | 98 { |
99 size_t inbytesleft = iblen; | 99 size_t inbytesleft = iblen; |
100 size_t icresult = (size_t)-1; | 100 size_t icresult = (size_t)-1; |
101 size_t outbytesleft = 0; | 101 size_t outbytesleft = 0; |
102 char *outbuf = NULL; | 102 char *outbuf = NULL; |
103 int myerrno; | |
103 | 104 |
104 ASSERT(unicode_up, "vb_utf16to8() called before unicode started."); | 105 ASSERT(unicode_up, "vb_utf16to8() called before unicode started."); |
105 | 106 vbresize(dest, iblen); |
106 if (2 > dest->blen) vbresize(dest, 2); | |
107 dest->dlen = 0; | |
108 | 107 |
109 //Bad Things can happen if a non-zero-terminated utf16 string comes through here | 108 //Bad Things can happen if a non-zero-terminated utf16 string comes through here |
110 if (!utf16_is_terminated(inbuf, iblen)) | 109 if (!utf16_is_terminated(inbuf, iblen)) |
111 return (size_t)-1; | 110 return (size_t)-1; |
112 | 111 |
113 do { | 112 do { |
114 outbytesleft = dest->blen - dest->dlen; | 113 outbytesleft = dest->blen - dest->dlen; |
115 outbuf = dest->b + dest->dlen; | 114 outbuf = dest->b + dest->dlen; |
116 icresult = iconv(i16to8, (ICONV_CONST char**)&inbuf, &inbytesleft, &outbuf, &outbytesleft); | 115 icresult = iconv(i16to8, (ICONV_CONST char**)&inbuf, &inbytesleft, &outbuf, &outbytesleft); |
116 myerrno = errno; | |
117 dest->dlen = outbuf - dest->b; | 117 dest->dlen = outbuf - dest->b; |
118 vbgrow(dest, inbytesleft); | 118 if (inbytesleft) vbgrow(dest, inbytesleft); |
119 } while ((size_t)-1 == icresult && E2BIG == errno); | 119 } while ((size_t)-1 == icresult && E2BIG == myerrno); |
120 | 120 |
121 if (icresult == (size_t)-1) { | 121 if (icresult == (size_t)-1) { |
122 DEBUG_WARN(("iconv failure: %s\n", strerror(errno))); | 122 DEBUG_WARN(("iconv failure: %s\n", strerror(myerrno))); |
123 unicode_init(); | 123 unicode_init(); |
124 return (size_t)-1; | 124 return (size_t)-1; |
125 } | 125 } |
126 return (icresult) ? (size_t)-1 : 0; | 126 return (icresult) ? (size_t)-1 : 0; |
127 } | 127 } |
156 { | 156 { |
157 size_t inbytesleft = iblen; | 157 size_t inbytesleft = iblen; |
158 size_t icresult = (size_t)-1; | 158 size_t icresult = (size_t)-1; |
159 size_t outbytesleft = 0; | 159 size_t outbytesleft = 0; |
160 char *outbuf = NULL; | 160 char *outbuf = NULL; |
161 int myerrno; | |
161 | 162 |
162 vbresize(dest, 2*iblen); | 163 vbresize(dest, 2*iblen); |
163 dest->dlen = 0; | |
164 | 164 |
165 do { | 165 do { |
166 outbytesleft = dest->blen - dest->dlen; | 166 outbytesleft = dest->blen - dest->dlen; |
167 outbuf = dest->b + dest->dlen; | 167 outbuf = dest->b + dest->dlen; |
168 icresult = iconv(conversion, (ICONV_CONST char**)&inbuf, &inbytesleft, &outbuf, &outbytesleft); | 168 icresult = iconv(conversion, (ICONV_CONST char**)&inbuf, &inbytesleft, &outbuf, &outbytesleft); |
169 myerrno = errno; | |
169 dest->dlen = outbuf - dest->b; | 170 dest->dlen = outbuf - dest->b; |
170 if (inbytesleft) vbgrow(dest, 2*inbytesleft); | 171 if (inbytesleft) vbgrow(dest, 2*inbytesleft); |
171 } while ((size_t)-1 == icresult && E2BIG == errno); | 172 } while ((size_t)-1 == icresult && E2BIG == myerrno); |
172 | 173 |
173 if (icresult == (size_t)-1) { | 174 if (icresult == (size_t)-1) { |
174 WARN(("iconv failure: %s\n", strerror(errno))); | 175 DEBUG_WARN(("iconv failure: %s\n", strerror(myerrno))); |
175 unicode_init(); | 176 unicode_init(); |
176 return (size_t)-1; | 177 return (size_t)-1; |
177 } | 178 } |
178 return (icresult) ? (size_t)-1 : 0; | 179 return (icresult) ? (size_t)-1 : 0; |
179 } | 180 } |
285 | 286 |
286 | 287 |
287 void vbset(vbuf * vb, void *b, size_t len) // set vbuf b size=len, resize if necessary, relen = how much to over-allocate | 288 void vbset(vbuf * vb, void *b, size_t len) // set vbuf b size=len, resize if necessary, relen = how much to over-allocate |
288 { | 289 { |
289 vbresize(vb, len); | 290 vbresize(vb, len); |
290 | |
291 memcpy(vb->b, b, len); | 291 memcpy(vb->b, b, len); |
292 vb->dlen = len; | 292 vb->dlen = len; |
293 } | 293 } |
294 | 294 |
295 | 295 |