-- |
--
-- Module      : Raaz.Core.Util.ByteString
-- Copyright   : (c) Piyush P Kurur, 2019
-- License     : Apache-2.0 OR BSD-3-Clause
-- Maintainer  : Piyush P Kurur <ppk@iitpkd.ac.in>
-- Stability   : experimental
--

{-# LANGUAGE FlexibleContexts #-}
module Raaz.Core.Util.ByteString
       ( length, replicate
       , create, createFrom
       , withByteString
       , unsafeCopyToPointer
       , unsafeNCopyToPointer
       ) where

import           Prelude            hiding (length, replicate)
import qualified Data.ByteString    as B
import           Data.ByteString    (ByteString)
import qualified Data.ByteString.Internal as BI
import           Data.Word
import           Foreign.ForeignPtr (withForeignPtr)
import           Foreign.Ptr        (castPtr, plusPtr)

import           Raaz.Core.Types.Pointer
import           Raaz.Core.Types.Copying

-- | A typesafe length for Bytestring
length :: ByteString -> BYTES Int
length :: ByteString -> BYTES Int
length = Int -> BYTES Int
forall a. a -> BYTES a
BYTES (Int -> BYTES Int)
-> (ByteString -> Int) -> ByteString -> BYTES Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
B.length

-- | A type safe version of replicate
replicate :: LengthUnit l => l -> Word8 -> ByteString
replicate :: l -> Word8 -> ByteString
replicate l
l = Int -> Word8 -> ByteString
B.replicate Int
sz
  where BYTES Int
sz = l -> BYTES Int
forall u. LengthUnit u => u -> BYTES Int
inBytes l
l

-- | Copy the bytestring to the crypto buffer. This operation leads to
-- undefined behaviour if the crypto pointer points to an area smaller
-- than the size of the byte string.
unsafeCopyToPointer :: Pointer ptr
                    => ByteString   -- ^ The source.
                    -> ptr a        -- ^ The destination.
                    -> IO ()
unsafeCopyToPointer :: ByteString -> ptr a -> IO ()
unsafeCopyToPointer ByteString
bs ptr a
cptr =  ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$
           \ Ptr Word8
p -> Dest (ptr a) -> Src (Ptr Any) -> BYTES Int -> IO ()
forall l (ptrS :: * -> *) (ptrD :: * -> *) dest src.
(LengthUnit l, Pointer ptrS, Pointer ptrD) =>
Dest (ptrD dest) -> Src (ptrS src) -> l -> IO ()
memcpy (ptr a -> Dest (ptr a)
forall a. a -> Dest a
destination ptr a
cptr) (Ptr Any -> Src (Ptr Any)
forall a. a -> Src a
source (Ptr Any -> Src (Ptr Any)) -> Ptr Any -> Src (Ptr Any)
forall a b. (a -> b) -> a -> b
$ Ptr Word8
p Ptr Word8 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
offset) (Int -> BYTES Int
forall a. a -> BYTES a
BYTES Int
n)
    where (ForeignPtr Word8
fptr, Int
offset,Int
n) = ByteString -> (ForeignPtr Word8, Int, Int)
BI.toForeignPtr ByteString
bs


-- | Similar to `unsafeCopyToPointer` but takes an additional input
-- @n@ which is the number of bytes (expressed in type safe length
-- units) to transfer. This operation leads to undefined behaviour if
-- either the bytestring is shorter than @n@ or the crypto pointer
-- points to an area smaller than @n@.
unsafeNCopyToPointer :: LengthUnit n
                     => n              -- ^ length of data to be copied
                     -> ByteString     -- ^ The source byte string
                     -> Ptr a         -- ^ The buffer
                     -> IO ()
unsafeNCopyToPointer :: n -> ByteString -> Ptr a -> IO ()
unsafeNCopyToPointer n
n ByteString
bs Ptr a
cptr = ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$
           \ Ptr Word8
p -> Dest (Ptr a) -> Src (Ptr Any) -> n -> IO ()
forall l (ptrS :: * -> *) (ptrD :: * -> *) dest src.
(LengthUnit l, Pointer ptrS, Pointer ptrD) =>
Dest (ptrD dest) -> Src (ptrS src) -> l -> IO ()
memcpy (Ptr a -> Dest (Ptr a)
forall a. a -> Dest a
destination Ptr a
cptr) (Ptr Any -> Src (Ptr Any)
forall a. a -> Src a
source (Ptr Any -> Src (Ptr Any)) -> Ptr Any -> Src (Ptr Any)
forall a b. (a -> b) -> a -> b
$ Ptr Word8
p Ptr Word8 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
offset) n
n
    where (ForeignPtr Word8
fptr, Int
offset,Int
_) = ByteString -> (ForeignPtr Word8, Int, Int)
BI.toForeignPtr ByteString
bs

-- | Works directly on the pointer associated with the
-- `ByteString`. This function should only read and not modify the
-- contents of the pointer.
withByteString :: ByteString -> (Ptr something -> IO a) -> IO a
withByteString :: ByteString -> (Ptr something -> IO a) -> IO a
withByteString ByteString
bs Ptr something -> IO a
f = ForeignPtr Word8 -> (Ptr Word8 -> IO a) -> IO a
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr (Ptr something -> IO a
f (Ptr something -> IO a)
-> (Ptr Word8 -> Ptr something) -> Ptr Word8 -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr Word8 -> Int -> Ptr something)
-> Int -> Ptr Word8 -> Ptr something
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr Word8 -> Int -> Ptr something
forall a b. Ptr a -> Int -> Ptr b
plusPtr Int
off)
  where (ForeignPtr Word8
fptr, Int
off, Int
_) = ByteString -> (ForeignPtr Word8, Int, Int)
BI.toForeignPtr ByteString
bs

