import ./blobsets/priv/hex import base32, cbor, siphash, tiger import std/asyncdispatch, std/asyncstreams import std/hashes, std/streams, std/strutils, std/bitops, std/unicode, std/endians, std/random const digestSize* = 24 ## Length of a chunk digest. blobLeafSize* = 1 shl 10 ## Size of blob hash leaves (THEX/ADC). blobLeafSizeMask* = blobLeafSize - 1 blobHexLen* = digestSize * 2 blobBase32Len* = (digestSize * 5 div 3) - 1 blobVisualLen* = digestSize * 3 type BlobId* = TigerDigest ## Blob Identifier SetId* = TigerDigest ## Set Identifier func parseStringId[T](s: string): T = case s.len of blobHexLen: hex.decode s, result.data of blobBase32Len: var tmp = base32.decode(s) copyMem(result.data[0].addr, tmp[0].addr, digestSize) of blobVisualLen: var pos: int r: Rune for b in result.data.mitems: fastRuneAt(s, pos, r, true) b = byte(r.int and 0xff) else: raise newException(ValueError, "invalid blobset id encoding of len " & $s.len) func parseCborId[T](c: CborNode): T = ## Parse a CBOR node to binary. if c.bytes.len == result.data.len: for i in 0..result.data.high: result.data[i] = c.bytes[i] func toBlobId*(s: string): BlobId = ## Parse a visual blob hash to binary. parseStringId[BlobId] s func toSetId*(s: string): SetId = ## Parse a visual set hash to binary. parseStringId[SetId] s func toSetId(c: CborNode): SetId = ## Parse a CBOR set hash to binary. parseCborId[SetId] c proc `==`*(x, y: BlobId): bool = x.data == y.data ## Compare two BlobIds. proc `==`*(cbor: CborNode; cid: BlobId): bool = ## Compare a CBOR node with a BlobId. if cbor.kind == cborBytes: for i in 0..