|
2 | 2 | using SecureFolderFS.Core.Security; |
3 | 3 | using SecureFolderFS.Core.FileHeaders; |
4 | 4 | using SecureFolderFS.Core.Tracking; |
5 | | -using SecureFolderFS.Shared.Extensions; |
6 | 5 | using SecureFolderFS.Core.Streams.Management; |
7 | 6 |
|
8 | 7 | namespace SecureFolderFS.Core.Chunks.IO |
@@ -34,28 +33,31 @@ public ICleartextChunk ReadChunk(long chunkNumber) |
34 | 33 | { |
35 | 34 | AssertNotDisposed(); |
36 | 35 |
|
| 36 | + // Calculate |
37 | 37 | var payloadSize = _security.ContentCryptor.FileContentCryptor.ChunkCleartextSize; |
38 | 38 | var chunkSize = _security.ContentCryptor.FileContentCryptor.ChunkFullCiphertextSize; |
39 | | - |
40 | 39 | var ciphertextPosition = _security.ContentCryptor.FileHeaderCryptor.HeaderSize + (chunkNumber * chunkSize); |
| 40 | + |
| 41 | + // Initialize buffer |
41 | 42 | var ciphertextBuffer = new byte[chunkSize]; |
| 43 | + var ciphertextBufferMemory = ciphertextBuffer.AsMemory(); |
42 | 44 |
|
| 45 | + // Read from stream |
43 | 46 | var ciphertextFileStream = _ciphertextStreamsManager.EnsureReadOnlyStreamInstance(); |
44 | 47 | ciphertextFileStream.Position = ciphertextPosition; |
45 | | - var read = ciphertextFileStream.Read(ciphertextBuffer, 0, ciphertextBuffer.Length); |
| 48 | + var read = ciphertextFileStream.Read(ciphertextBufferMemory.Span); |
46 | 49 |
|
| 50 | + // Check for end-of-file |
47 | 51 | if (read == Constants.IO.FILE_EOF) |
48 | 52 | { |
49 | 53 | return _chunkFactory.FromCleartextChunkBuffer(new byte[payloadSize], 0); |
50 | 54 | } |
51 | 55 |
|
52 | 56 | _fileSystemStatsTracker?.AddBytesRead(read); |
53 | 57 |
|
54 | | - var actualCiphertextBuffer = new byte[read]; |
55 | | - actualCiphertextBuffer.EmplaceArrays(ciphertextBuffer); |
56 | | - |
| 58 | + // Decrypt |
57 | 59 | var cleartextChunk = _security.ContentCryptor.FileContentCryptor.DecryptChunk( |
58 | | - _chunkFactory.FromCiphertextChunkBuffer(actualCiphertextBuffer), |
| 60 | + _chunkFactory.FromCiphertextChunkBuffer(ciphertextBufferMemory.Slice(0, read)), |
59 | 61 | chunkNumber, |
60 | 62 | _fileHeader, |
61 | 63 | Constants.Security.ALWAYS_CHECK_CHUNK_INTEGRITY); |
|
0 commit comments