blob: fbe5009c92b82380dc97d207adf3832004e83966 [file] [log] [blame]
Thomas Van Lenten30650d82015-05-01 08:57:16 -04001// 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
31#import <Foundation/Foundation.h>
32
33@class GPBMessage;
34@class GPBExtensionRegistry;
35
Thomas Van Lenten8c889572015-06-16 16:45:14 -040036NS_ASSUME_NONNULL_BEGIN
37
Sergio Campamáe34c0912016-06-02 11:14:26 -070038CF_EXTERN_C_BEGIN
39
Sergio Campamá32fadc02016-08-08 07:15:02 -070040/**
41 * @c GPBCodedInputStream exception name. Exceptions raised from
42 * @c GPBCodedInputStream contain an underlying error in the userInfo dictionary
43 * under the GPBCodedInputStreamUnderlyingErrorKey key.
44 **/
Sergio Campamáe34c0912016-06-02 11:14:26 -070045extern NSString *const GPBCodedInputStreamException;
46
Sergio Campamá32fadc02016-08-08 07:15:02 -070047/** The key under which the underlying NSError from the exception is stored. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070048extern NSString *const GPBCodedInputStreamUnderlyingErrorKey;
49
Sergio Campamá32fadc02016-08-08 07:15:02 -070050/** NSError domain used for @c GPBCodedInputStream errors. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070051extern NSString *const GPBCodedInputStreamErrorDomain;
52
Sergio Campamá32fadc02016-08-08 07:15:02 -070053/**
54 * Error code for NSError with @c GPBCodedInputStreamErrorDomain.
55 **/
Sergio Campamáe34c0912016-06-02 11:14:26 -070056typedef NS_ENUM(NSInteger, GPBCodedInputStreamErrorCode) {
Sergio Campamá32fadc02016-08-08 07:15:02 -070057 /** The size does not fit in the remaining bytes to be read. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070058 GPBCodedInputStreamErrorInvalidSize = -100,
Sergio Campamá32fadc02016-08-08 07:15:02 -070059 /** Attempted to read beyond the subsection limit. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070060 GPBCodedInputStreamErrorSubsectionLimitReached = -101,
Sergio Campamá32fadc02016-08-08 07:15:02 -070061 /** The requested subsection limit is invalid. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070062 GPBCodedInputStreamErrorInvalidSubsectionLimit = -102,
Sergio Campamá32fadc02016-08-08 07:15:02 -070063 /** Invalid tag read. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070064 GPBCodedInputStreamErrorInvalidTag = -103,
Sergio Campamá32fadc02016-08-08 07:15:02 -070065 /** Invalid UTF-8 character in a string. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070066 GPBCodedInputStreamErrorInvalidUTF8 = -104,
Sergio Campamá32fadc02016-08-08 07:15:02 -070067 /** Invalid VarInt read. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070068 GPBCodedInputStreamErrorInvalidVarInt = -105,
Sergio Campamá32fadc02016-08-08 07:15:02 -070069 /** The maximum recursion depth of messages was exceeded. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070070 GPBCodedInputStreamErrorRecursionDepthExceeded = -106,
71};
72
73CF_EXTERN_C_END
74
Sergio Campamá32fadc02016-08-08 07:15:02 -070075/**
76 * Reads and decodes protocol message fields.
77 *
78 * The common uses of protocol buffers shouldn't need to use this class.
79 * @c GPBMessage's provide a @c +parseFromData:error: and
80 * @c +parseFromData:extensionRegistry:error: method that will decode a
81 * message for you.
82 *
83 * @note Subclassing of @c GPBCodedInputStream is NOT supported.
84 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -040085@interface GPBCodedInputStream : NSObject
86
Sergio Campamá32fadc02016-08-08 07:15:02 -070087/**
88 * Creates a new stream wrapping some data.
89 *
90 * @param data The data to wrap inside the stream.
91 *
92 * @return A newly instanced GPBCodedInputStream.
93 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -040094+ (instancetype)streamWithData:(NSData *)data;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050095
Sergio Campamá32fadc02016-08-08 07:15:02 -070096/**
97 * Initializes a stream wrapping some data.
98 *
99 * @param data The data to wrap inside the stream.
100 *
101 * @return A newly initialized GPBCodedInputStream.
102 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400103- (instancetype)initWithData:(NSData *)data;
104
Sergio Campamá32fadc02016-08-08 07:15:02 -0700105/**
106 * Attempts to read a field tag, returning zero if we have reached EOF.
107 * Protocol message parsers use this to read tags, since a protocol message
108 * may legally end wherever a tag occurs, and zero is not a valid tag number.
109 *
110 * @return The field tag, or zero if EOF was reached.
111 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400112- (int32_t)readTag;
113
Sergio Campamá32fadc02016-08-08 07:15:02 -0700114/**
115 * @return A double read from the stream.
116 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400117- (double)readDouble;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700118/**
119 * @return A float read from the stream.
120 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400121- (float)readFloat;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700122/**
123 * @return A uint64 read from the stream.
124 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400125- (uint64_t)readUInt64;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700126/**
127 * @return A uint32 read from the stream.
128 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400129- (uint32_t)readUInt32;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700130/**
131 * @return An int64 read from the stream.
132 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400133- (int64_t)readInt64;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700134/**
135 * @return An int32 read from the stream.
136 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400137- (int32_t)readInt32;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700138/**
139 * @return A fixed64 read from the stream.
140 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400141- (uint64_t)readFixed64;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700142/**
143 * @return A fixed32 read from the stream.
144 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400145- (uint32_t)readFixed32;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700146/**
147 * @return An enum read from the stream.
148 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400149- (int32_t)readEnum;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700150/**
151 * @return A sfixed32 read from the stream.
152 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400153- (int32_t)readSFixed32;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700154/**
155 * @return A fixed64 read from the stream.
156 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400157- (int64_t)readSFixed64;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700158/**
159 * @return A sint32 read from the stream.
160 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400161- (int32_t)readSInt32;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700162/**
163 * @return A sint64 read from the stream.
164 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400165- (int64_t)readSInt64;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700166/**
167 * @return A boolean read from the stream.
168 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400169- (BOOL)readBool;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700170/**
171 * @return A string read from the stream.
172 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400173- (NSString *)readString;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700174/**
175 * @return Data read from the stream.
176 **/
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400177- (NSData *)readBytes;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400178
Sergio Campamá32fadc02016-08-08 07:15:02 -0700179/**
180 * Read an embedded message field value from the stream.
181 *
182 * @param message The message to set fields on as they are read.
183 * @param extensionRegistry An optional extension registry to use to lookup
184 * extensions for message.
185 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400186- (void)readMessage:(GPBMessage *)message
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500187 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400188
Sergio Campamá32fadc02016-08-08 07:15:02 -0700189/**
190 * Reads and discards a single field, given its tag value.
191 *
192 * @param tag The tag number of the field to skip.
193 *
194 * @return NO if the tag is an endgroup tag (in which case nothing is skipped),
195 * YES in all other cases.
196 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400197- (BOOL)skipField:(int32_t)tag;
198
Sergio Campamá32fadc02016-08-08 07:15:02 -0700199/**
200 * Reads and discards an entire message. This will read either until EOF or
201 * until an endgroup tag, whichever comes first.
202 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400203- (void)skipMessage;
204
Sergio Campamá32fadc02016-08-08 07:15:02 -0700205/**
206 * Check to see if the logical end of the stream has been reached.
207 *
208 * @note This can return NO when there is no more data, but the current parsing
209 * expected more data.
210 *
211 * @return YES if the logical end of the stream has been reached, NO otherwise.
212 **/
Thomas Van Lenten331cee52016-04-01 12:26:15 -0400213- (BOOL)isAtEnd;
214
Sergio Campamá32fadc02016-08-08 07:15:02 -0700215/**
216 * @return The offset into the stream.
217 **/
Thomas Van Lenten331cee52016-04-01 12:26:15 -0400218- (size_t)position;
219
Sergio Campamá32fadc02016-08-08 07:15:02 -0700220/**
Sergio Campamád58b92a2016-10-27 16:06:45 -0400221 * Moves the limit to the given byte offset starting at the current location.
222 *
223 * @exception GPBCodedInputStreamException If the requested bytes exceeed the
224 * current limit.
225 *
226 * @param byteLimit The number of bytes to move the limit, offset to the current
227 * location.
228 *
229 * @return The limit offset before moving the new limit.
230 */
231- (size_t)pushLimit:(size_t)byteLimit;
232
233/**
234 * Moves the limit back to the offset as it was before calling pushLimit:.
235 *
236 * @param oldLimit The number of bytes to move the current limit. Usually this
237 * is the value returned by the pushLimit: method.
238 */
239- (void)popLimit:(size_t)oldLimit;
240
241/**
Sergio Campamá32fadc02016-08-08 07:15:02 -0700242 * Verifies that the last call to -readTag returned the given tag value. This
243 * is used to verify that a nested group ended with the correct end tag.
244 *
245 * @exception NSParseErrorException If the value does not match the last tag.
246 *
247 * @param expected The tag that was expected.
248 **/
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500249- (void)checkLastTagWas:(int32_t)expected;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400250
251@end
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400252
253NS_ASSUME_NONNULL_END