DataRecord.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Copyright (C) 2014-2016 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. namespace DiskAccessLibrary.FileSystems.NTFS
  11. {
  12. // Data attribute can be either resident or non-resident attribute
  13. // This represents an AttributeRecord's data
  14. public class DataRecord
  15. {
  16. AttributeRecord m_record;
  17. public DataRecord(AttributeRecord record)
  18. {
  19. m_record = record;
  20. }
  21. public byte[] ReadDataCluster(NTFSVolume volume, long clusterVCN)
  22. {
  23. return ReadDataClusters(volume, clusterVCN, 1);
  24. }
  25. public byte[] ReadDataClusters(NTFSVolume volume, long clusterVCN, int count)
  26. {
  27. if (m_record is NonResidentAttributeRecord)
  28. {
  29. return ((NonResidentAttributeRecord)m_record).ReadDataClusters(volume, clusterVCN, count);
  30. }
  31. else
  32. {
  33. long numberOfClusters = (long)Math.Ceiling((double)((ResidentAttributeRecord)m_record).Data.Length / volume.BytesPerCluster);
  34. long highestVCN = Math.Max(numberOfClusters - 1, 0);
  35. if (clusterVCN < 0 || clusterVCN > highestVCN)
  36. {
  37. throw new ArgumentOutOfRangeException("Cluster VCN is not within the valid range");
  38. }
  39. long offset = clusterVCN * volume.BytesPerCluster;
  40. int bytesToRead;
  41. // last cluster could be partial
  42. if (clusterVCN + count < numberOfClusters)
  43. {
  44. bytesToRead = count * volume.BytesPerCluster;
  45. }
  46. else
  47. {
  48. bytesToRead = (int)(((ResidentAttributeRecord)m_record).Data.Length - offset);
  49. }
  50. byte[] data = new byte[bytesToRead];
  51. Array.Copy(((ResidentAttributeRecord)m_record).Data, offset, data, 0, bytesToRead);
  52. return data;
  53. }
  54. }
  55. public void WriteDataCluster(NTFSVolume volume, long clusterVCN, byte[] data)
  56. {
  57. WriteDataClusters(volume, clusterVCN, data);
  58. }
  59. public void WriteDataClusters(NTFSVolume volume, long clusterVCN, byte[] data)
  60. {
  61. if (m_record is NonResidentAttributeRecord)
  62. {
  63. ((NonResidentAttributeRecord)m_record).WriteDataClusters(volume, clusterVCN, data);
  64. }
  65. else
  66. {
  67. int numberOfClusters = (int)Math.Ceiling((double)((ResidentAttributeRecord)m_record).Data.Length / volume.BytesPerCluster);
  68. int count = (int)Math.Ceiling((double)data.Length / volume.BytesPerCluster);
  69. long highestVCN = Math.Max(numberOfClusters - 1, 0);
  70. if (clusterVCN < 0 || clusterVCN > highestVCN)
  71. {
  72. throw new ArgumentOutOfRangeException("Cluster VCN is not within the valid range");
  73. }
  74. long offset = clusterVCN * volume.BytesPerCluster;
  75. Array.Copy(data, 0, ((ResidentAttributeRecord)m_record).Data, offset, data.Length);
  76. }
  77. }
  78. public void ExtendRecord(NTFSVolume volume, ulong additionalLength)
  79. {
  80. ulong currentSize = this.DataRealSize;
  81. if (m_record is NonResidentAttributeRecord)
  82. {
  83. ((NonResidentAttributeRecord)m_record).ExtendRecord(volume, additionalLength);
  84. }
  85. else
  86. {
  87. // If the resident data record becomes too long, it will be replaced with a non-resident data record when the file record will be saved
  88. byte[] data = ((ResidentAttributeRecord)m_record).Data;
  89. int currentLength = data.Length;
  90. byte[] temp = new byte[currentLength + (int)additionalLength];
  91. Array.Copy(data, temp, data.Length);
  92. ((ResidentAttributeRecord)m_record).Data = temp;
  93. }
  94. }
  95. public ulong GetDataAllocatedSize(int bytesPerCluster)
  96. {
  97. if (m_record is NonResidentAttributeRecord)
  98. {
  99. return (ulong)(((NonResidentAttributeRecord)m_record).DataClusterCount * bytesPerCluster);
  100. }
  101. else
  102. {
  103. return (ulong)((ResidentAttributeRecord)m_record).Data.Length;
  104. }
  105. }
  106. public ulong DataRealSize
  107. {
  108. get
  109. {
  110. if (m_record is NonResidentAttributeRecord)
  111. {
  112. return ((NonResidentAttributeRecord)m_record).FileSize;
  113. }
  114. else
  115. {
  116. return (ulong)((ResidentAttributeRecord)m_record).Data.Length;
  117. }
  118. }
  119. }
  120. public long DataClusterCount
  121. {
  122. get
  123. {
  124. if (m_record is NonResidentAttributeRecord)
  125. {
  126. return ((NonResidentAttributeRecord)m_record).DataClusterCount;
  127. }
  128. else
  129. {
  130. // can it be more than 1?
  131. return 1;
  132. }
  133. }
  134. }
  135. public AttributeRecord Record
  136. {
  137. get
  138. {
  139. return m_record;
  140. }
  141. }
  142. }
  143. }