Do not raise ValueError at early end of binary stream
This commit is contained in:
parent
52faec6e43
commit
8b3b182ce7
|
@ -1,6 +1,6 @@
|
||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "20230823"
|
version = "20230825"
|
||||||
author = "Emery Hemingway"
|
author = "Emery Hemingway"
|
||||||
description = "data model and serialization format"
|
description = "data model and serialization format"
|
||||||
license = "Unlicense"
|
license = "Unlicense"
|
||||||
|
|
|
@ -631,12 +631,10 @@ proc encode*[E](pr: Preserve[E]): seq[byte] =
|
||||||
s.write pr
|
s.write pr
|
||||||
result = cast[seq[byte]](move s.data)
|
result = cast[seq[byte]](move s.data)
|
||||||
|
|
||||||
|
|
||||||
proc decodePreserves*(s: Stream; E = void): Preserve[E] =
|
proc decodePreserves*(s: Stream; E = void): Preserve[E] =
|
||||||
## Decode a Preserves value from a binary-encoded stream.
|
## Decode a Preserves value from a binary-encoded stream.
|
||||||
if s.atEnd: raise newException(ValueError, "End of Preserves stream")
|
if s.atEnd: raise newException(IOError, "End of Preserves stream")
|
||||||
proc assertStream(check: bool) =
|
|
||||||
if not check:
|
|
||||||
raise newException(ValueError, "invalid Preserves stream")
|
|
||||||
const endMarker = 0x84
|
const endMarker = 0x84
|
||||||
let tag = s.readUint8()
|
let tag = s.readUint8()
|
||||||
case tag
|
case tag
|
||||||
|
@ -715,7 +713,7 @@ proc decodePreserves*(s: Stream; E = void): Preserve[E] =
|
||||||
for _ in 1..len:
|
for _ in 1..len:
|
||||||
result.int = (result.int shl 8) + s.readUint8().BiggestInt
|
result.int = (result.int shl 8) + s.readUint8().BiggestInt
|
||||||
of endMarker:
|
of endMarker:
|
||||||
assertStream(false)
|
raise newException(ValueError, "invalid Preserves stream")
|
||||||
else:
|
else:
|
||||||
case 0xf0 and tag
|
case 0xf0 and tag
|
||||||
of 0x90:
|
of 0x90:
|
||||||
|
@ -731,7 +729,7 @@ proc decodePreserves*(s: Stream; E = void): Preserve[E] =
|
||||||
for i in 1..<len:
|
for i in 1..<len:
|
||||||
result.int = (result.int shl 8) or s.readUint8().BiggestInt
|
result.int = (result.int shl 8) or s.readUint8().BiggestInt
|
||||||
else:
|
else:
|
||||||
assertStream(false)
|
raise newException(ValueError, "invalid Preserves stream")
|
||||||
|
|
||||||
proc decodePreserves*(s: string; E = void): Preserve[E] =
|
proc decodePreserves*(s: string; E = void): Preserve[E] =
|
||||||
## Decode a string of binary-encoded Preserves.
|
## Decode a string of binary-encoded Preserves.
|
||||||
|
|
Loading…
Reference in New Issue