From fdae88dea011b7e000d68b8b82ebf682f09b3947 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 9 May 2014 20:29:22 -0400 Subject: pemParser --- PEM.hs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 PEM.hs diff --git a/PEM.hs b/PEM.hs new file mode 100644 index 0000000..e6c8259 --- /dev/null +++ b/PEM.hs @@ -0,0 +1,34 @@ +{-# LANGUAGE OverloadedStrings #-} +module PEM where + +import Data.Maybe +import Data.Monoid +import qualified Data.ByteString.Lazy as LW +import qualified Data.ByteString.Lazy.Char8 as L +import Control.Monad +import Control.Applicative +import qualified Codec.Binary.Base64 as Base64 +import ScanningParser + +data PEMBlob = PEMBlob { pemType :: L.ByteString + , pemBlob :: L.ByteString + } + +pemParser mtyp = ScanningParser (maybe fndany fndtyp mtyp) pbdy + where + hdr typ = "-----BEGIN " <> typ <> "-----" + fndtyp typ bs = if bs==hdr typ then Just typ else Nothing + fndany bs = do + guard $ "-----BEGIN " `L.isPrefixOf` bs + let x0 = L.drop 11 bs + guard $ "-----" `LW.isSuffixOf` x0 + let typ = L.take (L.length x0 - 5) x0 + return typ + + pbdy typ xs = (mblob, rs) + where + (ys,rs) = span (/="-----END " <> typ <> "-----") xs + mblob = PEMBlob typ <$> LW.pack <$> Base64.decode (L.unpack dta) + dta = case ys of + _:dta_lines -> L.concat dta_lines + [] -> "" -- cgit v1.2.3