GPBCodedInputStream.m 16 KB

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