-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

-- | Bounded in-memory Core Metadata extraction from verified Python distributions.
module Ecluse.Core.Registry.PyPI.Archive (archiveMetadata) where

import Conduit
import Control.Monad (foldM)
import Control.Monad.Except (throwError)
import Data.Bits (shiftR, (.&.))
import Data.ByteString qualified as BS
import Data.ByteString.Builder (toLazyByteString, word64LE)
import Data.ByteString.Lazy qualified as BSL
import Data.Digest.CRC32 (crc32Update)
import Data.Set qualified as Set
import Data.Text qualified as T
import Data.Text.Encoding qualified as TE
import UnliftIO.Exception (evaluate)

import Ecluse.Core.Package (PackageName)
import Ecluse.Core.Registry (ParseError (..), parseRefusal)
import Ecluse.Core.Registry.Archive.Inflate (InflateFormat (..), withInflatedChunks)
import Ecluse.Core.Registry.Archive.Path (checkPath)
import Ecluse.Core.Registry.Archive.Tar (sourceReader)
import Ecluse.Core.Registry.PyPI.Archive.BZip (withBzipChunks)
import Ecluse.Core.Registry.PyPI.Archive.Lzma (LzmaFormat (..), withLzmaChunks)
import Ecluse.Core.Registry.PyPI.Archive.Path (metadataPath)
import Ecluse.Core.Registry.PyPI.Archive.Tar (extractTar)
import Ecluse.Core.Registry.PyPI.Project (DistributionKind (..), FileCoordinate (..), fileCoordinate)
import Ecluse.Core.Security.Limits (Limits (..))
import Ecluse.Core.Server.Admission.Meter (newChargePair)
import Ecluse.Core.Version (Version)

-- | Request absolute extra workspace before retention. The caller separately charges the verified source bytes.
archiveMetadata :: Limits -> (Int -> IO ()) -> PackageName -> Version -> Text -> ByteString -> IO (Either ParseError ByteString)
archiveMetadata :: Limits
-> (Int -> IO ())
-> PackageName
-> Version
-> Text
-> ByteString
-> IO (Either ParseError ByteString)
archiveMetadata Limits
limits Int -> IO ()
charge PackageName
package Version
version Text
filename ByteString
bytes = case PackageName -> Text -> Maybe FileCoordinate
fileCoordinate PackageName
package Text
filename of
    Just FileCoordinate
coordinate
        | FileCoordinate -> Version
fcVersion FileCoordinate
coordinate Version -> Version -> Bool
forall a. Eq a => a -> a -> Bool
== Version
version
        , ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Limits -> Int
maxMirrorArtifactBytes Limits
limits
        , Limits -> Int
maxMirrorArtifactBytes Limits
limits Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 -> do
            (decoderCharge, contentCharge) <- (Int -> IO ()) -> IO (Int -> IO (), Int -> IO ())
newChargePair Int -> IO ()
charge
            contentCharge archiveScratchBytes
            result <-
                if any (`T.isSuffixOf` filename) [".whl", ".zip"]
                    then extractZip limits decoderCharge contentCharge package version (fcKind coordinate) bytes
                    else case () of
                        ()
_
                            | (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Text -> Text -> Bool
`T.isSuffixOf` Text
filename) [Text
".tar.gz", Text
".tgz"] ->
                                Limits
