{-# LINE 1 "src/OpenDHT/Internal/InfoHash.chs" #-}
module OpenDHT.Internal.InfoHash where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Marshal.Array as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import Foreign.Ptr
import Foreign.C.Types
import Foreign.C.String
import Foreign.Storable
import Foreign.Marshal.Array
import Foreign.Marshal.Utils
newtype InfoHash = InfoHash { InfoHash -> String
_infoHashString :: String }
deriving (InfoHash -> InfoHash -> Bool
(InfoHash -> InfoHash -> Bool)
-> (InfoHash -> InfoHash -> Bool) -> Eq InfoHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InfoHash -> InfoHash -> Bool
== :: InfoHash -> InfoHash -> Bool
$c/= :: InfoHash -> InfoHash -> Bool
/= :: InfoHash -> InfoHash -> Bool
Eq, Eq InfoHash
Eq InfoHash
-> (InfoHash -> InfoHash -> Ordering)
-> (InfoHash -> InfoHash -> Bool)
-> (InfoHash -> InfoHash -> Bool)
-> (InfoHash -> InfoHash -> Bool)
-> (InfoHash -> InfoHash -> Bool)
-> (InfoHash -> InfoHash -> InfoHash)
-> (InfoHash -> InfoHash -> InfoHash)
-> Ord InfoHash
InfoHash -> InfoHash -> Bool
InfoHash -> InfoHash -> Ordering
InfoHash -> InfoHash -> InfoHash
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: InfoHash -> InfoHash -> Ordering
compare :: InfoHash -> InfoHash -> Ordering
$c< :: InfoHash -> InfoHash -> Bool
< :: InfoHash -> InfoHash -> Bool
$c<= :: InfoHash -> InfoHash -> Bool
<= :: InfoHash -> InfoHash -> Bool
$c> :: InfoHash -> InfoHash -> Bool
> :: InfoHash -> InfoHash -> Bool
$c>= :: InfoHash -> InfoHash -> Bool
>= :: InfoHash -> InfoHash -> Bool
$cmax :: InfoHash -> InfoHash -> InfoHash
max :: InfoHash -> InfoHash -> InfoHash
$cmin :: InfoHash -> InfoHash -> InfoHash
min :: InfoHash -> InfoHash -> InfoHash
Ord)
instance Show InfoHash where
show :: InfoHash -> String
show = InfoHash -> String
_infoHashString
type CInfoHashPtr = C2HSImp.Ptr (CInfoHash)
{-# LINE 36 "src/OpenDHT/Internal/InfoHash.chs" #-}
newtype CInfoHash = CInfoHash { _infoHashData :: Ptr CUChar }
instance Storable CInfoHash where
sizeOf _ = 20
{-# LINE 42 "src/OpenDHT/Internal/InfoHash.chs" #-}
alignment _ = 1
{-# LINE 43 "src/OpenDHT/Internal/InfoHash.chs" #-}
poke p (CInfoHash cPtr) = (\ptr val -> do {C2HSImp.copyArray (ptr `C2HSImp.plusPtr` 0) (val :: (C2HSImp.Ptr C2HSImp.CUChar)) 20}) p cPtr
peek p = CInfoHash <$> (\ptr -> do {return $ ptr `C2HSImp.plusPtr` 0 :: IO (C2HSImp.Ptr C2HSImp.CUChar)}) p
hashHexLen :: Int
hashHexLen :: Int
hashHexLen = Int
40
foreign import ccall "dht_infohash_from_hex" dhtInfohashFromHexC :: CInfoHashPtr -> Ptr CChar -> IO ()
foreign import ccall "dht_infohash_print" dhtInfoHashPrintC :: CInfoHashPtr -> IO (Ptr CChar)
infoHashToString :: CInfoHashPtr -> IO String
infoHashToString :: Ptr CInfoHash -> IO String
infoHashToString Ptr CInfoHash
h = Ptr CInfoHash -> IO (Ptr CChar)
dhtInfoHashPrintC Ptr CInfoHash
h IO (Ptr CChar) -> (Ptr CChar -> IO String) -> IO String
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ptr CChar -> IO String
peekCString
emptyInfoHashWordArray :: [CUChar]
emptyInfoHashWordArray :: [CUChar]
emptyInfoHashWordArray = Int -> CUChar -> [CUChar]
forall a. Int -> a -> [a]
replicate Int
hashHexLen (Char -> CUChar
castCharToCUChar Char
'0')
withCInfohash :: (Ptr CInfoHash -> IO b) -> IO b
withCInfohash :: forall b. (Ptr CInfoHash -> IO b) -> IO b
withCInfohash Ptr CInfoHash -> IO b
f = [CUChar] -> (Ptr CUChar -> IO b) -> IO b
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray [CUChar]
emptyInfoHashWordArray ((Ptr CUChar -> IO b) -> IO b) -> (Ptr CUChar -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \ Ptr CUChar
cucharPtr -> CInfoHash -> (Ptr CInfoHash -> IO b) -> IO b
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with (Ptr CUChar -> CInfoHash
CInfoHash Ptr CUChar
cucharPtr) Ptr CInfoHash -> IO b
f