Skip to content

Commit d7049c6

Browse files
committed
Merge branch 'master' into develop
2 parents 8612d37 + c2d5220 commit d7049c6

File tree

4 files changed

+99
-103
lines changed

4 files changed

+99
-103
lines changed

core/common/src/RawSink.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ package kotlinx.io
3232
*
3333
* Implementors should abstain from throwing exceptions other than those that are documented for RawSink methods.
3434
*
35-
* @sample kotlinx.io.samples.CRC32Sink
3635
* @sample kotlinx.io.samples.Crc32Sample.crc32
3736
*/
3837
@OptIn(ExperimentalStdlibApi::class)

core/common/src/RawSource.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ package kotlinx.io
3232
*
3333
* Implementors should abstain from throwing exceptions other than those that are documented for RawSource methods.
3434
*
35-
* @sample kotlinx.io.samples.RC4DecryptingSource
3635
* @sample kotlinx.io.samples.RC4SourceSample.rc4
3736
*/
3837
@OptIn(ExperimentalStdlibApi::class)

core/common/test/samples/rawSinkSample.kt

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,60 @@ import kotlinx.io.*
99
import kotlin.test.Test
1010
import kotlin.test.assertEquals
1111

12-
@OptIn(ExperimentalUnsignedTypes::class)
13-
private fun generateCrc32Table(): UIntArray {
14-
val table = UIntArray(256)
15-
16-
for (idx in table.indices) {
17-
table[idx] = idx.toUInt()
18-
for (bit in 8 downTo 1) {
19-
table[idx] = if (table[idx] % 2U == 0U) {
20-
table[idx].shr(1)
21-
} else {
22-
table[idx].shr(1).xor(0xEDB88320U)
12+
class Crc32Sample {
13+
@OptIn(ExperimentalStdlibApi::class, ExperimentalUnsignedTypes::class)
14+
@Test
15+
fun crc32() {
16+
/**
17+
* Sink calculating CRC-32 code for all the data written to it and sending this data to the upstream afterward.
18+
* The CRC-32 value could be obtained using [crc32] method.
19+
*
20+
* See https://en.wikipedia.org/wiki/Cyclic_redundancy_check for more information about CRC-32.
21+
*/
22+
class CRC32Sink(private val upstream: RawSink): RawSink {
23+
private val tempBuffer = Buffer()
24+
private val crc32Table = generateCrc32Table()
25+
private var crc32: UInt = 0xffffffffU
26+
27+
private fun update(value: Byte) {
28+
val index = value.xor(crc32.toByte()).toUByte()
29+
crc32 = crc32Table[index.toInt()].xor(crc32.shr(8))
2330
}
24-
}
25-
}
2631

27-
return table
28-
}
32+
fun crc32(): UInt = crc32.xor(0xffffffffU)
2933

30-
/**
31-
* Sink calculating CRC-32 code for all the data written to it and sending this data to the upstream afterward.
32-
* The CRC-32 value could be obtained using [crc32] method.
33-
*
34-
* See https://en.wikipedia.org/wiki/Cyclic_redundancy_check for more information about CRC-32.
35-
*/
36-
@OptIn(ExperimentalUnsignedTypes::class)
37-
class CRC32Sink(private val upstream: RawSink): RawSink {
38-
private val tempBuffer = Buffer()
39-
private val crc32Table = generateCrc32Table()
40-
private var crc32: UInt = 0xffffffffU
41-
42-
private fun update(value: Byte) {
43-
val index = value.xor(crc32.toByte()).toUByte()
44-
crc32 = crc32Table[index.toInt()].xor(crc32.shr(8))
45-
}
34+
override fun write(source: Buffer, byteCount: Long) {
35+
source.copyTo(tempBuffer, 0, byteCount)
36+
37+
while (!tempBuffer.exhausted()) {
38+
update(tempBuffer.readByte())
39+
}
4640

47-
fun crc32(): UInt = crc32.xor(0xffffffffU)
41+
upstream.write(source, byteCount)
42+
}
4843

49-
override fun write(source: Buffer, byteCount: Long) {
50-
source.copyTo(tempBuffer, 0, byteCount)
44+
override fun flush() = upstream.flush()
5145

52-
while (!tempBuffer.exhausted()) {
53-
update(tempBuffer.readByte())
54-
}
46+
override fun close() = upstream.close()
5547

56-
upstream.write(source, byteCount)
57-
}
48+
private fun generateCrc32Table(): UIntArray {
49+
val table = UIntArray(256)
5850

59-
override fun flush() = upstream.flush()
51+
for (idx in table.indices) {
52+
table[idx] = idx.toUInt()
53+
for (bit in 8 downTo 1) {
54+
table[idx] = if (table[idx] % 2U == 0U) {
55+
table[idx].shr(1)
56+
} else {
57+
table[idx].shr(1).xor(0xEDB88320U)
58+
}
59+
}
60+
}
6061

61-
override fun close() = upstream.close()
62-
}
62+
return table
63+
}
64+
}
6365

64-
class Crc32Sample {
65-
@OptIn(ExperimentalStdlibApi::class)
66-
@Test
67-
fun crc32() {
6866
val crc32Sink = CRC32Sink(discardingSink())
6967

7068
crc32Sink.buffered().use {

core/common/test/samples/rawSourceSample.kt

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,72 +9,72 @@ import kotlinx.io.*
99
import kotlin.test.Test
1010
import kotlin.test.assertEquals
1111

12-
/**
13-
* Source decrypting all the data read from the downstream using RC4 algorithm.
14-
*
15-
* See https://en.wikipedia.org/wiki/RC4 for more information about the cypher.
16-
*
17-
* Implementation of RC4 stream cypher based on http://cypherpunks.venona.com/archive/1994/09/msg00304.html
18-
*/
19-
@OptIn(ExperimentalUnsignedTypes::class)
20-
class RC4DecryptingSource(private val downstream: RawSource, key: String): RawSource {
21-
private val buffer = Buffer()
22-
private val key = RC4Key(key)
12+
class RC4SourceSample {
13+
@Test
14+
fun rc4() {
15+
/**
16+
* Source decrypting all the data read from the downstream using RC4 algorithm.
17+
*
18+
* See https://en.wikipedia.org/wiki/RC4 for more information about the cypher.
19+
*
20+
* Implementation of RC4 stream cypher based on http://cypherpunks.venona.com/archive/1994/09/msg00304.html
21+
*/
22+
@OptIn(ExperimentalUnsignedTypes::class)
23+
class RC4DecryptingSource(private val downstream: RawSource, key: String): RawSource {
24+
private val buffer = Buffer()
25+
private val key = RC4Key(key)
2326

24-
override fun readAtMostTo(sink: Buffer, byteCount: Long): Long {
25-
val bytesRead = downstream.readAtMostTo(buffer, byteCount)
26-
if (bytesRead == -1L) {
27-
return -1L
28-
}
27+
override fun readAtMostTo(sink: Buffer, byteCount: Long): Long {
28+
val bytesRead = downstream.readAtMostTo(buffer, byteCount)
29+
if (bytesRead == -1L) {
30+
return -1L
31+
}
2932

30-
while (!buffer.exhausted()) {
31-
val byte = buffer.readByte()
32-
sink.writeByte(byte.xor(key.nextByte()))
33-
}
33+
while (!buffer.exhausted()) {
34+
val byte = buffer.readByte()
35+
sink.writeByte(byte.xor(key.nextByte()))
36+
}
3437

35-
return bytesRead
36-
}
38+
return bytesRead
39+
}
3740

38-
override fun close() = downstream.close()
41+
override fun close() = downstream.close()
3942

40-
private class RC4Key(key: String) {
41-
private var keyState: UByteArray
42-
private var keyX: Int = 0
43-
private var keyY: Int = 0
43+
private inner class RC4Key(key: String) {
44+
private var keyState: UByteArray
45+
private var keyX: Int = 0
46+
private var keyY: Int = 0
4447

45-
init {
46-
require(key.isNotEmpty()) { "Key could not be empty" }
47-
val keyBytes = key.encodeToByteArray()
48-
keyState = UByteArray(256) { it.toUByte() }
49-
var index1 = 0
50-
var index2 = 0
48+
init {
49+
require(key.isNotEmpty()) { "Key could not be empty" }
50+
val keyBytes = key.encodeToByteArray()
51+
keyState = UByteArray(256) { it.toUByte() }
52+
var index1 = 0
53+
var index2 = 0
5154

52-
for (idx in keyState.indices) {
53-
index2 = (keyBytes[index1] + keyState[idx].toInt() + index2) % 256
54-
swapStateBytes(idx, index2)
55-
index1 = (index1 + 1) % keyBytes.size
56-
}
57-
}
55+
for (idx in keyState.indices) {
56+
index2 = (keyBytes[index1] + keyState[idx].toInt() + index2) % 256
57+
swapStateBytes(idx, index2)
58+
index1 = (index1 + 1) % keyBytes.size
59+
}
60+
}
5861

59-
fun nextByte(): Byte {
60-
keyX = (keyX + 1) % 256
61-
keyY = (keyState[keyX].toInt() + keyY) % 256
62-
swapStateBytes(keyX, keyY)
63-
val idx = (keyState[keyX] + keyState[keyY]) % 256U
64-
return keyState[idx.toInt()].toByte()
65-
}
62+
fun nextByte(): Byte {
63+
keyX = (keyX + 1) % 256
64+
keyY = (keyState[keyX].toInt() + keyY) % 256
65+
swapStateBytes(keyX, keyY)
66+
val idx = (keyState[keyX] + keyState[keyY]) % 256U
67+
return keyState[idx.toInt()].toByte()
68+
}
6669

67-
private fun swapStateBytes(x: Int, y: Int) {
68-
val tmp = keyState[x]
69-
keyState[x] = keyState[y]
70-
keyState[y] = tmp
70+
private fun swapStateBytes(x: Int, y: Int) {
71+
val tmp = keyState[x]
72+
keyState[x] = keyState[y]
73+
keyState[y] = tmp
74+
}
75+
}
7176
}
72-
}
73-
}
7477

75-
class RC4SourceSample {
76-
@Test
77-
fun rc4() {
7878
val key = "key"
7979
val source = Buffer().also { it.write(byteArrayOf(0x58, 0x09, 0x57, 0x9fU.toByte(), 0x41, 0xfbU.toByte())) }
8080
val rc4Source = RC4DecryptingSource(source, key).buffered()

0 commit comments

Comments
 (0)