@cloudcare/browser-rum 2.2.15 → 3.0.1

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