30
30
#include "sys-core.h"
31
31
#include "sys-deci-funcs.h"
32
32
33
-
34
33
/***********************************************************************
35
34
**
36
35
** Hash Function Externs
@@ -291,19 +290,24 @@ static struct digest {
291
290
*/ REBNATIVE (compress )
292
291
/*
293
292
// compress: native [
294
- // {Compresses data. Default is deflate with Adler32 checksum.}
293
+ // {Compresses data. Default is deflate with Adler32 checksum and uncompressed size in last 4 bytes .}
295
294
// data [binary! string!] {If string, it will be UTF8 encoded}
296
- // /part length {Length of data (elements)}
297
- // /gzip {Use deflate with GZIP checksum (CRC32)}
295
+ // /part length {Length of source data}
296
+ // /zlib {Use ZLIB (Adler32 checksum) without uncompressed length appended}
297
+ // /gzip {Use ZLIB with GZIP envelope (using CRC32 checksum)}
298
298
// /lzma {Use LZMA compression}
299
+ // /level lvl [integer!] {Compression level 0-9}
299
300
// ]
300
301
***********************************************************************/
301
302
{
302
303
REBVAL * data = D_ARG (1 );
303
304
//REBOOL ref_part = D_REF(2);
304
305
REBVAL * length = D_ARG (3 );
305
- REBOOL ref_gzip = D_REF (4 );
306
- REBOOL ref_lzma = D_REF (5 );
306
+ REBOOL ref_zlib = D_REF (4 );
307
+ REBOOL ref_gzip = D_REF (5 );
308
+ REBOOL ref_lzma = D_REF (6 );
309
+ REBOOL ref_level = D_REF (7 );
310
+ REBVAL * level = D_ARG (8 );
307
311
308
312
REBSER * ser ;
309
313
REBCNT index ;
@@ -314,12 +318,14 @@ static struct digest {
314
318
315
319
if (ref_lzma ) {
316
320
#ifdef USE_LZMA
317
- Set_Binary (D_RET , CompressLzma (ser , index , (REBINT )len ));
321
+ Set_Binary (D_RET , CompressLzma (ser , index , (REBINT )len , ref_level ? VAL_INT32 ( level ) : -1 ));
318
322
#else
319
323
Trap0 (RE_FEATURE_NA );
320
324
#endif
321
325
} else {
322
- Set_Binary (D_RET , Compress (ser , index , (REBINT )len , ref_gzip )); // /gzip
326
+ int windowBits = MAX_WBITS ;
327
+ if (ref_gzip ) windowBits |= 16 ;
328
+ Set_Binary (D_RET , CompressZlib (ser , index , (REBINT )len , ref_level ? VAL_INT32 (level ) : -1 , windowBits ));
323
329
}
324
330
325
331
return R_RET ;
@@ -332,28 +338,32 @@ static struct digest {
332
338
/*
333
339
// decompress: native [
334
340
// {Decompresses data. Result is binary.}
335
- // data [binary!] {Data to decompress}
336
- // /part length {Length of compressed data (must match end marker)}
337
- // /gzip {Use GZIP checksum}
338
- // /lzma {Use LZMA encoding}
339
- // /limit size {Error out if result is larger than this}
341
+ // data [binary!] {Source data to decompress}
342
+ // /part "Limits source data to a given length or position"
343
+ // length [number! series!] {Length of compressed data (must match end marker)}
344
+ // /zlib {Data are in ZLIB format with Adler32 checksum}
345
+ // /gzip {Data are in ZLIB format with CRC32 checksum}
346
+ // /lzma {Data are in LZMA format}
347
+ // /size
348
+ // bytes [integer!] {Number of decompressed bytes. If not used, size is detected from last 4 source data bytes.}
340
349
]
341
350
***********************************************************************/
342
351
{
343
352
REBVAL * data = D_ARG (1 );
344
353
//REBOOL ref_part = D_REF(2);
345
354
REBVAL * length = D_ARG (3 );
346
- REBOOL ref_gzip = D_REF (4 );
347
- REBOOL ref_lzma = D_REF (5 );
348
- REBOOL ref_limit = D_REF (6 );
349
- REBVAL * size = D_ARG (7 );
355
+ REBOOL ref_zlib = D_REF (4 );
356
+ REBOOL ref_gzip = D_REF (5 );
357
+ REBOOL ref_lzma = D_REF (6 );
358
+ REBOOL ref_size = D_REF (7 );
359
+ REBVAL * size = D_ARG (8 );
350
360
351
361
REBINT limit = 0 ;
352
362
REBCNT len ;
353
363
354
364
len = Partial1 (D_ARG (1 ), D_ARG (3 ));
355
365
356
- if (ref_limit ) limit = Int32s (size , 1 ); // /limit size
366
+ if (ref_size ) limit = Int32s (size , 1 ); // /limit size
357
367
358
368
if (ref_lzma ) {
359
369
#ifdef USE_LZMA
@@ -362,7 +372,9 @@ static struct digest {
362
372
Trap0 (RE_FEATURE_NA );
363
373
#endif
364
374
} else {
365
- Set_Binary (D_RET , Decompress (VAL_SERIES (data ), VAL_INDEX (data ), (REBINT )len , limit , ref_gzip )); // /gzip
375
+ int windowBits = MAX_WBITS ;
376
+ if (ref_gzip ) windowBits |= 16 ;
377
+ Set_Binary (D_RET , DecompressZlib (VAL_SERIES (data ), VAL_INDEX (data ), (REBINT )len , limit , windowBits ));
366
378
}
367
379
368
380
0 commit comments