-> (Int -> IO ())
-> PackageName
-> Version
-> (forall a.
    (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
    -> IO a)
-> IO (Either ParseError ByteString)
extractTar Limits
limits Int -> IO ()
contentCharge PackageName
package Version
version ((forall a.
  (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
  -> IO a)
 -> IO (Either ParseError ByteString))
-> (forall a.
    (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
    -> IO a)
-> IO (Either ParseError ByteString)
forall a b. (a -> b) -> a -> b
$ \ConduitT () ByteString (ExceptT ParseError IO) () -> IO a
consume ->
                                    InflateFormat
-> (Int -> IO ())
-> ByteString
-> (IO (Either ParseError ByteString) -> IO a)
-> IO a
forall a.
InflateFormat
-> (Int -> IO ())
-> ByteString
-> (IO (Either ParseError ByteString) -> IO a)
-> IO a
withInflatedChunks (Int -> InflateFormat
GzipMembers (Limits -> Int
compressedMemberCeiling Limits
limits)) Int -> IO ()
decoderCharge ByteString
bytes (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a
consume (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
-> (IO (Either ParseError ByteString)
    -> ConduitT () ByteString (ExceptT ParseError IO) ())
-> IO (Either ParseError ByteString)
-> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (Either ParseError ByteString)
-> ConduitT () ByteString (ExceptT ParseError IO) ()
sourceReader)
                            | Text
".tar.bz2" Text -> Text -> Bool
`T.isSuffixOf` Text
filename ->
                                Limits
-> (Int -> IO ())
-> PackageName
-> Version
-> (forall a.
    (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
    -> IO a)
-> IO (Either ParseError ByteString)
extractTar Limits
limits Int -> IO ()
contentCharge PackageName
package Version
version (Int
-> (Int -> IO ())
-> ByteString
-> (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
-> IO a
forall a.
Int
-> (Int -> IO ())
-> ByteString
-> (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
-> IO a
withBzipChunks (Limits -> Int
compressedMemberCeiling Limits
limits) Int -> IO ()
decoderCharge ByteString
bytes)
                            | Text
".tar.xz" Text -> Text -> Bool
`T.isSuffixOf` Text
filename ->
                                Limits
-> (Int -> IO ())
-> PackageName
-> Version
-> (forall a.
    (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
    -> IO a)
-> IO (Either ParseError ByteString)
extractTar Limits
limits Int -> IO ()
contentCharge PackageName
package Version
version (LzmaFormat
-> (Int -> IO ())
-> [ByteString]
-> (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
-> IO a
forall a.
LzmaFormat
-> (Int -> IO ())
-> [ByteString]
-> (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
-> IO a
withLzmaChunks LzmaFormat
XzArchive Int -> IO ()
decoderCharge [ByteString
bytes])
                            | Bool
otherwise -> Either ParseError ByteString -> IO (Either ParseError ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Either ParseError ByteString
forall a. Text -> Either ParseError a
parseRefusal Text
"unsupported distribution archive")
            traverse (\ByteString
selected -> ByteString
selected ByteString -> IO Int -> IO ByteString
forall a b. a -> IO b -> IO a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Int -> IO Int
forall (m :: * -> *) a. MonadIO m => a -> m a
evaluate (ByteString -> Int
BS.length ByteString
selected)) result
    Maybe FileCoordinate
_ -> Either ParseError ByteString -> IO (Either ParseError ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParseError -> Either ParseError ByteString
forall a b. a -> Either a b
Left (Text -> ParseError
ParseError Text
"distribution filename disagrees with admitted identity"))

archiveScratchBytes :: Int
archiveScratchBytes :: Int
archiveScratchBytes = Int
65536

data Scan = Scan
    { Scan -> Int
scanCount :: !Int
    , Scan -> Int
scanPathBytes :: !Int
    , Scan -> Integer
scanInflated :: !Integer
    , Scan -> Set Text
scanNames :: Set Text
    , Scan -> Maybe ByteString
scanMetadata :: Maybe ByteString
    }

emptyScan :: Scan
emptyScan :: Scan
emptyScan = Int -> Int -> Integer -> Set Text -> Maybe ByteString -> Scan
Scan Int
0 Int
0 Integer
0 Set Text
forall a. Set a
Set.empty Maybe ByteString
forall a. Maybe a
Nothing

entryPathCost :: Limits -> Text -> Scan -> Either ParseError Int
entryPathCost :: Limits -> Text -> Scan -> Either ParseError Int
entryPathCost Limits
limits Text
path Scan
scan = do
    Either ParseError [Text] -> Either ParseError ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Text -> Either ParseError [Text]
checkPath Text
path)
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Scan -> Int
scanCount Scan
scan Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Limits -> Int
archiveEntryCeiling Limits
limits) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"archive entry count exceeds its bound")
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member Text
path (Scan -> Set Text
scanNames Scan
scan)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"duplicate archive entry")
    let charge :: Int
charge = ByteString -> Int
BS.length (Text -> ByteString
TE.encodeUtf8 Text
path) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
128
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
charge Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Limits -> Int
maxMetadataBytes Limits
limits Int -> Int -> Int
forall a. Num a => a -> a -> a
- Scan -> Int
scanPathBytes Scan
scan) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"archive path inventory exceeds its bound")
    Int -> Either ParseError Int
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
charge

addInflated :: Limits -> Integer -> Scan -> Either ParseError Scan
addInflated :: Limits -> Integer -> Scan -> Either ParseError Scan
addInflated Limits
limits Integer
size Scan
scan
    | Integer
size Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 Bool -> Bool -> Bool
|| Scan -> Integer
scanInflated Scan
scan Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
size Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Limits -> Int
maxMirrorArtifactBytes Limits
limits) = Text -> Either ParseError Scan
forall a. Text -> Either ParseError a
parseRefusal Text
"archive inflated bytes exceed their bound"
    | Bool
otherwise = Scan -> Either ParseError Scan
forall a b. b -> Either a b
Right Scan
scan{scanInflated = scanInflated scan + size}

keepMetadata :: Limits -> ByteString -> Scan -> Either ParseError Scan
keepMetadata :: Limits -> ByteString -> Scan -> Either ParseError Scan
keepMetadata Limits
limits ByteString
bytes Scan
scan
    | ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Limits -> Int
maxMetadataBytes Limits
limits = Text -> Either ParseError Scan
forall a. Text -> Either ParseError a
parseRefusal Text
"archive Core Metadata exceeds its bound"
    | Maybe ByteString -> Bool
forall a. Maybe a -> Bool
isJust (Scan -> Maybe ByteString
scanMetadata Scan
scan) = Text -> Either ParseError Scan
forall a. Text -> Either ParseError a
parseRefusal Text
"ambiguous archive Core Metadata"
    | Bool
otherwise = Scan -> Either ParseError Scan
forall a b. b -> Either a b
Right Scan
scan{scanMetadata = Just (BS.copy bytes)}

finishScan :: Scan -> Either ParseError ByteString
finishScan :: Scan -> Either ParseError ByteString
finishScan Scan
scan = Either ParseError ByteString
-> (ByteString -> Either ParseError ByteString)
-> Maybe ByteString
-> Either ParseError ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Either ParseError ByteString
forall a. Text -> Either ParseError a
parseRefusal Text
"archive has no Core Metadata") ByteString -> Either ParseError ByteString
forall a b. b -> Either a b
Right (Scan -> Maybe ByteString
scanMetadata Scan
scan)

data ZipMember = ZipMember
    { ZipMember -> Text
zmName :: Text
    , ZipMember -> Integer
zmStart :: Integer
    , ZipMember -> Integer
zmEnd :: Integer
    , ZipMember -> Integer
zmDataOffset :: Integer
    , ZipMember -> Integer
zmCompressed :: Integer
    , ZipMember -> Integer
zmInflated :: Integer
    , ZipMember -> Word32
zmCrc :: Word32
    , ZipMember -> Integer
zmMethod :: Integer
    , ZipMember -> Bool
zmLzmaEndMarker :: Bool
    }

extractZip :: Limits -> (Int -> IO ()) -> (Int -> IO ()) -> PackageName -> Version -> DistributionKind -> ByteString -> IO (Either ParseError ByteString)
extractZip :: Limits
-> (Int -> IO ())
-> (Int -> IO ())
-> PackageName
-> Version
-> DistributionKind
-> ByteString
-> IO (Either ParseError ByteString)
extractZip Limits
limits Int -> IO ()
decoderCharge Int -> IO ()
charge PackageName
package Version
version DistributionKind
kind ByteString
bytes = do
    directory <- Limits
-> (Int -> IO ())
-> ByteString
-> IO (Either ParseError ([ZipMember], Int))
zipDirectory Limits
limits Int -> IO ()
charge ByteString
bytes
    case directory of
        Left ParseError
fault -> Either ParseError ByteString -> IO (Either ParseError ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParseError -> Either ParseError ByteString
forall a b. a -> Either a b
Left ParseError
fault)
        Right ([ZipMember]
members, Int
directoryCharge) -> Int -> Scan -> [ZipMember] -> IO (Either ParseError ByteString)
go Int
directoryCharge Scan
emptyScan [ZipMember]
members
  where
    go :: Int -> Scan -> [ZipMember] -> IO (Either ParseError ByteString)
go Int
_ Scan
scan [] = Either ParseError ByteString -> IO (Either ParseError ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Scan -> Either ParseError ByteString
finishScan Scan
scan)
    go Int
directoryCharge Scan
scan (ZipMember
member : [ZipMember]
rest) = case Scan -> ZipMember -> Either ParseError (Int, Bool)
prepareMember Scan
scan ZipMember
member of
        Left ParseError
fault -> Either ParseError ByteString -> IO (Either ParseError ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParseError -> Either ParseError ByteString
forall a b. a -> Either a b
Left ParseError
fault)
        Right (Int
pathCost, Bool
selected) -> do
            Int -> IO ()
charge (Int -> Scan -> Int -> Int -> Int
workspace Int
directoryCharge Scan
scan Int
0 Int
0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
pathCost)
            let next :: Scan
next = Scan
scan{scanCount = scanCount scan + 1, scanPathBytes = scanPathBytes scan + pathCost, scanNames = Set.insert (zmName member) (scanNames scan)}
                compressed :: ByteString
compressed = Int -> ByteString -> ByteString
BS.take (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (ZipMember -> Integer
zmCompressed ZipMember
member)) (Int -> ByteString -> ByteString
BS.drop (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (ZipMember -> Integer
zmDataOffset ZipMember
member)) ByteString
bytes)
                consume :: ConduitT () ByteString (ExceptT ParseError m) ()
-> m (Either ParseError Scan)
consume ConduitT () ByteString (ExceptT ParseError m) ()
source = ExceptT ParseError m Scan -> m (Either ParseError Scan)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT ParseError m Scan -> m (Either ParseError Scan))
-> (ConduitT () Void (ExceptT ParseError m) Scan
    -> ExceptT ParseError m Scan)
-> ConduitT () Void (ExceptT ParseError m) Scan
-> m (Either ParseError Scan)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConduitT () Void (ExceptT ParseError m) Scan
-> ExceptT ParseError m Scan
forall (m :: * -> *) r. Monad m => ConduitT () Void m r -> m r
runConduit (ConduitT () Void (ExceptT ParseError m) Scan
 -> m (Either ParseError Scan))
-> ConduitT () Void (ExceptT ParseError m) Scan
-> m (Either ParseError Scan)
forall a b. (a -> b) -> a -> b
$ ConduitT () ByteString (ExceptT ParseError m) ()
source ConduitT () ByteString (ExceptT ParseError m) ()
-> ConduitT ByteString Void (ExceptT ParseError m) Scan
-> ConduitT () Void (ExceptT ParseError m) Scan
forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| Int
-> Scan
-> ZipMember
-> Bool
-> Int
-> Word32
-> [ByteString]
-> Int
-> ConduitT ByteString Void (ExceptT ParseError m) Scan
forall {m :: * -> *} {o}.
(MonadError ParseError m, MonadIO m) =>
Int
-> Scan
-> ZipMember
-> Bool
-> Int
-> Word32
-> [ByteString]
-> Int
-> ConduitT ByteString o m Scan
memberBytes Int
directoryCharge Scan
next ZipMember
member Bool
selected Int
0 Word32
0 [] Int
0
            result <- case ZipMember -> Integer
zmMethod ZipMember
member of
                Integer
0 -> ConduitT () ByteString (ExceptT ParseError IO) ()
-> IO (Either ParseError Scan)
forall {m :: * -> *}.
MonadIO m =>
ConduitT () ByteString (ExceptT ParseError m) ()
-> m (Either ParseError Scan)
consume (ByteString -> ConduitT () ByteString (ExceptT ParseError IO) ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ByteString
compressed)
                Integer
8 -> InflateFormat
-> (Int -> IO ())
-> ByteString
-> (IO (Either ParseError ByteString)
    -> IO (Either ParseError Scan))
-> IO (Either ParseError Scan)
forall a.
InflateFormat
-> (Int -> IO ())
-> ByteString
-> (IO (Either ParseError ByteString) -> IO a)
-> IO a
withInflatedChunks InflateFormat
RawDeflate Int -> IO ()
decoderCharge ByteString
compressed (ConduitT () ByteString (ExceptT ParseError IO) ()
-> IO (Either ParseError Scan)
forall {m :: * -> *}.
MonadIO m =>
ConduitT () ByteString (ExceptT ParseError m) ()
-> m (Either ParseError Scan)
consume (ConduitT () ByteString (ExceptT ParseError IO) ()
 -> IO (Either ParseError Scan))
-> (IO (Either ParseError ByteString)
    -> ConduitT () ByteString (ExceptT ParseError IO) ())
-> IO (Either ParseError ByteString)
-> IO (Either ParseError Scan)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (Either ParseError ByteString)
-> ConduitT () ByteString (ExceptT ParseError IO) ()
sourceReader)
                Integer
12 -> Int
-> (Int -> IO ())
-> ByteString
-> (ConduitT () ByteString (ExceptT ParseError IO) ()
    -> IO (Either ParseError Scan))
-> IO (Either ParseError Scan)
forall a.
Int
-> (Int -> IO ())
-> ByteString
-> (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
-> IO a
withBzipChunks Int
1 Int -> IO ()
decoderCharge ByteString
compressed ConduitT () ByteString (ExceptT ParseError IO) ()
-> IO (Either ParseError Scan)
forall {m :: * -> *}.
MonadIO m =>
ConduitT () ByteString (ExceptT ParseError m) ()
-> m (Either ParseError Scan)
consume
                Integer
14 -> case ZipMember -> ByteString -> Either ParseError [ByteString]
zipLzmaParts ZipMember
member ByteString
compressed of
                    Left ParseError
fault -> Either ParseError Scan -> IO (Either ParseError Scan)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParseError -> Either ParseError Scan
forall a b. a -> Either a b
Left ParseError
fault)
                    Right [ByteString]
parts -> LzmaFormat
-> (Int -> IO ())
-> [ByteString]
-> (ConduitT () ByteString (ExceptT ParseError IO) ()
    -> IO (Either ParseError Scan))
-> IO (Either ParseError Scan)
forall a.
LzmaFormat
-> (Int -> IO ())
-> [ByteString]
-> (ConduitT () ByteString (ExceptT ParseError IO) () -> IO a)
-> IO a
withLzmaChunks LzmaFormat
ZipLzmaMember Int -> IO ()
decoderCharge [ByteString]
parts ConduitT () ByteString (ExceptT ParseError IO) ()
-> IO (Either ParseError Scan)
forall {m :: * -> *}.
MonadIO m =>
ConduitT () ByteString (ExceptT ParseError m) ()
-> m (Either ParseError Scan)
consume
                Integer
_ -> Either ParseError Scan -> IO (Either ParseError Scan)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Either ParseError Scan
forall a. Text -> Either ParseError a
parseRefusal Text
"unsupported ZIP compression method")
            decoderCharge 0
            case result of
                Left ParseError
