zlib-streams 1.0.7 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/Makefile +23 -8
  2. package/dist/zlib-streams-dev.wasm +0 -0
  3. package/dist/zlib-streams.js +145 -75
  4. package/dist/zlib-streams.wasm +0 -0
  5. package/package.json +2 -2
  6. package/src/README.md +61 -1
  7. package/src/chunkcopy.h +565 -0
  8. package/src/compare256.h +56 -0
  9. package/src/cpu_features.h +22 -0
  10. package/src/deflate.c +2349 -0
  11. package/src/deflate.h +420 -0
  12. package/src/deflate_crc32.c +22 -0
  13. package/src/inffast.h +26 -0
  14. package/src/{inffast.c → inffast_chunk.c} +127 -71
  15. package/src/inffast_chunk.h +42 -0
  16. package/src/inflate.c +69 -33
  17. package/src/insert_string.h +80 -0
  18. package/src/wasm/api/zlib-streams.js +145 -75
  19. package/src/wasm/wasm_stream_common.c +8 -4
  20. package/build/debug/src/inffast.d +0 -8
  21. package/build/debug/src/inffast.o +0 -0
  22. package/build/debug/src/inflate.d +0 -8
  23. package/build/debug/src/inflate.o +0 -0
  24. package/build/debug/src/inftrees.d +0 -6
  25. package/build/debug/src/inftrees.o +0 -0
  26. package/build/debug/src/zlib/adler32.d +0 -5
  27. package/build/debug/src/zlib/adler32.o +0 -0
  28. package/build/debug/src/zlib/contrib/infback9/infback9.d +0 -13
  29. package/build/debug/src/zlib/contrib/infback9/infback9.o +0 -0
  30. package/build/debug/src/zlib/contrib/infback9/inftree9.d +0 -7
  31. package/build/debug/src/zlib/contrib/infback9/inftree9.o +0 -0
  32. package/build/debug/src/zlib/crc32.d +0 -6
  33. package/build/debug/src/zlib/crc32.o +0 -0
  34. package/build/debug/src/zlib/trees.d +0 -7
  35. package/build/debug/src/zlib/trees.o +0 -0
  36. package/build/debug/src/zlib/zutil.d +0 -6
  37. package/build/debug/src/zlib/zutil.o +0 -0
  38. package/tmp/all_runs/payload_decompress_nowindow_debug__100k_lines.deflate64.log +0 -34
  39. package/tmp/all_runs/payload_decompress_nowindow_debug__100k_lines.deflate64.out +0 -100000
  40. package/tmp/all_runs/payload_decompress_nowindow_debug__10k_lines.deflate64.log +0 -2
  41. package/tmp/all_runs/payload_decompress_nowindow_debug__10k_lines.deflate64.out +0 -10000
  42. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_63k.deflate64.log +0 -1
  43. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_63k.deflate64.out +0 -0
  44. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_64k.deflate64.log +0 -1
  45. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_64k.deflate64.out +0 -0
  46. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_65k.deflate64.log +0 -2
  47. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_65k.deflate64.out +0 -0
  48. package/tmp/all_runs/payload_decompress_nowindow_debug__rand_block_gap.deflate64.log +0 -1
  49. package/tmp/all_runs/payload_decompress_nowindow_debug__rand_block_gap.deflate64.out +0 -0
  50. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_63k.deflate64.log +0 -1
  51. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_63k.deflate64.out +0 -0
  52. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_64k.deflate64.log +0 -1
  53. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_64k.deflate64.out +0 -0
  54. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_65k.deflate64.log +0 -2
  55. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_65k.deflate64.out +0 -0
  56. package/tmp/all_runs/payload_decompress_nowindow_debug__zeros_100k.deflate64.log +0 -2
  57. package/tmp/all_runs/payload_decompress_nowindow_debug__zeros_100k.deflate64.out +0 -0
  58. package/tmp/all_runs/payload_decompress_ref_debug__100k_lines.deflate64.log +0 -1
  59. package/tmp/all_runs/payload_decompress_ref_debug__100k_lines.deflate64.out +0 -100000
  60. package/tmp/all_runs/payload_decompress_ref_debug__10k_lines.deflate64.log +0 -1
  61. package/tmp/all_runs/payload_decompress_ref_debug__10k_lines.deflate64.out +0 -10000
  62. package/tmp/all_runs/payload_decompress_ref_debug__payload_63k.deflate64.log +0 -1
  63. package/tmp/all_runs/payload_decompress_ref_debug__payload_63k.deflate64.out +0 -0
  64. package/tmp/all_runs/payload_decompress_ref_debug__payload_64k.deflate64.log +0 -1
  65. package/tmp/all_runs/payload_decompress_ref_debug__payload_64k.deflate64.out +0 -0
  66. package/tmp/all_runs/payload_decompress_ref_debug__payload_65k.deflate64.log +0 -1
  67. package/tmp/all_runs/payload_decompress_ref_debug__payload_65k.deflate64.out +0 -0
  68. package/tmp/all_runs/payload_decompress_ref_debug__rand_block_gap.deflate64.log +0 -1
  69. package/tmp/all_runs/payload_decompress_ref_debug__rand_block_gap.deflate64.out +0 -0
  70. package/tmp/all_runs/payload_decompress_ref_debug__repeat_63k.deflate64.log +0 -1
  71. package/tmp/all_runs/payload_decompress_ref_debug__repeat_63k.deflate64.out +0 -0
  72. package/tmp/all_runs/payload_decompress_ref_debug__repeat_64k.deflate64.log +0 -1
  73. package/tmp/all_runs/payload_decompress_ref_debug__repeat_64k.deflate64.out +0 -0
  74. package/tmp/all_runs/payload_decompress_ref_debug__repeat_65k.deflate64.log +0 -1
  75. package/tmp/all_runs/payload_decompress_ref_debug__repeat_65k.deflate64.out +0 -0
  76. package/tmp/all_runs/payload_decompress_ref_debug__zeros_100k.deflate64.log +0 -1
  77. package/tmp/all_runs/payload_decompress_ref_debug__zeros_100k.deflate64.out +0 -0
  78. package/tmp/all_runs/payload_decompress_test_debug__100k_lines.deflate64.log +0 -34
  79. package/tmp/all_runs/payload_decompress_test_debug__100k_lines.deflate64.out +0 -100000
  80. package/tmp/all_runs/payload_decompress_test_debug__10k_lines.deflate64.log +0 -2
  81. package/tmp/all_runs/payload_decompress_test_debug__10k_lines.deflate64.out +0 -10000
  82. package/tmp/all_runs/payload_decompress_test_debug__payload_63k.deflate64.log +0 -1
  83. package/tmp/all_runs/payload_decompress_test_debug__payload_63k.deflate64.out +0 -0
  84. package/tmp/all_runs/payload_decompress_test_debug__payload_64k.deflate64.log +0 -1
  85. package/tmp/all_runs/payload_decompress_test_debug__payload_64k.deflate64.out +0 -0
  86. package/tmp/all_runs/payload_decompress_test_debug__payload_65k.deflate64.log +0 -2
  87. package/tmp/all_runs/payload_decompress_test_debug__payload_65k.deflate64.out +0 -0
  88. package/tmp/all_runs/payload_decompress_test_debug__rand_block_gap.deflate64.log +0 -1
  89. package/tmp/all_runs/payload_decompress_test_debug__rand_block_gap.deflate64.out +0 -0
  90. package/tmp/all_runs/payload_decompress_test_debug__repeat_63k.deflate64.log +0 -1
  91. package/tmp/all_runs/payload_decompress_test_debug__repeat_63k.deflate64.out +0 -0
  92. package/tmp/all_runs/payload_decompress_test_debug__repeat_64k.deflate64.log +0 -1
  93. package/tmp/all_runs/payload_decompress_test_debug__repeat_64k.deflate64.out +0 -0
  94. package/tmp/all_runs/payload_decompress_test_debug__repeat_65k.deflate64.log +0 -2
  95. package/tmp/all_runs/payload_decompress_test_debug__repeat_65k.deflate64.out +0 -0
  96. package/tmp/all_runs/payload_decompress_test_debug__zeros_100k.deflate64.log +0 -2
  97. package/tmp/all_runs/payload_decompress_test_debug__zeros_100k.deflate64.out +0 -0
  98. package/tmp/all_runs/wasm__100k_lines.deflate64.out +0 -100000
  99. package/tmp/all_runs/wasm__10k_lines.deflate64.out +0 -10000
  100. package/tmp/all_runs/wasm__payload_63k.deflate64.out +0 -0
  101. package/tmp/all_runs/wasm__payload_64k.deflate64.out +0 -0
  102. package/tmp/all_runs/wasm__payload_65k.deflate64.out +0 -0
  103. package/tmp/all_runs/wasm__rand_block_gap.deflate64.out +0 -0
  104. package/tmp/all_runs/wasm__repeat_63k.deflate64.out +0 -0
  105. package/tmp/all_runs/wasm__repeat_64k.deflate64.out +0 -0
  106. package/tmp/all_runs/wasm__repeat_65k.deflate64.out +0 -0
  107. package/tmp/all_runs/wasm__zeros_100k.deflate64.out +0 -0
  108. package/tmp/diagnose/diagnose_stream_mem.json +0 -4202
