Skip to content

Commit

Permalink
Make varint decoder play well with streaming
Browse files Browse the repository at this point in the history
If more bits are expected but none is here, emit an `InsufficientBits`
error, so that `scodec-stream` can ask for more and retry.

Closes satabin#91
  • Loading branch information
satabin committed Jul 13, 2020
1 parent 3053c0d commit c9aaa00
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions core/src/swam/binary/varint.scala
Expand Up @@ -46,7 +46,7 @@ private class Varuint(bits: Int) extends Codec[Int] {
if (n <= 0) {
Attempt.failure(Err("integer representation too long"))
} else if (buffer.isEmpty) {
Attempt.failure(Err("unexpected end of input"))
Attempt.failure(Err.insufficientBits(8, 0))
} else {
val byte = buffer.head
if (n >= 7 || (byte & 0x7f) < (1 << n)) {
Expand Down Expand Up @@ -95,8 +95,8 @@ private class Varint(bits: Int) extends Codec[Long] {

private val bitsL = bits.toLong

val MaxValue = (1 << (bits - 1)) - 1
val MinValue = -(1 << (bits - 1))
val MaxValue = if (bits == 64) Long.MaxValue else (1L << (bits - 1)) - 1
val MinValue = if (bits == 64) Long.MinValue else -(1L << (bits - 1))

private def description = s"$bits-bit signed leb128 integer"

Expand All @@ -110,7 +110,7 @@ private class Varint(bits: Int) extends Codec[Long] {
if (n <= 0) {
Attempt.failure(Err("integer representation too long"))
} else if (buffer.isEmpty) {
Attempt.failure(Err("unexpected end of input"))
Attempt.failure(Err.insufficientBits(8, 0))
} else {
val byte = buffer.head
val mask = (-1 << (n - 1)) & 0x7f
Expand Down

0 comments on commit c9aaa00

Please sign in to comment.