-
Notifications
You must be signed in to change notification settings - Fork 2
/
kramer-old.js
443 lines (389 loc) · 11.6 KB
/
kramer-old.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
/*
Kramer crams JSON objects into strings that is usable as a URL component.
The characters used are:
Base64 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-
Extra ,$*()_.!'
Headers
=======
Headers contain a dictionary of words and/or a collection of propsets (object
property names). Headers may be supplied externally to the encode / decode
functions (in which case care must be taken to ensure that the same headers are
provided to both) or it may be embedded at the start of the document itself.
Encoding values
===============
null __
true _.
false _'
number _<base64_digits>,
string <base64+lookup>, // omit s where only strings are valid (object keys, dictionary entries)
dictlook $<id> // all bytes until first in range [A-Za-f]
object )[<psetinfl>|<keyval>].
array ([<value>].
psetinfl *<id>[<value>|<skip>] // number of repeats is known from propset definition
skip .
keyval <base64+lookup>,<value>
id hoffman coded positive integer
A base64 encoded string can have dictionary lookup sequences prefixed with a period.
If propset inflate occurs in an object key position, it is a set of properties of
the current object; if it occurs in a value position, it is a child object.
Encoding headers
====================
dicthdr $ [<base64+lookup>,] // repeats until * ( or )
psethdr ' [[<base64+lookup>,].] // repeats until ( or )
If the first character of the document is a $ or *, there are headers.
A $ starts the dictionary definition, which consists of base64 strings separated
by commas. These words may have embedded dictionary lookup sequences ($)
but only previously defined words may be referenced. The last word in the
dictionary should be followed by one of ' * ( or )
A ' starts the propsets header, which consists of multiple propset definitions
separated by periods. Each propset definition consists of base64 strings
separated by commas, which may contain dictionary lookups. The propsets header
is terminated by one of * ( or ), which start an array or object in the document
body.
Documents with embedded headers must have an object or array at the root.
Summary of non-base64 symbols (Xchars)
======================================
, terminates strings and numbers
where a value is expected, an empty string
$ where a value is expected looks up a dictionary word
at start of document, starts the dictionary header
* where a value is expected, creates an object using a propset
where an object key is expected, adds all keys from a propset
( begins an array definition
) begins an object definition
_ if the next char is also _ then null
if the next char is . then false
if the next char is ! then true
if the next char is valid b64 then number
! terminates multiple arrays and objects; the next one character is
an encoded number (0-63) specifying number of terminations
at start of document or in dicthdr context, starts propsets header
. terminates arrays and objects;
within propsets header, terminates propsets
in psetinfl context, a skipped property
' reserved for future use
*/
module.exports = function(head) {
var maps = {
num: " 0123456789+-.e", // one symbol is unused.
b64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-"
};
function stoa(str, map) {
var a=[], i, l=str.length, c;
for(i=0; i<l; i++) {
c = map.indexOf(str.charAt(i));
if(c == -1) throw Error(str + ' ' + i + ' ' + map);
a.push(c);
}
return a;
}
function atos(arr, map) {
var s='', i, l=arr.length, c;
for(i=0; i<l; i++) {
c = map[arr[i]];
if(typeof c == 'undefined') throw Error();
s += c;
}
return s;
}
function rebase(a, ba, bb) {
var b=[], i, l=a.length, c=0, f=1;
for(i=0; i<l; i++) {
c = c*ba + (a[i] | 0);
f*=ba;
while (f>bb || i == l-1 && f>1) {
f/=bb;
b.push((c/f)|0);
c = c%f;
}
}
return b;
}
function encode(value) {
head = head || {};
function insertTerminator(s) {
var c;
if(s[s.length-1] == '.') {
s = s.substr(0, s.length-1) + '!' + atos([0], maps.b64);
} else if(s[s.length-2] == '!') {
c = stoa(s[s.length-1], maps.b64)[0] + 1;
if(c < 64) {
s = s.substr(0, s.length-1) + atos([c], maps.b64);
} else {
s += '.';
}
} else {
s += '.';
}
return s;
}
function encodeId(id) {
var s = '', d;
do {
d = id%32;
id = (id/32)|0;
if(id>0) d&=0x20;
s += maps.b64[d];
} while(id>0);
return s;
};
function encodeVal(value) {
var i, l, p, s;
switch (typeof value) {
case 'undefined': return '!';
case 'number': return '_' + encodeNum(value) + ',';
case 'string':
if(head.dict && (i=head.dict.indexOf(value)) != -1) {
return '$' + encodeId(i);
}
return encodeStr(value) + ',';
case 'boolean': return value? "_!": '_.';
case 'object':
if(value === null) {
return '__';
}
if(Array.isArray(value)) {
s='(';
for(i=0, l=value.length; i<l; i++) s += encode(value[i]);
return insertTerminator(s);
}
if (head.pset && (p=choosePset(value)) !== -1) {
s='*' + encodeId(p);
p = head.pset[p];
for(i=0, l=p.length; i<l; i++) if(value.hasOwnProperty(p[i])) {
s += encode(value[p[i]]);
} else {
s += '.';
}
return s;
}
s=')';
for(i in value) s += encode(i) + encode(value[i]);
return insertTerminator(s);
default:
throw new Error(value + ' cannot be encoded.');
}
}
function choosePset(o) {
var i, l, p, j, s, bests=0, bestp=-1;
for(i=0, l=head.pset.length; i<l; i++) {
p = head.pset[i];
s=-p.length;
for(j in o) if(p.indexOf(j) == -1) {
s = -1; break;
} else {
s += (head.dict && head.dict[j])? 2: j.length;
}
if(s>bests) {
bestp = i;
bests = s;
}
}
return bestp;
}
function encodeNum(n) {
return atos(
rebase(stoa(n.toString().toLowerCase(), maps.num), 16, 64),
maps.b64
);
}
function encodeStr(s) {
// 0 padding
// 1-95 ascii 32-126
// 96-98 \t\n\r (stored in one byte)
// 99 begin dictionary lookup sequences > id 16
// 100-115 dictionary lookup sequence
// 116-123 first 3 bits of unicode 0x0080-0x047f (stored in two bytes)
// 124-127 first 2 bits of any 16-bit unicode (stored in three bytes)
// javascript only fakes support for 32 bit unicode
var i, l=s.length, a=[], c;
for(i=0; i<l; i++) {
c = s.charCodeAt(i);
if(c < 32) switch(c) {
// Control characters valid in JS strings:
case 9: a.push(96); break; // \t
case 10: a.push(97); break; // \n
case 13: a.push(98); break; // \r
default: a.push(124, 0, c); break;
} else if(c < 0x7f) {
a.push(c - 30);
} else if(c == 0x7f) {
a.push(124, 0, c); // DEL character;
} else if(c < 0x0480) {
a.push(116 + (((c-0x80)&0x0380)>>7), (c-0x80)&0x007f);
} else {
a.push(124 + ((c&0xc000)>>14), (c&0x3f80)>>7, c&0x007f);
}
}
return atos(rebase(a, 128, 64), maps.b64);
}
return encodeVal(value);
}
function decode(string) {
var pos = 0, pendingTerminators = 0;
string += '~';
head = head || {};
function handleTerminator() {
switch(string[pos++]) {
case ',': return;
case '.': pendingTerminators = 1; return;
case '!': pendingTerminators = stoa(string[pos++], maps.b64)[0] + 2; return;
default: pendingTerminators = Infinity; return;
// default: return false;
}
}
function lookupWord(n) {
if(!head.dict || typeof head.dict[n] === 'undefined') {
throw Error('Could not find dictionary entry ' + n);
}
return head.dict[n];
}
function inflatePset(n) {
var pset, i, l, o={};
if(!head.pset || typeof (pset=head.pset[n]) === 'undefined') {
throw Error('Could not find propset ' + n);
}
for(i=0, l=pset.length; i<l; i++) {
if(string[pos] == '.') {
pos++;
continue;
}
o[pset[i]] = decodeVal();
if(pendingTerminators && i < l-1) {
throw Error('Unexpected termination before pset inflation could complete');
}
}
return o;
}
function decodeId() {
var n = 0, d;
do {
d = maps.b64.indexOf(string[++pos]);
n = n*32 + (d&0x1f);
} while(d>=32);
pos++;
return n;
}
function decodeVal() {
switch (string[pos]) {
case ',': case '.': case '!': case '~':
handleTerminator();
return '';
case '(':
pos++;
return decodeArr();
case ')':
pos++;
return decodeObj();
case '_':
switch(string[pos+1]) {
case '_': pos+=2; return null;
case '.': pos+=2; return false;
case '!': pos+=2; return true;
}
pos+=1;
return decodeNum();
case '$':
return lookupWord(decodeId());
case '*':
return inflatePset(decodeId());
default:
return decodeStr();
}
}
function decodeArr() {
var a = [];
while(!pendingTerminators) {
if(string[pos] == '.' || string[pos] == '!' || string[pos] == '~') {
handleTerminator(); break;
}
a.push(decodeVal());
}
pendingTerminators--;
return a;
}
function decodeObj() {
var o = {}, k;
while(!pendingTerminators) {
if(string[pos] == '.' || string[pos] == '!' || string[pos] == '~') {
handleTerminator(); break;
} k = decodeVal();
if(pendingTerminators) {
throw Error('Invalid terminator after ' + k);
}
o[k] = decodeVal();
}
pendingTerminators--;
return o;
}
function decodeNum() {
var start=pos, n;
while(string[pos] != ',' && string[pos] != '.' && string[pos] != '!' && string[pos] != '~') pos++;
n = parseFloat(atos(
rebase(stoa(string.substring(start, pos), maps.b64), 64, 16), maps.num
));
handleTerminator();
return n;
}
function decodeStr() {
// 0 padding
// 1-95 ascii 32-126
// 96-98 \t\n\r (stored in one byte)
// 99 begin dictionary lookup sequences, id > 16
// 100-115 dictionary lookup sequence, id < 16
// 116-123 first 3 bits of unicode 0x0080-0x047f (stored in two bytes)
// 124-127 first 2 bits of any 16-bit unicode (stored in three bytes)
// javascript only fakes support for 32 bit unicode
var i, l, a, n, s='', start=pos;
while(string[pos] != ',' && string[pos] != '.' && string[pos] != '!' && string[pos] != '~') pos++;
a = rebase(stoa(string.substring(start, pos), maps.b64), 64, 128);
function decodeId() { // Decode lookup IDs within a 7-byte string
var acc = 0;
do {
acc = acc*64 + (a[++i]&0x3f);
} while(a[i] > 64);
return acc;
}
for(i=0, l=a.length; i<l; i++) {
n = a[i];
if(n === 0) {
continue;
} else if(n < 96) {
s += String.fromCharCode(a[i] + 30);
} else if(n < 100) switch(n) {
case 96: s += String.fromCharCode(9); break; // \t
case 97: s += String.fromCharCode(10); break; // \n
case 98: s += String.fromCharCode(13); break; // \r
case 99: s += lookupWord(16 + decodeId());
} else if(n < 116) {
lookupWord(n-100);
} else if(n < 124) {
s += String.fromCharCode(0x80 + (n-116)*128 + a[++i]);
} else {
s += String.fromCharCode((n-124)*128*128 + a[++i]*128 + a[++i]);
}
}
handleTerminator();
return s;
}
return decodeVal();
}
return {
encode: encode,
decode: decode
};
};
//
//var orig = {hello: "world", what: [12, false, 'wh\u7388at']};
//var head;
//// head = {dict: ['hello', 'what']};
//head = {dict: ['hello', 'world'], pset: [['hello', 'what'], ['hello', 'what', 'woo'], ['hello']]};
//var code = encode(orig);
//
//console.log(code, code.length);
//console.log(JSON.stringify(orig));
//console.log(JSON.stringify(decode(code)));
//
//var t = [12, 54, 65, 23, 80], u = rebase(t, 128, 64);
//console.log(t, u, rebase(u, 64, 128));
//