diff --git a/Codec/Sources/Codec/DataInput.swift b/Codec/Sources/Codec/DataInput.swift index 178885ee..31455066 100644 --- a/Codec/Sources/Codec/DataInput.swift +++ b/Codec/Sources/Codec/DataInput.swift @@ -10,10 +10,10 @@ public protocol DataInput { extension DataInput { public mutating func read() throws -> UInt8 { - try read(length: 1)[0] + try read(length: 1).first! } - public mutating func decode() throws -> UInt64 { + public mutating func decodeUInt64() throws -> UInt64 { // TODO: improve this by use `read(minLength: 8)` to avoid read byte by byte let res = try IntegerCodec.decode { try self.read() } guard let res else { diff --git a/Codec/Sources/Codec/JamDecoder.swift b/Codec/Sources/Codec/JamDecoder.swift index 86ab169c..2e20e9de 100644 --- a/Codec/Sources/Codec/JamDecoder.swift +++ b/Codec/Sources/Codec/JamDecoder.swift @@ -100,7 +100,7 @@ private class DecodeContext: Decoder { } fileprivate func decodeData(codingPath: @autoclosure () -> [CodingKey]) throws -> Data { - let length = try input.decode() + let length = try input.decodeUInt64() // sanity check: length must be less than 4gb guard length < 0x1_0000_0000 else { throw DecodingError.dataCorrupted( @@ -115,7 +115,7 @@ private class DecodeContext: Decoder { } fileprivate func decodeData(codingPath: @autoclosure () -> [CodingKey]) throws -> [UInt8] { - let length = try input.decode() + let length = try input.decodeUInt64() // sanity check: length must be less than 4gb guard length < 0x1_0000_0000 else { throw DecodingError.dataCorrupted( @@ -130,7 +130,7 @@ private class DecodeContext: Decoder { } fileprivate func decodeArray(_ type: T.Type, key: CodingKey?) throws -> T { - let length = try input.decode() + let length = try input.decodeUInt64() // sanity check: length can't be unreasonably large guard length < 0xFFFFFF else { throw DecodingError.dataCorrupted(