-- | The action @create l act@ creates a length @l@ bytestring where
-- the contents are filled using the the @act@ to fill the buffer.
create :: LengthUnit l => l -> (Ptr a -> IO ()) -> IO ByteString
create :: l -> (Ptr a -> IO ()) -> IO ByteString
create l
l Ptr a -> IO ()
act = (Ptr Word8 -> IO ()) -> IO ByteString
myCreate (Ptr a -> IO ()
act (Ptr a -> IO ()) -> (Ptr Word8 -> Ptr a) -> Ptr Word8 -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr Word8 -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr)
  where myCreate :: (Ptr Word8 -> IO ()) -> IO ByteString
myCreate =  Int -> (Ptr Word8 -> IO ()) -> IO ByteString
BI.create (Int -> (Ptr Word8 -> IO ()) -> IO ByteString)
-> Int -> (Ptr Word8 -> IO ()) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ BYTES Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (BYTES Int -> Int) -> BYTES Int -> Int
forall a b. (a -> b) -> a -> b
$ l -> BYTES Int
forall u. LengthUnit u => u -> BYTES Int
inBytes l
l

-- | The IO action @createFrom n cptr@ creates a bytestring by copying
-- @n@ bytes from the pointer @cptr@.
createFrom :: LengthUnit l => l -> Ptr a -> IO ByteString
createFrom :: l -> Ptr a -> IO ByteString
createFrom l
l Ptr a
cptr = l -> (Ptr Any -> IO ()) -> IO ByteString
forall l a. LengthUnit l => l -> (Ptr a -> IO ()) -> IO ByteString
create l
l Ptr Any -> IO ()
forall (ptrD :: * -> *) dest. Pointer ptrD => ptrD dest -> IO ()
filler
  where filler :: ptrD dest -> IO ()
filler ptrD dest
dptr = Dest (ptrD dest) -> Src (Ptr a) -> l -> IO ()
forall l (ptrS :: * -> *) (ptrD :: * -> *) dest src.
(LengthUnit l, Pointer ptrS, Pointer ptrD) =>
Dest (ptrD dest) -> Src (ptrS src) -> l -> IO ()
memcpy (ptrD dest -> Dest (ptrD dest)
forall a. a -> Dest a
destination ptrD dest
dptr) (Ptr a -> Src (Ptr a)
forall a. a -> Src a
source Ptr a
cptr) l
l

----------------------  Hexadecimal encoding. -----------------------------------