Skip to content

Commit 9e9ece8

Browse files
committed
Updated Filters
Added option to revert changes made to a file Added the ability to clear out an unused sector on the disk if it contains data
1 parent 1fa8eda commit 9e9ece8

File tree

9 files changed

+488
-243
lines changed

9 files changed

+488
-243
lines changed

DiskImageTool/ComboFileType.vb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
HasLongFileNames = 32
88
HasInvalidDirectoryEntries = 64
99
ModifiedFiles = 128
10+
UnusedClusters = 256
1011
End Enum
1112

1213
Public Class ComboFileType
@@ -56,6 +57,8 @@ Public Class ComboFileType
5657
Return "Has Invalid Directory Entries"
5758
Case FilterTypes.ModifiedFiles
5859
Return "Modified Files"
60+
Case FilterTypes.UnusedClusters
61+
Return "Unused Clusters with Data"
5962
Case Else
6063
Return ""
6164
End Select

DiskImageTool/DiskImage/BootSector.vb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
End If
8181
End Function
8282

83+
Public Function ImageSize() As UInteger
84+
Return SectorCount() * BytesPerSector
85+
End Function
86+
8387
Public Property BootStrapCode() As Byte()
8488
Get
8589
Return _Parent.GetBytes(BootSectorOffset.BootStrapCode, BootSectorSize.BootStrapCode)

DiskImageTool/DiskImage/DirectoryEntry.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ Namespace DiskImage
352352
End Function
353353

354354
Public Function HasInvalidFileSize() As Boolean
355-
Return FileSize > _Parent.BootSector.SectorCount * _Parent.BootSector.BytesPerSector
355+
Return FileSize > _Parent.BootSector.ImageSize
356356
End Function
357357

358358
Public Function HasLastAccessDate() As Boolean

DiskImageTool/DiskImage/Disk.vb

Lines changed: 107 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
Private ReadOnly _FilePath As String
1919
Private _Modified As Boolean
2020
Private ReadOnly _Modifications As Hashtable
21+
Private ReadOnly _OriginalData As Hashtable
2122

2223
Sub New(FilePath As String)
2324
_Modifications = New Hashtable()
25+
_OriginalData = New Hashtable()
2426
_Modified = False
2527
_FilePath = FilePath
2628
_FileBytes = System.IO.File.ReadAllBytes(FilePath)
@@ -104,32 +106,59 @@
104106
End Function
105107

106108
Public Sub SetBytes(Value As UShort, Offset As UInteger)
109+
If Not _OriginalData.ContainsKey(Offset) Then
110+
_OriginalData.Item(Offset) = GetBytesShort(Offset)
111+
End If
112+
107113
Array.Copy(BitConverter.GetBytes(Value), 0, _FileBytes, Offset, 2)
114+
108115
_Modified = True
109116
_Modifications.Item(Offset) = Value
110117
End Sub
111118

112119
Public Sub SetBytes(Value As UInteger, Offset As UInteger)
120+
If Not _OriginalData.ContainsKey(Offset) Then
121+
_OriginalData.Item(Offset) = GetBytesInteger(Offset)
122+
End If
123+
113124
Array.Copy(BitConverter.GetBytes(Value), 0, _FileBytes, Offset, 4)
125+
114126
_Modified = True
115127
_Modifications.Item(Offset) = Value
116128
End Sub
117129

118130
Public Sub SetBytes(Value As Byte, Offset As UInteger)
131+
If Not _OriginalData.ContainsKey(Offset) Then
132+
_OriginalData.Item(Offset) = GetByte(Offset)
133+
End If
134+
119135
_FileBytes(Offset) = Value
136+
120137
_Modified = True
121138
_Modifications.Item(Offset) = Value
122139
End Sub
123140

124141
Public Sub SetBytes(Value() As Byte, Offset As UInteger)
142+
If Not _OriginalData.ContainsKey(Offset) Then
143+
_OriginalData.Item(Offset) = GetBytes(Offset, Value.Length)
144+
End If
145+
125146
Array.Copy(Value, 0, _FileBytes, Offset, Value.Length)
147+
126148
_Modified = True
127149
_Modifications.Item(Offset) = Value
128150
End Sub
129151

130152
Public Sub SetBytes(Value() As Byte, Offset As UInteger, Size As UInteger, Padding As Byte)
131-
Disk.ResizeArray(Value, Size, Padding)
153+
If Not _OriginalData.ContainsKey(Offset) Then
154+
_OriginalData.Item(Offset) = GetBytes(Offset, Size)
155+
End If
156+
157+
If Value.Length <> Size Then
158+
Disk.ResizeArray(Value, Size, Padding)
159+
End If
132160
Array.Copy(Value, 0, _FileBytes, Offset, Size)
161+
133162
_Modified = True
134163
_Modifications.Item(Offset) = Value
135164
End Sub
@@ -192,17 +221,44 @@
192221
Return Count
193222
End Function
194223

