mirror of
https://github.com/QL-Win/QuickLook.git
synced 2026-01-13 07:05:24 +08:00
Add compound file magic header validation
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Runtime.InteropServices.ComTypes;
|
using System.Runtime.InteropServices.ComTypes;
|
||||||
|
|
||||||
namespace QuickLook.Plugin.ArchiveViewer.CompoundFileBinary;
|
namespace QuickLook.Plugin.ArchiveViewer.CompoundFileBinary;
|
||||||
@@ -42,6 +43,22 @@ public static class CompoundFileExtractor
|
|||||||
Directory.CreateDirectory(destinationDirectory);
|
Directory.CreateDirectory(destinationDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure the compound file exists
|
||||||
|
if (!File.Exists(compoundFilePath))
|
||||||
|
throw new FileNotFoundException("Compound file not found.", compoundFilePath);
|
||||||
|
|
||||||
|
// Validate magic header for OLE compound file: D0 CF 11 E0 A1 B1 1A E1
|
||||||
|
byte[] magicHeader = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
|
||||||
|
byte[] header = new byte[8];
|
||||||
|
using (FileStream fs = new(compoundFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
|
||||||
|
{
|
||||||
|
int read = fs.Read(header, 0, header.Length);
|
||||||
|
if (read < header.Length || !header.SequenceEqual(magicHeader))
|
||||||
|
{
|
||||||
|
throw new InvalidDataException("The specified file does not appear to be an OLE Compound File (invalid header).");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Open the compound file as an IStorage implementation wrapped by DisposableIStorage.
|
// Open the compound file as an IStorage implementation wrapped by DisposableIStorage.
|
||||||
using DisposableIStorage storage = new(compoundFilePath, STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE, IntPtr.Zero);
|
using DisposableIStorage storage = new(compoundFilePath, STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE, IntPtr.Zero);
|
||||||
IEnumerator<STATSTG> enumerator = storage.EnumElements();
|
IEnumerator<STATSTG> enumerator = storage.EnumElements();
|
||||||
|
|||||||
Reference in New Issue
Block a user