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.

336 lines
11 KiB

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