Repository navigation
Expand file tree
/
Copy pathbyte_tools.js
More file actions
164 lines (142 loc) · 3.96 KB
/
Copy pathbyte_tools.js
File metadata and controls
164 lines (142 loc) · 3.96 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
161
162
163
164
/**
* @fileoverview General-purpose Byte Tools.
*
* @license Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author mbordihn@google.com (Markus Bordihn)
*/
goog.provide('cwc.utils.ByteTools');
/**
* @param {Object} data
* @return {number}
*/
cwc.utils.ByteTools.bytesToInt = function(data) {
return data[0] << 8 | data[1];
};
/**
* @param {Object} data
* @return {number}
*/
cwc.utils.ByteTools.signedBytesToInt = function(data) {
return (cwc.utils.ByteTools.bytesToInt(data) << 16) >> 16;
};
/**
* @param {!Array} data
* @return {!Uint8Array}
*/
cwc.utils.ByteTools.toUint8Array = function(data) {
var dataLength = data.length;
var dataBuffer = new Uint8Array(dataLength);
for (var i=0; i < dataLength; i++) {
dataBuffer[i] = data[i];
}
return dataBuffer;
};
/**
* @param {!Uint8Array} data1
* @param {!Uint8Array} data2
* @return {!Uint8Array}
*/
cwc.utils.ByteTools.joinUint8Array = function(data1, data2) {
var data = new Uint8Array(data1.length + data2.length);
data.set(data1, 0);
data.set(data2, data1.length);
return data;
};
/**
* @param {ArrayBuffer|Uint8Array} data
* @param {Array=} opt_headers
* @param {number=} opt_size
* @param {ArrayBuffer|Uint8Array} opt_buffer
* @return {Uint8Array} result
*/
cwc.utils.ByteTools.getUint8Data = function(data,
opt_headers, opt_size, opt_buffer) {
if (!data) {
return null;
}
var dataView = data;
if (dataView instanceof ArrayBuffer) {
dataView = new Uint8Array(data);
}
// Data processing for data without headers.
if (!opt_headers && !opt_size) {
return dataView;
}
// Additional length checks if needed.
if (opt_size) {
// Perpend buffer if needed.
if (opt_buffer && dataView.length < opt_size) {
var buffer = opt_buffer;
if (opt_buffer instanceof ArrayBuffer) {
buffer = new Uint8Array(opt_buffer);
}
dataView = cwc.utils.ByteTools.joinUint8Array(buffer, dataView);
}
if (dataView.length < opt_size) {
return null;
}
}
// Data processing for data with headers.
if (opt_headers) {
var headerPosition = cwc.utils.ByteTools.getHeaderPosition(dataView,
opt_headers);
if (headerPosition === null) {
return null;
}
if (headerPosition !== 0) {
dataView = dataView.slice(headerPosition);
}
}
// Double check packet size to ignore chunks.
if (opt_size && dataView.length < opt_size) {
return null;
}
return dataView;
};
/**
* Returns the header position in the given data stream.
* @param {Uint8Array|Array} data
* @param {Array} headers
* @return {number}
*/
cwc.utils.ByteTools.getHeaderPosition = function(data, headers) {
var headerPos = null;
if (!data || !headers || data.length <= headers.length) {
return headerPos;
}
var dataLen = data.length - 1;
var headerLen = headers.length;
var searchPos = data.indexOf(headers[0]);
if (headerLen >= 2) {
for (;searchPos !== -1; searchPos = data.indexOf(headers[0], searchPos)) {
var foundHeaders = true;
for (var i = 0; i < headerLen; i++) {
if (data[searchPos + i] !== headers[i]) {
foundHeaders = false;
}
}
if (foundHeaders && searchPos + headerLen <= dataLen) {
headerPos = searchPos;
break;
} else {
searchPos++;
}
}
} else if (headerLen == 1 && searchPos !== -1 && searchPos !== dataLen) {
headerPos = searchPos;
}
return headerPos;
};