summaryrefslogtreecommitdiff
path: root/cryptfs/cryptfs_content.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2015-09-08 00:54:24 +0200
committerJakob Unterwurzacher2015-09-08 00:55:03 +0200
commit889ae900814aac1c17c38a76882b1aafa5744be6 (patch)
tree67b5c1e873574e208971ec6f98fa5febb76f3fa7 /cryptfs/cryptfs_content.go
parentb65882985dd7aa3ea7ae76c49d592ff30b353a8b (diff)
Add pathfs frontend (uses go-fuse instead of bazil-fuse), part I
Currently fails main_test.go, will be fixed in part II
Diffstat (limited to 'cryptfs/cryptfs_content.go')
-rw-r--r--cryptfs/cryptfs_content.go39
1 files changed, 38 insertions, 1 deletions
diff --git a/cryptfs/cryptfs_content.go b/cryptfs/cryptfs_content.go
index 4658529..e23d9b5 100644
--- a/cryptfs/cryptfs_content.go
+++ b/cryptfs/cryptfs_content.go
@@ -77,7 +77,7 @@ func (be *CryptFS) EncryptBlock(plaintext []byte) []byte {
return ciphertext
}
-// Split a plaintext byte range into (possible partial) blocks
+// Split a plaintext byte range into (possibly partial) blocks
func (be *CryptFS) SplitRange(offset uint64, length uint64) []intraBlock {
var b intraBlock
var parts []intraBlock
@@ -113,6 +113,22 @@ func (be *CryptFS) minu64(x uint64, y uint64) uint64 {
return y
}
+// CiphertextRange - Get byte range in backing ciphertext corresponding
+// to plaintext range. Returns a range aligned to ciphertext blocks.
+func (be *CryptFS) CiphertextRange(offset uint64, length uint64) (uint64, uint64, int) {
+ // Decrypting the ciphertext will yield too many plaintext bytes. Skip this number
+ // of bytes from the front.
+ skip := offset % be.plainBS
+
+ firstBlockNo := offset / be.plainBS
+ lastBlockNo := ( offset + length - 1 ) / be.plainBS
+
+ alignedOffset := firstBlockNo * be.cipherBS
+ alignedLength := (lastBlockNo - firstBlockNo + 1) * be.cipherBS
+
+ return alignedOffset, alignedLength, int(skip)
+}
+
// Get the byte range in the ciphertext corresponding to blocks
// (full blocks!)
func (be *CryptFS) JoinCiphertextRange(blocks []intraBlock) (uint64, uint64) {
@@ -138,3 +154,24 @@ func (be *CryptFS) CropPlaintext(plaintext []byte, blocks []intraBlock) []byte {
}
return cropped
}
+
+// MergeBlocks - Merge newData into oldData at offset
+// New block may be bigger than both newData and oldData
+func (be *CryptFS) MergeBlocks(oldData []byte, newData []byte, offset int) []byte {
+
+ // Make block of maximum size
+ out := make([]byte, be.plainBS)
+
+ // Copy old and new data into it
+ copy(out, oldData)
+ l := len(newData)
+ copy(out[offset:offset + l], newData)
+
+ // Crop to length
+ outLen := len(oldData)
+ newLen := offset + len(newData)
+ if outLen < newLen {
+ outLen = newLen
+ }
+ return out[0:outLen]
+}