{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ViewPatterns #-}
module Codec.Archive.Zip
(
Archive (..)
, Entry (..)
, CompressionMethod (..)
, EncryptionMethod (..)
, ZipOption (..)
, ZipException (..)
, emptyArchive
, toArchive
, toArchiveOrFail
, fromArchive
, filesInArchive
, addEntryToArchive
, deleteEntryFromArchive
, findEntryByPath
, fromEntry
, fromEncryptedEntry
, isEncryptedEntry
, toEntry
#ifndef _WINDOWS
, isEntrySymbolicLink
, symbolicLinkEntryTarget
, entryCMode
#endif
, readEntry
, writeEntry
#ifndef _WINDOWS
, writeSymbolicLinkEntry
#endif
, addFilesToArchive
, extractFilesFromArchive
) where
import Data.Time.Calendar ( toGregorian, fromGregorian )
import Data.Time.Clock ( UTCTime(..) )
import Data.Time.Clock.POSIX ( posixSecondsToUTCTime, utcTimeToPOSIXSeconds )
import Data.Time.LocalTime ( TimeOfDay(..), timeToTimeOfDay )
import Data.Bits ( shiftL, shiftR, (.&.), (.|.), xor, testBit )
import Data.Binary
import Data.Binary.Get
import Data.Binary.Put
import Data.List (nub, find, intercalate)
import Data.Data (Data)
import Data.Typeable (Typeable)
import Text.Printf
import System.FilePath
import System.Directory
(doesDirectoryExist, getDirectoryContents,
createDirectoryIfMissing, getModificationTime)
import Control.Monad ( when, unless, zipWithM_ )
import qualified Control.Exception as E
import System.IO ( stderr, hPutStrLn )
import qualified Data.Digest.CRC32 as CRC32
import qualified Data.Map as M
import Control.Applicative
#ifdef _WINDOWS
import Data.Char (isLetter)
#else
import System.Posix.Files ( setFileTimes, setFileMode, fileMode, getSymbolicLinkStatus, symbolicLinkMode, readSymbolicLink, isSymbolicLink, unionFileModes, createSymbolicLink, removeLink )
import System.Posix.Types ( CMode(..) )
import Data.List (partition)
import Data.Maybe (fromJust)
#endif
import GHC.Int (Int64)
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Char8 as C
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import qualified Codec.Compression.Zlib.Raw as Zlib
import System.IO.Error (isAlreadyExistsError)
manySig :: Word32 -> Get a -> Get [a]
manySig :: forall a. Word32 -> Get a -> Get [a]
manySig Word32
sig Get a
p = do
Word32
sig' <- forall a. Get a -> Get a
lookAhead Get Word32
getWord32le
if Word32
sig forall a. Eq a => a -> a -> Bool
== Word32
sig'
then do
a
r <- Get a
p
[a]
rs <- forall a. Word32 -> Get a -> Get [a]
manySig Word32
sig Get a
p
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ a
r forall a. a -> [a] -> [a]
: [a]
rs
else forall (m :: * -> *) a. Monad m => a -> m a
return []
data Archive = Archive
{ Archive -> [Entry]
zEntries :: [Entry]
, Archive -> Maybe ByteString
zSignature :: Maybe B.ByteString
, :: !B.ByteString
} deriving (ReadPrec [Archive]
ReadPrec Archive
Int -> ReadS Archive
ReadS [Archive]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Archive]
$creadListPrec :: ReadPrec [Archive]
readPrec :: ReadPrec Archive
$creadPrec :: ReadPrec Archive
readList :: ReadS [Archive]
$creadList :: ReadS [Archive]
readsPrec :: Int -> ReadS Archive
$creadsPrec :: Int -> ReadS Archive
Read, Int -> Archive -> ShowS
[Archive] -> ShowS
Archive -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [Archive] -> ShowS
$cshowList :: [Archive] -> ShowS
show :: Archive -> FilePath
$cshow :: Archive -> FilePath
showsPrec :: Int -> Archive -> ShowS
$cshowsPrec :: Int -> Archive -> ShowS
Show)
instance Binary Archive where
put :: Archive -> Put
put = Archive -> Put
putArchive
get :: Get Archive
get = Get Archive
getArchive
data Entry = Entry
{ Entry -> FilePath
eRelativePath :: FilePath
, Entry -> CompressionMethod
eCompressionMethod :: !CompressionMethod
, Entry -> EncryptionMethod
eEncryptionMethod :: !EncryptionMethod
, Entry -> Integer
eLastModified :: !Integer
, Entry -> Word32
eCRC32 :: !Word32
, Entry -> Word32
eCompressedSize :: !Word32
, Entry -> Word32
eUncompressedSize :: !Word32
, :: !B.ByteString
, :: !B.ByteString
, Entry -> Word16
eVersionMadeBy :: !Word16
, Entry -> Word16
eInternalFileAttributes :: !Word16
, Entry -> Word32
eExternalFileAttributes :: !Word32
, Entry -> ByteString
eCompressedData :: !B.ByteString
} deriving (ReadPrec [Entry]
ReadPrec Entry
Int -> ReadS Entry
ReadS [Entry]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Entry]
$creadListPrec :: ReadPrec [Entry]
readPrec :: ReadPrec Entry
$creadPrec :: ReadPrec Entry
readList :: ReadS [Entry]
$creadList :: ReadS [Entry]
readsPrec :: Int -> ReadS Entry
$creadsPrec :: Int -> ReadS Entry
Read, Int -> Entry -> ShowS
[Entry] -> ShowS
Entry -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [Entry] -> ShowS
$cshowList :: [Entry] -> ShowS
show :: Entry -> FilePath
$cshow :: Entry -> FilePath
showsPrec :: Int -> Entry -> ShowS
$cshowsPrec :: Int -> Entry -> ShowS
Show, Entry -> Entry -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Entry -> Entry -> Bool
$c/= :: Entry -> Entry -> Bool
== :: Entry -> Entry -> Bool
$c== :: Entry -> Entry -> Bool
Eq)
data CompressionMethod = Deflate
| NoCompression
deriving (ReadPrec [CompressionMethod]
ReadPrec CompressionMethod
Int -> ReadS CompressionMethod
ReadS [CompressionMethod]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CompressionMethod]
$creadListPrec :: ReadPrec [CompressionMethod]
readPrec :: ReadPrec CompressionMethod
$creadPrec :: ReadPrec CompressionMethod
readList :: ReadS [CompressionMethod]
$creadList :: ReadS [CompressionMethod]
readsPrec :: Int -> ReadS CompressionMethod
$creadsPrec :: Int -> ReadS CompressionMethod
Read, Int -> CompressionMethod -> ShowS
[CompressionMethod] -> ShowS
CompressionMethod -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [CompressionMethod] -> ShowS
$cshowList :: [CompressionMethod] -> ShowS
show :: CompressionMethod -> FilePath
$cshow :: CompressionMethod -> FilePath
showsPrec :: Int -> CompressionMethod -> ShowS
$cshowsPrec :: Int -> CompressionMethod -> ShowS
Show, CompressionMethod -> CompressionMethod -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CompressionMethod -> CompressionMethod -> Bool
$c/= :: CompressionMethod -> CompressionMethod -> Bool
== :: CompressionMethod -> CompressionMethod -> Bool
$c== :: CompressionMethod -> CompressionMethod -> Bool
Eq)
data EncryptionMethod = NoEncryption
| PKWAREEncryption !Word8
deriving (ReadPrec [EncryptionMethod]
ReadPrec EncryptionMethod
Int -> ReadS EncryptionMethod
ReadS [EncryptionMethod]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [EncryptionMethod]
$creadListPrec :: ReadPrec [EncryptionMethod]
readPrec :: ReadPrec EncryptionMethod
$creadPrec :: ReadPrec EncryptionMethod
readList :: ReadS [EncryptionMethod]
$creadList :: ReadS [EncryptionMethod]
readsPrec :: Int -> ReadS EncryptionMethod
$creadsPrec :: Int -> ReadS EncryptionMethod
Read, Int -> EncryptionMethod -> ShowS
[EncryptionMethod] -> ShowS
EncryptionMethod -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [EncryptionMethod] -> ShowS
$cshowList :: [EncryptionMethod] -> ShowS
show :: EncryptionMethod -> FilePath
$cshow :: EncryptionMethod -> FilePath
showsPrec :: Int -> EncryptionMethod -> ShowS
$cshowsPrec :: Int -> EncryptionMethod -> ShowS
Show, EncryptionMethod -> EncryptionMethod -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EncryptionMethod -> EncryptionMethod -> Bool
$c/= :: EncryptionMethod -> EncryptionMethod -> Bool
== :: EncryptionMethod -> EncryptionMethod -> Bool
$c== :: EncryptionMethod -> EncryptionMethod -> Bool
Eq)
data PKWAREVerificationType = CheckTimeByte
| CheckCRCByte
deriving (ReadPrec [PKWAREVerificationType]
ReadPrec PKWAREVerificationType
Int -> ReadS PKWAREVerificationType
ReadS [PKWAREVerificationType]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PKWAREVerificationType]
$creadListPrec :: ReadPrec [PKWAREVerificationType]
readPrec :: ReadPrec PKWAREVerificationType
$creadPrec :: ReadPrec PKWAREVerificationType
readList :: ReadS [PKWAREVerificationType]
$creadList :: ReadS [PKWAREVerificationType]
readsPrec :: Int -> ReadS PKWAREVerificationType
$creadsPrec :: Int -> ReadS PKWAREVerificationType
Read, Int -> PKWAREVerificationType -> ShowS
[PKWAREVerificationType] -> ShowS
PKWAREVerificationType -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [PKWAREVerificationType] -> ShowS
$cshowList :: [PKWAREVerificationType] -> ShowS
show :: PKWAREVerificationType -> FilePath
$cshow :: PKWAREVerificationType -> FilePath
showsPrec :: Int -> PKWAREVerificationType -> ShowS
$cshowsPrec :: Int -> PKWAREVerificationType -> ShowS
Show, PKWAREVerificationType -> PKWAREVerificationType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PKWAREVerificationType -> PKWAREVerificationType -> Bool
$c/= :: PKWAREVerificationType -> PKWAREVerificationType -> Bool
== :: PKWAREVerificationType -> PKWAREVerificationType -> Bool
$c== :: PKWAREVerificationType -> PKWAREVerificationType -> Bool
Eq)
data ZipOption = OptRecursive
| OptVerbose
| OptDestination FilePath
| OptLocation FilePath !Bool
| OptPreserveSymbolicLinks
deriving (ReadPrec [ZipOption]
ReadPrec ZipOption
Int -> ReadS ZipOption
ReadS [ZipOption]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ZipOption]
$creadListPrec :: ReadPrec [ZipOption]
readPrec :: ReadPrec ZipOption
$creadPrec :: ReadPrec ZipOption
readList :: ReadS [ZipOption]
$creadList :: ReadS [ZipOption]
readsPrec :: Int -> ReadS ZipOption
$creadsPrec :: Int -> ReadS ZipOption
Read, Int -> ZipOption -> ShowS
[ZipOption] -> ShowS
ZipOption -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [ZipOption] -> ShowS
$cshowList :: [ZipOption] -> ShowS
show :: ZipOption -> FilePath
$cshow :: ZipOption -> FilePath
showsPrec :: Int -> ZipOption -> ShowS
$cshowsPrec :: Int -> ZipOption -> ShowS
Show, ZipOption -> ZipOption -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ZipOption -> ZipOption -> Bool
$c/= :: ZipOption -> ZipOption -> Bool
== :: ZipOption -> ZipOption -> Bool
$c== :: ZipOption -> ZipOption -> Bool
Eq)
data ZipException =
CRC32Mismatch FilePath
| UnsafePath FilePath
| CannotWriteEncryptedEntry FilePath
deriving (Int -> ZipException -> ShowS
[ZipException] -> ShowS
ZipException -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [ZipException] -> ShowS
$cshowList :: [ZipException] -> ShowS
show :: ZipException -> FilePath
$cshow :: ZipException -> FilePath
showsPrec :: Int -> ZipException -> ShowS
$cshowsPrec :: Int -> ZipException -> ShowS
Show, Typeable, Typeable ZipException
ZipException -> DataType
ZipException -> Constr
(forall b. Data b => b -> b) -> ZipException -> ZipException
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> ZipException -> u
forall u. (forall d. Data d => d -> u) -> ZipException -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ZipException -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ZipException -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ZipException -> m ZipException
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ZipException -> m ZipException
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ZipException
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ZipException -> c ZipException
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ZipException)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ZipException)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ZipException -> m ZipException
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ZipException -> m ZipException
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ZipException -> m ZipException
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ZipException -> m ZipException
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ZipException -> m ZipException
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ZipException -> m ZipException
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ZipException -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ZipException -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> ZipException -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> ZipException -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ZipException -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ZipException -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ZipException -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ZipException -> r
gmapT :: (forall b. Data b => b -> b) -> ZipException -> ZipException
$cgmapT :: (forall b. Data b => b -> b) -> ZipException -> ZipException
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ZipException)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ZipException)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ZipException)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ZipException)
dataTypeOf :: ZipException -> DataType
$cdataTypeOf :: ZipException -> DataType
toConstr :: ZipException -> Constr
$ctoConstr :: ZipException -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ZipException
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ZipException
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ZipException -> c ZipException
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ZipException -> c ZipException
Data, ZipException -> ZipException -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ZipException -> ZipException -> Bool
$c/= :: ZipException -> ZipException -> Bool
== :: ZipException -> ZipException -> Bool
$c== :: ZipException -> ZipException -> Bool
Eq)
instance E.Exception ZipException
emptyArchive :: Archive
emptyArchive :: Archive
emptyArchive = Archive
{ zEntries :: [Entry]
zEntries = []
, zSignature :: Maybe ByteString
zSignature = forall a. Maybe a
Nothing
, zComment :: ByteString
zComment = ByteString
B.empty }
toArchive :: B.ByteString -> Archive
toArchive :: ByteString -> Archive
toArchive = forall a. Binary a => ByteString -> a
decode
toArchiveOrFail :: B.ByteString -> Either String Archive
toArchiveOrFail :: ByteString -> Either FilePath Archive
toArchiveOrFail ByteString
bs = case forall a.
Binary a =>
ByteString
-> Either (ByteString, Int64, FilePath) (ByteString, Int64, a)
decodeOrFail ByteString
bs of
Left (ByteString
_,Int64
_,FilePath
e) -> forall a b. a -> Either a b
Left FilePath
e
Right (ByteString
_,Int64
_,Archive
x) -> forall a b. b -> Either a b
Right Archive
x
fromArchive :: Archive -> B.ByteString
fromArchive :: Archive -> ByteString
fromArchive = forall a. Binary a => a -> ByteString
encode
filesInArchive :: Archive -> [FilePath]
filesInArchive :: Archive -> [FilePath]
filesInArchive = forall a b. (a -> b) -> [a] -> [b]
map Entry -> FilePath
eRelativePath forall b c a. (b -> c) -> (a -> b) -> a -> c
. Archive -> [Entry]
zEntries
addEntryToArchive :: Entry -> Archive -> Archive
addEntryToArchive :: Entry -> Archive -> Archive
addEntryToArchive Entry
entry Archive
archive =
let archive' :: Archive
archive' = FilePath -> Archive -> Archive
deleteEntryFromArchive (Entry -> FilePath
eRelativePath Entry
entry) Archive
archive
oldEntries :: [Entry]
oldEntries = Archive -> [Entry]
zEntries Archive
archive'
in Archive
archive' { zEntries :: [Entry]
zEntries = Entry
entry forall a. a -> [a] -> [a]
: [Entry]
oldEntries }
deleteEntryFromArchive :: FilePath -> Archive -> Archive
deleteEntryFromArchive :: FilePath -> Archive -> Archive
deleteEntryFromArchive FilePath
path Archive
archive =
Archive
archive { zEntries :: [Entry]
zEntries = [Entry
e | Entry
e <- Archive -> [Entry]
zEntries Archive
archive
, Bool -> Bool
not (Entry -> FilePath
eRelativePath Entry
e FilePath -> FilePath -> Bool
`matches` FilePath
path)] }
findEntryByPath :: FilePath -> Archive -> Maybe Entry
findEntryByPath :: FilePath -> Archive -> Maybe Entry
findEntryByPath FilePath
path Archive
archive =
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\Entry
e -> FilePath
path FilePath -> FilePath -> Bool
`matches` Entry -> FilePath
eRelativePath Entry
e) (Archive -> [Entry]
zEntries Archive
archive)
fromEntry :: Entry -> B.ByteString
fromEntry :: Entry -> ByteString
fromEntry Entry
entry =
CompressionMethod -> ByteString -> ByteString
decompressData (Entry -> CompressionMethod
eCompressionMethod Entry
entry) (Entry -> ByteString
eCompressedData Entry
entry)
fromEncryptedEntry :: String -> Entry -> Maybe B.ByteString
fromEncryptedEntry :: FilePath -> Entry -> Maybe ByteString
fromEncryptedEntry FilePath
password Entry
entry =
CompressionMethod -> ByteString -> ByteString
decompressData (Entry -> CompressionMethod
eCompressionMethod Entry
entry) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> EncryptionMethod -> ByteString -> Maybe ByteString
decryptData FilePath
password (Entry -> EncryptionMethod
eEncryptionMethod Entry
entry) (Entry -> ByteString
eCompressedData Entry
entry)
isEncryptedEntry :: Entry -> Bool
isEncryptedEntry :: Entry -> Bool
isEncryptedEntry Entry
entry =
case Entry -> EncryptionMethod
eEncryptionMethod Entry
entry of
(PKWAREEncryption Word8
_) -> Bool
True
EncryptionMethod
_ -> Bool
False
toEntry :: FilePath
-> Integer
-> B.ByteString
-> Entry
toEntry :: FilePath -> Integer -> ByteString -> Entry
toEntry FilePath
path Integer
modtime ByteString
contents =
let uncompressedSize :: Int64
uncompressedSize = ByteString -> Int64
B.length ByteString
contents
compressedData :: ByteString
compressedData = CompressionMethod -> ByteString -> ByteString
compressData CompressionMethod
Deflate ByteString
contents
compressedSize :: Int64
compressedSize = ByteString -> Int64
B.length ByteString
compressedData
(CompressionMethod
compressionMethod, ByteString
finalData, Int64
finalSize) =
if Int64
uncompressedSize forall a. Ord a => a -> a -> Bool
<= Int64
compressedSize
then (CompressionMethod
NoCompression, ByteString
contents, Int64
uncompressedSize)
else (CompressionMethod
Deflate, ByteString
compressedData, Int64
compressedSize)
crc32 :: Word32
crc32 = forall a. CRC32 a => a -> Word32
CRC32.crc32 ByteString
contents
in Entry { eRelativePath :: FilePath
eRelativePath = ShowS
normalizePath FilePath
path
, eCompressionMethod :: CompressionMethod
eCompressionMethod = CompressionMethod
compressionMethod
, eEncryptionMethod :: EncryptionMethod
eEncryptionMethod = EncryptionMethod
NoEncryption
, eLastModified :: Integer
eLastModified = Integer
modtime
, eCRC32 :: Word32
eCRC32 = Word32
crc32
, eCompressedSize :: Word32
eCompressedSize = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
finalSize
, eUncompressedSize :: Word32
eUncompressedSize = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
uncompressedSize
, eExtraField :: ByteString
eExtraField = ByteString
B.empty
, eFileComment :: ByteString
eFileComment = ByteString
B.empty
, eVersionMadeBy :: Word16
eVersionMadeBy = Word16
0
, eInternalFileAttributes :: Word16
eInternalFileAttributes = Word16
0
, eExternalFileAttributes :: Word32
eExternalFileAttributes = Word32
0
, eCompressedData :: ByteString
eCompressedData = ByteString
finalData
}
readEntry :: [ZipOption] -> FilePath -> IO Entry
readEntry :: [ZipOption] -> FilePath -> IO Entry
readEntry [ZipOption]
opts FilePath
path = do
Bool
isDir <- FilePath -> IO Bool
doesDirectoryExist FilePath
path
#ifdef _WINDOWS
let isSymLink = False
#else
FileStatus
fs <- FilePath -> IO FileStatus
getSymbolicLinkStatus FilePath
path
let isSymLink :: Bool
isSymLink = FileStatus -> Bool
isSymbolicLink FileStatus
fs
#endif
let path' :: FilePath
path' = let p :: FilePath
p = FilePath
path forall a. [a] -> [a] -> [a]
++ (case forall a. [a] -> [a]
reverse FilePath
path of
(Char
'/':FilePath
_) -> FilePath
""
FilePath
_ | Bool
isDir Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
isSymLink -> FilePath
"/"
FilePath
_ | Bool
isDir Bool -> Bool -> Bool
&& Bool
isSymLink -> FilePath
""
| Bool
otherwise -> FilePath
"") in
(case [(FilePath
l,Bool
a) | OptLocation FilePath
l Bool
a <- [ZipOption]
opts] of
((FilePath
l,Bool
a):[(FilePath, Bool)]
_) -> if Bool
a then FilePath
l FilePath -> ShowS
</> FilePath
p else FilePath
l FilePath -> ShowS
</> ShowS
takeFileName FilePath
p
[(FilePath, Bool)]
_ -> FilePath
p)
ByteString
contents <-
#ifndef _WINDOWS
if Bool
isSymLink
then do
FilePath
linkTarget <- FilePath -> IO FilePath
readSymbolicLink FilePath
path
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ FilePath -> ByteString
C.pack FilePath
linkTarget
else
#endif
if Bool
isDir
then
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
B.empty
else
ByteString -> ByteString
B.fromStrict forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO ByteString
S.readFile FilePath
path
Integer
modEpochTime <- (forall a b. (RealFrac a, Integral b) => a -> b
floor forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> POSIXTime
utcTimeToPOSIXSeconds) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO UTCTime
getModificationTime FilePath
path
let entry :: Entry
entry = FilePath -> Integer -> ByteString -> Entry
toEntry FilePath
path' Integer
modEpochTime ByteString
contents
Entry
entryE <-
#ifdef _WINDOWS
return $ entry { eVersionMadeBy = 0x0000 }
#else
do
let fm :: CMode
fm = if Bool
isSymLink
then CMode -> CMode -> CMode
unionFileModes CMode
symbolicLinkMode (FileStatus -> CMode
fileMode FileStatus
fs)
else FileStatus -> CMode
fileMode FileStatus
fs
let modes :: Word32
modes = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
shiftL (forall a. Integral a => a -> Integer
toInteger CMode
fm) Int
16
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Entry
entry { eExternalFileAttributes :: Word32
eExternalFileAttributes = Word32
modes,
eVersionMadeBy :: Word16
eVersionMadeBy = Word16
0x0300 }
#endif
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ZipOption
OptVerbose forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ZipOption]
opts) forall a b. (a -> b) -> a -> b
$ do
let compmethod :: FilePath
compmethod = case Entry -> CompressionMethod
eCompressionMethod Entry
entryE of
CompressionMethod
Deflate -> (FilePath
"deflated" :: String)
CompressionMethod
NoCompression -> FilePath
"stored"
Handle -> FilePath -> IO ()
hPutStrLn Handle
stderr forall a b. (a -> b) -> a -> b
$
forall r. PrintfType r => FilePath -> r
printf FilePath
" adding: %s (%s %.f%%)" (Entry -> FilePath
eRelativePath Entry
entryE)
FilePath
compmethod (Float
100 forall a. Num a => a -> a -> a
- (Float
100 forall a. Num a => a -> a -> a
* Entry -> Float
compressionRatio Entry
entryE))
forall (m :: * -> *) a. Monad m => a -> m a
return Entry
entryE
checkPath :: FilePath -> IO ()
checkPath :: FilePath -> IO ()
checkPath FilePath
fp =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall e a. Exception e => e -> IO a
E.throwIO (FilePath -> ZipException
UnsafePath FilePath
fp)) (\[FilePath]
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ())
([FilePath] -> Maybe [FilePath]
resolve forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> [FilePath]
splitDirectories forall a b. (a -> b) -> a -> b
$ FilePath
fp)
where
resolve :: [FilePath] -> Maybe [FilePath]
resolve =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall {m :: * -> *} {a}.
(Eq a, IsString a, MonadFail m) =>
m [a] -> a -> m [a]
go (forall (m :: * -> *) a. Monad m => a -> m a
return [])
where
go :: m [a] -> a -> m [a]
go m [a]
acc a
x = do
[a]
xs <- m [a]
acc
case a
x of
a
"." -> forall (m :: * -> *) a. Monad m => a -> m a
return [a]
xs
a
".." -> case [a]
xs of
[] -> forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"outside of root path"
(a
_:[a]
ys) -> forall (m :: * -> *) a. Monad m => a -> m a
return [a]
ys
a
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return (a
xforall a. a -> [a] -> [a]
:[a]
xs)
writeEntry :: [ZipOption] -> Entry -> IO ()
writeEntry :: [ZipOption] -> Entry -> IO ()
writeEntry [ZipOption]
opts Entry
entry = do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Entry -> Bool
isEncryptedEntry Entry
entry) forall a b. (a -> b) -> a -> b
$
forall e a. Exception e => e -> IO a
E.throwIO forall a b. (a -> b) -> a -> b
$ FilePath -> ZipException
CannotWriteEncryptedEntry (Entry -> FilePath
eRelativePath Entry
entry)
let relpath :: FilePath
relpath = Entry -> FilePath
eRelativePath Entry
entry
FilePath -> IO ()
checkPath FilePath
relpath
FilePath
path <- case [FilePath
d | OptDestination FilePath
d <- [ZipOption]
opts] of
(FilePath
x:[FilePath]
_) -> forall (m :: * -> *) a. Monad m => a -> m a
return (FilePath
x FilePath -> ShowS
</> FilePath
relpath)
[] | FilePath -> Bool
isAbsolute FilePath
relpath -> forall e a. Exception e => e -> IO a
E.throwIO forall a b. (a -> b) -> a -> b
$ FilePath -> ZipException
UnsafePath FilePath
relpath
| Bool
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
relpath
let dir :: FilePath
dir = ShowS
takeDirectory FilePath
path
Bool
exists <- FilePath -> IO Bool
doesDirectoryExist FilePath
dir
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
exists forall a b. (a -> b) -> a -> b
$ do
Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True FilePath
dir
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ZipOption
OptVerbose forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ZipOption]
opts) forall a b. (a -> b) -> a -> b
$
Handle -> FilePath -> IO ()
hPutStrLn Handle
stderr forall a b. (a -> b) -> a -> b
$ FilePath
" creating: " forall a. [a] -> [a] -> [a]
++ FilePath
dir
if Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null FilePath
path) Bool -> Bool -> Bool
&& forall a. [a] -> a
last FilePath
path forall a. Eq a => a -> a -> Bool
== Char
'/'
then forall (m :: * -> *) a. Monad m => a -> m a
return ()
else do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ZipOption
OptVerbose forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ZipOption]
opts) forall a b. (a -> b) -> a -> b
$
Handle -> FilePath -> IO ()
hPutStrLn Handle
stderr forall a b. (a -> b) -> a -> b
$ case Entry -> CompressionMethod
eCompressionMethod Entry
entry of
CompressionMethod
Deflate -> FilePath
" inflating: " forall a. [a] -> [a] -> [a]
++ FilePath
path
CompressionMethod
NoCompression -> FilePath
"extracting: " forall a. [a] -> [a] -> [a]
++ FilePath
path
let uncompressedData :: ByteString
uncompressedData = Entry -> ByteString
fromEntry Entry
entry
if Entry -> Word32
eCRC32 Entry
entry forall a. Eq a => a -> a -> Bool
== forall a. CRC32 a => a -> Word32
CRC32.crc32 ByteString
uncompressedData
then FilePath -> ByteString -> IO ()
B.writeFile FilePath
path ByteString
uncompressedData
else forall e a. Exception e => e -> IO a
E.throwIO forall a b. (a -> b) -> a -> b
$ FilePath -> ZipException
CRC32Mismatch FilePath
path
#ifndef _WINDOWS
let modes :: CMode
modes = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
shiftR (Entry -> Word32
eExternalFileAttributes Entry
entry) Int
16
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Entry -> Word16
eVersionMadeBy Entry
entry forall a. Bits a => a -> a -> a
.&. Word16
0xFF00 forall a. Eq a => a -> a -> Bool
== Word16
0x0300 Bool -> Bool -> Bool
&&
CMode
modes forall a. Eq a => a -> a -> Bool
/= CMode
0) forall a b. (a -> b) -> a -> b
$ FilePath -> CMode -> IO ()
setFileMode FilePath
path CMode
modes
#endif
FilePath -> Integer -> IO ()
setFileTimeStamp FilePath
path (Entry -> Integer
eLastModified Entry
entry)
#ifndef _WINDOWS
writeSymbolicLinkEntry :: [ZipOption] -> Entry -> IO ()
writeSymbolicLinkEntry :: [ZipOption] -> Entry -> IO ()
writeSymbolicLinkEntry [ZipOption]
opts Entry
entry =
if ZipOption
OptPreserveSymbolicLinks forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [ZipOption]
opts
then [ZipOption] -> Entry -> IO ()
writeEntry [ZipOption]
opts Entry
entry
else do
if Entry -> Bool
isEntrySymbolicLink Entry
entry
then do
let prefixPath :: FilePath
prefixPath = case [FilePath
d | OptDestination FilePath
d <- [ZipOption]
opts] of
(FilePath
x:[FilePath]
_) -> FilePath
x
[FilePath]
_ -> FilePath
""
let targetPath :: FilePath
targetPath = forall a. HasCallStack => Maybe a -> a
fromJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. Entry -> Maybe FilePath
symbolicLinkEntryTarget forall a b. (a -> b) -> a -> b
$ Entry
entry
let symlinkPath :: FilePath
symlinkPath = FilePath
prefixPath FilePath -> ShowS
</> Entry -> FilePath
eRelativePath Entry
entry
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ZipOption
OptVerbose forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ZipOption]
opts) forall a b. (a -> b) -> a -> b
$ do
Handle -> FilePath -> IO ()
hPutStrLn Handle
stderr forall a b. (a -> b) -> a -> b
$ FilePath
"linking " forall a. [a] -> [a] -> [a]
++ FilePath
symlinkPath forall a. [a] -> [a] -> [a]
++ FilePath
" to " forall a. [a] -> [a] -> [a]
++ FilePath
targetPath
FilePath -> FilePath -> IO ()
forceSymLink FilePath
targetPath FilePath
symlinkPath
else [ZipOption] -> Entry -> IO ()
writeEntry [ZipOption]
opts Entry
entry
forceSymLink :: FilePath -> FilePath -> IO ()
forceSymLink :: FilePath -> FilePath -> IO ()
forceSymLink FilePath
target FilePath
linkName =
FilePath -> FilePath -> IO ()
createSymbolicLink FilePath
target FilePath
linkName forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch`
(\IOError
e -> if IOError -> Bool
isAlreadyExistsError IOError
e
then FilePath -> IO ()
removeLink FilePath
linkName forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> FilePath -> FilePath -> IO ()
createSymbolicLink FilePath
target FilePath
linkName
else forall a. IOError -> IO a
ioError IOError
e)
symbolicLinkEntryTarget :: Entry -> Maybe FilePath
symbolicLinkEntryTarget :: Entry -> Maybe FilePath
symbolicLinkEntryTarget Entry
entry | Entry -> Bool
isEntrySymbolicLink Entry
entry = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> FilePath
C.unpack forall a b. (a -> b) -> a -> b
$ Entry -> ByteString
fromEntry Entry
entry
| Bool
otherwise = forall a. Maybe a
Nothing
isEntrySymbolicLink :: Entry -> Bool
isEntrySymbolicLink :: Entry -> Bool
isEntrySymbolicLink Entry
entry = Entry -> CMode
entryCMode Entry
entry forall a. Bits a => a -> a -> a
.&. CMode
symbolicLinkMode forall a. Eq a => a -> a -> Bool
== CMode
symbolicLinkMode
entryCMode :: Entry -> CMode
entryCMode :: Entry -> CMode
entryCMode Entry
entry = Word32 -> CMode
CMode (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
shiftR (Entry -> Word32
eExternalFileAttributes Entry
entry) Int
16)
#endif
addFilesToArchive :: [ZipOption] -> Archive -> [FilePath] -> IO Archive
addFilesToArchive :: [ZipOption] -> Archive -> [FilePath] -> IO Archive
addFilesToArchive [ZipOption]
opts Archive
archive [FilePath]
files = do
[FilePath]
filesAndChildren <- if ZipOption
OptRecursive forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ZipOption]
opts
#ifdef _WINDOWS
then mapM getDirectoryContentsRecursive files >>= return . nub . concat
#else
then forall a. Eq a => [a] -> [a]
nub forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([ZipOption] -> FilePath -> IO [FilePath]
getDirectoryContentsRecursive' [ZipOption]
opts) [FilePath]
files
#endif
else forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath]
files
[Entry]
entries <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([ZipOption] -> FilePath -> IO Entry
readEntry [ZipOption]
opts) [FilePath]
filesAndChildren
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Entry -> Archive -> Archive
addEntryToArchive Archive
archive [Entry]
entries
extractFilesFromArchive :: [ZipOption] -> Archive -> IO ()
[ZipOption]
opts Archive
archive = do
let entries :: [Entry]
entries = Archive -> [Entry]
zEntries Archive
archive
if ZipOption
OptPreserveSymbolicLinks forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ZipOption]
opts
then do
#ifdef _WINDOWS
mapM_ (writeEntry opts) entries
#else
let ([Entry]
symbolicLinkEntries, [Entry]
nonSymbolicLinkEntries) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition Entry -> Bool
isEntrySymbolicLink [Entry]
entries
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ([ZipOption] -> Entry -> IO ()
writeEntry [ZipOption]
opts) [Entry]
nonSymbolicLinkEntries
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ([ZipOption] -> Entry -> IO ()
writeSymbolicLinkEntry [ZipOption]
opts) [Entry]
symbolicLinkEntries
#endif
else forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ([ZipOption] -> Entry -> IO ()
writeEntry [ZipOption]
opts) [Entry]
entries
normalizePath :: FilePath -> String
normalizePath :: ShowS
normalizePath FilePath
path =
let dir :: FilePath
dir = ShowS
takeDirectory FilePath
path
fn :: FilePath
fn = ShowS
takeFileName FilePath
path
dir' :: FilePath
dir' = case FilePath
dir of
#ifdef _WINDOWS
(c:':':d:xs) | isLetter c
, d == '/' || d == '\\'
-> xs
#endif
FilePath
_ -> FilePath
dir
dirParts :: [FilePath]
dirParts = forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
/=FilePath
".") forall a b. (a -> b) -> a -> b
$ FilePath -> [FilePath]
splitDirectories FilePath
dir'
in forall a. [a] -> [[a]] -> [a]
intercalate FilePath
"/" ([FilePath]
dirParts forall a. [a] -> [a] -> [a]
++ [FilePath
fn])
matches :: FilePath -> FilePath -> Bool
matches :: FilePath -> FilePath -> Bool
matches FilePath
fp1 FilePath
fp2 = ShowS
normalizePath FilePath
fp1 forall a. Eq a => a -> a -> Bool
== ShowS
normalizePath FilePath
fp2
compressData :: CompressionMethod -> B.ByteString -> B.ByteString
compressData :: CompressionMethod -> ByteString -> ByteString
compressData CompressionMethod
Deflate = ByteString -> ByteString
Zlib.compress
compressData CompressionMethod
NoCompression = forall a. a -> a
id
decompressData :: CompressionMethod -> B.ByteString -> B.ByteString
decompressData :: CompressionMethod -> ByteString -> ByteString
decompressData CompressionMethod
Deflate = ByteString -> ByteString
Zlib.decompress
decompressData CompressionMethod
NoCompression = forall a. a -> a
id
decryptData :: String -> EncryptionMethod -> B.ByteString -> Maybe B.ByteString
decryptData :: FilePath -> EncryptionMethod -> ByteString -> Maybe ByteString
decryptData FilePath
_ EncryptionMethod
NoEncryption ByteString
s = forall a. a -> Maybe a
Just ByteString
s
decryptData FilePath
password (PKWAREEncryption Word8
controlByte) ByteString
s =
let headerlen :: Int64
headerlen = Int64
12
initKeys :: (Word32, Word32, Word32)
initKeys = (Word32
305419896, Word32
591751049, Word32
878082192)
startKeys :: (Word32, Word32, Word32)
startKeys = forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B.foldl (Word32, Word32, Word32) -> Word8 -> (Word32, Word32, Word32)
pkwareUpdateKeys (Word32, Word32, Word32)
initKeys (FilePath -> ByteString
C.pack FilePath
password)
(ByteString
header, ByteString
content) = Int64 -> ByteString -> (ByteString, ByteString)
B.splitAt Int64
headerlen forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall acc.
(acc -> Word8 -> (acc, Word8))
-> acc -> ByteString -> (acc, ByteString)
B.mapAccumL (Word32, Word32, Word32)
-> Word8 -> ((Word32, Word32, Word32), Word8)
pkwareDecryptByte (Word32, Word32, Word32)
startKeys ByteString
s
in if HasCallStack => ByteString -> Word8
B.last ByteString
header forall a. Eq a => a -> a -> Bool
== Word8
controlByte
then forall a. a -> Maybe a
Just ByteString
content
else forall a. Maybe a
Nothing
type DecryptionCtx = (Word32, Word32, Word32)
pkwareDecryptByte :: DecryptionCtx -> Word8 -> (DecryptionCtx, Word8)
pkwareDecryptByte :: (Word32, Word32, Word32)
-> Word8 -> ((Word32, Word32, Word32), Word8)
pkwareDecryptByte keys :: (Word32, Word32, Word32)
keys@(Word32
_, Word32
_, Word32
key2) Word8
inB =
let tmp :: Word32
tmp = Word32
key2 forall a. Bits a => a -> a -> a
.|. Word32
2
tmp' :: Word8
tmp' = forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word32
tmp forall a. Num a => a -> a -> a
* (Word32
tmp forall a. Bits a => a -> a -> a
`xor` Word32
1)) forall a. Bits a => a -> Int -> a
`shiftR` Int
8) :: Word8
outB :: Word8
outB = Word8
inB forall a. Bits a => a -> a -> a
`xor` Word8
tmp'
in ((Word32, Word32, Word32) -> Word8 -> (Word32, Word32, Word32)
pkwareUpdateKeys (Word32, Word32, Word32)
keys Word8
outB, Word8
outB)
pkwareUpdateKeys :: DecryptionCtx -> Word8 -> DecryptionCtx
pkwareUpdateKeys :: (Word32, Word32, Word32) -> Word8 -> (Word32, Word32, Word32)
pkwareUpdateKeys (Word32
key0, Word32
key1, Word32
key2) Word8
inB =
let key0' :: Word32
key0' = forall a. CRC32 a => Word32 -> a -> Word32
CRC32.crc32Update (Word32
key0 forall a. Bits a => a -> a -> a
`xor` Word32
0xffffffff) [Word8
inB] forall a. Bits a => a -> a -> a
`xor` Word32
0xffffffff
key1' :: Word32
key1' = (Word32
key1 forall a. Num a => a -> a -> a
+ (Word32
key0' forall a. Bits a => a -> a -> a
.&. Word32
0xff)) forall a. Num a => a -> a -> a
* Word32
134775813 forall a. Num a => a -> a -> a
+ Word32
1
key1Byte :: Word8
key1Byte = forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
key1' forall a. Bits a => a -> Int -> a
`shiftR` Int
24) :: Word8
key2' :: Word32
key2' = forall a. CRC32 a => Word32 -> a -> Word32
CRC32.crc32Update (Word32
key2 forall a. Bits a => a -> a -> a
`xor` Word32
0xffffffff) [Word8
key1Byte] forall a. Bits a => a -> a -> a
`xor` Word32
0xffffffff
in (Word32
key0', Word32
key1', Word32
key2')
compressionRatio :: Entry -> Float
compressionRatio :: Entry -> Float
compressionRatio Entry
entry =
if Entry -> Word32
eUncompressedSize Entry
entry forall a. Eq a => a -> a -> Bool
== Word32
0
then Float
1
else forall a b. (Integral a, Num b) => a -> b
fromIntegral (Entry -> Word32
eCompressedSize Entry
entry) forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral (Entry -> Word32
eUncompressedSize Entry
entry)
data MSDOSDateTime = MSDOSDateTime { MSDOSDateTime -> Word16
msDOSDate :: Word16
, MSDOSDateTime -> Word16
msDOSTime :: Word16
} deriving (ReadPrec [MSDOSDateTime]
ReadPrec MSDOSDateTime
Int -> ReadS MSDOSDateTime
ReadS [MSDOSDateTime]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [MSDOSDateTime]
$creadListPrec :: ReadPrec [MSDOSDateTime]
readPrec :: ReadPrec MSDOSDateTime
$creadPrec :: ReadPrec MSDOSDateTime
readList :: ReadS [MSDOSDateTime]
$creadList :: ReadS [MSDOSDateTime]
readsPrec :: Int -> ReadS MSDOSDateTime
$creadsPrec :: Int -> ReadS MSDOSDateTime
Read, Int -> MSDOSDateTime -> ShowS
[MSDOSDateTime] -> ShowS
MSDOSDateTime -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [MSDOSDateTime] -> ShowS
$cshowList :: [MSDOSDateTime] -> ShowS
show :: MSDOSDateTime -> FilePath
$cshow :: MSDOSDateTime -> FilePath
showsPrec :: Int -> MSDOSDateTime -> ShowS
$cshowsPrec :: Int -> MSDOSDateTime -> ShowS
Show, MSDOSDateTime -> MSDOSDateTime -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MSDOSDateTime -> MSDOSDateTime -> Bool
$c/= :: MSDOSDateTime -> MSDOSDateTime -> Bool
== :: MSDOSDateTime -> MSDOSDateTime -> Bool
$c== :: MSDOSDateTime -> MSDOSDateTime -> Bool
Eq)
minMSDOSDateTime :: Integer
minMSDOSDateTime :: Integer
minMSDOSDateTime = Integer
315532800
epochTimeToMSDOSDateTime :: Integer -> MSDOSDateTime
epochTimeToMSDOSDateTime :: Integer -> MSDOSDateTime
epochTimeToMSDOSDateTime Integer
epochtime | Integer
epochtime forall a. Ord a => a -> a -> Bool
< Integer
minMSDOSDateTime =
Integer -> MSDOSDateTime
epochTimeToMSDOSDateTime Integer
minMSDOSDateTime
epochTimeToMSDOSDateTime Integer
epochtime =
let
UTCTime
(Day -> (Integer, Int, Int)
toGregorian -> (forall a. Num a => Integer -> a
fromInteger -> Int
year, Int
month, Int
day))
(DiffTime -> TimeOfDay
timeToTimeOfDay -> (TimeOfDay Int
hour Int
minutes (forall a b. (RealFrac a, Integral b) => a -> b
floor -> Int
sec)))
= POSIXTime -> UTCTime
posixSecondsToUTCTime (forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
epochtime)
dosTime :: Word16
dosTime = forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ (Int
sec forall a. Integral a => a -> a -> a
`div` Int
2) forall a. Num a => a -> a -> a
+ forall a. Bits a => a -> Int -> a
shiftL Int
minutes Int
5 forall a. Num a => a -> a -> a
+ forall a. Bits a => a -> Int -> a
shiftL Int
hour Int
11
dosDate :: Word16
dosDate = forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ Int
day forall a. Num a => a -> a -> a
+ forall a. Bits a => a -> Int -> a
shiftL Int
month Int
5 forall a. Num a => a -> a -> a
+ forall a. Bits a => a -> Int -> a
shiftL (Int
year forall a. Num a => a -> a -> a
- Int
1980) Int
9
in MSDOSDateTime { msDOSDate :: Word16
msDOSDate = Word16
dosDate, msDOSTime :: Word16
msDOSTime = Word16
dosTime }
msDOSDateTimeToEpochTime :: MSDOSDateTime -> Integer
msDOSDateTimeToEpochTime :: MSDOSDateTime -> Integer
msDOSDateTimeToEpochTime MSDOSDateTime {msDOSDate :: MSDOSDateTime -> Word16
msDOSDate = Word16
dosDate, msDOSTime :: MSDOSDateTime -> Word16
msDOSTime = Word16
dosTime} =
let seconds :: DiffTime
seconds = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Word16
2 forall a. Num a => a -> a -> a
* (Word16
dosTime forall a. Bits a => a -> a -> a
.&. Word16
0O37)
minutes :: DiffTime
minutes = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
shiftR Word16
dosTime Int
5 forall a. Bits a => a -> a -> a
.&. Word16
0O77
hour :: DiffTime
hour = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
shiftR Word16
dosTime Int
11
day :: Int
day = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Word16
dosDate forall a. Bits a => a -> a -> a
.&. Word16
0O37
month :: Int
month = forall a b. (Integral a, Num b) => a -> b
fromIntegral ((forall a. Bits a => a -> Int -> a
shiftR Word16
dosDate Int
5) forall a. Bits a => a -> a -> a
.&. Word16
0O17)
year :: Integer
year = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
shiftR Word16
dosDate Int
9
utc :: UTCTime
utc = Day -> DiffTime -> UTCTime
UTCTime (Integer -> Int -> Int -> Day
fromGregorian (Integer
1980 forall a. Num a => a -> a -> a
+ Integer
year) Int
month Int
day) (DiffTime
3600 forall a. Num a => a -> a -> a
* DiffTime
hour forall a. Num a => a -> a -> a
+ DiffTime
60 forall a. Num a => a -> a -> a
* DiffTime
minutes forall a. Num a => a -> a -> a
+ DiffTime
seconds)
in forall a b. (RealFrac a, Integral b) => a -> b
floor (UTCTime -> POSIXTime
utcTimeToPOSIXSeconds UTCTime
utc)
#ifndef _WINDOWS
getDirectoryContentsRecursive' :: [ZipOption] -> FilePath -> IO [FilePath]
getDirectoryContentsRecursive' :: [ZipOption] -> FilePath -> IO [FilePath]
getDirectoryContentsRecursive' [ZipOption]
opts FilePath
path =
if ZipOption
OptPreserveSymbolicLinks forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ZipOption]
opts
then do
Bool
isDir <- FilePath -> IO Bool
doesDirectoryExist FilePath
path
if Bool
isDir
then do
Bool
isSymLink <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FileStatus -> Bool
isSymbolicLink forall a b. (a -> b) -> a -> b
$ FilePath -> IO FileStatus
getSymbolicLinkStatus FilePath
path
if Bool
isSymLink
then forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath
path]
else (FilePath -> IO [FilePath]) -> FilePath -> IO [FilePath]
getDirectoryContentsRecursivelyBy ([ZipOption] -> FilePath -> IO [FilePath]
getDirectoryContentsRecursive' [ZipOption]
opts) FilePath
path
else forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath
path]
else FilePath -> IO [FilePath]
getDirectoryContentsRecursive FilePath
path
#endif
getDirectoryContentsRecursive :: FilePath -> IO [FilePath]
getDirectoryContentsRecursive :: FilePath -> IO [FilePath]
getDirectoryContentsRecursive FilePath
path = do
Bool
isDir <- FilePath -> IO Bool
doesDirectoryExist FilePath
path
if Bool
isDir
then (FilePath -> IO [FilePath]) -> FilePath -> IO [FilePath]
getDirectoryContentsRecursivelyBy FilePath -> IO [FilePath]
getDirectoryContentsRecursive FilePath
path
else forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath
path]
getDirectoryContentsRecursivelyBy :: (FilePath -> IO [FilePath]) -> FilePath -> IO [FilePath]
getDirectoryContentsRecursivelyBy :: (FilePath -> IO [FilePath]) -> FilePath -> IO [FilePath]
getDirectoryContentsRecursivelyBy FilePath -> IO [FilePath]
exploreMethod FilePath
path = do
[FilePath]
contents <- FilePath -> IO [FilePath]
getDirectoryContents FilePath
path
let contents' :: [FilePath]
contents' = forall a b. (a -> b) -> [a] -> [b]
map (FilePath
path FilePath -> ShowS
</>) forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [FilePath
"..",FilePath
"."]) [FilePath]
contents
[[FilePath]]
children <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM FilePath -> IO [FilePath]
exploreMethod [FilePath]
contents'
if FilePath
path forall a. Eq a => a -> a -> Bool
== FilePath
"."
then forall (m :: * -> *) a. Monad m => a -> m a
return (forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[FilePath]]
children)
else forall (m :: * -> *) a. Monad m => a -> m a
return (FilePath
path forall a. a -> [a] -> [a]
: forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[FilePath]]
children)
setFileTimeStamp :: FilePath -> Integer -> IO ()
#ifdef _WINDOWS
setFileTimeStamp _ _ = return ()
#else
setFileTimeStamp :: FilePath -> Integer -> IO ()
setFileTimeStamp FilePath
file Integer
epochtime = do
let epochtime' :: EpochTime
epochtime' = forall a. Num a => Integer -> a
fromInteger Integer
epochtime
FilePath -> EpochTime -> EpochTime -> IO ()
setFileTimes FilePath
file EpochTime
epochtime' EpochTime
epochtime'
#endif
getArchive :: Get Archive
getArchive :: Get Archive
getArchive = do
[(Word32, ByteString)]
locals <- forall a. Word32 -> Get a -> Get [a]
manySig Word32
0x04034b50 Get (Word32, ByteString)
getLocalFile
[Entry]
files <- forall a. Word32 -> Get a -> Get [a]
manySig Word32
0x02014b50 (Map Word32 ByteString -> Get Entry
getFileHeader (forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Word32, ByteString)]
locals))
Maybe ByteString
digSig <- forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get ByteString
getDigitalSignature forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Word32
endSig <- Get Word32
getWord32le
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word32
endSig forall a. Eq a => a -> a -> Bool
== Word32
0x06054b50)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"Did not find end of central directory signature"
Int -> Get ()
skip Int
2
Int -> Get ()
skip Int
2
Int -> Get ()
skip Int
2
Int -> Get ()
skip Int
2
Int -> Get ()
skip Int
4
Int -> Get ()
skip Int
4
Word16
commentLength <- Get Word16
getWord16le
ByteString
zipComment <- Int64 -> Get ByteString
getLazyByteString (forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum Word16
commentLength)
forall (m :: * -> *) a. Monad m => a -> m a
return Archive
{ zEntries :: [Entry]
zEntries = [Entry]
files
, zSignature :: Maybe ByteString
zSignature = Maybe ByteString
digSig
, zComment :: ByteString
zComment = ByteString
zipComment
}
putArchive :: Archive -> Put
putArchive :: Archive -> Put
putArchive Archive
archive = do
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Entry -> Put
putLocalFile forall a b. (a -> b) -> a -> b
$ Archive -> [Entry]
zEntries Archive
archive
let localFileSizes :: [Word32]
localFileSizes = forall a b. (a -> b) -> [a] -> [b]
map Entry -> Word32
localFileSize forall a b. (a -> b) -> a -> b
$ Archive -> [Entry]
zEntries Archive
archive
let offsets :: [Word32]
offsets = forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl forall a. Num a => a -> a -> a
(+) Word32
0 [Word32]
localFileSizes
let cdOffset :: Word32
cdOffset = forall a. [a] -> a
last [Word32]
offsets
()
_ <- forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ Word32 -> Entry -> Put
putFileHeader [Word32]
offsets (Archive -> [Entry]
zEntries Archive
archive)
Maybe ByteString -> Put
putDigitalSignature forall a b. (a -> b) -> a -> b
$ Archive -> Maybe ByteString
zSignature Archive
archive
Word32 -> Put
putWord32le Word32
0x06054b50
Word16 -> Put
putWord16le Word16
0
Word16 -> Put
putWord16le Word16
0
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ Archive -> [Entry]
zEntries Archive
archive
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ Archive -> [Entry]
zEntries Archive
archive
Word32 -> Put
putWord32le forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Entry -> Word32
fileHeaderSize forall a b. (a -> b) -> a -> b
$ Archive -> [Entry]
zEntries Archive
archive
Word32 -> Put
putWord32le forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
cdOffset
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ByteString -> Int64
B.length forall a b. (a -> b) -> a -> b
$ Archive -> ByteString
zComment Archive
archive
ByteString -> Put
putLazyByteString forall a b. (a -> b) -> a -> b
$ Archive -> ByteString
zComment Archive
archive
fileHeaderSize :: Entry -> Word32
Entry
f =
forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Int64
4 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
4 forall a. Num a => a -> a -> a
+ Int64
4 forall a. Num a => a -> a -> a
+ Int64
4 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
4 forall a. Num a => a -> a -> a
+ Int64
4 forall a. Num a => a -> a -> a
+
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
B.length forall a b. (a -> b) -> a -> b
$ FilePath -> ByteString
fromString forall a b. (a -> b) -> a -> b
$ ShowS
normalizePath forall a b. (a -> b) -> a -> b
$ Entry -> FilePath
eRelativePath Entry
f) forall a. Num a => a -> a -> a
+
ByteString -> Int64
B.length (Entry -> ByteString
eExtraField Entry
f) forall a. Num a => a -> a -> a
+ ByteString -> Int64
B.length (Entry -> ByteString
eFileComment Entry
f)
localFileSize :: Entry -> Word32
localFileSize :: Entry -> Word32
localFileSize Entry
f =
forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Int64
4 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
4 forall a. Num a => a -> a -> a
+ Int64
4 forall a. Num a => a -> a -> a
+ Int64
4 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+ Int64
2 forall a. Num a => a -> a -> a
+
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
B.length forall a b. (a -> b) -> a -> b
$ FilePath -> ByteString
fromString forall a b. (a -> b) -> a -> b
$ ShowS
normalizePath forall a b. (a -> b) -> a -> b
$ Entry -> FilePath
eRelativePath Entry
f) forall a. Num a => a -> a -> a
+
ByteString -> Int64
B.length (Entry -> ByteString
eExtraField Entry
f) forall a. Num a => a -> a -> a
+ ByteString -> Int64
B.length (Entry -> ByteString
eCompressedData Entry
f)
getLocalFile :: Get (Word32, B.ByteString)
getLocalFile :: Get (Word32, ByteString)
getLocalFile = do
Int64
offset <- Get Int64
bytesRead
Get Word32
getWord32le forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. (a -> Bool) -> a -> Get ()
ensure (forall a. Eq a => a -> a -> Bool
== Word32
0x04034b50)
Int -> Get ()
skip Int
2
Word16
bitflag <- Get Word16
getWord16le
Int -> Get ()
skip Int
2
Int -> Get ()
skip Int
2
Int -> Get ()
skip Int
2
Int -> Get ()
skip Int
4
Word32
compressedSize <- Get Word32
getWord32le
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word32
compressedSize forall a. Eq a => a -> a -> Bool
== Word32
0xFFFFFFFF) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"Can't read ZIP64 archive."
Int -> Get ()
skip Int
4
Word16
fileNameLength <- Get Word16
getWord16le
Word16
extraFieldLength <- Get Word16
getWord16le
Int -> Get ()
skip (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
fileNameLength)
Int -> Get ()
skip (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
extraFieldLength)
ByteString
compressedData <- if Word16
bitflag forall a. Bits a => a -> a -> a
.&. Word16
0O10 forall a. Eq a => a -> a -> Bool
== Word16
0
then Int64 -> Get ByteString
getLazyByteString (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
compressedSize)
else
do ByteString
raw <- Get ByteString
getCompressedData
Word32
sig <- forall a. Get a -> Get a
lookAhead Get Word32
getWord32le
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word32
sig forall a. Eq a => a -> a -> Bool
== Word32
0x08074b50) forall a b. (a -> b) -> a -> b
$ Int -> Get ()
skip Int
4
Int -> Get ()
skip Int
4
Word32
cs <- Get Word32
getWord32le
Int -> Get ()
skip Int
4
if forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
cs forall a. Eq a => a -> a -> Bool
== ByteString -> Int64
B.length ByteString
raw
then forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
raw
else forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"Content size mismatch in data descriptor record"
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
offset, ByteString
compressedData)
getCompressedData :: Get B.ByteString
getCompressedData :: Get ByteString
getCompressedData = do
Int64
numbytes <- forall a. Get a -> Get a
lookAhead forall a b. (a -> b) -> a -> b
$ Int64 -> Get Int64
findEnd Int64
0
Int64 -> Get ByteString
getLazyByteString Int64
numbytes
where
chunkSize :: Int64
chunkSize :: Int64
chunkSize = Int64
16384
findEnd :: Int64 -> Get Int64
findEnd :: Int64 -> Get Int64
findEnd Int64
n = do
Word32
sig <- forall a. Get a -> Get a
lookAhead Get Word32
getWord32le
case Word32
sig of
Word32
0x08074b50 -> Int -> Get ()
skip Int
4 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return Int64
n
Word32
0x04034b50 ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Int64
n forall a. Num a => a -> a -> a
- Int64
12)
Word32
0x02014b50 ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Int64
n forall a. Num a => a -> a -> a
- Int64
12)
Word32
0x06054b50 ->
forall (m :: * -> *) a. Monad m => a -> m a
return (Int64
n forall a. Num a => a -> a -> a
- Int64
12)
Word32
x | Word32
x forall a. Bits a => a -> a -> a
.&. Word32
0xFF forall a. Eq a => a -> a -> Bool
== Word32
0x50 -> Int -> Get ()
skip Int
1 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> Get Int64
findEnd (Int64
n forall a. Num a => a -> a -> a
+ Int64
1)
Word32
_ -> do ByteString
bs <- forall a. Get a -> Get a
lookAhead forall a b. (a -> b) -> a -> b
$ Int64 -> Get ByteString
getLazyByteString Int64
chunkSize
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Get ByteString
getRemainingLazyByteString
let bsLen :: Int64
bsLen = ByteString -> Int64
B.length ByteString
bs
let mbIdx :: Maybe Int64
mbIdx = Word8 -> ByteString -> Maybe Int64
B.elemIndex Word8
0x50 ByteString
bs
case Maybe Int64
mbIdx of
Maybe Int64
Nothing -> Int -> Get ()
skip (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
bsLen) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> Get Int64
findEnd (Int64
n forall a. Num a => a -> a -> a
+ Int64
bsLen)
Just Int64
0 -> Int -> Get ()
skip Int
1 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> Get Int64
findEnd (Int64
n forall a. Num a => a -> a -> a
+ Int64
1)
Just Int64
idx -> Int -> Get ()
skip (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
idx) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> Get Int64
findEnd (Int64
n forall a. Num a => a -> a -> a
+ Int64
idx)
putLocalFile :: Entry -> Put
putLocalFile :: Entry -> Put
putLocalFile Entry
f = do
Word32 -> Put
putWord32le Word32
0x04034b50
Word16 -> Put
putWord16le Word16
20
Word16 -> Put
putWord16le Word16
0x802
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ case Entry -> CompressionMethod
eCompressionMethod Entry
f of
CompressionMethod
NoCompression -> Word16
0
CompressionMethod
Deflate -> Word16
8
let modTime :: MSDOSDateTime
modTime = Integer -> MSDOSDateTime
epochTimeToMSDOSDateTime forall a b. (a -> b) -> a -> b
$ Entry -> Integer
eLastModified Entry
f
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ MSDOSDateTime -> Word16
msDOSTime MSDOSDateTime
modTime
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ MSDOSDateTime -> Word16
msDOSDate MSDOSDateTime
modTime
Word32 -> Put
putWord32le forall a b. (a -> b) -> a -> b
$ Entry -> Word32
eCRC32 Entry
f
Word32 -> Put
putWord32le forall a b. (a -> b) -> a -> b
$ Entry -> Word32
eCompressedSize Entry
f
Word32 -> Put
putWord32le forall a b. (a -> b) -> a -> b
$ Entry -> Word32
eUncompressedSize Entry
f
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ByteString -> Int64
B.length forall a b. (a -> b) -> a -> b
$ FilePath -> ByteString
fromString
forall a b. (a -> b) -> a -> b
$ ShowS
normalizePath forall a b. (a -> b) -> a -> b
$ Entry -> FilePath
eRelativePath Entry
f
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ByteString -> Int64
B.length forall a b. (a -> b) -> a -> b
$ Entry -> ByteString
eExtraField Entry
f
ByteString -> Put
putLazyByteString forall a b. (a -> b) -> a -> b
$ FilePath -> ByteString
fromString forall a b. (a -> b) -> a -> b
$ ShowS
normalizePath forall a b. (a -> b) -> a -> b
$ Entry -> FilePath
eRelativePath Entry
f
ByteString -> Put
putLazyByteString forall a b. (a -> b) -> a -> b
$ Entry -> ByteString
eExtraField Entry
f
ByteString -> Put
putLazyByteString forall a b. (a -> b) -> a -> b
$ Entry -> ByteString
eCompressedData Entry
f
getFileHeader :: M.Map Word32 B.ByteString
-> Get Entry
Map Word32 ByteString
locals = do
Get Word32
getWord32le forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. (a -> Bool) -> a -> Get ()
ensure (forall a. Eq a => a -> a -> Bool
== Word32
0x02014b50)
Word16
vmb <- Get Word16
getWord16le
Word8
versionNeededToExtract <- Get Word8
getWord8
Int -> Get ()
skip Int
1
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word8
versionNeededToExtract forall a. Ord a => a -> a -> Bool
<= Word8
20) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"This archive requires zip >= 2.0 to extract."
Word16
bitflag <- Get Word16
getWord16le
Word16
rawCompressionMethod <- Get Word16
getWord16le
CompressionMethod
compressionMethod <- case Word16
rawCompressionMethod of
Word16
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return CompressionMethod
NoCompression
Word16
8 -> forall (m :: * -> *) a. Monad m => a -> m a
return CompressionMethod
Deflate
Word16
_ -> forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail forall a b. (a -> b) -> a -> b
$ FilePath
"Unknown compression method " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> FilePath
show Word16
rawCompressionMethod
Word16
lastModFileTime <- Get Word16
getWord16le
Word16
lastModFileDate <- Get Word16
getWord16le
Word32
crc32 <- Get Word32
getWord32le
EncryptionMethod
encryptionMethod <- case (forall a. Bits a => a -> Int -> Bool
testBit Word16
bitflag Int
0, forall a. Bits a => a -> Int -> Bool
testBit Word16
bitflag Int
3, forall a. Bits a => a -> Int -> Bool
testBit Word16
bitflag Int
6) of
(Bool
False, Bool
_, Bool
_) -> forall (m :: * -> *) a. Monad m => a -> m a
return EncryptionMethod
NoEncryption
(Bool
True, Bool
False, Bool
False) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Word8 -> EncryptionMethod
PKWAREEncryption (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
crc32 forall a. Bits a => a -> Int -> a
`shiftR` Int
24))
(Bool
True, Bool
True, Bool
False) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Word8 -> EncryptionMethod
PKWAREEncryption (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16
lastModFileTime forall a. Bits a => a -> Int -> a
`shiftR` Int
8))
(Bool
True, Bool
_, Bool
True) -> forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"Strong encryption is not supported"
Word32
compressedSize <- Get Word32
getWord32le
Word32
uncompressedSize <- Get Word32
getWord32le
Word16
fileNameLength <- Get Word16
getWord16le
Word16
extraFieldLength <- Get Word16
getWord16le
Word16
fileCommentLength <- Get Word16
getWord16le
Int -> Get ()
skip Int
2
Word16
internalFileAttributes <- Get Word16
getWord16le
Word32
externalFileAttributes <- Get Word32
getWord32le
Word32
relativeOffset <- Get Word32
getWord32le
ByteString
fileName <- Int64 -> Get ByteString
getLazyByteString (forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum Word16
fileNameLength)
ByteString
extraField <- Int64 -> Get ByteString
getLazyByteString (forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum Word16
extraFieldLength)
ByteString
fileComment <- Int64 -> Get ByteString
getLazyByteString (forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum Word16
fileCommentLength)
ByteString
compressedData <- case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Word32
relativeOffset Map Word32 ByteString
locals of
Just ByteString
x -> forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
x
Maybe ByteString
Nothing -> forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail forall a b. (a -> b) -> a -> b
$ FilePath
"Unable to find data at offset " forall a. [a] -> [a] -> [a]
++
forall a. Show a => a -> FilePath
show Word32
relativeOffset
forall (m :: * -> *) a. Monad m => a -> m a
return Entry
{ eRelativePath :: FilePath
eRelativePath = ByteString -> FilePath
toString ByteString
fileName
, eCompressionMethod :: CompressionMethod
eCompressionMethod = CompressionMethod
compressionMethod
, eEncryptionMethod :: EncryptionMethod
eEncryptionMethod = EncryptionMethod
encryptionMethod
, eLastModified :: Integer
eLastModified = MSDOSDateTime -> Integer
msDOSDateTimeToEpochTime forall a b. (a -> b) -> a -> b
$
MSDOSDateTime { msDOSDate :: Word16
msDOSDate = Word16
lastModFileDate,
msDOSTime :: Word16
msDOSTime = Word16
lastModFileTime }
, eCRC32 :: Word32
eCRC32 = Word32
crc32
, eCompressedSize :: Word32
eCompressedSize = Word32
compressedSize
, eUncompressedSize :: Word32
eUncompressedSize = Word32
uncompressedSize
, eExtraField :: ByteString
eExtraField = ByteString
extraField
, eFileComment :: ByteString
eFileComment = ByteString
fileComment
, eVersionMadeBy :: Word16
eVersionMadeBy = Word16
vmb
, eInternalFileAttributes :: Word16
eInternalFileAttributes = Word16
internalFileAttributes
, eExternalFileAttributes :: Word32
eExternalFileAttributes = Word32
externalFileAttributes
, eCompressedData :: ByteString
eCompressedData = ByteString
compressedData
}
putFileHeader :: Word32
-> Entry
-> Put
Word32
offset Entry
local = do
Word32 -> Put
putWord32le Word32
0x02014b50
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ Entry -> Word16
eVersionMadeBy Entry
local
Word16 -> Put
putWord16le Word16
20
Word16 -> Put
putWord16le Word16
0x802
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ case Entry -> CompressionMethod
eCompressionMethod Entry
local of
CompressionMethod
NoCompression -> Word16
0
CompressionMethod
Deflate -> Word16
8
let modTime :: MSDOSDateTime
modTime = Integer -> MSDOSDateTime
epochTimeToMSDOSDateTime forall a b. (a -> b) -> a -> b
$ Entry -> Integer
eLastModified Entry
local
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ MSDOSDateTime -> Word16
msDOSTime MSDOSDateTime
modTime
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ MSDOSDateTime -> Word16
msDOSDate MSDOSDateTime
modTime
Word32 -> Put
putWord32le forall a b. (a -> b) -> a -> b
$ Entry -> Word32
eCRC32 Entry
local
Word32 -> Put
putWord32le forall a b. (a -> b) -> a -> b
$ Entry -> Word32
eCompressedSize Entry
local
Word32 -> Put
putWord32le forall a b. (a -> b) -> a -> b
$ Entry -> Word32
eUncompressedSize Entry
local
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ByteString -> Int64
B.length forall a b. (a -> b) -> a -> b
$ FilePath -> ByteString
fromString
forall a b. (a -> b) -> a -> b
$ ShowS
normalizePath forall a b. (a -> b) -> a -> b
$ Entry -> FilePath
eRelativePath Entry
local
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ByteString -> Int64
B.length forall a b. (a -> b) -> a -> b
$ Entry -> ByteString
eExtraField Entry
local
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ByteString -> Int64
B.length forall a b. (a -> b) -> a -> b
$ Entry -> ByteString
eFileComment Entry
local
Word16 -> Put
putWord16le Word16
0
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ Entry -> Word16
eInternalFileAttributes Entry
local
Word32 -> Put
putWord32le forall a b. (a -> b) -> a -> b
$ Entry -> Word32
eExternalFileAttributes Entry
local
Word32 -> Put
putWord32le Word32
offset
ByteString -> Put
putLazyByteString forall a b. (a -> b) -> a -> b
$ FilePath -> ByteString
fromString forall a b. (a -> b) -> a -> b
$ ShowS
normalizePath forall a b. (a -> b) -> a -> b
$ Entry -> FilePath
eRelativePath Entry
local
ByteString -> Put
putLazyByteString forall a b. (a -> b) -> a -> b
$ Entry -> ByteString
eExtraField Entry
local
ByteString -> Put
putLazyByteString forall a b. (a -> b) -> a -> b
$ Entry -> ByteString
eFileComment Entry
local
getDigitalSignature :: Get B.ByteString
getDigitalSignature :: Get ByteString
getDigitalSignature = do
Get Word32
getWord32le forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. (a -> Bool) -> a -> Get ()
ensure (forall a. Eq a => a -> a -> Bool
== Word32
0x05054b50)
Word16
sigSize <- Get Word16
getWord16le
Int64 -> Get ByteString
getLazyByteString (forall a. Enum a => Int -> a
toEnum forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum Word16
sigSize)
putDigitalSignature :: Maybe B.ByteString -> Put
putDigitalSignature :: Maybe ByteString -> Put
putDigitalSignature Maybe ByteString
Nothing = forall (m :: * -> *) a. Monad m => a -> m a
return ()
putDigitalSignature (Just ByteString
sig) = do
Word32 -> Put
putWord32le Word32
0x05054b50
Word16 -> Put
putWord16le forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ByteString -> Int64
B.length ByteString
sig
ByteString -> Put
putLazyByteString ByteString
sig
ensure :: (a -> Bool) -> a -> Get ()
ensure :: forall a. (a -> Bool) -> a -> Get ()
ensure a -> Bool
p a
val =
if a -> Bool
p a
val
then forall (m :: * -> *) a. Monad m => a -> m a
return ()
else forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail FilePath
"ensure not satisfied"
toString :: B.ByteString -> String
toString :: ByteString -> FilePath
toString = Text -> FilePath
TL.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
TL.decodeUtf8
fromString :: String -> B.ByteString
fromString :: FilePath -> ByteString
fromString = Text -> ByteString
TL.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Text
TL.pack