31 int chunkRemaining = *((
int *)dc->
state);
38 while (inputRemaining > 0) {
39 if (chunkRemaining > 2) {
41 int copylen =
MIN(chunkRemaining - 2, inputRemaining);
43 chunkRemaining -= copylen;
44 inputRemaining -= copylen;
48 if ((chunkRemaining == 2) && (inputRemaining > 0)) {
54 if ((chunkRemaining == 1) && (inputRemaining > 0)) {
65 if (!(eol = (
char *)memchr(inputPtr,
'\n', inputRemaining))) {
69 if (!(chunkRemaining = strtol(inputPtr, NULL, 0x10))) {
73 inputRemaining -= (eol - inputPtr) + 1;
81 *(
int *)dc->
state = chunkRemaining;
99 (void)inflateEnd((z_stream *)dc->
state);
118 z_stream *zs = (z_stream *)dc->
state;
120 int inputConsumed = 0;
123 while ((rc == Z_OK) && (inputConsumed < inlen)) {
124 zs->next_in = (Bytef *)instr + inputConsumed;
125 zs->avail_in = inlen - inputConsumed;
127 zs->next_out = (Bytef *)dc->
buffer;
130 rc = inflate(zs, Z_SYNC_FLUSH);
134 if ((rc == Z_OK) || (rc == Z_STREAM_END)) {
137 inputConsumed += zs->total_in;
140 }
else if (rc == Z_DATA_ERROR) {
141 MSG_ERR(
"gzip decompression error\n");
154 z_stream *zs = (z_stream *)dc->
state;
156 int inputConsumed = 0;
159 while ((rc == Z_OK) && (inputConsumed < inlen)) {
160 zs->next_in = (Bytef *)instr + inputConsumed;
161 zs->avail_in = inlen - inputConsumed;
163 zs->next_out = (Bytef *)dc->
buffer;
166 rc = inflate(zs, Z_SYNC_FLUSH);
170 if ((rc == Z_OK) || (rc == Z_STREAM_END)) {
173 inputConsumed += zs->total_in;
176 }
else if (rc == Z_DATA_ERROR) {
177 MSG_ERR(
"raw deflate decompression also failed\n");
191 z_stream *zs = (z_stream *)dc->
state;
193 int inputConsumed = 0;
196 while ((rc == Z_OK) && (inputConsumed < inlen)) {
197 zs->next_in = (Bytef *)instr + inputConsumed;
198 zs->avail_in = inlen - inputConsumed;
200 zs->next_out = (Bytef *)dc->
buffer;
203 rc = inflate(zs, Z_SYNC_FLUSH);
207 if ((rc == Z_OK) || (rc == Z_STREAM_END)) {
210 inputConsumed += zs->total_in;
213 }
else if (rc == Z_DATA_ERROR) {
214 MSG_WARN(
"Deflate decompression error. Certain servers illegally fail"
215 " to send data in a zlib wrapper. Let's try raw deflate.\n");
217 (void)inflateEnd(zs);
227 inflateInit2(zs, -MAX_WBITS);
242 size_t inLeft, outRoom;
251 while ((rc != EINVAL) && (inLeft > 0)) {
256 rc = iconv((iconv_t)dc->
state, &inPtr, &inLeft, &outPtr, &outRoom);
282 (void)iconv_close((iconv_t)(dc->
state));
296 int *chunk_remaining =
dNew(
int, 1);
297 *chunk_remaining = 0;
300 dc->
state = chunk_remaining;
309 z_stream *zs =
dNew(z_stream, 1);
332 if (format && *format) {
335 _MSG(
"gzipped data!\n");
338 zs = (z_stream *)dc->
state;
340 inflateInit2(zs, MAX_WBITS+16);
344 _MSG(
"deflated data!\n");
347 zs = (z_stream *)dc->
state;
352 MSG(
"Content-Encoding '%s' not recognized.\n", format);
373 iconv_t ic = iconv_open(
"UTF-8", format);
374 if (ic != (iconv_t) -1) {
383 MSG_WARN(
"Unable to convert from character encoding: '%s'\n", format);
394 return dc->
decode(dc, instr, inlen);
Decode * a_Decode_charset_init(const char *format)
Initialize decoder to translate from any character set known to iconv() to UTF-8.
Dstr * a_Decode_transfer_process(DecodeTransfer *dc, const char *instr, int inlen)
Decode 'Transfer-Encoding: chunked' data.
static Dstr * Decode_charset(Decode *dc, const char *instr, int inlen)
Translate to desired character set (UTF-8)
void a_Decode_transfer_free(DecodeTransfer *dc)
Decode * a_Decode_content_init(const char *format)
Initialize content decoder.
static Dstr * Decode_raw_deflate(Decode *dc, const char *instr, int inlen)
Decode (raw) deflated data.
DecodeTransfer * a_Decode_transfer_init(const char *format)
Initialize transfer decoder.
Dstr * a_Decode_process(Decode *dc, const char *instr, int inlen)
Decode data.
static Decode * Decode_content_init_common(void)
static Dstr * Decode_deflate(Decode *dc, const char *instr, int inlen)
Decode deflated data, initially presuming that the required zlib wrapper is there.
void a_Decode_free(Decode *dc)
static void Decode_charset_free(Decode *dc)
static Dstr * Decode_gzip(Decode *dc, const char *instr, int inlen)
Decode gzipped data.
bool_t a_Decode_transfer_finished(DecodeTransfer *dc)
static void Decode_compression_free(Decode *dc)
int dStrAsciiCasecmp(const char *s1, const char *s2)
Dstr * dStr_sized_new(int sz)
Create a new string with a given size.
void dStr_erase(Dstr *ds, int pos_0, int len)
Erase a substring.
void dStr_free(Dstr *ds, int all)
Free a dillo string.
void dStr_append_l(Dstr *ds, const char *s, int l)
Append a C string to a Dstr (providing length).
Dstr * dStr_new(const char *s)
Create a new string.
#define dNew(type, count)
bool_t finished
has the terminating chunk been seen?
Dstr *(* decode)(struct Decode *dc, const char *instr, int inlen)
void(* free)(struct Decode *dc)
static const char utf8_replacement_char[]
Unicode replacement character U+FFFD.