Skip to content

Commit 761a179

Browse files
authored
Add preconditions to DataStream, tweak seek API (#3)
1 parent 82e3705 commit 761a179

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

Sources/OLEKit/DataStream.swift

+14-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,19 @@ public final class DataReader: Reader {
3737
self.data = data
3838
}
3939

40-
public func seek(toOffset offset: UInt64) {
41-
byteOffset = Int(offset)
40+
public var totalBytes: Int {
41+
data.count
42+
}
43+
44+
public func seek(toOffset offset: Int) {
45+
precondition(offset < data.count)
46+
47+
byteOffset = offset
4248
}
4349

4450
/// Read a single byte from the stream and increment `byteOffset` by 1.
4551
public func read() -> UInt8 {
52+
precondition(byteOffset + 1 <= data.count)
4653
defer { byteOffset += 1 }
4754

4855
return data[byteOffset]
@@ -51,6 +58,7 @@ public final class DataReader: Reader {
5158
/// Read two bytes in little-endian order as a single `UInt16` value and
5259
/// increment `byteOffset` by 2.
5360
public func read() -> UInt16 {
61+
precondition(byteOffset + 2 <= data.count)
5462
defer { byteOffset += 2 }
5563

5664
return (UInt16(data[byteOffset + 1]) << 8) + UInt16(data[byteOffset])
@@ -59,6 +67,7 @@ public final class DataReader: Reader {
5967
/// Read four bytes in little-endian order as a single `UInt32` value and
6068
/// increment `byteOffset` by 4.
6169
public func read() -> UInt32 {
70+
precondition(byteOffset + 4 <= data.count)
6271
defer { byteOffset += 4 }
6372

6473
return (UInt32(data[byteOffset + 3]) << 24)
@@ -69,12 +78,15 @@ public final class DataReader: Reader {
6978

7079
/// Read a given `count` of bytes as raw data and increment `byteOffset` by `count`.
7180
public func readData(ofLength length: Int) -> Data {
81+
precondition(byteOffset + length <= data.count)
7282
defer { byteOffset += length }
7383

7484
return data[byteOffset..<byteOffset + length]
7585
}
7686

7787
public func readDataToEnd() -> Data {
88+
guard data.count > 0 else { return data }
89+
7890
defer { byteOffset = data.count - 1 }
7991

8092
return data[byteOffset..<data.count]

Sources/OLEKit/OLEStream.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension Reader {
5454
guard currentSectorID >= 0 && UInt64(currentSectorID) < fat.count
5555
else { throw OLEError.invalidOLEStreamSectorID(id: currentSectorID, total: fat.count) }
5656

57-
seek(toOffset: firstSectorOffset + UInt64(sectorSize) * UInt64(currentSectorID))
57+
seek(toOffset: Int(firstSectorOffset) + Int(sectorSize) * Int(currentSectorID))
5858

5959
// if sector is the last of the file, sometimes it is not a
6060
// complete sector (of 512 or 4K), so we may read less than

Sources/OLEKit/Reader.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import Foundation
22

33
/// Helper protocol that presents a unified interface for both `FileHandle` and `DataReader`.
44
protocol Reader: AnyObject {
5-
func seek(toOffset: UInt64)
5+
func seek(toOffset: Int)
66
func readData(ofLength: Int) -> Data
77
func readDataToEnd() -> Data
88
}
99

1010
extension FileHandle: Reader {
11-
func seek(toOffset offset: UInt64) { seek(toFileOffset: offset) }
11+
func seek(toOffset offset: Int) { seek(toFileOffset: UInt64(offset)) }
1212
func readDataToEnd() -> Data { readDataToEndOfFile() }
1313
}

0 commit comments

Comments
 (0)