1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #import "GPBCodedOutputStream_PackagePrivate.h"
33 #import <mach/vm_param.h>
36 #import "GPBUnknownFieldSet_PackagePrivate.h"
37 #import "GPBUtilities_PackagePrivate.h"
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.
41 NSString *const GPBCodedOutputStreamException_OutOfSpace = @"OutOfSpace";
42 NSString *const GPBCodedOutputStreamException_WriteFailed = @"WriteFailed";
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.
47 typedef struct GPBOutputBufferState {
51 NSOutputStream *output;
52 } GPBOutputBufferState;
54 @implementation GPBCodedOutputStream {
55 GPBOutputBufferState state_;
56 NSMutableData *buffer_;
59 static const int32_t LITTLE_ENDIAN_32_SIZE = sizeof(uint32_t);
60 static const int32_t LITTLE_ENDIAN_64_SIZE = sizeof(uint64_t);
62 // Internal helper that writes the current buffer to the output. The
63 // buffer position is reset to its initial value when this returns.
64 static void GPBRefreshBuffer(GPBOutputBufferState *state) {
65 if (state->output == nil) {
66 // We're writing to a single buffer.
67 [NSException raise:GPBCodedOutputStreamException_OutOfSpace format:@""];
69 if (state->position != 0) {
71 [state->output write:state->bytes maxLength:state->position];
72 if (written != (NSInteger)state->position) {
73 [NSException raise:GPBCodedOutputStreamException_WriteFailed format:@""];
79 static void GPBWriteRawByte(GPBOutputBufferState *state, uint8_t value) {
80 if (state->position == state->size) {
81 GPBRefreshBuffer(state);
83 state->bytes[state->position++] = value;
86 static void GPBWriteRawVarint32(GPBOutputBufferState *state, int32_t value) {
88 if ((value & ~0x7F) == 0) {
89 uint8_t val = (uint8_t)value;
90 GPBWriteRawByte(state, val);
93 GPBWriteRawByte(state, (value & 0x7F) | 0x80);
94 value = GPBLogicalRightShift32(value, 7);
99 static void GPBWriteRawVarint64(GPBOutputBufferState *state, int64_t value) {
101 if ((value & ~0x7FL) == 0) {
102 uint8_t val = (uint8_t)value;
103 GPBWriteRawByte(state, val);
106 GPBWriteRawByte(state, ((int32_t)value & 0x7F) | 0x80);
107 value = GPBLogicalRightShift64(value, 7);
112 static void GPBWriteInt32NoTag(GPBOutputBufferState *state, int32_t value) {
114 GPBWriteRawVarint32(state, value);
117 GPBWriteRawVarint64(state, value);
121 static void GPBWriteUInt32(GPBOutputBufferState *state, int32_t fieldNumber,
123 GPBWriteTagWithFormat(state, fieldNumber, GPBWireFormatVarint);
124 GPBWriteRawVarint32(state, value);
127 static void GPBWriteTagWithFormat(GPBOutputBufferState *state,
128 uint32_t fieldNumber, GPBWireFormat format) {
129 GPBWriteRawVarint32(state, GPBWireFormatMakeTag(fieldNumber, format));
132 static void GPBWriteRawLittleEndian32(GPBOutputBufferState *state,
134 GPBWriteRawByte(state, (value)&0xFF);
135 GPBWriteRawByte(state, (value >> 8) & 0xFF);
136 GPBWriteRawByte(state, (value >> 16) & 0xFF);
137 GPBWriteRawByte(state, (value >> 24) & 0xFF);
140 static void GPBWriteRawLittleEndian64(GPBOutputBufferState *state,
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);
154 [state_.output close];
155 [state_.output release];
161 - (instancetype)initWithOutputStream:(NSOutputStream *)output {
162 NSMutableData *data = [NSMutableData dataWithLength:PAGE_SIZE];
163 return [self initWithOutputStream:output data:data];
166 - (instancetype)initWithData:(NSMutableData *)data {
167 return [self initWithOutputStream:nil data:data];
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];
185 + (instancetype)streamWithOutputStream:(NSOutputStream *)output {
186 NSMutableData *data = [NSMutableData dataWithLength:PAGE_SIZE];
187 return [[[self alloc] initWithOutputStream:output
188 data:data] autorelease];
191 + (instancetype)streamWithData:(NSMutableData *)data {
192 return [[[self alloc] initWithData:data] autorelease];
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"
201 - (void)writeDoubleNoTag:(double)value {
202 GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value));
205 - (void)writeDouble:(int32_t)fieldNumber value:(double)value {
206 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64);
207 GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value));
210 - (void)writeFloatNoTag:(float)value {
211 GPBWriteRawLittleEndian32(&state_, GPBConvertFloatToInt32(value));
214 - (void)writeFloat:(int32_t)fieldNumber value:(float)value {
215 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed32);
216 GPBWriteRawLittleEndian32(&state_, GPBConvertFloatToInt32(value));
219 - (void)writeUInt64NoTag:(uint64_t)value {
220 GPBWriteRawVarint64(&state_, value);
223 - (void)writeUInt64:(int32_t)fieldNumber value:(uint64_t)value {
224 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
225 GPBWriteRawVarint64(&state_, value);
228 - (void)writeInt64NoTag:(int64_t)value {
229 GPBWriteRawVarint64(&state_, value);
232 - (void)writeInt64:(int32_t)fieldNumber value:(int64_t)value {
233 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
234 GPBWriteRawVarint64(&state_, value);
237 - (void)writeInt32NoTag:(int32_t)value {
238 GPBWriteInt32NoTag(&state_, value);
241 - (void)writeInt32:(int32_t)fieldNumber value:(int32_t)value {
242 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
243 GPBWriteInt32NoTag(&state_, value);
246 - (void)writeFixed64NoTag:(uint64_t)value {
247 GPBWriteRawLittleEndian64(&state_, value);
250 - (void)writeFixed64:(int32_t)fieldNumber value:(uint64_t)value {
251 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64);
252 GPBWriteRawLittleEndian64(&state_, value);
255 - (void)writeFixed32NoTag:(uint32_t)value {
256 GPBWriteRawLittleEndian32(&state_, value);
259 - (void)writeFixed32:(int32_t)fieldNumber value:(uint32_t)value {
260 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed32);
261 GPBWriteRawLittleEndian32(&state_, value);
264 - (void)writeBoolNoTag:(BOOL)value {
265 GPBWriteRawByte(&state_, (value ? 1 : 0));
268 - (void)writeBool:(int32_t)fieldNumber value:(BOOL)value {
269 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
270 GPBWriteRawByte(&state_, (value ? 1 : 0));
273 - (void)writeStringNoTag:(const NSString *)value {
274 size_t length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
275 GPBWriteRawVarint32(&state_, (int32_t)length);
280 const char *quickString =
281 CFStringGetCStringPtr((CFStringRef)value, kCFStringEncodingUTF8);
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;
289 if (quickString != NULL) {
290 memcpy(state_.bytes + state_.position, quickString, length);
291 usedBufferLength = length;
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];
303 NSAssert2((usedBufferLength == length),
304 @"Our UTF8 calc was wrong? %tu vs %zd", usedBufferLength,
306 state_.position += usedBufferLength;
309 } else if (quickString != NULL) {
310 [self writeRawPtr:quickString offset:0 length:length];
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],
317 [self writeRawData:utf8Data];
321 - (void)writeString:(int32_t)fieldNumber value:(NSString *)value {
322 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatLengthDelimited);
323 [self writeStringNoTag:value];
326 - (void)writeGroupNoTag:(int32_t)fieldNumber value:(GPBMessage *)value {
327 [value writeToCodedOutputStream:self];
328 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatEndGroup);
331 - (void)writeGroup:(int32_t)fieldNumber value:(GPBMessage *)value {
332 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatStartGroup);
333 [self writeGroupNoTag:fieldNumber value:value];
336 - (void)writeUnknownGroupNoTag:(int32_t)fieldNumber
337 value:(const GPBUnknownFieldSet *)value {
338 [value writeToCodedOutputStream:self];
339 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatEndGroup);
342 - (void)writeUnknownGroup:(int32_t)fieldNumber
343 value:(GPBUnknownFieldSet *)value {
344 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatStartGroup);
345 [self writeUnknownGroupNoTag:fieldNumber value:value];
348 - (void)writeMessageNoTag:(GPBMessage *)value {
349 GPBWriteRawVarint32(&state_, (int32_t)[value serializedSize]);
350 [value writeToCodedOutputStream:self];
353 - (void)writeMessage:(int32_t)fieldNumber value:(GPBMessage *)value {
354 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatLengthDelimited);
355 [self writeMessageNoTag:value];
358 - (void)writeBytesNoTag:(NSData *)value {
359 GPBWriteRawVarint32(&state_, (int32_t)[value length]);
360 [self writeRawData:value];
363 - (void)writeBytes:(int32_t)fieldNumber value:(NSData *)value {
364 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatLengthDelimited);
365 [self writeBytesNoTag:value];
368 - (void)writeUInt32NoTag:(uint32_t)value {
369 GPBWriteRawVarint32(&state_, value);
372 - (void)writeUInt32:(int32_t)fieldNumber value:(uint32_t)value {
373 GPBWriteUInt32(&state_, fieldNumber, value);
376 - (void)writeEnumNoTag:(int32_t)value {
377 GPBWriteRawVarint32(&state_, value);
380 - (void)writeEnum:(int32_t)fieldNumber value:(int32_t)value {
381 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
382 GPBWriteRawVarint32(&state_, value);
385 - (void)writeSFixed32NoTag:(int32_t)value {
386 GPBWriteRawLittleEndian32(&state_, value);
389 - (void)writeSFixed32:(int32_t)fieldNumber value:(int32_t)value {
390 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed32);
391 GPBWriteRawLittleEndian32(&state_, value);
394 - (void)writeSFixed64NoTag:(int64_t)value {
395 GPBWriteRawLittleEndian64(&state_, value);
398 - (void)writeSFixed64:(int32_t)fieldNumber value:(int64_t)value {
399 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64);
400 GPBWriteRawLittleEndian64(&state_, value);
403 - (void)writeSInt32NoTag:(int32_t)value {
404 GPBWriteRawVarint32(&state_, GPBEncodeZigZag32(value));
407 - (void)writeSInt32:(int32_t)fieldNumber value:(int32_t)value {
408 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
409 GPBWriteRawVarint32(&state_, GPBEncodeZigZag32(value));
412 - (void)writeSInt64NoTag:(int64_t)value {
413 GPBWriteRawVarint64(&state_, GPBEncodeZigZag64(value));
416 - (void)writeSInt64:(int32_t)fieldNumber value:(int64_t)value {
417 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
418 GPBWriteRawVarint64(&state_, GPBEncodeZigZag64(value));
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 {
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);
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];
439 //% [values enumerate##ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) {
440 //%#pragma unused(idx, stop)
441 //% [self write##NAME:fieldNumber value:value];
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];
453 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Double, Double, double, )
454 // This block of code is generated, do not edit it directly.
456 - (void)writeDoubleArray:(int32_t)fieldNumber
457 values:(GPBDoubleArray *)values
460 if (values.count == 0) return;
461 __block size_t dataSize = 0;
462 [values enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
463 #pragma unused(idx, stop)
464 dataSize += GPBComputeDoubleSizeNoTag(value);
466 GPBWriteRawVarint32(&state_, tag);
467 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
468 [values enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
469 #pragma unused(idx, stop)
470 [self writeDoubleNoTag:value];
473 [values enumerateValuesWithBlock:^(double value, NSUInteger idx, BOOL *stop) {
474 #pragma unused(idx, stop)
475 [self writeDouble:fieldNumber value:value];
480 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Float, Float, float, )
481 // This block of code is generated, do not edit it directly.
483 - (void)writeFloatArray:(int32_t)fieldNumber
484 values:(GPBFloatArray *)values
487 if (values.count == 0) return;
488 __block size_t dataSize = 0;
489 [values enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
490 #pragma unused(idx, stop)
491 dataSize += GPBComputeFloatSizeNoTag(value);
493 GPBWriteRawVarint32(&state_, tag);
494 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
495 [values enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
496 #pragma unused(idx, stop)
497 [self writeFloatNoTag:value];
500 [values enumerateValuesWithBlock:^(float value, NSUInteger idx, BOOL *stop) {
501 #pragma unused(idx, stop)
502 [self writeFloat:fieldNumber value:value];
507 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(UInt64, UInt64, uint64_t, )
508 // This block of code is generated, do not edit it directly.
510 - (void)writeUInt64Array:(int32_t)fieldNumber
511 values:(GPBUInt64Array *)values
514 if (values.count == 0) return;
515 __block size_t dataSize = 0;
516 [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
517 #pragma unused(idx, stop)
518 dataSize += GPBComputeUInt64SizeNoTag(value);
520 GPBWriteRawVarint32(&state_, tag);
521 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
522 [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
523 #pragma unused(idx, stop)
524 [self writeUInt64NoTag:value];
527 [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
528 #pragma unused(idx, stop)
529 [self writeUInt64:fieldNumber value:value];
534 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Int64, Int64, int64_t, )
535 // This block of code is generated, do not edit it directly.
537 - (void)writeInt64Array:(int32_t)fieldNumber
538 values:(GPBInt64Array *)values
541 if (values.count == 0) return;
542 __block size_t dataSize = 0;
543 [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
544 #pragma unused(idx, stop)
545 dataSize += GPBComputeInt64SizeNoTag(value);
547 GPBWriteRawVarint32(&state_, tag);
548 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
549 [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
550 #pragma unused(idx, stop)
551 [self writeInt64NoTag:value];
554 [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
555 #pragma unused(idx, stop)
556 [self writeInt64:fieldNumber value:value];
561 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Int32, Int32, int32_t, )
562 // This block of code is generated, do not edit it directly.
564 - (void)writeInt32Array:(int32_t)fieldNumber
565 values:(GPBInt32Array *)values
568 if (values.count == 0) return;
569 __block size_t dataSize = 0;
570 [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
571 #pragma unused(idx, stop)
572 dataSize += GPBComputeInt32SizeNoTag(value);
574 GPBWriteRawVarint32(&state_, tag);
575 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
576 [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
577 #pragma unused(idx, stop)
578 [self writeInt32NoTag:value];
581 [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
582 #pragma unused(idx, stop)
583 [self writeInt32:fieldNumber value:value];
588 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(UInt32, UInt32, uint32_t, )
589 // This block of code is generated, do not edit it directly.
591 - (void)writeUInt32Array:(int32_t)fieldNumber
592 values:(GPBUInt32Array *)values
595 if (values.count == 0) return;
596 __block size_t dataSize = 0;
597 [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
598 #pragma unused(idx, stop)
599 dataSize += GPBComputeUInt32SizeNoTag(value);
601 GPBWriteRawVarint32(&state_, tag);
602 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
603 [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
604 #pragma unused(idx, stop)
605 [self writeUInt32NoTag:value];
608 [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
609 #pragma unused(idx, stop)
610 [self writeUInt32:fieldNumber value:value];
615 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Fixed64, UInt64, uint64_t, )
616 // This block of code is generated, do not edit it directly.
618 - (void)writeFixed64Array:(int32_t)fieldNumber
619 values:(GPBUInt64Array *)values
622 if (values.count == 0) return;
623 __block size_t dataSize = 0;
624 [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
625 #pragma unused(idx, stop)
626 dataSize += GPBComputeFixed64SizeNoTag(value);
628 GPBWriteRawVarint32(&state_, tag);
629 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
630 [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
631 #pragma unused(idx, stop)
632 [self writeFixed64NoTag:value];
635 [values enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
636 #pragma unused(idx, stop)
637 [self writeFixed64:fieldNumber value:value];
642 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Fixed32, UInt32, uint32_t, )
643 // This block of code is generated, do not edit it directly.
645 - (void)writeFixed32Array:(int32_t)fieldNumber
646 values:(GPBUInt32Array *)values
649 if (values.count == 0) return;
650 __block size_t dataSize = 0;
651 [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
652 #pragma unused(idx, stop)
653 dataSize += GPBComputeFixed32SizeNoTag(value);
655 GPBWriteRawVarint32(&state_, tag);
656 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
657 [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
658 #pragma unused(idx, stop)
659 [self writeFixed32NoTag:value];
662 [values enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
663 #pragma unused(idx, stop)
664 [self writeFixed32:fieldNumber value:value];
669 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SInt32, Int32, int32_t, )
670 // This block of code is generated, do not edit it directly.
672 - (void)writeSInt32Array:(int32_t)fieldNumber
673 values:(GPBInt32Array *)values
676 if (values.count == 0) return;
677 __block size_t dataSize = 0;
678 [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
679 #pragma unused(idx, stop)
680 dataSize += GPBComputeSInt32SizeNoTag(value);
682 GPBWriteRawVarint32(&state_, tag);
683 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
684 [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
685 #pragma unused(idx, stop)
686 [self writeSInt32NoTag:value];
689 [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
690 #pragma unused(idx, stop)
691 [self writeSInt32:fieldNumber value:value];
696 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SInt64, Int64, int64_t, )
697 // This block of code is generated, do not edit it directly.
699 - (void)writeSInt64Array:(int32_t)fieldNumber
700 values:(GPBInt64Array *)values
703 if (values.count == 0) return;
704 __block size_t dataSize = 0;
705 [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
706 #pragma unused(idx, stop)
707 dataSize += GPBComputeSInt64SizeNoTag(value);
709 GPBWriteRawVarint32(&state_, tag);
710 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
711 [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
712 #pragma unused(idx, stop)
713 [self writeSInt64NoTag:value];
716 [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
717 #pragma unused(idx, stop)
718 [self writeSInt64:fieldNumber value:value];
723 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SFixed64, Int64, int64_t, )
724 // This block of code is generated, do not edit it directly.
726 - (void)writeSFixed64Array:(int32_t)fieldNumber
727 values:(GPBInt64Array *)values
730 if (values.count == 0) return;
731 __block size_t dataSize = 0;
732 [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
733 #pragma unused(idx, stop)
734 dataSize += GPBComputeSFixed64SizeNoTag(value);
736 GPBWriteRawVarint32(&state_, tag);
737 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
738 [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
739 #pragma unused(idx, stop)
740 [self writeSFixed64NoTag:value];
743 [values enumerateValuesWithBlock:^(int64_t value, NSUInteger idx, BOOL *stop) {
744 #pragma unused(idx, stop)
745 [self writeSFixed64:fieldNumber value:value];
750 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(SFixed32, Int32, int32_t, )
751 // This block of code is generated, do not edit it directly.
753 - (void)writeSFixed32Array:(int32_t)fieldNumber
754 values:(GPBInt32Array *)values
757 if (values.count == 0) return;
758 __block size_t dataSize = 0;
759 [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
760 #pragma unused(idx, stop)
761 dataSize += GPBComputeSFixed32SizeNoTag(value);
763 GPBWriteRawVarint32(&state_, tag);
764 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
765 [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
766 #pragma unused(idx, stop)
767 [self writeSFixed32NoTag:value];
770 [values enumerateValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
771 #pragma unused(idx, stop)
772 [self writeSFixed32:fieldNumber value:value];
777 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Bool, Bool, BOOL, )
778 // This block of code is generated, do not edit it directly.
780 - (void)writeBoolArray:(int32_t)fieldNumber
781 values:(GPBBoolArray *)values
784 if (values.count == 0) return;
785 __block size_t dataSize = 0;
786 [values enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
787 #pragma unused(idx, stop)
788 dataSize += GPBComputeBoolSizeNoTag(value);
790 GPBWriteRawVarint32(&state_, tag);
791 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
792 [values enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
793 #pragma unused(idx, stop)
794 [self writeBoolNoTag:value];
797 [values enumerateValuesWithBlock:^(BOOL value, NSUInteger idx, BOOL *stop) {
798 #pragma unused(idx, stop)
799 [self writeBool:fieldNumber value:value];
804 //%PDDM-EXPAND WRITE_PACKABLE_DEFNS(Enum, Enum, int32_t, Raw)
805 // This block of code is generated, do not edit it directly.
807 - (void)writeEnumArray:(int32_t)fieldNumber
808 values:(GPBEnumArray *)values
811 if (values.count == 0) return;
812 __block size_t dataSize = 0;
813 [values enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
814 #pragma unused(idx, stop)
815 dataSize += GPBComputeEnumSizeNoTag(value);
817 GPBWriteRawVarint32(&state_, tag);
818 GPBWriteRawVarint32(&state_, (int32_t)dataSize);
819 [values enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
820 #pragma unused(idx, stop)
821 [self writeEnumNoTag:value];
824 [values enumerateRawValuesWithBlock:^(int32_t value, NSUInteger idx, BOOL *stop) {
825 #pragma unused(idx, stop)
826 [self writeEnum:fieldNumber value:value];
831 //%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(String, NSString)
832 // This block of code is generated, do not edit it directly.
834 - (void)writeStringArray:(int32_t)fieldNumber values:(NSArray *)values {
835 for (NSString *value in values) {
836 [self writeString:fieldNumber value:value];
840 //%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Message, GPBMessage)
841 // This block of code is generated, do not edit it directly.
843 - (void)writeMessageArray:(int32_t)fieldNumber values:(NSArray *)values {
844 for (GPBMessage *value in values) {
845 [self writeMessage:fieldNumber value:value];
849 //%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Bytes, NSData)
850 // This block of code is generated, do not edit it directly.
852 - (void)writeBytesArray:(int32_t)fieldNumber values:(NSArray *)values {
853 for (NSData *value in values) {
854 [self writeBytes:fieldNumber value:value];
858 //%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(Group, GPBMessage)
859 // This block of code is generated, do not edit it directly.
861 - (void)writeGroupArray:(int32_t)fieldNumber values:(NSArray *)values {
862 for (GPBMessage *value in values) {
863 [self writeGroup:fieldNumber value:value];
867 //%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(UnknownGroup, GPBUnknownFieldSet)
868 // This block of code is generated, do not edit it directly.
870 - (void)writeUnknownGroupArray:(int32_t)fieldNumber values:(NSArray *)values {
871 for (GPBUnknownFieldSet *value in values) {
872 [self writeUnknownGroup:fieldNumber value:value];
876 //%PDDM-EXPAND-END (19 expansions)
878 - (void)writeMessageSetExtension:(int32_t)fieldNumber
879 value:(GPBMessage *)value {
880 GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
881 GPBWireFormatStartGroup);
882 GPBWriteUInt32(&state_, GPBWireFormatMessageSetTypeId, fieldNumber);
883 [self writeMessage:GPBWireFormatMessageSetMessage value:value];
884 GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
885 GPBWireFormatEndGroup);
888 - (void)writeRawMessageSetExtension:(int32_t)fieldNumber value:(NSData *)value {
889 GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
890 GPBWireFormatStartGroup);
891 GPBWriteUInt32(&state_, GPBWireFormatMessageSetTypeId, fieldNumber);
892 [self writeBytes:GPBWireFormatMessageSetMessage value:value];
893 GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem,
894 GPBWireFormatEndGroup);
898 if (state_.output != nil) {
899 GPBRefreshBuffer(&state_);
903 - (void)writeRawByte:(uint8_t)value {
904 GPBWriteRawByte(&state_, value);
907 - (void)writeRawData:(const NSData *)data {
908 [self writeRawPtr:[data bytes] offset:0 length:[data length]];
911 - (void)writeRawPtr:(const void *)value
912 offset:(size_t)offset
913 length:(size_t)length {
914 if (value == nil || length == 0) {
918 NSUInteger bufferLength = state_.size;
919 NSUInteger bufferBytesLeft = bufferLength - state_.position;
920 if (bufferBytesLeft >= length) {
921 // We have room in the current buffer.
922 memcpy(state_.bytes + state_.position, ((uint8_t *)value) + offset, length);
923 state_.position += length;
925 // Write extends past current buffer. Fill the rest of this buffer and
927 size_t bytesWritten = bufferBytesLeft;
928 memcpy(state_.bytes + state_.position, ((uint8_t *)value) + offset,
930 offset += bytesWritten;
931 length -= bytesWritten;
932 state_.position = bufferLength;
933 GPBRefreshBuffer(&state_);
934 bufferLength = state_.size;
936 // Now deal with the rest.
937 // Since we have an output stream, this is our buffer
938 // and buffer offset == 0
939 if (length <= bufferLength) {
940 // Fits in new buffer.
941 memcpy(state_.bytes, ((uint8_t *)value) + offset, length);
942 state_.position = length;
944 // Write is very big. Let's do it all at once.
945 NSInteger written = [state_.output write:((uint8_t *)value) + offset maxLength:length];
946 if (written != (NSInteger)length) {
947 [NSException raise:GPBCodedOutputStreamException_WriteFailed format:@""];
953 - (void)writeTag:(uint32_t)fieldNumber format:(GPBWireFormat)format {
954 GPBWriteTagWithFormat(&state_, fieldNumber, format);
957 - (void)writeRawVarint32:(int32_t)value {
958 GPBWriteRawVarint32(&state_, value);
961 - (void)writeRawVarintSizeTAs32:(size_t)value {
962 // Note the truncation.
963 GPBWriteRawVarint32(&state_, (int32_t)value);
966 - (void)writeRawVarint64:(int64_t)value {
967 GPBWriteRawVarint64(&state_, value);
970 - (void)writeRawLittleEndian32:(int32_t)value {
971 GPBWriteRawLittleEndian32(&state_, value);
974 - (void)writeRawLittleEndian64:(int64_t)value {
975 GPBWriteRawLittleEndian64(&state_, value);
978 #pragma clang diagnostic pop
982 size_t GPBComputeDoubleSizeNoTag(Float64 value) {
983 #pragma unused(value)
984 return LITTLE_ENDIAN_64_SIZE;
987 size_t GPBComputeFloatSizeNoTag(Float32 value) {
988 #pragma unused(value)
989 return LITTLE_ENDIAN_32_SIZE;
992 size_t GPBComputeUInt64SizeNoTag(uint64_t value) {
993 return GPBComputeRawVarint64Size(value);
996 size_t GPBComputeInt64SizeNoTag(int64_t value) {
997 return GPBComputeRawVarint64Size(value);
1000 size_t GPBComputeInt32SizeNoTag(int32_t value) {
1002 return GPBComputeRawVarint32Size(value);
1004 // Must sign-extend.
1009 size_t GPBComputeSizeTSizeAsInt32NoTag(size_t value) {
1010 return GPBComputeInt32SizeNoTag((int32_t)value);
1013 size_t GPBComputeFixed64SizeNoTag(uint64_t value) {
1014 #pragma unused(value)
1015 return LITTLE_ENDIAN_64_SIZE;
1018 size_t GPBComputeFixed32SizeNoTag(uint32_t value) {
1019 #pragma unused(value)
1020 return LITTLE_ENDIAN_32_SIZE;
1023 size_t GPBComputeBoolSizeNoTag(BOOL value) {
1024 #pragma unused(value)
1028 size_t GPBComputeStringSizeNoTag(NSString *value) {
1029 NSUInteger length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
1030 return GPBComputeRawVarint32SizeForInteger(length) + length;
1033 size_t GPBComputeGroupSizeNoTag(GPBMessage *value) {
1034 return [value serializedSize];
1037 size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value) {
1038 return value.serializedSize;
1041 size_t GPBComputeMessageSizeNoTag(GPBMessage *value) {
1042 size_t size = [value serializedSize];
1043 return GPBComputeRawVarint32SizeForInteger(size) + size;
1046 size_t GPBComputeBytesSizeNoTag(NSData *value) {
1047 NSUInteger valueLength = [value length];
1048 return GPBComputeRawVarint32SizeForInteger(valueLength) + valueLength;
1051 size_t GPBComputeUInt32SizeNoTag(int32_t value) {
1052 return GPBComputeRawVarint32Size(value);
1055 size_t GPBComputeEnumSizeNoTag(int32_t value) {
1056 return GPBComputeRawVarint32Size(value);
1059 size_t GPBComputeSFixed32SizeNoTag(int32_t value) {
1060 #pragma unused(value)
1061 return LITTLE_ENDIAN_32_SIZE;
1064 size_t GPBComputeSFixed64SizeNoTag(int64_t value) {
1065 #pragma unused(value)
1066 return LITTLE_ENDIAN_64_SIZE;
1069 size_t GPBComputeSInt32SizeNoTag(int32_t value) {
1070 return GPBComputeRawVarint32Size(GPBEncodeZigZag32(value));
1073 size_t GPBComputeSInt64SizeNoTag(int64_t value) {
1074 return GPBComputeRawVarint64Size(GPBEncodeZigZag64(value));
1077 size_t GPBComputeDoubleSize(int32_t fieldNumber, double value) {
1078 return GPBComputeTagSize(fieldNumber) + GPBComputeDoubleSizeNoTag(value);
1081 size_t GPBComputeFloatSize(int32_t fieldNumber, float value) {
1082 return GPBComputeTagSize(fieldNumber) + GPBComputeFloatSizeNoTag(value);
1085 size_t GPBComputeUInt64Size(int32_t fieldNumber, uint64_t value) {
1086 return GPBComputeTagSize(fieldNumber) + GPBComputeUInt64SizeNoTag(value);
1089 size_t GPBComputeInt64Size(int32_t fieldNumber, int64_t value) {
1090 return GPBComputeTagSize(fieldNumber) + GPBComputeInt64SizeNoTag(value);
1093 size_t GPBComputeInt32Size(int32_t fieldNumber, int32_t value) {
1094 return GPBComputeTagSize(fieldNumber) + GPBComputeInt32SizeNoTag(value);
1097 size_t GPBComputeFixed64Size(int32_t fieldNumber, uint64_t value) {
1098 return GPBComputeTagSize(fieldNumber) + GPBComputeFixed64SizeNoTag(value);
1101 size_t GPBComputeFixed32Size(int32_t fieldNumber, uint32_t value) {
1102 return GPBComputeTagSize(fieldNumber) + GPBComputeFixed32SizeNoTag(value);
1105 size_t GPBComputeBoolSize(int32_t fieldNumber, BOOL value) {
1106 return GPBComputeTagSize(fieldNumber) + GPBComputeBoolSizeNoTag(value);
1109 size_t GPBComputeStringSize(int32_t fieldNumber, NSString *value) {
1110 return GPBComputeTagSize(fieldNumber) + GPBComputeStringSizeNoTag(value);
1113 size_t GPBComputeGroupSize(int32_t fieldNumber, GPBMessage *value) {
1114 return GPBComputeTagSize(fieldNumber) * 2 + GPBComputeGroupSizeNoTag(value);
1117 size_t GPBComputeUnknownGroupSize(int32_t fieldNumber,
1118 GPBUnknownFieldSet *value) {
1119 return GPBComputeTagSize(fieldNumber) * 2 +
1120 GPBComputeUnknownGroupSizeNoTag(value);
1123 size_t GPBComputeMessageSize(int32_t fieldNumber, GPBMessage *value) {
1124 return GPBComputeTagSize(fieldNumber) + GPBComputeMessageSizeNoTag(value);
1127 size_t GPBComputeBytesSize(int32_t fieldNumber, NSData *value) {
1128 return GPBComputeTagSize(fieldNumber) + GPBComputeBytesSizeNoTag(value);
1131 size_t GPBComputeUInt32Size(int32_t fieldNumber, uint32_t value) {
1132 return GPBComputeTagSize(fieldNumber) + GPBComputeUInt32SizeNoTag(value);
1135 size_t GPBComputeEnumSize(int32_t fieldNumber, int32_t value) {
1136 return GPBComputeTagSize(fieldNumber) + GPBComputeEnumSizeNoTag(value);
1139 size_t GPBComputeSFixed32Size(int32_t fieldNumber, int32_t value) {
1140 return GPBComputeTagSize(fieldNumber) + GPBComputeSFixed32SizeNoTag(value);
1143 size_t GPBComputeSFixed64Size(int32_t fieldNumber, int64_t value) {
1144 return GPBComputeTagSize(fieldNumber) + GPBComputeSFixed64SizeNoTag(value);
1147 size_t GPBComputeSInt32Size(int32_t fieldNumber, int32_t value) {
1148 return GPBComputeTagSize(fieldNumber) + GPBComputeSInt32SizeNoTag(value);
1151 size_t GPBComputeSInt64Size(int32_t fieldNumber, int64_t value) {
1152 return GPBComputeTagSize(fieldNumber) +
1153 GPBComputeRawVarint64Size(GPBEncodeZigZag64(value));
1156 size_t GPBComputeMessageSetExtensionSize(int32_t fieldNumber,
1157 GPBMessage *value) {
1158 return GPBComputeTagSize(GPBWireFormatMessageSetItem) * 2 +
1159 GPBComputeUInt32Size(GPBWireFormatMessageSetTypeId, fieldNumber) +
1160 GPBComputeMessageSize(GPBWireFormatMessageSetMessage, value);
1163 size_t GPBComputeRawMessageSetExtensionSize(int32_t fieldNumber,
1165 return GPBComputeTagSize(GPBWireFormatMessageSetItem) * 2 +
1166 GPBComputeUInt32Size(GPBWireFormatMessageSetTypeId, fieldNumber) +
1167 GPBComputeBytesSize(GPBWireFormatMessageSetMessage, value);
1170 size_t GPBComputeTagSize(int32_t fieldNumber) {
1171 return GPBComputeRawVarint32Size(
1172 GPBWireFormatMakeTag(fieldNumber, GPBWireFormatVarint));
1175 size_t GPBComputeWireFormatTagSize(int field_number, GPBDataType dataType) {
1176 size_t result = GPBComputeTagSize(field_number);
1177 if (dataType == GPBDataTypeGroup) {
1178 // Groups have both a start and an end tag.
1185 size_t GPBComputeRawVarint32Size(int32_t value) {
1186 // value is treated as unsigned, so it won't be sign-extended if negative.
1187 if ((value & (0xffffffff << 7)) == 0) return 1;
1188 if ((value & (0xffffffff << 14)) == 0) return 2;
1189 if ((value & (0xffffffff << 21)) == 0) return 3;
1190 if ((value & (0xffffffff << 28)) == 0) return 4;
1194 size_t GPBComputeRawVarint32SizeForInteger(NSInteger value) {
1195 // Note the truncation.
1196 return GPBComputeRawVarint32Size((int32_t)value);
1199 size_t GPBComputeRawVarint64Size(int64_t value) {
1200 if ((value & (0xffffffffffffffffL << 7)) == 0) return 1;
1201 if ((value & (0xffffffffffffffffL << 14)) == 0) return 2;
1202 if ((value & (0xffffffffffffffffL << 21)) == 0) return 3;
1203 if ((value & (0xffffffffffffffffL << 28)) == 0) return 4;
1204 if ((value & (0xffffffffffffffffL << 35)) == 0) return 5;
1205 if ((value & (0xffffffffffffffffL << 42)) == 0) return 6;
1206 if ((value & (0xffffffffffffffffL << 49)) == 0) return 7;
1207 if ((value & (0xffffffffffffffffL << 56)) == 0) return 8;
1208 if ((value & (0xffffffffffffffffL << 63)) == 0) return 9;