-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSector.cs
202 lines (159 loc) · 4.74 KB
/
Sector.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Collections.Specialized;
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is OpenMCDF - Compound Document Format library.
The Initial Developer of the Original Code is Federico Blaseotto.
*/
namespace OpenMcdf
{
internal enum SectorType
{
Normal, Mini, FAT, DIFAT, RangeLockSector, Directory
}
internal class Sector : IDisposable
{
public static int MINISECTOR_SIZE = 64;
public const int FREESECT = unchecked((int)0xFFFFFFFF);
public const int ENDOFCHAIN = unchecked((int)0xFFFFFFFE);
public const int FATSECT = unchecked((int)0xFFFFFFFD);
public const int DIFSECT = unchecked((int)0xFFFFFFFC);
private bool dirtyFlag = false;
public bool DirtyFlag
{
get { return dirtyFlag; }
set { dirtyFlag = value; }
}
public bool IsStreamed
{
get { return (stream != null && size != MINISECTOR_SIZE) ? (this.id * size) + size < stream.Length : false; }
}
private int size = 0;
private Stream stream;
public Sector(int size, Stream stream)
{
this.size = size;
this.stream = stream;
}
public Sector(int size, byte[] data)
{
this.size = size;
this.data = data;
this.stream = null;
}
public Sector(int size)
{
this.size = size;
this.data = null;
this.stream = null;
}
private SectorType type;
internal SectorType Type
{
get { return type; }
set { type = value; }
}
private int id = -1;
public int Id
{
get { return id; }
set
{
id = value;
}
}
public int Size
{
get
{
return size;
}
}
private byte[] data;
public byte[] GetData()
{
if (this.data == null)
{
data = new byte[size];
if (IsStreamed)
{
stream.Seek((long)size + (long)this.id * (long)size, SeekOrigin.Begin);
stream.Read(data, 0, size);
}
}
return data;
}
//public void SetSectorData(byte[] b)
//{
// this.data = b;
//}
//public void FillData(byte b)
//{
// if (data != null)
// {
// for (int i = 0; i < data.Length; i++)
// {
// data[i] = b;
// }
// }
//}
public void ZeroData()
{
data = new byte[size];
dirtyFlag = true;
}
internal void ReleaseData()
{
this.data = null;
}
private object lockObject = new Object();
/// <summary>
/// When called from user code, release all resources, otherwise, in the case runtime called it,
/// only unmanagd resources are released.
/// </summary>
/// <param name="disposing">If true, method has been called from User code, if false it's been called from .net runtime</param>
protected virtual void Dispose(bool disposing)
{
try
{
if (!_disposed)
{
lock (lockObject)
{
if (disposing)
{
// Call from user code...
}
this.data = null;
this.dirtyFlag = false;
this.id = Sector.ENDOFCHAIN;
this.size = 0;
}
}
}
finally
{
_disposed = true;
}
}
#region IDisposable Members
private bool _disposed;//false
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}