From 46313be8762a67d7b77d2315d3123c5f559a6734 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone <exarkun@twistedmatrix.com> Date: Tue, 16 May 2023 10:26:48 -0400 Subject: [PATCH] Get rid of the repeated bytesRead requests We use `isolate` so we know exactly how far we've advanced, we can trivially compute the correct position. --- src/Tahoe/SDMF/Internal/Share.hs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/Tahoe/SDMF/Internal/Share.hs b/src/Tahoe/SDMF/Internal/Share.hs index e01b8b4..f62cc96 100644 --- a/src/Tahoe/SDMF/Internal/Share.hs +++ b/src/Tahoe/SDMF/Internal/Share.hs @@ -144,23 +144,18 @@ instance Binary Share where encryptedPrivateKeyOffset <- getWord64be eofOffset <- getWord64be - pos <- bytesRead - shareVerificationKey <- Keys.Verification <$> isolate (fromIntegral signatureOffset - fromIntegral pos) getSubjectPublicKeyInfo - - pos <- bytesRead - shareSignature <- getByteString (fromIntegral hashChainOffset - fromIntegral pos) - - pos <- bytesRead - shareHashChain <- isolate (fromIntegral blockHashTreeOffset - fromIntegral pos) get - - pos <- bytesRead - shareBlockHashTree <- isolate (fromIntegral shareDataOffset - fromIntegral pos) get - - pos <- bytesRead - shareData <- getLazyByteString (fromIntegral encryptedPrivateKeyOffset - fromIntegral pos) - - pos <- bytesRead - shareEncryptedPrivateKey <- getByteString (fromIntegral eofOffset - fromIntegral pos) + -- This offset is not the encoded share but it's defined as being + -- right where we've read to. Give it a name that follows the + -- pattern. + shareVerificationOffset <- bytesRead + + -- Read in the values between all those offsets. + shareVerificationKey <- Keys.Verification <$> isolate (fromIntegral signatureOffset - fromIntegral shareVerificationOffset) getSubjectPublicKeyInfo + shareSignature <- getByteString (fromIntegral hashChainOffset - fromIntegral signatureOffset) + shareHashChain <- isolate (fromIntegral blockHashTreeOffset - fromIntegral hashChainOffset) get + shareBlockHashTree <- isolate (fromIntegral shareDataOffset - fromIntegral blockHashTreeOffset) get + shareData <- getLazyByteString (fromIntegral encryptedPrivateKeyOffset - fromIntegral shareDataOffset) + shareEncryptedPrivateKey <- getByteString (fromIntegral eofOffset - fromIntegral encryptedPrivateKeyOffset) empty <- isEmpty unless empty (fail "Expected end of input but there are more bytes") -- GitLab