typenative 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/README.md +17 -1
  3. package/bin/cli.js +25 -11
  4. package/bin/go/convert.js +11 -1
  5. package/bin/index.js +221 -44
  6. package/bin/main.js +35 -14
  7. package/bin/node/assert.js +2 -2
  8. package/bin/node/buffer.js +9 -0
  9. package/bin/node/dns.js +1 -1
  10. package/bin/node/events.js +1 -1
  11. package/bin/node/fs.js +1 -1
  12. package/bin/node/fs_watch.js +407 -0
  13. package/bin/node/http2.js +33 -6
  14. package/bin/node/readline.js +1 -1
  15. package/bin/node/registry.js +2 -1
  16. package/bin/node/stream.js +6 -3
  17. package/bin/node/timers.js +1 -1
  18. package/bin/node/tls.js +3 -1
  19. package/bin/node/typed_arrays.js +608 -0
  20. package/bin/node/util.js +3 -0
  21. package/bin/node/zlib.js +131 -16
  22. package/bin/transpiler.js +684 -62
  23. package/bin/version.js +1 -1
  24. package/conformance.md +16 -4
  25. package/examples/README.md +54 -0
  26. package/examples/fibonacci/README.md +89 -0
  27. package/examples/fibonacci/bench.mjs +87 -0
  28. package/examples/fibonacci/main.ts +21 -0
  29. package/examples/hello-world/README.md +62 -0
  30. package/examples/hello-world/bench.mjs +66 -0
  31. package/examples/hello-world/main.ts +7 -0
  32. package/examples/json-server/README.md +125 -0
  33. package/examples/json-server/bench.mjs +171 -0
  34. package/examples/json-server/main.ts +67 -0
  35. package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
  36. package/govendor/github.com/andybalholm/brotli/README.md +19 -0
  37. package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
  38. package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
  39. package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
  40. package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
  41. package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
  42. package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
  43. package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
  44. package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
  45. package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
  46. package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
  47. package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
  48. package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
  49. package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
  50. package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
  51. package/govendor/github.com/andybalholm/brotli/command.go +254 -0
  52. package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
  53. package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
  54. package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
  55. package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
  56. package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
  57. package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
  58. package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
  59. package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
  60. package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
  61. package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
  62. package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
  63. package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
  64. package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
  65. package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
  66. package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
  67. package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
  68. package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
  69. package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
  70. package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
  71. package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
  72. package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
  73. package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
  74. package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
  75. package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
  76. package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
  77. package/govendor/github.com/andybalholm/brotli/http.go +201 -0
  78. package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
  79. package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
  80. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
  81. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
  82. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
  83. package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
  84. package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
  85. package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
  86. package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
  87. package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
  88. package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
  89. package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
  90. package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
  91. package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
  92. package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
  93. package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
  94. package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
  95. package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
  96. package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
  97. package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
  98. package/govendor/github.com/andybalholm/brotli/params.go +37 -0
  99. package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
  100. package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
  101. package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
  102. package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
  103. package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
  104. package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
  105. package/govendor/github.com/andybalholm/brotli/state.go +313 -0
  106. package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
  107. package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
  108. package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
  109. package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
  110. package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
  111. package/govendor/github.com/andybalholm/brotli/util.go +7 -0
  112. package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
  113. package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
  114. package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
  115. package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
  116. package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
  117. package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
  118. package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
  119. package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
  120. package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
  121. package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
  122. package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
  123. package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
  124. package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
  125. package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
  126. package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
  127. package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
  128. package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
  129. package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
  130. package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
  131. package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
  132. package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
  133. package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
  134. package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
  135. package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
  136. package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
  137. package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
  138. package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
  139. package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
  140. package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
  141. package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
  142. package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
  143. package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
  144. package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
  145. package/govendor/manifest.txt +3 -1
  146. package/package.json +1 -1
  147. package/types/typenative-npm.d.ts +72 -0
  148. package/types/typenative.d.ts +117 -1
