You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
11 KiB

6 years ago
6 years ago
  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 "GPBUnknownField_PackagePrivate.h"
  31. #import "GPBArray.h"
  32. #import "GPBCodedOutputStream_PackagePrivate.h"
  33. #import "GPBUnknownFieldSet.h"
  34. @implementation GPBUnknownField {
  35. @protected
  36. int32_t number_;
  37. GPBUInt64Array *mutableVarintList_;
  38. GPBUInt32Array *mutableFixed32List_;
  39. GPBUInt64Array *mutableFixed64List_;
  40. NSMutableArray<NSData*> *mutableLengthDelimitedList_;
  41. NSMutableArray<GPBUnknownFieldSet*> *mutableGroupList_;
  42. }
  43. @synthesize number = number_;
  44. @synthesize varintList = mutableVarintList_;
  45. @synthesize fixed32List = mutableFixed32List_;
  46. @synthesize fixed64List = mutableFixed64List_;
  47. @synthesize lengthDelimitedList = mutableLengthDelimitedList_;
  48. @synthesize groupList = mutableGroupList_;
  49. - (instancetype)initWithNumber:(int32_t)number {
  50. if ((self = [super init])) {
  51. number_ = number;
  52. }
  53. return self;
  54. }
  55. - (void)dealloc {
  56. [mutableVarintList_ release];
  57. [mutableFixed32List_ release];
  58. [mutableFixed64List_ release];
  59. [mutableLengthDelimitedList_ release];
  60. [mutableGroupList_ release];
  61. [super dealloc];
  62. }
  63. // Direct access is use for speed, to avoid even internally declaring things
  64. // read/write, etc. The warning is enabled in the project to ensure code calling
  65. // protos can turn on -Wdirect-ivar-access without issues.
  66. #pragma clang diagnostic push
  67. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  68. - (id)copyWithZone:(NSZone *)zone {
  69. GPBUnknownField *result =
  70. [[GPBUnknownField allocWithZone:zone] initWithNumber:number_];
  71. result->mutableFixed32List_ = [mutableFixed32List_ copyWithZone:zone];
  72. result->mutableFixed64List_ = [mutableFixed64List_ copyWithZone:zone];
  73. result->mutableLengthDelimitedList_ =
  74. [mutableLengthDelimitedList_ mutableCopyWithZone:zone];
  75. result->mutableVarintList_ = [mutableVarintList_ copyWithZone:zone];
  76. if (mutableGroupList_.count) {
  77. result->mutableGroupList_ = [[NSMutableArray allocWithZone:zone]
  78. initWithCapacity:mutableGroupList_.count];
  79. for (GPBUnknownFieldSet *group in mutableGroupList_) {
  80. GPBUnknownFieldSet *copied = [group copyWithZone:zone];
  81. [result->mutableGroupList_ addObject:copied];
  82. [copied release];
  83. }
  84. }
  85. return result;
  86. }
  87. - (BOOL)isEqual:(id)object {
  88. if (self == object) return YES;
  89. if (![object isKindOfClass:[GPBUnknownField class]]) return NO;
  90. GPBUnknownField *field = (GPBUnknownField *)object;
  91. if (number_ != field->number_) return NO;
  92. BOOL equalVarint =
  93. (mutableVarintList_.count == 0 && field->mutableVarintList_.count == 0) ||
  94. [mutableVarintList_ isEqual:field->mutableVarintList_];
  95. if (!equalVarint) return NO;
  96. BOOL equalFixed32 = (mutableFixed32List_.count == 0 &&
  97. field->mutableFixed32List_.count == 0) ||
  98. [mutableFixed32List_ isEqual:field->mutableFixed32List_];
  99. if (!equalFixed32) return NO;
  100. BOOL equalFixed64 = (mutableFixed64List_.count == 0 &&
  101. field->mutableFixed64List_.count == 0) ||
  102. [mutableFixed64List_ isEqual:field->mutableFixed64List_];
  103. if (!equalFixed64) return NO;
  104. BOOL equalLDList =
  105. (mutableLengthDelimitedList_.count == 0 &&
  106. field->mutableLengthDelimitedList_.count == 0) ||
  107. [mutableLengthDelimitedList_ isEqual:field->mutableLengthDelimitedList_];
  108. if (!equalLDList) return NO;
  109. BOOL equalGroupList =
  110. (mutableGroupList_.count == 0 && field->mutableGroupList_.count == 0) ||
  111. [mutableGroupList_ isEqual:field->mutableGroupList_];
  112. if (!equalGroupList) return NO;
  113. return YES;
  114. }
  115. - (NSUInteger)hash {
  116. // Just mix the hashes of the possible sub arrays.
  117. const int prime = 31;
  118. NSUInteger result = prime + [mutableVarintList_ hash];
  119. result = prime * result + [mutableFixed32List_ hash];
  120. result = prime * result + [mutableFixed64List_ hash];
  121. result = prime * result + [mutableLengthDelimitedList_ hash];
  122. result = prime * result + [mutableGroupList_ hash];
  123. return result;
  124. }
  125. - (void)writeToOutput:(GPBCodedOutputStream *)output {
  126. NSUInteger count = mutableVarintList_.count;
  127. if (count > 0) {
  128. [output writeUInt64Array:number_ values:mutableVarintList_ tag:0];
  129. }
  130. count = mutableFixed32List_.count;
  131. if (count > 0) {
  132. [output writeFixed32Array:number_ values:mutableFixed32List_ tag:0];
  133. }
  134. count = mutableFixed64List_.count;
  135. if (count > 0) {
  136. [output writeFixed64Array:number_ values:mutableFixed64List_ tag:0];
  137. }
  138. count = mutableLengthDelimitedList_.count;
  139. if (count > 0) {
  140. [output writeBytesArray:number_ values:mutableLengthDelimitedList_];
  141. }
  142. count = mutableGroupList_.count;
  143. if (count > 0) {
  144. [output writeUnknownGroupArray:number_ values:mutableGroupList_];
  145. }
  146. }
  147. - (size_t)serializedSize {
  148. __block size_t result = 0;
  149. int32_t number = number_;
  150. [mutableVarintList_
  151. enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  152. #pragma unused(idx, stop)
  153. result += GPBComputeUInt64Size(number, value);
  154. }];
  155. [mutableFixed32List_
  156. enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
  157. #pragma unused(idx, stop)
  158. result += GPBComputeFixed32Size(number, value);
  159. }];
  160. [mutableFixed64List_
  161. enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  162. #pragma unused(idx, stop)
  163. result += GPBComputeFixed64Size(number, value);
  164. }];
  165. for (NSData *data in mutableLengthDelimitedList_) {
  166. result += GPBComputeBytesSize(number, data);
  167. }
  168. for (GPBUnknownFieldSet *set in mutableGroupList_) {
  169. result += GPBComputeUnknownGroupSize(number, set);
  170. }
  171. return result;
  172. }
  173. - (void)writeAsMessageSetExtensionToOutput:(GPBCodedOutputStream *)output {
  174. for (NSData *data in mutableLengthDelimitedList_) {
  175. [output writeRawMessageSetExtension:number_ value:data];
  176. }
  177. }
  178. - (size_t)serializedSizeAsMessageSetExtension {
  179. size_t result = 0;
  180. for (NSData *data in mutableLengthDelimitedList_) {
  181. result += GPBComputeRawMessageSetExtensionSize(number_, data);
  182. }
  183. return result;
  184. }
  185. - (NSString *)description {
  186. NSMutableString *description =
  187. [NSMutableString stringWithFormat:@"<%@ %p>: Field: %d {\n",
  188. [self class], self, number_];
  189. [mutableVarintList_
  190. enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  191. #pragma unused(idx, stop)
  192. [description appendFormat:@"\t%llu\n", value];
  193. }];
  194. [mutableFixed32List_
  195. enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
  196. #pragma unused(idx, stop)
  197. [description appendFormat:@"\t%u\n", value];
  198. }];
  199. [mutableFixed64List_
  200. enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  201. #pragma unused(idx, stop)
  202. [description appendFormat:@"\t%llu\n", value];
  203. }];
  204. for (NSData *data in mutableLengthDelimitedList_) {
  205. [description appendFormat:@"\t%@\n", data];
  206. }
  207. for (GPBUnknownFieldSet *set in mutableGroupList_) {
  208. [description appendFormat:@"\t%@\n", set];
  209. }
  210. [description appendString:@"}"];
  211. return description;
  212. }
  213. - (void)mergeFromField:(GPBUnknownField *)other {
  214. GPBUInt64Array *otherVarintList = other.varintList;
  215. if (otherVarintList.count > 0) {
  216. if (mutableVarintList_ == nil) {
  217. mutableVarintList_ = [otherVarintList copy];
  218. } else {
  219. [mutableVarintList_ addValuesFromArray:otherVarintList];
  220. }
  221. }
  222. GPBUInt32Array *otherFixed32List = other.fixed32List;
  223. if (otherFixed32List.count > 0) {
  224. if (mutableFixed32List_ == nil) {
  225. mutableFixed32List_ = [otherFixed32List copy];
  226. } else {
  227. [mutableFixed32List_ addValuesFromArray:otherFixed32List];
  228. }
  229. }
  230. GPBUInt64Array *otherFixed64List = other.fixed64List;
  231. if (otherFixed64List.count > 0) {
  232. if (mutableFixed64List_ == nil) {
  233. mutableFixed64List_ = [otherFixed64List copy];
  234. } else {
  235. [mutableFixed64List_ addValuesFromArray:otherFixed64List];
  236. }
  237. }
  238. NSArray *otherLengthDelimitedList = other.lengthDelimitedList;
  239. if (otherLengthDelimitedList.count > 0) {
  240. if (mutableLengthDelimitedList_ == nil) {
  241. mutableLengthDelimitedList_ = [otherLengthDelimitedList mutableCopy];
  242. } else {
  243. [mutableLengthDelimitedList_
  244. addObjectsFromArray:otherLengthDelimitedList];
  245. }
  246. }
  247. NSArray *otherGroupList = other.groupList;
  248. if (otherGroupList.count > 0) {
  249. if (mutableGroupList_ == nil) {
  250. mutableGroupList_ =
  251. [[NSMutableArray alloc] initWithCapacity:otherGroupList.count];
  252. }
  253. // Make our own mutable copies.
  254. for (GPBUnknownFieldSet *group in otherGroupList) {
  255. GPBUnknownFieldSet *copied = [group copy];
  256. [mutableGroupList_ addObject:copied];
  257. [copied release];
  258. }
  259. }
  260. }
  261. - (void)addVarint:(uint64_t)value {
  262. if (mutableVarintList_ == nil) {
  263. mutableVarintList_ = [[GPBUInt64Array alloc] initWithValues:&value count:1];
  264. } else {
  265. [mutableVarintList_ addValue:value];
  266. }
  267. }
  268. - (void)addFixed32:(uint32_t)value {
  269. if (mutableFixed32List_ == nil) {
  270. mutableFixed32List_ =
  271. [[GPBUInt32Array alloc] initWithValues:&value count:1];
  272. } else {
  273. [mutableFixed32List_ addValue:value];
  274. }
  275. }
  276. - (void)addFixed64:(uint64_t)value {
  277. if (mutableFixed64List_ == nil) {
  278. mutableFixed64List_ =
  279. [[GPBUInt64Array alloc] initWithValues:&value count:1];
  280. } else {
  281. [mutableFixed64List_ addValue:value];
  282. }
  283. }
  284. - (void)addLengthDelimited:(NSData *)value {
  285. if (mutableLengthDelimitedList_ == nil) {
  286. mutableLengthDelimitedList_ =
  287. [[NSMutableArray alloc] initWithObjects:&value count:1];
  288. } else {
  289. [mutableLengthDelimitedList_ addObject:value];
  290. }
  291. }
  292. - (void)addGroup:(GPBUnknownFieldSet *)value {
  293. if (mutableGroupList_ == nil) {
  294. mutableGroupList_ = [[NSMutableArray alloc] initWithObjects:&value count:1];
  295. } else {
  296. [mutableGroupList_ addObject:value];
  297. }
  298. }
  299. #pragma clang diagnostic pop
  300. @end