Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create fs2.compression.checksum #2606

Merged
merged 8 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ lazy val core = crossProject(JVMPlatform, JSPlatform)
"org.typelevel" %%% "cats-effect" % "3.2.9",
"org.typelevel" %%% "cats-effect-laws" % "3.2.9" % Test,
"org.typelevel" %%% "cats-effect-testkit" % "3.2.9" % Test,
"org.scodec" %%% "scodec-bits" % "1.1.28",
"org.scodec" %%% "scodec-bits" % "1.1.29",
"org.typelevel" %%% "scalacheck-effect-munit" % "1.0.2" % Test,
"org.typelevel" %%% "munit-cats-effect-3" % "1.0.5" % Test,
"org.typelevel" %%% "discipline-munit" % "1.0.9" % Test
Expand Down
60 changes: 60 additions & 0 deletions core/shared/src/main/scala/fs2/compression/checksum.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2013 Functional Streams for Scala
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package fs2
package compression

import scodec.bits
import scodec.bits.crc.CrcBuilder
import scodec.bits.BitVector

/** Provides various checksums as pipes. */
object checksum {

/** Computes a CRC32 checksum.
* @see [[scodec.bits.crc.crc32]]
*/
def crc32[F[_]]: Pipe[F, Byte, Byte] = fromCrcBuilder(bits.crc.crc32Builder)

/** Computes a CRC32C checksum.
* @see [[scodec.bits.crc.crc32c]]
*/
def crc32c[F[_]]: Pipe[F, Byte, Byte] = fromCrcBuilder(bits.crc.crc32cBuilder)

/** Computes a CRC checksum using the specified polynomial.
* @see [[scodec.bits.crc]]
*/
def crc[F[_]](
poly: BitVector,
initial: BitVector,
reflectInput: Boolean,
reflectOutput: Boolean,
finalXor: BitVector
): Pipe[F, Byte, Byte] =
fromCrcBuilder(bits.crc.builder(poly, initial, reflectInput, reflectOutput, finalXor))

private def fromCrcBuilder[F[_]](builder: CrcBuilder[BitVector]): Pipe[F, Byte, Byte] =
_.chunks
.fold(builder)((builder, bits) => builder.updated(bits.toBitVector))
.map(b => Chunk.byteVector(b.result.bytes))
.unchunks

}
53 changes: 53 additions & 0 deletions core/shared/src/test/scala/fs2/ChecksumSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2013 Functional Streams for Scala
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package fs2

import fs2.compression.checksum
import org.scalacheck.Prop.forAll
import scodec.bits.BitVector
import scodec.bits.crc

class ChecksumSuite extends Fs2Suite {

test("CRC32") {
forAll { (bytes: Stream[Pure, Byte]) =>
val result = bytes
.through(checksum.crc32)
.compile
.toVector
val expected = crc.crc32(BitVector(bytes.compile.toVector))
assertEquals(BitVector(result), expected)
}
}

test("CRC32C") {
forAll { (bytes: Stream[Pure, Byte]) =>
val result = bytes
.through(checksum.crc32c)
.compile
.toVector
val expected = crc.crc32c(BitVector(bytes.compile.toVector))
assertEquals(BitVector(result), expected)
}
}

}