Skip to content

Instantly share code, notes, and snippets.

@searler
Created August 1, 2015 22:10
Show Gist options
  • Save searler/984145e4485e375b32ef to your computer and use it in GitHub Desktop.
Save searler/984145e4485e375b32ef to your computer and use it in GitHub Desktop.
scodec XOR checksum Codec
object XorChecksumCodec {
def xorByte(bits: BitVector): BitVector = BitVector(bits.bytes.foldLeft[Byte](0)((a, b) => (a ^ b).asInstanceOf[Byte]))
def apply[T](target: Codec[T]) = checksummed(target, xorByte, trailer(target), true)
def trailer[T](target: Codec[T]): Codec[(BitVector, BitVector)] = new Codec[(BitVector, BitVector)] {
def sizeBound = target.sizeBound + new SizeBound(0, Some(8L))
def encode(value: (BitVector, BitVector)): Attempt[BitVector] = Attempt.successful(value._1 ++ value._2)
def decode(bits: BitVector): Attempt[DecodeResult[(BitVector, BitVector)]] =
Attempt.successful(DecodeResult((bits.dropRight(8), bits.takeRight(8)), BitVector.empty))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment