aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Messaging/FIRMessagingCodedInputStream.m
diff options
context:
space:
mode:
authorGravatar Paul Beusterien <paulbeusterien@google.com>2017-05-15 12:27:07 -0700
committerGravatar Paul Beusterien <paulbeusterien@google.com>2017-05-15 12:27:07 -0700
commit98ba64449a632518bd2b86fe8d927f4a960d3ddc (patch)
tree131d9c4272fa6179fcda6c5a33fcb3b1bd57ad2e /Firebase/Messaging/FIRMessagingCodedInputStream.m
parent32461366c9e204a527ca05e6e9b9404a2454ac51 (diff)
Initial
Diffstat (limited to 'Firebase/Messaging/FIRMessagingCodedInputStream.m')
-rw-r--r--Firebase/Messaging/FIRMessagingCodedInputStream.m142
1 files changed, 142 insertions, 0 deletions
diff --git a/Firebase/Messaging/FIRMessagingCodedInputStream.m b/Firebase/Messaging/FIRMessagingCodedInputStream.m
new file mode 100644
index 0000000..82c0677
--- /dev/null
+++ b/Firebase/Messaging/FIRMessagingCodedInputStream.m
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2017 Google
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import "FIRMessagingCodedInputStream.h"
+#import "FIRMessagingDefines.h"
+
+typedef struct {
+ const void *bytes;
+ size_t bufferSize;
+ size_t bufferPos;
+} BufferState;
+
+static BOOL CheckSize(BufferState *state, size_t size) {
+ size_t newSize = state->bufferPos + size;
+ if (newSize > state->bufferSize) {
+ return NO;
+ }
+ return YES;
+}
+
+static BOOL ReadRawByte(BufferState *state, int8_t *output) {
+ _FIRMessagingDevAssert(output != NULL && state != NULL, @"Invalid parameters");
+
+ if (CheckSize(state, sizeof(int8_t))) {
+ *output = ((int8_t *)state->bytes)[state->bufferPos++];
+ return YES;
+ }
+ return NO;
+}
+
+static BOOL ReadRawVarInt32(BufferState *state, int32_t *output) {
+ _FIRMessagingDevAssert(output != NULL && state != NULL, @"Invalid parameters");
+
+ int8_t tmp = 0;
+ if (!ReadRawByte(state, &tmp)) {
+ return NO;
+ }
+ if (tmp >= 0) {
+ *output = tmp;
+ return YES;
+ }
+ int32_t result = tmp & 0x7f;
+ if (!ReadRawByte(state, &tmp)) {
+ return NO;
+ }
+ if (tmp >= 0) {
+ result |= tmp << 7;
+ } else {
+ result |= (tmp & 0x7f) << 7;
+ if (!ReadRawByte(state, &tmp)) {
+ return NO;
+ }
+ if (tmp >= 0) {
+ result |= tmp << 14;
+ } else {
+ result |= (tmp & 0x7f) << 14;
+ if (!ReadRawByte(state, &tmp)) {
+ return NO;
+ }
+ if (tmp >= 0) {
+ result |= tmp << 21;
+ } else {
+ result |= (tmp & 0x7f) << 21;
+ if (!ReadRawByte(state, &tmp)) {
+ return NO;
+ }
+ result |= tmp << 28;
+ if (tmp < 0) {
+ // Discard upper 32 bits.
+ for (int i = 0; i < 5; ++i) {
+ if (!ReadRawByte(state, &tmp)) {
+ return NO;
+ }
+ if (tmp >= 0) {
+ *output = result;
+ return YES;
+ }
+ }
+ return NO;
+ }
+ }
+ }
+ }
+ *output = result;
+ return YES;
+}
+
+@interface FIRMessagingCodedInputStream()
+
+@property(nonatomic, readwrite, strong) NSData *buffer;
+@property(nonatomic, readwrite, assign) BufferState state;
+
+@end
+
+@implementation FIRMessagingCodedInputStream;
+
+- (instancetype)initWithData:(NSData *)data {
+ self = [super init];
+ if (self) {
+ _buffer = data;
+ _state.bytes = _buffer.bytes;
+ _state.bufferSize = _buffer.length;
+ }
+ return self;
+}
+
+- (size_t)offset {
+ return _state.bufferPos;
+}
+
+- (BOOL)readTag:(int8_t *)tag {
+ return ReadRawByte(&_state, tag);
+}
+
+- (BOOL)readLength:(int32_t *)length {
+ return ReadRawVarInt32(&_state, length);
+}
+
+- (NSData *)readDataWithLength:(uint32_t)length {
+ if (!CheckSize(&_state, length)) {
+ return nil;
+ }
+ const void *bytesToRead = _state.bytes + _state.bufferPos;
+ NSData *result = [NSData dataWithBytes:bytesToRead length:length];
+ _state.bufferPos += length;
+ return result;
+}
+
+@end