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

-- | BZIP2 decoding with one resource scope per compressed member.
module Ecluse.Core.Registry.PyPI.Archive.BZip (withBzipChunks) where

import Conduit
import Control.Monad.Except (throwError)
import Control.Monad.Trans.Resource (InternalState, allocate, closeInternalState, createInternalState, release, runInternalState)
import Data.ByteString qualified as BS
import Data.Conduit.BZlib qualified as BZip
import System.IO.Error (ioeGetErrorString, isUserError)
import UnliftIO.Exception (bracket, tryJust)

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

-- | Charge each validated block-size declaration before native allocation. Refusal closes every registered resource.
withBzipChunks :: Int -> (Int -> IO ()) -> ByteString -> (ConduitT () ByteString TarDecode () -> IO a) -> IO a
withBzipChunks :: forall a.
Int
-> (Int -> IO ())
-> ByteString
-> (ConduitT () ByteString TarDecode () -> IO a)
-> IO a
withBzipChunks Int
limit Int -> IO ()
charge ByteString
bytes ConduitT () ByteString TarDecode () -> IO a
consume =
    IO InternalState
-> (InternalState -> IO ()) -> (InternalState -> IO a) -> IO a
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket IO InternalState
forall (m :: * -> *). MonadIO m => m InternalState
createInternalState InternalState -> IO ()
forall (m :: * -> *). MonadIO m => InternalState -> m ()
closeInternalState ((InternalState -> IO a) -> IO a)
-> (InternalState -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \InternalState
scope ->
        ConduitT () ByteString TarDecode () -> IO a
consume (ByteString -> ConduitT () ByteString TarDecode ()
forall i. ByteString -> ConduitT i ByteString TarDecode ()
sourceChunks ByteString
bytes ConduitT () ByteString TarDecode ()
-> ConduitT ByteString ByteString TarDecode ()
-> ConduitT () ByteString TarDecode ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| InternalState -> Int -> ConduitT ByteString ByteString TarDecode ()
members InternalState
scope Int
0)
  where
    members :: InternalState -> Int -> ConduitT ByteString ByteString TarDecode ()
members InternalState
scope Int
count = do
        header <- Index ByteString -> ConduitT ByteString ByteString TarDecode ()
forall (m :: * -> *) seq.
(Monad m, IsSequence seq) =>
Index seq -> ConduitT seq seq m ()
takeCE (Int
4 :: Int) ConduitT ByteString ByteString TarDecode ()
-> ConduitT ByteString ByteString TarDecode ByteString
-> ConduitT ByteString ByteString TarDecode ByteString
forall (m :: * -> *) a b c r.
Monad m =>
ConduitT a b m () -> ConduitT b c m r -> ConduitT a c m r
.| ConduitT ByteString ByteString TarDecode ByteString
forall (m :: * -> *) a o. (Monad m, Monoid a) => ConduitT a o m a
foldC
        when (BS.null header && count == 0) (lift (throwError (ParseError "missing bzip2 stream")))
        unless (BS.null header) $ do
            when (count >= limit) (lift (throwError (ParseError "bzip2 member count exceeds its bound")))
            case BS.unpack header of
                [Word8
66, Word8
90, Word8
104, Word8
level] | Word8
level Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word8
49 Bool -> Bool -> Bool
&& Word8
level Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
57 -> do
                    -- libbzip2 documents 100 kB + 4 * block size. The conduit buffers 32 KiB input and 4 KiB output.
                    IO () -> ConduitT ByteString ByteString TarDecode ()
forall a. IO a -> ConduitT ByteString ByteString TarDecode a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int -> IO ()
charge (Int
100000 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
100000 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
level Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Word8
48) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
32768 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4096))
                    ByteString -> ConduitT ByteString ByteString TarDecode ()
forall i o (m :: * -> *). i -> ConduitT i o m ()
leftover ByteString
header
                    (key, memberScope) <- IO (ReleaseKey, InternalState)
