libgocryptfs/cryptfs/intrablock.go

40 lines
1.2 KiB
Go
Raw Normal View History

2015-09-05 20:30:20 +02:00
package cryptfs
// intraBlock identifies a part of a file block
type intraBlock struct {
2015-10-04 14:36:20 +02:00
BlockNo uint64 // Block number in file
Skip uint64 // Offset into block plaintext
Length uint64 // Length of data from this block
fs *CryptFS
2015-09-05 20:30:20 +02:00
}
// isPartial - is the block partial? This means we have to do read-modify-write.
func (ib *intraBlock) IsPartial() bool {
if ib.Skip > 0 || ib.Length < ib.fs.plainBS {
2015-09-05 20:30:20 +02:00
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 HEADER_LEN + ib.BlockNo * ib.fs.cipherBS, ib.fs.cipherBS
2015-09-05 20:30:20 +02:00
}
// 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
2015-10-04 14:36:20 +02:00
func (ib *intraBlock) CropBlock(d []byte) []byte {
2015-09-05 20:30:20 +02:00
lenHave := len(d)
2015-10-04 14:36:20 +02:00
lenWant := int(ib.Skip + ib.Length)
2015-09-05 20:30:20 +02:00
if lenHave < lenWant {
return d[ib.Skip:lenHave]
2015-09-05 20:30:20 +02:00
}
return d[ib.Skip:lenWant]
2015-09-05 20:30:20 +02:00
}