Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 100 lines (87 sloc) 2.016 kB
eb4af72 @phonohawk Many improvements
authored
1 module OpenSSL.Utils
2 ( failIfNull
1aabbfa @phonohawk Overhaul for GHC 6.12.1
authored
3 , failIfNull_
eb4af72 @phonohawk Many improvements
authored
4 , failIf
1aabbfa @phonohawk Overhaul for GHC 6.12.1
authored
5 , failIf_
eb4af72 @phonohawk Many improvements
authored
6 , raiseOpenSSLError
948c1b4 @agl Add utility functions to print and read hex numbers
agl authored
7 , toHex
8 , fromHex
f070547 @phonohawk Fix breakage on 64-bit architectures.
authored
9 , peekCStringCLen
eb4af72 @phonohawk Many improvements
authored
10 )
11 where
24f927e @phonohawk Workaround for broken versions of Cabal, including one that comes wit…
authored
12 import Foreign.C.String
13 import Foreign.C.Types
14 import Foreign.Ptr
15 import OpenSSL.ERR
16 import Data.Bits
17 import Data.List
eb4af72 @phonohawk Many improvements
authored
18
19 failIfNull :: Ptr a -> IO (Ptr a)
20 failIfNull ptr
21 = if ptr == nullPtr then
22 raiseOpenSSLError
23 else
24 return ptr
25
1aabbfa @phonohawk Overhaul for GHC 6.12.1
authored
26 failIfNull_ :: Ptr a -> IO ()
27 failIfNull_ ptr
28 = failIfNull ptr >> return ()
eb4af72 @phonohawk Many improvements
authored
29
014bbd0 @phonohawk RSA keys can now be written to an BIO.
authored
30 failIf :: (a -> Bool) -> a -> IO a
31 failIf f a
eb4af72 @phonohawk Many improvements
authored
32 | f a = raiseOpenSSLError
33 | otherwise = return a
34
35
1aabbfa @phonohawk Overhaul for GHC 6.12.1
authored
36 failIf_ :: (a -> Bool) -> a -> IO ()
37 failIf_ f a
38 = failIf f a >> return ()
39
40
eb4af72 @phonohawk Many improvements
authored
41 raiseOpenSSLError :: IO a
42 raiseOpenSSLError = getError >>= errorString >>= fail
948c1b4 @agl Add utility functions to print and read hex numbers
agl authored
43
44 -- | Convert an integer to a hex string
f318105 @bgamari Add Num to constraints with Bits
bgamari authored
45 toHex :: (Num i, Bits i) => i -> String
948c1b4 @agl Add utility functions to print and read hex numbers
agl authored
46 toHex = reverse . map hexByte . unfoldr step where
47 step 0 = Nothing
48 step i = Just (i .&. 0xf, i `shiftR` 4)
49
50 hexByte 0 = '0'
51 hexByte 1 = '1'
52 hexByte 2 = '2'
53 hexByte 3 = '3'
54 hexByte 4 = '4'
55 hexByte 5 = '5'
56 hexByte 6 = '6'
57 hexByte 7 = '7'
58 hexByte 8 = '8'
59 hexByte 9 = '9'
60 hexByte 10 = 'a'
61 hexByte 11 = 'b'
62 hexByte 12 = 'c'
63 hexByte 13 = 'd'
64 hexByte 14 = 'e'
65 hexByte 15 = 'f'
0f5a6a5 @phonohawk Added -Wall to the ghc-options.
authored
66 hexByte _ = undefined
948c1b4 @agl Add utility functions to print and read hex numbers
agl authored
67
68 -- | Convert a hex string to an integer
f318105 @bgamari Add Num to constraints with Bits
bgamari authored
69 fromHex :: (Num i, Bits i) => String -> i
948c1b4 @agl Add utility functions to print and read hex numbers
agl authored
70 fromHex = foldl step 0 where
be44eaa @phonohawk Cosmetic changes suggested by hlint.
authored
71 step acc hexchar = (acc `shiftL` 4) .|. byteHex hexchar
948c1b4 @agl Add utility functions to print and read hex numbers
agl authored
72
73 byteHex '0' = 0
74 byteHex '1' = 1
75 byteHex '2' = 2
76 byteHex '3' = 3
77 byteHex '4' = 4
78 byteHex '5' = 5
79 byteHex '6' = 6
80 byteHex '7' = 7
81 byteHex '8' = 8
82 byteHex '9' = 9
83 byteHex 'a' = 10
84 byteHex 'b' = 11
85 byteHex 'c' = 12
86 byteHex 'd' = 13
87 byteHex 'e' = 14
88 byteHex 'f' = 15
89 byteHex 'A' = 10
90 byteHex 'B' = 11
91 byteHex 'C' = 12
92 byteHex 'D' = 13
93 byteHex 'E' = 14
94 byteHex 'F' = 15
0f5a6a5 @phonohawk Added -Wall to the ghc-options.
authored
95 byteHex _ = undefined
f070547 @phonohawk Fix breakage on 64-bit architectures.
authored
96
97 peekCStringCLen :: (Ptr CChar, CInt) -> IO String
98 peekCStringCLen (p, n)
24f927e @phonohawk Workaround for broken versions of Cabal, including one that comes wit…
authored
99 = peekCStringLen (p, fromIntegral n)
Something went wrong with that request. Please try again.