SoftU2F/APDU/Response.swift

46 строки
883 B
Swift
Исходник Постоянная ссылка Обычный вид История

2017-02-08 03:25:44 +03:00
//
// Response.swift
2017-07-19 20:54:47 +03:00
// SoftU2F
2017-02-08 03:25:44 +03:00
//
// Created by Benjamin P Toews on 2/7/17.
//
import Foundation
enum ResponseError: Error {
case BadSize
case BadStatus
case BadCertificate
2017-02-08 03:49:05 +03:00
case BadData
2017-02-08 03:25:44 +03:00
}
public protocol Response {
2017-02-08 03:25:44 +03:00
var body: Data { get }
var trailer: ResponseStatus { get }
2017-02-08 03:25:44 +03:00
init(body: Data, trailer: ResponseStatus)
2017-02-08 03:25:44 +03:00
func validateBody() throws
}
// Implement RawConvertible
public extension Response {
2017-02-08 03:25:44 +03:00
public var raw: Data {
let writer = DataWriter()
writer.writeData(body)
writer.write(trailer)
2017-02-08 03:25:44 +03:00
return writer.buffer
}
2017-02-08 03:25:44 +03:00
public init(raw: Data) throws {
let reader = DataReader(data: raw)
let body = try reader.readData(reader.remaining - 2)
let trailer: ResponseStatus = try reader.read()
self.init(body: body, trailer: trailer)
2017-02-08 03:49:05 +03:00
try validateBody()
2017-02-08 03:25:44 +03:00
}
}