1 | 'use strict';
|
2 |
|
3 |
|
4 | var Transform = require('stream').Transform;
|
5 | var streamParser = require('stream-parser');
|
6 | var inherits = require('util').inherits;
|
7 |
|
8 |
|
9 | function ParserStream() {
|
10 | Transform.call(this, { readableObjectMode: true });
|
11 | }
|
12 |
|
13 | inherits(ParserStream, Transform);
|
14 | streamParser(ParserStream.prototype);
|
15 |
|
16 |
|
17 | exports.ParserStream = ParserStream;
|
18 |
|
19 |
|
20 | exports.sliceEq = function (src, start, dest) {
|
21 | for (var i = start, j = 0; j < dest.length;) {
|
22 | if (src[i++] !== dest[j++]) return false;
|
23 | }
|
24 | return true;
|
25 | };
|
26 |
|
27 | exports.str2arr = function (str, format) {
|
28 | var arr = [], i = 0;
|
29 |
|
30 | if (format && format === 'hex') {
|
31 | while (i < str.length) {
|
32 | arr.push(parseInt(str.slice(i, i + 2), 16));
|
33 | i += 2;
|
34 | }
|
35 | } else {
|
36 | for (; i < str.length; i++) {
|
37 |
|
38 | arr.push(str.charCodeAt(i) & 0xFF);
|
39 | }
|
40 | }
|
41 |
|
42 | return arr;
|
43 | };
|
44 |
|
45 | exports.readUInt16LE = function (data, offset) {
|
46 | return data[offset] | (data[offset + 1] << 8);
|
47 | };
|
48 |
|
49 | exports.readUInt16BE = function (data, offset) {
|
50 | return data[offset + 1] | (data[offset] << 8);
|
51 | };
|
52 |
|
53 | exports.readUInt32LE = function (data, offset) {
|
54 | return data[offset] |
|
55 | (data[offset + 1] << 8) |
|
56 | (data[offset + 2] << 16) |
|
57 | (data[offset + 3] * 0x1000000);
|
58 | };
|
59 |
|
60 | exports.readUInt32BE = function (data, offset) {
|
61 | return data[offset + 3] |
|
62 | (data[offset + 2] << 8) |
|
63 | (data[offset + 1] << 16) |
|
64 | (data[offset] * 0x1000000);
|
65 | };
|
66 |
|
67 |
|
68 | function ProbeError(message, code, statusCode) {
|
69 | Error.call(this);
|
70 | Error.captureStackTrace(this, this.constructor);
|
71 |
|
72 | this.name = this.constructor.name;
|
73 |
|
74 | this.message = message;
|
75 | if (code) this.code = code;
|
76 | if (statusCode) this.statusCode = statusCode;
|
77 | }
|
78 |
|
79 |
|
80 | require('inherits')(ProbeError, Error);
|
81 |
|
82 |
|
83 | exports.ProbeError = ProbeError;
|