Browse Source

Fix writing large encrypted files, remove slow truncates

timvisee 7 years ago
parent
commit
2d1c669cef
2 changed files with 8 additions and 8 deletions
  1. 2 1
      api/src/crypto/b64.rs
  2. 6 7
      api/src/reader.rs

+ 2 - 1
api/src/crypto/b64.rs

@@ -8,7 +8,8 @@ extern crate base64;
 
 
 pub use self::base64::{Config, DecodeError};
 pub use self::base64::{Config, DecodeError};
 
 
-/// Encode the given byte slice using base64, in an URL-safe manner.
+/// Encode the given byte slice using base64,
+/// in an URL-safe manner without padding.
 pub fn encode(input: &[u8]) -> String {
 pub fn encode(input: &[u8]) -> String {
     base64::encode_config(input, base64::URL_SAFE_NO_PAD)
     base64::encode_config(input, base64::URL_SAFE_NO_PAD)
 }
 }

+ 6 - 7
api/src/reader.rs

@@ -135,10 +135,9 @@ impl EncryptedFileReader {
 
 
         // Create an encrypted buffer, truncate the data buffer
         // Create an encrypted buffer, truncate the data buffer
         let mut encrypted = vec![0u8; len + block_size];
         let mut encrypted = vec![0u8; len + block_size];
-        data.truncate(len);
 
 
         // Encrypt the data that was read
         // Encrypt the data that was read
-        let len = self.crypter.update(&data, &mut encrypted)?;
+        let len = self.crypter.update(&data[..len], &mut encrypted)?;
 
 
         // Calculate how many bytes will be copied to the reader
         // Calculate how many bytes will be copied to the reader
         let out_len = min(buf.len(), len);
         let out_len = min(buf.len(), len);
@@ -472,10 +471,9 @@ impl Write for EncryptedFileWriter {
                 file_buf,
                 file_buf,
                 &mut decrypted,
                 &mut decrypted,
             )?;
             )?;
-            decrypted.truncate(len);
 
 
             // Write to the file
             // Write to the file
-            self.file.write_all(&decrypted)?;
+            self.file.write_all(&decrypted[..len])?;
         }
         }
 
 
         // Read from the tag part to fill the tag buffer
         // Read from the tag part to fill the tag buffer
@@ -494,15 +492,16 @@ impl Write for EncryptedFileWriter {
 
 
             // Finalize, write all remaining data
             // Finalize, write all remaining data
             let len = self.crypter.finalize(&mut extra)?;
             let len = self.crypter.finalize(&mut extra)?;
-            extra.truncate(len);
-            self.file.write_all(&extra)?;
+            self.file.write_all(&extra[..len])?;
 
 
             // Set the verified flag
             // Set the verified flag
             self.verified = true;
             self.verified = true;
         }
         }
 
 
         // Compute how many bytes were written
         // Compute how many bytes were written
-        Ok(file_buf.len() + tag_buf.len())
+        let len = file_buf.len() + min(tag_buf.len(), TAG_LEN);
+        self.cur += len;
+        Ok(len)
     }
     }
 
 
     fn flush(&mut self) -> Result<(), io::Error> {
     fn flush(&mut self) -> Result<(), io::Error> {