MessageReader.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. import Foundation
  34. // A reader for protocol buffers. Used internally.
  35. class MessageReader {
  36. var fileDescriptorSet: FileDescriptorSet
  37. var messageName: String
  38. var data : Data
  39. var cursor : Int = 0
  40. init(_ fileDescriptorSet: FileDescriptorSet, messageName: String, data: Data) {
  41. self.fileDescriptorSet = fileDescriptorSet
  42. self.messageName = messageName
  43. self.data = data
  44. }
  45. func readMessage() -> Message? {
  46. if let descriptor = fileDescriptorSet.messageDescriptor(name: messageName) {
  47. return readMessage(range: NSRange(location: 0, length:data.count),
  48. descriptor:descriptor)
  49. } else {
  50. return nil
  51. }
  52. }
  53. private func nextUInt8() -> (UInt8) {
  54. var result: UInt8 = 0
  55. data.copyBytes(to: &result, from:cursor..<cursor+1)
  56. cursor += 1
  57. return result
  58. }
  59. private func nextUInt32() -> (UInt32) {
  60. var result: UInt32 = 0
  61. _ = data.copyBytes(to: UnsafeMutableBufferPointer(start: &result, count: 1), from:cursor..<cursor+4)
  62. cursor += 4
  63. return result
  64. }
  65. private func nextUInt64() -> (UInt64) {
  66. var result: UInt64 = 0
  67. _ = data.copyBytes(to: UnsafeMutableBufferPointer(start: &result, count: 1), from:cursor..<cursor+8)
  68. cursor += 8
  69. return result
  70. }
  71. private func nextInt32() -> (Int32) {
  72. var result: Int32 = 0
  73. _ = data.copyBytes(to: UnsafeMutableBufferPointer(start: &result, count: 1), from:cursor..<cursor+4)
  74. cursor += 4
  75. return result
  76. }
  77. private func nextInt64() -> (Int64) {
  78. var result: Int64 = 0
  79. _ = data.copyBytes(to: UnsafeMutableBufferPointer(start: &result, count: 1), from:cursor..<cursor+8)
  80. cursor += 8
  81. return result
  82. }
  83. private func nextDouble() -> (Double) {
  84. var result: Double = 0
  85. _ = data.copyBytes(to: UnsafeMutableBufferPointer(start: &result, count: 1), from:cursor..<cursor+8)
  86. cursor += 8
  87. return result
  88. }
  89. private func nextFloat() -> (Float) {
  90. var result: Float = 0
  91. _ = data.copyBytes(to: UnsafeMutableBufferPointer(start: &result, count: 1), from:cursor..<cursor+4)
  92. cursor += 4
  93. return result
  94. }
  95. private func nextVarint() -> (Int) {
  96. var sum : Int = 0
  97. var shift : Int = 0
  98. var b : Int = Int(self.nextUInt8())
  99. while (b & 0x80 != 0) {
  100. sum += (b & 0x7f) << shift
  101. shift += 7
  102. b = Int(self.nextUInt8())
  103. }
  104. sum += b << shift
  105. return sum
  106. }
  107. private func nextString(length: Int) -> (String) {
  108. var buffer = [UInt8](repeating: 0, count: length)
  109. data.copyBytes(to: &buffer, from:cursor..<cursor+length)
  110. self.cursor = self.cursor + length
  111. return String(bytes: buffer, encoding:String.Encoding.utf8)!
  112. }
  113. private func nextData(length: Int) -> (Data) {
  114. var buffer = [UInt8](repeating: 0, count: length)
  115. data.copyBytes(to: &buffer, from:cursor..<cursor+length)
  116. self.cursor = self.cursor + length
  117. return Data(bytes: &buffer, count: length)
  118. }
  119. private func readMessage(range: NSRange, descriptor : MessageDescriptor) -> Message {
  120. var fields = [Field]()
  121. while (cursor < range.location + range.length) {
  122. let s = self.nextVarint()
  123. let tag = s >> 3
  124. let wiretype = WireType(rawValue:s & 0x07)!
  125. let field = descriptor.fieldDescriptor(tag:tag)
  126. switch (wiretype) {
  127. case .varint:
  128. var value = self.nextVarint()
  129. if let field = field {
  130. if ((field.type == .sint32) || (field.type == .sint64)) {
  131. // zigzag decoding
  132. let sign = value & 0x01
  133. value = value >> 1
  134. if sign == 1 {
  135. value = -value - 1
  136. }
  137. }
  138. fields.append(
  139. Field(
  140. descriptor: field,
  141. value: value
  142. ))
  143. }
  144. case .fixed64:
  145. if let field = field {
  146. if field.type == .double {
  147. let value = self.nextDouble()
  148. fields.append(
  149. Field(
  150. descriptor: field,
  151. value: value))
  152. } else {
  153. let value = self.nextInt64()
  154. fields.append(
  155. Field(
  156. descriptor: field,
  157. value: value))
  158. }
  159. }
  160. case .lengthDelimited: // length-delimited
  161. let length = self.nextVarint()
  162. if let field = field {
  163. switch (field.type) {
  164. case .string:
  165. let value = self.nextString(length: length)
  166. fields.append(
  167. Field(
  168. descriptor: field,
  169. value: value))
  170. case .bytes:
  171. let value = self.nextData(length: length)
  172. fields.append(
  173. Field(
  174. descriptor: field,
  175. value: value))
  176. default:
  177. if let fieldDescriptor = fileDescriptorSet.messageDescriptor(name:field.type_name) {
  178. let value = readMessage(range:NSMakeRange(cursor, length),
  179. descriptor: fieldDescriptor)
  180. fields.append(
  181. Field(
  182. descriptor: field,
  183. value: value))
  184. } else {
  185. // skip unknown messages
  186. cursor += length
  187. }
  188. }
  189. } else {
  190. // skip unknown messages
  191. cursor += length
  192. }
  193. case .startGroup:
  194. break
  195. case .endGroup:
  196. break
  197. case .fixed32:
  198. if let field = field {
  199. if field.type == .float {
  200. let value = self.nextFloat()
  201. fields.append(
  202. Field(
  203. descriptor: field,
  204. value: value))
  205. } else {
  206. let value = self.nextInt32()
  207. fields.append(
  208. Field(
  209. descriptor: field,
  210. value: value))
  211. }
  212. }
  213. }
  214. }
  215. return Message(descriptor:descriptor, fields:fields)
  216. }
  217. }