-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawFile.cs
More file actions
328 lines (265 loc) · 9.35 KB
/
RawFile.cs
File metadata and controls
328 lines (265 loc) · 9.35 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BrickVault
{
public class RawFile : IDisposable
{
public Stream fileStream;
private RawFile() { }
public RawFile(Stream stream) { fileStream = stream; }
public string FileLocation { get; private set; }
public RawFile(string fileLocation)
{
fileStream = File.Open(fileLocation, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
FileLocation = fileLocation;
}
/// <summary>
/// Creates a file, meaning that the length is reset to 0.
/// Ideal when writing to a file, that may be shorter than the original.
/// </summary>
/// <param name="fileLocation"></param>
/// <returns></returns>
public static RawFile Create(string fileLocation)
{
RawFile createOnly = new RawFile();
createOnly.fileStream = File.Create(fileLocation);
createOnly.FileLocation = fileLocation;
return createOnly;
}
private bool disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
fileStream?.Flush();
fileStream?.Dispose(); // Also flushes
}
disposed = true;
}
}
~RawFile()
{
Dispose(false);
}
public RawFile CreateView()
{
return new RawFile(FileLocation);
}
public long Position => fileStream.Position;
public long Seek(long offset, SeekOrigin origin) => fileStream.Seek(offset, origin);
public RawFile CreateMemoryFile(uint size)
{
var mem = new MemoryStream((int)size); // pre-size for efficiency
var buffer = new byte[81920]; // 80 KB buffer, same as CopyTo's default
long remaining = size;
while (remaining > 0)
{
int toRead = (int)Math.Min(buffer.Length, remaining);
int read = fileStream.Read(buffer, 0, toRead);
if (read <= 0)
throw new EndOfStreamException("Unexpected end of file.");
mem.Write(buffer, 0, read);
remaining -= read;
}
mem.Position = 0; // rewind for reading
var memFile = new RawFile(mem);
memFile.FileLocation = FileLocation;
return memFile;
}
private byte[] ReadBlock(int size, bool bigEndian)
{
byte[] data = new byte[size];
fileStream.Read(data, 0, size);
if (bigEndian)
{
Array.Reverse(data);
}
return data;
}
private void WriteBlock(byte[] block, bool bigEndian)
{
if (bigEndian)
{
Array.Reverse(block);
}
fileStream.Write(block);
}
public byte ReadByte()
{
return (byte)fileStream.ReadByte();
}
public void WriteByte(byte value) => fileStream.WriteByte(value);
public short ReadShort(bool bigEndian = false)
{
return BitConverter.ToInt16(ReadBlock(2, bigEndian));
}
public void WriteShort(short toWrite, bool bigEndian = false)
{
WriteBlock(BitConverter.GetBytes(toWrite), bigEndian);
}
public ushort ReadUShort(bool bigEndian = false)
{
return BitConverter.ToUInt16(ReadBlock(2, bigEndian));
}
public void WriteUShort(ushort toWrite, bool bigEndian = false)
{
WriteBlock(BitConverter.GetBytes(toWrite), bigEndian);
}
public int ReadInt(bool bigEndian = false)
{
return BitConverter.ToInt32(ReadBlock(4, bigEndian));
}
public void WriteInt(int toWrite, bool bigEndian = false)
{
WriteBlock(BitConverter.GetBytes(toWrite), bigEndian);
}
public float ReadFloat(bool bigEndian = false)
{
return BitConverter.ToSingle(ReadBlock(4, bigEndian));
}
public void WriteFloat(float toWrite, bool bigEndian = false)
{
WriteBlock(BitConverter.GetBytes(toWrite), bigEndian);
}
public uint ReadUInt(bool bigEndian = false)
{
return BitConverter.ToUInt32(ReadBlock(4, bigEndian));
}
public void WriteUInt(uint toWrite, bool bigEndian = false)
{
WriteBlock(BitConverter.GetBytes(toWrite), bigEndian);
}
public long ReadLong(bool bigEndian = false)
{
return BitConverter.ToInt64(ReadBlock(8, bigEndian));
}
public void WriteLong(long toWrite, bool bigEndian = false)
{
WriteBlock(BitConverter.GetBytes(toWrite), bigEndian);
}
public ulong ReadULong(bool bigEndian = false)
{
return BitConverter.ToUInt64(ReadBlock(8, bigEndian));
}
public void WriteULong(ulong toWrite, bool bigEndian = false)
{
WriteBlock(BitConverter.GetBytes(toWrite), bigEndian);
}
public string ReadString(int length)
{
if (length == 0) return string.Empty;
byte[] array = new byte[length];
fileStream.Read(array, 0, length);
if (array[array.Length - 1] == 0)
{
return Encoding.Default.GetString(array, 0, array.Length - 1);
}
return Encoding.Default.GetString(array);
}
public void WriteString(string toWrite, int padding = 0)
{
byte[] buffer = Encoding.Default.GetBytes(toWrite);
fileStream.Write(buffer, 0, buffer.Length);
byte[] pad = new byte[padding];
fileStream.Write(pad, 0, pad.Length);
}
private byte[] tempBuffer = new byte[1024]; // reusable buffer
public string ReadNullString()
{
int index = 0;
int b;
while ((b = fileStream.ReadByte()) != -1 && b != 0)
{
tempBuffer[index++] = (byte)b;
}
return System.Text.Encoding.ASCII.GetString(tempBuffer, 0, index);
}
/// <summary>
/// Reads a pascal string with a SHORT preceding it
/// </summary>
/// <param name="bigEndian"></param>
/// <param name="security"></param>
/// <returns></returns>
/// <exception cref="DataMisalignedException"></exception>
public string ReadPascalString(bool bigEndian = true, ushort security = 256)
{
ushort length = ReadUShort(true);
if (length > security)
{
Console.WriteLine("Attempting to read string of length {0} at position {1}!", length, Position);
throw new DataMisalignedException("Potential bad string quashed");
}
return ReadString(length);
}
public void WritePascalString(string toWrite, int padding = 0)
{
WriteShort((short)(toWrite.Length + padding), true);
WriteString(toWrite, padding);
}
public void ReadInto(byte[] destination, int size)
{
fileStream.Read(destination, 0, size);
}
private static readonly byte[] ZeroBuffer = new byte[8192]; // 8 KB reusable zero buffer
public void WritePadding(long count)
{
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
while (count > 0)
{
int toWrite = (int)Math.Min(count, ZeroBuffer.Length);
fileStream.Write(ZeroBuffer, 0, toWrite);
count -= toWrite;
}
}
/// <summary>
/// Create a blank space that will be filled later
/// </summary>
/// <returns></returns>
public long CreateIntMarker()
{
WritePadding(4);
return Position - 4;
}
public void FillIntMarker(long markerPosition, bool bigEndian = false)
{
long currentPosition = Position;
long length = currentPosition - markerPosition - 4;
Seek(markerPosition, SeekOrigin.Begin);
WriteInt((int)length, bigEndian);
Seek(currentPosition, SeekOrigin.Begin);
}
}
public class RawFileHop : IDisposable
{
public long OriginalPosition;
public long HopPosition;
public long HopDistance => OriginalPosition - HopPosition;
/// <summary>
/// Used when writing the length of a block
/// </summary>
public uint BlockLength => (uint)HopDistance - 4;
public RawFile File;
public RawFileHop(RawFile file, long hopPosition)
{
OriginalPosition = file.Position;
file.Seek(hopPosition, SeekOrigin.Begin);
HopPosition = hopPosition;
File = file;
}
public void Dispose()
{
File.Seek(OriginalPosition, SeekOrigin.Begin);
}
}
}