diff options
Diffstat (limited to 'Codec/Encryption/OpenPGP')
-rw-r--r-- | Codec/Encryption/OpenPGP/ASCIIArmor.hs | 4 | ||||
-rw-r--r-- | Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs | 26 |
2 files changed, 19 insertions, 11 deletions
diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor.hs b/Codec/Encryption/OpenPGP/ASCIIArmor.hs index 74c8c69..075a481 100644 --- a/Codec/Encryption/OpenPGP/ASCIIArmor.hs +++ b/Codec/Encryption/OpenPGP/ASCIIArmor.hs | |||
@@ -5,9 +5,9 @@ | |||
5 | 5 | ||
6 | module Codec.Encryption.OpenPGP.ASCIIArmor ( | 6 | module Codec.Encryption.OpenPGP.ASCIIArmor ( |
7 | armor | 7 | armor |
8 | , decodeArmor | 8 | , decode |
9 | , parseArmor | 9 | , parseArmor |
10 | ) where | 10 | ) where |
11 | 11 | ||
12 | import Codec.Encryption.OpenPGP.ASCIIArmor.Encode (armor) | 12 | import Codec.Encryption.OpenPGP.ASCIIArmor.Encode (armor) |
13 | import Codec.Encryption.OpenPGP.ASCIIArmor.Decode (decodeArmor, parseArmor) | 13 | import Codec.Encryption.OpenPGP.ASCIIArmor.Decode (decode, parseArmor) |
diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs b/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs index 8c2a8a3..e69087c 100644 --- a/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs +++ b/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs | |||
@@ -6,13 +6,14 @@ | |||
6 | 6 | ||
7 | module Codec.Encryption.OpenPGP.ASCIIArmor.Decode ( | 7 | module Codec.Encryption.OpenPGP.ASCIIArmor.Decode ( |
8 | parseArmor | 8 | parseArmor |
9 | , decodeArmor | 9 | , decode |
10 | ) where | 10 | ) where |
11 | 11 | ||
12 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types | 12 | import Codec.Encryption.OpenPGP.ASCIIArmor.Types |
13 | import Control.Applicative (many, (<|>), (<$>)) | 13 | import Control.Applicative (many, (<|>), (<$>), Alternative, (*>)) |
14 | import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>), parse, IResult(..)) | 14 | import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>), parse, IResult(..)) |
15 | import Data.Attoparsec.ByteString.Char8 (isDigit_w8) | 15 | import Data.Attoparsec.ByteString.Char8 (isDigit_w8, anyChar) |
16 | import Data.Attoparsec.Combinator (manyTill) | ||
16 | import Data.Bits (shiftL) | 17 | import Data.Bits (shiftL) |
17 | import Data.ByteString (ByteString) | 18 | import Data.ByteString (ByteString) |
18 | import qualified Data.ByteString as B | 19 | import qualified Data.ByteString as B |
@@ -25,15 +26,19 @@ import Data.Serialize.Put (runPut, putWord32be) | |||
25 | import Data.String (IsString, fromString) | 26 | import Data.String (IsString, fromString) |
26 | import Data.Word (Word32) | 27 | import Data.Word (Word32) |
27 | 28 | ||
28 | decodeArmor :: (Integral a, Read a, Show a, IsString e) => ByteString -> Either e (Armor a) | 29 | decode :: (Integral a, Read a, Show a, IsString e) => ByteString -> Either e ([Armor a]) |
29 | decodeArmor bs = case parse parseArmor bs of | 30 | decode bs = go (parse parseArmors bs) |
30 | Fail t c e -> Left (fromString e) | 31 | where |
31 | Partial _ -> Left (fromString "what") | 32 | go (Fail t c e) = Left (fromString e) |
32 | Done _ r -> Right r | 33 | go (Partial cont) = go (cont B.empty) |
34 | go (Done _ r) = Right r | ||
35 | |||
36 | parseArmors :: (Integral a, Read a, Show a) => Parser ([Armor a]) | ||
37 | parseArmors = many parseArmor | ||
33 | 38 | ||
34 | parseArmor :: (Integral a, Read a, Show a) => Parser (Armor a) | 39 | parseArmor :: (Integral a, Read a, Show a) => Parser (Armor a) |
35 | parseArmor = do | 40 | parseArmor = do |
36 | atype <- beginLine <?> "begin line" | 41 | atype <- prefixed beginLine <?> "begin line" |
37 | headers <- armorHeaders <?> "headers" | 42 | headers <- armorHeaders <?> "headers" |
38 | blankishLine <?> "blank line" | 43 | blankishLine <?> "blank line" |
39 | payload <- base64Data <?> "base64 data" | 44 | payload <- base64Data <?> "base64 data" |
@@ -126,3 +131,6 @@ d24 = do | |||
126 | b <- getWord8 | 131 | b <- getWord8 |
127 | c <- getWord8 | 132 | c <- getWord8 |
128 | return $ shiftL (fromIntegral a :: Word32) 16 + shiftL (fromIntegral b :: Word32) 8 + (fromIntegral c :: Word32) | 133 | return $ shiftL (fromIntegral a :: Word32) 16 + shiftL (fromIntegral b :: Word32) 8 + (fromIntegral c :: Word32) |
134 | |||
135 | prefixed :: Parser a -> Parser a | ||
136 | prefixed end = end <|> anyChar *> prefixed end | ||