GPBUnknownFieldSet.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "GPBUnknownFieldSet_PackagePrivate.h"
  31. #import "GPBCodedInputStream_PackagePrivate.h"
  32. #import "GPBCodedOutputStream.h"
  33. #import "GPBUnknownField_PackagePrivate.h"
  34. #import "GPBUtilities.h"
  35. #import "GPBWireFormat.h"
  36. #pragma mark CFDictionaryKeyCallBacks
  37. // We use a custom dictionary here because our keys are numbers and
  38. // conversion back and forth from NSNumber was costing us performance.
  39. // If/when we move to C++ this could be done using a std::map and some
  40. // careful retain/release calls.
  41. static const void *GPBUnknownFieldSetKeyRetain(CFAllocatorRef allocator,
  42. const void *value) {
  43. #pragma unused(allocator)
  44. return value;
  45. }
  46. static void GPBUnknownFieldSetKeyRelease(CFAllocatorRef allocator,
  47. const void *value) {
  48. #pragma unused(allocator)
  49. #pragma unused(value)
  50. }
  51. static CFStringRef GPBUnknownFieldSetCopyKeyDescription(const void *value) {
  52. return CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%d"),
  53. (int)value);
  54. }
  55. static Boolean GPBUnknownFieldSetKeyEqual(const void *value1,
  56. const void *value2) {
  57. return value1 == value2;
  58. }
  59. static CFHashCode GPBUnknownFieldSetKeyHash(const void *value) {
  60. return (CFHashCode)value;
  61. }
  62. #pragma mark Helpers
  63. static void checkNumber(int32_t number) {
  64. if (number == 0) {
  65. [NSException raise:NSInvalidArgumentException
  66. format:@"Zero is not a valid field number."];
  67. }
  68. }
  69. @implementation GPBUnknownFieldSet {
  70. @package
  71. CFMutableDictionaryRef fields_;
  72. }
  73. static void CopyWorker(const void *key, const void *value, void *context) {
  74. #pragma unused(key)
  75. GPBUnknownField *field = value;
  76. GPBUnknownFieldSet *result = context;
  77. GPBUnknownField *copied = [field copy];
  78. [result addField:copied];
  79. [copied release];
  80. }
  81. // Direct access is use for speed, to avoid even internally declaring things
  82. // read/write, etc. The warning is enabled in the project to ensure code calling
  83. // protos can turn on -Wdirect-ivar-access without issues.
  84. #pragma clang diagnostic push
  85. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  86. - (id)copyWithZone:(NSZone *)zone {
  87. GPBUnknownFieldSet *result = [[GPBUnknownFieldSet allocWithZone:zone] init];
  88. if (fields_) {
  89. CFDictionaryApplyFunction(fields_, CopyWorker, result);
  90. }
  91. return result;
  92. }
  93. - (void)dealloc {
  94. if (fields_) {
  95. CFRelease(fields_);
  96. }
  97. [super dealloc];
  98. }
  99. - (BOOL)isEqual:(id)object {
  100. BOOL equal = NO;
  101. if ([object isKindOfClass:[GPBUnknownFieldSet class]]) {
  102. GPBUnknownFieldSet *set = (GPBUnknownFieldSet *)object;
  103. if ((fields_ == NULL) && (set->fields_ == NULL)) {
  104. equal = YES;
  105. } else if ((fields_ != NULL) && (set->fields_ != NULL)) {
  106. equal = CFEqual(fields_, set->fields_);
  107. }
  108. }
  109. return equal;
  110. }
  111. - (NSUInteger)hash {
  112. // Return the hash of the fields dictionary (or just some value).
  113. if (fields_) {
  114. return CFHash(fields_);
  115. }
  116. return (NSUInteger)[GPBUnknownFieldSet class];
  117. }
  118. #pragma mark - Public Methods
  119. - (BOOL)hasField:(int32_t)number {
  120. ssize_t key = number;
  121. return fields_ ? (CFDictionaryGetValue(fields_, (void *)key) != nil) : NO;
  122. }
  123. - (GPBUnknownField *)getField:(int32_t)number {
  124. ssize_t key = number;
  125. GPBUnknownField *result =
  126. fields_ ? CFDictionaryGetValue(fields_, (void *)key) : nil;
  127. return result;
  128. }
  129. - (NSUInteger)countOfFields {
  130. return fields_ ? CFDictionaryGetCount(fields_) : 0;
  131. }
  132. - (NSArray *)sortedFields {
  133. if (!fields_) return [NSArray array];
  134. size_t count = CFDictionaryGetCount(fields_);
  135. ssize_t keys[count];
  136. GPBUnknownField *values[count];
  137. CFDictionaryGetKeysAndValues(fields_, (const void **)keys,
  138. (const void **)values);
  139. struct GPBFieldPair {
  140. ssize_t key;
  141. GPBUnknownField *value;
  142. } pairs[count];
  143. for (size_t i = 0; i < count; ++i) {
  144. pairs[i].key = keys[i];
  145. pairs[i].value = values[i];
  146. };
  147. qsort_b(pairs, count, sizeof(struct GPBFieldPair),
  148. ^(const void *first, const void *second) {
  149. const struct GPBFieldPair *a = first;
  150. const struct GPBFieldPair *b = second;
  151. return (a->key > b->key) ? 1 : ((a->key == b->key) ? 0 : -1);
  152. });
  153. for (size_t i = 0; i < count; ++i) {
  154. values[i] = pairs[i].value;
  155. };
  156. return [NSArray arrayWithObjects:values count:count];
  157. }
  158. #pragma mark - Internal Methods
  159. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output {
  160. if (!fields_) return;
  161. size_t count = CFDictionaryGetCount(fields_);
  162. ssize_t keys[count];
  163. GPBUnknownField *values[count];
  164. CFDictionaryGetKeysAndValues(fields_, (const void **)keys,
  165. (const void **)values);
  166. if (count > 1) {
  167. struct GPBFieldPair {
  168. ssize_t key;
  169. GPBUnknownField *value;
  170. } pairs[count];
  171. for (size_t i = 0; i < count; ++i) {
  172. pairs[i].key = keys[i];
  173. pairs[i].value = values[i];
  174. };
  175. qsort_b(pairs, count, sizeof(struct GPBFieldPair),
  176. ^(const void *first, const void *second) {
  177. const struct GPBFieldPair *a = first;
  178. const struct GPBFieldPair *b = second;
  179. return (a->key > b->key) ? 1 : ((a->key == b->key) ? 0 : -1);
  180. });
  181. for (size_t i = 0; i < count; ++i) {
  182. GPBUnknownField *value = pairs[i].value;
  183. [value writeToOutput:output];
  184. }
  185. } else {
  186. [values[0] writeToOutput:output];
  187. }
  188. }
  189. - (NSString *)description {
  190. NSMutableString *description = [NSMutableString
  191. stringWithFormat:@"<%@ %p>: TextFormat: {\n", [self class], self];
  192. NSString *textFormat = GPBTextFormatForUnknownFieldSet(self, @" ");
  193. [description appendString:textFormat];
  194. [description appendString:@"}"];
  195. return description;
  196. }
  197. static void GPBUnknownFieldSetSerializedSize(const void *key, const void *value,
  198. void *context) {
  199. #pragma unused(key)
  200. GPBUnknownField *field = value;
  201. size_t *result = context;
  202. *result += [field serializedSize];
  203. }
  204. - (size_t)serializedSize {
  205. size_t result = 0;
  206. if (fields_) {
  207. CFDictionaryApplyFunction(fields_, GPBUnknownFieldSetSerializedSize,
  208. &result);
  209. }
  210. return result;
  211. }
  212. static void GPBUnknownFieldSetWriteAsMessageSetTo(const void *key,
  213. const void *value,
  214. void *context) {
  215. #pragma unused(key)
  216. GPBUnknownField *field = value;
  217. GPBCodedOutputStream *output = context;
  218. [field writeAsMessageSetExtensionToOutput:output];
  219. }
  220. - (void)writeAsMessageSetTo:(GPBCodedOutputStream *)output {
  221. if (fields_) {
  222. CFDictionaryApplyFunction(fields_, GPBUnknownFieldSetWriteAsMessageSetTo,
  223. output);
  224. }
  225. }
  226. static void GPBUnknownFieldSetSerializedSizeAsMessageSet(const void *key,
  227. const void *value,
  228. void *context) {
  229. #pragma unused(key)
  230. GPBUnknownField *field = value;
  231. size_t *result = context;
  232. *result += [field serializedSizeAsMessageSetExtension];
  233. }
  234. - (size_t)serializedSizeAsMessageSet {
  235. size_t result = 0;
  236. if (fields_) {
  237. CFDictionaryApplyFunction(
  238. fields_, GPBUnknownFieldSetSerializedSizeAsMessageSet, &result);
  239. }
  240. return result;
  241. }
  242. - (NSData *)data {
  243. NSMutableData *data = [NSMutableData dataWithLength:self.serializedSize];
  244. GPBCodedOutputStream *output =
  245. [[GPBCodedOutputStream alloc] initWithData:data];
  246. [self writeToCodedOutputStream:output];
  247. [output release];
  248. return data;
  249. }
  250. + (BOOL)isFieldTag:(int32_t)tag {
  251. return GPBWireFormatGetTagWireType(tag) != GPBWireFormatEndGroup;
  252. }
  253. - (void)addField:(GPBUnknownField *)field {
  254. int32_t number = [field number];
  255. checkNumber(number);
  256. if (!fields_) {
  257. CFDictionaryKeyCallBacks keyCallBacks = {
  258. // See description above for reason for using custom dictionary.
  259. 0, GPBUnknownFieldSetKeyRetain, GPBUnknownFieldSetKeyRelease,
  260. GPBUnknownFieldSetCopyKeyDescription, GPBUnknownFieldSetKeyEqual,
  261. GPBUnknownFieldSetKeyHash,
  262. };
  263. fields_ = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &keyCallBacks,
  264. &kCFTypeDictionaryValueCallBacks);
  265. }
  266. ssize_t key = number;
  267. CFDictionarySetValue(fields_, (const void *)key, field);
  268. }
  269. - (GPBUnknownField *)mutableFieldForNumber:(int32_t)number create:(BOOL)create {
  270. ssize_t key = number;
  271. GPBUnknownField *existing =
  272. fields_ ? CFDictionaryGetValue(fields_, (const void *)key) : nil;
  273. if (!existing && create) {
  274. existing = [[GPBUnknownField alloc] initWithNumber:number];
  275. // This retains existing.
  276. [self addField:existing];
  277. [existing release];
  278. }
  279. return existing;
  280. }
  281. static void GPBUnknownFieldSetMergeUnknownFields(const void *key,
  282. const void *value,
  283. void *context) {
  284. #pragma unused(key)
  285. GPBUnknownField *field = value;
  286. GPBUnknownFieldSet *self = context;
  287. int32_t number = [field number];
  288. checkNumber(number);
  289. GPBUnknownField *oldField = [self mutableFieldForNumber:number create:NO];
  290. if (oldField) {
  291. [oldField mergeFromField:field];
  292. } else {
  293. // Merge only comes from GPBMessage's mergeFrom:, so it means we are on
  294. // mutable message and are an mutable instance, so make sure we need
  295. // mutable fields.
  296. GPBUnknownField *fieldCopy = [field copy];
  297. [self addField:fieldCopy];
  298. [fieldCopy release];
  299. }
  300. }
  301. - (void)mergeUnknownFields:(GPBUnknownFieldSet *)other {
  302. if (other && other->fields_) {
  303. CFDictionaryApplyFunction(other->fields_,
  304. GPBUnknownFieldSetMergeUnknownFields, self);
  305. }
  306. }
  307. - (void)mergeFromData:(NSData *)data {
  308. GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
  309. [self mergeFromCodedInputStream:input];
  310. [input checkLastTagWas:0];
  311. [input release];
  312. }
  313. - (void)mergeVarintField:(int32_t)number value:(int32_t)value {
  314. checkNumber(number);
  315. [[self mutableFieldForNumber:number create:YES] addVarint:value];
  316. }
  317. - (BOOL)mergeFieldFrom:(int32_t)tag input:(GPBCodedInputStream *)input {
  318. NSAssert(GPBWireFormatIsValidTag(tag), @"Got passed an invalid tag");
  319. int32_t number = GPBWireFormatGetTagFieldNumber(tag);
  320. GPBCodedInputStreamState *state = &input->state_;
  321. switch (GPBWireFormatGetTagWireType(tag)) {
  322. case GPBWireFormatVarint: {
  323. GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
  324. [field addVarint:GPBCodedInputStreamReadInt64(state)];
  325. return YES;
  326. }
  327. case GPBWireFormatFixed64: {
  328. GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
  329. [field addFixed64:GPBCodedInputStreamReadFixed64(state)];
  330. return YES;
  331. }
  332. case GPBWireFormatLengthDelimited: {
  333. NSData *data = GPBCodedInputStreamReadRetainedBytes(state);
  334. GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
  335. [field addLengthDelimited:data];
  336. [data release];
  337. return YES;
  338. }
  339. case GPBWireFormatStartGroup: {
  340. GPBUnknownFieldSet *unknownFieldSet = [[GPBUnknownFieldSet alloc] init];
  341. [input readUnknownGroup:number message:unknownFieldSet];
  342. GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
  343. [field addGroup:unknownFieldSet];
  344. [unknownFieldSet release];
  345. return YES;
  346. }
  347. case GPBWireFormatEndGroup:
  348. return NO;
  349. case GPBWireFormatFixed32: {
  350. GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
  351. [field addFixed32:GPBCodedInputStreamReadFixed32(state)];
  352. return YES;
  353. }
  354. }
  355. }
  356. - (void)mergeMessageSetMessage:(int32_t)number data:(NSData *)messageData {
  357. [[self mutableFieldForNumber:number create:YES]
  358. addLengthDelimited:messageData];
  359. }
  360. - (void)addUnknownMapEntry:(int32_t)fieldNum value:(NSData *)data {
  361. GPBUnknownField *field = [self mutableFieldForNumber:fieldNum create:YES];
  362. [field addLengthDelimited:data];
  363. }
  364. - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input {
  365. while (YES) {
  366. int32_t tag = GPBCodedInputStreamReadTag(&input->state_);
  367. if (tag == 0 || ![self mergeFieldFrom:tag input:input]) {
  368. break;
  369. }
  370. }
  371. }
  372. - (void)getTags:(int32_t *)tags {
  373. if (!fields_) return;
  374. size_t count = CFDictionaryGetCount(fields_);
  375. ssize_t keys[count];
  376. CFDictionaryGetKeysAndValues(fields_, (const void **)keys, NULL);
  377. for (size_t i = 0; i < count; ++i) {
  378. tags[i] = (int32_t)keys[i];
  379. }
  380. }
  381. #pragma clang diagnostic pop
  382. @end