fix encryption header error
When moving back the pointer to position 0 (using stream_seek), the pointer on the encrypted stream will be moved to the position immediately after the header. Reading the header again (invoked by stream_read) will cause an error, writing the header again (invoked by stream_write) will corrupt the file. Reading/writing the header should therefore happen when opening the file rather than upon read or write. Note that a side-effect of this PR is that empty files will still get an encryption header; I think that is OK, but it is different from how it was originally implemented.
This commit is contained in:
parent
4334e77035
commit
735f6cc037
|
@ -221,15 +221,28 @@ class Encryption extends Wrapper {
|
|||
|| $mode === 'w+'
|
||||
|| $mode === 'wb'
|
||||
|| $mode === 'wb+'
|
||||
|| $mode === 'r+'
|
||||
|| $mode === 'rb+'
|
||||
) {
|
||||
// We're writing a new file so start write counter with 0 bytes
|
||||
$this->unencryptedSize = 0;
|
||||
$this->size = 0;
|
||||
$this->readOnly = false;
|
||||
} else {
|
||||
$this->readOnly = true;
|
||||
}
|
||||
|
||||
if (
|
||||
$mode === 'w'
|
||||
|| $mode === 'w+'
|
||||
|| $mode === 'wb'
|
||||
|| $mode === 'wb+'
|
||||
) {
|
||||
// We're writing a new file so start write counter with 0 bytes
|
||||
$this->unencryptedSize = 0;
|
||||
$this->writeHeader();
|
||||
$this->size = $this->util->getHeaderSize();
|
||||
} else {
|
||||
parent::stream_read($this->util->getHeaderSize());
|
||||
}
|
||||
|
||||
$sharePath = $this->fullPath;
|
||||
if (!$this->storage->file_exists($this->internalPath)) {
|
||||
$sharePath = dirname($sharePath);
|
||||
|
@ -250,11 +263,6 @@ class Encryption extends Wrapper {
|
|||
|
||||
$result = '';
|
||||
|
||||
// skip the header if we read the file from the beginning
|
||||
if ($this->position === 0) {
|
||||
parent::stream_read($this->util->getHeaderSize());
|
||||
}
|
||||
|
||||
// $count = min($count, $this->unencryptedSize - $this->position);
|
||||
while ($count > 0) {
|
||||
$remainingLength = $count;
|
||||
|
@ -281,11 +289,6 @@ class Encryption extends Wrapper {
|
|||
|
||||
public function stream_write($data) {
|
||||
|
||||
if ($this->position === 0) {
|
||||
$this->writeHeader();
|
||||
$this->size = $this->util->getHeaderSize();
|
||||
}
|
||||
|
||||
$length = 0;
|
||||
// loop over $data to fit it in 6126 sized unencrypted blocks
|
||||
while (strlen($data) > 0) {
|
||||
|
|
Loading…
Reference in New Issue