@cloudcare/browser-rum 2.2.15 → 3.0.0

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 (166) hide show
  1. package/bundle/dataflux-rum.js +1 -1
  2. package/cjs/boot/buildEnv.js +1 -1
  3. package/cjs/boot/recorderApi.js +161 -0
  4. package/cjs/boot/rum.entry.js +6 -1
  5. package/cjs/boot/rum.js +15 -5
  6. package/cjs/boot/rumPublicApi.js +8 -4
  7. package/cjs/boot/startRecording.js +55 -0
  8. package/cjs/constants.js +52 -0
  9. package/cjs/domain/assembly.js +1 -7
  10. package/cjs/domain/configuration.js +3 -2
  11. package/cjs/domain/contexts/foregroundContexts.js +138 -0
  12. package/cjs/domain/contexts/internalContext.js +1 -1
  13. package/cjs/domain/contexts/urlContexts.js +9 -2
  14. package/cjs/domain/domMutationCollection.js +4 -5
  15. package/cjs/domain/initViewportObservable.js +52 -0
  16. package/cjs/domain/locationChangeObservable.js +12 -26
  17. package/cjs/domain/performanceCollection.js +2 -13
  18. package/cjs/domain/replay/record/elementsScrollPositions.js +27 -0
  19. package/cjs/domain/replay/record/index.js +41 -0
  20. package/cjs/domain/replay/record/mutationBatch.js +58 -0
  21. package/cjs/domain/replay/record/mutationObserver.js +383 -0
  22. package/cjs/domain/replay/record/observers.js +473 -0
  23. package/cjs/domain/replay/record/privacy.js +256 -0
  24. package/cjs/domain/replay/record/record.js +174 -0
  25. package/cjs/domain/replay/record/serializationUtils.js +137 -0
  26. package/cjs/domain/replay/record/serialize.js +488 -0
  27. package/cjs/domain/replay/record/shadowRootsController.js +61 -0
  28. package/cjs/domain/replay/record/utils.js +53 -0
  29. package/cjs/domain/replay/record/viewports.js +104 -0
  30. package/cjs/domain/replay/replayStats.js +83 -0
  31. package/cjs/domain/replay/segmentCollection/buildReplayPayload.js +43 -0
  32. package/cjs/domain/replay/segmentCollection/deflateWorker.js +4399 -0
  33. package/cjs/domain/replay/segmentCollection/index.js +33 -0
  34. package/cjs/domain/replay/segmentCollection/segment.js +101 -0
  35. package/cjs/domain/replay/segmentCollection/segmentCollection.js +179 -0
  36. package/cjs/domain/replay/segmentCollection/startDeflateWorker.js +126 -0
  37. package/cjs/domain/requestCollection.js +35 -17
  38. package/cjs/domain/rumEventsCollection/actions/actionCollection.js +12 -4
  39. package/cjs/domain/rumEventsCollection/actions/clickChain.js +3 -3
  40. package/cjs/domain/rumEventsCollection/actions/computeFrustration.js +6 -5
  41. package/cjs/domain/rumEventsCollection/actions/getActionNameFromElement.js +8 -6
  42. package/cjs/domain/rumEventsCollection/actions/getSelectorsFromElement.js +111 -75
  43. package/cjs/domain/rumEventsCollection/actions/listenActionEvents.js +22 -28
  44. package/cjs/domain/rumEventsCollection/actions/trackClickActions.js +94 -54
  45. package/cjs/domain/rumEventsCollection/error/errorCollection.js +13 -5
  46. package/cjs/domain/rumEventsCollection/view/trackViewEventCounts.js +45 -0
  47. package/cjs/domain/rumEventsCollection/view/trackViewMetrics.js +1 -19
  48. package/cjs/domain/rumEventsCollection/view/trackViews.js +21 -18
  49. package/cjs/domain/rumEventsCollection/view/viewCollection.js +12 -9
  50. package/cjs/domain/rumSessionManager.js +6 -3
  51. package/cjs/domain/tracing/getDocumentTraceId.js +2 -10
  52. package/cjs/domain/trackEventCounts.js +10 -2
  53. package/cjs/domain/waitPageActivityEnd.js +5 -1
  54. package/cjs/transport/startRumBatch.js +5 -7
  55. package/cjs/types/index.js +18 -0
  56. package/cjs/types/sessionReplayConstants.js +56 -0
  57. package/esm/boot/buildEnv.js +1 -1
  58. package/esm/boot/recorderApi.js +149 -0
  59. package/esm/boot/rum.entry.js +4 -1
  60. package/esm/boot/rum.js +15 -6
  61. package/esm/boot/rumPublicApi.js +8 -4
  62. package/esm/boot/startRecording.js +44 -0
  63. package/esm/constants.js +31 -0
  64. package/esm/domain/assembly.js +1 -7
  65. package/esm/domain/configuration.js +4 -3
  66. package/esm/domain/contexts/foregroundContexts.js +122 -0
  67. package/esm/domain/contexts/internalContext.js +1 -1
  68. package/esm/domain/contexts/urlContexts.js +10 -3
  69. package/esm/domain/domMutationCollection.js +5 -6
  70. package/esm/domain/initViewportObservable.js +39 -0
  71. package/esm/domain/locationChangeObservable.js +13 -27
  72. package/esm/domain/performanceCollection.js +1 -12
  73. package/esm/domain/replay/record/elementsScrollPositions.js +20 -0
  74. package/esm/domain/replay/record/index.js +3 -0
  75. package/esm/domain/replay/record/mutationBatch.js +35 -0
  76. package/esm/domain/replay/record/mutationObserver.js +331 -0
  77. package/esm/domain/replay/record/observers.js +457 -0
  78. package/esm/domain/replay/record/privacy.js +233 -0
  79. package/esm/domain/replay/record/record.js +158 -0
  80. package/esm/domain/replay/record/serializationUtils.js +107 -0
  81. package/esm/domain/replay/record/serialize.js +461 -0
  82. package/esm/domain/replay/record/shadowRootsController.js +49 -0
  83. package/esm/domain/replay/record/utils.js +38 -0
  84. package/esm/domain/replay/record/viewports.js +87 -0
  85. package/esm/domain/replay/replayStats.js +64 -0
  86. package/esm/domain/replay/segmentCollection/buildReplayPayload.js +31 -0
  87. package/esm/domain/replay/segmentCollection/deflateWorker.js +4390 -0
  88. package/esm/domain/replay/segmentCollection/index.js +3 -0
  89. package/esm/domain/replay/segmentCollection/segment.js +84 -0
  90. package/esm/domain/replay/segmentCollection/segmentCollection.js +158 -0
  91. package/esm/domain/replay/segmentCollection/startDeflateWorker.js +113 -0
  92. package/esm/domain/requestCollection.js +36 -18
  93. package/esm/domain/rumEventsCollection/actions/actionCollection.js +12 -4
  94. package/esm/domain/rumEventsCollection/actions/clickChain.js +3 -3
  95. package/esm/domain/rumEventsCollection/actions/computeFrustration.js +6 -5
  96. package/esm/domain/rumEventsCollection/actions/getActionNameFromElement.js +6 -6
  97. package/esm/domain/rumEventsCollection/actions/getSelectorsFromElement.js +105 -74
  98. package/esm/domain/rumEventsCollection/actions/listenActionEvents.js +22 -28
  99. package/esm/domain/rumEventsCollection/actions/trackClickActions.js +99 -59
  100. package/esm/domain/rumEventsCollection/error/errorCollection.js +13 -5
  101. package/esm/domain/rumEventsCollection/view/trackViewEventCounts.js +33 -0
  102. package/esm/domain/rumEventsCollection/view/trackViewMetrics.js +1 -18
  103. package/esm/domain/rumEventsCollection/view/trackViews.js +21 -19
  104. package/esm/domain/rumEventsCollection/view/viewCollection.js +12 -9
  105. package/esm/domain/rumSessionManager.js +6 -3
  106. package/esm/domain/tracing/getDocumentTraceId.js +1 -9
  107. package/esm/domain/trackEventCounts.js +10 -2
  108. package/esm/domain/waitPageActivityEnd.js +5 -1
  109. package/esm/transport/startRumBatch.js +5 -7
  110. package/esm/types/index.js +1 -0
  111. package/esm/types/sessionReplayConstants.js +45 -0
  112. package/package.json +3 -3
  113. package/src/boot/recorderApi.js +166 -0
  114. package/src/boot/rum.entry.js +4 -1
  115. package/src/boot/rum.js +38 -10
  116. package/src/boot/rumPublicApi.js +13 -3
  117. package/src/boot/startRecording.js +77 -0
  118. package/src/constants.js +37 -0
  119. package/src/domain/assembly.js +1 -6
  120. package/src/domain/configuration.js +10 -2
  121. package/src/domain/contexts/foregroundContexts.js +140 -0
  122. package/src/domain/contexts/internalContext.js +10 -4
  123. package/src/domain/contexts/urlContexts.js +29 -15
  124. package/src/domain/domMutationCollection.js +14 -6
  125. package/src/domain/initViewportObservable.js +46 -0
  126. package/src/domain/locationChangeObservable.js +17 -25
  127. package/src/domain/performanceCollection.js +42 -42
  128. package/src/domain/replay/record/elementsScrollPositions.js +22 -0
  129. package/src/domain/replay/record/index.js +7 -0
  130. package/src/domain/replay/record/mutationBatch.js +38 -0
  131. package/src/domain/replay/record/mutationObserver.js +393 -0
  132. package/src/domain/replay/record/observers.js +562 -0
  133. package/src/domain/replay/record/privacy.js +304 -0
  134. package/src/domain/replay/record/record.js +158 -0
  135. package/src/domain/replay/record/serializationUtils.js +134 -0
  136. package/src/domain/replay/record/serialize.js +497 -0
  137. package/src/domain/replay/record/shadowRootsController.js +58 -0
  138. package/src/domain/replay/record/utils.js +44 -0
  139. package/src/domain/replay/record/viewports.js +89 -0
  140. package/src/domain/replay/replayStats.js +67 -0
  141. package/src/domain/replay/segmentCollection/buildReplayPayload.js +34 -0
  142. package/src/domain/replay/segmentCollection/deflateWorker.js +4577 -0
  143. package/src/domain/replay/segmentCollection/index.js +6 -0
  144. package/src/domain/replay/segmentCollection/segment.js +87 -0
  145. package/src/domain/replay/segmentCollection/segmentCollection.js +205 -0
  146. package/src/domain/replay/segmentCollection/startDeflateWorker.js +101 -0
  147. package/src/domain/requestCollection.js +64 -28
  148. package/src/domain/rumEventsCollection/actions/actionCollection.js +11 -4
  149. package/src/domain/rumEventsCollection/actions/clickChain.js +3 -3
  150. package/src/domain/rumEventsCollection/actions/computeFrustration.js +7 -4
  151. package/src/domain/rumEventsCollection/actions/getActionNameFromElement.js +9 -6
  152. package/src/domain/rumEventsCollection/actions/getSelectorsFromElement.js +130 -108
  153. package/src/domain/rumEventsCollection/actions/listenActionEvents.js +26 -27
  154. package/src/domain/rumEventsCollection/actions/trackClickActions.js +162 -92
  155. package/src/domain/rumEventsCollection/error/errorCollection.js +11 -5
  156. package/src/domain/rumEventsCollection/view/trackViewEventCounts.js +38 -0
  157. package/src/domain/rumEventsCollection/view/trackViewMetrics.js +1 -19
  158. package/src/domain/rumEventsCollection/view/trackViews.js +98 -68
  159. package/src/domain/rumEventsCollection/view/viewCollection.js +15 -7
  160. package/src/domain/rumSessionManager.js +33 -19
  161. package/src/domain/tracing/getDocumentTraceId.js +6 -9
  162. package/src/domain/trackEventCounts.js +10 -4
  163. package/src/domain/waitPageActivityEnd.js +4 -1
  164. package/src/transport/startRumBatch.js +29 -24
  165. package/src/types/index.js +1 -0
  166. package/src/types/sessionReplayConstants.js +49 -0
