DatabaseRecordFragment.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.Text;
  10. using Utilities;
  11. namespace DiskAccessLibrary.LogicalDiskManager
  12. {
  13. // This class represents a single VBLK block (record can span multiple VBLK blocks)
  14. public class DatabaseRecordFragment
  15. {
  16. public const int HeaderLength = 16;
  17. public string Signature = "VBLK"; // VBLK
  18. public uint SequenceNumber; // each fragment have different SequenceNumber, SequenceNumber starts from 4 ( 0-3 are taken by the VMDB)
  19. public uint GroupNumber; // same for all fragments of the same record
  20. public ushort NumberInGroup; // (x of y), Zero-based
  21. public ushort FragmentCount; // Number of fragments in group
  22. public byte[] Data;
  23. public DatabaseRecordFragment()
  24. {
  25. }
  26. protected DatabaseRecordFragment(byte[] buffer)
  27. {
  28. Signature = ByteReader.ReadAnsiString(buffer, 0x00, 4);
  29. SequenceNumber = BigEndianConverter.ToUInt32(buffer, 0x04);
  30. GroupNumber = BigEndianConverter.ToUInt32(buffer, 0x08);
  31. NumberInGroup = BigEndianConverter.ToUInt16(buffer, 0x0C);
  32. FragmentCount = BigEndianConverter.ToUInt16(buffer, 0x0E);
  33. Data = new byte[buffer.Length - HeaderLength];
  34. Array.Copy(buffer, 0x10, Data, 0, buffer.Length - HeaderLength);
  35. }
  36. public byte[] GetBytes(int blockSize)
  37. {
  38. byte[] buffer = new byte[blockSize];
  39. ByteWriter.WriteAnsiString(buffer, 0, Signature, 4);
  40. BigEndianWriter.WriteUInt32(buffer, 0x04, SequenceNumber);
  41. BigEndianWriter.WriteUInt32(buffer, 0x08, GroupNumber);
  42. BigEndianWriter.WriteUInt16(buffer, 0x0C, NumberInGroup);
  43. BigEndianWriter.WriteUInt16(buffer, 0x0E, FragmentCount);
  44. ByteWriter.WriteBytes(buffer, 0x10, Data, Math.Min(Data.Length, blockSize - HeaderLength));
  45. return buffer;
  46. }
  47. public static DatabaseRecordFragment GetDatabaseRecordFragment(byte[] buffer)
  48. {
  49. string signature = ByteReader.ReadAnsiString(buffer, 0x00, 4);
  50. ushort fragmentCount = BigEndianConverter.ToUInt16(buffer, 0x0E);
  51. if (fragmentCount == 0 || signature != "VBLK")
  52. {
  53. return null;
  54. }
  55. else
  56. {
  57. return new DatabaseRecordFragment(buffer);
  58. }
  59. }
  60. /// <summary>
  61. /// Free fragment from record data (the result can be used to overwrite this fragment)
  62. /// </summary>
  63. public void Clear()
  64. {
  65. this.GroupNumber = 0;
  66. this.NumberInGroup = 0;
  67. this.FragmentCount = 0;
  68. this.Data = new byte[0];
  69. }
  70. }
  71. }