Rizin
unix-like reverse engineering framework and cli tools
qtm.h
Go to the documentation of this file.
1 /* This file is part of libmspack.
2  * (C) 2003-2004 Stuart Caie.
3  *
4  * The Quantum method was created by David Stafford, adapted by Microsoft
5  * Corporation.
6  *
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
9  *
10  * For further details, see the file COPYING.LIB distributed with libmspack
11  */
12 
13 #ifndef MSPACK_QTM_H
14 #define MSPACK_QTM_H 1
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /* Quantum compression / decompression definitions */
21 
22 #define QTM_FRAME_SIZE (32768)
23 
24 struct qtmd_modelsym {
25  unsigned short sym, cumfreq;
26 };
27 
28 struct qtmd_model {
31 };
32 
33 struct qtmd_stream {
34  struct mspack_system *sys; /* I/O routines */
35  struct mspack_file *input; /* input file handle */
36  struct mspack_file *output; /* output file handle */
37 
38  unsigned char *window; /* decoding window */
39  unsigned int window_size; /* window size */
40  unsigned int window_posn; /* decompression offset within window */
41  unsigned int frame_todo; /* bytes remaining for current frame */
42 
43  unsigned short H, L, C; /* high/low/current: arith coding state */
44  unsigned char header_read; /* have we started decoding a new frame? */
45 
46  int error;
47 
48  /* I/O buffers */
49  unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
50  unsigned int bit_buffer, inbuf_size;
51  unsigned char bits_left, input_end;
52 
53  /* four literal models, each representing 64 symbols
54  * model0 for literals from 0 to 63 (selector = 0)
55  * model1 for literals from 64 to 127 (selector = 1)
56  * model2 for literals from 128 to 191 (selector = 2)
57  * model3 for literals from 129 to 255 (selector = 3) */
58  struct qtmd_model model0, model1, model2, model3;
59 
60  /* three match models.
61  * model4 for match with fixed length of 3 bytes
62  * model5 for match with fixed length of 4 bytes
63  * model6 for variable length match, encoded with model6len model */
64  struct qtmd_model model4, model5, model6, model6len;
65 
66  /* selector model. 0-6 to say literal (0,1,2,3) or match (4,5,6) */
67  struct qtmd_model model7;
68 
69  /* symbol arrays for all models */
70  struct qtmd_modelsym m0sym[64 + 1];
71  struct qtmd_modelsym m1sym[64 + 1];
72  struct qtmd_modelsym m2sym[64 + 1];
73  struct qtmd_modelsym m3sym[64 + 1];
74  struct qtmd_modelsym m4sym[24 + 1];
75  struct qtmd_modelsym m5sym[36 + 1];
76  struct qtmd_modelsym m6sym[42 + 1], m6lsym[27 + 1];
77  struct qtmd_modelsym m7sym[7 + 1];
78 };
79 
80 /* allocates Quantum decompression state for decoding the given stream.
81  *
82  * - returns NULL if window_bits is outwith the range 10 to 21 (inclusive).
83  *
84  * - uses system->alloc() to allocate memory
85  *
86  * - returns NULL if not enough memory
87  *
88  * - window_bits is the size of the Quantum window, from 1Kb (10) to 2Mb (21).
89  *
90  * - input_buffer_size is the number of bytes to use to store bitstream data.
91  */
92 extern struct qtmd_stream *qtmd_init(struct mspack_system *system,
93  struct mspack_file *input,
94  struct mspack_file *output,
95  int window_bits,
96  int input_buffer_size);
97 
98 /* decompresses, or decompresses more of, a Quantum stream.
99  *
100  * - out_bytes of data will be decompressed and the function will return
101  * with an MSPACK_ERR_OK return code.
102  *
103  * - decompressing will stop as soon as out_bytes is reached. if the true
104  * amount of bytes decoded spills over that amount, they will be kept for
105  * a later invocation of qtmd_decompress().
106  *
107  * - the output bytes will be passed to the system->write() function given in
108  * qtmd_init(), using the output file handle given in qtmd_init(). More
109  * than one call may be made to system->write()
110  *
111  * - Quantum will read input bytes as necessary using the system->read()
112  * function given in qtmd_init(), using the input file handle given in
113  * qtmd_init(). This will continue until system->read() returns 0 bytes,
114  * or an error.
115  */
116 extern int qtmd_decompress(struct qtmd_stream *qtm, off_t out_bytes);
117 
118 /* frees all state associated with a Quantum data stream
119  *
120  * - calls system->free() using the system pointer given in qtmd_init()
121  */
122 void qtmd_free(struct qtmd_stream *qtm);
123 
124 #ifdef __cplusplus
125 }
126 #endif
127 
128 #endif
void qtmd_free(struct qtmd_stream *qtm)
Definition: qtmd.c:482
int qtmd_decompress(struct qtmd_stream *qtm, off_t out_bytes)
Definition: qtmd.c:256
struct qtmd_stream * qtmd_init(struct mspack_system *system, struct mspack_file *input, struct mspack_file *output, int window_bits, int input_buffer_size)
Definition: qtmd.c:186
int off_t
Definition: sftypes.h:41
Definition: qtm.h:28
struct qtmd_modelsym * syms
Definition: qtm.h:30
int entries
Definition: qtm.h:29
int shiftsleft
Definition: qtm.h:29
unsigned short sym
Definition: qtm.h:25
unsigned short cumfreq
Definition: qtm.h:25
unsigned char * i_end
Definition: qtm.h:49
struct qtmd_modelsym m1sym[64+1]
Definition: qtm.h:71
unsigned short H
Definition: qtm.h:43
struct qtmd_modelsym m2sym[64+1]
Definition: qtm.h:72
struct qtmd_model model7
Definition: qtm.h:67
struct qtmd_modelsym m6sym[42+1]
Definition: qtm.h:76
unsigned char * o_end
Definition: qtm.h:49
struct qtmd_modelsym m7sym[7+1]
Definition: qtm.h:77
struct mspack_system * sys
Definition: qtm.h:34
struct mspack_file * input
Definition: qtm.h:35
struct qtmd_model model0 model1 model2 model3
Definition: qtm.h:58
unsigned char * i_ptr
Definition: qtm.h:49
unsigned char input_end
Definition: qtm.h:51
unsigned int bit_buffer
Definition: qtm.h:50
unsigned char * inbuf
Definition: qtm.h:49
int error
Definition: qtm.h:46
struct qtmd_modelsym m4sym[24+1]
Definition: qtm.h:74
unsigned int inbuf_size
Definition: qtm.h:50
unsigned char * window
Definition: qtm.h:38
struct mspack_file * output
Definition: qtm.h:36
struct qtmd_modelsym m6lsym[27+1]
Definition: qtm.h:76
struct qtmd_modelsym m3sym[64+1]
Definition: qtm.h:73
unsigned char * o_ptr
Definition: qtm.h:49
unsigned char bits_left
Definition: qtm.h:51
unsigned int frame_todo
Definition: qtm.h:41
unsigned char header_read
Definition: qtm.h:44
struct qtmd_modelsym m5sym[36+1]
Definition: qtm.h:75
unsigned int window_size
Definition: qtm.h:39
unsigned short C
Definition: qtm.h:43
unsigned short L
Definition: qtm.h:43
struct qtmd_model model4 model5 model6 model6len
Definition: qtm.h:64
struct qtmd_modelsym m0sym[64+1]
Definition: qtm.h:70
unsigned int window_posn
Definition: qtm.h:40
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237