links

lynx-like text mode web browser
git clone anongit@rnpnr.xyz:links.git
Log | Files | Refs | Feed | README | LICENSE

Commit: deb0c6e8f924af59c95eb161a7705526c3de6167
Parent: 3af77190cf0737de521882be1d619e4dce18f5b5
Author: opask
Date:   Thu, 23 Aug 2018 19:14:29 -0600

style: compress.c

Diffstat:
Mcompress.c | 281++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 172 insertions(+), 109 deletions(-)

diff --git a/compress.c b/compress.c @@ -70,10 +70,10 @@ static int decode_gzip(struct terminal *term, struct cache_entry *ce, int defl, struct list_head *lf; size_t size; - retry_after_memory_error: +retry_after_memory_error: memory_error = 0; decoder_memory_init(&p, &size, ce->length); - init_again: +init_again: err = 0; skip_gzip_header = 0; old_zlib = 0; @@ -86,122 +86,157 @@ static int decode_gzip(struct terminal *term, struct cache_entry *ce, int defl, z.zfree = NULL; z.opaque = NULL; r = inflateInit2(&z, defl == 1 ? 15 : defl == 2 ? -15 : 15 + 16); - init_failed: +init_failed: switch (r) { - case Z_OK: break; - case Z_MEM_ERROR: memory_error = 1; - err = 1; - goto after_inflateend; - case Z_STREAM_ERROR: - if (!defl && !old_zlib) { - if (defrag_entry(ce)) { - memory_error = 1; - err = 1; - goto after_inflateend; - } - r = inflateInit2(&z, -15); - skip_gzip_header = 1; - old_zlib = 1; - goto init_failed; - } - decompress_error(term, ce, cast_uchar "zlib", z.msg ? (unsigned char *)z.msg : (unsigned char *)"Invalid parameter", errp); - err = 1; - goto after_inflateend; - case Z_VERSION_ERROR: decompress_error(term, ce, cast_uchar "zlib", z.msg ? (unsigned char *)z.msg : (unsigned char *)"Bad zlib version", errp); - err = 1; - goto after_inflateend; - default: decompress_error(term, ce, cast_uchar "zlib", z.msg ? (unsigned char *)z.msg : (unsigned char *)"Unknown return value on inflateInit2", errp); - err = 1; - goto after_inflateend; + case Z_OK: + break; + case Z_MEM_ERROR: + memory_error = 1; + err = 1; + goto after_inflateend; + case Z_STREAM_ERROR: + if (!defl && !old_zlib) { + if (defrag_entry(ce)) { + memory_error = 1; + err = 1; + goto after_inflateend; + } + r = inflateInit2(&z, -15); + skip_gzip_header = 1; + old_zlib = 1; + goto init_failed; + } + decompress_error(term, ce, cast_uchar "zlib", + z.msg ? (unsigned char *)z.msg + : (unsigned char *)"Invalid parameter", errp); + err = 1; + goto after_inflateend; + case Z_VERSION_ERROR: + decompress_error(term, ce, cast_uchar "zlib", + z.msg ? (unsigned char *)z.msg + : (unsigned char *)"Bad zlib version", errp); + err = 1; + goto after_inflateend; + default: + decompress_error(term, ce, cast_uchar "zlib", + z.msg ? (unsigned char *)z.msg + : (unsigned char *)"Unknown return value on inflateInit2", + errp); + err = 1; + goto after_inflateend; } offset = 0; foreach(struct fragment, f, lf, ce->frag) { - if (f->offset != offset) break; + if (f->offset != offset) + break; z.next_in = f->data; z.avail_in = (unsigned)f->length; - if ((off_t)z.avail_in != f->length) overalloc(); - repeat_frag: + if ((off_t)z.avail_in != f->length) + overalloc(); +repeat_frag: if (skip_gzip_header == 2) { - if (z.avail_in < 8) goto finish; + if (z.avail_in < 8) + goto finish; z.next_in = (unsigned char *)z.next_in + 8; z.avail_in -= 8; skip_gzip_header = 1; } if (skip_gzip_header) { + /* FIXME */ /* if zlib is old, we have to skip gzip header manually otherwise zlib 1.2.x can do it automatically */ unsigned char *head = z.next_in; unsigned headlen = 10; - if (z.avail_in <= 11) goto finish; + if (z.avail_in <= 11) + goto finish; if (head[0] != 0x1f || head[1] != 0x8b) { - decompress_error(term, ce, cast_uchar "zlib", TEXT_(T_COMPRESSED_ERROR), errp); + decompress_error(term, ce, cast_uchar "zlib", + TEXT_(T_COMPRESSED_ERROR), errp); err = 1; goto finish; } if (head[2] != 8 || head[3] & 0xe0) { - decompress_error(term, ce, cast_uchar "zlib", TEXT_(T_UNKNOWN_COMPRESSION_METHOD), errp); + decompress_error(term, ce, cast_uchar "zlib", + TEXT_(T_UNKNOWN_COMPRESSION_METHOD), + errp); err = 1; goto finish; } if (head[3] & 0x04) { headlen += 2 + head[10] + (head[11] << 8); - if (headlen >= z.avail_in) goto finish; + if (headlen >= z.avail_in) + goto finish; } - if (head[3] & 0x08) { + if (head[3] & 0x08) do { headlen++; if (headlen >= z.avail_in) goto finish; } while (head[headlen - 1]); - } - if (head[3] & 0x10) { + if (head[3] & 0x10) do { headlen++; if (headlen >= z.avail_in) goto finish; } while (head[headlen - 1]); - } if (head[3] & 0x01) { headlen += 2; - if (headlen >= z.avail_in) goto finish; + if (headlen >= z.avail_in) + goto finish; } z.next_in = (unsigned char *)z.next_in + headlen; z.avail_in -= headlen; skip_gzip_header = 0; } - r = inflate(&z, f->list_entry.next == &ce->frag ? Z_SYNC_FLUSH : Z_NO_FLUSH); + r = inflate(&z, f->list_entry.next == &ce->frag ? Z_SYNC_FLUSH + : Z_NO_FLUSH); switch (r) { - case Z_OK: break; - case Z_BUF_ERROR: break; - case Z_STREAM_END: r = inflateEnd(&z); - if (r != Z_OK) goto end_failed; - r = inflateInit2(&z, old_zlib ? -15 : defl ? 15 : 15 + 16); - if (r != Z_OK) { - old_zlib = 0; - goto init_failed; - } - if (old_zlib) { - skip_gzip_header = 2; - } - break; - case Z_NEED_DICT: - case Z_DATA_ERROR: if (defl == 1) { - defl = 2; - r = inflateEnd(&z); - if (r != Z_OK) goto end_failed; - goto init_again; - } - decompress_error(term, ce, cast_uchar "zlib", z.msg ? (unsigned char *)z.msg : TEXT_(T_COMPRESSED_ERROR), errp); - err = 1; - goto finish; - case Z_STREAM_ERROR: decompress_error(term, ce, cast_uchar "zlib", z.msg ? (unsigned char *)z.msg : (unsigned char *)"Internal error on inflate", errp); - err = 1; - goto finish; - case Z_MEM_ERROR: - mem_error: memory_error = 1; - err = 1; - goto finish; - default: decompress_error(term, ce, cast_uchar "zlib", z.msg ? (unsigned char *)z.msg : (unsigned char *)"Unknown return value on inflate", errp); - err = 1; - break; + case Z_OK: + case Z_BUF_ERROR: + break; + case Z_STREAM_END: + r = inflateEnd(&z); + if (r != Z_OK) + goto end_failed; + r = inflateInit2(&z, old_zlib ? -15 : defl ? 15 : 15 + 16); + if (r != Z_OK) { + old_zlib = 0; + goto init_failed; + } + if (old_zlib) + skip_gzip_header = 2; + break; + case Z_NEED_DICT: + case Z_DATA_ERROR: + if (defl == 1) { + defl = 2; + r = inflateEnd(&z); + if (r != Z_OK) + goto end_failed; + goto init_again; + } + decompress_error(term, ce, cast_uchar "zlib", + z.msg ? (unsigned char *)z.msg + : TEXT_(T_COMPRESSED_ERROR), errp); + err = 1; + goto finish; + case Z_STREAM_ERROR: + decompress_error(term, ce, cast_uchar "zlib", + z.msg ? (unsigned char *)z.msg + : (unsigned char *)"Internal error on inflate", + errp); + err = 1; + goto finish; + case Z_MEM_ERROR: +mem_error: + memory_error = 1; + err = 1; + goto finish; + default: + decompress_error(term, ce, cast_uchar "zlib", + z.msg ? (unsigned char *)z.msg + : (unsigned char *)"Unknown return value on inflate", + errp); + err = 1; + break; } if (!z.avail_out) { size_t addsize; @@ -211,34 +246,48 @@ static int decode_gzip(struct terminal *term, struct cache_entry *ce, int defl, z.avail_out = (unsigned)addsize; size += addsize; } - if (z.avail_in) goto repeat_frag; + if (z.avail_in) + goto repeat_frag; + /* FIXME */ /* In zlib 1.1.3, inflate(Z_SYNC_FLUSH) doesn't work. The following line fixes it --- for last fragment, loop until we get an eof. */ - if (r == Z_OK && f->list_entry.next == &ce->frag) goto repeat_frag; + if (r == Z_OK && f->list_entry.next == &ce->frag) + goto repeat_frag; offset += f->length; } - finish: +finish: r = inflateEnd(&z); - end_failed: +end_failed: switch (r) { - case Z_OK: break; - case Z_STREAM_ERROR: decompress_error(term, ce, cast_uchar "zlib", z.msg ? (unsigned char *)z.msg : (unsigned char *)"Internal error on inflateEnd", errp); - err = 1; - break; - case Z_MEM_ERROR: memory_error = 1; - err = 1; - break; - default: decompress_error(term, ce, cast_uchar "zlib", z.msg ? (unsigned char *)z.msg : (unsigned char *)"Unknown return value on inflateEnd", errp); - err = 1; - break; + case Z_OK: + break; + case Z_STREAM_ERROR: + decompress_error(term, ce, cast_uchar "zlib", + z.msg ? (unsigned char *)z.msg + : (unsigned char *)"Internal error on inflateEnd", + errp); + err = 1; + break; + case Z_MEM_ERROR: + memory_error = 1; + err = 1; + break; + default: + decompress_error(term, ce, cast_uchar "zlib", + z.msg ? (unsigned char *)z.msg + : (unsigned char *)"Unknown return value on inflateEnd", errp); + err = 1; + break; } - after_inflateend: +after_inflateend: if (memory_error) { free(p); if (out_of_memory()) goto retry_after_memory_error; - decompress_error(term, ce, cast_uchar "zlib", z.msg ? (unsigned char *)z.msg : TEXT_(T_OUT_OF_MEMORY), errp); + decompress_error(term, ce, cast_uchar "zlib", + z.msg ? (unsigned char *)z.msg + : TEXT_(T_OUT_OF_MEMORY), errp); return 1; } if (err && (unsigned char *)z.next_out == p) { @@ -257,37 +306,47 @@ int get_file_by_term(struct terminal *term, struct cache_entry *ce, unsigned cha unsigned char *enc; struct fragment *fr; int e; - if (errp) *errp = 0; + if (errp) + *errp = 0; *start = *end = NULL; - if (!ce) return 1; + if (!ce) + return 1; if (ce->decompressed) { - return_decompressed: +return_decompressed: *start = ce->decompressed; *end = ce->decompressed + ce->decompressed_len; return 0; } enc = get_content_encoding(ce->head, ce->url, 0); if (enc) { - if (!casestrcmp(enc, cast_uchar "gzip") || !casestrcmp(enc, cast_uchar "x-gzip") || !casestrcmp(enc, cast_uchar "deflate")) { + if (!casestrcmp(enc, cast_uchar "gzip") + || !casestrcmp(enc, cast_uchar "x-gzip") + || !casestrcmp(enc, cast_uchar "deflate")) { int defl = !casestrcmp(enc, cast_uchar "deflate"); free(enc); - if (decode_gzip(term, ce, defl, errp)) goto uncompressed; + if (decode_gzip(term, ce, defl, errp)) + goto uncompressed; goto return_decompressed; } free(enc); goto uncompressed; } - uncompressed: +uncompressed: if ((e = defrag_entry(ce)) < 0) { unsigned char *msg = get_err_msg(e); if (display_error(term, TEXT_(T_ERROR), errp)) { unsigned char *u = display_url(term, ce->url, 1); - msg_box(term, getml(u, NULL), TEXT_(T_ERROR), AL_CENTER, TEXT_(T_ERROR_LOADING), cast_uchar " ", u, cast_uchar ":\n\n", msg, MSG_BOX_END, NULL, 1, TEXT_(T_CANCEL), msg_box_null, B_ENTER | B_ESC); + msg_box(term, getml(u, NULL), TEXT_(T_ERROR), AL_CENTER, + TEXT_(T_ERROR_LOADING), cast_uchar " ", u, + cast_uchar ":\n\n", msg, MSG_BOX_END, NULL, 1, + TEXT_(T_CANCEL), msg_box_null, B_ENTER | B_ESC); } } - if (list_empty(ce->frag)) return 1; + if (list_empty(ce->frag)) + return 1; fr = list_struct(ce->frag.next, struct fragment); - if (fr->offset || !fr->length) return 1; + if (fr->offset || !fr->length) + return 1; *start = fr->data; *end = fr->data + fr->length; return 0; @@ -297,7 +356,8 @@ int get_file(struct object_request *o, unsigned char **start, unsigned char **en { struct terminal *term; *start = *end = NULL; - if (!o) return 1; + if (!o) + return 1; term = find_terminal(o->term); return get_file_by_term(term, o->ce, start, end, NULL); } @@ -306,7 +366,9 @@ void free_decompressed_data(struct cache_entry *e) { if (e->decompressed) { if (decompressed_cache_size < e->decompressed_len) - internal("free_decompressed_data: decompressed_cache_size underflow %lu, %lu", (unsigned long)decompressed_cache_size, (unsigned long)e->decompressed_len); + internal("free_decompressed_data: decompressed_cache_size underflow %lu, %lu", + (unsigned long)decompressed_cache_size, + (unsigned long)e->decompressed_len); decompressed_cache_size -= e->decompressed_len; e->decompressed_len = 0; free(e->decompressed); @@ -317,13 +379,14 @@ void free_decompressed_data(struct cache_entry *e) void add_compress_methods(unsigned char **s, int *l) { int cl = 0; - { - if (!cl) cl = 1; else add_to_str(s, l, cast_uchar ", "); - add_to_str(s, l, cast_uchar "ZLIB"); + if (!cl) + cl = 1; + else + add_to_str(s, l, cast_uchar ", "); + add_to_str(s, l, cast_uchar "ZLIB"); #ifdef zlib_version - add_to_str(s, l, cast_uchar " ("); - add_to_str(s, l, (unsigned char *)zlib_version); - add_to_str(s, l, cast_uchar ")"); + add_to_str(s, l, cast_uchar " ("); + add_to_str(s, l, (unsigned char *)zlib_version); + add_to_str(s, l, cast_uchar ")"); #endif - } }