-
Notifications
You must be signed in to change notification settings - Fork 3
/
files.js
104 lines (104 loc) · 2.07 KB
/
files.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
/**
* @constructor
*/
function GFile(data)
{
this.set=0;
this.cur=1;
this.end=2;
this.data=data;
this.length=data.length;
this.pos=0;
this.bit=0;
}
GFile.prototype.eof=function()
{
return this.pos>=this.length;
}
GFile.prototype.r8=function()
{
return this.data.charCodeAt(this.pos++)&0xff;
}
GFile.prototype.r16=function()
{
var v=(this.data.charCodeAt(this.pos++)&0xff)<<8;
v|=this.data.charCodeAt(this.pos++)&0xff;
return v;
}
GFile.prototype.r16le=function()
{
var v=this.data.charCodeAt(this.pos++)&0xff;
v|=(this.data.charCodeAt(this.pos++)&0xff)<<8;
return v;
}
GFile.prototype.r24=function()
{
var v=(this.data.charCodeAt(this.pos++)&0xff)<<16;
v|=(this.data.charCodeAt(this.pos++)&0xff)<<8;
v|=this.data.charCodeAt(this.pos++)&0xff;
return v;
}
GFile.prototype.r32=function()
{
var v=(this.data.charCodeAt(this.pos++)&0xff)<<24;
v|=(this.data.charCodeAt(this.pos++)&0xff)<<16;
v|=(this.data.charCodeAt(this.pos++)&0xff)<<8;
v|=this.data.charCodeAt(this.pos++)&0xff;
return v;
}
GFile.prototype.r32le=function()
{
var v=this.data.charCodeAt(this.pos++)&0xff;
v|=(this.data.charCodeAt(this.pos++)&0xff)<<8;
v|=(this.data.charCodeAt(this.pos++)&0xff)<<16;
v|=(this.data.charCodeAt(this.pos++)&0xff)<<24;
return v;
}
GFile.prototype.r4=function()
{
var v=this.data.substring(this.pos,this.pos+4);
this.pos+=4;
return v;
}
GFile.prototype.peek32=function()
{
var v=(this.data.charCodeAt(this.pos)&0xff)<<24;
v|=(this.data.charCodeAt(this.pos+1)&0xff)<<16;
v|=(this.data.charCodeAt(this.pos+2)&0xff)<<8;
v|=this.data.charCodeAt(this.pos+3)&0xff;
return v;
}
GFile.prototype.seek=function(newpos,rel)
{
switch (rel)
{
case this.set:
break;
case this.cur:
newpos+=this.pos;
break;
case this.end:
newpos+=this.length;
break;
}
this.pos=newpos;
}
GFile.prototype.read=function(len)
{
var oldpos=this.pos;
this.pos+=len;
return this.data.substring(oldpos,this.pos);
}
GFile.prototype.bits=function(num)
{
var v=this.peek32();
v>>>=(32-this.bit)-num;
v&=(1<<num)-1;
this.bit+=num;
if (this.bit&0x10)
{
this.bit&=0xf;
this.pos+=2;
}
return v;
}