Added function which allows creation of uncompressed deflate blocks, which in its turn allows us to enable test5 for deflation.

This commit is contained in:
Timofey Solomko committed 2017-02-08 19:41:02 +03:00
1 parent b1100a7594
commit 24576d892a
2 files changed
+35 -1

No files matched your search

+32
View File
@@ -232,11 +232,43 @@ public final class Deflate: DecompressionAlgorithm {
public static func compress(data: Data) throws -> Data {
let bytes = data.toArray(type: UInt8.self)
if bytes.count < 3 {
return Data(bytes: Deflate.createUncompressedBlock(bytes))
}
let bldCodes = Deflate.lengthEncode(bytes)
let huffmanEncodedBytes = Deflate.encodeHuffmanBlock(bldCodes)
return Data(bytes: huffmanEncodedBytes)
}
private static func createUncompressedBlock(_ bytes: [UInt8]) -> [UInt8] {
let bitWriter = BitToByteWriter(bitOrder: .reversed)
// Write block header.
// Note: Only one block is supported for now.
bitWriter.write(bit: 1)
bitWriter.write(bits: [0, 0])
// Before writing lengths we need to discard remaining bits in current byte.
bitWriter.finish()
// Write data's length.
bitWriter.write(number: bytes.count, bitsCount: 16)
// Write data's n-length.
bitWriter.write(number: bytes.count ^ (1 << 16 - 1), bitsCount: 16)
// Finishing block header.
bitWriter.finish()
var out = bitWriter.buffer
// Write actual data.
for byte in bytes {
out.append(byte)
}
return out
}
private static func encodeHuffmanBlock(_ bldCodes: [BLDCode]) -> [UInt8] {
let bitWriter = BitToByteWriter(bitOrder: .reversed)
+3 -1
View File
@@ -103,7 +103,9 @@ class DeflateTests: XCTestCase {
self.perform(compressionTest: "test4")
}
// - Note: Comrepssion Test #5 is skipped intentionally: because of precondition() in Deflate.swift.
func testDeflate5() {
self.perform(compressionTest: "test5")
}
func testDeflate6() {
self.perform(compressionTest: "test6")