111 lines
2.4 KiB
JavaScript
111 lines
2.4 KiB
JavaScript
'use strict'
|
|
|
|
|
|
var Transform = require('stream').Transform
|
|
var streamParser = require('stream-parser')
|
|
|
|
|
|
function ParserStream () {
|
|
Transform.call(this, { readableObjectMode: true })
|
|
}
|
|
|
|
// Inherit from Transform
|
|
ParserStream.prototype = Object.create(Transform.prototype)
|
|
ParserStream.prototype.constructor = ParserStream
|
|
|
|
streamParser(ParserStream.prototype)
|
|
|
|
|
|
exports.ParserStream = ParserStream
|
|
|
|
|
|
exports.sliceEq = function (src, start, dest) {
|
|
for (var i = start, j = 0; j < dest.length;) {
|
|
if (src[i++] !== dest[j++]) return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
exports.str2arr = function (str, format) {
|
|
var arr = []
|
|
var i = 0
|
|
|
|
if (format && format === 'hex') {
|
|
while (i < str.length) {
|
|
arr.push(parseInt(str.slice(i, i + 2), 16))
|
|
i += 2
|
|
}
|
|
} else {
|
|
for (; i < str.length; i++) {
|
|
arr.push(str.charCodeAt(i) & 0xFF)
|
|
}
|
|
}
|
|
|
|
return arr
|
|
}
|
|
|
|
exports.readUInt16LE = function (data, offset) {
|
|
return data[offset] | (data[offset + 1] << 8)
|
|
}
|
|
|
|
exports.readUInt16BE = function (data, offset) {
|
|
return (data[offset] << 8) | data[offset + 1]
|
|
}
|
|
|
|
exports.readInt16LE = function (data, offset) {
|
|
return (exports.readUInt16LE(data, offset) << 16) >> 16
|
|
}
|
|
|
|
exports.readInt16BE = function (data, offset) {
|
|
return (exports.readUInt16BE(data, offset) << 16) >> 16
|
|
}
|
|
|
|
exports.readUInt32LE = function (data, offset) {
|
|
return (data[offset] |
|
|
(data[offset + 1] << 8) |
|
|
(data[offset + 2] << 16)) +
|
|
data[offset + 3] * 0x1000000
|
|
}
|
|
|
|
exports.readUInt32BE = function (data, offset) {
|
|
return data[offset] * 0x1000000 +
|
|
((data[offset + 1] << 16) |
|
|
(data[offset + 2] << 8) |
|
|
data[offset + 3])
|
|
}
|
|
|
|
exports.readInt32LE = function (data, offset) {
|
|
return exports.readUInt32LE(data, offset) | 0
|
|
}
|
|
|
|
exports.readInt32BE = function (data, offset) {
|
|
return exports.readUInt32BE(data, offset) | 0
|
|
}
|
|
|
|
|
|
function ProbeError (message, code, statusCode) {
|
|
Error.call(this)
|
|
|
|
// Include stack trace in error object
|
|
if (Error.captureStackTrace) {
|
|
// Chrome and NodeJS
|
|
Error.captureStackTrace(this, this.constructor)
|
|
} else {
|
|
// FF, IE 10+ and Safari 6+. Fallback for others
|
|
this.stack = (new Error()).stack || ''
|
|
}
|
|
|
|
this.name = this.constructor.name
|
|
|
|
this.message = message
|
|
if (code) this.code = code
|
|
if (statusCode) this.statusCode = statusCode
|
|
}
|
|
|
|
// Inherit from Error
|
|
ProbeError.prototype = Object.create(Error.prototype)
|
|
ProbeError.prototype.constructor = ProbeError
|
|
|
|
|
|
exports.ProbeError = ProbeError
|