zstdlib 0.11.0-x86_64-darwin → 0.12.0-x86_64-darwin

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +9 -0
  3. data/ext/zstdlib_c/extconf.rb +8 -3
  4. data/ext/zstdlib_c/ruby/zlib-3.3/zstdlib.c +5090 -0
  5. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/adler32.c +5 -27
  6. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/compress.c +5 -16
  7. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/crc32.c +94 -161
  8. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/deflate.c +362 -434
  9. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/deflate.h +43 -12
  10. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/gzclose.c +1 -3
  11. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/gzguts.h +13 -18
  12. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/gzlib.c +28 -85
  13. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/gzread.c +23 -73
  14. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/gzwrite.c +19 -65
  15. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/infback.c +17 -30
  16. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/inffast.c +1 -4
  17. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/inffast.h +1 -1
  18. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/inflate.c +36 -102
  19. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/inftrees.c +6 -11
  20. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/inftrees.h +6 -6
  21. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/trees.c +290 -355
  22. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/uncompr.c +4 -12
  23. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/zconf.h +23 -14
  24. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/zlib.h +202 -199
  25. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/zutil.c +18 -44
  26. data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/zutil.h +13 -33
  27. data/lib/2.4/zstdlib_c.bundle +0 -0
  28. data/lib/2.5/zstdlib_c.bundle +0 -0
  29. data/lib/2.6/zstdlib_c.bundle +0 -0
  30. data/lib/2.7/zstdlib_c.bundle +0 -0
  31. data/lib/3.0/zstdlib_c.bundle +0 -0
  32. data/lib/3.1/zstdlib_c.bundle +0 -0
  33. data/lib/3.2/zstdlib_c.bundle +0 -0
  34. data/lib/3.3/zstdlib_c.bundle +0 -0
  35. metadata +34 -32
  36. /data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/crc32.h +0 -0
  37. /data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/inffixed.h +0 -0
  38. /data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/inflate.h +0 -0
  39. /data/ext/zstdlib_c/{zlib-1.2.12 → zlib-1.3.1}/trees.h +0 -0
@@ -1,5 +1,5 @@
1
1
  /* trees.c -- output deflated data using Huffman coding
2
- * Copyright (C) 1995-2021 Jean-loup Gailly
2
+ * Copyright (C) 1995-2024 Jean-loup Gailly
3
3
  * detect_data_type() function provided freely by Cosmin Truta, 2006
4
4
  * For conditions of distribution and use, see copyright notice in zlib.h
5
5
  */
@@ -122,39 +122,116 @@ struct static_tree_desc_s {
122
122
  int max_length; /* max bit length for the codes */
123
123
  };
124
124
 
125
- local const static_tree_desc static_l_desc =
125
+ #ifdef NO_INIT_GLOBAL_POINTERS
126
+ # define TCONST
127
+ #else
128
+ # define TCONST const
129
+ #endif
130
+
131
+ local TCONST static_tree_desc static_l_desc =
126
132
  {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
127
133
 
128
- local const static_tree_desc static_d_desc =
134
+ local TCONST static_tree_desc static_d_desc =
129
135
  {static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
130
136
 
131
- local const static_tree_desc static_bl_desc =
137
+ local TCONST static_tree_desc static_bl_desc =
132
138
  {(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
133
139
 
134
140
  /* ===========================================================================
135
- * Local (static) routines in this file.
141
+ * Output a short LSB first on the stream.
142
+ * IN assertion: there is enough room in pendingBuf.
143
+ */
144
+ #define put_short(s, w) { \
145
+ put_byte(s, (uch)((w) & 0xff)); \
146
+ put_byte(s, (uch)((ush)(w) >> 8)); \
147
+ }
148
+
149
+ /* ===========================================================================
150
+ * Reverse the first len bits of a code, using straightforward code (a faster
151
+ * method would use a table)
152
+ * IN assertion: 1 <= len <= 15
153
+ */
154
+ local unsigned bi_reverse(unsigned code, int len) {
155
+ register unsigned res = 0;
156
+ do {
157
+ res |= code & 1;
158
+ code >>= 1, res <<= 1;
159
+ } while (--len > 0);
160
+ return res >> 1;
161
+ }
162
+
163
+ /* ===========================================================================
164
+ * Flush the bit buffer, keeping at most 7 bits in it.
165
+ */
166
+ local void bi_flush(deflate_state *s) {
167
+ if (s->bi_valid == 16) {
168
+ put_short(s, s->bi_buf);
169
+ s->bi_buf = 0;
170
+ s->bi_valid = 0;
171
+ } else if (s->bi_valid >= 8) {
172
+ put_byte(s, (Byte)s->bi_buf);
173
+ s->bi_buf >>= 8;
174
+ s->bi_valid -= 8;
175
+ }
176
+ }
177
+
178
+ /* ===========================================================================
179
+ * Flush the bit buffer and align the output on a byte boundary
180
+ */
181
+ local void bi_windup(deflate_state *s) {
182
+ if (s->bi_valid > 8) {
183
+ put_short(s, s->bi_buf);
184
+ } else if (s->bi_valid > 0) {
185
+ put_byte(s, (Byte)s->bi_buf);
186
+ }
187
+ s->bi_buf = 0;
188
+ s->bi_valid = 0;
189
+ #ifdef ZLIB_DEBUG
190
+ s->bits_sent = (s->bits_sent + 7) & ~7;
191
+ #endif
192
+ }
193
+
194
+ /* ===========================================================================
195
+ * Generate the codes for a given tree and bit counts (which need not be
196
+ * optimal).
197
+ * IN assertion: the array bl_count contains the bit length statistics for
198
+ * the given tree and the field len is set for all tree elements.
199
+ * OUT assertion: the field code is set for all tree elements of non
200
+ * zero code length.
136
201
  */
202
+ local void gen_codes(ct_data *tree, int max_code, ushf *bl_count) {
203
+ ush next_code[MAX_BITS+1]; /* next code value for each bit length */
204
+ unsigned code = 0; /* running code value */
205
+ int bits; /* bit index */
206
+ int n; /* code index */
137
207
 
138
- local void tr_static_init OF((void));
139
- local void init_block OF((deflate_state *s));
140
- local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
141
- local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
142
- local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
143
- local void build_tree OF((deflate_state *s, tree_desc *desc));
144
- local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
145
- local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
146
- local int build_bl_tree OF((deflate_state *s));
147
- local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
148
- int blcodes));
149
- local void compress_block OF((deflate_state *s, const ct_data *ltree,
150
- const ct_data *dtree));
151
- local int detect_data_type OF((deflate_state *s));
152
- local unsigned bi_reverse OF((unsigned code, int len));
153
- local void bi_windup OF((deflate_state *s));
154
- local void bi_flush OF((deflate_state *s));
208
+ /* The distribution counts are first used to generate the code values
209
+ * without bit reversal.
210
+ */
211
+ for (bits = 1; bits <= MAX_BITS; bits++) {
212
+ code = (code + bl_count[bits - 1]) << 1;
213
+ next_code[bits] = (ush)code;
214
+ }
215
+ /* Check that the bit counts in bl_count are consistent. The last code
216
+ * must be all ones.
217
+ */
218
+ Assert (code + bl_count[MAX_BITS] - 1 == (1 << MAX_BITS) - 1,
219
+ "inconsistent bit counts");
220
+ Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
221
+
222
+ for (n = 0; n <= max_code; n++) {
223
+ int len = tree[n].Len;
224
+ if (len == 0) continue;
225
+ /* Now reverse the bits */
226
+ tree[n].Code = (ush)bi_reverse(next_code[len]++, len);
227
+
228
+ Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
229
+ n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len] - 1));
230
+ }
231
+ }
155
232
 
