TLV.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 TypeLenghtValue {
  20. associatedtype Type_
  21. associatedtype Lenght: BinaryInteger
  22. associatedtype Value
  23. var type: Type_ { get }
  24. var value: Value { get }
  25. }
  26. public protocol Endable: Equatable {
  27. static var endValue: Self { get }
  28. }
  29. public struct TLV<Type, Lenght>: TypeLenghtValue where Lenght: BinaryInteger {
  30. public let type: Type
  31. public private(set) var value: Bytes
  32. public init(type: Type, value: Bytes) {
  33. self.type = type
  34. self.value = value
  35. }
  36. public func get<T>() throws -> T where T: BytesRepresentable {
  37. try T(value)
  38. }
  39. public mutating func set<T>(_ value: T?) throws where T: BytesRepresentable {
  40. self.value = value?.bytes ?? []
  41. }
  42. }
  43. extension TLV: Readable where Type: Readable, Lenght: Readable {
  44. public init(from input: Input) throws {
  45. type = try input.read()
  46. let lenght = try input.read() as Lenght
  47. value = try input.read(lenght: Int(lenght))
  48. }
  49. }
  50. extension TLV: Writable where Type: Writable, Lenght: Writable {
  51. public func write(to output: Output) throws {
  52. try output.write(type)
  53. try output.write(Lenght(value.lenght))
  54. try output.write(value)
  55. }
  56. }
  57. extension Sequence where Element: TypeLenghtValue, Element.Type_: Equatable, Element.Value == Bytes {
  58. public func first<T>(valueOf type: Element.Type_) throws -> T? where T: BytesRepresentable {
  59. return try first(where: { $0.type == type }).map { try T($0.value) }
  60. }
  61. public func first<T>(where type: Element.Type_, _ predicate: (T) throws -> Bool) throws -> Element?
  62. where T: BytesRepresentable {
  63. return try first(where: { try predicate(try T($0.value)) })
  64. }
  65. public func sorted<T>(field: Element.Type_,
  66. by areInIncreasingOrder: (T, T) throws -> Bool) throws -> [Self.Element]
  67. where T: BytesRepresentable {
  68. return try sorted(by: { try areInIncreasingOrder(try T($0.value), try T($1.value)) })
  69. }
  70. public subscript<T>(_ type: Element.Type_) -> T? where T: BytesRepresentable {
  71. try? first(valueOf: type)
  72. }
  73. }
  74. extension RangeReplaceableCollection where Element: TypeLenghtValue, Element.Type_: Equatable {
  75. public mutating func removeAll(_ type: Element.Type_) {
  76. removeAll(where: { $0.type == type })
  77. }
  78. }
  79. extension TLV: CustomDebugStringConvertible {
  80. public var debugDescription: String {
  81. "(T:\(type) L:\(value.lenght))"
  82. }
  83. }
  84. extension Array: Readable where Element: TypeLenghtValue & Readable, Element.Type_: Endable {
  85. public init(from input: Input) throws {
  86. var fields: [Element] = []
  87. while true {
  88. let field: Element = try input.read()
  89. fields.append(field)
  90. if field.type == Element.Type_.endValue { break }
  91. }
  92. self = fields
  93. }
  94. }