add stricter partition(3|6) utils that doesn't use wire (and thus no binary)

This commit is contained in:
Vincent Hanquez 2011-01-05 09:24:01 +00:00
parent 857a4a06b8
commit af02dd0839

View file

@ -6,7 +6,6 @@ module Network.TLS.Util
) where
import Network.TLS.Struct (Bytes)
import Network.TLS.Wire
import qualified Data.ByteString as B
sub :: Bytes -> Int -> Int -> Maybe Bytes
@ -20,18 +19,20 @@ takelast i b
| otherwise = Nothing
partition3 :: Bytes -> (Int,Int,Int) -> Maybe (Bytes, Bytes, Bytes)
partition3 bytes (d1,d2,d3) = either (const Nothing) Just $ (flip runGet) bytes $ do
p1 <- getBytes d1
p2 <- getBytes d2
p3 <- getBytes d3
return (p1,p2,p3)
partition3 bytes (d1,d2,d3) = if B.length bytes /= s then Nothing else Just (p1,p2,p3)
where
s = sum [d1,d2,d3]
(p1, r1) = B.splitAt d1 bytes
(p2, r2) = B.splitAt d2 r1
(p3, _) = B.splitAt d3 r2
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)
partition6 bytes (d1,d2,d3,d4,d5,d6) = if B.length bytes /= s then Nothing else Just (p1,p2,p3,p4,p5,p6)
where
s = sum [d1,d2,d3,d4,d5,d6]
(p1, r1) = B.splitAt d1 bytes
(p2, r2) = B.splitAt d2 r1
(p3, r3) = B.splitAt d3 r2
(p4, r4) = B.splitAt d4 r3
(p5, r5) = B.splitAt d5 r4
(p6, _) = B.splitAt d6 r5