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.hs17
1 files changed, 8 insertions, 9 deletions
diff --git a/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs b/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs
index 64f7236..8c2a8a3 100644
--- a/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs
+++ b/Codec/Encryption/OpenPGP/ASCIIArmor/Decode.hs
@@ -1,7 +1,7 @@
1{-# LANGUAGE OverloadedStrings #-} 1{-# LANGUAGE OverloadedStrings #-}
2-- ASCIIArmor/Decode.hs: OpenPGP (RFC4880) ASCII armor implementation 2-- ASCIIArmor/Decode.hs: OpenPGP (RFC4880) ASCII armor implementation
3-- Copyright Ⓒ 2012 Clint Adams 3-- Copyright Ⓒ 2012 Clint Adams
4-- This software is released under the terms of the Expat (MIT) license. 4-- This software is released under the terms of the ISC license.
5-- (See the LICENSE file). 5-- (See the LICENSE file).
6 6
7module Codec.Encryption.OpenPGP.ASCIIArmor.Decode ( 7module Codec.Encryption.OpenPGP.ASCIIArmor.Decode (
@@ -9,8 +9,7 @@ module Codec.Encryption.OpenPGP.ASCIIArmor.Decode (
9 , decodeArmor 9 , decodeArmor
10) where 10) where
11 11
12import Codec.Encryption.OpenPGP.Serialize () 12import Codec.Encryption.OpenPGP.ASCIIArmor.Types
13import Codec.Encryption.OpenPGP.Types
14import Control.Applicative (many, (<|>), (<$>)) 13import Control.Applicative (many, (<|>), (<$>))
15import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>), parse, IResult(..)) 14import Data.Attoparsec.ByteString (Parser, many1, string, inClass, notInClass, satisfy, word8, (<?>), parse, IResult(..))
16import Data.Attoparsec.ByteString.Char8 (isDigit_w8) 15import Data.Attoparsec.ByteString.Char8 (isDigit_w8)
@@ -39,9 +38,7 @@ parseArmor = do
39 blankishLine <?> "blank line" 38 blankishLine <?> "blank line"
40 payload <- base64Data <?> "base64 data" 39 payload <- base64Data <?> "base64 data"
41 endLine atype <?> "end line" 40 endLine atype <?> "end line"
42 case runGet get payload of 41 return $ Armor atype headers payload
43 Left err -> fail err
44 Right packets -> return $ Armor atype headers (unBlock packets)
45 42
46beginLine :: (Integral a, Read a, Show a) => Parser (ArmorType a) 43beginLine :: (Integral a, Read a, Show a) => Parser (ArmorType a)
47beginLine = do 44beginLine = do
@@ -64,16 +61,18 @@ beginLine = do
64lineEnding :: Parser ByteString 61lineEnding :: Parser ByteString
65lineEnding = string "\n" <|> string "\r\n" 62lineEnding = string "\n" <|> string "\r\n"
66 63
67armorHeaders :: Parser [ArmorHeader] 64armorHeaders :: Parser [(String, String)]
68armorHeaders = many armorHeader 65armorHeaders = many armorHeader
69 66
70armorHeader :: Parser ArmorHeader 67armorHeader :: Parser (String, String)
71armorHeader = do 68armorHeader = do
72 key <- many1 (satisfy (inClass "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")) 69 key <- many1 (satisfy (inClass "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"))
73 string ": " 70 string ": "
74 val <- many1 (satisfy (notInClass "\n\r")) 71 val <- many1 (satisfy (notInClass "\n\r"))
75 lineEnding 72 lineEnding
76 return (B.pack key, B.pack val) 73 return (w8sToString key, w8sToString val)
74 where
75 w8sToString = BC8.unpack . B.pack
77 76
78blankishLine :: Parser ByteString 77blankishLine :: Parser ByteString
79blankishLine = many (satisfy (inClass " \t")) >> lineEnding 78blankishLine = many (satisfy (inClass " \t")) >> lineEnding