AbstractCompoundFileDetailDetector.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. using System.Reflection;
  2. using System.Text;
  3. using Masuit.Tools.Mime;
  4. namespace Masuit.Tools.Files.FileDetector;
  5. public abstract class AbstractCompoundFileDetailDetector : AbstractSignatureDetector
  6. {
  7. private static readonly SignatureInformation[] CfSignatureInfo = {
  8. new() { Position = 0, Signature = [0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1]
  9. },
  10. };
  11. /// <inheritdoc />
  12. protected override SignatureInformation[] SignatureInformations => CfSignatureInfo;
  13. public abstract IEnumerable<string> Chunks { get; }
  14. public override string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
  15. public override List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
  16. protected abstract bool IsValidChunk(string chunkName, byte[] chunkData);
  17. public override bool Detect(Stream stream)
  18. {
  19. if (!base.Detect(stream))
  20. {
  21. return false;
  22. }
  23. stream.Position = 0;
  24. try
  25. {
  26. using var cf = new CompoundFile(stream, CFSConfiguration.LeaveOpen | CFSConfiguration.Default);
  27. return !(from chunk in Chunks
  28. let compoundFileStream = cf.RootStorage.GetStream(chunk)
  29. where compoundFileStream == null || !IsValidChunk(chunk, compoundFileStream.GetData())
  30. select chunk).Any();
  31. }
  32. catch
  33. {
  34. return false;
  35. }
  36. }
  37. #region Modified OpenMCDF
  38. internal enum Color
  39. {
  40. Red = 0,
  41. Black = 1
  42. }
  43. internal interface IRBNode : IComparable
  44. {
  45. IRBNode Left { get; set; }
  46. IRBNode Right { get; set; }
  47. Color Color { get; set; }
  48. IRBNode Parent { get; set; }
  49. IRBNode Grandparent();
  50. IRBNode Sibling();
  51. IRBNode Uncle();
  52. }
  53. internal sealed class RBTree
  54. {
  55. public IRBNode Root { get; set; }
  56. private static Color NodeColor(IRBNode n) => n?.Color ?? Color.Black;
  57. public RBTree()
  58. {
  59. }
  60. public RBTree(IRBNode root)
  61. {
  62. Root = root;
  63. }
  64. private IRBNode Lookup(IRBNode template)
  65. {
  66. var n = Root;
  67. while (n != null)
  68. {
  69. int compResult = template.CompareTo(n);
  70. if (compResult == 0)
  71. {
  72. return n;
  73. }
  74. n = compResult < 0 ? n.Left : n.Right;
  75. }
  76. return n;
  77. }
  78. public bool TryLookup(IRBNode template, out IRBNode val)
  79. {
  80. val = Lookup(template);
  81. return val != null;
  82. }
  83. private void Replace(IRBNode oldn, IRBNode newn)
  84. {
  85. if (oldn.Parent == null)
  86. {
  87. Root = newn;
  88. }
  89. else
  90. {
  91. if (oldn == oldn.Parent.Left)
  92. {
  93. oldn.Parent.Left = newn;
  94. }
  95. else
  96. {
  97. oldn.Parent.Right = newn;
  98. }
  99. }
  100. if (newn != null)
  101. {
  102. newn.Parent = oldn.Parent;
  103. }
  104. }
  105. private void RotateL(IRBNode n)
  106. {
  107. var r = n.Right;
  108. Replace(n, r);
  109. n.Right = r.Left;
  110. if (r.Left != null)
  111. {
  112. r.Left.Parent = n;
  113. }
  114. r.Left = n;
  115. n.Parent = r;
  116. }
  117. private void RotateR(IRBNode n)
  118. {
  119. var l = n.Left;
  120. Replace(n, l);
  121. n.Left = l.Right;
  122. if (l.Right != null)
  123. {
  124. l.Right.Parent = n;
  125. }
  126. l.Right = n;
  127. n.Parent = l;
  128. }
  129. public void Insert(IRBNode newNode)
  130. {
  131. newNode.Color = Color.Red;
  132. if (Root == null)
  133. {
  134. Root = newNode;
  135. }
  136. else
  137. {
  138. var n = Root;
  139. while (true)
  140. {
  141. var compResult = newNode.CompareTo(n);
  142. if (compResult == 0)
  143. {
  144. throw new Exception($"RBNode {newNode} already present in tree");
  145. }
  146. if (compResult < 0)
  147. {
  148. if (n.Left == null)
  149. {
  150. n.Left = newNode;
  151. break;
  152. }
  153. n = n.Left;
  154. }
  155. else
  156. {
  157. if (n.Right == null)
  158. {
  159. n.Right = newNode;
  160. break;
  161. }
  162. n = n.Right;
  163. }
  164. }
  165. newNode.Parent = n;
  166. }
  167. Insert1(newNode);
  168. NodeInserted?.Invoke(newNode);
  169. }
  170. private void Insert1(IRBNode n)
  171. {
  172. if (n.Parent == null)
  173. {
  174. n.Color = Color.Black;
  175. }
  176. else Insert2(n);
  177. }
  178. private void Insert2(IRBNode n)
  179. {
  180. if (NodeColor(n.Parent) == Color.Black)
  181. {
  182. return;
  183. }
  184. Insert3(n);
  185. }
  186. private void Insert3(IRBNode n)
  187. {
  188. if (NodeColor(n.Uncle()) == Color.Red)
  189. {
  190. n.Parent.Color = Color.Black;
  191. n.Uncle().Color = Color.Black;
  192. n.Grandparent().Color = Color.Red;
  193. Insert1(n.Grandparent());
  194. }
  195. else
  196. {
  197. Insert4(n);
  198. }
  199. }
  200. private void Insert4(IRBNode n)
  201. {
  202. if (n == n.Parent.Right && n.Parent == n.Grandparent().Left)
  203. {
  204. RotateL(n.Parent);
  205. n = n.Left;
  206. }
  207. else if (n == n.Parent.Left && n.Parent == n.Grandparent().Right)
  208. {
  209. RotateR(n.Parent);
  210. n = n.Right;
  211. }
  212. Insert5(n);
  213. }
  214. private void Insert5(IRBNode n)
  215. {
  216. n.Parent.Color = Color.Black;
  217. n.Grandparent().Color = Color.Red;
  218. if (n == n.Parent.Left && n.Parent == n.Grandparent().Left)
  219. {
  220. RotateR(n.Grandparent());
  221. }
  222. else
  223. {
  224. RotateL(n.Grandparent());
  225. }
  226. }
  227. internal void VisitTreeNodes(Action<IRBNode> action)
  228. {
  229. var walker = Root;
  230. if (walker != null)
  231. {
  232. DoVisitTreeNodes(action, walker);
  233. }
  234. }
  235. private static void DoVisitTreeNodes(Action<IRBNode> action, IRBNode walker)
  236. {
  237. while (true)
  238. {
  239. if (walker.Left != null)
  240. {
  241. DoVisitTreeNodes(action, walker.Left);
  242. }
  243. action?.Invoke(walker);
  244. if (walker.Right != null)
  245. {
  246. walker = walker.Right;
  247. continue;
  248. }
  249. break;
  250. }
  251. }
  252. internal event Action<IRBNode> NodeInserted;
  253. }
  254. // http://mozilla.org/MPL/2.0/.
  255. internal enum SectorType
  256. {
  257. Normal,
  258. Mini,
  259. FAT,
  260. DIFAT,
  261. RangeLockSector,
  262. Directory
  263. }
  264. internal sealed class Sector(int size, Stream stream) : IDisposable
  265. {
  266. public static int MinisectorSize = 64;
  267. public const int Freesect = unchecked((int) 0xFFFFFFFF);
  268. public const int Endofchain = unchecked((int) 0xFFFFFFFE);
  269. public bool DirtyFlag { get; set; }
  270. public bool IsStreamed => stream != null && Size != MinisectorSize && Id * Size + Size < stream.Length;
  271. internal SectorType Type { get; set; }
  272. public int Id { get; set; } = -1;
  273. public int Size { get; private set; } = size;
  274. private byte[] _data;
  275. public byte[] GetData()
  276. {
  277. if (_data == null)
  278. {
  279. _data = new byte[Size];
  280. if (IsStreamed)
  281. {
  282. stream.Seek(Size + Id * Size, SeekOrigin.Begin);
  283. _=stream.Read(_data, 0, Size);
  284. }
  285. }
  286. return _data;
  287. }
  288. public void InitFATData()
  289. {
  290. _data = new byte[Size];
  291. for (int i = 0; i < Size; i++)
  292. {
  293. _data[i] = 0xFF;
  294. }
  295. DirtyFlag = true;
  296. }
  297. private readonly object _lockObject = new();
  298. #region IDisposable Members
  299. private bool _disposed;
  300. void IDisposable.Dispose()
  301. {
  302. try
  303. {
  304. if (!_disposed)
  305. {
  306. lock (_lockObject)
  307. {
  308. _data = null;
  309. DirtyFlag = false;
  310. Id = Endofchain;
  311. Size = 0;
  312. }
  313. }
  314. }
  315. finally
  316. {
  317. _disposed = true;
  318. }
  319. GC.SuppressFinalize(this);
  320. }
  321. #endregion IDisposable Members
  322. }
  323. internal enum StgType
  324. {
  325. StgInvalid = 0,
  326. StgStorage = 1,
  327. StgStream = 2,
  328. StgLockbytes = 3,
  329. StgProperty = 4,
  330. StgRoot = 5
  331. }
  332. internal sealed class DirectoryEntry : IRBNode
  333. {
  334. internal const int ThisIsGreater = 1;
  335. internal const int OtherIsGreater = -1;
  336. private readonly IList<DirectoryEntry> _dirRepository;
  337. public int Sid { get; set; } = -1;
  338. internal const int Nostream = unchecked((int) 0xFFFFFFFF);
  339. private DirectoryEntry(string name, StgType stgType, IList<DirectoryEntry> dirRepository)
  340. {
  341. _dirRepository = dirRepository;
  342. StgType = stgType;
  343. switch (stgType)
  344. {
  345. case StgType.StgStream:
  346. StorageClsid = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  347. CreationDate = new byte[8];
  348. ModifyDate = new byte[8];
  349. break;
  350. case StgType.StgStorage:
  351. CreationDate = BitConverter.GetBytes((DateTime.Now.ToFileTime()));
  352. break;
  353. case StgType.StgRoot:
  354. CreationDate = new byte[8];
  355. ModifyDate = new byte[8];
  356. break;
  357. }
  358. SetEntryName(name);
  359. }
  360. public byte[] EntryName { get; private set; } = new byte[64];
  361. public string GetEntryName()
  362. {
  363. return EntryName is {Length: > 0} ? Encoding.Unicode.GetString(EntryName).Remove((NameLength - 1) / 2) : string.Empty;
  364. }
  365. public void SetEntryName(string entryName)
  366. {
  367. if (entryName.Contains(@"\") || entryName.Contains("/") || entryName.Contains(":") || entryName.Contains("!"))
  368. {
  369. throw new ArgumentException("Invalid character in entry: the characters '\\', '/', ':','!' cannot be used in entry name");
  370. }
  371. if (entryName.Length > 31)
  372. {
  373. throw new ArgumentException("Entry name MUST be smaller than 31 characters");
  374. }
  375. byte[] temp = Encoding.Unicode.GetBytes(entryName);
  376. var newName = new byte[64];
  377. Buffer.BlockCopy(temp, 0, newName, 0, temp.Length);
  378. newName[temp.Length] = 0x00;
  379. newName[temp.Length + 1] = 0x00;
  380. EntryName = newName;
  381. NameLength = (ushort) (temp.Length + 2);
  382. }
  383. public ushort NameLength { get; private set; }
  384. public StgType StgType { get; set; }
  385. public Color Color { get; set; } = Color.Black;
  386. public int LeftSibling { get; set; } = Nostream;
  387. public int RightSibling { get; set; } = Nostream;
  388. public int Child { get; set; } = Nostream;
  389. public Guid StorageClsid { get; set; } = Guid.NewGuid();
  390. public int StateBits { get; set; }
  391. public byte[] CreationDate { get; set; } = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
  392. public byte[] ModifyDate { get; set; } = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
  393. public int StartSetc { get; set; } = Sector.Endofchain;
  394. public long Size { get; set; }
  395. public int CompareTo(object obj)
  396. {
  397. if (obj is not DirectoryEntry otherDir)
  398. {
  399. throw new ArgumentException("Invalid casting: compared object does not implement IDirectorEntry interface");
  400. }
  401. if (NameLength > otherDir.NameLength)
  402. {
  403. return ThisIsGreater;
  404. }
  405. if (NameLength < otherDir.NameLength)
  406. {
  407. return OtherIsGreater;
  408. }
  409. var thisName = Encoding.Unicode.GetString(EntryName, 0, NameLength);
  410. var otherName = Encoding.Unicode.GetString(otherDir.EntryName, 0, otherDir.NameLength);
  411. for (int z = 0; z < thisName.Length; z++)
  412. {
  413. var thisChar = char.ToUpperInvariant(thisName[z]);
  414. var otherChar = char.ToUpperInvariant(otherName[z]);
  415. if (thisChar > otherChar)
  416. {
  417. return ThisIsGreater;
  418. }
  419. if (thisChar < otherChar)
  420. {
  421. return OtherIsGreater;
  422. }
  423. }
  424. return 0;
  425. }
  426. public override bool Equals(object obj)
  427. {
  428. return CompareTo(obj) == 0;
  429. }
  430. private static ulong Fnv_hash(byte[] buffer)
  431. {
  432. ulong h = 2166136261;
  433. int i;
  434. for (i = 0; i < buffer.Length; i++)
  435. {
  436. h = (h * 16777619) ^ buffer[i];
  437. }
  438. return h;
  439. }
  440. public override int GetHashCode()
  441. {
  442. return (int) Fnv_hash(EntryName);
  443. }
  444. public void Read(Stream stream, int ver = 3)
  445. {
  446. using var rw = new BinaryReader(stream, Encoding.UTF8, true);
  447. EntryName = rw.ReadBytes(64);
  448. NameLength = rw.ReadUInt16();
  449. StgType = (StgType) rw.ReadByte();
  450. Color = (Color) rw.ReadByte();
  451. LeftSibling = rw.ReadInt32();
  452. RightSibling = rw.ReadInt32();
  453. Child = rw.ReadInt32();
  454. if (StgType == StgType.StgInvalid)
  455. {
  456. LeftSibling = Nostream;
  457. RightSibling = Nostream;
  458. Child = Nostream;
  459. }
  460. StorageClsid = new Guid(rw.ReadBytes(16));
  461. StateBits = rw.ReadInt32();
  462. CreationDate = rw.ReadBytes(8);
  463. ModifyDate = rw.ReadBytes(8);
  464. StartSetc = rw.ReadInt32();
  465. if (ver == 3)
  466. {
  467. Size = rw.ReadInt32();
  468. rw.ReadBytes(4);
  469. }
  470. else
  471. {
  472. Size = rw.ReadInt64();
  473. }
  474. }
  475. public string Name => GetEntryName();
  476. public IRBNode Left
  477. {
  478. get => LeftSibling == Nostream ? null : _dirRepository[LeftSibling];
  479. set
  480. {
  481. LeftSibling = (value as DirectoryEntry)?.Sid ?? Nostream;
  482. if (LeftSibling != Nostream)
  483. {
  484. _dirRepository[LeftSibling].Parent = this;
  485. }
  486. }
  487. }
  488. public IRBNode Right
  489. {
  490. get => RightSibling == Nostream ? null : _dirRepository[RightSibling];
  491. set
  492. {
  493. RightSibling = ((DirectoryEntry) value)?.Sid ?? Nostream;
  494. if (RightSibling != Nostream)
  495. {
  496. _dirRepository[RightSibling].Parent = this;
  497. }
  498. }
  499. }
  500. public IRBNode Parent { get; set; }
  501. public IRBNode Grandparent() => Parent?.Parent;
  502. public IRBNode Sibling() => (this == Parent.Left) ? Parent.Right : Parent.Left;
  503. public IRBNode Uncle() => Parent?.Sibling();
  504. internal static DirectoryEntry New(string name, StgType stgType, IList<DirectoryEntry> dirRepository)
  505. {
  506. DirectoryEntry de;
  507. if (dirRepository != null)
  508. {
  509. de = new DirectoryEntry(name, stgType, dirRepository);
  510. dirRepository.Add(de);
  511. de.Sid = dirRepository.Count - 1;
  512. }
  513. else
  514. {
  515. throw new ArgumentNullException(nameof(dirRepository), "Directory repository cannot be null in New() method");
  516. }
  517. return de;
  518. }
  519. internal static DirectoryEntry Mock(string name, StgType stgType) => new(name, stgType, null);
  520. public override string ToString() => $"{Name} [{Sid}]{(StgType == StgType.StgStream ? "Stream" : "Storage")}";
  521. }
  522. internal abstract class CFItem(CompoundFile compoundFile) : IComparable<CFItem>
  523. {
  524. protected CompoundFile CompoundFile { get; } = compoundFile;
  525. protected void CheckDisposed()
  526. {
  527. if (CompoundFile.IsClosed)
  528. {
  529. throw new ObjectDisposedException("Owner Compound file has been closed and owned items have been invalidated");
  530. }
  531. }
  532. internal DirectoryEntry DirEntry { get; set; }
  533. internal int CompareTo(CFItem other) => DirEntry.CompareTo(other.DirEntry);
  534. public int CompareTo(object obj) => DirEntry.CompareTo((obj as CFItem).DirEntry);
  535. public static bool operator ==(CFItem leftItem, CFItem rightItem)
  536. {
  537. if (ReferenceEquals(leftItem, rightItem))
  538. {
  539. return true;
  540. }
  541. if (((object) leftItem == null) || (object) rightItem == null)
  542. {
  543. return false;
  544. }
  545. return leftItem.CompareTo(rightItem) == 0;
  546. }
  547. public static bool operator !=(CFItem leftItem, CFItem rightItem) => !(leftItem == rightItem);
  548. public override bool Equals(object obj) => CompareTo(obj) == 0;
  549. public override int GetHashCode() => DirEntry.GetEntryName().GetHashCode();
  550. public long Size => DirEntry.Size;
  551. int IComparable<CFItem>.CompareTo(CFItem other) => DirEntry.CompareTo(other.DirEntry);
  552. public override string ToString()
  553. {
  554. return DirEntry != null ? $"[{DirEntry.LeftSibling},{DirEntry.Sid},{DirEntry.RightSibling}] {DirEntry.GetEntryName()}" : string.Empty;
  555. }
  556. }
  557. internal sealed class CFStream : CFItem
  558. {
  559. internal CFStream(CompoundFile compoundFile, DirectoryEntry dirEntry) : base(compoundFile)
  560. {
  561. if (dirEntry == null || dirEntry.Sid < 0)
  562. {
  563. throw new Exception("Attempting to add a CFStream using an unitialized directory");
  564. }
  565. DirEntry = dirEntry;
  566. }
  567. public byte[] GetData()
  568. {
  569. CheckDisposed();
  570. return CompoundFile.GetData(this);
  571. }
  572. }
  573. internal sealed class CfStorage : CFItem
  574. {
  575. private RBTree _children;
  576. internal RBTree Children => _children ??= LoadChildren(DirEntry.Sid) ?? CompoundFile.CreateNewTree();
  577. internal CfStorage(CompoundFile compFile, DirectoryEntry dirEntry) : base(compFile)
  578. {
  579. if (dirEntry == null || dirEntry.Sid < 0)
  580. {
  581. throw new ArgumentException("Attempting to create a CFStorage using an unitialized directory");
  582. }
  583. DirEntry = dirEntry;
  584. }
  585. private RBTree LoadChildren(int sid)
  586. {
  587. var childrenTree = CompoundFile.GetChildrenTree(sid);
  588. DirEntry.Child = (childrenTree.Root as DirectoryEntry)?.Sid ?? DirectoryEntry.Nostream;
  589. return childrenTree;
  590. }
  591. public CFStream GetStream(string streamName)
  592. {
  593. CheckDisposed();
  594. var tmp = DirectoryEntry.Mock(streamName, StgType.StgStream);
  595. if (Children.TryLookup(tmp, out var outDe) && ((DirectoryEntry) outDe).StgType == StgType.StgStream)
  596. {
  597. return new CFStream(CompoundFile, (DirectoryEntry) outDe);
  598. }
  599. throw new KeyNotFoundException("Cannot find item [" + streamName + "] within the current storage");
  600. }
  601. public void VisitEntries(Action<CFItem> action, bool recursive)
  602. {
  603. CheckDisposed();
  604. if (action != null)
  605. {
  606. var subStorages = new List<IRBNode>();
  607. void InternalAction(IRBNode targetNode)
  608. {
  609. var d = targetNode as DirectoryEntry;
  610. if (d.StgType == StgType.StgStream)
  611. {
  612. action(new CFStream(CompoundFile, d));
  613. }
  614. else
  615. {
  616. action(new CfStorage(CompoundFile, d));
  617. }
  618. if (d.Child != DirectoryEntry.Nostream)
  619. {
  620. subStorages.Add(targetNode);
  621. }
  622. }
  623. Children.VisitTreeNodes(InternalAction);
  624. if (recursive)
  625. {
  626. foreach (var n in subStorages)
  627. {
  628. new CfStorage(CompoundFile, n as DirectoryEntry).VisitEntries(action, true);
  629. }
  630. }
  631. }
  632. }
  633. }
  634. internal sealed class Header
  635. {
  636. public byte[] HeaderSignature { get; private set; } = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
  637. public byte[] CLSID { get; set; } = new byte[16];
  638. public ushort MinorVersion { get; private set; } = 0x003E;
  639. public ushort MajorVersion { get; private set; } = 0x0003;
  640. public ushort ByteOrder { get; private set; } = 0xFFFE;
  641. public ushort SectorShift { get; private set; } = 9;
  642. public ushort MiniSectorShift { get; private set; } = 6;
  643. public int DirectorySectorsNumber { get; set; }
  644. public int FATSectorsNumber { get; set; }
  645. public int FirstDirectorySectorID { get; set; } = Sector.Endofchain;
  646. public uint MinSizeStandardStream { get; set; } = 4096;
  647. public int FirstMiniFATSectorID { get; set; } = unchecked((int) 0xFFFFFFFE);
  648. public uint MiniFATSectorsNumber { get; set; }
  649. public int FirstDIFATSectorID { get; set; } = Sector.Endofchain;
  650. public uint DIFATSectorsNumber { get; set; }
  651. public int[] DIFAT { get; } = new int[109];
  652. public Header() : this(3)
  653. {
  654. }
  655. public Header(ushort version)
  656. {
  657. switch (version)
  658. {
  659. case 3:
  660. MajorVersion = 3;
  661. SectorShift = 0x0009;
  662. break;
  663. case 4:
  664. MajorVersion = 4;
  665. SectorShift = 0x000C;
  666. break;
  667. default:
  668. throw new Exception("Invalid Compound File Format version");
  669. }
  670. for (int i = 0; i < 109; i++)
  671. {
  672. DIFAT[i] = Sector.Freesect;
  673. }
  674. }
  675. public void Read(Stream stream)
  676. {
  677. var rw = new BinaryReader(stream, Encoding.UTF8, true);
  678. HeaderSignature = rw.ReadBytes(8);
  679. CheckSignature();
  680. CLSID = rw.ReadBytes(16);
  681. MinorVersion = rw.ReadUInt16();
  682. MajorVersion = rw.ReadUInt16();
  683. CheckVersion();
  684. ByteOrder = rw.ReadUInt16();
  685. SectorShift = rw.ReadUInt16();
  686. MiniSectorShift = rw.ReadUInt16();
  687. rw.ReadBytes(6);
  688. DirectorySectorsNumber = rw.ReadInt32();
  689. FATSectorsNumber = rw.ReadInt32();
  690. FirstDirectorySectorID = rw.ReadInt32();
  691. rw.ReadUInt32();
  692. MinSizeStandardStream = rw.ReadUInt32();
  693. FirstMiniFATSectorID = rw.ReadInt32();
  694. MiniFATSectorsNumber = rw.ReadUInt32();
  695. FirstDIFATSectorID = rw.ReadInt32();
  696. DIFATSectorsNumber = rw.ReadUInt32();
  697. for (int i = 0; i < 109; i++)
  698. {
  699. DIFAT[i] = rw.ReadInt32();
  700. }
  701. }
  702. private void CheckVersion()
  703. {
  704. if (MajorVersion != 3 && MajorVersion != 4)
  705. {
  706. throw new InvalidDataException("Unsupported Binary File Format version: OpenMcdf only supports Compound Files with major version equal to 3 or 4 ");
  707. }
  708. }
  709. private readonly byte[] _oleCfsSignature = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
  710. private void CheckSignature()
  711. {
  712. if (HeaderSignature.Where((t, i) => t != _oleCfsSignature[i]).Any())
  713. {
  714. throw new InvalidDataException("Invalid OLE structured storage file");
  715. }
  716. }
  717. }
  718. internal sealed class StreamView : Stream
  719. {
  720. private readonly int _sectorSize;
  721. private long _position;
  722. private readonly List<Sector> _sectorChain;
  723. private readonly Stream _stream;
  724. private readonly bool _isFatStream;
  725. public StreamView(List<Sector> sectorChain, int sectorSize, Stream stream)
  726. {
  727. if (sectorSize <= 0)
  728. {
  729. throw new Exception("Sector size must be greater than zero");
  730. }
  731. _sectorChain = sectorChain ?? throw new Exception("Sector Chain cannot be null");
  732. _sectorSize = sectorSize;
  733. _stream = stream;
  734. }
  735. public StreamView(List<Sector> sectorChain, int sectorSize, long length, Queue<Sector> availableSectors, Stream stream, bool isFatStream = false) : this(sectorChain, sectorSize, stream)
  736. {
  737. _isFatStream = isFatStream;
  738. AdjustLength(length, availableSectors);
  739. }
  740. public override bool CanRead => true;
  741. public override bool CanSeek => true;
  742. public override bool CanWrite => true;
  743. public override void Flush()
  744. {
  745. }
  746. private long _length;
  747. public override long Length => _length;
  748. public override long Position
  749. {
  750. get => _position;
  751. set
  752. {
  753. if (_position > _length - 1)
  754. {
  755. throw new ArgumentOutOfRangeException(nameof(value));
  756. }
  757. _position = value;
  758. }
  759. }
  760. private byte[] buf = new byte[4];
  761. public int ReadInt32()
  762. {
  763. _=Read(buf, 0, 4);
  764. return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  765. }
  766. public override int Read(byte[] buffer, int offset, int count)
  767. {
  768. int nRead = 0;
  769. if (_sectorChain is {Count: > 0})
  770. {
  771. int secIndex = (int) (_position / _sectorSize);
  772. var nToRead = Math.Min(_sectorChain[0].Size - ((int) _position % _sectorSize), count);
  773. if (secIndex < _sectorChain.Count)
  774. {
  775. Buffer.BlockCopy(_sectorChain[secIndex].GetData(), (int) (_position % _sectorSize), buffer, offset, nToRead);
  776. }
  777. nRead += nToRead;
  778. ++secIndex;
  779. while (nRead < (count - _sectorSize))
  780. {
  781. nToRead = _sectorSize;
  782. Buffer.BlockCopy(_sectorChain[secIndex].GetData(), 0, buffer, offset + nRead, nToRead);
  783. nRead += nToRead;
  784. ++secIndex;
  785. }
  786. nToRead = count - nRead;
  787. if (nToRead != 0)
  788. {
  789. Buffer.BlockCopy(_sectorChain[secIndex].GetData(), 0, buffer, offset + nRead, nToRead);
  790. nRead += nToRead;
  791. }
  792. _position += nRead;
  793. return nRead;
  794. }
  795. return 0;
  796. }
  797. public override long Seek(long offset, SeekOrigin origin)
  798. {
  799. switch (origin)
  800. {
  801. case SeekOrigin.Begin:
  802. _position = offset;
  803. break;
  804. case SeekOrigin.Current:
  805. _position += offset;
  806. break;
  807. case SeekOrigin.End:
  808. _position = Length - offset;
  809. break;
  810. }
  811. AdjustLength(_position);
  812. return _position;
  813. }
  814. private void AdjustLength(long value, Queue<Sector> availableSectors = null)
  815. {
  816. _length = value;
  817. long delta = value - _sectorChain.Count * (long) _sectorSize;
  818. if (delta > 0)
  819. {
  820. int nSec = (int) Math.Ceiling(((double) delta / _sectorSize));
  821. while (nSec > 0)
  822. {
  823. Sector t;
  824. if (availableSectors == null || availableSectors.Count == 0)
  825. {
  826. t = new Sector(_sectorSize, _stream);
  827. if (_sectorSize == Sector.MinisectorSize)
  828. {
  829. t.Type = SectorType.Mini;
  830. }
  831. }
  832. else
  833. {
  834. t = availableSectors.Dequeue();
  835. }
  836. if (_isFatStream)
  837. {
  838. t.InitFATData();
  839. }
  840. _sectorChain.Add(t);
  841. nSec--;
  842. }
  843. }
  844. }
  845. public override void SetLength(long value) => AdjustLength(value);
  846. public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException();
  847. }
  848. [Flags]
  849. internal enum CFSConfiguration
  850. {
  851. Default = 1,
  852. LeaveOpen = 16,
  853. }
  854. internal sealed class CompoundFile : IDisposable
  855. {
  856. public CFSConfiguration Configuration => CFSConfiguration.Default;
  857. internal int GetSectorSize() => 2 << (_header.SectorShift - 1);
  858. private List<Sector> _sectors = [];
  859. private Header _header;
  860. internal Stream SourceStream;
  861. public CompoundFile(Stream stream, CFSConfiguration configParameters)
  862. {
  863. _closeStream = !configParameters.HasFlag(CFSConfiguration.LeaveOpen);
  864. LoadStream(stream);
  865. }
  866. private void Load(Stream stream)
  867. {
  868. try
  869. {
  870. _header = new Header();
  871. _directoryEntries = new List<DirectoryEntry>();
  872. SourceStream = stream;
  873. _header.Read(stream);
  874. int nSector = Ceiling((stream.Length - GetSectorSize()) / (double) GetSectorSize());
  875. if (stream.Length > 0x7FFFFF0)
  876. {
  877. TransactionLockAllocated = true;
  878. }
  879. _sectors = [];
  880. for (int i = 0; i < nSector; i++)
  881. {
  882. _sectors.Add(null);
  883. }
  884. LoadDirectories();
  885. RootStorage = new CfStorage(this, _directoryEntries[0]);
  886. }
  887. catch (Exception)
  888. {
  889. if (stream != null && _closeStream)
  890. {
  891. stream.Dispose();
  892. }
  893. throw;
  894. }
  895. }
  896. private void LoadStream(Stream stream)
  897. {
  898. if (stream == null)
  899. {
  900. throw new Exception("Stream parameter cannot be null");
  901. }
  902. if (!stream.CanSeek)
  903. {
  904. throw new Exception("Cannot load a non-seekable Stream");
  905. }
  906. stream.Seek(0, SeekOrigin.Begin);
  907. Load(stream);
  908. }
  909. internal bool TransactionLockAllocated;
  910. private List<Sector> GetDifatSectorChain()
  911. {
  912. var result = new List<Sector>();
  913. if (_header.DIFATSectorsNumber != 0)
  914. {
  915. var validationCount = (int) _header.DIFATSectorsNumber;
  916. var s = _sectors[_header.FirstDIFATSectorID];
  917. if (s == null)
  918. {
  919. _sectors[_header.FirstDIFATSectorID] = s = new Sector(GetSectorSize(), SourceStream)
  920. {
  921. Type = SectorType.DIFAT,
  922. Id = _header.FirstDIFATSectorID
  923. };
  924. }
  925. result.Add(s);
  926. while (validationCount >= 0)
  927. {
  928. var nextSecId = BitConverter.ToInt32(s.GetData(), GetSectorSize() - 4);
  929. if (nextSecId is Sector.Freesect or Sector.Endofchain)
  930. {
  931. break;
  932. }
  933. validationCount--;
  934. if (validationCount < 0)
  935. {
  936. Dispose();
  937. throw new InvalidDataException("DIFAT sectors count mismatched. Corrupted compound file");
  938. }
  939. s = _sectors[nextSecId];
  940. if (s == null)
  941. {
  942. _sectors[nextSecId] = s = new Sector(GetSectorSize(), SourceStream)
  943. {
  944. Id = nextSecId
  945. };
  946. }
  947. result.Add(s);
  948. }
  949. }
  950. return result;
  951. }
  952. private List<Sector> GetFatSectorChain()
  953. {
  954. const int nHeaderFatEntry = 109;
  955. List<Sector> result = [];
  956. int nextSecId;
  957. var difatSectors = GetDifatSectorChain();
  958. var idx = 0;
  959. while (idx < _header.FATSectorsNumber && idx < nHeaderFatEntry)
  960. {
  961. nextSecId = _header.DIFAT[idx];
  962. var s = _sectors[nextSecId];
  963. if (s == null)
  964. {
  965. _sectors[nextSecId] = s = new Sector(GetSectorSize(), SourceStream)
  966. {
  967. Id = nextSecId,
  968. Type = SectorType.FAT
  969. };
  970. }
  971. result.Add(s);
  972. ++idx;
  973. }
  974. if (difatSectors.Count > 0)
  975. {
  976. var difatStream = new StreamView(difatSectors, GetSectorSize(), _header.FATSectorsNumber > nHeaderFatEntry ? (_header.FATSectorsNumber - nHeaderFatEntry) * 4 : 0, null, SourceStream);
  977. var nextDifatSectorBuffer = new byte[4];
  978. _=difatStream.Read(nextDifatSectorBuffer, 0, 4);
  979. nextSecId = BitConverter.ToInt32(nextDifatSectorBuffer, 0);
  980. int i = 0;
  981. int nFat = nHeaderFatEntry;
  982. while (nFat < _header.FATSectorsNumber)
  983. {
  984. if (difatStream.Position == ((GetSectorSize() - 4) + i * GetSectorSize()))
  985. {
  986. difatStream.Seek(4, SeekOrigin.Current);
  987. ++i;
  988. continue;
  989. }
  990. var s = _sectors[nextSecId];
  991. if (s == null)
  992. {
  993. _sectors[nextSecId] = s = new Sector(GetSectorSize(), SourceStream)
  994. {
  995. Type = SectorType.FAT,
  996. Id = nextSecId
  997. };
  998. }
  999. result.Add(s);
  1000. _=difatStream.Read(nextDifatSectorBuffer, 0, 4);
  1001. nextSecId = BitConverter.ToInt32(nextDifatSectorBuffer, 0);
  1002. nFat++;
  1003. }
  1004. }
  1005. return result;
  1006. }
  1007. private List<Sector> GetNormalSectorChain(int secId)
  1008. {
  1009. var result = new List<Sector>();
  1010. int nextSecId = secId;
  1011. var fatSectors = GetFatSectorChain();
  1012. var fatStream = new StreamView(fatSectors, GetSectorSize(), fatSectors.Count * GetSectorSize(), null, SourceStream);
  1013. while (true)
  1014. {
  1015. if (nextSecId == Sector.Endofchain)
  1016. {
  1017. break;
  1018. }
  1019. if (nextSecId < 0)
  1020. {
  1021. throw new InvalidDataException($"Next Sector ID reference is below zero. NextID : {nextSecId}");
  1022. }
  1023. if (nextSecId >= _sectors.Count)
  1024. {
  1025. throw new InvalidDataException($"Next Sector ID reference an out of range sector. NextID : {nextSecId} while sector count {_sectors.Count}");
  1026. }
  1027. var s = _sectors[nextSecId];
  1028. if (s == null)
  1029. {
  1030. _sectors[nextSecId] = s = new Sector(GetSectorSize(), SourceStream)
  1031. {
  1032. Id = nextSecId,
  1033. Type = SectorType.Normal
  1034. };
  1035. }
  1036. result.Add(s);
  1037. fatStream.Seek(nextSecId * 4, SeekOrigin.Begin);
  1038. int next = fatStream.ReadInt32();
  1039. if (next != nextSecId)
  1040. {
  1041. nextSecId = next;
  1042. }
  1043. else
  1044. {
  1045. throw new InvalidDataException("Cyclic sector chain found. File is corrupted");
  1046. }
  1047. }
  1048. return result;
  1049. }
  1050. private List<Sector> GetMiniSectorChain(int secId)
  1051. {
  1052. List<Sector> result = [];
  1053. if (secId == Sector.Endofchain)
  1054. {
  1055. return result;
  1056. }
  1057. var miniFat = GetNormalSectorChain(_header.FirstMiniFATSectorID);
  1058. var miniStream = GetNormalSectorChain(RootEntry.StartSetc);
  1059. var miniFatView = new StreamView(miniFat, GetSectorSize(), _header.MiniFATSectorsNumber * Sector.MinisectorSize, null, SourceStream);
  1060. var miniStreamView = new StreamView(miniStream, GetSectorSize(), RootStorage.Size, null, SourceStream);
  1061. var miniFatReader = new BinaryReader(miniFatView);
  1062. var nextSecId = secId;
  1063. while (true)
  1064. {
  1065. if (nextSecId == Sector.Endofchain)
  1066. {
  1067. break;
  1068. }
  1069. var ms = new Sector(Sector.MinisectorSize, SourceStream);
  1070. ms.Id = nextSecId;
  1071. ms.Type = SectorType.Mini;
  1072. miniStreamView.Seek(nextSecId * Sector.MinisectorSize, SeekOrigin.Begin);
  1073. _=miniStreamView.Read(ms.GetData(), 0, Sector.MinisectorSize);
  1074. result.Add(ms);
  1075. miniFatView.Seek(nextSecId * 4, SeekOrigin.Begin);
  1076. nextSecId = miniFatReader.ReadInt32();
  1077. }
  1078. return result;
  1079. }
  1080. internal List<Sector> GetSectorChain(int secID, SectorType chainType)
  1081. {
  1082. switch (chainType)
  1083. {
  1084. case SectorType.DIFAT:
  1085. return GetDifatSectorChain();
  1086. case SectorType.FAT:
  1087. return GetFatSectorChain();
  1088. case SectorType.Normal:
  1089. return GetNormalSectorChain(secID);
  1090. case SectorType.Mini:
  1091. return GetMiniSectorChain(secID);
  1092. default:
  1093. throw new Exception("Unsupproted chain type");
  1094. }
  1095. }
  1096. public CfStorage RootStorage { get; private set; }
  1097. public int Version => _header.MajorVersion;
  1098. internal RBTree CreateNewTree()
  1099. {
  1100. return new RBTree();
  1101. }
  1102. internal RBTree GetChildrenTree(int sid)
  1103. {
  1104. var bst = new RBTree();
  1105. DoLoadChildren(bst, _directoryEntries[sid]);
  1106. return bst;
  1107. }
  1108. private void DoLoadChildren(RBTree bst, DirectoryEntry de)
  1109. {
  1110. if (de.Child == DirectoryEntry.Nostream)
  1111. {
  1112. return;
  1113. }
  1114. if (_directoryEntries[de.Child].StgType == StgType.StgInvalid)
  1115. {
  1116. return;
  1117. }
  1118. LoadSiblings(bst, _directoryEntries[de.Child]);
  1119. NullifyChildNodes(_directoryEntries[de.Child]);
  1120. bst.Insert(_directoryEntries[de.Child]);
  1121. }
  1122. private static void NullifyChildNodes(DirectoryEntry de)
  1123. {
  1124. de.Parent = null;
  1125. de.Left = null;
  1126. de.Right = null;
  1127. }
  1128. private readonly List<int> _levelSiDs = [];
  1129. private void LoadSiblings(RBTree bst, DirectoryEntry de)
  1130. {
  1131. _levelSiDs.Clear();
  1132. if (de.LeftSibling != DirectoryEntry.Nostream)
  1133. {
  1134. DoLoadSiblings(bst, _directoryEntries[de.LeftSibling]);
  1135. }
  1136. if (de.RightSibling == DirectoryEntry.Nostream)
  1137. {
  1138. return;
  1139. }
  1140. _levelSiDs.Add(de.RightSibling);
  1141. DoLoadSiblings(bst, _directoryEntries[de.RightSibling]);
  1142. }
  1143. private void DoLoadSiblings(RBTree bst, DirectoryEntry de)
  1144. {
  1145. if (ValidateSibling(de.LeftSibling))
  1146. {
  1147. _levelSiDs.Add(de.LeftSibling);
  1148. DoLoadSiblings(bst, _directoryEntries[de.LeftSibling]);
  1149. }
  1150. if (ValidateSibling(de.RightSibling))
  1151. {
  1152. _levelSiDs.Add(de.RightSibling);
  1153. DoLoadSiblings(bst, _directoryEntries[de.RightSibling]);
  1154. }
  1155. NullifyChildNodes(de);
  1156. bst.Insert(de);
  1157. }
  1158. private bool ValidateSibling(int sid)
  1159. {
  1160. if (sid == DirectoryEntry.Nostream)
  1161. {
  1162. return false;
  1163. }
  1164. if (sid >= _directoryEntries.Count)
  1165. {
  1166. return false;
  1167. }
  1168. if (_directoryEntries[sid].StgType == StgType.StgInvalid)
  1169. {
  1170. return false;
  1171. }
  1172. if (!Enum.IsDefined(typeof(StgType), _directoryEntries[sid].StgType))
  1173. {
  1174. return false;
  1175. }
  1176. if (_levelSiDs.Contains(sid))
  1177. {
  1178. throw new InvalidDataException("Cyclic reference of directory item");
  1179. }
  1180. return true;
  1181. }
  1182. private void LoadDirectories()
  1183. {
  1184. var directoryChain = GetSectorChain(_header.FirstDirectorySectorID, SectorType.Normal);
  1185. if (_header.FirstDirectorySectorID == Sector.Endofchain)
  1186. {
  1187. _header.FirstDirectorySectorID = directoryChain[0].Id;
  1188. }
  1189. var dirReader = new StreamView(directoryChain, GetSectorSize(), directoryChain.Count * GetSectorSize(), null, SourceStream);
  1190. while (dirReader.Position < directoryChain.Count * GetSectorSize())
  1191. {
  1192. var de = DirectoryEntry.New(string.Empty, StgType.StgInvalid, _directoryEntries);
  1193. de.Read(dirReader, Version);
  1194. }
  1195. }
  1196. internal byte[] GetData(CFStream cFStream)
  1197. {
  1198. AssertDisposed();
  1199. byte[] result;
  1200. var de = cFStream.DirEntry;
  1201. if (de.Size < _header.MinSizeStandardStream)
  1202. {
  1203. var miniView = new StreamView(GetSectorChain(de.StartSetc, SectorType.Mini), Sector.MinisectorSize, de.Size, null, SourceStream);
  1204. using var br = new BinaryReader(miniView);
  1205. result = br.ReadBytes((int) de.Size);
  1206. }
  1207. else
  1208. {
  1209. var sView = new StreamView(GetSectorChain(de.StartSetc, SectorType.Normal), GetSectorSize(), de.Size, null, SourceStream);
  1210. result = new byte[(int) de.Size];
  1211. _=sView.Read(result, 0, result.Length);
  1212. }
  1213. return result;
  1214. }
  1215. private static int Ceiling(double d) => (int) Math.Ceiling(d);
  1216. private readonly bool _closeStream = true;
  1217. internal bool IsClosed { get; private set; }
  1218. #region IDisposable Members
  1219. private readonly object _lockObject = new();
  1220. public void Dispose()
  1221. {
  1222. try
  1223. {
  1224. if (!IsClosed)
  1225. {
  1226. lock (_lockObject)
  1227. {
  1228. if (_sectors != null)
  1229. {
  1230. _sectors.Clear();
  1231. _sectors = null;
  1232. }
  1233. RootStorage = null;
  1234. _header = null;
  1235. _directoryEntries.Clear();
  1236. _directoryEntries = null;
  1237. }
  1238. if (SourceStream != null && _closeStream && !Configuration.HasFlag(CFSConfiguration.LeaveOpen))
  1239. {
  1240. SourceStream.Dispose();
  1241. }
  1242. }
  1243. }
  1244. finally
  1245. {
  1246. IsClosed = true;
  1247. }
  1248. GC.SuppressFinalize(this);
  1249. }
  1250. #endregion IDisposable Members
  1251. private List<DirectoryEntry> _directoryEntries = [];
  1252. internal DirectoryEntry RootEntry => _directoryEntries[0];
  1253. private void AssertDisposed()
  1254. {
  1255. if (IsClosed)
  1256. {
  1257. throw new ObjectDisposedException("Compound File closed: cannot access data");
  1258. }
  1259. }
  1260. }
  1261. #endregion Modified OpenMCDF
  1262. }