Skip to content

Commit 9af23cd

Browse files
authored
Merge pull request #17 from aserdobintsev/fix-15
Fix for mixed reading
2 parents c3c68ec + ea97712 commit 9af23cd

2 files changed

Lines changed: 55 additions & 12 deletions

File tree

Sources/BinaryKit/Binary.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public struct Binary {
4242
}
4343
self.init(bytes: bytes)
4444
}
45+
4546

46-
47-
47+
4848
// MARK: - Cursor
4949

5050
/// Returns an `Int` with the value of `readBitCursor` incremented by `bits`.
@@ -95,7 +95,7 @@ public struct Binary {
9595
}
9696

9797
/// Returns the `Int`-value of the given range.
98-
public mutating func getBits(range: Range<Int>) throws -> Int {
98+
public func getBits(range: Range<Int>) throws -> Int {
9999
// Check if the request is within bounds
100100
let storeRange = 0...(bytesStore.count * byteSize)
101101
guard storeRange.contains(range.endIndex) else {
@@ -166,17 +166,19 @@ public struct Binary {
166166
/// Returns the `UInt8`-value of the next byte and
167167
/// increments the reading cursor by 1 byte.
168168
public mutating func readByte() throws -> UInt8 {
169-
let result = try getByte(index: readBitCursor / byteSize)
170-
incrementReadCursorBy(bytes: 1)
171-
return result
169+
return UInt8(try readBits(byteSize))
172170
}
173171

174172
/// Returns a `[UInt8]` of the next n-bytes (`quantitiy`) and
175173
/// increments the reading cursor by n-bytes.
176174
public mutating func readBytes(_ quantitiy: Int) throws -> [UInt8] {
177-
let readByteCursor = readBitCursor / byteSize
178-
incrementReadCursorBy(bytes: quantitiy)
179-
return try getBytes(range: readByteCursor..<(readByteCursor + quantitiy))
175+
// Check if the request is within bounds
176+
let range = (readBitCursor..<(readBitCursor + quantitiy * byteSize))
177+
let storeRange = 0...(bytesStore.count * byteSize)
178+
guard storeRange.contains(range.endIndex) else {
179+
throw BinaryError.outOfBounds
180+
}
181+
return try (0..<quantitiy).map{ _ in try readByte() }
180182
}
181183

182184
public mutating func readBytes(_ quantitiy: UInt8) throws -> [UInt8] {

Tests/BinaryKitTests/BinaryKitTests.swift

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ final class BinaryKitTests: XCTestCase {
7979
XCTAssertEqual(try bin.readByte(), 173)
8080
XCTAssertEqual(try bin.readByte(), 175)
8181
}
82-
82+
8383
func testByteIndex() {
8484
let bytes: [UInt8] = [0b1010_1101, 0b1010_1111]
8585
let bin = Binary(bytes: bytes)
@@ -108,7 +108,46 @@ final class BinaryKitTests: XCTestCase {
108108
XCTAssertEqual(try bin.getBytes(range: 0..<2), [173, 175])
109109
XCTAssertThrowsError(try bin.getBytes(range: 2..<3))
110110
}
111-
111+
112+
// MARK: - Mixed Reading
113+
114+
func testMixedReadByte() {
115+
let bytes: [UInt8] = [0b1010_1101, 0b1010_1111]
116+
var bin = Binary(bytes: bytes)
117+
118+
XCTAssertEqual(try bin.readBit(), 1)
119+
XCTAssertEqual(try bin.readByte(), 91)
120+
XCTAssertEqual(bin.readBitCursor, 9)
121+
}
122+
123+
func testMixedReadBytes() {
124+
let bytes: [UInt8] = [0b1010_1101, 0b1010_1111, 0b1000_1101]
125+
var bin = Binary(bytes: bytes)
126+
127+
XCTAssertEqual(try bin.readBit(), 1)
128+
XCTAssertEqual(try bin.readBytes(2), [UInt8(91),UInt8(95)])
129+
XCTAssertEqual(bin.readBitCursor, 17)
130+
}
131+
132+
func testReadBytesThrowsBeforeReading() {
133+
let bytes: [UInt8] = [0b1010_1101, 0b1010_1111, 0b1000_1101]
134+
var bin = Binary(bytes: bytes)
135+
136+
XCTAssertEqual(try bin.readBit(), 1)
137+
XCTAssertEqual(bin.readBitCursor, 1)
138+
XCTAssertThrowsError(try bin.readBytes(3))
139+
XCTAssertEqual(bin.readBitCursor, 1)
140+
}
141+
142+
func testReadBitsThrowsBeforeReading() {
143+
let bytes: [UInt8] = [0b1010_1101, 0b1010_1111, 0b1000_1101]
144+
var bin = Binary(bytes: bytes)
145+
146+
XCTAssertEqual(bin.readBitCursor, 0)
147+
XCTAssertThrowsError(try bin.readBits(100))
148+
XCTAssertEqual(bin.readBitCursor, 0)
149+
}
150+
112151
// MARK: - Init
113152

114153
func testHexInit() {
@@ -204,12 +243,14 @@ final class BinaryKitTests: XCTestCase {
204243
XCTAssertEqual(try bin.readBit(), 1)
205244
XCTAssertEqual(try bin.readBit(), 1)
206245
XCTAssertEqual(try bin.readBit(), 1)
246+
207247
XCTAssertEqual(try bin.readBit(), 1)
208248
XCTAssertEqual(try bin.readBit(), 1)
209249
XCTAssertEqual(try bin.readBit(), 1)
210250
XCTAssertEqual(try bin.readBit(), 1)
211-
XCTAssertEqual(try bin.readBit(), 1)
251+
212252
XCTAssertEqual(try bin.readByte(), 128)
253+
213254
XCTAssertEqual(try bin.readByte(), 7)
214255
XCTAssertEqual(try bin.readByte(), 128)
215256
XCTAssertEqual(try bin.readString(quantitiyOfBytes: 12), "hello world!")

0 commit comments

Comments
 (0)