diff options
Diffstat (limited to 'Data/OpenPGP/Internal.hs')
-rw-r--r-- | Data/OpenPGP/Internal.hs | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/Data/OpenPGP/Internal.hs b/Data/OpenPGP/Internal.hs index 175a62e..f9a8803 100644 --- a/Data/OpenPGP/Internal.hs +++ b/Data/OpenPGP/Internal.hs | |||
@@ -16,20 +16,20 @@ import System.IO.Unsafe | |||
16 | 16 | ||
17 | 17 | ||
18 | decode_s2k_count :: Word8 -> Word32 | 18 | decode_s2k_count :: Word8 -> Word32 |
19 | decode_s2k_count c = (16 + (fromIntegral c .&. 15)) `shiftL` | 19 | decode_s2k_count c = shiftL (16 + (fromIntegral c .&. 15)) |
20 | ((fromIntegral c `shiftR` 4) + 6) | 20 | ((fromIntegral c `shiftR` 4) + 6) |
21 | 21 | ||
22 | encode_s2k_count :: Word32 -> Word8 | 22 | encode_s2k_count :: Word32 -> Word8 |
23 | encode_s2k_count iterations | 23 | encode_s2k_count iterations |
24 | | iterations >= 65011712 = 255 | 24 | | iterations >= 65011712 = 255 |
25 | | decode_s2k_count result < iterations = result+1 | 25 | | decode_s2k_count result < iterations = result+1 |
26 | | otherwise = result | 26 | | otherwise = result |
27 | where | 27 | where |
28 | result = fromIntegral $ (fromIntegral c `shiftL` 4) .|. (count - 16) | 28 | result = fromIntegral $ (fromIntegral c `shiftL` 4) .|. (count - 16) |
29 | (count, c) = encode_s2k_count' (iterations `shiftR` 6) (0::Word8) | 29 | (count, c) = encode_s2k_count' (iterations `shiftR` 6) (0::Word8) |
30 | encode_s2k_count' count c | 30 | encode_s2k_count' count c |
31 | | count < 32 = (count, c) | 31 | | count < 32 = (count, c) |
32 | | otherwise = encode_s2k_count' (count `shiftR` 1) (c+1) | 32 | | otherwise = encode_s2k_count' (count `shiftR` 1) (c+1) |
33 | 33 | ||
34 | {- | 34 | {- |
35 | getBigNum :: BS.ByteString -> Integer | 35 | getBigNum :: BS.ByteString -> Integer |
@@ -70,8 +70,26 @@ putBigNum i = ( (fromIntegral (BS.length bytes) - 1) * 8 + sigBit | |||
70 | sigBit = fromIntegral $ 8 - countLeadingZeros (BS.index bytes 0) | 70 | sigBit = fromIntegral $ 8 - countLeadingZeros (BS.index bytes 0) |
71 | bytes = integerToBS i | 71 | bytes = integerToBS i |
72 | 72 | ||
73 | -- big-endian | ||
73 | integerToBS :: Integer -> BS.ByteString | 74 | integerToBS :: Integer -> BS.ByteString |
74 | integerToBS i = BS.unsafeCreate (I# (word2Int# (sizeInBaseInteger i 256#))) $ \ptr -> do | 75 | integerToBS i = BS.unsafeCreate (I# (word2Int# (sizeInBaseInteger i 256#))) $ \ptr -> do |
75 | let Ptr addr = ptr | 76 | let Ptr addr = ptr |
76 | cnt <- exportIntegerToAddr i addr 1# | 77 | cnt <- exportIntegerToAddr i addr 1# -- 1# for big-endian (use 0# for little-endian) |
77 | return () | 78 | return () |
79 | |||
80 | {- | ||
81 | getBigNumLE :: BS.ByteString -> Integer | ||
82 | getBigNumLE bytes = unsafeDupablePerformIO $ | ||
83 | let (fptr,offset,len) = BS.toForeignPtr bytes | ||
84 | in withForeignPtr fptr $ \ptr -> do | ||
85 | let Ptr addr = ptr `plusPtr` offset :: Ptr Word64 | ||
86 | I# n = len | ||
87 | importIntegerFromAddr addr (int2Word# n) 0# | ||
88 | |||
89 | -- little-endian | ||
90 | integerToLE :: Integer -> BS.ByteString | ||
91 | integerToLE i = BS.unsafeCreate (I# (word2Int# (sizeInBaseInteger i 256#))) $ \ptr -> do | ||
92 | let Ptr addr = ptr | ||
93 | cnt <- exportIntegerToAddr i addr 0# | ||
94 | return () | ||
95 | -} | ||