Add splitUnarchive function for XZ

This commit is contained in:
Timofey Solomko
2017-10-31 21:37:36 +03:00
parent c8a0ea72f2
commit eeaee7cd9d
+35
View File
@@ -65,6 +65,41 @@ public class XZArchive: Archive {
return result
}
public static func splitUnarchive(archive data: Data) throws -> [Data] {
// Same code as in `unarchive(archive:)` but with different type of `result`.
let pointerData = DataWithPointer(data: data)
var result = [Data]()
streamLoop: while !pointerData.isAtTheEnd {
result.append(try processStream(pointerData))
guard !pointerData.isAtTheEnd else { break streamLoop }
var paddingBytes = 0
while true {
let byte = pointerData.byte()
if byte != 0 {
if paddingBytes % 4 != 0 {
throw XZError.wrongPadding
} else {
break
}
}
if pointerData.isAtTheEnd {
if byte != 0 || paddingBytes % 4 != 3 {
throw XZError.wrongPadding
} else {
break streamLoop
}
}
paddingBytes += 1
}
pointerData.index -= 1
}
return result
}
private static func processStream(_ pointerData: DataWithPointer) throws -> Data {
var out = Data()