summaryrefslogtreecommitdiff
path: root/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs')
-rw-r--r--Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs23
1 files changed, 16 insertions, 7 deletions
diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs b/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs
index b89fbfa..bfaef39 100644
--- a/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs
+++ b/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs
@@ -7,12 +7,15 @@
7module Codec.Encryption.OpenPGP.ASCIIArmor.Decode ( 7module Codec.Encryption.OpenPGP.ASCIIArmor.Decode (
8 parseArmor 8 parseArmor
9 , decode 9 , decode
10 , decodeLazy
10) where 11) where
11 12
12import Codec.Encryption.OpenPGP.ASCIIArmor.Types 13import Codec.Encryption.OpenPGP.ASCIIArmor.Types
13import Codec.Encryption.OpenPGP.ASCIIArmor.Utils 14import Codec.Encryption.OpenPGP.ASCIIArmor.Utils
14import Control.Applicative (many, (<|>), (<$>), Alternative, (<*), (<*>), (*>), optional) 15import Control.Applicative (many, (<|>), (<$>), Alternative, (<*), (<*>), (*>), optional)
15import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>), parse, IResult(..)) 16import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>))
17import qualified Data.Attoparsec.ByteString as AS
18import qualified Data.Attoparsec.ByteString.Lazy as AL
16import Data.Attoparsec.ByteString.Char8 (isDigit_w8, anyChar) 19import Data.Attoparsec.ByteString.Char8 (isDigit_w8, anyChar)
17import Data.Attoparsec.Combinator (manyTill) 20import Data.Attoparsec.Combinator (manyTill)
18import Data.Bits (shiftL) 21import Data.Bits (shiftL)
@@ -29,11 +32,17 @@ import Data.String (IsString, fromString)
29import Data.Word (Word32) 32import Data.Word (Word32)
30 33
31decode :: IsString e => ByteString -> Either e [Armor] 34decode :: IsString e => ByteString -> Either e [Armor]
32decode bs = go (parse parseArmors bs) 35decode bs = go (AS.parse parseArmors bs)
33 where 36 where
34 go (Fail t c e) = Left (fromString e) 37 go (AS.Fail t c e) = Left (fromString e)
35 go (Partial cont) = go (cont B.empty) 38 go (AS.Partial cont) = go (cont B.empty)
36 go (Done _ r) = Right r 39 go (AS.Done _ r) = Right r
40
41decodeLazy :: IsString e => BL.ByteString -> Either e [Armor]
42decodeLazy bs = go (AL.parse parseArmors bs)
43 where
44 go (AL.Fail t c e) = Left (fromString e)
45 go (AL.Done _ r) = Right r
37 46
38parseArmors :: Parser [Armor] 47parseArmors :: Parser [Armor]
39parseArmors = many parseArmor 48parseArmors = many parseArmor
@@ -110,8 +119,8 @@ aType :: ArmorType -> ByteString
110aType (ArmorMessage) = BC8.pack "MESSAGE" 119aType (ArmorMessage) = BC8.pack "MESSAGE"
111aType (ArmorPublicKeyBlock) = BC8.pack "PUBLIC KEY BLOCK" 120aType (ArmorPublicKeyBlock) = BC8.pack "PUBLIC KEY BLOCK"
112aType (ArmorPrivateKeyBlock) = BC8.pack "PRIVATE KEY BLOCK" 121aType (ArmorPrivateKeyBlock) = BC8.pack "PRIVATE KEY BLOCK"
113aType (ArmorSplitMessage x y) = BC8.pack "MESSAGE, PART " `B.append` (l2s x) `B.append` BC8.singleton '/' `B.append` (l2s y) 122aType (ArmorSplitMessage x y) = BC8.pack "MESSAGE, PART " `B.append` l2s x `B.append` BC8.singleton '/' `B.append` l2s y
114aType (ArmorSplitMessageIndefinite x) = BC8.pack "MESSAGE, PART " `B.append` (l2s x) 123aType (ArmorSplitMessageIndefinite x) = BC8.pack "MESSAGE, PART " `B.append` l2s x
115aType (ArmorSignature) = BC8.pack "SIGNATURE" 124aType (ArmorSignature) = BC8.pack "SIGNATURE"
116 125
117l2s :: BL.ByteString -> ByteString 126l2s :: BL.ByteString -> ByteString