fault -> Either ParseError ByteString -> IO (Either ParseError ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParseError -> Either ParseError ByteString
forall a b. a -> Either a b
Left ParseError
fault)
                Right Scan
retained -> Int -> Scan -> [ZipMember] -> IO (Either ParseError ByteString)
go Int
directoryCharge Scan
retained [ZipMember]
rest
    prepareMember :: Scan -> ZipMember -> Either ParseError (Int, Bool)
prepareMember Scan
scan ZipMember
member = do
        pathCost <- Limits -> Text -> Scan -> Either ParseError Int
entryPathCost Limits
limits (ZipMember -> Text
zmName ZipMember
member) Scan
scan
        selected <- checkPath (zmName member) >>= metadataPath package version kind
        void (addInflated limits (zmInflated member) scan)
        pure (pathCost, selected)
    memberBytes :: Int
-> Scan
-> ZipMember
-> Bool
-> Int
-> Word32
-> [ByteString]
-> Int
-> ConduitT ByteString o m Scan
memberBytes Int
directoryCharge Scan
scan ZipMember
member Bool
selected Int
seen Word32
crc [ByteString]
chunks Int
chunkCount =
        ConduitT ByteString o m (Maybe ByteString)
forall (m :: * -> *) i o. Monad m => ConduitT i o m (Maybe i)
await ConduitT ByteString o m (Maybe ByteString)
-> (Maybe ByteString -> ConduitT ByteString o m Scan)
-> ConduitT ByteString o m Scan
forall a b.
ConduitT ByteString o m a
-> (a -> ConduitT ByteString o m b) -> ConduitT ByteString o m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Maybe ByteString
Nothing -> do
                Bool -> ConduitT ByteString o m () -> ConduitT ByteString o m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
