GPBCodedInputStream.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import <Foundation/Foundation.h>
  31. @class GPBMessage;
  32. @class GPBExtensionRegistry;
  33. NS_ASSUME_NONNULL_BEGIN
  34. CF_EXTERN_C_BEGIN
  35. /// GPBCodedInputStream exception name. Exceptions raised from
  36. /// GPBCodedInputStream contain an underlying error in the userInfo dictionary
  37. /// under the GPBCodedInputStreamUnderlyingErrorKey key.
  38. extern NSString *const GPBCodedInputStreamException;
  39. /// The key under which the underlying NSError from the exception is stored.
  40. extern NSString *const GPBCodedInputStreamUnderlyingErrorKey;
  41. /// NSError domain used for GPBCodedInputStream errors.
  42. extern NSString *const GPBCodedInputStreamErrorDomain;
  43. /// Error code for NSError with GPBCodedInputStreamErrorDomain.
  44. typedef NS_ENUM(NSInteger, GPBCodedInputStreamErrorCode) {
  45. /// The size does not fit in the remaining bytes to be read.
  46. GPBCodedInputStreamErrorInvalidSize = -100,
  47. /// Attempted to read beyond the subsection limit.
  48. GPBCodedInputStreamErrorSubsectionLimitReached = -101,
  49. /// The requested subsection limit is invalid.
  50. GPBCodedInputStreamErrorInvalidSubsectionLimit = -102,
  51. /// Invalid tag read.
  52. GPBCodedInputStreamErrorInvalidTag = -103,
  53. /// Invalid UTF-8 character in a string.
  54. GPBCodedInputStreamErrorInvalidUTF8 = -104,
  55. /// Invalid VarInt read.
  56. GPBCodedInputStreamErrorInvalidVarInt = -105,
  57. /// The maximum recursion depth of messages was exceeded.
  58. GPBCodedInputStreamErrorRecursionDepthExceeded = -106,
  59. };
  60. CF_EXTERN_C_END
  61. /// Reads and decodes protocol message fields.
  62. ///
  63. /// The common uses of protocol buffers shouldn't need to use this class.
  64. /// @c GPBMessage's provide a @c +parseFromData:error: and @c
  65. /// +parseFromData:extensionRegistry:error: method that will decode a
  66. /// message for you.
  67. ///
  68. /// @note Subclassing of GPBCodedInputStream is NOT supported.
  69. @interface GPBCodedInputStream : NSObject
  70. /// Creates a new stream wrapping some data.
  71. + (instancetype)streamWithData:(NSData *)data;
  72. /// Initializes a stream wrapping some data.
  73. - (instancetype)initWithData:(NSData *)data;
  74. /// Attempt to read a field tag, returning zero if we have reached EOF.
  75. /// Protocol message parsers use this to read tags, since a protocol message
  76. /// may legally end wherever a tag occurs, and zero is not a valid tag number.
  77. - (int32_t)readTag;
  78. /// Read and return a double.
  79. - (double)readDouble;
  80. /// Read and return a float.
  81. - (float)readFloat;
  82. /// Read and return a uint64.
  83. - (uint64_t)readUInt64;
  84. /// Read and return a uint32.
  85. - (uint32_t)readUInt32;
  86. /// Read and return an int64.
  87. - (int64_t)readInt64;
  88. /// Read and return an int32.
  89. - (int32_t)readInt32;
  90. /// Read and return a fixed64.
  91. - (uint64_t)readFixed64;
  92. /// Read and return a fixed32.
  93. - (uint32_t)readFixed32;
  94. /// Read and return an enum (int).
  95. - (int32_t)readEnum;
  96. /// Read and return a sfixed32.
  97. - (int32_t)readSFixed32;
  98. /// Read and return a sfixed64.
  99. - (int64_t)readSFixed64;
  100. /// Read and return a sint32.
  101. - (int32_t)readSInt32;
  102. /// Read and return a sint64.
  103. - (int64_t)readSInt64;
  104. /// Read and return a boolean.
  105. - (BOOL)readBool;
  106. /// Read and return a string.
  107. - (NSString *)readString;
  108. /// Read and return length delimited data.
  109. - (NSData *)readBytes;
  110. /// Read an embedded message field value from the stream.
  111. ///
  112. /// @param message The message to set fields on as they are read.
  113. /// @param extensionRegistry An optional extension registry to use to lookup
  114. /// extensions for @c message.
  115. - (void)readMessage:(GPBMessage *)message
  116. extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry;
  117. /// Reads and discards a single field, given its tag value.
  118. ///
  119. /// @param tag The tag number of the field to skip.
  120. ///
  121. /// @return NO if the tag is an endgroup tag (in which case nothing is skipped),
  122. /// YES in all other cases.
  123. - (BOOL)skipField:(int32_t)tag;
  124. /// Reads and discards an entire message. This will read either until EOF
  125. /// or until an endgroup tag, whichever comes first.
  126. - (void)skipMessage;
  127. /// Check to see if the logical end of the stream has been reached.
  128. ///
  129. /// This can return NO when there is no more data, but the current parsing
  130. /// expected more data.
  131. - (BOOL)isAtEnd;
  132. /// The offset into the stream.
  133. - (size_t)position;
  134. /// Verifies that the last call to @c -readTag returned the given tag value.
  135. /// This is used to verify that a nested group ended with the correct end tag.
  136. /// Throws @c NSParseErrorException if value does not match the last tag.
  137. ///
  138. /// @param expected The tag that was expected.
  139. - (void)checkLastTagWas:(int32_t)expected;
  140. @end
  141. NS_ASSUME_NONNULL_END