-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISONodeRecord.cs
More file actions
208 lines (172 loc) · 6.07 KB
/
ISONodeRecord.cs
File metadata and controls
208 lines (172 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ISOParser {
/// <summary>
/// Class to represent the file/directory information read from the disk.
/// </summary>
public class ISONodeRecord {
#region Constants
/// <summary>
/// String representing the current directory entry
/// </summary>
public const string CURRENT_DIRECTORY = ".";
/// <summary>
/// String representing the parent directory entry
/// </summary>
public const string PARENT_DIRECTORY = "..";
#endregion
#region Public Properties
/// <summary>
/// The length of the record in bytes.
/// </summary>
public byte Length;
/// <summary>
/// The file offset of the data for this file/directory (in sectors).
/// </summary>
public long OffsetOfData;
/// <summary>
/// The length of the data for this file/directory (in bytes).
/// </summary>
public long LengthOfData;
/// <summary>
/// The file/directory creation year since 1900.
/// </summary>
public byte Year;
/// <summary>
/// The file/directory creation month.
/// </summary>
public byte Month;
/// <summary>
/// The file/directory creation day.
/// </summary>
public byte Day;
/// <summary>
/// The file/directory creation hour.
/// </summary>
public byte Hour;
/// <summary>
/// The file/directory creation minute.
/// </summary>
public byte Minute;
/// <summary>
/// The file/directory creation second.
/// </summary>
public byte Second;
/// <summary>
/// The file time offset from GMT.
/// </summary>
public byte TimeZoneOffset;
/// <summary>
/// Flags representing the attributes of this file/directory.
/// </summary>
public byte Flags;
/// <summary>
/// The length of the file/directory name.
/// </summary>
public byte NameLength;
/// <summary>
/// The file/directory name.
/// </summary>
public string Name;
#endregion
#region Construction
/// <summary>
/// Constructor
/// </summary>
public ISONodeRecord() {
// Set initial values
this.Length = 0;
this.OffsetOfData = 0;
this.LengthOfData = 0;
this.Year = 0;
this.Month = 0;
this.Day = 0;
this.Hour = 0;
this.Minute = 0;
this.Second = 0;
this.TimeZoneOffset = 0;
this.Flags = 0;
this.NameLength = 0;
this.Name = null;
}
#endregion
#region File/Directory Methods
/// <summary>
/// Return true if the record represents a file.
/// </summary>
/// <returns>True if a file.</returns>
public bool IsFile() {
return ((this.Flags >> 1) & 0x01) == 0;
}
/// <summary>
/// Return true if the record represents a directory.
/// </summary>
/// <returns>True if a directory.</returns>
public bool IsDirectory() {
return ((this.Flags >> 1) & 0x01) == 1;
}
#endregion
#region Parsing
/// <summary>
/// Parse the record from an array and offset.
/// </summary>
/// <param name="data">The array to parse from.</param>
/// <param name="cursor">The offset to start parsing at.</param>
public void Parse(byte[] data, int cursor) {
// Put the array into a memory stream and pass to the main parsing function
MemoryStream s = new MemoryStream(data);
s.Seek(cursor, SeekOrigin.Begin);
this.Parse(s);
}
/// <summary>
/// Parse the node record from the given stream.
/// </summary>
/// <param name="s">The stream to parse from.</param>
public void Parse(Stream s) {
EndianBitConverter bc = EndianBitConverter.CreateForLittleEndian();
long startPosition = s.Position;
byte[] buffer = new byte[ISOFile.SECTOR_SIZE];
// Get the length
s.Read(buffer, 0, 1);
this.Length = buffer[0];
// "CD001" + 0x01
s.Read(buffer, 0, 1);
// Read Data Offset
s.Read(buffer, 0, 8);
this.OffsetOfData = (long)bc.ToInt32(buffer);
// Read Data Length
s.Read(buffer, 0, 8);
this.LengthOfData = (long)bc.ToInt32(buffer);
// Read the time and flags
s.Read(buffer, 0, 8);
this.Year = buffer[0];
this.Month = buffer[1];
this.Day = buffer[2];
this.Hour = buffer[3];
this.Minute = buffer[4];
this.Second = buffer[5];
this.TimeZoneOffset = buffer[6];
this.Flags = buffer[7];
s.Read(buffer, 0, 6);
// Read the name length
s.Read(buffer, 0, 1);
this.NameLength = buffer[0];
// Read the directory name
s.Read(buffer, 0, this.NameLength);
if (this.NameLength == 1 && (buffer[0] == 0 || buffer[0] == 1)) {
if (buffer[0] == 0)
this.Name = ISONodeRecord.CURRENT_DIRECTORY;
else
this.Name = ISONodeRecord.PARENT_DIRECTORY;
}
else {
this.Name = ASCIIEncoding.ASCII.GetString(buffer, 0, this.NameLength);
}
// Seek to end
s.Seek(startPosition + this.Length, SeekOrigin.Begin);
}
#endregion
}
}