GPBCodedInputStream.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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 = GPBCreateGPBStringWithUTF8(&state->bytes[state->bufferPos], size);
  193. state->bufferPos += size;
  194. }
  195. return result;
  196. }
  197. NSData *GPBCodedInputStreamReadRetainedBytes(GPBCodedInputStreamState *state) {
  198. int32_t size = ReadRawVarint32(state);
  199. if (size < 0) return nil;
  200. CheckSize(state, size);
  201. NSData *result = [[NSData alloc] initWithBytes:state->bytes + state->bufferPos
  202. length:size];
  203. state->bufferPos += size;
  204. return result;
  205. }
  206. NSData *GPBCodedInputStreamReadRetainedBytesNoCopy(
  207. GPBCodedInputStreamState *state) {
  208. int32_t size = ReadRawVarint32(state);
  209. if (size < 0) return nil;
  210. CheckSize(state, size);
  211. // Cast is safe because freeWhenDone is NO.
  212. NSData *result = [[NSData alloc]
  213. initWithBytesNoCopy:(void *)(state->bytes + state->bufferPos)
  214. length:size
  215. freeWhenDone:NO];
  216. state->bufferPos += size;
  217. return result;
  218. }
  219. size_t GPBCodedInputStreamPushLimit(GPBCodedInputStreamState *state,
  220. size_t byteLimit) {
  221. byteLimit += state->bufferPos;
  222. size_t oldLimit = state->currentLimit;
  223. if (byteLimit > oldLimit) {
  224. [NSException raise:NSInvalidArgumentException
  225. format:@"byteLimit > oldLimit: %tu > %tu", byteLimit, oldLimit];
  226. }
  227. state->currentLimit = byteLimit;
  228. return oldLimit;
  229. }
  230. void GPBCodedInputStreamPopLimit(GPBCodedInputStreamState *state,
  231. size_t oldLimit) {
  232. state->currentLimit = oldLimit;
  233. }
  234. size_t GPBCodedInputStreamBytesUntilLimit(GPBCodedInputStreamState *state) {
  235. return state->currentLimit - state->bufferPos;
  236. }
  237. BOOL GPBCodedInputStreamIsAtEnd(GPBCodedInputStreamState *state) {
  238. return (state->bufferPos == state->bufferSize) ||
  239. (state->bufferPos == state->currentLimit);
  240. }
  241. void GPBCodedInputStreamCheckLastTagWas(GPBCodedInputStreamState *state,
  242. int32_t value) {
  243. if (state->lastTag != value) {
  244. [NSException raise:NSParseErrorException
  245. format:@"Last tag: %d should be %d", state->lastTag, value];
  246. }
  247. }
  248. @implementation GPBCodedInputStream
  249. + (instancetype)streamWithData:(NSData *)data {
  250. return [[[self alloc] initWithData:data] autorelease];
  251. }
  252. - (instancetype)initWithData:(NSData *)data {
  253. if ((self = [super init])) {
  254. #ifdef DEBUG
  255. NSCAssert([self class] == [GPBCodedInputStream class],
  256. @"Subclassing of GPBCodedInputStream is not allowed.");
  257. #endif
  258. buffer_ = [data retain];
  259. state_.bytes = (const uint8_t *)[data bytes];
  260. state_.bufferSize = [data length];
  261. state_.currentLimit = state_.bufferSize;
  262. }
  263. return self;
  264. }
  265. - (void)dealloc {
  266. [buffer_ release];
  267. [super dealloc];
  268. }
  269. - (int32_t)readTag {
  270. return GPBCodedInputStreamReadTag(&state_);
  271. }
  272. - (void)checkLastTagWas:(int32_t)value {
  273. GPBCodedInputStreamCheckLastTagWas(&state_, value);
  274. }
  275. - (BOOL)skipField:(int32_t)tag {
  276. switch (GPBWireFormatGetTagWireType(tag)) {
  277. case GPBWireFormatVarint:
  278. GPBCodedInputStreamReadInt32(&state_);
  279. return YES;
  280. case GPBWireFormatFixed64:
  281. SkipRawData(&state_, sizeof(int64_t));
  282. return YES;
  283. case GPBWireFormatLengthDelimited:
  284. SkipRawData(&state_, ReadRawVarint32(&state_));
  285. return YES;
  286. case GPBWireFormatStartGroup:
  287. [self skipMessage];
  288. GPBCodedInputStreamCheckLastTagWas(
  289. &state_, GPBWireFormatMakeTag(GPBWireFormatGetTagFieldNumber(tag),
  290. GPBWireFormatEndGroup));
  291. return YES;
  292. case GPBWireFormatEndGroup:
  293. return NO;
  294. case GPBWireFormatFixed32:
  295. SkipRawData(&state_, sizeof(int32_t));
  296. return YES;
  297. }
  298. [NSException raise:NSParseErrorException format:@"Invalid tag %d", tag];
  299. return NO;
  300. }
  301. - (void)skipMessage {
  302. while (YES) {
  303. int32_t tag = GPBCodedInputStreamReadTag(&state_);
  304. if (tag == 0 || ![self skipField:tag]) {
  305. return;
  306. }
  307. }
  308. }
  309. - (double)readDouble {
  310. return GPBCodedInputStreamReadDouble(&state_);
  311. }
  312. - (float)readFloat {
  313. return GPBCodedInputStreamReadFloat(&state_);
  314. }
  315. - (uint64_t)readUInt64 {
  316. return GPBCodedInputStreamReadUInt64(&state_);
  317. }
  318. - (int64_t)readInt64 {
  319. return GPBCodedInputStreamReadInt64(&state_);
  320. }
  321. - (int32_t)readInt32 {
  322. return GPBCodedInputStreamReadInt32(&state_);
  323. }
  324. - (uint64_t)readFixed64 {
  325. return GPBCodedInputStreamReadFixed64(&state_);
  326. }
  327. - (uint32_t)readFixed32 {
  328. return GPBCodedInputStreamReadFixed32(&state_);
  329. }
  330. - (BOOL)readBool {
  331. return GPBCodedInputStreamReadBool(&state_);
  332. }
  333. - (NSString *)readString {
  334. return [GPBCodedInputStreamReadRetainedString(&state_) autorelease];
  335. }
  336. - (void)readGroup:(int32_t)fieldNumber
  337. message:(GPBMessage *)message
  338. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  339. if (state_.recursionDepth >= kDefaultRecursionLimit) {
  340. [NSException raise:NSParseErrorException
  341. format:@"recursionDepth(%tu) >= %tu", state_.recursionDepth,
  342. kDefaultRecursionLimit];
  343. }
  344. ++state_.recursionDepth;
  345. [message mergeFromCodedInputStream:self extensionRegistry:extensionRegistry];
  346. GPBCodedInputStreamCheckLastTagWas(
  347. &state_, GPBWireFormatMakeTag(fieldNumber, GPBWireFormatEndGroup));
  348. --state_.recursionDepth;
  349. }
  350. - (void)readUnknownGroup:(int32_t)fieldNumber
  351. message:(GPBUnknownFieldSet *)message {
  352. if (state_.recursionDepth >= kDefaultRecursionLimit) {
  353. [NSException raise:NSParseErrorException
  354. format:@"recursionDepth(%tu) >= %tu", state_.recursionDepth,
  355. kDefaultRecursionLimit];
  356. }
  357. ++state_.recursionDepth;
  358. [message mergeFromCodedInputStream:self];
  359. GPBCodedInputStreamCheckLastTagWas(
  360. &state_, GPBWireFormatMakeTag(fieldNumber, GPBWireFormatEndGroup));
  361. --state_.recursionDepth;
  362. }
  363. - (void)readMessage:(GPBMessage *)message
  364. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  365. int32_t length = ReadRawVarint32(&state_);
  366. if (state_.recursionDepth >= kDefaultRecursionLimit) {
  367. [NSException raise:NSParseErrorException
  368. format:@"recursionDepth(%tu) >= %tu", state_.recursionDepth,
  369. kDefaultRecursionLimit];
  370. }
  371. size_t oldLimit = GPBCodedInputStreamPushLimit(&state_, length);
  372. ++state_.recursionDepth;
  373. [message mergeFromCodedInputStream:self extensionRegistry:extensionRegistry];
  374. GPBCodedInputStreamCheckLastTagWas(&state_, 0);
  375. --state_.recursionDepth;
  376. GPBCodedInputStreamPopLimit(&state_, oldLimit);
  377. }
  378. - (void)readMapEntry:(id)mapDictionary
  379. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  380. field:(GPBFieldDescriptor *)field
  381. parentMessage:(GPBMessage *)parentMessage {
  382. int32_t length = ReadRawVarint32(&state_);
  383. if (state_.recursionDepth >= kDefaultRecursionLimit) {
  384. [NSException raise:NSParseErrorException
  385. format:@"recursionDepth(%tu) >= %tu", state_.recursionDepth,
  386. kDefaultRecursionLimit];
  387. }
  388. size_t oldLimit = GPBCodedInputStreamPushLimit(&state_, length);
  389. ++state_.recursionDepth;
  390. GPBDictionaryReadEntry(mapDictionary, self, extensionRegistry, field,
  391. parentMessage);
  392. GPBCodedInputStreamCheckLastTagWas(&state_, 0);
  393. --state_.recursionDepth;
  394. GPBCodedInputStreamPopLimit(&state_, oldLimit);
  395. }
  396. - (NSData *)readBytes {
  397. return [GPBCodedInputStreamReadRetainedBytes(&state_) autorelease];
  398. }
  399. - (uint32_t)readUInt32 {
  400. return GPBCodedInputStreamReadUInt32(&state_);
  401. }
  402. - (int32_t)readEnum {
  403. return GPBCodedInputStreamReadEnum(&state_);
  404. }
  405. - (int32_t)readSFixed32 {
  406. return GPBCodedInputStreamReadSFixed32(&state_);
  407. }
  408. - (int64_t)readSFixed64 {
  409. return GPBCodedInputStreamReadSFixed64(&state_);
  410. }
  411. - (int32_t)readSInt32 {
  412. return GPBCodedInputStreamReadSInt32(&state_);
  413. }
  414. - (int64_t)readSInt64 {
  415. return GPBCodedInputStreamReadSInt64(&state_);
  416. }
  417. @end
  418. @implementation GPBString {
  419. @package
  420. CFStringRef string_;
  421. unsigned char *utf8_;
  422. NSUInteger utf8Len_;
  423. // This lock is used to gate access to utf8_. Once GPBStringInitStringValue()
  424. // has been called, string_ will be filled in, and utf8_ will be NULL.
  425. OSSpinLock lock_;
  426. BOOL hasBOM_;
  427. BOOL is7BitAscii_;
  428. }
  429. // Returns true if the passed in bytes are 7 bit ascii.
  430. // This routine needs to be fast.
  431. static bool AreBytesIn7BitASCII(const uint8_t *bytes, NSUInteger len) {
  432. // In the loops below, it's more efficient to collect rather than do
  433. // conditional at every step.
  434. #if __LP64__
  435. // Align bytes. This is especially important in case of 3 byte BOM.
  436. while (len > 0 && ((size_t)bytes & 0x07)) {
  437. if (*bytes++ & 0x80) return false;
  438. len--;
  439. }
  440. while (len >= 32) {
  441. uint64_t val = *(const uint64_t *)bytes;
  442. uint64_t hiBits = (val & 0x8080808080808080ULL);
  443. bytes += 8;
  444. val = *(const uint64_t *)bytes;
  445. hiBits |= (val & 0x8080808080808080ULL);
  446. bytes += 8;
  447. val = *(const uint64_t *)bytes;
  448. hiBits |= (val & 0x8080808080808080ULL);
  449. bytes += 8;
  450. val = *(const uint64_t *)bytes;
  451. if (hiBits | (val & 0x8080808080808080ULL)) return false;
  452. bytes += 8;
  453. len -= 32;
  454. }
  455. while (len >= 16) {
  456. uint64_t val = *(const uint64_t *)bytes;
  457. uint64_t hiBits = (val & 0x8080808080808080ULL);
  458. bytes += 8;
  459. val = *(const uint64_t *)bytes;
  460. if (hiBits | (val & 0x8080808080808080ULL)) return false;
  461. bytes += 8;
  462. len -= 16;
  463. }
  464. while (len >= 8) {
  465. uint64_t val = *(const uint64_t *)bytes;
  466. if (val & 0x8080808080808080ULL) return false;
  467. bytes += 8;
  468. len -= 8;
  469. }
  470. #else // __LP64__
  471. // Align bytes. This is especially important in case of 3 byte BOM.
  472. while (len > 0 && ((size_t)bytes & 0x03)) {
  473. if (*bytes++ & 0x80) return false;
  474. len--;
  475. }
  476. while (len >= 16) {
  477. uint32_t val = *(const uint32_t *)bytes;
  478. uint32_t hiBits = (val & 0x80808080U);
  479. bytes += 4;
  480. val = *(const uint32_t *)bytes;
  481. hiBits |= (val & 0x80808080U);
  482. bytes += 4;
  483. val = *(const uint32_t *)bytes;
  484. hiBits |= (val & 0x80808080U);
  485. bytes += 4;
  486. val = *(const uint32_t *)bytes;
  487. if (hiBits | (val & 0x80808080U)) return false;
  488. bytes += 4;
  489. len -= 16;
  490. }
  491. while (len >= 8) {
  492. uint32_t val = *(const uint32_t *)bytes;
  493. uint32_t hiBits = (val & 0x80808080U);
  494. bytes += 4;
  495. val = *(const uint32_t *)bytes;
  496. if (hiBits | (val & 0x80808080U)) return false;
  497. bytes += 4;
  498. len -= 8;
  499. }
  500. #endif // __LP64__
  501. while (len >= 4) {
  502. uint32_t val = *(const uint32_t *)bytes;
  503. if (val & 0x80808080U) return false;
  504. bytes += 4;
  505. len -= 4;
  506. }
  507. while (len--) {
  508. if (*bytes++ & 0x80) return false;
  509. }
  510. return true;
  511. }
  512. static void GPBStringInitStringValue(GPBString *string) {
  513. OSSpinLockLock(&string->lock_);
  514. GPBStringInitStringValueAlreadyLocked(string);
  515. OSSpinLockUnlock(&string->lock_);
  516. }
  517. static void GPBStringInitStringValueAlreadyLocked(GPBString *string) {
  518. if (string->string_ == NULL && string->utf8_ != NULL) {
  519. // Using kCFAllocatorMalloc for contentsDeallocator, as buffer in
  520. // string->utf8_ is being handed off.
  521. string->string_ = CFStringCreateWithBytesNoCopy(
  522. NULL, string->utf8_, string->utf8Len_, kCFStringEncodingUTF8, false,
  523. kCFAllocatorMalloc);
  524. if (!string->string_) {
  525. #ifdef DEBUG
  526. // https://developers.google.com/protocol-buffers/docs/proto#scalar
  527. NSLog(@"UTF8 failure, is some field type 'string' when it should be "
  528. @"'bytes'?");
  529. #endif
  530. string->string_ = CFSTR("");
  531. string->utf8Len_ = 0;
  532. // On failure, we have to clean up the buffer.
  533. free(string->utf8_);
  534. }
  535. string->utf8_ = NULL;
  536. }
  537. }
  538. GPBString *GPBCreateGPBStringWithUTF8(const void *bytes, NSUInteger length) {
  539. GPBString *result = [[GPBString alloc] initWithBytes:bytes length:length];
  540. return result;
  541. }
  542. - (instancetype)initWithBytes:(const void *)bytes length:(NSUInteger)length {
  543. self = [super init];
  544. if (self) {
  545. utf8_ = malloc(length);
  546. memcpy(utf8_, bytes, length);
  547. utf8Len_ = length;
  548. lock_ = OS_SPINLOCK_INIT;
  549. is7BitAscii_ = AreBytesIn7BitASCII(bytes, length);
  550. if (length >= 3 && memcmp(utf8_, "\xef\xbb\xbf", 3) == 0) {
  551. // We can't just remove the BOM from the string here, because in the case
  552. // where we have > 1 BOM at the beginning of the string, we will remove one,
  553. // and the internal NSString we create will remove the next one, and we will
  554. // end up with a GPBString != NSString issue.
  555. // We also just can't remove all the BOMs because then we would end up with
  556. // potential cases where a GPBString and an NSString made with the same
  557. // UTF8 buffer would in fact be different.
  558. // We record the fact we have a BOM, and use it as necessary to simulate
  559. // what NSString would return for various calls.
  560. hasBOM_ = YES;
  561. #if DEBUG
  562. // Sending BOMs across the line is just wasting bits.
  563. NSLog(@"Bad data? String should not have BOM!");
  564. #endif // DEBUG
  565. }
  566. }
  567. return self;
  568. }
  569. - (void)dealloc {
  570. if (string_ != NULL) {
  571. CFRelease(string_);
  572. }
  573. if (utf8_ != NULL) {
  574. free(utf8_);
  575. }
  576. [super dealloc];
  577. }
  578. // Required NSString overrides.
  579. - (NSUInteger)length {
  580. if (is7BitAscii_) {
  581. return utf8Len_;
  582. } else {
  583. GPBStringInitStringValue(self);
  584. return CFStringGetLength(string_);
  585. }
  586. }
  587. - (unichar)characterAtIndex:(NSUInteger)anIndex {
  588. OSSpinLockLock(&lock_);
  589. if (is7BitAscii_ && utf8_) {
  590. unichar result = utf8_[anIndex];
  591. OSSpinLockUnlock(&lock_);
  592. return result;
  593. } else {
  594. GPBStringInitStringValueAlreadyLocked(self);
  595. OSSpinLockUnlock(&lock_);
  596. return CFStringGetCharacterAtIndex(string_, anIndex);
  597. }
  598. }
  599. // Override a couple of methods that typically want high performance.
  600. - (id)copyWithZone:(NSZone *)zone {
  601. GPBStringInitStringValue(self);
  602. return [(NSString *)string_ copyWithZone:zone];
  603. }
  604. - (id)mutableCopyWithZone:(NSZone *)zone {
  605. GPBStringInitStringValue(self);
  606. return [(NSString *)string_ mutableCopyWithZone:zone];
  607. }
  608. - (NSUInteger)hash {
  609. // Must convert to string here to make sure that the hash is always
  610. // consistent no matter what state the GPBString is in.
  611. GPBStringInitStringValue(self);
  612. return CFHash(string_);
  613. }
  614. - (BOOL)isEqual:(id)object {
  615. if (self == object) {
  616. return YES;
  617. }
  618. if ([object isKindOfClass:[NSString class]]) {
  619. GPBStringInitStringValue(self);
  620. return CFStringCompare(string_, (CFStringRef)object, 0) ==
  621. kCFCompareEqualTo;
  622. }
  623. return NO;
  624. }
  625. - (void)getCharacters:(unichar *)buffer range:(NSRange)aRange {
  626. OSSpinLockLock(&lock_);
  627. if (is7BitAscii_ && utf8_) {
  628. unsigned char *bytes = &(utf8_[aRange.location]);
  629. for (NSUInteger i = 0; i < aRange.length; ++i) {
  630. buffer[i] = bytes[i];
  631. }
  632. OSSpinLockUnlock(&lock_);
  633. } else {
  634. GPBStringInitStringValueAlreadyLocked(self);
  635. OSSpinLockUnlock(&lock_);
  636. CFStringGetCharacters(string_, CFRangeMake(aRange.location, aRange.length),
  637. buffer);
  638. }
  639. }
  640. - (NSUInteger)lengthOfBytesUsingEncoding:(NSStringEncoding)encoding {
  641. if ((encoding == NSUTF8StringEncoding) ||
  642. (encoding == NSASCIIStringEncoding && is7BitAscii_)) {
  643. return utf8Len_ - (hasBOM_ ? 3 : 0);
  644. } else {
  645. GPBStringInitStringValue(self);
  646. return [(NSString *)string_ lengthOfBytesUsingEncoding:encoding];
  647. }
  648. }
  649. - (BOOL)getBytes:(void *)buffer
  650. maxLength:(NSUInteger)maxLength
  651. usedLength:(NSUInteger *)usedLength
  652. encoding:(NSStringEncoding)encoding
  653. options:(NSStringEncodingConversionOptions)options
  654. range:(NSRange)range
  655. remainingRange:(NSRangePointer)remainingRange {
  656. // [NSString getBytes:maxLength:usedLength:encoding:options:range:remainingRange]
  657. // does not return reliable results if the maxLength argument is 0
  658. // (Radar 16385183). Therefore we have special cased it as a slow case so
  659. // that it behaves however Apple intends it to behave. It should be a rare
  660. // case.
  661. //
  662. // [NSString getBytes:maxLength:usedLength:encoding:options:range:remainingRange]
  663. // does not return reliable results if the range is outside of the strings
  664. // length (Radar 16396177). Therefore we have special cased it as a slow
  665. // case so that it behaves however Apple intends it to behave. It should
  666. // be a rare case.
  667. //
  668. // We can optimize the UTF8StringEncoding and NSASCIIStringEncoding with no
  669. // options cases.
  670. if ((options == 0) &&
  671. (encoding == NSUTF8StringEncoding || encoding == NSASCIIStringEncoding) &&
  672. (maxLength != 0) &&
  673. (NSMaxRange(range) <= utf8Len_)) {
  674. // Might be able to optimize it.
  675. OSSpinLockLock(&lock_);
  676. if (is7BitAscii_ && utf8_) {
  677. NSUInteger length = range.length;
  678. length = (length < maxLength) ? length : maxLength;
  679. memcpy(buffer, utf8_ + range.location, length);
  680. if (usedLength) {
  681. *usedLength = length;
  682. }
  683. if (remainingRange) {
  684. remainingRange->location = range.location + length;
  685. remainingRange->length = range.length - length;
  686. }
  687. OSSpinLockUnlock(&lock_);
  688. if (length > 0) {
  689. return YES;
  690. } else {
  691. return NO;
  692. }
  693. } else {
  694. GPBStringInitStringValueAlreadyLocked(self);
  695. OSSpinLockUnlock(&lock_);
  696. }
  697. } else {
  698. GPBStringInitStringValue(self);
  699. }
  700. return [(NSString *)string_ getBytes:buffer
  701. maxLength:maxLength
  702. usedLength:usedLength
  703. encoding:encoding
  704. options:options
  705. range:range
  706. remainingRange:remainingRange];
  707. }
  708. @end