forked from 9ee1/Capstone.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNativeInstructionCollectionEnumerator.cs
142 lines (126 loc) · 4.5 KB
/
NativeInstructionCollectionEnumerator.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Gee.External.Capstone {
/// <summary>
/// Native Instruction Collection Enumerator.
/// </summary>
[Obsolete("Deprecated.")]
internal sealed class NativeInstructionCollectionEnumerator : IEnumerator<NativeInstruction> {
/// <summary>
/// Current Native Instruction's Index.
/// </summary>
private int _currentIndex;
/// <summary>
/// Disposed Flag.
/// </summary>
private bool _disposed;
/// <summary>
/// Enumerator's Native Instructions.
/// </summary>
private readonly NativeInstruction[] _nativeInstructions;
/// <summary>
/// Pointer to Native Instructions in Unmanaged Memory.
/// </summary>
private readonly IntPtr _pNativeInstructions;
/// <summary>
/// Get Current Element.
/// </summary>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the enumerator is disposed.
/// </exception>
public NativeInstruction Current {
get {
this.CheckDisposed();
return this._nativeInstructions[this._currentIndex];
}
}
/// <summary>
/// Get Current Element.
/// </summary>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the enumerator is disposed.
/// </exception>
object IEnumerator.Current {
get {
return this.Current;
}
}
/// <summary>
/// Create a Native Instruction Collection Enumerator.
/// </summary>
/// <param name="nativeInstructions">
/// An array of native instructions. Should not be a null reference.
/// </param>
/// <param name="pNativeInstructions">
/// A pointer to the array of native instructions in unmanaged memory.
/// </param>
internal NativeInstructionCollectionEnumerator(NativeInstruction[] nativeInstructions, IntPtr pNativeInstructions) {
this._nativeInstructions = nativeInstructions;
this._pNativeInstructions = pNativeInstructions;
this._currentIndex = -1;
this._disposed = false;
}
/// <summary>
/// Destroy Native Instruction Collection Enumerator.
/// </summary>
~NativeInstructionCollectionEnumerator() {
this.Dispose(false);
}
/// <summary>
/// Dispose Enumerator.
/// </summary>
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Get Next Element.
/// </summary>
/// <returns>
/// A boolean true if an element is returned. A boolean false otherwise.
/// </returns>
public bool MoveNext() {
this.CheckDisposed();
if ((this._currentIndex + 1 == this._nativeInstructions.Length)) {
return false;
}
this._currentIndex++;
return true;
}
/// <summary>
/// Reset Enumerator.
/// </summary>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the enumerator is disposed.
/// </exception>
public void Reset() {
this.CheckDisposed();
this._currentIndex = -1;
}
/// <summary>
/// Check if Enumerator is Disposed.
/// </summary>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the enumerator is disposed.
/// </exception>
private void CheckDisposed() {
if (this._disposed) {
throw new ObjectDisposedException("NativeInstructionCollectionEnumerator");
}
}
/// <summary>
/// Dispose Enumerator
/// </summary>
/// <param name="disposing">
/// A boolean true if the enumerator is being disposed from client code. A boolean false otherwise.
/// </param>
private void Dispose(bool disposing) {
if (!this._disposed) {
var pNativeInstructionCount = (IntPtr) this._nativeInstructions.Length;
CapstoneImport.Free(this._pNativeInstructions, pNativeInstructionCount);
}
this._disposed = true;
}
}
}