1 /*****************************************************************************/
2 /* pklib.h                                Copyright (c) Ladislav Zezula 2003 */
3 /*---------------------------------------------------------------------------*/
4 /* Header file for PKWARE Data Compression Library                           */
5 /*---------------------------------------------------------------------------*/
6 /*   Date    Ver   Who  Comment                                              */
7 /* --------  ----  ---  -------                                              */
8 /* 31.03.03  1.00  Lad  The first version of pkware.h                        */
9 /*****************************************************************************/
10 
11 #ifndef __PKLIB_H__
12 #define __PKLIB_H__
13 
14 //-----------------------------------------------------------------------------
15 // Defines
16 
17 #define CMP_BINARY             0            // Binary compression
18 #define CMP_ASCII              1            // Ascii compression
19 
20 #define CMP_NO_ERROR           0
21 #define CMP_INVALID_DICTSIZE   1
22 #define CMP_INVALID_MODE       2
23 #define CMP_BAD_DATA           3
24 #define CMP_ABORT              4
25 
26 #define CMP_IMPLODE_DICT_SIZE1   1024       // Dictionary size of 1024
27 #define CMP_IMPLODE_DICT_SIZE2   2048       // Dictionary size of 2048
28 #define CMP_IMPLODE_DICT_SIZE3   4096       // Dictionary size of 4096
29 
30 //-----------------------------------------------------------------------------
31 // Define calling convention
32 
33 #ifndef PKEXPORT
34 #ifdef WIN32
35 #define PKEXPORT  __cdecl                   // Use for normal __cdecl calling
36 #else
37 #define PKEXPORT
38 #endif
39 #endif
40 
41 //-----------------------------------------------------------------------------
42 // Internal structures
43 
44 // Compression structure
45 typedef struct
46 {
47     unsigned int   distance;                // 0000: Backward distance of the currently found repetition, decreased by 1
48     unsigned int   out_bytes;               // 0004: # bytes available in out_buff
49     unsigned int   out_bits;                // 0008: # of bits available in the last out byte
50     unsigned int   dsize_bits;              // 000C: Number of bits needed for dictionary size. 4 = 0x400, 5 = 0x800, 6 = 0x1000
51     unsigned int   dsize_mask;              // 0010: Bit mask for dictionary. 0x0F = 0x400, 0x1F = 0x800, 0x3F = 0x1000
52     unsigned int   ctype;                   // 0014: Compression type (CMP_ASCII or CMP_BINARY)
53     unsigned int   dsize_bytes;             // 0018: Dictionary size in bytes
54     unsigned char  dist_bits[0x40];         // 001C: Distance bits
55     unsigned char  dist_codes[0x40];        // 005C: Distance codes
56     unsigned char  nChBits[0x306];          // 009C: Table of literal bit lengths to be put to the output stream
57     unsigned short nChCodes[0x306];         // 03A2: Table of literal codes to be put to the output stream
58     unsigned short offs09AE;                // 09AE:
59 
60     void         * param;                   // 09B0: User parameter
61     unsigned int (*read_buf)(char *buf, unsigned int *size, void *param);  // 9B4
62     void         (*write_buf)(char *buf, unsigned int *size, void *param); // 9B8
63 
64     unsigned short offs09BC[0x204];         // 09BC:
65     unsigned long  offs0DC4;                // 0DC4:
66     unsigned short phash_to_index[0x900];   // 0DC8: Array of indexes (one for each PAIR_HASH) to the "pair_hash_offsets" table
67     unsigned short phash_to_index_end;      // 1FC8: End marker for "phash_to_index" table
68     char           out_buff[0x802];         // 1FCA: Compressed data
69     unsigned char  work_buff[0x2204];       // 27CC: Work buffer
70                                             //  + DICT_OFFSET  => Dictionary
71                                             //  + UNCMP_OFFSET => Uncompressed data
72     unsigned short phash_offs[0x2204];      // 49D0: Table of offsets for each PAIR_HASH
73 } TCmpStruct;
74 
75 #define CMP_BUFFER_SIZE  sizeof(TCmpStruct) // Size of compression structure.
76                                             // Defined as 36312 in pkware header file
77 
78 
79 // Decompression structure
80 typedef struct
81 {
82     unsigned long offs0000;                 // 0000
83     unsigned long ctype;                    // 0004: Compression type (CMP_BINARY or CMP_ASCII)
84     unsigned long outputPos;                // 0008: Position in output buffer
85     unsigned long dsize_bits;               // 000C: Dict size (4, 5, 6 for 0x400, 0x800, 0x1000)
86     unsigned long dsize_mask;               // 0010: Dict size bitmask (0x0F, 0x1F, 0x3F for 0x400, 0x800, 0x1000)
87     unsigned long bit_buff;                 // 0014: 16-bit buffer for processing input data
88     unsigned long extra_bits;               // 0018: Number of extra (above 8) bits in bit buffer
89     unsigned int  in_pos;                   // 001C: Position in in_buff
90     unsigned long in_bytes;                 // 0020: Number of bytes in input buffer
91     void        * param;                    // 0024: Custom parameter
92     unsigned int (*read_buf)(char *buf, unsigned int *size, void *param); // Pointer to function that reads data from the input stream
93     void         (*write_buf)(char *buf, unsigned int *size, void *param);// Pointer to function that writes data to the output stream
94 
95     unsigned char out_buff[0x2204];         // 0030: Output circle buffer.
96                                             //       0x0000 - 0x0FFF: Previous uncompressed data, kept for repetitions
97                                             //       0x1000 - 0x1FFF: Currently decompressed data
98                                             //       0x2000 - 0x2203: Reserve space for the longest possible repetition
99     unsigned char in_buff[0x800];           // 2234: Buffer for data to be decompressed
100     unsigned char DistPosCodes[0x100];      // 2A34: Table of distance position codes
101     unsigned char LengthCodes[0x100];       // 2B34: Table of length codes
102     unsigned char offs2C34[0x100];          // 2C34: Buffer for
103     unsigned char offs2D34[0x100];          // 2D34: Buffer for
104     unsigned char offs2E34[0x80];           // 2E34: Buffer for
105     unsigned char offs2EB4[0x100];          // 2EB4: Buffer for
106     unsigned char ChBitsAsc[0x100];         // 2FB4: Buffer for
107     unsigned char DistBits[0x40];           // 30B4: Numbers of bytes to skip copied block length
108     unsigned char LenBits[0x10];            // 30F4: Numbers of bits for skip copied block length
109     unsigned char ExLenBits[0x10];          // 3104: Number of valid bits for copied block
110     unsigned short LenBase[0x10];           // 3114: Buffer for
111 } TDcmpStruct;
112 
113 #define EXP_BUFFER_SIZE sizeof(TDcmpStruct) // Size of decompression structure
114                                             // Defined as 12596 in pkware headers
115 
116 //-----------------------------------------------------------------------------
117 // Tables (in explode.c)
118 
119 extern unsigned char DistBits[0x40];
120 extern unsigned char DistCode[0x40];
121 extern unsigned char ExLenBits[0x10];
122 extern unsigned short LenBase[0x10];
123 extern unsigned char LenBits[0x10];
124 extern unsigned char LenCode[0x10];
125 extern unsigned char ChBitsAsc[0x100];
126 extern unsigned short ChCodeAsc[0x100];
127 
128 //-----------------------------------------------------------------------------
129 // Public functions
130 
131 #ifdef __cplusplus
132    extern "C" {
133 #endif
134 
135 unsigned int PKEXPORT implode(
136    unsigned int (*read_buf)(char *buf, unsigned int *size, void *param),
137    void         (*write_buf)(char *buf, unsigned int *size, void *param),
138    char         *work_buf,
139    void         *param,
140    unsigned int *type,
141    unsigned int *dsize);
142 
143 
144 unsigned int PKEXPORT explode(
145    unsigned int (*read_buf)(char *buf, unsigned  int *size, void *param),
146    void         (*write_buf)(char *buf, unsigned  int *size, void *param),
147    char         *work_buf,
148    void         *param);
149 
150 // The original name "crc32" was changed to "crc32pk" due
151 // to compatibility with zlib
152 unsigned long PKEXPORT crc32_pklib(char *buffer, unsigned int *size, unsigned long *old_crc);
153 
154 #ifdef __cplusplus
155    }                         // End of 'extern "C"' declaration
156 #endif
157 
158 #endif // __PKLIB_H__
159