-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
80 lines (72 loc) · 2.63 KB
/
Utils.cs
File metadata and controls
80 lines (72 loc) · 2.63 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
using System;
using System.IO;
namespace XboxKit
{
internal class Utils
{
public const long SECTOR_SIZE = 2048;
// Read uint16 from filestream
public static ushort ReadUShort(FileStream fs)
{
byte[] buffer = new byte[2];
if (fs.Read(buffer, 0, 2) != 2)
throw new EndOfStreamException("[ERROR] Failed to read UShort");
return BitConverter.ToUInt16(buffer, 0);
}
// Read uint32 from filestream
public static uint ReadUInt(FileStream fs)
{
byte[] buffer = new byte[4];
if (fs.Read(buffer, 0, 4) != 4)
throw new EndOfStreamException("[ERROR] Failed to read UInt32");
return BitConverter.ToUInt32(buffer, 0);
}
// Ensure proper writing to byte array
public static bool WriteBytes(FileStream fs, byte[] outBA, long offset)
{
long numBytes = 0;
if (offset >= 0)
fs.Seek(offset, SeekOrigin.Begin);
while (numBytes < outBA.Length)
{
int bytesRead = fs.Read(outBA, 0, (int)(outBA.Length - numBytes));
if (bytesRead == 0)
break;
numBytes += bytesRead;
}
return numBytes == outBA.Length;
}
// Ensure proper writing to filestream
public static bool WriteBytes(FileStream inFS, FileStream outFS, long offset, long length)
{
byte[] buf = new byte[64 * SECTOR_SIZE];
long numBytes = 0;
if (offset >= 0)
inFS.Seek(offset, SeekOrigin.Begin);
while (numBytes < length)
{
int bytesRead = inFS.Read(buf, 0, (int)Math.Min(buf.Length, length - numBytes));
if (bytesRead == 0)
break;
outFS.Write(buf, 0, bytesRead);
numBytes += bytesRead;
}
return numBytes == length;
}
// Write zeroes to filestream
public static void WriteZeroes(FileStream outFS, long offset, long length)
{
byte[] buf = new byte[64 * SECTOR_SIZE];
long numBytes = 0;
if (offset >= 0)
outFS.Seek(offset, SeekOrigin.Begin);
while (numBytes < length)
{
int bytesToWrite = (int)Math.Min(buf.Length, length - numBytes);
outFS.Write(buf, 0, bytesToWrite);
numBytes += bytesToWrite;
}
return;
}
}
}