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

-- | Scoped XZ and legacy LZMA decoding under the same native dictionary limit.
module Ecluse.Core.Registry.PyPI.Archive.Lzma (LzmaFormat (..), withLzmaChunks) where

import Codec.Compression.Lzma qualified as Lzma
import Conduit
import Control.Monad.Except (throwError)
import Data.ByteString qualified as BS
import UnliftIO.Exception (throwIO)

import Ecluse.Core.Registry (ParseError (..))
import Ecluse.Core.Registry.Archive.Tar (TarDecode)

-- | ZIP's LZMA properties are converted to an alone-format header before decoding.
data LzmaFormat = XzArchive | ZipLzmaMember

data NativeLzmaFault = LzmaAllocationFailed | LzmaInvalidState
    deriving stock (Int -> NativeLzmaFault -> ShowS
[NativeLzmaFault] -> ShowS
NativeLzmaFault -> String
(Int -> NativeLzmaFault -> ShowS)
-> (NativeLzmaFault -> String)
-> ([NativeLzmaFault] -> ShowS)
-> Show NativeLzmaFault
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NativeLzmaFault -> ShowS
showsPrec :: Int -> NativeLzmaFault -> ShowS
$cshow :: NativeLzmaFault -> String
show :: NativeLzmaFault -> String
$cshowList :: [NativeLzmaFault] -> ShowS
showList :: [NativeLzmaFault] -> ShowS
Show)

instance Exception NativeLzmaFault

-- | Pay the enforced native memory limit before creation. The callback cannot retain the source beyond its scope.
withLzmaChunks :: LzmaFormat -> (Int -> IO ()) -> [ByteString] -> (ConduitT () ByteString TarDecode () -> IO a) -> IO a
withLzmaChunks :: forall a.
LzmaFormat
-> (Int -> IO ())
-> [ByteString]
-> (ConduitT () ByteString TarDecode () -> IO a)
-> IO a
withLzmaChunks LzmaFormat
format Int -> IO ()
charge [ByteString]
input ConduitT () ByteString TarDecode () -> IO a
consume = do
    Int -> IO ()
charge (Int
64 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
1024 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
1024 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
32752)
    DecompressParams -> (DecompressStream IO -> IO a) -> IO a
forall a. DecompressParams -> (DecompressStream IO -> IO a) -> IO a
Lzma.withDecompressIO DecompressParams
params (ConduitT () ByteString TarDecode () -> IO a
consume (ConduitT () ByteString TarDecode () -> IO a)
-> (DecompressStream IO -> ConduitT () ByteString TarDecode ())
-> DecompressStream IO
-> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString]
-> DecompressStream IO -> ConduitT () ByteString TarDecode ()
forall {m :: * -> *} {i}.
(MonadIO m, MonadError ParseError m) =>
[ByteString] -> DecompressStream IO -> ConduitT i ByteString m ()
go [ByteString]
input)
  where
    params :: DecompressParams
params =
        DecompressParams
Lzma.defaultDecompressParams
            { Lzma.decompressMemLimit = 64 * 1024 * 1024
            , Lzma.decompressAutoDecoder = case format of LzmaFormat
XzArchive -> Bool
False; LzmaFormat
ZipLzmaMember -> Bool
True
            , Lzma.decompressConcatenated = case format of LzmaFormat
XzArchive -> Bool
True; LzmaFormat
ZipLzmaMember -> Bool
False
            }
    go :: [ByteString] -> DecompressStream IO -> ConduitT i ByteString m ()
go [ByteString]
remaining = \case
        Lzma.DecompressInputRequired ByteString -> IO (DecompressStream IO)
supply -> do
            let (ByteString
chunk, [ByteString]
rest) = [ByteString] -> (ByteString, [ByteString])
inputChunk [ByteString]
remaining
            IO (DecompressStream IO)
-> ConduitT i ByteString m (DecompressStream IO)
forall a. IO a -> ConduitT i ByteString m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ByteString -> IO (DecompressStream IO)
supply ByteString
chunk) ConduitT i ByteString m (DecompressStream IO)
-> (DecompressStream IO -> ConduitT i ByteString m ())
-> ConduitT i ByteString m ()
forall a b.
ConduitT i ByteString m a
-> (a -> ConduitT i ByteString m b) -> ConduitT i ByteString m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [ByteString] -> DecompressStream IO -> ConduitT i ByteString m ()
go [ByteString]
rest
        Lzma.DecompressOutputAvailable ByteString
chunk IO (DecompressStream IO)
next -> ByteString -> ConduitT i ByteString m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ByteString
chunk ConduitT i ByteString m ()
-> ConduitT i ByteString m (DecompressStream IO)
-> ConduitT i ByteString m (DecompressStream IO)
forall a b.
ConduitT i ByteString m a
-> ConduitT i ByteString m b -> ConduitT i ByteString m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO (DecompressStream IO)
-> ConduitT i ByteString m (DecompressStream IO)
forall a. IO a -> ConduitT i ByteString m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (DecompressStream IO)
next ConduitT i ByteString m (DecompressStream IO)
-> (DecompressStream IO -> ConduitT i ByteString m ())
-> ConduitT i ByteString m ()
forall a b.
ConduitT i ByteString m a
-> (a -> ConduitT i ByteString m b) -> ConduitT i ByteString m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [ByteString] -> DecompressStream IO -> ConduitT i ByteString m ()
go [ByteString]
remaining
        Lzma.DecompressStreamEnd ByteString
rest ->
            Bool -> ConduitT i ByteString m () -> ConduitT i ByteString m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Bool
BS.null ByteString
rest Bool -> Bool -> Bool
&& (ByteString -> Bool) -> [ByteString] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ByteString -> Bool
BS.null [ByteString]
remaining) (m () -> ConduitT i ByteString m ()
forall (m :: * -> *) a. Monad m => m a -> ConduitT i ByteString 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
"LZMA stream has trailing compressed bytes")))
        Lzma.DecompressStreamError LzmaRet
failure -> case LzmaRet
failure of
            -- Native allocation and programming faults must reach the worker fault boundary.
            LzmaRet
Lzma.LzmaRetMemError -> IO () -> ConduitT i ByteString m ()
forall a. IO a -> ConduitT i ByteString m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (NativeLzmaFault -> IO ()
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO NativeLzmaFault
LzmaAllocationFailed)
            LzmaRet
Lzma.LzmaRetProgError -> IO () -> ConduitT i ByteString m ()
forall a. IO a -> ConduitT i ByteString m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (NativeLzmaFault -> IO ()
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO NativeLzmaFault
LzmaInvalidState)
            LzmaRet
_ -> m () -> ConduitT i ByteString m ()
forall (m :: * -> *) a. Monad m => m a -> ConduitT i ByteString 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
"malformed, truncated, or excessive-memory LZMA stream"))

inputChunk :: [ByteString] -> (ByteString, [ByteString])
inputChunk :: [ByteString] -> (ByteString, [ByteString])
inputChunk [] = (ByteString
BS.empty, [])
inputChunk (ByteString
bytes : [ByteString]
remaining)
    | ByteString -> Bool
BS.null ByteString
bytes = [ByteString] -> (ByteString, [ByteString])
inputChunk [ByteString]
remaining
    | Bool
otherwise =
        let (ByteString
chunk, ByteString
rest) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
32768 ByteString
bytes
         in (ByteString
chunk, if ByteString -> Bool
BS.null ByteString
rest then [ByteString]
remaining else ByteString
rest ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
remaining)