GPBCodedInputStream.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file or at
  6. // https://developers.google.com/open-source/licenses/bsd
  7. #import <Foundation/Foundation.h>
  8. #import "GPBExtensionRegistry.h"
  9. #import "GPBMessage.h"
  10. NS_ASSUME_NONNULL_BEGIN
  11. CF_EXTERN_C_BEGIN
  12. /**
  13. * @c GPBCodedInputStream exception name. Exceptions raised from
  14. * @c GPBCodedInputStream contain an underlying error in the userInfo dictionary
  15. * under the GPBCodedInputStreamUnderlyingErrorKey key.
  16. **/
  17. extern NSString *const GPBCodedInputStreamException;
  18. /** The key under which the underlying NSError from the exception is stored. */
  19. extern NSString *const GPBCodedInputStreamUnderlyingErrorKey;
  20. /** NSError domain used for @c GPBCodedInputStream errors. */
  21. extern NSString *const GPBCodedInputStreamErrorDomain;
  22. /**
  23. * Error code for NSError with @c GPBCodedInputStreamErrorDomain.
  24. **/
  25. typedef NS_ENUM(NSInteger, GPBCodedInputStreamErrorCode) {
  26. /** The size does not fit in the remaining bytes to be read. */
  27. GPBCodedInputStreamErrorInvalidSize = -100,
  28. /** Attempted to read beyond the subsection limit. */
  29. GPBCodedInputStreamErrorSubsectionLimitReached = -101,
  30. /** The requested subsection limit is invalid. */
  31. GPBCodedInputStreamErrorInvalidSubsectionLimit = -102,
  32. /** Invalid tag read. */
  33. GPBCodedInputStreamErrorInvalidTag = -103,
  34. /** Invalid UTF-8 character in a string. */
  35. GPBCodedInputStreamErrorInvalidUTF8 = -104,
  36. /** Invalid VarInt read. */
  37. GPBCodedInputStreamErrorInvalidVarInt = -105,
  38. /** The maximum recursion depth of messages was exceeded. */
  39. GPBCodedInputStreamErrorRecursionDepthExceeded = -106,
  40. };
  41. CF_EXTERN_C_END
  42. /**
  43. * Reads and decodes protocol message fields.
  44. *
  45. * The common uses of protocol buffers shouldn't need to use this class.
  46. * @c GPBMessage's provide a @c +parseFromData:error: and
  47. * @c +parseFromData:extensionRegistry:error: method that will decode a
  48. * message for you.
  49. *
  50. * @note Subclassing of @c GPBCodedInputStream is NOT supported.
  51. **/
  52. __attribute__((objc_subclassing_restricted))
  53. @interface GPBCodedInputStream : NSObject
  54. /**
  55. * Creates a new stream wrapping some data.
  56. *
  57. * @param data The data to wrap inside the stream.
  58. *
  59. * @return A newly instanced GPBCodedInputStream.
  60. **/
  61. + (instancetype)streamWithData:(NSData *)data;
  62. /**
  63. * Initializes a stream wrapping some data.
  64. *
  65. * @param data The data to wrap inside the stream.
  66. *
  67. * @return A newly initialized GPBCodedInputStream.
  68. **/
  69. - (instancetype)initWithData:(NSData *)data;
  70. /**
  71. * Attempts to read a field tag, returning zero if we have reached EOF.
  72. * Protocol message parsers use this to read tags, since a protocol message
  73. * may legally end wherever a tag occurs, and zero is not a valid tag number.
  74. *
  75. * @return The field tag, or zero if EOF was reached.
  76. **/
  77. - (int32_t)readTag;
  78. /**
  79. * @return A double read from the stream.
  80. **/
  81. - (double)readDouble;
  82. /**
  83. * @return A float read from the stream.
  84. **/
  85. - (float)readFloat;
  86. /**
  87. * @return A uint64 read from the stream.
  88. **/
  89. - (uint64_t)readUInt64;
  90. /**
  91. * @return A uint32 read from the stream.
  92. **/
  93. - (uint32_t)readUInt32;
  94. /**
  95. * @return An int64 read from the stream.
  96. **/
  97. - (int64_t)readInt64;
  98. /**
  99. * @return An int32 read from the stream.
  100. **/
  101. - (int32_t)readInt32;
  102. /**
  103. * @return A fixed64 read from the stream.
  104. **/
  105. - (uint64_t)readFixed64;
  106. /**
  107. * @return A fixed32 read from the stream.
  108. **/
  109. - (uint32_t)readFixed32;
  110. /**
  111. * @return An enum read from the stream.
  112. **/
  113. - (int32_t)readEnum;
  114. /**
  115. * @return A sfixed32 read from the stream.
  116. **/
  117. - (int32_t)readSFixed32;
  118. /**
  119. * @return A fixed64 read from the stream.
  120. **/
  121. - (int64_t)readSFixed64;
  122. /**
  123. * @return A sint32 read from the stream.
  124. **/
  125. - (int32_t)readSInt32;
  126. /**
  127. * @return A sint64 read from the stream.
  128. **/
  129. - (int64_t)readSInt64;
  130. /**
  131. * @return A boolean read from the stream.
  132. **/
  133. - (BOOL)readBool;
  134. /**
  135. * @return A string read from the stream.
  136. **/
  137. - (NSString *)readString;
  138. /**
  139. * @return Data read from the stream.
  140. **/
  141. - (NSData *)readBytes;
  142. /**
  143. * Read an embedded message field value from the stream.
  144. *
  145. * @param message The message to set fields on as they are read.
  146. * @param extensionRegistry An optional extension registry to use to lookup
  147. * extensions for message.
  148. **/
  149. - (void)readMessage:(GPBMessage *)message
  150. extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry;
  151. /**
  152. * Reads and discards a single field, given its tag value.
  153. *
  154. * @param tag The tag number of the field to skip.
  155. *
  156. * @return NO if the tag is an endgroup tag (in which case nothing is skipped),
  157. * YES in all other cases.
  158. **/
  159. - (BOOL)skipField:(int32_t)tag;
  160. /**
  161. * Reads and discards an entire message. This will read either until EOF or
  162. * until an endgroup tag, whichever comes first.
  163. **/
  164. - (void)skipMessage;
  165. /**
  166. * Check to see if the logical end of the stream has been reached.
  167. *
  168. * @note This can return NO when there is no more data, but the current parsing
  169. * expected more data.
  170. *
  171. * @return YES if the logical end of the stream has been reached, NO otherwise.
  172. **/
  173. - (BOOL)isAtEnd;
  174. /**
  175. * @return The offset into the stream.
  176. **/
  177. - (size_t)position;
  178. /**
  179. * Moves the limit to the given byte offset starting at the current location.
  180. *
  181. * @exception GPBCodedInputStreamException If the requested bytes exceed the
  182. * current limit.
  183. *
  184. * @param byteLimit The number of bytes to move the limit, offset to the current
  185. * location.
  186. *
  187. * @return The limit offset before moving the new limit.
  188. */
  189. - (size_t)pushLimit:(size_t)byteLimit;
  190. /**
  191. * Moves the limit back to the offset as it was before calling pushLimit:.
  192. *
  193. * @param oldLimit The number of bytes to move the current limit. Usually this
  194. * is the value returned by the pushLimit: method.
  195. */
  196. - (void)popLimit:(size_t)oldLimit;
  197. /**
  198. * @return The number of bytes from the current position to the current limit.
  199. */
  200. - (size_t)bytesUntilLimit;
  201. /**
  202. * Verifies that the last call to -readTag returned the given tag value. This
  203. * is used to verify that a nested group ended with the correct end tag.
  204. *
  205. * @exception NSParseErrorException If the value does not match the last tag.
  206. *
  207. * @param expected The tag that was expected.
  208. **/
  209. - (void)checkLastTagWas:(int32_t)expected;
  210. @end
  211. NS_ASSUME_NONNULL_END