| 1 |
'use strict' |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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) |
| 58 |
this._lookbehind_size = 0 |
| 59 |
this._needle = needle |
| 60 |
this._bufpos = 0 |
| 61 |
|
| 62 |
this._lookbehind = Buffer.alloc(needleLastCharIndex) |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
for (var i = 0; i < needleLastCharIndex; ++i) { |
| 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 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
let pos = -this._lookbehind_size |
| 101 |
let ch |
| 102 |
|
| 103 |
if (pos < 0) { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 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 |
|
| 133 |
|
| 134 |
while (pos < 0 && !this._sbmh_memcmp(data, pos, len - pos)) { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
++pos |
| 144 |
} |
| 145 |
|
| 146 |
if (pos >= 0) { |
| 147 |
|
| 148 |
this.emit('info', false, this._lookbehind, 0, this._lookbehind_size) |
| 149 |
this._lookbehind_size = 0 |
| 150 |
} else { |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
const bytesToCutOff = this._lookbehind_size + pos |
| 155 |
if (bytesToCutOff > 0) { |
| 156 |
|
| 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 |
|
| 172 |
|
| 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 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 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) { |
| 225 |
if (this._sbmh_lookup_char(data, pos + i) !== this._needle[i]) { return false } |
| 226 |
} |
| 227 |
return true |
| 228 |
} |
| 229 |
|
| 230 |
module.exports = SBMH |