-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_reader.js
116 lines (89 loc) · 2.66 KB
/
binary_reader.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
/** @fileoverview Reads binary data. */
/**
* @param {!ArrayBuffer} arraybuffer The binary data to read.
* @constructor
*/
keepasschrome.BinaryReader = function(arraybuffer) {
/** @private @const {!Uint8Array} */
this.data_ = new Uint8Array(arraybuffer);
/** @private {number} */
this.pos_ = 0;
};
/** @return {boolean} True if there's at least one more byte. */
keepasschrome.BinaryReader.prototype.hasNextByte = function() {
return this.pos_ < this.data_.length;
};
/** @return {boolean} True if there's at least one more int. */
keepasschrome.BinaryReader.prototype.hasNextInt = function() {
return this.pos_ < this.data_.length - 3;
};
/**
* @return {number} The next byte.
* @private
*/
keepasschrome.BinaryReader.prototype.readByte_ = function() {
if (!this.hasNextByte()) {
throw new RangeError();
}
return this.data_[this.pos_++];
};
/**
* @param {number} num The number of bytes to read.
* @return {!Uint8Array} The bytes.
*/
keepasschrome.BinaryReader.prototype.readBytes = function(num) {
var bytes = this.data_.subarray(this.pos_, this.pos_ + num);
this.pos_ += num;
return bytes;
};
/**
* @param {number} numBytes The numbers of bytes to read.
* @return {number} The number.
* @private
*/
keepasschrome.BinaryReader.prototype.readNumber_ = function(numBytes) {
var bytes = this.readBytes(numBytes);
var result = 0;
for (var i = bytes.length - 1; i >= 0; i--) {
result = (result * 256) + bytes[i];
}
return result;
};
/** @return {number} The short. */
keepasschrome.BinaryReader.prototype.readShort = function() {
return this.readNumber_(2);
};
/** @return {number} The int. */
keepasschrome.BinaryReader.prototype.readInt = function() {
return this.readNumber_(4);
};
/** @return {!Uint8Array} The bytes. */
keepasschrome.BinaryReader.prototype.readRest = function() {
var bytes = this.data_.subarray(this.pos_);
this.pos_ = this.data_.length;
return bytes;
};
/**
* Reads in a null-terminated string.
* @return {string} The string;
*/
keepasschrome.BinaryReader.prototype.readString = function() {
var result = '';
var b = this.readByte_();
while (b != 0) {
result += String.fromCharCode(b);
b = this.readByte_();
}
return result;
};
/** @return {!Date} The date. */
keepasschrome.BinaryReader.prototype.readDate = function() {
var bytes = this.readBytes(5);
var year = (bytes[0] << 6) | (bytes[1] >> 2);
var month = ((bytes[1] & 3) << 2) | (bytes[2] >> 6);
var day = (bytes[2] >> 1) & 31;
var hour = ((bytes[2] & 1) << 4) | (bytes[3] >> 4);
var min = ((bytes[3] & 15) << 2) | (bytes[4] >> 6);
var sec = bytes[4] & 63;
return new Date(year, month - 1, day, hour, min, sec);
};