@@ -89,13 +89,14 @@ public class ByteReader {
8989 */
9090 public func bytes( count: Int ) -> [ UInt8 ] {
9191 precondition ( count >= 0 )
92- precondition ( bytesLeft >= count)
9392 #if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
9493 return { ( data: Data , offset: inout Int ) -> [ UInt8 ] in
94+ precondition ( data. endIndex - offset >= count)
9595 defer { offset += count }
9696 return data [ offset..< offset + count] . toArray ( type: UInt8 . self, count: count)
9797 } ( self . data, & self . offset)
9898 #else
99+ precondition ( bytesLeft >= count)
99100 defer { self . offset += count }
100101 return self . data [ self . offset..< self . offset + count] . toArray ( type: UInt8 . self, count: count)
101102 #endif
@@ -109,11 +110,11 @@ public class ByteReader {
109110 */
110111 public func int( fromBytes count: Int ) -> Int {
111112 precondition ( count >= 0 )
112- precondition ( bytesLeft >= count)
113113 // TODO: If uintX() could be force inlined or something in the future then probably it would make sense
114114 // to use them for `count` == 2, 4 or 8.
115115 #if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
116116 return { ( data: Data , offset: inout Int ) -> Int in
117+ precondition ( data. endIndex - offset >= count)
117118 var result = 0
118119 for i in 0 ..< count {
119120 result += Int ( truncatingIfNeeded: data [ offset] ) << ( 8 * i)
@@ -122,6 +123,7 @@ public class ByteReader {
122123 return result
123124 } ( self . data, & self . offset)
124125 #else
126+ precondition ( bytesLeft >= count)
125127 var result = 0
126128 for i in 0 ..< count {
127129 result += Int ( truncatingIfNeeded: self . data [ self . offset] ) << ( 8 * i)
@@ -137,13 +139,14 @@ public class ByteReader {
137139 - Precondition: There MUST be enough data left.
138140 */
139141 public func uint64( ) -> UInt64 {
140- precondition ( bytesLeft >= 8 )
141142 #if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
142143 return { ( data: Data , offset: inout Int ) -> UInt64 in
144+ precondition ( data. endIndex - offset >= 8 )
143145 defer { offset += 8 }
144146 return data [ offset..< offset + 8 ] . to ( type: UInt64 . self)
145147 } ( self . data, & self . offset)
146148 #else
149+ precondition ( bytesLeft >= 8 )
147150 defer { self . offset += 8 }
148151 return self . data [ self . offset..< self . offset + 8 ] . to ( type: UInt64 . self)
149152 #endif
@@ -160,9 +163,9 @@ public class ByteReader {
160163 */
161164 public func uint64( fromBytes count: Int ) -> UInt64 {
162165 precondition ( 0 ... 8 ~= count)
163- precondition ( bytesLeft >= count)
164166 #if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
165167 return { ( data: Data , offset: inout Int ) -> UInt64 in
168+ precondition ( data. endIndex - offset >= count)
166169 var result = 0 as UInt64
167170 for i in 0 ..< count {
168171 result += UInt64 ( truncatingIfNeeded: data [ offset] ) << ( 8 * i)
@@ -171,6 +174,7 @@ public class ByteReader {
171174 return result
172175 } ( self . data, & self . offset)
173176 #else
177+ precondition ( bytesLeft >= count)
174178 var result = 0 as UInt64
175179 for i in 0 ..< count {
176180 result += UInt64 ( truncatingIfNeeded: self . data [ self . offset] ) << ( 8 * i)
@@ -186,13 +190,14 @@ public class ByteReader {
186190 - Precondition: There MUST be enough data left.
187191 */
188192 public func uint32( ) -> UInt32 {
189- precondition ( bytesLeft >= 4 )
190193 #if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
191194 return { ( data: Data , offset: inout Int ) -> UInt32 in
195+ precondition ( data. endIndex - offset >= 4 )
192196 defer { offset += 4 }
193197 return data [ offset..< offset + 4 ] . to ( type: UInt32 . self)
194198 } ( self . data, & self . offset)
195199 #else
200+ precondition ( bytesLeft >= 4 )
196201 defer { self . offset += 4 }
197202 return self . data [ self . offset..< self . offset + 4 ] . to ( type: UInt32 . self)
198203 #endif
@@ -209,9 +214,9 @@ public class ByteReader {
209214 */
210215 public func uint32( fromBytes count: Int ) -> UInt32 {
211216 precondition ( 0 ... 4 ~= count)
212- precondition ( bytesLeft >= count)
213217 #if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
214218 return { ( data: Data , offset: inout Int ) -> UInt32 in
219+ precondition ( data. endIndex - offset >= count)
215220 var result = 0 as UInt32
216221 for i in 0 ..< count {
217222 result += UInt32 ( truncatingIfNeeded: data [ offset] ) << ( 8 * i)
@@ -220,6 +225,7 @@ public class ByteReader {
220225 return result
221226 } ( self . data, & self . offset)
222227 #else
228+ precondition ( bytesLeft >= count)
223229 var result = 0 as UInt32
224230 for i in 0 ..< count {
225231 result += UInt32 ( truncatingIfNeeded: self . data [ self . offset] ) << ( 8 * i)
@@ -235,13 +241,14 @@ public class ByteReader {
235241 - Precondition: There MUST be enough data left.
236242 */
237243 public func uint16( ) -> UInt16 {
238- precondition ( bytesLeft >= 2 )
239244 #if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
240245 return { ( data: Data , offset: inout Int ) -> UInt16 in
246+ precondition ( data. endIndex - offset >= 2 )
241247 defer { offset += 2 }
242248 return data [ offset..< offset + 2 ] . to ( type: UInt16 . self)
243249 } ( self . data, & self . offset)
244250 #else
251+ precondition ( bytesLeft >= 2 )
245252 defer { self . offset += 2 }
246253 return self . data [ self . offset..< self . offset + 2 ] . to ( type: UInt16 . self)
247254 #endif
@@ -258,9 +265,9 @@ public class ByteReader {
258265 */
259266 public func uint16( fromBytes count: Int ) -> UInt16 {
260267 precondition ( 0 ... 2 ~= count)
261- precondition ( bytesLeft >= count)
262268 #if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
263269 return { ( data: Data , offset: inout Int ) -> UInt16 in
270+ precondition ( data. endIndex - offset >= count)
264271 var result = 0 as UInt16
265272 for i in 0 ..< count {
266273 result += UInt16 ( truncatingIfNeeded: data [ offset] ) << ( 8 * i)
@@ -269,6 +276,7 @@ public class ByteReader {
269276 return result
270277 } ( self . data, & self . offset)
271278 #else
279+ precondition ( bytesLeft >= count)
272280 var result = 0 as UInt16
273281 for i in 0 ..< count {
274282 result += UInt16 ( truncatingIfNeeded: self . data [ self . offset] ) << ( 8 * i)
0 commit comments