implicit endiannes

This commit is contained in:
Johannes Weiss 2023-01-18 17:19:20 +00:00
parent f0375d35aa
commit 2c9f2cffce
11 changed files with 69 additions and 78 deletions

View File

@ -59,9 +59,9 @@ extension ByteBuffer {
bytesWritten += self.writeNFS3FileHandle(reply.fileHandle)
precondition(reply.authFlavors == [.unix] || reply.authFlavors == [.noAuth],
"Sorry, anything but [.unix] / [.system] / [.noAuth] unimplemented.")
bytesWritten += self.writeInteger(UInt32(reply.authFlavors.count), endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(UInt32(reply.authFlavors.count), as: UInt32.self)
for flavor in reply.authFlavors {
bytesWritten += self.writeInteger(flavor.rawValue, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(flavor.rawValue, as: UInt32.self)
}
case .fail(_, _):
()

View File

@ -60,7 +60,7 @@ extension ByteBuffer {
@discardableResult public mutating func writeNFS3CallAccess(_ call: NFS3CallAccess) -> Int {
return self.writeNFS3FileHandle(call.object)
+ self.writeInteger(call.access.rawValue, endianness: .big)
+ self.writeInteger(call.access.rawValue)
}
public mutating func readNFS3ReplyAccess() throws -> NFS3ReplyAccess {
@ -84,22 +84,22 @@ extension ByteBuffer {
switch accessResult.result {
case .okay(let result):
bytesWritten += self.writeInteger(NFS3Status.ok.rawValue, endianness: .big)
bytesWritten += self.writeInteger(NFS3Status.ok.rawValue)
if let attrs = result.dirAttributes {
bytesWritten += self.writeInteger(1, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(1, as: UInt32.self)
+ self.writeNFS3FileAttr(attrs)
} else {
bytesWritten += self.writeInteger(0, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(0, as: UInt32.self)
}
bytesWritten += self.writeInteger(result.access.rawValue, endianness: .big)
bytesWritten += self.writeInteger(result.access.rawValue)
case .fail(let status, let fail):
precondition(status != .ok)
bytesWritten += self.writeInteger(status.rawValue, endianness: .big)
bytesWritten += self.writeInteger(status.rawValue)
if let attrs = fail.dirAttributes {
bytesWritten += self.writeInteger(1, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(1, as: UInt32.self)
+ self.writeNFS3FileAttr(attrs)
} else {
bytesWritten += self.writeInteger(0, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(0, as: UInt32.self)
}
}
return bytesWritten

View File

@ -525,7 +525,7 @@ public struct NFS3FileHandle: Hashable & Sendable & CustomStringConvertible {
precondition(bytes.readableBytes == MemoryLayout<UInt64>.size,
"Sorry, at the moment only file handles with exactly 8 bytes are implemented.")
var bytes = bytes
self = NFS3FileHandle(bytes.readInteger(endianness: .big, as: UInt64.self)!)
self = NFS3FileHandle(bytes.readInteger(as: UInt64.self)!)
}
public var description: String {
@ -648,7 +648,7 @@ extension ByteBuffer {
}
public mutating func readNFS3Integer<I: FixedWidthInteger>(as: I.Type = I.self) throws -> I {
if let value = self.readInteger(endianness: .big, as: I.self) {
if let value = self.readInteger(as: I.self) {
return value
} else {
throw NFS3Error.illegalRPCTooShort
@ -666,7 +666,7 @@ extension ByteBuffer {
@discardableResult public mutating func writeNFS3Blob(_ blob: ByteBuffer) -> Int {
let byteCount = blob.readableBytes
return self.writeInteger(UInt32(byteCount), endianness: .big)
return self.writeInteger(UInt32(byteCount))
+ self.writeImmutableBuffer(blob)
+ self.writeRepeatingByte(0x42, count: nfsStringFillBytes(byteCount))
}
@ -678,13 +678,13 @@ extension ByteBuffer {
@discardableResult public mutating func writeNFS3String(_ string: String) -> Int {
let byteCount = string.utf8.count
return self.writeInteger(UInt32(byteCount), endianness: .big)
return self.writeInteger(UInt32(byteCount))
+ self.writeString(string)
+ self.writeRepeatingByte(0x42, count: nfsStringFillBytes(byteCount))
}
public mutating func readNFS3FileHandle() throws -> NFS3FileHandle {
guard let values = self.readMultipleIntegers(endianness: .big, as: (UInt32, UInt64).self) else {
guard let values = self.readMultipleIntegers(as: (UInt32, UInt64).self) else {
throw NFS3Error.illegalRPCTooShort
}
let length = values.0
@ -700,20 +700,19 @@ extension ByteBuffer {
@discardableResult public mutating func writeNFS3FileHandle(_ fileHandle: NFS3FileHandle) -> Int {
// TODO: This ! is safe at the moment until the file handle == 64 bits limitation is lifted
let id = UInt64(fileHandle)!
return self.writeMultipleIntegers(UInt32(MemoryLayout.size(ofValue: id)), id, endianness: .big)
return self.writeMultipleIntegers(UInt32(MemoryLayout.size(ofValue: id)), id)
}
@discardableResult public mutating func writeNFS3FileType(_ fileType: NFS3FileType) -> Int {
self.writeInteger(fileType.rawValue, endianness: .big)
self.writeInteger(fileType.rawValue)
}
@discardableResult public mutating func writeNFS3Time(_ time: NFS3Time) -> Int {
self.writeMultipleIntegers(time.seconds, time.nanoseconds, endianness: .big)
self.writeMultipleIntegers(time.seconds, time.nanoseconds)
}
public mutating func read3NFS3Times() throws -> (NFS3Time, NFS3Time, NFS3Time) {
guard let values = self.readMultipleIntegers(endianness: .big,
as: (UInt32, UInt32, UInt32, UInt32, UInt32, UInt32).self) else {
guard let values = self.readMultipleIntegers(as: (UInt32, UInt32, UInt32, UInt32, UInt32, UInt32).self) else {
throw NFS3Error.illegalRPCTooShort
}
return (NFS3Time(seconds: values.0, nanoseconds: values.1),
@ -728,7 +727,7 @@ extension ByteBuffer {
}
public mutating func readNFS3Time() throws -> NFS3Time {
guard let values = self.readMultipleIntegers(endianness: .big, as: (UInt32, UInt32).self) else {
guard let values = self.readMultipleIntegers(as: (UInt32, UInt32).self) else {
throw NFS3Error.illegalRPCTooShort
}
@ -746,8 +745,7 @@ extension ByteBuffer {
public mutating func readNFS3FileAttr() throws -> NFS3FileAttr {
let type = try self.readNFS3FileType()
guard let values = self.readMultipleIntegers(endianness: .big,
as: (UInt32, UInt32, UInt32, UInt32, NFS3Size.RawValue,
guard let values = self.readMultipleIntegers(as: (UInt32, UInt32, UInt32, UInt32, NFS3Size.RawValue,
NFS3Size.RawValue, UInt64, UInt64, NFS3FileID.RawValue,
UInt32, UInt32, UInt32, UInt32, UInt32, UInt32).self) else {
throw NFS3Error.illegalRPCTooShort
@ -789,12 +787,11 @@ extension ByteBuffer {
attributes.mtime.seconds,
attributes.mtime.nanoseconds,
attributes.ctime.seconds,
attributes.ctime.nanoseconds,
endianness: .big)
attributes.ctime.nanoseconds)
}
@discardableResult public mutating func writeNFS3Bool(_ bool: NFS3Bool) -> Int {
self.writeInteger(bool == true ? 1 : 0, endianness: .big, as: UInt32.self)
self.writeInteger(bool == true ? 1 : 0, as: UInt32.self)
}
public mutating func readNFS3Bool() throws -> Bool {
@ -803,7 +800,7 @@ extension ByteBuffer {
}
public mutating func readNFS3Optional<T>(_ reader: (inout ByteBuffer) throws -> T) rethrows -> T? {
if self.readInteger(endianness: .big, as: UInt32.self) == 1 {
if self.readInteger(as: UInt32.self) == 1 {
return try reader(&self)
} else {
return nil
@ -812,10 +809,10 @@ extension ByteBuffer {
@discardableResult public mutating func writeNFS3Optional<T>(_ value: T?, writer: (inout ByteBuffer, T) -> Int) -> Int {
if let value = value {
return self.writeInteger(1, endianness: .big, as: UInt32.self)
return self.writeInteger(1, as: UInt32.self)
+ writer(&self, value)
} else {
return self.writeInteger(0, endianness: .big, as: UInt32.self)
return self.writeInteger(0, as: UInt32.self)
}
}
@ -832,7 +829,7 @@ extension ByteBuffer {
}
@discardableResult public mutating func writeNFS3ResultStatus<O, F>(_ result: NFS3Result<O, F>) -> Int {
self.writeInteger(result.status.rawValue, endianness: .big, as: UInt32.self)
self.writeInteger(result.status.rawValue, as: UInt32.self)
}
public mutating func readNFS3Status() throws -> NFS3Status {

View File

@ -153,7 +153,7 @@ internal func nfsStringFillBytes(_ byteCount: Int) -> Int {
extension ByteBuffer {
mutating func readRPCVerifier() throws -> RPCOpaqueAuth {
guard let (flavor, length) = self.readMultipleIntegers(endianness: .big, as: (UInt32, UInt32).self) else {
guard let (flavor, length) = self.readMultipleIntegers(as: (UInt32, UInt32).self) else {
throw NFS3Error.illegalRPCTooShort
}
guard (flavor == RPCAuthFlavor.system.rawValue || flavor == RPCAuthFlavor.noAuth.rawValue) && length == 0 else {
@ -163,17 +163,17 @@ extension ByteBuffer {
}
@discardableResult public mutating func writeRPCVerifier(_ verifier: RPCOpaqueAuth) -> Int {
var bytesWritten = self.writeInteger(verifier.flavor.rawValue, endianness: .big)
var bytesWritten = self.writeInteger(verifier.flavor.rawValue)
if let opaqueBlob = verifier.opaque {
bytesWritten += self.writeNFS3Blob(opaqueBlob)
} else {
bytesWritten += self.writeInteger(0, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(0, as: UInt32.self)
}
return bytesWritten
}
public mutating func readRPCCredentials() throws -> RPCCredentials {
guard let flavor = self.readInteger(endianness: .big, as: UInt32.self) else {
guard let flavor = self.readInteger(as: UInt32.self) else {
throw NFS3Error.illegalRPCTooShort
}
let blob = try self.readNFS3Blob()
@ -187,7 +187,7 @@ extension ByteBuffer {
public mutating func readRPCFragmentHeader() throws -> RPCFragmentHeader? {
let save = self
guard let lastAndLength = self.readInteger(endianness: .big, as: UInt32.self) else {
guard let lastAndLength = self.readInteger(as: UInt32.self) else {
self = save
return nil
}
@ -196,7 +196,7 @@ extension ByteBuffer {
@discardableResult
public mutating func setRPCFragmentHeader(_ header: RPCFragmentHeader, at index: Int) -> Int {
return self.setInteger(header.rawValue, at: index, endianness: .big)
return self.setInteger(header.rawValue, at: index)
}
@discardableResult public mutating func writeRPCFragmentHeader(_ header: RPCFragmentHeader) -> Int {
@ -263,31 +263,29 @@ extension ByteBuffer {
call.rpcVersion,
call.program,
call.programVersion,
call.procedure,
endianness: .big)
call.procedure)
+ self.writeRPCCredentials(call.credentials)
+ self.writeRPCVerifier(call.verifier)
}
@discardableResult public mutating func writeRPCReply(_ reply: RPCReply) -> Int {
var bytesWritten = self.writeInteger(RPCMessageType.reply.rawValue, endianness: .big)
var bytesWritten = self.writeInteger(RPCMessageType.reply.rawValue)
switch reply.status {
case .messageAccepted(_):
bytesWritten += self.writeInteger(0 /* accepted */, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(0 /* accepted */, as: UInt32.self)
case .messageDenied(_):
// FIXME: MSG_DENIED (spec name) isn't actually handled correctly here.
bytesWritten += self.writeInteger(1 /* denied */, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(1 /* denied */, as: UInt32.self)
}
bytesWritten += self.writeInteger(0 /* verifier */, endianness: .big, as: UInt64.self)
+ self.writeInteger(0 /* executed successfully */, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(0 /* verifier */, as: UInt64.self)
+ self.writeInteger(0 /* executed successfully */, as: UInt32.self)
return bytesWritten
}
public mutating func readRPCCall(xid: UInt32) throws -> RPCCall {
guard let values = self.readMultipleIntegers(endianness: .big,
as: (UInt32, UInt32, UInt32, UInt32).self) else {
guard let values = self.readMultipleIntegers(as: (UInt32, UInt32, UInt32, UInt32).self) else {
throw NFS3Error.illegalRPCTooShort
}
@ -385,7 +383,7 @@ extension ByteBuffer {
@discardableResult public mutating func writeRPCNFS3Call(_ rpcNFS3Call: RPCNFS3Call) -> Int {
let startWriterIndex = self.writerIndex
self.writeRPCFragmentHeader(.init(length: 12345678, last: false)) // placeholder, overwritten later
self.writeInteger(rpcNFS3Call.rpcCall.xid, endianness: .big)
self.writeInteger(rpcNFS3Call.rpcCall.xid)
self.writeRPCCall(rpcNFS3Call.rpcCall)
@ -436,7 +434,7 @@ extension ByteBuffer {
let startWriterIndex = self.writerIndex
self.writeRPCFragmentHeader(.init(length: 12345678, last: false)) // placeholder, overwritten later
self.writeInteger(rpcNFS3Reply.rpcReply.xid, endianness: .big)
self.writeInteger(rpcNFS3Reply.rpcReply.xid)
self.writeRPCReply(rpcNFS3Reply.rpcReply)
@ -498,8 +496,8 @@ extension ByteBuffer {
public mutating func readRPCMessage() throws -> (RPCMessage, ByteBuffer)? {
let save = self
guard let fragmentHeader = try self.readRPCFragmentHeader(),
let xid = self.readInteger(endianness: .big, as: UInt32.self),
let messageType = self.readInteger(endianness: .big, as: UInt32.self) else {
let xid = self.readInteger(as: UInt32.self),
let messageType = self.readInteger(as: UInt32.self) else {
self = save
return nil
}

View File

@ -118,10 +118,9 @@ extension ByteBuffer {
reply.wtpref,
reply.wtmult,
reply.dtpref,
reply.maxFileSize.rawValue,
endianness: .big)
reply.maxFileSize.rawValue)
+ self.writeNFS3Time(reply.timeDelta)
+ self.writeInteger(reply.properties.rawValue, endianness: .big)
+ self.writeInteger(reply.properties.rawValue)
case .fail(_, let fail):
bytesWritten += self.writeNFS3Optional(fail.attributes, writer: { $0.writeNFS3FileAttr($1) })
}
@ -130,8 +129,7 @@ extension ByteBuffer {
private mutating func readNFS3ReplyFSInfoOkay() throws -> NFS3ReplyFSInfo.Okay {
let fileAttr = try self.readNFS3Optional { try $0.readNFS3FileAttr() }
guard let values = self.readMultipleIntegers(endianness: .big,
as: (UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32).self) else {
guard let values = self.readMultipleIntegers(as: (UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32).self) else {
throw NFS3Error.illegalRPCTooShort
}
let rtmax = values.0

View File

@ -121,8 +121,7 @@ extension ByteBuffer {
okay.tfiles.rawValue,
okay.ffiles.rawValue,
okay.afiles.rawValue,
okay.invarsec,
endianness: .big)
okay.invarsec)
case .fail(_, let fail):
bytesWritten += self.writeNFS3Optional(fail.attributes, writer: { $0.writeNFS3FileAttr($1) })
}

View File

@ -93,28 +93,28 @@ extension ByteBuffer {
switch lookupResult.result {
case .okay(let result):
bytesWritten += self.writeInteger(NFS3Status.ok.rawValue, endianness: .big)
bytesWritten += self.writeInteger(NFS3Status.ok.rawValue)
+ self.writeNFS3FileHandle(result.fileHandle)
if let attrs = result.attributes {
bytesWritten += self.writeInteger(1, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(1, as: UInt32.self)
+ self.writeNFS3FileAttr(attrs)
} else {
bytesWritten += self.writeInteger(0, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(0, as: UInt32.self)
}
if let attrs = result.dirAttributes {
bytesWritten += self.writeInteger(1, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(1, as: UInt32.self)
+ self.writeNFS3FileAttr(attrs)
} else {
bytesWritten += self.writeInteger(0, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(0, as: UInt32.self)
}
case .fail(let status, let fail):
precondition(status != .ok)
bytesWritten += self.writeInteger(status.rawValue, endianness: .big)
bytesWritten += self.writeInteger(status.rawValue)
if let attrs = fail.dirAttributes {
bytesWritten += self.writeInteger(1, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(1, as: UInt32.self)
+ self.writeNFS3FileAttr(attrs)
} else {
bytesWritten += self.writeInteger(0, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(0, as: UInt32.self)
}
}
return bytesWritten

View File

@ -71,7 +71,7 @@ extension ByteBuffer {
@discardableResult public mutating func writeNFS3CallRead(_ call: NFS3CallRead) -> Int {
return self.writeNFS3FileHandle(call.fileHandle)
+ self.writeMultipleIntegers(call.offset.rawValue, call.count.rawValue, endianness: .big)
+ self.writeMultipleIntegers(call.offset.rawValue, call.count.rawValue)
}
public mutating func readNFS3ReplyRead() throws -> NFS3ReplyRead {
@ -102,7 +102,7 @@ extension ByteBuffer {
public mutating func writeNFS3ReplyReadPartially(_ read: NFS3ReplyRead) -> NFS3PartialWriteNextStep {
switch read.result {
case .okay(let result):
self.writeInteger(NFS3Status.ok.rawValue, endianness: .big)
self.writeInteger(NFS3Status.ok.rawValue)
self.writeNFS3Optional(result.attributes, writer: { $0.writeNFS3FileAttr($1) })
self.writeMultipleIntegers(
result.count.rawValue,
@ -112,7 +112,7 @@ extension ByteBuffer {
return .writeBlob(result.data, numberOfFillBytes: nfsStringFillBytes(result.data.readableBytes))
case .fail(let status, let fail):
precondition(status != .ok)
self.writeInteger(status.rawValue, endianness: .big)
self.writeInteger(status.rawValue)
self.writeNFS3Optional(fail.attributes, writer: { $0.writeNFS3FileAttr($1) })
return .doNothing
}

View File

@ -140,18 +140,18 @@ extension ByteBuffer {
var bytesWritten = 0
switch rd.result {
case .okay(let result):
bytesWritten += self.writeInteger(NFS3Status.ok.rawValue, endianness: .big)
bytesWritten += self.writeInteger(NFS3Status.ok.rawValue)
+ self.writeNFS3Optional(result.dirAttributes, writer: { $0.writeNFS3FileAttr($1) })
+ self.writeNFS3CookieVerifier(result.cookieVerifier)
for entry in result.entries {
bytesWritten += self.writeInteger(1, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(1, as: UInt32.self)
+ self.writeReadDirEntry(entry)
}
bytesWritten += self.writeInteger(0, endianness: .big, as: UInt32.self)
+ self.writeInteger(result.eof == true ? 1 : 0, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(0, as: UInt32.self)
+ self.writeInteger(result.eof == true ? 1 : 0, as: UInt32.self)
case .fail(let status, let fail):
precondition(status != .ok)
bytesWritten += self.writeInteger(status.rawValue, endianness: .big)
bytesWritten += self.writeInteger(status.rawValue)
+ self.writeNFS3Optional(fail.dirAttributes, writer: { $0.writeNFS3FileAttr($1) })
}
return bytesWritten

View File

@ -156,18 +156,18 @@ extension ByteBuffer {
switch rdp.result {
case .okay(let result):
bytesWritten += self.writeInteger(NFS3Status.ok.rawValue, endianness: .big)
bytesWritten += self.writeInteger(NFS3Status.ok.rawValue)
+ self.writeNFS3Optional(result.dirAttributes, writer: { $0.writeNFS3FileAttr($1) })
+ self.writeNFS3CookieVerifier(result.cookieVerifier)
for entry in result.entries {
bytesWritten += self.writeInteger(1, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(1, as: UInt32.self)
+ self.writeReadDirPlusEntry(entry)
}
bytesWritten += self.writeInteger(0, endianness: .big, as: UInt32.self)
+ self.writeInteger(result.eof == true ? 1 : 0, endianness: .big, as: UInt32.self)
bytesWritten += self.writeInteger(0, as: UInt32.self)
+ self.writeInteger(result.eof == true ? 1 : 0, as: UInt32.self)
case .fail(let status, let fail):
precondition(status != .ok)
bytesWritten += self.writeInteger(status.rawValue, endianness: .big)
bytesWritten += self.writeInteger(status.rawValue)
+ self.writeNFS3Optional(fail.dirAttributes, writer: { $0.writeNFS3FileAttr($1) })
}

View File

@ -56,7 +56,6 @@ final class NFS3ReplyEncoderTest: XCTestCase {
fullSerialisation.readableBytes)
XCTAssertEqual(UInt32(payloadLength),
partialSerialisation.getInteger(at: partialSerialisation.writerIndex - 4,
endianness: .big,
as: UInt32.self))
}
}