123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace DiskAccessLibrary.FileSystems.NTFS
- {
- public class DataRun
- {
-
-
- public long RunLength;
- public long RunOffset;
- public bool IsSparse;
-
- public int Read(byte[] buffer, int offset)
- {
- int runOffsetSize = buffer[offset] >> 4;
- int runLengthSize = buffer[offset] & 0x0F;
- RunLength = ReadVarLong(ref buffer, offset + 1, runLengthSize);
- RunOffset = ReadVarLong(ref buffer, offset + 1 + runLengthSize, runOffsetSize);
- IsSparse = runOffsetSize == 0;
- return 1 + runLengthSize + runOffsetSize;
- }
- public byte[] GetBytes()
- {
- byte[] buffer = new byte[RecordLength];
- int runLengthSize = WriteVarLong(buffer, 1, RunLength);
- int runOffsetSize;
- if (IsSparse)
- {
- runOffsetSize = 0;
- }
- else
- {
- runOffsetSize = WriteVarLong(buffer, 1 + runLengthSize, RunOffset);
- }
- buffer[0] = (byte)((runLengthSize & 0x0F) | ((runOffsetSize << 4) & 0xF0));
- return buffer;
- }
- private static long ReadVarLong(ref byte[] buffer, int offset, int size)
- {
- ulong val = 0;
- bool signExtend = false;
- for (int i = 0; i < size; ++i)
- {
- byte b = buffer[offset + i];
- val = val | (((ulong)b) << (i * 8));
- signExtend = (b & 0x80) != 0;
- }
- if (signExtend)
- {
- for (int i = size; i < 8; ++i)
- {
- val = val | (((ulong)0xFF) << (i * 8));
- }
- }
- return (long)val;
- }
- private static int WriteVarLong(byte[] buffer, int offset, long val)
- {
- bool isPositive = val >= 0;
- int pos = 0;
- do
- {
- buffer[offset + pos] = (byte)(val & 0xFF);
- val >>= 8;
- pos++;
- }
- while (val != 0 && val != -1);
-
-
- if (isPositive && (buffer[offset + pos - 1] & 0x80) != 0)
- {
- buffer[offset + pos] = 0;
- pos++;
- }
- else if (!isPositive && (buffer[offset + pos - 1] & 0x80) != 0x80)
- {
- buffer[offset + pos] = 0xFF;
- pos++;
- }
- return pos;
- }
- private static int VarLongSize(long val)
- {
- bool isPositive = val >= 0;
- bool lastByteHighBitSet = false;
- int len = 0;
- do
- {
- lastByteHighBitSet = (val & 0x80) != 0;
- val >>= 8;
- len++;
- }
- while (val != 0 && val != -1);
- if ((isPositive && lastByteHighBitSet) || (!isPositive && !lastByteHighBitSet))
- {
- len++;
- }
- return len;
- }
-
-
-
- public int RecordLength
- {
- get
- {
- int runLengthSize = VarLongSize(RunLength);
- int runOffsetSize = VarLongSize(RunOffset);
- return 1 + runLengthSize + runOffsetSize;
- }
- }
- }
- }
|