-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpa105CsObjInt.cpp
339 lines (286 loc) · 14.5 KB
/
vpa105CsObjInt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
========== licence begin GPL
Copyright (c) 2000-2005 SAP AG
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
========== licence end
*/
// CsObjectInt.cpp: implementation of the CSObjectInt class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hpa101saptype.h"
// #include "cscompr.h"
#include "hpa106cslzc.h"
#include "hpa107cslzh.h"
#include "hpa104CsObject.h"
#include "hpa105CsObjInt.h"
static SAP_BYTE CsMagicHead[] = { "\037\235" }; /* 1F 9D */
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CsObjectInt::CsObjectInt ()
{
}
CsObjectInt::~CsObjectInt ()
{
}
int CsObjectInt::CsCompr (SAP_INT sumlen,
SAP_BYTE * inbuf,
SAP_INT inlen,
SAP_BYTE * outbuf,
SAP_INT outlen,
SAP_INT option,
SAP_INT * bytes_read,
SAP_INT * bytes_written)
/*--------------------------------------------------------------------*/
/* Compress a memory segmented */
/* */
/* Adaptive Dictionary Compression */
/* Lempel-Zip */
/* */
/* */
/* Input: */
/* ----- */
/* sumlen length of data stream to compress */
/* inbuf Pointer to input memory */
/* inlen Length of input memory */
/* outbuf Pointer to output area */
/* outlen Length of output area */
/* option Compress option: */
/* CS_INIT_COMPRESS initial */
/* CS_LZC or CS_LZH initial */
/* initial options have to be or'ed */
/* CS_NORMAL_COMPRESS */
/* Output: */
/* ------ */
/* bytes_read Bytes read from input buffer */
/* bytes_compressed Bytes compressed to output buffer */
/* */
/* Return Code: */
/* ----------- */
/* CS_END_OF_STREAM End of input reached */
/* CS_END_INBUFFER End of input reached */
/* CS_END_OUTBUFFER End of output reached */
/* */
/* CS_E_IN_BUFFER_LEN Input buffer length to short */
/* CS_E_OUT_BUFFER_LEN Output Buffer length to short */
/* CS_E_INVALID_SUMLEN sumlen <= 0 */
/* CS_E_INVALID_ADDR Invalid addr for input or output buffer*/
/* CS_E_FATAL internal (should never happen) */
/* */
/*--------------------------------------------------------------------*/
{
if (option & CS_INIT_COMPRESS)
{
algorithm = 0;
algorithm = (int) (option & 0xE);
}
/* Plausibility check for negative input length */
if (inlen < 0) inlen = 0;
switch (algorithm)
{
case CS_LZC:
return CsComprLZC (sumlen, inbuf, inlen, outbuf, outlen,
option, bytes_read, bytes_written);
case CS_LZH:
return CsComprLZH (sumlen, inbuf, inlen, outbuf, outlen,
option, bytes_read, bytes_written);
default: return CS_E_UNKNOWN_ALG;
}
}
int CsObjectInt::CsDecompr (SAP_BYTE * inbuf, /* ptr input .......*/
SAP_INT inlen, /* len of input ....*/
SAP_BYTE * outbuf, /* ptr output ......*/
SAP_INT outlen, /* len output ......*/
SAP_INT option, /* decompr. option */
SAP_INT * bytes_read, /* bytes read ......*/
SAP_INT * bytes_decompressed) /* bytes decompr. */
/*--------------------------------------------------------------------*/
/* Decompress */
/* */
/* Adaptive Dictionary Compression */
/* Lempel-Zip */
/* */
/* Input: */
/* ----- */
/* inbuf Pointer to input memory */
/* inlen Length of input memory */
/* outbuf Pointer to output area */
/* outlen Length of output area */
/* option DeCompress option: */
/* CS_INIT_DECOMPRESS initial */
/* CS_NORMAL_COMPRESS */
/* */
/* Output: */
/* ------ */
/* bytes_read Bytes read from input buffer */
/* bytes_written Bytes decompressed to output buffer */
/* */
/* Return Code: */
/* ----------- */
/* CS_END_OF_STREAM End of input stream reached */
/* CS_END_INBUFFER End of input buffer reached */
/* CS_END_OUTBUFFER End of output buffer reached */
/* */
/* CS_E_OUT_BUFFER_LEN Output buffer length to short */
/* CS_E_IN_BUFFER_LEN Input buffer length to short */
/* CS_E_MAXBITS_TOO_BIG No internal memory to decompress */
/* CS_E_INVALID_LEN inlen < 0 or outlen < CS_BITS */
/* CS_E_FILENOTCOMPRESSED Input is not compressed */
/* CS_E_IN_EQU_OUT Same addr for input and output buffer */
/* CS_E_INVALID_ADDR Invalid addr for input or output buffer*/
/* CS_E_FATAL Internal (should never happen) */
/* */
/*--------------------------------------------------------------------*/
{
if (option & CS_INIT_DECOMPRESS)
{
if (inlen < CS_HEAD_SIZE) return CS_E_IN_BUFFER_LEN;
algorithm = CsGetAlgorithm (inbuf);
}
switch (algorithm)
{
case CS_ALGORITHM_LZC:
return CsDecomprLZC (inbuf, inlen, outbuf, outlen,
option, bytes_read, bytes_decompressed);
case CS_ALGORITHM_LZH:
return CsDecomprLZH (inbuf, inlen, outbuf, outlen,
option, bytes_read, bytes_decompressed);
default: return CS_E_UNKNOWN_ALG;
}
}
int CsObjectInt::CsGetAlgorithm (SAP_BYTE * data)
/*--------------------------------------------------------------------*/
/* Get Algorithm number of compressed data */
/*--------------------------------------------------------------------*/
{
return ((int) (data[4] & (unsigned char)0x0F));
}
SAP_INT CsObjectInt::CsGetLen (SAP_BYTE * data)
/*--------------------------------------------------------------------*/
/* Get the length of the original data stream */
/* */
/* Returns CS_E_FILENOT_COMPRESSED if the magic number is */
/* different from magic header */
/* else */
/* Length of org. data stream */
/*--------------------------------------------------------------------*/
{
SAP_INT len;
/* file not compressed !!! .....*/
if ((CsMagicHead[0] != data[5]) ||
(CsMagicHead[1] != data[6]))
{
return ((SAP_INT)CS_E_FILENOTCOMPRESSED);
}
len = (SAP_INT)data[0] + /* read length from first buf ..*/
((SAP_INT)data[1] << 8) +
((SAP_INT)data[2] << 16) +
((SAP_INT)data[3] << 24);
return len;
}
int CsObjectInt::CsGetVersion (SAP_BYTE * data)
/*--------------------------------------------------------------------*/
/* Get version number of compressed data */
/*--------------------------------------------------------------------*/
{
return ((int)data[4] & 0xF0) >> 4;
}
int CsObjectInt::CsSetHead (SAP_BYTE * outbuf,
SAP_INT len,
SAP_BYTE veralg,
SAP_BYTE special)
/*--------------------------------------------------------------------*/
/* Setup length of data stream and header information */
/*--------------------------------------------------------------------*/
{
SAP_UINT l;
if (len < 0) return CS_E_INVALID_SUMLEN;
l = (SAP_UINT) len;
/* set length ......................................................*/
outbuf[0] = (SAP_BYTE) (l & 0xff);
outbuf[1] = (SAP_BYTE) ((l & 0xff00) >> 8);
outbuf[2] = (SAP_BYTE) ((l & 0xff0000L) >> 16);
outbuf[3] = (SAP_BYTE) ((l & 0xff000000L) >> 24);
/* setup header - magic number and maxbits .........................*/
outbuf[4] = veralg;
outbuf[5] = CsMagicHead[0];
outbuf[6] = CsMagicHead[1];
outbuf[7] = special;
return 0;
}
int CsObjectInt::CsInitCompr (SAP_BYTE * outbuf, SAP_INT sumlen, SAP_INT option)
/*--------------------------------------------------------------------*/
/* Only initalize Compression */
/* outbuf must point to a location which has at least */
/* CS_HEAD_SIZE bytes left */
/* */
/* Input: */
/* ----- */
/* outbuf Pointer to output memory */
/* sumlen length of data stream to compress */
/* option Compress option CS_LZC, CS_LZH */
/* */
/* Return Code: */
/* ----------- */
/* 0 OK */
/* */
/* CS_E_OUT_BUFFER_LEN Output Buffer length to short */
/* CS_E_INVALID_SUMLEN sumlen <= 0 */
/* CS_E_INVALID_ADDR Invalid addr for input or output buffer*/
/* CS_E_FATAL internal (should never happen) */
/* */
/*--------------------------------------------------------------------*/
{
SAP_BYTE input_field;
SAP_BYTE *inbuf = &input_field;
SAP_INT bytes_read, bytes_written;
int rc;
rc = CsCompr (sumlen, inbuf, 0, outbuf,
CS_HEAD_SIZE, option | CS_INIT_COMPRESS,
&bytes_read, &bytes_written);
if (rc < 0) return rc;
return 0;
}
int CsObjectInt::CsInitDecompr (SAP_BYTE * inbuf)
/*--------------------------------------------------------------------*/
/* Only initalize Decompression */
/* inbuf must point to a location which has at least */
/* CS_HEAD_SIZE bytes left */
/* */
/* Input: */
/* ----- */
/* inbuf Pointer to input memory */
/* */
/* Return Code: */
/* ----------- */
/* 0 OK */
/* */
/* CS_E_IN_BUFFER_LEN Input Buffer length to short */
/* CS_E_INVALID_ADDR Invalid addr for input */
/* CS_E_FATAL internal (should never happen) */
/* */
/*--------------------------------------------------------------------*/
{
SAP_BYTE output_field;
SAP_BYTE * outbuf = &output_field;
SAP_INT bytes_read, bytes_written;
int rc;
rc = CsDecompr (inbuf, CS_HEAD_SIZE,
outbuf, 0, CS_INIT_DECOMPRESS,
&bytes_read, &bytes_written);
if (rc < 0) return rc;
return 0;
}