Do not raise ValueError at early end of binary stream

This commit is contained in:
Emery Hemingway 2023-08-25 18:20:01 +01:00
parent 52faec6e43
commit 8b3b182ce7
2 changed files with 5 additions and 7 deletions

View File

@ -1,6 +1,6 @@
# Package
version = "20230823"
version = "20230825"
author = "Emery Hemingway"
description = "data model and serialization format"
license = "Unlicense"

View File

@ -631,12 +631,10 @@ proc encode*[E](pr: Preserve[E]): seq[byte] =
s.write pr
result = cast[seq[byte]](move s.data)
proc decodePreserves*(s: Stream; E = void): Preserve[E] =
## Decode a Preserves value from a binary-encoded stream.
if s.atEnd: raise newException(ValueError, "End of Preserves stream")
proc assertStream(check: bool) =
if not check:
raise newException(ValueError, "invalid Preserves stream")
if s.atEnd: raise newException(IOError, "End of Preserves stream")
const endMarker = 0x84
let tag = s.readUint8()
case tag
@ -715,7 +713,7 @@ proc decodePreserves*(s: Stream; E = void): Preserve[E] =
for _ in 1..len:
result.int = (result.int shl 8) + s.readUint8().BiggestInt
of endMarker:
assertStream(false)
raise newException(ValueError, "invalid Preserves stream")
else:
case 0xf0 and tag
of 0x90:
@ -731,7 +729,7 @@ proc decodePreserves*(s: Stream; E = void): Preserve[E] =
for i in 1..<len:
result.int = (result.int shl 8) or s.readUint8().BiggestInt
else:
assertStream(false)
raise newException(ValueError, "invalid Preserves stream")
proc decodePreserves*(s: string; E = void): Preserve[E] =
## Decode a string of binary-encoded Preserves.