TLV.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // TLV.swift
  2. // This file is part of MiKee.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // MiKee is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // MiKee is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with MiKee. If not, see <https://www.gnu.org/licenses/>.
  18. import Foundation
  19. public protocol TLVProtocol {
  20. associatedtype `Type`
  21. associatedtype Lenght: BinaryInteger
  22. associatedtype Value
  23. var type: Type { get }
  24. var value: Value { get set }
  25. }
  26. public struct TLV<Type, Lenght>: TLVProtocol where Lenght: BinaryInteger {
  27. public let type: Type
  28. public var value: Bytes
  29. public init(type: Type, value: Bytes) {
  30. self.type = type
  31. self.value = value
  32. }
  33. public func get<T>() throws -> T where T: BytesRepresentable {
  34. return try T(value)
  35. }
  36. public mutating func set<T>(_ value: T?) throws where T: BytesRepresentable {
  37. self.value = value?.bytes ?? []
  38. }
  39. }
  40. extension TLV: Readable where Type: Readable, Lenght: Readable {
  41. public init(from input: Input) throws {
  42. type = try input.read()
  43. let lenght = try input.read() as Lenght
  44. value = try input.read(lenght: Int(lenght))
  45. }
  46. }
  47. extension TLV: Writable where Type: Writable, Lenght: Writable {
  48. public func write(to output: Output) throws {
  49. try output.write(type)
  50. try output.write(Lenght(value.lenght))
  51. try output.write(value)
  52. }
  53. }
  54. extension Sequence where Element: TLVProtocol, Element.`Type`: Equatable, Element.Value == Bytes {
  55. public func first<T>(valueOf type: Element.`Type`) throws -> T? where T: BytesRepresentable {
  56. return try first(where: { $0.type == type }).map { try T($0.value) }
  57. }
  58. public func first<T>(where type: Element.`Type`, _ predicate: (T) throws -> Bool) throws -> Element? where T: BytesRepresentable {
  59. return try first(where: { try predicate(try T($0.value)) })
  60. }
  61. public func sorted<T>(field: Element.`Type`, by areInIncreasingOrder: (T, T) throws -> Bool) throws -> [Self.Element] where T: BytesRepresentable {
  62. return try sorted(by: { try areInIncreasingOrder(try T($0.value), try T($1.value)) })
  63. }
  64. public subscript<T>(_ type: Element.`Type`) -> T? where T: BytesRepresentable {
  65. try? first(valueOf: type)
  66. }
  67. }
  68. extension RangeReplaceableCollection where Element: TLVProtocol, Element.`Type`: Equatable {
  69. public mutating func removeAll(_ type: Element.`Type`) {
  70. removeAll(where: { $0.type == type })
  71. }
  72. }
  73. extension TLV: CustomDebugStringConvertible {
  74. public var debugDescription: String {
  75. "(T:\(type) L:\(value.lenght))"
  76. }
  77. }