@@ -0,0 +1,1435 @@
1
+ package brotli
2
+
3
+ import (
4
+ "slices"
5
+ "sync"
6
+ )
7
+
8
+ const maxHuffmanTreeSize = (2*numCommandSymbols + 1)
9
+
10
+ /*
11
+ The maximum size of Huffman dictionary for distances assuming that
12
+
13
+ NPOSTFIX = 0 and NDIRECT = 0.
14
+ */
15
+ const maxSimpleDistanceAlphabetSize = 140
16
+
17
+ /*
18
+ Represents the range of values belonging to a prefix code:
19
+
20
+ [offset, offset + 2^nbits)
21
+ */
22
+ type prefixCodeRange struct {
23
+ offset uint32
24
+ nbits uint32
25
+ }
26
+
27
+ var kBlockLengthPrefixCode = [numBlockLenSymbols]prefixCodeRange{
28
+ prefixCodeRange{1, 2},
29
+ prefixCodeRange{5, 2},
30
+ prefixCodeRange{9, 2},
31
+ prefixCodeRange{13, 2},
32
+ prefixCodeRange{17, 3},
33
+ prefixCodeRange{25, 3},
34
+ prefixCodeRange{33, 3},
35
+ prefixCodeRange{41, 3},
36
+ prefixCodeRange{49, 4},
37
+ prefixCodeRange{65, 4},
38
+ prefixCodeRange{81, 4},
39
+ prefixCodeRange{97, 4},
40
+ prefixCodeRange{113, 5},
41
+ prefixCodeRange{145, 5},
42
+ prefixCodeRange{177, 5},
43
+ prefixCodeRange{209, 5},
44
+ prefixCodeRange{241, 6},
45
+ prefixCodeRange{305, 6},
46
+ prefixCodeRange{369, 7},
47
+ prefixCodeRange{497, 8},
48
+ prefixCodeRange{753, 9},
49
+ prefixCodeRange{1265, 10},
50
+ prefixCodeRange{2289, 11},
51
+ prefixCodeRange{4337, 12},
52
+ prefixCodeRange{8433, 13},
53
+ prefixCodeRange{16625, 24},
54
+ }
55
+
56
+ func blockLengthPrefixCode(len uint32) uint32 {
57
+ var code uint32
58
+ if len >= 177 {
59
+ if len >= 753 {
60
+ code = 20
61
+ } else {
62
+ code = 14
63
+ }
64
+ } else if len >= 41 {
65
+ code = 7
66
+ } else {
67
+ code = 0
68
+ }
69
+ for code < (numBlockLenSymbols-1) && len >= kBlockLengthPrefixCode[code+1].offset {
70
+ code++
71
+ }
72
+ return code
73
+ }
74
+
75
+ func getBlockLengthPrefixCode(len uint32, code *uint, n_extra *uint32, extra *uint32) {
76
+ *code = uint(blockLengthPrefixCode(uint32(len)))
77
+ *n_extra = kBlockLengthPrefixCode[*code].nbits
78
+ *extra = len - kBlockLengthPrefixCode[*code].offset
79
+ }
80
+
81
+ type blockTypeCodeCalculator struct {
82
+ last_type uint
83
+ second_last_type uint
84
+ }
85
+
86
+ func initBlockTypeCodeCalculator(self *blockTypeCodeCalculator) {
87
+ self.last_type = 1
88
+ self.second_last_type = 0
89
+ }
90
+
91
+ func nextBlockTypeCode(calculator *blockTypeCodeCalculator, type_ byte) uint {
92
+ var type_code uint
93
+ if uint(type_) == calculator.last_type+1 {
94
+ type_code = 1
95
+ } else if uint(type_) == calculator.second_last_type {
96
+ type_code = 0
97
+ } else {
98
+ type_code = uint(type_) + 2
99
+ }
100
+ calculator.second_last_type = calculator.last_type
101
+ calculator.last_type = uint(type_)
102
+ return type_code
103
+ }
104
+
105
+ /*
106
+ |nibblesbits| represents the 2 bits to encode MNIBBLES (0-3)
107
+
108
+ REQUIRES: length > 0
109
+ REQUIRES: length <= (1 << 24)
110
+ */
111
+ func encodeMlen(length uint, bits *uint64, numbits *uint, nibblesbits *uint64) {
112
+ var lg uint
113
+ if length == 1 {
114
+ lg = 1
115
+ } else {
116
+ lg = uint(log2FloorNonZero(uint(uint32(length-1)))) + 1
117
+ }
118
+ var tmp uint
119
+ if lg < 16 {
120
+ tmp = 16
121
+ } else {
122
+ tmp = (lg + 3)
123
+ }
124
+ var mnibbles uint = tmp / 4
125
+ assert(length > 0)
126
+ assert(length <= 1<<24)
127
+ assert(lg <= 24)
128
+ *nibblesbits = uint64(mnibbles) - 4
129
+ *numbits = mnibbles * 4
130
+ *bits = uint64(length) - 1
131
+ }
132
+
133
+ func storeCommandExtra(cmd *command, storage_ix *uint, storage []byte) {
134
+ var copylen_code uint32 = commandCopyLenCode(cmd)
135
+ var inscode uint16 = getInsertLengthCode(uint(cmd.insert_len_))
136
+ var copycode uint16 = getCopyLengthCode(uint(copylen_code))
137
+ var insnumextra uint32 = getInsertExtra(inscode)
138
+ var insextraval uint64 = uint64(cmd.insert_len_) - uint64(getInsertBase(inscode))
139
+ var copyextraval uint64 = uint64(copylen_code) - uint64(getCopyBase(copycode))
140
+ var bits uint64 = copyextraval<<insnumextra | insextraval
141
+ writeBits(uint(insnumextra+getCopyExtra(copycode)), bits, storage_ix, storage)
142
+ }
143
+
144
+ /*
145
+ Data structure that stores almost everything that is needed to encode each
146
+
147
+ block switch command.
148
+ */
149
+ type blockSplitCode struct {
150
+ type_code_calculator blockTypeCodeCalculator
151
+ type_depths [maxBlockTypeSymbols]byte
152
+ type_bits [maxBlockTypeSymbols]uint16
153
+ length_depths [numBlockLenSymbols]byte
154
+ length_bits [numBlockLenSymbols]uint16
155
+ }
156
+
157
+ /* Stores a number between 0 and 255. */
158
+ func storeVarLenUint8(n uint, storage_ix *uint, storage []byte) {
159
+ if n == 0 {
160
+ writeBits(1, 0, storage_ix, storage)
161
+ } else {
162
+ var nbits uint = uint(log2FloorNonZero(n))
163
+ writeBits(1, 1, storage_ix, storage)
164
+ writeBits(3, uint64(nbits), storage_ix, storage)
165
+ writeBits(nbits, uint64(n)-(uint64(uint(1))<<nbits), storage_ix, storage)
166
+ }
167
+ }
168
+
169
+ /*
170
+ Stores the compressed meta-block header.
171
+
172
+ REQUIRES: length > 0
173
+ REQUIRES: length <= (1 << 24)
174
+ */
175
+ func storeCompressedMetaBlockHeader(is_final_block bool, length uint, storage_ix *uint, storage []byte) {
176
+ var lenbits uint64
177
+ var nlenbits uint
178
+ var nibblesbits uint64
179
+ var is_final uint64
180
+ if is_final_block {
181
+ is_final = 1
182
+ } else {
183
+ is_final = 0
184
+ }
185
+
186
+ /* Write ISLAST bit. */
187
+ writeBits(1, is_final, storage_ix, storage)
188
+
189
+ /* Write ISEMPTY bit. */
190
+ if is_final_block {
191
+ writeBits(1, 0, storage_ix, storage)
192
+ }
193
+
194
+ encodeMlen(length, &lenbits, &nlenbits, &nibblesbits)
195
+ writeBits(2, nibblesbits, storage_ix, storage)
196
+ writeBits(nlenbits, lenbits, storage_ix, storage)
197
+
198
+ if !is_final_block {
199
+ /* Write ISUNCOMPRESSED bit. */
200
+ writeBits(1, 0, storage_ix, storage)
201
+ }
202
+ }
203
+
204
+ /*
205
+ Stores the uncompressed meta-block header.
206
+
207
+ REQUIRES: length > 0
208
+ REQUIRES: length <= (1 << 24)
209
+ */
210
+ func storeUncompressedMetaBlockHeader(length uint, storage_ix *uint, storage []byte) {
211
+ var lenbits uint64
212
+ var nlenbits uint
213
+ var nibblesbits uint64
214
+
215
+ /* Write ISLAST bit.
216
+ Uncompressed block cannot be the last one, so set to 0. */
217
+ writeBits(1, 0, storage_ix, storage)
218
+
219
+ encodeMlen(length, &lenbits, &nlenbits, &nibblesbits)
220
+ writeBits(2, nibblesbits, storage_ix, storage)
221
+ writeBits(nlenbits, lenbits, storage_ix, storage)
222
+
223
+ /* Write ISUNCOMPRESSED bit. */
224
+ writeBits(1, 1, storage_ix, storage)
225
+ }
226
+
227
+ var storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder = [codeLengthCodes]byte{1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15}
228
+
229
+ var storeHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeSymbols = [6]byte{0, 7, 3, 2, 1, 15}
230
+ var storeHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeBitLengths = [6]byte{2, 4, 3, 2, 2, 4}
231
+
232
+ func storeHuffmanTreeOfHuffmanTreeToBitMask(num_codes int, code_length_bitdepth []byte, storage_ix *uint, storage []byte) {
233
+ var skip_some uint = 0
234
+ var codes_to_store uint = codeLengthCodes
235
+ /* The bit lengths of the Huffman code over the code length alphabet
236
+ are compressed with the following static Huffman code:
237
+ Symbol Code
238
+ ------ ----
239
+ 0 00
240
+ 1 1110
241
+ 2 110
242
+ 3 01
243
+ 4 10
244
+ 5 1111 */
245
+
246
+ /* Throw away trailing zeros: */
247
+ if num_codes > 1 {
248
+ for ; codes_to_store > 0; codes_to_store-- {
249
+ if code_length_bitdepth[storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[codes_to_store-1]] != 0 {
250
+ break
251
+ }
252
+ }
253
+ }
254
+
255
+ if code_length_bitdepth[storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[0]] == 0 && code_length_bitdepth[storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[1]] == 0 {
256
+ skip_some = 2 /* skips two. */
257
+ if code_length_bitdepth[storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[2]] == 0 {
258
+ skip_some = 3 /* skips three. */
259
+ }
260
+ }
261
+
262
+ writeBits(2, uint64(skip_some), storage_ix, storage)
263
+ {
264
+ var i uint
265
+ for i = skip_some; i < codes_to_store; i++ {
266
+ var l uint = uint(code_length_bitdepth[storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[i]])
267
+ writeBits(uint(storeHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeBitLengths[l]), uint64(storeHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeSymbols[l]), storage_ix, storage)
268
+ }
269
+ }
270
+ }
271
+
272
+ func storeHuffmanTreeToBitMask(huffman_tree_size uint, huffman_tree []byte, huffman_tree_extra_bits []byte, code_length_bitdepth []byte, code_length_bitdepth_symbols []uint16, storage_ix *uint, storage []byte) {
273
+ var i uint
274
+ for i = 0; i < huffman_tree_size; i++ {
275
+ var ix uint = uint(huffman_tree[i])
276
+ writeBits(uint(code_length_bitdepth[ix]), uint64(code_length_bitdepth_symbols[ix]), storage_ix, storage)
277
+
278
+ /* Extra bits */
279
+ switch ix {
280
+ case repeatPreviousCodeLength:
281
+ writeBits(2, uint64(huffman_tree_extra_bits[i]), storage_ix, storage)
282
+
283
+ case repeatZeroCodeLength:
284
+ writeBits(3, uint64(huffman_tree_extra_bits[i]), storage_ix, storage)
285
+ }
286
+ }
287
+ }
288
+
289
+ func storeSimpleHuffmanTree(depths []byte, symbols []uint, num_symbols uint, max_bits uint, storage_ix *uint, storage []byte) {
290
+ /* value of 1 indicates a simple Huffman code */
291
+ writeBits(2, 1, storage_ix, storage)
292
+
293
+ writeBits(2, uint64(num_symbols)-1, storage_ix, storage) /* NSYM - 1 */
294
+ {
295
+ /* Sort */
296
+ var i uint
297
+ for i = 0; i < num_symbols; i++ {
298
+ var j uint
299
+ for j = i + 1; j < num_symbols; j++ {
300
+ if depths[symbols[j]] < depths[symbols[i]] {
301
+ var tmp uint = symbols[j]
302
+ symbols[j] = symbols[i]
303
+ symbols[i] = tmp
304
+ }
305
+ }
306
+ }
307
+ }
308
+
309
+ if num_symbols == 2 {
310
+ writeBits(max_bits, uint64(symbols[0]), storage_ix, storage)
311
+ writeBits(max_bits, uint64(symbols[1]), storage_ix, storage)
312
+ } else if num_symbols == 3 {
313
+ writeBits(max_bits, uint64(symbols[0]), storage_ix, storage)
314
+ writeBits(max_bits, uint64(symbols[1]), storage_ix, storage)
315
+ writeBits(max_bits, uint64(symbols[2]), storage_ix, storage)
316
+ } else {
317
+ writeBits(max_bits, uint64(symbols[0]), storage_ix, storage)
318
+ writeBits(max_bits, uint64(symbols[1]), storage_ix, storage)
319
+ writeBits(max_bits, uint64(symbols[2]), storage_ix, storage)
320
+ writeBits(max_bits, uint64(symbols[3]), storage_ix, storage)
321
+
322
+ /* tree-select */
323
+ var tmp int
324
+ if depths[symbols[0]] == 1 {
325
+ tmp = 1
326
+ } else {
327
+ tmp = 0
328
+ }
329
+ writeBits(1, uint64(tmp), storage_ix, storage)
330
+ }
331
+ }
332
+
333
+ /*
334
+ num = alphabet size
335
+
336
+ depths = symbol depths
337
+ */
338
+ func storeHuffmanTree(depths []byte, num uint, tree []huffmanTree, storage_ix *uint, storage []byte) {
339
+ var huffman_tree [numCommandSymbols]byte
340
+ var huffman_tree_extra_bits [numCommandSymbols]byte
341
+ var huffman_tree_size uint = 0
342
+ var code_length_bitdepth = [codeLengthCodes]byte{0}
343
+ var code_length_bitdepth_symbols [codeLengthCodes]uint16
344
+ var huffman_tree_histogram = [codeLengthCodes]uint32{0}
345
+ var i uint
346
+ var num_codes int = 0
347
+ /* Write the Huffman tree into the brotli-representation.
348
+ The command alphabet is the largest, so this allocation will fit all
349
+ alphabets. */
350
+
351
+ var code uint = 0
352
+
353
+ assert(num <= numCommandSymbols)
354
+
355
+ writeHuffmanTree(depths, num, &huffman_tree_size, huffman_tree[:], huffman_tree_extra_bits[:])
356
+
357
+ /* Calculate the statistics of the Huffman tree in brotli-representation. */
358
+ for i = 0; i < huffman_tree_size; i++ {
359
+ huffman_tree_histogram[huffman_tree[i]]++
360
+ }
361
+
362
+ for i = 0; i < codeLengthCodes; i++ {
363
+ if huffman_tree_histogram[i] != 0 {
364
+ if num_codes == 0 {
365
+ code = i
366
+ num_codes = 1
367
+ } else if num_codes == 1 {
368
+ num_codes = 2
369
+ break
370
+ }
371
+ }
372
+ }
373
+
374
+ /* Calculate another Huffman tree to use for compressing both the
375
+ earlier Huffman tree with. */
376
+ createHuffmanTree(huffman_tree_histogram[:], codeLengthCodes, 5, tree, code_length_bitdepth[:])
377
+
378
+ convertBitDepthsToSymbols(code_length_bitdepth[:], codeLengthCodes, code_length_bitdepth_symbols[:])
379
+
380
+ /* Now, we have all the data, let's start storing it */
381
+ storeHuffmanTreeOfHuffmanTreeToBitMask(num_codes, code_length_bitdepth[:], storage_ix, storage)
382
+
383
+ if num_codes == 1 {
384
+ code_length_bitdepth[code] = 0
385
+ }
386
+
387
+ /* Store the real Huffman tree now. */
388
+ storeHuffmanTreeToBitMask(huffman_tree_size, huffman_tree[:], huffman_tree_extra_bits[:], code_length_bitdepth[:], code_length_bitdepth_symbols[:], storage_ix, storage)
389
+ }
390
+
391
+ /*
392
+ Builds a Huffman tree from histogram[0:length] into depth[0:length] and
393
+
394
+ bits[0:length] and stores the encoded tree to the bit stream.
395
+ */
396
+ func buildAndStoreHuffmanTree(histogram []uint32, histogram_length uint, alphabet_size uint, tree []huffmanTree, depth []byte, bits []uint16, storage_ix *uint, storage []byte) {
397
+ var count uint = 0
398
+ var s4 = [4]uint{0}
399
+ var i uint
400
+ var max_bits uint = 0
401
+ for i = 0; i < histogram_length; i++ {
402
+ if histogram[i] != 0 {
403
+ if count < 4 {
404
+ s4[count] = i
405
+ } else if count > 4 {
406
+ break
407
+ }
408
+
409
+ count++
410
+ }
411
+ }
412
+ {
413
+ var max_bits_counter uint = alphabet_size - 1
414
+ for max_bits_counter != 0 {
415
+ max_bits_counter >>= 1
416
+ max_bits++
417
+ }
418
+ }
419
+
420
+ if count <= 1 {
421
+ writeBits(4, 1, storage_ix, storage)
422
+ writeBits(max_bits, uint64(s4[0]), storage_ix, storage)
423
+ depth[s4[0]] = 0
424
+ bits[s4[0]] = 0
425
+ return
426
+ }
427
+
428
+ for i := 0; i < int(histogram_length); i++ {
429
+ depth[i] = 0
430
+ }
431
+ createHuffmanTree(histogram, histogram_length, 15, tree, depth)
432
+ convertBitDepthsToSymbols(depth, histogram_length, bits)
433
+
434
+ if count <= 4 {
435
+ storeSimpleHuffmanTree(depth, s4[:], count, max_bits, storage_ix, storage)
436
+ } else {
437
+ storeHuffmanTree(depth, histogram_length, tree, storage_ix, storage)
438
+ }
439
+ }
440
+
441
+ func buildAndStoreHuffmanTreeFast(histogram []uint32, histogram_total uint, max_bits uint, depth []byte, bits []uint16, storage_ix *uint, storage []byte) {
442
+ var count uint = 0
443
+ var symbols = [4]uint{0}
444
+ var length uint = 0
445
+ var total uint = histogram_total
446
+ for total != 0 {
447
+ if histogram[length] != 0 {
448
+ if count < 4 {
449
+ symbols[count] = length
450
+ }
451
+
452
+ count++
453
+ total -= uint(histogram[length])
454
+ }
455
+
456
+ length++
457
+ }
458
+
459
+ if count <= 1 {
460
+ writeBits(4, 1, storage_ix, storage)
461
+ writeBits(max_bits, uint64(symbols[0]), storage_ix, storage)
462
+ depth[symbols[0]] = 0
463
+ bits[symbols[0]] = 0
464
+ return
465
+ }
466
+
467
+ chooseBitDepths(histogram[:length], depth[:length], 14)
468
+
469
+ convertBitDepthsToSymbols(depth, length, bits)
470
+ if count <= 4 {
471
+ var i uint
472
+
473
+ /* value of 1 indicates a simple Huffman code */
474
+ writeBits(2, 1, storage_ix, storage)
475
+
476
+ writeBits(2, uint64(count)-1, storage_ix, storage) /* NSYM - 1 */
477
+
478
+ /* Sort */
479
+ for i = 0; i < count; i++ {
480
+ var j uint
481
+ for j = i + 1; j < count; j++ {
482
+ if depth[symbols[j]] < depth[symbols[i]] {
483
+ var tmp uint = symbols[j]
484
+ symbols[j] = symbols[i]
485
+ symbols[i] = tmp
486
+ }
487
+ }
488
+ }
489
+
490
+ if count == 2 {
491
+ writeBits(max_bits, uint64(symbols[0]), storage_ix, storage)
492
+ writeBits(max_bits, uint64(symbols[1]), storage_ix, storage)
493
+ } else if count == 3 {
494
+ writeBits(max_bits, uint64(symbols[0]), storage_ix, storage)
495
+ writeBits(max_bits, uint64(symbols[1]), storage_ix, storage)
496
+ writeBits(max_bits, uint64(symbols[2]), storage_ix, storage)
497
+ } else {
498
+ writeBits(max_bits, uint64(symbols[0]), storage_ix, storage)
499
+ writeBits(max_bits, uint64(symbols[1]), storage_ix, storage)
500
+ writeBits(max_bits, uint64(symbols[2]), storage_ix, storage)
501
+ writeBits(max_bits, uint64(symbols[3]), storage_ix, storage)
502
+
503
+ /* tree-select */
504
+ var tmp int
505
+ if depth[symbols[0]] == 1 {
506
+ tmp = 1
507
+ } else {
508
+ tmp = 0
509
+ }
510
+ writeBits(1, uint64(tmp), storage_ix, storage)
511
+ }
512
+ } else {
513
+ var previous_value byte = 8
514
+ var i uint
515
+
516
+ /* Complex Huffman Tree */
517
+ storeStaticCodeLengthCode(storage_ix, storage)
518
+
519
+ /* Actual RLE coding. */
520
+ for i = 0; i < length; {
521
+ var value byte = depth[i]
522
+ var reps uint = 1
523
+ var k uint
524
+ for k = i + 1; k < length && depth[k] == value; k++ {
525
+ reps++
526
+ }
527
+
528
+ i += reps
529
+ if value == 0 {
530
+ writeBits(uint(kZeroRepsDepth[reps]), kZeroRepsBits[reps], storage_ix, storage)
531
+ } else {
532
+ if previous_value != value {
533
+ writeBits(uint(kCodeLengthDepth[value]), uint64(kCodeLengthBits[value]), storage_ix, storage)
534
+ reps--
535
+ }
536
+
537
+ if reps < 3 {
538
+ for reps != 0 {
539
+ reps--
540
+ writeBits(uint(kCodeLengthDepth[value]), uint64(kCodeLengthBits[value]), storage_ix, storage)
541
+ }
542
+ } else {
543
+ reps -= 3
544
+ writeBits(uint(kNonZeroRepsDepth[reps]), kNonZeroRepsBits[reps], storage_ix, storage)
545
+ }
546
+
547
+ previous_value = value
548
+ }
549
+ }
550
+ }
551
+ }
552
+
553
+ type symbolAndCount struct {
554
+ symbol uint32
555
+ count uint32
556
+ }
557
+
558
+ func chooseBitDepths(histogram []uint32, depth []byte, maxBits int) {
559
+ totalCodeSpace := 1 << maxBits
560
+ symbols := make([]symbolAndCount, 0, 704 /* static capacity so that it will be stack allocated */)
561
+ var totalCount uint32 = 0
562
+ for i, n := range histogram {
563
+ if n != 0 {
564
+ symbols = append(symbols, symbolAndCount{
565
+ symbol: uint32(i),
566
+ count: n,
567
+ })
568
+ totalCount += n
569
+ }
570
+ }
571
+ slices.SortFunc(symbols, func(a, b symbolAndCount) int {
572
+ return int(b.count) - int(a.count)
573
+ })
574
+
575
+ // boundaries contains indexes into symbols such that (for example)
576
+ // boundaries[8] is the index of the first 8-bit symbol.
577
+ boundaries := make([]int, maxBits+1, 20 /* static capacity for stack allocation */)
578
+
579
+ codeSpaceUsed := 0
580
+
581
+ // Assign initial boundaries conservatively, making sure no symbol uses more
582
+ // than its share of code space.
583
+ totalRatio := float64(totalCount) / float64(totalCodeSpace)
584
+ currentDepth := 1
585
+ for i, symbol := range symbols {
586
+ for currentDepth < maxBits && float64(symbol.count)/float64(int(1)<<(maxBits-currentDepth)) < totalRatio {
587
+ currentDepth++
588
+ boundaries[currentDepth] = i
589
+ }
590
+ codeSpaceUsed += 1 << (maxBits - currentDepth)
591
+ }
592
+ for i := currentDepth + 1; i <= maxBits; i++ {
593
+ boundaries[i] = len(symbols)
594
+ }
595
+
596
+ // Move the boundaries till the code space is filled.
597
+ for codeSpaceUsed < totalCodeSpace {
598
+ available := totalCodeSpace - codeSpaceUsed
599
+ // Find the most efficient boundary to move, based on the ratio of how
600
+ // many times the symbol was used to how much code space will be consumed.
601
+ bestRatio := 0.0
602
+ bestBoundary := 0
603
+ for i := 2; i <= maxBits; i++ {
604
+ cost := 1 << (maxBits - i) // code space that would be used by moving this boundary
605
+ if cost > available {
606
+ continue
607
+ }
608
+ if boundaries[i] == len(symbols) {
609
+ continue
610
+ }
611
+ if i < maxBits && boundaries[i] == boundaries[i+1] {
612
+ continue
613
+ }
614
+ ratio := float64(symbols[boundaries[i]].count) / float64(cost)
615
+ if ratio > bestRatio {
616
+ bestRatio = ratio
617
+ bestBoundary = i
618
+ }
619
+ }
620
+ boundaries[bestBoundary]++
621
+ codeSpaceUsed += 1 << (maxBits - bestBoundary)
622
+ }
623
+
624
+ for i := range depth {
625
+ depth[i] = 0
626
+ }
627
+ for i := 1; i < maxBits; i++ {
628
+ for j := boundaries[i]; j < boundaries[i+1]; j++ {
629
+ depth[symbols[j].symbol] = byte(i)
630
+ }
631
+ }
632
+ for j := boundaries[maxBits]; j < len(symbols); j++ {
633
+ depth[symbols[j].symbol] = byte(maxBits)
634
+ }
635
+ }
636
+
637
+ func buildAndStoreHuffmanTreeFastBW(histogram []uint32, histogram_total uint, max_bits uint, depth []byte, bits []uint16, bw *bitWriter) {
638
+ var count uint = 0
639
+ var symbols = [4]uint{0}
640
+ var length uint = 0
641
+ var total uint = histogram_total
642
+ for total != 0 {
643
+ if histogram[length] != 0 {
644
+ if count < 4 {
645
+ symbols[count] = length
646
+ }
647
+
648
+ count++
649
+ total -= uint(histogram[length])
650
+ }
651
+
652
+ length++
653
+ }
654
+
655
+ if count <= 1 {
656
+ bw.writeBits(4, 1)
657
+ bw.writeBits(max_bits, uint64(symbols[0]))
658
+ depth[symbols[0]] = 0
659
+ bits[symbols[0]] = 0
660
+ return
661
+ }
662
+
663
+ chooseBitDepths(histogram[:length], depth[:length], 14)
664
+
665
+ convertBitDepthsToSymbols(depth, length, bits)
666
+ if count <= 4 {
667
+ var i uint
668
+
669
+ /* value of 1 indicates a simple Huffman code */
670
+ bw.writeBits(2, 1)
671
+
672
+ bw.writeBits(2, uint64(count)-1) /* NSYM - 1 */
673
+
674
+ /* Sort */
675
+ for i = 0; i < count; i++ {
676
+ var j uint
677
+ for j = i + 1; j < count; j++ {
678
+ if depth[symbols[j]] < depth[symbols[i]] {
679
+ var tmp uint = symbols[j]
680
+ symbols[j] = symbols[i]
681
+ symbols[i] = tmp
682
+ }
683
+ }
684
+ }
685
+
686
+ if count == 2 {
687
+ bw.writeBits(max_bits, uint64(symbols[0]))
688
+ bw.writeBits(max_bits, uint64(symbols[1]))
689
+ } else if count == 3 {
690
+ bw.writeBits(max_bits, uint64(symbols[0]))
691
+ bw.writeBits(max_bits, uint64(symbols[1]))
692
+ bw.writeBits(max_bits, uint64(symbols[2]))
693
+ } else {
694
+ bw.writeBits(max_bits, uint64(symbols[0]))
695
+ bw.writeBits(max_bits, uint64(symbols[1]))
696
+ bw.writeBits(max_bits, uint64(symbols[2]))
697
+ bw.writeBits(max_bits, uint64(symbols[3]))
698
+
699
+ /* tree-select */
700
+ bw.writeSingleBit(depth[symbols[0]] == 1)
701
+ }
702
+ } else {
703
+ var previous_value byte = 8
704
+ var i uint
705
+
706
+ /* Complex Huffman Tree */
707
+ storeStaticCodeLengthCodeBW(bw)
708
+
709
+ /* Actual RLE coding. */
710
+ for i = 0; i < length; {
711
+ var value byte = depth[i]
712
+ var reps uint = 1
713
+ var k uint
714
+ for k = i + 1; k < length && depth[k] == value; k++ {
715
+ reps++
716
+ }
717
+
718
+ i += reps
719
+ if value == 0 {
720
+ bw.writeBits(uint(kZeroRepsDepth[reps]), kZeroRepsBits[reps])
721
+ } else {
722
+ if previous_value != value {
723
+ bw.writeBits(uint(kCodeLengthDepth[value]), uint64(kCodeLengthBits[value]))
724
+ reps--
725
+ }
726
+
727
+ if reps < 3 {
728
+ for reps != 0 {
729
+ reps--
730
+ bw.writeBits(uint(kCodeLengthDepth[value]), uint64(kCodeLengthBits[value]))
731
+ }
732
+ } else {
733
+ reps -= 3
734
+ bw.writeBits(uint(kNonZeroRepsDepth[reps]), kNonZeroRepsBits[reps])
735
+ }
736
+
737
+ previous_value = value
738
+ }
739
+ }
740
+ }
741
+ }
742
+
743
+ func indexOf(v []byte, v_size uint, value byte) uint {
744
+ var i uint = 0
745
+ for ; i < v_size; i++ {
746
+ if v[i] == value {
747
+ return i
748
+ }
749
+ }
750
+
751
+ return i
752
+ }
753
+
754
+ func moveToFront(v []byte, index uint) {
755
+ var value byte = v[index]
756
+ var i uint
757
+ for i = index; i != 0; i-- {
758
+ v[i] = v[i-1]
759
+ }
760
+
761
+ v[0] = value
762
+ }
763
+
764
+ func moveToFrontTransform(v_in []uint32, v_size uint, v_out []uint32) {
765
+ var i uint
766
+ var mtf [256]byte
767
+ var max_value uint32
768
+ if v_size == 0 {
769
+ return
770
+ }
771
+
772
+ max_value = v_in[0]
773
+ for i = 1; i < v_size; i++ {
774
+ if v_in[i] > max_value {
775
+ max_value = v_in[i]
776
+ }
777
+ }
778
+
779
+ assert(max_value < 256)
780
+ for i = 0; uint32(i) <= max_value; i++ {
781
+ mtf[i] = byte(i)
782
+ }
783
+ {
784
+ var mtf_size uint = uint(max_value + 1)
785
+ for i = 0; i < v_size; i++ {
786
+ var index uint = indexOf(mtf[:], mtf_size, byte(v_in[i]))
787
+ assert(index < mtf_size)
788
+ v_out[i] = uint32(index)
789
+ moveToFront(mtf[:], index)
790
+ }
791
+ }
792
+ }
793
+
794
+ /*
795
+ Finds runs of zeros in v[0..in_size) and replaces them with a prefix code of
796
+
797
+ the run length plus extra bits (lower 9 bits is the prefix code and the rest
798
+ are the extra bits). Non-zero values in v[] are shifted by
799
+ *max_length_prefix. Will not create prefix codes bigger than the initial
800
+ value of *max_run_length_prefix. The prefix code of run length L is simply
801
+ Log2Floor(L) and the number of extra bits is the same as the prefix code.
802
+ */
803
+ func runLengthCodeZeros(in_size uint, v []uint32, out_size *uint, max_run_length_prefix *uint32) {
804
+ var max_reps uint32 = 0
805
+ var i uint
806
+ var max_prefix uint32
807
+ for i = 0; i < in_size; {
808
+ var reps uint32 = 0
809
+ for ; i < in_size && v[i] != 0; i++ {
810
+ }
811
+ for ; i < in_size && v[i] == 0; i++ {
812
+ reps++
813
+ }
814
+
815
+ max_reps = brotli_max_uint32_t(reps, max_reps)
816
+ }
817
+
818
+ if max_reps > 0 {
819
+ max_prefix = log2FloorNonZero(uint(max_reps))
820
+ } else {
821
+ max_prefix = 0
822
+ }
823
+ max_prefix = brotli_min_uint32_t(max_prefix, *max_run_length_prefix)
824
+ *max_run_length_prefix = max_prefix
825
+ *out_size = 0
826
+ for i = 0; i < in_size; {
827
+ assert(*out_size <= i)
828
+ if v[i] != 0 {
829
+ v[*out_size] = v[i] + *max_run_length_prefix
830
+ i++
831
+ (*out_size)++
832
+ } else {
833
+ var reps uint32 = 1
834
+ var k uint
835
+ for k = i + 1; k < in_size && v[k] == 0; k++ {
836
+ reps++
837
+ }
838
+
839
+ i += uint(reps)
840
+ for reps != 0 {
841
+ if reps < 2<<max_prefix {
842
+ var run_length_prefix uint32 = log2FloorNonZero(uint(reps))
843
+ var extra_bits uint32 = reps - (1 << run_length_prefix)
844
+ v[*out_size] = run_length_prefix + (extra_bits << 9)
845
+ (*out_size)++
846
+ break
847
+ } else {
848
+ var extra_bits uint32 = (1 << max_prefix) - 1
849
+ v[*out_size] = max_prefix + (extra_bits << 9)
850
+ reps -= (2 << max_prefix) - 1
851
+ (*out_size)++
852
+ }
853
+ }
854
+ }
855
+ }
856
+ }
857
+
858
+ const symbolBits = 9
859
+
860
+ var encodeContextMap_kSymbolMask uint32 = (1 << symbolBits) - 1
861
+
862
+ func encodeContextMap(context_map []uint32, context_map_size uint, num_clusters uint, tree []huffmanTree, storage_ix *uint, storage []byte) {
863
+ var i uint
864
+ var rle_symbols []uint32
865
+ var max_run_length_prefix uint32 = 6
866
+ var num_rle_symbols uint = 0
867
+ var histogram [maxContextMapSymbols]uint32
868
+ var depths [maxContextMapSymbols]byte
869
+ var bits [maxContextMapSymbols]uint16
870
+
871
+ storeVarLenUint8(num_clusters-1, storage_ix, storage)
872
+
873
+ if num_clusters == 1 {
874
+ return
875
+ }
876
+
877
+ rle_symbols = make([]uint32, context_map_size)
878
+ moveToFrontTransform(context_map, context_map_size, rle_symbols)
879
+ runLengthCodeZeros(context_map_size, rle_symbols, &num_rle_symbols, &max_run_length_prefix)
880
+ histogram = [maxContextMapSymbols]uint32{}
881
+ for i = 0; i < num_rle_symbols; i++ {
882
+ histogram[rle_symbols[i]&encodeContextMap_kSymbolMask]++
883
+ }
884
+ {
885
+ var use_rle bool = (max_run_length_prefix > 0)
886
+ writeSingleBit(use_rle, storage_ix, storage)
887
+ if use_rle {
888
+ writeBits(4, uint64(max_run_length_prefix)-1, storage_ix, storage)
889
+ }
890
+ }
891
+
892
+ buildAndStoreHuffmanTree(histogram[:], uint(uint32(num_clusters)+max_run_length_prefix), uint(uint32(num_clusters)+max_run_length_prefix), tree, depths[:], bits[:], storage_ix, storage)
893
+ for i = 0; i < num_rle_symbols; i++ {
894
+ var rle_symbol uint32 = rle_symbols[i] & encodeContextMap_kSymbolMask
895
+ var extra_bits_val uint32 = rle_symbols[i] >> symbolBits
896
+ writeBits(uint(depths[rle_symbol]), uint64(bits[rle_symbol]), storage_ix, storage)
897
+ if rle_symbol > 0 && rle_symbol <= max_run_length_prefix {
898
+ writeBits(uint(rle_symbol), uint64(extra_bits_val), storage_ix, storage)
899
+ }
900
+ }
901
+
902
+ writeBits(1, 1, storage_ix, storage) /* use move-to-front */
903
+ rle_symbols = nil
904
+ }
905
+
906
+ /* Stores the block switch command with index block_ix to the bit stream. */
907
+ func storeBlockSwitch(code *blockSplitCode, block_len uint32, block_type byte, is_first_block bool, storage_ix *uint, storage []byte) {
908
+ var typecode uint = nextBlockTypeCode(&code.type_code_calculator, block_type)
909
+ var lencode uint
910
+ var len_nextra uint32
911
+ var len_extra uint32
912
+ if !is_first_block {
913
+ writeBits(uint(code.type_depths[typecode]), uint64(code.type_bits[typecode]), storage_ix, storage)
914
+ }
915
+
916
+ getBlockLengthPrefixCode(block_len, &lencode, &len_nextra, &len_extra)
917
+
918
+ writeBits(uint(code.length_depths[lencode]), uint64(code.length_bits[lencode]), storage_ix, storage)
919
+ writeBits(uint(len_nextra), uint64(len_extra), storage_ix, storage)
920
+ }
921
+
922
+ /*
923
+ Builds a BlockSplitCode data structure from the block split given by the
924
+
925
+ vector of block types and block lengths and stores it to the bit stream.
926
+ */
927
+ func buildAndStoreBlockSplitCode(types []byte, lengths []uint32, num_blocks uint, num_types uint, tree []huffmanTree, code *blockSplitCode, storage_ix *uint, storage []byte) {
928
+ var type_histo [maxBlockTypeSymbols]uint32
929
+ var length_histo [numBlockLenSymbols]uint32
930
+ var i uint
931
+ var type_code_calculator blockTypeCodeCalculator
932
+ for i := 0; i < int(num_types+2); i++ {
933
+ type_histo[i] = 0
934
+ }
935
+ length_histo = [numBlockLenSymbols]uint32{}
936
+ initBlockTypeCodeCalculator(&type_code_calculator)
937
+ for i = 0; i < num_blocks; i++ {
938
+ var type_code uint = nextBlockTypeCode(&type_code_calculator, types[i])
939
+ if i != 0 {
940
+ type_histo[type_code]++
941
+ }
942
+ length_histo[blockLengthPrefixCode(lengths[i])]++
943
+ }
944
+
945
+ storeVarLenUint8(num_types-1, storage_ix, storage)
946
+ if num_types > 1 { /* TODO: else? could StoreBlockSwitch occur? */
947
+ buildAndStoreHuffmanTree(type_histo[0:], num_types+2, num_types+2, tree, code.type_depths[0:], code.type_bits[0:], storage_ix, storage)
948
+ buildAndStoreHuffmanTree(length_histo[0:], numBlockLenSymbols, numBlockLenSymbols, tree, code.length_depths[0:], code.length_bits[0:], storage_ix, storage)
949
+ storeBlockSwitch(code, lengths[0], types[0], true, storage_ix, storage)
950
+ }
951
+ }
952
+
953
+ /* Stores a context map where the histogram type is always the block type. */
954
+ func storeTrivialContextMap(num_types uint, context_bits uint, tree []huffmanTree, storage_ix *uint, storage []byte) {
955
+ storeVarLenUint8(num_types-1, storage_ix, storage)
956
+ if num_types > 1 {
957
+ var repeat_code uint = context_bits - 1
958
+ var repeat_bits uint = (1 << repeat_code) - 1
959
+ var alphabet_size uint = num_types + repeat_code
960
+ var histogram [maxContextMapSymbols]uint32
961
+ var depths [maxContextMapSymbols]byte
962
+ var bits [maxContextMapSymbols]uint16
963
+ var i uint
964
+ for i := 0; i < int(alphabet_size); i++ {
965
+ histogram[i] = 0
966
+ }
967
+
968
+ /* Write RLEMAX. */
969
+ writeBits(1, 1, storage_ix, storage)
970
+
971
+ writeBits(4, uint64(repeat_code)-1, storage_ix, storage)
972
+ histogram[repeat_code] = uint32(num_types)
973
+ histogram[0] = 1
974
+ for i = context_bits; i < alphabet_size; i++ {
975
+ histogram[i] = 1
976
+ }
977
+
978
+ buildAndStoreHuffmanTree(histogram[:], alphabet_size, alphabet_size, tree, depths[:], bits[:], storage_ix, storage)
979
+ for i = 0; i < num_types; i++ {
980
+ var tmp uint
981
+ if i == 0 {
982
+ tmp = 0
983
+ } else {
984
+ tmp = i + context_bits - 1
985
+ }
986
+ var code uint = tmp
987
+ writeBits(uint(depths[code]), uint64(bits[code]), storage_ix, storage)
988
+ writeBits(uint(depths[repeat_code]), uint64(bits[repeat_code]), storage_ix, storage)
989
+ writeBits(repeat_code, uint64(repeat_bits), storage_ix, storage)
990
+ }
991
+
992
+ /* Write IMTF (inverse-move-to-front) bit. */
993
+ writeBits(1, 1, storage_ix, storage)
994
+ }
995
+ }
996
+
997
+ /* Manages the encoding of one block category (literal, command or distance). */
998
+ type blockEncoder struct {
999
+ histogram_length_ uint
1000
+ num_block_types_ uint
1001
+ block_types_ []byte
1002
+ block_lengths_ []uint32
1003
+ num_blocks_ uint
1004
+ block_split_code_ blockSplitCode
1005
+ block_ix_ uint
1006
+ block_len_ uint
1007
+ entropy_ix_ uint
1008
+ depths_ []byte
1009
+ bits_ []uint16
1010
+ }
1011
+
1012
+ var blockEncoderPool sync.Pool
1013
+
1014
+ func getBlockEncoder(histogram_length uint, num_block_types uint, block_types []byte, block_lengths []uint32, num_blocks uint) *blockEncoder {
1015
+ self, _ := blockEncoderPool.Get().(*blockEncoder)
1016
+
1017
+ if self != nil {
1018
+ self.block_ix_ = 0
1019
+ self.entropy_ix_ = 0
1020
+ self.depths_ = self.depths_[:0]
1021
+ self.bits_ = self.bits_[:0]
1022
+ } else {
1023
+ self = &blockEncoder{}
1024
+ }
1025
+
1026
+ self.histogram_length_ = histogram_length
1027
+ self.num_block_types_ = num_block_types
1028
+ self.block_types_ = block_types
1029
+ self.block_lengths_ = block_lengths
1030
+ self.num_blocks_ = num_blocks
1031
+ initBlockTypeCodeCalculator(&self.block_split_code_.type_code_calculator)
1032
+ if num_blocks == 0 {
1033
+ self.block_len_ = 0
1034
+ } else {
1035
+ self.block_len_ = uint(block_lengths[0])
1036
+ }
1037
+
1038
+ return self
1039
+ }
1040
+
1041
+ func cleanupBlockEncoder(self *blockEncoder) {
1042
+ blockEncoderPool.Put(self)
1043
+ }
1044
+
1045
+ /*
1046
+ Creates entropy codes of block lengths and block types and stores them
1047
+
1048
+ to the bit stream.
1049
+ */
1050
+ func buildAndStoreBlockSwitchEntropyCodes(self *blockEncoder, tree []huffmanTree, storage_ix *uint, storage []byte) {
1051
+ buildAndStoreBlockSplitCode(self.block_types_, self.block_lengths_, self.num_blocks_, self.num_block_types_, tree, &self.block_split_code_, storage_ix, storage)
1052
+ }
1053
+
1054
+ /*
1055
+ Stores the next symbol with the entropy code of the current block type.
1056
+
1057
+ Updates the block type and block length at block boundaries.
1058
+ */
1059
+ func storeSymbol(self *blockEncoder, symbol uint, storage_ix *uint, storage []byte) {
1060
+ if self.block_len_ == 0 {
1061
+ self.block_ix_++
1062
+ var block_ix uint = self.block_ix_
1063
+ var block_len uint32 = self.block_lengths_[block_ix]
1064
+ var block_type byte = self.block_types_[block_ix]
1065
+ self.block_len_ = uint(block_len)
1066
+ self.entropy_ix_ = uint(block_type) * self.histogram_length_
1067
+ storeBlockSwitch(&self.block_split_code_, block_len, block_type, false, storage_ix, storage)
1068
+ }
1069
+
1070
+ self.block_len_--
1071
+ {
1072
+ var ix uint = self.entropy_ix_ + symbol
1073
+ writeBits(uint(self.depths_[ix]), uint64(self.bits_[ix]), storage_ix, storage)
1074
+ }
1075
+ }
1076
+
1077
+ /*
1078
+ Stores the next symbol with the entropy code of the current block type and
1079
+
1080
+ context value.
1081
+ Updates the block type and block length at block boundaries.
1082
+ */
1083
+ func storeSymbolWithContext(self *blockEncoder, symbol uint, context uint, context_map []uint32, storage_ix *uint, storage []byte, context_bits uint) {
1084
+ if self.block_len_ == 0 {
1085
+ self.block_ix_++
1086
+ var block_ix uint = self.block_ix_
1087
+ var block_len uint32 = self.block_lengths_[block_ix]
1088
+ var block_type byte = self.block_types_[block_ix]
1089
+ self.block_len_ = uint(block_len)
1090
+ self.entropy_ix_ = uint(block_type) << context_bits
1091
+ storeBlockSwitch(&self.block_split_code_, block_len, block_type, false, storage_ix, storage)
1092
+ }
1093
+
1094
+ self.block_len_--
1095
+ {
1096
+ var histo_ix uint = uint(context_map[self.entropy_ix_+context])
1097
+ var ix uint = histo_ix*self.histogram_length_ + symbol
1098
+ writeBits(uint(self.depths_[ix]), uint64(self.bits_[ix]), storage_ix, storage)
1099
+ }
1100
+ }
1101
+
1102
+ func buildAndStoreEntropyCodesLiteral(self *blockEncoder, histograms []histogramLiteral, histograms_size uint, alphabet_size uint, tree []huffmanTree, storage_ix *uint, storage []byte) {
1103
+ var table_size uint = histograms_size * self.histogram_length_
1104
+ if cap(self.depths_) < int(table_size) {
1105
+ self.depths_ = make([]byte, table_size)
1106
+ } else {
1107
+ self.depths_ = self.depths_[:table_size]
1108
+ }
1109
+ if cap(self.bits_) < int(table_size) {
1110
+ self.bits_ = make([]uint16, table_size)
1111
+ } else {
1112
+ self.bits_ = self.bits_[:table_size]
1113
+ }
1114
+ {
1115
+ var i uint
1116
+ for i = 0; i < histograms_size; i++ {
1117
+ var ix uint = i * self.histogram_length_
1118
+ buildAndStoreHuffmanTree(histograms[i].data_[0:], self.histogram_length_, alphabet_size, tree, self.depths_[ix:], self.bits_[ix:], storage_ix, storage)
1119
+ }
1120
+ }
1121
+ }
1122
+
1123
+ func buildAndStoreEntropyCodesCommand(self *blockEncoder, histograms []histogramCommand, histograms_size uint, alphabet_size uint, tree []huffmanTree, storage_ix *uint, storage []byte) {
1124
+ var table_size uint = histograms_size * self.histogram_length_
1125
+ if cap(self.depths_) < int(table_size) {
1126
+ self.depths_ = make([]byte, table_size)
1127
+ } else {
1128
+ self.depths_ = self.depths_[:table_size]
1129
+ }
1130
+ if cap(self.bits_) < int(table_size) {
1131
+ self.bits_ = make([]uint16, table_size)
1132
+ } else {
1133
+ self.bits_ = self.bits_[:table_size]
1134
+ }
1135
+ {
1136
+ var i uint
1137
+ for i = 0; i < histograms_size; i++ {
1138
+ var ix uint = i * self.histogram_length_
1139
+ buildAndStoreHuffmanTree(histograms[i].data_[0:], self.histogram_length_, alphabet_size, tree, self.depths_[ix:], self.bits_[ix:], storage_ix, storage)
1140
+ }
1141
+ }
1142
+ }
1143
+
1144
+ func buildAndStoreEntropyCodesDistance(self *blockEncoder, histograms []histogramDistance, histograms_size uint, alphabet_size uint, tree []huffmanTree, storage_ix *uint, storage []byte) {
1145
+ var table_size uint = histograms_size * self.histogram_length_
1146
+ if cap(self.depths_) < int(table_size) {
1147
+ self.depths_ = make([]byte, table_size)
1148
+ } else {
1149
+ self.depths_ = self.depths_[:table_size]
1150
+ }
1151
+ if cap(self.bits_) < int(table_size) {
1152
+ self.bits_ = make([]uint16, table_size)
1153
+ } else {
1154
+ self.bits_ = self.bits_[:table_size]
1155
+ }
1156
+ {
1157
+ var i uint
1158
+ for i = 0; i < histograms_size; i++ {
1159
+ var ix uint = i * self.histogram_length_
1160
+ buildAndStoreHuffmanTree(histograms[i].data_[0:], self.histogram_length_, alphabet_size, tree, self.depths_[ix:], self.bits_[ix:], storage_ix, storage)
1161
+ }
1162
+ }
1163
+ }
1164
+
1165
+ func jumpToByteBoundary(storage_ix *uint, storage []byte) {
1166
+ *storage_ix = (*storage_ix + 7) &^ 7
1167
+ storage[*storage_ix>>3] = 0
1168
+ }
1169
+
1170
+ func storeMetaBlock(input []byte, start_pos uint, length uint, mask uint, prev_byte byte, prev_byte2 byte, is_last bool, params *encoderParams, literal_context_mode int, commands []command, mb *metaBlockSplit, storage_ix *uint, storage []byte) {
1171
+ var pos uint = start_pos
1172
+ var i uint
1173
+ var num_distance_symbols uint32 = params.dist.alphabet_size
1174
+ var num_effective_distance_symbols uint32 = num_distance_symbols
1175
+ var tree []huffmanTree
1176
+ var literal_context_lut contextLUT = getContextLUT(literal_context_mode)
1177
+ var dist *distanceParams = &params.dist
1178
+ if params.large_window && num_effective_distance_symbols > numHistogramDistanceSymbols {
1179
+ num_effective_distance_symbols = numHistogramDistanceSymbols
1180
+ }
1181
+
1182
+ storeCompressedMetaBlockHeader(is_last, length, storage_ix, storage)
1183
+
1184
+ tree = make([]huffmanTree, maxHuffmanTreeSize)
1185
+ literal_enc := getBlockEncoder(numLiteralSymbols, mb.literal_split.num_types, mb.literal_split.types, mb.literal_split.lengths, mb.literal_split.num_blocks)
1186
+ command_enc := getBlockEncoder(numCommandSymbols, mb.command_split.num_types, mb.command_split.types, mb.command_split.lengths, mb.command_split.num_blocks)
1187
+ distance_enc := getBlockEncoder(uint(num_effective_distance_symbols), mb.distance_split.num_types, mb.distance_split.types, mb.distance_split.lengths, mb.distance_split.num_blocks)
1188
+
1189
+ buildAndStoreBlockSwitchEntropyCodes(literal_enc, tree, storage_ix, storage)
1190
+ buildAndStoreBlockSwitchEntropyCodes(command_enc, tree, storage_ix, storage)
1191
+ buildAndStoreBlockSwitchEntropyCodes(distance_enc, tree, storage_ix, storage)
1192
+
1193
+ writeBits(2, uint64(dist.distance_postfix_bits), storage_ix, storage)
1194
+ writeBits(4, uint64(dist.num_direct_distance_codes)>>dist.distance_postfix_bits, storage_ix, storage)
1195
+ for i = 0; i < mb.literal_split.num_types; i++ {
1196
+ writeBits(2, uint64(literal_context_mode), storage_ix, storage)
1197
+ }
1198
+
1199
+ if mb.literal_context_map_size == 0 {
1200
+ storeTrivialContextMap(mb.literal_histograms_size, literalContextBits, tree, storage_ix, storage)
1201
+ } else {
1202
+ encodeContextMap(mb.literal_context_map, mb.literal_context_map_size, mb.literal_histograms_size, tree, storage_ix, storage)
1203
+ }
1204
+
1205
+ if mb.distance_context_map_size == 0 {
1206
+ storeTrivialContextMap(mb.distance_histograms_size, distanceContextBits, tree, storage_ix, storage)
1207
+ } else {
1208
+ encodeContextMap(mb.distance_context_map, mb.distance_context_map_size, mb.distance_histograms_size, tree, storage_ix, storage)
1209
+ }
1210
+
1211
+ buildAndStoreEntropyCodesLiteral(literal_enc, mb.literal_histograms, mb.literal_histograms_size, numLiteralSymbols, tree, storage_ix, storage)
1212
+ buildAndStoreEntropyCodesCommand(command_enc, mb.command_histograms, mb.command_histograms_size, numCommandSymbols, tree, storage_ix, storage)
1213
+ buildAndStoreEntropyCodesDistance(distance_enc, mb.distance_histograms, mb.distance_histograms_size, uint(num_distance_symbols), tree, storage_ix, storage)
1214
+ tree = nil
1215
+
1216
+ for _, cmd := range commands {
1217
+ var cmd_code uint = uint(cmd.cmd_prefix_)
1218
+ storeSymbol(command_enc, cmd_code, storage_ix, storage)
1219
+ storeCommandExtra(&cmd, storage_ix, storage)
1220
+ if mb.literal_context_map_size == 0 {
1221
+ var j uint
1222
+ for j = uint(cmd.insert_len_); j != 0; j-- {
1223
+ storeSymbol(literal_enc, uint(input[pos&mask]), storage_ix, storage)
1224
+ pos++
1225
+ }
1226
+ } else {
1227
+ var j uint
1228
+ for j = uint(cmd.insert_len_); j != 0; j-- {
1229
+ var context uint = uint(getContext(prev_byte, prev_byte2, literal_context_lut))
1230
+ var literal byte = input[pos&mask]
1231
+ storeSymbolWithContext(literal_enc, uint(literal), context, mb.literal_context_map, storage_ix, storage, literalContextBits)
1232
+ prev_byte2 = prev_byte
1233
+ prev_byte = literal
1234
+ pos++
1235
+ }
1236
+ }
1237
+
1238
+ pos += uint(commandCopyLen(&cmd))
1239
+ if commandCopyLen(&cmd) != 0 {
1240
+ prev_byte2 = input[(pos-2)&mask]
1241
+ prev_byte = input[(pos-1)&mask]
1242
+ if cmd.cmd_prefix_ >= 128 {
1243
+ var dist_code uint = uint(cmd.dist_prefix_) & 0x3FF
1244
+ var distnumextra uint32 = uint32(cmd.dist_prefix_) >> 10
1245
+ var distextra uint64 = uint64(cmd.dist_extra_)
1246
+ if mb.distance_context_map_size == 0 {
1247
+ storeSymbol(distance_enc, dist_code, storage_ix, storage)
1248
+ } else {
1249
+ var context uint = uint(commandDistanceContext(&cmd))
1250
+ storeSymbolWithContext(distance_enc, dist_code, context, mb.distance_context_map, storage_ix, storage, distanceContextBits)
1251
+ }
1252
+
1253
+ writeBits(uint(distnumextra), distextra, storage_ix, storage)
1254
+ }
1255
+ }
1256
+ }
1257
+
1258
+ cleanupBlockEncoder(distance_enc)
1259
+ cleanupBlockEncoder(command_enc)
1260
+ cleanupBlockEncoder(literal_enc)
1261
+ if is_last {
1262
+ jumpToByteBoundary(storage_ix, storage)
1263
+ }
1264
+ }
1265
+
1266
+ func buildHistograms(input []byte, start_pos uint, mask uint, commands []command, lit_histo *histogramLiteral, cmd_histo *histogramCommand, dist_histo *histogramDistance) {
1267
+ var pos uint = start_pos
1268
+ for _, cmd := range commands {
1269
+ var j uint
1270
+ histogramAddCommand(cmd_histo, uint(cmd.cmd_prefix_))
1271
+ for j = uint(cmd.insert_len_); j != 0; j-- {
1272
+ histogramAddLiteral(lit_histo, uint(input[pos&mask]))
1273
+ pos++
1274
+ }
1275
+
1276
+ pos += uint(commandCopyLen(&cmd))
1277
+ if commandCopyLen(&cmd) != 0 && cmd.cmd_prefix_ >= 128 {
1278
+ histogramAddDistance(dist_histo, uint(cmd.dist_prefix_)&0x3FF)
1279
+ }
1280
+ }
1281
+ }
1282
+
1283
+ func storeDataWithHuffmanCodes(input []byte, start_pos uint, mask uint, commands []command, lit_depth []byte, lit_bits []uint16, cmd_depth []byte, cmd_bits []uint16, dist_depth []byte, dist_bits []uint16, storage_ix *uint, storage []byte) {
1284
+ var pos uint = start_pos
1285
+ for _, cmd := range commands {
1286
+ var cmd_code uint = uint(cmd.cmd_prefix_)
1287
+ var j uint
1288
+ writeBits(uint(cmd_depth[cmd_code]), uint64(cmd_bits[cmd_code]), storage_ix, storage)
1289
+ storeCommandExtra(&cmd, storage_ix, storage)
1290
+ for j = uint(cmd.insert_len_); j != 0; j-- {
1291
+ var literal byte = input[pos&mask]
1292
+ writeBits(uint(lit_depth[literal]), uint64(lit_bits[literal]), storage_ix, storage)
1293
+ pos++
1294
+ }
1295
+
1296
+ pos += uint(commandCopyLen(&cmd))
1297
+ if commandCopyLen(&cmd) != 0 && cmd.cmd_prefix_ >= 128 {
1298
+ var dist_code uint = uint(cmd.dist_prefix_) & 0x3FF
1299
+ var distnumextra uint32 = uint32(cmd.dist_prefix_) >> 10
1300
+ var distextra uint32 = cmd.dist_extra_
1301
+ writeBits(uint(dist_depth[dist_code]), uint64(dist_bits[dist_code]), storage_ix, storage)
1302
+ writeBits(uint(distnumextra), uint64(distextra), storage_ix, storage)
1303
+ }
1304
+ }
1305
+ }
1306
+
1307
+ func storeMetaBlockTrivial(input []byte, start_pos uint, length uint, mask uint, is_last bool, params *encoderParams, commands []command, storage_ix *uint, storage []byte) {
1308
+ var lit_histo histogramLiteral
1309
+ var cmd_histo histogramCommand
1310
+ var dist_histo histogramDistance
1311
+ var lit_depth [numLiteralSymbols]byte
1312
+ var lit_bits [numLiteralSymbols]uint16
1313
+ var cmd_depth [numCommandSymbols]byte
1314
+ var cmd_bits [numCommandSymbols]uint16
1315
+ var dist_depth [maxSimpleDistanceAlphabetSize]byte
1316
+ var dist_bits [maxSimpleDistanceAlphabetSize]uint16
1317
+ var tree []huffmanTree
1318
+ var num_distance_symbols uint32 = params.dist.alphabet_size
1319
+
1320
+ storeCompressedMetaBlockHeader(is_last, length, storage_ix, storage)
1321
+
1322
+ histogramClearLiteral(&lit_histo)
1323
+ histogramClearCommand(&cmd_histo)
1324
+ histogramClearDistance(&dist_histo)
1325
+
1326
+ buildHistograms(input, start_pos, mask, commands, &lit_histo, &cmd_histo, &dist_histo)
1327
+
1328
+ writeBits(13, 0, storage_ix, storage)
1329
+
1330
+ tree = make([]huffmanTree, maxHuffmanTreeSize)
1331
+ buildAndStoreHuffmanTree(lit_histo.data_[:], numLiteralSymbols, numLiteralSymbols, tree, lit_depth[:], lit_bits[:], storage_ix, storage)
1332
+ buildAndStoreHuffmanTree(cmd_histo.data_[:], numCommandSymbols, numCommandSymbols, tree, cmd_depth[:], cmd_bits[:], storage_ix, storage)
1333
+ buildAndStoreHuffmanTree(dist_histo.data_[:], maxSimpleDistanceAlphabetSize, uint(num_distance_symbols), tree, dist_depth[:], dist_bits[:], storage_ix, storage)
1334
+ tree = nil
1335
+ storeDataWithHuffmanCodes(input, start_pos, mask, commands, lit_depth[:], lit_bits[:], cmd_depth[:], cmd_bits[:], dist_depth[:], dist_bits[:], storage_ix, storage)
1336
+ if is_last {
1337
+ jumpToByteBoundary(storage_ix, storage)
1338
+ }
1339
+ }
1340
+
1341
+ func storeMetaBlockFast(input []byte, start_pos uint, length uint, mask uint, is_last bool, params *encoderParams, commands []command, storage_ix *uint, storage []byte) {
1342
+ var num_distance_symbols uint32 = params.dist.alphabet_size
1343
+ var distance_alphabet_bits uint32 = log2FloorNonZero(uint(num_distance_symbols-1)) + 1
1344
+
1345
+ storeCompressedMetaBlockHeader(is_last, length, storage_ix, storage)
1346
+
1347
+ writeBits(13, 0, storage_ix, storage)
1348
+
1349
+ if len(commands) <= 128 {
1350
+ var histogram = [numLiteralSymbols]uint32{0}
1351
+ var pos uint = start_pos
1352
+ var num_literals uint = 0
1353
+ var lit_depth [numLiteralSymbols]byte
1354
+ var lit_bits [numLiteralSymbols]uint16
1355
+ for _, cmd := range commands {
1356
+ var j uint
1357
+ for j = uint(cmd.insert_len_); j != 0; j-- {
1358
+ histogram[input[pos&mask]]++
1359
+ pos++
1360
+ }
1361
+
1362
+ num_literals += uint(cmd.insert_len_)
1363
+ pos += uint(commandCopyLen(&cmd))
1364
+ }
1365
+
1366
+ buildAndStoreHuffmanTreeFast(histogram[:], num_literals, /* max_bits = */
1367
+ 8, lit_depth[:], lit_bits[:], storage_ix, storage)
1368
+
1369
+ storeStaticCommandHuffmanTree(storage_ix, storage)
1370
+ storeStaticDistanceHuffmanTree(storage_ix, storage)
1371
+ storeDataWithHuffmanCodes(input, start_pos, mask, commands, lit_depth[:], lit_bits[:], kStaticCommandCodeDepth[:], kStaticCommandCodeBits[:], kStaticDistanceCodeDepth[:], kStaticDistanceCodeBits[:], storage_ix, storage)
1372
+ } else {
1373
+ var lit_histo histogramLiteral
1374
+ var cmd_histo histogramCommand
1375
+ var dist_histo histogramDistance
1376
+ var lit_depth [numLiteralSymbols]byte
1377
+ var lit_bits [numLiteralSymbols]uint16
1378
+ var cmd_depth [numCommandSymbols]byte
1379
+ var cmd_bits [numCommandSymbols]uint16
1380
+ var dist_depth [maxSimpleDistanceAlphabetSize]byte
1381
+ var dist_bits [maxSimpleDistanceAlphabetSize]uint16
1382
+ histogramClearLiteral(&lit_histo)
1383
+ histogramClearCommand(&cmd_histo)
1384
+ histogramClearDistance(&dist_histo)
1385
+ buildHistograms(input, start_pos, mask, commands, &lit_histo, &cmd_histo, &dist_histo)
1386
+ buildAndStoreHuffmanTreeFast(lit_histo.data_[:], lit_histo.total_count_, /* max_bits = */
1387
+ 8, lit_depth[:], lit_bits[:], storage_ix, storage)
1388
+
1389
+ buildAndStoreHuffmanTreeFast(cmd_histo.data_[:], cmd_histo.total_count_, /* max_bits = */
1390
+ 10, cmd_depth[:], cmd_bits[:], storage_ix, storage)
1391
+
1392
+ buildAndStoreHuffmanTreeFast(dist_histo.data_[:], dist_histo.total_count_, /* max_bits = */
1393
+ uint(distance_alphabet_bits), dist_depth[:], dist_bits[:], storage_ix, storage)
1394
+
1395
+ storeDataWithHuffmanCodes(input, start_pos, mask, commands, lit_depth[:], lit_bits[:], cmd_depth[:], cmd_bits[:], dist_depth[:], dist_bits[:], storage_ix, storage)
1396
+ }
1397
+
1398
+ if is_last {
1399
+ jumpToByteBoundary(storage_ix, storage)
1400
+ }
1401
+ }
1402
+
1403
+ /*
1404
+ This is for storing uncompressed blocks (simple raw storage of
1405
+
1406
+ bytes-as-bytes).
1407
+ */
1408
+ func storeUncompressedMetaBlock(is_final_block bool, input []byte, position uint, mask uint, len uint, storage_ix *uint, storage []byte) {
1409
+ var masked_pos uint = position & mask
1410
+ storeUncompressedMetaBlockHeader(uint(len), storage_ix, storage)
1411
+ jumpToByteBoundary(storage_ix, storage)
1412
+
1413
+ if masked_pos+len > mask+1 {
1414
+ var len1 uint = mask + 1 - masked_pos
1415
+ copy(storage[*storage_ix>>3:], input[masked_pos:][:len1])
1416
+ *storage_ix += len1 << 3
1417
+ len -= len1
1418
+ masked_pos = 0
1419
+ }
1420
+
1421
+ copy(storage[*storage_ix>>3:], input[masked_pos:][:len])
1422
+ *storage_ix += uint(len << 3)
1423
+
1424
+ /* We need to clear the next 4 bytes to continue to be
1425
+ compatible with BrotliWriteBits. */
1426
+ writeBitsPrepareStorage(*storage_ix, storage)
1427
+
1428
+ /* Since the uncompressed block itself may not be the final block, add an
1429
+ empty one after this. */
1430
+ if is_final_block {
1431
+ writeBits(1, 1, storage_ix, storage) /* islast */
1432
+ writeBits(1, 1, storage_ix, storage) /* isempty */
1433
+ jumpToByteBoundary(storage_ix, storage)
1434
+ }
1435
+ }