Skip to content

Commit

Permalink
Fix padding stored block
Browse files Browse the repository at this point in the history
  • Loading branch information
ruvmello committed Oct 26, 2023
1 parent 6f5dd6f commit a390c20
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
1 change: 0 additions & 1 deletion dummy.txt

This file was deleted.

9 changes: 4 additions & 5 deletions src/main/kotlin/huffman/HuffmanCompressor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ class HuffmanCompressor {
totalBitsSet += 3

// Padding first byte
val totalBytesSet = totalBitsSet / 8
byte = byte shl (8 - totalBitsSet - totalBytesSet * 8)
totalBitsSet += (8 - totalBitsSet - totalBytesSet * 8)
byte = byte shl (8 - totalBitsSet % 8)
totalBitsSet += (8 - totalBitsSet % 8)

val len = literal.size
val outputBytes = getListOfNReversedBytes(byte, totalBitsSet).toByteArray()
val outputBytes = getBytesAndReset().toByteArray()
byte = 0
totalBitsSet = 0

Expand Down Expand Up @@ -125,7 +124,7 @@ class HuffmanCompressor {
}

// End of block marker (256 - 7 bits)
byte = (byte shl 7) xor ((256 shl 1).toByte().toInt() shr 1) // Cut off 25 most significant bits
byte = byte shl 7 // First 7 bits of 256 are just 0, so shift 7 is enough
totalBitsSet += 7

encoded.addAll(getBytesAndReset())
Expand Down

0 comments on commit a390c20

Please sign in to comment.