2016-02-06 19:20:54 +01:00
|
|
|
package contentenc
|
2015-09-05 20:30:20 +02:00
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// IntraBlock identifies a part of a file block
|
|
|
|
type IntraBlock struct {
|
|
|
|
// BlockNo is the block number in the file
|
2016-08-30 00:20:31 +02:00
|
|
|
BlockNo uint64
|
2016-10-02 06:14:18 +02:00
|
|
|
// Skip is an offset into the block payload
|
2016-08-30 00:20:31 +02:00
|
|
|
// In forwared mode: block plaintext
|
|
|
|
// In reverse mode: offset into block ciphertext. Takes the header into
|
|
|
|
// account.
|
|
|
|
Skip uint64
|
|
|
|
// Length of payload data in this block
|
|
|
|
// In forwared mode: length of the plaintext
|
|
|
|
// In reverse mode: length of the ciphertext. Takes header and trailer into
|
|
|
|
// account.
|
|
|
|
Length uint64
|
|
|
|
fs *ContentEnc
|
2015-09-05 20:30:20 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// IsPartial - is the block partial? This means we have to do read-modify-write.
|
|
|
|
func (ib *IntraBlock) IsPartial() bool {
|
2015-10-04 14:24:43 +02:00
|
|
|
if ib.Skip > 0 || ib.Length < ib.fs.plainBS {
|
2015-09-05 20:30:20 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-07-02 00:12:36 +02:00
|
|
|
// BlockCipherOff returns the ciphertext offset corresponding to BlockNo
|
2016-10-02 06:14:18 +02:00
|
|
|
func (ib *IntraBlock) BlockCipherOff() (offset uint64) {
|
2016-07-02 00:12:36 +02:00
|
|
|
return ib.fs.BlockNoToCipherOff(ib.BlockNo)
|
2015-09-05 20:30:20 +02:00
|
|
|
}
|
|
|
|
|
2016-07-02 00:12:36 +02:00
|
|
|
// BlockPlainOff returns the plaintext offset corresponding to BlockNo
|
2016-10-02 06:14:18 +02:00
|
|
|
func (ib *IntraBlock) BlockPlainOff() (offset uint64) {
|
2016-07-02 00:12:36 +02:00
|
|
|
return ib.fs.BlockNoToPlainOff(ib.BlockNo)
|
2015-09-05 20:30:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CropBlock - crop a potentially larger plaintext block down to the relevant part
|
2016-10-02 06:14:18 +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 {
|
2015-10-04 14:24:43 +02:00
|
|
|
return d[ib.Skip:lenHave]
|
2015-09-05 20:30:20 +02:00
|
|
|
}
|
2015-10-04 14:24:43 +02:00
|
|
|
return d[ib.Skip:lenWant]
|
2015-09-05 20:30:20 +02:00
|
|
|
}
|
2015-11-01 12:11:36 +01:00
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// JointCiphertextRange is the ciphertext range corresponding to the sum of all
|
|
|
|
// "blocks" (complete blocks)
|
|
|
|
func (ib *IntraBlock) JointCiphertextRange(blocks []IntraBlock) (offset uint64, length uint64) {
|
2015-11-01 12:11:36 +01:00
|
|
|
firstBlock := blocks[0]
|
|
|
|
lastBlock := blocks[len(blocks)-1]
|
|
|
|
|
|
|
|
offset = ib.fs.BlockNoToCipherOff(firstBlock.BlockNo)
|
|
|
|
offsetLast := ib.fs.BlockNoToCipherOff(lastBlock.BlockNo)
|
|
|
|
length = offsetLast + ib.fs.cipherBS - offset
|
|
|
|
|
|
|
|
return offset, length
|
|
|
|
}
|
2016-08-30 00:20:31 +02:00
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// JointPlaintextRange is the plaintext range corresponding to the sum of all
|
|
|
|
// "blocks" (complete blocks)
|
|
|
|
func JointPlaintextRange(blocks []IntraBlock) (offset uint64, length uint64) {
|
2016-08-30 00:20:31 +02:00
|
|
|
firstBlock := blocks[0]
|
|
|
|
lastBlock := blocks[len(blocks)-1]
|
|
|
|
|
|
|
|
offset = firstBlock.BlockPlainOff()
|
|
|
|
length = lastBlock.BlockPlainOff() + lastBlock.fs.PlainBS() - offset
|
|
|
|
|
|
|
|
return offset, length
|
|
|
|
}
|