~usmteam/usm/usm

« back to all changes in this revision

Viewing changes to usr/share/usm/themes/web/js/webgl/binaryReader.js

  • Committer: Oliver Marks
  • Date: 2011-08-04 19:35:12 UTC
  • Revision ID: oly@digitaloctave.com-20110804193512-mnd9qkqpd06wrl98
changes to file uploader

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// BinaryReader
 
3
// Refactored by Vjeux <vjeuxx@gmail.com>
 
4
// http://blog.vjeux.com/2010/javascript/javascript-binary-reader.html
 
5
 
 
6
// Original
 
7
//+ Jonas Raoni Soares Silva
 
8
//@ http://jsfromhell.com/classes/binary-parser [rev. #1]
 
9
 
 
10
BinaryReader = function (data) {
 
11
        this._buffer = data;
 
12
        this._pos = 0;
 
13
};
 
14
 
 
15
BinaryReader.prototype = {
 
16
 
 
17
        /* Public */
 
18
 
 
19
        readInt8:       function (){ return this._decodeInt(8, true); },
 
20
        readUInt8:      function (){ return this._decodeInt(8, false); },
 
21
        readInt16:      function (){ return this._decodeInt(16, true); },
 
22
        readUInt16:     function (){ return this._decodeInt(16, false); },
 
23
        readInt32:      function (){ return this._decodeInt(32, true); },
 
24
        readUInt32:     function (){ return this._decodeInt(32, false); },
 
25
 
 
26
        readFloat:      function (){ return this._decodeFloat(23, 8); },
 
27
        readDouble:     function (){ return this._decodeFloat(52, 11); },
 
28
        
 
29
        readChar:       function () { return this.readString(1); },
 
30
        readString: function (length) {
 
31
                this._checkSize(length * 8);
 
32
                var result = this._buffer.substr(this._pos, length);
 
33
                this._pos += length;
 
34
                return result;
 
35
        },
 
36
 
 
37
        seek: function (pos) {
 
38
                this._pos = pos;
 
39
                this._checkSize(0);
 
40
        },
 
41
        
 
42
        getPosition: function () {
 
43
                return this._pos;
 
44
        },
 
45
        
 
46
        getSize: function () {
 
47
                return this._buffer.length;
 
48
        },
 
49
 
 
50
 
 
51
        /* Private */
 
52
        
 
53
        _decodeFloat: function(precisionBits, exponentBits){
 
54
                var length = precisionBits + exponentBits + 1;
 
55
                var size = length >> 3;
 
56
                this._checkSize(length);
 
57
 
 
58
                var bias = Math.pow(2, exponentBits - 1) - 1;
 
59
                var signal = this._readBits(precisionBits + exponentBits, 1, size);
 
60
                var exponent = this._readBits(precisionBits, exponentBits, size);
 
61
                var significand = 0;
 
62
                var divisor = 2;
 
63
                var curByte = 0; //length + (-precisionBits >> 3) - 1;
 
64
                do {
 
65
                        var byteValue = this._readByte(++curByte, size);
 
66
                        var startBit = precisionBits % 8 || 8;
 
67
                        var mask = 1 << startBit;
 
68
                        while (mask >>= 1) {
 
69
                                if (byteValue & mask) {
 
70
                                        significand += 1 / divisor;
 
71
                                }
 
72
                                divisor *= 2;
 
73
                        }
 
74
                } while (precisionBits -= startBit);
 
75
 
 
76
                this._pos += size;
 
77
 
 
78
                return exponent == (bias << 1) + 1 ? significand ? NaN : signal ? -Infinity : +Infinity
 
79
                        : (1 + signal * -2) * (exponent || significand ? !exponent ? Math.pow(2, -bias + 1) * significand
 
80
                        : Math.pow(2, exponent - bias) * (1 + significand) : 0);
 
81
        },
 
82
 
 
83
        _decodeInt: function(bits, signed){
 
84
                var x = this._readBits(0, bits, bits / 8), max = Math.pow(2, bits);
 
85
                var result = signed && x >= max / 2 ? x - max : x;
 
86
 
 
87
                this._pos += bits / 8;
 
88
                return result;
 
89
        },
 
90
 
 
91
        //shl fix: Henri Torgemane ~1996 (compressed by Jonas Raoni)
 
92
        _shl: function (a, b){
 
93
                for (++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
 
94
                return a;
 
95
        },
 
96
        
 
97
        _readByte: function (i, size) {
 
98
                return this._buffer.charCodeAt(this._pos + size - i - 1) & 0xff;
 
99
        },
 
100
 
 
101
        _readBits: function (start, length, size) {
 
102
                var offsetLeft = (start + length) % 8;
 
103
                var offsetRight = start % 8;
 
104
                var curByte = size - (start >> 3) - 1;
 
105
                var lastByte = size + (-(start + length) >> 3);
 
106
                var diff = curByte - lastByte;
 
107
 
 
108
                var sum = (this._readByte(curByte, size) >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1);
 
109
 
 
110
                if (diff && offsetLeft) {
 
111
                        sum += (this._readByte(lastByte++, size) & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight; 
 
112
                }
 
113
 
 
114
                while (diff) {
 
115
                        sum += this._shl(this._readByte(lastByte++, size), (diff-- << 3) - offsetRight);
 
116
                }
 
117
 
 
118
                return sum;
 
119
        },
 
120
 
 
121
        _checkSize: function (neededBits) {
 
122
                if (!(this._pos + Math.ceil(neededBits / 8) < this._buffer.length)) {
 
123
                        console.log(this._buffer.length);
 
124
                        throw new Error("Index out of bound");
 
125
                }
 
126
        }
 
127
};