Created
August 1, 2015 22:10
-
-
Save searler/984145e4485e375b32ef to your computer and use it in GitHub Desktop.
scodec XOR checksum Codec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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