mirror of
https://github.com/tsolomko/SWCompression.git
synced 2026-06-23 14:56:41 +00:00
Significantly reduce number of error cases in 7zError
Also some of them were renamed.
This commit is contained in:
@@ -35,13 +35,11 @@ class SevenZipCoder {
|
||||
init(_ bitReader: BitReader) throws {
|
||||
let flags = bitReader.byte()
|
||||
guard flags & 0xC0 == 0
|
||||
else { throw SevenZipError.reservedCodecFlags }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
idSize = (flags & 0x0F).toInt()
|
||||
isComplex = flags & 0x10 != 0
|
||||
hasAttributes = flags & 0x20 != 0
|
||||
|
||||
guard flags & 0x80 == 0 else { throw SevenZipError.altMethodsNotSupported }
|
||||
|
||||
id = bitReader.bytes(count: idSize)
|
||||
|
||||
numInStreams = isComplex ? bitReader.szMbd() : 1
|
||||
|
||||
@@ -19,7 +19,7 @@ class SevenZipCoderInfo {
|
||||
|
||||
init(_ bitReader: BitReader) throws {
|
||||
var type = bitReader.byte()
|
||||
guard type == 0x0B else { throw SevenZipError.wrongPropertyID }
|
||||
guard type == 0x0B else { throw SevenZipError.internalStructureError }
|
||||
|
||||
numFolders = bitReader.szMbd()
|
||||
external = bitReader.byte()
|
||||
@@ -32,7 +32,7 @@ class SevenZipCoderInfo {
|
||||
}
|
||||
|
||||
type = bitReader.byte()
|
||||
guard type == 0x0C else { throw SevenZipError.wrongPropertyID }
|
||||
guard type == 0x0C else { throw SevenZipError.internalStructureError }
|
||||
|
||||
for folder in folders {
|
||||
for _ in 0..<folder.totalOutputStreams {
|
||||
@@ -54,7 +54,7 @@ class SevenZipCoderInfo {
|
||||
}
|
||||
|
||||
if type != 0x00 {
|
||||
throw SevenZipError.wrongEnd
|
||||
throw SevenZipError.internalStructureError
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
-11
@@ -49,9 +49,9 @@ public class SevenZipContainer: Container {
|
||||
// Without `SevenZipStreamInfo` and `SevenZipPackInfo` objects,
|
||||
// we cannot find file data location in container.
|
||||
guard let streamInfo = header.mainStreams
|
||||
else { throw SevenZipError.noStreamFound }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
guard let packInfo = streamInfo.packInfo
|
||||
else { throw SevenZipError.noPackInfoFound }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
|
||||
// SubstreamInfo is required to get files' data, and without it we can only return files' info.
|
||||
// Additionally, `SevenZipEntry.data()` will throw `SevenZipError.dataIsUnavailable`
|
||||
@@ -64,7 +64,7 @@ public class SevenZipContainer: Container {
|
||||
|
||||
// Check if there is enough folders.
|
||||
guard folderIndex < streamInfo.coderInfo.numFolders
|
||||
else { throw SevenZipError.notEnoughFolders }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
|
||||
/// Folder, which contains current file.
|
||||
let folder = streamInfo.coderInfo.folders[folderIndex]
|
||||
@@ -108,13 +108,13 @@ public class SevenZipContainer: Container {
|
||||
// File's unpack size is required to proceed.
|
||||
// Next check ensures that we don't `unpackSizes` array's boundaries.
|
||||
guard nonEmptyFileIndex < substreamInfo.unpackSizes.count
|
||||
else { throw SevenZipError.noFileSize }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
|
||||
let fileSize = substreamInfo.unpackSizes[nonEmptyFileIndex]
|
||||
|
||||
// Check, if we aren't about to read too much from a stream.
|
||||
guard rawFileData.index + fileSize <= rawFileData.size
|
||||
else { throw SevenZipError.streamOverread }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
|
||||
let fileData = Data(bytes: rawFileData.bytes(count: fileSize))
|
||||
|
||||
@@ -137,7 +137,7 @@ public class SevenZipContainer: Container {
|
||||
if fileInFolderCount >= folder.numUnpackSubstreams { // If we read all files in folder...
|
||||
// We need to check folder's unpacked size as well as its CRC32 (if it is available).
|
||||
guard folderUnpackSize == folder.unpackSize()
|
||||
else { throw SevenZipError.wrongDataSize }
|
||||
else { throw SevenZipError.wrongSize }
|
||||
if let storedFolderCRC = folder.crc {
|
||||
guard folderCRC == storedFolderCRC
|
||||
else { throw SevenZipError.wrongCRC }
|
||||
@@ -196,7 +196,7 @@ public class SevenZipContainer: Container {
|
||||
|
||||
// Check archive version.
|
||||
guard bitReader.bytes(count: 2) == [0, 4] // 7zFormat.txt says it should be [0, 2] instead.
|
||||
else { throw SevenZipError.wrongVersion }
|
||||
else { throw SevenZipError.wrongFormatVersion }
|
||||
|
||||
let startHeaderCRC = bitReader.uint32()
|
||||
|
||||
@@ -207,7 +207,7 @@ public class SevenZipContainer: Container {
|
||||
|
||||
bitReader.index = 12
|
||||
guard CheckSums.crc32(bitReader.bytes(count: 20)) == startHeaderCRC
|
||||
else { throw SevenZipError.wrongStartHeaderCRC }
|
||||
else { throw SevenZipError.wrongCRC }
|
||||
|
||||
// **Header**
|
||||
bitReader.index += nextHeaderOffset
|
||||
@@ -229,17 +229,17 @@ public class SevenZipContainer: Container {
|
||||
header = try SevenZipHeader(bitReader)
|
||||
headerEndIndex = bitReader.index
|
||||
} else {
|
||||
throw SevenZipError.wrongPropertyID
|
||||
throw SevenZipError.internalStructureError
|
||||
}
|
||||
|
||||
// Check header size
|
||||
guard headerEndIndex - headerStartIndex == nextHeaderSize
|
||||
else { throw SevenZipError.wrongHeaderSize }
|
||||
else { throw SevenZipError.wrongSize }
|
||||
|
||||
// Check header CRC
|
||||
bitReader.index = headerStartIndex
|
||||
guard CheckSums.crc32(bitReader.bytes(count: nextHeaderSize)) == nextHeaderCRC
|
||||
else { throw SevenZipError.wrongHeaderCRC }
|
||||
else { throw SevenZipError.wrongCRC }
|
||||
|
||||
return header
|
||||
}
|
||||
|
||||
+11
-28
@@ -7,36 +7,19 @@ import Foundation
|
||||
|
||||
public enum SevenZipError: Error {
|
||||
case wrongSignature
|
||||
case wrongVersion
|
||||
case wrongStartHeaderCRC
|
||||
case wrongHeaderSize
|
||||
case wrongPropertyID
|
||||
case wrongHeaderCRC
|
||||
case wrongExternal
|
||||
case reservedCodecFlags
|
||||
case unknownNumFolders
|
||||
case wrongEnd
|
||||
case externalNotSupported
|
||||
case altMethodsNotSupported
|
||||
case wrongStreamsNumber
|
||||
case multiStreamNotSupported
|
||||
case compressionNotSupported
|
||||
case wrongDataSize
|
||||
case wrongFormatVersion
|
||||
|
||||
case wrongCRC
|
||||
case wrongCoderProperties
|
||||
case noPackInfo
|
||||
case wrongFileProperty
|
||||
case wrongFileNameLength
|
||||
case wrongFileNames
|
||||
case wrongSize
|
||||
|
||||
case startPosNotSupported
|
||||
case incompleteProperty
|
||||
case externalNotSupported
|
||||
case multiStreamNotSupported
|
||||
case additionalStreamsNotSupported
|
||||
case noFileSize
|
||||
case notEnoughFolders
|
||||
case notEnoughStreams
|
||||
case noStreamFound
|
||||
case noPackInfoFound
|
||||
case streamOverread
|
||||
case dataIsUnavailable
|
||||
case compressionNotSupported
|
||||
case encryptionNotSupported
|
||||
|
||||
case dataIsUnavailable
|
||||
|
||||
case internalStructureError
|
||||
}
|
||||
|
||||
@@ -45,12 +45,12 @@ class SevenZipFileInfo {
|
||||
bitReader.align()
|
||||
case 0x0F: // EmptyFile
|
||||
guard let emptyStreamCount = isEmptyStream?.reduce(0, { $0 + $1})
|
||||
else { throw SevenZipError.wrongFileProperty }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
isEmptyFile = bitReader.bits(count: emptyStreamCount.toInt())
|
||||
bitReader.align()
|
||||
case 0x10: // AntiFile (used in backups to indicate that file was removed)
|
||||
guard let emptyStreamCount = isEmptyStream?.reduce(0, { $0 + $1})
|
||||
else { throw SevenZipError.wrongFileProperty }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
isAntiFile = bitReader.bits(count: emptyStreamCount.toInt())
|
||||
bitReader.align()
|
||||
case 0x11: // File name
|
||||
@@ -58,7 +58,7 @@ class SevenZipFileInfo {
|
||||
guard external == 0
|
||||
else { throw SevenZipError.externalNotSupported }
|
||||
guard (propertySize - 1) & 1 == 0
|
||||
else { throw SevenZipError.wrongFileNameLength }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
let names = bitReader.bytes(count: propertySize - 1)
|
||||
var nextFile = 0
|
||||
var nextName = 0
|
||||
@@ -74,7 +74,7 @@ class SevenZipFileInfo {
|
||||
}
|
||||
}
|
||||
guard nextName == names.count && nextFile == numFiles
|
||||
else { throw SevenZipError.wrongFileNames }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
case 0x12: // Creation time
|
||||
let timesDefined = bitReader.defBits(count: numFiles)
|
||||
bitReader.align()
|
||||
@@ -130,11 +130,11 @@ class SevenZipFileInfo {
|
||||
throw SevenZipError.startPosNotSupported
|
||||
case 0x19: // "Dummy". Used for alignment/padding.
|
||||
guard bitReader.size - bitReader.index >= propertySize
|
||||
else { throw SevenZipError.incompleteProperty }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
bitReader.index += propertySize
|
||||
default: // Unknown property
|
||||
guard bitReader.size - bitReader.index >= propertySize
|
||||
else { throw SevenZipError.incompleteProperty }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
unknownProperties.append(SevenZipProperty(propertyType, propertySize,
|
||||
bitReader.bytes(count: propertySize)))
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ class SevenZipFolder {
|
||||
totalInputStreams += coder.numInStreams
|
||||
}
|
||||
|
||||
guard totalOutputStreams != 0 else { throw SevenZipError.wrongStreamsNumber }
|
||||
guard totalOutputStreams != 0 else { throw SevenZipError.internalStructureError }
|
||||
|
||||
numBindPairs = totalOutputStreams - 1
|
||||
if numBindPairs > 0 {
|
||||
@@ -56,7 +56,7 @@ class SevenZipFolder {
|
||||
}
|
||||
}
|
||||
|
||||
guard totalInputStreams >= numBindPairs else { throw SevenZipError.wrongStreamsNumber }
|
||||
guard totalInputStreams >= numBindPairs else { throw SevenZipError.internalStructureError }
|
||||
|
||||
numPackedStreams = totalInputStreams - numBindPairs
|
||||
if numPackedStreams == 1 {
|
||||
@@ -68,7 +68,7 @@ class SevenZipFolder {
|
||||
i += 1
|
||||
}
|
||||
if i == totalInputStreams {
|
||||
throw SevenZipError.wrongStreamsNumber
|
||||
throw SevenZipError.internalStructureError
|
||||
}
|
||||
packedStreams.append(i)
|
||||
} else {
|
||||
@@ -153,9 +153,9 @@ class SevenZipFolder {
|
||||
} else if coder.id == SevenZipCoder.ID.lzma2 {
|
||||
// Dictionary size is stored in coder's properties.
|
||||
guard let properties = coder.properties
|
||||
else { throw SevenZipError.wrongCoderProperties }
|
||||
else { throw LZMA2Error.wrongProperties }
|
||||
guard properties.count == 1
|
||||
else { throw SevenZipError.wrongCoderProperties }
|
||||
else { throw LZMA2Error.wrongProperties }
|
||||
|
||||
let pointerData = DataWithPointer(data: decodedData)
|
||||
decodedData = Data(bytes: try LZMA2.decompress(LZMA2.dictionarySize(properties[0]),
|
||||
@@ -163,9 +163,9 @@ class SevenZipFolder {
|
||||
} else if coder.id == SevenZipCoder.ID.lzma {
|
||||
// Both properties' byte (lp, lc, pb) and dictionary size are stored in coder's properties.
|
||||
guard let properties = coder.properties
|
||||
else { throw SevenZipError.wrongCoderProperties }
|
||||
else { throw LZMAError.wrongProperties }
|
||||
guard properties.count == 5
|
||||
else { throw SevenZipError.wrongCoderProperties }
|
||||
else { throw LZMAError.wrongProperties }
|
||||
|
||||
let pointerData = DataWithPointer(data: decodedData)
|
||||
let lzmaDecoder = try LZMADecoder(pointerData)
|
||||
@@ -186,7 +186,7 @@ class SevenZipFolder {
|
||||
}
|
||||
|
||||
guard decodedData.count == unpackSize
|
||||
else { throw SevenZipError.wrongDataSize }
|
||||
else { throw SevenZipError.wrongSize }
|
||||
}
|
||||
return decodedData
|
||||
}
|
||||
|
||||
@@ -35,14 +35,14 @@ class SevenZipHeader {
|
||||
}
|
||||
|
||||
if type != 0x00 {
|
||||
throw SevenZipError.wrongEnd
|
||||
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.noPackInfo }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
|
||||
let folderOffset = SevenZipContainer.signatureHeaderSize + packInfo.packPosition
|
||||
bitReader.index = folderOffset
|
||||
@@ -51,7 +51,7 @@ class SevenZipHeader {
|
||||
let headerData = try folder.unpack(data: packedHeaderData)
|
||||
|
||||
guard headerData.count == folder.unpackSize()
|
||||
else { throw SevenZipError.wrongDataSize }
|
||||
else { throw SevenZipError.wrongSize }
|
||||
if let crc = folder.crc {
|
||||
guard CheckSums.crc32(headerData) == crc
|
||||
else { throw SevenZipError.wrongCRC }
|
||||
@@ -60,7 +60,7 @@ class SevenZipHeader {
|
||||
let headerBitReader = BitReader(data: headerData, bitOrder: .straight)
|
||||
|
||||
guard headerBitReader.byte() == 0x01
|
||||
else { throw SevenZipError.wrongPropertyID }
|
||||
else { throw SevenZipError.internalStructureError }
|
||||
try self.init(headerBitReader)
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class SevenZipPackInfo {
|
||||
}
|
||||
|
||||
if type != 0x00 {
|
||||
throw SevenZipError.wrongEnd
|
||||
throw SevenZipError.internalStructureError
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class SevenZipStreamInfo {
|
||||
}
|
||||
|
||||
if type != 0x00 {
|
||||
throw SevenZipError.wrongEnd
|
||||
throw SevenZipError.internalStructureError
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ class SevenZipSubstreamInfo {
|
||||
}
|
||||
|
||||
if type != 0x00 {
|
||||
throw SevenZipError.wrongEnd
|
||||
throw SevenZipError.internalStructureError
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user