blob: d64b64e3f3d655b70a778db6d60f08bf2a24c0e6 [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
Thomas Van Lenten36650a02016-03-07 12:07:03 -050038/// Reads and decodes protocol message fields.
39///
40/// The common uses of protocol buffers shouldn't need to use this class.
41/// @c GPBMessage's provide a @c +parseFromData:error: and @c
42/// +parseFromData:extensionRegistry:error: method that will decode a
43/// message for you.
44///
45/// @note Subclassing of GPBCodedInputStream is NOT supported.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040046@interface GPBCodedInputStream : NSObject
47
Thomas Van Lenten36650a02016-03-07 12:07:03 -050048/// Creates a new stream wrapping some data.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040049+ (instancetype)streamWithData:(NSData *)data;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050050
51/// Initializes a stream wrapping some data.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040052- (instancetype)initWithData:(NSData *)data;
53
Thomas Van Lenten36650a02016-03-07 12:07:03 -050054/// Attempt to read a field tag, returning zero if we have reached EOF.
55/// Protocol message parsers use this to read tags, since a protocol message
56/// may legally end wherever a tag occurs, and zero is not a valid tag number.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040057- (int32_t)readTag;
58
Thomas Van Lenten36650a02016-03-07 12:07:03 -050059/// Read and return a double.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040060- (double)readDouble;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050061/// Read and return a float.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040062- (float)readFloat;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050063/// Read and return a uint64.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040064- (uint64_t)readUInt64;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050065/// Read and return a uint32.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040066- (uint32_t)readUInt32;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050067/// Read and return an int64.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040068- (int64_t)readInt64;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050069/// Read and return an int32.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040070- (int32_t)readInt32;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050071/// Read and return a fixed64.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040072- (uint64_t)readFixed64;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050073/// Read and return a fixed32.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040074- (uint32_t)readFixed32;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050075/// Read and return an enum (int).
Thomas Van Lenten30650d82015-05-01 08:57:16 -040076- (int32_t)readEnum;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050077/// Read and return a sfixed32.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040078- (int32_t)readSFixed32;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050079/// Read and return a sfixed64.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040080- (int64_t)readSFixed64;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050081/// Read and return a sint32.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040082- (int32_t)readSInt32;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050083/// Read and return a sint64.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040084- (int64_t)readSInt64;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050085/// Read and return a boolean.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040086- (BOOL)readBool;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050087/// Read and return a string.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040088- (NSString *)readString;
Thomas Van Lenten36650a02016-03-07 12:07:03 -050089/// Read and return length delimited data.
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040090- (NSData *)readBytes;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040091
Thomas Van Lenten36650a02016-03-07 12:07:03 -050092/// Read an embedded message field value from the stream.
93///
94/// @param message The message to set fields on as they are read.
95/// @param extensionRegistry An optional extension registry to use to lookup
96/// extensions for @message.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040097- (void)readMessage:(GPBMessage *)message
Thomas Van Lenten36650a02016-03-07 12:07:03 -050098 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040099
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500100/// Reads and discards a single field, given its tag value.
101///
102/// @param tag The tag number of the field to skip.
103///
104/// @return NO if the tag is an endgroup tag (in which case nothing is skipped),
105/// YES in all other cases.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400106- (BOOL)skipField:(int32_t)tag;
107
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500108/// Reads and discards an entire message. This will read either until EOF
109/// or until an endgroup tag, whichever comes first.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400110- (void)skipMessage;
111
Thomas Van Lenten331cee52016-04-01 12:26:15 -0400112/// Check to see if the logical end of the stream has been reached.
113///
114/// This can return NO when there is no more data, but the current parsing
115/// expected more data.
116- (BOOL)isAtEnd;
117
118/// The offset into the stream.
119- (size_t)position;
120
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500121/// Verifies that the last call to @c -readTag returned the given tag value.
122/// This is used to verify that a nested group ended with the correct end tag.
123/// Throws @c NSParseErrorException if value does not match the last tag.
124///
125/// @param expected The tag that was expected.
126- (void)checkLastTagWas:(int32_t)expected;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400127
128@end
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400129
130NS_ASSUME_NONNULL_END