GPBCodedOutputStream.m 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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 "GPBCodedOutputStream_PackagePrivate.h"
  31. #import <mach/vm_param.h>
  32. #import "GPBArray.h"
  33. #import "GPBUnknownFieldSet_PackagePrivate.h"
  34. #import "GPBUtilities_PackagePrivate.h"
  35. // These values are the existing values so as not to break any code that might
  36. // have already been inspecting them when they weren't documented/exposed.
  37. NSString *const GPBCodedOutputStreamException_OutOfSpace = @"OutOfSpace";
  38. NSString *const GPBCodedOutputStreamException_WriteFailed = @"WriteFailed";
  39. // Structure for containing state of a GPBCodedInputStream. Brought out into
  40. // a struct so that we can inline several common functions instead of dealing
  41. // with overhead of ObjC dispatch.
  42. typedef struct GPBOutputBufferState {
  43. uint8_t *bytes;
  44. size_t size;
  45. size_t position;
  46. NSOutputStream *output;
  47. } GPBOutputBufferState;
  48. @implementation GPBCodedOutputStream {
  49. GPBOutputBufferState state_;
  50. NSMutableData *buffer_;
  51. }
  52. static const int32_t LITTLE_ENDIAN_32_SIZE = sizeof(uint32_t);
  53. static const int32_t LITTLE_ENDIAN_64_SIZE = sizeof(uint64_t);
  54. // Internal helper that writes the current buffer to the output. The
  55. // buffer position is reset to its initial value when this returns.
  56. static void GPBRefreshBuffer(GPBOutputBufferState *state) {
  57. if (state->output == nil) {
  58. // We're writing to a single buffer.
  59. [NSException raise:GPBCodedOutputStreamException_OutOfSpace format:@""];
  60. }
  61. if (state->position != 0) {
  62. NSInteger written =
  63. [state->output write:state->bytes maxLength:state->position];
  64. if (written != (NSInteger)state->position) {
  65. [NSException raise:GPBCodedOutputStreamException_WriteFailed format:@""];
  66. }
  67. state->position = 0;
  68. }
  69. }
  70. static void GPBWriteRawByte(GPBOutputBufferState *state, uint8_t value) {
  71. if (state->position == state->size) {
  72. GPBRefreshBuffer(state);
  73. }
  74. state->bytes[state->position++] = value;
  75. }
  76. static void GPBWriteRawVarint32(GPBOutputBufferState *state, int32_t value) {
  77. while (YES) {
  78. if ((value & ~0x7F) == 0) {
  79. uint8_t val = (uint8_t)value;
  80. GPBWriteRawByte(state, val);
  81. return;
  82. } else {
  83. GPBWriteRawByte(state, (value & 0x7F) | 0x80);
  84. value = GPBLogicalRightShift32(value, 7);
  85. }
  86. }
  87. }
  88. static void GPBWriteRawVarint64(GPBOutputBufferState *state, int64_t value) {
  89. while (YES) {
  90. if ((value & ~0x7FL) == 0) {
  91. uint8_t val = (uint8_t)value;
  92. GPBWriteRawByte(state, val);
  93. return;
  94. } else {
  95. GPBWriteRawByte(state, ((int32_t)value & 0x7F) | 0x80);
  96. value = GPBLogicalRightShift64(value, 7);
  97. }
  98. }
  99. }
  100. static void GPBWriteInt32NoTag(GPBOutputBufferState *state, int32_t value) {
  101. if (value >= 0) {
  102. GPBWriteRawVarint32(state, value);
  103. } else {
  104. // Must sign-extend
  105. GPBWriteRawVarint64(state, value);
  106. }
  107. }
  108. static void GPBWriteUInt32(GPBOutputBufferState *state, int32_t fieldNumber,
  109. uint32_t value) {
  110. GPBWriteTagWithFormat(state, fieldNumber, GPBWireFormatVarint);
  111. GPBWriteRawVarint32(state, value);
  112. }
  113. static void GPBWriteTagWithFormat(GPBOutputBufferState *state,
  114. uint32_t fieldNumber, GPBWireFormat format) {
  115. GPBWriteRawVarint32(state, GPBWireFormatMakeTag(fieldNumber, format));
  116. }
  117. static void GPBWriteRawLittleEndian32(GPBOutputBufferState *state,
  118. int32_t value) {
  119. GPBWriteRawByte(state, (value)&0xFF);
  120. GPBWriteRawByte(state, (value >> 8) & 0xFF);
  121. GPBWriteRawByte(state, (value >> 16) & 0xFF);
  122. GPBWriteRawByte(state, (value >> 24) & 0xFF);
  123. }
  124. static void GPBWriteRawLittleEndian64(GPBOutputBufferState *state,
  125. int64_t value) {
  126. GPBWriteRawByte(state, (int32_t)(value)&0xFF);
  127. GPBWriteRawByte(state, (int32_t)(value >> 8) & 0xFF);
  128. GPBWriteRawByte(state, (int32_t)(value >> 16) & 0xFF);
  129. GPBWriteRawByte(state, (int32_t)(value >> 24) & 0xFF);
  130. GPBWriteRawByte(state, (int32_t)(value >> 32) & 0xFF);
  131. GPBWriteRawByte(state, (int32_t)(value >> 40) & 0xFF);
  132. GPBWriteRawByte(state, (int32_t)(value >> 48) & 0xFF);
  133. GPBWriteRawByte(state, (int32_t)(value >> 56) & 0xFF);
  134. }
  135. - (void)dealloc {
  136. [self flush];
  137. [state_.output close];
  138. [state_.output release];
  139. [buffer_ release];
  140. [super dealloc];
  141. }
  142. - (instancetype)initWithOutputStream:(NSOutputStream *)output {
  143. NSMutableData *data = [NSMutableData dataWithLength:PAGE_SIZE];
  144. return [self initWithOutputStream:output data:data];
  145. }
  146. - (instancetype)initWithData:(NSMutableData *)data {
  147. return [self initWithOutputStream:nil data:data];
  148. }
  149. // This initializer isn't exposed, but it is the designated initializer.
  150. // Setting OutputStream and NSData is to control the buffering behavior/size
  151. // of the work, but that is more obvious via the bufferSize: version.
  152. - (instancetype)initWithOutputStream:(NSOutputStream *)output
  153. data:(NSMutableData *)data {
  154. if ((self = [super init])) {
  155. buffer_ = [data retain];
  156. state_.bytes = [data mutableBytes];
  157. state_.size = [data length];
  158. state_.output = [output retain];
  159. [state_.output open];
  160. }
  161. return self;
  162. }
  163. + (instancetype)streamWithOutputStream:(NSOutputStream *)output {
  164. NSMutableData *data = [NSMutableData dataWithLength:PAGE_SIZE];
  165. return [[[self alloc] initWithOutputStream:output
  166. data:data] autorelease];
  167. }
  168. + (instancetype)streamWithData:(NSMutableData *)data {
  169. return [[[self alloc] initWithData:data] autorelease];
  170. }
  171. // Direct access is use for speed, to avoid even internally declaring things
  172. // read/write, etc. The warning is enabled in the project to ensure code calling
  173. // protos can turn on -Wdirect-ivar-access without issues.
  174. #pragma clang diagnostic push
  175. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  176. - (void)writeDoubleNoTag:(double)value {
  177. GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value));
  178. }
  179. - (void)writeDouble:(int32_t)fieldNumber value:(double)value {
  180. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64);
  181. GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value));
  182. }
  183. - (void)writeFloatNoTag:(float)value {
  184. GPBWriteRawLittleEndian32(&state_, GPBConvertFloatToInt32(value));
  185. }
  186. - (void)writeFloat:(int32_t)fieldNumber value:(float)value {
  187. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed32);
  188. GPBWriteRawLittleEndian32(&state_, GPBConvertFloatToInt32(value));
  189. }
  190. - (void)writeUInt64NoTag:(uint64_t)value {
  191. GPBWriteRawVarint64(&state_, value);
  192. }
  193. - (void)writeUInt64:(int32_t)fieldNumber value:(uint64_t)value {
  194. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  195. GPBWriteRawVarint64(&state_, value);
  196. }
  197. - (void)writeInt64NoTag:(int64_t)value {
  198. GPBWriteRawVarint64(&state_, value);
  199. }
  200. - (void)writeInt64:(int32_t)fieldNumber value:(int64_t)value {
  201. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  202. GPBWriteRawVarint64(&state_, value);
  203. }
  204. - (void)writeInt32NoTag:(int32_t)value {
  205. GPBWriteInt32NoTag(&state_, value);
  206. }
  207. - (void)writeInt32:(int32_t)fieldNumber value:(int32_t)value {
  208. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  209. GPBWriteInt32NoTag(&state_, value);
  210. }
  211. - (void)writeFixed64NoTag:(uint64_t)value {
  212. GPBWriteRawLittleEndian64(&state_, value);
  213. }
  214. - (void)writeFixed64:(int32_t)fieldNumber value:(uint64_t)value {
  215. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64);
  216. GPBWriteRawLittleEndian64(&state_, value);
  217. }
  218. - (void)writeFixed32NoTag:(uint32_t)value {
  219. GPBWriteRawLittleEndian32(&state_, value);
  220. }
  221. - (void)writeFixed32:(int32_t)fieldNumber value:(uint32_t)value {
  222. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed32);
  223. GPBWriteRawLittleEndian32(&state_, value);
  224. }
  225. - (void)writeBoolNoTag:(BOOL)value {
  226. GPBWriteRawByte(&state_, (value ? 1 : 0));
  227. }
  228. - (void)writeBool:(int32_t)fieldNumber value:(BOOL)value {
  229. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  230. GPBWriteRawByte(&state_, (value ? 1 : 0));
  231. }
  232. - (void)writeStringNoTag:(const NSString *)value {
  233. size_t length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  234. GPBWriteRawVarint32(&state_, (int32_t)length);
  235. if (length == 0) {
  236. return;
  237. }
  238. const char *quickString =
  239. CFStringGetCStringPtr((CFStringRef)value, kCFStringEncodingUTF8);
  240. // Fast path: Most strings are short, if the buffer already has space,
  241. // add to it directly.
  242. NSUInteger bufferBytesLeft = state_.size - state_.position;
  243. if (bufferBytesLeft >= length) {
  244. NSUInteger usedBufferLength = 0;
  245. BOOL result;
  246. if (quickString != NULL) {
  247. memcpy(state_.bytes + state_.position, quickString, length);
  248. usedBufferLength = length;
  249. result = YES;
  250. } else {
  251. result = [value getBytes:state_.bytes + state_.position
  252. maxLength:bufferBytesLeft
  253. usedLength:&usedBufferLength
  254. encoding:NSUTF8StringEncoding
  255. options:(NSStringEncodingConversionOptions)0
  256. range:NSMakeRange(0, [value length])
  257. remainingRange:NULL];
  258. }
  259. if (result) {
  260. NSAssert2((usedBufferLength == length),
  261. @"Our UTF8 calc was wrong? %tu vs %zd", usedBufferLength,
  262. length);
  263. state_.position += usedBufferLength;
  264. return;
  265. }
  266. } else if (quickString != NULL) {
  267. [self writeRawPtr:quickString offset:0 length:length];
  268. } else {
  269. // Slow path: just get it as data and write it out.
  270. NSData *utf8Data = [value dataUsingEncoding:NSUTF8StringEncoding];
  271. NSAssert2(([utf8Data length] == length),
  272. @"Strings UTF8 length was wrong? %tu vs %zd", [utf8Data length],
  273. length);
  274. [self writeRawData:utf8Data];
  275. }
  276. }
  277. - (void)writeString:(int32_t)fieldNumber value:(NSString *)value {
  278. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatLengthDelimited);
  279. [self writeStringNoTag:value];
  280. }
  281. - (void)writeGroupNoTag:(int32_t)fieldNumber value:(GPBMessage *)value {
  282. [value writeToCodedOutputStream:self];
  283. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatEndGroup);
  284. }
  285. - (void)writeGroup:(int32_t)fieldNumber value:(GPBMessage *)value {
  286. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatStartGroup);
  287. [self writeGroupNoTag:fieldNumber value:value];
  288. }
  289. - (void)writeUnknownGroupNoTag:(int32_t)fieldNumber
  290. value:(const GPBUnknownFieldSet *)value {
  291. [value writeToCodedOutputStream:self];
  292. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatEndGroup);
  293. }
  294. - (void)writeUnknownGroup:(int32_t)fieldNumber
  295. value:(GPBUnknownFieldSet *)value {
  296. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatStartGroup);
  297. [self writeUnknownGroupNoTag:fieldNumber value:value];
  298. }
  299. - (void)writeMessageNoTag:(GPBMessage *)value {
  300. GPBWriteRawVarint32(&state_, (int32_t)[value serializedSize]);
  301. [value writeToCodedOutputStream:self];
  302. }
  303. - (void)writeMessage:(int32_t)fieldNumber value:(GPBMessage *)value {
  304. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatLengthDelimited);
  305. [self writeMessageNoTag:value];
  306. }
  307. - (void)writeBytesNoTag:(NSData *)value {
  308. GPBWriteRawVarint32(&state_, (int32_t)[value length]);
  309. [self writeRawData:value];
  310. }
  311. - (void)writeBytes:(int32_t)fieldNumber value:(NSData *)value {
  312. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatLengthDelimited);
  313. [self writeBytesNoTag:value];
  314. }
  315. - (void)writeUInt32NoTag:(uint32_t)value {
  316. GPBWriteRawVarint32(&state_, value);
  317. }
  318. - (void)writeUInt32:(int32_t)fieldNumber value:(uint32_t)value {
  319. GPBWriteUInt32(&state_, fieldNumber, value);
  320. }
  321. - (void)writeEnumNoTag:(int32_t)value {
  322. GPBWriteInt32NoTag(&state_, value);
  323. }
  324. - (void)writeEnum:(int32_t)fieldNumber value:(int32_t)value {
  325. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  326. GPBWriteInt32NoTag(&state_, value);
  327. }
  328. - (void)writeSFixed32NoTag:(int32_t)value {
  329. GPBWriteRawLittleEndian32(&state_, value);
  330. }
  331. - (void)writeSFixed32:(int32_t)fieldNumber value:(int32_t)value {
  332. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed32);
  333. GPBWriteRawLittleEndian32(&state_, value);
  334. }
  335. - (void)writeSFixed64NoTag:(int64_t)value {
  336. GPBWriteRawLittleEndian64(&state_, value);
  337. }
  338. - (void)writeSFixed64:(int32_t)fieldNumber value:(int64_t)value {
  339. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64);
  340. GPBWriteRawLittleEndian64(&state_, value);
  341. }
  342. - (void)writeSInt32NoTag:(int32_t)value {
  343. GPBWriteRawVarint32(&state_, GPBEncodeZigZag32(value));
  344. }
  345. - (void)writeSInt32:(int32_t)fieldNumber value:(int32_t)value {
  346. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  347. GPBWriteRawVarint32(&state_, GPBEncodeZigZag32(value));
  348. }
  349. - (void)writeSInt64NoTag:(int64_t)value {
  350. GPBWriteRawVarint64(&state_, GPBEncodeZigZag64(value));
  351. }
  352. - (void)writeSInt64:(int32_t)fieldNumber value:(int64_t)value {
  353. GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
  354. GPBWriteRawVarint64(&state_, GPBEncodeZigZag64(value));
  355. }
  356. //%PDDM-DEFINE WRITE_PACKABLE_DEFNS(NAME, ARRAY_TYPE, TYPE, ACCESSOR_NAME)
  357. //%- (void)write##NAME##Array:(int32_t)fieldNumber
  358. //% NAME$S values:(GPB##ARRAY_TYPE##Array *)values
  359. //% NAME$S tag:(uint32_t)tag {
  360. //% if (tag != 0) {
  361. //% if (values.count == 0) return;
  362. //% __block size_t dataSize = 0;
  363. //% [values enumerate##ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) {
  364. //%#pragma unused(idx, stop)
  365. //% dataSize += GPBCompute##NAME##SizeNoTag(value);
  366. //% }];
  367. //% GPBWriteRawVarint32(&state_, tag);
  368. //% GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  369. //% [values enumerate##ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) {
  370. //%#pragma unused(idx, stop)
  371. //% [self write##NAME##NoTag:value];
  372. //% }];
  373. //% } else {
  374. //% [values enumerate##ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) {
  375. //%#pragma unused(idx, stop)
  376. //% [self write##NAME:fieldNumber value:value];
  377. //% }];
  378. //% }
  379. //%}
  380. //%
  381. //%PDDM-DEFINE WRITE_UNPACKABLE_DEFNS(NAME, TYPE)
  382. //%- (void)write##NAME##Array:(int32_t)fieldNumber values:(NSArray *)values {
  383. //% for (TYPE *value in values) {
  384. //% [self write##NAME:fieldNumber value:value];
  385. //% }
  386. //%}
  387. //%
  388. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Double, Double, double, )
  389. // This block of code is generated, do not edit it directly.
  390. - (void)writeDoubleArray:(int32_t)fieldNumber
  391. values:(GPBDoubleArray *)values
  392. tag:(uint32_t)tag {
  393. if (tag != 0) {
  394. if (values.count == 0) return;
  395. __block size_t dataSize = 0;
  396. [values enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
  397. #pragma unused(idx, stop)
  398. dataSize += GPBComputeDoubleSizeNoTag(value);
  399. }];
  400. GPBWriteRawVarint32(&state_, tag);
  401. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  402. [values enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
  403. #pragma unused(idx, stop)
  404. [self writeDoubleNoTag:value];
  405. }];
  406. } else {
  407. [values enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
  408. #pragma unused(idx, stop)
  409. [self writeDouble:fieldNumber value:value];
  410. }];
  411. }
  412. }
  413. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Float, Float, float, )
  414. // This block of code is generated, do not edit it directly.
  415. - (void)writeFloatArray:(int32_t)fieldNumber
  416. values:(GPBFloatArray *)values
  417. tag:(uint32_t)tag {
  418. if (tag != 0) {
  419. if (values.count == 0) return;
  420. __block size_t dataSize = 0;
  421. [values enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
  422. #pragma unused(idx, stop)
  423. dataSize += GPBComputeFloatSizeNoTag(value);
  424. }];
  425. GPBWriteRawVarint32(&state_, tag);
  426. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  427. [values enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
  428. #pragma unused(idx, stop)
  429. [self writeFloatNoTag:value];
  430. }];
  431. } else {
  432. [values enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
  433. #pragma unused(idx, stop)
  434. [self writeFloat:fieldNumber value:value];
  435. }];
  436. }
  437. }
  438. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(UInt64, UInt64, uint64_t, )
  439. // This block of code is generated, do not edit it directly.
  440. - (void)writeUInt64Array:(int32_t)fieldNumber
  441. values:(GPBUInt64Array *)values
  442. tag:(uint32_t)tag {
  443. if (tag != 0) {
  444. if (values.count == 0) return;
  445. __block size_t dataSize = 0;
  446. [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  447. #pragma unused(idx, stop)
  448. dataSize += GPBComputeUInt64SizeNoTag(value);
  449. }];
  450. GPBWriteRawVarint32(&state_, tag);
  451. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  452. [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  453. #pragma unused(idx, stop)
  454. [self writeUInt64NoTag:value];
  455. }];
  456. } else {
  457. [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  458. #pragma unused(idx, stop)
  459. [self writeUInt64:fieldNumber value:value];
  460. }];
  461. }
  462. }
  463. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Int64, Int64, int64_t, )
  464. // This block of code is generated, do not edit it directly.
  465. - (void)writeInt64Array:(int32_t)fieldNumber
  466. values:(GPBInt64Array *)values
  467. tag:(uint32_t)tag {
  468. if (tag != 0) {
  469. if (values.count == 0) return;
  470. __block size_t dataSize = 0;
  471. [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  472. #pragma unused(idx, stop)
  473. dataSize += GPBComputeInt64SizeNoTag(value);
  474. }];
  475. GPBWriteRawVarint32(&state_, tag);
  476. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  477. [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  478. #pragma unused(idx, stop)
  479. [self writeInt64NoTag:value];
  480. }];
  481. } else {
  482. [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  483. #pragma unused(idx, stop)
  484. [self writeInt64:fieldNumber value:value];
  485. }];
  486. }
  487. }
  488. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Int32, Int32, int32_t, )
  489. // This block of code is generated, do not edit it directly.
  490. - (void)writeInt32Array:(int32_t)fieldNumber
  491. values:(GPBInt32Array *)values
  492. tag:(uint32_t)tag {
  493. if (tag != 0) {
  494. if (values.count == 0) return;
  495. __block size_t dataSize = 0;
  496. [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  497. #pragma unused(idx, stop)
  498. dataSize += GPBComputeInt32SizeNoTag(value);
  499. }];
  500. GPBWriteRawVarint32(&state_, tag);
  501. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  502. [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  503. #pragma unused(idx, stop)
  504. [self writeInt32NoTag:value];
  505. }];
  506. } else {
  507. [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  508. #pragma unused(idx, stop)
  509. [self writeInt32:fieldNumber value:value];
  510. }];
  511. }
  512. }
  513. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(UInt32, UInt32, uint32_t, )
  514. // This block of code is generated, do not edit it directly.
  515. - (void)writeUInt32Array:(int32_t)fieldNumber
  516. values:(GPBUInt32Array *)values
  517. tag:(uint32_t)tag {
  518. if (tag != 0) {
  519. if (values.count == 0) return;
  520. __block size_t dataSize = 0;
  521. [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
  522. #pragma unused(idx, stop)
  523. dataSize += GPBComputeUInt32SizeNoTag(value);
  524. }];
  525. GPBWriteRawVarint32(&state_, tag);
  526. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  527. [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
  528. #pragma unused(idx, stop)
  529. [self writeUInt32NoTag:value];
  530. }];
  531. } else {
  532. [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
  533. #pragma unused(idx, stop)
  534. [self writeUInt32:fieldNumber value:value];
  535. }];
  536. }
  537. }
  538. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Fixed64, UInt64, uint64_t, )
  539. // This block of code is generated, do not edit it directly.
  540. - (void)writeFixed64Array:(int32_t)fieldNumber
  541. values:(GPBUInt64Array *)values
  542. tag:(uint32_t)tag {
  543. if (tag != 0) {
  544. if (values.count == 0) return;
  545. __block size_t dataSize = 0;
  546. [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  547. #pragma unused(idx, stop)
  548. dataSize += GPBComputeFixed64SizeNoTag(value);
  549. }];
  550. GPBWriteRawVarint32(&state_, tag);
  551. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  552. [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  553. #pragma unused(idx, stop)
  554. [self writeFixed64NoTag:value];
  555. }];
  556. } else {
  557. [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
  558. #pragma unused(idx, stop)
  559. [self writeFixed64:fieldNumber value:value];
  560. }];
  561. }
  562. }
  563. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Fixed32, UInt32, uint32_t, )
  564. // This block of code is generated, do not edit it directly.
  565. - (void)writeFixed32Array:(int32_t)fieldNumber
  566. values:(GPBUInt32Array *)values
  567. tag:(uint32_t)tag {
  568. if (tag != 0) {
  569. if (values.count == 0) return;
  570. __block size_t dataSize = 0;
  571. [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
  572. #pragma unused(idx, stop)
  573. dataSize += GPBComputeFixed32SizeNoTag(value);
  574. }];
  575. GPBWriteRawVarint32(&state_, tag);
  576. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  577. [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
  578. #pragma unused(idx, stop)
  579. [self writeFixed32NoTag:value];
  580. }];
  581. } else {
  582. [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
  583. #pragma unused(idx, stop)
  584. [self writeFixed32:fieldNumber value:value];
  585. }];
  586. }
  587. }
  588. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SInt32, Int32, int32_t, )
  589. // This block of code is generated, do not edit it directly.
  590. - (void)writeSInt32Array:(int32_t)fieldNumber
  591. values:(GPBInt32Array *)values
  592. tag:(uint32_t)tag {
  593. if (tag != 0) {
  594. if (values.count == 0) return;
  595. __block size_t dataSize = 0;
  596. [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  597. #pragma unused(idx, stop)
  598. dataSize += GPBComputeSInt32SizeNoTag(value);
  599. }];
  600. GPBWriteRawVarint32(&state_, tag);
  601. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  602. [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  603. #pragma unused(idx, stop)
  604. [self writeSInt32NoTag:value];
  605. }];
  606. } else {
  607. [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  608. #pragma unused(idx, stop)
  609. [self writeSInt32:fieldNumber value:value];
  610. }];
  611. }
  612. }
  613. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SInt64, Int64, int64_t, )
  614. // This block of code is generated, do not edit it directly.
  615. - (void)writeSInt64Array:(int32_t)fieldNumber
  616. values:(GPBInt64Array *)values
  617. tag:(uint32_t)tag {
  618. if (tag != 0) {
  619. if (values.count == 0) return;
  620. __block size_t dataSize = 0;
  621. [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  622. #pragma unused(idx, stop)
  623. dataSize += GPBComputeSInt64SizeNoTag(value);
  624. }];
  625. GPBWriteRawVarint32(&state_, tag);
  626. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  627. [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  628. #pragma unused(idx, stop)
  629. [self writeSInt64NoTag:value];
  630. }];
  631. } else {
  632. [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  633. #pragma unused(idx, stop)
  634. [self writeSInt64:fieldNumber value:value];
  635. }];
  636. }
  637. }
  638. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SFixed64, Int64, int64_t, )
  639. // This block of code is generated, do not edit it directly.
  640. - (void)writeSFixed64Array:(int32_t)fieldNumber
  641. values:(GPBInt64Array *)values
  642. tag:(uint32_t)tag {
  643. if (tag != 0) {
  644. if (values.count == 0) return;
  645. __block size_t dataSize = 0;
  646. [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  647. #pragma unused(idx, stop)
  648. dataSize += GPBComputeSFixed64SizeNoTag(value);
  649. }];
  650. GPBWriteRawVarint32(&state_, tag);
  651. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  652. [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  653. #pragma unused(idx, stop)
  654. [self writeSFixed64NoTag:value];
  655. }];
  656. } else {
  657. [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
  658. #pragma unused(idx, stop)
  659. [self writeSFixed64:fieldNumber value:value];
  660. }];
  661. }
  662. }
  663. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SFixed32, Int32, int32_t, )
  664. // This block of code is generated, do not edit it directly.
  665. - (void)writeSFixed32Array:(int32_t)fieldNumber
  666. values:(GPBInt32Array *)values
  667. tag:(uint32_t)tag {
  668. if (tag != 0) {
  669. if (values.count == 0) return;
  670. __block size_t dataSize = 0;
  671. [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  672. #pragma unused(idx, stop)
  673. dataSize += GPBComputeSFixed32SizeNoTag(value);
  674. }];
  675. GPBWriteRawVarint32(&state_, tag);
  676. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  677. [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  678. #pragma unused(idx, stop)
  679. [self writeSFixed32NoTag:value];
  680. }];
  681. } else {
  682. [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  683. #pragma unused(idx, stop)
  684. [self writeSFixed32:fieldNumber value:value];
  685. }];
  686. }
  687. }
  688. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Bool, Bool, BOOL, )
  689. // This block of code is generated, do not edit it directly.
  690. - (void)writeBoolArray:(int32_t)fieldNumber
  691. values:(GPBBoolArray *)values
  692. tag:(uint32_t)tag {
  693. if (tag != 0) {
  694. if (values.count == 0) return;
  695. __block size_t dataSize = 0;
  696. [values enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
  697. #pragma unused(idx, stop)
  698. dataSize += GPBComputeBoolSizeNoTag(value);
  699. }];
  700. GPBWriteRawVarint32(&state_, tag);
  701. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  702. [values enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
  703. #pragma unused(idx, stop)
  704. [self writeBoolNoTag:value];
  705. }];
  706. } else {
  707. [values enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
  708. #pragma unused(idx, stop)
  709. [self writeBool:fieldNumber value:value];
  710. }];
  711. }
  712. }
  713. //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Enum, Enum, int32_t, Raw)
  714. // This block of code is generated, do not edit it directly.
  715. - (void)writeEnumArray:(int32_t)fieldNumber
  716. values:(GPBEnumArray *)values
  717. tag:(uint32_t)tag {
  718. if (tag != 0) {
  719. if (values.count == 0) return;
  720. __block size_t dataSize = 0;
  721. [values enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  722. #pragma unused(idx, stop)
  723. dataSize += GPBComputeEnumSizeNoTag(value);
  724. }];
  725. GPBWriteRawVarint32(&state_, tag);
  726. GPBWriteRawVarint32(&state_, (int32_t)dataSize);
  727. [values enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  728. #pragma unused(idx, stop)
  729. [self writeEnumNoTag:value];
  730. }];
  731. } else {
  732. [values enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
  733. #pragma unused(idx, stop)
  734. [self writeEnum:fieldNumber value:value];
  735. }];
  736. }
  737. }
  738. //%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(String, NSString)
  739. // This block of code is generated, do not edit it directly.
  740. - (void)writeStringArray:(int32_t)fieldNumber values:(NSArray *)values {
  741. for (NSString *value in values) {
  742. [self writeString:fieldNumber value:value];
  743. }
  744. }
  745. //%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Message, GPBMessage)
  746. // This block of code is generated, do not edit it directly.
  747. - (void)writeMessageArray:(int32_t)fieldNumber values:(NSArray *)values {
  748. for (GPBMessage *value in values) {
  749. [self writeMessage:fieldNumber value:value];
  750. }
  751. }
  752. //%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Bytes, NSData)
  753. // This block of code is generated, do not edit it directly.
  754. - (void)writeBytesArray:(int32_t)fieldNumber values:(NSArray *)values {
  755. for (NSData *value in values) {
  756. [self writeBytes:fieldNumber value:value];
  757. }
  758. }
  759. //%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Group, GPBMessage)
  760. // This block of code is generated, do not edit it directly.
  761. - (void)writeGroupArray:(int32_t)fieldNumber values:(NSArray *)values {
  762. for (GPBMessage *value in values) {
  763. [self writeGroup:fieldNumber value:value];
  764. }
  765. }
  766. //%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(UnknownGroup, GPBUnknownFieldSet)
  767. // This block of code is generated, do not edit it directly.
  768. - (void)writeUnknownGroupArray:(int32_t)fieldNumber values:(NSArray *)values {
  769. for (GPBUnknownFieldSet *value in values) {
  770. [self writeUnknownGroup:fieldNumber value:value];
  771. }
  772. }
  773. //%PDDM-EXPAND-END (19 expansions)
  774. - (void)writeMessageSetExtension:(int32_t)fieldNumber
  775. value:(GPBMessage *)value {
  776. GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
  777. GPBWireFormatStartGroup);
  778. GPBWriteUInt32(&state_, GPBWireFormatMessageSetTypeId, fieldNumber);
  779. [self writeMessage:GPBWireFormatMessageSetMessage value:value];
  780. GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
  781. GPBWireFormatEndGroup);
  782. }
  783. - (void)writeRawMessageSetExtension:(int32_t)fieldNumber value:(NSData *)value {
  784. GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
  785. GPBWireFormatStartGroup);
  786. GPBWriteUInt32(&state_, GPBWireFormatMessageSetTypeId, fieldNumber);
  787. [self writeBytes:GPBWireFormatMessageSetMessage value:value];
  788. GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
  789. GPBWireFormatEndGroup);
  790. }
  791. - (void)flush {
  792. if (state_.output != nil) {
  793. GPBRefreshBuffer(&state_);
  794. }
  795. }
  796. - (void)writeRawByte:(uint8_t)value {
  797. GPBWriteRawByte(&state_, value);
  798. }
  799. - (void)writeRawData:(const NSData *)data {
  800. [self writeRawPtr:[data bytes] offset:0 length:[data length]];
  801. }
  802. - (void)writeRawPtr:(const void *)value
  803. offset:(size_t)offset
  804. length:(size_t)length {
  805. if (value == nil || length == 0) {
  806. return;
  807. }
  808. NSUInteger bufferLength = state_.size;
  809. NSUInteger bufferBytesLeft = bufferLength - state_.position;
  810. if (bufferBytesLeft >= length) {
  811. // We have room in the current buffer.
  812. memcpy(state_.bytes + state_.position, ((uint8_t *)value) + offset, length);
  813. state_.position += length;
  814. } else {
  815. // Write extends past current buffer. Fill the rest of this buffer and
  816. // flush.
  817. size_t bytesWritten = bufferBytesLeft;
  818. memcpy(state_.bytes + state_.position, ((uint8_t *)value) + offset,
  819. bytesWritten);
  820. offset += bytesWritten;
  821. length -= bytesWritten;
  822. state_.position = bufferLength;
  823. GPBRefreshBuffer(&state_);
  824. bufferLength = state_.size;
  825. // Now deal with the rest.
  826. // Since we have an output stream, this is our buffer
  827. // and buffer offset == 0
  828. if (length <= bufferLength) {
  829. // Fits in new buffer.
  830. memcpy(state_.bytes, ((uint8_t *)value) + offset, length);
  831. state_.position = length;
  832. } else {
  833. // Write is very big. Let's do it all at once.
  834. NSInteger written = [state_.output write:((uint8_t *)value) + offset maxLength:length];
  835. if (written != (NSInteger)length) {
  836. [NSException raise:GPBCodedOutputStreamException_WriteFailed format:@""];
  837. }
  838. }
  839. }
  840. }
  841. - (void)writeTag:(uint32_t)fieldNumber format:(GPBWireFormat)format {
  842. GPBWriteTagWithFormat(&state_, fieldNumber, format);
  843. }
  844. - (void)writeRawVarint32:(int32_t)value {
  845. GPBWriteRawVarint32(&state_, value);
  846. }
  847. - (void)writeRawVarintSizeTAs32:(size_t)value {
  848. // Note the truncation.
  849. GPBWriteRawVarint32(&state_, (int32_t)value);
  850. }
  851. - (void)writeRawVarint64:(int64_t)value {
  852. GPBWriteRawVarint64(&state_, value);
  853. }
  854. - (void)writeRawLittleEndian32:(int32_t)value {
  855. GPBWriteRawLittleEndian32(&state_, value);
  856. }
  857. - (void)writeRawLittleEndian64:(int64_t)value {
  858. GPBWriteRawLittleEndian64(&state_, value);
  859. }
  860. #pragma clang diagnostic pop
  861. @end
  862. size_t GPBComputeDoubleSizeNoTag(Float64 value) {
  863. #pragma unused(value)
  864. return LITTLE_ENDIAN_64_SIZE;
  865. }
  866. size_t GPBComputeFloatSizeNoTag(Float32 value) {
  867. #pragma unused(value)
  868. return LITTLE_ENDIAN_32_SIZE;
  869. }
  870. size_t GPBComputeUInt64SizeNoTag(uint64_t value) {
  871. return GPBComputeRawVarint64Size(value);
  872. }
  873. size_t GPBComputeInt64SizeNoTag(int64_t value) {
  874. return GPBComputeRawVarint64Size(value);
  875. }
  876. size_t GPBComputeInt32SizeNoTag(int32_t value) {
  877. if (value >= 0) {
  878. return GPBComputeRawVarint32Size(value);
  879. } else {
  880. // Must sign-extend.
  881. return 10;
  882. }
  883. }
  884. size_t GPBComputeSizeTSizeAsInt32NoTag(size_t value) {
  885. return GPBComputeInt32SizeNoTag((int32_t)value);
  886. }
  887. size_t GPBComputeFixed64SizeNoTag(uint64_t value) {
  888. #pragma unused(value)
  889. return LITTLE_ENDIAN_64_SIZE;
  890. }
  891. size_t GPBComputeFixed32SizeNoTag(uint32_t value) {
  892. #pragma unused(value)
  893. return LITTLE_ENDIAN_32_SIZE;
  894. }
  895. size_t GPBComputeBoolSizeNoTag(BOOL value) {
  896. #pragma unused(value)
  897. return 1;
  898. }
  899. size_t GPBComputeStringSizeNoTag(NSString *value) {
  900. NSUInteger length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  901. return GPBComputeRawVarint32SizeForInteger(length) + length;
  902. }
  903. size_t GPBComputeGroupSizeNoTag(GPBMessage *value) {
  904. return [value serializedSize];
  905. }
  906. size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value) {
  907. return value.serializedSize;
  908. }
  909. size_t GPBComputeMessageSizeNoTag(GPBMessage *value) {
  910. size_t size = [value serializedSize];
  911. return GPBComputeRawVarint32SizeForInteger(size) + size;
  912. }
  913. size_t GPBComputeBytesSizeNoTag(NSData *value) {
  914. NSUInteger valueLength = [value length];
  915. return GPBComputeRawVarint32SizeForInteger(valueLength) + valueLength;
  916. }
  917. size_t GPBComputeUInt32SizeNoTag(int32_t value) {
  918. return GPBComputeRawVarint32Size(value);
  919. }
  920. size_t GPBComputeEnumSizeNoTag(int32_t value) {
  921. return GPBComputeInt32SizeNoTag(value);
  922. }
  923. size_t GPBComputeSFixed32SizeNoTag(int32_t value) {
  924. #pragma unused(value)
  925. return LITTLE_ENDIAN_32_SIZE;
  926. }
  927. size_t GPBComputeSFixed64SizeNoTag(int64_t value) {
  928. #pragma unused(value)
  929. return LITTLE_ENDIAN_64_SIZE;
  930. }
  931. size_t GPBComputeSInt32SizeNoTag(int32_t value) {
  932. return GPBComputeRawVarint32Size(GPBEncodeZigZag32(value));
  933. }
  934. size_t GPBComputeSInt64SizeNoTag(int64_t value) {
  935. return GPBComputeRawVarint64Size(GPBEncodeZigZag64(value));
  936. }
  937. size_t GPBComputeDoubleSize(int32_t fieldNumber, double value) {
  938. return GPBComputeTagSize(fieldNumber) + GPBComputeDoubleSizeNoTag(value);
  939. }
  940. size_t GPBComputeFloatSize(int32_t fieldNumber, float value) {
  941. return GPBComputeTagSize(fieldNumber) + GPBComputeFloatSizeNoTag(value);
  942. }
  943. size_t GPBComputeUInt64Size(int32_t fieldNumber, uint64_t value) {
  944. return GPBComputeTagSize(fieldNumber) + GPBComputeUInt64SizeNoTag(value);
  945. }
  946. size_t GPBComputeInt64Size(int32_t fieldNumber, int64_t value) {
  947. return GPBComputeTagSize(fieldNumber) + GPBComputeInt64SizeNoTag(value);
  948. }
  949. size_t GPBComputeInt32Size(int32_t fieldNumber, int32_t value) {
  950. return GPBComputeTagSize(fieldNumber) + GPBComputeInt32SizeNoTag(value);
  951. }
  952. size_t GPBComputeFixed64Size(int32_t fieldNumber, uint64_t value) {
  953. return GPBComputeTagSize(fieldNumber) + GPBComputeFixed64SizeNoTag(value);
  954. }
  955. size_t GPBComputeFixed32Size(int32_t fieldNumber, uint32_t value) {
  956. return GPBComputeTagSize(fieldNumber) + GPBComputeFixed32SizeNoTag(value);
  957. }
  958. size_t GPBComputeBoolSize(int32_t fieldNumber, BOOL value) {
  959. return GPBComputeTagSize(fieldNumber) + GPBComputeBoolSizeNoTag(value);
  960. }
  961. size_t GPBComputeStringSize(int32_t fieldNumber, NSString *value) {
  962. return GPBComputeTagSize(fieldNumber) + GPBComputeStringSizeNoTag(value);
  963. }
  964. size_t GPBComputeGroupSize(int32_t fieldNumber, GPBMessage *value) {
  965. return GPBComputeTagSize(fieldNumber) * 2 + GPBComputeGroupSizeNoTag(value);
  966. }
  967. size_t GPBComputeUnknownGroupSize(int32_t fieldNumber,
  968. GPBUnknownFieldSet *value) {
  969. return GPBComputeTagSize(fieldNumber) * 2 +
  970. GPBComputeUnknownGroupSizeNoTag(value);
  971. }
  972. size_t GPBComputeMessageSize(int32_t fieldNumber, GPBMessage *value) {
  973. return GPBComputeTagSize(fieldNumber) + GPBComputeMessageSizeNoTag(value);
  974. }
  975. size_t GPBComputeBytesSize(int32_t fieldNumber, NSData *value) {
  976. return GPBComputeTagSize(fieldNumber) + GPBComputeBytesSizeNoTag(value);
  977. }
  978. size_t GPBComputeUInt32Size(int32_t fieldNumber, uint32_t value) {
  979. return GPBComputeTagSize(fieldNumber) + GPBComputeUInt32SizeNoTag(value);
  980. }
  981. size_t GPBComputeEnumSize(int32_t fieldNumber, int32_t value) {
  982. return GPBComputeTagSize(fieldNumber) + GPBComputeEnumSizeNoTag(value);
  983. }
  984. size_t GPBComputeSFixed32Size(int32_t fieldNumber, int32_t value) {
  985. return GPBComputeTagSize(fieldNumber) + GPBComputeSFixed32SizeNoTag(value);
  986. }
  987. size_t GPBComputeSFixed64Size(int32_t fieldNumber, int64_t value) {
  988. return GPBComputeTagSize(fieldNumber) + GPBComputeSFixed64SizeNoTag(value);
  989. }
  990. size_t GPBComputeSInt32Size(int32_t fieldNumber, int32_t value) {
  991. return GPBComputeTagSize(fieldNumber) + GPBComputeSInt32SizeNoTag(value);
  992. }
  993. size_t GPBComputeSInt64Size(int32_t fieldNumber, int64_t value) {
  994. return GPBComputeTagSize(fieldNumber) +
  995. GPBComputeRawVarint64Size(GPBEncodeZigZag64(value));
  996. }
  997. size_t GPBComputeMessageSetExtensionSize(int32_t fieldNumber,
  998. GPBMessage *value) {
  999. return GPBComputeTagSize(GPBWireFormatMessageSetItem) * 2 +
  1000. GPBComputeUInt32Size(GPBWireFormatMessageSetTypeId, fieldNumber) +
  1001. GPBComputeMessageSize(GPBWireFormatMessageSetMessage, value);
  1002. }
  1003. size_t GPBComputeRawMessageSetExtensionSize(int32_t fieldNumber,
  1004. NSData *value) {
  1005. return GPBComputeTagSize(GPBWireFormatMessageSetItem) * 2 +
  1006. GPBComputeUInt32Size(GPBWireFormatMessageSetTypeId, fieldNumber) +
  1007. GPBComputeBytesSize(GPBWireFormatMessageSetMessage, value);
  1008. }
  1009. size_t GPBComputeTagSize(int32_t fieldNumber) {
  1010. return GPBComputeRawVarint32Size(
  1011. GPBWireFormatMakeTag(fieldNumber, GPBWireFormatVarint));
  1012. }
  1013. size_t GPBComputeWireFormatTagSize(int field_number, GPBDataType dataType) {
  1014. size_t result = GPBComputeTagSize(field_number);
  1015. if (dataType == GPBDataTypeGroup) {
  1016. // Groups have both a start and an end tag.
  1017. return result * 2;
  1018. } else {
  1019. return result;
  1020. }
  1021. }
  1022. size_t GPBComputeRawVarint32Size(int32_t value) {
  1023. // value is treated as unsigned, so it won't be sign-extended if negative.
  1024. if ((value & (0xffffffff << 7)) == 0) return 1;
  1025. if ((value & (0xffffffff << 14)) == 0) return 2;
  1026. if ((value & (0xffffffff << 21)) == 0) return 3;
  1027. if ((value & (0xffffffff << 28)) == 0) return 4;
  1028. return 5;
  1029. }
  1030. size_t GPBComputeRawVarint32SizeForInteger(NSInteger value) {
  1031. // Note the truncation.
  1032. return GPBComputeRawVarint32Size((int32_t)value);
  1033. }
  1034. size_t GPBComputeRawVarint64Size(int64_t value) {
  1035. if ((value & (0xffffffffffffffffL << 7)) == 0) return 1;
  1036. if ((value & (0xffffffffffffffffL << 14)) == 0) return 2;
  1037. if ((value & (0xffffffffffffffffL << 21)) == 0) return 3;
  1038. if ((value & (0xffffffffffffffffL << 28)) == 0) return 4;
  1039. if ((value & (0xffffffffffffffffL << 35)) == 0) return 5;
  1040. if ((value & (0xffffffffffffffffL << 42)) == 0) return 6;
  1041. if ((value & (0xffffffffffffffffL << 49)) == 0) return 7;
  1042. if ((value & (0xffffffffffffffffL << 56)) == 0) return 8;
  1043. if ((value & (0xffffffffffffffffL << 63)) == 0) return 9;
  1044. return 10;
  1045. }