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

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