GPBExtensionInternals.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 "GPBExtensionInternals.h"
  31. #import <objc/runtime.h>
  32. #import "GPBCodedInputStream_PackagePrivate.h"
  33. #import "GPBCodedOutputStream_PackagePrivate.h"
  34. #import "GPBDescriptor_PackagePrivate.h"
  35. #import "GPBMessage_PackagePrivate.h"
  36. #import "GPBUtilities_PackagePrivate.h"
  37. static id NewSingleValueFromInputStream(GPBExtensionDescriptor *extension,
  38. GPBCodedInputStream *input,
  39. GPBExtensionRegistry *extensionRegistry,
  40. GPBMessage *existingValue)
  41. __attribute__((ns_returns_retained));
  42. GPB_INLINE size_t DataTypeSize(GPBDataType dataType) {
  43. #pragma clang diagnostic push
  44. #pragma clang diagnostic ignored "-Wswitch-enum"
  45. switch (dataType) {
  46. case GPBDataTypeBool:
  47. return 1;
  48. case GPBDataTypeFixed32:
  49. case GPBDataTypeSFixed32:
  50. case GPBDataTypeFloat:
  51. return 4;
  52. case GPBDataTypeFixed64:
  53. case GPBDataTypeSFixed64:
  54. case GPBDataTypeDouble:
  55. return 8;
  56. default:
  57. return 0;
  58. }
  59. #pragma clang diagnostic pop
  60. }
  61. static size_t ComputePBSerializedSizeNoTagOfObject(GPBDataType dataType, id object) {
  62. #define FIELD_CASE(TYPE, ACCESSOR) \
  63. case GPBDataType##TYPE: \
  64. return GPBCompute##TYPE##SizeNoTag([(NSNumber *)object ACCESSOR]);
  65. #define FIELD_CASE2(TYPE) \
  66. case GPBDataType##TYPE: \
  67. return GPBCompute##TYPE##SizeNoTag(object);
  68. switch (dataType) {
  69. FIELD_CASE(Bool, boolValue)
  70. FIELD_CASE(Float, floatValue)
  71. FIELD_CASE(Double, doubleValue)
  72. FIELD_CASE(Int32, intValue)
  73. FIELD_CASE(SFixed32, intValue)
  74. FIELD_CASE(SInt32, intValue)
  75. FIELD_CASE(Enum, intValue)
  76. FIELD_CASE(Int64, longLongValue)
  77. FIELD_CASE(SInt64, longLongValue)
  78. FIELD_CASE(SFixed64, longLongValue)
  79. FIELD_CASE(UInt32, unsignedIntValue)
  80. FIELD_CASE(Fixed32, unsignedIntValue)
  81. FIELD_CASE(UInt64, unsignedLongLongValue)
  82. FIELD_CASE(Fixed64, unsignedLongLongValue)
  83. FIELD_CASE2(Bytes)
  84. FIELD_CASE2(String)
  85. FIELD_CASE2(Message)
  86. FIELD_CASE2(Group)
  87. }
  88. #undef FIELD_CASE
  89. #undef FIELD_CASE2
  90. }
  91. static size_t ComputeSerializedSizeIncludingTagOfObject(
  92. GPBExtensionDescription *description, id object) {
  93. #define FIELD_CASE(TYPE, ACCESSOR) \
  94. case GPBDataType##TYPE: \
  95. return GPBCompute##TYPE##Size(description->fieldNumber, \
  96. [(NSNumber *)object ACCESSOR]);
  97. #define FIELD_CASE2(TYPE) \
  98. case GPBDataType##TYPE: \
  99. return GPBCompute##TYPE##Size(description->fieldNumber, object);
  100. switch (description->dataType) {
  101. FIELD_CASE(Bool, boolValue)
  102. FIELD_CASE(Float, floatValue)
  103. FIELD_CASE(Double, doubleValue)
  104. FIELD_CASE(Int32, intValue)
  105. FIELD_CASE(SFixed32, intValue)
  106. FIELD_CASE(SInt32, intValue)
  107. FIELD_CASE(Enum, intValue)
  108. FIELD_CASE(Int64, longLongValue)
  109. FIELD_CASE(SInt64, longLongValue)
  110. FIELD_CASE(SFixed64, longLongValue)
  111. FIELD_CASE(UInt32, unsignedIntValue)
  112. FIELD_CASE(Fixed32, unsignedIntValue)
  113. FIELD_CASE(UInt64, unsignedLongLongValue)
  114. FIELD_CASE(Fixed64, unsignedLongLongValue)
  115. FIELD_CASE2(Bytes)
  116. FIELD_CASE2(String)
  117. FIELD_CASE2(Group)
  118. case GPBDataTypeMessage:
  119. if (GPBExtensionIsWireFormat(description)) {
  120. return GPBComputeMessageSetExtensionSize(description->fieldNumber,
  121. object);
  122. } else {
  123. return GPBComputeMessageSize(description->fieldNumber, object);
  124. }
  125. }
  126. #undef FIELD_CASE
  127. #undef FIELD_CASE2
  128. }
  129. static size_t ComputeSerializedSizeIncludingTagOfArray(
  130. GPBExtensionDescription *description, NSArray *values) {
  131. if (GPBExtensionIsPacked(description)) {
  132. size_t size = 0;
  133. size_t typeSize = DataTypeSize(description->dataType);
  134. if (typeSize != 0) {
  135. size = values.count * typeSize;
  136. } else {
  137. for (id value in values) {
  138. size +=
  139. ComputePBSerializedSizeNoTagOfObject(description->dataType, value);
  140. }
  141. }
  142. return size + GPBComputeTagSize(description->fieldNumber) +
  143. GPBComputeRawVarint32SizeForInteger(size);
  144. } else {
  145. size_t size = 0;
  146. for (id value in values) {
  147. size += ComputeSerializedSizeIncludingTagOfObject(description, value);
  148. }
  149. return size;
  150. }
  151. }
  152. static void WriteObjectIncludingTagToCodedOutputStream(
  153. id object, GPBExtensionDescription *description,
  154. GPBCodedOutputStream *output) {
  155. #define FIELD_CASE(TYPE, ACCESSOR) \
  156. case GPBDataType##TYPE: \
  157. [output write##TYPE:description->fieldNumber \
  158. value:[(NSNumber *)object ACCESSOR]]; \
  159. return;
  160. #define FIELD_CASE2(TYPE) \
  161. case GPBDataType##TYPE: \
  162. [output write##TYPE:description->fieldNumber value:object]; \
  163. return;
  164. switch (description->dataType) {
  165. FIELD_CASE(Bool, boolValue)
  166. FIELD_CASE(Float, floatValue)
  167. FIELD_CASE(Double, doubleValue)
  168. FIELD_CASE(Int32, intValue)
  169. FIELD_CASE(SFixed32, intValue)
  170. FIELD_CASE(SInt32, intValue)
  171. FIELD_CASE(Enum, intValue)
  172. FIELD_CASE(Int64, longLongValue)
  173. FIELD_CASE(SInt64, longLongValue)
  174. FIELD_CASE(SFixed64, longLongValue)
  175. FIELD_CASE(UInt32, unsignedIntValue)
  176. FIELD_CASE(Fixed32, unsignedIntValue)
  177. FIELD_CASE(UInt64, unsignedLongLongValue)
  178. FIELD_CASE(Fixed64, unsignedLongLongValue)
  179. FIELD_CASE2(Bytes)
  180. FIELD_CASE2(String)
  181. FIELD_CASE2(Group)
  182. case GPBDataTypeMessage:
  183. if (GPBExtensionIsWireFormat(description)) {
  184. [output writeMessageSetExtension:description->fieldNumber value:object];
  185. } else {
  186. [output writeMessage:description->fieldNumber value:object];
  187. }
  188. return;
  189. }
  190. #undef FIELD_CASE
  191. #undef FIELD_CASE2
  192. }
  193. static void WriteObjectNoTagToCodedOutputStream(
  194. id object, GPBExtensionDescription *description,
  195. GPBCodedOutputStream *output) {
  196. #define FIELD_CASE(TYPE, ACCESSOR) \
  197. case GPBDataType##TYPE: \
  198. [output write##TYPE##NoTag:[(NSNumber *)object ACCESSOR]]; \
  199. return;
  200. #define FIELD_CASE2(TYPE) \
  201. case GPBDataType##TYPE: \
  202. [output write##TYPE##NoTag:object]; \
  203. return;
  204. switch (description->dataType) {
  205. FIELD_CASE(Bool, boolValue)
  206. FIELD_CASE(Float, floatValue)
  207. FIELD_CASE(Double, doubleValue)
  208. FIELD_CASE(Int32, intValue)
  209. FIELD_CASE(SFixed32, intValue)
  210. FIELD_CASE(SInt32, intValue)
  211. FIELD_CASE(Enum, intValue)
  212. FIELD_CASE(Int64, longLongValue)
  213. FIELD_CASE(SInt64, longLongValue)
  214. FIELD_CASE(SFixed64, longLongValue)
  215. FIELD_CASE(UInt32, unsignedIntValue)
  216. FIELD_CASE(Fixed32, unsignedIntValue)
  217. FIELD_CASE(UInt64, unsignedLongLongValue)
  218. FIELD_CASE(Fixed64, unsignedLongLongValue)
  219. FIELD_CASE2(Bytes)
  220. FIELD_CASE2(String)
  221. FIELD_CASE2(Message)
  222. case GPBDataTypeGroup:
  223. [output writeGroupNoTag:description->fieldNumber value:object];
  224. return;
  225. }
  226. #undef FIELD_CASE
  227. #undef FIELD_CASE2
  228. }
  229. static void WriteArrayIncludingTagsToCodedOutputStream(
  230. NSArray *values, GPBExtensionDescription *description,
  231. GPBCodedOutputStream *output) {
  232. if (GPBExtensionIsPacked(description)) {
  233. [output writeTag:description->fieldNumber
  234. format:GPBWireFormatLengthDelimited];
  235. size_t dataSize = 0;
  236. size_t typeSize = DataTypeSize(description->dataType);
  237. if (typeSize != 0) {
  238. dataSize = values.count * typeSize;
  239. } else {
  240. for (id value in values) {
  241. dataSize +=
  242. ComputePBSerializedSizeNoTagOfObject(description->dataType, value);
  243. }
  244. }
  245. [output writeRawVarintSizeTAs32:dataSize];
  246. for (id value in values) {
  247. WriteObjectNoTagToCodedOutputStream(value, description, output);
  248. }
  249. } else {
  250. for (id value in values) {
  251. WriteObjectIncludingTagToCodedOutputStream(value, description, output);
  252. }
  253. }
  254. }
  255. // Direct access is use for speed, to avoid even internally declaring things
  256. // read/write, etc. The warning is enabled in the project to ensure code calling
  257. // protos can turn on -Wdirect-ivar-access without issues.
  258. #pragma clang diagnostic push
  259. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  260. void GPBExtensionMergeFromInputStream(GPBExtensionDescriptor *extension,
  261. BOOL isPackedOnStream,
  262. GPBCodedInputStream *input,
  263. GPBExtensionRegistry *extensionRegistry,
  264. GPBMessage *message) {
  265. GPBExtensionDescription *description = extension->description_;
  266. GPBCodedInputStreamState *state = &input->state_;
  267. if (isPackedOnStream) {
  268. NSCAssert(GPBExtensionIsRepeated(description),
  269. @"How was it packed if it isn't repeated?");
  270. int32_t length = GPBCodedInputStreamReadInt32(state);
  271. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  272. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  273. id value = NewSingleValueFromInputStream(extension,
  274. input,
  275. extensionRegistry,
  276. nil);
  277. [message addExtension:extension value:value];
  278. [value release];
  279. }
  280. GPBCodedInputStreamPopLimit(state, limit);
  281. } else {
  282. id existingValue = nil;
  283. BOOL isRepeated = GPBExtensionIsRepeated(description);
  284. if (!isRepeated && GPBDataTypeIsMessage(description->dataType)) {
  285. existingValue = [message getExistingExtension:extension];
  286. }
  287. id value = NewSingleValueFromInputStream(extension,
  288. input,
  289. extensionRegistry,
  290. existingValue);
  291. if (isRepeated) {
  292. [message addExtension:extension value:value];
  293. } else {
  294. [message setExtension:extension value:value];
  295. }
  296. [value release];
  297. }
  298. }
  299. void GPBWriteExtensionValueToOutputStream(GPBExtensionDescriptor *extension,
  300. id value,
  301. GPBCodedOutputStream *output) {
  302. GPBExtensionDescription *description = extension->description_;
  303. if (GPBExtensionIsRepeated(description)) {
  304. WriteArrayIncludingTagsToCodedOutputStream(value, description, output);
  305. } else {
  306. WriteObjectIncludingTagToCodedOutputStream(value, description, output);
  307. }
  308. }
  309. size_t GPBComputeExtensionSerializedSizeIncludingTag(
  310. GPBExtensionDescriptor *extension, id value) {
  311. GPBExtensionDescription *description = extension->description_;
  312. if (GPBExtensionIsRepeated(description)) {
  313. return ComputeSerializedSizeIncludingTagOfArray(description, value);
  314. } else {
  315. return ComputeSerializedSizeIncludingTagOfObject(description, value);
  316. }
  317. }
  318. // Note that this returns a retained value intentionally.
  319. static id NewSingleValueFromInputStream(GPBExtensionDescriptor *extension,
  320. GPBCodedInputStream *input,
  321. GPBExtensionRegistry *extensionRegistry,
  322. GPBMessage *existingValue) {
  323. GPBExtensionDescription *description = extension->description_;
  324. GPBCodedInputStreamState *state = &input->state_;
  325. switch (description->dataType) {
  326. case GPBDataTypeBool: return [[NSNumber alloc] initWithBool:GPBCodedInputStreamReadBool(state)];
  327. case GPBDataTypeFixed32: return [[NSNumber alloc] initWithUnsignedInt:GPBCodedInputStreamReadFixed32(state)];
  328. case GPBDataTypeSFixed32: return [[NSNumber alloc] initWithInt:GPBCodedInputStreamReadSFixed32(state)];
  329. case GPBDataTypeFloat: return [[NSNumber alloc] initWithFloat:GPBCodedInputStreamReadFloat(state)];
  330. case GPBDataTypeFixed64: return [[NSNumber alloc] initWithUnsignedLongLong:GPBCodedInputStreamReadFixed64(state)];
  331. case GPBDataTypeSFixed64: return [[NSNumber alloc] initWithLongLong:GPBCodedInputStreamReadSFixed64(state)];
  332. case GPBDataTypeDouble: return [[NSNumber alloc] initWithDouble:GPBCodedInputStreamReadDouble(state)];
  333. case GPBDataTypeInt32: return [[NSNumber alloc] initWithInt:GPBCodedInputStreamReadInt32(state)];
  334. case GPBDataTypeInt64: return [[NSNumber alloc] initWithLongLong:GPBCodedInputStreamReadInt64(state)];
  335. case GPBDataTypeSInt32: return [[NSNumber alloc] initWithInt:GPBCodedInputStreamReadSInt32(state)];
  336. case GPBDataTypeSInt64: return [[NSNumber alloc] initWithLongLong:GPBCodedInputStreamReadSInt64(state)];
  337. case GPBDataTypeUInt32: return [[NSNumber alloc] initWithUnsignedInt:GPBCodedInputStreamReadUInt32(state)];
  338. case GPBDataTypeUInt64: return [[NSNumber alloc] initWithUnsignedLongLong:GPBCodedInputStreamReadUInt64(state)];
  339. case GPBDataTypeBytes: return GPBCodedInputStreamReadRetainedBytes(state);
  340. case GPBDataTypeString: return GPBCodedInputStreamReadRetainedString(state);
  341. case GPBDataTypeEnum: return [[NSNumber alloc] initWithInt:GPBCodedInputStreamReadEnum(state)];
  342. case GPBDataTypeGroup:
  343. case GPBDataTypeMessage: {
  344. GPBMessage *message;
  345. if (existingValue) {
  346. message = [existingValue retain];
  347. } else {
  348. GPBDescriptor *decriptor = [extension.msgClass descriptor];
  349. message = [[decriptor.messageClass alloc] init];
  350. }
  351. if (description->dataType == GPBDataTypeGroup) {
  352. [input readGroup:description->fieldNumber
  353. message:message
  354. extensionRegistry:extensionRegistry];
  355. } else {
  356. // description->dataType == GPBDataTypeMessage
  357. if (GPBExtensionIsWireFormat(description)) {
  358. // For MessageSet fields the message length will have already been
  359. // read.
  360. [message mergeFromCodedInputStream:input
  361. extensionRegistry:extensionRegistry];
  362. } else {
  363. [input readMessage:message extensionRegistry:extensionRegistry];
  364. }
  365. }
  366. return message;
  367. }
  368. }
  369. return nil;
  370. }
  371. #pragma clang diagnostic pop