1 | #define INCL_DOS
|
---|
2 | #define INCL_DOSERRORS
|
---|
3 | #include <os2.h>
|
---|
4 |
|
---|
5 | #include <stdio.h>
|
---|
6 | #include <stdlib.h>
|
---|
7 |
|
---|
8 |
|
---|
9 | #define _PMPRINTF_
|
---|
10 | #include <PMPRINTF.H>
|
---|
11 |
|
---|
12 |
|
---|
13 | #define VERSION "0.9.1"
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 | /********************************************************************
|
---|
18 | * *
|
---|
19 | * LZW decompression *
|
---|
20 | * *
|
---|
21 | *******************************************************************/
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * This is based on code (W) by Peter Fitzsimmons, pfitz@ican.net.
|
---|
25 | * His liner notes in the original:
|
---|
26 | * has its roots in a June 1990
|
---|
27 | * DDJ article "LZW REVISITED", by Shawn M. Regan
|
---|
28 | * --=>revision history<=--
|
---|
29 | * 1 lzw.c 21-Aug-96,2:24:36,`PLF' ;
|
---|
30 | * 2 lzw.c 24-Aug-96,2:27:24,`PLF' wip
|
---|
31 | *
|
---|
32 | * The code has been modified to take the input not from an
|
---|
33 | * open file, but from any memory region. For this, a double
|
---|
34 | * pointer is used, which must be passed to LZWDecompressBlock.
|
---|
35 | * I've also added a few comments for clarity.
|
---|
36 | *
|
---|
37 | */
|
---|
38 |
|
---|
39 | /* -- Stuff for LZW decompression -- */
|
---|
40 | #define INIT_BITS 9
|
---|
41 | #define MAX_BITS 12 /*PLF Tue 95-10-03 02:16:56*/
|
---|
42 | #define HASHING_SHIFT MAX_BITS - 8
|
---|
43 |
|
---|
44 | #if MAX_BITS == 15
|
---|
45 | #define TABLE_SIZE 36768
|
---|
46 | #elif MAX_BITS == 14
|
---|
47 | #define TABLE_SIZE 18041
|
---|
48 | #elif MAX_BITS == 13
|
---|
49 | #define TABLE_SIZE 9029
|
---|
50 | #else
|
---|
51 | #define TABLE_SIZE 5021
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | #define CLEAR_TABLE 256
|
---|
55 | #define TERMINATOR 257
|
---|
56 | #define FIRST_CODE 258
|
---|
57 | #define CHECK_TIME 100
|
---|
58 |
|
---|
59 | #define MAXVAL(n) (( 1 << (n)) -1)
|
---|
60 |
|
---|
61 | static unsigned int prefix_code[ TABLE_SIZE ];
|
---|
62 | static unsigned char append_character[ TABLE_SIZE ];
|
---|
63 |
|
---|
64 | static unsigned char decode_stack[4000];
|
---|
65 | static int num_bits;
|
---|
66 | static int max_code;
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * decode_string:
|
---|
70 | *
|
---|
71 | */
|
---|
72 | char* decode_string(unsigned char* buffer, unsigned int code, BOOL* error) {
|
---|
73 | int i = 0;
|
---|
74 | *error = FALSE;
|
---|
75 |
|
---|
76 | while (code > 255) {
|
---|
77 | *buffer++ = append_character[code];
|
---|
78 | code = prefix_code[code];
|
---|
79 | if (i++ >= 4000) {
|
---|
80 | *error = TRUE;
|
---|
81 | return( buffer );
|
---|
82 | }
|
---|
83 | }
|
---|
84 | *buffer = code;
|
---|
85 | return (buffer);
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * input_code:
|
---|
92 | * this function reads in bytes from the input
|
---|
93 | * stream.
|
---|
94 | */
|
---|
95 | unsigned input_code(PBYTE* ppbInput, unsigned bytes_to_read) {
|
---|
96 | unsigned int return_value;
|
---|
97 | static unsigned long bytes_out = 0;
|
---|
98 | static int input_bit_count = 0;
|
---|
99 | static unsigned long input_bit_buffer = 0L;
|
---|
100 |
|
---|
101 | while (input_bit_count <= 24) {
|
---|
102 | if (bytes_out <= bytes_to_read) {
|
---|
103 | input_bit_buffer |= (unsigned long)(**ppbInput) << (24 - input_bit_count);
|
---|
104 | (*ppbInput)++;
|
---|
105 | } else
|
---|
106 | input_bit_buffer |= 0x00;
|
---|
107 | bytes_out++;
|
---|
108 | input_bit_count += 8;
|
---|
109 | }
|
---|
110 | return_value = input_bit_buffer >> (32 - num_bits);
|
---|
111 | input_bit_buffer <<= num_bits;
|
---|
112 | input_bit_count -= num_bits;
|
---|
113 | if (bytes_out > bytes_to_read) { /* flush static vars and quit */
|
---|
114 | bytes_out = 0;
|
---|
115 | input_bit_count = 0;
|
---|
116 | input_bit_buffer = 0L;
|
---|
117 | return (TERMINATOR);
|
---|
118 | }
|
---|
119 | else
|
---|
120 | return (return_value);
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * LZWDecompressBlock:
|
---|
126 | * this takes one of the INF bitmap blocks
|
---|
127 | * and decompresses it using LZW algorithms.
|
---|
128 | */
|
---|
129 | BOOL APIENTRY LZWDecompressBlock( PBYTE pbInput, // in: compressed data
|
---|
130 | PBYTE pOutput, // out: uncompressed data
|
---|
131 | unsigned int number_bytes, // in: bytes to decompress
|
---|
132 | unsigned long * pBytesOut, // out: bytes decompressed.
|
---|
133 | PBYTE pLastCode) // out: last byte decompressed.
|
---|
134 | {
|
---|
135 | unsigned int next_code = FIRST_CODE;
|
---|
136 | unsigned int new_code;
|
---|
137 | unsigned int old_code;
|
---|
138 | int character, clear_flag = 1;
|
---|
139 | unsigned char *string;
|
---|
140 | BOOL error;
|
---|
141 |
|
---|
142 | num_bits = INIT_BITS;
|
---|
143 | max_code = MAXVAL(num_bits);
|
---|
144 |
|
---|
145 | *pBytesOut = 0;
|
---|
146 |
|
---|
147 | _Pmpf(("LZWDecompressBlock"));
|
---|
148 |
|
---|
149 | while ((new_code = input_code(&pbInput, number_bytes)) != TERMINATOR) {
|
---|
150 | if (clear_flag) {
|
---|
151 | clear_flag = 0;
|
---|
152 | old_code = new_code;
|
---|
153 | character = old_code;
|
---|
154 | *pOutput = (BYTE)old_code;
|
---|
155 | pOutput ++;
|
---|
156 | *pLastCode = (BYTE)old_code;
|
---|
157 | (*pBytesOut) ++;
|
---|
158 | continue;
|
---|
159 | }
|
---|
160 | if (new_code == CLEAR_TABLE) {
|
---|
161 | clear_flag = 1;
|
---|
162 | num_bits = INIT_BITS;
|
---|
163 | next_code = FIRST_CODE;
|
---|
164 | max_code = MAXVAL(num_bits);
|
---|
165 | continue;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (new_code >= next_code) {
|
---|
169 | *decode_stack = character;
|
---|
170 | string = decode_string(decode_stack + 1, old_code, &error);
|
---|
171 | }
|
---|
172 | else
|
---|
173 | string = decode_string(decode_stack, new_code, &error);
|
---|
174 |
|
---|
175 | if ( error ) {
|
---|
176 | return FALSE;
|
---|
177 | }
|
---|
178 | character = *string;
|
---|
179 | while (string >= decode_stack) {
|
---|
180 | *pOutput = *string;
|
---|
181 | pOutput ++;
|
---|
182 | string --;
|
---|
183 |
|
---|
184 | (*pBytesOut)++;
|
---|
185 | }
|
---|
186 | *pLastCode = *( string+1 );
|
---|
187 |
|
---|
188 | if (next_code <= max_code) {
|
---|
189 | prefix_code[next_code] = old_code;
|
---|
190 | append_character[next_code++] = character;
|
---|
191 | if (next_code == max_code && num_bits < MAX_BITS) {
|
---|
192 | max_code = MAXVAL(++num_bits);
|
---|
193 | }
|
---|
194 | }
|
---|
195 | old_code = new_code;
|
---|
196 | }
|
---|
197 | return (TRUE);
|
---|
198 | }
|
---|