GPBCodedInputStream.m 15 KB

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