-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary.cs
More file actions
148 lines (119 loc) · 3.6 KB
/
Copy pathBinary.cs
File metadata and controls
148 lines (119 loc) · 3.6 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace GaloreWare.IO
{
public class Binary
{
MemoryStream _data;
public bool Loaded { get; private set; }
public long Length { get { return _data.Length; } }
public List<string> Errors { get; private set; }
public long Offset(long index)
{
if (_data.Length < index)
_data.SetLength(index);
long r = 0;
long offset = index % _data.Length;
r = offset < 0 ? _data.Length - offset : offset;
return r;
}
public int this[int index]
{
get
{
_data.Position = Offset(index);
return _data.ReadByte();
}
set
{
_data.Position = Offset(index);
_data.WriteByte((byte)value);
}
}
public byte[] this[int index, int length]
{
get
{
long position = Offset(index);
byte[] r = new byte[length];
_data.Position = position;
_data.Read(r, 0, length);
return r;
}
}
public Binary(string filename)
{
_data = new MemoryStream();
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))
{
fs.CopyTo(_data);
}
}
public Binary(byte[] data)
{
_data = new MemoryStream(data);
Loaded = true;
}
public Binary(int init_size = -1)
{
_data = new MemoryStream();
if (init_size > 0)
_data.SetLength(init_size);
}
public void Write(int offset, byte[] data, int size)
{
_data.Position = offset;
_data.Write(data, 0, size);
}
public void Write(int offset, byte value)
{
if (offset > _data.Length)
_data.SetLength(offset + 1);
_data.Position = offset;
_data.WriteByte(value);
}
public string ReadASCIIString(int offset, int length)
{
return ASCIIEncoding.ASCII.GetString(this[offset, length]).Replace("\0", string.Empty);
}
public int ReadInt16(int offset)
{
return BitConverter.ToInt16(this[offset, 2], 0);
}
public int ReadInt32(int offset)
{
return BitConverter.ToInt32(this[offset, 4], 0);
}
public void WriteASCII(int offset, string str, int size)
{
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(str);
Write(offset, buffer, size);
}
public void WriteInt16(int offset, short value)
{
byte[] buffer = BitConverter.GetBytes(value);
Write(offset, buffer, 2);
}
public void WriteInt32(int offset,int value)
{
byte[] buffer = BitConverter.GetBytes(value);
Write(offset, buffer, 4);
}
public void Fill(int offset, int size)
{
int s = (int)Math.Max(_data.Length,offset+size);
_data.SetLength(s);
}
public void Save(string filename)
{
File.WriteAllBytes(filename, _data.ToArray());
}
public void SaveOffset(string filename, int offset, int lenght)
{
File.WriteAllBytes(filename, this[offset, lenght]);
}
}
}