seen Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== ZipMember -> Integer
zmInflated ZipMember
member Bool -> Bool -> Bool
&& Word32
crc Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== ZipMember -> Word32
zmCrc ZipMember
member) (m () -> ConduitT ByteString o m ()
forall (m :: * -> *) a. Monad m => m a -> ConduitT ByteString o m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ParseError -> m ()
forall a. ParseError -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> ParseError
ParseError Text
"ZIP entry integrity check failed")))
                charged <- (ParseError -> ConduitT ByteString o m Scan)
-> (Scan -> ConduitT ByteString o m Scan)
-> Either ParseError Scan
-> ConduitT ByteString o m Scan
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (m Scan -> ConduitT ByteString o m Scan
forall (m :: * -> *) a. Monad m => m a -> ConduitT ByteString o m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Scan -> ConduitT ByteString o m Scan)
-> (ParseError -> m Scan)
-> ParseError
-> ConduitT ByteString o m Scan
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> m Scan
forall a. ParseError -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError) Scan -> ConduitT ByteString o m Scan
forall a. a -> ConduitT ByteString o m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Limits -> Integer -> Scan -> Either ParseError Scan
addInflated Limits
limits (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
seen) Scan
scan)
                if selected then either (lift . throwError) pure (keepMetadata limits (BS.concat (reverse chunks)) charged) else pure charged
            Just ByteString
chunk -> do
                let next :: Int
next = Int
seen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
chunk
                Bool -> ConduitT ByteString o m () -> ConduitT ByteString o m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
next Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> ZipMember -> Integer
zmInflated ZipMember
member Bool -> Bool -> Bool
|| Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
next Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Scan -> Integer
scanInflated Scan
scan Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Limits -> Int
maxMirrorArtifactBytes Limits
limits)) (m () -> ConduitT ByteString o m ()
forall (m :: * -> *) a. Monad m => m a -> ConduitT ByteString o m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ParseError -> m ()
forall a. ParseError -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> ParseError
ParseError Text
"ZIP inflated bytes exceed their bound")))
                Bool -> ConduitT ByteString o m () -> ConduitT ByteString o m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
selected Bool -> Bool -> Bool
&& Int
next Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Limits -> Int
maxMetadataBytes Limits
limits) (m () -> ConduitT ByteString o m ()
forall (m :: * -> *) a. Monad m => m a -> ConduitT ByteString o m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ParseError -> m ()
forall a. ParseError -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> ParseError
ParseError Text
"archive Core Metadata exceeds its bound")))
                Bool -> ConduitT ByteString o m () -> ConduitT ByteString o m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
selected (IO () -> ConduitT ByteString o m ()
forall a. IO a -> ConduitT ByteString o m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int -> IO ()
charge (Int -> Scan -> Int -> Int -> Int
workspace Int
directoryCharge Scan
scan Int
next (Int
chunkCount Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))))
                Int
