1
/* This file is part of libmspack.
2
* (C) 2003-2004 Stuart Caie.
4
* The Quantum method was created by David Stafford, adapted by Microsoft
7
* libmspack is free software; you can redistribute it and/or modify it under
8
* the terms of the GNU Lesser General Public License (LGPL) version 2.1
10
* For further details, see the file COPYING.LIB distributed with libmspack
14
#define MSPACK_QTM_H 1
16
/* Quantum compression / decompression definitions */
18
#define QTM_FRAME_SIZE (32768)
20
struct qtmd_modelsym {
21
unsigned short sym, cumfreq;
25
int shiftsleft, entries;
26
struct qtmd_modelsym *syms;
30
struct mspack_system *sys; /* I/O routines */
31
struct mspack_file *input; /* input file handle */
32
struct mspack_file *output; /* output file handle */
34
unsigned char *window; /* decoding window */
35
unsigned int window_size; /* window size */
36
unsigned int window_posn; /* decompression offset within window */
37
unsigned int frame_start; /* start of current frame within window */
39
unsigned short H, L, C; /* high/low/current: arith coding state */
40
unsigned char header_read; /* have we started decoding a new frame? */
45
unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
46
unsigned int bit_buffer, inbuf_size;
47
unsigned char bits_left;
49
/* four literal models, each representing 64 symbols
50
* model0 for literals from 0 to 63 (selector = 0)
51
* model1 for literals from 64 to 127 (selector = 1)
52
* model2 for literals from 128 to 191 (selector = 2)
53
* model3 for literals from 129 to 255 (selector = 3) */
54
struct qtmd_model model0, model1, model2, model3;
56
/* three match models.
57
* model4 for match with fixed length of 3 bytes
58
* model5 for match with fixed length of 4 bytes
59
* model6 for variable length match, encoded with model6len model */
60
struct qtmd_model model4, model5, model6, model6len;
62
/* selector model. 0-6 to say literal (0,1,2,3) or match (4,5,6) */
63
struct qtmd_model model7;
65
/* symbol arrays for all models */
66
struct qtmd_modelsym m0sym[64 + 1];
67
struct qtmd_modelsym m1sym[64 + 1];
68
struct qtmd_modelsym m2sym[64 + 1];
69
struct qtmd_modelsym m3sym[64 + 1];
70
struct qtmd_modelsym m4sym[24 + 1];
71
struct qtmd_modelsym m5sym[36 + 1];
72
struct qtmd_modelsym m6sym[42 + 1], m6lsym[27 + 1];
73
struct qtmd_modelsym m7sym[7 + 1];
76
/* allocates Quantum decompression state for decoding the given stream.
78
* - returns NULL if window_bits is outwith the range 10 to 21 (inclusive).
80
* - uses system->alloc() to allocate memory
82
* - returns NULL if not enough memory
84
* - window_bits is the size of the Quantum window, from 1Kb (10) to 2Mb (21).
86
* - input_buffer_size is the number of bytes to use to store bitstream data.
88
extern struct qtmd_stream *qtmd_init(struct mspack_system *system,
89
struct mspack_file *input,
90
struct mspack_file *output,
92
int input_buffer_size);
94
/* decompresses, or decompresses more of, a Quantum stream.
96
* - out_bytes of data will be decompressed and the function will return
97
* with an MSPACK_ERR_OK return code.
99
* - decompressing will stop as soon as out_bytes is reached. if the true
100
* amount of bytes decoded spills over that amount, they will be kept for
101
* a later invocation of qtmd_decompress().
103
* - the output bytes will be passed to the system->write() function given in
104
* qtmd_init(), using the output file handle given in qtmd_init(). More
105
* than one call may be made to system->write()
107
* - Quantum will read input bytes as necessary using the system->read()
108
* function given in qtmd_init(), using the input file handle given in
109
* qtmd_init(). This will continue until system->read() returns 0 bytes,
112
extern int qtmd_decompress(struct qtmd_stream *qtm, off_t out_bytes);
114
/* frees all state associated with a Quantum data stream
116
* - calls system->free() using the system pointer given in qtmd_init()
118
void qtmd_free(struct qtmd_stream *qtm);