/* * Copyright (c) 2021 Antoine Martin */ const RENCODE = { DEFAULT_FLOAT_BITS : 32, MAX_INT_LENGTH : 64, CHR_LIST : 59, CHR_DICT : 60, CHR_INT : 61, CHR_INT1 : 62, CHR_INT2 : 63, CHR_INT4 : 64, CHR_INT8 : 65, CHR_FLOAT32 : 66, CHR_FLOAT64 : 44, CHR_TRUE : 67, CHR_FALSE : 68, CHR_NONE : 69, CHR_TERM : 127, INT_POS_FIXED_START : 0, INT_POS_FIXED_COUNT : 44, DICT_FIXED_START : 102, DICT_FIXED_COUNT : 25, INT_NEG_FIXED_START : 70, INT_NEG_FIXED_COUNT : 32, STR_FIXED_START : 128, STR_FIXED_COUNT : 64, LIST_FIXED_START : 128+64, //STR_FIXED_START + STR_FIXED_COUNT, LIST_FIXED_COUNT : 64, COLON_CHARCODE : ":".charCodeAt(0), //for char strings SLASH_CHARCODE : "/".charCodeAt(0), //for byte strings }; Number.isSafeInteger = Number.isSafeInteger || function (value) { return Number.isInteger(value) && Math.abs(value) <= Number.MAX_SAFE_INTEGER; }; function utf8ByteArrayToString(bytes) { let out = [], pos = 0, c = 0; while (pos < bytes.length) { const c1 = bytes[pos++]; if (c1 < 128) { out[c++] = String.fromCharCode(c1); } else if (c1 > 191 && c1 < 224) { const c2 = bytes[pos++]; out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63); } else if (c1 > 239 && c1 < 365) { // Surrogate Pair const c2 = bytes[pos++]; const c3 = bytes[pos++]; const c4 = bytes[pos++]; const u = ((c1 & 7) << 18 | (c2 & 63) << 12 | (c3 & 63) << 6 | c4 & 63) - 0x10000; out[c++] = String.fromCharCode(0xD800 + (u >> 10)); out[c++] = String.fromCharCode(0xDC00 + (u & 1023)); } else { const c2 = bytes[pos++]; const c3 = bytes[pos++]; out[c++] = String.fromCharCode((c1 & 15) << 12 | (c2 & 63) << 6 | c3 & 63); } } return out.join(''); } function stringToUtf8ByteArray(str) { let out = [], p = 0; for (let i = 0; i < str.length; i++) { let c = str.charCodeAt(i); if (c < 128) { out[p++] = c; } else if (c < 2048) { out[p++] = (c >> 6) | 192; out[p++] = (c & 63) | 128; } else if ( ((c & 0xFC00) == 0xD800) && (i + 1) < str.length && ((str.charCodeAt(i + 1) & 0xFC00) == 0xDC00)) { // Surrogate Pair c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF); out[p++] = (c >> 18) | 240; out[p++] = ((c >> 12) & 63) | 128; out[p++] = ((c >> 6) & 63) | 128; out[p++] = (c & 63) | 128; } else { out[p++] = (c >> 12) | 224; out[p++] = ((c >> 6) & 63) | 128; out[p++] = (c & 63) | 128; } } return out; } function rencode_string(str) { const bytes = stringToUtf8ByteArray(str); const len = bytes.length; if (len < RENCODE.STR_FIXED_COUNT) { const u8a = new Uint8Array(len+1); u8a[0] = RENCODE.STR_FIXED_START+len; for (let i=0; i= RENCODE.MAX_INT_LENGTH) { throw "number too big: "+i; } const str_len = str.length; u8a = new Uint8Array(str_len+2); u8a[0] = RENCODE.CHR_INT; for (let j=0; j>24; dec.pos += 2; return b; } function rdecode_inth(dec) { const slice = dec.buf.slice(dec.pos+1, dec.pos+3) const dv = new DataView(slice.buffer); const s = dv.getInt16(0); dec.pos += 3; return s; } function rdecode_intl(dec) { const slice = dec.buf.slice(dec.pos+1, dec.pos+5) const dv = new DataView(slice.buffer); const s = dv.getInt32(0); dec.pos += 5; return s; } function rdecode_intq(dec) { const slice = dec.buf.slice(dec.pos+1, dec.pos+9) const dv = new DataView(slice.buffer); const s = dv.getBigInt64(0); dec.pos += 9; if (!Number.isSafeInteger(s)) { //console.warn("value is not a safe integer: ", s); } return parseInt(s); } function rdecode_true(dec) { dec.pos++; return true; } function rdecode_false(dec) { dec.pos++; return false; } function rdecode_none(dec) { dec.pos++; return null; } const decode_func = new Map(); for(let i=0; i<10; i++) { const charcode = i.toString().charCodeAt(0); decode_func[charcode] = rdecode_string; } decode_func[RENCODE.CHR_LIST] = rdecode_list decode_func[RENCODE.CHR_DICT] = rdecode_dict decode_func[RENCODE.CHR_INT] = rdecode_int decode_func[RENCODE.CHR_INT1] = rdecode_intb decode_func[RENCODE.CHR_INT2] = rdecode_inth decode_func[RENCODE.CHR_INT4] = rdecode_intl decode_func[RENCODE.CHR_INT8] = rdecode_intq decode_func[RENCODE.CHR_TRUE] = rdecode_true decode_func[RENCODE.CHR_FALSE] = rdecode_false decode_func[RENCODE.CHR_NONE] = rdecode_none function make_fixed_length_string_decoder(len) { function fixed_length_string_decoder(dec) { dec.pos++; const u8a = dec.buf.subarray(dec.pos, dec.pos+len); dec.pos += len; return utf8ByteArrayToString(u8a); } return fixed_length_string_decoder; } for(let i=0; i=dec.buf.length) { throw "reached end of buffer" } const typecode = dec.buf[dec.pos]; const decode = decode_func[typecode]; if (decode === null || decode === undefined) { //console.log("buffer pos:", dec.pos); //console.log("buffer:", dec.buf.subarray(dec.pos, dec.pos+20)) //const str = String.fromCharCode.apply(null, dec.buf.subarray(dec.pos, dec.pos+20)); throw "no decoder for typecode "+typecode+" at position "+dec.pos; } return decode(dec); } function rdecode(buf) { const type = typeof buf; if (type === "string") { const u8a = new Uint8Array(buf.length); for(let i=0,j=buf.length;i