@@ -67,24 +67,29 @@ public byte ReadByte(IntPtr offset)
6767
6868 public byte ReadByte ( int offset )
6969 {
70- if ( Offset + offset < 0 || Offset + offset > data . Length )
70+ Contract . Requires ( offset >= 0 ) ;
71+
72+ if ( Offset + offset > data . Length )
7173 {
72- throw new IndexOutOfRangeException ( ) ;
74+ return 0 ;
7375 }
7476
7577 return data [ Offset + offset ] ;
7678 }
7779
7880 public byte [ ] ReadBytes ( int offset , int length )
7981 {
80- if ( Offset + offset < 0 || Offset + offset + length > data . Length )
82+ Contract . Requires ( offset >= 0 ) ;
83+
84+ var bytes = new byte [ length ] ;
85+
86+ if ( Offset + offset + length > data . Length )
8187 {
82- throw new IndexOutOfRangeException ( ) ;
88+ return bytes ;
8389 }
8490
85- var b = new byte [ length ] ;
86- Array . Copy ( data , Offset + offset , b , 0 , length ) ;
87- return b ;
91+ Array . Copy ( data , Offset + offset , bytes , 0 , length ) ;
92+ return bytes ;
8893 }
8994
9095 public T ReadObject < T > ( IntPtr offset ) where T : struct
@@ -94,6 +99,13 @@ public T ReadObject<T>(IntPtr offset) where T : struct
9499
95100 public T ReadObject < T > ( int offset ) where T : struct
96101 {
102+ Contract . Requires ( offset >= 0 ) ;
103+
104+ if ( Offset + offset + Marshal . SizeOf ( typeof ( T ) ) > data . Length )
105+ {
106+ return default ( T ) ;
107+ }
108+
97109 var handle = GCHandle . Alloc ( data , GCHandleType . Pinned ) ;
98110 var obj = ( T ) Marshal . PtrToStructure ( handle . AddrOfPinnedObject ( ) + Offset + offset , typeof ( T ) ) ;
99111 handle . Free ( ) ;
@@ -112,16 +124,18 @@ public string ReadPrintableASCIIString(IntPtr offset, int length)
112124 public string ReadPrintableASCIIString ( int offset , int length )
113125 {
114126 Contract . Requires ( offset >= 0 ) ;
115- //Contract.Requires(offset < data.Length);
116127 Contract . Requires ( length >= 0 ) ;
117- //Contract.Requires(length < data.Length);
118- //Contract.Requires(offset + length < data.Length);
119128 Contract . Ensures ( Contract . Result < string > ( ) != null ) ;
120129
130+ if ( Offset + offset + length > data . Length )
131+ {
132+ length = data . Length - Offset - offset ;
133+ }
134+
121135 var sb = new StringBuilder ( length ) ;
122136 for ( var i = 0 ; i < length ; ++ i )
123137 {
124- var c = ( char ) data [ offset + i ] ;
138+ var c = ( char ) data [ Offset + offset + i ] ;
125139 sb . Append ( c . IsPrintable ( ) ? c : '.' ) ;
126140 }
127141 return sb . ToString ( ) ;
@@ -130,8 +144,15 @@ public string ReadPrintableASCIIString(int offset, int length)
130144 private string ReadString ( Encoding encoding , int offset , int length )
131145 {
132146 Contract . Requires ( encoding != null ) ;
147+ Contract . Requires ( offset >= 0 ) ;
148+ Contract . Requires ( length >= 0 ) ;
133149 Contract . Ensures ( Contract . Result < string > ( ) != null ) ;
134150
151+ if ( Offset + offset + length > data . Length )
152+ {
153+ length = data . Length - Offset - offset ;
154+ }
155+
135156 var sb = new StringBuilder ( encoding . GetString ( data , offset , length ) ) ;
136157 for ( var i = 0 ; i < sb . Length ; ++ i )
137158 {
0 commit comments