GPBUnknownFieldSet.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file or at
  6. // https://developers.google.com/open-source/licenses/bsd
  7. #import "GPBUnknownFieldSet.h"
  8. #import "GPBUnknownFieldSet_PackagePrivate.h"
  9. #import "GPBCodedInputStream.h"
  10. #import "GPBCodedInputStream_PackagePrivate.h"
  11. #import "GPBCodedOutputStream.h"
  12. #import "GPBUnknownField.h"
  13. #import "GPBUnknownField_PackagePrivate.h"
  14. #import "GPBUtilities.h"
  15. #import "GPBWireFormat.h"
  16. #pragma clang diagnostic push
  17. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  18. #pragma clang diagnostic ignored "-Wdeprecated-implementations"
  19. #pragma mark Helpers
  20. static void checkNumber(int32_t number) {
  21. if (number == 0) {
  22. [NSException raise:NSInvalidArgumentException format:@"Zero is not a valid field number."];
  23. }
  24. }
  25. @implementation GPBUnknownFieldSet {
  26. @package
  27. CFMutableDictionaryRef fields_;
  28. }
  29. static void CopyWorker(__unused const void *key, const void *value, void *context) {
  30. GPBUnknownField *field = value;
  31. GPBUnknownFieldSet *result = context;
  32. GPBUnknownField *copied = [field copy];
  33. [result addField:copied];
  34. [copied release];
  35. }
  36. // Direct access is use for speed, to avoid even internally declaring things
  37. // read/write, etc. The warning is enabled in the project to ensure code calling
  38. // protos can turn on -Wdirect-ivar-access without issues.
  39. #pragma clang diagnostic push
  40. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  41. - (id)copyWithZone:(NSZone *)zone {
  42. GPBUnknownFieldSet *result = [[GPBUnknownFieldSet allocWithZone:zone] init];
  43. if (fields_) {
  44. CFDictionaryApplyFunction(fields_, CopyWorker, result);
  45. }
  46. return result;
  47. }
  48. - (void)dealloc {
  49. if (fields_) {
  50. CFRelease(fields_);
  51. }
  52. [super dealloc];
  53. }
  54. - (BOOL)isEqual:(id)object {
  55. BOOL equal = NO;
  56. if ([object isKindOfClass:[GPBUnknownFieldSet class]]) {
  57. GPBUnknownFieldSet *set = (GPBUnknownFieldSet *)object;
  58. if ((fields_ == NULL) && (set->fields_ == NULL)) {
  59. equal = YES;
  60. } else if ((fields_ != NULL) && (set->fields_ != NULL)) {
  61. equal = CFEqual(fields_, set->fields_);
  62. }
  63. }
  64. return equal;
  65. }
  66. - (NSUInteger)hash {
  67. // Return the hash of the fields dictionary (or just some value).
  68. if (fields_) {
  69. return CFHash(fields_);
  70. }
  71. return (NSUInteger)[GPBUnknownFieldSet class];
  72. }
  73. #pragma mark - Public Methods
  74. - (BOOL)hasField:(int32_t)number {
  75. ssize_t key = number;
  76. return fields_ ? (CFDictionaryGetValue(fields_, (void *)key) != nil) : NO;
  77. }
  78. - (GPBUnknownField *)getField:(int32_t)number {
  79. ssize_t key = number;
  80. GPBUnknownField *result = fields_ ? CFDictionaryGetValue(fields_, (void *)key) : nil;
  81. return result;
  82. }
  83. - (NSUInteger)countOfFields {
  84. return fields_ ? CFDictionaryGetCount(fields_) : 0;
  85. }
  86. - (NSArray *)sortedFields {
  87. if (!fields_) return [NSArray array];
  88. size_t count = CFDictionaryGetCount(fields_);
  89. ssize_t keys[count];
  90. GPBUnknownField *values[count];
  91. CFDictionaryGetKeysAndValues(fields_, (const void **)keys, (const void **)values);
  92. struct GPBFieldPair {
  93. ssize_t key;
  94. GPBUnknownField *value;
  95. } pairs[count];
  96. for (size_t i = 0; i < count; ++i) {
  97. pairs[i].key = keys[i];
  98. pairs[i].value = values[i];
  99. };
  100. qsort_b(pairs, count, sizeof(struct GPBFieldPair), ^(const void *first, const void *second) {
  101. const struct GPBFieldPair *a = first;
  102. const struct GPBFieldPair *b = second;
  103. return (a->key > b->key) ? 1 : ((a->key == b->key) ? 0 : -1);
  104. });
  105. for (size_t i = 0; i < count; ++i) {
  106. values[i] = pairs[i].value;
  107. };
  108. return [NSArray arrayWithObjects:values count:count];
  109. }
  110. #pragma mark - Internal Methods
  111. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output {
  112. if (!fields_) return;
  113. size_t count = CFDictionaryGetCount(fields_);
  114. ssize_t keys[count];
  115. GPBUnknownField *values[count];
  116. CFDictionaryGetKeysAndValues(fields_, (const void **)keys, (const void **)values);
  117. if (count > 1) {
  118. struct GPBFieldPair {
  119. ssize_t key;
  120. GPBUnknownField *value;
  121. } pairs[count];
  122. for (size_t i = 0; i < count; ++i) {
  123. pairs[i].key = keys[i];
  124. pairs[i].value = values[i];
  125. };
  126. qsort_b(pairs, count, sizeof(struct GPBFieldPair), ^(const void *first, const void *second) {
  127. const struct GPBFieldPair *a = first;
  128. const struct GPBFieldPair *b = second;
  129. return (a->key > b->key) ? 1 : ((a->key == b->key) ? 0 : -1);
  130. });
  131. for (size_t i = 0; i < count; ++i) {
  132. GPBUnknownField *value = pairs[i].value;
  133. [value writeToOutput:output];
  134. }
  135. } else {
  136. [values[0] writeToOutput:output];
  137. }
  138. }
  139. - (NSString *)description {
  140. NSMutableString *description =
  141. [NSMutableString stringWithFormat:@"<%@ %p>: TextFormat: {\n", [self class], self];
  142. NSString *textFormat = GPBTextFormatForUnknownFieldSet(self, @" ");
  143. [description appendString:textFormat];
  144. [description appendString:@"}"];
  145. return description;
  146. }
  147. static void GPBUnknownFieldSetSerializedSize(__unused const void *key, const void *value,
  148. void *context) {
  149. GPBUnknownField *field = value;
  150. size_t *result = context;
  151. *result += [field serializedSize];
  152. }
  153. - (size_t)serializedSize {
  154. size_t result = 0;
  155. if (fields_) {
  156. CFDictionaryApplyFunction(fields_, GPBUnknownFieldSetSerializedSize, &result);
  157. }
  158. return result;
  159. }
  160. static void GPBUnknownFieldSetWriteAsMessageSetTo(__unused const void *key, const void *value,
  161. void *context) {
  162. GPBUnknownField *field = value;
  163. GPBCodedOutputStream *output = context;
  164. [field writeAsMessageSetExtensionToOutput:output];
  165. }
  166. - (void)writeAsMessageSetTo:(GPBCodedOutputStream *)output {
  167. if (fields_) {
  168. CFDictionaryApplyFunction(fields_, GPBUnknownFieldSetWriteAsMessageSetTo, output);
  169. }
  170. }
  171. static void GPBUnknownFieldSetSerializedSizeAsMessageSet(__unused const void *key,
  172. const void *value, void *context) {
  173. GPBUnknownField *field = value;
  174. size_t *result = context;
  175. *result += [field serializedSizeAsMessageSetExtension];
  176. }
  177. - (size_t)serializedSizeAsMessageSet {
  178. size_t result = 0;
  179. if (fields_) {
  180. CFDictionaryApplyFunction(fields_, GPBUnknownFieldSetSerializedSizeAsMessageSet, &result);
  181. }
  182. return result;
  183. }
  184. - (NSData *)data {
  185. NSMutableData *data = [NSMutableData dataWithLength:self.serializedSize];
  186. GPBCodedOutputStream *output = [[GPBCodedOutputStream alloc] initWithData:data];
  187. [self writeToCodedOutputStream:output];
  188. [output flush];
  189. [output release];
  190. return data;
  191. }
  192. - (void)addField:(GPBUnknownField *)field {
  193. int32_t number = [field number];
  194. checkNumber(number);
  195. if (!fields_) {
  196. // Use a custom dictionary here because the keys are numbers and conversion
  197. // back and forth from NSNumber isn't worth the cost.
  198. fields_ =
  199. CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, &kCFTypeDictionaryValueCallBacks);
  200. }
  201. ssize_t key = number;
  202. CFDictionarySetValue(fields_, (const void *)key, field);
  203. }
  204. - (GPBUnknownField *)mutableFieldForNumber:(int32_t)number create:(BOOL)create {
  205. ssize_t key = number;
  206. GPBUnknownField *existing = fields_ ? CFDictionaryGetValue(fields_, (const void *)key) : nil;
  207. if (!existing && create) {
  208. existing = [[GPBUnknownField alloc] initWithNumber:number];
  209. // This retains existing.
  210. [self addField:existing];
  211. [existing release];
  212. }
  213. return existing;
  214. }
  215. static void GPBUnknownFieldSetMergeUnknownFields(__unused const void *key, const void *value,
  216. void *context) {
  217. GPBUnknownField *field = value;
  218. GPBUnknownFieldSet *self = context;
  219. int32_t number = [field number];
  220. checkNumber(number);
  221. GPBUnknownField *oldField = [self mutableFieldForNumber:number create:NO];
  222. if (oldField) {
  223. [oldField mergeFromField:field];
  224. } else {
  225. // Merge only comes from GPBMessage's mergeFrom:, so it means we are on
  226. // mutable message and are an mutable instance, so make sure we need
  227. // mutable fields.
  228. GPBUnknownField *fieldCopy = [field copy];
  229. [self addField:fieldCopy];
  230. [fieldCopy release];
  231. }
  232. }
  233. - (void)mergeUnknownFields:(GPBUnknownFieldSet *)other {
  234. if (other && other->fields_) {
  235. CFDictionaryApplyFunction(other->fields_, GPBUnknownFieldSetMergeUnknownFields, self);
  236. }
  237. }
  238. - (void)mergeVarintField:(int32_t)number value:(int32_t)value {
  239. checkNumber(number);
  240. [[self mutableFieldForNumber:number create:YES] addVarint:value];
  241. }
  242. - (void)mergeLengthDelimited:(int32_t)fieldNum value:(NSData *)value {
  243. checkNumber(fieldNum);
  244. [[self mutableFieldForNumber:fieldNum create:YES] addLengthDelimited:value];
  245. }
  246. - (BOOL)mergeFieldFrom:(int32_t)tag input:(GPBCodedInputStream *)input {
  247. NSAssert(GPBWireFormatIsValidTag(tag), @"Got passed an invalid tag");
  248. int32_t number = GPBWireFormatGetTagFieldNumber(tag);
  249. GPBCodedInputStreamState *state = &input->state_;
  250. switch (GPBWireFormatGetTagWireType(tag)) {
  251. case GPBWireFormatVarint: {
  252. GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
  253. [field addVarint:GPBCodedInputStreamReadInt64(state)];
  254. return YES;
  255. }
  256. case GPBWireFormatFixed64: {
  257. GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
  258. [field addFixed64:GPBCodedInputStreamReadFixed64(state)];
  259. return YES;
  260. }
  261. case GPBWireFormatLengthDelimited: {
  262. NSData *data = GPBCodedInputStreamReadRetainedBytes(state);
  263. GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
  264. [field addLengthDelimited:data];
  265. [data release];
  266. return YES;
  267. }
  268. case GPBWireFormatStartGroup: {
  269. GPBUnknownFieldSet *unknownFieldSet = [[GPBUnknownFieldSet alloc] init];
  270. GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
  271. [field addGroup:unknownFieldSet];
  272. // The field will now retain unknownFieldSet, so go ahead and release it in case
  273. // -readUnknownGroup:message: throws so it won't be leaked.
  274. [unknownFieldSet release];
  275. [input readUnknownGroup:number message:unknownFieldSet];
  276. return YES;
  277. }
  278. case GPBWireFormatEndGroup:
  279. return NO;
  280. case GPBWireFormatFixed32: {
  281. GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
  282. [field addFixed32:GPBCodedInputStreamReadFixed32(state)];
  283. return YES;
  284. }
  285. }
  286. }
  287. - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input {
  288. while (YES) {
  289. int32_t tag = GPBCodedInputStreamReadTag(&input->state_);
  290. if (tag == 0 || ![self mergeFieldFrom:tag input:input]) {
  291. break;
  292. }
  293. }
  294. }
  295. - (void)getTags:(int32_t *)tags {
  296. if (!fields_) return;
  297. size_t count = CFDictionaryGetCount(fields_);
  298. ssize_t keys[count];
  299. CFDictionaryGetKeysAndValues(fields_, (const void **)keys, NULL);
  300. for (size_t i = 0; i < count; ++i) {
  301. tags[i] = (int32_t)keys[i];
  302. }
  303. }
  304. #pragma clang diagnostic pop
  305. @end
  306. #pragma clang diagnostic pop // -Wdeprecated-declarations suppression