101 lines
3.7 KiB
Luau
101 lines
3.7 KiB
Luau
--!strict
|
|
-- data/cbor: CBOR (RFC 8949). New code. Supports: every major type, indefinite-length strings / arrays / maps, half /
|
|
-- single / double floats, tags (0 / 1 date-times as Types.Date, 2 / 3 bignums as numbers or exact decimal text,
|
|
-- 55799 self-describe stripped, others kept as Cbor.tagged(tag, value)), simple values (Cbor.simple(n),
|
|
-- Cbor.undefined), byte strings as buffers, maps as Types.object (key order kept; keys of any type), sequences
|
|
-- (RFC 8742, opts.all). Encode: preferred (shortest) integer and length heads, float64 unless opts.compactFloats,
|
|
-- definite lengths. Not supported: other semantic tags (decimal fractions, bigfloats, ...) stay tagged values.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Codec = require("@formats/data/cbor/Codec")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Cbor = {
|
|
name = "cbor",
|
|
category = "data",
|
|
extensions = { "cbor" },
|
|
mime = { "application/cbor", "application/cbor-seq" },
|
|
deps = { "core/Err", "core/Bytes", "Types" },
|
|
caps = { decode = true, encode = true, binary = true, ordered = true, tags = true },
|
|
}
|
|
|
|
export type DecodeOptions = Codec.DecodeOptions & {
|
|
all: boolean?, -- a CBOR sequence: every item into an array (default false: exactly one item)
|
|
}
|
|
export type EncodeOptions = Codec.EncodeOptions & {
|
|
all: boolean?, -- the value is an array of items, written as a sequence
|
|
selfDescribe: boolean?, -- prefix the 55799 self-describe tag (d9 d9 f7)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Cbor.sniff(r: Bytes.Reader): number
|
|
return if r:peek(3) == "\217\217\247" then 1 else 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
local function codeOf(e: string): string
|
|
if string.find(e, "truncated", 1, true) or string.find(e, "past the end", 1, true) then
|
|
return Err.TRUNCATED
|
|
elseif string.find(e, "nested deeper", 1, true) then
|
|
return Err.LIMIT
|
|
end
|
|
return Err.CORRUPT
|
|
end
|
|
|
|
function Cbor.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
|
local o: DecodeOptions = opts or {}
|
|
if o.all then
|
|
local out = {}
|
|
while not r:eof() do
|
|
local v, e, at = Codec.decode(r, o)
|
|
if e then
|
|
return nil, Err.new(codeOf(e), e, at)
|
|
end
|
|
table.insert(out, v)
|
|
end
|
|
return out, nil
|
|
end
|
|
local v, e, at = Codec.decode(r, o)
|
|
if e then
|
|
return nil, Err.new(codeOf(e), e, at)
|
|
end
|
|
if not r:eof() then
|
|
return nil, Err.new(Err.CORRUPT, "trailing data after the item (opts.all reads a sequence)", r.pos - r.base)
|
|
end
|
|
return v, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function Cbor.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
local w = Bytes.writer()
|
|
if opts and opts.selfDescribe then
|
|
w:string("\217\217\247")
|
|
end
|
|
local items = { value }
|
|
if opts and opts.all then
|
|
if type(value) ~= "table" then
|
|
return nil, Err.new(Err.ARG, "all: expected an array of items")
|
|
end
|
|
items = value
|
|
end
|
|
for _, v in items do
|
|
local b, e = Codec.encode(v, opts)
|
|
if not b then
|
|
return nil, Err.new(Err.ARG, e or "cannot encode")
|
|
end
|
|
w:bytes(b)
|
|
end
|
|
return w:finish(), nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ extras
|
|
Cbor.tagged = Codec.tagged -- tagged(tag, value) -> a value written as that tag
|
|
Cbor.isTagged = Codec.isTagged
|
|
Cbor.simple = Codec.simple -- simple(n) -> a simple value
|
|
Cbor.isSimple = Codec.isSimple
|
|
Cbor.undefined = Codec.undefined
|
|
|
|
return Cbor
|