@@ -0,0 +1,565 @@
1
+ /* chunkcopy.h -- fast chunk copy and set operations
2
+ * Copyright (C) 2017 ARM, Inc.
3
+ * Copyright 2017 The Chromium Authors
4
+ * Use of this source code is governed by a BSD-style license that can be
5
+ * found in the Chromium source repository LICENSE file.
6
+ */
7
+
8
+ #ifndef CHUNKCOPY_H
9
+ #define CHUNKCOPY_H
10
+
11
+ #include <stdint.h>
12
+ #include <stddef.h>
13
+ #include "zutil.h"
14
+
15
+ #define Z_STATIC_ASSERT(name, assert) typedef char name[(assert) ? 1 : -1]
16
+
17
+ #if __STDC_VERSION__ >= 199901L
18
+ #define Z_RESTRICT restrict
19
+ #else
20
+ #define Z_RESTRICT
21
+ #endif
22
+
23
+ #if defined(__clang__) || defined(__GNUC__) || defined(__llvm__)
24
+ #define Z_BUILTIN_MEMCPY __builtin_memcpy
25
+ #define Z_BUILTIN_MEMSET __builtin_memset
26
+ #else
27
+ #define Z_BUILTIN_MEMCPY zmemcpy
28
+ #define Z_BUILTIN_MEMSET zmemset
29
+ #endif
30
+
31
+ #if defined(INFLATE_CHUNK_SIMD_NEON)
32
+ #include <arm_neon.h>
33
+ typedef uint8x16_t z_vec128i_t;
34
+ #elif defined(INFLATE_CHUNK_SIMD_SSE2)
35
+ #include <emmintrin.h>
36
+ typedef __m128i z_vec128i_t;
37
+ #elif defined(INFLATE_CHUNK_GENERIC)
38
+ typedef struct { uint8_t x[16]; } z_vec128i_t;
39
+ #else
40
+ #error chunkcopy.h inflate chunk SIMD is not defined for your build target
41
+ #endif
42
+
43
+ /*
44
+ * Suppress MSan errors about copying uninitialized bytes (crbug.com/1376033).
45
+ */
46
+ #define Z_DISABLE_MSAN
47
+ #if defined(__has_feature)
48
+ #if __has_feature(memory_sanitizer)
49
+ #undef Z_DISABLE_MSAN
50
+ #define Z_DISABLE_MSAN __attribute__((no_sanitize("memory")))
51
+ #endif
52
+ #endif
53
+
54
+ /*
55
+ * chunk copy type: the z_vec128i_t type size should be exactly 128-bits
56
+ * and equal to CHUNKCOPY_CHUNK_SIZE.
57
+ */
58
+ #define CHUNKCOPY_CHUNK_SIZE sizeof(z_vec128i_t)
59
+
60
+ Z_STATIC_ASSERT(vector_128_bits_wide,
61
+ CHUNKCOPY_CHUNK_SIZE == sizeof(int8_t) * 16);
62
+
63
+ /*
64
+ * Ask the compiler to perform a wide, unaligned load with a machine
65
+ * instruction appropriate for the z_vec128i_t type.
66
+ */
67
+ static inline z_vec128i_t loadchunk(
68
+ const unsigned char FAR* s) Z_DISABLE_MSAN {
69
+ z_vec128i_t v;
70
+ Z_BUILTIN_MEMCPY(&v, s, sizeof(v));
71
+ return v;
72
+ }
73
+
74
+ /*
75
+ * Ask the compiler to perform a wide, unaligned store with a machine
76
+ * instruction appropriate for the z_vec128i_t type.
77
+ */
78
+ static inline void storechunk(
79
+ unsigned char FAR* d,
80
+ const z_vec128i_t v) {
81
+ Z_BUILTIN_MEMCPY(d, &v, sizeof(v));
82
+ }
83
+
84
+ /*
85
+ * Perform a memcpy-like operation, assuming that length is non-zero and that
86
+ * it's OK to overwrite at least CHUNKCOPY_CHUNK_SIZE bytes of output even if
87
+ * the length is shorter than this.
88
+ *
89
+ * It also guarantees that it will properly unroll the data if the distance
90
+ * between `out` and `from` is at least CHUNKCOPY_CHUNK_SIZE, which we rely on
91
+ * in chunkcopy_relaxed().
92
+ *
93
+ * Aside from better memory bus utilisation, this means that short copies
94
+ * (CHUNKCOPY_CHUNK_SIZE bytes or fewer) will fall straight through the loop
95
+ * without iteration, which will hopefully make the branch prediction more
96
+ * reliable.
97
+ */
98
+ static inline unsigned char FAR* chunkcopy_core(
99
+ unsigned char FAR* out,
100
+ const unsigned char FAR* from,
101
+ unsigned len) Z_DISABLE_MSAN {
102
+ const int bump = (--len % CHUNKCOPY_CHUNK_SIZE) + 1;
103
+ storechunk(out, loadchunk(from));
104
+ out += bump;
105
+ from += bump;
106
+ len /= CHUNKCOPY_CHUNK_SIZE;
107
+ while (len-- > 0) {
108
+ storechunk(out, loadchunk(from));
109
+ out += CHUNKCOPY_CHUNK_SIZE;
110
+ from += CHUNKCOPY_CHUNK_SIZE;
111
+ }
112
+ return out;
113
+ }
114
+
115
+ /*
116
+ * Like chunkcopy_core(), but avoid writing beyond of legal output.
117
+ *
118
+ * Accepts an additional pointer to the end of safe output. A generic safe
119
+ * copy would use (out + len), but it's normally the case that the end of the
120
+ * output buffer is beyond the end of the current copy, and this can still be
121
+ * exploited.
122
+ */
123
+ static inline unsigned char FAR* chunkcopy_core_safe(
124
+ unsigned char FAR* out,
125
+ const unsigned char FAR* from,
126
+ unsigned len,
127
+ unsigned char FAR* limit) {
128
+ Assert(out + len <= limit, "chunk copy exceeds safety limit");
129
+ if ((limit - out) < (ptrdiff_t)CHUNKCOPY_CHUNK_SIZE) {
130
+ const unsigned char FAR* Z_RESTRICT rfrom = from;
131
+ Assert((uintptr_t)out - (uintptr_t)from >= len,
132
+ "invalid restrict in chunkcopy_core_safe");
133
+ Assert((uintptr_t)from - (uintptr_t)out >= len,
134
+ "invalid restrict in chunkcopy_core_safe");
135
+ if (len & 8) {
136
+ Z_BUILTIN_MEMCPY(out, rfrom, 8);
137
+ out += 8;
138
+ rfrom += 8;
139
+ }
140
+ if (len & 4) {
141
+ Z_BUILTIN_MEMCPY(out, rfrom, 4);
142
+ out += 4;
143
+ rfrom += 4;
144
+ }
145
+ if (len & 2) {
146
+ Z_BUILTIN_MEMCPY(out, rfrom, 2);
147
+ out += 2;
148
+ rfrom += 2;
149
+ }
150
+ if (len & 1) {
151
+ *out++ = *rfrom++;
152
+ }
153
+ return out;
154
+ }
155
+ return chunkcopy_core(out, from, len);
156
+ }
157
+
158
+ /*
159
+ * Perform short copies until distance can be rewritten as being at least
160
+ * CHUNKCOPY_CHUNK_SIZE.
161
+ *
162
+ * Assumes it's OK to overwrite at least the first 2*CHUNKCOPY_CHUNK_SIZE
163
+ * bytes of output even if the copy is shorter than this. This assumption
164
+ * holds within zlib inflate_fast(), which starts every iteration with at
165
+ * least 258 bytes of output space available (258 being the maximum length
166
+ * output from a single token; see inffast.c).
167
+ */
168
+ static inline unsigned char FAR* chunkunroll_relaxed(
169
+ unsigned char FAR* out,
170
+ unsigned FAR* dist,
171
+ unsigned FAR* len) Z_DISABLE_MSAN {
172
+ const unsigned char FAR* from = out - *dist;
173
+ while (*dist < *len && *dist < CHUNKCOPY_CHUNK_SIZE) {
174
+ storechunk(out, loadchunk(from));
175
+ out += *dist;
176
+ *len -= *dist;
177
+ *dist += *dist;
178
+ }
179
+ return out;
180
+ }
181
+
182
+ #if defined(INFLATE_CHUNK_SIMD_NEON)
183
+ /*
184
+ * v_load64_dup(): load *src as an unaligned 64-bit int and duplicate it in
185
+ * every 64-bit component of the 128-bit result (64-bit int splat).
186
+ */
187
+ static inline z_vec128i_t v_load64_dup(const void* src) {
188
+ return vcombine_u8(vld1_u8(src), vld1_u8(src));
189
+ }
190
+
191
+ /*
192
+ * v_load32_dup(): load *src as an unaligned 32-bit int and duplicate it in
193
+ * every 32-bit component of the 128-bit result (32-bit int splat).
194
+ */
195
+ static inline z_vec128i_t v_load32_dup(const void* src) {
196
+ int32_t i32;
197
+ Z_BUILTIN_MEMCPY(&i32, src, sizeof(i32));
198
+ return vreinterpretq_u8_s32(vdupq_n_s32(i32));
199
+ }
200
+
201
+ /*
202
+ * v_load16_dup(): load *src as an unaligned 16-bit int and duplicate it in
203
+ * every 16-bit component of the 128-bit result (16-bit int splat).
204
+ */
205
+ static inline z_vec128i_t v_load16_dup(const void* src) {
206
+ int16_t i16;
207
+ Z_BUILTIN_MEMCPY(&i16, src, sizeof(i16));
208
+ return vreinterpretq_u8_s16(vdupq_n_s16(i16));
209
+ }
210
+
211
+ /*
212
+ * v_load8_dup(): load the 8-bit int *src and duplicate it in every 8-bit
213
+ * component of the 128-bit result (8-bit int splat).
214
+ */
215
+ static inline z_vec128i_t v_load8_dup(const void* src) {
216
+ return vld1q_dup_u8((const uint8_t*)src);
217
+ }
218
+
219
+ /*
220
+ * v_store_128(): store the 128-bit vec in a memory destination (that might
221
+ * not be 16-byte aligned) void* out.
222
+ */
223
+ static inline void v_store_128(void* out, const z_vec128i_t vec) {
224
+ vst1q_u8(out, vec);
225
+ }
226
+
227
+ #elif defined(INFLATE_CHUNK_SIMD_SSE2)
228
+ /*
229
+ * v_load64_dup(): load *src as an unaligned 64-bit int and duplicate it in
230
+ * every 64-bit component of the 128-bit result (64-bit int splat).
231
+ */
232
+ static inline z_vec128i_t v_load64_dup(const void* src) {
233
+ int64_t i64;
234
+ Z_BUILTIN_MEMCPY(&i64, src, sizeof(i64));
235
+ return _mm_set1_epi64x(i64);
236
+ }
237
+
238
+ /*
239
+ * v_load32_dup(): load *src as an unaligned 32-bit int and duplicate it in
240
+ * every 32-bit component of the 128-bit result (32-bit int splat).
241
+ */
242
+ static inline z_vec128i_t v_load32_dup(const void* src) {
243
+ int32_t i32;
244
+ Z_BUILTIN_MEMCPY(&i32, src, sizeof(i32));
245
+ return _mm_set1_epi32(i32);
246
+ }
247
+
248
+ /*
249
+ * v_load16_dup(): load *src as an unaligned 16-bit int and duplicate it in
250
+ * every 16-bit component of the 128-bit result (16-bit int splat).
251
+ */
252
+ static inline z_vec128i_t v_load16_dup(const void* src) {
253
+ int16_t i16;
254
+ Z_BUILTIN_MEMCPY(&i16, src, sizeof(i16));
255
+ return _mm_set1_epi16(i16);
256
+ }
257
+
258
+ /*
259
+ * v_load8_dup(): load the 8-bit int *src and duplicate it in every 8-bit
260
+ * component of the 128-bit result (8-bit int splat).
261
+ */
262
+ static inline z_vec128i_t v_load8_dup(const void* src) {
263
+ return _mm_set1_epi8(*(const char*)src);
264
+ }
265
+
266
+ /*
267
+ * v_store_128(): store the 128-bit vec in a memory destination (that might
268
+ * not be 16-byte aligned) void* out.
269
+ */
270
+ static inline void v_store_128(void* out, const z_vec128i_t vec) {
271
+ _mm_storeu_si128((__m128i*)out, vec);
272
+ }
273
+ #elif defined(INFLATE_CHUNK_GENERIC)
274
+ /*
275
+ * Default implementations for chunk-copy functions rely on memcpy() being
276
+ * inlined by the compiler for best performance. This is most likely to work
277
+ * as expected when the length argument is constant (as is the case here) and
278
+ * the target supports unaligned loads and stores. Since that's not always a
279
+ * safe assumption, this may need extra compiler arguments such as
280
+ * `-mno-strict-align` or `-munaligned-access`, or the availability of
281
+ * extensions like SIMD.
282
+ */
283
+
284
+ /*
285
+ * v_load64_dup(): load *src as an unaligned 64-bit int and duplicate it in
286
+ * every 64-bit component of the 128-bit result (64-bit int splat).
287
+ */
288
+ static inline z_vec128i_t v_load64_dup(const void* src) {
289
+ int64_t in;
290
+ Z_BUILTIN_MEMCPY(&in, src, sizeof(in));
291
+ z_vec128i_t out;
292
+ for (int i = 0; i < sizeof(out); i += sizeof(in)) {
293
+ Z_BUILTIN_MEMCPY((uint8_t*)&out + i, &in, sizeof(in));
294
+ }
295
+ return out;
296
+ }
297
+
298
+ /*
299
+ * v_load32_dup(): load *src as an unaligned 32-bit int and duplicate it in
300
+ * every 32-bit component of the 128-bit result (32-bit int splat).
301
+ */
302
+ static inline z_vec128i_t v_load32_dup(const void* src) {
303
+ int32_t in;
304
+ Z_BUILTIN_MEMCPY(&in, src, sizeof(in));
305
+ z_vec128i_t out;
306
+ for (int i = 0; i < sizeof(out); i += sizeof(in)) {
307
+ Z_BUILTIN_MEMCPY((uint8_t*)&out + i, &in, sizeof(in));
308
+ }
309
+ return out;
310
+ }
311
+
312
+ /*
313
+ * v_load16_dup(): load *src as an unaligned 16-bit int and duplicate it in
314
+ * every 16-bit component of the 128-bit result (16-bit int splat).
315
+ */
316
+ static inline z_vec128i_t v_load16_dup(const void* src) {
317
+ int16_t in;
318
+ Z_BUILTIN_MEMCPY(&in, src, sizeof(in));
319
+ z_vec128i_t out;
320
+ for (int i = 0; i < sizeof(out); i += sizeof(in)) {
321
+ Z_BUILTIN_MEMCPY((uint8_t*)&out + i, &in, sizeof(in));
322
+ }
323
+ return out;
324
+ }
325
+
326
+ /*
327
+ * v_load8_dup(): load the 8-bit int *src and duplicate it in every 8-bit
328
+ * component of the 128-bit result (8-bit int splat).
329
+ */
330
+ static inline z_vec128i_t v_load8_dup(const void* src) {
331
+ int8_t in = *(const uint8_t*)src;
332
+ z_vec128i_t out;
333
+ Z_BUILTIN_MEMSET(&out, in, sizeof(out));
334
+ return out;
335
+ }
336
+
337
+ /*
338
+ * v_store_128(): store the 128-bit vec in a memory destination (that might
339
+ * not be 16-byte aligned) void* out.
340
+ */
341
+ static inline void v_store_128(void* out, const z_vec128i_t vec) {
342
+ Z_BUILTIN_MEMCPY(out, &vec, sizeof(vec));
343
+ }
344
+ #endif
345
+
346
+ /*
347
+ * Perform an overlapping copy which behaves as a memset() operation, but
348
+ * supporting periods other than one, and assume that length is non-zero and
349
+ * that it's OK to overwrite at least CHUNKCOPY_CHUNK_SIZE*3 bytes of output
350
+ * even if the length is shorter than this.
351
+ */
352
+ static inline unsigned char FAR* chunkset_core(
353
+ unsigned char FAR* out,
354
+ unsigned period,
355
+ unsigned len) {
356
+ z_vec128i_t v;
357
+ const int bump = ((len - 1) % sizeof(v)) + 1;
358
+
359
+ switch (period) {
360
+ case 1:
361
+ v = v_load8_dup(out - 1);
362
+ v_store_128(out, v);
363
+ out += bump;
364
+ len -= bump;
365
+ while (len > 0) {
366
+ v_store_128(out, v);
367
+ out += sizeof(v);
368
+ len -= sizeof(v);
369
+ }
370
+ return out;
371
+ case 2:
372
+ v = v_load16_dup(out - 2);
373
+ v_store_128(out, v);
374
+ out += bump;
375
+ len -= bump;
376
+ if (len > 0) {
377
+ v = v_load16_dup(out - 2);
378
+ do {
379
+ v_store_128(out, v);
380
+ out += sizeof(v);
381
+ len -= sizeof(v);
382
+ } while (len > 0);
383
+ }
384
+ return out;
385
+ case 4:
386
+ v = v_load32_dup(out - 4);
387
+ v_store_128(out, v);
388
+ out += bump;
389
+ len -= bump;
390
+ if (len > 0) {
391
+ v = v_load32_dup(out - 4);
392
+ do {
393
+ v_store_128(out, v);
394
+ out += sizeof(v);
395
+ len -= sizeof(v);
396
+ } while (len > 0);
397
+ }
398
+ return out;
399
+ case 8:
400
+ v = v_load64_dup(out - 8);
401
+ v_store_128(out, v);
402
+ out += bump;
403
+ len -= bump;
404
+ if (len > 0) {
405
+ v = v_load64_dup(out - 8);
406
+ do {
407
+ v_store_128(out, v);
408
+ out += sizeof(v);
409
+ len -= sizeof(v);
410
+ } while (len > 0);
411
+ }
412
+ return out;
413
+ }
414
+ out = chunkunroll_relaxed(out, &period, &len);
415
+ return chunkcopy_core(out, out - period, len);
416
+ }
417
+
418
+ /*
419
+ * Perform a memcpy-like operation, but assume that length is non-zero and that
420
+ * it's OK to overwrite at least CHUNKCOPY_CHUNK_SIZE bytes of output even if
421
+ * the length is shorter than this.
422
+ *
423
+ * Unlike chunkcopy_core() above, no guarantee is made regarding the behaviour
424
+ * of overlapping buffers, regardless of the distance between the pointers.
425
+ * This is reflected in the `restrict`-qualified pointers, allowing the
426
+ * compiler to re-order loads and stores.
427
+ */
428
+ static inline unsigned char FAR* chunkcopy_relaxed(
429
+ unsigned char FAR* Z_RESTRICT out,
430
+ const unsigned char FAR* Z_RESTRICT from,
431
+ unsigned len) {
432
+ Assert((uintptr_t)out - (uintptr_t)from >= len,
433
+ "invalid restrict in chunkcopy_relaxed");
434
+ Assert((uintptr_t)from - (uintptr_t)out >= len,
435
+ "invalid restrict in chunkcopy_relaxed");
436
+ return chunkcopy_core(out, from, len);
437
+ }
438
+
439
+ /*
440
+ * Like chunkcopy_relaxed(), but avoid writing beyond of legal output.
441
+ *
442
+ * Unlike chunkcopy_core_safe() above, no guarantee is made regarding the
443
+ * behaviour of overlapping buffers, regardless of the distance between the
444
+ * pointers. This is reflected in the `restrict`-qualified pointers, allowing
445
+ * the compiler to re-order loads and stores.
446
+ *
447
+ * Accepts an additional pointer to the end of safe output. A generic safe
448
+ * copy would use (out + len), but it's normally the case that the end of the
449
+ * output buffer is beyond the end of the current copy, and this can still be
450
+ * exploited.
451
+ */
452
+ static inline unsigned char FAR* chunkcopy_safe(
453
+ unsigned char FAR* out,
454
+ const unsigned char FAR* Z_RESTRICT from,
455
+ unsigned len,
456
+ unsigned char FAR* limit) {
457
+ Assert(out + len <= limit, "chunk copy exceeds safety limit");
458
+ Assert((uintptr_t)out - (uintptr_t)from >= len,
459
+ "invalid restrict in chunkcopy_safe");
460
+ Assert((uintptr_t)from - (uintptr_t)out >= len,
461
+ "invalid restrict in chunkcopy_safe");
462
+
463
+ return chunkcopy_core_safe(out, from, len, limit);
464
+ }
465
+
466
+ /*
467
+ * Perform chunky copy within the same buffer, where the source and destination
468
+ * may potentially overlap.
469
+ *
470
+ * Assumes that len > 0 on entry, and that it's safe to write at least
471
+ * CHUNKCOPY_CHUNK_SIZE*3 bytes to the output.
472
+ */
473
+ static inline unsigned char FAR* chunkcopy_lapped_relaxed(
474
+ unsigned char FAR* out,
475
+ unsigned dist,
476
+ unsigned len) {
477
+ if (dist < len && dist < CHUNKCOPY_CHUNK_SIZE) {
478
+ return chunkset_core(out, dist, len);
479
+ }
480
+ return chunkcopy_core(out, out - dist, len);
481
+ }
482
+
483
+ /*
484
+ * Behave like chunkcopy_lapped_relaxed(), but avoid writing beyond of legal
485
+ * output.
486
+ *
487
+ * Accepts an additional pointer to the end of safe output. A generic safe
488
+ * copy would use (out + len), but it's normally the case that the end of the
489
+ * output buffer is beyond the end of the current copy, and this can still be
490
+ * exploited.
491
+ */
492
+ static inline unsigned char FAR* chunkcopy_lapped_safe(
493
+ unsigned char FAR* out,
494
+ unsigned dist,
495
+ unsigned len,
496
+ unsigned char FAR* limit) {
497
+ Assert(out + len <= limit, "chunk copy exceeds safety limit");
498
+ if ((limit - out) < (ptrdiff_t)(3 * CHUNKCOPY_CHUNK_SIZE)) {
499
+ /* TODO(cavalcantii): try harder to optimise this */
500
+ while (len-- > 0) {
501
+ *out = *(out - dist);
502
+ out++;
503
+ }
504
+ return out;
505
+ }
506
+ return chunkcopy_lapped_relaxed(out, dist, len);
507
+ }
508
+
509
+ /* TODO(cavalcanti): see crbug.com/1110083. */
510
+ static inline unsigned char FAR* chunkcopy_safe_ugly(unsigned char FAR* out,
511
+ unsigned dist,
512
+ unsigned len,
513
+ unsigned char FAR* limit) {
514
+ #if defined(__GNUC__) && !defined(__clang__)
515
+ /* Speed is the same as using chunkcopy_safe
516
+ w/ GCC on ARM (tested gcc 6.3 and 7.5) and avoids
517
+ undefined behavior.
518
+ */
519
+ return chunkcopy_core_safe(out, out - dist, len, limit);
520
+ #elif defined(__clang__) && defined(ARMV8_OS_ANDROID) && !defined(__aarch64__)
521
+ /* Seems to perform better on 32bit (i.e. Android). */
522
+ return chunkcopy_core_safe(out, out - dist, len, limit);
523
+ #else
524
+ /* Seems to perform better on 64bit. */
525
+ return chunkcopy_lapped_safe(out, dist, len, limit);
526
+ #endif
527
+ }
528
+
529
+ /*
530
+ * The chunk-copy code above deals with writing the decoded DEFLATE data to
531
+ * the output with SIMD methods to increase decode speed. Reading the input
532
+ * to the DEFLATE decoder with a wide, SIMD method can also increase decode
533
+ * speed. This option is supported on little endian machines, and reads the
534
+ * input data in 64-bit (8 byte) chunks.
535
+ */
536
+
537
+ #ifdef INFLATE_CHUNK_READ_64LE
538
+ /*
539
+ * Buffer the input in a uint64_t (8 bytes) in the wide input reading case.
540
+ */
541
+ typedef uint64_t inflate_holder_t;
542
+
543
+ /*
544
+ * Ask the compiler to perform a wide, unaligned load of a uint64_t using a
545
+ * machine instruction appropriate for the uint64_t type.
546
+ */
547
+ static inline inflate_holder_t read64le(const unsigned char FAR *in) {
548
+ inflate_holder_t input;
549
+ Z_BUILTIN_MEMCPY(&input, in, sizeof(input));
550
+ return input;
551
+ }
552
+ #else
553
+ /*
554
+ * Otherwise, buffer the input bits using zlib's default input buffer type.
555
+ */
556
+ typedef unsigned long inflate_holder_t;
557
+
558
+ #endif /* INFLATE_CHUNK_READ_64LE */
559
+
560
+ #undef Z_STATIC_ASSERT
561
+ #undef Z_RESTRICT
562
+ #undef Z_BUILTIN_MEMCPY
563
+ #undef Z_DISABLE_MSAN
564
+
565
+ #endif /* CHUNKCOPY_H */
@@ -0,0 +1,56 @@
1
+ /* compare256.h
2
+ *
3
+ * This does 256-byte match comparison for deflate's longest_match.
4
+ *
5
+ * Copyright 2026 The Chromium Authors
6
+ * Use of this source code is governed by a BSD-style license that can be
7
+ * found in the Chromium source repository LICENSE file.
8
+ */
9
+ #ifndef COMPARE256_H
10
+ #define COMPARE256_H
11
+
12
+ #include <stdint.h>
13
+ #include <string.h>
14
+ #if defined(_MSC_VER) && !defined(__clang__)
15
+ #include <intrin.h>
16
+ #endif
17
+
18
+ /* Safe unaligned 16-bit load. Compilers optimize this into a single load
19
+ * instruction. */
20
+ static inline uint16_t read16(const void* p) {
21
+ uint16_t v;
22
+ memcpy(&v, p, sizeof(v));
23
+ return v;
24
+ }
25
+
26
+ /* Finds the byte offset (0..7) of the first difference in non-zero XOR mask
27
+ * `x`:
28
+ */
29
+ static inline int compare256_diff(uint64_t x) {
30
+ #if defined(_MSC_VER) && !defined(__clang__)
31
+ unsigned long i;
32
+ _BitScanForward64(&i, x);
33
+ return (int)i / 8;
34
+ #else
35
+ return __builtin_ctzll(x) / 8;
36
+ #endif
37
+ }
38
+
39
+ /* Returns the number of matching leading bytes (0 to 256) between src0 and
40
+ * src1. Compares 8 bytes per iteration with early exit. */
41
+ static inline int compare256(const unsigned char* src0,
42
+ const unsigned char* src1) {
43
+ int len = 0;
44
+ do {
45
+ uint64_t a, b, x;
46
+ memcpy(&a, src0 + len, sizeof(a));
47
+ memcpy(&b, src1 + len, sizeof(b));
48
+ x = a ^ b;
49
+ if (x)
50
+ return len + compare256_diff(x);
51
+ len += (int)sizeof(a);
52
+ } while (len < 256);
53
+ return 256;
54
+ }
55
+
56
+ #endif /* COMPARE256_H */
@@ -0,0 +1,22 @@
1
+ /* cpu_features.h -- Processor features detection.
2
+ *
3
+ * Copyright 2018 The Chromium Authors
4
+ * Use of this source code is governed by a BSD-style license that can be
5
+ * found in the Chromium source repository LICENSE file.
6
+ */
7
+
8
+ #include "zlib.h"
9
+
10
+ /* TODO(cavalcantii): remove checks for x86_flags on deflate.
11
+ */
12
+ extern int arm_cpu_enable_crc32;
13
+ extern int arm_cpu_enable_pmull;
14
+ extern int x86_cpu_enable_sse2;
15
+ extern int x86_cpu_enable_ssse3;
16
+ extern int x86_cpu_enable_simd;
17
+ extern int x86_cpu_enable_avx512;
18
+
19
+ extern int riscv_cpu_enable_rvv;
20
+ extern int riscv_cpu_enable_vclmul;
21
+
22
+ void cpu_check_features(void);