source: trunk/termtv/js/decoder.js@ 18292

Last change on this file since 18292 was 17409, checked in by tbretz, 11 years ago
Enable strict mode.
File size: 3.9 KB
Line 
1'use strict';
2
3/**
4 * @class TTVDecoder
5 * @constructor
6 *
7 * Splits the stream written by ttyrec or termrec into chunks and
8 * interpreted the control data. Checks its validity.
9 *
10 * @returns {Array}
11 * An array with each entry (chunk) having the following
12 * properties is returned: time {Number}, len {Number} and data {String}.
13 *
14 * @throws An error is thrown when a length of a chunk is less than 0
15 * or when a chunks header points well below the end of the data.
16 */
17function TTVDecoder(contents)
18{
19 var out = [];
20
21 var pos = 0;
22 while (pos<contents.length)
23 {
24 var sec = this.r_uint32le(contents, pos);
25 var usec = this.r_uint32le(contents, pos+4);
26 var len = this.r_uint32le(contents, pos+8);
27
28 if (len<0)
29 throw new Error("The stream seems to be broken [chunk size < 0]");
30
31 if (len==0 && pos+12+len>contents.length)
32 break;
33
34 if (pos+12+len>contents.length)
35 throw new Error("The stream seems to be broken ["+len+";"+(pos+12+len)+">"+contents.length+"]");
36
37 var data = contents.substr(pos+12, len);
38
39 pos += len+12;
40
41 out.push({ time: sec + usec/1000000, len: len, data: this.fixHighCharCodes(data) });
42 }
43
44 return out;
45};
46
47/**
48 * Convert each four bytes into the correct byte order.
49 *
50 * @param {String} data
51 * The string from which the bytes are extracted
52 *
53 * @param {number} offset
54 * The index from which on the four bytes are evaluated
55 *
56 * @returns {number}
57 *
58 * @private
59 */
60TTVDecoder.prototype.r_uint32le = function(data, offset)
61{
62 var hh = (data.charCodeAt(offset+3) & 0xff) << 24;
63 var hl = (data.charCodeAt(offset+2) & 0xff) << 16;
64 var lh = (data.charCodeAt(offset+1) & 0xff) << 8;
65 var ll = (data.charCodeAt(offset) & 0xff);
66
67 return hh | hl | lh | ll;
68}
69
70/**
71 * Remove the upper byte from some char codes.
72 *
73 * @param {String} data
74 * The data stream for which the upper bytes should be removed
75 *
76 * @returns {string}
77 *
78 * @private
79 */
80TTVDecoder.prototype.fixHighCharCodes = function(data)
81{
82 var ch = "";
83 for (var i = 0; i < data.length; i++)
84 ch += String.fromCharCode( data.charCodeAt(i) & 0xff );
85 return ch;
86}
87/*
88function uint32le(stream, pos)
89{
90 var hh = (stream.charCodeAt(pos+3) & 0xff) << 24;
91 var hl = (stream.charCodeAt(pos+2) & 0xff) << 16;
92 var lh = (stream.charCodeAt(pos+1) & 0xff) << 8;
93 var ll = (stream.charCodeAt(pos) & 0xff);
94
95 return hh | hl | lh | ll;
96}
97
98function extract(data, pos, len)
99{
100 var str = "";
101 for (var i=pos; i<pos+len; i++)
102 str += String.fromCharCode(data.charCodeAt(i)&0xff);
103 return str;
104}
105
106function getChunk(stream, pos)
107{
108 if (pos==stream.length)
109 return;
110
111 if (pos===undefined)
112 pos = 0;
113
114 var sec = uint32le(stream, pos);
115 var usec = uint32le(stream, pos+4);
116 var len = uint32le(stream, pos+8);
117
118 if (len<0)
119 throw new Error("The stream seems to be broken [pos="+pos+"; len="+len+"]");
120
121 if (len==0 && pos+12>stream.length)
122 return;
123
124 if (pos+12+len>stream.length)
125 throw new Error("The stream seems to be broken [pos="+pos+"; len="+len+"; LEN="+stream.length+"]");
126
127 var ret = { };
128 ret.time = sec+usec/1000000;
129 ret.pos = pos+12+len;
130 ret.data = extract(stream, pos+12, len);
131 return ret;
132};
133
134
135function getChunkHeader(stream, pos)
136{
137 if (pos==stream.length)
138 return;
139
140 if (pos===undefined)
141 pos = 0;
142
143 var sec = uint32le(stream, pos);
144 var usec = uint32le(stream, pos+4);
145 var len = uint32le(stream, pos+8);
146
147 if (len<0)
148 throw new Error("The stream seems to be broken [pos="+pos+"; len="+len+"]");
149
150 if (len==0 && pos+12>stream.length)
151 return;
152
153 if (pos+12+len>stream.length)
154 throw new Error("The stream seems to be broken [pos="+pos+"; len="+len+"; LEN="+stream.length+"]");
155
156 var ret = { };
157 ret.time = sec+usec/1000000;
158 ret.pos = pos+12+len;
159 return ret;
160};
161
162*/
Note: See TracBrowser for help on using the repository browser.