Last active
December 8, 2025 03:52
-
-
Save mrenouf/a9069f73ffaacb2e7219f97870dc0554 to your computer and use it in GitHub Desktop.
crc32b in Kotlin
This file contains hidden or 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
| private const val CRC32_POLYNOMIAL_MSB = 0xEDB88320.toInt() | |
| private val CRC32_REVERSE_TABLE = IntArray(256) { | |
| var crc = it | |
| repeat(8) { | |
| if ((crc and 1) != 0) { | |
| crc = (crc ushr 1) xor CRC32_POLYNOMIAL_MSB | |
| } else { | |
| crc = crc ushr 1 | |
| } | |
| } | |
| crc | |
| } | |
| fun crc32b(data: ByteArray, offset: Int = 0, length: Int = data.size): Int { | |
| var crc = -1 | |
| var i = offset | |
| val limit = offset + length | |
| while (i < limit) { | |
| crc = CRC32_REVERSE_TABLE[(crc xor data[i++].toInt()) and 0xFF] xor (crc ushr 8) | |
| } | |
| return crc.inv() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment