contentenc: add PlainOffToCipherOff helper
Will be used for improving Lseek()
This commit is contained in:
parent
b4794bedec
commit
07164cbb3a
|
@ -76,20 +76,20 @@ func (be *ContentEnc) CipherSizeToPlainSize(cipherSize uint64) uint64 {
|
||||||
return cipherSize - overhead
|
return cipherSize - overhead
|
||||||
}
|
}
|
||||||
|
|
||||||
// PlainSizeToCipherSize calculates the ciphertext size from a plaintext size
|
// PlainSizeToCipherSize calculates the ciphertext size from a plaintext size.
|
||||||
func (be *ContentEnc) PlainSizeToCipherSize(plainSize uint64) uint64 {
|
func (be *ContentEnc) PlainSizeToCipherSize(plainSize uint64) uint64 {
|
||||||
// Zero-sized files stay zero-sized
|
// Zero-sized files stay zero-sized
|
||||||
if plainSize == 0 {
|
if plainSize == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
return be.PlainOffToCipherOff(plainSize-1) + 1
|
||||||
|
}
|
||||||
|
|
||||||
// Block number at last byte
|
// PlainOffToCipherOff tells you the highest ciphertext offset that is
|
||||||
blockNo := be.PlainOffToBlockNo(plainSize - 1)
|
// *guaranteed* to be written/read when you write/read at `plainOff`.
|
||||||
blockCount := blockNo + 1
|
func (be *ContentEnc) PlainOffToCipherOff(plainOff uint64) uint64 {
|
||||||
|
startOfBlock := be.BlockNoToCipherOff(be.PlainOffToBlockNo(plainOff))
|
||||||
overhead := be.BlockOverhead()*blockCount + HeaderLen
|
return startOfBlock + plainOff%be.PlainBS() + be.BlockOverhead()
|
||||||
|
|
||||||
return plainSize + overhead
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExplodePlainRange splits a plaintext byte range into (possibly partial) blocks
|
// ExplodePlainRange splits a plaintext byte range into (possibly partial) blocks
|
||||||
|
|
|
@ -15,38 +15,39 @@ func TestSizeToSize(t *testing.T) {
|
||||||
|
|
||||||
const rangeMax = 10000
|
const rangeMax = 10000
|
||||||
|
|
||||||
var c2p [rangeMax]uint64
|
// y values in this order:
|
||||||
var p2c [rangeMax]uint64
|
// 0 ... CipherSizeToPlainSize
|
||||||
|
// 1 ... PlainSizeToCipherSize
|
||||||
|
// 2 ... PlainOffToCipherOff
|
||||||
|
var yTable [rangeMax][3]uint64
|
||||||
|
|
||||||
// Calculate values
|
// Calculate values
|
||||||
for i := range c2p {
|
for x := range yTable {
|
||||||
c2p[i] = ce.CipherSizeToPlainSize(uint64(i))
|
yTable[x][0] = ce.CipherSizeToPlainSize(uint64(x))
|
||||||
p2c[i] = ce.PlainSizeToCipherSize(uint64(i))
|
yTable[x][1] = ce.PlainSizeToCipherSize(uint64(x))
|
||||||
|
yTable[x][2] = ce.PlainOffToCipherOff(uint64(x))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print data table
|
// Print data table
|
||||||
fmt.Print("x\tToPlainSize\tToCipherSize\n")
|
fmt.Print("x\tCipherSizeToPlainSize\tPlainSizeToCipherSize\tPlainOffToCipherOff\n")
|
||||||
for i := range c2p {
|
for x := range yTable {
|
||||||
if i > 1 && i < rangeMax-1 {
|
if x > 1 && x < rangeMax-1 {
|
||||||
// If the point before has value-1 and the point after has value+1,
|
// If the point before has value-1 and the point after has value+1,
|
||||||
// it is not interesting. Don't print it out.
|
// it is not interesting. Don't print it out.
|
||||||
if c2p[i] == c2p[i-1]+1 && p2c[i] == p2c[i-1]+1 && c2p[i+1] == c2p[i]+1 && p2c[i+1] == p2c[i]+1 {
|
interesting := false
|
||||||
continue
|
for i := 0; i <= 2; i++ {
|
||||||
|
if yTable[x-1][i]+1 != yTable[x][i] && yTable[x][i]+1 != yTable[x+1][i]+1 {
|
||||||
|
interesting = true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
fmt.Printf("%d\t%d\t%d\n", i, c2p[i], p2c[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Monotonicity check
|
// Monotonicity check
|
||||||
for i := range c2p {
|
if yTable[x][i] < yTable[x-1][i] {
|
||||||
if i < 1 {
|
t.Errorf("column %d is non-monotonic!", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !interesting {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if c2p[i-1] > c2p[i] {
|
|
||||||
t.Errorf("error: c2p is non-monotonic: c2p[%d]=%d c2p[%d]=%d ", i-1, c2p[i-1], i, c2p[i])
|
|
||||||
}
|
|
||||||
if p2c[i-1] > p2c[i] {
|
|
||||||
t.Errorf("error: p2c is non-monotonic: p2c[%d]=%d p2c[%d]=%d ", i-1, p2c[i-1], i, p2c[i])
|
|
||||||
}
|
}
|
||||||
|
fmt.Printf("%d\t%d\t%d\t%d\n", x, yTable[x][0], yTable[x][1], yTable[x][2])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue