### Bug Summary

File: | builds/wireshark/wireshark/wsutil/saplzclzh/csdecompr.c
Warning: | line 436, column 7 Value stored to 'e' is never read

### Annotated Source Code

Press '?' to see keyboard shortcuts

Show analyzer invocation
clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name csdecompr.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -D BUILD_WSUTIL -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -D wsutil_EXPORTS -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/build/wsutil -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-09-02-100927-3660-1 -x c /builds/wireshark/wireshark/wsutil/saplzclzh/csdecompr.c

Keyboard shortcuts:

- Use 'j/k' keys for keyboard navigation
- Use 'Shift+S' to show/hide relevant lines
- Use '?' to toggle this window

Close
Show only relevant lines Show control flow arrows

1 | /*@(#)cslzh.c 20.7 SAP 97/11/11
2 |
3 |
4 | ========== licence begin GPL
5 | Copyright (c) 1994-2005 SAP AG
6 |
7 | This program is free software; you can redistribute it and/or
8 | modify it under the terms of the GNU General Public License
9 | as published by the Free Software Foundation; either version 2
10 | of the License, or (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program; If not, see <https://www.gnu.org/licenses/>.
19 | ========== licence end
20 |
21 |
22 |
23 |
24 | */
25 | /*
26 | * Source Code Taken and Adapted from
27 | * ftp://ftp.sap.com/pub/maxdb/current/7.6.00/maxdb-source-7_6_00_37.zip
28 | * sys/src/pa/XXXCsObject...
29 | * sys/src/pa/XXXxxxclzc/h.cpp
30 | *
31 | * Changes to the source in ftp.sap.com:
32 | * Dont use a static CSHU and CSC structure for status handle, but
33 | * transport it locally to be thread safe. Therefore many function
34 | * signatures were changed and many csc. (to csc->) places, however
35 | * the functionality from ftp.sap.com is the same.
36 | */
37 | /*
38 | * SAP AG Walldorf
39 | * Systeme, Anwendungen und Produkte in der Datenverarbeitung
40 | *
41 | * (C) Copyright (c) 1994-2005 SAP AG
42 | */
43 | /*--------------------------------------------------------------------*/
44 | /* Adapter defines */
45 | /*--------------------------------------------------------------------*/
46 | #ifdef SAPwithUNICODE
47 | #undef SAPwithUNICODE
48 | #undef UNICODE
49 | #undef _UNICODE
50 | #endif
51 |
52 | /*--------------------------------------------------------------------*/
53 | /* system includes (OS-dependent) */
54 | /*--------------------------------------------------------------------*/
55 | #ifdef _WIN32
56 | #ifndef WIN32_MEAN_AND_LEAN
57 | #define WIN32_MEAN_AND_LEAN
58 | #include <windows.h>
59 | #endif
60 | #endif
61 |
62 | #include <stdlib.h>
63 | #include <stdio.h>
64 | #include <ctype.h>
65 | #include <string.h>
66 | #include <sys/stat.h>
67 |
68 | /*--------------------------------------------------------------------*/
69 | /* SAP includes */
70 | /*--------------------------------------------------------------------*/
71 | #include "csdecompr.h"
72 |
73 | static SAP_BYTE CsMagicHead[] = { "\037\235" }; /* 1F 9D */
74 | static unsigned short mask_bits[] =
75 | {
76 | 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
77 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
78 | };
79 |
80 | static unsigned border[] =
81 | { /* Order of the bit length code lengths */
82 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
83 | };
84 |
85 | static int cplens[] =
86 | { /* Copy lengths for literal codes 257..285 */
87 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
88 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
89 | };
90 | /* note: see note #13 above about the 258 in this list. */
91 |
92 | static int CPLENS_LEN = sizeof(cplens)/sizeof(int);
93 |
94 | static int cpdist[] =
95 | { /* Copy offsets for distance codes 0..29 */
96 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
97 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
98 | 8193, 12289, 16385, 24577
99 | };
100 |
101 | static int CsExtraLenBits[LENGTH_CODES29+2] /* extra bits for each length code */
102 | = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,99,99};
103 | static int *cplext = &CsExtraLenBits[0];
104 |
105 | static int CsExtraDistBits[D_CODES30] /* extra bits for each distance code */
106 | = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
107 | static int * cpdext = &CsExtraDistBits[0];
108 |
109 |
110 | SAP_INT CsGetAlgorithm (const SAP_BYTE * data)
111 | /*--------------------------------------------------------------------*/
112 | /* Get Algorithm number of compressed data */
113 | /*--------------------------------------------------------------------*/
114 | {
115 | return ((int) (data[4] & (unsigned char)0x0F));
116 | }
117 |
118 | SAP_INT CsGetLen (SAP_BYTE * data)
119 | /*--------------------------------------------------------------------*/
120 | /* Get the length of the original data stream */
121 | /* */
122 | /* Returns CS_E_FILENOT_COMPRESSED if the magic number is */
123 | /* different from magic header */
124 | /* else */
125 | /* Length of org. data stream */
126 | /*--------------------------------------------------------------------*/
127 | {
128 | SAP_INT len;
129 | /* file not compressed !!! .....*/
130 | if ((CsMagicHead[0] != data[5]) ||
131 | (CsMagicHead[1] != data[6]))
132 | {
133 | return ((SAP_INT)CS_E_FILENOTCOMPRESSED-50);
134 | }
135 |
136 | len = (SAP_INT)data[0] + /* read length from first buf ..*/
137 | ((SAP_INT)data[1] << 8) +
138 | ((SAP_INT)data[2] << 16) +
139 | ((SAP_INT)data[3] << 24);
140 |
141 | return len;
142 | }
143 | void NoBits (CSHU *cshu)
144 | /*--------------------------------------------------------------------*/
145 | /* */
146 | /*--------------------------------------------------------------------*/
147 | {
148 | unsigned x; /* number of bits in bit buffer ............*/
149 | int bitcount = 1; /* bitcount ................................*/
150 |
151 | NEEDBITS(NONSENSE_LENBITS){ while (cshu->bk < (2)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
152 | x = (unsigned) (cshu->bb & ((1 << NONSENSE_LENBITS2) - 1));
153 | DUMPBITS(NONSENSE_LENBITS){ cshu->bb >>= (2); cshu->bk -= (2); }
154 |
155 | if (x)
156 | {
157 | NEEDBITS(x){ while (cshu->bk < (x)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
158 | DUMPBITS(x){ cshu->bb >>= (x); cshu->bk -= (x); }
159 | }
160 | }
161 |
162 | HUFTREE * AllocHufTree (CSHU *cshu, unsigned size)
163 | {
164 | HUFTREE * p;
165 |
166 | p = cshu->InterBuf + cshu->AllocStackSize;
167 |
168 | cshu->AllocStackSize += size; /* * sizeof(HUFTREE); */
169 | /* InterBuf is at least 0x1000 in size (5.9.96 ab) .................*/
170 | if (cshu->AllocStackSize >= DE_STACK_SIZE0x1000) return (HUFTREE *) 0;
171 |
172 | return p;
173 | }
174 |
175 | static int BuildHufTree (CSHU *cshu,
176 | unsigned * b, /* code lengths in bits (all assumed <= BMAX) */
177 | unsigned n, /* number of codes (assumed <= N_MAX) */
178 | unsigned s, /* number of simple-valued codes (0..s-1) */
179 | int * d, /* list of base values for non-simple codes */
180 | int * e, /* list of extra bits for non-simple codes */
181 | int ll, /* length of lists d and e */
182 | HUFTREE **t, /* result: starting table */
183 | int * m) /* maximum lookup bits, returns actual */
184 |
185 | /* Given a list of code lengths and a maximum table size, make a set of
186 | tables to decode that set of codes. Return zero on success, one if
187 | the given code set is incomplete (the tables are still built in this
188 | case), two if the input is invalid (all zero length codes or an
189 | oversubscribed set of lengths), and three if not enough memory. */
190 | {
191 | unsigned a = 0; /* counter for codes of length k */
192 | unsigned c[BMAX16+1] = {0}; /* bit length count table */
193 | unsigned f = 0; /* i repeats in table every f entries */
194 | int g = 0; /* maximum code length */
195 | int h = 0; /* table level */
196 | REGISTERregister unsigned i = 0; /* counter, current code */
197 | REGISTERregister unsigned j = 0; /* counter */
198 | REGISTERregister int k = 0; /* number of bits in current code */
199 | int l = 0; /* bits per table (returned in m) */
200 | REGISTERregister unsigned *p = NULL((void*)0); /* pointer into c[], b[], or v[] */
201 | REGISTERregister HUFTREE *q = NULL((void*)0); /* points to current table */
202 | HUFTREE r = {0}; /* table entry for struct assignment */
203 | HUFTREE *u[BMAX16] = {NULL((void*)0)}; /* table stack */
204 | unsigned v[N_MAX288] = {0}; /* values in order of bit length */
205 | REGISTERregister int w = 0; /* bits before this table == (l * h) */
206 | unsigned x[BMAX16+1] = {0}; /* bit offsets, then code stack */
207 | unsigned *xp = NULL((void*)0); /* pointer into x */
208 | int y = 0; /* number of dummy codes added */
209 | unsigned z = 0; /* number of entries in current table */
210 |
211 | if(!cshu ||!b || !m )
212 | return CS_E_BAD_HUF_TREE-52;
213 |
214 | /* Generate counts for each bit length .............................*/
215 | p = b;
216 | for(i = 0; i < n; i++)
217 | {
218 | if(*p > BMAX16) /* make sure that all entries <= BMAX .......*/
219 | return CS_E_BAD_HUF_TREE-52;
220 | c[*p++]++;
221 | }
222 |
223 | if (c[0] == n) /* bad input--all zero length codes .*/
224 | return CS_E_BAD_HUF_TREE-52;
225 |
226 | /* Find minimum and maximum length, bound *m by those ..............*/
227 | l = *m;
228 | for (j = 1; j <= BMAX16; j++)
229 | if (c[j]) break;
230 |
231 | k = j; /* minimum code length ...............*/
232 | if ((unsigned)l < j) l = j;
233 |
234 | for (i = BMAX16; i; i--)
235 | if (c[i]) break;
236 |
237 | g = i; /* maximum code length ...............*/
238 | if (( unsigned)l > i) l = i;
239 | *m = l;
240 |
241 | /* Adjust last length count to fill out codes, if needed ...........*/
242 | for (y = 1 << j; j < i; j++, y <<= 1)
243 | if ((y -= c[j]) < 0)
244 | return CS_E_BAD_HUF_TREE-52; /* bad input: more codes than bits ..*/
245 |
246 | if ((y -= c[i]) < 0) return CS_E_BAD_HUF_TREE-52;
247 |
248 | c[i] += y;
249 |
250 | /* Generate starting offsets into the value table for each length ..*/
251 | x[1] = j = 0;
252 | p = c + 1; xp = x + 2;
253 | while (--i) /* note that i == g from above ............*/
254 | {
255 | *xp++ = (j += *p++);
256 | }
257 |
258 | /* Make a table of values in order of bit lengths ..................*/
259 | p = b;
260 | for(i = 0; i < n; i++)
261 | {
262 | if ((j = *p++) != 0) v[x[j]++] = i;
263 | }
264 |
265 | /* Generate the Huffman codes and for each, make the table entries */
266 | x[0] = i = 0; /* first Huffman code is zero */
267 | p = v; /* grab values in bit order */
268 | h = -1; /* no tables yet--level -1 */
269 | w = -l; /* bits decoded == (l * h) */
270 |
271 | /* go through the bit lengths (k already is bits in shortest code) */
272 | for (; k <= g; k++)
273 | {
274 | a = c[k];
275 | while (a--)
276 | {
277 | /* here i is the Huffman code of length k bits for value *p */
278 | /* make tables up to required level */
279 | while (k > w + l)
280 | {
281 | h++;
282 | w += l; /* previous table always l bits */
283 |
284 | /* compute minimum size table less than or equal to l bits */
285 | z = (z = g - w) > (unsigned)l ? (unsigned)l : z; /* upper limit on table size */
286 | j = k - w;
287 | f = 1 << j;
288 | if (f > a + 1) /* try a k-w bit table */
289 | { /* too few codes for k-w bit table */
290 | f -= a + 1; /* deduct codes from patterns left */
291 | xp = c + k;
292 | while (++j < z) /* try smaller tables up to z bits */
293 | {
294 | if ((f <<= 1) <= *++xp)
295 | break; /* enough codes to use up j bits */
296 | f -= *xp; /* else deduct codes from patterns */
297 | }
298 | }
299 | z = 1 << j; /* table entries for j-bit table */
300 |
301 | /* allocate and link in new table ............................*/
302 | q = AllocHufTree (cshu, z+1);
303 |
304 | if (q == (HUFTREE *) 0)
305 | {
306 | return CS_E_NO_STACKMEM-53; /* not enough memory ........*/
307 | }
308 | if(!t)
309 | return CS_E_BAD_HUF_TREE-52;
310 | *t = q + 1;
311 | *(t = &(q->v.t)) = (HUFTREE *)NULL((void*)0);
312 | u[h] = ++q; /* table starts after link ..*/
313 |
314 | /* connect to last table, if there is one ................... */
315 | if (h)
316 | {
317 | x[h] = i; /* save pattern for backing up */
318 | r.b = (unsigned char)l; /* bits to dump before this .. */
319 | r.e = (unsigned char)(16 + j); /* bits in this table .......*/
320 | r.v.t = q; /* pointer to this table ....*/
321 | j = i >> (w - l); /* (get around Turbo C bug) .*/
322 | u[h-1][j] = r; /* connect to last table ....*/
323 | }
324 | }
325 |
326 | /* set up table entry in r .....................................*/
327 | r.b = (unsigned char)(k - w);
328 | if (p >= v + n)
329 | {
330 | r.e = INVALIDCODE99; /* out of values--invalid code */
331 | }
332 | else if (*p < s)
333 | { /* 256 is end-of-block code */
334 | r.e = (unsigned char)(*p < 256 ? LITCODE16 : EOBCODE15);
335 | r.v.n = (unsigned short) *p; /* simple code is just the value*/
336 | p++;
337 | }
338 | else
339 | {
340 | if (!e || !d || (*p - s >=(unsigned)ll) )
341 | return CS_E_BAD_HUF_TREE-52;
342 | r.e = (unsigned char) e[*p - s]; /*non-simple,look up in lists*/
343 | r.v.n = (unsigned short) d[*p - s];
344 | p++;
345 | }
346 |
347 | /* fill code-like entries with r ...............................*/
348 | f = 1 << (k - w);
349 | for (j = i >> w; j < z; j += f)
350 | q[j] = r;
351 |
352 | /* backwards increment the k-bit code i ........................*/
353 | for (j = 1 << (k - 1); i & j; j >>= 1)
354 | i ^= j;
355 | i ^= j;
356 |
357 | /* backup over finished tables .................................*/
358 | while ((i & ((1 << w) - 1)) != x[h])
359 | {
360 | h--; /* don't need to update q ............*/
361 | w -= l;
362 | }
363 | }
364 | }
365 |
366 | /* Return true (1) if we were given an incomplete table ............*/
367 | return y != 0 && n != 1;
368 | }
369 |
370 |
371 | int FlushOut (CSHU *cshu, unsigned w) /* number of bytes to flush */
372 | /*--------------------------------------------------------------------*/
373 | /* Do the equivalent of OUTB for the bytes Slide[0..w-1]. ............*/
374 | /*--------------------------------------------------------------------*/
375 | {
376 | unsigned n;
377 | const unsigned char *p;
378 |
379 | p = cshu->Slide + cshu->SlideOffset;
380 | if (w)
381 | { /* try to fill up buffer .....*/
382 | if (cshu->MemOutoffset + (int)w <= cshu->MemOutsize)
383 | {
384 | memcpy (cshu->OutPtr, p, w);
385 | cshu->OutPtr += w;
386 | cshu->BytesPending = 0;
387 | cshu->MemOutoffset += w;
388 | cshu->SumOut += w;
389 | cshu->SlideOffset = 0;
390 | }
391 | else
392 | {
393 | n = (unsigned) (cshu->MemOutsize - cshu->MemOutoffset);
394 | memcpy (cshu->OutPtr, p, n);
395 | cshu->BytesPending = (int)w - (int)n;
396 | cshu->MemOutoffset += n;
397 | cshu->SumOut += n;
398 | cshu->SlideOffset += n;
399 |
400 | return CS_END_OUTBUFFER2;
401 | }
402 | }
403 | return 0;
404 | }
405 |
406 | int DecompCodes ( CSHU *cshu,
407 | int *state, /* state of last run ...............*/
408 | HUFTREE *tl, /* literal/length decoder tables */
409 | HUFTREE *td, /* distance decoder tables */
410 | int bl, /* number of bits decoded by tl[] */
411 | int bd) /* number of bits decoded by td[] */
412 |
413 | /* inflate (decompress) the codes in a deflated (compressed) block.
414 | Return an error code or zero if it all goes ok. ...................*/
415 | {
416 | REGISTERregister unsigned e; /* table entry flag/number of extra bits */
417 | unsigned n, d; /* length and index for copy */
418 | unsigned w; /* current window position */
419 | unsigned ml, md; /* masks for bl and bd bits */
420 | REGISTERregister int bitcount;
421 | int rc;
422 |
423 | /* make local copies of globals ....................................*/
424 | bitcount = 1;
425 | w = cshu->wp; /* initialize window position */
426 |
427 | /* precompute masks for speed ......................................*/
428 | ml = mask_bits[bl];
429 | md = mask_bits[bd];
430 |
431 | switch (*state) /* depending on state in last run ...............*/
432 | {
433 | case 2:
434 | n = cshu->save_n;
435 | d = cshu->save_d;
436 | e = cshu->save_e;
| Value stored to 'e' is never read
437 | *state = 0;
438 |
439 | goto STATE_2;
440 |
441 | case 20:
442 | *state = 0;
443 | break;
444 |
445 | case 21:
446 | *state = 0;
447 | e = cshu->save_e;
448 | goto STATE_21;
449 |
450 | case 22:
451 | *state = 0;
452 | e = cshu->save_e;
453 | goto STATE_22;
454 |
455 | case 23:
456 | n = cshu->save_n;
457 | e = cshu->save_e;
458 | *state = 0;
459 | goto STATE_23;
460 |
461 | case 24:
462 | e = cshu->save_e;
463 | n = cshu->save_n;
464 | *state = 0;
465 | goto STATE_24;
466 |
467 | case 25:
468 | n = cshu->save_n;
469 | e = cshu->save_e;
470 | *state = 0;
471 | goto STATE_25;
472 |
473 | default: break;
474 | }
475 |
476 | *state = 0;
477 |
478 | for (;;)
479 | {
480 | NEEDBITS((unsigned)bl){ while (cshu->bk < ((unsigned)bl)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short ) cshu->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8 ; } else bitcount = 0; if (!bitcount) break; cshu->bb |= ( cshu->bytebuf) << cshu->bk; cshu->bk += 8; } }
481 | if (bitcount == 0)
482 | {
483 | *state = 20;
484 | cshu->wp = w;
485 |
486 | return CS_END_INBUFFER3;
487 | }
488 |
489 | if ((e = (cshu->htp = tl + ((unsigned)cshu->bb & ml))->e) > LITCODE16)
490 | {
491 | do
492 | {
493 | if (e == INVALIDCODE99) return CS_E_INVALIDCODE-54;
494 |
495 | DUMPBITS(cshu->htp->b){ cshu->bb >>= (cshu->htp->b); cshu->bk -= ( cshu->htp->b); }
496 | e -= LITCODE16;
497 |
498 | STATE_21:
499 | NEEDBITS(e){ while (cshu->bk < (e)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
500 | if (bitcount == 0)
501 | {
502 | cshu->wp = w;
503 | cshu->save_e = e;
504 | *state = 21;
505 |
506 | return CS_END_INBUFFER3;
507 | }
508 | }
509 | while ((e = (cshu->htp = cshu->htp->v.t + ((unsigned)cshu->bb & mask_bits[e]))->e) > LITCODE16);
510 | }
511 |
512 | DUMPBITS(cshu->htp->b){ cshu->bb >>= (cshu->htp->b); cshu->bk -= ( cshu->htp->b); }
513 |
514 | if (e == LITCODE16) /* then it's a literal ...............*/
515 | {
516 | cshu->Slide[w++] = (unsigned char)cshu->htp->v.n;
517 | if (w == WSIZE((unsigned) 0x4000))
518 | {
519 | if ((rc = FlushOut (cshu,w)) != 0)
520 | {
521 | cshu->wp = 0;
522 |
523 | *state = 1;
524 | return rc;
525 | }
526 | w = 0;
527 | }
528 | }
529 | else /* it's an EOB or a length ...........*/
530 | {
531 | /* exit if end of block ........................................*/
532 | if (e == EOBCODE15)
533 | {
534 | break;
535 | }
536 |
537 | /* get length of block to copy .................................*/
538 | STATE_22:
539 | NEEDBITS(e){ while (cshu->bk < (e)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
540 | if (bitcount == 0)
541 | {
542 | cshu->wp = w;
543 | cshu->save_e = e;
544 | *state = 22;
545 | return CS_END_INBUFFER3;
546 | }
547 | n = cshu->htp->v.n + ((unsigned)cshu->bb & mask_bits[e]);
548 | DUMPBITS(e){ cshu->bb >>= (e); cshu->bk -= (e); };
549 |
550 | /* decode distance of block to copy ............................*/
551 | STATE_23:
552 | NEEDBITS((unsigned)bd){ while (cshu->bk < ((unsigned)bd)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short ) cshu->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8 ; } else bitcount = 0; if (!bitcount) break; cshu->bb |= ( cshu->bytebuf) << cshu->bk; cshu->bk += 8; } }
553 | if (bitcount == 0)
554 | {
555 | cshu->wp = w;
556 | cshu->save_n = n;
557 | cshu->save_e = e;
558 | *state = 23;
559 | return CS_END_INBUFFER3;
560 | }
561 |
562 | if ((e = (cshu->htp = td + ((unsigned)cshu->bb & md))->e) > LITCODE16)
563 | {
564 | do
565 | {
566 | if (e == INVALIDCODE99) return CS_E_INVALIDCODE-54;
567 |
568 | DUMPBITS(cshu->htp->b){ cshu->bb >>= (cshu->htp->b); cshu->bk -= ( cshu->htp->b); }
569 | e -= LITCODE16;
570 | STATE_24:
571 | NEEDBITS(e){ while (cshu->bk < (e)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
572 | if (bitcount == 0)
573 | {
574 | cshu->wp = w;
575 | cshu->save_e = e;
576 | cshu->save_n = n;
577 | *state = 24;
578 | return CS_END_INBUFFER3;
579 | }
580 | }
581 | while ((e = (cshu->htp = cshu->htp->v.t + ((unsigned)cshu->bb & mask_bits[e]))->e) > LITCODE16);
582 | }
583 |
584 | DUMPBITS(cshu->htp->b){ cshu->bb >>= (cshu->htp->b); cshu->bk -= ( cshu->htp->b); }
585 |
586 | STATE_25:
587 | NEEDBITS(e){ while (cshu->bk < (e)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
588 | if (bitcount == 0)
589 | {
590 | cshu->wp = w;
591 |
592 | cshu->save_e = e;
593 | cshu->save_n = n;
594 | *state = 25;
595 | return CS_END_INBUFFER3;
596 | }
597 |
598 | d = w - cshu->htp->v.n - ((unsigned)cshu->bb & mask_bits[e]);
599 | DUMPBITS(e){ cshu->bb >>= (e); cshu->bk -= (e); }
600 |
601 | /* do the copy .................................................*/
602 | do
603 | {
604 | n -= (e = (e = WSIZE((unsigned) 0x4000) - ((d &= WSIZE((unsigned) 0x4000)-1) > w ? d : w)) > n ? n : e);
605 |
606 | if (w - d >= e) /* (this test assumes unsigned comparison) */
607 | {
608 | memcpy (cshu->Slide + w, cshu->Slide + d, e);
609 | w += e;
610 | d += e;
611 | }
612 | else /* do it slow to avoid memcpy() overlap ..*/
613 | {
614 | do
615 | {
616 | cshu->Slide[w++] = cshu->Slide[d++];
617 | } while (--e);
618 | }
619 |
620 | if (w == WSIZE((unsigned) 0x4000))
621 | {
622 | if ((rc = FlushOut (cshu,w)) != 0)
623 | {
624 | cshu->wp = 0;
625 |
626 | cshu->save_n = n;
627 | cshu->save_d = d;
628 | cshu->save_e = e;
629 | *state = 2;
630 | return rc;
631 | }
632 | STATE_2:
633 | w = 0;
634 | }
635 | } while (n);
636 | }
637 | }
638 |
639 | cshu->wp = w; /* restore global window pointer .....*/
640 |
641 | return 0;
642 | }
643 |
644 | int
645 | DecompFixed (CSHU *cshu, int *state)
646 | /*--------------------------------------------------------------------*/
647 | /* Decompress an fixed Huffman codes block. */
648 | /*--------------------------------------------------------------------*/
649 | {
650 | int i, rc; /* temporary variable */
651 | unsigned l[288]; /* length list for BuildHufTree */
652 |
653 | if (*state == 0)
654 | {
655 | /* set up literal table, make a complete, but wrong code set .....*/
656 | for (i = 0; i < 144; i++) l[i] = 8;
657 | for (; i < 256; i++) l[i] = 9;
658 | for (; i < 280; i++) l[i] = 7;
659 | for (; i < 288; i++) l[i] = 8;
660 |
661 | cshu->blitlen = 7;
662 | rc = BuildHufTree (cshu, l, 288, 257, cplens, cplext, CPLENS_LEN, &(cshu->tlitlen), &(cshu->blitlen));
663 | if (rc)
664 | {
665 | cshu->AllocStackSize = 0;
666 | return rc;
667 | }
668 |
669 | /* set up distance table .........................................*/
670 | for (i = 0; i < 30; i++) l[i] = 5; /* make an incomplete code set */
671 |
672 | cshu->bdistlen = 5;
673 | if ((rc = BuildHufTree (cshu, l, 30, 0, cpdist, cpdext, CPLENS_LEN, &(cshu->tdistcode), &(cshu->bdistlen))) < 0)
674 | {
675 | cshu->AllocStackSize = 0;
676 | return rc;
677 | }
678 | }
679 |
680 | /* decompress until an end-of-block code ...........................*/
681 | if ((rc = DecompCodes (cshu, state, cshu->tlitlen, cshu->tdistcode, cshu->blitlen, cshu->bdistlen)) != 0)
682 | return rc;
683 |
684 | /* free the decoding tables, return ................................*/
685 | cshu->AllocStackSize = 0;
686 |
687 | return 0;
688 | }
689 |
690 |
691 | int DecompDynamic (CSHU *cshu, int *state)
692 | /*--------------------------------------------------------------------*/
693 | /* Decompress an dynamic Huffman Code block. */
694 | /*--------------------------------------------------------------------*/
695 | {
696 | unsigned j;
697 |
698 | REGISTERregister int bitcount;
699 | int rc;
700 |
701 | bitcount = 1;
702 |
703 | switch (*state)
704 | {
705 | case 0:
706 | case 5:
707 | NEEDBITS(5){ while (cshu->bk < (5)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
708 | if (bitcount == 0)
709 | {
710 | *state = 5;
711 | return CS_END_INBUFFER3;
712 | }
713 | /* number of literal/length codes */
714 | cshu->dd_nl = 257 + ((unsigned)cshu->bb & 0x1f);
715 | DUMPBITS(5){ cshu->bb >>= (5); cshu->bk -= (5); }
716 |
717 | case 6:
718 | NEEDBITS(5){ while (cshu->bk < (5)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
719 | if (bitcount == 0)
720 | {
721 | *state = 6;
722 | return CS_END_INBUFFER3;
723 | }
724 |
725 | cshu->dd_nd = 1 + ((unsigned)cshu->bb & 0x1f); /* number of distance codes ....*/
726 | DUMPBITS(5){ cshu->bb >>= (5); cshu->bk -= (5); }
727 |
728 | case 7:
729 | NEEDBITS(4){ while (cshu->bk < (4)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
730 | if (bitcount == 0)
731 | {
732 | *state = 7;
733 | return CS_END_INBUFFER3;
734 | }
735 |
736 | cshu->dd_nb = 4 + ((unsigned)cshu->bb & 0xf); /* number of bit length codes ..*/
737 | DUMPBITS(4){ cshu->bb >>= (4); cshu->bk -= (4); }
738 |
739 | if (cshu->dd_nl > 286 || cshu->dd_nd > 30)
740 | return CS_E_BADLENGTH-55; /* bad lengths .................*/
741 |
742 | *state = 0;
743 | break;
744 |
745 | case 8:
746 | j = cshu->dd_jj;
747 | *state = 0;
748 | goto STATE_8;
749 |
750 | case 9:
751 | *state = 0;
752 | goto STATE_9;
753 |
754 | case 10:
755 | *state = 0;
756 | goto STATE_10;
757 |
758 | case 11:
759 | *state = 0;
760 | goto STATE_11;
761 |
762 | case 12:
763 | *state = 0;
764 | goto STATE_12;
765 |
766 | default: break;
767 | }
768 |
769 | if (*state == 0)
770 | {
771 | cshu->dd_jj = 0;
772 | for (j = cshu->dd_jj; j < cshu->dd_nb; j++) /* read in bit-length-code lengths ....*/
773 | {
774 | STATE_8:
775 | NEEDBITS(3){ while (cshu->bk < (3)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
776 | if (bitcount == 0)
777 | {
778 | cshu->dd_jj = j;
779 | *state = 8;
780 | return CS_END_INBUFFER3;
781 | }
782 |
783 | cshu->dd_ll[border[j]] = (unsigned)cshu->bb & 7;
784 | DUMPBITS(3){ cshu->bb >>= (3); cshu->bk -= (3); }
785 | }
786 |
787 | for (; j < 19; j++) cshu->dd_ll[border[j]] = 0;
788 |
789 | /* build decoding table for trees--single level, 7 bit lookup ....*/
790 | cshu->dd_bl = 7;
791 | if ((rc = BuildHufTree (cshu, cshu->dd_ll, 19, 19, NULL((void*)0), NULL((void*)0), CPLENS_LEN, &(cshu->dd_tl), &(cshu->dd_bl))) != 0)
792 | {
793 | cshu->AllocStackSize = 0;
794 | return rc; /* incomplete code set ............*/
795 | }
796 |
797 | cshu->dd_nolen = cshu->dd_nl + cshu->dd_nd;
798 | cshu->dd_maskbit = mask_bits[cshu->dd_bl];
799 | cshu->dd_ii = cshu->dd_lastlen = 0;
800 | /* read in literal and distance code lengths .....*/
801 | while ((unsigned)cshu->dd_ii < cshu->dd_nolen)
802 | {
803 | STATE_9:
804 | NEEDBITS((unsigned)cshu->dd_bl){ while (cshu->bk < ((unsigned)cshu->dd_bl)) { if (cshu ->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu->MemInbuffer[(cshu->MemInoffset) ++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu->bytebuf) << cshu->bk; cshu ->bk += 8; } }
805 | if (bitcount == 0)
806 | {
807 | *state = 9;
808 | return CS_END_INBUFFER3;
809 | }
810 |
811 | j = (cshu->dd_td = cshu->dd_tl + ((unsigned)cshu->bb & cshu->dd_maskbit))->b;
812 | DUMPBITS(j){ cshu->bb >>= (j); cshu->bk -= (j); }
813 | j = cshu->dd_td->v.n;
814 | if (j < 16) /* length of code in bits (0..15) ..*/
815 | cshu->dd_ll[cshu->dd_ii++] = cshu->dd_lastlen = j; /* save last length in l ...........*/
816 | else if (j == 16) /* repeat last length 3 to 6 times .*/
817 | {
818 | STATE_10:
819 | NEEDBITS(2){ while (cshu->bk < (2)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
820 | if (bitcount == 0)
821 | {
822 | *state = 10;
823 | return CS_END_INBUFFER3;
824 | }
825 | j = 3 + ((unsigned)cshu->bb & 3);
826 | DUMPBITS(2){ cshu->bb >>= (2); cshu->bk -= (2); }
827 | if ((unsigned)cshu->dd_ii + j > cshu->dd_nolen) return CS_E_INVALIDCODE-54;
828 | while (j--) cshu->dd_ll[cshu->dd_ii++] = cshu->dd_lastlen;
829 | }
830 | else if (j == 17) /* 3 to 10 zero length codes .......*/
831 | {
832 | STATE_11:
833 | NEEDBITS(3){ while (cshu->bk < (3)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
834 | if (bitcount == 0)
835 | {
836 | *state = 11;
837 | return CS_END_INBUFFER3;
838 | }
839 |
840 | j = 3 + ((unsigned)cshu->bb & 7);
841 | DUMPBITS(3){ cshu->bb >>= (3); cshu->bk -= (3); }
842 | if ((unsigned)cshu->dd_ii + j > cshu->dd_nolen) return CS_E_INVALIDCODE-54;
843 | while (j--) cshu->dd_ll[cshu->dd_ii++] = 0;
844 | cshu->dd_lastlen = 0;
845 | }
846 | else /* j == 18: 11 to 138 zero length codes */
847 | {
848 | STATE_12:
849 | NEEDBITS(7){ while (cshu->bk < (7)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
850 | if (bitcount == 0)
851 | {
852 | *state = 12;
853 | return CS_END_INBUFFER3;
854 | }
855 |
856 | j = 11 + ((unsigned)cshu->bb & 0x7f);
857 | DUMPBITS(7){ cshu->bb >>= (7); cshu->bk -= (7); }
858 | if ((unsigned)cshu->dd_ii + j > cshu->dd_nolen) return CS_E_INVALIDCODE-54;
859 | while (j--) cshu->dd_ll[cshu->dd_ii++] = 0;
860 | cshu->dd_lastlen = 0;
861 | }
862 | }
863 |
864 | /* build the decoding tables for literal/length and distance codes*/
865 | cshu->dd_bl = LBITS9;
866 | if ((rc = BuildHufTree (cshu, cshu->dd_ll, cshu->dd_nl, 257, cplens, cplext, CPLENS_LEN, &(cshu->dd_tl), &(cshu->dd_bl))) !=0)
867 | {
868 | cshu->AllocStackSize = 0;
869 | return rc; /* incomplete code set ............*/
870 | }
871 |
872 | cshu->dd_bd = DBITS6;
873 | rc = BuildHufTree (cshu, cshu->dd_ll + cshu->dd_nl, cshu->dd_nd, 0, cpdist, cpdext, CPLENS_LEN, &(cshu->dd_td), &(cshu->dd_bd));
874 | if (rc)
875 | {
876 | cshu->AllocStackSize = 0;
877 | return rc; /* incomplete code set or no stack .............*/
878 | }
879 | }
880 |
881 | /* decompress until an end-of-block code ...........................*/
882 | if ((rc = DecompCodes (cshu, state, cshu->dd_tl, cshu->dd_td, cshu->dd_bl, cshu->dd_bd)) != 0)
883 | return rc;
884 |
885 | /* free the decoding tables, return ................................*/
886 | cshu->AllocStackSize = 0;
887 | return 0;
888 | }
889 |
890 | int
891 | DecompBlock (CSHU *cshu, int *state, int *e) /* state, last block flag */
892 | /*--------------------------------------------------------------------*/
893 | /* Decompress a block of codes */
894 | /*--------------------------------------------------------------------*/
895 | {
896 | REGISTERregister int bitcount; /* bitcount ...........................*/
897 |
898 | bitcount = 1;
899 |
900 | switch (*state)
901 | {
902 | case 0:
903 | case 3:
904 | /* read in last block bit ............................*/
905 | NEEDBITS(1){ while (cshu->bk < (1)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
906 | if (bitcount == 0)
907 | {
908 | *state = 3;
909 | return CS_END_INBUFFER3;
910 | }
911 |
912 | *e = (int)cshu->bb & 1;
913 | DUMPBITS(1){ cshu->bb >>= (1); cshu->bk -= (1); }
914 |
915 | case 4: /* read in block type ................................*/
916 | NEEDBITS(2){ while (cshu->bk < (2)) { if (cshu->MemInoffset < cshu->MemInsize) { cshu->bytebuf = (unsigned short) cshu ->MemInbuffer[(cshu->MemInoffset)++]; bitcount = 8; } else bitcount = 0; if (!bitcount) break; cshu->bb |= (cshu-> bytebuf) << cshu->bk; cshu->bk += 8; } }
917 | if (bitcount == 0)
918 | {
919 | *state = 4;
920 | return CS_END_INBUFFER3;
921 | }
922 |
923 | cshu->blocktype = (unsigned)cshu->bb & 3;
924 | DUMPBITS(2){ cshu->bb >>= (2); cshu->bk -= (2); }
925 |
926 | *state = 0;
927 | break;
928 |
929 | default: break;
930 | }
931 |
932 | switch (cshu->blocktype) /* inflate that block type ............................*/
933 | {
934 | case 2: return DecompDynamic (cshu,state);
935 | case 1: return DecompFixed (cshu,state);
936 | default: return CS_E_UNKNOWN_TYPE-22;
937 | }
938 | }
939 |
940 | int CsDecomprLZH (CSHU * cshu,
941 | SAP_BYTE * inp, /* ptr input .......*/
942 | SAP_INT inlen, /* len of input ....*/
943 | SAP_BYTE * outp, /* ptr output ......*/
944 | SAP_INT outlen, /* len output ......*/
945 | SAP_INT option, /* decompr. option */
946 | SAP_INT * bytes_read, /* bytes read ......*/
947 | SAP_INT * bytes_decompressed) /* bytes decompr. */
948 | /*--------------------------------------------------------------------*/
949 | /* Lempel-Ziv-Huffman */
950 | /*--------------------------------------------------------------------*/
951 | {
952 | int rc;
953 |
954 | cshu->MemOutbuffer = outp;
955 | cshu->MemOutoffset = 0;
956 | cshu->MemOutsize = (unsigned) outlen;
957 |
958 | cshu->OutPtr = outp;
959 |
960 | cshu->MemInbuffer = inp;
961 | cshu->MemInoffset = 0;
962 | cshu->MemInsize = (unsigned) inlen;
963 |
964 | if (inlen == 0 && outlen == 0) return CS_E_BOTH_ZERO-20;
965 |
966 | if (option & CS_INIT_DECOMPRESS0x1)
967 | {
968 | cshu->BytesPending = 0; /* bytes to flush in next run ......*/
969 | cshu->SlideOffset = 0; /* offset in window ................*/
970 | cshu->AllocStackSize = 0; /* stack counter for trees .........*/
971 | cshu->staterun = 0; /* state of uncompress .............*/
972 | cshu->lastblockflag = 0; /* last block flag (1 = last block) */
973 |
974 | if (inlen < CS_HEAD_SIZE8) return CS_E_IN_BUFFER_LEN-11;
975 |
976 | cshu->OrgLen = CsGetLen (inp);
977 | if (cshu->OrgLen < 0)
978 | return CS_E_FILENOTCOMPRESSED-50; /* Input not compressed .......*/
979 |
980 | cshu->SumOut = 0;
981 | cshu->MemInoffset = CS_HEAD_SIZE8;
982 |
983 | /* initialize window, bit buffer .................................*/
984 | cshu->wp = 0;
985 | cshu->bk = 0;
986 | cshu->bb = 0;
987 | cshu->save_e = cshu->save_n = cshu->save_d = 0;
988 | cshu->NonSenseflag = 0;
989 | if (inlen == CS_HEAD_SIZE8) return CS_END_INBUFFER3;
990 | }
991 |
992 | if (cshu->NonSenseflag == 0)
993 | {
994 | NoBits (cshu);
995 | cshu->NonSenseflag = 1;
996 | }
997 |
998 | if (cshu->staterun == 1 || cshu->staterun == 2) /* end of outbuffer in last run ....*/
999 | {
1000 | rc = FlushOut (cshu,cshu->BytesPending);
1001 | if (rc || ((SAP_INT)cshu->SumOut >= cshu->OrgLen))
1002 | {
1003 | *bytes_read = cshu->MemInoffset;
1004 | *bytes_decompressed = cshu->MemOutoffset;
1005 |
1006 | if (rc) return rc;
1007 | *bytes_read = inlen;
1008 | return CS_END_OF_STREAM1;
1009 | }
1010 | }
1011 |
1012 | do /* decompress until the last block ....*/
1013 | {
1014 | rc = DecompBlock (cshu, &(cshu->staterun), &(cshu->lastblockflag));
1015 | if (rc) break;
1016 | } while (!(cshu->lastblockflag));
1017 |
1018 | if ((rc == 0) && (cshu->staterun == 0) && (cshu->lastblockflag))
1019 | {
1020 | rc = FlushOut (cshu, cshu->wp); /* flush out Slide ....................*/
1021 | if (rc) cshu->staterun = 2;
1022 | }
1023 | /* set output params ..................*/
1024 | *bytes_read = cshu->MemInoffset;
1025 | *bytes_decompressed = cshu->MemOutoffset;
1026 |
1027 | if (rc) return rc;
1028 | *bytes_read = inlen;
1029 | return CS_END_OF_STREAM1; /* all done ...........................*/
1030 | }
1031 |
1032 | /* Macros for STACK CHECK in CsDecomp */
1033 | #ifdef CS_STACK_CHECK
1034 |
1035 | /*
1036 | #define STACK_OVERFLOW_CHECK(p) ((p) >= ((BYTE_TYP *)&CsDeInterBuf[csbufsize2 / 4 - 1]))
1037 | */
1038 | #define STACK_OVERFLOW_CHECK(p) ((p) >= (csc->stack_end))
1039 |
1040 | #define STACK_UNDERFLOW_CHECK(p) ((p) < (DE_STACK&csc->Suffixtab[DE_STACK_OFFSET]))
1041 |
1042 | #define OVERFLOW_CHECK \
1043 | if (STACK_OVERFLOW_CHECK(stackp)) \
1044 | { \
1045 | return CS_E_STACK_OVERFLOW-60; \
1046 | }
1047 |
1048 | #define UNDERFLOW_CHECK \
1049 | if (STACK_UNDERFLOW_CHECK(stackp)) \
1050 | { \
1051 | return CS_E_STACK_UNDERFLOW-61; \
1052 | }
1053 |
1054 | #else
1055 |
1056 | #define OVERFLOW_CHECK
1057 | #define UNDERFLOW_CHECK
1058 |
1059 | #endif
1060 |
1061 | CODE_INT DE_STACK_OFFSET = 1<<(CS_BITS13+1);
1062 |
1063 | CODE_INT GetCode (struct CSC *csc)
1064 | /*--------------------------------------------------------------------*/
1065 | /* Read the next code from input stream */
1066 | /*--------------------------------------------------------------------*/
1067 | /* Returns: code on default */
1068 | /* CS_IEND_INBUFFER on end of input buffer */
1069 | /* */
1070 | /*--------------------------------------------------------------------*/
1071 | {
1072 | register CODE_INT code;
1073 |
1074 | register int r_off, bits;
1075 | register BYTE_TYP *bp = csc->buf1;
1076 | /* 2 ** i - 1 (i=0..8) ...............................................*/
1077 | static BYTE_TYP rmask[9] =
1078 | {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1079 | for (;;)
1080 | {
1081 | if (csc->get_r_bits > 0) /* does not fit in last run .....*/
1082 | {
1083 | csc->get_r_bits = (SAP_INT) MIN (csc->get_r_bits, csc->end_inbuf - csc->in_ptr)(((csc->get_r_bits)<(csc->end_inbuf - csc->in_ptr ))?(csc->get_r_bits):(csc->end_inbuf - csc->in_ptr));
1084 | csc->get_r_bits = MAX (0, csc->get_r_bits)(((0)>(csc->get_r_bits))?(0):(csc->get_r_bits));
1085 | BYTES_IN (csc->buf1 + csc->get_size, csc->in_ptr, csc->get_r_bits){ register int i_i = csc->get_r_bits; register BYTE_TYP *bufp = csc->buf1 + csc->get_size; while (i_i-- > 0) *bufp ++ = *csc->in_ptr++; }
1086 | csc->get_size += csc->get_r_bits;
1087 | csc->get_r_bits = 0;
1088 | csc->get_size = (csc->get_size << 3) - (csc->n_bits - 1);
1089 | csc->csc_offset = 0;
1090 | }
1091 | else
1092 | if (csc->clear_flg > 0 || csc->csc_offset >= csc->get_size || csc->free_ent > csc->maxcode)
1093 | {
1094 | /*
1095 | * If the next entry will be too big for the current code
1096 | * get_size, then we must increase the get_size.
1097 | * This implies reading a new buffer full, too.
1098 | */
1099 |
1100 | if (csc->free_ent > csc->maxcode)
1101 | {
1102 | (csc->n_bits)++;
1103 | if (csc->n_bits == csc->maxbits)
1104 | csc->maxcode = csc->maxmaxcode; /* won't get any bigger now .....*/
1105 | else
1106 | csc->maxcode = MAXCODE(csc->n_bits)(((CODE_INT) 1 << (csc->n_bits)) - 1);
1107 | }
1108 |
1109 | if (csc->clear_flg > 0)
1110 | {
1111 | csc->maxcode = MAXCODE (csc->n_bits = INIT_CS_BITS)(((CODE_INT) 1 << (csc->n_bits = 9)) - 1);
1112 | csc->clear_flg = 0;
1113 | }
1114 |
1115 | csc->get_size = (SAP_INT) (csc->end_inbuf - csc->in_ptr);
1116 | if (csc->get_size < csc->n_bits) /* does not fit in buffer .....*/
1117 | {
1118 | if (csc->get_size <= 0)
1119 | {
1120 | code = CS_IEND_INBUFFER-3; /* end of stream ..............*/
1121 | break;
1122 | }
1123 | if (csc->get_r_bits < 0) /* initial !!! ................*/
1124 | csc->get_r_bits = 0;
1125 | else /* end of input buffer ........*/
1126 | {
1127 | BYTES_IN (csc->buf1, csc->in_ptr, csc->get_size){ register int i_i = csc->get_size; register BYTE_TYP *bufp = csc->buf1; while (i_i-- > 0) *bufp++ = *csc->in_ptr ++; }
1128 | csc->get_r_bits = csc->n_bits - csc->get_size;
1129 | csc->csc_offset = 0;
1130 | code = CS_IEND_INBUFFER-3;
1131 | break;
1132 | }
1133 | }
1134 | else /* min (n_bits, get_size) .*/
1135 | csc->get_size = csc->n_bits;
1136 |
1137 | BYTES_IN (csc->buf1, csc->in_ptr, csc->get_size){ register int i_i = csc->get_size; register BYTE_TYP *bufp = csc->buf1; while (i_i-- > 0) *bufp++ = *csc->in_ptr ++; }
1138 | csc->csc_offset = 0;
1139 |
1140 | /* Round get_size down to integral number of codes ...........*/
1141 | csc->get_size = (csc->get_size << 3) - (csc->n_bits - 1);
1142 | }
1143 |
1144 | /* Do all the terrible bit staff ...............................*/
1145 | r_off = csc->csc_offset;
1146 | bits = csc->n_bits;
1147 |
1148 | bp += (r_off >> 3);
1149 | r_off &= 7;
1150 |
1151 | /* Get first part (low order bits) .............................*/
1152 | code = (int)*bp >> r_off;
1153 | bp++;
1154 | r_off = 8 - r_off;
1155 | bits -= r_off;
1156 |
1157 | /* Get any 8 bit parts in the middle (<=1 for up to 16 bits) */
1158 | if (bits >= 8)
1159 | {
1160 | code |= (int) *bp << r_off;
1161 | bp++;
1162 | r_off += 8;
1163 | bits -= 8;
1164 | }
1165 |
1166 | /* high order bits .............................................*/
1167 | code |= ((int)*bp & rmask[bits]) << r_off;
1168 | csc->csc_offset += csc->n_bits;
1169 | break;
1170 | }
1171 |
1172 | return code;
1173 | }
1174 |
1175 | int CsDecomprLZC (CSC * csc,
1176 | SAP_BYTE * inbuf,
1177 | SAP_INT inlen,
1178 | SAP_BYTE * outbuf,
1179 | SAP_INT outlen,
1180 | SAP_INT option,
1181 | SAP_INT * bytes_read,
1182 | SAP_INT * bytes_written)
1183 | {
1184 | /*--------------------------------------------------------------------*/
1185 | /* LZC decompress */
1186 | /* */
1187 | /* Adaptive Dictionary Compression */
1188 | /* Lempel-Zip-Welch-Thomas */
1189 | /* */
1190 | /* Input: */
1191 | /* ----- */
1192 | /* inbuf Pointer to input memory */
1193 | /* inlen Length of input memory */
1194 | /* outbuf Pointer to output area */
1195 | /* outlen Length of output area */
1196 | /* option Compress option: */
1197 | /* CS_INIT_COMPRESS initial */
1198 | /* CS_NORMAL_COMPRESS */
1199 | /* */
1200 | /* Output: */
1201 | /* ------ */
1202 | /* bytes_read Bytes read from input buffer */
1203 | /* bytes_written Bytes decompressed to output buffer */
1204 | /* */
1205 | /* Internal Functions: */
1206 | /* ------------------ */
1207 | /* GetCode Get a code from input buffer */
1208 | /* */
1209 | /* Return Code: */
1210 | /* ----------- */
1211 | /* CS_END_OF_STREAM End of input stream reached */
1212 | /* CS_END_INBUFFER End of input buffer reached */
1213 | /* CS_END_OUTBUFFER End of output buffer reached */
1214 | /* */
1215 | /* CS_E_OUT_BUFFER_LEN Output buffer length to short */
1216 | /* CS_E_IN_BUFFER_LEN Input buffer length to short */
1217 | /* CS_E_MAXBITS_TOO_BIG No internal memory to decompress */
1218 | /* CS_E_INVALID_LEN inlen < 0 or outlen < CS_BITS */
1219 | /* CS_E_FILENOTCOMPRESSED Input is not compressed */
1220 | /* CS_E_IN_EQU_OUT Same addr for input and output buffer */
1221 | /* CS_E_INVALID_ADDR Invalid addr for input or output buffer*/
1222 | /* CS_E_FATAL Internal (should never happen) */
1223 | /* */
1224 | /*--------------------------------------------------------------------*/
1225 | {
1226 | register BYTE_TYP *stackp;
1227 | register CODE_INT code, oldcode = 0, incode, finchar = 0;
1228 | register SAP_INT rest_lenr;
1229 |
1230 | /*
1231 | static BYTE_TYP *sstackp = (BYTE_TYP *) 0;
1232 |
1233 | static long dorg_len;
1234 | static CODE_INT scode, soldcode, sincode, sfinchar;
1235 | static int restart;
1236 | */
1237 | #ifdef SAPonWINDOWS
1238 | Suffixtab = (BYTE_TYP *) CsDeInterBuf;
1239 |
1240 | Prefixtab = (CODE_ENTRY *) CsDeWindowBuf;
1241 | #endif
1242 |
1243 | *bytes_read = 0; /* init output parameters ...*/
1244 | *bytes_written = 0;
1245 |
1246 | /* Check input parameters ..........................................*/
1247 | if (inlen < 0) /* invalid len of inbuf .....*/
1248 | return CS_E_IN_BUFFER_LEN-11;
1249 | /* invalid addr .............*/
1250 | if (inbuf == (BYTE_TYP *) 0 || outbuf == (BYTE_TYP *) 0)
1251 | return CS_E_INVALID_ADDR-15;
1252 |
1253 | if (inbuf == outbuf) /* inbuf == outbuf: invalid !*/
1254 | return CS_E_IN_EQU_OUT-14;
1255 |
1256 | csc->end_inbuf = inbuf + inlen; /* set start & end ptrs .....*/
1257 | csc->end_outbuf = outbuf + outlen;
1258 | csc->outptr = outbuf;
1259 | rest_lenr = (SAP_INT)csc->rest_len; /* push to register .........*/
1260 |
1261 | if (option & CS_INIT_DECOMPRESS0x1) /* only initial .............*/
1262 | {
1263 | csc->sstackp = (BYTE_TYP *) 0;
1264 | csc->restart = 0;
1265 | csc->csc_offset = 0;
1266 | csc->get_size = 0;
1267 | csc->get_r_bits = -1;
1268 | /* csc->stack_end = &(csc->Suffixtab[csbufsize2 - 1]); */
1269 |
1270 | if (inlen < CS_HEAD_SIZE8) /* input buffer too small ......*/
1271 | return CS_E_IN_BUFFER_LEN-11;
1272 |
1273 | csc->dorg_len = CsGetLen (inbuf); /* get sum length */
1274 |
1275 | if (csc->dorg_len < 0) /* and check if file is compr. .*/
1276 | return (CS_E_FILENOTCOMPRESSED-50);
1277 |
1278 | csc->maxbits = inbuf[7]; /* get max. bits ...............*/
1279 | csc->block_compress = csc->maxbits & BLOCK_MASK0x80;
1280 | csc->maxbits &= BIT_MASK0x1f;
1281 | csc->maxmaxcode = (CODE_INT) 1 << csc->maxbits;
1282 | csc->maxcode = MAXCODE(csc->n_bits = INIT_CS_BITS)(((CODE_INT) 1 << (csc->n_bits = 9)) - 1);
1283 |
1284 | if (csc->maxbits > CS_BITS13 + 1) /* not enough memory to decompress */
1285 | return CS_E_MAXBITS_TOO_BIG-51;
1286 |
1287 | /* get version and algorithm .....................................*/
1288 | /* not supported at the moment ...................................*/
1289 |
1290 | for (code = 255; code >= 0; code--) /* init. code table .......*/
1291 | {
1292 | TAB_PREFIXOF(code)csc->Prefixtab[code] = 0;
1293 | TAB_SUFFIXOF(code)csc->Suffixtab[code] = (BYTE_TYP) code;
1294 | }
1295 |
1296 | csc->free_ent = ((csc->block_compress) ? FIRST257 : 256); /* first entry ....*/
1297 |
1298 | csc->in_ptr = inbuf + CS_HEAD_SIZE8; /* skip header .................*/
1299 | csc->rest_len = csc->dorg_len; /* save sum length .............*/
1300 | rest_lenr = (SAP_INT)csc->rest_len;
1301 | stackp = DE_STACK&csc->Suffixtab[DE_STACK_OFFSET]; /* init. stack ptr .............*/
1302 |
1303 | if (outlen == 0) /* End of output buffer ........*/
1304 | {
1305 | code = CS_END_OUTBUFFER2;
1306 | goto ende;
1307 | }
1308 |
1309 | if (csc->in_ptr >= csc->end_inbuf) /* End of input buffer .........*/
1310 | {
1311 | code = CS_END_INBUFFER3;
1312 | goto ende;
1313 | }
1314 | }
1315 | else /* not initial .................*/
1316 | {
1317 | csc->in_ptr = inbuf;
1318 | stackp = csc->sstackp; /* restore states ..............*/
1319 | finchar = csc->sfinchar;
1320 | oldcode = csc->soldcode;
1321 |
1322 | if (outlen <= 0) /* min. size for outbuffer .....*/
1323 | return CS_E_OUT_BUFFER_LEN-10;
1324 |
1325 | if (rest_lenr <= 0) /* end of input ................*/
1326 | {
1327 | code = CS_END_OF_STREAM1;
1328 | goto ende;
1329 | }
1330 |
1331 | if (csc->restart) /* output buffer to small in last run ..............*/
1332 | {
1333 | /* restore machine state .......................................*/
1334 | code = csc->scode;
1335 | incode = csc->sincode;
1336 | csc->restart = 0;
1337 | goto contin;
1338 | }
1339 | }
1340 |
1341 | if (csc->get_r_bits == -1) /* init. decoding ..............*/
1342 | {
1343 | finchar = oldcode = (CODE_INT) GetCode (csc);
1344 | csc->get_r_bits = 0; /* not redundant !!! ...........*/
1345 |
1346 | if (outlen == 0) /* must have some space ........*/
1347 | {
1348 | code = CS_END_OUTBUFFER2;
1349 | goto ende;
1350 | }
1351 |
1352 | *csc->outptr++ = (BYTE_TYP) finchar;
1353 | if (--rest_lenr <= 0) /* End of stream ...............*/
1354 | {
1355 | code = CS_END_OF_STREAM1;
1356 | goto ende;
1357 | }
1358 | }
1359 |
1360 | for (;;) /* until not end of inbuf ......*/
1361 | {
1362 | code = GetCode (csc);
1363 | if (code < 0) break;
1364 |
1365 | if ((code == CLEAR256) && csc->block_compress)
1366 | {
1367 | /* clear code table ............................................*/
1368 | memset (csc->Prefixtab, '\0', sizeof (CODE_ENTRY) << 8);
1369 | csc->clear_flg = 1;
1370 | csc->free_ent = FIRST257 - 1;
1371 |
1372 | if ((code = GetCode (csc)) < 0)
1373 | break;
1374 | }
1375 |
1376 | incode = code;
1377 |
1378 | /* Special case for ababa string .................................*/
1379 | if (code >= csc->free_ent)
1380 | {
1381 | *stackp++ = (BYTE_TYP) finchar;
1382 | OVERFLOW_CHECK
1383 | code = oldcode;
1384 | }
1385 |
1386 | /* Generate output characters in reverse order ...................*/
1387 | while (code >= 256)
1388 | {
1389 | /* Check for end of stack */
1390 | if (stackp >= (DE_STACK&csc->Suffixtab[DE_STACK_OFFSET] + DE_STACK_OFFSET)){
1391 | return (CS_E_STACK_OVERFLOW-60);
1392 | }
1393 | *stackp++ = TAB_SUFFIXOF(code)csc->Suffixtab[code];
1394 | OVERFLOW_CHECK
1395 | code = TAB_PREFIXOF(code)csc->Prefixtab[code];
1396 | }
1397 |
1398 | finchar = TAB_SUFFIXOF(code)csc->Suffixtab[code];
1399 | *stackp++ = (BYTE_TYP) finchar;
1400 | OVERFLOW_CHECK
1401 |
1402 | contin:
1403 | /* and put them out in forward order .............................*/
1404 | for (;;)
1405 | {
1406 | if (csc->outptr >= csc->end_outbuf) /* End of outbuffer ...........*/
1407 | {
1408 | csc->scode = code;
1409 | csc->sincode = incode;
1410 | csc->restart = 1;
1411 | code = CS_END_OUTBUFFER2;
1412 | goto ende;
1413 | }
1414 |
1415 | *csc->outptr++ = *--stackp;
1416 |
1417 | if (--rest_lenr <= 0) /* End of Stream ..............*/
1418 | {
1419 | code = CS_END_OF_STREAM1;
1420 | goto ende;
1421 | }
1422 |
1423 | if (stackp == DE_STACK&csc->Suffixtab[DE_STACK_OFFSET]) break; /* End of Stack ...............*/
1424 | } /* end for (;;) ...............................................*/
1425 |
1426 | /* Generate the new entry ........................................*/
1427 | if ((code = csc->free_ent) < csc->maxmaxcode)
1428 | {
1429 | TAB_PREFIXOF(code)csc->Prefixtab[code] = (CODE_ENTRY)oldcode;
1430 | TAB_SUFFIXOF(code)csc->Suffixtab[code] = (BYTE_TYP) finchar;
1431 | csc->free_ent = code + 1;
1432 | }
1433 |
1434 | /* Remember previous code ........................................*/
1435 | oldcode = incode;
1436 |
1437 | } /* end for (;;) .................................................*/
1438 |
1439 | ende:
1440 | csc->sstackp = stackp; /* save state of the compressor ...*/
1441 | csc->soldcode = oldcode;
1442 | csc->sfinchar = finchar;
1443 | csc->rest_len = rest_lenr;
1444 | /* set output parameters ..........*/
1445 | *bytes_written = (SAP_INT) (csc->outptr - outbuf);
1446 | *bytes_read = (SAP_INT) (csc->in_ptr - inbuf);
1447 |
1448 | if (code == CS_IEND_INBUFFER-3) return CS_END_INBUFFER3;
1449 | else
1450 | return code;
1451 | }
1452 | }
1453 |
1454 | int CsDecompr (CSHDL * hdl, /* handle */
1455 | SAP_BYTE * inbuf, /* ptr input .......*/
1456 | SAP_INT inlen, /* len of input ....*/
1457 | SAP_BYTE * outbuf, /* ptr output ......*/
1458 | SAP_INT outlen, /* len output ......*/
1459 | SAP_INT option, /* decompr. option */
1460 | SAP_INT * bytes_read, /* bytes read ......*/
1461 | SAP_INT * bytes_decompressed) /* bytes decompr. */
1462 | /*--------------------------------------------------------------------*/
1463 | /* Decompress */
1464 | /* */
1465 | /* Adaptive Dictionary Compression */
1466 | /* Lempel-Zip */
1467 | /* */
1468 | /* Input: */
1469 | /* ----- */
1470 | /* inbuf Pointer to input memory */
1471 | /* inlen Length of input memory */
1472 | /* outbuf Pointer to output area */
1473 | /* outlen Length of output area */
1474 | /* option DeCompress option: */
1475 | /* CS_INIT_DECOMPRESS initial */
1476 | /* CS_NORMAL_COMPRESS */
1477 | /* */
1478 | /* Output: */
1479 | /* ------ */
1480 | /* bytes_read Bytes read from input buffer */
1481 | /* bytes_written Bytes decompressed to output buffer */
1482 | /* */
1483 | /* Return Code: */
1484 | /* ----------- */
1485 | /* CS_END_OF_STREAM End of input stream reached */
1486 | /* CS_END_INBUFFER End of input buffer reached */
1487 | /* CS_END_OUTBUFFER End of output buffer reached */
1488 | /* */
1489 | /* CS_E_OUT_BUFFER_LEN Output buffer length to short */
1490 | /* CS_E_IN_BUFFER_LEN Input buffer length to short */
1491 | /* CS_E_MAXBITS_TOO_BIG No internal memory to decompress */
1492 | /* CS_E_INVALID_LEN inlen < 0 or outlen < CS_BITS */
1493 | /* CS_E_FILENOTCOMPRESSED Input is not compressed */
1494 | /* CS_E_IN_EQU_OUT Same addr for input and output buffer */
1495 | /* CS_E_INVALID_ADDR Invalid addr for input or output buffer*/
1496 | /* CS_E_FATAL Internal (should never happen) */
1497 | /* */
1498 | /*--------------------------------------------------------------------*/
1499 | {
1500 |
1501 | if (option & CS_INIT_DECOMPRESS0x1)
1502 | {
1503 | if (inlen < CS_HEAD_SIZE8) return CS_E_IN_BUFFER_LEN-11;
1504 | }
1505 |
1506 | switch (CsGetAlgorithm (inbuf))
1507 | {
1508 | case CS_ALGORITHM_LZC(SAP_BYTE) 1:
1509 | return CsDecomprLZC (&hdl->handle.csc, inbuf, inlen, outbuf, outlen,
1510 | option, bytes_read, bytes_decompressed);
1511 | case CS_ALGORITHM_LZH(SAP_BYTE) 2:
1512 | return CsDecomprLZH (&hdl->handle.cshu, inbuf, inlen, outbuf, outlen,
1513 | option, bytes_read, bytes_decompressed);
1514 |
1515 | default: return CS_E_UNKNOWN_ALG-21;
1516 | }
1517 | }