From 52fd14101521efd0c8e970024226e48fc4e8d2ed Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Mon, 14 Nov 2016 23:17:42 +0300 Subject: [PATCH] Added parsing magic number of bzip2 archives. --- Sources/BZip2.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/BZip2.swift b/Sources/BZip2.swift index f9a9f438..f5480444 100644 --- a/Sources/BZip2.swift +++ b/Sources/BZip2.swift @@ -17,6 +17,7 @@ import Foundation - `WrongBlockType`: unsupported block type (not 'pi' or 'sqrt(pi)'). */ public enum BZip2Error: Error { + case WrongMagic /// Compression method was not type 'h' (not Huffman). case WrongCompressionMethod /// Unknown block size (not from '0' to '9'). @@ -51,6 +52,8 @@ public class BZip2: DecompressionAlgorithm { /// Object with input data which supports convenient work with bit shifts. let pointerData = DataWithPointer(data: data) + let magic = pointerData.intFromBits(count: 16) + guard magic == 0x425a else { throw BZip2Error.WrongMagic } let method = pointerData.intFromBits(count: 8) guard method == 104 else { throw BZip2Error.WrongCompressionMethod }