-- GENERATED by C->Haskell Compiler, version 0.28.8 Switcheroo, 25 November 2017 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "src/OpenDHT/Internal/InfoHash.chs" #-}

{-|
  Module      : OpenDHT.Internal.InfoHash
  Description : Internal definitions for InfoHash.
  Copyright   : (c) Simon Désaulniers, 2025
  License     : GPL-3

  Maintainer  : sim.desaulniers@gmail.com
-}

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



{-| This type represents a 160-bit string of bytes consistent with the SHA1
   specification.

   It is at the heart of this library and is used as arguments for the get/put
   functions.
-}
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 synonym for C-bindings. Not meant to be used by the library user.
-}
type CInfoHashPtr = C2HSImp.Ptr (CInfoHash)
{-# LINE 36 "src/OpenDHT/Internal/InfoHash.chs" #-}


-- struct OPENDHT_C_PUBLIC dht_infohash { uint8_t d[HASH_LEN]; };
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)

{-| Get the string representation of an InfoHash.

   For example:

   > unDht $ do
   >   h <- infoHashFromHex "fae0f12f4f2f5eaaadceff21987ff1f121ff478f"
   >   liftIO $ print $ show h

   would yield the following string:

   > fae0f12f4f2f5eaaadceff21987ff1f121ff478f
-}
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

--  vim: set sts=2 ts=2 sw=2 tw=120 et :