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.

147 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
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 "Firebase/Messaging/FIRMessagingCodedInputStream.h"
  17. #import "Firebase/Messaging/FIRMMessageCode.h"
  18. #import "Firebase/Messaging/FIRMessagingLogger.h"
  19. typedef struct {
  20. const void *bytes;
  21. size_t bufferSize;
  22. size_t bufferPos;
  23. } BufferState;
  24. static BOOL CheckSize(BufferState *state, size_t size) {
  25. size_t newSize = state->bufferPos + size;
  26. if (newSize > state->bufferSize) {
  27. return NO;
  28. }
  29. return YES;
  30. }
  31. static BOOL ReadRawByte(BufferState *state, int8_t *output) {
  32. if (state == NULL || output == NULL) {
  33. FIRMessagingLoggerDebug(kFIRMessagingCodeInputStreamInvalidParameters, @"Invalid parameters.");
  34. }
  35. if (output != nil && CheckSize(state, sizeof(int8_t))) {
  36. *output = ((int8_t *)state->bytes)[state->bufferPos++];
  37. return YES;
  38. }
  39. return NO;
  40. }
  41. static BOOL ReadRawVarInt32(BufferState *state, int32_t *output) {
  42. if (state == NULL || output == NULL) {
  43. FIRMessagingLoggerDebug(kFIRMessagingCodeInputStreamInvalidParameters, @"Invalid parameters.");
  44. return NO;
  45. }
  46. int8_t tmp = 0;
  47. if (!ReadRawByte(state, &tmp)) {
  48. return NO;
  49. }
  50. if (tmp >= 0) {
  51. *output = tmp;
  52. return YES;
  53. }
  54. int32_t result = tmp & 0x7f;
  55. if (!ReadRawByte(state, &tmp)) {
  56. return NO;
  57. }
  58. if (tmp >= 0) {
  59. result |= tmp << 7;
  60. } else {
  61. result |= (tmp & 0x7f) << 7;
  62. if (!ReadRawByte(state, &tmp)) {
  63. return NO;
  64. }
  65. if (tmp >= 0) {
  66. result |= tmp << 14;
  67. } else {
  68. result |= (tmp & 0x7f) << 14;
  69. if (!ReadRawByte(state, &tmp)) {
  70. return NO;
  71. }
  72. if (tmp >= 0) {
  73. result |= tmp << 21;
  74. } else {
  75. result |= (tmp & 0x7f) << 21;
  76. if (!ReadRawByte(state, &tmp)) {
  77. return NO;
  78. }
  79. result |= tmp << 28;
  80. if (tmp < 0) {
  81. // Discard upper 32 bits.
  82. for (int i = 0; i < 5; ++i) {
  83. if (!ReadRawByte(state, &tmp)) {
  84. return NO;
  85. }
  86. if (tmp >= 0) {
  87. *output = result;
  88. return YES;
  89. }
  90. }
  91. return NO;
  92. }
  93. }
  94. }
  95. }
  96. *output = result;
  97. return YES;
  98. }
  99. @interface FIRMessagingCodedInputStream()
  100. @property(nonatomic, readwrite, strong) NSData *buffer;
  101. @property(nonatomic, readwrite, assign) BufferState state;
  102. @end
  103. @implementation FIRMessagingCodedInputStream;
  104. - (instancetype)initWithData:(NSData *)data {
  105. self = [super init];
  106. if (self) {
  107. _buffer = data;
  108. _state.bytes = _buffer.bytes;
  109. _state.bufferSize = _buffer.length;
  110. }
  111. return self;
  112. }
  113. - (size_t)offset {
  114. return _state.bufferPos;
  115. }
  116. - (BOOL)readTag:(int8_t *)tag {
  117. return ReadRawByte(&_state, tag);
  118. }
  119. - (BOOL)readLength:(int32_t *)length {
  120. return ReadRawVarInt32(&_state, length);
  121. }
  122. - (NSData *)readDataWithLength:(uint32_t)length {
  123. if (!CheckSize(&_state, length)) {
  124. return nil;
  125. }
  126. const void *bytesToRead = _state.bytes + _state.bufferPos;
  127. NSData *result = [NSData dataWithBytes:bytesToRead length:length];
  128. _state.bufferPos += length;
  129. return result;
  130. }
  131. @end