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