forked from docmarionum1/jsAtari
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinFileReader.js
More file actions
160 lines (128 loc) · 3.58 KB
/
BinFileReader.js
File metadata and controls
160 lines (128 loc) · 3.58 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* BinFileReader.js
* You can find more about this function at
* http://nagoon97.com/reading-binary-files-using-ajax/
*
* Copyright (c) 2008 Andy G.P. Na <nagoon97@naver.com>
* The source code is freely distributable under the terms of an MIT-style license.
*/
function BinFileReader(fileURL){
var _exception = {};
_exception.FileLoadFailed = 1;
_exception.EOFReached = 2;
var filePointer = 0;
var fileSize = -1;
var fileContents;
this.getFileSize = function(){
return fileSize;
}
this.getFilePointer = function(){
return filePointer;
}
this.movePointerTo = function(iTo){
if(iTo < 0) filePointer = 0;
else if(iTo > this.getFileSize()) throwException(_exception.EOFReached);
else filePointer = iTo;
return filePointer;
};
this.movePointer = function(iDirection){
this.movePointerTo(filePointer + iDirection);
return filePointer;
};
this.readNumber = function(iNumBytes, iFrom){
iNumBytes = iNumBytes || 1;
iFrom = iFrom || filePointer;
this.movePointerTo(iFrom + iNumBytes);
var result = 0;
for(var i=iFrom + iNumBytes; i>iFrom; i--){
result = result * 256 + this.readByteAt(i-1);
}
return result;
};
this.readString = function(iNumChars, iFrom){
iNumChars = iNumChars || 1;
iFrom = iFrom || filePointer;
this.movePointerTo(iFrom);
var result = "";
var tmpTo = iFrom + iNumChars;
for(var i=iFrom; i<tmpTo; i++){
result += String.fromCharCode(this.readNumber(1));
}
return result;
};
this.readUnicodeString = function(iNumChars, iFrom){
iNumChars = iNumChars || 1;
iFrom = iFrom || filePointer;
this.movePointerTo(iFrom);
var result = "";
var tmpTo = iFrom + iNumChars*2;
for(var i=iFrom; i<tmpTo; i+=2){
result += String.fromCharCode(this.readNumber(2));
}
return result;
};
function throwException(errorCode){
switch(errorCode){
case _exception.FileLoadFailed:
throw new Error('Error: Filed to load "'+fileURL+'"');
break;
case _exception.EOFReached:
throw new Error("Error: EOF reached");
break;
}
}
function BinFileReaderImpl_IE(fileURL){
var vbArr = BinFileReaderImpl_IE_VBAjaxLoader(fileURL);
fileContents = vbArr.toArray();
fileSize = fileContents.length-1;
if(fileSize < 0) throwException(_exception.FileLoadFailed);
this.readByteAt = function(i){
return fileContents[i];
}
}
function BinFileReaderImpl(fileURL){
var req = new XMLHttpRequest();
req.open('GET', fileURL, false);
//XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
req.overrideMimeType('text/plain; charset=x-user-defined');
req.send(null);
if (req.status != 200) throwException(_exception.FileLoadFailed);
fileContents = req.responseText;
fileSize = fileContents.length;
this.readByteAt = function(i){
return fileContents.charCodeAt(i) & 0xff;
}
}
if(/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent))
BinFileReaderImpl_IE.apply(this, [fileURL]);
else
BinFileReaderImpl.apply(this, [fileURL]);
}
document.write('<script type="text/vbscript">\n\
Function BinFileReaderImpl_IE_VBAjaxLoader(fileName)\n\
Dim xhr\n\
Set xhr = CreateObject("Microsoft.XMLHTTP")\n\
\n\
xhr.Open "GET", fileName, False\n\
\n\
xhr.setRequestHeader "Accept-Charset", "x-user-defined"\n\
xhr.send\n\
\n\
Dim byteArray()\n\
\n\
if xhr.Status = 200 Then\n\
Dim byteString\n\
Dim i\n\
\n\
byteString=xhr.responseBody\n\
\n\
ReDim byteArray(LenB(byteString))\n\
\n\
For i = 1 To LenB(byteString)\n\
byteArray(i-1) = AscB(MidB(byteString, i, 1))\n\
Next\n\
End If\n\
\n\
BinFileReaderImpl_IE_VBAjaxLoader=byteArray\n\
End Function\n\
</script>');