Files
SWCompression/Sources/7zHeader.swift
T
2017-08-19 15:46:43 +03:00

68 lines
2.0 KiB
Swift

// Copyright (c) 2017 Timofey Solomko
// Licensed under MIT License
//
// See LICENSE for license information
import Foundation
class SevenZipHeader {
var archiveProperties: [SevenZipProperty]?
var additionalStreams: SevenZipStreamInfo?
var mainStreams: SevenZipStreamInfo?
var fileInfo: SevenZipFileInfo?
init(_ bitReader: BitReader) throws {
var type = bitReader.byte()
if type == 0x02 {
archiveProperties = try SevenZipProperty.getProperties(bitReader)
type = bitReader.byte()
}
if type == 0x03 {
throw SevenZipError.additionalStreamsNotSupported
}
if type == 0x04 {
mainStreams = try SevenZipStreamInfo(bitReader)
type = bitReader.byte()
}
if type == 0x05 {
fileInfo = try SevenZipFileInfo(bitReader)
type = bitReader.byte()
}
if type != 0x00 {
throw SevenZipError.internalStructureError
}
}
convenience init(_ bitReader: BitReader, using streamInfo: SevenZipStreamInfo) throws {
let folder = streamInfo.coderInfo.folders[0]
guard let packInfo = streamInfo.packInfo
else { throw SevenZipError.internalStructureError }
let folderOffset = SevenZipContainer.signatureHeaderSize + packInfo.packPosition
bitReader.index = folderOffset
let packedHeaderData = Data(bitReader.bytes(count: packInfo.packSizes[0]))
let headerData = try folder.unpack(data: packedHeaderData)
guard headerData.count == folder.unpackSize()
else { throw SevenZipError.wrongSize }
if let crc = folder.crc {
guard CheckSums.crc32(headerData) == crc
else { throw SevenZipError.wrongCRC }
}
let headerBitReader = BitReader(data: headerData, bitOrder: .straight)
guard headerBitReader.byte() == 0x01
else { throw SevenZipError.internalStructureError }
try self.init(headerBitReader)
}
}