@@ -0,0 +1,4577 @@
1
+ let workerURL
2
+
3
+ export function createDeflateWorker() {
4
+ // Lazily compute the worker URL to allow importing the SDK in NodeJS
5
+ if (!workerURL) {
6
+ workerURL = URL.createObjectURL(new Blob([`(${workerCodeFn})(self)`]))
7
+ }
8
+ return new Worker(workerURL)
9
+ }
10
+
11
+ function workerCodeFn() {
12
+ monitor(function () {
13
+ const { Deflate, constants, string2buf } = makePakoDeflate()
14
+
15
+ let deflate = new Deflate()
16
+ let rawBytesCount = 0
17
+ self.addEventListener(
18
+ 'message',
19
+ monitor((event) => {
20
+ const data = event.data
21
+ switch (data.action) {
22
+ case 'init':
23
+ self.postMessage({
24
+ type: 'initialized'
25
+ })
26
+ break
27
+ case 'write': {
28
+ const additionalBytesCount = pushData(data.data)
29
+ self.postMessage({
30
+ type: 'wrote',
31
+ id: data.id,
32
+ compressedBytesCount: deflate.chunks.reduce(
33
+ (total, chunk) => total + chunk.length,
34
+ 0
35
+ ),
36
+ additionalBytesCount
37
+ })
38
+ break
39
+ }
40
+ case 'flush': {
41
+ const additionalBytesCount = data.data ? pushData(data.data) : 0
42
+ deflate.push('', constants.Z_FINISH)
43
+ self.postMessage({
44
+ type: 'flushed',
45
+ id: data.id,
46
+ result: deflate.result,
47
+ additionalBytesCount,
48
+ rawBytesCount
49
+ })
50
+ deflate = new Deflate()
51
+ rawBytesCount = 0
52
+ break
53
+ }
54
+ }
55
+ })
56
+ )
57
+
58
+ function pushData(data) {
59
+ // TextEncoder is not supported on old browser version like Edge 18, therefore we use string2buf
60
+ const binaryData = string2buf(data)
61
+ deflate.push(binaryData, constants.Z_SYNC_FLUSH)
62
+ rawBytesCount += binaryData.length
63
+ return binaryData.length
64
+ }
65
+ })()
66
+
67
+ function monitor(fn) {
68
+ return function () {
69
+ try {
70
+ return fn.apply(this, arguments)
71
+ } catch (e) {
72
+ try {
73
+ self.postMessage({
74
+ type: 'errored',
75
+ error: e
76
+ })
77
+ } catch (_) {
78
+ // DATA_CLONE_ERR, cf https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
79
+ self.postMessage({
80
+ type: 'errored',
81
+ error: `${e}`
82
+ })
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ // https://github.com/nodeca/pako/blob/034669ba0f1a4c0590e45f7c2820128200f972b3/dist/pako_deflate.es5.js
89
+ function makePakoDeflate() {
90
+ /* eslint-disable camelcase, no-bitwise */
91
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
92
+ //
93
+ // This software is provided 'as-is', without any express or implied
94
+ // warranty. In no event will the authors be held liable for any damages
95
+ // arising from the use of this software.
96
+ //
97
+ // Permission is granted to anyone to use this software for any purpose,
98
+ // including commercial applications, and to alter it and redistribute it
99
+ // freely, subject to the following restrictions:
100
+ //
101
+ // 1. The origin of this software must not be misrepresented; you must not
102
+ // claim that you wrote the original software. If you use this software
103
+ // in a product, an acknowledgment in the product documentation would be
104
+ // appreciated but is not required.
105
+ // 2. Altered source versions must be plainly marked as such, and must not be
106
+ // misrepresented as being the original software.
107
+ // 3. This notice may not be removed or altered from any source distribution.
108
+
109
+ /* Public constants ========================================================== */
110
+
111
+ /* =========================================================================== */
112
+ // const Z_FILTERED = 1;
113
+ // const Z_HUFFMAN_ONLY = 2;
114
+ // const Z_RLE = 3;
115
+
116
+ var Z_FIXED = 4 // const Z_DEFAULT_STRATEGY = 0;
117
+
118
+ /* Possible values of the data_type field (though see inflate()) */
119
+
120
+ var Z_BINARY = 0
121
+ var Z_TEXT = 1 // const Z_ASCII = 1; // = Z_TEXT
122
+
123
+ var Z_UNKNOWN = 2
124
+ /* ============================================================================ */
125
+
126
+ function zero(buf) {
127
+ var len = buf.length
128
+
129
+ while (--len >= 0) {
130
+ buf[len] = 0
131
+ }
132
+ } // From zutil.h
133
+
134
+ var STORED_BLOCK = 0
135
+ var STATIC_TREES = 1
136
+ var DYN_TREES = 2
137
+ /* The three kinds of block type */
138
+
139
+ var MIN_MATCH = 3
140
+ var MAX_MATCH = 258
141
+ /* The minimum and maximum match lengths */
142
+ // From deflate.h
143
+
144
+ /* ===========================================================================
145
+ * Internal compression state.
146
+ */
147
+
148
+ var LENGTH_CODES = 29
149
+ /* number of length codes, not counting the special END_BLOCK code */
150
+
151
+ var LITERALS = 256
152
+ /* number of literal bytes 0..255 */
153
+
154
+ var L_CODES = LITERALS + 1 + LENGTH_CODES
155
+ /* number of Literal or Length codes, including the END_BLOCK code */
156
+
157
+ var D_CODES = 30
158
+ /* number of distance codes */
159
+
160
+ var BL_CODES = 19
161
+ /* number of codes used to transfer the bit lengths */
162
+
163
+ var HEAP_SIZE = 2 * L_CODES + 1
164
+ /* maximum heap size */
165
+
166
+ var MAX_BITS = 15
167
+ /* All codes must not exceed MAX_BITS bits */
168
+
169
+ var Buf_size = 16
170
+ /* size of bit buffer in bi_buf */
171
+
172
+ /* ===========================================================================
173
+ * Constants
174
+ */
175
+
176
+ var MAX_BL_BITS = 7
177
+ /* Bit length codes must not exceed MAX_BL_BITS bits */
178
+
179
+ var END_BLOCK = 256
180
+ /* end of block literal code */
181
+
182
+ var REP_3_6 = 16
183
+ /* repeat previous bit length 3-6 times (2 bits of repeat count) */
184
+
185
+ var REPZ_3_10 = 17
186
+ /* repeat a zero length 3-10 times (3 bits of repeat count) */
187
+
188
+ var REPZ_11_138 = 18
189
+ /* repeat a zero length 11-138 times (7 bits of repeat count) */
190
+
191
+ var extra_lbits =
192
+ /* extra bits for each length code */
193
+ new Uint8Array([
194
+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
195
+ 5, 5, 5, 5, 0
196
+ ])
197
+ var extra_dbits =
198
+ /* extra bits for each distance code */
199
+ new Uint8Array([
200
+ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
201
+ 10, 11, 11, 12, 12, 13, 13
202
+ ])
203
+ var extra_blbits =
204
+ /* extra bits for each bit length code */
205
+ new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7])
206
+ var bl_order = new Uint8Array([
207
+ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
208
+ ])
209
+ /* eslint-enable comma-spacing,array-bracket-spacing */
210
+
211
+ /* The lengths of the bit length codes are sent in order of decreasing
212
+ * probability, to avoid transmitting the lengths for unused bit length codes.
213
+ */
214
+
215
+ /* ===========================================================================
216
+ * Local data. These are initialized only once.
217
+ */
218
+ // We pre-fill arrays with 0 to avoid uninitialized gaps
219
+
220
+ var DIST_CODE_LEN = 512
221
+ /* see definition of array dist_code below */
222
+ // !!!! Use flat array instead of structure, Freq = i*2, Len = i*2+1
223
+
224
+ var static_ltree = new Array((L_CODES + 2) * 2)
225
+ zero(static_ltree)
226
+ /* The static literal tree. Since the bit lengths are imposed, there is no
227
+ * need for the L_CODES extra codes used during heap construction. However
228
+ * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
229
+ * below).
230
+ */
231
+
232
+ var static_dtree = new Array(D_CODES * 2)
233
+ zero(static_dtree)
234
+ /* The static distance tree. (Actually a trivial tree since all codes use
235
+ * 5 bits.)
236
+ */
237
+
238
+ var _dist_code = new Array(DIST_CODE_LEN)
239
+
240
+ zero(_dist_code)
241
+ /* Distance codes. The first 256 values correspond to the distances
242
+ * 3 .. 258, the last 256 values correspond to the top 8 bits of
243
+ * the 15 bit distances.
244
+ */
245
+
246
+ var _length_code = new Array(MAX_MATCH - MIN_MATCH + 1)
247
+
248
+ zero(_length_code)
249
+ /* length code for each normalized match length (0 == MIN_MATCH) */
250
+
251
+ var base_length = new Array(LENGTH_CODES)
252
+ zero(base_length)
253
+ /* First normalized length for each code (0 = MIN_MATCH) */
254
+
255
+ var base_dist = new Array(D_CODES)
256
+ zero(base_dist)
257
+ /* First normalized distance for each code (0 = distance of 1) */
258
+
259
+ function StaticTreeDesc(
260
+ static_tree,
261
+ extra_bits,
262
+ extra_base,
263
+ elems,
264
+ max_length
265
+ ) {
266
+ this.static_tree = static_tree
267
+ /* static tree or NULL */
268
+
269
+ this.extra_bits = extra_bits
270
+ /* extra bits for each code or NULL */
271
+
272
+ this.extra_base = extra_base
273
+ /* base index for extra_bits */
274
+
275
+ this.elems = elems
276
+ /* max number of elements in the tree */
277
+
278
+ this.max_length = max_length
279
+ /* max bit length for the codes */
280
+ // show if `static_tree` has data or dummy - needed for monomorphic objects
281
+
282
+ this.has_stree = static_tree && static_tree.length
283
+ }
284
+
285
+ var static_l_desc
286
+ var static_d_desc
287
+ var static_bl_desc
288
+
289
+ function TreeDesc(dyn_tree, stat_desc) {
290
+ this.dyn_tree = dyn_tree
291
+ /* the dynamic tree */
292
+
293
+ this.max_code = 0
294
+ /* largest code with non zero frequency */
295
+
296
+ this.stat_desc = stat_desc
297
+ /* the corresponding static tree */
298
+ }
299
+
300
+ var d_code = function d_code(dist) {
301
+ return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)]
302
+ }
303
+ /* ===========================================================================
304
+ * Output a short LSB first on the stream.
305
+ * IN assertion: there is enough room in pendingBuf.
306
+ */
307
+
308
+ var put_short = function put_short(s, w) {
309
+ // put_byte(s, (uch)((w) & 0xff));
310
+ // put_byte(s, (uch)((ush)(w) >> 8));
311
+ s.pending_buf[s.pending++] = w & 0xff
312
+ s.pending_buf[s.pending++] = (w >>> 8) & 0xff
313
+ }
314
+ /* ===========================================================================
315
+ * Send a value on a given number of bits.
316
+ * IN assertion: length <= 16 and value fits in length bits.
317
+ */
318
+
319
+ var send_bits = function send_bits(s, value, length) {
320
+ if (s.bi_valid > Buf_size - length) {
321
+ s.bi_buf |= (value << s.bi_valid) & 0xffff
322
+ put_short(s, s.bi_buf)
323
+ s.bi_buf = value >> (Buf_size - s.bi_valid)
324
+ s.bi_valid += length - Buf_size
325
+ } else {
326
+ s.bi_buf |= (value << s.bi_valid) & 0xffff
327
+ s.bi_valid += length
328
+ }
329
+ }
330
+
331
+ var send_code = function send_code(s, c, tree) {
332
+ send_bits(
333
+ s,
334
+ tree[c * 2],
335
+ /* .Code */
336
+ tree[c * 2 + 1]
337
+ /* .Len */
338
+ )
339
+ }
340
+ /* ===========================================================================
341
+ * Reverse the first len bits of a code, using straightforward code (a faster
342
+ * method would use a table)
343
+ * IN assertion: 1 <= len <= 15
344
+ */
345
+
346
+ var bi_reverse = function bi_reverse(code, len) {
347
+ var res = 0
348
+
349
+ do {
350
+ res |= code & 1
351
+ code >>>= 1
352
+ res <<= 1
353
+ } while (--len > 0)
354
+
355
+ return res >>> 1
356
+ }
357
+ /* ===========================================================================
358
+ * Flush the bit buffer, keeping at most 7 bits in it.
359
+ */
360
+
361
+ var bi_flush = function bi_flush(s) {
362
+ if (s.bi_valid === 16) {
363
+ put_short(s, s.bi_buf)
364
+ s.bi_buf = 0
365
+ s.bi_valid = 0
366
+ } else if (s.bi_valid >= 8) {
367
+ s.pending_buf[s.pending++] = s.bi_buf & 0xff
368
+ s.bi_buf >>= 8
369
+ s.bi_valid -= 8
370
+ }
371
+ }
372
+ /* ===========================================================================
373
+ * Compute the optimal bit lengths for a tree and update the total bit length
374
+ * for the current block.
375
+ * IN assertion: the fields freq and dad are set, heap[heap_max] and
376
+ * above are the tree nodes sorted by increasing frequency.
377
+ * OUT assertions: the field len is set to the optimal bit length, the
378
+ * array bl_count contains the frequencies for each bit length.
379
+ * The length opt_len is updated; static_len is also updated if stree is
380
+ * not null.
381
+ */
382
+
383
+ var gen_bitlen = function gen_bitlen(
384
+ s,
385
+ desc // deflate_state *s; // tree_desc *desc; /* the tree descriptor */
386
+ ) {
387
+ var tree = desc.dyn_tree
388
+ var max_code = desc.max_code
389
+ var stree = desc.stat_desc.static_tree
390
+ var has_stree = desc.stat_desc.has_stree
391
+ var extra = desc.stat_desc.extra_bits
392
+ var base = desc.stat_desc.extra_base
393
+ var max_length = desc.stat_desc.max_length
394
+ var h
395
+ /* heap index */
396
+
397
+ var n
398
+ var m
399
+ /* iterate over the tree elements */
400
+
401
+ var bits
402
+ /* bit length */
403
+
404
+ var xbits
405
+ /* extra bits */
406
+
407
+ var f
408
+ /* frequency */
409
+
410
+ var overflow = 0
411
+ /* number of elements with bit length too large */
412
+
413
+ for (bits = 0; bits <= MAX_BITS; bits++) {
414
+ s.bl_count[bits] = 0
415
+ }
416
+ /* In a first pass, compute the optimal bit lengths (which may
417
+ * overflow in the case of the bit length tree).
418
+ */
419
+
420
+ tree[s.heap[s.heap_max] * 2 + 1] =
421
+ /* .Len */
422
+ 0
423
+ /* root of the heap */
424
+
425
+ for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
426
+ n = s.heap[h]
427
+ bits =
428
+ tree[
429
+ tree[n * 2 + 1] *
430
+ /* .Dad */
431
+ 2 +
432
+ 1
433
+ ] +
434
+ /* .Len */
435
+ 1
436
+
437
+ if (bits > max_length) {
438
+ bits = max_length
439
+ overflow++
440
+ }
441
+
442
+ tree[n * 2 + 1] =
443
+ /* .Len */
444
+ bits
445
+ /* We overwrite tree[n].Dad which is no longer needed */
446
+
447
+ if (n > max_code) {
448
+ continue
449
+ }
450
+ /* not a leaf node */
451
+
452
+ s.bl_count[bits]++
453
+ xbits = 0
454
+
455
+ if (n >= base) {
456
+ xbits = extra[n - base]
457
+ }
458
+
459
+ f = tree[n * 2]
460
+ /* .Freq */
461
+ s.opt_len += f * (bits + xbits)
462
+
463
+ if (has_stree) {
464
+ s.static_len +=
465
+ f *
466
+ (stree[n * 2 + 1] +
467
+ /* .Len */
468
+ xbits)
469
+ }
470
+ }
471
+
472
+ if (overflow === 0) {
473
+ return
474
+ } // Trace((stderr,"\nbit length overflow\n"));
475
+
476
+ /* This happens for example on obj2 and pic of the Calgary corpus */
477
+
478
+ /* Find the first bit length which could increase: */
479
+
480
+ do {
481
+ bits = max_length - 1
482
+
483
+ while (s.bl_count[bits] === 0) {
484
+ bits--
485
+ }
486
+
487
+ s.bl_count[bits]--
488
+ /* move one leaf down the tree */
489
+
490
+ s.bl_count[bits + 1] += 2
491
+ /* move one overflow item as its brother */
492
+
493
+ s.bl_count[max_length]--
494
+ /* The brother of the overflow item also moves one step up,
495
+ * but this does not affect bl_count[max_length]
496
+ */
497
+
498
+ overflow -= 2
499
+ } while (overflow > 0)
500
+ /* Now recompute all bit lengths, scanning in increasing frequency.
501
+ * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
502
+ * lengths instead of fixing only the wrong ones. This idea is taken
503
+ * from 'ar' written by Haruhiko Okumura.)
504
+ */
505
+
506
+ for (bits = max_length; bits !== 0; bits--) {
507
+ n = s.bl_count[bits]
508
+
509
+ while (n !== 0) {
510
+ m = s.heap[--h]
511
+
512
+ if (m > max_code) {
513
+ continue
514
+ }
515
+
516
+ if (
517
+ tree[m * 2 + 1] !==
518
+ /* .Len */
519
+ bits
520
+ ) {
521
+ // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
522
+ s.opt_len +=
523
+ (bits - tree[m * 2 + 1]) *
524
+ /* .Len */
525
+ tree[m * 2]
526
+ /* .Freq */
527
+ tree[m * 2 + 1] =
528
+ /* .Len */
529
+ bits
530
+ }
531
+
532
+ n--
533
+ }
534
+ }
535
+ }
536
+ /* ===========================================================================
537
+ * Generate the codes for a given tree and bit counts (which need not be
538
+ * optimal).
539
+ * IN assertion: the array bl_count contains the bit length statistics for
540
+ * the given tree and the field len is set for all tree elements.
541
+ * OUT assertion: the field code is set for all tree elements of non
542
+ * zero code length.
543
+ */
544
+
545
+ var gen_codes = function gen_codes(
546
+ tree,
547
+ max_code,
548
+ bl_count
549
+ // ct_data *tree; /* the tree to decorate */
550
+ // int max_code; /* largest code with non zero frequency */
551
+ // ushf *bl_count; /* number of codes at each bit length */
552
+ ) {
553
+ var next_code = new Array(MAX_BITS + 1)
554
+ /* next code value for each bit length */
555
+
556
+ var code = 0
557
+ /* running code value */
558
+
559
+ var bits
560
+ /* bit index */
561
+
562
+ var n
563
+ /* code index */
564
+
565
+ /* The distribution counts are first used to generate the code values
566
+ * without bit reversal.
567
+ */
568
+
569
+ for (bits = 1; bits <= MAX_BITS; bits++) {
570
+ next_code[bits] = code = (code + bl_count[bits - 1]) << 1
571
+ }
572
+ /* Check that the bit counts in bl_count are consistent. The last code
573
+ * must be all ones.
574
+ */
575
+ // Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
576
+ // "inconsistent bit counts");
577
+ // Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
578
+
579
+ for (n = 0; n <= max_code; n++) {
580
+ var len = tree[n * 2 + 1]
581
+ /* .Len */
582
+ if (len === 0) {
583
+ continue
584
+ }
585
+ /* Now reverse the bits */
586
+
587
+ tree[n * 2] =
588
+ /* .Code */
589
+ bi_reverse(next_code[len]++, len) // Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
590
+ // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
591
+ }
592
+ }
593
+ /* ===========================================================================
594
+ * Initialize the various 'constant' tables.
595
+ */
596
+
597
+ var tr_static_init = function tr_static_init() {
598
+ var n
599
+ /* iterates over tree elements */
600
+
601
+ var bits
602
+ /* bit counter */
603
+
604
+ var length
605
+ /* length value */
606
+
607
+ var code
608
+ /* code value */
609
+
610
+ var dist
611
+ /* distance index */
612
+
613
+ var bl_count = new Array(MAX_BITS + 1)
614
+ /* number of codes at each bit length for an optimal tree */
615
+ // do check in _tr_init()
616
+ // if (static_init_done) return;
617
+
618
+ /* For some embedded targets, global variables are not initialized: */
619
+
620
+ /* #ifdef NO_INIT_GLOBAL_POINTERS
621
+ static_l_desc.static_tree = static_ltree;
622
+ static_l_desc.extra_bits = extra_lbits;
623
+ static_d_desc.static_tree = static_dtree;
624
+ static_d_desc.extra_bits = extra_dbits;
625
+ static_bl_desc.extra_bits = extra_blbits;
626
+ #endif */
627
+
628
+ /* Initialize the mapping length (0..255) -> length code (0..28) */
629
+
630
+ length = 0
631
+
632
+ for (code = 0; code < LENGTH_CODES - 1; code++) {
633
+ base_length[code] = length
634
+
635
+ for (n = 0; n < 1 << extra_lbits[code]; n++) {
636
+ _length_code[length++] = code
637
+ }
638
+ } // Assert (length == 256, "tr_static_init: length != 256");
639
+
640
+ /* Note that the length 255 (match length 258) can be represented
641
+ * in two different ways: code 284 + 5 bits or code 285, so we
642
+ * overwrite length_code[255] to use the best encoding:
643
+ */
644
+
645
+ _length_code[length - 1] = code
646
+ /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
647
+
648
+ dist = 0
649
+
650
+ for (code = 0; code < 16; code++) {
651
+ base_dist[code] = dist
652
+
653
+ for (n = 0; n < 1 << extra_dbits[code]; n++) {
654
+ _dist_code[dist++] = code
655
+ }
656
+ } // Assert (dist == 256, "tr_static_init: dist != 256");
657
+
658
+ dist >>= 7
659
+ /* from now on, all distances are divided by 128 */
660
+
661
+ for (; code < D_CODES; code++) {
662
+ base_dist[code] = dist << 7
663
+
664
+ for (n = 0; n < 1 << (extra_dbits[code] - 7); n++) {
665
+ _dist_code[256 + dist++] = code
666
+ }
667
+ } // Assert (dist == 256, "tr_static_init: 256+dist != 512");
668
+
669
+ /* Construct the codes of the static literal tree */
670
+
671
+ for (bits = 0; bits <= MAX_BITS; bits++) {
672
+ bl_count[bits] = 0
673
+ }
674
+
675
+ n = 0
676
+
677
+ while (n <= 143) {
678
+ static_ltree[n * 2 + 1] =
679
+ /* .Len */
680
+ 8
681
+ n++
682
+ bl_count[8]++
683
+ }
684
+
685
+ while (n <= 255) {
686
+ static_ltree[n * 2 + 1] =
687
+ /* .Len */
688
+ 9
689
+ n++
690
+ bl_count[9]++
691
+ }
692
+
693
+ while (n <= 279) {
694
+ static_ltree[n * 2 + 1] =
695
+ /* .Len */
696
+ 7
697
+ n++
698
+ bl_count[7]++
699
+ }
700
+
701
+ while (n <= 287) {
702
+ static_ltree[n * 2 + 1] =
703
+ /* .Len */
704
+ 8
705
+ n++
706
+ bl_count[8]++
707
+ }
708
+ /* Codes 286 and 287 do not exist, but we must include them in the
709
+ * tree construction to get a canonical Huffman tree (longest code
710
+ * all ones)
711
+ */
712
+
713
+ gen_codes(static_ltree, L_CODES + 1, bl_count)
714
+ /* The static distance tree is trivial: */
715
+
716
+ for (n = 0; n < D_CODES; n++) {
717
+ static_dtree[n * 2 + 1] =
718
+ /* .Len */
719
+ 5
720
+ static_dtree[n * 2] =
721
+ /* .Code */
722
+ bi_reverse(n, 5)
723
+ } // Now data ready and we can init static trees
724
+
725
+ static_l_desc = new StaticTreeDesc(
726
+ static_ltree,
727
+ extra_lbits,
728
+ LITERALS + 1,
729
+ L_CODES,
730
+ MAX_BITS
731
+ )
732
+ static_d_desc = new StaticTreeDesc(
733
+ static_dtree,
734
+ extra_dbits,
735
+ 0,
736
+ D_CODES,
737
+ MAX_BITS
738
+ )
739
+ static_bl_desc = new StaticTreeDesc(
740
+ new Array(0),
741
+ extra_blbits,
742
+ 0,
743
+ BL_CODES,
744
+ MAX_BL_BITS
745
+ )
746
+ // static_init_done = true;
747
+ }
748
+ /* ===========================================================================
749
+ * Initialize a new block.
750
+ */
751
+
752
+ var init_block = function init_block(s) {
753
+ var n
754
+ /* iterates over tree elements */
755
+
756
+ /* Initialize the trees. */
757
+
758
+ for (n = 0; n < L_CODES; n++) {
759
+ s.dyn_ltree[n * 2] =
760
+ /* .Freq */
761
+ 0
762
+ }
763
+
764
+ for (n = 0; n < D_CODES; n++) {
765
+ s.dyn_dtree[n * 2] =
766
+ /* .Freq */
767
+ 0
768
+ }
769
+
770
+ for (n = 0; n < BL_CODES; n++) {
771
+ s.bl_tree[n * 2] =
772
+ /* .Freq */
773
+ 0
774
+ }
775
+
776
+ s.dyn_ltree[END_BLOCK * 2] =
777
+ /* .Freq */
778
+ 1
779
+ s.opt_len = s.static_len = 0
780
+ s.last_lit = s.matches = 0
781
+ }
782
+ /* ===========================================================================
783
+ * Flush the bit buffer and align the output on a byte boundary
784
+ */
785
+
786
+ var bi_windup = function bi_windup(s) {
787
+ if (s.bi_valid > 8) {
788
+ put_short(s, s.bi_buf)
789
+ } else if (s.bi_valid > 0) {
790
+ // put_byte(s, (Byte)s->bi_buf);
791
+ s.pending_buf[s.pending++] = s.bi_buf
792
+ }
793
+
794
+ s.bi_buf = 0
795
+ s.bi_valid = 0
796
+ }
797
+ /* ===========================================================================
798
+ * Copy a stored block, storing first the length and its
799
+ * one's complement if requested.
800
+ */
801
+
802
+ var copy_block = function copy_block(
803
+ s,
804
+ buf,
805
+ len,
806
+ header
807
+ // DeflateState *s;
808
+ // charf *buf; /* the input data */
809
+ // unsigned len; /* its length */
810
+ // int header; /* true if block header must be written */
811
+ ) {
812
+ bi_windup(s)
813
+ /* align on byte boundary */
814
+
815
+ if (header) {
816
+ put_short(s, len)
817
+ put_short(s, ~len)
818
+ }
819
+ // while (len--) {
820
+ // put_byte(s, *buf++);
821
+ // }
822
+
823
+ s.pending_buf.set(s.window.subarray(buf, buf + len), s.pending)
824
+ s.pending += len
825
+ }
826
+ /* ===========================================================================
827
+ * Compares to subtrees, using the tree depth as tie breaker when
828
+ * the subtrees have equal frequency. This minimizes the worst case length.
829
+ */
830
+
831
+ var smaller = function smaller(tree, n, m, depth) {
832
+ var _n2 = n * 2
833
+
834
+ var _m2 = m * 2
835
+
836
+ return (
837
+ tree[_n2] <
838
+ /* .Freq */
839
+ tree[_m2] ||
840
+ /* .Freq */
841
+ (tree[_n2] ===
842
+ /* .Freq */
843
+ tree[_m2] &&
844
+ /* .Freq */
845
+ depth[n] <= depth[m])
846
+ )
847
+ }
848
+ /* ===========================================================================
849
+ * Restore the heap property by moving down the tree starting at node k,
850
+ * exchanging a node with the smallest of its two sons if necessary, stopping
851
+ * when the heap property is re-established (each father smaller than its
852
+ * two sons).
853
+ */
854
+
855
+ var pqdownheap = function pqdownheap(
856
+ s,
857
+ tree,
858
+ k
859
+ // deflate_state *s;
860
+ // ct_data *tree; /* the tree to restore */
861
+ // int k; /* node to move down */
862
+ ) {
863
+ var v = s.heap[k]
864
+ var j = k << 1
865
+ /* left son of k */
866
+
867
+ while (j <= s.heap_len) {
868
+ /* Set j to the smallest of the two sons: */
869
+ if (
870
+ j < s.heap_len &&
871
+ smaller(tree, s.heap[j + 1], s.heap[j], s.depth)
872
+ ) {
873
+ j++
874
+ }
875
+ /* Exit if v is smaller than both sons */
876
+
877
+ if (smaller(tree, v, s.heap[j], s.depth)) {
878
+ break
879
+ }
880
+ /* Exchange v with the smallest son */
881
+
882
+ s.heap[k] = s.heap[j]
883
+ k = j
884
+ /* And continue down the tree, setting j to the left son of k */
885
+
886
+ j <<= 1
887
+ }
888
+
889
+ s.heap[k] = v
890
+ } // inlined manually
891
+ // const SMALLEST = 1;
892
+
893
+ /* ===========================================================================
894
+ * Send the block data compressed using the given Huffman trees
895
+ */
896
+
897
+ var compress_block = function compress_block(
898
+ s,
899
+ ltree,
900
+ dtree
901
+ // deflate_state *s;
902
+ // const ct_data *ltree; /* literal tree */
903
+ // const ct_data *dtree; /* distance tree */
904
+ ) {
905
+ var dist
906
+ /* distance of matched string */
907
+
908
+ var lc
909
+ /* match length or unmatched char (if dist == 0) */
910
+
911
+ var lx = 0
912
+ /* running index in l_buf */
913
+
914
+ var code
915
+ /* the code to send */
916
+
917
+ var extra
918
+ /* number of extra bits to send */
919
+
920
+ if (s.last_lit !== 0) {
921
+ do {
922
+ dist =
923
+ (s.pending_buf[s.d_buf + lx * 2] << 8) |
924
+ s.pending_buf[s.d_buf + lx * 2 + 1]
925
+ lc = s.pending_buf[s.l_buf + lx]
926
+ lx++
927
+
928
+ if (dist === 0) {
929
+ send_code(s, lc, ltree)
930
+ /* send a literal byte */
931
+ // Tracecv(isgraph(lc), (stderr," '%c' ", lc));
932
+ } else {
933
+ /* Here, lc is the match length - MIN_MATCH */
934
+ code = _length_code[lc]
935
+ send_code(s, code + LITERALS + 1, ltree)
936
+ /* send the length code */
937
+
938
+ extra = extra_lbits[code]
939
+
940
+ if (extra !== 0) {
941
+ lc -= base_length[code]
942
+ send_bits(s, lc, extra)
943
+ /* send the extra length bits */
944
+ }
945
+
946
+ dist--
947
+ /* dist is now the match distance - 1 */
948
+
949
+ code = d_code(dist) // Assert (code < D_CODES, "bad d_code");
950
+
951
+ send_code(s, code, dtree)
952
+ /* send the distance code */
953
+
954
+ extra = extra_dbits[code]
955
+
956
+ if (extra !== 0) {
957
+ dist -= base_dist[code]
958
+ send_bits(s, dist, extra)
959
+ /* send the extra distance bits */
960
+ }
961
+ }
962
+ /* literal or match pair ? */
963
+
964
+ /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
965
+ // Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
966
+ // "pendingBuf overflow");
967
+ } while (lx < s.last_lit)
968
+ }
969
+
970
+ send_code(s, END_BLOCK, ltree)
971
+ }
972
+ /* ===========================================================================
973
+ * Construct one Huffman tree and assigns the code bit strings and lengths.
974
+ * Update the total bit length for the current block.
975
+ * IN assertion: the field freq is set for all tree elements.
976
+ * OUT assertions: the fields len and code are set to the optimal bit length
977
+ * and corresponding code. The length opt_len is updated; static_len is
978
+ * also updated if stree is not null. The field max_code is set.
979
+ */
980
+
981
+ var build_tree = function build_tree(
982
+ s,
983
+ desc // deflate_state *s; // tree_desc *desc; /* the tree descriptor */
984
+ ) {
985
+ var tree = desc.dyn_tree
986
+ var stree = desc.stat_desc.static_tree
987
+ var has_stree = desc.stat_desc.has_stree
988
+ var elems = desc.stat_desc.elems
989
+ var n
990
+ var m
991
+ /* iterate over heap elements */
992
+
993
+ var max_code = -1
994
+ /* largest code with non zero frequency */
995
+
996
+ var node
997
+ /* new node being created */
998
+
999
+ /* Construct the initial heap, with least frequent element in
1000
+ * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
1001
+ * heap[0] is not used.
1002
+ */
1003
+
1004
+ s.heap_len = 0
1005
+ s.heap_max = HEAP_SIZE
1006
+
1007
+ for (n = 0; n < elems; n++) {
1008
+ if (
1009
+ tree[n * 2] !==
1010
+ /* .Freq */
1011
+ 0
1012
+ ) {
1013
+ s.heap[++s.heap_len] = max_code = n
1014
+ s.depth[n] = 0
1015
+ } else {
1016
+ tree[n * 2 + 1] =
1017
+ /* .Len */
1018
+ 0
1019
+ }
1020
+ }
1021
+ /* The pkzip format requires that at least one distance code exists,
1022
+ * and that at least one bit should be sent even if there is only one
1023
+ * possible code. So to avoid special checks later on we force at least
1024
+ * two codes of non zero frequency.
1025
+ */
1026
+
1027
+ while (s.heap_len < 2) {
1028
+ node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0
1029
+ tree[node * 2] =
1030
+ /* .Freq */
1031
+ 1
1032
+ s.depth[node] = 0
1033
+ s.opt_len--
1034
+
1035
+ if (has_stree) {
1036
+ s.static_len -= stree[node * 2 + 1]
1037
+ /* .Len */
1038
+ }
1039
+ /* node is 0 or 1 so it does not have extra bits */
1040
+ }
1041
+
1042
+ desc.max_code = max_code
1043
+ /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
1044
+ * establish sub-heaps of increasing lengths:
1045
+ */
1046
+
1047
+ for (
1048
+ n = s.heap_len >> 1;
1049
+ /* int /2 */
1050
+ n >= 1;
1051
+ n--
1052
+ ) {
1053
+ pqdownheap(s, tree, n)
1054
+ }
1055
+ /* Construct the Huffman tree by repeatedly combining the least two
1056
+ * frequent nodes.
1057
+ */
1058
+
1059
+ node = elems
1060
+ /* next internal node of the tree */
1061
+
1062
+ do {
1063
+ // pqremove(s, tree, n); /* n = node of least frequency */
1064
+
1065
+ /** * pqremove ** */
1066
+ n = s.heap[1]
1067
+ /* SMALLEST */
1068
+ s.heap[1] = s.heap[s.heap_len--]
1069
+ /* SMALLEST */
1070
+ pqdownheap(
1071
+ s,
1072
+ tree,
1073
+ 1
1074
+ /* SMALLEST */
1075
+ )
1076
+ /***/
1077
+
1078
+ m = s.heap[1]
1079
+ /* SMALLEST */
1080
+ /* m = node of next least frequency */
1081
+
1082
+ s.heap[--s.heap_max] = n
1083
+ /* keep the nodes sorted by frequency */
1084
+
1085
+ s.heap[--s.heap_max] = m
1086
+ /* Create a new node father of n and m */
1087
+
1088
+ tree[node * 2] =
1089
+ /* .Freq */
1090
+ tree[n * 2] +
1091
+ /* .Freq */
1092
+ tree[m * 2]
1093
+ /* .Freq */
1094
+ s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1
1095
+ tree[n * 2 + 1] =
1096
+ /* .Dad */
1097
+ tree[m * 2 + 1] =
1098
+ /* .Dad */
1099
+ node
1100
+ /* and insert the new node in the heap */
1101
+
1102
+ s.heap[1] = node++
1103
+ /* SMALLEST */
1104
+ pqdownheap(
1105
+ s,
1106
+ tree,
1107
+ 1
1108
+ /* SMALLEST */
1109
+ )
1110
+ } while (s.heap_len >= 2)
1111
+
1112
+ s.heap[--s.heap_max] = s.heap[1]
1113
+ /* SMALLEST */
1114
+ /* At this point, the fields freq and dad are set. We can now
1115
+ * generate the bit lengths.
1116
+ */
1117
+
1118
+ gen_bitlen(s, desc)
1119
+ /* The field len is now set, we can generate the bit codes */
1120
+
1121
+ gen_codes(tree, max_code, s.bl_count)
1122
+ }
1123
+ /* ===========================================================================
1124
+ * Scan a literal or distance tree to determine the frequencies of the codes
1125
+ * in the bit length tree.
1126
+ */
1127
+
1128
+ var scan_tree = function scan_tree(
1129
+ s,
1130
+ tree,
1131
+ max_code
1132
+ // deflate_state *s;
1133
+ // ct_data *tree; /* the tree to be scanned */
1134
+ // int max_code; /* and its largest code of non zero frequency */
1135
+ ) {
1136
+ var n
1137
+ /* iterates over all tree elements */
1138
+
1139
+ var prevlen = -1
1140
+ /* last emitted length */
1141
+
1142
+ var curlen
1143
+ /* length of current code */
1144
+
1145
+ var nextlen = tree[0 * 2 + 1]
1146
+ /* .Len */
1147
+ /* length of next code */
1148
+
1149
+ var count = 0
1150
+ /* repeat count of the current code */
1151
+
1152
+ var max_count = 7
1153
+ /* max repeat count */
1154
+
1155
+ var min_count = 4
1156
+ /* min repeat count */
1157
+
1158
+ if (nextlen === 0) {
1159
+ max_count = 138
1160
+ min_count = 3
1161
+ }
1162
+
1163
+ tree[(max_code + 1) * 2 + 1] =
1164
+ /* .Len */
1165
+ 0xffff
1166
+ /* guard */
1167
+
1168
+ for (n = 0; n <= max_code; n++) {
1169
+ curlen = nextlen
1170
+ nextlen = tree[(n + 1) * 2 + 1]
1171
+ /* .Len */
1172
+
1173
+ if (++count < max_count && curlen === nextlen) {
1174
+ continue
1175
+ } else if (count < min_count) {
1176
+ s.bl_tree[curlen * 2] +=
1177
+ /* .Freq */
1178
+ count
1179
+ } else if (curlen !== 0) {
1180
+ if (curlen !== prevlen) {
1181
+ s.bl_tree[curlen * 2] /* .Freq */++
1182
+ }
1183
+
1184
+ s.bl_tree[REP_3_6 * 2] /* .Freq */++
1185
+ } else if (count <= 10) {
1186
+ s.bl_tree[REPZ_3_10 * 2] /* .Freq */++
1187
+ } else {
1188
+ s.bl_tree[REPZ_11_138 * 2] /* .Freq */++
1189
+ }
1190
+
1191
+ count = 0
1192
+ prevlen = curlen
1193
+
1194
+ if (nextlen === 0) {
1195
+ max_count = 138
1196
+ min_count = 3
1197
+ } else if (curlen === nextlen) {
1198
+ max_count = 6
1199
+ min_count = 3
1200
+ } else {
1201
+ max_count = 7
1202
+ min_count = 4
1203
+ }
1204
+ }
1205
+ }
1206
+ /* ===========================================================================
1207
+ * Send a literal or distance tree in compressed form, using the codes in
1208
+ * bl_tree.
1209
+ */
1210
+
1211
+ var send_tree = function send_tree(
1212
+ s,
1213
+ tree,
1214
+ max_code
1215
+ // deflate_state *s;
1216
+ // ct_data *tree; /* the tree to be scanned */
1217
+ // int max_code; /* and its largest code of non zero frequency */
1218
+ ) {
1219
+ var n
1220
+ /* iterates over all tree elements */
1221
+
1222
+ var prevlen = -1
1223
+ /* last emitted length */
1224
+
1225
+ var curlen
1226
+ /* length of current code */
1227
+
1228
+ var nextlen = tree[0 * 2 + 1]
1229
+ /* .Len */
1230
+ /* length of next code */
1231
+
1232
+ var count = 0
1233
+ /* repeat count of the current code */
1234
+
1235
+ var max_count = 7
1236
+ /* max repeat count */
1237
+
1238
+ var min_count = 4
1239
+ /* min repeat count */
1240
+
1241
+ /* tree[max_code+1].Len = -1; */
1242
+
1243
+ /* guard already set */
1244
+
1245
+ if (nextlen === 0) {
1246
+ max_count = 138
1247
+ min_count = 3
1248
+ }
1249
+
1250
+ for (n = 0; n <= max_code; n++) {
1251
+ curlen = nextlen
1252
+ nextlen = tree[(n + 1) * 2 + 1]
1253
+ /* .Len */
1254
+
1255
+ if (++count < max_count && curlen === nextlen) {
1256
+ continue
1257
+ } else if (count < min_count) {
1258
+ do {
1259
+ send_code(s, curlen, s.bl_tree)
1260
+ } while (--count !== 0)
1261
+ } else if (curlen !== 0) {
1262
+ if (curlen !== prevlen) {
1263
+ send_code(s, curlen, s.bl_tree)
1264
+ count--
1265
+ } // Assert(count >= 3 && count <= 6, " 3_6?");
1266
+
1267
+ send_code(s, REP_3_6, s.bl_tree)
1268
+ send_bits(s, count - 3, 2)
1269
+ } else if (count <= 10) {
1270
+ send_code(s, REPZ_3_10, s.bl_tree)
1271
+ send_bits(s, count - 3, 3)
1272
+ } else {
1273
+ send_code(s, REPZ_11_138, s.bl_tree)
1274
+ send_bits(s, count - 11, 7)
1275
+ }
1276
+
1277
+ count = 0
1278
+ prevlen = curlen
1279
+
1280
+ if (nextlen === 0) {
1281
+ max_count = 138
1282
+ min_count = 3
1283
+ } else if (curlen === nextlen) {
1284
+ max_count = 6
1285
+ min_count = 3
1286
+ } else {
1287
+ max_count = 7
1288
+ min_count = 4
1289
+ }
1290
+ }
1291
+ }
1292
+ /* ===========================================================================
1293
+ * Construct the Huffman tree for the bit lengths and return the index in
1294
+ * bl_order of the last bit length code to send.
1295
+ */
1296
+
1297
+ var build_bl_tree = function build_bl_tree(s) {
1298
+ var max_blindex
1299
+ /* index of last bit length code of non zero freq */
1300
+
1301
+ /* Determine the bit length frequencies for literal and distance trees */
1302
+
1303
+ scan_tree(s, s.dyn_ltree, s.l_desc.max_code)
1304
+ scan_tree(s, s.dyn_dtree, s.d_desc.max_code)
1305
+ /* Build the bit length tree: */
1306
+
1307
+ build_tree(s, s.bl_desc)
1308
+ /* opt_len now includes the length of the tree representations, except
1309
+ * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
1310
+ */
1311
+
1312
+ /* Determine the number of bit length codes to send. The pkzip format
1313
+ * requires that at least 4 bit length codes be sent. (appnote.txt says
1314
+ * 3 but the actual value used is 4.)
1315
+ */
1316
+
1317
+ for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
1318
+ if (
1319
+ s.bl_tree[bl_order[max_blindex] * 2 + 1] !==
1320
+ /* .Len */
1321
+ 0
1322
+ ) {
1323
+ break
1324
+ }
1325
+ }
1326
+ /* Update opt_len to include the bit length tree and counts */
1327
+
1328
+ s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4 // Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
1329
+ // s->opt_len, s->static_len));
1330
+
1331
+ return max_blindex
1332
+ }
1333
+ /* ===========================================================================
1334
+ * Send the header for a block using dynamic Huffman trees: the counts, the
1335
+ * lengths of the bit length codes, the literal tree and the distance tree.
1336
+ * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
1337
+ */
1338
+
1339
+ var send_all_trees = function send_all_trees(
1340
+ s,
1341
+ lcodes,
1342
+ dcodes,
1343
+ blcodes // deflate_state *s; // int lcodes, dcodes, blcodes; /* number of codes for each tree */
1344
+ ) {
1345
+ var rank
1346
+ /* index in bl_order */
1347
+ // Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
1348
+ // Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
1349
+ // "too many codes");
1350
+ // Tracev((stderr, "\nbl counts: "));
1351
+
1352
+ send_bits(s, lcodes - 257, 5)
1353
+ /* not +255 as stated in appnote.txt */
1354
+
1355
+ send_bits(s, dcodes - 1, 5)
1356
+ send_bits(s, blcodes - 4, 4)
1357
+ /* not -3 as stated in appnote.txt */
1358
+
1359
+ for (rank = 0; rank < blcodes; rank++) {
1360
+ // Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
1361
+ send_bits(
1362
+ s,
1363
+ s.bl_tree[bl_order[rank] * 2 + 1],
1364
+ /* .Len */
1365
+ 3
1366
+ )
1367
+ } // Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
1368
+
1369
+ send_tree(s, s.dyn_ltree, lcodes - 1)
1370
+ /* literal tree */
1371
+ // Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
1372
+
1373
+ send_tree(s, s.dyn_dtree, dcodes - 1)
1374
+ /* distance tree */
1375
+ // Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
1376
+ }
1377
+ /* ===========================================================================
1378
+ * Check if the data type is TEXT or BINARY, using the following algorithm:
1379
+ * - TEXT if the two conditions below are satisfied:
1380
+ * a) There are no non-portable control characters belonging to the
1381
+ * "black list" (0..6, 14..25, 28..31).
1382
+ * b) There is at least one printable character belonging to the
1383
+ * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
1384
+ * - BINARY otherwise.
1385
+ * - The following partially-portable control characters form a
1386
+ * "gray list" that is ignored in this detection algorithm:
1387
+ * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
1388
+ * IN assertion: the fields Freq of dyn_ltree are set.
1389
+ */
1390
+
1391
+ var detect_data_type = function detect_data_type(s) {
1392
+ /* black_mask is the bit mask of black-listed bytes
1393
+ * set bits 0..6, 14..25, and 28..31
1394
+ * 0xf3ffc07f = binary 11110011111111111100000001111111
1395
+ */
1396
+ var black_mask = 0xf3ffc07f
1397
+ var n
1398
+ /* Check for non-textual ("black-listed") bytes. */
1399
+
1400
+ for (n = 0; n <= 31; n++, black_mask >>>= 1) {
1401
+ if (
1402
+ black_mask & 1 &&
1403
+ s.dyn_ltree[n * 2] !==
1404
+ /* .Freq */
1405
+ 0
1406
+ ) {
1407
+ return Z_BINARY
1408
+ }
1409
+ }
1410
+ /* Check for textual ("white-listed") bytes. */
1411
+
1412
+ if (
1413
+ s.dyn_ltree[9 * 2] !==
1414
+ /* .Freq */
1415
+ 0 ||
1416
+ s.dyn_ltree[10 * 2] !==
1417
+ /* .Freq */
1418
+ 0 ||
1419
+ s.dyn_ltree[13 * 2] !==
1420
+ /* .Freq */
1421
+ 0
1422
+ ) {
1423
+ return Z_TEXT
1424
+ }
1425
+
1426
+ for (n = 32; n < LITERALS; n++) {
1427
+ if (
1428
+ s.dyn_ltree[n * 2] !==
1429
+ /* .Freq */
1430
+ 0
1431
+ ) {
1432
+ return Z_TEXT
1433
+ }
1434
+ }
1435
+ /* There are no "black-listed" or "white-listed" bytes:
1436
+ * this stream either is empty or has tolerated ("gray-listed") bytes only.
1437
+ */
1438
+
1439
+ return Z_BINARY
1440
+ }
1441
+
1442
+ var static_init_done = false
1443
+ /* ===========================================================================
1444
+ * Initialize the tree data structures for a new zlib stream.
1445
+ */
1446
+
1447
+ var _tr_init = function _tr_init(s) {
1448
+ if (!static_init_done) {
1449
+ tr_static_init()
1450
+ static_init_done = true
1451
+ }
1452
+
1453
+ s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc)
1454
+ s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc)
1455
+ s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc)
1456
+ s.bi_buf = 0
1457
+ s.bi_valid = 0
1458
+ /* Initialize the first block of the first file: */
1459
+
1460
+ init_block(s)
1461
+ }
1462
+ /* ===========================================================================
1463
+ * Send a stored block
1464
+ */
1465
+
1466
+ var _tr_stored_block = function _tr_stored_block(
1467
+ s,
1468
+ buf,
1469
+ stored_len,
1470
+ last
1471
+ // DeflateState *s;
1472
+ // charf *buf; /* input block */
1473
+ // ulg stored_len; /* length of input block */
1474
+ // int last; /* one if this is the last block for a file */
1475
+ ) {
1476
+ send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3)
1477
+ /* send block type */
1478
+
1479
+ copy_block(s, buf, stored_len, true)
1480
+ /* with header */
1481
+ }
1482
+ /* ===========================================================================
1483
+ * Send one empty static block to give enough lookahead for inflate.
1484
+ * This takes 10 bits, of which 7 may remain in the bit buffer.
1485
+ */
1486
+
1487
+ var _tr_align = function _tr_align(s) {
1488
+ send_bits(s, STATIC_TREES << 1, 3)
1489
+ send_code(s, END_BLOCK, static_ltree)
1490
+ bi_flush(s)
1491
+ }
1492
+ /* ===========================================================================
1493
+ * Determine the best encoding for the current block: dynamic trees, static
1494
+ * trees or store, and output the encoded block to the zip file.
1495
+ */
1496
+
1497
+ var _tr_flush_block = function _tr_flush_block(
1498
+ s,
1499
+ buf,
1500
+ stored_len,
1501
+ last
1502
+ // DeflateState *s;
1503
+ // charf *buf; /* input block, or NULL if too old */
1504
+ // ulg stored_len; /* length of input block */
1505
+ // int last; /* one if this is the last block for a file */
1506
+ ) {
1507
+ var opt_lenb
1508
+ var static_lenb
1509
+ /* opt_len and static_len in bytes */
1510
+
1511
+ var max_blindex = 0
1512
+ /* index of last bit length code of non zero freq */
1513
+
1514
+ /* Build the Huffman trees unless a stored block is forced */
1515
+
1516
+ if (s.level > 0) {
1517
+ /* Check if the file is binary or text */
1518
+ if (s.strm.data_type === Z_UNKNOWN) {
1519
+ s.strm.data_type = detect_data_type(s)
1520
+ }
1521
+ /* Construct the literal and distance trees */
1522
+
1523
+ build_tree(s, s.l_desc) // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
1524
+ // s->static_len));
1525
+
1526
+ build_tree(s, s.d_desc) // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
1527
+ // s->static_len));
1528
+
1529
+ /* At this point, opt_len and static_len are the total bit lengths of
1530
+ * the compressed block data, excluding the tree representations.
1531
+ */
1532
+
1533
+ /* Build the bit length tree for the above two trees, and get the index
1534
+ * in bl_order of the last bit length code to send.
1535
+ */
1536
+
1537
+ max_blindex = build_bl_tree(s)
1538
+ /* Determine the best encoding. Compute the block lengths in bytes. */
1539
+
1540
+ opt_lenb = (s.opt_len + 3 + 7) >>> 3
1541
+ static_lenb = (s.static_len + 3 + 7) >>> 3 // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
1542
+ // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
1543
+ // s->last_lit));
1544
+
1545
+ if (static_lenb <= opt_lenb) {
1546
+ opt_lenb = static_lenb
1547
+ }
1548
+ } else {
1549
+ // Assert(buf != (char*)0, "lost buf");
1550
+ opt_lenb = static_lenb = stored_len + 5
1551
+ /* force a stored block */
1552
+ }
1553
+
1554
+ if (stored_len + 4 <= opt_lenb && buf !== -1) {
1555
+ /* 4: two words for the lengths */
1556
+
1557
+ /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
1558
+ * Otherwise we can't have processed more than WSIZE input bytes since
1559
+ * the last block flush, because compression would have been
1560
+ * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
1561
+ * transform a block into a stored block.
1562
+ */
1563
+ _tr_stored_block(s, buf, stored_len, last)
1564
+ } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
1565
+ send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3)
1566
+ compress_block(s, static_ltree, static_dtree)
1567
+ } else {
1568
+ send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3)
1569
+ send_all_trees(
1570
+ s,
1571
+ s.l_desc.max_code + 1,
1572
+ s.d_desc.max_code + 1,
1573
+ max_blindex + 1
1574
+ )
1575
+ compress_block(s, s.dyn_ltree, s.dyn_dtree)
1576
+ } // Assert (s->compressed_len == s->bits_sent, "bad compressed size");
1577
+
1578
+ /* The above check is made mod 2^32, for files larger than 512 MB
1579
+ * and uLong implemented on 32 bits.
1580
+ */
1581
+
1582
+ init_block(s)
1583
+
1584
+ if (last) {
1585
+ bi_windup(s)
1586
+ } // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1587
+ // s->compressed_len-7*last));
1588
+ }
1589
+ /* ===========================================================================
1590
+ * Save the match info and tally the frequency counts. Return true if
1591
+ * the current block must be flushed.
1592
+ */
1593
+
1594
+ var _tr_tally = function _tr_tally(
1595
+ s,
1596
+ dist,
1597
+ lc
1598
+ // deflate_state *s;
1599
+ // unsigned dist; /* distance of matched string */
1600
+ // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
1601
+ ) {
1602
+ // let out_length, in_length, dcode;
1603
+ s.pending_buf[s.d_buf + s.last_lit * 2] = (dist >>> 8) & 0xff
1604
+ s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff
1605
+ s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff
1606
+ s.last_lit++
1607
+
1608
+ if (dist === 0) {
1609
+ /* lc is the unmatched char */
1610
+ s.dyn_ltree[lc * 2] /* .Freq */++
1611
+ } else {
1612
+ s.matches++
1613
+ /* Here, lc is the match length - MIN_MATCH */
1614
+
1615
+ dist--
1616
+ /* dist = match distance - 1 */
1617
+ // Assert((ush)dist < (ush)MAX_DIST(s) &&
1618
+ // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1619
+ // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1620
+
1621
+ s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2] /* .Freq */++
1622
+ s.dyn_dtree[d_code(dist) * 2] /* .Freq */++
1623
+ } // (!) This block is disabled in zlib defaults,
1624
+ // don't enable it for binary compatibility
1625
+ // #ifdef TRUNCATE_BLOCK
1626
+ // /* Try to guess if it is profitable to stop the current block here */
1627
+ // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) {
1628
+ // /* Compute an upper bound for the compressed length */
1629
+ // out_length = s.last_lit*8;
1630
+ // in_length = s.strstart - s.block_start;
1631
+ //
1632
+ // for (dcode = 0; dcode < D_CODES; dcode++) {
1633
+ // out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]);
1634
+ // }
1635
+ // out_length >>>= 3;
1636
+ // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1637
+ // // s->last_lit, in_length, out_length,
1638
+ // // 100L - out_length*100L/in_length));
1639
+ // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) {
1640
+ // return true;
1641
+ // }
1642
+ // }
1643
+ // #endif
1644
+
1645
+ return s.last_lit === s.lit_bufsize - 1
1646
+ /* We avoid equality with lit_bufsize because of wraparound at 64K
1647
+ * on 16 bit machines and because stored blocks are restricted to
1648
+ * 64K-1 bytes.
1649
+ */
1650
+ }
1651
+
1652
+ var _tr_init_1 = _tr_init
1653
+ var _tr_stored_block_1 = _tr_stored_block
1654
+ var _tr_flush_block_1 = _tr_flush_block
1655
+ var _tr_tally_1 = _tr_tally
1656
+ var _tr_align_1 = _tr_align
1657
+ var trees = {
1658
+ _tr_init: _tr_init_1,
1659
+ _tr_stored_block: _tr_stored_block_1,
1660
+ _tr_flush_block: _tr_flush_block_1,
1661
+ _tr_tally: _tr_tally_1,
1662
+ _tr_align: _tr_align_1
1663
+ }
1664
+
1665
+ // It isn't worth it to make additional optimizations as in original.
1666
+ // Small size is preferable.
1667
+ // (C) 1995-2013 Jean-loup Gailly and Mark Adler
1668
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
1669
+ //
1670
+ // This software is provided 'as-is', without any express or implied
1671
+ // warranty. In no event will the authors be held liable for any damages
1672
+ // arising from the use of this software.
1673
+ //
1674
+ // Permission is granted to anyone to use this software for any purpose,
1675
+ // including commercial applications, and to alter it and redistribute it
1676
+ // freely, subject to the following restrictions:
1677
+ //
1678
+ // 1. The origin of this software must not be misrepresented; you must not
1679
+ // claim that you wrote the original software. If you use this software
1680
+ // in a product, an acknowledgment in the product documentation would be
1681
+ // appreciated but is not required.
1682
+ // 2. Altered source versions must be plainly marked as such, and must not be
1683
+ // misrepresented as being the original software.
1684
+ // 3. This notice may not be removed or altered from any source distribution.
1685
+
1686
+ var adler32 = function adler32(adler, buf, len, pos) {
1687
+ var s1 = (adler & 0xffff) | 0
1688
+ var s2 = ((adler >>> 16) & 0xffff) | 0
1689
+ var n = 0
1690
+
1691
+ while (len !== 0) {
1692
+ // Set limit ~ twice less than 5552, to keep
1693
+ // s2 in 31-bits, because we force signed ints.
1694
+ // in other case %= will fail.
1695
+ n = len > 2000 ? 2000 : len
1696
+ len -= n
1697
+
1698
+ do {
1699
+ s1 = (s1 + buf[pos++]) | 0
1700
+ s2 = (s2 + s1) | 0
1701
+ } while (--n)
1702
+
1703
+ s1 %= 65521
1704
+ s2 %= 65521
1705
+ }
1706
+
1707
+ return s1 | (s2 << 16) | 0
1708
+ }
1709
+
1710
+ var adler32_1 = adler32
1711
+
1712
+ // So write code to minimize size - no pregenerated tables
1713
+ // and array tools dependencies.
1714
+ // (C) 1995-2013 Jean-loup Gailly and Mark Adler
1715
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
1716
+ //
1717
+ // This software is provided 'as-is', without any express or implied
1718
+ // warranty. In no event will the authors be held liable for any damages
1719
+ // arising from the use of this software.
1720
+ //
1721
+ // Permission is granted to anyone to use this software for any purpose,
1722
+ // including commercial applications, and to alter it and redistribute it
1723
+ // freely, subject to the following restrictions:
1724
+ //
1725
+ // 1. The origin of this software must not be misrepresented; you must not
1726
+ // claim that you wrote the original software. If you use this software
1727
+ // in a product, an acknowledgment in the product documentation would be
1728
+ // appreciated but is not required.
1729
+ // 2. Altered source versions must be plainly marked as such, and must not be
1730
+ // misrepresented as being the original software.
1731
+ // 3. This notice may not be removed or altered from any source distribution.
1732
+ // Use ordinary array, since untyped makes no boost here
1733
+
1734
+ var makeTable = function makeTable() {
1735
+ var c
1736
+ var table = []
1737
+
1738
+ for (var n = 0; n < 256; n++) {
1739
+ c = n
1740
+
1741
+ for (var k = 0; k < 8; k++) {
1742
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
1743
+ }
1744
+
1745
+ table[n] = c
1746
+ }
1747
+
1748
+ return table
1749
+ } // Create table on load. Just 255 signed longs. Not a problem.
1750
+
1751
+ var crcTable = new Uint32Array(makeTable())
1752
+
1753
+ var crc32 = function crc32(crc, buf, len, pos) {
1754
+ var t = crcTable
1755
+ var end = pos + len
1756
+ crc ^= -1
1757
+
1758
+ for (var i = pos; i < end; i++) {
1759
+ crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xff]
1760
+ }
1761
+
1762
+ return crc ^ -1 // >>> 0;
1763
+ }
1764
+
1765
+ var crc32_1 = crc32
1766
+
1767
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
1768
+ //
1769
+ // This software is provided 'as-is', without any express or implied
1770
+ // warranty. In no event will the authors be held liable for any damages
1771
+ // arising from the use of this software.
1772
+ //
1773
+ // Permission is granted to anyone to use this software for any purpose,
1774
+ // including commercial applications, and to alter it and redistribute it
1775
+ // freely, subject to the following restrictions:
1776
+ //
1777
+ // 1. The origin of this software must not be misrepresented; you must not
1778
+ // claim that you wrote the original software. If you use this software
1779
+ // in a product, an acknowledgment in the product documentation would be
1780
+ // appreciated but is not required.
1781
+ // 2. Altered source versions must be plainly marked as such, and must not be
1782
+ // misrepresented as being the original software.
1783
+ // 3. This notice may not be removed or altered from any source distribution.
1784
+
1785
+ var messages = {
1786
+ 2: 'need dictionary',
1787
+
1788
+ /* Z_NEED_DICT 2 */
1789
+ 1: 'stream end',
1790
+
1791
+ /* Z_STREAM_END 1 */
1792
+ 0: '',
1793
+
1794
+ /* Z_OK 0 */
1795
+ '-1': 'file error',
1796
+
1797
+ /* Z_ERRNO (-1) */
1798
+ '-2': 'stream error',
1799
+
1800
+ /* Z_STREAM_ERROR (-2) */
1801
+ '-3': 'data error',
1802
+
1803
+ /* Z_DATA_ERROR (-3) */
1804
+ '-4': 'insufficient memory',
1805
+
1806
+ /* Z_MEM_ERROR (-4) */
1807
+ '-5': 'buffer error',
1808
+
1809
+ /* Z_BUF_ERROR (-5) */
1810
+ '-6': 'incompatible version'
1811
+ /* Z_VERSION_ERROR (-6) */
1812
+ }
1813
+
1814
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
1815
+ //
1816
+ // This software is provided 'as-is', without any express or implied
1817
+ // warranty. In no event will the authors be held liable for any damages
1818
+ // arising from the use of this software.
1819
+ //
1820
+ // Permission is granted to anyone to use this software for any purpose,
1821
+ // including commercial applications, and to alter it and redistribute it
1822
+ // freely, subject to the following restrictions:
1823
+ //
1824
+ // 1. The origin of this software must not be misrepresented; you must not
1825
+ // claim that you wrote the original software. If you use this software
1826
+ // in a product, an acknowledgment in the product documentation would be
1827
+ // appreciated but is not required.
1828
+ // 2. Altered source versions must be plainly marked as such, and must not be
1829
+ // misrepresented as being the original software.
1830
+ // 3. This notice may not be removed or altered from any source distribution.
1831
+
1832
+ var constants = {
1833
+ /* Allowed flush values; see deflate() and inflate() below for details */
1834
+ Z_NO_FLUSH: 0,
1835
+ Z_PARTIAL_FLUSH: 1,
1836
+ Z_SYNC_FLUSH: 2,
1837
+ Z_FULL_FLUSH: 3,
1838
+ Z_FINISH: 4,
1839
+ Z_BLOCK: 5,
1840
+ Z_TREES: 6,
1841
+
1842
+ /* Return codes for the compression/decompression functions. Negative values
1843
+ * are errors, positive values are used for special but normal events.
1844
+ */
1845
+ Z_OK: 0,
1846
+ Z_STREAM_END: 1,
1847
+ Z_NEED_DICT: 2,
1848
+ Z_ERRNO: -1,
1849
+ Z_STREAM_ERROR: -2,
1850
+ Z_DATA_ERROR: -3,
1851
+ Z_MEM_ERROR: -4,
1852
+ Z_BUF_ERROR: -5,
1853
+ // Z_VERSION_ERROR: -6,
1854
+
1855
+ /* compression levels */
1856
+ Z_NO_COMPRESSION: 0,
1857
+ Z_BEST_SPEED: 1,
1858
+ Z_BEST_COMPRESSION: 9,
1859
+ Z_DEFAULT_COMPRESSION: -1,
1860
+ Z_FILTERED: 1,
1861
+ Z_HUFFMAN_ONLY: 2,
1862
+ Z_RLE: 3,
1863
+ Z_FIXED: 4,
1864
+ Z_DEFAULT_STRATEGY: 0,
1865
+
1866
+ /* Possible values of the data_type field (though see inflate()) */
1867
+ Z_BINARY: 0,
1868
+ Z_TEXT: 1,
1869
+ // Z_ASCII: 1, // = Z_TEXT (deprecated)
1870
+ Z_UNKNOWN: 2,
1871
+
1872
+ /* The deflate compression method */
1873
+ Z_DEFLATED: 8 // Z_NULL: null // Use -1 or null inline, depending on var type
1874
+ }
1875
+
1876
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
1877
+ //
1878
+ // This software is provided 'as-is', without any express or implied
1879
+ // warranty. In no event will the authors be held liable for any damages
1880
+ // arising from the use of this software.
1881
+ //
1882
+ // Permission is granted to anyone to use this software for any purpose,
1883
+ // including commercial applications, and to alter it and redistribute it
1884
+ // freely, subject to the following restrictions:
1885
+ //
1886
+ // 1. The origin of this software must not be misrepresented; you must not
1887
+ // claim that you wrote the original software. If you use this software
1888
+ // in a product, an acknowledgment in the product documentation would be
1889
+ // appreciated but is not required.
1890
+ // 2. Altered source versions must be plainly marked as such, and must not be
1891
+ // misrepresented as being the original software.
1892
+ // 3. This notice may not be removed or altered from any source distribution.
1893
+
1894
+ var _tr_init$1 = trees._tr_init
1895
+ var _tr_stored_block$1 = trees._tr_stored_block
1896
+ var _tr_flush_block$1 = trees._tr_flush_block
1897
+ var _tr_tally$1 = trees._tr_tally
1898
+ var _tr_align$1 = trees._tr_align
1899
+ /* Public constants ========================================================== */
1900
+
1901
+ /* =========================================================================== */
1902
+
1903
+ var Z_NO_FLUSH = constants.Z_NO_FLUSH
1904
+ var Z_PARTIAL_FLUSH = constants.Z_PARTIAL_FLUSH
1905
+ var Z_FULL_FLUSH = constants.Z_FULL_FLUSH
1906
+ var Z_FINISH = constants.Z_FINISH
1907
+ var Z_BLOCK = constants.Z_BLOCK
1908
+ var Z_OK = constants.Z_OK
1909
+ var Z_STREAM_END = constants.Z_STREAM_END
1910
+ var Z_STREAM_ERROR = constants.Z_STREAM_ERROR
1911
+ var Z_DATA_ERROR = constants.Z_DATA_ERROR
1912
+ var Z_BUF_ERROR = constants.Z_BUF_ERROR
1913
+ var Z_DEFAULT_COMPRESSION = constants.Z_DEFAULT_COMPRESSION
1914
+ var Z_FILTERED = constants.Z_FILTERED
1915
+ var Z_HUFFMAN_ONLY = constants.Z_HUFFMAN_ONLY
1916
+ var Z_RLE = constants.Z_RLE
1917
+ var Z_FIXED$1 = constants.Z_FIXED
1918
+ var Z_DEFAULT_STRATEGY = constants.Z_DEFAULT_STRATEGY
1919
+ var Z_UNKNOWN$1 = constants.Z_UNKNOWN
1920
+ var Z_DEFLATED = constants.Z_DEFLATED
1921
+ /* ============================================================================ */
1922
+
1923
+ var MAX_MEM_LEVEL = 9
1924
+ /* Maximum value for memLevel in deflateInit2 */
1925
+
1926
+ var MAX_WBITS = 15
1927
+ /* 32K LZ77 window */
1928
+
1929
+ var DEF_MEM_LEVEL = 8
1930
+ var LENGTH_CODES$1 = 29
1931
+ /* number of length codes, not counting the special END_BLOCK code */
1932
+
1933
+ var LITERALS$1 = 256
1934
+ /* number of literal bytes 0..255 */
1935
+
1936
+ var L_CODES$1 = LITERALS$1 + 1 + LENGTH_CODES$1
1937
+ /* number of Literal or Length codes, including the END_BLOCK code */
1938
+
1939
+ var D_CODES$1 = 30
1940
+ /* number of distance codes */
1941
+
1942
+ var BL_CODES$1 = 19
1943
+ /* number of codes used to transfer the bit lengths */
1944
+
1945
+ var HEAP_SIZE$1 = 2 * L_CODES$1 + 1
1946
+ /* maximum heap size */
1947
+
1948
+ var MAX_BITS$1 = 15
1949
+ /* All codes must not exceed MAX_BITS bits */
1950
+
1951
+ var MIN_MATCH$1 = 3
1952
+ var MAX_MATCH$1 = 258
1953
+ var MIN_LOOKAHEAD = MAX_MATCH$1 + MIN_MATCH$1 + 1
1954
+ var PRESET_DICT = 0x20
1955
+ var INIT_STATE = 42
1956
+ var EXTRA_STATE = 69
1957
+ var NAME_STATE = 73
1958
+ var COMMENT_STATE = 91
1959
+ var HCRC_STATE = 103
1960
+ var BUSY_STATE = 113
1961
+ var FINISH_STATE = 666
1962
+ var BS_NEED_MORE = 1
1963
+ /* block not completed, need more input or more output */
1964
+
1965
+ var BS_BLOCK_DONE = 2
1966
+ /* block flush performed */
1967
+
1968
+ var BS_FINISH_STARTED = 3
1969
+ /* finish started, need only more output at next deflate */
1970
+
1971
+ var BS_FINISH_DONE = 4
1972
+ /* finish done, accept no more input or output */
1973
+
1974
+ var OS_CODE = 0x03 // Unix :) . Don't detect, use this default.
1975
+
1976
+ var err = function err(strm, errorCode) {
1977
+ strm.msg = messages[errorCode]
1978
+ return errorCode
1979
+ }
1980
+
1981
+ var rank = function rank(f) {
1982
+ return (f << 1) - (f > 4 ? 9 : 0)
1983
+ }
1984
+
1985
+ var zero$1 = function zero(buf) {
1986
+ var len = buf.length
1987
+
1988
+ while (--len >= 0) {
1989
+ buf[len] = 0
1990
+ }
1991
+ }
1992
+
1993
+ var HASH_ZLIB = function HASH_ZLIB(s, prev, data) {
1994
+ return ((prev << s.hash_shift) ^ data) & s.hash_mask
1995
+ } // This hash causes less collisions, https://github.com/nodeca/pako/issues/135
1996
+ // But breaks binary compatibility
1997
+ // let HASH_FAST = (s, prev, data) => ((prev << 8) + (prev >> 8) + (data << 4)) & s.hash_mask;
1998
+
1999
+ var HASH = HASH_ZLIB
2000
+ /* =========================================================================
2001
+ * Flush as much pending output as possible. All deflate() output goes
2002
+ * through this function so some applications may wish to modify it
2003
+ * to avoid allocating a large strm->output buffer and copying into it.
2004
+ * (See also read_buf()).
2005
+ */
2006
+
2007
+ var flush_pending = function flush_pending(strm) {
2008
+ var s = strm.state // _tr_flush_bits(s);
2009
+
2010
+ var len = s.pending
2011
+
2012
+ if (len > strm.avail_out) {
2013
+ len = strm.avail_out
2014
+ }
2015
+
2016
+ if (len === 0) {
2017
+ return
2018
+ }
2019
+
2020
+ strm.output.set(
2021
+ s.pending_buf.subarray(s.pending_out, s.pending_out + len),
2022
+ strm.next_out
2023
+ )
2024
+ strm.next_out += len
2025
+ s.pending_out += len
2026
+ strm.total_out += len
2027
+ strm.avail_out -= len
2028
+ s.pending -= len
2029
+
2030
+ if (s.pending === 0) {
2031
+ s.pending_out = 0
2032
+ }
2033
+ }
2034
+
2035
+ var flush_block_only = function flush_block_only(s, last) {
2036
+ _tr_flush_block$1(
2037
+ s,
2038
+ s.block_start >= 0 ? s.block_start : -1,
2039
+ s.strstart - s.block_start,
2040
+ last
2041
+ )
2042
+
2043
+ s.block_start = s.strstart
2044
+ flush_pending(s.strm)
2045
+ }
2046
+
2047
+ var put_byte = function put_byte(s, b) {
2048
+ s.pending_buf[s.pending++] = b
2049
+ }
2050
+ /* =========================================================================
2051
+ * Put a short in the pending buffer. The 16-bit value is put in MSB order.
2052
+ * IN assertion: the stream state is correct and there is enough room in
2053
+ * pending_buf.
2054
+ */
2055
+
2056
+ var putShortMSB = function putShortMSB(s, b) {
2057
+ // put_byte(s, (Byte)(b >> 8));
2058
+ // put_byte(s, (Byte)(b & 0xff));
2059
+ s.pending_buf[s.pending++] = (b >>> 8) & 0xff
2060
+ s.pending_buf[s.pending++] = b & 0xff
2061
+ }
2062
+ /* ===========================================================================
2063
+ * Read a new buffer from the current input stream, update the adler32
2064
+ * and total number of bytes read. All deflate() input goes through
2065
+ * this function so some applications may wish to modify it to avoid
2066
+ * allocating a large strm->input buffer and copying from it.
2067
+ * (See also flush_pending()).
2068
+ */
2069
+
2070
+ var read_buf = function read_buf(strm, buf, start, size) {
2071
+ var len = strm.avail_in
2072
+
2073
+ if (len > size) {
2074
+ len = size
2075
+ }
2076
+
2077
+ if (len === 0) {
2078
+ return 0
2079
+ }
2080
+
2081
+ strm.avail_in -= len // zmemcpy(buf, strm->next_in, len);
2082
+
2083
+ buf.set(strm.input.subarray(strm.next_in, strm.next_in + len), start)
2084
+
2085
+ if (strm.state.wrap === 1) {
2086
+ strm.adler = adler32_1(strm.adler, buf, len, start)
2087
+ } else if (strm.state.wrap === 2) {
2088
+ strm.adler = crc32_1(strm.adler, buf, len, start)
2089
+ }
2090
+
2091
+ strm.next_in += len
2092
+ strm.total_in += len
2093
+ return len
2094
+ }
2095
+ /* ===========================================================================
2096
+ * Set match_start to the longest match starting at the given string and
2097
+ * return its length. Matches shorter or equal to prev_length are discarded,
2098
+ * in which case the result is equal to prev_length and match_start is
2099
+ * garbage.
2100
+ * IN assertions: cur_match is the head of the hash chain for the current
2101
+ * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
2102
+ * OUT assertion: the match length is not greater than s->lookahead.
2103
+ */
2104
+
2105
+ var longest_match = function longest_match(s, cur_match) {
2106
+ var chain_length = s.max_chain_length
2107
+ /* max hash chain length */
2108
+
2109
+ var scan = s.strstart
2110
+ /* current string */
2111
+
2112
+ var match
2113
+ /* matched string */
2114
+
2115
+ var len
2116
+ /* length of current match */
2117
+
2118
+ var best_len = s.prev_length
2119
+ /* best match length so far */
2120
+
2121
+ var nice_match = s.nice_match
2122
+ /* stop if match long enough */
2123
+
2124
+ var limit =
2125
+ s.strstart > s.w_size - MIN_LOOKAHEAD
2126
+ ? s.strstart - (s.w_size - MIN_LOOKAHEAD)
2127
+ : 0
2128
+ /* NIL */
2129
+ var _win = s.window // shortcut
2130
+
2131
+ var wmask = s.w_mask
2132
+ var prev = s.prev
2133
+ /* Stop when cur_match becomes <= limit. To simplify the code,
2134
+ * we prevent matches with the string of window index 0.
2135
+ */
2136
+
2137
+ var strend = s.strstart + MAX_MATCH$1
2138
+ var scan_end1 = _win[scan + best_len - 1]
2139
+ var scan_end = _win[scan + best_len]
2140
+ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
2141
+ * It is easy to get rid of this optimization if necessary.
2142
+ */
2143
+ // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
2144
+
2145
+ /* Do not waste too much time if we already have a good match: */
2146
+
2147
+ if (s.prev_length >= s.good_match) {
2148
+ chain_length >>= 2
2149
+ }
2150
+ /* Do not look for matches beyond the end of the input. This is necessary
2151
+ * to make deflate deterministic.
2152
+ */
2153
+
2154
+ if (nice_match > s.lookahead) {
2155
+ nice_match = s.lookahead
2156
+ } // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
2157
+
2158
+ do {
2159
+ // Assert(cur_match < s->strstart, "no future");
2160
+ match = cur_match
2161
+ /* Skip to next match if the match length cannot increase
2162
+ * or if the match length is less than 2. Note that the checks below
2163
+ * for insufficient lookahead only occur occasionally for performance
2164
+ * reasons. Therefore uninitialized memory will be accessed, and
2165
+ * conditional jumps will be made that depend on those values.
2166
+ * However the length of the match is limited to the lookahead, so
2167
+ * the output of deflate is not affected by the uninitialized values.
2168
+ */
2169
+
2170
+ if (
2171
+ _win[match + best_len] !== scan_end ||
2172
+ _win[match + best_len - 1] !== scan_end1 ||
2173
+ _win[match] !== _win[scan] ||
2174
+ _win[++match] !== _win[scan + 1]
2175
+ ) {
2176
+ continue
2177
+ }
2178
+ /* The check at best_len-1 can be removed because it will be made
2179
+ * again later. (This heuristic is not always a win.)
2180
+ * It is not necessary to compare scan[2] and match[2] since they
2181
+ * are always equal when the other bytes match, given that
2182
+ * the hash keys are equal and that HASH_BITS >= 8.
2183
+ */
2184
+
2185
+ scan += 2
2186
+ match++ // Assert(*scan == *match, "match[2]?");
2187
+
2188
+ /* We check for insufficient lookahead only every 8th comparison;
2189
+ * the 256th check will be made at strstart+258.
2190
+ */
2191
+
2192
+ do {
2193
+ /* jshint noempty:false */
2194
+ } while (
2195
+ _win[++scan] === _win[++match] &&
2196
+ _win[++scan] === _win[++match] &&
2197
+ _win[++scan] === _win[++match] &&
2198
+ _win[++scan] === _win[++match] &&
2199
+ _win[++scan] === _win[++match] &&
2200
+ _win[++scan] === _win[++match] &&
2201
+ _win[++scan] === _win[++match] &&
2202
+ _win[++scan] === _win[++match] &&
2203
+ scan < strend
2204
+ ) // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
2205
+
2206
+ len = MAX_MATCH$1 - (strend - scan)
2207
+ scan = strend - MAX_MATCH$1
2208
+
2209
+ if (len > best_len) {
2210
+ s.match_start = cur_match
2211
+ best_len = len
2212
+
2213
+ if (len >= nice_match) {
2214
+ break
2215
+ }
2216
+
2217
+ scan_end1 = _win[scan + best_len - 1]
2218
+ scan_end = _win[scan + best_len]
2219
+ }
2220
+ } while (
2221
+ (cur_match = prev[cur_match & wmask]) > limit &&
2222
+ --chain_length !== 0
2223
+ )
2224
+
2225
+ if (best_len <= s.lookahead) {
2226
+ return best_len
2227
+ }
2228
+
2229
+ return s.lookahead
2230
+ }
2231
+ /* ===========================================================================
2232
+ * Fill the window when the lookahead becomes insufficient.
2233
+ * Updates strstart and lookahead.
2234
+ *
2235
+ * IN assertion: lookahead < MIN_LOOKAHEAD
2236
+ * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
2237
+ * At least one byte has been read, or avail_in == 0; reads are
2238
+ * performed for at least two bytes (required for the zip translate_eol
2239
+ * option -- not supported here).
2240
+ */
2241
+
2242
+ var fill_window = function fill_window(s) {
2243
+ var _w_size = s.w_size
2244
+ var p
2245
+ var n
2246
+ var m
2247
+ var more
2248
+ var str // Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
2249
+
2250
+ do {
2251
+ more = s.window_size - s.lookahead - s.strstart // JS ints have 32 bit, block below not needed
2252
+
2253
+ /* Deal with !@#$% 64K limit: */
2254
+ // if (sizeof(int) <= 2) {
2255
+ // if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
2256
+ // more = wsize;
2257
+ //
2258
+ // } else if (more == (unsigned)(-1)) {
2259
+ // /* Very unlikely, but possible on 16 bit machine if
2260
+ // * strstart == 0 && lookahead == 1 (input done a byte at time)
2261
+ // */
2262
+ // more--;
2263
+ // }
2264
+ // }
2265
+
2266
+ /* If the window is almost full and there is insufficient lookahead,
2267
+ * move the upper half to the lower one to make room in the upper half.
2268
+ */
2269
+
2270
+ if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
2271
+ s.window.set(s.window.subarray(_w_size, _w_size + _w_size), 0)
2272
+ s.match_start -= _w_size
2273
+ s.strstart -= _w_size
2274
+ /* we now have strstart >= MAX_DIST */
2275
+
2276
+ s.block_start -= _w_size
2277
+ /* Slide the hash table (could be avoided with 32 bit values
2278
+ at the expense of memory usage). We slide even when level == 0
2279
+ to keep the hash table consistent if we switch back to level > 0
2280
+ later. (Using level 0 permanently is not an optimal usage of
2281
+ zlib, so we don't care about this pathological case.)
2282
+ */
2283
+
2284
+ n = s.hash_size
2285
+ p = n
2286
+
2287
+ do {
2288
+ m = s.head[--p]
2289
+ s.head[p] = m >= _w_size ? m - _w_size : 0
2290
+ } while (--n)
2291
+
2292
+ n = _w_size
2293
+ p = n
2294
+
2295
+ do {
2296
+ m = s.prev[--p]
2297
+ s.prev[p] = m >= _w_size ? m - _w_size : 0
2298
+ /* If n is not on any hash chain, prev[n] is garbage but
2299
+ * its value will never be used.
2300
+ */
2301
+ } while (--n)
2302
+
2303
+ more += _w_size
2304
+ }
2305
+
2306
+ if (s.strm.avail_in === 0) {
2307
+ break
2308
+ }
2309
+ /* If there was no sliding:
2310
+ * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
2311
+ * more == window_size - lookahead - strstart
2312
+ * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
2313
+ * => more >= window_size - 2*WSIZE + 2
2314
+ * In the BIG_MEM or MMAP case (not yet supported),
2315
+ * window_size == input_size + MIN_LOOKAHEAD &&
2316
+ * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
2317
+ * Otherwise, window_size == 2*WSIZE so more >= 2.
2318
+ * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
2319
+ */
2320
+ // Assert(more >= 2, "more < 2");
2321
+
2322
+ n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more)
2323
+ s.lookahead += n
2324
+ /* Initialize the hash value now that we have some input: */
2325
+
2326
+ if (s.lookahead + s.insert >= MIN_MATCH$1) {
2327
+ str = s.strstart - s.insert
2328
+ s.ins_h = s.window[str]
2329
+ /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */
2330
+
2331
+ s.ins_h = HASH(s, s.ins_h, s.window[str + 1]) // #if MIN_MATCH != 3
2332
+ // Call update_hash() MIN_MATCH-3 more times
2333
+ // #endif
2334
+
2335
+ while (s.insert) {
2336
+ /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
2337
+ s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH$1 - 1])
2338
+ s.prev[str & s.w_mask] = s.head[s.ins_h]
2339
+ s.head[s.ins_h] = str
2340
+ str++
2341
+ s.insert--
2342
+
2343
+ if (s.lookahead + s.insert < MIN_MATCH$1) {
2344
+ break
2345
+ }
2346
+ }
2347
+ }
2348
+ /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
2349
+ * but this is not important since only literal bytes will be emitted.
2350
+ */
2351
+ } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0)
2352
+ /* If the WIN_INIT bytes after the end of the current data have never been
2353
+ * written, then zero those bytes in order to avoid memory check reports of
2354
+ * the use of uninitialized (or uninitialised as Julian writes) bytes by
2355
+ * the longest match routines. Update the high water mark for the next
2356
+ * time through here. WIN_INIT is set to MAX_MATCH since the longest match
2357
+ * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
2358
+ */
2359
+ // if (s.high_water < s.window_size) {
2360
+ // const curr = s.strstart + s.lookahead;
2361
+ // let init = 0;
2362
+ //
2363
+ // if (s.high_water < curr) {
2364
+ // /* Previous high water mark below current data -- zero WIN_INIT
2365
+ // * bytes or up to end of window, whichever is less.
2366
+ // */
2367
+ // init = s.window_size - curr;
2368
+ // if (init > WIN_INIT)
2369
+ // init = WIN_INIT;
2370
+ // zmemzero(s->window + curr, (unsigned)init);
2371
+ // s->high_water = curr + init;
2372
+ // }
2373
+ // else if (s->high_water < (ulg)curr + WIN_INIT) {
2374
+ // /* High water mark at or above current data, but below current data
2375
+ // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
2376
+ // * to end of window, whichever is less.
2377
+ // */
2378
+ // init = (ulg)curr + WIN_INIT - s->high_water;
2379
+ // if (init > s->window_size - s->high_water)
2380
+ // init = s->window_size - s->high_water;
2381
+ // zmemzero(s->window + s->high_water, (unsigned)init);
2382
+ // s->high_water += init;
2383
+ // }
2384
+ // }
2385
+ //
2386
+ // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
2387
+ // "not enough room for search");
2388
+ }
2389
+ /* ===========================================================================
2390
+ * Copy without compression as much as possible from the input stream, return
2391
+ * the current block state.
2392
+ * This function does not insert new strings in the dictionary since
2393
+ * uncompressible data is probably not useful. This function is used
2394
+ * only for the level=0 compression option.
2395
+ * NOTE: this function should be optimized to avoid extra copying from
2396
+ * window to pending_buf.
2397
+ */
2398
+
2399
+ var deflate_stored = function deflate_stored(s, flush) {
2400
+ /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
2401
+ * to pending_buf_size, and each stored block has a 5 byte header:
2402
+ */
2403
+ var max_block_size = 0xffff
2404
+
2405
+ if (max_block_size > s.pending_buf_size - 5) {
2406
+ max_block_size = s.pending_buf_size - 5
2407
+ }
2408
+ /* Copy as much as possible from input to output: */
2409
+
2410
+ for (;;) {
2411
+ /* Fill the window as much as possible: */
2412
+ if (s.lookahead <= 1) {
2413
+ // Assert(s->strstart < s->w_size+MAX_DIST(s) ||
2414
+ // s->block_start >= (long)s->w_size, "slide too late");
2415
+ // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) ||
2416
+ // s.block_start >= s.w_size)) {
2417
+ // throw new Error("slide too late");
2418
+ // }
2419
+ fill_window(s)
2420
+
2421
+ if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
2422
+ return BS_NEED_MORE
2423
+ }
2424
+
2425
+ if (s.lookahead === 0) {
2426
+ break
2427
+ }
2428
+ /* flush the current block */
2429
+ } // Assert(s->block_start >= 0L, "block gone");
2430
+ // if (s.block_start < 0) throw new Error("block gone");
2431
+
2432
+ s.strstart += s.lookahead
2433
+ s.lookahead = 0
2434
+ /* Emit a stored block if pending_buf will be full: */
2435
+
2436
+ var max_start = s.block_start + max_block_size
2437
+
2438
+ if (s.strstart === 0 || s.strstart >= max_start) {
2439
+ /* strstart == 0 is possible when wraparound on 16-bit machine */
2440
+ s.lookahead = s.strstart - max_start
2441
+ s.strstart = max_start
2442
+ /** * FLUSH_BLOCK(s, 0); ** */
2443
+
2444
+ flush_block_only(s, false)
2445
+
2446
+ if (s.strm.avail_out === 0) {
2447
+ return BS_NEED_MORE
2448
+ }
2449
+ /***/
2450
+ }
2451
+ /* Flush if we may have to slide, otherwise block_start may become
2452
+ * negative and the data will be gone:
2453
+ */
2454
+
2455
+ if (s.strstart - s.block_start >= s.w_size - MIN_LOOKAHEAD) {
2456
+ /** * FLUSH_BLOCK(s, 0); ** */
2457
+ flush_block_only(s, false)
2458
+
2459
+ if (s.strm.avail_out === 0) {
2460
+ return BS_NEED_MORE
2461
+ }
2462
+ /***/
2463
+ }
2464
+ }
2465
+
2466
+ s.insert = 0
2467
+
2468
+ if (flush === Z_FINISH) {
2469
+ /** * FLUSH_BLOCK(s, 1); ** */
2470
+ flush_block_only(s, true)
2471
+
2472
+ if (s.strm.avail_out === 0) {
2473
+ return BS_FINISH_STARTED
2474
+ }
2475
+ /***/
2476
+
2477
+ return BS_FINISH_DONE
2478
+ }
2479
+
2480
+ if (s.strstart > s.block_start) {
2481
+ /** * FLUSH_BLOCK(s, 0); ** */
2482
+ flush_block_only(s, false)
2483
+
2484
+ if (s.strm.avail_out === 0) {
2485
+ return BS_NEED_MORE
2486
+ }
2487
+ /***/
2488
+ }
2489
+
2490
+ return BS_NEED_MORE
2491
+ }
2492
+ /* ===========================================================================
2493
+ * Compress as much as possible from the input stream, return the current
2494
+ * block state.
2495
+ * This function does not perform lazy evaluation of matches and inserts
2496
+ * new strings in the dictionary only for unmatched strings or for short
2497
+ * matches. It is used only for the fast compression options.
2498
+ */
2499
+
2500
+ var deflate_fast = function deflate_fast(s, flush) {
2501
+ var hash_head
2502
+ /* head of the hash chain */
2503
+
2504
+ var bflush
2505
+ /* set if current block must be flushed */
2506
+
2507
+ for (;;) {
2508
+ /* Make sure that we always have enough lookahead, except
2509
+ * at the end of the input file. We need MAX_MATCH bytes
2510
+ * for the next match, plus MIN_MATCH bytes to insert the
2511
+ * string following the next match.
2512
+ */
2513
+ if (s.lookahead < MIN_LOOKAHEAD) {
2514
+ fill_window(s)
2515
+
2516
+ if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
2517
+ return BS_NEED_MORE
2518
+ }
2519
+
2520
+ if (s.lookahead === 0) {
2521
+ break
2522
+ /* flush the current block */
2523
+ }
2524
+ }
2525
+ /* Insert the string window[strstart .. strstart+2] in the
2526
+ * dictionary, and set hash_head to the head of the hash chain:
2527
+ */
2528
+
2529
+ hash_head = 0
2530
+ /* NIL */
2531
+
2532
+ if (s.lookahead >= MIN_MATCH$1) {
2533
+ /** * INSERT_STRING(s, s.strstart, hash_head); ** */
2534
+ s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH$1 - 1])
2535
+ hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]
2536
+ s.head[s.ins_h] = s.strstart
2537
+ /***/
2538
+ }
2539
+ /* Find the longest match, discarding those <= prev_length.
2540
+ * At this point we have always match_length < MIN_MATCH
2541
+ */
2542
+
2543
+ if (
2544
+ hash_head !== 0 &&
2545
+ /* NIL */
2546
+ s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD
2547
+ ) {
2548
+ /* To simplify the code, we prevent matches with the string
2549
+ * of window index 0 (in particular we have to avoid a match
2550
+ * of the string with itself at the start of the input file).
2551
+ */
2552
+ s.match_length = longest_match(s, hash_head)
2553
+ /* longest_match() sets match_start */
2554
+ }
2555
+
2556
+ if (s.match_length >= MIN_MATCH$1) {
2557
+ // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only
2558
+
2559
+ /** * _tr_tally_dist(s, s.strstart - s.match_start,
2560
+ s.match_length - MIN_MATCH, bflush); ** */
2561
+ bflush = _tr_tally$1(
2562
+ s,
2563
+ s.strstart - s.match_start,
2564
+ s.match_length - MIN_MATCH$1
2565
+ )
2566
+ s.lookahead -= s.match_length
2567
+ /* Insert new strings in the hash table only if the match length
2568
+ * is not too large. This saves time but degrades compression.
2569
+ */
2570
+
2571
+ if (
2572
+ s.match_length <= s.max_lazy_match &&
2573
+ /* max_insert_length */
2574
+ s.lookahead >= MIN_MATCH$1
2575
+ ) {
2576
+ s.match_length--
2577
+ /* string at strstart already in table */
2578
+
2579
+ do {
2580
+ s.strstart++
2581
+ /** * INSERT_STRING(s, s.strstart, hash_head); ** */
2582
+
2583
+ s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH$1 - 1])
2584
+ hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]
2585
+ s.head[s.ins_h] = s.strstart
2586
+ /***/
2587
+
2588
+ /* strstart never exceeds WSIZE-MAX_MATCH, so there are
2589
+ * always MIN_MATCH bytes ahead.
2590
+ */
2591
+ } while (--s.match_length !== 0)
2592
+
2593
+ s.strstart++
2594
+ } else {
2595
+ s.strstart += s.match_length
2596
+ s.match_length = 0
2597
+ s.ins_h = s.window[s.strstart]
2598
+ /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */
2599
+
2600
+ s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + 1]) // #if MIN_MATCH != 3
2601
+ // Call UPDATE_HASH() MIN_MATCH-3 more times
2602
+ // #endif
2603
+
2604
+ /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
2605
+ * matter since it will be recomputed at next deflate call.
2606
+ */
2607
+ }
2608
+ } else {
2609
+ /* No match, output a literal byte */
2610
+ // Tracevv((stderr,"%c", s.window[s.strstart]));
2611
+
2612
+ /** * _tr_tally_lit(s, s.window[s.strstart], bflush); ** */
2613
+ bflush = _tr_tally$1(s, 0, s.window[s.strstart])
2614
+ s.lookahead--
2615
+ s.strstart++
2616
+ }
2617
+
2618
+ if (bflush) {
2619
+ /** * FLUSH_BLOCK(s, 0); ** */
2620
+ flush_block_only(s, false)
2621
+
2622
+ if (s.strm.avail_out === 0) {
2623
+ return BS_NEED_MORE
2624
+ }
2625
+ /***/
2626
+ }
2627
+ }
2628
+
2629
+ s.insert = s.strstart < MIN_MATCH$1 - 1 ? s.strstart : MIN_MATCH$1 - 1
2630
+
2631
+ if (flush === Z_FINISH) {
2632
+ /** * FLUSH_BLOCK(s, 1); ** */
2633
+ flush_block_only(s, true)
2634
+
2635
+ if (s.strm.avail_out === 0) {
2636
+ return BS_FINISH_STARTED
2637
+ }
2638
+ /***/
2639
+
2640
+ return BS_FINISH_DONE
2641
+ }
2642
+
2643
+ if (s.last_lit) {
2644
+ /** * FLUSH_BLOCK(s, 0); ** */
2645
+ flush_block_only(s, false)
2646
+
2647
+ if (s.strm.avail_out === 0) {
2648
+ return BS_NEED_MORE
2649
+ }
2650
+ /***/
2651
+ }
2652
+
2653
+ return BS_BLOCK_DONE
2654
+ }
2655
+ /* ===========================================================================
2656
+ * Same as above, but achieves better compression. We use a lazy
2657
+ * evaluation for matches: a match is finally adopted only if there is
2658
+ * no better match at the next window position.
2659
+ */
2660
+
2661
+ var deflate_slow = function deflate_slow(s, flush) {
2662
+ var hash_head
2663
+ /* head of hash chain */
2664
+
2665
+ var bflush
2666
+ /* set if current block must be flushed */
2667
+
2668
+ var max_insert
2669
+ /* Process the input block. */
2670
+
2671
+ for (;;) {
2672
+ /* Make sure that we always have enough lookahead, except
2673
+ * at the end of the input file. We need MAX_MATCH bytes
2674
+ * for the next match, plus MIN_MATCH bytes to insert the
2675
+ * string following the next match.
2676
+ */
2677
+ if (s.lookahead < MIN_LOOKAHEAD) {
2678
+ fill_window(s)
2679
+
2680
+ if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
2681
+ return BS_NEED_MORE
2682
+ }
2683
+
2684
+ if (s.lookahead === 0) {
2685
+ break
2686
+ }
2687
+ /* flush the current block */
2688
+ }
2689
+ /* Insert the string window[strstart .. strstart+2] in the
2690
+ * dictionary, and set hash_head to the head of the hash chain:
2691
+ */
2692
+
2693
+ hash_head = 0
2694
+ /* NIL */
2695
+
2696
+ if (s.lookahead >= MIN_MATCH$1) {
2697
+ /** * INSERT_STRING(s, s.strstart, hash_head); ** */
2698
+ s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH$1 - 1])
2699
+ hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]
2700
+ s.head[s.ins_h] = s.strstart
2701
+ /***/
2702
+ }
2703
+ /* Find the longest match, discarding those <= prev_length.
2704
+ */
2705
+
2706
+ s.prev_length = s.match_length
2707
+ s.prev_match = s.match_start
2708
+ s.match_length = MIN_MATCH$1 - 1
2709
+
2710
+ if (
2711
+ hash_head !== 0 &&
2712
+ /* NIL */
2713
+ s.prev_length < s.max_lazy_match &&
2714
+ s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD
2715
+ /* MAX_DIST(s) */
2716
+ ) {
2717
+ /* To simplify the code, we prevent matches with the string
2718
+ * of window index 0 (in particular we have to avoid a match
2719
+ * of the string with itself at the start of the input file).
2720
+ */
2721
+ s.match_length = longest_match(s, hash_head)
2722
+ /* longest_match() sets match_start */
2723
+
2724
+ if (
2725
+ s.match_length <= 5 &&
2726
+ (s.strategy === Z_FILTERED ||
2727
+ (s.match_length === MIN_MATCH$1 &&
2728
+ s.strstart - s.match_start > 4096))
2729
+ /* TOO_FAR */
2730
+ ) {
2731
+ /* If prev_match is also MIN_MATCH, match_start is garbage
2732
+ * but we will ignore the current match anyway.
2733
+ */
2734
+ s.match_length = MIN_MATCH$1 - 1
2735
+ }
2736
+ }
2737
+ /* If there was a match at the previous step and the current
2738
+ * match is not better, output the previous match:
2739
+ */
2740
+
2741
+ if (s.prev_length >= MIN_MATCH$1 && s.match_length <= s.prev_length) {
2742
+ max_insert = s.strstart + s.lookahead - MIN_MATCH$1
2743
+ /* Do not insert strings in hash table beyond this. */
2744
+ // check_match(s, s.strstart-1, s.prev_match, s.prev_length);
2745
+
2746
+ /** *_tr_tally_dist(s, s.strstart - 1 - s.prev_match,
2747
+ s.prev_length - MIN_MATCH, bflush);** */
2748
+
2749
+ bflush = _tr_tally$1(
2750
+ s,
2751
+ s.strstart - 1 - s.prev_match,
2752
+ s.prev_length - MIN_MATCH$1
2753
+ )
2754
+ /* Insert in hash table all strings up to the end of the match.
2755
+ * strstart-1 and strstart are already inserted. If there is not
2756
+ * enough lookahead, the last two strings are not inserted in
2757
+ * the hash table.
2758
+ */
2759
+
2760
+ s.lookahead -= s.prev_length - 1
2761
+ s.prev_length -= 2
2762
+
2763
+ do {
2764
+ if (++s.strstart <= max_insert) {
2765
+ /** * INSERT_STRING(s, s.strstart, hash_head); ** */
2766
+ s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH$1 - 1])
2767
+ hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]
2768
+ s.head[s.ins_h] = s.strstart
2769
+ /***/
2770
+ }
2771
+ } while (--s.prev_length !== 0)
2772
+
2773
+ s.match_available = 0
2774
+ s.match_length = MIN_MATCH$1 - 1
2775
+ s.strstart++
2776
+
2777
+ if (bflush) {
2778
+ /** * FLUSH_BLOCK(s, 0); ** */
2779
+ flush_block_only(s, false)
2780
+
2781
+ if (s.strm.avail_out === 0) {
2782
+ return BS_NEED_MORE
2783
+ }
2784
+ /***/
2785
+ }
2786
+ } else if (s.match_available) {
2787
+ /* If there was no match at the previous position, output a
2788
+ * single literal. If there was a match but the current match
2789
+ * is longer, truncate the previous match to a single literal.
2790
+ */
2791
+ // Tracevv((stderr,"%c", s->window[s->strstart-1]));
2792
+
2793
+ /** * _tr_tally_lit(s, s.window[s.strstart-1], bflush); ** */
2794
+ bflush = _tr_tally$1(s, 0, s.window[s.strstart - 1])
2795
+
2796
+ if (bflush) {
2797
+ /** * FLUSH_BLOCK_ONLY(s, 0) ** */
2798
+ flush_block_only(s, false)
2799
+ /***/
2800
+ }
2801
+
2802
+ s.strstart++
2803
+ s.lookahead--
2804
+
2805
+ if (s.strm.avail_out === 0) {
2806
+ return BS_NEED_MORE
2807
+ }
2808
+ } else {
2809
+ /* There is no previous match to compare with, wait for
2810
+ * the next step to decide.
2811
+ */
2812
+ s.match_available = 1
2813
+ s.strstart++
2814
+ s.lookahead--
2815
+ }
2816
+ } // Assert (flush != Z_NO_FLUSH, "no flush?");
2817
+
2818
+ if (s.match_available) {
2819
+ // Tracevv((stderr,"%c", s->window[s->strstart-1]));
2820
+
2821
+ /** * _tr_tally_lit(s, s.window[s.strstart-1], bflush); ** */
2822
+ bflush = _tr_tally$1(s, 0, s.window[s.strstart - 1])
2823
+ s.match_available = 0
2824
+ }
2825
+
2826
+ s.insert = s.strstart < MIN_MATCH$1 - 1 ? s.strstart : MIN_MATCH$1 - 1
2827
+
2828
+ if (flush === Z_FINISH) {
2829
+ /** * FLUSH_BLOCK(s, 1); ** */
2830
+ flush_block_only(s, true)
2831
+
2832
+ if (s.strm.avail_out === 0) {
2833
+ return BS_FINISH_STARTED
2834
+ }
2835
+ /***/
2836
+
2837
+ return BS_FINISH_DONE
2838
+ }
2839
+
2840
+ if (s.last_lit) {
2841
+ /** * FLUSH_BLOCK(s, 0); ** */
2842
+ flush_block_only(s, false)
2843
+
2844
+ if (s.strm.avail_out === 0) {
2845
+ return BS_NEED_MORE
2846
+ }
2847
+ /***/
2848
+ }
2849
+
2850
+ return BS_BLOCK_DONE
2851
+ }
2852
+ /* ===========================================================================
2853
+ * For Z_RLE, simply look for runs of bytes, generate matches only of distance
2854
+ * one. Do not maintain a hash table. (It will be regenerated if this run of
2855
+ * deflate switches away from Z_RLE.)
2856
+ */
2857
+
2858
+ var deflate_rle = function deflate_rle(s, flush) {
2859
+ var bflush
2860
+ /* set if current block must be flushed */
2861
+
2862
+ var prev
2863
+ /* byte at distance one to match */
2864
+
2865
+ var scan
2866
+ var strend
2867
+ /* scan goes up to strend for length of run */
2868
+
2869
+ var _win = s.window
2870
+
2871
+ for (;;) {
2872
+ /* Make sure that we always have enough lookahead, except
2873
+ * at the end of the input file. We need MAX_MATCH bytes
2874
+ * for the longest run, plus one for the unrolled loop.
2875
+ */
2876
+ if (s.lookahead <= MAX_MATCH$1) {
2877
+ fill_window(s)
2878
+
2879
+ if (s.lookahead <= MAX_MATCH$1 && flush === Z_NO_FLUSH) {
2880
+ return BS_NEED_MORE
2881
+ }
2882
+
2883
+ if (s.lookahead === 0) {
2884
+ break
2885
+ }
2886
+ /* flush the current block */
2887
+ }
2888
+ /* See how many times the previous byte repeats */
2889
+
2890
+ s.match_length = 0
2891
+
2892
+ if (s.lookahead >= MIN_MATCH$1 && s.strstart > 0) {
2893
+ scan = s.strstart - 1
2894
+ prev = _win[scan]
2895
+
2896
+ if (
2897
+ prev === _win[++scan] &&
2898
+ prev === _win[++scan] &&
2899
+ prev === _win[++scan]
2900
+ ) {
2901
+ strend = s.strstart + MAX_MATCH$1
2902
+
2903
+ do {
2904
+ /* jshint noempty:false */
2905
+ } while (
2906
+ prev === _win[++scan] &&
2907
+ prev === _win[++scan] &&
2908
+ prev === _win[++scan] &&
2909
+ prev === _win[++scan] &&
2910
+ prev === _win[++scan] &&
2911
+ prev === _win[++scan] &&
2912
+ prev === _win[++scan] &&
2913
+ prev === _win[++scan] &&
2914
+ scan < strend
2915
+ )
2916
+
2917
+ s.match_length = MAX_MATCH$1 - (strend - scan)
2918
+
2919
+ if (s.match_length > s.lookahead) {
2920
+ s.match_length = s.lookahead
2921
+ }
2922
+ } // Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
2923
+ }
2924
+ /* Emit match if have run of MIN_MATCH or longer, else emit literal */
2925
+
2926
+ if (s.match_length >= MIN_MATCH$1) {
2927
+ // check_match(s, s.strstart, s.strstart - 1, s.match_length);
2928
+
2929
+ /** * _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ** */
2930
+ bflush = _tr_tally$1(s, 1, s.match_length - MIN_MATCH$1)
2931
+ s.lookahead -= s.match_length
2932
+ s.strstart += s.match_length
2933
+ s.match_length = 0
2934
+ } else {
2935
+ /* No match, output a literal byte */
2936
+ // Tracevv((stderr,"%c", s->window[s->strstart]));
2937
+
2938
+ /** * _tr_tally_lit(s, s.window[s.strstart], bflush); ** */
2939
+ bflush = _tr_tally$1(s, 0, s.window[s.strstart])
2940
+ s.lookahead--
2941
+ s.strstart++
2942
+ }
2943
+
2944
+ if (bflush) {
2945
+ /** * FLUSH_BLOCK(s, 0); ** */
2946
+ flush_block_only(s, false)
2947
+
2948
+ if (s.strm.avail_out === 0) {
2949
+ return BS_NEED_MORE
2950
+ }
2951
+ /***/
2952
+ }
2953
+ }
2954
+
2955
+ s.insert = 0
2956
+
2957
+ if (flush === Z_FINISH) {
2958
+ /** * FLUSH_BLOCK(s, 1); ** */
2959
+ flush_block_only(s, true)
2960
+
2961
+ if (s.strm.avail_out === 0) {
2962
+ return BS_FINISH_STARTED
2963
+ }
2964
+ /***/
2965
+
2966
+ return BS_FINISH_DONE
2967
+ }
2968
+
2969
+ if (s.last_lit) {
2970
+ /** * FLUSH_BLOCK(s, 0); ** */
2971
+ flush_block_only(s, false)
2972
+
2973
+ if (s.strm.avail_out === 0) {
2974
+ return BS_NEED_MORE
2975
+ }
2976
+ /***/
2977
+ }
2978
+
2979
+ return BS_BLOCK_DONE
2980
+ }
2981
+ /* ===========================================================================
2982
+ * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
2983
+ * (It will be regenerated if this run of deflate switches away from Huffman.)
2984
+ */
2985
+
2986
+ var deflate_huff = function deflate_huff(s, flush) {
2987
+ var bflush
2988
+ /* set if current block must be flushed */
2989
+
2990
+ for (;;) {
2991
+ /* Make sure that we have a literal to write. */
2992
+ if (s.lookahead === 0) {
2993
+ fill_window(s)
2994
+
2995
+ if (s.lookahead === 0) {
2996
+ if (flush === Z_NO_FLUSH) {
2997
+ return BS_NEED_MORE
2998
+ }
2999
+
3000
+ break
3001
+ /* flush the current block */
3002
+ }
3003
+ }
3004
+ /* Output a literal byte */
3005
+
3006
+ s.match_length = 0 // Tracevv((stderr,"%c", s->window[s->strstart]));
3007
+
3008
+ /** * _tr_tally_lit(s, s.window[s.strstart], bflush); ** */
3009
+
3010
+ bflush = _tr_tally$1(s, 0, s.window[s.strstart])
3011
+ s.lookahead--
3012
+ s.strstart++
3013
+
3014
+ if (bflush) {
3015
+ /** * FLUSH_BLOCK(s, 0); ** */
3016
+ flush_block_only(s, false)
3017
+
3018
+ if (s.strm.avail_out === 0) {
3019
+ return BS_NEED_MORE
3020
+ }
3021
+ /***/
3022
+ }
3023
+ }
3024
+
3025
+ s.insert = 0
3026
+
3027
+ if (flush === Z_FINISH) {
3028
+ /** * FLUSH_BLOCK(s, 1); ** */
3029
+ flush_block_only(s, true)
3030
+
3031
+ if (s.strm.avail_out === 0) {
3032
+ return BS_FINISH_STARTED
3033
+ }
3034
+ /***/
3035
+
3036
+ return BS_FINISH_DONE
3037
+ }
3038
+
3039
+ if (s.last_lit) {
3040
+ /** * FLUSH_BLOCK(s, 0); ** */
3041
+ flush_block_only(s, false)
3042
+
3043
+ if (s.strm.avail_out === 0) {
3044
+ return BS_NEED_MORE
3045
+ }
3046
+ /***/
3047
+ }
3048
+
3049
+ return BS_BLOCK_DONE
3050
+ }
3051
+ /* Values for max_lazy_match, good_match and max_chain_length, depending on
3052
+ * the desired pack level (0..9). The values given below have been tuned to
3053
+ * exclude worst case performance for pathological files. Better values may be
3054
+ * found for specific files.
3055
+ */
3056
+
3057
+ function Config(good_length, max_lazy, nice_length, max_chain, func) {
3058
+ this.good_length = good_length
3059
+ this.max_lazy = max_lazy
3060
+ this.nice_length = nice_length
3061
+ this.max_chain = max_chain
3062
+ this.func = func
3063
+ }
3064
+
3065
+ var configuration_table = [
3066
+ /* good lazy nice chain */
3067
+ new Config(0, 0, 0, 0, deflate_stored),
3068
+ /* 0 store only */
3069
+ new Config(4, 4, 8, 4, deflate_fast),
3070
+ /* 1 max speed, no lazy matches */
3071
+ new Config(4, 5, 16, 8, deflate_fast),
3072
+ /* 2 */
3073
+ new Config(4, 6, 32, 32, deflate_fast),
3074
+ /* 3 */
3075
+ new Config(4, 4, 16, 16, deflate_slow),
3076
+ /* 4 lazy matches */
3077
+ new Config(8, 16, 32, 32, deflate_slow),
3078
+ /* 5 */
3079
+ new Config(8, 16, 128, 128, deflate_slow),
3080
+ /* 6 */
3081
+ new Config(8, 32, 128, 256, deflate_slow),
3082
+ /* 7 */
3083
+ new Config(32, 128, 258, 1024, deflate_slow),
3084
+ /* 8 */
3085
+ new Config(32, 258, 258, 4096, deflate_slow)
3086
+ /* 9 max compression */
3087
+ ]
3088
+ /* ===========================================================================
3089
+ * Initialize the "longest match" routines for a new zlib stream
3090
+ */
3091
+
3092
+ var lm_init = function lm_init(s) {
3093
+ s.window_size = 2 * s.w_size
3094
+ /** * CLEAR_HASH(s); ** */
3095
+
3096
+ zero$1(s.head) // Fill with NIL (= 0);
3097
+
3098
+ /* Set the default configuration parameters:
3099
+ */
3100
+
3101
+ s.max_lazy_match = configuration_table[s.level].max_lazy
3102
+ s.good_match = configuration_table[s.level].good_length
3103
+ s.nice_match = configuration_table[s.level].nice_length
3104
+ s.max_chain_length = configuration_table[s.level].max_chain
3105
+ s.strstart = 0
3106
+ s.block_start = 0
3107
+ s.lookahead = 0
3108
+ s.insert = 0
3109
+ s.match_length = s.prev_length = MIN_MATCH$1 - 1
3110
+ s.match_available = 0
3111
+ s.ins_h = 0
3112
+ }
3113
+
3114
+ function DeflateState() {
3115
+ this.strm = null
3116
+ /* pointer back to this zlib stream */
3117
+
3118
+ this.status = 0
3119
+ /* as the name implies */
3120
+
3121
+ this.pending_buf = null
3122
+ /* output still pending */
3123
+
3124
+ this.pending_buf_size = 0
3125
+ /* size of pending_buf */
3126
+
3127
+ this.pending_out = 0
3128
+ /* next pending byte to output to the stream */
3129
+
3130
+ this.pending = 0
3131
+ /* nb of bytes in the pending buffer */
3132
+
3133
+ this.wrap = 0
3134
+ /* bit 0 true for zlib, bit 1 true for gzip */
3135
+
3136
+ this.gzhead = null
3137
+ /* gzip header information to write */
3138
+
3139
+ this.gzindex = 0
3140
+ /* where in extra, name, or comment */
3141
+
3142
+ this.method = Z_DEFLATED
3143
+ /* can only be DEFLATED */
3144
+
3145
+ this.last_flush = -1
3146
+ /* value of flush param for previous deflate call */
3147
+
3148
+ this.w_size = 0
3149
+ /* LZ77 window size (32K by default) */
3150
+
3151
+ this.w_bits = 0
3152
+ /* log2(w_size) (8..16) */
3153
+
3154
+ this.w_mask = 0
3155
+ /* w_size - 1 */
3156
+
3157
+ this.window = null
3158
+ /* Sliding window. Input bytes are read into the second half of the window,
3159
+ * and move to the first half later to keep a dictionary of at least wSize
3160
+ * bytes. With this organization, matches are limited to a distance of
3161
+ * wSize-MAX_MATCH bytes, but this ensures that IO is always
3162
+ * performed with a length multiple of the block size.
3163
+ */
3164
+
3165
+ this.window_size = 0
3166
+ /* Actual size of window: 2*wSize, except when the user input buffer
3167
+ * is directly used as sliding window.
3168
+ */
3169
+
3170
+ this.prev = null
3171
+ /* Link to older string with same hash index. To limit the size of this
3172
+ * array to 64K, this link is maintained only for the last 32K strings.
3173
+ * An index in this array is thus a window index modulo 32K.
3174
+ */
3175
+
3176
+ this.head = null
3177
+ /* Heads of the hash chains or NIL. */
3178
+
3179
+ this.ins_h = 0
3180
+ /* hash index of string to be inserted */
3181
+
3182
+ this.hash_size = 0
3183
+ /* number of elements in hash table */
3184
+
3185
+ this.hash_bits = 0
3186
+ /* log2(hash_size) */
3187
+
3188
+ this.hash_mask = 0
3189
+ /* hash_size-1 */
3190
+
3191
+ this.hash_shift = 0
3192
+ /* Number of bits by which ins_h must be shifted at each input
3193
+ * step. It must be such that after MIN_MATCH steps, the oldest
3194
+ * byte no longer takes part in the hash key, that is:
3195
+ * hash_shift * MIN_MATCH >= hash_bits
3196
+ */
3197
+
3198
+ this.block_start = 0
3199
+ /* Window position at the beginning of the current output block. Gets
3200
+ * negative when the window is moved backwards.
3201
+ */
3202
+
3203
+ this.match_length = 0
3204
+ /* length of best match */
3205
+
3206
+ this.prev_match = 0
3207
+ /* previous match */
3208
+
3209
+ this.match_available = 0
3210
+ /* set if previous match exists */
3211
+
3212
+ this.strstart = 0
3213
+ /* start of string to insert */
3214
+
3215
+ this.match_start = 0
3216
+ /* start of matching string */
3217
+
3218
+ this.lookahead = 0
3219
+ /* number of valid bytes ahead in window */
3220
+
3221
+ this.prev_length = 0
3222
+ /* Length of the best match at previous step. Matches not greater than this
3223
+ * are discarded. This is used in the lazy match evaluation.
3224
+ */
3225
+
3226
+ this.max_chain_length = 0
3227
+ /* To speed up deflation, hash chains are never searched beyond this
3228
+ * length. A higher limit improves compression ratio but degrades the
3229
+ * speed.
3230
+ */
3231
+
3232
+ this.max_lazy_match = 0
3233
+ /* Attempt to find a better match only when the current match is strictly
3234
+ * smaller than this value. This mechanism is used only for compression
3235
+ * levels >= 4.
3236
+ */
3237
+ // That's alias to max_lazy_match, don't use directly
3238
+ // this.max_insert_length = 0;
3239
+
3240
+ /* Insert new strings in the hash table only if the match length is not
3241
+ * greater than this length. This saves time but degrades compression.
3242
+ * max_insert_length is used only for compression levels <= 3.
3243
+ */
3244
+
3245
+ this.level = 0
3246
+ /* compression level (1..9) */
3247
+
3248
+ this.strategy = 0
3249
+ /* favor or force Huffman coding */
3250
+
3251
+ this.good_match = 0
3252
+ /* Use a faster search when the previous match is longer than this */
3253
+
3254
+ this.nice_match = 0
3255
+ /* Stop searching when current match exceeds this */
3256
+
3257
+ /* used by trees.c: */
3258
+
3259
+ /* Didn't use ct_data typedef below to suppress compiler warning */
3260
+ // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
3261
+ // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
3262
+ // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
3263
+ // Use flat array of DOUBLE size, with interleaved fata,
3264
+ // because JS does not support effective
3265
+
3266
+ this.dyn_ltree = new Uint16Array(HEAP_SIZE$1 * 2)
3267
+ this.dyn_dtree = new Uint16Array((2 * D_CODES$1 + 1) * 2)
3268
+ this.bl_tree = new Uint16Array((2 * BL_CODES$1 + 1) * 2)
3269
+ zero$1(this.dyn_ltree)
3270
+ zero$1(this.dyn_dtree)
3271
+ zero$1(this.bl_tree)
3272
+ this.l_desc = null
3273
+ /* desc. for literal tree */
3274
+
3275
+ this.d_desc = null
3276
+ /* desc. for distance tree */
3277
+
3278
+ this.bl_desc = null
3279
+ /* desc. for bit length tree */
3280
+ // ush bl_count[MAX_BITS+1];
3281
+
3282
+ this.bl_count = new Uint16Array(MAX_BITS$1 + 1)
3283
+ /* number of codes at each bit length for an optimal tree */
3284
+ // int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
3285
+
3286
+ this.heap = new Uint16Array(2 * L_CODES$1 + 1)
3287
+ /* heap used to build the Huffman trees */
3288
+
3289
+ zero$1(this.heap)
3290
+ this.heap_len = 0
3291
+ /* number of elements in the heap */
3292
+
3293
+ this.heap_max = 0
3294
+ /* element of largest frequency */
3295
+
3296
+ /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
3297
+ * The same heap array is used to build all trees.
3298
+ */
3299
+
3300
+ this.depth = new Uint16Array(2 * L_CODES$1 + 1) // uch depth[2*L_CODES+1];
3301
+
3302
+ zero$1(this.depth)
3303
+ /* Depth of each subtree used as tie breaker for trees of equal frequency
3304
+ */
3305
+
3306
+ this.l_buf = 0
3307
+ /* buffer index for literals or lengths */
3308
+
3309
+ this.lit_bufsize = 0
3310
+ /* Size of match buffer for literals/lengths. There are 4 reasons for
3311
+ * limiting lit_bufsize to 64K:
3312
+ * - frequencies can be kept in 16 bit counters
3313
+ * - if compression is not successful for the first block, all input
3314
+ * data is still in the window so we can still emit a stored block even
3315
+ * when input comes from standard input. (This can also be done for
3316
+ * all blocks if lit_bufsize is not greater than 32K.)
3317
+ * - if compression is not successful for a file smaller than 64K, we can
3318
+ * even emit a stored file instead of a stored block (saving 5 bytes).
3319
+ * This is applicable only for zip (not gzip or zlib).
3320
+ * - creating new Huffman trees less frequently may not provide fast
3321
+ * adaptation to changes in the input data statistics. (Take for
3322
+ * example a binary file with poorly compressible code followed by
3323
+ * a highly compressible string table.) Smaller buffer sizes give
3324
+ * fast adaptation but have of course the overhead of transmitting
3325
+ * trees more frequently.
3326
+ * - I can't count above 4
3327
+ */
3328
+
3329
+ this.last_lit = 0
3330
+ /* running index in l_buf */
3331
+
3332
+ this.d_buf = 0
3333
+ /* Buffer index for distances. To simplify the code, d_buf and l_buf have
3334
+ * the same number of elements. To use different lengths, an extra flag
3335
+ * array would be necessary.
3336
+ */
3337
+
3338
+ this.opt_len = 0
3339
+ /* bit length of current block with optimal trees */
3340
+
3341
+ this.static_len = 0
3342
+ /* bit length of current block with static trees */
3343
+
3344
+ this.matches = 0
3345
+ /* number of string matches in current block */
3346
+
3347
+ this.insert = 0
3348
+ /* bytes at end of window left to insert */
3349
+
3350
+ this.bi_buf = 0
3351
+ /* Output buffer. bits are inserted starting at the bottom (least
3352
+ * significant bits).
3353
+ */
3354
+
3355
+ this.bi_valid = 0
3356
+ /* Number of valid bits in bi_buf. All bits above the last valid bit
3357
+ * are always zero.
3358
+ */
3359
+ // Used for window memory init. We safely ignore it for JS. That makes
3360
+ // sense only for pointers and memory check tools.
3361
+ // this.high_water = 0;
3362
+
3363
+ /* High water mark offset in window for initialized bytes -- bytes above
3364
+ * this are set to zero in order to avoid memory check warnings when
3365
+ * longest match routines access bytes past the input. This is then
3366
+ * updated to the new high water mark.
3367
+ */
3368
+ }
3369
+
3370
+ var deflateResetKeep = function deflateResetKeep(strm) {
3371
+ if (!strm || !strm.state) {
3372
+ return err(strm, Z_STREAM_ERROR)
3373
+ }
3374
+
3375
+ strm.total_in = strm.total_out = 0
3376
+ strm.data_type = Z_UNKNOWN$1
3377
+ var s = strm.state
3378
+ s.pending = 0
3379
+ s.pending_out = 0
3380
+
3381
+ if (s.wrap < 0) {
3382
+ s.wrap = -s.wrap
3383
+ /* was made negative by deflate(..., Z_FINISH); */
3384
+ }
3385
+
3386
+ s.status = s.wrap ? INIT_STATE : BUSY_STATE
3387
+ strm.adler =
3388
+ s.wrap === 2
3389
+ ? 0 // crc32(0, Z_NULL, 0)
3390
+ : 1 // adler32(0, Z_NULL, 0)
3391
+
3392
+ s.last_flush = Z_NO_FLUSH
3393
+
3394
+ _tr_init$1(s)
3395
+
3396
+ return Z_OK
3397
+ }
3398
+
3399
+ var deflateReset = function deflateReset(strm) {
3400
+ var ret = deflateResetKeep(strm)
3401
+
3402
+ if (ret === Z_OK) {
3403
+ lm_init(strm.state)
3404
+ }
3405
+
3406
+ return ret
3407
+ }
3408
+
3409
+ var deflateSetHeader = function deflateSetHeader(strm, head) {
3410
+ if (!strm || !strm.state) {
3411
+ return Z_STREAM_ERROR
3412
+ }
3413
+
3414
+ if (strm.state.wrap !== 2) {
3415
+ return Z_STREAM_ERROR
3416
+ }
3417
+
3418
+ strm.state.gzhead = head
3419
+ return Z_OK
3420
+ }
3421
+
3422
+ var deflateInit2 = function deflateInit2(
3423
+ strm,
3424
+ level,
3425
+ method,
3426
+ windowBits,
3427
+ memLevel,
3428
+ strategy
3429
+ ) {
3430
+ if (!strm) {
3431
+ // === Z_NULL
3432
+ return Z_STREAM_ERROR
3433
+ }
3434
+
3435
+ var wrap = 1
3436
+
3437
+ if (level === Z_DEFAULT_COMPRESSION) {
3438
+ level = 6
3439
+ }
3440
+
3441
+ if (windowBits < 0) {
3442
+ /* suppress zlib wrapper */
3443
+ wrap = 0
3444
+ windowBits = -windowBits
3445
+ } else if (windowBits > 15) {
3446
+ wrap = 2
3447
+ /* write gzip wrapper instead */
3448
+
3449
+ windowBits -= 16
3450
+ }
3451
+
3452
+ if (
3453
+ memLevel < 1 ||
3454
+ memLevel > MAX_MEM_LEVEL ||
3455
+ method !== Z_DEFLATED ||
3456
+ windowBits < 8 ||
3457
+ windowBits > 15 ||
3458
+ level < 0 ||
3459
+ level > 9 ||
3460
+ strategy < 0 ||
3461
+ strategy > Z_FIXED$1
3462
+ ) {
3463
+ return err(strm, Z_STREAM_ERROR)
3464
+ }
3465
+
3466
+ if (windowBits === 8) {
3467
+ windowBits = 9
3468
+ }
3469
+ /* until 256-byte window bug fixed */
3470
+
3471
+ var s = new DeflateState()
3472
+ strm.state = s
3473
+ s.strm = strm
3474
+ s.wrap = wrap
3475
+ s.gzhead = null
3476
+ s.w_bits = windowBits
3477
+ s.w_size = 1 << s.w_bits
3478
+ s.w_mask = s.w_size - 1
3479
+ s.hash_bits = memLevel + 7
3480
+ s.hash_size = 1 << s.hash_bits
3481
+ s.hash_mask = s.hash_size - 1
3482
+ s.hash_shift = ~~((s.hash_bits + MIN_MATCH$1 - 1) / MIN_MATCH$1)
3483
+ s.window = new Uint8Array(s.w_size * 2)
3484
+ s.head = new Uint16Array(s.hash_size)
3485
+ s.prev = new Uint16Array(s.w_size) // Don't need mem init magic for JS.
3486
+ // s.high_water = 0; /* nothing written to s->window yet */
3487
+
3488
+ s.lit_bufsize = 1 << (memLevel + 6)
3489
+ /* 16K elements by default */
3490
+
3491
+ s.pending_buf_size = s.lit_bufsize * 4
3492
+ // overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
3493
+ // s->pending_buf = (uchf *) overlay;
3494
+
3495
+ // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`)
3496
+ s.pending_buf = new Uint8Array(s.pending_buf_size)
3497
+ // s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
3498
+
3499
+ s.d_buf = 1 * s.lit_bufsize // s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
3500
+
3501
+ s.l_buf = (1 + 2) * s.lit_bufsize
3502
+ s.level = level
3503
+ s.strategy = strategy
3504
+ s.method = method
3505
+ return deflateReset(strm)
3506
+ }
3507
+
3508
+ var deflateInit = function deflateInit(strm, level) {
3509
+ return deflateInit2(
3510
+ strm,
3511
+ level,
3512
+ Z_DEFLATED,
3513
+ MAX_WBITS,
3514
+ DEF_MEM_LEVEL,
3515
+ Z_DEFAULT_STRATEGY
3516
+ )
3517
+ }
3518
+
3519
+ var deflate = function deflate(strm, flush) {
3520
+ var beg
3521
+ var val // for gzip header write only
3522
+
3523
+ if (!strm || !strm.state || flush > Z_BLOCK || flush < 0) {
3524
+ return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR
3525
+ }
3526
+
3527
+ var s = strm.state
3528
+
3529
+ if (
3530
+ !strm.output ||
3531
+ (!strm.input && strm.avail_in !== 0) ||
3532
+ (s.status === FINISH_STATE && flush !== Z_FINISH)
3533
+ ) {
3534
+ return err(strm, strm.avail_out === 0 ? Z_BUF_ERROR : Z_STREAM_ERROR)
3535
+ }
3536
+
3537
+ s.strm = strm
3538
+ /* just in case */
3539
+
3540
+ var old_flush = s.last_flush
3541
+ s.last_flush = flush
3542
+ /* Write the header */
3543
+
3544
+ if (s.status === INIT_STATE) {
3545
+ if (s.wrap === 2) {
3546
+ // GZIP header
3547
+ strm.adler = 0 // crc32(0L, Z_NULL, 0);
3548
+
3549
+ put_byte(s, 31)
3550
+ put_byte(s, 139)
3551
+ put_byte(s, 8)
3552
+
3553
+ if (!s.gzhead) {
3554
+ // s->gzhead == Z_NULL
3555
+ put_byte(s, 0)
3556
+ put_byte(s, 0)
3557
+ put_byte(s, 0)
3558
+ put_byte(s, 0)
3559
+ put_byte(s, 0)
3560
+ put_byte(
3561
+ s,
3562
+ s.level === 9
3563
+ ? 2
3564
+ : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2
3565
+ ? 4
3566
+ : 0
3567
+ )
3568
+ put_byte(s, OS_CODE)
3569
+ s.status = BUSY_STATE
3570
+ } else {
3571
+ put_byte(
3572
+ s,
3573
+ (s.gzhead.text ? 1 : 0) +
3574
+ (s.gzhead.hcrc ? 2 : 0) +
3575
+ (!s.gzhead.extra ? 0 : 4) +
3576
+ (!s.gzhead.name ? 0 : 8) +
3577
+ (!s.gzhead.comment ? 0 : 16)
3578
+ )
3579
+ put_byte(s, s.gzhead.time & 0xff)
3580
+ put_byte(s, (s.gzhead.time >> 8) & 0xff)
3581
+ put_byte(s, (s.gzhead.time >> 16) & 0xff)
3582
+ put_byte(s, (s.gzhead.time >> 24) & 0xff)
3583
+ put_byte(
3584
+ s,
3585
+ s.level === 9
3586
+ ? 2
3587
+ : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2
3588
+ ? 4
3589
+ : 0
3590
+ )
3591
+ put_byte(s, s.gzhead.os & 0xff)
3592
+
3593
+ if (s.gzhead.extra && s.gzhead.extra.length) {
3594
+ put_byte(s, s.gzhead.extra.length & 0xff)
3595
+ put_byte(s, (s.gzhead.extra.length >> 8) & 0xff)
3596
+ }
3597
+
3598
+ if (s.gzhead.hcrc) {
3599
+ strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending, 0)
3600
+ }
3601
+
3602
+ s.gzindex = 0
3603
+ s.status = EXTRA_STATE
3604
+ }
3605
+ } // DEFLATE header
3606
+ else {
3607
+ var header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8
3608
+ var level_flags = -1
3609
+
3610
+ if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
3611
+ level_flags = 0
3612
+ } else if (s.level < 6) {
3613
+ level_flags = 1
3614
+ } else if (s.level === 6) {
3615
+ level_flags = 2
3616
+ } else {
3617
+ level_flags = 3
3618
+ }
3619
+
3620
+ header |= level_flags << 6
3621
+
3622
+ if (s.strstart !== 0) {
3623
+ header |= PRESET_DICT
3624
+ }
3625
+
3626
+ header += 31 - (header % 31)
3627
+ s.status = BUSY_STATE
3628
+ putShortMSB(s, header)
3629
+ /* Save the adler32 of the preset dictionary: */
3630
+
3631
+ if (s.strstart !== 0) {
3632
+ putShortMSB(s, strm.adler >>> 16)
3633
+ putShortMSB(s, strm.adler & 0xffff)
3634
+ }
3635
+
3636
+ strm.adler = 1 // adler32(0L, Z_NULL, 0);
3637
+ }
3638
+ } // #ifdef GZIP
3639
+
3640
+ if (s.status === EXTRA_STATE) {
3641
+ if (
3642
+ s.gzhead.extra
3643
+ /* != Z_NULL */
3644
+ ) {
3645
+ beg = s.pending
3646
+ /* start of bytes to update crc */
3647
+
3648
+ while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
3649
+ if (s.pending === s.pending_buf_size) {
3650
+ if (s.gzhead.hcrc && s.pending > beg) {
3651
+ strm.adler = crc32_1(
3652
+ strm.adler,
3653
+ s.pending_buf,
3654
+ s.pending - beg,
3655
+ beg
3656
+ )
3657
+ }
3658
+
3659
+ flush_pending(strm)
3660
+ beg = s.pending
3661
+
3662
+ if (s.pending === s.pending_buf_size) {
3663
+ break
3664
+ }
3665
+ }
3666
+
3667
+ put_byte(s, s.gzhead.extra[s.gzindex] & 0xff)
3668
+ s.gzindex++
3669
+ }
3670
+
3671
+ if (s.gzhead.hcrc && s.pending > beg) {
3672
+ strm.adler = crc32_1(
3673
+ strm.adler,
3674
+ s.pending_buf,
3675
+ s.pending - beg,
3676
+ beg
3677
+ )
3678
+ }
3679
+
3680
+ if (s.gzindex === s.gzhead.extra.length) {
3681
+ s.gzindex = 0
3682
+ s.status = NAME_STATE
3683
+ }
3684
+ } else {
3685
+ s.status = NAME_STATE
3686
+ }
3687
+ }
3688
+
3689
+ if (s.status === NAME_STATE) {
3690
+ if (
3691
+ s.gzhead.name
3692
+ /* != Z_NULL */
3693
+ ) {
3694
+ beg = s.pending
3695
+ /* start of bytes to update crc */
3696
+ // int val;
3697
+
3698
+ do {
3699
+ if (s.pending === s.pending_buf_size) {
3700
+ if (s.gzhead.hcrc && s.pending > beg) {
3701
+ strm.adler = crc32_1(
3702
+ strm.adler,
3703
+ s.pending_buf,
3704
+ s.pending - beg,
3705
+ beg
3706
+ )
3707
+ }
3708
+
3709
+ flush_pending(strm)
3710
+ beg = s.pending
3711
+
3712
+ if (s.pending === s.pending_buf_size) {
3713
+ val = 1
3714
+ break
3715
+ }
3716
+ } // JS specific: little magic to add zero terminator to end of string
3717
+
3718
+ if (s.gzindex < s.gzhead.name.length) {
3719
+ val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff
3720
+ } else {
3721
+ val = 0
3722
+ }
3723
+
3724
+ put_byte(s, val)
3725
+ } while (val !== 0)
3726
+
3727
+ if (s.gzhead.hcrc && s.pending > beg) {
3728
+ strm.adler = crc32_1(
3729
+ strm.adler,
3730
+ s.pending_buf,
3731
+ s.pending - beg,
3732
+ beg
3733
+ )
3734
+ }
3735
+
3736
+ if (val === 0) {
3737
+ s.gzindex = 0
3738
+ s.status = COMMENT_STATE
3739
+ }
3740
+ } else {
3741
+ s.status = COMMENT_STATE
3742
+ }
3743
+ }
3744
+
3745
+ if (s.status === COMMENT_STATE) {
3746
+ if (
3747
+ s.gzhead.comment
3748
+ /* != Z_NULL */
3749
+ ) {
3750
+ beg = s.pending
3751
+ /* start of bytes to update crc */
3752
+ // int val;
3753
+
3754
+ do {
3755
+ if (s.pending === s.pending_buf_size) {
3756
+ if (s.gzhead.hcrc && s.pending > beg) {
3757
+ strm.adler = crc32_1(
3758
+ strm.adler,
3759
+ s.pending_buf,
3760
+ s.pending - beg,
3761
+ beg
3762
+ )
3763
+ }
3764
+
3765
+ flush_pending(strm)
3766
+ beg = s.pending
3767
+
3768
+ if (s.pending === s.pending_buf_size) {
3769
+ val = 1
3770
+ break
3771
+ }
3772
+ } // JS specific: little magic to add zero terminator to end of string
3773
+
3774
+ if (s.gzindex < s.gzhead.comment.length) {
3775
+ val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff
3776
+ } else {
3777
+ val = 0
3778
+ }
3779
+
3780
+ put_byte(s, val)
3781
+ } while (val !== 0)
3782
+
3783
+ if (s.gzhead.hcrc && s.pending > beg) {
3784
+ strm.adler = crc32_1(
3785
+ strm.adler,
3786
+ s.pending_buf,
3787
+ s.pending - beg,
3788
+ beg
3789
+ )
3790
+ }
3791
+
3792
+ if (val === 0) {
3793
+ s.status = HCRC_STATE
3794
+ }
3795
+ } else {
3796
+ s.status = HCRC_STATE
3797
+ }
3798
+ }
3799
+
3800
+ if (s.status === HCRC_STATE) {
3801
+ if (s.gzhead.hcrc) {
3802
+ if (s.pending + 2 > s.pending_buf_size) {
3803
+ flush_pending(strm)
3804
+ }
3805
+
3806
+ if (s.pending + 2 <= s.pending_buf_size) {
3807
+ put_byte(s, strm.adler & 0xff)
3808
+ put_byte(s, (strm.adler >> 8) & 0xff)
3809
+ strm.adler = 0 // crc32(0L, Z_NULL, 0);
3810
+
3811
+ s.status = BUSY_STATE
3812
+ }
3813
+ } else {
3814
+ s.status = BUSY_STATE
3815
+ }
3816
+ } // #endif
3817
+
3818
+ /* Flush as much pending output as possible */
3819
+
3820
+ if (s.pending !== 0) {
3821
+ flush_pending(strm)
3822
+
3823
+ if (strm.avail_out === 0) {
3824
+ /* Since avail_out is 0, deflate will be called again with
3825
+ * more output space, but possibly with both pending and
3826
+ * avail_in equal to zero. There won't be anything to do,
3827
+ * but this is not an error situation so make sure we
3828
+ * return OK instead of BUF_ERROR at next call of deflate:
3829
+ */
3830
+ s.last_flush = -1
3831
+ return Z_OK
3832
+ }
3833
+ /* Make sure there is something to do and avoid duplicate consecutive
3834
+ * flushes. For repeated and useless calls with Z_FINISH, we keep
3835
+ * returning Z_STREAM_END instead of Z_BUF_ERROR.
3836
+ */
3837
+ } else if (
3838
+ strm.avail_in === 0 &&
3839
+ rank(flush) <= rank(old_flush) &&
3840
+ flush !== Z_FINISH
3841
+ ) {
3842
+ return err(strm, Z_BUF_ERROR)
3843
+ }
3844
+ /* User must not provide more input after the first FINISH: */
3845
+
3846
+ if (s.status === FINISH_STATE && strm.avail_in !== 0) {
3847
+ return err(strm, Z_BUF_ERROR)
3848
+ }
3849
+ /* Start a new block or continue the current one.
3850
+ */
3851
+
3852
+ if (
3853
+ strm.avail_in !== 0 ||
3854
+ s.lookahead !== 0 ||
3855
+ (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)
3856
+ ) {
3857
+ var bstate =
3858
+ s.strategy === Z_HUFFMAN_ONLY
3859
+ ? deflate_huff(s, flush)
3860
+ : s.strategy === Z_RLE
3861
+ ? deflate_rle(s, flush)
3862
+ : configuration_table[s.level].func(s, flush)
3863
+
3864
+ if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
3865
+ s.status = FINISH_STATE
3866
+ }
3867
+
3868
+ if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
3869
+ if (strm.avail_out === 0) {
3870
+ s.last_flush = -1
3871
+ /* avoid BUF_ERROR next call, see above */
3872
+ }
3873
+
3874
+ return Z_OK
3875
+ /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
3876
+ * of deflate should use the same flush parameter to make sure
3877
+ * that the flush is complete. So we don't have to output an
3878
+ * empty block here, this will be done at next call. This also
3879
+ * ensures that for a very small output buffer, we emit at most
3880
+ * one empty block.
3881
+ */
3882
+ }
3883
+
3884
+ if (bstate === BS_BLOCK_DONE) {
3885
+ if (flush === Z_PARTIAL_FLUSH) {
3886
+ _tr_align$1(s)
3887
+ } else if (flush !== Z_BLOCK) {
3888
+ /* FULL_FLUSH or SYNC_FLUSH */
3889
+ _tr_stored_block$1(s, 0, 0, false)
3890
+ /* For a full flush, this empty block will be recognized
3891
+ * as a special marker by inflate_sync().
3892
+ */
3893
+
3894
+ if (flush === Z_FULL_FLUSH) {
3895
+ /** * CLEAR_HASH(s); ** */
3896
+
3897
+ /* forget history */
3898
+ zero$1(s.head) // Fill with NIL (= 0);
3899
+
3900
+ if (s.lookahead === 0) {
3901
+ s.strstart = 0
3902
+ s.block_start = 0
3903
+ s.insert = 0
3904
+ }
3905
+ }
3906
+ }
3907
+
3908
+ flush_pending(strm)
3909
+
3910
+ if (strm.avail_out === 0) {
3911
+ s.last_flush = -1
3912
+ /* avoid BUF_ERROR at next call, see above */
3913
+
3914
+ return Z_OK
3915
+ }
3916
+ }
3917
+ } // Assert(strm->avail_out > 0, "bug2");
3918
+ // if (strm.avail_out <= 0) { throw new Error("bug2");}
3919
+
3920
+ if (flush !== Z_FINISH) {
3921
+ return Z_OK
3922
+ }
3923
+
3924
+ if (s.wrap <= 0) {
3925
+ return Z_STREAM_END
3926
+ }
3927
+ /* Write the trailer */
3928
+
3929
+ if (s.wrap === 2) {
3930
+ put_byte(s, strm.adler & 0xff)
3931
+ put_byte(s, (strm.adler >> 8) & 0xff)
3932
+ put_byte(s, (strm.adler >> 16) & 0xff)
3933
+ put_byte(s, (strm.adler >> 24) & 0xff)
3934
+ put_byte(s, strm.total_in & 0xff)
3935
+ put_byte(s, (strm.total_in >> 8) & 0xff)
3936
+ put_byte(s, (strm.total_in >> 16) & 0xff)
3937
+ put_byte(s, (strm.total_in >> 24) & 0xff)
3938
+ } else {
3939
+ putShortMSB(s, strm.adler >>> 16)
3940
+ putShortMSB(s, strm.adler & 0xffff)
3941
+ }
3942
+
3943
+ flush_pending(strm)
3944
+ /* If avail_out is zero, the application will call deflate again
3945
+ * to flush the rest.
3946
+ */
3947
+
3948
+ if (s.wrap > 0) {
3949
+ s.wrap = -s.wrap
3950
+ }
3951
+ /* write the trailer only once! */
3952
+
3953
+ return s.pending !== 0 ? Z_OK : Z_STREAM_END
3954
+ }
3955
+
3956
+ var deflateEnd = function deflateEnd(strm) {
3957
+ if (
3958
+ !strm ||
3959
+ /* == Z_NULL */
3960
+ !strm.state
3961
+ /* == Z_NULL */
3962
+ ) {
3963
+ return Z_STREAM_ERROR
3964
+ }
3965
+
3966
+ var status = strm.state.status
3967
+
3968
+ if (
3969
+ status !== INIT_STATE &&
3970
+ status !== EXTRA_STATE &&
3971
+ status !== NAME_STATE &&
3972
+ status !== COMMENT_STATE &&
3973
+ status !== HCRC_STATE &&
3974
+ status !== BUSY_STATE &&
3975
+ status !== FINISH_STATE
3976
+ ) {
3977
+ return err(strm, Z_STREAM_ERROR)
3978
+ }
3979
+
3980
+ strm.state = null
3981
+ return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK
3982
+ }
3983
+ /* =========================================================================
3984
+ * Initializes the compression dictionary from the given byte
3985
+ * sequence without producing any compressed output.
3986
+ */
3987
+
3988
+ var deflateSetDictionary = function deflateSetDictionary(strm, dictionary) {
3989
+ var dictLength = dictionary.length
3990
+
3991
+ if (
3992
+ !strm ||
3993
+ /* == Z_NULL */
3994
+ !strm.state
3995
+ /* == Z_NULL */
3996
+ ) {
3997
+ return Z_STREAM_ERROR
3998
+ }
3999
+
4000
+ var s = strm.state
4001
+ var wrap = s.wrap
4002
+
4003
+ if (
4004
+ wrap === 2 ||
4005
+ (wrap === 1 && s.status !== INIT_STATE) ||
4006
+ s.lookahead
4007
+ ) {
4008
+ return Z_STREAM_ERROR
4009
+ }
4010
+ /* when using zlib wrappers, compute Adler-32 for provided dictionary */
4011
+
4012
+ if (wrap === 1) {
4013
+ /* adler32(strm->adler, dictionary, dictLength); */
4014
+ strm.adler = adler32_1(strm.adler, dictionary, dictLength, 0)
4015
+ }
4016
+
4017
+ s.wrap = 0
4018
+ /* avoid computing Adler-32 in read_buf */
4019
+
4020
+ /* if dictionary would fill window, just replace the history */
4021
+
4022
+ if (dictLength >= s.w_size) {
4023
+ if (wrap === 0) {
4024
+ /* already empty otherwise */
4025
+
4026
+ /** * CLEAR_HASH(s); ** */
4027
+ zero$1(s.head) // Fill with NIL (= 0);
4028
+
4029
+ s.strstart = 0
4030
+ s.block_start = 0
4031
+ s.insert = 0
4032
+ }
4033
+ /* use the tail */
4034
+ // dictionary = dictionary.slice(dictLength - s.w_size);
4035
+
4036
+ var tmpDict = new Uint8Array(s.w_size)
4037
+ tmpDict.set(dictionary.subarray(dictLength - s.w_size, dictLength), 0)
4038
+ dictionary = tmpDict
4039
+ dictLength = s.w_size
4040
+ }
4041
+ /* insert dictionary into window and hash */
4042
+
4043
+ var avail = strm.avail_in
4044
+ var next = strm.next_in
4045
+ var input = strm.input
4046
+ strm.avail_in = dictLength
4047
+ strm.next_in = 0
4048
+ strm.input = dictionary
4049
+ fill_window(s)
4050
+
4051
+ while (s.lookahead >= MIN_MATCH$1) {
4052
+ var str = s.strstart
4053
+ var n = s.lookahead - (MIN_MATCH$1 - 1)
4054
+
4055
+ do {
4056
+ /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */
4057
+ s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH$1 - 1])
4058
+ s.prev[str & s.w_mask] = s.head[s.ins_h]
4059
+ s.head[s.ins_h] = str
4060
+ str++
4061
+ } while (--n)
4062
+
4063
+ s.strstart = str
4064
+ s.lookahead = MIN_MATCH$1 - 1
4065
+ fill_window(s)
4066
+ }
4067
+
4068
+ s.strstart += s.lookahead
4069
+ s.block_start = s.strstart
4070
+ s.insert = s.lookahead
4071
+ s.lookahead = 0
4072
+ s.match_length = s.prev_length = MIN_MATCH$1 - 1
4073
+ s.match_available = 0
4074
+ strm.next_in = next
4075
+ strm.input = input
4076
+ strm.avail_in = avail
4077
+ s.wrap = wrap
4078
+ return Z_OK
4079
+ }
4080
+
4081
+ var deflateInit_1 = deflateInit
4082
+ var deflateInit2_1 = deflateInit2
4083
+ var deflateReset_1 = deflateReset
4084
+ var deflateResetKeep_1 = deflateResetKeep
4085
+ var deflateSetHeader_1 = deflateSetHeader
4086
+ var deflate_2 = deflate
4087
+ var deflateEnd_1 = deflateEnd
4088
+ var deflateSetDictionary_1 = deflateSetDictionary
4089
+ var deflateInfo = 'pako deflate (from Nodeca project)'
4090
+ /* Not implemented
4091
+ module.exports.deflateBound = deflateBound;
4092
+ module.exports.deflateCopy = deflateCopy;
4093
+ module.exports.deflateParams = deflateParams;
4094
+ module.exports.deflatePending = deflatePending;
4095
+ module.exports.deflatePrime = deflatePrime;
4096
+ module.exports.deflateTune = deflateTune;
4097
+ */
4098
+
4099
+ var deflate_1 = {
4100
+ deflateInit: deflateInit_1,
4101
+ deflateInit2: deflateInit2_1,
4102
+ deflateReset: deflateReset_1,
4103
+ deflateResetKeep: deflateResetKeep_1,
4104
+ deflateSetHeader: deflateSetHeader_1,
4105
+ deflate: deflate_2,
4106
+ deflateEnd: deflateEnd_1,
4107
+ deflateSetDictionary: deflateSetDictionary_1,
4108
+ deflateInfo
4109
+ }
4110
+
4111
+ // Join array of chunks to single array.
4112
+ function flattenChunks(chunks) {
4113
+ // calculate data length
4114
+ var len = 0
4115
+
4116
+ for (var i = 0, l = chunks.length; i < l; i++) {
4117
+ len += chunks[i].length
4118
+ } // join chunks
4119
+
4120
+ var result = new Uint8Array(len)
4121
+
4122
+ for (var _i = 0, pos = 0, _l = chunks.length; _i < _l; _i++) {
4123
+ var chunk = chunks[_i]
4124
+ result.set(chunk, pos)
4125
+ pos += chunk.length
4126
+ }
4127
+
4128
+ return result
4129
+ }
4130
+
4131
+ // String encode/decode helpers
4132
+ //
4133
+ // - apply(Array) can fail on Android 2.2
4134
+ // - apply(Uint8Array) can fail on iOS 5.1 Safari
4135
+ //
4136
+
4137
+ // Table with utf8 lengths (calculated by first byte of sequence)
4138
+ // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
4139
+ // because max possible codepoint is 0x10ffff
4140
+
4141
+ var _utf8len = new Uint8Array(256)
4142
+
4143
+ for (var q = 0; q < 256; q++) {
4144
+ _utf8len[q] =
4145
+ q >= 252
4146
+ ? 6
4147
+ : q >= 248
4148
+ ? 5
4149
+ : q >= 240
4150
+ ? 4
4151
+ : q >= 224
4152
+ ? 3
4153
+ : q >= 192
4154
+ ? 2
4155
+ : 1
4156
+ }
4157
+
4158
+ _utf8len[254] = _utf8len[254] = 1 // Invalid sequence start
4159
+ // convert string to array (typed, when possible)
4160
+
4161
+ // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
4162
+ //
4163
+ // This software is provided 'as-is', without any express or implied
4164
+ // warranty. In no event will the authors be held liable for any damages
4165
+ // arising from the use of this software.
4166
+ //
4167
+ // Permission is granted to anyone to use this software for any purpose,
4168
+ // including commercial applications, and to alter it and redistribute it
4169
+ // freely, subject to the following restrictions:
4170
+ //
4171
+ // 1. The origin of this software must not be misrepresented; you must not
4172
+ // claim that you wrote the original software. If you use this software
4173
+ // in a product, an acknowledgment in the product documentation would be
4174
+ // appreciated but is not required.
4175
+ // 2. Altered source versions must be plainly marked as such, and must not be
4176
+ // misrepresented as being the original software.
4177
+ // 3. This notice may not be removed or altered from any source distribution.
4178
+
4179
+ function ZStream() {
4180
+ /* next input byte */
4181
+ this.input = null // JS specific, because we have no pointers
4182
+
4183
+ this.next_in = 0
4184
+ /* number of bytes available at input */
4185
+
4186
+ this.avail_in = 0
4187
+ /* total number of input bytes read so far */
4188
+
4189
+ this.total_in = 0
4190
+ /* next output byte should be put there */
4191
+
4192
+ this.output = null // JS specific, because we have no pointers
4193
+
4194
+ this.next_out = 0
4195
+ /* remaining free space at output */
4196
+
4197
+ this.avail_out = 0
4198
+ /* total number of bytes output so far */
4199
+
4200
+ this.total_out = 0
4201
+ /* last error message, NULL if no error */
4202
+
4203
+ this.msg = ''
4204
+ /* Z_NULL */
4205
+ /* not visible by applications */
4206
+
4207
+ this.state = null
4208
+ /* best guess about the data type: binary or text */
4209
+
4210
+ this.data_type = 2
4211
+ /* Z_UNKNOWN */
4212
+ /* adler32 value of the uncompressed data */
4213
+
4214
+ this.adler = 0
4215
+ }
4216
+
4217
+ var zstream = ZStream
4218
+
4219
+ // eslint-disable-next-line @typescript-eslint/unbound-method
4220
+ var toString = Object.prototype.toString
4221
+ /* Public constants ========================================================== */
4222
+
4223
+ /* =========================================================================== */
4224
+
4225
+ var Z_NO_FLUSH$1 = constants.Z_NO_FLUSH
4226
+ var Z_SYNC_FLUSH = constants.Z_SYNC_FLUSH
4227
+ var Z_FULL_FLUSH$1 = constants.Z_FULL_FLUSH
4228
+ var Z_FINISH$1 = constants.Z_FINISH
4229
+ var Z_OK$1 = constants.Z_OK
4230
+ var Z_STREAM_END$1 = constants.Z_STREAM_END
4231
+ var Z_DEFAULT_COMPRESSION$1 = constants.Z_DEFAULT_COMPRESSION
4232
+ var Z_DEFAULT_STRATEGY$1 = constants.Z_DEFAULT_STRATEGY
4233
+ var Z_DEFLATED$1 = constants.Z_DEFLATED
4234
+ /* =========================================================================== */
4235
+
4236
+ /**
4237
+ * class Deflate
4238
+ *
4239
+ * Generic JS-style wrapper for zlib calls. If you don't need
4240
+ * streaming behaviour - use more simple functions: [[deflate]],
4241
+ * [[deflateRaw]] and [[gzip]].
4242
+ * */
4243
+
4244
+ /* internal
4245
+ * Deflate.chunks -> Array
4246
+ *
4247
+ * Chunks of output data, if [[Deflate#onData]] not overridden.
4248
+ * */
4249
+
4250
+ /**
4251
+ * Deflate.result -> Uint8Array
4252
+ *
4253
+ * Compressed result, generated by default [[Deflate#onData]]
4254
+ * and [[Deflate#onEnd]] handlers. Filled after you push last chunk
4255
+ * (call [[Deflate#push]] with `Z_FINISH` / `true` param).
4256
+ * */
4257
+
4258
+ /**
4259
+ * Deflate.err -> Number
4260
+ *
4261
+ * Error code after deflate finished. 0 (Z_OK) on success.
4262
+ * You will not need it in real life, because deflate errors
4263
+ * are possible only on wrong options or bad `onData` / `onEnd`
4264
+ * custom handlers.
4265
+ * */
4266
+
4267
+ /**
4268
+ * Deflate.msg -> String
4269
+ *
4270
+ * Error message, if [[Deflate.err]] != 0
4271
+ * */
4272
+
4273
+ /**
4274
+ * new Deflate(options)
4275
+ * - options (Object): zlib deflate options.
4276
+ *
4277
+ * Creates new deflator instance with specified params. Throws exception
4278
+ * on bad params. Supported options:
4279
+ *
4280
+ * - `level`
4281
+ * - `windowBits`
4282
+ * - `memLevel`
4283
+ * - `strategy`
4284
+ * - `dictionary`
4285
+ *
4286
+ * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
4287
+ * for more information on these.
4288
+ *
4289
+ * Additional options, for internal needs:
4290
+ *
4291
+ * * `chunkSize` - size of generated data chunks (16K by default)
4292
+ * * `raw` (Boolean) - do raw deflate
4293
+ * * `gzip` (Boolean) - create gzip wrapper
4294
+ * * `header` (Object) - custom header for gzip
4295
+ * ** `text` (Boolean) - true if compressed data believed to be text
4296
+ * ** `time` (Number) - modification time, unix timestamp
4297
+ * ** `os` (Number) - operation system code
4298
+ * ** `extra` (Array) - array of bytes with extra data (max 65536)
4299
+ * ** `name` (String) - file name (binary string)
4300
+ * ** `comment` (String) - comment (binary string)
4301
+ * ** `hcrc` (Boolean) - true if header crc should be added
4302
+ *
4303
+ * ##### Example:
4304
+ *
4305
+ * ```javascript
4306
+ * const pako = require('pako')
4307
+ * , chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9])
4308
+ * , chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]);
4309
+ *
4310
+ * const deflate = new pako.Deflate({ level: 3});
4311
+ *
4312
+ * deflate.push(chunk1, false);
4313
+ * deflate.push(chunk2, true); // true -> last chunk
4314
+ *
4315
+ * if (deflate.err) { throw new Error(deflate.err); }
4316
+ *
4317
+ * console.log(deflate.result);
4318
+ * ```
4319
+ * */
4320
+
4321
+ function Deflate() {
4322
+ this.options = {
4323
+ level: Z_DEFAULT_COMPRESSION$1,
4324
+ method: Z_DEFLATED$1,
4325
+ chunkSize: 16384,
4326
+ windowBits: 15,
4327
+ memLevel: 8,
4328
+ strategy: Z_DEFAULT_STRATEGY$1
4329
+ }
4330
+ var opt = this.options
4331
+
4332
+ if (opt.raw && opt.windowBits > 0) {
4333
+ opt.windowBits = -opt.windowBits
4334
+ } else if (opt.gzip && opt.windowBits > 0 && opt.windowBits < 16) {
4335
+ opt.windowBits += 16
4336
+ }
4337
+
4338
+ this.err = 0 // error code, if happens (0 = Z_OK)
4339
+
4340
+ this.msg = '' // error message
4341
+
4342
+ this.ended = false // used to avoid multiple onEnd() calls
4343
+
4344
+ this.chunks = [] // chunks of compressed data
4345
+
4346
+ this.strm = new zstream()
4347
+ this.strm.avail_out = 0
4348
+ var status = deflate_1.deflateInit2(
4349
+ this.strm,
4350
+ opt.level,
4351
+ opt.method,
4352
+ opt.windowBits,
4353
+ opt.memLevel,
4354
+ opt.strategy
4355
+ )
4356
+
4357
+ if (status !== Z_OK$1) {
4358
+ throw new Error(messages[status])
4359
+ }
4360
+
4361
+ if (opt.header) {
4362
+ deflate_1.deflateSetHeader(this.strm, opt.header)
4363
+ }
4364
+
4365
+ if (opt.dictionary) {
4366
+ var dict // Convert data if needed
4367
+
4368
+ if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
4369
+ dict = new Uint8Array(opt.dictionary)
4370
+ } else {
4371
+ dict = opt.dictionary
4372
+ }
4373
+
4374
+ status = deflate_1.deflateSetDictionary(this.strm, dict)
4375
+
4376
+ if (status !== Z_OK$1) {
4377
+ throw new Error(messages[status])
4378
+ }
4379
+
4380
+ this._dict_set = true
4381
+ }
4382
+ }
4383
+ /**
4384
+ * Deflate#push(data[, flush_mode]) -> Boolean
4385
+ * - data (Uint8Array|ArrayBuffer|String): input data. Strings will be
4386
+ * converted to utf8 byte sequence.
4387
+ * - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
4388
+ * See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH.
4389
+ *
4390
+ * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with
4391
+ * new compressed chunks. Returns `true` on success. The last data block must
4392
+ * have `flush_mode` Z_FINISH (or `true`). That will flush internal pending
4393
+ * buffers and call [[Deflate#onEnd]].
4394
+ *
4395
+ * On fail call [[Deflate#onEnd]] with error code and return false.
4396
+ *
4397
+ * ##### Example
4398
+ *
4399
+ * ```javascript
4400
+ * push(chunk, false); // push one of data chunks
4401
+ * ...
4402
+ * push(chunk, true); // push last chunk
4403
+ * ```
4404
+ * */
4405
+
4406
+ Deflate.prototype.push = function (data, flush_mode) {
4407
+ var strm = this.strm
4408
+ var chunkSize = this.options.chunkSize
4409
+
4410
+ var status
4411
+ var _flush_mode
4412
+
4413
+ if (this.ended) {
4414
+ return false
4415
+ }
4416
+
4417
+ if (flush_mode === ~~flush_mode) {
4418
+ _flush_mode = flush_mode
4419
+ } else {
4420
+ _flush_mode = flush_mode === true ? Z_FINISH$1 : Z_NO_FLUSH$1
4421
+ } // Convert data if needed
4422
+
4423
+ if (toString.call(data) === '[object ArrayBuffer]') {
4424
+ strm.input = new Uint8Array(data)
4425
+ } else {
4426
+ strm.input = data
4427
+ }
4428
+
4429
+ strm.next_in = 0
4430
+ strm.avail_in = strm.input.length
4431
+
4432
+ for (;;) {
4433
+ if (strm.avail_out === 0) {
4434
+ strm.output = new Uint8Array(chunkSize)
4435
+ strm.next_out = 0
4436
+ strm.avail_out = chunkSize
4437
+ } // Make sure avail_out > 6 to avoid repeating markers
4438
+
4439
+ if (
4440
+ (_flush_mode === Z_SYNC_FLUSH || _flush_mode === Z_FULL_FLUSH$1) &&
4441
+ strm.avail_out <= 6
4442
+ ) {
4443
+ this.onData(strm.output.subarray(0, strm.next_out))
4444
+ strm.avail_out = 0
4445
+ continue
4446
+ }
4447
+
4448
+ status = deflate_1.deflate(strm, _flush_mode) // Ended => flush and finish
4449
+
4450
+ if (status === Z_STREAM_END$1) {
4451
+ if (strm.next_out > 0) {
4452
+ this.onData(strm.output.subarray(0, strm.next_out))
4453
+ }
4454
+
4455
+ status = deflate_1.deflateEnd(this.strm)
4456
+ this.onEnd(status)
4457
+ this.ended = true
4458
+ return status === Z_OK$1
4459
+ } // Flush if out buffer full
4460
+
4461
+ if (strm.avail_out === 0) {
4462
+ this.onData(strm.output)
4463
+ continue
4464
+ } // Flush if requested and has data
4465
+
4466
+ if (_flush_mode > 0 && strm.next_out > 0) {
4467
+ this.onData(strm.output.subarray(0, strm.next_out))
4468
+ strm.avail_out = 0
4469
+ continue
4470
+ }
4471
+
4472
+ if (strm.avail_in === 0) {
4473
+ break
4474
+ }
4475
+ }
4476
+
4477
+ return true
4478
+ }
4479
+ /**
4480
+ * Deflate#onData(chunk) -> Void
4481
+ * - chunk (Uint8Array): output data.
4482
+ *
4483
+ * By default, stores data blocks in `chunks[]` property and glue
4484
+ * those in `onEnd`. Override this handler, if you need another behaviour.
4485
+ * */
4486
+
4487
+ Deflate.prototype.onData = function (chunk) {
4488
+ this.chunks.push(chunk)
4489
+ }
4490
+ /**
4491
+ * Deflate#onEnd(status) -> Void
4492
+ * - status (Number): deflate status. 0 (Z_OK) on success,
4493
+ * other if not.
4494
+ *
4495
+ * Called once after you tell deflate that the input stream is
4496
+ * complete (Z_FINISH). By default - join collected chunks,
4497
+ * free memory and fill `results` / `err` properties.
4498
+ * */
4499
+
4500
+ Deflate.prototype.onEnd = function (status) {
4501
+ // On success - join
4502
+ if (status === Z_OK$1) {
4503
+ this.result = flattenChunks(this.chunks)
4504
+ }
4505
+
4506
+ this.chunks = []
4507
+ this.err = status
4508
+ this.msg = this.strm.msg
4509
+ }
4510
+
4511
+ // https://github.com/nodeca/pako/blob/26dff4fb3472c5532b3bd8856421146d35ab7592/lib/utils/strings.js#L26
4512
+ function string2buf(str) {
4513
+ if (typeof TextEncoder === 'function' && TextEncoder.prototype.encode) {
4514
+ return new TextEncoder().encode(str)
4515
+ }
4516
+
4517
+ let buf
4518
+ let c
4519
+ let c2
4520
+ let m_pos
4521
+ let i
4522
+ let str_len = str.length
4523
+ let buf_len = 0
4524
+
4525
+ // count binary size
4526
+ for (m_pos = 0; m_pos < str_len; m_pos++) {
4527
+ c = str.charCodeAt(m_pos)
4528
+ if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) {
4529
+ c2 = str.charCodeAt(m_pos + 1)
4530
+ if ((c2 & 0xfc00) === 0xdc00) {
4531
+ c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00)
4532
+ m_pos++
4533
+ }
4534
+ }
4535
+ buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4
4536
+ }
4537
+
4538
+ // allocate buffer
4539
+ buf = new Uint8Array(buf_len)
4540
+
4541
+ // convert
4542
+ for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
4543
+ c = str.charCodeAt(m_pos)
4544
+ if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) {
4545
+ c2 = str.charCodeAt(m_pos + 1)
4546
+ if ((c2 & 0xfc00) === 0xdc00) {
4547
+ c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00)
4548
+ m_pos++
4549
+ }
4550
+ }
4551
+ if (c < 0x80) {
4552
+ /* one byte */
4553
+ buf[i++] = c
4554
+ } else if (c < 0x800) {
4555
+ /* two bytes */
4556
+ buf[i++] = 0xc0 | (c >>> 6)
4557
+ buf[i++] = 0x80 | (c & 0x3f)
4558
+ } else if (c < 0x10000) {
4559
+ /* three bytes */
4560
+ buf[i++] = 0xe0 | (c >>> 12)
4561
+ buf[i++] = 0x80 | ((c >>> 6) & 0x3f)
4562
+ buf[i++] = 0x80 | (c & 0x3f)
4563
+ } else {
4564
+ /* four bytes */
4565
+ buf[i++] = 0xf0 | (c >>> 18)
4566
+ buf[i++] = 0x80 | ((c >>> 12) & 0x3f)
4567
+ buf[i++] = 0x80 | ((c >>> 6) & 0x3f)
4568
+ buf[i++] = 0x80 | (c & 0x3f)
4569
+ }
4570
+ }
4571
+
4572
+ return buf
4573
+ }
4574
+
4575
+ return { Deflate, constants, string2buf }
4576
+ }
4577
+ }