|
| 1 | +var btoa = this.btoa || require('./btoa'); |
1 | 2 |
|
2 |
| -BinaryFile = function(strData, iDataOffset, iDataLength) { |
3 |
| - var data = strData; |
4 |
| - var dataOffset = iDataOffset || 0; |
5 |
| - var dataLength = 0; |
| 3 | +// these are created (dynamically!) via |
| 4 | +// a <script type="text/vbscript"> insertion. |
| 5 | +var IEBinary_getLength = this.IEBinary_getLength |
| 6 | + , IEBinary_getByteAt = this.IEBinary_getByteAt; |
6 | 7 |
|
7 |
| - this.getRawData = function() { |
8 |
| - return data; |
9 |
| - } |
10 |
| - |
11 |
| - this.slice = function(begin, optionalEnd) { |
12 |
| - var arr = []; |
13 |
| - var end = optionalEnd || this.getLength(); |
14 |
| - var i; |
15 |
| - for (i = begin; i < end; i++) { |
16 |
| - arr.push(this.getByteAt(i)); |
17 |
| - } |
18 |
| - return arr; |
19 |
| - } |
| 8 | +BinaryFile = function(strData, iDataOffset, iDataLength) { |
| 9 | + this.data = strData; |
| 10 | + this.dataOffset = iDataOffset || 0; |
| 11 | + this.dataLength = 0; |
20 | 12 |
|
21 | 13 | if (typeof strData == "string") {
|
22 |
| - dataLength = iDataLength || data.length; |
23 |
| - |
24 |
| - this.getByteAt = function(iOffset) { |
25 |
| - return data.charCodeAt(iOffset + dataOffset) & 0xFF; |
26 |
| - } |
| 14 | + this.dataLength = iDataLength || this.data.length; |
| 15 | + this.getByteAt = this.getByteAt_CharCode; |
27 | 16 | } else if (typeof strData == "unknown") {
|
28 |
| - dataLength = iDataLength || IEBinary_getLength(data); |
29 |
| - |
30 |
| - this.getByteAt = function(iOffset) { |
31 |
| - return IEBinary_getByteAt(data, iOffset + dataOffset); |
32 |
| - } |
| 17 | + this.dataLength = iDataLength || IEBinary_getLength(this.data); |
| 18 | + this.getByteAt = this.getByteAt_IEBinary; |
33 | 19 | } else {
|
34 |
| - throw(Error("BinaryFile was given a " + (typeof strData) + " and doesn't know what to do with it")) |
| 20 | + throw new Error( |
| 21 | + "BinaryFile was given a " + (typeof strData) + " and doesn't know what to do with it" |
| 22 | + ); |
35 | 23 | }
|
| 24 | +}; |
36 | 25 |
|
37 |
| - this.getLength = function() { |
38 |
| - return dataLength; |
39 |
| - } |
40 |
| - |
41 |
| - this.getSByteAt = function(iOffset) { |
42 |
| - var iByte = this.getByteAt(iOffset); |
43 |
| - if (iByte > 127) |
44 |
| - return iByte - 256; |
45 |
| - else |
46 |
| - return iByte; |
47 |
| - } |
48 |
| - |
49 |
| - this.getShortAt = function(iOffset, bBigEndian) { |
50 |
| - var iShort = bBigEndian ? |
51 |
| - (this.getByteAt(iOffset) << 8) + this.getByteAt(iOffset + 1) |
52 |
| - : (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset) |
53 |
| - if (iShort < 0) iShort += 65536; |
54 |
| - return iShort; |
55 |
| - } |
56 |
| - this.getSShortAt = function(iOffset, bBigEndian) { |
57 |
| - var iUShort = this.getShortAt(iOffset, bBigEndian); |
58 |
| - if (iUShort > 32767) |
59 |
| - return iUShort - 65536; |
60 |
| - else |
61 |
| - return iUShort; |
62 |
| - } |
63 |
| - this.getLongAt = function(iOffset, bBigEndian) { |
64 |
| - var iByte1 = this.getByteAt(iOffset), |
65 |
| - iByte2 = this.getByteAt(iOffset + 1), |
66 |
| - iByte3 = this.getByteAt(iOffset + 2), |
67 |
| - iByte4 = this.getByteAt(iOffset + 3); |
68 |
| - |
69 |
| - var iLong = bBigEndian ? |
70 |
| - (((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4 |
71 |
| - : (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; |
72 |
| - if (iLong < 0) iLong += 4294967296; |
73 |
| - return iLong; |
74 |
| - } |
75 |
| - this.getSLongAt = function(iOffset, bBigEndian) { |
76 |
| - var iULong = this.getLongAt(iOffset, bBigEndian); |
77 |
| - if (iULong > 2147483647) |
78 |
| - return iULong - 4294967296; |
79 |
| - else |
80 |
| - return iULong; |
81 |
| - } |
82 |
| - this.getStringAt = function(iOffset, iLength) { |
83 |
| - var aStr = []; |
84 |
| - for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) { |
85 |
| - aStr[j] = String.fromCharCode(this.getByteAt(i)); |
86 |
| - } |
87 |
| - return aStr.join(""); |
88 |
| - } |
89 |
| - |
90 |
| - this.getCharAt = function(iOffset) { |
91 |
| - return String.fromCharCode(this.getByteAt(iOffset)); |
92 |
| - } |
93 |
| - this.toBase64 = function() { |
94 |
| - return window.btoa(data); |
95 |
| - } |
96 |
| - this.fromBase64 = function(strBase64) { |
97 |
| - data = window.atob(strBase64); |
98 |
| - } |
| 26 | +BinaryFile.prototype.getByteAt_CharCode = function(iOffset) { |
| 27 | + return this.data.charCodeAt(iOffset + this.dataOffset) & 0xFF; |
99 | 28 | };
|
| 29 | + |
| 30 | +BinaryFile.prototype.getByteAt_IEBinary = function(iOffset) { |
| 31 | + return IEBinary_getByteAt(this.data, iOffset + this.dataOffset); |
| 32 | +}; |
| 33 | + |
| 34 | +BinaryFile.prototype.getRawData = function() { |
| 35 | + return this.data; |
| 36 | +} |
| 37 | + |
| 38 | +BinaryFile.prototype.slice = function(begin, optionalEnd) { |
| 39 | + var arr = []; |
| 40 | + var end = optionalEnd || this.getLength(); |
| 41 | + var i; |
| 42 | + for (i = begin; i < end; i++) { |
| 43 | + arr.push(this.getByteAt(i)); |
| 44 | + } |
| 45 | + return arr; |
| 46 | +} |
| 47 | + |
| 48 | +BinaryFile.prototype.getLength = function() { |
| 49 | + return this.dataLength; |
| 50 | +} |
| 51 | + |
| 52 | +BinaryFile.prototype.getSByteAt = function(iOffset) { |
| 53 | + var iByte = this.getByteAt(iOffset); |
| 54 | + if (iByte > 127) |
| 55 | + return iByte - 256; |
| 56 | + else |
| 57 | + return iByte; |
| 58 | +} |
| 59 | + |
| 60 | +BinaryFile.prototype.getShortAt = function(iOffset, bBigEndian) { |
| 61 | + var iShort = bBigEndian ? |
| 62 | + (this.getByteAt(iOffset) << 8) + this.getByteAt(iOffset + 1) |
| 63 | + : (this.getByteAt(iOffset + 1) << 8) + this.getByteAt(iOffset) |
| 64 | + if (iShort < 0) iShort += 65536; |
| 65 | + return iShort; |
| 66 | +} |
| 67 | + |
| 68 | +BinaryFile.prototype.getSShortAt = function(iOffset, bBigEndian) { |
| 69 | + var iUShort = this.getShortAt(iOffset, bBigEndian); |
| 70 | + if (iUShort > 32767) |
| 71 | + return iUShort - 65536; |
| 72 | + else |
| 73 | + return iUShort; |
| 74 | +} |
| 75 | + |
| 76 | +BinaryFile.prototype.getLongAt = function(iOffset, bBigEndian) { |
| 77 | + var iByte1 = this.getByteAt(iOffset), |
| 78 | + iByte2 = this.getByteAt(iOffset + 1), |
| 79 | + iByte3 = this.getByteAt(iOffset + 2), |
| 80 | + iByte4 = this.getByteAt(iOffset + 3); |
| 81 | + |
| 82 | + var iLong = bBigEndian ? |
| 83 | + (((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4 |
| 84 | + : (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1; |
| 85 | + if (iLong < 0) iLong += 4294967296; |
| 86 | + return iLong; |
| 87 | +} |
| 88 | + |
| 89 | +BinaryFile.prototype.getSLongAt = function(iOffset, bBigEndian) { |
| 90 | + var iULong = this.getLongAt(iOffset, bBigEndian); |
| 91 | + if (iULong > 2147483647) |
| 92 | + return iULong - 4294967296; |
| 93 | + else |
| 94 | + return iULong; |
| 95 | +} |
| 96 | + |
| 97 | +BinaryFile.prototype.getStringAt = function(iOffset, iLength) { |
| 98 | + var aStr = []; |
| 99 | + for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) { |
| 100 | + aStr[j] = String.fromCharCode(this.getByteAt(i)); |
| 101 | + } |
| 102 | + return aStr.join(""); |
| 103 | +} |
| 104 | + |
| 105 | +BinaryFile.prototype.getCharAt = function(iOffset) { |
| 106 | + return String.fromCharCode(this.getByteAt(iOffset)); |
| 107 | +} |
| 108 | + |
| 109 | +BinaryFile.prototype.toBase64 = function() { |
| 110 | + return btoa(this.data); |
| 111 | +} |
| 112 | + |
| 113 | +BinaryFile.prototype.fromBase64 = function(strBase64) { |
| 114 | + data = window.atob(strBase64); |
| 115 | +} |
| 116 | + |
| 117 | +module.exports = exports = BinaryFile; |
0 commit comments