GPBUnknownFieldSet.m 11 KB

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