Entry.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Entry.swift
  2. // This file is part of KeePassKit.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // KeePassKit 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. // KeePassKit 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 KeePassKit. If not, see <https://www.gnu.org/licenses/>.
  18. import Binary
  19. import Foundation
  20. public final class Entry: Row, Streamable {
  21. public typealias Property = TLV<Column, UInt32>
  22. public enum Column: UInt16, Streamable, Endable {
  23. case reserved = 0x0000
  24. case uuid = 0x0001
  25. case groupID = 0x0002
  26. case iconID = 0x0003
  27. case title = 0x0004
  28. case url = 0x0005
  29. case username = 0x0006
  30. case password = 0x0007
  31. case notes = 0x0008
  32. case creationTime = 0x0009
  33. case lastModifiedTime = 0x000A
  34. case lastAccessTime = 0x000B
  35. case expirationTime = 0x000C
  36. case binaryDesc = 0x000D
  37. case binaryData = 0x000E
  38. case end = 0xFFFF
  39. public static var endValue: Self { .end }
  40. }
  41. public internal(set) weak var parent: Group?
  42. public var properties: [Property]
  43. public init() {
  44. properties = []
  45. }
  46. public init(from input: Input) throws {
  47. properties = try input.read()
  48. }
  49. }
  50. extension Entry {
  51. public func removeFromParent() {
  52. parent?.entries.removeAll(where: { $0 == self })
  53. self[.groupID] = -1
  54. parent = nil
  55. }
  56. }
  57. extension Entry: Hashable {
  58. public static func == (lhs: Entry, rhs: Entry) -> Bool {
  59. lhs[.uuid] == rhs[.uuid]
  60. }
  61. public func hash(into hasher: inout Hasher) {
  62. hasher.combine(self[.uuid])
  63. }
  64. }