@@ -11,6 +11,7 @@ public class MemoryBuffer
1111 public RemoteProcess Process { get ; set ; }
1212
1313 private byte [ ] data ;
14+ private byte [ ] historyData ;
1415
1516 public int Size
1617 {
@@ -23,6 +24,7 @@ public int Size
2324 if ( value != data . Length )
2425 {
2526 data = new byte [ value ] ;
27+ historyData = new byte [ value ] ;
2628 }
2729 }
2830 }
@@ -33,21 +35,26 @@ public int Size
3335 private void ObjectInvariants ( )
3436 {
3537 Contract . Invariant ( data != null ) ;
38+ Contract . Invariant ( historyData != null ) ;
3639 }
3740
3841 public MemoryBuffer ( )
3942 {
4043 Contract . Ensures ( data != null ) ;
44+ Contract . Ensures ( historyData != null ) ;
4145
4246 data = new byte [ 0 ] ;
47+ historyData = new byte [ 0 ] ;
4348 }
4449
4550 public MemoryBuffer ( MemoryBuffer other )
4651 {
4752 Contract . Requires ( other != null ) ;
4853 Contract . Ensures ( data != null ) ;
54+ Contract . Ensures ( historyData != null ) ;
4955
5056 data = other . data ;
57+ historyData = other . historyData ;
5158 }
5259
5360 public MemoryBuffer Clone ( )
@@ -62,9 +69,19 @@ public MemoryBuffer Clone()
6269 }
6370
6471 public void Update ( IntPtr address )
72+ {
73+ Update ( address , true ) ;
74+ }
75+
76+ public void Update ( IntPtr address , bool setHistory )
6577 {
6678 Contract . Requires ( Process != null ) ;
6779
80+ if ( setHistory )
81+ {
82+ Array . Copy ( data , historyData , data . Length ) ;
83+ }
84+
6885 Process . ReadRemoteMemoryIntoBuffer ( address , ref data ) ;
6986 }
7087
@@ -92,15 +109,31 @@ public byte[] ReadBytes(int offset, int length)
92109 Contract . Requires ( offset >= 0 ) ;
93110 Contract . Requires ( length >= 0 ) ;
94111
95- var bytes = new byte [ length ] ;
112+ var buffer = new byte [ length ] ;
96113
97- if ( Offset + offset + length > data . Length )
114+ ReadBytes ( offset , buffer ) ;
115+
116+ return buffer ;
117+ }
118+
119+ public void ReadBytes ( IntPtr offset , byte [ ] buffer )
120+ {
121+ Contract . Requires ( buffer != null ) ;
122+
123+ ReadBytes ( offset . ToInt32 ( ) , buffer ) ;
124+ }
125+
126+ public void ReadBytes ( int offset , byte [ ] buffer )
127+ {
128+ Contract . Requires ( offset >= 0 ) ;
129+ Contract . Requires ( buffer != null ) ;
130+
131+ if ( Offset + offset + buffer . Length > data . Length )
98132 {
99- return bytes ;
133+ return ;
100134 }
101135
102- Array . Copy ( data , Offset + offset , bytes , 0 , length ) ;
103- return bytes ;
136+ Array . Copy ( data , Offset + offset , buffer , 0 , buffer . Length ) ;
104137 }
105138
106139 public T ReadObject < T > ( IntPtr offset ) where T : struct
@@ -213,5 +246,30 @@ public string ReadUTF32String(IntPtr offset, int length)
213246
214247 return ReadString ( Encoding . UTF32 , offset . ToInt32 ( ) , length ) ;
215248 }
249+
250+ public bool HasChanged ( IntPtr offset , int length )
251+ {
252+ return HasChanged ( offset . ToInt32 ( ) , length ) ;
253+ }
254+
255+ public bool HasChanged ( int offset , int length )
256+ {
257+ if ( Offset + offset + length > data . Length )
258+ {
259+ return false ;
260+ }
261+
262+ var end = Offset + offset + length ;
263+
264+ for ( var i = Offset + offset ; i < end ; ++ i )
265+ {
266+ if ( data [ i ] != historyData [ i ] )
267+ {
268+ return true ;
269+ }
270+ }
271+
272+ return false ;
273+ }
216274 }
217275}
0 commit comments