mirror of
https://github.com/tsolomko/SWCompression.git
synced 2026-11-18 21:40:30 +00:00
Merge branch 'develop'
This commit is contained in:
15 files changed
+98
-194
No files matched your search
@@ -1,4 +1,10 @@
|
||||
# Changelog
|
||||
v2.0.1
|
||||
----------------
|
||||
- Fixed incorrect reading adler32 checksum from zlib archives.
|
||||
- Removed LZMA_INFO and LZMA_DIAG build options.
|
||||
- (2.0.0) GZip archives with multiple 'members' are now parsed correctly.
|
||||
|
||||
v2.0.0
|
||||
----------------
|
||||
- LZMA decompression.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Pod::Spec.new do |s|
|
||||
|
||||
s.name = "SWCompression"
|
||||
s.version = "2.0.0"
|
||||
s.version = "2.0.1"
|
||||
s.summary = "Framework with implementations in Swift of different (de)compression algorithms"
|
||||
|
||||
s.description = <<-DESC
|
||||
|
||||
@@ -17,7 +17,7 @@ final class DataWithPointer {
|
||||
|
||||
let bitOrder: BitOrder
|
||||
let size: Int
|
||||
private(set) var bitArray: [UInt8]
|
||||
private var bitArray: [UInt8]
|
||||
var index: Int = 0
|
||||
private(set) var bitMask: UInt8
|
||||
|
||||
@@ -25,6 +25,10 @@ final class DataWithPointer {
|
||||
return self.size == self.index
|
||||
}
|
||||
|
||||
var prevAlignedByte: UInt8 {
|
||||
return self.bitArray[self.index - 1]
|
||||
}
|
||||
|
||||
init(array: inout [UInt8], bitOrder: BitOrder) {
|
||||
self.bitOrder = bitOrder
|
||||
self.bitArray = array
|
||||
|
||||
+25
-2
@@ -59,9 +59,32 @@ public final class LZMA: DecompressionAlgorithm {
|
||||
/// Object with input data which supports convenient work with bit shifts.
|
||||
var pointerData = DataWithPointer(data: data, bitOrder: .reversed)
|
||||
|
||||
let lzmaDecoder = try LZMADecoder(&pointerData)
|
||||
return Data(bytes: try decompress(&pointerData))
|
||||
}
|
||||
|
||||
return Data(bytes: try lzmaDecoder.decodeLZMA())
|
||||
static func decompress(_ pointerData: inout DataWithPointer) throws -> [UInt8] {
|
||||
// Firstly, we need to parse LZMA properties.
|
||||
var properties = pointerData.alignedByte()
|
||||
if properties >= (9 * 5 * 5) {
|
||||
throw LZMAError.WrongProperties
|
||||
}
|
||||
/// The number of literal context bits
|
||||
let lc = properties % 9
|
||||
properties /= 9
|
||||
/// The number of pos bits
|
||||
let pb = properties / 5
|
||||
/// The number of literal pos bits
|
||||
let lp = properties % 5
|
||||
var dictionarySize = pointerData.intFromAlignedBytes(count: 4)
|
||||
dictionarySize = dictionarySize < (1 << 12) ? 1 << 12 : dictionarySize
|
||||
|
||||
/// Size of uncompressed data. -1 means it is unknown/undefined.
|
||||
var uncompressedSize = pointerData.intFromAlignedBytes(count: 8)
|
||||
uncompressedSize = Double(uncompressedSize) == pow(Double(2), Double(64)) - 1 ? -1 : uncompressedSize
|
||||
|
||||
let lzmaDecoder = try LZMADecoder(&pointerData, lc, pb, lp, dictionarySize)
|
||||
|
||||
return try lzmaDecoder.decodeLZMA(&uncompressedSize)
|
||||
}
|
||||
|
||||
}
|
||||
+18
-18
@@ -37,23 +37,6 @@ public enum LZMA2Error: Error {
|
||||
/// Provides function to decompress data, which were compressed with LZMA2
|
||||
public final class LZMA2: DecompressionAlgorithm {
|
||||
|
||||
static func dictionarySize(_ byte: UInt8) throws -> Int {
|
||||
let bits = byte & 0x3F
|
||||
guard byte & 0xC0 == 0
|
||||
else { throw LZMA2Error.WrongProperties }
|
||||
guard bits < 40
|
||||
else { throw LZMA2Error.WrongDictionarySize }
|
||||
|
||||
var dictSize: UInt32 = 0
|
||||
if bits == 40 {
|
||||
dictSize = UInt32.max
|
||||
} else {
|
||||
dictSize = UInt32(2 | (bits.toInt() & 1))
|
||||
dictSize <<= UInt32(bits.toInt() / 2 + 11)
|
||||
}
|
||||
return Int(dictSize)
|
||||
}
|
||||
|
||||
/**
|
||||
Decompresses `compressedData` with LZMA2 algortihm. LZMA2 is a modification of LZMA.
|
||||
|
||||
@@ -77,7 +60,7 @@ public final class LZMA2: DecompressionAlgorithm {
|
||||
|
||||
static func decompress(_ dictionarySize: Int, _ pointerData: inout DataWithPointer) throws -> [UInt8] {
|
||||
// At this point lzmaDecoder will be in a VERY bad state.
|
||||
let lzmaDecoder = try LZMADecoder(&pointerData, false)
|
||||
let lzmaDecoder = try LZMADecoder(&pointerData, 0, 0, 0, 0)
|
||||
|
||||
var out: [UInt8] = []
|
||||
|
||||
@@ -103,4 +86,21 @@ public final class LZMA2: DecompressionAlgorithm {
|
||||
return out
|
||||
}
|
||||
|
||||
static func dictionarySize(_ byte: UInt8) throws -> Int {
|
||||
let bits = byte & 0x3F
|
||||
guard byte & 0xC0 == 0
|
||||
else { throw LZMA2Error.WrongProperties }
|
||||
guard bits < 40
|
||||
else { throw LZMA2Error.WrongDictionarySize }
|
||||
|
||||
var dictSize: UInt32 = 0
|
||||
if bits == 40 {
|
||||
dictSize = UInt32.max
|
||||
} else {
|
||||
dictSize = UInt32(2 | (bits.toInt() & 1))
|
||||
dictSize <<= UInt32(bits.toInt() / 2 + 11)
|
||||
}
|
||||
return Int(dictSize)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,16 +25,13 @@ final class LZMABitTreeDecoder {
|
||||
|
||||
func decode(with rangeDecoder: inout LZMARangeDecoder) -> Int {
|
||||
var m = 1
|
||||
for i in 0..<self.numBits {
|
||||
for _ in 0..<self.numBits {
|
||||
m = (m << 1) + rangeDecoder.decode(bitWithProb: &self.probs[m])
|
||||
lzmaDiagPrint("bitTreeDecoder_decode_m_\(i): \(m)")
|
||||
}
|
||||
lzmaDiagPrint("bitTreeDecoder_decode_result: \(m - (1 << self.numBits))")
|
||||
return m - (1 << self.numBits)
|
||||
}
|
||||
|
||||
func reverseDecode(with rangeDecoder: inout LZMARangeDecoder) -> Int {
|
||||
lzmaDiagPrint("!!!BitTreeReverseDecode")
|
||||
return LZMABitTreeDecoder.bitTreeReverseDecode(probs: &self.probs,
|
||||
startIndex: 0,
|
||||
bits: self.numBits,
|
||||
|
||||
+36
-144
@@ -21,6 +21,7 @@ struct LZMAConstants {
|
||||
static let endPosModelIndex: Int = 14
|
||||
static let numFullDistances: Int = (1 << (endPosModelIndex >> 1))
|
||||
static let matchMinLen: Int = 2
|
||||
// LZMAConstants.numStates << LZMAConstants.numPosBitsMax = 192
|
||||
}
|
||||
|
||||
final class LZMADecoder {
|
||||
@@ -31,7 +32,7 @@ final class LZMADecoder {
|
||||
private var lp: UInt8
|
||||
private var pb: UInt8
|
||||
private var dictionarySize: Int
|
||||
private var uncompressedSize: Int
|
||||
// private var uncompressedSize: Int
|
||||
|
||||
private var outWindow: LZMAOutWindow
|
||||
private var rangeDecoder: LZMARangeDecoder
|
||||
@@ -45,19 +46,18 @@ final class LZMADecoder {
|
||||
Each table contains 0x300 probabilities.
|
||||
*/
|
||||
private var literalProbs: [[Int]]
|
||||
// These arrays are used to select type of match or literal.
|
||||
private var isMatch: [Int] = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates << LZMAConstants.numPosBitsMax)
|
||||
private var isRep: [Int] = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
private var isRepG0: [Int] = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
private var isRepG1: [Int] = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
private var isRepG2: [Int] = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
private var isRep0Long: [Int] = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates << LZMAConstants.numPosBitsMax)
|
||||
|
||||
/**
|
||||
Array with all probabilities:
|
||||
|
||||
- 0..<192: isMatch
|
||||
- 193..<205: isRep
|
||||
- 205..<217: isRepG0
|
||||
- 217..<229: isRepG1
|
||||
- 229..<241: isRepG2
|
||||
- 241..<433: isRep0Long
|
||||
*/
|
||||
private var probabilities: [Int] = Array(repeating: LZMAConstants.probInitValue, count: 2 * 192 + 4 * 12)
|
||||
|
||||
private var posDecoders: [Int]
|
||||
|
||||
@@ -70,66 +70,19 @@ final class LZMADecoder {
|
||||
/// Is used to select exact variable from 'IsRep', 'IsRepG0', 'IsRepG1æ and 'IsRepG2' arrays.
|
||||
private var state: Int = 0
|
||||
|
||||
init(_ pointerData: inout DataWithPointer, _ initProperties: Bool = true) throws {
|
||||
init(_ pointerData: inout DataWithPointer, _ lc: UInt8, _ pb: UInt8, _ lp: UInt8,
|
||||
_ dictionarySize: Int) throws {
|
||||
self.pointerData = pointerData
|
||||
|
||||
// First byte contains lzma properties.
|
||||
if initProperties {
|
||||
var properties = pointerData.alignedByte()
|
||||
if properties >= (9 * 5 * 5) {
|
||||
throw LZMAError.WrongProperties
|
||||
}
|
||||
/// The number of literal context bits
|
||||
let lc = properties % 9
|
||||
properties /= 9
|
||||
/// The number of pos bits
|
||||
let pb = properties / 5
|
||||
/// The number of literal pos bits
|
||||
let lp = properties % 5
|
||||
var dictionarySize = pointerData.intFromAlignedBytes(count: 4)
|
||||
dictionarySize = dictionarySize < (1 << 12) ? 1 << 12 : dictionarySize
|
||||
self.lc = lc
|
||||
self.lp = lp
|
||||
self.pb = pb
|
||||
self.dictionarySize = dictionarySize
|
||||
|
||||
/// Size of uncompressed data. -1 means it is unknown.
|
||||
var uncompressedSize = pointerData.intFromAlignedBytes(count: 8)
|
||||
uncompressedSize = Double(uncompressedSize) == pow(Double(2), Double(64)) - 1 ? -1 : uncompressedSize
|
||||
|
||||
self.lc = lc
|
||||
self.lp = lp
|
||||
self.pb = pb
|
||||
self.dictionarySize = dictionarySize
|
||||
self.uncompressedSize = uncompressedSize
|
||||
} else {
|
||||
self.lc = 0
|
||||
self.lp = 0
|
||||
self.pb = 0
|
||||
self.dictionarySize = 0
|
||||
self.uncompressedSize = -1
|
||||
}
|
||||
self.rangeDecoder = LZMARangeDecoder()
|
||||
|
||||
self.outWindow = LZMAOutWindow(dictSize: self.dictionarySize)
|
||||
|
||||
if initProperties {
|
||||
guard let rD = LZMARangeDecoder(&self.pointerData) else {
|
||||
throw LZMAError.RangeDecoderInitError
|
||||
}
|
||||
self.rangeDecoder = rD
|
||||
} else {
|
||||
self.rangeDecoder = LZMARangeDecoder()
|
||||
}
|
||||
|
||||
self.isMatch = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates << LZMAConstants.numPosBitsMax)
|
||||
self.isRep = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
self.isRepG0 = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
self.isRepG1 = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
self.isRepG2 = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
self.isRep0Long = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates << LZMAConstants.numPosBitsMax)
|
||||
|
||||
self.literalProbs = Array(repeating: Array(repeating: LZMAConstants.probInitValue,
|
||||
count: 0x300),
|
||||
count: 1 << (lc + lp).toInt())
|
||||
@@ -174,19 +127,7 @@ final class LZMADecoder {
|
||||
self.rep2 = 0
|
||||
self.rep3 = 0
|
||||
|
||||
self.isMatch = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates << LZMAConstants.numPosBitsMax)
|
||||
self.isRep = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
self.isRepG0 = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
self.isRepG1 = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
self.isRepG2 = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates)
|
||||
self.isRep0Long = Array(repeating: LZMAConstants.probInitValue,
|
||||
count: LZMAConstants.numStates << LZMAConstants.numPosBitsMax)
|
||||
|
||||
self.probabilities = Array(repeating: LZMAConstants.probInitValue, count: 2 * 192 + 4 * 12)
|
||||
self.literalProbs = Array(repeating: Array(repeating: LZMAConstants.probInitValue,
|
||||
count: 0x300),
|
||||
count: 1 << (lc + lp).toInt())
|
||||
@@ -238,23 +179,19 @@ final class LZMADecoder {
|
||||
default:
|
||||
throw LZMA2Error.WrongReset
|
||||
}
|
||||
|
||||
// Apparently, we need to reinitialize RangeDecoder each time.
|
||||
guard let rD = LZMARangeDecoder(&self.pointerData) else {
|
||||
throw LZMAError.RangeDecoderInitError
|
||||
}
|
||||
self.rangeDecoder = rD
|
||||
|
||||
self.uncompressedSize = unpackSize
|
||||
out = try decodeLZMA()
|
||||
var uncompressedSize = unpackSize
|
||||
out = try decodeLZMA(&uncompressedSize)
|
||||
guard unpackSize == out.count && pointerData.index - dataStartIndex == compressedSize
|
||||
else { throw LZMA2Error.WrongSizes }
|
||||
return out
|
||||
}
|
||||
|
||||
func decodeLZMA() throws -> [UInt8] {
|
||||
lzmaInfoPrint("lc: \(lc), lp: \(lp), pb: \(pb), dictionarySize: \(dictionarySize)")
|
||||
lzmaInfoPrint("uncompressedSize: \(uncompressedSize)")
|
||||
func decodeLZMA(_ uncompressedSize: inout Int) throws -> [UInt8] {
|
||||
// First, we need to initialize Rande Decoder.
|
||||
guard let rD = LZMARangeDecoder(&self.pointerData) else {
|
||||
throw LZMAError.RangeDecoderInitError
|
||||
}
|
||||
self.rangeDecoder = rD
|
||||
|
||||
/// An array for storing output data
|
||||
var out: [UInt8] = uncompressedSize == -1 ? [] : Array(repeating: 0, count: uncompressedSize)
|
||||
@@ -262,8 +199,6 @@ final class LZMADecoder {
|
||||
|
||||
// Main decoding cycle.
|
||||
while true {
|
||||
lzmaDiagPrint("=========================")
|
||||
lzmaDiagPrint("start_unpackSize: \(uncompressedSize)")
|
||||
// If uncompressed size was defined and everything is unpacked then stop.
|
||||
if uncompressedSize == 0 {
|
||||
if rangeDecoder.isFinishedOK {
|
||||
@@ -272,14 +207,12 @@ final class LZMADecoder {
|
||||
}
|
||||
|
||||
let posState = outWindow.totalPosition & ((1 << pb.toInt()) - 1)
|
||||
lzmaDiagPrint("posState: \(posState)")
|
||||
if rangeDecoder.decode(bitWithProb: &isMatch[(state << LZMAConstants.numPosBitsMax) + posState]) == 0 {
|
||||
if rangeDecoder.decode(bitWithProb: &probabilities[(state << LZMAConstants.numPosBitsMax) + posState]) == 0 {
|
||||
if uncompressedSize == 0 { throw LZMAError.ExceededUncompressedSize }
|
||||
|
||||
// DECODE LITERAL:
|
||||
/// Previous literal (zero, if there was none).
|
||||
let prevByte = outWindow.isEmpty ? 0 : outWindow.byte(at: 1)
|
||||
lzmaDiagPrint("decodeLiteral_prevByte: \(prevByte)")
|
||||
/// Decoded symbol. Initial value is 1.
|
||||
var symbol = 1
|
||||
/**
|
||||
@@ -309,7 +242,6 @@ final class LZMADecoder {
|
||||
while symbol < 0x100 {
|
||||
symbol = (symbol << 1) | rangeDecoder.decode(bitWithProb: &literalProbs[litState][symbol])
|
||||
}
|
||||
lzmaDiagPrint("decodeLiteral_symbol: \(symbol)")
|
||||
let byte = (symbol - 0x100).toUInt8()
|
||||
outWindow.put(byte, &out, &outIndex, &uncompressedSize)
|
||||
// END.
|
||||
@@ -322,24 +254,21 @@ final class LZMADecoder {
|
||||
} else {
|
||||
state -= 6
|
||||
}
|
||||
lzmaDiagPrint("decodeLiteral_updatedState: \(state)")
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
var len: Int
|
||||
if rangeDecoder.decode(bitWithProb: &isRep[state]) != 0 {
|
||||
if rangeDecoder.decode(bitWithProb: &probabilities[193 + state]) != 0 {
|
||||
// REP MATCH CASE
|
||||
if uncompressedSize == 0 { throw LZMAError.ExceededUncompressedSize }
|
||||
if outWindow.isEmpty { throw LZMAError.WindowIsEmpty }
|
||||
if rangeDecoder.decode(bitWithProb: &isRepG0[state]) == 0 {
|
||||
if rangeDecoder.decode(bitWithProb: &probabilities[205 + state]) == 0 {
|
||||
// (We use last distance from 'distance history table').
|
||||
if rangeDecoder.decode(bitWithProb: &isRep0Long[(state << LZMAConstants.numPosBitsMax) + posState]) == 0 {
|
||||
if rangeDecoder.decode(bitWithProb: &probabilities[241 + (state << LZMAConstants.numPosBitsMax) + posState]) == 0 {
|
||||
// SHORT REP MATCH CASE
|
||||
state = state < 7 ? 9 : 11
|
||||
lzmaDiagPrint("updatedState: \(state)")
|
||||
let byte = outWindow.byte(at: rep0 + 1)
|
||||
lzmaDiagPrint("byte_to_put: \(byte)")
|
||||
outWindow.put(byte, &out, &outIndex, &uncompressedSize)
|
||||
continue
|
||||
}
|
||||
@@ -348,10 +277,10 @@ final class LZMADecoder {
|
||||
// So the following code selectes one distance from history...
|
||||
// based on the binary data.
|
||||
let dist: Int
|
||||
if rangeDecoder.decode(bitWithProb: &isRepG1[state]) == 0 {
|
||||
if rangeDecoder.decode(bitWithProb: &probabilities[217 + state]) == 0 {
|
||||
dist = rep1
|
||||
} else {
|
||||
if rangeDecoder.decode(bitWithProb: &isRepG2[state]) == 0 {
|
||||
if rangeDecoder.decode(bitWithProb: &probabilities[229 + state]) == 0 {
|
||||
dist = rep2
|
||||
} else {
|
||||
dist = rep3
|
||||
@@ -361,16 +290,9 @@ final class LZMADecoder {
|
||||
}
|
||||
rep1 = rep0
|
||||
rep0 = dist
|
||||
lzmaDiagPrint("rep_match_rep0: \(rep0)")
|
||||
lzmaDiagPrint("rep_match_rep1: \(rep1)")
|
||||
lzmaDiagPrint("rep_match_rep2: \(rep2)")
|
||||
lzmaDiagPrint("rep_match_rep3: \(rep3)")
|
||||
lzmaDiagPrint("rep_match_dist: \(dist)")
|
||||
}
|
||||
len = repLenDecoder.decode(with: &rangeDecoder, posState: posState)
|
||||
lzmaDiagPrint("read_len: \(len)")
|
||||
state = state < 7 ? 8 : 11
|
||||
lzmaDiagPrint("updatedState: \(state)")
|
||||
} else { // SIMPLE MATCH CASE
|
||||
// First, we need to move history of distance values.
|
||||
rep3 = rep2
|
||||
@@ -379,12 +301,6 @@ final class LZMADecoder {
|
||||
len = lenDecoder.decode(with: &rangeDecoder, posState: posState)
|
||||
state = state < 7 ? 7 : 10
|
||||
|
||||
lzmaDiagPrint("beforeDecodeDistance_updatedRep3: \(rep3)")
|
||||
lzmaDiagPrint("beforeDecodeDistance_updatedRep2: \(rep2)")
|
||||
lzmaDiagPrint("beforeDecodeDistance_updatedRep1: \(rep1)")
|
||||
lzmaDiagPrint("beforeDecodeDistance_updatedLen: \(len)")
|
||||
lzmaDiagPrint("beforeDecodeDistance_updatedState: \(state)")
|
||||
|
||||
// DECODE DISTANCE:
|
||||
/// Is used to define context for distance decoding.
|
||||
var lenState = len
|
||||
@@ -392,22 +308,16 @@ final class LZMADecoder {
|
||||
lenState = LZMAConstants.numLenToPosStates - 1
|
||||
}
|
||||
|
||||
lzmaDiagPrint("decodeDistance_lenState: \(lenState)")
|
||||
|
||||
/// Defines decoding scheme for distance value.
|
||||
let posSlot = posSlotDecoder[lenState].decode(with: &rangeDecoder)
|
||||
lzmaDiagPrint("decodeDistance_posSlot: \(posSlot)")
|
||||
if posSlot < 4 {
|
||||
// If `posSlot` is less than 4 then distance has defined value (no need to decode).
|
||||
// And distance is actually equal to `posSlot`.
|
||||
rep0 = posSlot
|
||||
lzmaDiagPrint("decodeDistance: posSlot => rep0")
|
||||
} else {
|
||||
lzmaDiagPrint("decodeDistance: dist => rep0")
|
||||
let numDirectBits = (posSlot >> 1) - 1
|
||||
lzmaDiagPrint("decodeDistance_numDirectBits: \(numDirectBits)")
|
||||
var dist = ((2 | (posSlot & 1)) << numDirectBits)
|
||||
lzmaDiagPrint("decodeDistance_startDist: \(dist)")
|
||||
if posSlot < LZMAConstants.endPosModelIndex {
|
||||
// In this case we need a sequence of bits decoded with bit tree...
|
||||
// ...(separate trees for different `posSlot` values)...
|
||||
@@ -416,16 +326,13 @@ final class LZMADecoder {
|
||||
startIndex: dist - posSlot,
|
||||
bits: numDirectBits,
|
||||
rangeDecoder: &rangeDecoder)
|
||||
lzmaDiagPrint("decodeDistance_updatedDist_0: \(dist)")
|
||||
} else {
|
||||
// Middle bits of distance are decoded as direct bits from RangeDecoder.
|
||||
dist += rangeDecoder.decode(directBits: (numDirectBits - LZMAConstants.numAlignBits))
|
||||
<< LZMAConstants.numAlignBits
|
||||
lzmaDiagPrint("decodeDistance_updatedDist_1_1: \(dist)")
|
||||
// Low 4 bits are decoded with a bit tree decoder (called 'AlignDecoder')...
|
||||
// ...with "Reverse" scheme.
|
||||
dist += alignDecoder.reverseDecode(with: &rangeDecoder)
|
||||
lzmaDiagPrint("decodeDistance_updatedDist_1_2: \(dist)")
|
||||
}
|
||||
rep0 = dist
|
||||
}
|
||||
@@ -434,7 +341,6 @@ final class LZMADecoder {
|
||||
// Check if finish marker is encountered.
|
||||
// Distance value of 2^32 is used to indicate 'End of Stream' marker.
|
||||
if UInt32(rep0) == 0xFFFFFFFF {
|
||||
lzmaDiagPrint("finish_marker")
|
||||
guard rangeDecoder.isFinishedOK else { throw LZMAError.RangeDecoderFinishError }
|
||||
break
|
||||
}
|
||||
@@ -444,9 +350,7 @@ final class LZMADecoder {
|
||||
}
|
||||
// Converting from zero-based length of the match to the real one.
|
||||
len += LZMAConstants.matchMinLen
|
||||
lzmaDiagPrint("finalLen: \(len)")
|
||||
if uncompressedSize > -1 && uncompressedSize < len { throw LZMAError.RepeatWillExceed }
|
||||
lzmaDiagPrint("copyMatch_at: \(rep0 + 1)")
|
||||
outWindow.copyMatch(at: rep0 + 1, length: len, &out, &outIndex, &uncompressedSize)
|
||||
}
|
||||
|
||||
@@ -454,15 +358,3 @@ final class LZMADecoder {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func lzmaDiagPrint(_ s: String) {
|
||||
#if LZMA_DIAG
|
||||
print(s)
|
||||
#endif
|
||||
}
|
||||
|
||||
func lzmaInfoPrint(_ s: String) {
|
||||
#if LZMA_INFO
|
||||
print(s)
|
||||
#endif
|
||||
}
|
||||
@@ -31,18 +31,15 @@ final class LZMALenDecoder {
|
||||
/// Decodes zero-based match length.
|
||||
func decode(with rangeDecoder: inout LZMARangeDecoder, posState: Int) -> Int {
|
||||
// There can be one of three options.
|
||||
lzmaDiagPrint("!!!LenDecoder_DECODE")
|
||||
// We need one or two bits to find out which decoding scheme to use.
|
||||
// `choice` is used to decode first bit.
|
||||
// `choice2` is used to decode second bit.
|
||||
// If binary sequence starts with 0 then:
|
||||
if rangeDecoder.decode(bitWithProb: &self.choice) == 0 {
|
||||
lzmaDiagPrint("lenDecoder_lowCoder_posState: \(posState)")
|
||||
return self.lowCoder[posState].decode(with: &rangeDecoder)
|
||||
}
|
||||
// If binary sequence starts with 1 0 then:
|
||||
if rangeDecoder.decode(bitWithProb: &self.choice2) == 0 {
|
||||
lzmaDiagPrint("lenDecoder_midCoder_posState: \(posState)")
|
||||
return 8 + self.midCoder[posState].decode(with: &rangeDecoder)
|
||||
}
|
||||
// If binary sequence starts with 1 1 then:
|
||||
|
||||
@@ -50,23 +50,15 @@ final class LZMARangeDecoder {
|
||||
|
||||
/// Decodes sequence of direct bits (binary symbols with fixed and equal probabilities).
|
||||
func decode(directBits: Int) -> Int {
|
||||
lzmaDiagPrint("!!!decodeDirectBits")
|
||||
lzmaDiagPrint("decodeDirectBits_code_start: \(self.code)")
|
||||
lzmaDiagPrint("decodeDirectBits_range_start: \(self.range)")
|
||||
var res: UInt32 = 0
|
||||
var count = directBits
|
||||
lzmaDiagPrint("decodeDirectBits_count: \(count)")
|
||||
repeat {
|
||||
self.range >>= 1
|
||||
lzmaDiagPrint("decodeDirectBits_range_1: \(self.range)")
|
||||
self.code = UInt32.subtractWithOverflow(self.code, self.range).0
|
||||
lzmaDiagPrint("decodeDirectBits_code_1: \(self.code)")
|
||||
let t = UInt32.subtractWithOverflow(0, self.code >> 31).0
|
||||
lzmaDiagPrint("decodeDirectBits_t: \(t)")
|
||||
self.code = UInt32.addWithOverflow(self.code, self.range & t).0
|
||||
|
||||
if self.code == self.range {
|
||||
lzmaDiagPrint("???Corrupted")
|
||||
self.isCorrupted = true
|
||||
}
|
||||
|
||||
@@ -82,9 +74,6 @@ final class LZMARangeDecoder {
|
||||
/// Decodes binary symbol (bit) with predicted (estimated) probability.
|
||||
func decode(bitWithProb prob: inout Int) -> Int {
|
||||
let bound = (self.range >> UInt32(LZMAConstants.numBitModelTotalBits)) * UInt32(prob)
|
||||
lzmaDiagPrint("decodebit")
|
||||
lzmaDiagPrint("bound: \(bound)")
|
||||
lzmaDiagPrint("probBefore: \(prob)")
|
||||
let symbol: Int
|
||||
if self.code < bound {
|
||||
prob += ((1 << LZMAConstants.numBitModelTotalBits) - prob) >> LZMAConstants.numMoveBits
|
||||
@@ -96,10 +85,6 @@ final class LZMARangeDecoder {
|
||||
self.range -= bound
|
||||
symbol = 1
|
||||
}
|
||||
lzmaDiagPrint("probAfter: \(prob)")
|
||||
lzmaDiagPrint("codeAfter: \(self.code)")
|
||||
lzmaDiagPrint("rangeAfter: \(self.range)")
|
||||
lzmaDiagPrint("-------------------")
|
||||
self.normalize()
|
||||
return symbol
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>2.0.0</string>
|
||||
<string>2.0.1</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||
<key>NSPrincipalClass</key>
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>2.0.0</string>
|
||||
<string>2.0.1</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||
<key>NSPrincipalClass</key>
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>2.0.0</string>
|
||||
<string>2.0.1</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||
<key>NSPrincipalClass</key>
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>2.0.0</string>
|
||||
<string>2.0.1</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||
<key>NSHumanReadableCopyright</key>
|
||||
|
||||
@@ -354,7 +354,7 @@ extension DataWithPointer {
|
||||
return (result, bytes)
|
||||
}
|
||||
result &= 0x7F
|
||||
while self.bitArray[self.index - 1] & 0x80 != 0 {
|
||||
while self.prevAlignedByte & 0x80 != 0 {
|
||||
let byte = self.alignedByte()
|
||||
if i >= 9 || byte == 0x00 {
|
||||
throw XZError.MultiByteIntegerError
|
||||
|
||||
@@ -148,7 +148,7 @@ public final class ZlibArchive: Archive {
|
||||
|
||||
let out = try Deflate.decompress(&pointerData)
|
||||
|
||||
let adler32 = pointerData.intFromBits(count: 32).reverseBytes()
|
||||
let adler32 = pointerData.intFromAlignedBytes(count: 4).reverseBytes()
|
||||
guard CheckSums.adler32(out) == adler32 else { throw ZlibError.WrongAdler32(Data(bytes: out)) }
|
||||
|
||||
return Data(bytes: out)
|
||||
|
||||
Reference in new issue
Block a user