GPBUnknownFields.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2024 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 "GPBUnknownFields.h"
  8. #import "GPBUnknownFields_PackagePrivate.h"
  9. #import <Foundation/Foundation.h>
  10. #import "GPBCodedInputStream.h"
  11. #import "GPBCodedInputStream_PackagePrivate.h"
  12. #import "GPBCodedOutputStream.h"
  13. #import "GPBCodedOutputStream_PackagePrivate.h"
  14. #import "GPBDescriptor.h"
  15. #import "GPBMessage.h"
  16. #import "GPBMessage_PackagePrivate.h"
  17. #import "GPBUnknownField.h"
  18. #import "GPBUnknownFieldSet.h"
  19. #import "GPBUnknownFieldSet_PackagePrivate.h"
  20. #import "GPBUnknownField_PackagePrivate.h"
  21. #import "GPBWireFormat.h"
  22. #define CHECK_FIELD_NUMBER(number) \
  23. if (number <= 0) { \
  24. [NSException raise:NSInvalidArgumentException format:@"Not a valid field number."]; \
  25. }
  26. // TODO: Consider using on other functions to reduce bloat when
  27. // some compiler optimizations are enabled.
  28. #define GPB_NOINLINE __attribute__((noinline))
  29. @interface GPBUnknownFields () {
  30. @package
  31. NSMutableArray<GPBUnknownField *> *fields_;
  32. }
  33. @end
  34. // Direct access is use for speed, to avoid even internally declaring things
  35. // read/write, etc. The warning is enabled in the project to ensure code calling
  36. // protos can turn on -Wdirect-ivar-access without issues.
  37. #pragma clang diagnostic push
  38. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  39. GPB_NOINLINE
  40. static size_t ComputeSerializeSize(GPBUnknownFields *_Nonnull self) {
  41. size_t result = 0;
  42. for (GPBUnknownField *field in self->fields_) {
  43. uint32_t fieldNumber = field->number_;
  44. switch (field->type_) {
  45. case GPBUnknownFieldTypeVarint:
  46. result += GPBComputeUInt64Size(fieldNumber, field->storage_.intValue);
  47. break;
  48. case GPBUnknownFieldTypeFixed32:
  49. result += GPBComputeFixed32Size(fieldNumber, (uint32_t)field->storage_.intValue);
  50. break;
  51. case GPBUnknownFieldTypeFixed64:
  52. result += GPBComputeFixed64Size(fieldNumber, field->storage_.intValue);
  53. break;
  54. case GPBUnknownFieldTypeLengthDelimited:
  55. result += GPBComputeBytesSize(fieldNumber, field->storage_.lengthDelimited);
  56. break;
  57. case GPBUnknownFieldTypeGroup:
  58. result +=
  59. (GPBComputeTagSize(fieldNumber) * 2) + ComputeSerializeSize(field->storage_.group);
  60. break;
  61. case GPBUnknownFieldTypeLegacy:
  62. #if defined(DEBUG) && DEBUG
  63. NSCAssert(NO, @"Internal error within the library");
  64. #endif
  65. break;
  66. }
  67. }
  68. return result;
  69. }
  70. GPB_NOINLINE
  71. static void WriteToCoddedOutputStream(GPBUnknownFields *_Nonnull self,
  72. GPBCodedOutputStream *_Nonnull output) {
  73. for (GPBUnknownField *field in self->fields_) {
  74. uint32_t fieldNumber = field->number_;
  75. switch (field->type_) {
  76. case GPBUnknownFieldTypeVarint:
  77. [output writeUInt64:fieldNumber value:field->storage_.intValue];
  78. break;
  79. case GPBUnknownFieldTypeFixed32:
  80. [output writeFixed32:fieldNumber value:(uint32_t)field->storage_.intValue];
  81. break;
  82. case GPBUnknownFieldTypeFixed64:
  83. [output writeFixed64:fieldNumber value:field->storage_.intValue];
  84. break;
  85. case GPBUnknownFieldTypeLengthDelimited:
  86. [output writeBytes:fieldNumber value:field->storage_.lengthDelimited];
  87. break;
  88. case GPBUnknownFieldTypeGroup:
  89. [output writeRawVarint32:GPBWireFormatMakeTag(fieldNumber, GPBWireFormatStartGroup)];
  90. WriteToCoddedOutputStream(field->storage_.group, output);
  91. [output writeRawVarint32:GPBWireFormatMakeTag(fieldNumber, GPBWireFormatEndGroup)];
  92. break;
  93. case GPBUnknownFieldTypeLegacy:
  94. #if defined(DEBUG) && DEBUG
  95. NSCAssert(NO, @"Internal error within the library");
  96. #endif
  97. break;
  98. }
  99. }
  100. }
  101. GPB_NOINLINE
  102. static BOOL MergeFromInputStream(GPBUnknownFields *self, GPBCodedInputStream *input,
  103. uint32_t endTag) {
  104. #if defined(DEBUG) && DEBUG
  105. NSCAssert(endTag == 0 || GPBWireFormatGetTagWireType(endTag) == GPBWireFormatEndGroup,
  106. @"Internal error:Invalid end tag: %u", endTag);
  107. #endif
  108. GPBCodedInputStreamState *state = &input->state_;
  109. NSMutableArray<GPBUnknownField *> *fields = self->fields_;
  110. @try {
  111. while (YES) {
  112. uint32_t tag = GPBCodedInputStreamReadTag(state);
  113. if (tag == endTag) {
  114. return YES;
  115. }
  116. if (tag == 0) {
  117. // Reached end of input without finding the end tag.
  118. return NO;
  119. }
  120. GPBWireFormat wireType = GPBWireFormatGetTagWireType(tag);
  121. int32_t fieldNumber = GPBWireFormatGetTagFieldNumber(tag);
  122. switch (wireType) {
  123. case GPBWireFormatVarint: {
  124. uint64_t value = GPBCodedInputStreamReadInt64(state);
  125. GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber
  126. varint:value];
  127. [fields addObject:field];
  128. [field release];
  129. break;
  130. }
  131. case GPBWireFormatFixed32: {
  132. uint32_t value = GPBCodedInputStreamReadFixed32(state);
  133. GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber
  134. fixed32:value];
  135. [fields addObject:field];
  136. [field release];
  137. break;
  138. }
  139. case GPBWireFormatFixed64: {
  140. uint64_t value = GPBCodedInputStreamReadFixed64(state);
  141. GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber
  142. fixed64:value];
  143. [fields addObject:field];
  144. [field release];
  145. break;
  146. }
  147. case GPBWireFormatLengthDelimited: {
  148. NSData *data = GPBCodedInputStreamReadRetainedBytes(state);
  149. GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber
  150. lengthDelimited:data];
  151. [fields addObject:field];
  152. [field release];
  153. [data release];
  154. break;
  155. }
  156. case GPBWireFormatStartGroup: {
  157. GPBUnknownFields *group = [[GPBUnknownFields alloc] init];
  158. GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber group:group];
  159. [fields addObject:field];
  160. [field release];
  161. [group release]; // Still will be held in the field/fields.
  162. uint32_t endGroupTag = GPBWireFormatMakeTag(fieldNumber, GPBWireFormatEndGroup);
  163. if (MergeFromInputStream(group, input, endGroupTag)) {
  164. GPBCodedInputStreamCheckLastTagWas(state, endGroupTag);
  165. } else {
  166. [NSException
  167. raise:NSInternalInconsistencyException
  168. format:@"Internal error: Unknown field data for nested group was malformed."];
  169. }
  170. break;
  171. }
  172. case GPBWireFormatEndGroup:
  173. [NSException raise:NSInternalInconsistencyException
  174. format:@"Unexpected end group tag: %u", tag];
  175. break;
  176. }
  177. }
  178. } @catch (NSException *exception) {
  179. #if defined(DEBUG) && DEBUG
  180. NSLog(@"%@: Internal exception while parsing unknown data, this shouldn't happen!: %@",
  181. [self class], exception);
  182. #endif
  183. }
  184. }
  185. @implementation GPBUnknownFields
  186. - (instancetype)initFromMessage:(nonnull GPBMessage *)message {
  187. self = [super init];
  188. if (self) {
  189. fields_ = [[NSMutableArray alloc] init];
  190. // This shouldn't happen with the annotations, but just incase something claiming nonnull
  191. // does return nil, block it.
  192. if (!message) {
  193. [self release];
  194. [NSException raise:NSInvalidArgumentException format:@"Message cannot be nil"];
  195. }
  196. NSData *data = GPBMessageUnknownFieldsData(message);
  197. if (data) {
  198. GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
  199. // Parse until the end of the data (tag will be zero).
  200. if (!MergeFromInputStream(self, input, 0)) {
  201. [input release];
  202. [self release];
  203. [NSException raise:NSInternalInconsistencyException
  204. format:@"Internal error: Unknown field data from message was malformed."];
  205. }
  206. [input release];
  207. }
  208. }
  209. return self;
  210. }
  211. - (instancetype)init {
  212. self = [super init];
  213. if (self) {
  214. fields_ = [[NSMutableArray alloc] init];
  215. }
  216. return self;
  217. }
  218. - (id)copyWithZone:(NSZone *)zone {
  219. GPBUnknownFields *copy = [[GPBUnknownFields allocWithZone:zone] init];
  220. copy->fields_ = [[NSMutableArray allocWithZone:zone] initWithArray:fields_ copyItems:YES];
  221. return copy;
  222. }
  223. - (void)dealloc {
  224. [fields_ release];
  225. [super dealloc];
  226. }
  227. - (BOOL)isEqual:(id)object {
  228. if (![object isKindOfClass:[GPBUnknownFields class]]) {
  229. return NO;
  230. }
  231. GPBUnknownFields *ufs = (GPBUnknownFields *)object;
  232. // The type is defined with order of fields mattering, so just compare the arrays.
  233. return [fields_ isEqual:ufs->fields_];
  234. }
  235. - (NSUInteger)hash {
  236. return [fields_ hash];
  237. }
  238. - (NSString *)description {
  239. return [NSString
  240. stringWithFormat:@"<%@ %p>: %lu fields", [self class], self, (unsigned long)fields_.count];
  241. }
  242. #pragma mark - Public Methods
  243. - (NSUInteger)count {
  244. return fields_.count;
  245. }
  246. - (BOOL)empty {
  247. return fields_.count == 0;
  248. }
  249. - (void)clear {
  250. [fields_ removeAllObjects];
  251. }
  252. - (NSArray<GPBUnknownField *> *)fields:(int32_t)fieldNumber {
  253. CHECK_FIELD_NUMBER(fieldNumber);
  254. NSMutableArray<GPBUnknownField *> *result = [[NSMutableArray alloc] init];
  255. for (GPBUnknownField *field in fields_) {
  256. if (field.number == fieldNumber) {
  257. [result addObject:field];
  258. }
  259. }
  260. if (result.count == 0) {
  261. [result release];
  262. return nil;
  263. }
  264. return [result autorelease];
  265. }
  266. - (void)addFieldNumber:(int32_t)fieldNumber varint:(uint64_t)value {
  267. CHECK_FIELD_NUMBER(fieldNumber);
  268. GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber varint:value];
  269. [fields_ addObject:field];
  270. [field release];
  271. }
  272. - (void)addFieldNumber:(int32_t)fieldNumber fixed32:(uint32_t)value {
  273. CHECK_FIELD_NUMBER(fieldNumber);
  274. GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber fixed32:value];
  275. [fields_ addObject:field];
  276. [field release];
  277. }
  278. - (void)addFieldNumber:(int32_t)fieldNumber fixed64:(uint64_t)value {
  279. CHECK_FIELD_NUMBER(fieldNumber);
  280. GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber fixed64:value];
  281. [fields_ addObject:field];
  282. [field release];
  283. }
  284. - (void)addFieldNumber:(int32_t)fieldNumber lengthDelimited:(NSData *)value {
  285. CHECK_FIELD_NUMBER(fieldNumber);
  286. GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber
  287. lengthDelimited:value];
  288. [fields_ addObject:field];
  289. [field release];
  290. }
  291. - (GPBUnknownFields *)addGroupWithFieldNumber:(int32_t)fieldNumber {
  292. CHECK_FIELD_NUMBER(fieldNumber);
  293. GPBUnknownFields *group = [[GPBUnknownFields alloc] init];
  294. GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber group:group];
  295. [fields_ addObject:field];
  296. [field release];
  297. return [group autorelease];
  298. }
  299. - (GPBUnknownField *)addCopyOfField:(nonnull GPBUnknownField *)field {
  300. if (field->type_ == GPBUnknownFieldTypeLegacy) {
  301. [NSException raise:NSInternalInconsistencyException
  302. format:@"GPBUnknownField is the wrong type"];
  303. }
  304. GPBUnknownField *result = [field copy];
  305. [fields_ addObject:result];
  306. return [result autorelease];
  307. }
  308. - (void)removeField:(nonnull GPBUnknownField *)field {
  309. NSUInteger count = fields_.count;
  310. [fields_ removeObjectIdenticalTo:field];
  311. if (count == fields_.count) {
  312. [NSException raise:NSInvalidArgumentException format:@"The field was not present."];
  313. }
  314. }
  315. - (void)clearFieldNumber:(int32_t)fieldNumber {
  316. CHECK_FIELD_NUMBER(fieldNumber);
  317. NSMutableIndexSet *toRemove = nil;
  318. NSUInteger idx = 0;
  319. for (GPBUnknownField *field in fields_) {
  320. if (field->number_ == fieldNumber) {
  321. if (toRemove == nil) {
  322. toRemove = [[NSMutableIndexSet alloc] initWithIndex:idx];
  323. } else {
  324. [toRemove addIndex:idx];
  325. }
  326. }
  327. ++idx;
  328. }
  329. if (toRemove) {
  330. [fields_ removeObjectsAtIndexes:toRemove];
  331. [toRemove release];
  332. }
  333. }
  334. #pragma mark - NSFastEnumeration protocol
  335. - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
  336. objects:(__unsafe_unretained id _Nonnull *)stackbuf
  337. count:(NSUInteger)len {
  338. return [fields_ countByEnumeratingWithState:state objects:stackbuf count:len];
  339. }
  340. #pragma mark - Internal Methods
  341. - (NSData *)serializeAsData {
  342. if (fields_.count == 0) {
  343. return [NSData data];
  344. }
  345. size_t expectedSize = ComputeSerializeSize(self);
  346. NSMutableData *data = [NSMutableData dataWithLength:expectedSize];
  347. GPBCodedOutputStream *stream = [[GPBCodedOutputStream alloc] initWithData:data];
  348. @try {
  349. WriteToCoddedOutputStream(self, stream);
  350. [stream flush];
  351. } @catch (NSException *exception) {
  352. #if defined(DEBUG) && DEBUG
  353. NSLog(@"Internal exception while building GPBUnknownFields serialized data: %@", exception);
  354. #endif
  355. }
  356. #if defined(DEBUG) && DEBUG
  357. NSAssert([stream bytesWritten] == expectedSize, @"Internal error within the library");
  358. #endif
  359. [stream release];
  360. return data;
  361. }
  362. @end
  363. @implementation GPBUnknownFields (AccessHelpers)
  364. - (BOOL)getFirst:(int32_t)fieldNumber varint:(nonnull uint64_t *)outValue {
  365. CHECK_FIELD_NUMBER(fieldNumber);
  366. for (GPBUnknownField *field in fields_) {
  367. if (field.number == fieldNumber && field.type == GPBUnknownFieldTypeVarint) {
  368. *outValue = field.varint;
  369. return YES;
  370. }
  371. }
  372. return NO;
  373. }
  374. - (BOOL)getFirst:(int32_t)fieldNumber fixed32:(nonnull uint32_t *)outValue {
  375. CHECK_FIELD_NUMBER(fieldNumber);
  376. for (GPBUnknownField *field in fields_) {
  377. if (field.number == fieldNumber && field.type == GPBUnknownFieldTypeFixed32) {
  378. *outValue = field.fixed32;
  379. return YES;
  380. }
  381. }
  382. return NO;
  383. }
  384. - (BOOL)getFirst:(int32_t)fieldNumber fixed64:(nonnull uint64_t *)outValue {
  385. CHECK_FIELD_NUMBER(fieldNumber);
  386. for (GPBUnknownField *field in fields_) {
  387. if (field.number == fieldNumber && field.type == GPBUnknownFieldTypeFixed64) {
  388. *outValue = field.fixed64;
  389. return YES;
  390. }
  391. }
  392. return NO;
  393. }
  394. - (nullable NSData *)firstLengthDelimited:(int32_t)fieldNumber {
  395. CHECK_FIELD_NUMBER(fieldNumber);
  396. for (GPBUnknownField *field in fields_) {
  397. if (field.number == fieldNumber && field.type == GPBUnknownFieldTypeLengthDelimited) {
  398. return field.lengthDelimited;
  399. }
  400. }
  401. return nil;
  402. }
  403. - (nullable GPBUnknownFields *)firstGroup:(int32_t)fieldNumber {
  404. CHECK_FIELD_NUMBER(fieldNumber);
  405. for (GPBUnknownField *field in fields_) {
  406. if (field.number == fieldNumber && field.type == GPBUnknownFieldTypeGroup) {
  407. return field.group;
  408. }
  409. }
  410. return nil;
  411. }
  412. @end
  413. #pragma clang diagnostic pop