From 07164cbb3a42946a8bb0286da1bc7ea52bb33ee1 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Wed, 26 May 2021 18:28:59 +0200 Subject: contentenc: add PlainOffToCipherOff helper Will be used for improving Lseek() --- internal/contentenc/offsets.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'internal/contentenc/offsets.go') diff --git a/internal/contentenc/offsets.go b/internal/contentenc/offsets.go index 7487baf..3a0abf3 100644 --- a/internal/contentenc/offsets.go +++ b/internal/contentenc/offsets.go @@ -76,20 +76,20 @@ func (be *ContentEnc) CipherSizeToPlainSize(cipherSize uint64) uint64 { return cipherSize - overhead } -// PlainSizeToCipherSize calculates the ciphertext size from a plaintext size +// PlainSizeToCipherSize calculates the ciphertext size from a plaintext size. func (be *ContentEnc) PlainSizeToCipherSize(plainSize uint64) uint64 { // Zero-sized files stay zero-sized if plainSize == 0 { return 0 } + return be.PlainOffToCipherOff(plainSize-1) + 1 +} - // Block number at last byte - blockNo := be.PlainOffToBlockNo(plainSize - 1) - blockCount := blockNo + 1 - - overhead := be.BlockOverhead()*blockCount + HeaderLen - - return plainSize + overhead +// PlainOffToCipherOff tells you the highest ciphertext offset that is +// *guaranteed* to be written/read when you write/read at `plainOff`. +func (be *ContentEnc) PlainOffToCipherOff(plainOff uint64) uint64 { + startOfBlock := be.BlockNoToCipherOff(be.PlainOffToBlockNo(plainOff)) + return startOfBlock + plainOff%be.PlainBS() + be.BlockOverhead() } // ExplodePlainRange splits a plaintext byte range into (possibly partial) blocks -- cgit v1.2.3