Skip to content

Commit 5f628dc

Browse files
committed
Import v2 code.
1 parent 11e389f commit 5f628dc

File tree

2,539 files changed

+236703
-222960
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,539 files changed

+236703
-222960
lines changed

EditSection/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1"/>
55
</startup>
66
</configuration>

EditSection/CorruptSectionForm.Designer.cs

Lines changed: 350 additions & 351 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EditSection/CorruptSectionForm.cs

Lines changed: 60 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,90 +17,81 @@
1717
using System.Text;
1818
using System.Windows.Forms;
1919

20-
namespace EditSection
21-
{
22-
partial class CorruptSectionForm : Form
23-
{
24-
ICorruptSection _corrupt_section;
20+
namespace EditSection;
2521

26-
public CorruptSectionForm()
22+
partial class CorruptSectionForm : Form
23+
{
24+
public CorruptSectionForm()
25+
{
26+
InitializeComponent();
27+
foreach (object value in Enum.GetValues(typeof(CorruptSectionOperation)))
2728
{
28-
InitializeComponent();
29-
foreach (object value in Enum.GetValues(typeof(CorruptSectionOperation)))
30-
{
31-
comboBoxRandomOperation.Items.Add(value);
32-
comboBoxFixedOperation.Items.Add(value);
33-
comboBoxStringOperation.Items.Add(value);
34-
}
35-
36-
comboBoxRandomOperation.SelectedIndex = 0;
37-
comboBoxFixedOperation.SelectedIndex = 0;
38-
comboBoxStringOperation.SelectedIndex = 0;
29+
comboBoxRandomOperation.Items.Add(value);
30+
comboBoxFixedOperation.Items.Add(value);
31+
comboBoxStringOperation.Items.Add(value);
3932
}
4033

41-
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
42-
internal ICorruptSection CorruptSectionObject
43-
{
44-
get
45-
{
46-
return _corrupt_section;
47-
}
48-
}
34+
comboBoxRandomOperation.SelectedIndex = 0;
35+
comboBoxFixedOperation.SelectedIndex = 0;
36+
comboBoxStringOperation.SelectedIndex = 0;
37+
}
4938

50-
private void radioRandom_CheckedChanged(object sender, System.EventArgs e)
51-
{
52-
groupBoxRandomCorruption.Enabled = radioRandom.Checked;
39+
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
40+
internal ICorruptSection? CorruptSectionObject { get; private set; }
5341

54-
}
42+
private void radioRandom_CheckedChanged(object sender, System.EventArgs e)
43+
{
44+
groupBoxRandomCorruption.Enabled = radioRandom.Checked;
5545

56-
private void radioFixed_CheckedChanged(object sender, EventArgs e)
57-
{
58-
groupBoxFixedCorruption.Enabled = radioFixed.Checked;
59-
}
46+
}
6047

61-
private void radioString_CheckedChanged(object sender, EventArgs e)
62-
{
63-
groupBoxStringCorruption.Enabled = radioString.Checked;
64-
}
48+
private void radioFixed_CheckedChanged(object sender, EventArgs e)
49+
{
50+
groupBoxFixedCorruption.Enabled = radioFixed.Checked;
51+
}
6552

66-
private void btnOK_Click(object sender, EventArgs e)
67-
{
68-
bool succes = false;
53+
private void radioString_CheckedChanged(object sender, EventArgs e)
54+
{
55+
groupBoxStringCorruption.Enabled = radioString.Checked;
56+
}
6957

70-
if (radioRandom.Checked)
71-
{
72-
if (numericMinimum.Value > numericMaximum.Value)
73-
{
74-
MessageBox.Show(this, "Minimum value must be less than or equal to maximum", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
75-
}
76-
else
77-
{
78-
_corrupt_section = new CorruptSectionRandomValue((byte)numericMinimum.Value,
79-
(byte)numericMaximum.Value, (CorruptSectionOperation)comboBoxRandomOperation.SelectedItem);
80-
succes = true;
81-
}
82-
}
83-
else if (radioFixed.Checked)
58+
private void btnOK_Click(object sender, EventArgs e)
59+
{
60+
bool succes = false;
61+
62+
if (radioRandom.Checked)
63+
{
64+
if (numericMinimum.Value > numericMaximum.Value)
8465
{
85-
byte[] data = new byte[1];
86-
data[0] = (byte)numericFixedValue.Value;
87-
_corrupt_section = new CorruptSectionFixedValue(data,
88-
(CorruptSectionOperation)comboBoxFixedOperation.SelectedItem);
89-
succes = true;
66+
MessageBox.Show(this, "Minimum value must be less than or equal to maximum", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
9067
}
91-
else if (radioString.Checked)
68+
else
9269
{
93-
byte[] data = Encoding.UTF8.GetBytes(textBoxString.Text);
94-
_corrupt_section = new CorruptSectionFixedValue(data,
95-
(CorruptSectionOperation)comboBoxStringOperation.SelectedItem);
70+
CorruptSectionObject = new CorruptSectionRandomValue((byte)numericMinimum.Value,
71+
(byte)numericMaximum.Value, (CorruptSectionOperation)comboBoxRandomOperation.SelectedItem);
9672
succes = true;
9773
}
98-
99-
if(succes)
100-
{
101-
DialogResult = DialogResult.OK;
102-
Close();
103-
}
74+
}
75+
else if (radioFixed.Checked)
76+
{
77+
byte[] data = new byte[1];
78+
data[0] = (byte)numericFixedValue.Value;
79+
CorruptSectionObject = new CorruptSectionFixedValue(data,
80+
(CorruptSectionOperation)comboBoxFixedOperation.SelectedItem);
81+
succes = true;
82+
}
83+
else if (radioString.Checked)
84+
{
85+
byte[] data = Encoding.UTF8.GetBytes(textBoxString.Text);
86+
CorruptSectionObject = new CorruptSectionFixedValue(data,
87+
(CorruptSectionOperation)comboBoxStringOperation.SelectedItem);
88+
succes = true;
89+
}
90+
91+
if(succes)
92+
{
93+
DialogResult = DialogResult.OK;
94+
Close();
10495
}
10596
}
10697
}

EditSection/CorruptSectionImpl.cs

Lines changed: 92 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -12,130 +12,128 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using Be.Windows.Forms;
1615
using System;
1716

18-
namespace EditSection
17+
namespace EditSection;
18+
19+
enum CorruptSectionOperation
1920
{
20-
enum CorruptSectionOperation
21-
{
22-
Overwrite,
23-
And,
24-
Or,
25-
Xor
26-
}
21+
Overwrite,
22+
And,
23+
Or,
24+
Xor
25+
}
2726

28-
abstract class CorruptSectionBase : ICorruptSection
27+
abstract class CorruptSectionBase : ICorruptSection
28+
{
29+
internal static byte ApplyOperationToByte(byte left, byte right, CorruptSectionOperation op)
2930
{
30-
internal static byte ApplyOperationToByte(byte left, byte right, CorruptSectionOperation op)
31+
switch (op)
3132
{
32-
switch (op)
33-
{
34-
case CorruptSectionOperation.Xor:
35-
return (byte)(left ^ right);
36-
case CorruptSectionOperation.And:
37-
return (byte)(left & right);
38-
case CorruptSectionOperation.Or:
39-
return (byte)(left | right);
40-
default:
41-
return right;
42-
}
33+
case CorruptSectionOperation.Xor:
34+
return (byte)(left ^ right);
35+
case CorruptSectionOperation.And:
36+
return (byte)(left & right);
37+
case CorruptSectionOperation.Or:
38+
return (byte)(left | right);
39+
default:
40+
return right;
4341
}
42+
}
4443

45-
internal static void ApplyOperationToArray(byte[] array, Func<byte> generator, CorruptSectionOperation op)
44+
internal static void ApplyOperationToArray(byte[] array, Func<byte> generator, CorruptSectionOperation op)
45+
{
46+
for (int i = 0; i < array.Length; ++i)
4647
{
47-
for (int i = 0; i < array.Length; ++i)
48-
{
49-
array[i] = ApplyOperationToByte(array[i], generator(), op);
50-
}
48+
array[i] = ApplyOperationToByte(array[i], generator(), op);
5149
}
50+
}
5251

53-
private readonly CorruptSectionOperation _op;
54-
private const int CHUNK_LENGTH = 64 * 1024;
52+
private readonly CorruptSectionOperation _op;
53+
private const int CHUNK_LENGTH = 64 * 1024;
5554

56-
protected CorruptSectionBase(CorruptSectionOperation op)
57-
{
58-
_op = op;
59-
}
55+
protected CorruptSectionBase(CorruptSectionOperation op)
56+
{
57+
_op = op;
58+
}
6059

61-
protected abstract Func<byte> GetGenerator();
60+
protected abstract Func<byte> GetGenerator();
6261

63-
public void Corrupt(NativeMappedFileByteProvider prov, long start, long end)
62+
public void Corrupt(NativeMappedFileByteProvider prov, long start, long end)
63+
{
64+
try
6465
{
65-
try
66+
prov.DisableByteWritten(true);
67+
long total_length = end - start;
68+
long chunks = total_length / CHUNK_LENGTH;
69+
long remaining = total_length % CHUNK_LENGTH;
70+
var generator = GetGenerator();
71+
byte[] temp_chunk = new byte[CHUNK_LENGTH];
72+
73+
for (long chunk = 0; chunk < chunks; ++chunk)
6674
{
67-
prov.DisableByteWritten(true);
68-
long total_length = end - start;
69-
long chunks = total_length / CHUNK_LENGTH;
70-
long remaining = total_length % CHUNK_LENGTH;
71-
var generator = GetGenerator();
72-
byte[] temp_chunk = new byte[CHUNK_LENGTH];
73-
74-
for (long chunk = 0; chunk < chunks; ++chunk)
75-
{
76-
long ofs = start + (chunk * CHUNK_LENGTH);
77-
// No point reading chunk if we're just going to overwrite it.
78-
byte[] data = _op == CorruptSectionOperation.Overwrite ? temp_chunk : prov.ReadBytes(ofs, CHUNK_LENGTH);
79-
ApplyOperationToArray(data, generator, _op);
80-
prov.WriteBytes(ofs, data);
81-
}
82-
83-
if (remaining > 0)
84-
{
85-
long ofs = start + (chunks * CHUNK_LENGTH);
86-
byte[] data = _op == CorruptSectionOperation.Overwrite ? temp_chunk : prov.ReadBytes(ofs, (int)remaining);
87-
ApplyOperationToArray(data, generator, _op);
88-
prov.WriteBytes(ofs, data);
89-
}
75+
long ofs = start + (chunk * CHUNK_LENGTH);
76+
// No point reading chunk if we're just going to overwrite it.
77+
byte[] data = _op == CorruptSectionOperation.Overwrite ? temp_chunk : prov.ReadBytes(ofs, CHUNK_LENGTH);
78+
ApplyOperationToArray(data, generator, _op);
79+
prov.WriteBytes(ofs, data);
9080
}
91-
finally
81+
82+
if (remaining > 0)
9283
{
93-
prov.DisableByteWritten(false);
84+
long ofs = start + (chunks * CHUNK_LENGTH);
85+
byte[] data = _op == CorruptSectionOperation.Overwrite ? temp_chunk : prov.ReadBytes(ofs, (int)remaining);
86+
ApplyOperationToArray(data, generator, _op);
87+
prov.WriteBytes(ofs, data);
9488
}
9589
}
90+
finally
91+
{
92+
prov.DisableByteWritten(false);
93+
}
9694
}
95+
}
9796

98-
class CorruptSectionFixedValue : CorruptSectionBase
97+
class CorruptSectionFixedValue : CorruptSectionBase
98+
{
99+
private readonly byte[] _data;
100+
public CorruptSectionFixedValue(byte[] data, CorruptSectionOperation op) : base(op)
99101
{
100-
private readonly byte[] _data;
101-
public CorruptSectionFixedValue(byte[] data, CorruptSectionOperation op) : base(op)
102+
if (data.Length < 1)
102103
{
103-
if (data.Length < 1)
104-
{
105-
throw new ArgumentException("Data array must contain at least one byte");
106-
}
107-
108-
_data = (byte[])data.Clone();
104+
throw new ArgumentException("Data array must contain at least one byte");
109105
}
110106

111-
protected override Func<byte> GetGenerator()
112-
{
113-
int data_pos = 0;
114-
return () =>
115-
{
116-
byte ret = _data[data_pos++];
117-
data_pos %= _data.Length;
118-
return ret;
119-
};
120-
}
107+
_data = (byte[])data.Clone();
121108
}
122109

123-
class CorruptSectionRandomValue : CorruptSectionBase
110+
protected override Func<byte> GetGenerator()
124111
{
125-
private readonly Random _rand;
126-
private readonly int _low_byte;
127-
private readonly int _high_byte;
128-
129-
public CorruptSectionRandomValue(byte low_byte, byte high_byte, CorruptSectionOperation op) : base(op)
112+
int data_pos = 0;
113+
return () =>
130114
{
131-
_rand = new Random();
132-
_low_byte = low_byte;
133-
_high_byte = high_byte + 1;
134-
}
115+
byte ret = _data[data_pos++];
116+
data_pos %= _data.Length;
117+
return ret;
118+
};
119+
}
120+
}
135121

136-
protected override Func<byte> GetGenerator()
137-
{
138-
return () => (byte)_rand.Next(_low_byte, _high_byte);
139-
}
122+
class CorruptSectionRandomValue : CorruptSectionBase
123+
{
124+
private readonly Random _rand;
125+
private readonly int _low_byte;
126+
private readonly int _high_byte;
127+
128+
public CorruptSectionRandomValue(byte low_byte, byte high_byte, CorruptSectionOperation op) : base(op)
129+
{
130+
_rand = new Random();
131+
_low_byte = low_byte;
132+
_high_byte = high_byte + 1;
133+
}
134+
135+
protected override Func<byte> GetGenerator()
136+
{
137+
return () => (byte)_rand.Next(_low_byte, _high_byte);
140138
}
141139
}

0 commit comments

Comments
 (0)