From 0ff3e2f5e36a9d5f719e0113232b62fc0af006f7 Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Sat, 19 Aug 2017 15:46:43 +0300 Subject: [PATCH] Significantly reduce number of error cases in 7zError Also some of them were renamed. --- Sources/7zCoder.swift | 4 +--- Sources/7zCoderInfo.swift | 6 +++--- Sources/7zContainer.swift | 22 ++++++++++---------- Sources/7zError.swift | 39 ++++++++++------------------------- Sources/7zFileInfo.swift | 12 +++++------ Sources/7zFolder.swift | 16 +++++++------- Sources/7zHeader.swift | 8 +++---- Sources/7zPackInfo.swift | 2 +- Sources/7zStreamInfo.swift | 2 +- Sources/7zSubstreamInfo.swift | 2 +- 10 files changed, 47 insertions(+), 66 deletions(-) diff --git a/Sources/7zCoder.swift b/Sources/7zCoder.swift index 09bf199e..27011372 100644 --- a/Sources/7zCoder.swift +++ b/Sources/7zCoder.swift @@ -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 diff --git a/Sources/7zCoderInfo.swift b/Sources/7zCoderInfo.swift index 79f4952b..27b59ee2 100644 --- a/Sources/7zCoderInfo.swift +++ b/Sources/7zCoderInfo.swift @@ -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.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 } diff --git a/Sources/7zError.swift b/Sources/7zError.swift index b4869f9e..b7960351 100644 --- a/Sources/7zError.swift +++ b/Sources/7zError.swift @@ -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 } diff --git a/Sources/7zFileInfo.swift b/Sources/7zFileInfo.swift index 3f302ca1..c996e687 100644 --- a/Sources/7zFileInfo.swift +++ b/Sources/7zFileInfo.swift @@ -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))) } diff --git a/Sources/7zFolder.swift b/Sources/7zFolder.swift index cff6843b..713cf40f 100644 --- a/Sources/7zFolder.swift +++ b/Sources/7zFolder.swift @@ -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 } diff --git a/Sources/7zHeader.swift b/Sources/7zHeader.swift index 16b2f7e8..3be7c58f 100644 --- a/Sources/7zHeader.swift +++ b/Sources/7zHeader.swift @@ -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) } diff --git a/Sources/7zPackInfo.swift b/Sources/7zPackInfo.swift index 6e70aceb..acd1a25e 100644 --- a/Sources/7zPackInfo.swift +++ b/Sources/7zPackInfo.swift @@ -41,7 +41,7 @@ class SevenZipPackInfo { } if type != 0x00 { - throw SevenZipError.wrongEnd + throw SevenZipError.internalStructureError } } diff --git a/Sources/7zStreamInfo.swift b/Sources/7zStreamInfo.swift index 06c24c3b..61828896 100644 --- a/Sources/7zStreamInfo.swift +++ b/Sources/7zStreamInfo.swift @@ -32,7 +32,7 @@ class SevenZipStreamInfo { } if type != 0x00 { - throw SevenZipError.wrongEnd + throw SevenZipError.internalStructureError } } diff --git a/Sources/7zSubstreamInfo.swift b/Sources/7zSubstreamInfo.swift index c2904d39..befb66f6 100644 --- a/Sources/7zSubstreamInfo.swift +++ b/Sources/7zSubstreamInfo.swift @@ -80,7 +80,7 @@ class SevenZipSubstreamInfo { } if type != 0x00 { - throw SevenZipError.wrongEnd + throw SevenZipError.internalStructureError } }