Ind.ie is now Small Technology Foundation.
Commit df62bdae authored by Dapperstout's avatar Dapperstout

Add support for using 16 bit integers in XDR.

parent 4d02ddb9
......@@ -8,6 +8,8 @@ public func xdr(objects : Any...) -> [UInt8]? {
writer.writeString(string)
case let data as [UInt8]:
writer.writeData(data)
case let int16 as Int16:
writer.writeInt16(int16)
case let uint32 as UInt32:
writer.writeUInt32(uint32)
case let int32 as Int32:
......
......@@ -35,6 +35,20 @@ public class XdrReader {
return nil
}
public func readUInt16() -> UInt16? {
if let bytes = read(2) {
return concatenateBytes(bytes[0], bytes[1])
}
return nil
}
public func readInt16() -> Int16? {
if let unsigned = readUInt16() {
return signed(unsigned)
}
return nil
}
public func readUInt32() -> UInt32? {
if let bytes = read(4) {
return concatenateBytes(bytes[0], bytes[1], bytes[2], bytes[3])
......
......@@ -20,6 +20,15 @@ public class XdrWriter {
return self
}
public func writeUInt16(uint16: UInt16) -> XdrWriter {
xdrBytes += bytes(uint16)
return self
}
public func writeInt16(int16: Int16) -> XdrWriter {
return writeUInt16(unsigned(int16))
}
public func writeUInt32(uint32 : UInt32) -> XdrWriter {
xdrBytes += bytes(uint32)
return self
......
......@@ -38,6 +38,13 @@ class XdrReaderTests: QuickSpec {
expect(reader.readUInt32()) == uint32
}
it("reads Int16") {
let int16 = Int16(bitPattern: 0xF00F)
let reader = XdrReader(xdrBytes: xdr(int16)!)
expect(reader.readInt16()) == int16
}
it("reads Int32") {
let int32 = Int32(bitPattern: 0xF00FA00A)
let reader = XdrReader(xdrBytes: xdr(int32)!)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment