2022-07-01 18:19:57 +00:00
|
|
|
# Nim-Libp2p
|
|
|
|
# Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2018-11-20 21:39:35 +00:00
|
|
|
## This module implements Variable Integer `VARINT`.
|
|
|
|
## This module supports two variants of variable integer
|
|
|
|
## - Google ProtoBuf varint, which is able to encode full uint64 number and
|
|
|
|
## maximum size of encoded value is 10 octets (bytes).
|
2020-01-29 09:14:25 +00:00
|
|
|
## https://developers.google.com/protocol-buffers/docs/encoding#varints
|
2018-11-20 21:39:35 +00:00
|
|
|
## - LibP2P varint, which is able to encode only 63bits of uint64 number and
|
|
|
|
## maximum size of encoded value is 9 octets (bytes).
|
2020-01-29 09:14:25 +00:00
|
|
|
## https://github.com/multiformats/unsigned-varint
|
2020-05-31 14:22:49 +00:00
|
|
|
|
2022-08-03 11:33:19 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2020-05-31 14:22:49 +00:00
|
|
|
|
2020-12-15 18:15:22 +00:00
|
|
|
import stew/[byteutils, leb128, results]
|
|
|
|
export leb128, results
|
2018-11-19 02:52:11 +00:00
|
|
|
|
|
|
|
type
|
2020-05-31 14:22:49 +00:00
|
|
|
VarintError* {.pure.} = enum
|
2018-11-19 02:52:11 +00:00
|
|
|
Error,
|
|
|
|
Overflow,
|
|
|
|
Incomplete,
|
2020-01-28 15:29:42 +00:00
|
|
|
Overlong,
|
2018-11-19 02:52:11 +00:00
|
|
|
Overrun
|
|
|
|
|
2020-05-31 14:22:49 +00:00
|
|
|
VarintResult*[T] = Result[T, VarintError]
|
|
|
|
|
2018-11-20 21:39:35 +00:00
|
|
|
PB* = object
|
|
|
|
## Use this type to specify Google ProtoBuf's varint encoding
|
|
|
|
LP* = object
|
|
|
|
## Use this type to specify LibP2P varint encoding
|
|
|
|
|
2020-03-06 19:19:43 +00:00
|
|
|
zint32* = distinct int32
|
|
|
|
zint64* = distinct int64
|
|
|
|
zint* = distinct int
|
|
|
|
## Signed integer types which will be encoded using zigzag encoding.
|
|
|
|
|
|
|
|
hint32* = distinct int32
|
|
|
|
hint64* = distinct int64
|
|
|
|
hint* = distinct int
|
|
|
|
## Signed integer types which will be encoded using simple cast.
|
|
|
|
|
2018-11-20 21:39:35 +00:00
|
|
|
PBSomeUVarint* = uint | uint64 | uint32
|
2020-03-06 19:19:43 +00:00
|
|
|
PBSomeSVarint* = hint | hint64 | hint32
|
|
|
|
PBZigVarint* = zint | zint64 | zint32
|
|
|
|
PBSomeVarint* = PBSomeUVarint | PBSomeSVarint | PBZigVarint
|
2018-11-20 21:39:35 +00:00
|
|
|
LPSomeUVarint* = uint | uint64 | uint32 | uint16 | uint8
|
|
|
|
LPSomeVarint* = LPSomeUVarint
|
|
|
|
SomeVarint* = PBSomeVarint | LPSomeVarint
|
|
|
|
SomeUVarint* = PBSomeUVarint | LPSomeUVarint
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2022-12-09 16:23:33 +00:00
|
|
|
template toUleb[T: uint64|uint32|uint16|uint8|uint](x: T): T = x
|
2020-03-06 19:19:43 +00:00
|
|
|
|
2020-12-15 18:15:22 +00:00
|
|
|
func toUleb(x: zint64): uint64 =
|
|
|
|
let v = cast[uint64](x)
|
|
|
|
(v shl 1) xor (0 - (v shr 63))
|
|
|
|
|
|
|
|
func toUleb(x: zint32): uint32 =
|
|
|
|
let v = cast[uint32](x)
|
|
|
|
(v shl 1) xor (0 - (v shr 31))
|
|
|
|
|
|
|
|
template toUleb(x: hint64): uint64 = cast[uint64](x)
|
|
|
|
template toUleb(x: hint32): uint32 = cast[uint32](x)
|
|
|
|
|
|
|
|
template toUleb(x: zint): uint64 =
|
|
|
|
when sizeof(x) == sizeof(zint64):
|
|
|
|
uint(toUleb(zint64(x)))
|
2020-03-06 19:19:43 +00:00
|
|
|
else:
|
2020-12-15 18:15:22 +00:00
|
|
|
uint(toUleb(zint32(x)))
|
2020-03-06 19:19:43 +00:00
|
|
|
|
2020-12-15 18:15:22 +00:00
|
|
|
template toUleb(x: hint): uint =
|
|
|
|
when sizeof(x) == sizeof(hint64):
|
|
|
|
uint(toUleb(hint64(x)))
|
2020-03-06 19:19:43 +00:00
|
|
|
else:
|
2020-12-15 18:15:22 +00:00
|
|
|
uint(toUleb(hint32(x)))
|
|
|
|
|
|
|
|
template fromUleb(x: uint64, T: type uint64): T = x
|
|
|
|
template fromUleb(x: uint32, T: type uint32): T = x
|
|
|
|
|
|
|
|
template fromUleb(x: uint64, T: type zint64): T =
|
|
|
|
cast[T]((x shr 1) xor (0 - (x and 1)))
|
|
|
|
|
|
|
|
template fromUleb(x: uint32, T: type zint32): T =
|
|
|
|
cast[T]((x shr 1) xor (0 - (x and 1)))
|
|
|
|
|
|
|
|
template fromUleb(x: uint64, T: type hint64): T =
|
|
|
|
cast[T](x)
|
|
|
|
|
|
|
|
template fromUleb(x: uint32, T: type hint32): T =
|
|
|
|
cast[T](x)
|
|
|
|
|
|
|
|
proc vsizeof*(x: SomeVarint): int {.inline.} =
|
|
|
|
## Returns number of bytes required to encode integer ``x`` as varint.
|
|
|
|
Leb128.len(toUleb(x))
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2018-11-20 21:39:35 +00:00
|
|
|
proc getUVarint*[T: PB|LP](vtype: typedesc[T],
|
2021-12-16 10:05:20 +00:00
|
|
|
pbytes: openArray[byte],
|
2018-11-20 21:39:35 +00:00
|
|
|
outlen: var int,
|
2020-05-31 14:22:49 +00:00
|
|
|
outval: var SomeUVarint): VarintResult[void] =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Decode `unsigned varint` from buffer ``pbytes`` and store it to ``outval``.
|
|
|
|
## On success ``outlen`` will be set to number of bytes processed while
|
|
|
|
## decoding `unsigned varint`.
|
|
|
|
##
|
|
|
|
## If array ``pbytes`` is empty, ``Incomplete`` error will be returned.
|
|
|
|
##
|
|
|
|
## If there not enough bytes available in array ``pbytes`` to decode `unsigned
|
|
|
|
## varint`, ``Incomplete`` error will be returned.
|
|
|
|
##
|
|
|
|
## If encoded value can produce integer overflow, ``Overflow`` error will be
|
|
|
|
## returned.
|
|
|
|
##
|
2018-11-20 21:39:35 +00:00
|
|
|
## Google ProtoBuf
|
|
|
|
## When decoding 10th byte of Google Protobuf's 64bit integer only 1 bit from
|
|
|
|
## byte will be decoded, all other bits will be ignored. When decoding 5th
|
|
|
|
## byte of 32bit integer only 4 bits from byte will be decoded, all other bits
|
|
|
|
## will be ignored.
|
2019-06-13 09:05:03 +00:00
|
|
|
##
|
2018-11-20 21:39:35 +00:00
|
|
|
## LibP2P
|
|
|
|
## When decoding 5th byte of 32bit integer only 4 bits from byte will be
|
|
|
|
## decoded, all other bits will be ignored.
|
2018-11-19 02:52:11 +00:00
|
|
|
outlen = 0
|
2020-03-06 19:19:43 +00:00
|
|
|
outval = type(outval)(0)
|
2020-12-15 18:15:22 +00:00
|
|
|
|
|
|
|
let parsed = type(outval).fromBytes(pbytes, Leb128)
|
|
|
|
|
|
|
|
if parsed.len == 0:
|
|
|
|
return err(VarintError.Incomplete)
|
|
|
|
if parsed.len < 0:
|
|
|
|
return err(VarintError.Overflow)
|
|
|
|
|
|
|
|
when vtype is LP and sizeof(outval) == 8:
|
|
|
|
if parsed.val >= 0x8000_0000_0000_0000'u64:
|
2020-05-31 14:22:49 +00:00
|
|
|
return err(VarintError.Overflow)
|
2020-12-15 18:15:22 +00:00
|
|
|
|
|
|
|
if vsizeof(parsed.val) != parsed.len:
|
|
|
|
return err(VarintError.Overlong)
|
|
|
|
|
|
|
|
(outval, outlen) = parsed
|
|
|
|
|
|
|
|
ok()
|
2020-01-28 15:29:42 +00:00
|
|
|
|
2018-11-20 21:39:35 +00:00
|
|
|
proc putUVarint*[T: PB|LP](vtype: typedesc[T],
|
2021-12-16 10:05:20 +00:00
|
|
|
pbytes: var openArray[byte],
|
2018-11-20 21:39:35 +00:00
|
|
|
outlen: var int,
|
2020-05-31 14:22:49 +00:00
|
|
|
outval: SomeUVarint): VarintResult[void] =
|
2018-11-19 02:52:11 +00:00
|
|
|
## Encode `unsigned varint` ``outval`` and store it to array ``pbytes``.
|
|
|
|
##
|
|
|
|
## On success ``outlen`` will hold number of bytes (octets) used to encode
|
|
|
|
## unsigned integer ``v``.
|
|
|
|
##
|
|
|
|
## If there not enough bytes available in buffer ``pbytes``, ``Incomplete``
|
|
|
|
## error will be returned and ``outlen`` will be set to number of bytes
|
|
|
|
## required.
|
|
|
|
##
|
2018-11-20 21:39:35 +00:00
|
|
|
## Google ProtoBuf
|
2018-11-19 02:52:11 +00:00
|
|
|
## Maximum encoded length of 64bit integer is 10 octets.
|
|
|
|
## Maximum encoded length of 32bit integer is 5 octets.
|
2019-06-13 09:05:03 +00:00
|
|
|
##
|
2018-11-20 21:39:35 +00:00
|
|
|
## LibP2P
|
|
|
|
## Maximum encoded length of 63bit integer is 9 octets.
|
|
|
|
## Maximum encoded length of 32bit integer is 5 octets.
|
2020-12-15 18:15:22 +00:00
|
|
|
when vtype is LP and sizeof(outval) == 8:
|
|
|
|
if (uint64(outval) and 0x8000_0000_0000_0000'u64) != 0'u64:
|
|
|
|
return err(VarintError.Overflow)
|
|
|
|
|
|
|
|
let bytes = toBytes(outval, Leb128)
|
|
|
|
outlen = len(bytes)
|
|
|
|
if len(pbytes) >= outlen:
|
|
|
|
pbytes[0..<outlen] = bytes.toOpenArray()
|
2020-05-31 14:22:49 +00:00
|
|
|
ok()
|
2018-11-19 02:52:11 +00:00
|
|
|
else:
|
2020-05-31 14:22:49 +00:00
|
|
|
err(VarintError.Overrun)
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc getSVarint*(pbytes: openArray[byte], outsize: var int,
|
2020-12-15 18:15:22 +00:00
|
|
|
outval: var (PBZigVarint | PBSomeSVarint)): VarintResult[void] {.inline.} =
|
2020-03-06 19:19:43 +00:00
|
|
|
## Decode signed integer (``int32`` or ``int64``) from buffer ``pbytes``
|
|
|
|
## and store it to ``outval``.
|
|
|
|
##
|
|
|
|
## On success ``outlen`` will be set to number of bytes processed while
|
|
|
|
## decoding signed varint.
|
2018-11-19 02:52:11 +00:00
|
|
|
##
|
|
|
|
## If array ``pbytes`` is empty, ``Incomplete`` error will be returned.
|
|
|
|
##
|
|
|
|
## If there not enough bytes available in array ``pbytes`` to decode `signed
|
|
|
|
## varint`, ``Incomplete`` error will be returned.
|
|
|
|
##
|
|
|
|
## If encoded value can produce integer overflow, ``Overflow`` error will be
|
|
|
|
## returned.
|
|
|
|
##
|
|
|
|
## Note, when decoding 10th byte of 64bit integer only 1 bit from byte will be
|
|
|
|
## decoded, all other bits will be ignored. When decoding 5th byte of 32bit
|
|
|
|
## integer only 4 bits from byte will be decoded, all other bits will be
|
|
|
|
## ignored.
|
|
|
|
when sizeof(outval) == 8:
|
|
|
|
var value: uint64
|
|
|
|
else:
|
|
|
|
var value: uint32
|
|
|
|
|
2020-05-31 14:22:49 +00:00
|
|
|
let res = PB.getUVarint(pbytes, outsize, value)
|
|
|
|
if res.isOk():
|
2020-12-15 18:15:22 +00:00
|
|
|
outval = fromUleb(value, type(outval))
|
2020-05-31 14:22:49 +00:00
|
|
|
res
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc putSVarint*(pbytes: var openArray[byte], outsize: var int,
|
2020-12-15 18:15:22 +00:00
|
|
|
outval: (PBZigVarint | PBSomeSVarint)): VarintResult[void] {.inline.} =
|
2020-03-06 19:19:43 +00:00
|
|
|
## Encode signed integer ``outval`` using ProtoBuffer's zigzag encoding
|
|
|
|
## (``sint32`` or ``sint64``) and store it to array ``pbytes``.
|
2018-11-19 02:52:11 +00:00
|
|
|
##
|
|
|
|
## On success ``outlen`` will hold number of bytes (octets) used to encode
|
|
|
|
## unsigned integer ``v``.
|
|
|
|
##
|
|
|
|
## If there not enough bytes available in buffer ``pbytes``, ``Incomplete``
|
|
|
|
## error will be returned and ``outlen`` will be set to number of bytes
|
|
|
|
## required.
|
|
|
|
##
|
|
|
|
## Maximum encoded length of 64bit integer is 10 octets.
|
|
|
|
## Maximum encoded length of 32bit integer is 5 octets.
|
2020-12-15 18:15:22 +00:00
|
|
|
PB.putUVarint(pbytes, outsize, toUleb(outval))
|
2020-03-06 19:19:43 +00:00
|
|
|
|
|
|
|
template varintFatal(msg) =
|
|
|
|
const m = msg
|
|
|
|
{.fatal: m.}
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc putVarint*[T: PB|LP](vtype: typedesc[T], pbytes: var openArray[byte],
|
2020-05-31 14:22:49 +00:00
|
|
|
nbytes: var int, value: SomeVarint): VarintResult[void] {.inline.} =
|
2020-03-06 19:19:43 +00:00
|
|
|
when vtype is PB:
|
|
|
|
when (type(value) is PBSomeSVarint) or (type(value) is PBZigVarint):
|
2020-05-31 14:22:49 +00:00
|
|
|
putSVarint(pbytes, nbytes, value)
|
2020-03-06 19:19:43 +00:00
|
|
|
elif (type(value) is PBSomeUVarint):
|
2020-05-31 14:22:49 +00:00
|
|
|
PB.putUVarint(pbytes, nbytes, value)
|
2020-03-06 19:19:43 +00:00
|
|
|
else:
|
|
|
|
varintFatal("Protobuf's varint do not support type [" &
|
|
|
|
typetraits.name(type(value)) & "]")
|
|
|
|
elif vtype is LP:
|
|
|
|
when (type(value) is LPSomeVarint):
|
2020-05-31 14:22:49 +00:00
|
|
|
LP.putUVarint(pbytes, nbytes, value)
|
2020-03-06 19:19:43 +00:00
|
|
|
else:
|
|
|
|
varintFatal("LibP2P's varint do not support type [" &
|
|
|
|
typetraits.name(type(value)) & "]")
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc getVarint*[T: PB|LP](vtype: typedesc[T], pbytes: openArray[byte],
|
2020-03-06 19:19:43 +00:00
|
|
|
nbytes: var int,
|
2020-05-31 14:22:49 +00:00
|
|
|
value: var SomeVarint): VarintResult[void] {.inline.} =
|
2020-03-06 19:19:43 +00:00
|
|
|
when vtype is PB:
|
|
|
|
when (type(value) is PBSomeSVarint) or (type(value) is PBZigVarint):
|
2020-05-31 14:22:49 +00:00
|
|
|
getSVarint(pbytes, nbytes, value)
|
2020-03-06 19:19:43 +00:00
|
|
|
elif (type(value) is PBSomeUVarint):
|
2020-05-31 14:22:49 +00:00
|
|
|
PB.getUVarint(pbytes, nbytes, value)
|
2020-03-06 19:19:43 +00:00
|
|
|
else:
|
|
|
|
varintFatal("Protobuf's varint do not support type [" &
|
|
|
|
typetraits.name(type(value)) & "]")
|
|
|
|
elif vtype is LP:
|
|
|
|
when (type(value) is LPSomeVarint):
|
2020-05-31 14:22:49 +00:00
|
|
|
LP.getUVarint(pbytes, nbytes, value)
|
2020-03-06 19:19:43 +00:00
|
|
|
else:
|
|
|
|
varintFatal("LibP2P's varint do not support type [" &
|
|
|
|
typetraits.name(type(value)) & "]")
|
|
|
|
|
2020-12-15 18:15:22 +00:00
|
|
|
template toBytes*(vtype: typedesc[PB], value: PBSomeVarint): auto =
|
|
|
|
toBytes(toUleb(value), Leb128)
|
|
|
|
|
2018-11-20 21:39:35 +00:00
|
|
|
proc encodeVarint*(vtype: typedesc[PB],
|
2020-05-31 14:22:49 +00:00
|
|
|
value: PBSomeVarint): VarintResult[seq[byte]] {.inline.} =
|
2018-11-20 21:39:35 +00:00
|
|
|
## Encode integer to Google ProtoBuf's `signed/unsigned varint` and returns
|
2020-05-31 14:22:49 +00:00
|
|
|
## sequence of bytes as buffer.
|
2018-11-19 02:52:11 +00:00
|
|
|
var outsize = 0
|
2020-05-31 14:22:49 +00:00
|
|
|
var buffer = newSeqOfCap[byte](10)
|
2018-11-19 02:52:11 +00:00
|
|
|
when sizeof(value) == 4:
|
2020-05-31 14:22:49 +00:00
|
|
|
buffer.setLen(5)
|
2018-11-19 02:52:11 +00:00
|
|
|
else:
|
2020-05-31 14:22:49 +00:00
|
|
|
buffer.setLen(10)
|
2020-03-06 19:19:43 +00:00
|
|
|
when (type(value) is PBSomeSVarint) or (type(value) is PBZigVarint):
|
2020-05-31 14:22:49 +00:00
|
|
|
let res = putSVarint(buffer, outsize, value)
|
2018-11-19 02:52:11 +00:00
|
|
|
else:
|
2020-05-31 14:22:49 +00:00
|
|
|
let res = PB.putUVarint(buffer, outsize, value)
|
|
|
|
if res.isOk():
|
|
|
|
buffer.setLen(outsize)
|
|
|
|
ok(buffer)
|
2018-11-19 02:52:11 +00:00
|
|
|
else:
|
2020-05-31 14:22:49 +00:00
|
|
|
err(res.error())
|
2018-11-19 02:52:11 +00:00
|
|
|
|
2018-11-20 21:39:35 +00:00
|
|
|
proc encodeVarint*(vtype: typedesc[LP],
|
2020-05-31 14:22:49 +00:00
|
|
|
value: LPSomeVarint): VarintResult[seq[byte]] {.inline.} =
|
2018-11-20 21:39:35 +00:00
|
|
|
## Encode integer to LibP2P `unsigned varint` and returns sequence of bytes
|
2020-05-31 14:22:49 +00:00
|
|
|
## as buffer.
|
2018-11-20 21:39:35 +00:00
|
|
|
var outsize = 0
|
2020-05-31 14:22:49 +00:00
|
|
|
var buffer = newSeqOfCap[byte](9)
|
2018-11-20 21:39:35 +00:00
|
|
|
when sizeof(value) == 1:
|
2020-05-31 14:22:49 +00:00
|
|
|
buffer.setLen(2)
|
2018-11-20 21:39:35 +00:00
|
|
|
elif sizeof(value) == 2:
|
2020-05-31 14:22:49 +00:00
|
|
|
buffer.setLen(3)
|
2018-11-20 21:39:35 +00:00
|
|
|
elif sizeof(value) == 4:
|
2020-05-31 14:22:49 +00:00
|
|
|
buffer.setLen(5)
|
2018-11-20 21:39:35 +00:00
|
|
|
else:
|
2020-05-31 14:22:49 +00:00
|
|
|
buffer.setLen(9)
|
|
|
|
let res = LP.putUVarint(buffer, outsize, value)
|
|
|
|
if res.isOk():
|
|
|
|
buffer.setLen(outsize)
|
|
|
|
ok(buffer)
|
2018-11-20 21:39:35 +00:00
|
|
|
else:
|
2020-05-31 14:22:49 +00:00
|
|
|
err(res.error)
|