mirror of
https://github.com/tsolomko/SWCompression.git
synced 2026-06-23 14:56:41 +00:00
Replace usage of ByteReader with LittleEndianByteReader
This commit is contained in:
@@ -49,10 +49,10 @@ public class SevenZipContainer: Container {
|
||||
/// Combined calculated CRC of entire folder == all files in folder.
|
||||
var folderCRC = CheckSums.crc32(Data())
|
||||
|
||||
/// `ByteReader` object with unpacked stream's data.
|
||||
var unpackedStreamData = ByteReader(data: Data())
|
||||
/// `LittleEndianByteReader` object with unpacked stream's data.
|
||||
var unpackedStreamData = LittleEndianByteReader(data: Data())
|
||||
|
||||
let byteReader = ByteReader(data: data)
|
||||
let byteReader = LittleEndianByteReader(data: data)
|
||||
|
||||
for file in files {
|
||||
if file.isEmptyStream {
|
||||
@@ -108,7 +108,7 @@ public class SevenZipContainer: Container {
|
||||
|
||||
// One stream can contain data for several files, so we need to decode the stream first, then split
|
||||
// it into files.
|
||||
unpackedStreamData = ByteReader(data: try folder.unpack(data: streamData))
|
||||
unpackedStreamData = LittleEndianByteReader(data: try folder.unpack(data: streamData))
|
||||
}
|
||||
|
||||
// `SevenZipSubstreamInfo` object must contain information about file's size and may also contain
|
||||
|
||||
@@ -164,7 +164,7 @@ class SevenZipFolder {
|
||||
properties.count == 1
|
||||
else { throw LZMA2Error.wrongDictionarySize }
|
||||
|
||||
decodedData = try LZMA2.decompress(ByteReader(data: decodedData), properties[0])
|
||||
decodedData = try LZMA2.decompress(LittleEndianByteReader(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,
|
||||
@@ -185,7 +185,7 @@ class SevenZipFolder {
|
||||
properties.count == 1
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
|
||||
decodedData = DeltaFilter.decode(ByteReader(data: decodedData), (properties[0] &+ 1).toInt())
|
||||
decodedData = DeltaFilter.decode(LittleEndianByteReader(data: decodedData), (properties[0] &+ 1).toInt())
|
||||
} else if coder.isEncryptionMethod {
|
||||
throw SevenZipError.encryptionNotSupported
|
||||
} else {
|
||||
|
||||
@@ -8,7 +8,7 @@ import BitByteData
|
||||
|
||||
final class DeltaFilter {
|
||||
|
||||
static func decode(_ byteReader: ByteReader, _ distance: Int) -> Data {
|
||||
static func decode(_ byteReader: LittleEndianByteReader, _ distance: Int) -> Data {
|
||||
var out = [UInt8]()
|
||||
|
||||
var pos = 0
|
||||
|
||||
@@ -53,11 +53,11 @@ public struct GzipHeader {
|
||||
it might not be archived with GZip at all.
|
||||
*/
|
||||
public init(archive data: Data) throws {
|
||||
let byteReader = ByteReader(data: data)
|
||||
let byteReader = LittleEndianByteReader(data: data)
|
||||
try self.init(byteReader)
|
||||
}
|
||||
|
||||
init(_ byteReader: ByteReader) throws {
|
||||
init(_ byteReader: LittleEndianByteReader) throws {
|
||||
// First two bytes should be correct 'magic' bytes
|
||||
let magic = byteReader.uint16()
|
||||
guard magic == 0x8b1f else { throw GzipError.wrongMagic }
|
||||
|
||||
@@ -23,7 +23,7 @@ public class LZMA: DecompressionAlgorithm {
|
||||
- Returns: Decompressed data.
|
||||
*/
|
||||
public static func decompress(data: Data) throws -> Data {
|
||||
let byteReader = ByteReader(data: data)
|
||||
let byteReader = LittleEndianByteReader(data: data)
|
||||
let properties = try LZMAProperties(byteReader)
|
||||
let uncompSize = byteReader.int(fromBytes: 8)
|
||||
return try decompress(byteReader, properties, uncompSize)
|
||||
@@ -52,11 +52,11 @@ public class LZMA: DecompressionAlgorithm {
|
||||
public static func decompress(data: Data,
|
||||
properties: LZMAProperties,
|
||||
uncompressedSize: Int? = nil) throws -> Data {
|
||||
let byteReader = ByteReader(data: data)
|
||||
let byteReader = LittleEndianByteReader(data: data)
|
||||
return try decompress(byteReader, properties, uncompressedSize)
|
||||
}
|
||||
|
||||
static func decompress(_ byteReader: ByteReader,
|
||||
static func decompress(_ byteReader: LittleEndianByteReader,
|
||||
_ properties: LZMAProperties,
|
||||
_ uncompSize: Int?) throws -> Data {
|
||||
let decoder = LZMADecoder(byteReader)
|
||||
|
||||
@@ -8,7 +8,7 @@ import BitByteData
|
||||
|
||||
final class LZMADecoder {
|
||||
|
||||
private let byteReader: ByteReader
|
||||
private let byteReader: LittleEndianByteReader
|
||||
|
||||
var properties = LZMAProperties()
|
||||
|
||||
@@ -72,7 +72,7 @@ final class LZMADecoder {
|
||||
/// Used to select exact variable from 'IsRep', 'IsRepG0', 'IsRepG1' and 'IsRepG2' arrays.
|
||||
private var state = 0
|
||||
|
||||
init(_ byteReader: ByteReader) {
|
||||
init(_ byteReader: LittleEndianByteReader) {
|
||||
self.byteReader = byteReader
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ public struct LZMAProperties {
|
||||
self.dictionarySize = dictSize
|
||||
}
|
||||
|
||||
init(_ byteReader: ByteReader) throws {
|
||||
init(_ byteReader: LittleEndianByteReader) throws {
|
||||
try self.init(lzmaByte: byteReader.byte(), byteReader.int(fromBytes: 4))
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import BitByteData
|
||||
|
||||
final class LZMARangeDecoder {
|
||||
|
||||
private var byteReader: ByteReader
|
||||
private var byteReader: LittleEndianByteReader
|
||||
|
||||
private var range = 0xFFFFFFFF as UInt32
|
||||
private var code = 0 as UInt32
|
||||
@@ -18,7 +18,7 @@ final class LZMARangeDecoder {
|
||||
return self.code == 0
|
||||
}
|
||||
|
||||
init?(_ byteReader: ByteReader) {
|
||||
init?(_ byteReader: LittleEndianByteReader) {
|
||||
self.byteReader = byteReader
|
||||
|
||||
let byte = self.byteReader.byte()
|
||||
@@ -31,7 +31,7 @@ final class LZMARangeDecoder {
|
||||
}
|
||||
|
||||
init() {
|
||||
self.byteReader = ByteReader(data: Data())
|
||||
self.byteReader = LittleEndianByteReader(data: Data())
|
||||
}
|
||||
|
||||
/// `range` property cannot be smaller than `(1 << 24)`. This function keeps it bigger.
|
||||
|
||||
@@ -23,11 +23,11 @@ public class LZMA2: DecompressionAlgorithm {
|
||||
- Returns: Decompressed data.
|
||||
*/
|
||||
public static func decompress(data: Data) throws -> Data {
|
||||
let byteReader = ByteReader(data: data)
|
||||
let byteReader = LittleEndianByteReader(data: data)
|
||||
return try decompress(byteReader, byteReader.byte())
|
||||
}
|
||||
|
||||
static func decompress(_ byteReader: ByteReader, _ dictSizeByte: UInt8) throws -> Data {
|
||||
static func decompress(_ byteReader: LittleEndianByteReader, _ dictSizeByte: UInt8) throws -> Data {
|
||||
let decoder = try LZMA2Decoder(byteReader, dictSizeByte)
|
||||
try decoder.decode()
|
||||
return Data(decoder.out)
|
||||
|
||||
@@ -8,14 +8,14 @@ import BitByteData
|
||||
|
||||
final class LZMA2Decoder {
|
||||
|
||||
private let byteReader: ByteReader
|
||||
private let byteReader: LittleEndianByteReader
|
||||
private let decoder: LZMADecoder
|
||||
|
||||
var out: [UInt8] {
|
||||
return self.decoder.out
|
||||
}
|
||||
|
||||
init(_ byteReader: ByteReader, _ dictSizeByte: UInt8) throws {
|
||||
init(_ byteReader: LittleEndianByteReader, _ dictSizeByte: UInt8) throws {
|
||||
self.byteReader = byteReader
|
||||
self.decoder = LZMADecoder(byteReader)
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import Foundation
|
||||
import BitByteData
|
||||
|
||||
extension ByteReader {
|
||||
extension LittleEndianByteReader {
|
||||
|
||||
/**
|
||||
Reads a `String` field from TAR container. The end of the field is defined by either:
|
||||
|
||||
@@ -199,7 +199,7 @@ public struct TarEntryInfo: ContainerEntryInfo {
|
||||
self.blockStartIndex = 0
|
||||
}
|
||||
|
||||
init(_ byteReader: ByteReader, _ global: TarExtendedHeader?, _ local: TarExtendedHeader?,
|
||||
init(_ byteReader: LittleEndianByteReader, _ global: TarExtendedHeader?, _ local: TarExtendedHeader?,
|
||||
_ longName: String?, _ longLinkName: String?) throws {
|
||||
self.blockStartIndex = byteReader.offset
|
||||
|
||||
|
||||
@@ -10,14 +10,14 @@ import BitByteData
|
||||
// impossible to do so, since `TarEntryInfo.init(...)` is throwing and `IteratorProtocol.next()` cannot be throwing.
|
||||
struct TarEntryInfoProvider {
|
||||
|
||||
private let byteReader: ByteReader
|
||||
private let byteReader: LittleEndianByteReader
|
||||
private var lastGlobalExtendedHeader: TarExtendedHeader?
|
||||
private var lastLocalExtendedHeader: TarExtendedHeader?
|
||||
private var longLinkName: String?
|
||||
private var longName: String?
|
||||
|
||||
init(_ data: Data) {
|
||||
self.byteReader = ByteReader(data: data)
|
||||
self.byteReader = LittleEndianByteReader(data: data)
|
||||
}
|
||||
|
||||
mutating func next() throws -> TarEntryInfo? {
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import Foundation
|
||||
import BitByteData
|
||||
|
||||
extension ByteReader {
|
||||
extension LittleEndianByteReader {
|
||||
|
||||
func multiByteDecode() throws -> Int {
|
||||
var i = 1
|
||||
|
||||
@@ -26,7 +26,7 @@ public class XZArchive: Archive {
|
||||
*/
|
||||
public static func unarchive(archive data: Data) throws -> Data {
|
||||
/// Object with input data which supports convenient work with bytes.
|
||||
let byteReader = ByteReader(data: data)
|
||||
let byteReader = LittleEndianByteReader(data: data)
|
||||
|
||||
// Note: We don't check footer's magic bytes at the beginning,
|
||||
// because it is impossible to determine the end of each stream in multi-stream archives
|
||||
@@ -64,7 +64,7 @@ public class XZArchive: Archive {
|
||||
*/
|
||||
public static func splitUnarchive(archive data: Data) throws -> [Data] {
|
||||
// Same code as in `unarchive(archive:)` but with different type of `result`.
|
||||
let byteReader = ByteReader(data: data)
|
||||
let byteReader = LittleEndianByteReader(data: data)
|
||||
|
||||
var result = [Data]()
|
||||
while !byteReader.isFinished {
|
||||
@@ -79,7 +79,7 @@ public class XZArchive: Archive {
|
||||
return result
|
||||
}
|
||||
|
||||
private static func processStream(_ byteReader: ByteReader) throws -> (data: Data, checkError: Bool) {
|
||||
private static func processStream(_ byteReader: LittleEndianByteReader) throws -> (data: Data, checkError: Bool) {
|
||||
var out = Data()
|
||||
|
||||
let streamHeader = try XZStreamHeader(byteReader)
|
||||
@@ -122,7 +122,7 @@ public class XZArchive: Archive {
|
||||
}
|
||||
|
||||
private static func processIndex(_ blockInfos: [(unpaddedSize: Int, uncompSize: Int)],
|
||||
_ byteReader: ByteReader) throws -> Int {
|
||||
_ byteReader: LittleEndianByteReader) throws -> Int {
|
||||
let indexStartIndex = byteReader.offset - 1
|
||||
let recordsCount = try byteReader.multiByteDecode()
|
||||
guard recordsCount == blockInfos.count
|
||||
@@ -159,7 +159,7 @@ public class XZArchive: Archive {
|
||||
}
|
||||
|
||||
private static func processFooter(_ streamHeader: XZStreamHeader, _ indexSize: Int,
|
||||
_ byteReader: ByteReader) throws {
|
||||
_ byteReader: LittleEndianByteReader) throws {
|
||||
let footerCRC = byteReader.uint32()
|
||||
/// Indicates the size of Index field. Should match its real size.
|
||||
let backwardSize = (byteReader.int(fromBytes: 4) + 1) * 4
|
||||
@@ -184,7 +184,7 @@ public class XZArchive: Archive {
|
||||
}
|
||||
|
||||
/// Returns `true` if end of archive is reached, `false` otherwise.
|
||||
private static func processPadding(_ byteReader: ByteReader) throws {
|
||||
private static func processPadding(_ byteReader: LittleEndianByteReader) throws {
|
||||
guard !byteReader.isFinished
|
||||
else { return }
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ struct XZBlock {
|
||||
return data.count
|
||||
}
|
||||
|
||||
init(_ blockHeaderSize: UInt8, _ byteReader: ByteReader, _ checkSize: Int) throws {
|
||||
init(_ blockHeaderSize: UInt8, _ byteReader: LittleEndianByteReader, _ checkSize: Int) throws {
|
||||
let blockHeaderStartIndex = byteReader.offset - 1
|
||||
let realBlockHeaderSize = (blockHeaderSize.toInt() + 1) * 4
|
||||
|
||||
@@ -34,7 +34,7 @@ struct XZBlock {
|
||||
/// Should match the size of data after decompression.
|
||||
let uncompressedSize = blockFlags & 0x80 != 0 ? try byteReader.multiByteDecode() : -1
|
||||
|
||||
var filters: [(ByteReader) throws -> Data] = []
|
||||
var filters: [(LittleEndianByteReader) throws -> Data] = []
|
||||
for _ in 0..<filtersCount {
|
||||
let filterID = try byteReader.multiByteDecode()
|
||||
guard UInt64(filterID) < 0x4000000000000000
|
||||
@@ -75,7 +75,7 @@ struct XZBlock {
|
||||
byteReader.offset += 4
|
||||
|
||||
let compressedDataStart = byteReader.offset
|
||||
let out = try filters.reversed().reduce(byteReader) { try ByteReader(data: $1($0)) }
|
||||
let out = try filters.reversed().reduce(byteReader) { try LittleEndianByteReader(data: $1($0)) }
|
||||
|
||||
guard compressedSize < 0 || compressedSize == byteReader.offset - compressedDataStart,
|
||||
uncompressedSize < 0 || uncompressedSize == out.data.count
|
||||
|
||||
@@ -31,7 +31,7 @@ struct XZStreamHeader {
|
||||
let checkType: CheckType
|
||||
let flagsCRC: UInt32
|
||||
|
||||
init(_ byteReader: ByteReader) throws {
|
||||
init(_ byteReader: LittleEndianByteReader) throws {
|
||||
// Check magic number.
|
||||
guard byteReader.bytes(count: 6) == [0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00]
|
||||
else { throw XZError.wrongMagic }
|
||||
|
||||
@@ -16,7 +16,7 @@ struct ExtendedTimestampExtraField: ZipExtraField {
|
||||
var ctime: UInt32?
|
||||
var mtime: UInt32?
|
||||
|
||||
init(_ byteReader: ByteReader, _ size: Int, location: ZipExtraFieldLocation) {
|
||||
init(_ byteReader: LittleEndianByteReader, _ size: Int, location: ZipExtraFieldLocation) {
|
||||
self.size = size
|
||||
self.location = location
|
||||
switch location {
|
||||
@@ -52,7 +52,7 @@ struct NtfsExtraField: ZipExtraField {
|
||||
let ctime: UInt64
|
||||
let mtime: UInt64
|
||||
|
||||
init?(_ byteReader: ByteReader, _ size: Int, location: ZipExtraFieldLocation) {
|
||||
init?(_ byteReader: LittleEndianByteReader, _ size: Int, location: ZipExtraFieldLocation) {
|
||||
self.size = size
|
||||
self.location = location
|
||||
byteReader.offset += 4 // Skip reserved bytes
|
||||
@@ -77,7 +77,7 @@ struct InfoZipUnixExtraField: ZipExtraField {
|
||||
let uid: Int
|
||||
let gid: Int
|
||||
|
||||
init?(_ byteReader: ByteReader, _ size: Int, location: ZipExtraFieldLocation) {
|
||||
init?(_ byteReader: LittleEndianByteReader, _ size: Int, location: ZipExtraFieldLocation) {
|
||||
self.size = size
|
||||
self.location = location
|
||||
switch location {
|
||||
@@ -101,7 +101,7 @@ struct InfoZipNewUnixExtraField: ZipExtraField {
|
||||
var uid: Int?
|
||||
var gid: Int?
|
||||
|
||||
init?(_ byteReader: ByteReader, _ size: Int, location: ZipExtraFieldLocation) {
|
||||
init?(_ byteReader: LittleEndianByteReader, _ size: Int, location: ZipExtraFieldLocation) {
|
||||
self.size = size
|
||||
self.location = location
|
||||
guard byteReader.byte() == 1 // Version must be 1
|
||||
|
||||
@@ -10,7 +10,7 @@ import BitByteData
|
||||
import CoreFoundation
|
||||
#endif
|
||||
|
||||
extension ByteReader {
|
||||
extension LittleEndianByteReader {
|
||||
|
||||
func zipString(_ length: Int, _ useUtf8: Bool) -> String? {
|
||||
guard length > 0
|
||||
|
||||
@@ -43,7 +43,7 @@ struct ZipCentralDirectoryEntry {
|
||||
|
||||
let nextEntryOffset: Int
|
||||
|
||||
init(_ byteReader: ByteReader) throws {
|
||||
init(_ byteReader: LittleEndianByteReader) throws {
|
||||
// Check signature.
|
||||
guard byteReader.uint32() == 0x02014b50
|
||||
else { throw ZipError.wrongSignature }
|
||||
|
||||
@@ -62,7 +62,7 @@ public class ZipContainer: Container {
|
||||
var crc32 = helper.entryInfo.crc
|
||||
|
||||
let fileData: Data
|
||||
let byteReader = ByteReader(data: data)
|
||||
let byteReader = LittleEndianByteReader(data: data)
|
||||
byteReader.offset = helper.dataOffset
|
||||
switch helper.entryInfo.compressionMethod {
|
||||
case .copy:
|
||||
@@ -141,7 +141,7 @@ public class ZipContainer: Container {
|
||||
}
|
||||
|
||||
private static func infoWithHelper(_ data: Data) throws -> [ZipEntryInfoHelper] {
|
||||
let byteReader = ByteReader(data: data)
|
||||
let byteReader = LittleEndianByteReader(data: data)
|
||||
var entries = [ZipEntryInfoHelper]()
|
||||
|
||||
// First, we are looking for End of Central Directory record, specifically, for its signature.
|
||||
|
||||
@@ -17,7 +17,7 @@ struct ZipEndOfCentralDirectory {
|
||||
private(set) var cdSize: UInt64
|
||||
private(set) var cdOffset: UInt64
|
||||
|
||||
init(_ byteReader: ByteReader) throws {
|
||||
init(_ byteReader: LittleEndianByteReader) throws {
|
||||
/// Indicates if Zip64 records should be present.
|
||||
var zip64RecordExists = false
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ public struct ZipEntryInfo: ContainerEntryInfo {
|
||||
/// CRC32 of entry's data.
|
||||
public let crc: UInt32
|
||||
|
||||
init(_ byteReader: ByteReader, _ cdEntry: ZipCentralDirectoryEntry, _ localHeader: ZipLocalHeader,
|
||||
init(_ byteReader: LittleEndianByteReader, _ cdEntry: ZipCentralDirectoryEntry, _ localHeader: ZipLocalHeader,
|
||||
_ hasDataDescriptor: Bool) {
|
||||
self.name = cdEntry.fileName
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ struct ZipEntryInfoHelper {
|
||||
let compSize: UInt64
|
||||
let uncompSize: UInt64
|
||||
|
||||
init(_ byteReader: ByteReader, _ currentDiskNumber: UInt32) throws {
|
||||
init(_ byteReader: LittleEndianByteReader, _ currentDiskNumber: UInt32) throws {
|
||||
// Read Central Directory entry.
|
||||
let cdEntry = try ZipCentralDirectoryEntry(byteReader)
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ public protocol ZipExtraField {
|
||||
- Postcondition: `size` property of a created instance must be equal to the second argument.
|
||||
- Postcondition: exactly `size` amount of bytes must be read by initializer from `byteReader`.
|
||||
*/
|
||||
init?(_ byteReader: ByteReader, _ size: Int, location: ZipExtraFieldLocation)
|
||||
init?(_ byteReader: LittleEndianByteReader, _ size: Int, location: ZipExtraFieldLocation)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ struct ZipLocalHeader {
|
||||
|
||||
let dataOffset: Int
|
||||
|
||||
init(_ byteReader: ByteReader) throws {
|
||||
init(_ byteReader: LittleEndianByteReader) throws {
|
||||
// Check signature.
|
||||
guard byteReader.uint32() == 0x04034b50
|
||||
else { throw ZipError.wrongSignature }
|
||||
|
||||
@@ -40,11 +40,11 @@ public struct ZlibHeader {
|
||||
- Throws: `ZlibError`. It may indicate that either archive is damaged or it might not be archived with Zlib at all.
|
||||
*/
|
||||
public init(archive data: Data) throws {
|
||||
let byteReader = ByteReader(data: data)
|
||||
let byteReader = LittleEndianByteReader(data: data)
|
||||
try self.init(byteReader)
|
||||
}
|
||||
|
||||
init(_ byteReader: ByteReader) throws {
|
||||
init(_ byteReader: LittleEndianByteReader) throws {
|
||||
// compressionMethod and compressionInfo combined are needed later for integrity check.
|
||||
let cmf = byteReader.byte()
|
||||
// First four bits are compression method.
|
||||
|
||||
@@ -16,7 +16,7 @@ struct TestZipExtraField: ZipExtraField {
|
||||
|
||||
var helloString: String?
|
||||
|
||||
init(_ byteReader: ByteReader, _ size: Int, location: ZipExtraFieldLocation) {
|
||||
init(_ byteReader: LittleEndianByteReader, _ size: Int, location: ZipExtraFieldLocation) {
|
||||
self.size = size
|
||||
self.location = location
|
||||
self.helloString = String(data: Data(byteReader.bytes(count: size)), encoding: .utf8)
|
||||
|
||||
Reference in New Issue
Block a user