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