GPBCodedInputStream.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // 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. #import "GPBCodedInputStream_PackagePrivate.h"
  31. #import "GPBDictionary_PackagePrivate.h"
  32. #import "GPBMessage_PackagePrivate.h"
  33. #import "GPBUnknownFieldSet_PackagePrivate.h"
  34. #import "GPBUtilities_PackagePrivate.h"
  35. #import "GPBWireFormat.h"
  36. NSString *const GPBCodedInputStreamException =
  37. GPBNSStringifySymbol(GPBCodedInputStreamException);
  38. NSString *const GPBCodedInputStreamUnderlyingErrorKey =
  39. GPBNSStringifySymbol(GPBCodedInputStreamUnderlyingErrorKey);
  40. NSString *const GPBCodedInputStreamErrorDomain =
  41. GPBNSStringifySymbol(GPBCodedInputStreamErrorDomain);
  42. // Matching:
  43. // https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/main/java/com/google/protobuf/CodedInputStream.java#L62
  44. // private static final int DEFAULT_RECURSION_LIMIT = 100;
  45. // https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/io/coded_stream.cc#L86
  46. // int CodedInputStream::default_recursion_limit_ = 100;
  47. static const NSUInteger kDefaultRecursionLimit = 100;
  48. static void RaiseException(NSInteger code, NSString *reason) {
  49. NSDictionary *errorInfo = nil;
  50. if ([reason length]) {
  51. errorInfo = @{ GPBErrorReasonKey: reason };
  52. }
  53. NSError *error = [NSError errorWithDomain:GPBCodedInputStreamErrorDomain
  54. code:code
  55. userInfo:errorInfo];
  56. NSDictionary *exceptionInfo =
  57. @{ GPBCodedInputStreamUnderlyingErrorKey: error };
  58. [[NSException exceptionWithName:GPBCodedInputStreamException
  59. reason:reason
  60. userInfo:exceptionInfo] raise];
  61. }
  62. static void CheckRecursionLimit(GPBCodedInputStreamState *state) {
  63. if (state->recursionDepth >= kDefaultRecursionLimit) {
  64. RaiseException(GPBCodedInputStreamErrorRecursionDepthExceeded, nil);
  65. }
  66. }
  67. static void CheckSize(GPBCodedInputStreamState *state, size_t size) {
  68. size_t newSize = state->bufferPos + size;
  69. if (newSize > state->bufferSize) {
  70. RaiseException(GPBCodedInputStreamErrorInvalidSize, nil);
  71. }
  72. if (newSize > state->currentLimit) {
  73. // Fast forward to end of currentLimit;
  74. state->bufferPos = state->currentLimit;
  75. RaiseException(GPBCodedInputStreamErrorSubsectionLimitReached, nil);
  76. }
  77. }
  78. static int8_t ReadRawByte(GPBCodedInputStreamState *state) {
  79. CheckSize(state, sizeof(int8_t));
  80. return ((int8_t *)state->bytes)[state->bufferPos++];
  81. }
  82. static int32_t ReadRawLittleEndian32(GPBCodedInputStreamState *state) {
  83. CheckSize(state, sizeof(int32_t));
  84. // Not using OSReadLittleInt32 because it has undocumented dependency
  85. // on reads being aligned.
  86. int32_t value;
  87. memcpy(&value, state->bytes + state->bufferPos, sizeof(int32_t));
  88. value = OSSwapLittleToHostInt32(value);
  89. state->bufferPos += sizeof(int32_t);
  90. return value;
  91. }
  92. static int64_t ReadRawLittleEndian64(GPBCodedInputStreamState *state) {
  93. CheckSize(state, sizeof(int64_t));
  94. // Not using OSReadLittleInt64 because it has undocumented dependency
  95. // on reads being aligned.
  96. int64_t value;
  97. memcpy(&value, state->bytes + state->bufferPos, sizeof(int64_t));
  98. value = OSSwapLittleToHostInt64(value);
  99. state->bufferPos += sizeof(int64_t);
  100. return value;
  101. }
  102. static int64_t ReadRawVarint64(GPBCodedInputStreamState *state) {
  103. int32_t shift = 0;
  104. int64_t result = 0;
  105. while (shift < 64) {
  106. int8_t b = ReadRawByte(state);
  107. result |= (int64_t)((uint64_t)(b & 0x7F) << shift);
  108. if ((b & 0x80) == 0) {
  109. return result;
  110. }
  111. shift += 7;
  112. }
  113. RaiseException(GPBCodedInputStreamErrorInvalidVarInt, @"Invalid VarInt64");
  114. return 0;
  115. }
  116. static int32_t ReadRawVarint32(GPBCodedInputStreamState *state) {
  117. return (int32_t)ReadRawVarint64(state);
  118. }
  119. static void SkipRawData(GPBCodedInputStreamState *state, size_t size) {
  120. CheckSize(state, size);
  121. state->bufferPos += size;
  122. }
  123. double GPBCodedInputStreamReadDouble(GPBCodedInputStreamState *state) {
  124. int64_t value = ReadRawLittleEndian64(state);
  125. return GPBConvertInt64ToDouble(value);
  126. }
  127. float GPBCodedInputStreamReadFloat(GPBCodedInputStreamState *state) {
  128. int32_t value = ReadRawLittleEndian32(state);
  129. return GPBConvertInt32ToFloat(value);
  130. }
  131. uint64_t GPBCodedInputStreamReadUInt64(GPBCodedInputStreamState *state) {
  132. uint64_t value = ReadRawVarint64(state);
  133. return value;
  134. }
  135. uint32_t GPBCodedInputStreamReadUInt32(GPBCodedInputStreamState *state) {
  136. uint32_t value = ReadRawVarint32(state);
  137. return value;
  138. }
  139. int64_t GPBCodedInputStreamReadInt64(GPBCodedInputStreamState *state) {
  140. int64_t value = ReadRawVarint64(state);
  141. return value;
  142. }
  143. int32_t GPBCodedInputStreamReadInt32(GPBCodedInputStreamState *state) {
  144. int32_t value = ReadRawVarint32(state);
  145. return value;
  146. }
  147. uint64_t GPBCodedInputStreamReadFixed64(GPBCodedInputStreamState *state) {
  148. uint64_t value = ReadRawLittleEndian64(state);
  149. return value;
  150. }
  151. uint32_t GPBCodedInputStreamReadFixed32(GPBCodedInputStreamState *state) {
  152. uint32_t value = ReadRawLittleEndian32(state);
  153. return value;
  154. }
  155. int32_t GPBCodedInputStreamReadEnum(GPBCodedInputStreamState *state) {
  156. int32_t value = ReadRawVarint32(state);
  157. return value;
  158. }
  159. int32_t GPBCodedInputStreamReadSFixed32(GPBCodedInputStreamState *state) {
  160. int32_t value = ReadRawLittleEndian32(state);
  161. return value;
  162. }
  163. int64_t GPBCodedInputStreamReadSFixed64(GPBCodedInputStreamState *state) {
  164. int64_t value = ReadRawLittleEndian64(state);
  165. return value;
  166. }
  167. int32_t GPBCodedInputStreamReadSInt32(GPBCodedInputStreamState *state) {
  168. int32_t value = GPBDecodeZigZag32(ReadRawVarint32(state));
  169. return value;
  170. }
  171. int64_t GPBCodedInputStreamReadSInt64(GPBCodedInputStreamState *state) {
  172. int64_t value = GPBDecodeZigZag64(ReadRawVarint64(state));
  173. return value;
  174. }
  175. BOOL GPBCodedInputStreamReadBool(GPBCodedInputStreamState *state) {
  176. return ReadRawVarint64(state) != 0;
  177. }
  178. int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state) {
  179. if (GPBCodedInputStreamIsAtEnd(state)) {
  180. state->lastTag = 0;
  181. return 0;
  182. }
  183. state->lastTag = ReadRawVarint32(state);
  184. // Tags have to include a valid wireformat.
  185. if (!GPBWireFormatIsValidTag(state->lastTag)) {
  186. RaiseException(GPBCodedInputStreamErrorInvalidTag,
  187. @"Invalid wireformat in tag.");
  188. }
  189. // Zero is not a valid field number.
  190. if (GPBWireFormatGetTagFieldNumber(state->lastTag) == 0) {
  191. RaiseException(GPBCodedInputStreamErrorInvalidTag,
  192. @"A zero field number on the wire is invalid.");
  193. }
  194. return state->lastTag;
  195. }
  196. NSString *GPBCodedInputStreamReadRetainedString(
  197. GPBCodedInputStreamState *state) {
  198. int32_t size = ReadRawVarint32(state);
  199. NSString *result;
  200. if (size == 0) {
  201. result = @"";
  202. } else {
  203. CheckSize(state, size);
  204. result = [[NSString alloc] initWithBytes:&state->bytes[state->bufferPos]
  205. length:size
  206. encoding:NSUTF8StringEncoding];
  207. state->bufferPos += size;
  208. if (!result) {
  209. #ifdef DEBUG
  210. // https://developers.google.com/protocol-buffers/docs/proto#scalar
  211. NSLog(@"UTF-8 failure, is some field type 'string' when it should be "
  212. @"'bytes'?");
  213. #endif
  214. RaiseException(GPBCodedInputStreamErrorInvalidUTF8, nil);
  215. }
  216. }
  217. return result;
  218. }
  219. NSData *GPBCodedInputStreamReadRetainedBytes(GPBCodedInputStreamState *state) {
  220. int32_t size = ReadRawVarint32(state);
  221. if (size < 0) return nil;
  222. CheckSize(state, size);
  223. NSData *result = [[NSData alloc] initWithBytes:state->bytes + state->bufferPos
  224. length:size];
  225. state->bufferPos += size;
  226. return result;
  227. }
  228. NSData *GPBCodedInputStreamReadRetainedBytesNoCopy(
  229. GPBCodedInputStreamState *state) {
  230. int32_t size = ReadRawVarint32(state);
  231. if (size < 0) return nil;
  232. CheckSize(state, size);
  233. // Cast is safe because freeWhenDone is NO.
  234. NSData *result = [[NSData alloc]
  235. initWithBytesNoCopy:(void *)(state->bytes + state->bufferPos)
  236. length:size
  237. freeWhenDone:NO];
  238. state->bufferPos += size;
  239. return result;
  240. }
  241. size_t GPBCodedInputStreamPushLimit(GPBCodedInputStreamState *state,
  242. size_t byteLimit) {
  243. byteLimit += state->bufferPos;
  244. size_t oldLimit = state->currentLimit;
  245. if (byteLimit > oldLimit) {
  246. RaiseException(GPBCodedInputStreamErrorInvalidSubsectionLimit, nil);
  247. }
  248. state->currentLimit = byteLimit;
  249. return oldLimit;
  250. }
  251. void GPBCodedInputStreamPopLimit(GPBCodedInputStreamState *state,
  252. size_t oldLimit) {
  253. state->currentLimit = oldLimit;
  254. }
  255. size_t GPBCodedInputStreamBytesUntilLimit(GPBCodedInputStreamState *state) {
  256. return state->currentLimit - state->bufferPos;
  257. }
  258. BOOL GPBCodedInputStreamIsAtEnd(GPBCodedInputStreamState *state) {
  259. return (state->bufferPos == state->bufferSize) ||
  260. (state->bufferPos == state->currentLimit);
  261. }
  262. void GPBCodedInputStreamCheckLastTagWas(GPBCodedInputStreamState *state,
  263. int32_t value) {
  264. if (state->lastTag != value) {
  265. RaiseException(GPBCodedInputStreamErrorInvalidTag, @"Unexpected tag read");
  266. }
  267. }
  268. @implementation GPBCodedInputStream
  269. + (instancetype)streamWithData:(NSData *)data {
  270. return [[[self alloc] initWithData:data] autorelease];
  271. }
  272. - (instancetype)initWithData:(NSData *)data {
  273. if ((self = [super init])) {
  274. #ifdef DEBUG
  275. NSCAssert([self class] == [GPBCodedInputStream class],
  276. @"Subclassing of GPBCodedInputStream is not allowed.");
  277. #endif
  278. buffer_ = [data retain];
  279. state_.bytes = (const uint8_t *)[data bytes];
  280. state_.bufferSize = [data length];
  281. state_.currentLimit = state_.bufferSize;
  282. }
  283. return self;
  284. }
  285. - (void)dealloc {
  286. [buffer_ release];
  287. [super dealloc];
  288. }
  289. // Direct access is use for speed, to avoid even internally declaring things
  290. // read/write, etc. The warning is enabled in the project to ensure code calling
  291. // protos can turn on -Wdirect-ivar-access without issues.
  292. #pragma clang diagnostic push
  293. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  294. - (int32_t)readTag {
  295. return GPBCodedInputStreamReadTag(&state_);
  296. }
  297. - (void)checkLastTagWas:(int32_t)value {
  298. GPBCodedInputStreamCheckLastTagWas(&state_, value);
  299. }
  300. - (BOOL)skipField:(int32_t)tag {
  301. NSAssert(GPBWireFormatIsValidTag(tag), @"Invalid tag");
  302. switch (GPBWireFormatGetTagWireType(tag)) {
  303. case GPBWireFormatVarint:
  304. GPBCodedInputStreamReadInt32(&state_);
  305. return YES;
  306. case GPBWireFormatFixed64:
  307. SkipRawData(&state_, sizeof(int64_t));
  308. return YES;
  309. case GPBWireFormatLengthDelimited:
  310. SkipRawData(&state_, ReadRawVarint32(&state_));
  311. return YES;
  312. case GPBWireFormatStartGroup:
  313. [self skipMessage];
  314. GPBCodedInputStreamCheckLastTagWas(
  315. &state_, GPBWireFormatMakeTag(GPBWireFormatGetTagFieldNumber(tag),
  316. GPBWireFormatEndGroup));
  317. return YES;
  318. case GPBWireFormatEndGroup:
  319. return NO;
  320. case GPBWireFormatFixed32:
  321. SkipRawData(&state_, sizeof(int32_t));
  322. return YES;
  323. }
  324. }
  325. - (void)skipMessage {
  326. while (YES) {
  327. int32_t tag = GPBCodedInputStreamReadTag(&state_);
  328. if (tag == 0 || ![self skipField:tag]) {
  329. return;
  330. }
  331. }
  332. }
  333. - (BOOL)isAtEnd {
  334. return GPBCodedInputStreamIsAtEnd(&state_);
  335. }
  336. - (size_t)position {
  337. return state_.bufferPos;
  338. }
  339. - (size_t)pushLimit:(size_t)byteLimit {
  340. return GPBCodedInputStreamPushLimit(&state_, byteLimit);
  341. }
  342. - (void)popLimit:(size_t)oldLimit {
  343. GPBCodedInputStreamPopLimit(&state_, oldLimit);
  344. }
  345. - (double)readDouble {
  346. return GPBCodedInputStreamReadDouble(&state_);
  347. }
  348. - (float)readFloat {
  349. return GPBCodedInputStreamReadFloat(&state_);
  350. }
  351. - (uint64_t)readUInt64 {
  352. return GPBCodedInputStreamReadUInt64(&state_);
  353. }
  354. - (int64_t)readInt64 {
  355. return GPBCodedInputStreamReadInt64(&state_);
  356. }
  357. - (int32_t)readInt32 {
  358. return GPBCodedInputStreamReadInt32(&state_);
  359. }
  360. - (uint64_t)readFixed64 {
  361. return GPBCodedInputStreamReadFixed64(&state_);
  362. }
  363. - (uint32_t)readFixed32 {
  364. return GPBCodedInputStreamReadFixed32(&state_);
  365. }
  366. - (BOOL)readBool {
  367. return GPBCodedInputStreamReadBool(&state_);
  368. }
  369. - (NSString *)readString {
  370. return [GPBCodedInputStreamReadRetainedString(&state_) autorelease];
  371. }
  372. - (void)readGroup:(int32_t)fieldNumber
  373. message:(GPBMessage *)message
  374. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  375. CheckRecursionLimit(&state_);
  376. ++state_.recursionDepth;
  377. [message mergeFromCodedInputStream:self extensionRegistry:extensionRegistry];
  378. GPBCodedInputStreamCheckLastTagWas(
  379. &state_, GPBWireFormatMakeTag(fieldNumber, GPBWireFormatEndGroup));
  380. --state_.recursionDepth;
  381. }
  382. - (void)readUnknownGroup:(int32_t)fieldNumber
  383. message:(GPBUnknownFieldSet *)message {
  384. CheckRecursionLimit(&state_);
  385. ++state_.recursionDepth;
  386. [message mergeFromCodedInputStream:self];
  387. GPBCodedInputStreamCheckLastTagWas(
  388. &state_, GPBWireFormatMakeTag(fieldNumber, GPBWireFormatEndGroup));
  389. --state_.recursionDepth;
  390. }
  391. - (void)readMessage:(GPBMessage *)message
  392. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  393. CheckRecursionLimit(&state_);
  394. int32_t length = ReadRawVarint32(&state_);
  395. size_t oldLimit = GPBCodedInputStreamPushLimit(&state_, length);
  396. ++state_.recursionDepth;
  397. [message mergeFromCodedInputStream:self extensionRegistry:extensionRegistry];
  398. GPBCodedInputStreamCheckLastTagWas(&state_, 0);
  399. --state_.recursionDepth;
  400. GPBCodedInputStreamPopLimit(&state_, oldLimit);
  401. }
  402. - (void)readMapEntry:(id)mapDictionary
  403. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  404. field:(GPBFieldDescriptor *)field
  405. parentMessage:(GPBMessage *)parentMessage {
  406. CheckRecursionLimit(&state_);
  407. int32_t length = ReadRawVarint32(&state_);
  408. size_t oldLimit = GPBCodedInputStreamPushLimit(&state_, length);
  409. ++state_.recursionDepth;
  410. GPBDictionaryReadEntry(mapDictionary, self, extensionRegistry, field,
  411. parentMessage);
  412. GPBCodedInputStreamCheckLastTagWas(&state_, 0);
  413. --state_.recursionDepth;
  414. GPBCodedInputStreamPopLimit(&state_, oldLimit);
  415. }
  416. - (NSData *)readBytes {
  417. return [GPBCodedInputStreamReadRetainedBytes(&state_) autorelease];
  418. }
  419. - (uint32_t)readUInt32 {
  420. return GPBCodedInputStreamReadUInt32(&state_);
  421. }
  422. - (int32_t)readEnum {
  423. return GPBCodedInputStreamReadEnum(&state_);
  424. }
  425. - (int32_t)readSFixed32 {
  426. return GPBCodedInputStreamReadSFixed32(&state_);
  427. }
  428. - (int64_t)readSFixed64 {
  429. return GPBCodedInputStreamReadSFixed64(&state_);
  430. }
  431. - (int32_t)readSInt32 {
  432. return GPBCodedInputStreamReadSInt32(&state_);
  433. }
  434. - (int64_t)readSInt64 {
  435. return GPBCodedInputStreamReadSInt64(&state_);
  436. }
  437. #pragma clang diagnostic pop
  438. @end