From 495bd340f5d0ee277f012cc7561bff410222cd9e Mon Sep 17 00:00:00 2001 From: manusoft Date: Mon, 25 May 2026 17:27:14 +0400 Subject: [PATCH] Add FiloConstants and update chunk metadata handling Introduce FiloConstants class for shared chunk metadata constants (FooterMagic, IvSize, LengthSize). Refactor FiloWriter to use these constants for chunk length and offset calculations, improving consistency for both encrypted and non-encrypted data. Rename offset variable to chunkStartOffset for clarity. --- src/Filo/Core/FiloWriter.cs | 13 +++++++------ src/Filo/Shared/FiloConstants.cs | 9 +++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 src/Filo/Shared/FiloConstants.cs diff --git a/src/Filo/Core/FiloWriter.cs b/src/Filo/Core/FiloWriter.cs index bb63c35..58b7b6d 100644 --- a/src/Filo/Core/FiloWriter.cs +++ b/src/Filo/Core/FiloWriter.cs @@ -1,4 +1,5 @@ -using ManuHub.Filo.Utils; +using ManuHub.Filo.Shared; +using ManuHub.Filo.Utils; using System.Security.Cryptography; using System.Text; using System.Text.Json; @@ -145,7 +146,7 @@ public async Task WriteAsync() while ((read = await fs.ReadAsync(buffer)) > 0) { - var offset = output.Position; + var chunkStartOffset = output.Position; byte[] chunk = buffer[..read]; string hash = Convert.ToHexString(SHA256.HashData(chunk)); @@ -162,8 +163,8 @@ public async Task WriteAsync() entry.Chunks.Add(new FiloChunkIndex { Id = chunkId++, - Offset = offset, - Length = encrypted.Length, + Offset = chunkStartOffset, + Length = FiloConstants.IvSize + FiloConstants.LengthSize + encrypted.Length, Hash = hash }); } @@ -175,8 +176,8 @@ public async Task WriteAsync() entry.Chunks.Add(new FiloChunkIndex { Id = chunkId++, - Offset = offset, - Length = read, + Offset = chunkStartOffset, + Length = FiloConstants.LengthSize + read, Hash = hash }); } diff --git a/src/Filo/Shared/FiloConstants.cs b/src/Filo/Shared/FiloConstants.cs new file mode 100644 index 0000000..09ae3e6 --- /dev/null +++ b/src/Filo/Shared/FiloConstants.cs @@ -0,0 +1,9 @@ +namespace ManuHub.Filo.Shared; + +public static class FiloConstants +{ + public const string FooterMagic = "FLOF"; + + public const int IvSize = 16; + public const int LengthSize = 4; +} \ No newline at end of file