From 4713ad5ed8a9504e189bb4e6461c48e150f35040 Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Wed, 15 Aug 2018 18:52:32 +0300 Subject: [PATCH] [LZMA2] Use LZMA2.decompress with dict size byte argument function instead of using LZMA2Decoder directly in "clients" --- Sources/7-Zip/7zFolder.swift | 5 +---- Sources/LZMA2/LZMA2.swift | 6 +++--- Sources/XZ/XZBlock.swift | 10 ++-------- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/Sources/7-Zip/7zFolder.swift b/Sources/7-Zip/7zFolder.swift index 8f81743f..50856802 100644 --- a/Sources/7-Zip/7zFolder.swift +++ b/Sources/7-Zip/7zFolder.swift @@ -164,10 +164,7 @@ class SevenZipFolder { properties.count == 1 else { throw LZMA2Error.wrongDictionarySize } - let byteReader = ByteReader(data: decodedData) - let decoder = try LZMA2Decoder(byteReader, properties[0]) - try decoder.decode() - decodedData = Data(bytes: decoder.out) + decodedData = try Data(bytes: LZMA2.decompress(ByteReader(data: decodedData), properties[0])) case .lzma: // Both properties' byte (lp, lc, pb) and dictionary size are stored in coder's properties. guard let properties = coder.properties, diff --git a/Sources/LZMA2/LZMA2.swift b/Sources/LZMA2/LZMA2.swift index 8b1aa8bb..2561fb67 100644 --- a/Sources/LZMA2/LZMA2.swift +++ b/Sources/LZMA2/LZMA2.swift @@ -24,11 +24,11 @@ public class LZMA2: DecompressionAlgorithm { */ public static func decompress(data: Data) throws -> Data { let byteReader = ByteReader(data: data) - return Data(bytes: try decompress(byteReader)) + return Data(bytes: try decompress(byteReader, byteReader.byte())) } - static func decompress(_ byteReader: ByteReader) throws -> [UInt8] { - let decoder = try LZMA2Decoder(byteReader, byteReader.byte()) + static func decompress(_ byteReader: ByteReader, _ dictSizeByte: UInt8) throws -> [UInt8] { + let decoder = try LZMA2Decoder(byteReader, dictSizeByte) try decoder.decode() return decoder.out } diff --git a/Sources/XZ/XZBlock.swift b/Sources/XZ/XZBlock.swift index 0f08464f..332abe2b 100644 --- a/Sources/XZ/XZBlock.swift +++ b/Sources/XZ/XZBlock.swift @@ -45,15 +45,9 @@ struct XZBlock { let propertiesSize = try byteReader.multiByteDecode() guard propertiesSize == 1 else { throw LZMA2Error.wrongDictionarySize } - /// In case of LZMA2 filters property is a dicitonary size. + /// Filter property for LZMA2 is a dictionary size. let filterPropeties = byteReader.byte() - let closure = { (dwp: ByteReader) -> Data in - let decoder = try LZMA2Decoder(byteReader, filterPropeties) - - try decoder.decode() - return Data(bytes: decoder.out) - } - filters.append(closure) + filters.append { try Data(bytes: LZMA2.decompress($0, filterPropeties)) } } else { throw XZError.wrongFilterID }