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