diff options
Diffstat (limited to 'Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs')
-rw-r--r-- | Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs | 23 |
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 @@ | |||
7 | module Codec.Encryption.OpenPGP.ASCIIArmor.Decode ( | 7 | module Codec.Encryption.OpenPGP.ASCIIArmor.Decode ( |
8 | parseArmor | 8 | parseArmor |
9 | , decode | 9 | , decode |
10 | , decodeLazy | ||
10 | ) where | 11 | ) where |
11 | 12 | ||
12 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types | 13 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types |
13 | import Codec.Encryption.OpenPGP.ASCIIArmor.Utils | 14 | import Codec.Encryption.OpenPGP.ASCIIArmor.Utils |
14 | import Control.Applicative (many, (<|>), (<$>), Alternative, (<*), (<*>), (*>), optional) | 15 | import Control.Applicative (many, (<|>), (<$>), Alternative, (<*), (<*>), (*>), optional) |
15 | import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>), parse, IResult(..)) | 16 | import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>)) |
17 | import qualified Data.Attoparsec.ByteString as AS | ||
18 | import qualified Data.Attoparsec.ByteString.Lazy as AL | ||
16 | import Data.Attoparsec.ByteString.Char8 (isDigit_w8, anyChar) | 19 | import Data.Attoparsec.ByteString.Char8 (isDigit_w8, anyChar) |
17 | import Data.Attoparsec.Combinator (manyTill) | 20 | import Data.Attoparsec.Combinator (manyTill) |
18 | import Data.Bits (shiftL) | 21 | import Data.Bits (shiftL) |
@@ -29,11 +32,17 @@ import Data.String (IsString, fromString) | |||
29 | import Data.Word (Word32) | 32 | import Data.Word (Word32) |
30 | 33 | ||
31 | decode :: IsString e => ByteString -> Either e [Armor] | 34 | decode :: IsString e => ByteString -> Either e [Armor] |
32 | decode bs = go (parse parseArmors bs) | 35 | decode 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 | |||
41 | decodeLazy :: IsString e => BL.ByteString -> Either e [Armor] | ||
42 | decodeLazy 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 | ||
38 | parseArmors :: Parser [Armor] | 47 | parseArmors :: Parser [Armor] |
39 | parseArmors = many parseArmor | 48 | parseArmors = many parseArmor |
@@ -110,8 +119,8 @@ aType :: ArmorType -> ByteString | |||
110 | aType (ArmorMessage) = BC8.pack "MESSAGE" | 119 | aType (ArmorMessage) = BC8.pack "MESSAGE" |
111 | aType (ArmorPublicKeyBlock) = BC8.pack "PUBLIC KEY BLOCK" | 120 | aType (ArmorPublicKeyBlock) = BC8.pack "PUBLIC KEY BLOCK" |
112 | aType (ArmorPrivateKeyBlock) = BC8.pack "PRIVATE KEY BLOCK" | 121 | aType (ArmorPrivateKeyBlock) = BC8.pack "PRIVATE KEY BLOCK" |
113 | aType (ArmorSplitMessage x y) = BC8.pack "MESSAGE, PART " `B.append` (l2s x) `B.append` BC8.singleton '/' `B.append` (l2s y) | 122 | aType (ArmorSplitMessage x y) = BC8.pack "MESSAGE, PART " `B.append` l2s x `B.append` BC8.singleton '/' `B.append` l2s y |
114 | aType (ArmorSplitMessageIndefinite x) = BC8.pack "MESSAGE, PART " `B.append` (l2s x) | 123 | aType (ArmorSplitMessageIndefinite x) = BC8.pack "MESSAGE, PART " `B.append` l2s x |
115 | aType (ArmorSignature) = BC8.pack "SIGNATURE" | 124 | aType (ArmorSignature) = BC8.pack "SIGNATURE" |
116 | 125 | ||
117 | l2s :: BL.ByteString -> ByteString | 126 | l2s :: BL.ByteString -> ByteString |