-> Scan
-> ZipMember
-> Bool
-> Int
-> Word32
-> [ByteString]
-> Int
-> ConduitT ByteString o m Scan
memberBytes Int
directoryCharge Scan
scan ZipMember
member Bool
selected Int
next (Word32 -> ByteString -> Word32
forall a. CRC32 a => Word32 -> a -> Word32
crc32Update Word32
crc ByteString
chunk) (if Bool
selected then ByteString
chunk ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
chunks else []) (Int
chunkCount Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
    workspace :: Int -> Scan -> Int -> Int -> Int
workspace Int
directoryCharge Scan
scan Int
held Int
chunks = Int
archiveScratchBytes Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
directoryCharge Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Scan -> Int
scanPathBytes Scan
scan Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
held Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
64 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
chunks Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> (ByteString -> Int) -> Maybe ByteString -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 ByteString -> Int
BS.length (Scan -> Maybe ByteString
scanMetadata Scan
scan)

zipLzmaParts :: ZipMember -> ByteString -> Either ParseError [ByteString]
zipLzmaParts :: ZipMember -> ByteString -> Either ParseError [ByteString]
zipLzmaParts ZipMember
member ByteString
bytes
    | ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
9 Bool -> Bool -> Bool
|| Int -> Int -> ByteString -> Integer
little Int
2 Int
2 ByteString
bytes Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
5 = Text -> Either ParseError [ByteString]
forall a. Text -> Either ParseError a
parseRefusal Text
"malformed ZIP LZMA properties"
    | Bool
otherwise = [ByteString] -> Either ParseError [ByteString]
forall a b. b -> Either a b
Right [Int -> ByteString -> ByteString
BS.take Int
5 (Int -> ByteString -> ByteString
BS.drop Int
4 ByteString
bytes), LazyByteString -> ByteString
BSL.toStrict (Builder -> LazyByteString
toLazyByteString (Word64 -> Builder
word64LE (if ZipMember -> Bool
zmLzmaEndMarker ZipMember
member then Word64
forall a. Bounded a => a
maxBound else Integer -> Word64
forall a. Num a => Integer -> a
fromInteger (ZipMember -> Integer
zmInflated ZipMember
member)))), Int -> ByteString -> ByteString
BS.drop Int
9 ByteString
bytes]

zipDirectory :: Limits -> (Int -> IO ()) -> ByteString -> IO (Either ParseError ([ZipMember], Int))
zipDirectory :: Limits
-> (Int -> IO ())
-> ByteString
-> IO (Either ParseError ([ZipMember], Int))
zipDirectory Limits
limits Int -> IO ()
charge ByteString
bytes = ExceptT ParseError IO ([ZipMember], Int)
-> IO (Either ParseError ([ZipMember], Int))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT ParseError IO ([ZipMember], Int)
 -> IO (Either ParseError ([ZipMember], Int)))
-> ExceptT ParseError IO ([ZipMember], Int)
-> IO (Either ParseError ([ZipMember], Int))
forall a b. (a -> b) -> a -> b
$ do
    end <- IO (Either ParseError Int) -> ExceptT ParseError IO Int
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Either ParseError Int -> IO (Either ParseError Int)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Either ParseError Int
findZipEnd ByteString
bytes))
    (directoryEnd, count, size, offset) <- ExceptT (pure (directoryBounds bytes end))
    when (count > toInteger (archiveEntryCeiling limits) || size > toInteger (maxMetadataBytes limits)) (throwError (ParseError "ZIP directory exceeds its bound"))
    unless (offset + size == directoryEnd) (throwError (ParseError "ZIP directory offset is inconsistent"))
    entries <- readDirectory charge 0 bytes (fromInteger count) (BS.take (fromInteger size) (BS.drop (fromInteger offset) bytes))
    let ordered = (ZipMember -> Integer) -> [ZipMember] -> [ZipMember]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn ZipMember -> Integer
zmStart [ZipMember]
entries
    finalOffset <- ExceptT (pure (foldM advance 0 ordered))
    unless (finalOffset == offset) (throwError (ParseError "ZIP local records do not fill the declared file area"))
    let weight = [Int] -> Int
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int
512 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
* ByteString -> Int
BS.length (Text -> ByteString
TE.encodeUtf8 (ZipMember -> Text
zmName ZipMember
member)) | ZipMember
member <- [ZipMember]
ordered]
    liftIO (charge (archiveScratchBytes + weight))
    pure (ordered, weight)
  where
    advance :: Integer -> ZipMember -> Either ParseError Integer
advance Integer
expected ZipMember
member
        | ZipMember -> Integer
zmStart ZipMember
member Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
expected Bool -> Bool -> Bool
&& ZipMember -> Integer
zmEnd ZipMember
member Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> ZipMember -> Integer
zmStart ZipMember
member = Integer -> Either ParseError Integer
forall a b. b -> Either a b
Right (ZipMember -> Integer
zmEnd ZipMember
member)
        | Bool
otherwise = Text -> Either ParseError Integer
forall a. Text -> Either ParseError a
parseRefusal Text
"ZIP central offset does not identify the actual next local entry"

directoryBounds :: ByteString -> Int -> Either ParseError (Integer, Integer, Integer, Integer)
directoryBounds :: ByteString
-> Int -> Either ParseError (Integer, Integer, Integer, Integer)
directoryBounds ByteString
archive Int
end = do
    let footer :: ByteString
footer = Int -> ByteString -> ByteString
BS.drop Int
end ByteString
archive
        count :: Integer
count = Int -> Int -> ByteString -> Integer
little Int
10 Int
2 ByteString
footer
        size :: Integer
size = Int -> Int -> ByteString -> Integer
little Int
12 Int
4 ByteString
footer
        offset :: Integer
offset = Int -> Int -> ByteString -> Integer
little Int
16 Int
4 ByteString
footer
        locator :: ByteString
locator = Int -> ByteString -> ByteString
BS.take Int
20 (Int -> ByteString -> ByteString
BS.drop (Int
end Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
20) ByteString
archive)
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int -> Int -> ByteString -> Integer
little Int
4 Int
2 ByteString
footer Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 Bool -> Bool -> Bool
&& Int -> Int -> ByteString -> Integer
little Int
6 Int
2 ByteString
footer Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 Bool -> Bool -> Bool
&& Int -> Int -> ByteString -> Integer
little Int
8 Int
2 ByteString
footer Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
count) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"multi-disk ZIP is unsupported")
    if Int
end Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
20 Bool -> Bool -> Bool
&& ByteString -> ByteString -> Bool
BS.isPrefixOf ByteString
"PK\x06\x07" ByteString
locator
        then do
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int -> Int -> ByteString -> Integer
little Int
4 Int
4 ByteString
locator Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 Bool -> Bool -> Bool
&& Int -> Int -> ByteString -> Integer
little Int
16 Int
4 ByteString
locator Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"multi-disk ZIP64 is unsupported")
            let position :: Integer
position = Int -> Int -> ByteString -> Integer
little Int
8 Int
8 ByteString
locator
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
position Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
end) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"ZIP64 end offset is outside its archive")
            let record :: ByteString
record = Int -> ByteString -> ByteString
BS.drop (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
position) ByteString
archive
                recordSize :: Integer
