Debian Codegraph @

fastify-busboy/deps/streamsearch/sbmh.js

raw symbols SCIP
1 'use strict'
2
3 /**
4 * Copyright Brian White. All rights reserved.
5 *
6 * @see https://github.com/mscdex/streamsearch
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to
10 * deal in the Software without restriction, including without limitation the
11 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 * sell copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * IN THE SOFTWARE.
25 *
26 * Based heavily on the Streaming Boyer-Moore-Horspool C++ implementation
27 * by Hongli Lai at: https://github.com/FooBarWidget/boyer-moore-horspool
28 */
29
30 const { EventEmitter } = require('node:events')
31 const { inherits } = require('node:util')
32
33 function SBMH (needle) {
34 if (typeof needle === 'string') {
35 needle = Buffer.from(needle)
36 }
37
38 if (!Buffer.isBuffer(needle)) {
39 throw new TypeError('The needle has to be a String or a Buffer.')
40 }
41
42 const needleLength = needle.length
43 const needleLastCharIndex = needleLength - 1
44
45 if (needleLength === 0) {
46 throw new Error('The needle cannot be an empty String/Buffer.')
47 }
48
49 if (needleLength > 256) {
50 throw new Error('The needle cannot have a length bigger than 256.')
51 }
52
53 this.maxMatches = Infinity
54 this.matches = 0
55
56 this._occ = new Uint8Array(256)
57 .fill(needleLength) // Initialize occurrence table.
58 this._lookbehind_size = 0
59 this._needle = needle
60 this._bufpos = 0
61
62 this._lookbehind = Buffer.alloc(needleLastCharIndex)
63
64 // Populate occurrence table with analysis of the needle,
65 // ignoring last letter.
66 for (var i = 0; i < needleLastCharIndex; ++i) { // eslint-disable-line no-var
67 this._occ[needle[i]] = needleLastCharIndex - i
68 }
69 }
70 inherits(SBMH, EventEmitter)
71
72 SBMH.prototype.reset = function () {
73 this._lookbehind_size = 0
74 this.matches = 0
75 this._bufpos = 0
76 }
77
78 SBMH.prototype.push = function (chunk, pos) {
79 if (!Buffer.isBuffer(chunk)) {
80 chunk = Buffer.from(chunk, 'binary')
81 }
82 const chlen = chunk.length
83 this._bufpos = pos || 0
84 let r
85 while (r !== chlen && this.matches < this.maxMatches) { r = this._sbmh_feed(chunk) }
86 return r
87 }
88
89 SBMH.prototype._sbmh_feed = function (data) {
90 const len = data.length
91 const needle = this._needle
92 const needleLength = needle.length
93 const needleLastCharIndex = needleLength - 1
94 const needleLastChar = needle[needleLastCharIndex]
95
96 // Positive: points to a position in `data`
97 // pos == 3 points to data[3]
98 // Negative: points to a position in the lookbehind buffer
99 // pos == -2 points to lookbehind[lookbehind_size - 2]
100 let pos = -this._lookbehind_size
101 let ch
102
103 if (pos < 0) {
104 // Lookbehind buffer is not empty. Perform Boyer-Moore-Horspool
105 // search with character lookup code that considers both the
106 // lookbehind buffer and the current round's haystack data.
107 //
108 // Loop until
109 // there is a match.
110 // or until
111 // we've moved past the position that requires the
112 // lookbehind buffer. In this case we switch to the
113 // optimized loop.
114 // or until
115 // the character to look at lies outside the haystack.
116 while (pos < 0 && pos <= len - needleLength) {
117 ch = data[pos + needleLastCharIndex]
118
119 if (
120 ch === needleLastChar &&
121 this._sbmh_memcmp(data, pos, needleLastCharIndex)
122 ) {
123 this._lookbehind_size = 0
124 ++this.matches
125 this.emit('info', true)
126 return (this._bufpos = pos + needleLength)
127 }
128
129 pos += this._occ[ch]
130 }
131
132 // No match.
133
134 while (pos < 0 && !this._sbmh_memcmp(data, pos, len - pos)) {
135 // There's too few data for Boyer-Moore-Horspool to run,
136 // so let's use a different algorithm to skip as much as
137 // we can.
138 // Forward pos until
139 // the trailing part of lookbehind + data
140 // looks like the beginning of the needle
141 // or until
142 // pos == 0
143 ++pos
144 }
145
146 if (pos >= 0) {
147 // Discard lookbehind buffer.
148 this.emit('info', false, this._lookbehind, 0, this._lookbehind_size)
149 this._lookbehind_size = 0
150 } else {
151 // Cut off part of the lookbehind buffer that has
152 // been processed and append the entire haystack
153 // into it.
154 const bytesToCutOff = this._lookbehind_size + pos
155 if (bytesToCutOff > 0) {
156 // The cut off data is guaranteed not to contain the needle.
157 this.emit('info', false, this._lookbehind, 0, bytesToCutOff)
158 }
159
160 this._lookbehind_size -= bytesToCutOff
161 this._lookbehind.copy(this._lookbehind, 0, bytesToCutOff, this._lookbehind_size)
162
163 data.copy(this._lookbehind, this._lookbehind_size)
164 this._lookbehind_size += len
165
166 this._bufpos = len
167 return len
168 }
169 }
170
171 // Lookbehind buffer is now empty. We only need to check if the
172 // needle is in the haystack.
173 pos = data.indexOf(needle, pos + this._bufpos)
174
175 if (pos !== -1) {
176 ++this.matches
177 if (pos === 0) { this.emit('info', true) } else { this.emit('info', true, data, this._bufpos, pos) }
178 return (this._bufpos = pos + needleLength)
179 }
180
181 pos = len - needleLastCharIndex
182 if (pos < 0) {
183 pos = 0
184 }
185
186 // There was no match. If there's trailing haystack data that we cannot
187 // match yet using the Boyer-Moore-Horspool algorithm (because the trailing
188 // data is less than the needle size) then match using a modified
189 // algorithm that starts matching from the beginning instead of the end.
190 // Whatever trailing data is left after running this algorithm is added to
191 // the lookbehind buffer.
192 while (
193 pos !== len &&
194 (
195 data[pos] !== needle[0] ||
196 Buffer.compare(
197 data.subarray(pos + 1, len),
198 needle.subarray(1, len - pos)
199 ) !== 0
200 )
201 ) {
202 ++pos
203 }
204
205 if (pos !== len) {
206 data.copy(this._lookbehind, 0, pos, len)
207 this._lookbehind_size = len - pos
208 }
209
210 // Everything until pos is guaranteed not to contain needle data.
211 if (pos !== 0) { this.emit('info', false, data, this._bufpos, pos) }
212
213 this._bufpos = len
214 return len
215 }
216
217 SBMH.prototype._sbmh_lookup_char = function (data, pos) {
218 return pos < 0
219 ? this._lookbehind[this._lookbehind_size + pos]
220 : data[pos]
221 }
222
223 SBMH.prototype._sbmh_memcmp = function (data, pos, len) {
224 for (var i = 0; i < len; ++i) { // eslint-disable-line no-var
225 if (this._sbmh_lookup_char(data, pos + i) !== this._needle[i]) { return false }
226 }
227 return true
228 }
229
230 module.exports = SBMH