123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- namespace IsoFilePatcher.Iso9660
- {
- [DebuggerDisplay("\\{ Name={Name} {ChildPathEntries} \\}")]
- internal class PathEntry
- {
- public static PathEntry Parse(Stream stream, VolumeDescriptor volumeDescriptor)
- {
- var obj = new PathEntry();
- obj.VolumeDescriptor = volumeDescriptor;
- obj.Read(stream);
- return obj;
- }
- public VolumeDescriptor VolumeDescriptor { get; set; }
- private void Read(Stream stream)
- {
- var begin = stream.Position; // For DEBUGGING watch
- var len = stream.ReadByte(); //this byte included in size
- if (len == 0)
- {
- InvalidData = true;
- return;
- }
- int nameByteLen;
- using (var br = new BinaryReader(stream, Encoding.ASCII, true))
- {
- ExtendedAttributeRecordlength = br.ReadByte();
- ExtentLba = br.ReadUInt32LsbMsb();
- DataLength = br.ReadUInt32LsbMsb();
- Date = br.ReadFileRecordDate();
- FileFlags = br.ReadByte();
- FileUnitSize = br.ReadByte();
- InterleaveGapSize = br.ReadByte();
- VolumeSequenceNumber = br.ReadUInt16LsbMsb();
- nameByteLen = br.ReadByte();
- Name = br.ReadString(nameByteLen, VolumeDescriptor.Encoding);
- if (Name.EndsWith(";1"))
- Name = Name.Substring(0, Name.Length - 2);
- if (nameByteLen % 2 == 0)
- {
- PaddingByte = br.ReadByte();
- if (PaddingByte != 0)
- {
- throw new InvalidDataException("Invalid padding byte, data error?");
- }
- }
- var remain = len - (stream.Position - begin);
- if (remain > 0)
- RemainBytes = br.ReadBytes((int)remain);
- }
- }
- public byte? PaddingByte { get; set; }
- public byte[] RemainBytes { get; set; }
- public bool InvalidData { get; set; }
- public string Name { get; set; }
- public ushort VolumeSequenceNumber { get; set; }
- public byte InterleaveGapSize { get; set; }
- public byte FileUnitSize { get; set; }
- public byte FileFlags { get; set; }
- public DateTime Date { get; set; }
- public uint DataLength { get; set; }
- public uint ExtentLba { get; set; }
- public byte ExtendedAttributeRecordlength { get; set; }
- public bool IsHidden => (FileFlags & 1) != 0;
- public bool IsDirectory => (FileFlags & (1 << 1)) != 0;
- public bool IsAssociatedFile => (FileFlags & (1 << 2)) != 0;
- public PathEntry[] ChildPathEntries { get; set; }
- public void ScanChilds(Stream stream)
- {
- stream.Seek(ExtentLba * VolumeDescriptor.LogicalBlockSize, SeekOrigin.Begin);
- var list = new List<PathEntry>();
- var endofData = stream.Position + DataLength;
- while (stream.Position < endofData)
- {
- var item = Parse(stream, VolumeDescriptor);
- if (VolumeDescriptor.Type == 1)
- if (item.Name == "\0" || item.Name == "\u0001") continue;
- if(VolumeDescriptor.Type==2)
- if (item.Name == "�" || item.Name == "\u0001") continue;
- if (item.InvalidData) break;
- list.Add(item);
- }
- ChildPathEntries = list.ToArray();
- list.ForEach(p =>
- {
- if (p.IsDirectory)
- p.ScanChilds(stream);
- });
- }
- }
- }
|