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 {
29   int shiftsleft, entries;
30   struct qtmd_modelsym *syms;
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
129