    -- ASCIIArmor/Multipart.hs: OpenPGP (legacy RFC4880) ASCII armor implementation
    -- Copyright © 2012-2026  Clint Adams
    -- This software is released under the terms of the Expat license.
    -- (See the LICENSE file).

    module Codec.Encryption.OpenPGP.ASCIIArmor.Multipart (
       multipartMerge
    ) where

    import Codec.Encryption.OpenPGP.ASCIIArmor.Types

    import Data.ByteString.Lazy (ByteString)
    import qualified Data.ByteString.Lazy as BL

    multipartMerge :: [Armor] -> Armor
    multipartMerge :: [Armor] -> Armor
    multipartMerge [Armor]
    as' = [Armor] -> Armor -> Armor
    go [Armor]
    as' (ArmorType -> [(String, String)] -> ByteString -> Armor
    Armor ArmorType
    ArmorMessage [] ByteString
    BL.empty)
        where
            go :: [Armor] -> Armor -> Armor
            go :: [Armor] -> Armor -> Armor
    go [] Armor
    state = Armor
    state
            go (Armor ArmorType
    at [(String, String)]
    hs ByteString
    bs:[Armor]
    as) Armor
    state = [Armor] -> Armor -> Armor
    go [Armor]
    as (ArmorType -> [(String, String)] -> ByteString -> Armor -> Armor
    go' ArmorType
    at [(String, String)]
    hs ByteString
    bs Armor
    state)
            go [Armor]
    _ Armor
    _ = String -> Armor
    forall a. HasCallStack => String -> a
    error String
    "This shouldn't happen."
            go' :: ArmorType -> [(String,String)] -> ByteString -> Armor -> Armor
            go' :: ArmorType -> [(String, String)] -> ByteString -> Armor -> Armor
    go' (ArmorSplitMessage ByteString
    _ ByteString
    _) [(String, String)]
    hs ByteString
    bs (Armor ArmorType
    _ [(String, String)]
    ohs ByteString
    obs) = ArmorType -> [(String, String)] -> ByteString -> Armor
    Armor ArmorType
    ArmorMessage ([(String, String)]
    ohs [(String, String)] -> [(String, String)] -> [(String, String)]
    forall a. [a] -> [a] -> [a]
    ++ [(String, String)]
    hs) (ByteString
    obs ByteString -> ByteString -> ByteString
    `BL.append` ByteString
    bs)
            go' (ArmorSplitMessageIndefinite ByteString
    _) [(String, String)]
    hs ByteString
    bs (Armor ArmorType
    _ [(String, String)]
    ohs ByteString
    obs) = ArmorType -> [(String, String)] -> ByteString -> Armor
    Armor ArmorType
    ArmorMessage ([(String, String)]
    ohs [(String, String)] -> [(String, String)] -> [(String, String)]
    forall a. [a] -> [a] -> [a]
    ++ [(String, String)]
    hs) (ByteString
    obs ByteString -> ByteString -> ByteString
    `BL.append` ByteString
    bs)
            go' ArmorType
    _ [(String, String)]
    _ ByteString
    _ Armor
    state = Armor
    state