MultiSectorHelper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Text;
  11. using Utilities;
  12. namespace DiskAccessLibrary.FileSystems.NTFS
  13. {
  14. public class MultiSectorHelper
  15. {
  16. public const int BytesPerStride = 512;
  17. public static void DecodeSegmentBuffer(byte[] buffer, int offset, ushort updateSequenceNumber, List<byte[]> updateSequenceReplacementData)
  18. {
  19. // The USN will be written at the end of each 512-byte stride, even if the device has more (or less) than 512 bytes per sector.
  20. // http://msdn.microsoft.com/en-us/library/bb470212%28v=vs.85%29.aspx
  21. // First do validation check - make sure the USN matches on all sectors)
  22. for (int i = 0; i < updateSequenceReplacementData.Count; ++i)
  23. {
  24. if (updateSequenceNumber != LittleEndianConverter.ToUInt16(buffer, offset + (BytesPerStride * (i + 1)) - 2))
  25. {
  26. throw new InvalidDataException("Corrupt file system record found");
  27. }
  28. }
  29. // Now replace the USNs with the actual data from the UpdateSequenceReplacementData array
  30. for (int i = 0; i < updateSequenceReplacementData.Count; ++i)
  31. {
  32. Array.Copy(updateSequenceReplacementData[i], 0, buffer, offset + (BytesPerStride * (i + 1)) - 2, 2);
  33. }
  34. }
  35. public static List<byte[]> EncodeSegmentBuffer(byte[] buffer, int offset, int segmentLength, ushort updateSequenceNumber)
  36. {
  37. int numberOfStrides = segmentLength / BytesPerStride;
  38. List<byte[]> updateSequenceReplacementData = new List<byte[]>();
  39. // Read in the bytes that are replaced by the USN
  40. for (int i = 0; i < numberOfStrides; i++)
  41. {
  42. byte[] endOfSectorBytes = new byte[2];
  43. endOfSectorBytes[0] = buffer[offset + (BytesPerStride * (i + 1)) - 2];
  44. endOfSectorBytes[1] = buffer[offset + (BytesPerStride * (i + 1)) - 2];
  45. updateSequenceReplacementData.Add(endOfSectorBytes);
  46. }
  47. // Overwrite the bytes that are replaced with the USN
  48. for (int i = 0; i < updateSequenceReplacementData.Count; i++)
  49. {
  50. Array.Copy(LittleEndianConverter.GetBytes(updateSequenceNumber), 0, buffer, offset + (BytesPerStride * (i + 1)) - 2, 2);
  51. }
  52. return updateSequenceReplacementData;
  53. }
  54. }
  55. }