156
233
  #ifdef GEN_TREES_H
157
- local void gen_trees_header OF((void));
234
+ local void gen_trees_header(void);
158
235
  #endif
159
236
 
160
237
  #ifndef ZLIB_DEBUG
@@ -167,33 +244,18 @@ local void gen_trees_header OF((void));
167
244
  send_bits(s, tree[c].Code, tree[c].Len); }
168
245
  #endif
169
246
 
170
- /* ===========================================================================
171
- * Output a short LSB first on the stream.
172
- * IN assertion: there is enough room in pendingBuf.
173
- */
174
- #define put_short(s, w) { \
175
- put_byte(s, (uch)((w) & 0xff)); \
176
- put_byte(s, (uch)((ush)(w) >> 8)); \
177
- }
178
-
179
247
  /* ===========================================================================
180
248
  * Send a value on a given number of bits.
181
249
  * IN assertion: length <= 16 and value fits in length bits.
182
250
  */
183
251
  #ifdef ZLIB_DEBUG
184
- local void send_bits OF((deflate_state *s, int value, int length));
185
-
186
- local void send_bits(s, value, length)
187
- deflate_state *s;
188
- int value; /* value to send */
189
- int length; /* number of bits */
190
- {
252
+ local void send_bits(deflate_state *s, int value, int length) {
191
253
  Tracevv((stderr," l %2d v %4x ", length, value));
192
254
  Assert(length > 0 && length <= 15, "invalid length");
193
255
  s->bits_sent += (ulg)length;
194
256
 
195
257
  /* If not enough room in bi_buf, use (valid) bits from bi_buf and
196
- * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
258
+ * (16 - bi_valid) bits from value, leaving (width - (16 - bi_valid))
197
259
  * unused bits in value.
198
260
  */
199
261
  if (s->bi_valid > (int)Buf_size - length) {
@@ -229,8 +291,7 @@ local void send_bits(s, value, length)
229
291
  /* ===========================================================================
230
292
  * Initialize the various 'constant' tables.
231
293
  */
232
- local void tr_static_init()
233
- {
294
+ local void tr_static_init(void) {
234
295
  #if defined(GEN_TREES_H) || !defined(STDC)
235
296
  static int static_init_done = 0;
236
297
  int n; /* iterates over tree elements */
@@ -256,7 +317,7 @@ local void tr_static_init()
256
317
  length = 0;
257
318
  for (code = 0; code < LENGTH_CODES-1; code++) {
258
319
  base_length[code] = length;
259
- for (n = 0; n < (1<<extra_lbits[code]); n++) {
320
+ for (n = 0; n < (1 << extra_lbits[code]); n++) {
260
321
  _length_code[length++] = (uch)code;
261
322
  }
262
323
  }
@@ -265,13 +326,13 @@ local void tr_static_init()
265
326
  * in two different ways: code 284 + 5 bits or code 285, so we
266
327
  * overwrite length_code[255] to use the best encoding:
267
328
  */
268
- _length_code[length-1] = (uch)code;
329
+ _length_code[length - 1] = (uch)code;
269
330
 
270
331
  /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
271
332
  dist = 0;
272
333
  for (code = 0 ; code < 16; code++) {
273
334
  base_dist[code] = dist;
274
- for (n = 0; n < (1<<extra_dbits[code]); n++) {
335
+ for (n = 0; n < (1 << extra_dbits[code]); n++) {
275
336
  _dist_code[dist++] = (uch)code;
276
337
  }
277
338
  }
@@ -279,11 +340,11 @@ local void tr_static_init()
279
340
  dist >>= 7; /* from now on, all distances are divided by 128 */
280
341
  for ( ; code < D_CODES; code++) {
281
342
  base_dist[code] = dist << 7;
282
- for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
343
+ for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++) {
283
344
  _dist_code[256 + dist++] = (uch)code;
284
345
  }
285
346
  }
286
- Assert (dist == 256, "tr_static_init: 256+dist != 512");
347
+ Assert (dist == 256, "tr_static_init: 256 + dist != 512");
287
348
 
288
349
  /* Construct the codes of the static literal tree */
289
350
  for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
@@ -312,7 +373,7 @@ local void tr_static_init()
312
373
  }
313
374
 
314
375
  /* ===========================================================================
315
- * Genererate the file trees.h describing the static trees.
376
+ * Generate the file trees.h describing the static trees.
316
377
  */
317
378
  #ifdef GEN_TREES_H
318
379
  # ifndef ZLIB_DEBUG
@@ -321,10 +382,9 @@ local void tr_static_init()
321
382
 
322
383
  # define SEPARATOR(i, last, width) \
323
384
  ((i) == (last)? "\n};\n\n" : \
324
- ((i) % (width) == (width)-1 ? ",\n" : ", "))
385
+ ((i) % (width) == (width) - 1 ? ",\n" : ", "))
325
386
 
326
- void gen_trees_header()
327
- {
387
+ void gen_trees_header(void) {
328
388
  FILE *header = fopen("trees.h", "w");
329
389
  int i;
330
390
 
@@ -373,12 +433,26 @@ void gen_trees_header()
373
433
  }
374
434
  #endif /* GEN_TREES_H */
375
435
 
436
+ /* ===========================================================================
437
+ * Initialize a new block.
438
+ */
439
+ local void init_block(deflate_state *s) {
440
+ int n; /* iterates over tree elements */
441
+
442
+ /* Initialize the trees. */
443
+ for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
444
+ for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
445
+ for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
446
+
447
+ s->dyn_ltree[END_BLOCK].Freq = 1;
448
+ s->opt_len = s->static_len = 0L;
449
+ s->sym_next = s->matches = 0;
450
+ }
451
+
376
452
  /* ===========================================================================
377
453
  * Initialize the tree data structures for a new zlib stream.
378
454
  */
379
- void ZLIB_INTERNAL _tr_init(s)
380
- deflate_state *s;
381
- {
455
+ void ZLIB_INTERNAL _tr_init(deflate_state *s) {
382
456
  tr_static_init();
383
457
 
384
458
  s->l_desc.dyn_tree = s->dyn_ltree;
@@ -401,24 +475,6 @@ void ZLIB_INTERNAL _tr_init(s)
401
475
  init_block(s);
402
476
  }
403
477
 
404
- /* ===========================================================================
405
- * Initialize a new block.
406
- */
407
- local void init_block(s)
408
- deflate_state *s;
409
- {
410
- int n; /* iterates over tree elements */
411
-
412
- /* Initialize the trees. */
413
- for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
414
- for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
415
- for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
416
-
417
- s->dyn_ltree[END_BLOCK].Freq = 1;
418
- s->opt_len = s->static_len = 0L;
419
- s->sym_next = s->matches = 0;
420
- }
421
-
422
478
  #define SMALLEST 1
423
479
  /* Index within the heap array of least frequent node in the Huffman tree */
424
480
 
@@ -448,17 +504,13 @@ local void init_block(s)
448
504
  * when the heap property is re-established (each father smaller than its
449
505
  * two sons).
450
506
  */
451
- local void pqdownheap(s, tree, k)
452
- deflate_state *s;
453
- ct_data *tree; /* the tree to restore */
454
- int k; /* node to move down */
455
- {
507
+ local void pqdownheap(deflate_state *s, ct_data *tree, int k) {
456
508
  int v = s->heap[k];
457
509
  int j = k << 1; /* left son of k */
458
510
  while (j <= s->heap_len) {
459
511
  /* Set j to the smallest of the two sons: */
460
512
  if (j < s->heap_len &&
461
- smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
513
+ smaller(tree, s->heap[j + 1], s->heap[j], s->depth)) {
462
514
  j++;
463
515
  }
464
516
  /* Exit if v is smaller than both sons */
@@ -483,10 +535,7 @@ local void pqdownheap(s, tree, k)
483
535
  * The length opt_len is updated; static_len is also updated if stree is
484
536
  * not null.
485
537
  */
486
- local void gen_bitlen(s, desc)
487
- deflate_state *s;
488
- tree_desc *desc; /* the tree descriptor */
489
- {
538
+ local void gen_bitlen(deflate_state *s, tree_desc *desc) {
490
539
  ct_data *tree = desc->dyn_tree;
491
540
  int max_code = desc->max_code;
492
541
  const ct_data *stree = desc->stat_desc->static_tree;
@@ -507,7 +556,7 @@ local void gen_bitlen(s, desc)
507
556
  */
508
557
  tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
509
558
 
510
- for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
559
+ for (h = s->heap_max + 1; h < HEAP_SIZE; h++) {
511
560
  n = s->heap[h];
512
561
  bits = tree[tree[n].Dad].Len + 1;
513
562
  if (bits > max_length) bits = max_length, overflow++;
@@ -518,7 +567,7 @@ local void gen_bitlen(s, desc)
518
567
 
519
568
  s->bl_count[bits]++;
520
569
  xbits = 0;
521
- if (n >= base) xbits = extra[n-base];
570
+ if (n >= base) xbits = extra[n - base];
522
571
  f = tree[n].Freq;
523
572
  s->opt_len += (ulg)f * (unsigned)(bits + xbits);
524
573
  if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits);
@@ -530,10 +579,10 @@ local void gen_bitlen(s, desc)
530
579
 
531
580
  /* Find the first bit length which could increase: */
532
581
  do {
533
- bits = max_length-1;
582
+ bits = max_length - 1;
534
583
  while (s->bl_count[bits] == 0) bits--;
535
- s->bl_count[bits]--; /* move one leaf down the tree */
536
- s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
584
+ s->bl_count[bits]--; /* move one leaf down the tree */
585
+ s->bl_count[bits + 1] += 2; /* move one overflow item as its brother */
537
586
  s->bl_count[max_length]--;
538
587
  /* The brother of the overflow item also moves one step up,
539
588
  * but this does not affect bl_count[max_length]
@@ -561,48 +610,9 @@ local void gen_bitlen(s, desc)
561
610
  }
562
611
  }
563
612
 
564
- /* ===========================================================================
565
- * Generate the codes for a given tree and bit counts (which need not be
566
- * optimal).
567
- * IN assertion: the array bl_count contains the bit length statistics for
568
- * the given tree and the field len is set for all tree elements.
569
- * OUT assertion: the field code is set for all tree elements of non
570
- * zero code length.
571
- */
572
- local void gen_codes (tree, max_code, bl_count)
573
- ct_data *tree; /* the tree to decorate */
574
- int max_code; /* largest code with non zero frequency */
575
- ushf *bl_count; /* number of codes at each bit length */
576
- {
577
- ush next_code[MAX_BITS+1]; /* next code value for each bit length */
578
- unsigned code = 0; /* running code value */
579
- int bits; /* bit index */
580
- int n; /* code index */
581
-
582
- /* The distribution counts are first used to generate the code values
583
- * without bit reversal.
584
- */
585
- for (bits = 1; bits <= MAX_BITS; bits++) {
586
- code = (code + bl_count[bits-1]) << 1;
587
- next_code[bits] = (ush)code;
588
- }
589
- /* Check that the bit counts in bl_count are consistent. The last code
590
- * must be all ones.
591
- */
592
- Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
593
- "inconsistent bit counts");
594
- Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
595
-
596
- for (n = 0; n <= max_code; n++) {
597
- int len = tree[n].Len;
598
- if (len == 0) continue;
599
- /* Now reverse the bits */
600
- tree[n].Code = (ush)bi_reverse(next_code[len]++, len);
601
-
602
- Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
603
- n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
604
- }
605
- }
613
+ #ifdef DUMP_BL_TREE
614
+ # include <stdio.h>
615
+ #endif
606
616
 
607
617
  /* ===========================================================================
608
618
  * Construct one Huffman tree and assigns the code bit strings and lengths.
@@ -612,10 +622,7 @@ local void gen_codes (tree, max_code, bl_count)
612
622
  * and corresponding code. The length opt_len is updated; static_len is
613
623
  * also updated if stree is not null. The field max_code is set.
614
624
  */
615
- local void build_tree(s, desc)
616
- deflate_state *s;
617
- tree_desc *desc; /* the tree descriptor */
618
- {
625
+ local void build_tree(deflate_state *s, tree_desc *desc) {
619
626
  ct_data *tree = desc->dyn_tree;
620
627
  const ct_data *stree = desc->stat_desc->static_tree;
621
628
  int elems = desc->stat_desc->elems;
@@ -624,7 +631,7 @@ local void build_tree(s, desc)
624
631
  int node; /* new node being created */
625
632
 
626
633
  /* Construct the initial heap, with least frequent element in
627
- * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
634
+ * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n + 1].
628
635
  * heap[0] is not used.
629
636
  */
630
637
  s->heap_len = 0, s->heap_max = HEAP_SIZE;
@@ -652,7 +659,7 @@ local void build_tree(s, desc)
652
659
  }
653
660
  desc->max_code = max_code;
654
661
 
655
- /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
662
+ /* The elements heap[heap_len/2 + 1 .. heap_len] are leaves of the tree,
656
663
  * establish sub-heaps of increasing lengths:
657
664
  */
658
665
  for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
@@ -700,11 +707,7 @@ local void build_tree(s, desc)
700
707
  * Scan a literal or distance tree to determine the frequencies of the codes
701
708
  * in the bit length tree.
702
709
  */
703
- local void scan_tree (s, tree, max_code)
704
- deflate_state *s;
705
- ct_data *tree; /* the tree to be scanned */
706
- int max_code; /* and its largest code of non zero frequency */
707
- {
710
+ local void scan_tree(deflate_state *s, ct_data *tree, int max_code) {
708
711
  int n; /* iterates over all tree elements */
709
712
  int prevlen = -1; /* last emitted length */
710
713
  int curlen; /* length of current code */
@@ -714,10 +717,10 @@ local void scan_tree (s, tree, max_code)
714
717
  int min_count = 4; /* min repeat count */
715
718
 
716
719
  if (nextlen == 0) max_count = 138, min_count = 3;
717
- tree[max_code+1].Len = (ush)0xffff; /* guard */
720
+ tree[max_code + 1].Len = (ush)0xffff; /* guard */
718
721
 
719
722
  for (n = 0; n <= max_code; n++) {
720
- curlen = nextlen; nextlen = tree[n+1].Len;
723
+ curlen = nextlen; nextlen = tree[n + 1].Len;
721
724
  if (++count < max_count && curlen == nextlen) {
722
725
  continue;
723
726
  } else if (count < min_count) {
@@ -745,11 +748,7 @@ local void scan_tree (s, tree, max_code)
745
748
  * Send a literal or distance tree in compressed form, using the codes in
746
749
  * bl_tree.
747
750
  */
748
- local void send_tree (s, tree, max_code)
749
- deflate_state *s;
750
- ct_data *tree; /* the tree to be scanned */
751
- int max_code; /* and its largest code of non zero frequency */
752
- {
751
+ local void send_tree(deflate_state *s, ct_data *tree, int max_code) {
753
752
  int n; /* iterates over all tree elements */
754
753
  int prevlen = -1; /* last emitted length */
755
754
  int curlen; /* length of current code */
@@ -758,11 +757,11 @@ local void send_tree (s, tree, max_code)
758
757
  int max_count = 7; /* max repeat count */
759
758
  int min_count = 4; /* min repeat count */
760
759
 
761
- /* tree[max_code+1].Len = -1; */ /* guard already set */
760
+ /* tree[max_code + 1].Len = -1; */ /* guard already set */
762
761
  if (nextlen == 0) max_count = 138, min_count = 3;
763
762
 
764
763
  for (n = 0; n <= max_code; n++) {
765
- curlen = nextlen; nextlen = tree[n+1].Len;
764
+ curlen = nextlen; nextlen = tree[n + 1].Len;
766
765
  if (++count < max_count && curlen == nextlen) {
767
766
  continue;
768
767
  } else if (count < min_count) {
@@ -773,13 +772,13 @@ local void send_tree (s, tree, max_code)
773
772
  send_code(s, curlen, s->bl_tree); count--;
774
773
  }
775
774
  Assert(count >= 3 && count <= 6, " 3_6?");
776
- send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
775
+ send_code(s, REP_3_6, s->bl_tree); send_bits(s, count - 3, 2);
777
776
 
778
777
  } else if (count <= 10) {
779
- send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
778
+ send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count - 3, 3);
780
779
 
781
780
  } else {
782
- send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
781
+ send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count - 11, 7);
783
782
  }
784
783
  count = 0; prevlen = curlen;
785
784
  if (nextlen == 0) {
@@ -796,9 +795,7 @@ local void send_tree (s, tree, max_code)
796
795
  * Construct the Huffman tree for the bit lengths and return the index in
797
796
  * bl_order of the last bit length code to send.
798
797
  */
799
- local int build_bl_tree(s)
800
- deflate_state *s;
801
- {
798
+ local int build_bl_tree(deflate_state *s) {
802
799
  int max_blindex; /* index of last bit length code of non zero freq */
803
800
 
804
801
  /* Determine the bit length frequencies for literal and distance trees */
@@ -807,8 +804,8 @@ local int build_bl_tree(s)
807
804
 
808
805
  /* Build the bit length tree: */
809
806
  build_tree(s, (tree_desc *)(&(s->bl_desc)));
810
- /* opt_len now includes the length of the tree representations, except
811
- * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
807
+ /* opt_len now includes the length of the tree representations, except the
808
+ * lengths of the bit lengths codes and the 5 + 5 + 4 bits for the counts.
812
809
  */
813
810
 
814
811
  /* Determine the number of bit length codes to send. The pkzip format
@@ -819,7 +816,7 @@ local int build_bl_tree(s)
819
816
  if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
820
817
  }
821
818
  /* Update opt_len to include the bit length tree and counts */
822
- s->opt_len += 3*((ulg)max_blindex+1) + 5+5+4;
819
+ s->opt_len += 3*((ulg)max_blindex + 1) + 5 + 5 + 4;
823
820
  Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
824
821
  s->opt_len, s->static_len));
825
822
 
@@ -831,42 +828,36 @@ local int build_bl_tree(s)
831
828
  * lengths of the bit length codes, the literal tree and the distance tree.
832
829
  * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
833
830
  */
834
- local void send_all_trees(s, lcodes, dcodes, blcodes)
835
- deflate_state *s;
836
- int lcodes, dcodes, blcodes; /* number of codes for each tree */
837
- {
831
+ local void send_all_trees(deflate_state *s, int lcodes, int dcodes,
832
+ int blcodes) {
838
833
  int rank; /* index in bl_order */
839
834
 
840
835
  Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
841
836
  Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
842
837
  "too many codes");
843
838
  Tracev((stderr, "\nbl counts: "));
844
- send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
845
- send_bits(s, dcodes-1, 5);
846
- send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
839
+ send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
840
+ send_bits(s, dcodes - 1, 5);
841
+ send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
847
842
  for (rank = 0; rank < blcodes; rank++) {
848
843
  Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
849
844
  send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
850
845
  }
851
846
  Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
852
847
 
853
- send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
848
+ send_tree(s, (ct_data *)s->dyn_ltree, lcodes - 1); /* literal tree */
854
849
  Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
855
850
 
856
- send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
851
+ send_tree(s, (ct_data *)s->dyn_dtree, dcodes - 1); /* distance tree */
857
852
  Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
858
853
  }
859
854
 
860
855
  /* ===========================================================================
861
856
  * Send a stored block
862
857
  */
863
- void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
864
- deflate_state *s;
865
- charf *buf; /* input block */
866
- ulg stored_len; /* length of input block */
867
- int last; /* one if this is the last block for a file */
868
- {
869
- send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */
858
+ void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf,
859
+ ulg stored_len, int last) {
860
+ send_bits(s, (STORED_BLOCK<<1) + last, 3); /* send block type */
870
861
  bi_windup(s); /* align on byte boundary */
871
862
  put_short(s, (ush)stored_len);
872
863
  put_short(s, (ush)~stored_len);
@@ -877,16 +868,14 @@ void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
877
868
  s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
878
869
  s->compressed_len += (stored_len + 4) << 3;
879
870
  s->bits_sent += 2*16;
880
- s->bits_sent += stored_len<<3;
871
+ s->bits_sent += stored_len << 3;
881
872
  #endif
882
873
  }
883
874
 
884
875
  /* ===========================================================================
885
876
  * Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
886
877
  */
887
- void ZLIB_INTERNAL _tr_flush_bits(s)
888
- deflate_state *s;
889
- {
878
+ void ZLIB_INTERNAL _tr_flush_bits(deflate_state *s) {
890
879
  bi_flush(s);
891
880
  }
892
881
 
@@ -894,9 +883,7 @@ void ZLIB_INTERNAL _tr_flush_bits(s)
894
883
  * Send one empty static block to give enough lookahead for inflate.
895
884
  * This takes 10 bits, of which 7 may remain in the bit buffer.
896
885
  */
897
- void ZLIB_INTERNAL _tr_align(s)
898
- deflate_state *s;
899
- {
886
+ void ZLIB_INTERNAL _tr_align(deflate_state *s) {
900
887
  send_bits(s, STATIC_TREES<<1, 3);
901
888
  send_code(s, END_BLOCK, static_ltree);
902
889
  #ifdef ZLIB_DEBUG
@@ -905,16 +892,108 @@ void ZLIB_INTERNAL _tr_align(s)
905
892
  bi_flush(s);
906
893
  }
907
894
 
895
+ /* ===========================================================================
896
+ * Send the block data compressed using the given Huffman trees
897
+ */
898
+ local void compress_block(deflate_state *s, const ct_data *ltree,
899
+ const ct_data *dtree) {
900
+ unsigned dist; /* distance of matched string */
901
+ int lc; /* match length or unmatched char (if dist == 0) */
902
+ unsigned sx = 0; /* running index in symbol buffers */
903
+ unsigned code; /* the code to send */
904
+ int extra; /* number of extra bits to send */
905
+
906
+ if (s->sym_next != 0) do {
907
+ #ifdef LIT_MEM
908
+ dist = s->d_buf[sx];
909
+ lc = s->l_buf[sx++];
910
+ #else
911
+ dist = s->sym_buf[sx++] & 0xff;
912
+ dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
913
+ lc = s->sym_buf[sx++];
914
+ #endif
915
+ if (dist == 0) {
916
+ send_code(s, lc, ltree); /* send a literal byte */
917
+ Tracecv(isgraph(lc), (stderr," '%c' ", lc));
918
+ } else {
919
+ /* Here, lc is the match length - MIN_MATCH */
920
+ code = _length_code[lc];
921
+ send_code(s, code + LITERALS + 1, ltree); /* send length code */
922
+ extra = extra_lbits[code];
923
+ if (extra != 0) {
924
+ lc -= base_length[code];
925
+ send_bits(s, lc, extra); /* send the extra length bits */
926
+ }
927
+ dist--; /* dist is now the match distance - 1 */
928
+ code = d_code(dist);
929
+ Assert (code < D_CODES, "bad d_code");
930
+
931
+ send_code(s, code, dtree); /* send the distance code */
932
+ extra = extra_dbits[code];
933
+ if (extra != 0) {
934
+ dist -= (unsigned)base_dist[code];
935
+ send_bits(s, dist, extra); /* send the extra distance bits */
936
+ }
937
+ } /* literal or match pair ? */
938
+
939
+ /* Check for no overlay of pending_buf on needed symbols */
940
+ #ifdef LIT_MEM
941
+ Assert(s->pending < 2 * (s->lit_bufsize + sx), "pendingBuf overflow");
942
+ #else
943
+ Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
944
+ #endif
945
+
946
+ } while (sx < s->sym_next);
947
+
948
+ send_code(s, END_BLOCK, ltree);
949
+ }
950
+
951
+ /* ===========================================================================
952
+ * Check if the data type is TEXT or BINARY, using the following algorithm:
953
+ * - TEXT if the two conditions below are satisfied:
954
+ * a) There are no non-portable control characters belonging to the
955
+ * "block list" (0..6, 14..25, 28..31).
956
+ * b) There is at least one printable character belonging to the
957
+ * "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
958
+ * - BINARY otherwise.
959
+ * - The following partially-portable control characters form a
960
+ * "gray list" that is ignored in this detection algorithm:
961
+ * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
962
+ * IN assertion: the fields Freq of dyn_ltree are set.
963
+ */
964
+ local int detect_data_type(deflate_state *s) {
965
+ /* block_mask is the bit mask of block-listed bytes
966
+ * set bits 0..6, 14..25, and 28..31
967
+ * 0xf3ffc07f = binary 11110011111111111100000001111111
968
+ */
969
+ unsigned long block_mask = 0xf3ffc07fUL;
970
+ int n;
971
+
972
+ /* Check for non-textual ("block-listed") bytes. */
973
+ for (n = 0; n <= 31; n++, block_mask >>= 1)
974
+ if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0))
975
+ return Z_BINARY;
976
+
977
+ /* Check for textual ("allow-listed") bytes. */
978
+ if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
979
+ || s->dyn_ltree[13].Freq != 0)
980
+ return Z_TEXT;
981
+ for (n = 32; n < LITERALS; n++)
982
+ if (s->dyn_ltree[n].Freq != 0)
983
+ return Z_TEXT;
984
+
985
+ /* There are no "block-listed" or "allow-listed" bytes:
986
+ * this stream either is empty or has tolerated ("gray-listed") bytes only.
987
+ */
988
+ return Z_BINARY;
989
+ }
990
+
908
991
  /* ===========================================================================
909
992
  * Determine the best encoding for the current block: dynamic trees, static
910
993
  * trees or store, and write out the encoded block.
911
994
  */
912
- void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
913
- deflate_state *s;
914
- charf *buf; /* input block, or NULL if too old */
915
- ulg stored_len; /* length of input block */
916
- int last; /* one if this is the last block for a file */
917
- {
995
+ void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf,
996
+ ulg stored_len, int last) {
918
997
  ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
919
998
  int max_blindex = 0; /* index of last bit length code of non zero freq */
920
999
 
@@ -943,14 +1022,17 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
943
1022
  max_blindex = build_bl_tree(s);
944
1023
 
945
1024
  /* Determine the best encoding. Compute the block lengths in bytes. */
946
- opt_lenb = (s->opt_len+3+7)>>3;
947
- static_lenb = (s->static_len+3+7)>>3;
1025
+ opt_lenb = (s->opt_len + 3 + 7) >> 3;
1026
+ static_lenb = (s->static_len + 3 + 7) >> 3;
948
1027
 
949
1028
  Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
950
1029
  opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
951
1030
  s->sym_next / 3));
952
1031
 
953
- if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
1032
+ #ifndef FORCE_STATIC
1033
+ if (static_lenb <= opt_lenb || s->strategy == Z_FIXED)
1034
+ #endif
1035
+ opt_lenb = static_lenb;
954
1036
 
955
1037
  } else {
956
1038
  Assert(buf != (char*)0, "lost buf");
@@ -960,7 +1042,7 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
960
1042
  #ifdef FORCE_STORED
961
1043
  if (buf != (char*)0) { /* force stored block */
962
1044
  #else
963
- if (stored_len+4 <= opt_lenb && buf != (char*)0) {
1045
+ if (stored_len + 4 <= opt_lenb && buf != (char*)0) {
964
1046
  /* 4: two words for the lengths */
965
1047
  #endif
966
1048
  /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
@@ -971,21 +1053,17 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
971
1053
  */
972
1054
  _tr_stored_block(s, buf, stored_len, last);
973
1055
 
974
- #ifdef FORCE_STATIC
975
- } else if (static_lenb >= 0) { /* force static trees */
976
- #else
977
- } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
978
- #endif
979
- send_bits(s, (STATIC_TREES<<1)+last, 3);
1056
+ } else if (static_lenb == opt_lenb) {
1057
+ send_bits(s, (STATIC_TREES<<1) + last, 3);
980
1058
  compress_block(s, (const ct_data *)static_ltree,
981
1059
  (const ct_data *)static_dtree);
982
1060
  #ifdef ZLIB_DEBUG
983
1061
  s->compressed_len += 3 + s->static_len;
984
1062
  #endif
985
1063
  } else {
986
- send_bits(s, (DYN_TREES<<1)+last, 3);
987
- send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
988
- max_blindex+1);
1064
+ send_bits(s, (DYN_TREES<<1) + last, 3);
1065
+ send_all_trees(s, s->l_desc.max_code + 1, s->d_desc.max_code + 1,
1066
+ max_blindex + 1);
989
1067
  compress_block(s, (const ct_data *)s->dyn_ltree,
990
1068
  (const ct_data *)s->dyn_dtree);
991
1069
  #ifdef ZLIB_DEBUG
@@ -1004,22 +1082,23 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
1004
1082
  s->compressed_len += 7; /* align on byte boundary */
1005
1083
  #endif
1006
1084
  }
1007
- Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1008
- s->compressed_len-7*last));
1085
+ Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len >> 3,
1086
+ s->compressed_len - 7*last));
1009
1087
  }
1010
1088
 
1011
1089
  /* ===========================================================================
1012
1090
  * Save the match info and tally the frequency counts. Return true if
1013
1091
  * the current block must be flushed.
1014
1092
  */
1015
- int ZLIB_INTERNAL _tr_tally (s, dist, lc)
1016
- deflate_state *s;
1017
- unsigned dist; /* distance of matched string */
1018
- unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
1019
- {
1020
- s->sym_buf[s->sym_next++] = dist;
1021
- s->sym_buf[s->sym_next++] = dist >> 8;
1022
- s->sym_buf[s->sym_next++] = lc;
1093
+ int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc) {
1094
+ #ifdef LIT_MEM
1095
+ s->d_buf[s->sym_next] = (ush)dist;
1096
+ s->l_buf[s->sym_next++] = (uch)lc;
1097
+ #else
1098
+ s->sym_buf[s->sym_next++] = (uch)dist;
1099
+ s->sym_buf[s->sym_next++] = (uch)(dist >> 8);
1100
+ s->sym_buf[s->sym_next++] = (uch)lc;
1101
+ #endif
1023
1102
  if (dist == 0) {
1024
1103
  /* lc is the unmatched char */
1025
1104
  s->dyn_ltree[lc].Freq++;
@@ -1031,152 +1110,8 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc)
1031
1110
  (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1032
1111
  (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1033
1112
 
1034
- s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
1113
+ s->dyn_ltree[_length_code[lc] + LITERALS + 1].Freq++;
1035
1114
  s->dyn_dtree[d_code(dist)].Freq++;
1036
1115
  }
1037
1116
  return (s->sym_next == s->sym_end);
1038
1117
  }
1039
-
1040
- /* ===========================================================================
1041
- * Send the block data compressed using the given Huffman trees
1042
- */
1043
- local void compress_block(s, ltree, dtree)
1044
- deflate_state *s;
1045
- const ct_data *ltree; /* literal tree */
1046
- const ct_data *dtree; /* distance tree */
1047
- {
1048
- unsigned dist; /* distance of matched string */
1049
- int lc; /* match length or unmatched char (if dist == 0) */
1050
- unsigned sx = 0; /* running index in sym_buf */
1051
- unsigned code; /* the code to send */
1052
- int extra; /* number of extra bits to send */
1053
-
1054
- if (s->sym_next != 0) do {
1055
- dist = s->sym_buf[sx++] & 0xff;
1056
- dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
1057
- lc = s->sym_buf[sx++];
1058
- if (dist == 0) {
1059
- send_code(s, lc, ltree); /* send a literal byte */
1060
- Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1061
- } else {
1062
- /* Here, lc is the match length - MIN_MATCH */
1063
- code = _length_code[lc];
1064
- send_code(s, code+LITERALS+1, ltree); /* send the length code */
1065
- extra = extra_lbits[code];
1066
- if (extra != 0) {
1067
- lc -= base_length[code];
1068
- send_bits(s, lc, extra); /* send the extra length bits */
1069
- }
1070
- dist--; /* dist is now the match distance - 1 */
1071
- code = d_code(dist);
1072
- Assert (code < D_CODES, "bad d_code");
1073
-
1074
- send_code(s, code, dtree); /* send the distance code */
1075
- extra = extra_dbits[code];
1076
- if (extra != 0) {
1077
- dist -= (unsigned)base_dist[code];
1078
- send_bits(s, dist, extra); /* send the extra distance bits */
1079
- }
1080
- } /* literal or match pair ? */
1081
-
1082
- /* Check that the overlay between pending_buf and sym_buf is ok: */
1083
- Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
1084
-
1085
- } while (sx < s->sym_next);
1086
-
1087
- send_code(s, END_BLOCK, ltree);
1088
- }
1089
-
1090
- /* ===========================================================================
1091
- * Check if the data type is TEXT or BINARY, using the following algorithm:
1092
- * - TEXT if the two conditions below are satisfied:
1093
- * a) There are no non-portable control characters belonging to the
1094
- * "block list" (0..6, 14..25, 28..31).
1095
- * b) There is at least one printable character belonging to the
1096
- * "allow list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
1097
- * - BINARY otherwise.
1098
- * - The following partially-portable control characters form a
1099
- * "gray list" that is ignored in this detection algorithm:
1100
- * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
1101
- * IN assertion: the fields Freq of dyn_ltree are set.
1102
- */
1103
- local int detect_data_type(s)
1104
- deflate_state *s;
1105
- {
1106
- /* block_mask is the bit mask of block-listed bytes
1107
- * set bits 0..6, 14..25, and 28..31
1108
- * 0xf3ffc07f = binary 11110011111111111100000001111111
1109
- */
1110
- unsigned long block_mask = 0xf3ffc07fUL;
1111
- int n;
1112
-
1113
- /* Check for non-textual ("block-listed") bytes. */
1114
- for (n = 0; n <= 31; n++, block_mask >>= 1)
1115
- if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0))
1116
- return Z_BINARY;
1117
-
1118
- /* Check for textual ("allow-listed") bytes. */
1119
- if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
1120
- || s->dyn_ltree[13].Freq != 0)
1121
- return Z_TEXT;
1122
- for (n = 32; n < LITERALS; n++)
1123
- if (s->dyn_ltree[n].Freq != 0)
1124
- return Z_TEXT;
1125
-
1126
- /* There are no "block-listed" or "allow-listed" bytes:
1127
- * this stream either is empty or has tolerated ("gray-listed") bytes only.
1128
- */
1129
- return Z_BINARY;
1130
- }
1131
-
1132
- /* ===========================================================================
1133
- * Reverse the first len bits of a code, using straightforward code (a faster
1134
- * method would use a table)
1135
- * IN assertion: 1 <= len <= 15
1136
- */
1137
- local unsigned bi_reverse(code, len)
1138
- unsigned code; /* the value to invert */
1139
- int len; /* its bit length */
1140
- {
1141
- register unsigned res = 0;
1142
- do {
1143
- res |= code & 1;
1144
- code >>= 1, res <<= 1;
1145
- } while (--len > 0);
1146
- return res >> 1;
1147
- }
1148
-
1149
- /* ===========================================================================
1150
- * Flush the bit buffer, keeping at most 7 bits in it.
1151
- */
1152
- local void bi_flush(s)
1153
- deflate_state *s;
1154
- {
1155
- if (s->bi_valid == 16) {
1156
- put_short(s, s->bi_buf);
1157
- s->bi_buf = 0;
1158
- s->bi_valid = 0;
1159
- } else if (s->bi_valid >= 8) {
1160
- put_byte(s, (Byte)s->bi_buf);
1161
- s->bi_buf >>= 8;
1162
- s->bi_valid -= 8;
1163
- }
1164
- }
1165
-
1166
- /* ===========================================================================
1167
- * Flush the bit buffer and align the output on a byte boundary
1168
- */
1169
- local void bi_windup(s)
1170
- deflate_state *s;
1171
- {
1172
- if (s->bi_valid > 8) {
1173
- put_short(s, s->bi_buf);
1174
- } else if (s->bi_valid > 0) {
1175
- put_byte(s, (Byte)s->bi_buf);
1176
- }
1177
- s->bi_buf = 0;
1178
- s->bi_valid = 0;
1179
- #ifdef ZLIB_DEBUG
1180
- s->bits_sent = (s->bits_sent+7) & ~7;
1181
- #endif
1182
- }