-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparsing.js
212 lines (190 loc) · 5.24 KB
/
parsing.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
/*
* Parsing functions
*/
var stream = require('binary-stream');
// exports function to make them available to other files
exports.parseStreamToJson = function(data) {
var getDataReadFunction = function(instream) {
var readFcn;
var dataType = instream.readUInt8();
switch(dataType) {
case 0x00: // no data
readFcn = function(stream) {
return;
};
break;
case 0x01: // array
readFcn = parseArrayFromStream;
break;
case 0x02: // string
readFcn = parseStringFromStream;
break;
case 0x03: // numeric types
readFcn = function(stream) {
return stream.readUInt8();
};
break;
case 0x04:
readFcn = function(stream) {
return stream.readInt8();
};
break;
case 0x05:
readFcn = function(stream) {
return stream.readUInt16();
};
break;
case 0x06:
readFcn = function(stream) {
return stream.readInt16();
};
break;
case 0x07:
readFcn = function(stream) {
return stream.readUInt32();
};
break;
case 0x08:
readFcn = function(stream) {
return stream.readInt32();
};
break;
case 0x09: // js object
readFcn = parseObjectFromStream;
break;
case 0x0A: // boolean (could also be done as a uint8)
readFcn = parseBooleanFromStream;
break;
default:
console.log("Data type unknown: " + dataType);
// just remove a byte from the input stream...what else to do?
readFcn = function(stream) {
return stream.readUInt8();
};
}
return readFcn;
};
var parseArrayFromStream = function(instream) {
var length = instream.readUInt8();
var readFcn = getDataReadFunction(instream);
var array = [];
for (var j = 0; j < length; j++) {
array.push(readFcn(instream));
}
return array;
};
var parseStringFromStream = function(instream) {
var length = instream.readUInt8();
return instream.readAscii(length);
};
var parseObjectFromStream = function(instream) {
var length = instream.readUInt8();
var obj = {};
for (var i = 0; i < length; i++) {
var fieldName = parseStringFromStream(instream);
obj[fieldName] = parseDataFromStream(instream);
}
return obj;
};
var parseBooleanFromStream = function(instream) {
return (instream.readUInt8() != 0);
};
var parseDataFromStream = function(instream) {
var readFcn = getDataReadFunction(instream);
return readFcn(instream);
};
var reader = new stream.Reader(data, stream.BIG_ENDIAN);
return {
type: reader.readUInt8(),
content: parseDataFromStream(reader)
};
}
exports.parseJsonToStream = function(message) {
var parseArrayToStream = function(outstream, array, doWriteType) {
if (doWriteType)
outstream.writeUInt8(0x01);
outstream.writeUInt8(array.length);
for (var j = 0 ; j < array.length; j++) {
parseDataToStream(outstream, array[j], (j == 0));
}
};
var parseStringToStream = function(outstream, str, doWriteType) {
if (doWriteType)
outstream.writeUInt8(0x02);
outstream.writeUInt8(str.length);
outstream.writeAscii(str);
};
var parseNumericToStream = function(outstream, num, doWriteType) {
// default to I16
if (doWriteType)
outstream.writeUInt8(types[i].id);
outstream.writeInt16(num);
};
var parseNumericObjectToStream = function(obj) {
var NUMERIC_TYPES = {
U8: { fcn: 'writeUInt8', id: 0x03 },
U16: { fcn: 'writeUInt16', id: 0x05 },
U32: { fcn: 'writeUInt32', id: 0x07 },
I8: { fcn: 'writeInt8', id: 0x04 },
I16: { fcn: 'writeInt16', id: 0x06 },
I32: { fcn: 'writeInt32', id: 0x08 }
};
var objType = NUMERIC_TYPES[obj.numericType];
return function(outstream, num, doWriteType) {
if (doWriteType)
outstream.writeUInt8(objType.id);
outstream[objType.fcn](num.numericValue);
};
};
var parseObjectToStream = function(outstream, obj, doWriteType) {
if (doWriteType)
outstream.writeUInt8(0x09);
outstream.writeUInt8(Object.keys(obj).length);
for (var field in obj) {
outstream.parseStringToStream(outstream, field, false);
outstream.parseDataToStream(outstream, obj[field], true);
}
};
var parseBooleanToStream = function(outstream, bool, doWriteType) {
if (doWriteType)
outstream.writeUInt8(0x0A);
outstream.writeUInt8(bool?0x01:0x00);
};
var parseNothingToStream = function(outstream, data, doWriteType) {
if (doWriteType)
outstream.writeUInt8(0x00);
};
var parseDataToStream = function(outstream, data, doWriteType) {
var parseFcn;
switch ( Object.prototype.toString.call(data) ) {
case '[object String]':
parseFcn = parseStringToStream;
break;
case '[object Boolean]':
parseFcn = parseBooleanToStream;
break;
case '[object Number]':
parseFcn = parseNumericToStream;
break;
case '[object Array]':
parseFcn = parseArrayToStream;
break;
case '[object Null]':
case '[object Undefined]':
parseFcn = parseNothingToStream;
break;
default:
if (data.numericType) {
parseFcn = parseNumericObjectToStream(data);
} else {
parseFcn = parseObjectToStream;
}
break;
}
parseFcn(outstream, data, doWriteType);
};
var writer = new stream.Writer(255, stream.BIG_ENDIAN);
writer.writeUInt8(message.type);
parseDataToStream(writer, message.content, true);
return writer.toArray();
}