|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -using Be.Windows.Forms; |
16 | 15 | using System; |
17 | 16 |
|
18 | | -namespace EditSection |
| 17 | +namespace EditSection; |
| 18 | + |
| 19 | +enum CorruptSectionOperation |
19 | 20 | { |
20 | | - enum CorruptSectionOperation |
21 | | - { |
22 | | - Overwrite, |
23 | | - And, |
24 | | - Or, |
25 | | - Xor |
26 | | - } |
| 21 | + Overwrite, |
| 22 | + And, |
| 23 | + Or, |
| 24 | + Xor |
| 25 | +} |
27 | 26 |
|
28 | | - abstract class CorruptSectionBase : ICorruptSection |
| 27 | +abstract class CorruptSectionBase : ICorruptSection |
| 28 | +{ |
| 29 | + internal static byte ApplyOperationToByte(byte left, byte right, CorruptSectionOperation op) |
29 | 30 | { |
30 | | - internal static byte ApplyOperationToByte(byte left, byte right, CorruptSectionOperation op) |
| 31 | + switch (op) |
31 | 32 | { |
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; |
43 | 41 | } |
| 42 | + } |
44 | 43 |
|
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) |
46 | 47 | { |
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); |
51 | 49 | } |
| 50 | + } |
52 | 51 |
|
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; |
55 | 54 |
|
56 | | - protected CorruptSectionBase(CorruptSectionOperation op) |
57 | | - { |
58 | | - _op = op; |
59 | | - } |
| 55 | + protected CorruptSectionBase(CorruptSectionOperation op) |
| 56 | + { |
| 57 | + _op = op; |
| 58 | + } |
60 | 59 |
|
61 | | - protected abstract Func<byte> GetGenerator(); |
| 60 | + protected abstract Func<byte> GetGenerator(); |
62 | 61 |
|
63 | | - public void Corrupt(NativeMappedFileByteProvider prov, long start, long end) |
| 62 | + public void Corrupt(NativeMappedFileByteProvider prov, long start, long end) |
| 63 | + { |
| 64 | + try |
64 | 65 | { |
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) |
66 | 74 | { |
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); |
90 | 80 | } |
91 | | - finally |
| 81 | + |
| 82 | + if (remaining > 0) |
92 | 83 | { |
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); |
94 | 88 | } |
95 | 89 | } |
| 90 | + finally |
| 91 | + { |
| 92 | + prov.DisableByteWritten(false); |
| 93 | + } |
96 | 94 | } |
| 95 | +} |
97 | 96 |
|
98 | | - class CorruptSectionFixedValue : CorruptSectionBase |
| 97 | +class CorruptSectionFixedValue : CorruptSectionBase |
| 98 | +{ |
| 99 | + private readonly byte[] _data; |
| 100 | + public CorruptSectionFixedValue(byte[] data, CorruptSectionOperation op) : base(op) |
99 | 101 | { |
100 | | - private readonly byte[] _data; |
101 | | - public CorruptSectionFixedValue(byte[] data, CorruptSectionOperation op) : base(op) |
| 102 | + if (data.Length < 1) |
102 | 103 | { |
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"); |
109 | 105 | } |
110 | 106 |
|
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(); |
121 | 108 | } |
122 | 109 |
|
123 | | - class CorruptSectionRandomValue : CorruptSectionBase |
| 110 | + protected override Func<byte> GetGenerator() |
124 | 111 | { |
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 () => |
130 | 114 | { |
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 | +} |
135 | 121 |
|
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); |
140 | 138 | } |
141 | 139 | } |
0 commit comments