-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataDecoders.js
323 lines (282 loc) · 13.6 KB
/
DataDecoders.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// This file is part of DQX - (C) Copyright 2014, Paul Vauterin, Ben Jeffery, Alistair Miles <[email protected]>
// This program is free software licensed under the GNU Affero General Public License.
// You can find a copy of this license in LICENSE in the top directory of the source code or at <http://opensource.org/licenses/AGPL-3.0>
/************************************************************************************************************************************
*************************************************************************************************************************************
Contains codecs that are used to efficiently communicate with the DQXServer counterpart
*************************************************************************************************************************************
*************************************************************************************************************************************/
define(["jquery", "DQX/Utils"],
function ($, DQX) {
var DataDecoders = {}
/////////////////////////////////////////////////////////////////////////////////////////
//Basic base64 encoding/decoding
/////////////////////////////////////////////////////////////////////////////////////////
DataDecoders.B64 = function () {
var that = {};
that.encodestr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
that.invencode = [];
for (var i = 0; i < 255; i++) that.invencode.push(0);
for (var i = 0; i < that.encodestr.length; i++)
that.invencode[that.encodestr[i].charCodeAt(0)] = i;
//Converts a 64bit encoded integer to an integer
that.B642Int = function (st) {
rs = 0;
for (var i = 0; i < st.length; i++)
rs = (rs << 6) + this.invencode[st.charCodeAt(i)]
return rs;
}
//Converts a 64bit encoded integer to an integer
that.B642IntFixed = function (st, offset, len) {
if (st[offset] == '~')
return null;
rs = 0;
for (var i = 0; i < len; i++)
rs = (rs << 6) + this.invencode[st.charCodeAt(offset + i)]
return rs;
}
//Converts a set of 64bit encoded integers to float array, applying a linear mapping using slope and offset
that.arrayB642Float = function (st, bytecount, slope, offset) {
var vals = [];
var cnt = st.length / bytecount;
var ps = 0;
for (var i = 0; i < cnt; i++) {
if ((st[ps] == '~') || (st[ps] == '#')) {//coding for absent value
vals.push(null);
ps += bytecount;
}
else {
var rs = 0;
for (var j = 0; j < bytecount; j++) {
rs = (rs << 6) + this.invencode[st.charCodeAt(ps)];
ps++;
}
//rs = Math.random()*Math.pow(64,bytecount);//!!!
vals.push(rs * slope + offset);
}
}
return vals;
}
return that;
}
/////////////////////////////////////////////////////////////////////////////////////////
//Decoder for different formats of value lists as provided by the server
/////////////////////////////////////////////////////////////////////////////////////////
DataDecoders.ValueListDecoder = function () {
var that = {};
that.b64codec = DataDecoders.B64();
that.doDecode = function (data) {
if (data['Encoding'] == 'IntegerDiffB64') {
if (data['Data'].length == 0) return [];
var vals = [];
var offset = data['Offset'];
var datastrlist = data['Data'].split(',');
for (var i = 0; i < datastrlist.length; i++) {
offset += this.b64codec.B642Int(datastrlist[i]);
vals.push(offset);
}
return vals;
}
if (data['Encoding'] == 'IntegerB64') {
if (data['Data'].length == 0) return [];
var vals = [];
var datastrlist = data['Data'].split(',');
for (var i = 0; i < datastrlist.length; i++)
vals.push(this.b64codec.B642Int(datastrlist[i]));
return vals;
}
if (data['Encoding'] == 'FloatAsIntB64') {
if (data['Data'].length == 0) return [];
var offset = data['Offset'];
var slope = data['Slope'];
var bytecount = data['ByteCount'];
var datastr = data['Data'];
var vals = this.b64codec.arrayB642Float(datastr, bytecount, slope, offset);
return vals;
}
if (data['Encoding'] == 'FloatAsH') {
if (data['Data'].length == 0) return [];
var vals = [];
var datastrlist = data['Data'].split(',');
for (var i = 0; i < datastrlist.length; i++) {
if (datastrlist[i]!='~')
vals.push(parseFloat(datastrlist[i]));
else
vals.push(null);
}
return vals;
}
if (data['Encoding'] == 'Integer') {
if (data['Data'].length == 0) return [];
var vals = [];
var datastrlist = data['Data'].split(',');
for (var i = 0; i < datastrlist.length; i++) {
vals.push(parseInt(datastrlist[i]));
}
return vals;
}
if (data['Encoding'] == 'String') {
if (data['Data'].length == 0) return [];
var vals = data['Data'].split('~');
return vals;
}
DQX.reportError("Unknown value list encoding: " + data['Encoding']);
}
return that;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Decoder/encoder factory
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DataDecoders.Encoder = {};
DataDecoders.Encoder.FixedString = function (info) {
var that = {};
DQX.checkIsNumber(info.Len);
that.length = parseFloat(info.Len);
that.decodeArray = function (datastr) {
var rs = [];
var ct = datastr.length / this.length;
for (var i = 0; i < ct; i++)
rs.push(datastr[i * this.length, (i + 1) * this.length])
return rs;
}
that.decodeSingle = function (datastr, offset) {
return datastr.substring(offset, offset + this.length);
}
that.getRecordLength = function () { return this.length; };
return that;
}
DataDecoders.Encoder.Boolean = function (info) {
var that = {};
that.decodeArray = function (datastr) {
var rs = [];
var ct = datastr.length;
for (var i = 0; i < ct; i++)
rs.push(datastr[i] == '1')
return rs;
}
that.decodeSingle = function (datastr, offset) {
return datastr[offset] == '1';
}
that.getRecordLength = function () { return 1; };
return that;
}
DataDecoders.Encoder.Int2B64 = function (info) {
var that = {};
DQX.checkIsNumber(info.Len);
that.length = parseFloat(info.Len);
var _b64codec = DataDecoders.B64();
that.decodeSingle = function (datastr, offset) {
return _b64codec.B642IntFixed(datastr, offset, this.length);
}
that.getRecordLength = function () { return this.length; };
return that;
}
DataDecoders.Encoder.Float2B64 = function (info) {
var that = {};
DQX.checkIsNumber(info.Len); DQX.checkIsNumber(info.Offset); DQX.checkIsNumber(info.Slope);
that.offset = parseFloat(info.Offset);
that.slope = parseFloat(info.Slope);
that.length = parseFloat(info.Len);
var _b64codec = DataDecoders.B64();
that.decodeArray = function (datastr) {
return _b64codec.arrayB642Float(datastr, this.length, this.slope, this.offset);
}
that.decodeSingle = function (datastr, offset) {
return _b64codec.B642IntFixed(datastr, offset, this.length) * this.slope + this.offset;
}
that.getRecordLength = function () { return this.length; };
return that;
}
DataDecoders.Encoder.FloatList2B64 = function (info) {
var that = {};
DQX.checkIsNumber(info.Count); DQX.checkIsNumber(info.RangeOffset); DQX.checkIsNumber(info.RangeSlope);
that.rangeOffset = parseFloat(info.RangeOffset);
that.rangeSlope = parseFloat(info.RangeSlope);
that.count = parseInt(info.Count);
var _b64codec = DataDecoders.B64();
that.decodeArray = function (datastr) {
var strlen = datastr.length;
var reclen = that.count + 4;
var rs = [];
for (var offset = 0; offset < strlen; offset += reclen) {
var minval = _b64codec.B642IntFixed(datastr, offset + 0, 2) * that.rangeSlope + that.rangeOffset;
var maxval = _b64codec.B642IntFixed(datastr, offset + 2, 2) * that.rangeSlope + that.rangeOffset;
var subrs = [];
for (var i = 0; i < this.count; i++)
subrs.push(minval + _b64codec.B642IntFixed(datastr, offset + 4 + i, 1) / 63.0 * (maxval - minval));
rs.push(subrs);
}
return rs;
}
that.getRecordLength = function () { DQX.reportError("Undefined record length") };
return that;
}
DataDecoders.Encoder.BooleanListB64 = function (info) {
var that = {};
DQX.checkIsNumber(info.Count);
that.rangeSlope = parseFloat(info.RangeSlope);
that.valueCount = parseInt(info.Count);
that.byteCount = Math.floor((that.valueCount + 5) / 6.0)
var _b64codec = DataDecoders.B64();
that.decodeArray = function (datastr) {
return '-';
}
that.decodeSingle = function (datastr, offset) {
var vl = _b64codec.B642IntFixed(datastr, offset, this.byteCount);
var rs = [];
for (var i = 0; i < this.valueCount; i++) {
rs.unshift(vl & 1);
vl = vl >> 1;
}
return rs;
}
that.getRecordLength = function () { return this.byteCount; };
return that;
}
DataDecoders.Encoder.MultiCatCount = function (info) {
var that = {};
DQX.checkIsNumber(info.CatCount);
DQX.checkIsNumber(info.EncoderLen);
that.catCount = parseInt(info.CatCount);
that.encoderlen = parseInt(info.EncoderLen);
var _b64codec = DataDecoders.B64();
that.decodeArray = function (datastr) {
var strlen = datastr.length;
var reclen = that.catCount * that.encoderlen;
var rs = [];
for (var offset = 0; offset < strlen; ) {
var subrs = [];
for (var i = 0; i < that.catCount; i++) {
subrs.push(_b64codec.B642IntFixed(datastr, offset, that.encoderlen));
offset += that.encoderlen;
}
rs.push(subrs);
}
return rs;
}
that.decodeSingle = function (datastr, offset) {
throw 'Not implemented';
}
that.getRecordLength = function () { return this.byteCount; };
return that;
}
//Factory function that automatically creates a codec based on the properties
DataDecoders.Encoder.Create = function (info) {
if (info['ID'] == 'Int2B64')
return DataDecoders.Encoder.Int2B64(info);
if (info['ID'] == 'Float2B64')
return DataDecoders.Encoder.Float2B64(info);
if (info['ID'] == 'FloatList2B64')
return DataDecoders.Encoder.FloatList2B64(info);
if (info['ID'] == 'FixedString')
return DataDecoders.Encoder.FixedString(info);
if (info['ID'] == 'Boolean')
return DataDecoders.Encoder.Boolean(info);
if (info['ID'] == 'BooleanListB64')
return DataDecoders.Encoder.BooleanListB64(info);
if (info['ID'] == 'MultiCatCount')
return DataDecoders.Encoder.MultiCatCount(info);
DQX.reportError("Invalid encoder id " + info['ID']);
}
return DataDecoders;
});