Skip to content

Commit 85d9593

Browse files
committed
sync zlib with userland
1 parent 62a6fda commit 85d9593

File tree

7 files changed

+79
-15
lines changed

7 files changed

+79
-15
lines changed

sys/lib/libz/deflate.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,11 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
489489
* symbols from which it is being constructed.
490490
*/
491491

492+
#ifdef LIT_MEM
493+
s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 5);
494+
#else
492495
s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
496+
#endif
493497
s->pending_buf_size = (ulg)s->lit_bufsize * 4;
494498

495499
if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
@@ -499,8 +503,14 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
499503
deflateEnd (strm);
500504
return Z_MEM_ERROR;
501505
}
506+
#ifdef LIT_MEM
507+
s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1));
508+
s->l_buf = s->pending_buf + (s->lit_bufsize << 2);
509+
s->sym_end = s->lit_bufsize - 1;
510+
#else
502511
s->sym_buf = s->pending_buf + s->lit_bufsize;
503512
s->sym_end = (s->lit_bufsize - 1) * 3;
513+
#endif
504514
/* We avoid equality with lit_bufsize*3 because of wraparound at 64K
505515
* on 16 bit machines and because stored blocks are restricted to
506516
* 64K-1 bytes.
@@ -716,9 +726,15 @@ int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
716726

717727
if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
718728
s = strm->state;
729+
#ifdef LIT_MEM
730+
if (bits < 0 || bits > 16 ||
731+
(uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3))
732+
return Z_BUF_ERROR;
733+
#else
719734
if (bits < 0 || bits > 16 ||
720735
s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
721736
return Z_BUF_ERROR;
737+
#endif
722738
do {
723739
put = Buf_size - s->bi_valid;
724740
if (put > bits)
@@ -1304,7 +1320,12 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
13041320
zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
13051321

13061322
ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1323+
#ifdef LIT_MEM
1324+
ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1));
1325+
ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
1326+
#else
13071327
ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1328+
#endif
13081329

13091330
ds->l_desc.dyn_tree = ds->dyn_ltree;
13101331
ds->d_desc.dyn_tree = ds->dyn_dtree;

sys/lib/libz/deflate.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
# define GZIP
2222
#endif
2323

24+
/* define LIT_MEM to slightly increase the speed of deflate (order 1% to 2%) at
25+
the cost of a larger memory footprint */
26+
/* #define LIT_MEM */
27+
2428
/* ===========================================================================
2529
* Internal compression state.
2630
*/
@@ -215,7 +219,12 @@ typedef struct internal_state {
215219
/* Depth of each subtree used as tie breaker for trees of equal frequency
216220
*/
217221

222+
#ifdef LIT_MEM
223+
ushf *d_buf; /* buffer for distances */
224+
uchf *l_buf; /* buffer for literals/lengths */
225+
#else
218226
uchf *sym_buf; /* buffer for distances and literals/lengths */
227+
#endif
219228

220229
uInt lit_bufsize;
221230
/* Size of match buffer for literals/lengths. There are 4 reasons for
@@ -237,7 +246,7 @@ typedef struct internal_state {
237246
* - I can't count above 4
238247
*/
239248

240-
uInt sym_next; /* running index in sym_buf */
249+
uInt sym_next; /* running index in symbol buffer */
241250
uInt sym_end; /* symbol table full when sym_next reaches this */
242251

243252
ulg opt_len; /* bit length of current block with optimal trees */
@@ -316,6 +325,25 @@ void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf,
316325
extern const uch ZLIB_INTERNAL _dist_code[];
317326
#endif
318327

328+
#ifdef LIT_MEM
329+
# define _tr_tally_lit(s, c, flush) \
330+
{ uch cc = (c); \
331+
s->d_buf[s->sym_next] = 0; \
332+
s->l_buf[s->sym_next++] = cc; \
333+
s->dyn_ltree[cc].Freq++; \
334+
flush = (s->sym_next == s->sym_end); \
335+
}
336+
# define _tr_tally_dist(s, distance, length, flush) \
337+
{ uch len = (uch)(length); \
338+
ush dist = (ush)(distance); \
339+
s->d_buf[s->sym_next] = dist; \
340+
s->l_buf[s->sym_next++] = len; \
341+
dist--; \
342+
s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
343+
s->dyn_dtree[d_code(dist)].Freq++; \
344+
flush = (s->sym_next == s->sym_end); \
345+
}
346+
#else
319347
# define _tr_tally_lit(s, c, flush) \
320348
{ uch cc = (c); \
321349
s->sym_buf[s->sym_next++] = 0; \
@@ -335,6 +363,7 @@ void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf,
335363
s->dyn_dtree[d_code(dist)].Freq++; \
336364
flush = (s->sym_next == s->sym_end); \
337365
}
366+
#endif
338367
#else
339368
# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
340369
# define _tr_tally_dist(s, distance, length, flush) \

sys/lib/libz/inflate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ int ZEXPORT inflateSync(z_streamp strm) {
14731473
/* if first time, start search in bit buffer */
14741474
if (state->mode != SYNC) {
14751475
state->mode = SYNC;
1476-
state->hold <<= state->bits & 7;
1476+
state->hold >>= state->bits & 7;
14771477
state->bits -= state->bits & 7;
14781478
len = 0;
14791479
while (state->bits >= 8) {

sys/lib/libz/inftrees.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens,
5555
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
5656
static const unsigned short lext[31] = { /* Length codes 257..285 extra */
5757
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
58-
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 198, 203};
58+
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 70, 200};
5959
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
6060
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
6161
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,

sys/lib/libz/inftrees.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ typedef struct {
4141
examples/enough.c found in the zlib distribution. The arguments to that
4242
program are the number of symbols, the initial root table size, and the
4343
maximum bit length of a code. "enough 286 9 15" for literal/length codes
44-
returns returns 852, and "enough 30 6 15" for distance codes returns 592.
45-
The initial root table size (9 or 6) is found in the fifth argument of the
44+
returns 852, and "enough 30 6 15" for distance codes returns 592. The
45+
initial root table size (9 or 6) is found in the fifth argument of the
4646
inflate_table() calls in inflate.c and infback.c. If the root table size is
4747
changed, then these maximum sizes would be need to be recalculated and
4848
updated. */

sys/lib/libz/trees.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,14 +897,19 @@ local void compress_block(deflate_state *s, const ct_data *ltree,
897897
const ct_data *dtree) {
898898
unsigned dist; /* distance of matched string */
899899
int lc; /* match length or unmatched char (if dist == 0) */
900-
unsigned sx = 0; /* running index in sym_buf */
900+
unsigned sx = 0; /* running index in symbol buffers */
901901
unsigned code; /* the code to send */
902902
int extra; /* number of extra bits to send */
903903

904904
if (s->sym_next != 0) do {
905+
#ifdef LIT_MEM
906+
dist = s->d_buf[sx];
907+
lc = s->l_buf[sx++];
908+
#else
905909
dist = s->sym_buf[sx++] & 0xff;
906910
dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
907911
lc = s->sym_buf[sx++];
912+
#endif
908913
if (dist == 0) {
909914
send_code(s, lc, ltree); /* send a literal byte */
910915
Tracecv(isgraph(lc), (stderr," '%c' ", lc));
@@ -929,8 +934,12 @@ local void compress_block(deflate_state *s, const ct_data *ltree,
929934
}
930935
} /* literal or match pair ? */
931936

932-
/* Check that the overlay between pending_buf and sym_buf is ok: */
937+
/* Check for no overlay of pending_buf on needed symbols */
938+
#ifdef LIT_MEM
939+
Assert(s->pending < (s->lit_bufsize << 1) + sx, "pendingBuf overflow");
940+
#else
933941
Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
942+
#endif
934943

935944
} while (sx < s->sym_next);
936945

@@ -1080,9 +1089,14 @@ void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf,
10801089
* the current block must be flushed.
10811090
*/
10821091
int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc) {
1092+
#ifdef LIT_MEM
1093+
s->d_buf[s->sym_next] = (ush)dist;
1094+
s->l_buf[s->sym_next++] = (uch)lc;
1095+
#else
10831096
s->sym_buf[s->sym_next++] = (uch)dist;
10841097
s->sym_buf[s->sym_next++] = (uch)(dist >> 8);
10851098
s->sym_buf[s->sym_next++] = (uch)lc;
1099+
#endif
10861100
if (dist == 0) {
10871101
/* lc is the unmatched char */
10881102
s->dyn_ltree[lc].Freq++;

sys/lib/libz/zlib.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* zlib.h -- interface of the 'zlib' general purpose compression library
2-
version 1.3, August 18th, 2023
2+
version 1.3.0.1, August xxth, 2023
33
44
Copyright (C) 1995-2023 Jean-loup Gailly and Mark Adler
55
@@ -37,12 +37,12 @@
3737
extern "C" {
3838
#endif
3939

40-
#define ZLIB_VERSION "1.3"
41-
#define ZLIB_VERNUM 0x1300
40+
#define ZLIB_VERSION "1.3.0.1-motley"
41+
#define ZLIB_VERNUM 0x1301
4242
#define ZLIB_VER_MAJOR 1
4343
#define ZLIB_VER_MINOR 3
4444
#define ZLIB_VER_REVISION 0
45-
#define ZLIB_VER_SUBREVISION 0
45+
#define ZLIB_VER_SUBREVISION 1
4646

4747
/*
4848
The 'zlib' compression library provides in-memory compression and
@@ -936,10 +936,10 @@ ZEXTERN int ZEXPORT inflateSync(z_streamp strm);
936936
inflateSync returns Z_OK if a possible full flush point has been found,
937937
Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point
938938
has been found, or Z_STREAM_ERROR if the stream structure was inconsistent.
939-
In the success case, the application may save the current current value of
940-
total_in which indicates where valid compressed data was found. In the
941-
error case, the application may repeatedly call inflateSync, providing more
942-
input each time, until success or end of the input data.
939+
In the success case, the application may save the current value of total_in
940+
which indicates where valid compressed data was found. In the error case,
941+
the application may repeatedly call inflateSync, providing more input each
942+
time, until success or end of the input data.
943943
*/
944944

945945
ZEXTERN int ZEXPORT inflateCopy(z_streamp dest,

0 commit comments

Comments
 (0)