From af02dd08396c9c7ea53e22d6c492cf9cc799891d Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Wed, 5 Jan 2011 09:24:01 +0000 Subject: [PATCH] add stricter partition(3|6) utils that doesn't use wire (and thus no binary) --- Network/TLS/Util.hs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Network/TLS/Util.hs b/Network/TLS/Util.hs index 2015f76..32aff1a 100644 --- a/Network/TLS/Util.hs +++ b/Network/TLS/Util.hs @@ -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