Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

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