-> ConduitT
     ByteString ByteString TarDecode (ReleaseKey, InternalState)
forall a. IO a -> ConduitT ByteString ByteString TarDecode a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ResourceT IO (ReleaseKey, InternalState)
-> InternalState -> IO (ReleaseKey, InternalState)
forall (m :: * -> *) a. ResourceT m a -> InternalState -> m a
runInternalState (IO InternalState
-> (InternalState -> IO ())
-> ResourceT IO (ReleaseKey, InternalState)
forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
allocate IO InternalState
forall (m :: * -> *). MonadIO m => m InternalState
createInternalState InternalState -> IO ()
forall (m :: * -> *). MonadIO m => InternalState -> m ()
closeInternalState) InternalState
scope)
                    transPipe (runNative memberScope) (BZip.decompress1 BZip.def)
                    liftIO (runInternalState (release key) scope)
                    members scope (count + 1)
                [Word8]
_ -> ExceptT ParseError IO ()
-> ConduitT ByteString ByteString TarDecode ()
forall (m :: * -> *) a.
Monad m =>
m a -> ConduitT ByteString ByteString m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ParseError -> ExceptT ParseError IO ()
forall a. ParseError -> ExceptT ParseError IO a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> ParseError
ParseError Text
"malformed bzip2 block-size header"))

sourceChunks :: ByteString -> ConduitT i ByteString TarDecode ()
sourceChunks :: forall i. ByteString -> ConduitT i ByteString TarDecode ()
sourceChunks ByteString
bytes = Bool
-> ConduitT i ByteString TarDecode ()
-> ConduitT i ByteString TarDecode ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Bool
BS.null ByteString
bytes) (ConduitT i ByteString TarDecode ()
 -> ConduitT i ByteString TarDecode ())
-> ConduitT i ByteString TarDecode ()
-> ConduitT i ByteString TarDecode ()
forall a b. (a -> b) -> a -> b
$ do
    let (ByteString
chunk, ByteString
remaining) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
32768 ByteString
bytes
    ByteString -> ConduitT i ByteString TarDecode ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ByteString
chunk
    ByteString -> ConduitT i ByteString TarDecode ()
forall i. ByteString -> ConduitT i ByteString TarDecode ()
sourceChunks ByteString
remaining

runNative :: InternalState -> ResourceT IO a -> TarDecode a
runNative :: forall a. InternalState -> ResourceT IO a -> TarDecode a
runNative InternalState
scope ResourceT IO a
action = IO (Either ParseError a) -> ExceptT ParseError IO a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (IO (Either ParseError a) -> ExceptT ParseError IO a)
-> IO (Either ParseError a) -> ExceptT ParseError IO a
forall a b. (a -> b) -> a -> b
$ do
    result <- (IOError -> Maybe ()) -> IO a -> IO (Either () a)
forall (m :: * -> *) e b a.
(MonadUnliftIO m, Exception e) =>
(e -> Maybe b) -> m a -> m (Either b a)
tryJust IOError -> Maybe ()
malformed (ResourceT IO a -> InternalState -> IO a
forall (m :: * -> *) a. ResourceT m a -> InternalState -> m a
runInternalState ResourceT IO a
action InternalState
scope)
    pure (first (const (ParseError "malformed or truncated bzip2 archive")) result)
  where
    malformed :: IOError -> Maybe ()
malformed IOError
fault
        | IOError -> Bool
isUserError IOError
fault Bool -> Bool -> Bool
&& IOError -> String
ioeGetErrorString IOError
fault String -> [String] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [String
"BZ2_bzDecompress: -4", String
"BZ2_bzDecompress: -5", String
"unexpected EOF on decompress"] = () -> Maybe ()
forall a. a -> Maybe a
Just ()
        | Bool
otherwise = Maybe ()
forall a. Maybe a
Nothing