typenative 0.3.1 → 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 (147) hide show
  1. package/CHANGELOG.md +57 -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 -47
  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/typed_arrays.js +608 -0
  19. package/bin/node/util.js +3 -0
  20. package/bin/node/zlib.js +131 -16
  21. package/bin/transpiler.js +635 -44
  22. package/bin/version.js +1 -1
  23. package/conformance.md +16 -4
  24. package/examples/README.md +54 -0
  25. package/examples/fibonacci/README.md +89 -0
  26. package/examples/fibonacci/bench.mjs +87 -0
  27. package/examples/fibonacci/main.ts +21 -0
  28. package/examples/hello-world/README.md +62 -0
  29. package/examples/hello-world/bench.mjs +66 -0
  30. package/examples/hello-world/main.ts +7 -0
  31. package/examples/json-server/README.md +125 -0
  32. package/examples/json-server/bench.mjs +171 -0
  33. package/examples/json-server/main.ts +67 -0
  34. package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
  35. package/govendor/github.com/andybalholm/brotli/README.md +19 -0
  36. package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
  37. package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
  38. package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
  39. package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
  40. package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
  41. package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
  42. package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
  43. package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
  44. package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
  45. package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
  46. package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
  47. package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
  48. package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
  49. package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
  50. package/govendor/github.com/andybalholm/brotli/command.go +254 -0
  51. package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
  52. package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
  53. package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
  54. package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
  55. package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
  56. package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
  57. package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
  58. package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
  59. package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
  60. package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
  61. package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
  62. package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
  63. package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
  64. package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
  65. package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
  66. package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
  67. package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
  68. package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
  69. package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
  70. package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
  71. package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
  72. package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
  73. package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
  74. package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
  75. package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
  76. package/govendor/github.com/andybalholm/brotli/http.go +201 -0
  77. package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
  78. package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
  79. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
  80. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
  81. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
  82. package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
  83. package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
  84. package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
  85. package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
  86. package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
  87. package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
  88. package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
  89. package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
  90. package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
  91. package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
  92. package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
  93. package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
  94. package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
  95. package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
  96. package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
  97. package/govendor/github.com/andybalholm/brotli/params.go +37 -0
  98. package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
  99. package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
  100. package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
  101. package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
  102. package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
  103. package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
  104. package/govendor/github.com/andybalholm/brotli/state.go +313 -0
  105. package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
  106. package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
  107. package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
  108. package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
  109. package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
  110. package/govendor/github.com/andybalholm/brotli/util.go +7 -0
  111. package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
  112. package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
  113. package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
  114. package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
  115. package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
  116. package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
  117. package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
  118. package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
  119. package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
  120. package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
  121. package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
  122. package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
  123. package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
  124. package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
  125. package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
  126. package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
  127. package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
  128. package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
  129. package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
  130. package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
  131. package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
  132. package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
  133. package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
  134. package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
  135. package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
  136. package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
  137. package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
  138. package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
  139. package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
  140. package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
  141. package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
  142. package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
  143. package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
  144. package/govendor/manifest.txt +3 -1
  145. package/package.json +1 -1
  146. package/types/typenative-npm.d.ts +72 -0
  147. package/types/typenative.d.ts +117 -1