224+
Public Function GetFillCharacter() As Byte
225+
Dim Offset = Math.Min(_FileBytes.Length, _BootSector.ImageSize)
226+
Dim FillChar = _FileBytes(Offset - 1)
227+
If FillChar = &H0 Then
228+
For Counter As Integer = Offset - 1 To Offset - _BootSector.BytesPerSector Step -1
229+
If _FileBytes(Counter) <> &H0 Then
230+
FillChar = &HFF
231+
Exit For
232+
End If
233+
Next
234+
ElseIf FillChar <> &HF6 Then
235+
FillChar = &HFF
236+
End If
237+
238+
Return FillChar
239+
End Function
240+
195241
Public Function HasUnusedClustersWithData() As Boolean
196242
If _FreeSpaceClusterStart > 0 Then
197243
Dim ClusterCount As UInteger = _BootSector.NumberOfFATEntries + 1
198244

199-
For Counter = _FreeSpaceClusterStart To ClusterCount
200-
Dim Data = GetBytes(ClusterToOffset(Counter), _BootSector.BytesPerCluster)
201-
For Each B In Data
202-
If B <> &HF6 And B <> &H0 Then
245+
For Cluster = _FreeSpaceClusterStart To ClusterCount
246+
Dim Offset = ClusterToOffset(Cluster)
247+
Dim Length = _BootSector.BytesPerCluster
248+
If _FileBytes.Length >= Offset + Length Then
249+
Dim Data = GetBytes(Offset, Length)
250+
Dim EmptyByte As Byte = Data(0)
251+
If EmptyByte <> &HF6 And EmptyByte <> &H0 Then
203252
Return True
204253
End If
205-
Next
254+
For Each B In Data
255+
If B <> EmptyByte Then
256+
Return True
257+
End If
258+
Next
259+
Else
260+
Exit For
261+
End If
206262
Next
207263
End If
208264

@@ -214,27 +270,39 @@
214270
Dim Found As Boolean
215271

216272
If _FreeSpaceClusterStart > 0 Then
217-
Dim SectorStart As UInteger = ClusterToSector(_FreeSpaceClusterStart)
218-
Dim SectorCount As UInteger = _BootSector.SectorCount - 1
219-
220-
For Counter = SectorStart To SectorCount
221-
Dim Block As DataBlock
222-
With Block
223-
.Cluster = SectorToCluster(Counter)
224-
.Sector = Counter
225-
.Offset = SectorToOffset(Counter)
226-
.Data = GetBytes(.Offset, _BootSector.BytesPerSector)
227-
End With
273+
Dim ClusterCount As UInteger = _BootSector.NumberOfFATEntries + 1
274+
For Cluster = _FreeSpaceClusterStart To ClusterCount
228275
Found = False
229-
For Each B In Block.Data
230-
If B <> &HF6 And B <> &H0 Then
231-
Found = True
232-
Exit For
276+
Dim Sector = ClusterToSector(Cluster)
277+
For Counter = 0 To _BootSector.SectorsPerCluster - 1
278+
Dim Offset = SectorToOffset(Sector + Counter)
279+
Dim Length = _BootSector.BytesPerSector
280+
If _FileBytes.Length >= Offset + Length Then
281+
Dim Block As DataBlock
282+
With Block
283+
.Cluster = Cluster
284+
.Sector = Sector + Counter
285+
.Offset = Offset
286+
.Data = GetBytes(.Offset, Length)
287+
End With
288+
If Not Found Then
289+
Dim EmptyByte As Byte = Block.Data(0)
290+
If EmptyByte <> &HF6 And EmptyByte <> &H0 Then
291+
Found = True
292+
Else
293+
For Each B In Block.Data
294+
If B <> EmptyByte Then
295+
Found = True
296+
Exit For
297+
End If
298+
Next
299+
End If
300+
End If
301+
If Found Then
302+
Result.Add(Block)
303+
End If
233304
End If
234305
Next
235-
If Found Then
236-
Result.Add(Block)
237-
End If
238306
Next
239307
End If
240308

@@ -295,8 +363,22 @@
295363

296364
Public Sub SaveFile(FilePath As String)
297365
System.IO.File.WriteAllBytes(FilePath, _FileBytes)
298-
_Modified = False
366+
_OriginalData.Clear()
299367
_Modifications.Clear()
368+
_Modified = False
300369
End Sub
370+
371+
Public Function RevertChanges() As Boolean
372+
Dim Result As Boolean = False
373+
374+
If _OriginalData.Count > 0 Then
375+
ApplyModifications(_OriginalData)
376+
_OriginalData.Clear()
377+
_Modifications.Clear()
378+
_Modified = False
379+
End If
380+
381+
Return Result
382+
End Function
301383
End Class
302384
End Namespace

0 commit comments

Comments
 (0)