-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitgram.js
536 lines (500 loc) · 10.2 KB
/
bitgram.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
function checkValid(str)
{
var xmlBody = "<bitgrams>" + str + "</bitgrams>";
var doc = null;
if (window.DOMParser)
{
var parser = new DOMParser();
doc = parser.parseFromString(xmlBody, "text/xml");
}
else
{
doc = new ActiveXObject("Microsoft.XMLDOM");
doc.async = true;
doc.loadXML(xmlBody);
}
var top = doc.childNodes;
if (top.length != 1)
{
console.log("impossible: bitgrams length is 1.\n");
return true;
}
var asciiArt = parseBitgrams(top[0].childNodes, bitgramsToAsciiArt);
if (asciiArt == null)
{
return true;
}
var midCol = document.getElementById("clm2");
var midChildren = midCol.childNodes;
for (var i = midChildren.length; i > 0; i--)
{
midCol.removeChild(midChildren[i - 1]);
}
var codeNode = document.createElement("pre");
codeNode.innerHTML = asciiArt;
midCol.appendChild(codeNode);
var table = parseBitgrams(top[0].childNodes, bitgramsToTable);
if (table == null)
{
return true;
}
var lastCol = document.getElementById("clm3");
lastCol.innerHTML = table;
return true;
}
function parseBitgrams(bitgrams, bitgramsFunc)
{
var bitgramFunc = bitgramsFunc();
for (var i = 0; i < bitgrams.length; i++)
{
var node = bitgrams[i];
if (node.nodeType == 3)
{
var data = node.data;
data = data.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
if (data != "")
{
console.log("non-empty text element in top level.\n");
return null;
}
continue;
}
else if (node.nodeType == 1)
{
if (node.nodeName != "bitgram")
{
console.log("unknown node when expecting bitgram: " + node.nodeName);
return null;
}
}
else if (node.nodeType == 8)
{
continue;
}
else
{
console.log("unexpected node type: " + node.nodeType.toString());
return null;
}
var nodeId = null;
var width = 32;
var attrs = node.attributes;
for (var j = 0; j < attrs.length; j++)
{
if (attrs[j].nodeName == "id")
nodeId = attrs[j].nodeValue;
else if (attrs[j].nodeName == "width")
{
width = parseInt(attrs[j].nodeValue);
if (isNaN(width))
{
console.log("invalid width for bitgram: " + attrs[j].nodeValue);
return;
}
}
else
{
console.log("unknown attribute: " + attrs[j].nodeName);
}
}
bitgramFunc(node.childNodes, nodeId, width);
}
return bitgramFunc(null, null, null);
}
function parseFields(nodes, id, gramWidth, fieldsFunc)
{
var fieldFunc = fieldsFunc(nodes, id, gramWidth);
for (var i = 0; i < nodes.length; i++)
{
var node = nodes[i];
if (node.nodeType == 3)
{
var data = node.data;
data = data.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
if (data != "")
{
console.log("non-empty text element |" + data + "| in bitgram: " + id);
return null;
}
continue;
}
else if (node.nodeType == 1)
{
if (node.nodeName != "field")
{
console.log("unknown node when expecting field: " + node.nodeName);
return null;
}
}
else if (node.nodeType == 8)
{
continue;
}
else
{
console.log("unexpected node type: " + node.nodeType.toString());
return null;
}
var name = null;
var width = 32;
var stride = 0;
var big = false;
var value = null;
var attrs = node.attributes;
for (var j = 0; j < attrs.length; j++)
{
if (attrs[j].nodeName == "name")
name = attrs[j].nodeValue;
else if (attrs[j].nodeName == "value")
value = attrs[j].nodeValue;
else if (attrs[j].nodeName == "big" && attrs[j].nodeValue == "true")
big = true;
else if (attrs[j].nodeName == "width")
{
if (attrs[j].nodeValue == "variable")
width = "variable";
else
{
width = parseInt(attrs[j].nodeValue);
if (width != "variable" && isNaN(width))
{
console.log("invalid width for bitgram field: " +
attrs[j].nodeValue);
return;
}
}
}
else if (attrs[j].nodeName == "stride")
{
stride = parseInt(attrs[j].nodeValue);
if (isNaN(stride))
{
console.log("invalid stride for bitgram field: " +
attrs[j].nodeValue);
return;
}
}
else
{
console.log("unknown attribute: " + attrs[j].nodeName);
}
if (value == null)
value = name;
if (stride == null)
stride = width;
}
fieldFunc(name, value, big, width, stride);
}
return fieldFunc(null, null, null, null, null);
}
function bitgramsToAsciiArt()
{
var rv = "";
return function(fields, nodeId, width)
{
if (fields == null)
{
if (rv == "")
{
console.log("no bitgrams.");
return null;
}
return rv;
}
rv = rv + " ";
for (var j = 0; j < width; j++)
{
if ((j & 7) == 0)
rv = rv + "+-";
else
rv = rv + "--";
}
rv = rv + "+\n ";
for (var j = 0; j < width; j++)
{
if ((j & 7) == 0)
rv = rv + "|"
else
rv = rv + " ";
rv = rv + (j & 7).toString();
}
rv = rv + "|\n";
var subrv = parseFields(fields, nodeId, width, fieldsToAsciiArt);
if (subrv == null)
return null;
rv = rv + subrv + "\n\n";
};
}
function fieldsToAsciiArt(nodes, id, gramWidth)
{
var rv = " ";
var line1 = " ";
var line2 = " ";
var spaces = "";
for (i = 0; i < gramWidth; i++)
{
if ((i & 7) == 0)
{
rv += '+-';
}
else
rv += '--';
spaces += " ";
}
rv += '+\n';
var at = 0;
return function(name, value, big, width, stride)
{
if (name == null)
{
if (line2 != " ")
{
rv = (rv +
line2.substring(0, lim) + "|\n" +
line1.substring(0, lim) + "+\n");
}
if (rv == "")
{
console.log("no bitgrams.");
return null;
}
return rv;
}
console.log("name = " + name +
" value = " + value +
" big = " + big.toString() +
" width = " + width.toString() +
" stride = " + stride.toString());
var remain = 0;
var lim;
var end = "|";
if (width == "variable")
{
if (at != 0)
{
remain = gramWidth;
middle = "~";
end = ":";
}
else
{
remain = 0;
}
lim = gramWidth;
}
else
{
if (gramWidth - at < width)
{
remain = width - (gramWidth - at);
lim = gramWidth;
}
else
{
remain = 0;
lim = at + width;
}
}
while (at != lim)
{
var off = rv.length;
off -= 2; // the newline and terminating mark
off -= gramWidth * 2; // back to the beginning of the line
off += at * 2; // forward to current position.
rv = rv.substr(0, off) + "+" + rv.substr(off + 1);
var space = (lim - at) * 2;
if (name.length + 1 < space)
{
var ava = (space - name.length - 1);
line2 += "|";
if (ava & 1)
{
line2 += " ";
ava -= 1;
}
ava = ava >> 1;
line2 += spaces.substr(0, ava) + name + spaces.substr(0, ava);
}
else
{
line2 += "|" + name.substr(0, space - 1);
}
for (var j = at; j < lim; j++)
{
if (j < remain)
{
if (j == 0 || j == at)
line1 += "+ "
else
line1 += " ";
}
else
{
if (j == 0 || j == at || j == remain)
line1 += "+-";
else
line1 += "--";
}
}
if (lim == gramWidth)
{
rv = rv + line2 + end + "\n" + line1 + "+\n";
line2 = " ";
line1 = " ";
at = 0;
}
else
{
at = lim;
}
if (remain >= gramWidth)
{
remain -= (gramWidth);
lim = gramWidth;
if (end == "~")
end = ":";
}
else if (remain)
{
lim = remain;
remain = 0;
}
else
{
lim = at;
}
}
};
}
function bitgramsToTable()
{
var rv = "";
return function(fields, nodeId, width)
{
if (fields == null)
{
if (rv == "")
{
console.log("no bitgrams.");
return null;
}
return rv;
}
var subrv = parseFields(fields, nodeId, width, fieldsToTable);
if (subrv == null)
return null;
rv = rv + subrv + "\n\n";
};
}
function fieldsToTable(nodes, id, gramWidth)
{
var bitwidth = (((84 / gramWidth * 10) | 0) / 10).toString();
var rv = "<table class='bitgram-table' id='" + id + "'>\n";
var bitoff = 0;
var octoff = 0;
rv += " <tbody class='bitgram-tbody'\n";
rv += " <tr>\n";
rv += " <th class='offsets'>Offsets</th>\n";
rv += " <th class='left-octet'>Octet</th>\n";
for (var i = 0; i < gramWidth >> 3; i++)
{
rv += " <th colspan='8' class='octet'>" + i.toString() + "</th>\n";
}
rv += " </tr>\n";
rv += " <tr>\n";
rv += " <th class='top-octet'>Octet</th>\n";
rv += " <th class='bit-label'>Bit</th>\n";
for (var i = 0; i < gramWidth; i++)
{
rv += (" <th style='width:" +
bitwidth + "%' class='bit'>" + i.toString() + "</th>\n");
}
var at = 0;
return function(name, value, big, width, stride)
{
if (name == null)
{
if (at != 0)
{
if (at != gramWidth)
{
rv += (" <td class='blank-field' colspan='" +
(gramWidth - at).toString() +
"'></td>\n");
}
rv += " </tr>\n";
rv += " </tbody>\n";
rv += "</table>\n";
}
return rv;
}
var remain = 0;
var lim;
if (width == "variable")
{
if (at != 0)
{
remain = gramWidth;
}
else
{
remain = 0;
}
lim = gramWidth;
}
else
{
if (gramWidth - at < width)
{
remain = width - (gramWidth - at);
lim = gramWidth;
}
else
{
remain = 0;
lim = at + width;
}
}
while (at != lim)
{
if (at == 0)
{
rv += " <tr>\n";
rv += " <th class='octet-offset'>" + octoff.toString() + "</th>\n";
rv += " <th class='bit-offset'>" + bitoff.toString() + "</th>\n";
bitoff += gramWidth;
octoff += (gramWidth >> 3);
}
var space = (lim - at).toString();
rv += (" <td class='field' colspan='" + space +
"'>" + name + "</td>\n");
if (lim == gramWidth)
{
rv += " </tr>\n";
at = 0;
}
else
{
at = lim;
}
if (remain >= gramWidth)
{
remain -= (gramWidth);
lim = gramWidth;
}
else if (remain)
{
lim = remain;
remain = 0;
}
else
{
lim = at;
}
}
};
}
// Local Variables:
// mode:Javascript
// js-class-style:"gnu"
// end: