WebUpdateConnectionAckRequest.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. class WebConnectionAckUpdateRequest: WebAbstractMessage {
  22. var sequenceNumber: UInt32?
  23. override init(message:WebAbstractMessage) {
  24. let data = message.data! as! [AnyHashable : Any]
  25. super.init(message: message)
  26. sequenceNumber = convertToUInt32(sn: data["sequenceNumber"]!)
  27. }
  28. func convertToUInt32(sn: Any) -> UInt32 {
  29. var converted: UInt32 = 0
  30. if let sq = sn as? UInt8 {
  31. converted = UInt32(sq)
  32. }
  33. else if let sq = sn as? Int8 {
  34. converted = UInt32(sq)
  35. }
  36. else if let sq = sn as? UInt16 {
  37. converted = UInt32(sq)
  38. }
  39. else if let sq = sn as? Int16 {
  40. converted = UInt32(sq)
  41. }
  42. else if let sq = sn as? UInt32 {
  43. converted = sq
  44. }
  45. else if let sq = sn as? Int32 {
  46. converted = UInt32(sq)
  47. }
  48. else if let sq = sn as? UInt64 {
  49. if sq > UINT32_MAX {
  50. // error
  51. } else {
  52. converted = UInt32(sq)
  53. }
  54. }
  55. else if let sq = sn as? Int64 {
  56. if sq > UINT32_MAX {
  57. // error
  58. } else {
  59. converted = UInt32(sq)
  60. }
  61. }
  62. else {
  63. // error
  64. }
  65. return converted
  66. }
  67. }