Ejemplo n.º 1
0
        public static PartitionEntry Parse(byte[] entry)
        {
            if (entry.Length != 16)
            {
                throw new Exception("Buffer length invalid.");
            }

            using var ms = new MemoryStream(entry);
            using var r  = new BinaryReader(ms);
            var part = new PartitionEntry();

            part.PartStatus = r.ReadByte();
            part.StartHead  = r.ReadByte();
            var temp = r.ReadByte();

            part.StartSector   = GetBitValue(temp, 0, 6);
            part.StartTrackMsb = GetBitValue(temp, 6, 2);
            part.StartTrackLsb = r.ReadByte();
            part.Type          = r.ReadByte();
            part.EndHead       = r.ReadByte();
            temp                 = r.ReadByte();
            part.EndSector       = GetBitValue(temp, 0, 6);
            part.EndTrackMsb     = GetBitValue(temp, 6, 2);
            part.EndTrackLsb     = r.ReadByte();
            part.Lba             = r.ReadUInt32();
            part.NumberOfSectors = r.ReadUInt32();

            return(part);
        }
Ejemplo n.º 2
0
        public static Mbr Parse(byte[] rawMbr)
        {
            if (rawMbr.Length != 512)
            {
                throw new Exception("Invalid mbr buffer length");
            }

            var mbr = new Mbr();

            using var s  = new MemoryStream(rawMbr);
            using var r  = new BinaryReader(s);
            mbr.Legacy   = r.ReadBytes(mbr.Legacy.Length);
            mbr.Sig      = r.ReadUInt32();
            mbr.Reserved = r.ReadBytes(mbr.Reserved.Length);

            for (var i = 0; i < 4; i++)
            {
                var bytes = r.ReadBytes(16);
                mbr.Partitions.Add(PartitionEntry.Parse(bytes));
            }

            mbr.Magic = r.ReadUInt16();
            return(mbr);
        }