-
Notifications
You must be signed in to change notification settings - Fork 11
/
dataReader.js
124 lines (100 loc) · 2.53 KB
/
dataReader.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function DataReader(dataArray, offset, length, filename)
{
this.data = new Array();
this.filename = filename;
if (typeof dataArray !== 'undefined')
{
if (dataArray instanceof DataReader)
{
//- copy constructor
this.data = dataArray.data;
}
else
{
this.data = dataArray;
}
}
if (typeof offset === 'undefined') offset = 0;
if (typeof length === 'undefined') length = this.data.length - offset;
this.hiddenOffset = offset;
this.length = length;
this.pos = this.hiddenOffset;
var self = this;
/// Read one Byte from stream
this.readByte = function(offset)
{
if (typeof offset !== 'undefined') self.pos = offset + self.hiddenOffset;
var v = self.data[self.pos];
self.pos++;
return v;
}
/// Read one DWord (4 Byte) from stream (little ending)
this.readDWord = function()
{
var v = (self.data[self.pos]<<24) | (self.data[self.pos + 1]<<16) | (self.data[self.pos + 2]<<8) | (self.data[self.pos + 3]);
self.pos += 4;
return v;
}
/// Read one DWord (4 Byte) from stream (big ending)
this.readIntBE = function()
{
v = (self.data[self.pos]) | (self.data[self.pos + 1]<<8) | (self.data[self.pos + 2]<<16) | (self.data[self.pos + 3]<<24);
self.pos += 4;
return v;
}
/// Read one Word (2 Byte) from stream (big ending)
this.readWord = function()
{
v = (self.data[self.pos]<<8) | (self.data[self.pos + 1]);
self.pos += 2;
return v;
}
/// Read one Word (2 Byte) from stream (big ending)
this.readWordBE = function()
{
v = (self.data[self.pos]) | (self.data[self.pos + 1]<<8);
self.pos += 2;
return v;
}
/// Read [lenght] bytes and converts it to a number
this.readInt = function(length)
{
var v = 0;
for (var i = length; i > 0; i--)
{
v = (v<<8) | self.data[self.pos];
self.pos++;
}
return v;
}
/// Read a String
this.readString = function(length, offset)
{
if (typeof offset !== 'undefined') self.pos = offset + self.hiddenOffset;
var result = "";
for (var i = 0; i < length; i++)
{
v = self.data[self.pos];
self.pos++;
result += String.fromCharCode(v);
}
return result;
}
this.getOffset = function()
{
return self.pos - self.hiddenOffset;
}
this.setOffset = function(newPos)
{
self.pos = newPos + self.hiddenOffset;
}
this.eof = function()
{
var pos = self.pos - self.hiddenOffset;
return ((pos >= self.length) || (pos < 0));
}
this.reverse = function()
{
this.data.reverse();
}
}