source: trunk/newview_dll/newview.c@ 363

Last change on this file since 363 was 363, checked in by RBRi, 16 years ago

logging via pmprintf added

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
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
61static unsigned int prefix_code[ TABLE_SIZE ];
62static unsigned char append_character[ TABLE_SIZE ];
63
64static unsigned char decode_stack[4000];
65static int num_bits;
66static int max_code;
67
68/*
69 * decode_string:
70 *
71 */
72char *decode_string(unsigned char *buffer, unsigned int code, BOOL* error) {
73 int i = 0;
74 *error = FALSE;
75 while (code > 255) {
76 *buffer++ = append_character[code];
77 code = prefix_code[code];
78 if (i++ >= 4000) {
79 *error = TRUE;
80 return( buffer );
81 }
82 }
83 *buffer = code;
84 return (buffer);
85}
86
87
88
89/*
90 * input_code:
91 * this function reads in bytes from the input
92 * stream.
93 */
94unsigned input_code(PBYTE *ppbInput, unsigned bytes_to_read) {
95 unsigned int return_value;
96 static unsigned long bytes_out = 0;
97 static int input_bit_count = 0;
98 static unsigned long input_bit_buffer = 0L;
99
100 while (input_bit_count <= 24) {
101 if (bytes_out <= bytes_to_read) {
102 input_bit_buffer |= (unsigned long)(**ppbInput) << (24 - input_bit_count);
103 (*ppbInput)++;
104 } else
105 input_bit_buffer |= 0x00;
106 bytes_out++;
107 input_bit_count += 8;
108 }
109 return_value = input_bit_buffer >> (32 - num_bits);
110 input_bit_buffer <<= num_bits;
111 input_bit_count -= num_bits;
112 if (bytes_out > bytes_to_read) { /* flush static vars and quit */
113 bytes_out = 0;
114 input_bit_count = 0;
115 input_bit_buffer = 0L;
116 return (TERMINATOR);
117 }
118 else
119 return (return_value);
120}
121
122
123/*
124 * LZWDecompressBlock:
125 * this takes one of the INF bitmap blocks
126 * and decompresses it using LZW algorithms.
127 */
128BOOL APIENTRY LZWDecompressBlock( PBYTE pbInput, // in: compressed data
129 PBYTE pOutput, // out: uncompressed data
130 unsigned int number_bytes, // in: bytes to decompress
131 unsigned long * pBytesOut, // out: bytes decompressed.
132 PBYTE pLastCode) // out: last byte decompressed.
133{
134 unsigned int next_code = FIRST_CODE;
135 unsigned int new_code;
136 unsigned int old_code;
137 int character, clear_flag = 1;
138 unsigned char *string;
139 BOOL error;
140
141 num_bits = INIT_BITS;
142 max_code = MAXVAL(num_bits);
143
144 *pBytesOut = 0;
145
146 _Pmpf(("LZWDecompressBlock"));
147
148 while ((new_code = input_code(&pbInput, number_bytes)) != TERMINATOR) {
149 if (clear_flag) {
150 clear_flag = 0;
151 old_code = new_code;
152 character = old_code;
153 *pOutput = (BYTE)old_code;
154 pOutput ++;
155 *pLastCode = (BYTE)old_code;
156 (*pBytesOut) ++;
157 continue;
158 }
159 if (new_code == CLEAR_TABLE) {
160 clear_flag = 1;
161 num_bits = INIT_BITS;
162 next_code = FIRST_CODE;
163 max_code = MAXVAL(num_bits);
164 continue;
165 }
166
167 if (new_code >= next_code) {
168 *decode_stack = character;
169 string = decode_string(decode_stack + 1, old_code, &error);
170 }
171 else
172 string = decode_string(decode_stack, new_code, &error);
173
174 if ( error ) {
175 return FALSE;
176 }
177 character = *string;
178 while (string >= decode_stack) {
179 *pOutput = *string;
180 pOutput ++;
181 string --;
182
183 (*pBytesOut)++;
184 }
185 *pLastCode = *( string+1 );
186
187 if (next_code <= max_code) {
188 prefix_code[next_code] = old_code;
189 append_character[next_code++] = character;
190 if (next_code == max_code && num_bits < MAX_BITS) {
191 max_code = MAXVAL(++num_bits);
192 }
193 }
194 old_code = new_code;
195 }
196 return (TRUE);
197}
Note: See TracBrowser for help on using the repository browser.