TLV.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 protocol Endable {
  27. var isAtEnd: Bool { get }
  28. }
  29. public struct TLV<Type, Lenght>: TLVProtocol where Lenght: BinaryInteger {
  30. public let type: Type
  31. public 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. return 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: TLVProtocol, 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? where T: BytesRepresentable {
  62. return try first(where: { try predicate(try T($0.value)) })
  63. }
  64. public func sorted<T>(field: Element.`Type`, by areInIncreasingOrder: (T, T) throws -> Bool) throws -> [Self.Element] where T: BytesRepresentable {
  65. return try sorted(by: { try areInIncreasingOrder(try T($0.value), try T($1.value)) })
  66. }
  67. public subscript<T>(_ type: Element.`Type`) -> T? where T: BytesRepresentable {
  68. try? first(valueOf: type)
  69. }
  70. }
  71. extension RangeReplaceableCollection where Element: TLVProtocol, Element.`Type`: Equatable {
  72. public mutating func removeAll(_ type: Element.`Type`) {
  73. removeAll(where: { $0.type == type })
  74. }
  75. }
  76. extension TLV: CustomDebugStringConvertible {
  77. public var debugDescription: String {
  78. "(T:\(type) L:\(value.lenght))"
  79. }
  80. }