summaryrefslogtreecommitdiff
path: root/cryptfs/intrablock.go
blob: 78fd1403e912df9b26c3fff25f80225520b9280b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package cryptfs

// intraBlock identifies a part of a file block
type intraBlock struct {
	BlockNo uint64  // Block number in file
	Offset  uint64  // Offset into block plaintext
	Length  uint64  // Length of data from this block
	fs    *CryptFS
}

// isPartial - is the block partial? This means we have to do read-modify-write.
func (ib *intraBlock) IsPartial() bool {
	if ib.Offset > 0 || ib.Length < ib.fs.plainBS {
		return true
	}
	return false
}

// CiphertextRange - get byte range in ciphertext file corresponding to BlockNo
// (complete block)
func (ib *intraBlock) CiphertextRange() (offset uint64, length uint64) {
	return ib.BlockNo * ib.fs.cipherBS, ib.fs.cipherBS
}

// PlaintextRange - get byte range in plaintext corresponding to BlockNo
// (complete block)
func (ib *intraBlock) PlaintextRange() (offset uint64, length uint64) {
	return ib.BlockNo * ib.fs.plainBS, ib.fs.plainBS
}

// CropBlock - crop a potentially larger plaintext block down to the relevant part
func (ib *intraBlock) CropBlock(d []byte) []byte{
	lenHave := len(d)
	lenWant := int(ib.Offset+ib.Length)
	if lenHave < lenWant {
		return d[ib.Offset:lenHave]
	}
	return d[ib.Offset:lenWant]
}