@@ -87,7 +87,7 @@ public static int EncodeInt64(System.IO.Stream buffer, Int64 value)
87
87
88
88
public static int DecodeByte ( System . IO . Stream buffer , out byte value )
89
89
{
90
- if ( buffer . Position >= buffer . Length )
90
+ if ( buffer . Length - buffer . Position < 1 )
91
91
{
92
92
value = 0 ;
93
93
return 0 ;
@@ -98,7 +98,7 @@ public static int DecodeByte(System.IO.Stream buffer, out byte value)
98
98
99
99
public static int DecodeUInt16 ( System . IO . Stream buffer , out UInt16 value )
100
100
{
101
- if ( buffer . Position >= buffer . Length )
101
+ if ( buffer . Length - buffer . Position < 2 )
102
102
{
103
103
value = 0 ;
104
104
return 0 ;
@@ -110,7 +110,7 @@ public static int DecodeUInt16(System.IO.Stream buffer, out UInt16 value)
110
110
// Little Endian
111
111
public static int DecodeUInt16LE ( System . IO . Stream buffer , out UInt16 value )
112
112
{
113
- if ( buffer . Position >= buffer . Length )
113
+ if ( buffer . Length - buffer . Position < 2 )
114
114
{
115
115
value = 0 ;
116
116
return 0 ;
@@ -121,7 +121,7 @@ public static int DecodeUInt16LE(System.IO.Stream buffer, out UInt16 value)
121
121
122
122
public static int DecodeInt16 ( System . IO . Stream buffer , out Int16 value )
123
123
{
124
- if ( buffer . Position >= buffer . Length )
124
+ if ( buffer . Length - buffer . Position < 2 )
125
125
{
126
126
value = 0 ;
127
127
return 0 ;
@@ -132,7 +132,7 @@ public static int DecodeInt16(System.IO.Stream buffer, out Int16 value)
132
132
133
133
public static int DecodeUInt32 ( System . IO . Stream buffer , out UInt32 value )
134
134
{
135
- if ( buffer . Position >= buffer . Length )
135
+ if ( buffer . Length - buffer . Position < 4 )
136
136
{
137
137
value = 0 ;
138
138
return 0 ;
@@ -144,7 +144,7 @@ public static int DecodeUInt32(System.IO.Stream buffer, out UInt32 value)
144
144
// Little Endian
145
145
public static int DecodeUInt32LE ( System . IO . Stream buffer , out UInt32 value )
146
146
{
147
- if ( buffer . Position >= buffer . Length )
147
+ if ( buffer . Length - buffer . Position < 4 )
148
148
{
149
149
value = 0 ;
150
150
return 0 ;
@@ -156,7 +156,7 @@ public static int DecodeUInt32LE(System.IO.Stream buffer, out UInt32 value)
156
156
// Little Endian
157
157
public static int DecodeInt32LE ( System . IO . Stream buffer , out Int32 value )
158
158
{
159
- if ( buffer . Position >= buffer . Length )
159
+ if ( buffer . Length - buffer . Position < 4 )
160
160
{
161
161
value = 0 ;
162
162
return 0 ;
@@ -167,7 +167,7 @@ public static int DecodeInt32LE(System.IO.Stream buffer, out Int32 value)
167
167
168
168
public static int DecodeInt32 ( System . IO . Stream buffer , out Int32 value )
169
169
{
170
- if ( buffer . Position >= buffer . Length )
170
+ if ( buffer . Length - buffer . Position < 4 )
171
171
{
172
172
value = 0 ;
173
173
return 0 ;
@@ -178,7 +178,7 @@ public static int DecodeInt32(System.IO.Stream buffer, out Int32 value)
178
178
179
179
public static int DecodeUInt64 ( System . IO . Stream buffer , out UInt64 value )
180
180
{
181
- if ( buffer . Position >= buffer . Length )
181
+ if ( buffer . Length - buffer . Position < 8 )
182
182
{
183
183
value = 0 ;
184
184
return 0 ;
@@ -192,7 +192,7 @@ public static int DecodeUInt64(System.IO.Stream buffer, out UInt64 value)
192
192
193
193
public static int DecodeInt64 ( System . IO . Stream buffer , out Int64 value )
194
194
{
195
- if ( buffer . Position >= buffer . Length )
195
+ if ( buffer . Length - buffer . Position < 8 )
196
196
{
197
197
value = 0 ;
198
198
return 0 ;
@@ -600,6 +600,11 @@ public static int EncodeWString(System.IO.Stream buffer, string value)
600
600
601
601
public static int DecodeWString ( System . IO . Stream buffer , int length , out string value )
602
602
{
603
+ if ( buffer . Length - buffer . Position < length )
604
+ {
605
+ value = string . Empty ;
606
+ return 0 ;
607
+ }
603
608
byte [ ] tmp = new byte [ length ] ;
604
609
buffer . Read ( tmp , 0 , length ) ;
605
610
value = Encoding . UTF8 . GetString ( tmp ) ;
@@ -615,7 +620,7 @@ public static int EncodeOctets(System.IO.Stream buffer, byte[] value)
615
620
616
621
public static int DecodeOctets ( System . IO . Stream buffer , int length , out byte [ ] value )
617
622
{
618
- if ( length <= 0 )
623
+ if ( length <= 0 || buffer . Length - buffer . Position < length )
619
624
{
620
625
value = null ;
621
626
return 0 ;
@@ -715,7 +720,14 @@ public static int DecodeObject(System.IO.Stream buffer, ref PObject obj, bool As
715
720
716
721
public static int DecodeHeader ( System . IO . Stream buffer , out byte version , out UInt16 length )
717
722
{
718
- buffer . ReadByte ( ) ;
723
+ if ( buffer . Length - buffer . Position < 4 )
724
+ {
725
+ version = 0 ;
726
+ length = 0 ;
727
+ return 0 ;
728
+ }
729
+
730
+ buffer . ReadByte ( ) ; // Skip one byte (purpose unclear)
719
731
version = ( byte ) buffer . ReadByte ( ) ;
720
732
DecodeUInt16 ( buffer , out length ) ;
721
733
return 4 ;
0 commit comments