recordSize = Int -> Int -> ByteString -> Integer
little Int
4 Int
8 ByteString
record
                count64 :: Integer
count64 = Int -> Int -> ByteString -> Integer
little Int
32 Int
8 ByteString
record
                size64 :: Integer
size64 = Int -> Int -> ByteString -> Integer
little Int
40 Int
8 ByteString
record
                offset64 :: Integer
offset64 = Int -> Int -> ByteString -> Integer
little Int
48 Int
8 ByteString
record
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Int
BS.length ByteString
record Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
56 Bool -> Bool -> Bool
&& ByteString -> ByteString -> Bool
BS.isPrefixOf ByteString
"PK\x06\x06" ByteString
record Bool -> Bool -> Bool
&& Integer
recordSize Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
44 Bool -> Bool -> Bool
&& Integer
recordSize Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
16384 Bool -> Bool -> Bool
&& Integer
position Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
12 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
recordSize Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int
end Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
20)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"malformed ZIP64 end record")
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int -> Int -> ByteString -> Integer
little Int
16 Int
4 ByteString
record Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 Bool -> Bool -> Bool
&& Int -> Int -> ByteString -> Integer
little Int
20 Int
4 ByteString
record Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 Bool -> Bool -> Bool
&& Int -> Int -> ByteString -> Integer
little Int
24 Int
8 ByteString
record Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
count64) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"inconsistent ZIP64 disk inventory")
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Integer
count Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
65535 Bool -> Bool -> Bool
|| Integer
count Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
count64) Bool -> Bool -> Bool
&& (Integer
size Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
4294967295 Bool -> Bool -> Bool
|| Integer
size Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
size64) Bool -> Bool -> Bool
&& (Integer
offset Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
4294967295 Bool -> Bool -> Bool
|| Integer
offset Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
offset64)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"ZIP end records disagree")
            (Integer, Integer, Integer, Integer)
-> Either ParseError (Integer, Integer, Integer, Integer)
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer
position, Integer
count64, Integer
size64, Integer
offset64)
        else do
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
count Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
65535 Bool -> Bool -> Bool
|| Integer
size Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
4294967295 Bool -> Bool -> Bool
|| Integer
offset Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
4294967295) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"missing ZIP64 end record")
            (Integer, Integer, Integer, Integer)
-> Either ParseError (Integer, Integer, Integer, Integer)
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
end, Integer
count, Integer
size, Integer
offset)

readDirectory :: (Int -> IO ()) -> Int -> ByteString -> Int -> ByteString -> ExceptT ParseError IO [ZipMember]
readDirectory :: (Int -> IO ())
-> Int
-> ByteString
-> Int
-> ByteString
-> ExceptT ParseError IO [ZipMember]
readDirectory Int -> IO ()
charge Int
held ByteString
archive Int
count ByteString
bytes
    | Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = if ByteString -> Bool
BS.null ByteString
bytes then [ZipMember] -> ExceptT ParseError IO [ZipMember]
forall a. a -> ExceptT ParseError IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [] else ParseError -> ExceptT ParseError IO [ZipMember]
forall a. ParseError -> ExceptT ParseError IO a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> ParseError
ParseError Text
"ZIP directory count is inconsistent")
    | ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
46 Bool -> Bool -> Bool
|| Bool -> Bool
not (ByteString -> ByteString -> Bool
BS.isPrefixOf ByteString
"PK\x01\x02" ByteString
bytes) = ParseError -> ExceptT ParseError IO [ZipMember]
forall a. ParseError -> ExceptT ParseError IO a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> ParseError
ParseError Text
"truncated ZIP directory")
    | Bool
otherwise = do
        (member, total) <- IO (Either ParseError (ZipMember, Int))
-> ExceptT ParseError IO (ZipMember, Int)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (IO (Either ParseError (ZipMember, Int))
 -> ExceptT ParseError IO (ZipMember, Int))
-> (Either ParseError (ZipMember, Int)
    -> IO (Either ParseError (ZipMember, Int)))
-> Either ParseError (ZipMember, Int)
-> ExceptT ParseError IO (ZipMember, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either ParseError (ZipMember, Int)
-> IO (Either ParseError (ZipMember, Int))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ParseError (ZipMember, Int)
 -> ExceptT ParseError IO (ZipMember, Int))
-> Either ParseError (ZipMember, Int)
-> ExceptT ParseError IO (ZipMember, Int)
forall a b. (a -> b) -> a -> b
$ do
            let nameSize :: Int
nameSize = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Int -> Int -> ByteString -> Integer
little Int
28 Int
2 ByteString
bytes)
                extraSize :: Int
extraSize = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Int -> Int -> ByteString -> Integer
little Int
30 Int
2 ByteString
bytes)
                commentSize :: Int
commentSize = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Int -> Int -> ByteString -> Integer
little Int
32 Int
2 ByteString
bytes)
                total :: Int
total = Int
46 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
nameSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
extraSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
commentSize
                mode :: Integer
