diff options
author | Jakob Unterwurzacher | 2016-08-30 00:20:31 +0200 |
---|---|---|
committer | Jakob Unterwurzacher | 2016-09-25 16:43:17 +0200 |
commit | 5931eea3877ae34cdb18ac36e5cf5fba3f74b82b (patch) | |
tree | f6cd8dd41c213013741ae893e67ec1adf3ff5b4c /internal/contentenc/offsets.go | |
parent | 1d4c6288f29d74efce83ada1ea2a7b3819178bde (diff) |
contentenc: add helpers for reverse mode
Add the reverse variant of DecryptBlocks etc:
* EncryptBlocks
* JointPlaintextRange
* ExplodeCipherRange
Diffstat (limited to 'internal/contentenc/offsets.go')
-rw-r--r-- | internal/contentenc/offsets.go | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/internal/contentenc/offsets.go b/internal/contentenc/offsets.go index 813a15f..e331d55 100644 --- a/internal/contentenc/offsets.go +++ b/internal/contentenc/offsets.go @@ -11,8 +11,11 @@ func (be *ContentEnc) PlainOffToBlockNo(plainOffset uint64) uint64 { return plainOffset / be.plainBS } -// get the block number at ciphter-text offset +// get the block number at cipher-text offset func (be *ContentEnc) CipherOffToBlockNo(cipherOffset uint64) uint64 { + if cipherOffset < HEADER_LEN { + panic("BUG: offset is inside the file header") + } return (cipherOffset - HEADER_LEN) / be.cipherBS } @@ -89,6 +92,32 @@ func (be *ContentEnc) ExplodePlainRange(offset uint64, length uint64) []intraBlo return blocks } +// Split a ciphertext byte range into (possibly partial) blocks +// This is used in reverse mode when reading files +func (be *ContentEnc) ExplodeCipherRange(offset uint64, length uint64) []intraBlock { + var blocks []intraBlock + var nextBlock intraBlock + nextBlock.fs = be + + for length > 0 { + nextBlock.BlockNo = be.CipherOffToBlockNo(offset) + nextBlock.Skip = offset - be.BlockNoToCipherOff(nextBlock.BlockNo) + + // This block can carry up to "maxLen" payload bytes + maxLen := be.cipherBS - nextBlock.Skip + nextBlock.Length = maxLen + // But if the user requested less, we truncate the block to "length". + if length < maxLen { + nextBlock.Length = length + } + + blocks = append(blocks, nextBlock) + offset += nextBlock.Length + length -= nextBlock.Length + } + return blocks +} + func (be *ContentEnc) BlockOverhead() uint64 { return be.cipherBS - be.plainBS } |