GPBUnknownFieldSet.m 13 KB

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