-
Notifications
You must be signed in to change notification settings - Fork 15
/
BufferReader.js
87 lines (68 loc) · 1.98 KB
/
BufferReader.js
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use strict";
var BufferReader = function( buffer ) {
this.buffer = buffer;
this.offset = 0;
};
BufferReader.prototype.readUInt24BE = function( offset ) {
return ( this.readUInt8( offset ) << 16 ) + this.readUInt16BE();
};
BufferReader.prototype.readUInt24LE = function( offset ) {
return this.readUInt8( offset ) + ( this.readUInt16LE() << 8 );
};
BufferReader.prototype.readBytes = function( size ) {
var value = this.buffer.slice( this.offset, this.offset + size );
this.offset += size;
return value;
};
BufferReader.prototype.seek = function( pos ) {
this.offset = pos;
};
BufferReader.prototype.remaining = function() {
return this.buffer.slice( this.offset );
};
BufferReader.prototype.available = function() {
return this.offset < this.buffer.length;
};
var makeDelegate = function( type, size ) {
if( type instanceof Object ) {
return Object.keys( type ).forEach( function( k ) {
makeDelegate( k, type[k] );
});
}
BufferReader.prototype[ 'read' + type ] = function( offset ) {
if( offset !== undefined )
this.offset = offset;
var value = this.buffer[ 'read' + type ]( this.offset );
this.offset += size;
return value;
};
};
var expandTypes = function( types /*, ... */ ) {
for( var i = 1; i < arguments.length; i++ ) {
var patterns = arguments[i];
var newTypes = [];
for( var t in types ) {
for( var p in patterns ) {
newTypes[ patterns[p].replace( '*', t ) ] = types[t];
}
}
types = newTypes;
}
return types;
};
// Types with Little and Big endian alternatives
makeDelegate(
expandTypes( {
Int16: 2,
Int32: 4
}, [ '*LE', '*BE' ], [ 'U*', '*' ] ) );
makeDelegate(
expandTypes( {
Int8: 1
}, [ 'U*', '*' ] ) );
makeDelegate(
expandTypes( {
Float: 4,
Double: 8
}, [ '*LE', '*BE' ] ) );
module.exports = BufferReader;