Minor refactoring, related to lzah
[deark.git] / src / fmtutil-lzah.c
blobe599f639165e09fa0cb250c028fe9e374fb724fc
1 // This file is part of Deark.
2 // Copyright (C) 2021 Jason Summers
3 // See the file COPYING for terms of use.
5 // LZH with adaptive Huffman coding
7 #define DE_NOT_IN_MODULE
8 #include "deark-private.h"
9 #include "deark-fmtutil.h"
11 #include "../foreign/lzhuf.h"
13 void fmtutil_get_lzhuf_d_code_and_len(UI n, UI *pd_code, UI *pd_len)
15 if(n<32 || n>=256) { *pd_code = 0; *pd_len = 3; }
16 else if(n<80) { *pd_code = (n-16)>>4; *pd_len = 4; }
17 else if(n<144) { *pd_code = (n-48)>>3; *pd_len = 5; }
18 else if(n<192) { *pd_code = (n-96)>>2; *pd_len = 6; }
19 else if(n<240) { *pd_code = (n-144)>>1; *pd_len = 7; }
20 else { *pd_code = n-192; *pd_len = 8; };
23 // codec_private_params: 'struct de_lh1_params'. Can be NULL.
24 void fmtutil_lh1_codectype1(deark *c, struct de_dfilter_in_params *dcmpri,
25 struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres,
26 void *codec_private_params)
28 struct lzahuf_ctx *cctx = NULL;
30 cctx = de_malloc(c, sizeof(struct lzahuf_ctx));
31 cctx->c = c;
32 cctx->modname = "lzhuf";
33 cctx->dcmpri = dcmpri;
34 cctx->dcmpro = dcmpro;
35 cctx->dres = dres;
37 if(codec_private_params) {
38 // Use params from caller, if present.
39 de_memcpy(&cctx->lh1p, codec_private_params, sizeof(struct de_lh1_params));
41 else {
42 // Set default params.
43 cctx->lh1p.history_fill_val = 0x20;
46 cctx->bitrd.f = dcmpri->f;
47 cctx->bitrd.curpos = dcmpri->pos;
48 cctx->bitrd.endpos = dcmpri->pos + dcmpri->len;
50 lzhuf_Decode(cctx);
52 de_bitreader_skip_to_byte_boundary(&cctx->bitrd);
53 cctx->dres->bytes_consumed = cctx->bitrd.curpos - cctx->dcmpri->pos;
54 if(cctx->dres->bytes_consumed<0) {
55 cctx->dres->bytes_consumed = 0;
57 cctx->dres->bytes_consumed_valid = 1;
59 de_free(c, cctx);