Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #57 from github/swift4
Browse files Browse the repository at this point in the history
Swift 4
  • Loading branch information
mastahyeti authored Sep 25, 2018
2 parents 7c3cfc3 + 6c22f5b commit 9031092
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 103 deletions.
8 changes: 4 additions & 4 deletions APDU/AuthenticationResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import Foundation

public struct AuthenticationResponse: RawConvertible {
let body: Data
let trailer: ResponseStatus
public let body: Data
public let trailer: ResponseStatus

public var userPresence: UInt8 {
return body[0]
Expand Down Expand Up @@ -43,12 +43,12 @@ public struct AuthenticationResponse: RawConvertible {
}

extension AuthenticationResponse: Response {
init(body: Data, trailer: ResponseStatus) {
public init(body: Data, trailer: ResponseStatus) {
self.body = body
self.trailer = trailer
}

func validateBody() throws {
public func validateBody() throws {
// TODO: minimum signature size?
if body.count < MemoryLayout<UInt8>.size + MemoryLayout<UInt32>.size + 1 {
throw ResponseError.BadSize
Expand Down
16 changes: 6 additions & 10 deletions APDU/Data/DataReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,21 @@ public class DataReader {
}

// Read n bytes from the data, advancing our offset into the data.
func readData<I:Integer>(_ n: I) throws -> Data {
let intN = Int(n.toIntMax())

guard let d = peekData(intN) else {
func readData<I:BinaryInteger>(_ n: I) throws -> Data {
guard let d = peekData(n) else {
throw DataReaderError.End
}

offset += intN
offset += Int(n)
return d
}

// Read n bytes from the data, without advancing our offset into the data.
func peekData<I:Integer>(_ n: I) -> Data? {
let intN = Int(n.toIntMax())

if remaining < intN {
func peekData<I:BinaryInteger>(_ n: I) -> Data? {
if remaining < n {
return nil
}

return rest.subdata(in: 0..<intN)
return rest.subdata(in: 0..<Int(n))
}
}
4 changes: 1 addition & 3 deletions APDU/Data/Endian.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,4 @@ extension UInt8: EndianProtocol {
public var littleEndian: UInt8 { return self }
}

public protocol EndianEnumProtocol: RawRepresentable {
associatedtype RawValue: EndianProtocol
}
public protocol EndianEnumProtocol: RawRepresentable where RawValue: EndianProtocol {}
8 changes: 4 additions & 4 deletions APDU/ErrorResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import Foundation

public struct ErrorResponse: RawConvertible {
let body: Data
let trailer: ResponseStatus
public let body: Data
public let trailer: ResponseStatus

public init(status s: ResponseStatus) {
body = Data()
Expand All @@ -18,12 +18,12 @@ public struct ErrorResponse: RawConvertible {
}

extension ErrorResponse: Response {
init(body: Data, trailer: ResponseStatus) {
public init(body: Data, trailer: ResponseStatus) {
self.body = body
self.trailer = trailer
}

func validateBody() throws {
public func validateBody() throws {
if body.count != 0 {
throw ResponseError.BadSize
}
Expand Down
8 changes: 4 additions & 4 deletions APDU/RegisterResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Foundation
import SelfSignedCertificate

public struct RegisterResponse: RawConvertible {
let body: Data
let trailer: ResponseStatus
public let body: Data
public let trailer: ResponseStatus

var reserved: UInt8 {
return body.subdata(in: reservedRange)[0]
Expand Down Expand Up @@ -99,12 +99,12 @@ public struct RegisterResponse: RawConvertible {
}

extension RegisterResponse: Response {
init(body: Data, trailer: ResponseStatus) {
public init(body: Data, trailer: ResponseStatus) {
self.body = body
self.trailer = trailer
}

func validateBody() throws {
public func validateBody() throws {
// Check that we at least have key-handle length.
var min = MemoryLayout<UInt8>.size + U2F_EC_POINT_SIZE + MemoryLayout<UInt8>.size
if body.count < min {
Expand Down
13 changes: 2 additions & 11 deletions APDU/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum ResponseError: Error {
case BadData
}

protocol Response {
public protocol Response {
var body: Data { get }
var trailer: ResponseStatus { get }

Expand All @@ -24,7 +24,7 @@ protocol Response {
}

// Implement RawConvertible
extension Response {
public extension Response {
public var raw: Data {
let writer = DataWriter()
writer.writeData(body)
Expand All @@ -42,13 +42,4 @@ extension Response {

try validateBody()
}

// For testing with libu2f-host
public init(raw: Data, bodyOnly: Bool) throws {
if bodyOnly {
self.init(body: raw, trailer: .NoError)
} else {
try self.init(raw: raw)
}
}
}
8 changes: 4 additions & 4 deletions APDU/VersionResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import Foundation

public struct VersionResponse: RawConvertible {
let body: Data
let trailer: ResponseStatus
public let body: Data
public let trailer: ResponseStatus

public var version: String {
return String(data: body, encoding: .utf8) ?? ""
Expand All @@ -22,12 +22,12 @@ public struct VersionResponse: RawConvertible {
}

extension VersionResponse: Response {
init(body: Data, trailer: ResponseStatus) {
public init(body: Data, trailer: ResponseStatus) {
self.body = body
self.trailer = trailer
}

func validateBody() throws {
public func validateBody() throws {
if version.lengthOfBytes(using: .utf8) < 1 {
throw ResponseError.BadSize
}
Expand Down
Loading

0 comments on commit 9031092

Please sign in to comment.