module Crypto.Number.Serialize
( i2osp
, os2ip
, i2ospOf
, i2ospOf_
, lengthBytes
) where
import Data.Bits
import Data.Word
import Foreign.Storable
import Foreign.Ptr
import Crypto.Number.Compat
import Crypto.Internal.Compat (unsafeDoIO)
import qualified Crypto.Internal.ByteArray as B
import Data.Memory.PtrMethods
divMod256 :: Integer -> (Integer, Word8)
divMod256 n = (n `shiftR` 8, fromIntegral n)
os2ip :: B.ByteArrayAccess ba => ba -> Integer
os2ip bs = unsafeDoIO $ B.withByteArray bs (loop 0 0)
where
len = B.length bs
loop :: Integer -> Int -> Ptr Word8 -> IO Integer
loop !acc i p
| i == len = return acc
| otherwise = do
w <- peekByteOff p i :: IO Word8
loop ((acc `shiftL` 8) .|. fromIntegral w) (i+1) p
i2osp :: B.ByteArray ba => Integer -> ba
i2osp 0 = B.allocAndFreeze 1 $ \p -> pokeByteOff p 0 (0 :: Word8)
i2osp m = B.allocAndFreeze sz (\p -> fillPtr p >> return ())
where
!sz = lengthBytes m
fillPtr p = gmpExportInteger m p `onGmpUnsupported` export p (sz1) m
export p ofs i
| ofs == 0 = pokeByteOff p ofs (fromIntegral i :: Word8)
| otherwise = do
let (i', b) = divMod256 i
pokeByteOff p ofs b
export p (ofs1) i'
i2ospOf :: B.ByteArray ba => Int -> Integer -> Maybe ba
i2ospOf 0 _ = error "cannot create integer serialization in 0 bytes"
i2ospOf len 0 = Just $ B.allocAndFreeze len $ \p -> memSet p 0 len
i2ospOf len m
| sz > len = Nothing
| otherwise = Just $ B.allocAndFreeze len $ \p -> memSet p 0 len >> fillPtr (p `plusPtr` (len sz))
where
!sz = lengthBytes m
fillPtr p = gmpExportInteger m p `onGmpUnsupported` export p (sz1) m
export p ofs i
| ofs == 0 = pokeByteOff p ofs (fromIntegral i :: Word8)
| otherwise = do
let (i', b) = divMod256 i
pokeByteOff p ofs b
export p (ofs1) i'
i2ospOf_ :: B.ByteArray ba => Int -> Integer -> ba
i2ospOf_ len = maybe (error "i2ospOf_: integer is larger than expected") id . i2ospOf len
lengthBytes :: Integer -> Int
lengthBytes n = gmpSizeInBytes n `onGmpUnsupported` nbBytes n
where
nbBytes !v
| v < 256 = 1
| otherwise = 1 + nbBytes (v `shiftR` 8)