CRC-protected frames
This commit is contained in:
1 parent
ee2c958445
commit
8c73ec9e30
8 files changed
+155
-27
No files matched your search
@@ -0,0 +1,26 @@
|
||||
package net.sergeych.bintools
|
||||
|
||||
/**
|
||||
* The filter-type sink that calculates CRC32 on the fly passing data to
|
||||
* another sink
|
||||
*/
|
||||
class CRC32Sink(val sink: DataSink,
|
||||
polynomial:UInt = 0x04C11DB7U): DataSink {
|
||||
|
||||
private val checksum = CRC32(polynomial)
|
||||
override fun writeByte(data: Byte) {
|
||||
checksum.update(data.toUByte())
|
||||
sink.writeByte(data)
|
||||
}
|
||||
|
||||
override fun writeBytes(data: ByteArray) {
|
||||
checksum.update(data.toUByteArray())
|
||||
sink.writeBytes(data)
|
||||
}
|
||||
|
||||
/**
|
||||
* current value of the CRC32 checkcum, using the bytes that
|
||||
* were written by now.
|
||||
*/
|
||||
val crc: UInt get() = checksum.value
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package net.sergeych.bintools
|
||||
|
||||
class CRC32Source(
|
||||
val source: DataSource,
|
||||
polynomial: UInt = 0x04C11DB7U
|
||||
): DataSource {
|
||||
|
||||
private val checksum = CRC32(polynomial)
|
||||
override fun readByte(): Byte = source.readByte().also {
|
||||
checksum.update(it.toUByte())
|
||||
}
|
||||
|
||||
override fun readBytes(size: Int): ByteArray = source.readBytes(size).also {
|
||||
checksum.update(it.toUByteArray())
|
||||
}
|
||||
|
||||
val crc: UInt get() = checksum.value
|
||||
}
|
||||
@@ -10,6 +10,9 @@ import kotlin.reflect.typeOf
|
||||
*/
|
||||
interface DataSource {
|
||||
|
||||
/**
|
||||
* Exception that implementations must throw on end of data
|
||||
*/
|
||||
class EndOfData() : Exception("no more data available")
|
||||
|
||||
fun readByte(): Byte
|
||||
|
||||
Reference in new issue
Block a user