summaryrefslogtreecommitdiff
path: root/Data/OpenPGP/Internal.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Data/OpenPGP/Internal.hs')
-rw-r--r--Data/OpenPGP/Internal.hs48
1 files changed, 27 insertions, 21 deletions
diff --git a/Data/OpenPGP/Internal.hs b/Data/OpenPGP/Internal.hs
index 24330f1..175a62e 100644
--- a/Data/OpenPGP/Internal.hs
+++ b/Data/OpenPGP/Internal.hs
@@ -1,17 +1,20 @@
1{-# LANGUAGE BangPatterns #-} 1{-# LANGUAGE BangPatterns, MagicHash #-}
2module Data.OpenPGP.Internal where 2module Data.OpenPGP.Internal where
3 3
4import Data.Bits 4import Data.Bits
5import qualified Data.ByteString as BS 5import qualified Data.ByteString as BS
6import qualified Data.ByteString.Internal as BS 6import qualified Data.ByteString.Internal as BS
7import Data.Word 7import Data.Word
8import Foreign.ForeignPtr 8import Foreign.ForeignPtr
9import Foreign.Ptr 9import Foreign.Ptr
10import Foreign.Storable 10import Foreign.Storable
11import GHC.Exts
12import GHC.Integer.GMP.Internals
11import System.Endian 13import System.Endian
12import System.IO.Unsafe 14import System.IO.Unsafe
13 15
14 16
17
15decode_s2k_count :: Word8 -> Word32 18decode_s2k_count :: Word8 -> Word32
16decode_s2k_count c = (16 + (fromIntegral c .&. 15)) `shiftL` 19decode_s2k_count c = (16 + (fromIntegral c .&. 15)) `shiftL`
17 ((fromIntegral c `shiftR` 4) + 6) 20 ((fromIntegral c `shiftR` 4) + 6)
@@ -28,6 +31,7 @@ encode_s2k_count iterations
28 | count < 32 = (count, c) 31 | count < 32 = (count, c)
29 | otherwise = encode_s2k_count' (count `shiftR` 1) (c+1) 32 | otherwise = encode_s2k_count' (count `shiftR` 1) (c+1)
30 33
34{-
31getBigNum :: BS.ByteString -> Integer 35getBigNum :: BS.ByteString -> Integer
32getBigNum bytes = unsafeDupablePerformIO $ 36getBigNum bytes = unsafeDupablePerformIO $
33 let (fptr,offset,len) = BS.toForeignPtr bytes 37 let (fptr,offset,len) = BS.toForeignPtr bytes
@@ -49,23 +53,25 @@ getBigNum bytes = unsafeDupablePerformIO $
49 go2 (p `plusPtr` 1) a' 53 go2 (p `plusPtr` 1) a'
50 a <- go p 0 54 a <- go p 0
51 go2 (castPtr e :: Ptr Word8) a 55 go2 (castPtr e :: Ptr Word8) a
56-}
57getBigNum :: BS.ByteString -> Integer
58getBigNum bytes = unsafeDupablePerformIO $
59 let (fptr,offset,len) = BS.toForeignPtr bytes
60 in withForeignPtr fptr $ \ptr -> do
61 let Ptr addr = ptr `plusPtr` offset :: Ptr Word64
62 I# n = len
63 importIntegerFromAddr addr (int2Word# n) 1#
52 64
53{- 65putBigNum :: Integer -> (Word16, BS.ByteString)
54unchunk :: L.ByteString -> BS.ByteString 66putBigNum 0 = (1,BS.singleton 0)
55unchunk b = L.toStrict $ foldr reappend L.empty $ L.toChunks b 67putBigNum i = ( (fromIntegral (BS.length bytes) - 1) * 8 + sigBit
56 68 , bytes )
57reappend :: BS.ByteString -> BS.ByteString -> Maybe BS.ByteString 69 where
58reappend a b = 70 sigBit = fromIntegral $ 8 - countLeadingZeros (BS.index bytes 0)
59 let (ap,ao,al) = BS.toForeignPtr a 71 bytes = integerToBS i
60 (bp,bo,bl) = BS.toForeignPtr b
61 in if ap == bp && ao+al == bo
62 then Just $ BS.PS ap ao (al+bl)
63 else Nothing
64 72
65reconsChunk :: S.ByteString -> L.ByteString -> L.ByteString 73integerToBS :: Integer -> BS.ByteString
66reconsChunk b bs = case L.toChunks bs of 74integerToBS i = BS.unsafeCreate (I# (word2Int# (sizeInBaseInteger i 256#))) $ \ptr -> do
67 (c:cs) -> case reappend b c of 75 let Ptr addr = ptr
68 Just x -> L.fromChunks (x:cs) 76 cnt <- exportIntegerToAddr i addr 1#
69 Nothing -> L.fromChunks (b:c:cs) 77 return ()
70 _ -> L.fromChunks [b]
71-}