mode = Int -> Int -> ByteString -> Integer
little Int
38 Int
4 ByteString
bytes Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftR` Int
16 Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.&. Integer
61440
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
total Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> ByteString -> Int
BS.length ByteString
bytes Bool -> Bool -> Bool
|| Int
nameSize Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
4096 Bool -> Bool -> Bool
|| Int
extraSize Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
16384 Bool -> Bool -> Bool
|| Int
commentSize Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
16384) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"ZIP entry header exceeds its bound")
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
mode Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0 Bool -> Bool -> Bool
&& Integer
mode Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
32768 Bool -> Bool -> Bool
&& Integer
mode Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
16384) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"ZIP contains a link or special file")
            name <- Integer -> ByteString -> Either ParseError Text
zipName (Int -> Int -> ByteString -> Integer
little Int
8 Int
2 ByteString
bytes) (Int -> ByteString -> ByteString
BS.take Int
nameSize (Int -> ByteString -> ByteString
BS.drop Int
46 ByteString
bytes))
            void (checkPath name)
            extra <- zip64Extra (BS.take extraSize (BS.drop (46 + nameSize) bytes))
            (inflated, rest1) <- expanded 4294967295 (little 24 4 bytes) 8 extra
            (compressed, rest2) <- expanded 4294967295 (little 20 4 bytes) 8 rest1
            (offset, rest3) <- expanded 4294967295 (little 42 4 bytes) 8 rest2
            (disk, _) <- expanded 65535 (little 34 2 bytes) 4 rest3
            unless (disk == 0) (parseRefusal "multi-disk ZIP member is unsupported")
            (dataOffset, end) <- checkLocalHeader archive bytes name offset compressed inflated
            let method = Int -> Int -> ByteString -> Integer
little Int
10 Int
2 ByteString
bytes
            unless (method `elem` [0, 8, 12, 14]) (parseRefusal "unsupported ZIP compression method")
            pure (ZipMember name offset end dataOffset compressed inflated (fromInteger (little 16 4 bytes)) method (little 8 2 bytes .&. 2 /= 0), total)
        let wanted = Int
held Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
512 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
* ByteString -> Int
BS.length (Text -> ByteString
TE.encodeUtf8 (ZipMember -> Text
zmName ZipMember
member))
        liftIO (charge (archiveScratchBytes + wanted))
        remaining <- readDirectory charge wanted archive (count - 1) (BS.drop total bytes)
        pure (member : remaining)

zip64Extra :: ByteString -> Either ParseError ByteString
zip64Extra :: ByteString -> Either ParseError ByteString
zip64Extra = Maybe ByteString -> ByteString -> Either ParseError ByteString
go Maybe ByteString
forall a. Maybe a
Nothing
  where
    go :: Maybe ByteString -> ByteString -> Either ParseError ByteString
go Maybe ByteString
found ByteString
bytes
        | ByteString -> Bool
BS.null ByteString
bytes = ByteString -> Either ParseError ByteString
forall a b. b -> Either a b
Right (ByteString -> Maybe ByteString -> ByteString
forall a. a -> Maybe a -> a
fromMaybe ByteString
BS.empty Maybe ByteString
found)
        | ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
4 = Text -> Either ParseError ByteString
forall a. Text -> Either ParseError a
parseRefusal Text
"truncated ZIP extra field"
        | Bool
otherwise = do
            let tag :: Integer
tag = Int -> Int -> ByteString -> Integer
little Int
0 Int
2 ByteString
bytes
                size :: Int
size = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Int -> Int -> ByteString -> Integer
little Int
2 Int
2 ByteString
bytes)
                value :: ByteString
value = Int -> ByteString -> ByteString
BS.take Int
size (Int -> ByteString -> ByteString
BS.drop Int
4 ByteString
bytes)
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
4) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"truncated ZIP extra field")
            Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
tag Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 Bool -> Bool -> Bool
&& Maybe ByteString -> Bool
forall a. Maybe a -> Bool
isJust Maybe ByteString
found) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"ambiguous ZIP64 extra field")
            Maybe ByteString -> ByteString -> Either ParseError ByteString
go (if Integer
tag Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 then ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
value else Maybe ByteString
found) (Int -> ByteString -> ByteString
BS.drop (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) ByteString
bytes)

expanded :: Integer -> Integer -> Int -> ByteString -> Either ParseError (Integer, ByteString)
expanded :: Integer
-> Integer
-> Int
-> ByteString
-> Either ParseError (Integer, ByteString)
expanded Integer
sentinel Integer
original Int
width ByteString
bytes
    | Integer
original Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
sentinel = (Integer, ByteString) -> Either ParseError (Integer, ByteString)
forall a b. b -> Either a b
Right (Integer
original, ByteString
bytes)
    | ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
width = Text -> Either ParseError (Integer, ByteString)
forall a. Text -> Either ParseError a
parseRefusal Text
"missing ZIP64 size or offset"
    | Bool
otherwise = (Integer, ByteString) -> Either ParseError (Integer, ByteString)
forall a b. b -> Either a b
Right (Int -> Int -> ByteString -> Integer
little Int
0 Int
width ByteString
bytes, Int -> ByteString -> ByteString
BS.drop Int
width ByteString
bytes)

checkLocalHeader :: ByteString -> ByteString -> Text -> Integer -> Integer -> Integer -> Either ParseError (Integer, Integer)
checkLocalHeader :: ByteString
-> ByteString
-> Text
-> Integer
-> Integer
-> Integer
-> Either ParseError (Integer, Integer)
checkLocalHeader ByteString
archive ByteString
central Text
name Integer
offset Integer
compressed Integer
inflated = do
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
offset Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
BS.length ByteString
archive)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"ZIP directory points outside local entries")
    let localHeader :: ByteString
localHeader = Int -> ByteString -> ByteString
BS.drop (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
offset) ByteString
archive
        flags :: Integer
flags = Int -> Int -> ByteString -> Integer
little Int
8 Int
2 ByteString
central
        nameSize :: Int
nameSize = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Int -> Int -> ByteString -> Integer
little Int
26 Int
2 ByteString
localHeader)
        extraSize :: Int
extraSize = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Int -> Int -> ByteString -> Integer
little Int
28 Int
2 ByteString
localHeader)
        dataOffset :: Integer
dataOffset = Integer
offset Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
30 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
nameSize Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
extraSize
        dataEnd :: Integer
dataEnd = Integer
dataOffset Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
compressed
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int
BS.length ByteString
localHeader Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
30 Bool -> Bool -> Bool
|| Bool -> Bool
not (ByteString -> ByteString -> Bool
BS.isPrefixOf ByteString
"PK\x03\x04" ByteString
localHeader) Bool -> Bool -> Bool
|| Int
nameSize Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
4096 Bool -> Bool -> Bool
|| Int
extraSize Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
16384 Bool -> Bool -> Bool
|| Integer
dataEnd Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
BS.length ByteString
archive)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"invalid ZIP local entry extent")
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Integer
flags Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.&. Integer
0xf7f1 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 Bool -> Bool -> Bool
&& Int -> Int -> ByteString -> Integer
little Int
4 Int
2 ByteString
localHeader Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
63) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"unsupported ZIP encryption flags or version")
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int -> Int -> ByteString -> Integer
little Int
6 Int
2 ByteString
localHeader Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
flags Bool -> Bool -> Bool
&& Int -> Int -> ByteString -> Integer
little Int
8 Int
2 ByteString
localHeader Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Int -> ByteString -> Integer
little Int
10 Int
2 ByteString
central) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"ZIP entry compression flags disagree")
    localName <- Integer -> ByteString -> Either ParseError Text
zipName Integer
flags (Int -> ByteString -> ByteString
BS.take Int
nameSize (Int -> ByteString -> ByteString
BS.drop Int
30 ByteString
localHeader))
    unless (localName == name) (parseRefusal "ZIP directory names another local entry")
    extra <- zip64Extra (BS.take extraSize (BS.drop (30 + nameSize) localHeader))
    if flags .&. 8 /= 0
        then do
            let width = if Int -> Int -> ByteString -> Integer
little Int
18 Int
4 ByteString
localHeader Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
4294967295 Bool -> Bool -> Bool
|| Int -> Int -> ByteString -> Integer
little Int
22 Int
4 ByteString
localHeader Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
4294967295 Bool -> Bool -> Bool
|| Bool -> Bool
not (ByteString -> Bool
BS.null ByteString
extra) then Int
8 else Int
4
            descriptorSize <- checkDescriptor (BS.drop (fromInteger dataEnd) archive) (little 16 4 central) compressed inflated width
            pure (dataOffset, dataEnd + descriptorSize)
        else do
            unless (little 14 4 localHeader == little 16 4 central) (parseRefusal "ZIP entry CRC declarations disagree")
            (localInflated, rest) <- expanded 4294967295 (little 22 4 localHeader) 8 extra
            (localCompressed, _) <- expanded 4294967295 (little 18 4 localHeader) 8 rest
            unless (localInflated == inflated && localCompressed == compressed) (parseRefusal "ZIP entry size declarations disagree")
            pure (dataOffset, dataEnd)

checkDescriptor :: ByteString -> Integer -> Integer -> Integer -> Int -> Either ParseError Integer
checkDescriptor :: ByteString
-> Integer
-> Integer
-> Integer
-> Int
-> Either ParseError Integer
checkDescriptor ByteString
raw Integer
crc Integer
compressed Integer
inflated Int
width = case [Integer]
matching of
    [Integer
size] -> Integer -> Either ParseError Integer
forall a b. b -> Either a b
Right Integer
size
    [Integer]
_ -> Text -> Either ParseError Integer
forall a. Text -> Either ParseError a
parseRefusal Text
"ZIP descriptor disagrees with central directory"
  where
    candidates :: [(Int, ByteString)]
candidates = (Int
0, ByteString
raw) (Int, ByteString) -> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. a -> [a] -> [a]
: [(Int
4, Int -> ByteString -> ByteString
BS.drop Int
4 ByteString
raw) | ByteString -> ByteString -> Bool
BS.isPrefixOf ByteString
"PK\x07\x08" ByteString
raw]
    matching :: [Integer]
matching = [Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int
prefix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
width) | (Int
prefix, ByteString
descriptor) <- [(Int, ByteString)]
candidates, ByteString -> Int
BS.length ByteString
descriptor Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
width, Int -> Int -> ByteString -> Integer
little Int
0 Int
4 ByteString
descriptor Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
crc, Int -> Int -> ByteString -> Integer
little Int
4 Int
width ByteString
descriptor Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
compressed, Int -> Int -> ByteString -> Integer
little (Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
width) Int
width ByteString
descriptor Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
inflated]

zipName :: Integer -> ByteString -> Either ParseError Text
zipName :: Integer -> ByteString -> Either ParseError Text
zipName Integer
flags ByteString
bytes
    | Integer
flags Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.&. Integer
2048 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0 = ByteString -> Either ParseError Text
utf8 ByteString
bytes
    | Bool
otherwise = Text -> Either ParseError Text
forall a b. b -> Either a b
Right (ByteString -> Text
TE.decodeLatin1 ByteString
bytes)

findZipEnd :: ByteString -> Either ParseError Int
findZipEnd :: ByteString -> Either ParseError Int
findZipEnd ByteString
bytes = case [Int]
candidates of
    [Int
offset] -> Int -> Either ParseError Int
forall a b. b -> Either a b
Right Int
offset
    [Int]
_ -> Text -> Either ParseError Int
forall a. Text -> Either ParseError a
parseRefusal Text
"missing or ambiguous ZIP end record"
  where
    start :: Int
start = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
65557)
    candidates :: [Int]
candidates = Int -> ByteString -> [Int]
search Int
start (Int -> ByteString -> ByteString
BS.drop Int
start ByteString
bytes)
    search :: Int -> ByteString -> [Int]
search Int
offset ByteString
remaining = case ByteString -> ByteString -> (ByteString, ByteString)
BS.breakSubstring ByteString
"PK\x05\x06" ByteString
remaining of
        (ByteString
_, ByteString
suffix) | ByteString -> Bool
BS.null ByteString
suffix -> []
        (ByteString
prefix, ByteString
suffix) ->
            let found :: Int
found = Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
prefix
                valid :: Bool
valid = ByteString -> Int
BS.length ByteString
suffix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
22 Bool -> Bool -> Bool
&& Int -> Int -> ByteString -> Integer
little Int
20 Int
2 ByteString
suffix Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
22 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
BS.length ByteString
suffix)
             in [Int
found | Bool
valid] [Int] -> [Int] -> [Int]
forall a. Semigroup a => a -> a -> a
<> Int -> ByteString -> [Int]
search (Int
found Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) (Int -> ByteString -> ByteString
BS.drop Int
4 ByteString
suffix)

little :: Int -> Int -> ByteString -> Integer
little :: Int -> Int -> ByteString -> Integer
little Int
offset Int
size = (Word8 -> Integer -> Integer) -> Integer -> ByteString -> Integer
forall a. (Word8 -> a -> a) -> a -> ByteString -> a
BS.foldr (\Word8
byte Integer
value -> Word8 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
byte Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
value Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
256) Integer
0 (ByteString -> Integer)
-> (ByteString -> ByteString) -> ByteString -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> ByteString
BS.take Int
size (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> ByteString
BS.drop Int
offset

utf8 :: ByteString -> Either ParseError Text
utf8 :: ByteString -> Either ParseError Text
utf8 = (UnicodeException -> ParseError)
-> Either UnicodeException Text -> Either ParseError Text
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (ParseError -> UnicodeException -> ParseError
forall a b. a -> b -> a
const (Text -> ParseError
ParseError Text
"archive path is not UTF-8")) (Either UnicodeException Text -> Either ParseError Text)
-> (ByteString -> Either UnicodeException Text)
-> ByteString
-> Either ParseError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
TE.decodeUtf8'