Use LsbBitReader argument in the Gzip and Zlib headers' initializers

This fixes issues arising from the removal of _subclass_ relations between bit and byte readers in BBD.
This commit is contained in:
Timofey Solomko
2020-01-02 12:29:24 +03:00
parent 3c2769a47a
commit 82f150d437
2 changed files with 20 additions and 20 deletions
+14 -14
View File
@@ -53,23 +53,23 @@ public struct GzipHeader {
it might not be archived with GZip at all.
*/
public init(archive data: Data) throws {
let byteReader = LittleEndianByteReader(data: data)
try self.init(byteReader)
let reader = LsbBitReader(data: data)
try self.init(reader)
}
init(_ byteReader: LittleEndianByteReader) throws {
init(_ reader: LsbBitReader) throws {
// First two bytes should be correct 'magic' bytes
let magic = byteReader.uint16()
let magic = reader.uint16()
guard magic == 0x8b1f else { throw GzipError.wrongMagic }
var headerBytes: [UInt8] = [0x1f, 0x8b]
// Third byte is a method of compression. Only type 8 (DEFLATE) compression is supported for GZip archives.
let method = byteReader.byte()
let method = reader.byte()
guard method == 8 else { throw GzipError.wrongCompressionMethod }
headerBytes.append(method)
self.compressionMethod = .deflate
let rawFlags = byteReader.byte()
let rawFlags = reader.byte()
guard rawFlags & 0xE0 == 0
else { throw GzipError.wrongFlags }
let flags = Flags(rawValue: rawFlags)
@@ -77,16 +77,16 @@ public struct GzipHeader {
var mtime = 0
for i in 0..<4 {
let byte = byteReader.byte()
let byte = reader.byte()
mtime |= byte.toInt() << (8 * i)
headerBytes.append(byte)
}
self.modificationTime = mtime == 0 ? nil : Date(timeIntervalSince1970: TimeInterval(mtime))
let extraFlags = byteReader.byte()
let extraFlags = reader.byte()
headerBytes.append(extraFlags)
let rawOsType = byteReader.byte()
let rawOsType = reader.byte()
self.osType = FileSystemType(rawOsType)
headerBytes.append(rawOsType)
@@ -96,12 +96,12 @@ public struct GzipHeader {
if flags.contains(.fextra) {
var xlen = 0
for i in 0..<2 {
let byte = byteReader.byte()
let byte = reader.byte()
xlen |= byte.toInt() << (8 * i)
headerBytes.append(byte)
}
for _ in 0..<xlen {
headerBytes.append(byteReader.byte())
headerBytes.append(reader.byte())
}
}
@@ -109,7 +109,7 @@ public struct GzipHeader {
if flags.contains(.fname) {
var fnameBytes: [UInt8] = []
while true {
let byte = byteReader.byte()
let byte = reader.byte()
headerBytes.append(byte)
guard byte != 0 else { break }
fnameBytes.append(byte)
@@ -123,7 +123,7 @@ public struct GzipHeader {
if flags.contains(.fcomment) {
var fcommentBytes: [UInt8] = []
while true {
let byte = byteReader.byte()
let byte = reader.byte()
headerBytes.append(byte)
guard byte != 0 else { break }
fcommentBytes.append(byte)
@@ -136,7 +136,7 @@ public struct GzipHeader {
// Some archives may contain 2-bytes checksum
if flags.contains(.fhcrc) {
// Note: it is not actual CRC-16, it is just two least significant bytes of CRC-32.
let crc16 = byteReader.uint16()
let crc16 = reader.uint16()
guard CheckSums.crc32(headerBytes) & 0xFFFF == crc16 else { throw GzipError.wrongHeaderCRC }
}
}
+6 -6
View File
@@ -40,13 +40,13 @@ 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 = LittleEndianByteReader(data: data)
try self.init(byteReader)
let reader = LsbBitReader(data: data)
try self.init(reader)
}
init(_ byteReader: LittleEndianByteReader) throws {
init(_ reader: LsbBitReader) throws {
// compressionMethod and compressionInfo combined are needed later for integrity check.
let cmf = byteReader.byte()
let cmf = reader.byte()
// First four bits are compression method.
// Only compression method = 8 (DEFLATE) is supported.
let compressionMethod = cmf & 0xF
@@ -63,7 +63,7 @@ public struct ZlibHeader {
self.windowSize = windowSize
// fcheck, fdict and compresionLevel together make flags byte which is used in integrity check.
let flags = byteReader.byte()
let flags = reader.byte()
// First five bits are fcheck bits which are supposed to be integrity check:
// let fcheck = flags & 0x1F
@@ -81,7 +81,7 @@ public struct ZlibHeader {
// If preset dictionary is present 4 bytes will be skipped.
if fdict == 1 {
byteReader.offset += 4
reader.offset += 4
}
}