hs-tls/Network/TLS/Util.hs
2010-09-26 18:51:23 +01:00

29 lines
805 B
Haskell

module Network.TLS.Util
( sub
, takelast
, partition6
) where
import Network.TLS.Struct (Bytes)
import Network.TLS.Wire
import qualified Data.ByteString as B
sub :: Bytes -> Int -> Int -> Maybe Bytes
sub b offset len
| B.length b < offset + len = Nothing
| otherwise = Just $ B.take len $ snd $ B.splitAt offset b
takelast :: Int -> Bytes -> Maybe Bytes
takelast i b
| B.length b >= i = sub b (B.length b - i) i
| otherwise = Nothing
partition6 :: Bytes -> (Int,Int,Int,Int,Int,Int) -> Maybe (Bytes, Bytes, Bytes, Bytes, Bytes, Bytes)
partition6 bytes (d1,d2,d3,d4,d5,d6) = either (const Nothing) Just $ (flip runGet) bytes $ do
p1 <- getBytes d1
p2 <- getBytes d2
p3 <- getBytes d3
p4 <- getBytes d4
p5 <- getBytes d5
p6 <- getBytes d6
return (p1,p2,p3,p4,p5,p6)