@@ -0,0 +1,196 @@
1
+ package brotli
2
+
3
+ const fastOnePassCompressionQuality = 0
4
+
5
+ const fastTwoPassCompressionQuality = 1
6
+
7
+ const zopflificationQuality = 10
8
+
9
+ const hqZopflificationQuality = 11
10
+
11
+ const maxQualityForStaticEntropyCodes = 2
12
+
13
+ const minQualityForBlockSplit = 4
14
+
15
+ const minQualityForNonzeroDistanceParams = 4
16
+
17
+ const minQualityForOptimizeHistograms = 4
18
+
19
+ const minQualityForExtensiveReferenceSearch = 5
20
+
21
+ const minQualityForContextModeling = 5
22
+
23
+ const minQualityForHqContextModeling = 7
24
+
25
+ const minQualityForHqBlockSplitting = 10
26
+
27
+ /* For quality below MIN_QUALITY_FOR_BLOCK_SPLIT there is no block splitting,
28
+ so we buffer at most this much literals and commands. */
29
+ const maxNumDelayedSymbols = 0x2FFF
30
+
31
+ /* Returns hash-table size for quality levels 0 and 1. */
32
+ func maxHashTableSize(quality int) uint {
33
+ if quality == fastOnePassCompressionQuality {
34
+ return 1 << 15
35
+ } else {
36
+ return 1 << 17
37
+ }
38
+ }
39
+
40
+ /* The maximum length for which the zopflification uses distinct distances. */
41
+ const maxZopfliLenQuality10 = 150
42
+
43
+ const maxZopfliLenQuality11 = 325
44
+
45
+ /* Do not thoroughly search when a long copy is found. */
46
+ const longCopyQuickStep = 16384
47
+
48
+ func maxZopfliLen(params *encoderParams) uint {
49
+ if params.quality <= 10 {
50
+ return maxZopfliLenQuality10
51
+ } else {
52
+ return maxZopfliLenQuality11
53
+ }
54
+ }
55
+
56
+ /* Number of best candidates to evaluate to expand Zopfli chain. */
57
+ func maxZopfliCandidates(params *encoderParams) uint {
58
+ if params.quality <= 10 {
59
+ return 1
60
+ } else {
61
+ return 5
62
+ }
63
+ }
64
+
65
+ func sanitizeParams(params *encoderParams) {
66
+ params.quality = brotli_min_int(maxQuality, brotli_max_int(minQuality, params.quality))
67
+ if params.quality <= maxQualityForStaticEntropyCodes {
68
+ params.large_window = false
69
+ }
70
+
71
+ if params.lgwin < minWindowBits {
72
+ params.lgwin = minWindowBits
73
+ } else {
74
+ var max_lgwin int
75
+ if params.large_window {
76
+ max_lgwin = largeMaxWindowBits
77
+ } else {
78
+ max_lgwin = maxWindowBits
79
+ }
80
+ if params.lgwin > uint(max_lgwin) {
81
+ params.lgwin = uint(max_lgwin)
82
+ }
83
+ }
84
+ }
85
+
86
+ /* Returns optimized lg_block value. */
87
+ func computeLgBlock(params *encoderParams) int {
88
+ var lgblock int = params.lgblock
89
+ if params.quality == fastOnePassCompressionQuality || params.quality == fastTwoPassCompressionQuality {
90
+ lgblock = int(params.lgwin)
91
+ } else if params.quality < minQualityForBlockSplit {
92
+ lgblock = 14
93
+ } else if lgblock == 0 {
94
+ lgblock = 16
95
+ if params.quality >= 9 && params.lgwin > uint(lgblock) {
96
+ lgblock = brotli_min_int(18, int(params.lgwin))
97
+ }
98
+ } else {
99
+ lgblock = brotli_min_int(maxInputBlockBits, brotli_max_int(minInputBlockBits, lgblock))
100
+ }
101
+
102
+ return lgblock
103
+ }
104
+
105
+ /* Returns log2 of the size of main ring buffer area.
106
+ Allocate at least lgwin + 1 bits for the ring buffer so that the newly
107
+ added block fits there completely and we still get lgwin bits and at least
108
+ read_block_size_bits + 1 bits because the copy tail length needs to be
109
+ smaller than ring-buffer size. */
110
+ func computeRbBits(params *encoderParams) int {
111
+ return 1 + brotli_max_int(int(params.lgwin), params.lgblock)
112
+ }
113
+
114
+ func maxMetablockSize(params *encoderParams) uint {
115
+ var bits int = brotli_min_int(computeRbBits(params), maxInputBlockBits)
116
+ return uint(1) << uint(bits)
117
+ }
118
+
119
+ /* When searching for backward references and have not seen matches for a long
120
+ time, we can skip some match lookups. Unsuccessful match lookups are very
121
+ expensive and this kind of a heuristic speeds up compression quite a lot.
122
+ At first 8 byte strides are taken and every second byte is put to hasher.
123
+ After 4x more literals stride by 16 bytes, every put 4-th byte to hasher.
124
+ Applied only to qualities 2 to 9. */
125
+ func literalSpreeLengthForSparseSearch(params *encoderParams) uint {
126
+ if params.quality < 9 {
127
+ return 64
128
+ } else {
129
+ return 512
130
+ }
131
+ }
132
+
133
+ func chooseHasher(params *encoderParams, hparams *hasherParams) {
134
+ if params.quality > 9 {
135
+ hparams.type_ = 10
136
+ } else if params.quality == 4 && params.size_hint >= 1<<20 {
137
+ hparams.type_ = 54
138
+ } else if params.quality < 5 {
139
+ hparams.type_ = params.quality
140
+ } else if params.lgwin <= 16 {
141
+ if params.quality < 7 {
142
+ hparams.type_ = 40
143
+ } else if params.quality < 9 {
144
+ hparams.type_ = 41
145
+ } else {
146
+ hparams.type_ = 42
147
+ }
148
+ } else if params.size_hint >= 1<<20 && params.lgwin >= 19 {
149
+ hparams.type_ = 6
150
+ hparams.block_bits = params.quality - 1
151
+ hparams.bucket_bits = 15
152
+ hparams.hash_len = 5
153
+ if params.quality < 7 {
154
+ hparams.num_last_distances_to_check = 4
155
+ } else if params.quality < 9 {
156
+ hparams.num_last_distances_to_check = 10
157
+ } else {
158
+ hparams.num_last_distances_to_check = 16
159
+ }
160
+ } else {
161
+ hparams.type_ = 5
162
+ hparams.block_bits = params.quality - 1
163
+ if params.quality < 7 {
164
+ hparams.bucket_bits = 14
165
+ } else {
166
+ hparams.bucket_bits = 15
167
+ }
168
+ if params.quality < 7 {
169
+ hparams.num_last_distances_to_check = 4
170
+ } else if params.quality < 9 {
171
+ hparams.num_last_distances_to_check = 10
172
+ } else {
173
+ hparams.num_last_distances_to_check = 16
174
+ }
175
+ }
176
+
177
+ if params.lgwin > 24 {
178
+ /* Different hashers for large window brotli: not for qualities <= 2,
179
+ these are too fast for large window. Not for qualities >= 10: their
180
+ hasher already works well with large window. So the changes are:
181
+ H3 --> H35: for quality 3.
182
+ H54 --> H55: for quality 4 with size hint > 1MB
183
+ H6 --> H65: for qualities 5, 6, 7, 8, 9. */
184
+ if hparams.type_ == 3 {
185
+ hparams.type_ = 35
186
+ }
187
+
188
+ if hparams.type_ == 54 {
189
+ hparams.type_ = 55
190
+ }
191
+
192
+ if hparams.type_ == 6 {
193
+ hparams.type_ = 65
194
+ }
195
+ }
196
+ }
@@ -0,0 +1,127 @@
1
+ package brotli
2
+
3
+ import (
4
+ "errors"
5
+ "io"
6
+ )
7
+
8
+ type decodeError int
9
+
10
+ func (err decodeError) Error() string {
11
+ return "brotli: " + string(decoderErrorString(int(err)))
12
+ }
13
+
14
+ var errExcessiveInput = errors.New("brotli: excessive input")
15
+ var errInvalidState = errors.New("brotli: invalid state")
16
+
17
+ // readBufSize is a "good" buffer size that avoids excessive round-trips
18
+ // between C and Go but doesn't waste too much memory on buffering.
19
+ // It is arbitrarily chosen to be equal to the constant used in io.Copy.
20
+ const readBufSize = 32 * 1024
21
+
22
+ // NewReader creates a new Reader reading the given reader.
23
+ func NewReader(src io.Reader) *Reader {
24
+ r := new(Reader)
25
+ r.Reset(src)
26
+ return r
27
+ }
28
+
29
+ // Reset discards the Reader's state and makes it equivalent to the result of
30
+ // its original state from NewReader, but reading from src instead.
31
+ // This permits reusing a Reader rather than allocating a new one.
32
+ // Error is always nil
33
+ func (r *Reader) Reset(src io.Reader) error {
34
+ if r.error_code < 0 {
35
+ // There was an unrecoverable error, leaving the Reader's state
36
+ // undefined. Clear out everything but the buffers.
37
+ *r = Reader{
38
+ buf: r.buf,
39
+ block_type_trees: r.block_type_trees,
40
+ literal_hgroup: huffmanTreeGroup{
41
+ htrees: r.literal_hgroup.htrees,
42
+ codes: r.literal_hgroup.codes,
43
+ },
44
+ distance_hgroup: huffmanTreeGroup{
45
+ htrees: r.distance_hgroup.htrees,
46
+ codes: r.distance_hgroup.codes,
47
+ },
48
+ insert_copy_hgroup: huffmanTreeGroup{
49
+ htrees: r.insert_copy_hgroup.htrees,
50
+ codes: r.insert_copy_hgroup.codes,
51
+ },
52
+ }
53
+ }
54
+
55
+ decoderStateInit(r)
56
+ r.src = src
57
+ if r.buf == nil {
58
+ r.buf = make([]byte, readBufSize)
59
+ }
60
+ r.in = r.buf[:0]
61
+ return nil
62
+ }
63
+
64
+ func (r *Reader) Read(p []byte) (n int, err error) {
65
+ if !decoderHasMoreOutput(r) && len(r.in) == 0 {
66
+ m, readErr := r.src.Read(r.buf)
67
+ if m == 0 {
68
+ if readErr == io.EOF && r.state != stateDone {
69
+ readErr = io.ErrUnexpectedEOF
70
+ }
71
+ // If readErr is `nil`, we just proxy underlying stream behavior.
72
+ return 0, readErr
73
+ }
74
+ r.in = r.buf[:m]
75
+ }
76
+
77
+ if len(p) == 0 {
78
+ return 0, nil
79
+ }
80
+
81
+ for {
82
+ var written uint
83
+ in_len := uint(len(r.in))
84
+ out_len := uint(len(p))
85
+ in_remaining := in_len
86
+ out_remaining := out_len
87
+ result := decoderDecompressStream(r, &in_remaining, &r.in, &out_remaining, &p)
88
+ written = out_len - out_remaining
89
+ n = int(written)
90
+
91
+ switch result {
92
+ case decoderResultSuccess:
93
+ if len(r.in) > 0 {
94
+ return n, errExcessiveInput
95
+ }
96
+ return n, nil
97
+ case decoderResultError:
98
+ return n, decodeError(decoderGetErrorCode(r))
99
+ case decoderResultNeedsMoreOutput:
100
+ if n == 0 {
101
+ return 0, io.ErrShortBuffer
102
+ }
103
+ return n, nil
104
+ case decoderNeedsMoreInput:
105
+ }
106
+
107
+ if len(r.in) != 0 {
108
+ return 0, errInvalidState
109
+ }
110
+
111
+ // Calling r.src.Read may block. Don't block if we have data to return.
112
+ if n > 0 {
113
+ return n, nil
114
+ }
115
+
116
+ // Top off the buffer.
117
+ encN, err := r.src.Read(r.buf)
118
+ if encN == 0 {
119
+ // Not enough data to complete decoding.
120
+ if err == io.EOF {
121
+ return 0, io.ErrUnexpectedEOF
122
+ }
123
+ return 0, err
124
+ }
125
+ r.in = r.buf[:encN]
126
+ }
127
+ }
@@ -0,0 +1,134 @@
1
+ package brotli
2
+
3
+ /* Copyright 2013 Google Inc. All Rights Reserved.
4
+
5
+ Distributed under MIT license.
6
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ /* A ringBuffer(window_bits, tail_bits) contains `1 << window_bits' bytes of
10
+ data in a circular manner: writing a byte writes it to:
11
+ `position() % (1 << window_bits)'.
12
+ For convenience, the ringBuffer array contains another copy of the
13
+ first `1 << tail_bits' bytes:
14
+ buffer_[i] == buffer_[i + (1 << window_bits)], if i < (1 << tail_bits),
15
+ and another copy of the last two bytes:
16
+ buffer_[-1] == buffer_[(1 << window_bits) - 1] and
17
+ buffer_[-2] == buffer_[(1 << window_bits) - 2]. */
18
+ type ringBuffer struct {
19
+ size_ uint32
20
+ mask_ uint32
21
+ tail_size_ uint32
22
+ total_size_ uint32
23
+ cur_size_ uint32
24
+ pos_ uint32
25
+ data_ []byte
26
+ buffer_ []byte
27
+ }
28
+
29
+ func ringBufferInit(rb *ringBuffer) {
30
+ rb.pos_ = 0
31
+ }
32
+
33
+ func ringBufferSetup(params *encoderParams, rb *ringBuffer) {
34
+ var window_bits int = computeRbBits(params)
35
+ var tail_bits int = params.lgblock
36
+ *(*uint32)(&rb.size_) = 1 << uint(window_bits)
37
+ *(*uint32)(&rb.mask_) = (1 << uint(window_bits)) - 1
38
+ *(*uint32)(&rb.tail_size_) = 1 << uint(tail_bits)
39
+ *(*uint32)(&rb.total_size_) = rb.size_ + rb.tail_size_
40
+ }
41
+
42
+ const kSlackForEightByteHashingEverywhere uint = 7
43
+
44
+ /* Allocates or re-allocates data_ to the given length + plus some slack
45
+ region before and after. Fills the slack regions with zeros. */
46
+ func ringBufferInitBuffer(buflen uint32, rb *ringBuffer) {
47
+ var new_data []byte
48
+ var i uint
49
+ size := 2 + int(buflen) + int(kSlackForEightByteHashingEverywhere)
50
+ if cap(rb.data_) < size {
51
+ new_data = make([]byte, size)
52
+ } else {
53
+ new_data = rb.data_[:size]
54
+ }
55
+ if rb.data_ != nil {
56
+ copy(new_data, rb.data_[:2+rb.cur_size_+uint32(kSlackForEightByteHashingEverywhere)])
57
+ }
58
+
59
+ rb.data_ = new_data
60
+ rb.cur_size_ = buflen
61
+ rb.buffer_ = rb.data_[2:]
62
+ rb.data_[1] = 0
63
+ rb.data_[0] = rb.data_[1]
64
+ for i = 0; i < kSlackForEightByteHashingEverywhere; i++ {
65
+ rb.buffer_[rb.cur_size_+uint32(i)] = 0
66
+ }
67
+ }
68
+
69
+ func ringBufferWriteTail(bytes []byte, n uint, rb *ringBuffer) {
70
+ var masked_pos uint = uint(rb.pos_ & rb.mask_)
71
+ if uint32(masked_pos) < rb.tail_size_ {
72
+ /* Just fill the tail buffer with the beginning data. */
73
+ var p uint = uint(rb.size_ + uint32(masked_pos))
74
+ copy(rb.buffer_[p:], bytes[:brotli_min_size_t(n, uint(rb.tail_size_-uint32(masked_pos)))])
75
+ }
76
+ }
77
+
78
+ /* Push bytes into the ring buffer. */
79
+ func ringBufferWrite(bytes []byte, n uint, rb *ringBuffer) {
80
+ if rb.pos_ == 0 && uint32(n) < rb.tail_size_ {
81
+ /* Special case for the first write: to process the first block, we don't
82
+ need to allocate the whole ring-buffer and we don't need the tail
83
+ either. However, we do this memory usage optimization only if the
84
+ first write is less than the tail size, which is also the input block
85
+ size, otherwise it is likely that other blocks will follow and we
86
+ will need to reallocate to the full size anyway. */
87
+ rb.pos_ = uint32(n)
88
+
89
+ ringBufferInitBuffer(rb.pos_, rb)
90
+ copy(rb.buffer_, bytes[:n])
91
+ return
92
+ }
93
+
94
+ if rb.cur_size_ < rb.total_size_ {
95
+ /* Lazily allocate the full buffer. */
96
+ ringBufferInitBuffer(rb.total_size_, rb)
97
+
98
+ /* Initialize the last two bytes to zero, so that we don't have to worry
99
+ later when we copy the last two bytes to the first two positions. */
100
+ rb.buffer_[rb.size_-2] = 0
101
+
102
+ rb.buffer_[rb.size_-1] = 0
103
+ }
104
+ {
105
+ var masked_pos uint = uint(rb.pos_ & rb.mask_)
106
+
107
+ /* The length of the writes is limited so that we do not need to worry
108
+ about a write */
109
+ ringBufferWriteTail(bytes, n, rb)
110
+
111
+ if uint32(masked_pos+n) <= rb.size_ {
112
+ /* A single write fits. */
113
+ copy(rb.buffer_[masked_pos:], bytes[:n])
114
+ } else {
115
+ /* Split into two writes.
116
+ Copy into the end of the buffer, including the tail buffer. */
117
+ copy(rb.buffer_[masked_pos:], bytes[:brotli_min_size_t(n, uint(rb.total_size_-uint32(masked_pos)))])
118
+
119
+ /* Copy into the beginning of the buffer */
120
+ copy(rb.buffer_, bytes[rb.size_-uint32(masked_pos):][:uint32(n)-(rb.size_-uint32(masked_pos))])
121
+ }
122
+ }
123
+ {
124
+ var not_first_lap bool = rb.pos_&(1<<31) != 0
125
+ var rb_pos_mask uint32 = (1 << 31) - 1
126
+ rb.data_[0] = rb.buffer_[rb.size_-2]
127
+ rb.data_[1] = rb.buffer_[rb.size_-1]
128
+ rb.pos_ = (rb.pos_ & rb_pos_mask) + uint32(uint32(n)&rb_pos_mask)
129
+ if not_first_lap {
130
+ /* Wrap, but preserve not-a-first-lap feature. */
131
+ rb.pos_ |= 1 << 31
132
+ }
133
+ }
134
+ }