Wireshark 4.7.3
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
csdecompr.h
1/*@(#)cslzh.h 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/*--------------------------------------------------------------------*/
45/* */
46/* Definitions for LZH Algorithm */
47/* Lempel-Ziv-Huffman */
48/*--------------------------------------------------------------------*/
49
50#ifndef CSDECOMPR_H /* cannot be included twice .................*/
51#define CSDECOMPR_H
52
53#include "hpa101saptype.h"
54
55
56/*--------------------------------------------------------------------*/
57/* Flags for CsCompr and CsDecompr */
58/*--------------------------------------------------------------------*/
59#define CS_LZH_VERSION 1
60
61#define CS_LZH1 (1 << 4)
62#define CS_LZH2 (2 << 4)
63#define CS_LZH3 (3 << 4)
64#define CS_LZH4 (4 << 4)
65#define CS_LZH5 (5 << 4)
66#define CS_LZH6 (6 << 4)
67#define CS_LZH7 (7 << 4)
68#define CS_LZH8 (8 << 4)
69#define CS_LZH9 (9 << 4)
70
71
72/* call flags for compression ........................................*/
73#define CS_NORMAL_COMPRESS 0x0 /* normal .....................*/
74#define CS_INIT_COMPRESS 0x1 /* first call CsCompr..........*/
75#define CS_INIT_DECOMPRESS 0x1 /* first call CsDeCompr........*/
76
77#define CS_LZC 0x0 /* use lzc ....................*/
78#define CS_LZH 0x2 /* use lzh ....................*/
79#define CS_GRAPHIC_DATA 0x8 /* not supported now ..........*/
80
81/* Header info in compressed file ....................................*/
82#define CS_ALGORITHM_LZC (SAP_BYTE) 1
83#define CS_ALGORITHM_LZH (SAP_BYTE) 2
84
85#define CS_HEAD_SIZE 8 /* size of common header ......*/
86
87/*--------------------------------------------------------------------*/
88/* Error & Return Codes for CsCompr and CsDecompr */
89/*--------------------------------------------------------------------*/
90#define CS_END_INBUFFER 3 /* End of input buffer ........*/
91#define CS_END_OUTBUFFER 2 /* End of output buffer .......*/
92#define CS_END_OF_STREAM 1 /* End of data ................*/
93#define CS_OK 0
94
95#define CS_IEND_OF_STREAM -1 /* End of data (internal) .....*/
96#define CS_IEND_OUTBUFFER -2 /* End of output buffer .......*/
97#define CS_IEND_INBUFFER -3 /* End of input buffer ........*/
98
99#define CS_ERROR -10 /* First Error Code ...........*/
100#define CS_E_OUT_BUFFER_LEN -10 /* Invalid output length ......*/
101#define CS_E_IN_BUFFER_LEN -11 /* Invalid input length .......*/
102#define CS_E_NOSAVINGS -12
103#define CS_E_INVALID_SUMLEN -13 /* Invalid len of stream ......*/
104#define CS_E_IN_EQU_OUT -14 /* inbuf == outbuf ............*/
105#define CS_E_INVALID_ADDR -15 /* inbuf == NULL,outbuf == NULL*/
106#define CS_E_FATAL -19 /* Internal Error ! ...........*/
107#define CS_E_BOTH_ZERO -20 /* inlen = outlen = 0 .........*/
108#define CS_E_UNKNOWN_ALG -21 /* unknown algorithm ..........*/
109#define CS_E_UNKNOWN_TYPE -22
110
111/* for decompress */
112#define CS_E_FILENOTCOMPRESSED -50 /* Input not compressed .......*/
113#define CS_E_MAXBITS_TOO_BIG -51 /* maxbits to large ...........*/
114#define CS_E_BAD_HUF_TREE -52 /* bad hufman tree ..........*/
115#define CS_E_NO_STACKMEM -53 /* no stack memory in decomp ..*/
116#define CS_E_INVALIDCODE -54 /* invalid code ...............*/
117#define CS_E_BADLENGTH -55 /* bad lengths ................*/
118
119#define CS_E_STACK_OVERFLOW -60 /* stack overflow in decomp */
120#define CS_E_STACK_UNDERFLOW -61 /* stack underflow in decomp */
121
122/* only Windows */
123#define CS_NOT_INITIALIZED -71 /* storage not allocated ......*/
124
125/* definition of byte arrays
126 * corresponding to their primitive
127 * data type
128 */
129typedef unsigned char BYTEARRAY_2[2]; /* unsigned short */
130typedef unsigned char BYTEARRAY_4[4]; /* unsigned integer */
131typedef unsigned char BYTEARRAY_8[8]; /* unsigned long long */
132
133
134#define REGISTER register
135/* The minimum and maximum match lengths .............................*/
136#define MIN_MATCH 3
137#define MAX_MATCH 258
138
139/* Minimum amount of lookahead, except at the end of the input file ..*/
140#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
141
142/* In order to simplify the code, particularly on 16 bit machines, match
143 * distances are limited to MAX_DIST instead of WSIZE. ...............*/
144#define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
145
146#define NONSENSE_LENBITS 2
147
148#define EODATA (-1)
149
150/* Maximum window size = 32K (must be a power of 2) ..................*/
151#define WSIZE ((unsigned) 0x4000) /* 16K */
152
153#define CS_HASH_BITS 14
154
155#define CS_HASH_SIZE (unsigned)(1 << CS_HASH_BITS)
156
157#define CS_LIT_BUFSIZE (unsigned) 0x4000 /* 16K */
158#define CS_DIST_BUFSIZE CS_LIT_BUFSIZE
159
160#define HASH_MASK (CS_HASH_SIZE-1)
161#define WMASK (WSIZE-1)
162
163/* Configuration parameters ..........................................*/
164/* speed options for the general purpose bit flag */
165#define FAST_PA107 4
166#define SLOW_PA107 0
167
168/* Matches of length 3 are discarded if their distance exceeds TOO_FAR*/
169#ifndef TOO_FAR
170#define TOO_FAR 4096
171#endif
172
173/* Types centralized here for easy modification ......................*/
174typedef SAP_BYTE uch; /* unsigned 8-bit value ................*/
175typedef SAP_USHORT ush; /* unsigned 16-bit value ................*/
176typedef SAP_UINT ulg; /* unsigned 32-bit value ................*/
177
178/*
179 * SAP AG Walldorf
180 * Systeme, Anwendungen und Produkte in der Datenverarbeitung
181 *
182 * (C) Copyright (c) 1992-2005 SAP AG - 1994
183 */
184
185/*--------------------------------------------------------------------*/
186/* */
187/* Definitions for LZC Algorithm */
188/* */
189/*--------------------------------------------------------------------*/
190
191/*--------------------------------------------------------------------*/
192/* Macros (internal) */
193/*--------------------------------------------------------------------*/
194 /* for 16 Bit OS ................*/
195#if defined ( OS_16 )
196#define LARGE_ARRAY far
197#else
198#define LARGE_ARRAY
199#endif
200
201#ifndef min
202#define min(a,b) ((a) < (b) ? (a) : (b))
203#endif
204
205#ifndef max
206#define max(a,b) ((a) > (b) ? (a) : (b))
207#endif
208
209#ifndef MIN
210#define MIN(a,b) ((a) < (b) ? (a) : (b))
211#endif
212
213#ifndef MAX
214#define MAX(a,b) ((a) > (b) ? (a) : (b))
215#endif
216
217#ifndef CS_BITS
218#define CS_BITS 13 /* max. number of bits (default) ......*/
219#endif
220
221/* If a new version is added, change CS_VERSION
222 */
223
224#define CS_VERSION (BYTE_TYP) 1
225
226#define CS_ALGORITHM (BYTE_TYP) 1 /* never change this ..........*/
227
228
229#define FIRST 257 /* first free entry ...................*/
230#define CLEAR 256 /* table clear output code ............*/
231 /* ratio check interval ...............*/
232#define CHECK_GAP 4096 /* 10000 */
233
234/* Defines for 8. byte of header .....................................*/
235#define BIT_MASK 0x1f
236#define BLOCK_MASK 0x80
237
238/* Masks 0x40 and 0x20 are free. */
239
240#define INIT_CS_BITS 9 /* initial number of bits/code ........*/
241#define MAX_CS_BITS 16 /* max. number of bits/code ...........*/
242
243#if CS_BITS <= INIT_CS_BITS /* CS_BITS at least INIT_CS_BITS + 1 ..*/
244#undef CS_BITS
245#define CS_BITS (INIT_CS_BITS + 1)
246#endif
247
248#if CS_BITS > MAX_CS_BITS /* CS_BITS at most MAX_CS_BITS ........*/
249#undef CS_BITS
250#define CS_BITS MAX_CS_BITS
251#endif
252
253/*--------------------------------------------------------------------*/
254/* TYPES (depending on CS_BITS) */
255/*--------------------------------------------------------------------*/
256
257typedef SAP_BYTE BYTE_TYP;
258
259#if CS_BITS > 15
260typedef long int CODE_INT;
261#else
262typedef int CODE_INT;
263#endif
264
265typedef long int COUNT_INT;
266typedef unsigned short CODE_ENTRY;
267
268/*--------------------------------------------------------------------*/
269/* Size of the hash table depending on CS_BITS .......................*/
270/*--------------------------------------------------------------------*/
271
272#if CS_BITS == 16
273#define HSIZE 69001U /* 95% occupancy 65536/69001 .........*/
274#define CSIZE 69001U
275#endif
276#if CS_BITS == 15
277#define HSIZE 35023U /* 94% occupancy 32768/35023 .........*/
278#define CSIZE 69001U
279#endif
280#if CS_BITS == 14
281#define HSIZE 18013U /* 91% occupancy 16384/18013 .........*/
282#define CSIZE 35023U
283#endif
284#if CS_BITS == 13
285#define HSIZE 9001U /* 91% occupancy 8192/9001 .........*/
286#define CSIZE 18013U
287#endif
288#if CS_BITS <= 12
289#define HSIZE 5003U /* 80% occupancy 4096/5003 .........*/
290#define CSIZE 9001U
291#endif
292
293/*--------------------------------------------------------------------*/
294/* Access Macros for code and hash tables ............................*/
295/*--------------------------------------------------------------------*/
296
297#define MAXCODE(n_bits) (((CODE_INT) 1 << (n_bits)) - 1)
298#define HTABOF(i) csc->htab[i]
299#define CODETABOF(i) csc->codetab[i]
300
301/*
302 * To save much memory, we overlay the table used by CsCompr () with
303 * those used by CsDecompr (). The TAB_PREFIX table is the same size
304 * and type as the codetab. The TAB_SUFFIX table needs 2^BITS
305 * characters. We get this from the beginning of htab.
306 * The output stack uses the rest of htab, and contains characters.
307 * There is plenty of room for any possible stack (stack used to
308 * be 8000 characters).
309 */
310
311#define TAB_PREFIXOF(i) csc->Prefixtab[i]
312#define TAB_SUFFIXOF(i) csc->Suffixtab[i]
313
314/* following definition gives a compiler warning on HP 64 bit (2001-05-15)
315 Maybe this will work again some time later.
316
317#define DE_STACK &TAB_SUFFIXOF((CODE_INT)1<<(CS_BITS+1))
318
319 Definition beneath works on HP, too, but requires an extra global var.
320*/
321
322extern CODE_INT DE_STACK_OFFSET;
323#define DE_STACK &TAB_SUFFIXOF(DE_STACK_OFFSET)
324
325/* Clear Hash Table ..................................................*/
326#define CL_HASH(size) \
327 memset (csc->htab, 0xff, (size) * sizeof (COUNT_INT));
328
329
330#ifdef USE_MEMCPY /* use memcpy to transfer bytes ..........*/
331
332#define BYTES_OUT(dst,src,len) \
333 memcpy (dst,src,len); dst += len;
334
335#define BYTES_IN(to,from,len) \
336 memcpy (to,from,len); from += len;
337
338#else /* native copy ...........................*/
339
340#define BYTES_OUT(dst,src,len) \
341 { \
342 register int i_i = len; \
343 register BYTE_TYP *bufp = src; \
344 while (i_i-- > 0) *dst++ = *bufp++; \
345 }
346
347#define BYTES_IN(to,from,len) \
348 { \
349 register int i_i = len; \
350 register BYTE_TYP *bufp = to; \
351 while (i_i-- > 0) *bufp++ = *from++; \
352 }
353
354#endif /* USE_MEMCPY */
355
356#define EOBCODE 15
357#define LITCODE 16
358#define INVALIDCODE 99
359/* number of length codes, not counting the special END_BLOCK code */
360#define LENGTH_CODES 29
361#define D_CODES 30 /* number of distance codes */
362
363#define NONSENSE_LENBITS 2
364#define LBITS 9
365#define DBITS 6
366
367/* If BMAX needs to be larger than 16, then h and x[] should be ULONG */
368#define BMAX 16 /* maximum bit length of any code (16 for explode) */
369#define N_MAX 288 /* maximum number of codes in any set .............*/
370
371
372#define DUMPBITS(n) { cshu->bb >>= (n); cshu->bk -= (n); }
373
374#define NEEDBITS(n) \
375{ \
376 while (cshu->bk < (n)) \
377 { \
378 if (cshu->MemInoffset < cshu->MemInsize) \
379 { \
380 cshu->bytebuf = (unsigned short) cshu->MemInbuffer[(cshu->MemInoffset)++]; \
381 bitcount = 8; \
382 } \
383 else bitcount = 0; \
384 if (!bitcount) break; \
385 cshu->bb |= (cshu->bytebuf) << cshu->bk; \
386 cshu->bk += 8; \
387 } \
388}
389
390struct HUFT
391{
392 unsigned char e; /* number of extra bits or operation .........*/
393 unsigned char b; /* number of bits in this code or subcode ....*/
394 union
395 {
396 unsigned short n; /* literal, length base, or distance base ....*/
397 struct HUFT *t; /* pointer to next level of table ............*/
398 } v;
399};
400
401typedef struct HUFT HUFTREE;
402#define DE_STACK_SIZE 0x1000
403
404#define BUF_SIZE1 (sizeof(COUNT_INT) * HSIZE > 2*WSIZE ? \
405 sizeof(COUNT_INT) * HSIZE : 2*WSIZE)
406
407#ifdef SAPonWINDOWS
408#define DUMMY_SIZE 32768U
409#else
410
411#define DUMMY_SIZE (sizeof(HUFTREE) > 8 ? \
412 sizeof(HUFTREE) * 0x1E00 : 0xF000)
413#endif
414
415#define BUF_SIZE2 (sizeof(CODE_ENTRY) * CSIZE > DUMMY_SIZE ? \
416 sizeof(CODE_ENTRY) * CSIZE : DUMMY_SIZE)
417
418
419typedef struct CSHU
420{
421 SAP_BYTE *OutPtr;
422 SAP_UINT SumOut;
423
424 SAP_BYTE *MemOutbuffer;
425 SAP_BYTE *MemInbuffer;
426 unsigned MemOutoffset;
427 unsigned MemOutsize;
428 unsigned MemInoffset;
429 unsigned MemInsize;
430 unsigned BytesPending, SlideOffset;
431 unsigned wp;
432
433 SAP_UINT bb; /* bit buffer ............*/
434 unsigned bk; /* bits in bit buffer ....*/
435
436 SAP_UINT bytebuf;
437 unsigned AllocStackSize;
438
439 HUFTREE *htp; /* pointer to table entry */
440 unsigned save_n, save_d, save_e;
441
442 int lastblockflag; /* last block flag ....................*/
443 int staterun; /* state of last run ..................*/
444 int NonSenseflag;
445 SAP_INT OrgLen;
446 unsigned blocktype; /* block type */
447
448 HUFTREE *tlitlen; /* literal/length code table */
449 HUFTREE *tdistcode; /* distance code table */
450 int blitlen; /* lookup bits for tl */
451 int bdistlen; /* lookup bits for td */
452
453 int dd_ii;
454 unsigned dd_jj;
455 unsigned dd_lastlen; /* last length */
456 unsigned dd_maskbit; /* mask for bit lengths table */
457 unsigned dd_nolen; /* number of lengths to get */
458 HUFTREE *dd_tl; /* literal/length code table */
459 HUFTREE *dd_td; /* distance code table */
460 int dd_bl; /* lookup bits for tl */
461 int dd_bd; /* lookup bits for td */
462 unsigned dd_nb; /* number of bit length codes */
463 unsigned dd_nl; /* number of literal/length codes */
464 unsigned dd_nd; /* number of distance codes */
465 unsigned dd_ll[286+30]; /* literal/length and distance lengths */
466 SAP_BYTE Slide[(BUF_SIZE1)];
467 HUFTREE InterBuf[DE_STACK_SIZE];
468} CSHU;
469
470typedef struct CSC
471{
472 int n_bits; /* number of bits/code ............*/
473 int maxbits; /* user settable max # bits/code ..*/
474 CODE_INT maxcode; /* maximum code, given n_bits .....*/
475
476 /* storage for GETCODE / PUTCODE ..*/
477 BYTE_TYP buf1[MAX_CS_BITS];
478
479 int cs_offset;
480 int csc_offset;
481 int put_n_bytes;
482
483 BYTE_TYP *outptr;
484 BYTE_TYP *end_outbuf;
485
486 /* should never be generated ......*/
487 CODE_INT maxmaxcode; /* = (CODE_INT)1 << CS_BITS; */
488
489 COUNT_INT LARGE_ARRAY htab [HSIZE]; /* hash table ........*/
490 CODE_ENTRY LARGE_ARRAY codetab [CSIZE]; /* code table ........*/
491 CODE_ENTRY Prefixtab[(BUF_SIZE1)/4]; /* = (CODE_ENTRY *) &CsDeWindowBuf[0]; */
492 BYTE_TYP Suffixtab[(BUF_SIZE2)/4]; /* = (BYTE_TYP *) &CsDeInterBuf[0]; */
493 CODE_INT hsize; /* = HSIZE; */ /* for dynamic table sizing .......*/
494
495
496 CODE_INT free_ent; /* first unused entry ..........*/
497 long int bytes_out; /* length of compressed output .*/
498 long int rest_len; /* rest bytes to decompress ....*/
499 int block_compress; /* block compression ...........*/
500 int clear_flg; /* clear hash table ............*/
501 long int ratio; /* compression ratio ...........*/
502 COUNT_INT checkpoint; /* ratio check point for compr. */
503
504/* states for get_code ...............................................*/
505 int get_size, get_r_bits;
506
507 BYTE_TYP * in_ptr; /* global input ptr ............*/
508 BYTE_TYP * end_inbuf; /* end of input buffer .........*/
509 BYTE_TYP *stack_end;
510
511 int hshift;
512 CODE_INT ent;
513 int sflush;
514 long org_len;
515 long in_count_sum;
516
517
518 BYTE_TYP *sstackp; /* = (BYTE_TYP *) 0; */
519 long dorg_len;
520 CODE_INT scode, soldcode, sincode, sfinchar;
521 int restart;
522
523} CSC;
524
525typedef struct CSHDL
526{
527 union {
528 CSC csc;
529 CSHU cshu;
530 } handle;
531
532} CSHDL;
533
534int CsDecompr (CSHDL * hdl, /* handle */
535 SAP_BYTE * inbuf, /* ptr input .......*/
536 SAP_INT inlen, /* len of input ....*/
537 SAP_BYTE * outbuf, /* ptr output ......*/
538 SAP_INT outlen, /* len output ......*/
539 SAP_INT option, /* decompr. option */
540 SAP_INT * bytes_read, /* bytes read ......*/
541 SAP_INT * bytes_decompressed); /* bytes decompr. */
542
543int CsDecomprLZC (CSC * csc,
544 SAP_BYTE * inbuf,
545 SAP_INT inlen,
546 SAP_BYTE * outbuf,
547 SAP_INT outlen,
548 SAP_INT option,
549 SAP_INT * bytes_read,
550 SAP_INT * bytes_written);
551
552int CsDecomprLZH (CSHU * cshu,
553 SAP_BYTE * inp,
554 SAP_INT inlen,
555 SAP_BYTE * outp,
556 SAP_INT outlen,
557 SAP_INT option,
558 SAP_INT * bytes_read,
559 SAP_INT * bytes_decompressed);
560
561#endif /* CSDECOMPR_H */
Definition csdecompr.h:471
Definition csdecompr.h:526
Definition csdecompr.h:420
Definition csdecompr.h:391