Updated version number for development
[deark.git] / src / fmtutil-zip.c
blob349bcaabe2552f365d2797068ee0ff43844411f2
1 // This file is part of Deark.
2 // Copyright (C) 2019 Jason Summers
3 // See the file COPYING for terms of use.
5 // Compression formats specific to ZIP
7 #define DE_NOT_IN_MODULE
8 #include "deark-config.h"
9 #include "deark-private.h"
10 #include "deark-fmtutil.h"
12 #define OZUR_UINT8 u8
13 #define OZUR_OFF_T i64
14 #include "../foreign/ozunreduce.h"
16 // Struct for userdata
17 struct ozXX_udatatype {
18 deark *c;
19 dbuf *inf;
20 i64 inf_curpos;
21 dbuf *outf;
22 int dumptrees;
25 static size_t ozXX_read(struct ozXX_udatatype *uctx, u8 *buf, size_t size)
27 dbuf_read(uctx->inf, buf, uctx->inf_curpos, (i64)size);
28 uctx->inf_curpos += (i64)size;
29 return size;
32 static size_t ozXX_write(struct ozXX_udatatype *uctx, const u8 *buf, size_t size)
34 dbuf_write(uctx->outf, buf, (i64)size);
35 return size;
38 // params: Unused, should be NULL
39 void fmtutil_decompress_zip_shrink(deark *c, struct de_dfilter_in_params *dcmpri,
40 struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres,
41 void *params)
43 struct de_lzw_params delzwp;
45 de_zeromem(&delzwp, sizeof(struct de_lzw_params));
46 delzwp.fmt = DE_LZWFMT_ZIPSHRINK;
47 fmtutil_decompress_lzw(c, dcmpri, dcmpro, dres, &delzwp);
50 static size_t my_ozur_read(ozur_ctx *ozur, OZUR_UINT8 *buf, size_t size)
52 return ozXX_read((struct ozXX_udatatype*)ozur->userdata, buf, size);
55 static size_t my_ozur_write(ozur_ctx *ozur, const OZUR_UINT8 *buf, size_t size)
57 return ozXX_write((struct ozXX_udatatype*)ozur->userdata, buf, size);
60 static void my_ozur_post_follower_sets_hook(ozur_ctx *ozur)
62 struct ozXX_udatatype *uctx = (struct ozXX_udatatype*)ozur->userdata;
64 de_dbg2(uctx->c, "finished reading follower sets, pos=%"I64_FMT, uctx->inf_curpos);
67 void fmtutil_decompress_zip_reduce(deark *c, struct de_dfilter_in_params *dcmpri,
68 struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres,
69 struct de_zipreduce_params *params)
71 int retval = 0;
72 ozur_ctx *ozur = NULL;
73 struct ozXX_udatatype uctx;
74 static const char *modname = "unreduce";
76 if(!dcmpro->len_known) goto done;
78 de_zeromem(&uctx, sizeof(struct ozXX_udatatype));
79 uctx.c = c;
80 uctx.inf = dcmpri->f;
81 uctx.inf_curpos = dcmpri->pos;
82 uctx.outf = dcmpro->f;
84 ozur = de_malloc(c, sizeof(ozur_ctx));
85 ozur->userdata = (void*)&uctx;
86 ozur->cb_read = my_ozur_read;
87 ozur->cb_write = my_ozur_write;
88 ozur->cb_post_follower_sets = my_ozur_post_follower_sets_hook;
90 ozur->cmpr_size = dcmpri->len;
91 ozur->uncmpr_size = dcmpro->expected_len;
92 ozur->cmpr_factor = params->cmpr_factor;
94 ozur_run(ozur);
96 if(ozur->error_code) {
97 de_dfilter_set_errorf(c, dres, modname, "Decompression failed (code %d)",
98 ozur->error_code);
100 else {
101 dres->bytes_consumed = ozur->cmpr_nbytes_consumed;
102 dres->bytes_consumed_valid = 1;
103 retval = 1;
106 done:
107 de_free(c, ozur);
108 if(retval==0 && !dres->errcode) {
109 de_dfilter_set_generic_error(c, dres, modname);