summaryrefslogtreecommitdiff
path: root/cryptfs/cryptfs_content.go
diff options
context:
space:
mode:
Diffstat (limited to 'cryptfs/cryptfs_content.go')
-rw-r--r--cryptfs/cryptfs_content.go16
1 files changed, 12 insertions, 4 deletions
diff --git a/cryptfs/cryptfs_content.go b/cryptfs/cryptfs_content.go
index b9dd61f..374b4f4 100644
--- a/cryptfs/cryptfs_content.go
+++ b/cryptfs/cryptfs_content.go
@@ -97,12 +97,20 @@ func (be *CryptFS) SplitRange(offset uint64, length uint64) []intraBlock {
// PlainSize - calculate plaintext size from ciphertext size
func (be *CryptFS) PlainSize(size uint64) uint64 {
+
// Zero sized files stay zero-sized
- if size > 0 {
- overhead := be.cipherBS - be.plainBS
- nBlocks := (size + be.cipherBS - 1) / be.cipherBS
- size -= nBlocks * overhead
+ if size == 0 {
+ return 0
+ }
+
+ overhead := be.cipherBS - be.plainBS
+ nBlocks := (size + be.cipherBS - 1) / be.cipherBS
+ if nBlocks * overhead > size {
+ Warn.Printf("PlainSize: Negative size, returning 0 instead\n")
+ return 0
}
+ size -= nBlocks * overhead
+
return size
}