blob: 560fa9c207e84cae7bfe28b12646c0bfe425b79c [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.
Thomas Van Lenten30650d82015-05-01 08:57:16 -04003//
Joshua Haberman44bd65b2023-09-08 17:43:14 -07004// 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 Lenten30650d82015-05-01 08:57:16 -04007
8#import <Foundation/Foundation.h>
9
Thomas Van Lenten5392dc12022-09-19 16:06:12 -040010#import "GPBExtensionRegistry.h"
11
Thomas Van Lenten30650d82015-05-01 08:57:16 -040012@class GPBMessage;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040013
Thomas Van Lenten8c889572015-06-16 16:45:14 -040014NS_ASSUME_NONNULL_BEGIN
15
Sergio Campamáe34c0912016-06-02 11:14:26 -070016CF_EXTERN_C_BEGIN
17
Sergio Campamá32fadc02016-08-08 07:15:02 -070018/**
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áe34c0912016-06-02 11:14:26 -070023extern NSString *const GPBCodedInputStreamException;
24
Sergio Campamá32fadc02016-08-08 07:15:02 -070025/** The key under which the underlying NSError from the exception is stored. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070026extern NSString *const GPBCodedInputStreamUnderlyingErrorKey;
27
Sergio Campamá32fadc02016-08-08 07:15:02 -070028/** NSError domain used for @c GPBCodedInputStream errors. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070029extern NSString *const GPBCodedInputStreamErrorDomain;
30
Sergio Campamá32fadc02016-08-08 07:15:02 -070031/**
32 * Error code for NSError with @c GPBCodedInputStreamErrorDomain.
33 **/
Sergio Campamáe34c0912016-06-02 11:14:26 -070034typedef NS_ENUM(NSInteger, GPBCodedInputStreamErrorCode) {
Sergio Campamá32fadc02016-08-08 07:15:02 -070035 /** The size does not fit in the remaining bytes to be read. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070036 GPBCodedInputStreamErrorInvalidSize = -100,
Sergio Campamá32fadc02016-08-08 07:15:02 -070037 /** Attempted to read beyond the subsection limit. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070038 GPBCodedInputStreamErrorSubsectionLimitReached = -101,
Sergio Campamá32fadc02016-08-08 07:15:02 -070039 /** The requested subsection limit is invalid. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070040 GPBCodedInputStreamErrorInvalidSubsectionLimit = -102,
Sergio Campamá32fadc02016-08-08 07:15:02 -070041 /** Invalid tag read. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070042 GPBCodedInputStreamErrorInvalidTag = -103,
Sergio Campamá32fadc02016-08-08 07:15:02 -070043 /** Invalid UTF-8 character in a string. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070044 GPBCodedInputStreamErrorInvalidUTF8 = -104,
Sergio Campamá32fadc02016-08-08 07:15:02 -070045 /** Invalid VarInt read. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070046 GPBCodedInputStreamErrorInvalidVarInt = -105,
Sergio Campamá32fadc02016-08-08 07:15:02 -070047 /** The maximum recursion depth of messages was exceeded. */
Sergio Campamáe34c0912016-06-02 11:14:26 -070048 GPBCodedInputStreamErrorRecursionDepthExceeded = -106,
49};
50
51CF_EXTERN_C_END
52
Sergio Campamá32fadc02016-08-08 07:15:02 -070053/**
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 Bota185a6e2023-01-27 15:19:53 -080063__attribute__((objc_subclassing_restricted))
Thomas Van Lenten30650d82015-05-01 08:57:16 -040064@interface GPBCodedInputStream : NSObject
65
Sergio Campamá32fadc02016-08-08 07:15:02 -070066/**
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 Lenten30650d82015-05-01 08:57:16 -040073+ (instancetype)streamWithData:(NSData *)data;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050074
Sergio Campamá32fadc02016-08-08 07:15:02 -070075/**
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 Lenten30650d82015-05-01 08:57:16 -040082- (instancetype)initWithData:(NSData *)data;
83
Sergio Campamá32fadc02016-08-08 07:15:02 -070084/**
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 Lenten30650d82015-05-01 08:57:16 -040091- (int32_t)readTag;
92
Sergio Campamá32fadc02016-08-08 07:15:02 -070093/**
94 * @return A double read from the stream.
95 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -040096- (double)readDouble;
Sergio Campamá32fadc02016-08-08 07:15:02 -070097/**
98 * @return A float read from the stream.
99 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400100- (float)readFloat;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700101/**
102 * @return A uint64 read from the stream.
103 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400104- (uint64_t)readUInt64;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700105/**
106 * @return A uint32 read from the stream.
107 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400108- (uint32_t)readUInt32;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700109/**
110 * @return An int64 read from the stream.
111 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400112- (int64_t)readInt64;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700113/**
114 * @return An int32 read from the stream.
115 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400116- (int32_t)readInt32;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700117/**
118 * @return A fixed64 read from the stream.
119 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400120- (uint64_t)readFixed64;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700121/**
122 * @return A fixed32 read from the stream.
123 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400124- (uint32_t)readFixed32;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700125/**
126 * @return An enum read from the stream.
127 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400128- (int32_t)readEnum;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700129/**
130 * @return A sfixed32 read from the stream.
131 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400132- (int32_t)readSFixed32;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700133/**
134 * @return A fixed64 read from the stream.
135 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400136- (int64_t)readSFixed64;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700137/**
138 * @return A sint32 read from the stream.
139 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400140- (int32_t)readSInt32;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700141/**
142 * @return A sint64 read from the stream.
143 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400144- (int64_t)readSInt64;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700145/**
146 * @return A boolean read from the stream.
147 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400148- (BOOL)readBool;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700149/**
150 * @return A string read from the stream.
151 **/
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400152- (NSString *)readString;
Sergio Campamá32fadc02016-08-08 07:15:02 -0700153/**
154 * @return Data read from the stream.
155 **/
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400156- (NSData *)readBytes;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400157
Sergio Campamá32fadc02016-08-08 07:15:02 -0700158/**
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 Lenten30650d82015-05-01 08:57:16 -0400165- (void)readMessage:(GPBMessage *)message
Thomas Van Lenten5d0b2172022-09-19 17:20:35 -0400166 extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400167
Sergio Campamá32fadc02016-08-08 07:15:02 -0700168/**
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 Lenten30650d82015-05-01 08:57:16 -0400176- (BOOL)skipField:(int32_t)tag;
177
Sergio Campamá32fadc02016-08-08 07:15:02 -0700178/**
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 Lenten30650d82015-05-01 08:57:16 -0400182- (void)skipMessage;
183
Sergio Campamá32fadc02016-08-08 07:15:02 -0700184/**
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 Lenten331cee52016-04-01 12:26:15 -0400192- (BOOL)isAtEnd;
193
Sergio Campamá32fadc02016-08-08 07:15:02 -0700194/**
195 * @return The offset into the stream.
196 **/
Thomas Van Lenten331cee52016-04-01 12:26:15 -0400197- (size_t)position;
198
Sergio Campamá32fadc02016-08-08 07:15:02 -0700199/**
Sergio Campamád58b92a2016-10-27 16:06:45 -0400200 * Moves the limit to the given byte offset starting at the current location.
201 *
Mike Kruskal701dd832022-08-20 14:22:08 -0700202 * @exception GPBCodedInputStreamException If the requested bytes exceed the
Sergio Campamád58b92a2016-10-27 16:06:45 -0400203 * 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á32fadc02016-08-08 07:15:02 -0700221 * 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 Lenten36650a02016-03-07 12:07:03 -0500228- (void)checkLastTagWas:(int32_t)expected;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400229
230@end
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400231
232NS_ASSUME_NONNULL_END