summaryrefslogtreecommitdiffstats
path: root/apps/files_encryption/lib
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2012-06-21 17:37:53 +0200
committerRobin Appelman <icewind@owncloud.com>2012-06-21 17:38:00 +0200
commitd0455c5819099751fb8614a76c272a23c6494b65 (patch)
treec6068672ac891b51e1ae690515a897995ac9039d /apps/files_encryption/lib
parent1338279ca02af2444a7277a041bec18aab615774 (diff)
downloadnextcloud-server-d0455c5819099751fb8614a76c272a23c6494b65.tar.gz
nextcloud-server-d0455c5819099751fb8614a76c272a23c6494b65.zip
truncate decrypted files based on filelength
Diffstat (limited to 'apps/files_encryption/lib')
-rw-r--r--apps/files_encryption/lib/crypt.php8
-rw-r--r--apps/files_encryption/lib/cryptstream.php17
-rw-r--r--apps/files_encryption/lib/proxy.php23
3 files changed, 41 insertions, 7 deletions
diff --git a/apps/files_encryption/lib/crypt.php b/apps/files_encryption/lib/crypt.php
index 4c0ffa978ed..1c90954cec8 100644
--- a/apps/files_encryption/lib/crypt.php
+++ b/apps/files_encryption/lib/crypt.php
@@ -206,12 +206,16 @@ class OC_Crypt {
/**
* decrypt data in 8192b sized blocks
*/
- public static function blockDecrypt($data, $key=''){
+ public static function blockDecrypt($data, $key='',$maxLength=0){
$result='';
while(strlen($data)){
$result.=self::decrypt(substr($data,0,8192),$key);
$data=substr($data,8192);
}
- return rtrim($result, "\0");
+ if($maxLength>0){
+ return substr($result,0,$maxLength);
+ }else{
+ return rtrim($result, "\0");
+ }
}
}
diff --git a/apps/files_encryption/lib/cryptstream.php b/apps/files_encryption/lib/cryptstream.php
index 64fec381ded..4ef7d1e08bb 100644
--- a/apps/files_encryption/lib/cryptstream.php
+++ b/apps/files_encryption/lib/cryptstream.php
@@ -35,6 +35,7 @@ class OC_CryptStream{
private $meta=array();//header/meta for source stream
private $count;
private $writeCache;
+ private $size;
private static $rootView;
public function stream_open($path, $mode, $options, &$opened_path){
@@ -45,8 +46,14 @@ class OC_CryptStream{
if(dirname($path)=='streams' and isset(self::$sourceStreams[basename($path)])){
$this->source=self::$sourceStreams[basename($path)]['stream'];
$this->path=self::$sourceStreams[basename($path)]['path'];
+ $this->size=self::$sourceStreams[basename($path)]['size'];
}else{
$this->path=$path;
+ if($mode=='w' or $mode=='w+' or $mode=='wb' or $mode=='wb+'){
+ $this->size=0;
+ }else{
+ $this->size=self::$rootView->filesize($path,$mode);
+ }
OC_FileProxy::$enabled=false;//disable fileproxies so we can open the source file
$this->source=self::$rootView->fopen($path,$mode);
OC_FileProxy::$enabled=true;
@@ -77,14 +84,16 @@ class OC_CryptStream{
OCP\Util::writeLog('files_encryption','php bug 21641 no longer holds, decryption will not work',OCP\Util::FATAL);
die();
}
+ $pos=ftell($this->source);
$data=fread($this->source,8192);
if(strlen($data)){
$result=OC_Crypt::decrypt($data);
}else{
$result='';
}
- if($this->stream_eof()){
- $result=rtrim($result, "\0");
+ $length=$this->size-$pos;
+ if($length<8192){
+ $result=substr($result,0,$length);
}
return $result;
}
@@ -116,6 +125,8 @@ class OC_CryptStream{
$data=substr($data,8192);
}
}
+ $currentPos=ftell($this->source);
+ $this->size=max($this->size,$currentPos);
return $length;
}
@@ -159,7 +170,7 @@ class OC_CryptStream{
public function stream_close(){
$this->flush();
if($this->meta['mode']!='r' and $this->meta['mode']!='rb'){
- OC_FileCache::put($this->path,array('encrypted'=>true),'');
+ OC_FileCache::put($this->path,array('encrypted'=>true,'size'=>$this->size),'');
}
return fclose($this->source);
}
diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php
index b9e719448a3..f25e4a662f6 100644
--- a/apps/files_encryption/lib/proxy.php
+++ b/apps/files_encryption/lib/proxy.php
@@ -66,15 +66,17 @@ class OC_FileProxy_Encryption extends OC_FileProxy{
public function preFile_put_contents($path,&$data){
if(self::shouldEncrypt($path)){
if (!is_resource($data)) {//stream put contents should have been converter to fopen
+ $size=strlen($data);
$data=OC_Crypt::blockEncrypt($data);
- OC_FileCache::put($path,array('encrypted'=>true),'');
+ OC_FileCache::put($path,array('encrypted'=>true,'size'=>$size),'');
}
}
}
public function postFile_get_contents($path,$data){
if(self::isEncrypted($path)){
- $data=OC_Crypt::blockDecrypt($data);
+ $cached=OC_FileCache_Cached::get($path,'');
+ $data=OC_Crypt::blockDecrypt($data,'',$cached['size']);
}
return $data;
}
@@ -108,4 +110,21 @@ class OC_FileProxy_Encryption extends OC_FileProxy{
}
return $mime;
}
+
+ public function postStat($path,$data){
+ if(self::isEncrypted($path)){
+ $cached=OC_FileCache_Cached::get($path,'');
+ $data['size']=$cached['size'];
+ }
+ return $data;
+ }
+
+ public function postFileSize($path,$size){
+ if(self::isEncrypted($path)){
+ $cached=OC_FileCache_Cached::get($path,'');
+ return $cached['size'];
+ }else{
+ return $size;
+ }
+ }
}