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"
13 #define OZUR_OFF_T i64
14 #include "../foreign/ozunreduce.h"
16 // Struct for userdata
17 struct ozXX_udatatype
{
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
;
32 static size_t ozXX_write(struct ozXX_udatatype
*uctx
, const u8
*buf
, size_t size
)
34 dbuf_write(uctx
->outf
, buf
, (i64
)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
,
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
)
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
));
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
;
96 if(ozur
->error_code
) {
97 de_dfilter_set_errorf(c
, dres
, modname
, "Decompression failed (code %d)",
101 dres
->bytes_consumed
= ozur
->cmpr_nbytes_consumed
;
102 dres
->bytes_consumed_valid
= 1;
108 if(retval
==0 && !dres
->errcode
) {
109 de_dfilter_set_generic_error(c
, dres
, modname
);