diff options
Diffstat (limited to 'lib/PEM.hs')
-rw-r--r-- | lib/PEM.hs | 12 |
1 files changed, 11 insertions, 1 deletions
@@ -1,4 +1,5 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | 1 | {-# LANGUAGE OverloadedStrings #-} |
2 | {-# LANGUAGE CPP #-} | ||
2 | module PEM where | 3 | module PEM where |
3 | 4 | ||
4 | import Data.Monoid | 5 | import Data.Monoid |
@@ -6,9 +7,14 @@ import qualified Data.ByteString.Lazy as LW | |||
6 | import qualified Data.ByteString.Lazy.Char8 as L | 7 | import qualified Data.ByteString.Lazy.Char8 as L |
7 | import Control.Monad | 8 | import Control.Monad |
8 | import Control.Applicative | 9 | import Control.Applicative |
10 | #if defined(VERSION_memory) | ||
11 | import qualified Data.ByteString.Char8 as S8 | ||
12 | import Data.ByteArray.Encoding | ||
13 | #elif defined(VERSION_dataenc) | ||
9 | import qualified Codec.Binary.Base64 as Base64 | 14 | import qualified Codec.Binary.Base64 as Base64 |
15 | #endif | ||
10 | import ScanningParser | 16 | import ScanningParser |
11 | 17 | import FunctorToMaybe | |
12 | data PEMBlob = PEMBlob { pemType :: L.ByteString | 18 | data PEMBlob = PEMBlob { pemType :: L.ByteString |
13 | , pemBlob :: L.ByteString | 19 | , pemBlob :: L.ByteString |
14 | } | 20 | } |
@@ -28,7 +34,11 @@ pemParser mtyp = ScanningParser (maybe fndany fndtyp mtyp) pbdy | |||
28 | pbdy typ xs = (mblob, drop 1 rs) | 34 | pbdy typ xs = (mblob, drop 1 rs) |
29 | where | 35 | where |
30 | (ys,rs) = span (/="-----END " <> typ <> "-----") xs | 36 | (ys,rs) = span (/="-----END " <> typ <> "-----") xs |
37 | #if defined(VERSION_memory) | ||
38 | mblob = PEMBlob typ <$> LW.fromStrict <$> (functorToMaybe $ convertFromBase Base64 $ L.toStrict dta) | ||
39 | #elif defined(VERSION_dataenc) | ||
31 | mblob = PEMBlob typ <$> LW.pack <$> Base64.decode (L.unpack dta) | 40 | mblob = PEMBlob typ <$> LW.pack <$> Base64.decode (L.unpack dta) |
41 | #endif | ||
32 | dta = case ys of | 42 | dta = case ys of |
33 | [] -> "" | 43 | [] -> "" |
34 | dta_lines -> L.concat dta_lines | 44 | dta_lines -> L.concat dta_lines |