team-ljyzipelo

공개
89줄4.3 KB
1using Zipelo.Core;
2
3namespace Zipelo.Runtime;
4
5public sealed record ZipExtractedEntry(ArchiveEntry Entry, byte[] Content, uint Crc32);
6
7internal static class ZipExtraction
8{
9 internal static void ValidatePaths(IReadOnlyList<ArchiveEntry> entries)
10 {
11 if (entries.Count > WorkerProtocol.MaxEntries) { throw new ArchiveException(ArchiveError.BudgetExceeded); }
12 var comparer = OutputPathPolicy.Comparer(OutputPathPolicy.CurrentPolicy);
13 var explicitPaths = new HashSet<string>(comparer);
14 var nodes = new Dictionary<string, EntryKind>(comparer);
15 var ids = new HashSet<string>(StringComparer.Ordinal);
16 foreach (var entry in entries)
17 {
18 if (entry is null || string.IsNullOrEmpty(entry.Id) || !ids.Add(entry.Id) ||
19 entry.Kind is not (EntryKind.File or EntryKind.Directory) || entry.DeclaredSize is null or < 0)
20 {
21 throw new ArchiveException(ArchiveError.UnsupportedEntry);
22 }
23 if (entry.Name.Length > WorkerProtocol.MaxNameLength) { throw new ArchiveException(ArchiveError.BudgetExceeded); }
24 var segments = OutputPathPolicy.Validate(entry.Name, OutputPathPolicy.CurrentPolicy);
25 if (segments.Count > 32) { throw new ArchiveException(ArchiveError.BudgetExceeded); }
26 var path = string.Join('/', segments);
27 if (!explicitPaths.Add(path)) { throw new ArchiveException(ArchiveError.OutputConflict); }
28 for (var index = 0; index < segments.Count; index++)
29 {
30 var prefix = string.Join('/', segments.Take(index + 1));
31 var kind = index == segments.Count - 1 ? entry.Kind : EntryKind.Directory;
32 if (nodes.TryGetValue(prefix, out var existing) && (existing != kind || kind == EntryKind.File))
33 {
34 throw new ArchiveException(ArchiveError.OutputConflict);
35 }
36 nodes[prefix] = kind;
37 if (nodes.Count > 4096) { throw new ArchiveException(ArchiveError.BudgetExceeded); }
38 }
39 }
40 }
41
42 internal static ZipExtractedEntry[] ValidateResult(ZipWorkerResult result, CancellationToken token, bool snapshot)
43 {
44 if (result.Job.State != JobState.Completed || result.Job.Error != ArchiveError.None ||
45 result.CreatedZip is not null || result.ExtractedEntries is null ||
46 result.ExtractedEntries.Count != result.Job.Entries.Count)
47 {
48 throw new ArchiveException(ArchiveError.EngineFailure);
49 }
50 var items = result.ExtractedEntries.ToArray();
51 ValidatePaths(items.Select(item => item.Entry).ToArray());
52 long total = 0;
53 for (var index = 0; index < items.Length; index++)
54 {
55 token.ThrowIfCancellationRequested();
56 var item = items[index];
57 var report = result.Job.Entries[index];
58 if (item.Content is null || report.Entry != item.Entry || report.Error != ArchiveError.None ||
59 report.Verification != VerificationState.Passed || report.Outcome != EntryOutcome.Staged ||
60 item.Entry.DeclaredSize != item.Content.LongLength ||
61 (item.Entry.Kind == EntryKind.Directory && item.Content.Length != 0))
62 {
63 throw new ArchiveException(ArchiveError.EngineFailure);
64 }
65 total += item.Content.Length;
66 if (total > WorkerProtocol.MaxDataBytes) { throw new ArchiveException(ArchiveError.BudgetExceeded); }
67 var bytes = snapshot ? item.Content.ToArray() : item.Content;
68 var crc = uint.MaxValue;
69 for (var offset = 0; offset < bytes.Length; offset += 65536)
70 {
71 token.ThrowIfCancellationRequested();
72 crc = UpdateCrc(crc, bytes.AsSpan(offset, Math.Min(65536, bytes.Length - offset)));
73 }
74 if (~crc != item.Crc32) { throw new ArchiveException(ArchiveError.IntegrityFailure); }
75 items[index] = item with { Content = bytes };
76 }
77 return items;
78 }
79
80 internal static uint UpdateCrc(uint crc, ReadOnlySpan<byte> bytes)
81 {
82 foreach (var value in bytes)
83 {
84 crc ^= value;
85 for (var bit = 0; bit < 8; bit++) { crc = (crc >> 1) ^ ((crc & 1) != 0 ? 0xEDB88320u : 0u); }
86 }
87 return crc;
88 }
89}