'use strict'
const BinaryParseStream = require('../vendor/binary-parse-stream')
const Tagged = require('./tagged')
const Simple = require('./simple')
const utils = require('./utils')
const NoFilter = require('nofilter')
const stream = require('stream')
const constants = require('./constants')
const {MT, NUMBYTES, SYMS, BI} = constants
const {Buffer} = require('buffer')
const COUNT = Symbol('count')
const MAJOR = Symbol('major type')
const ERROR = Symbol('error')
const NOT_FOUND = Symbol('not found')
function parentArray(parent, typ, count) {
const a = []
a[COUNT] = count
a[SYMS.PARENT] = parent
a[MAJOR] = typ
return a
}
function parentBufferStream(parent, typ) {
const b = new NoFilter()
b[COUNT] = -1
b[SYMS.PARENT] = parent
b[MAJOR] = typ
return b
}
class UnexpectedDataError extends Error {
constructor(byte, value) {
super(`Unexpected data: 0x${byte.toString(16)}`)
this.name = 'UnexpectedDataError'
this.byte = byte
this.value = value
}
}
function normalizeOptions(opts, cb) {
switch (typeof opts) {
case 'function':
return {options: {}, cb: (opts)}
case 'string':
return {options: {encoding: (opts)}, cb}
case 'object':
return {options: opts || {}, cb}
default:
throw new TypeError('Unknown option type')
}
}
class Decoder extends BinaryParseStream {
constructor(options = {}) {
const {
tags = {},
max_depth = -1,
preferMap = false,
preferWeb = false,
required = false,
encoding = 'hex',
extendedResults = false,
preventDuplicateKeys = false,
...superOpts
} = options
super({defaultEncoding: encoding, ...superOpts})
this.running = true
this.max_depth = max_depth
this.tags = tags
this.preferMap = preferMap
this.preferWeb = preferWeb
this.extendedResults = extendedResults
this.required = required
this.preventDuplicateKeys = preventDuplicateKeys
if (extendedResults) {
this.bs.on('read', this._onRead.bind(this))
this.valueBytes = (new NoFilter())
}
}
static nullcheck(val) {
switch (val) {
case SYMS.NULL:
return null
case SYMS.UNDEFINED:
return undefined
case NOT_FOUND:
throw new Error('Value not found')
default:
return val
}
}
static decodeFirstSync(input, options = {}) {
if (input == null) {
throw new TypeError('input required')
}
({options} = normalizeOptions(options))
const {encoding = 'hex', ...opts} = options
const c = new Decoder(opts)
const s = utils.guessEncoding(input, encoding)
const parser = c._parse()
let state = parser.next()
while (!state.done) {
const b = s.read(state.value)
if ((b == null) || (b.length !== state.value)) {
throw new Error('Insufficient data')
}
if (c.extendedResults) {
c.valueBytes.write(b)
}
state = parser.next(b)
}
let val = null
if (c.extendedResults) {
val = state.value
val.unused = s.read()
} else {
val = Decoder.nullcheck(state.value)
if (s.length > 0) {
const nextByte = s.read(1)
s.unshift(nextByte)
throw new UnexpectedDataError(nextByte[0], val)
}
}
return val
}
static decodeAllSync(input, options = {}) {
if (input == null) {
throw new TypeError('input required')
}
({options} = normalizeOptions(options))
const {encoding = 'hex', ...opts} = options
const c = new Decoder(opts)
const s = utils.guessEncoding(input, encoding)
const res = []
while (s.length > 0) {
const parser = c._parse()
let state = parser.next()
while (!state.done) {
const b = s.read(state.value)
if ((b == null) || (b.length !== state.value)) {
throw new Error('Insufficient data')
}
if (c.extendedResults) {
c.valueBytes.write(b)
}
state = parser.next(b)
}
res.push(Decoder.nullcheck(state.value))
}
return res
}
static decodeFirst(input, options = {}, cb = null) {
if (input == null) {
throw new TypeError('input required')
}
({options, cb} = normalizeOptions(options, cb))
const {encoding = 'hex', required = false, ...opts} = options
const c = new Decoder(opts)
let v = (NOT_FOUND)
const s = utils.guessEncoding(input, encoding)
const p = new Promise((resolve, reject) => {
c.on('data', val => {
v = Decoder.nullcheck(val)
c.close()
})
c.once('error', er => {
if (c.extendedResults && (er instanceof UnexpectedDataError)) {
v.unused = c.bs.slice()
return resolve(v)
}
if (v !== NOT_FOUND) {
er['value'] = v
}
v = ERROR
c.close()
return reject(er)
})
c.once('end', () => {
switch (v) {
case NOT_FOUND:
if (required) {
return reject(new Error('No CBOR found'))
}
return resolve(v)
case ERROR:
return undefined
default:
return resolve(v)
}
})
})
if (typeof cb === 'function') {
p.then(val => cb(null, val), cb)
}
s.pipe(c)
return p
}
static decodeAll(input, options = {}, cb = null) {
if (input == null) {
throw new TypeError('input required')
}
({options, cb} = normalizeOptions(options, cb))
const {encoding = 'hex', ...opts} = options
const c = new Decoder(opts)
const vals = []
c.on('data', val => vals.push(Decoder.nullcheck(val)))
const p = new Promise((resolve, reject) => {
c.on('error', reject)
c.on('end', () => resolve(vals))
})
if (typeof cb === 'function') {
p.then(v => cb(undefined, v), er => cb(er, undefined))
}
utils.guessEncoding(input, encoding).pipe(c)
return p
}
close() {
this.running = false
this.__fresh = true
}
_onRead(data) {
this.valueBytes.write(data)
}
*_parse() {
let parent = null
let depth = 0
let val = null
while (true) {
if ((this.max_depth >= 0) && (depth > this.max_depth)) {
throw new Error(`Maximum depth ${this.max_depth} exceeded`)
}
const [octet] = yield 1
if (!this.running) {
this.bs.unshift(Buffer.from([octet]))
throw new UnexpectedDataError(octet)
}
const mt = octet >> 5
const ai = octet & 0x1f
const parent_major = (parent == null) ? undefined : parent[MAJOR]
const parent_length = (parent == null) ? undefined : parent.length
switch (ai) {
case NUMBYTES.ONE:
this.emit('more-bytes', mt, 1, parent_major, parent_length)
;[val] = yield 1
break
case NUMBYTES.TWO:
case NUMBYTES.FOUR:
case NUMBYTES.EIGHT: {
const numbytes = 1 << (ai - 24)
this.emit('more-bytes', mt, numbytes, parent_major, parent_length)
const buf = yield numbytes
val = (mt === MT.SIMPLE_FLOAT) ?
buf :
utils.parseCBORint(ai, buf)
break
}
case 28:
case 29:
case 30:
this.running = false
throw new Error(`Additional info not implemented: ${ai}`)
case NUMBYTES.INDEFINITE:
switch (mt) {
case MT.POS_INT:
case MT.NEG_INT:
case MT.TAG:
throw new Error(`Invalid indefinite encoding for MT ${mt}`)
}
val = -1
break
default:
val = ai
}
switch (mt) {
case MT.POS_INT:
break
case MT.NEG_INT:
if (val === Number.MAX_SAFE_INTEGER) {
val = BI.NEG_MAX
} else {
val = (typeof val === 'bigint') ? BI.MINUS_ONE - val : -1 - val
}
break
case MT.BYTE_STRING:
case MT.UTF8_STRING:
switch (val) {
case 0:
this.emit('start-string', mt, val, parent_major, parent_length)
if (mt === MT.UTF8_STRING) {
val = ''
} else {
val = this.preferWeb ? new Uint8Array(0) : Buffer.allocUnsafe(0)
}
break
case -1:
this.emit('start', mt, SYMS.STREAM, parent_major, parent_length)
parent = parentBufferStream(parent, mt)
depth++
continue
default:
this.emit('start-string', mt, val, parent_major, parent_length)
val = yield val
if (mt === MT.UTF8_STRING) {
val = utils.utf8(val)
} else if (this.preferWeb) {
val = new Uint8Array(val.buffer, val.byteOffset, val.length)
}
}
break
case MT.ARRAY:
case MT.MAP:
switch (val) {
case 0:
if (mt === MT.MAP) {
val = (this.preferMap) ? new Map() : {}
} else {
val = []
}
break
case -1:
this.emit('start', mt, SYMS.STREAM, parent_major, parent_length)
parent = parentArray(parent, mt, -1)
depth++
continue
default:
this.emit('start', mt, val, parent_major, parent_length)
parent = parentArray(parent, mt, val * (mt - 3))
depth++
continue
}
break
case MT.TAG:
this.emit('start', mt, val, parent_major, parent_length)
parent = parentArray(parent, mt, 1)
parent.push(val)
depth++
continue
case MT.SIMPLE_FLOAT:
if (typeof val === 'number') {
if ((ai === NUMBYTES.ONE) && (val < 32)) {
throw new Error(
`Invalid two-byte encoding of simple value ${val}`
)
}
const hasParent = (parent != null)
val = Simple.decode(
val,
hasParent,
hasParent && (parent[COUNT] < 0)
)
} else {
val = utils.parseCBORfloat(val)
}
}
this.emit('value', val, parent_major, parent_length, ai)
let again = false
while (parent != null) {
if (val === SYMS.BREAK) {
parent[COUNT] = 1
} else if (Array.isArray(parent)) {
parent.push(val)
} else {
const pm = parent[MAJOR]
if ((pm != null) && (pm !== mt)) {
this.running = false
throw new Error('Invalid major type in indefinite encoding')
}
parent.write(val)
}
if ((--parent[COUNT]) !== 0) {
again = true
break
}
--depth
delete parent[COUNT]
if (Array.isArray(parent)) {
switch (parent[MAJOR]) {
case MT.ARRAY:
val = parent
break
case MT.MAP: {
let allstrings = !this.preferMap
if ((parent.length % 2) !== 0) {
throw new Error(`Invalid map length: ${parent.length}`)
}
for (
let i = 0, len = parent.length;
allstrings && (i < len);
i += 2
) {
if ((typeof parent[i] !== 'string') ||
(parent[i] === '__proto__')) {
allstrings = false
break
}
}
if (allstrings) {
val = {}
for (let i = 0, len = parent.length; i < len; i += 2) {
if (this.preventDuplicateKeys &&
Object.prototype.hasOwnProperty.call(val, parent[i])) {
throw new Error('Duplicate keys in a map')
}
val[parent[i]] = parent[i + 1]
}
} else {
val = new Map()
for (let i = 0, len = parent.length; i < len; i += 2) {
if (this.preventDuplicateKeys && val.has(parent[i])) {
throw new Error('Duplicate keys in a map')
}
val.set(parent[i], parent[i + 1])
}
}
break
}
case MT.TAG: {
const t = new Tagged(parent[0], parent[1])
val = t.convert(this.tags)
break
}
}
} else if (parent instanceof NoFilter) {
switch (parent[MAJOR]) {
case MT.BYTE_STRING:
val = parent.slice()
if (this.preferWeb) {
val = new Uint8Array(
(val).buffer,
(val).byteOffset,
(val).length
)
}
break
case MT.UTF8_STRING:
val = parent.toString('utf-8')
break
}
}
this.emit('stop', parent[MAJOR])
const old = parent
parent = parent[SYMS.PARENT]
delete old[SYMS.PARENT]
delete old[MAJOR]
}
if (!again) {
if (this.extendedResults) {
const bytes = this.valueBytes.slice()
const ret = {
value: Decoder.nullcheck(val),
bytes,
length: bytes.length,
}
this.valueBytes = new NoFilter()
return ret
}
return val
}
}
}
}
Decoder.NOT_FOUND = NOT_FOUND
module.exports = Decoder