You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.3 KiB

6 years ago
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRMessagingCodedInputStream.h"
  17. #import "FIRMessagingDefines.h"
  18. typedef struct {
  19. const void *bytes;
  20. size_t bufferSize;
  21. size_t bufferPos;
  22. } BufferState;
  23. static BOOL CheckSize(BufferState *state, size_t size) {
  24. size_t newSize = state->bufferPos + size;
  25. if (newSize > state->bufferSize) {
  26. return NO;
  27. }
  28. return YES;
  29. }
  30. static BOOL ReadRawByte(BufferState *state, int8_t *output) {
  31. _FIRMessagingDevAssert(output != NULL && state != NULL, @"Invalid parameters");
  32. if (CheckSize(state, sizeof(int8_t))) {
  33. *output = ((int8_t *)state->bytes)[state->bufferPos++];
  34. return YES;
  35. }
  36. return NO;
  37. }
  38. static BOOL ReadRawVarInt32(BufferState *state, int32_t *output) {
  39. _FIRMessagingDevAssert(output != NULL && state != NULL, @"Invalid parameters");
  40. int8_t tmp = 0;
  41. if (!ReadRawByte(state, &tmp)) {
  42. return NO;
  43. }
  44. if (tmp >= 0) {
  45. *output = tmp;
  46. return YES;
  47. }
  48. int32_t result = tmp & 0x7f;
  49. if (!ReadRawByte(state, &tmp)) {
  50. return NO;
  51. }
  52. if (tmp >= 0) {
  53. result |= tmp << 7;
  54. } else {
  55. result |= (tmp & 0x7f) << 7;
  56. if (!ReadRawByte(state, &tmp)) {
  57. return NO;
  58. }
  59. if (tmp >= 0) {
  60. result |= tmp << 14;
  61. } else {
  62. result |= (tmp & 0x7f) << 14;
  63. if (!ReadRawByte(state, &tmp)) {
  64. return NO;
  65. }
  66. if (tmp >= 0) {
  67. result |= tmp << 21;
  68. } else {
  69. result |= (tmp & 0x7f) << 21;
  70. if (!ReadRawByte(state, &tmp)) {
  71. return NO;
  72. }
  73. result |= tmp << 28;
  74. if (tmp < 0) {
  75. // Discard upper 32 bits.
  76. for (int i = 0; i < 5; ++i) {
  77. if (!ReadRawByte(state, &tmp)) {
  78. return NO;
  79. }
  80. if (tmp >= 0) {
  81. *output = result;
  82. return YES;
  83. }
  84. }
  85. return NO;
  86. }
  87. }
  88. }
  89. }
  90. *output = result;
  91. return YES;
  92. }
  93. @interface FIRMessagingCodedInputStream()
  94. @property(nonatomic, readwrite, strong) NSData *buffer;
  95. @property(nonatomic, readwrite, assign) BufferState state;
  96. @end
  97. @implementation FIRMessagingCodedInputStream;
  98. - (instancetype)initWithData:(NSData *)data {
  99. self = [super init];
  100. if (self) {
  101. _buffer = data;
  102. _state.bytes = _buffer.bytes;
  103. _state.bufferSize = _buffer.length;
  104. }
  105. return self;
  106. }
  107. - (size_t)offset {
  108. return _state.bufferPos;
  109. }
  110. - (BOOL)readTag:(int8_t *)tag {
  111. return ReadRawByte(&_state, tag);
  112. }
  113. - (BOOL)readLength:(int32_t *)length {
  114. return ReadRawVarInt32(&_state, length);
  115. }
  116. - (NSData *)readDataWithLength:(uint32_t)length {
  117. if (!CheckSize(&_state, length)) {
  118. return nil;
  119. }
  120. const void *bytesToRead = _state.bytes + _state.bufferPos;
  121. NSData *result = [NSData dataWithBytes:bytesToRead length:length];
  122. _state.bufferPos += length;
  123. return result;
  124. }
  125. @end