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